blob: 79f7de82a7087a4616e36dd4d3f9643094640a0a [file] [log] [blame]
Archit Tanejac76b78d2016-02-03 14:29:50 +05301/*
2 * Copyright (c) 2016, The Linux Foundation. All rights reserved.
3 *
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/clk.h>
15#include <linux/slab.h>
16#include <linux/bitops.h>
17#include <linux/dma-mapping.h>
18#include <linux/dmaengine.h>
19#include <linux/module.h>
20#include <linux/mtd/nand.h>
21#include <linux/mtd/partitions.h>
22#include <linux/of.h>
23#include <linux/of_device.h>
Archit Tanejac76b78d2016-02-03 14:29:50 +053024#include <linux/delay.h>
25
26/* NANDc reg offsets */
27#define NAND_FLASH_CMD 0x00
28#define NAND_ADDR0 0x04
29#define NAND_ADDR1 0x08
30#define NAND_FLASH_CHIP_SELECT 0x0c
31#define NAND_EXEC_CMD 0x10
32#define NAND_FLASH_STATUS 0x14
33#define NAND_BUFFER_STATUS 0x18
34#define NAND_DEV0_CFG0 0x20
35#define NAND_DEV0_CFG1 0x24
36#define NAND_DEV0_ECC_CFG 0x28
37#define NAND_DEV1_ECC_CFG 0x2c
38#define NAND_DEV1_CFG0 0x30
39#define NAND_DEV1_CFG1 0x34
40#define NAND_READ_ID 0x40
41#define NAND_READ_STATUS 0x44
42#define NAND_DEV_CMD0 0xa0
43#define NAND_DEV_CMD1 0xa4
44#define NAND_DEV_CMD2 0xa8
45#define NAND_DEV_CMD_VLD 0xac
46#define SFLASHC_BURST_CFG 0xe0
47#define NAND_ERASED_CW_DETECT_CFG 0xe8
48#define NAND_ERASED_CW_DETECT_STATUS 0xec
49#define NAND_EBI2_ECC_BUF_CFG 0xf0
50#define FLASH_BUF_ACC 0x100
51
52#define NAND_CTRL 0xf00
53#define NAND_VERSION 0xf08
54#define NAND_READ_LOCATION_0 0xf20
55#define NAND_READ_LOCATION_1 0xf24
56
57/* dummy register offsets, used by write_reg_dma */
58#define NAND_DEV_CMD1_RESTORE 0xdead
59#define NAND_DEV_CMD_VLD_RESTORE 0xbeef
60
61/* NAND_FLASH_CMD bits */
62#define PAGE_ACC BIT(4)
63#define LAST_PAGE BIT(5)
64
65/* NAND_FLASH_CHIP_SELECT bits */
66#define NAND_DEV_SEL 0
67#define DM_EN BIT(2)
68
69/* NAND_FLASH_STATUS bits */
70#define FS_OP_ERR BIT(4)
71#define FS_READY_BSY_N BIT(5)
72#define FS_MPU_ERR BIT(8)
73#define FS_DEVICE_STS_ERR BIT(16)
74#define FS_DEVICE_WP BIT(23)
75
76/* NAND_BUFFER_STATUS bits */
77#define BS_UNCORRECTABLE_BIT BIT(8)
78#define BS_CORRECTABLE_ERR_MSK 0x1f
79
80/* NAND_DEVn_CFG0 bits */
81#define DISABLE_STATUS_AFTER_WRITE 4
82#define CW_PER_PAGE 6
83#define UD_SIZE_BYTES 9
84#define ECC_PARITY_SIZE_BYTES_RS 19
85#define SPARE_SIZE_BYTES 23
86#define NUM_ADDR_CYCLES 27
87#define STATUS_BFR_READ 30
88#define SET_RD_MODE_AFTER_STATUS 31
89
90/* NAND_DEVn_CFG0 bits */
91#define DEV0_CFG1_ECC_DISABLE 0
92#define WIDE_FLASH 1
93#define NAND_RECOVERY_CYCLES 2
94#define CS_ACTIVE_BSY 5
95#define BAD_BLOCK_BYTE_NUM 6
96#define BAD_BLOCK_IN_SPARE_AREA 16
97#define WR_RD_BSY_GAP 17
98#define ENABLE_BCH_ECC 27
99
100/* NAND_DEV0_ECC_CFG bits */
101#define ECC_CFG_ECC_DISABLE 0
102#define ECC_SW_RESET 1
103#define ECC_MODE 4
104#define ECC_PARITY_SIZE_BYTES_BCH 8
105#define ECC_NUM_DATA_BYTES 16
106#define ECC_FORCE_CLK_OPEN 30
107
108/* NAND_DEV_CMD1 bits */
109#define READ_ADDR 0
110
111/* NAND_DEV_CMD_VLD bits */
Abhishek Sahud8a9b322017-08-11 17:09:16 +0530112#define READ_START_VLD BIT(0)
113#define READ_STOP_VLD BIT(1)
114#define WRITE_START_VLD BIT(2)
115#define ERASE_START_VLD BIT(3)
116#define SEQ_READ_START_VLD BIT(4)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530117
118/* NAND_EBI2_ECC_BUF_CFG bits */
119#define NUM_STEPS 0
120
121/* NAND_ERASED_CW_DETECT_CFG bits */
122#define ERASED_CW_ECC_MASK 1
123#define AUTO_DETECT_RES 0
124#define MASK_ECC (1 << ERASED_CW_ECC_MASK)
125#define RESET_ERASED_DET (1 << AUTO_DETECT_RES)
126#define ACTIVE_ERASED_DET (0 << AUTO_DETECT_RES)
127#define CLR_ERASED_PAGE_DET (RESET_ERASED_DET | MASK_ECC)
128#define SET_ERASED_PAGE_DET (ACTIVE_ERASED_DET | MASK_ECC)
129
130/* NAND_ERASED_CW_DETECT_STATUS bits */
131#define PAGE_ALL_ERASED BIT(7)
132#define CODEWORD_ALL_ERASED BIT(6)
133#define PAGE_ERASED BIT(5)
134#define CODEWORD_ERASED BIT(4)
135#define ERASED_PAGE (PAGE_ALL_ERASED | PAGE_ERASED)
136#define ERASED_CW (CODEWORD_ALL_ERASED | CODEWORD_ERASED)
137
138/* Version Mask */
139#define NAND_VERSION_MAJOR_MASK 0xf0000000
140#define NAND_VERSION_MAJOR_SHIFT 28
141#define NAND_VERSION_MINOR_MASK 0x0fff0000
142#define NAND_VERSION_MINOR_SHIFT 16
143
144/* NAND OP_CMDs */
145#define PAGE_READ 0x2
146#define PAGE_READ_WITH_ECC 0x3
147#define PAGE_READ_WITH_ECC_SPARE 0x4
148#define PROGRAM_PAGE 0x6
149#define PAGE_PROGRAM_WITH_ECC 0x7
150#define PROGRAM_PAGE_SPARE 0x9
151#define BLOCK_ERASE 0xa
152#define FETCH_ID 0xb
153#define RESET_DEVICE 0xd
154
Abhishek Sahud8a9b322017-08-11 17:09:16 +0530155/* Default Value for NAND_DEV_CMD_VLD */
156#define NAND_DEV_CMD_VLD_VAL (READ_START_VLD | WRITE_START_VLD | \
157 ERASE_START_VLD | SEQ_READ_START_VLD)
158
Archit Tanejac76b78d2016-02-03 14:29:50 +0530159/*
160 * the NAND controller performs reads/writes with ECC in 516 byte chunks.
161 * the driver calls the chunks 'step' or 'codeword' interchangeably
162 */
163#define NANDC_STEP_SIZE 512
164
165/*
166 * the largest page size we support is 8K, this will have 16 steps/codewords
167 * of 512 bytes each
168 */
169#define MAX_NUM_STEPS (SZ_8K / NANDC_STEP_SIZE)
170
171/* we read at most 3 registers per codeword scan */
172#define MAX_REG_RD (3 * MAX_NUM_STEPS)
173
174/* ECC modes supported by the controller */
175#define ECC_NONE BIT(0)
176#define ECC_RS_4BIT BIT(1)
177#define ECC_BCH_4BIT BIT(2)
178#define ECC_BCH_8BIT BIT(3)
179
180struct desc_info {
181 struct list_head node;
182
183 enum dma_data_direction dir;
184 struct scatterlist sgl;
185 struct dma_async_tx_descriptor *dma_desc;
186};
187
188/*
189 * holds the current register values that we want to write. acts as a contiguous
190 * chunk of memory which we use to write the controller registers through DMA.
191 */
192struct nandc_regs {
193 __le32 cmd;
194 __le32 addr0;
195 __le32 addr1;
196 __le32 chip_sel;
197 __le32 exec;
198
199 __le32 cfg0;
200 __le32 cfg1;
201 __le32 ecc_bch_cfg;
202
203 __le32 clrflashstatus;
204 __le32 clrreadstatus;
205
206 __le32 cmd1;
207 __le32 vld;
208
209 __le32 orig_cmd1;
210 __le32 orig_vld;
211
212 __le32 ecc_buf_cfg;
213};
214
215/*
216 * NAND controller data struct
217 *
218 * @controller: base controller structure
219 * @host_list: list containing all the chips attached to the
220 * controller
221 * @dev: parent device
222 * @base: MMIO base
223 * @base_dma: physical base address of controller registers
224 * @core_clk: controller clock
225 * @aon_clk: another controller clock
226 *
227 * @chan: dma channel
228 * @cmd_crci: ADM DMA CRCI for command flow control
229 * @data_crci: ADM DMA CRCI for data flow control
230 * @desc_list: DMA descriptor list (list of desc_infos)
231 *
232 * @data_buffer: our local DMA buffer for page read/writes,
233 * used when we can't use the buffer provided
234 * by upper layers directly
235 * @buf_size/count/start: markers for chip->read_buf/write_buf functions
236 * @reg_read_buf: local buffer for reading back registers via DMA
237 * @reg_read_pos: marker for data read in reg_read_buf
238 *
239 * @regs: a contiguous chunk of memory for DMA register
240 * writes. contains the register values to be
241 * written to controller
242 * @cmd1/vld: some fixed controller register values
Abhishek Sahu58f1f222017-08-11 17:09:17 +0530243 * @props: properties of current NAND controller,
Archit Tanejac76b78d2016-02-03 14:29:50 +0530244 * initialized via DT match data
245 */
246struct qcom_nand_controller {
247 struct nand_hw_control controller;
248 struct list_head host_list;
249
250 struct device *dev;
251
252 void __iomem *base;
253 dma_addr_t base_dma;
254
255 struct clk *core_clk;
256 struct clk *aon_clk;
257
258 struct dma_chan *chan;
259 unsigned int cmd_crci;
260 unsigned int data_crci;
261 struct list_head desc_list;
262
263 u8 *data_buffer;
264 int buf_size;
265 int buf_count;
266 int buf_start;
267
268 __le32 *reg_read_buf;
269 int reg_read_pos;
270
271 struct nandc_regs *regs;
272
273 u32 cmd1, vld;
Abhishek Sahu58f1f222017-08-11 17:09:17 +0530274 const struct qcom_nandc_props *props;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530275};
276
277/*
278 * NAND chip structure
279 *
280 * @chip: base NAND chip structure
281 * @node: list node to add itself to host_list in
282 * qcom_nand_controller
283 *
284 * @cs: chip select value for this chip
285 * @cw_size: the number of bytes in a single step/codeword
286 * of a page, consisting of all data, ecc, spare
287 * and reserved bytes
288 * @cw_data: the number of bytes within a codeword protected
289 * by ECC
290 * @use_ecc: request the controller to use ECC for the
291 * upcoming read/write
292 * @bch_enabled: flag to tell whether BCH ECC mode is used
293 * @ecc_bytes_hw: ECC bytes used by controller hardware for this
294 * chip
295 * @status: value to be returned if NAND_CMD_STATUS command
296 * is executed
297 * @last_command: keeps track of last command on this chip. used
298 * for reading correct status
299 *
300 * @cfg0, cfg1, cfg0_raw..: NANDc register configurations needed for
301 * ecc/non-ecc mode for the current nand flash
302 * device
303 */
304struct qcom_nand_host {
305 struct nand_chip chip;
306 struct list_head node;
307
308 int cs;
309 int cw_size;
310 int cw_data;
311 bool use_ecc;
312 bool bch_enabled;
313 int ecc_bytes_hw;
314 int spare_bytes;
315 int bbm_size;
316 u8 status;
317 int last_command;
318
319 u32 cfg0, cfg1;
320 u32 cfg0_raw, cfg1_raw;
321 u32 ecc_buf_cfg;
322 u32 ecc_bch_cfg;
323 u32 clrflashstatus;
324 u32 clrreadstatus;
325};
326
Abhishek Sahu58f1f222017-08-11 17:09:17 +0530327/*
328 * This data type corresponds to the NAND controller properties which varies
329 * among different NAND controllers.
330 * @ecc_modes - ecc mode for NAND
331 */
332struct qcom_nandc_props {
333 u32 ecc_modes;
334};
335
Archit Tanejac76b78d2016-02-03 14:29:50 +0530336static inline struct qcom_nand_host *to_qcom_nand_host(struct nand_chip *chip)
337{
338 return container_of(chip, struct qcom_nand_host, chip);
339}
340
341static inline struct qcom_nand_controller *
342get_qcom_nand_controller(struct nand_chip *chip)
343{
344 return container_of(chip->controller, struct qcom_nand_controller,
345 controller);
346}
347
348static inline u32 nandc_read(struct qcom_nand_controller *nandc, int offset)
349{
350 return ioread32(nandc->base + offset);
351}
352
353static inline void nandc_write(struct qcom_nand_controller *nandc, int offset,
354 u32 val)
355{
356 iowrite32(val, nandc->base + offset);
357}
358
359static __le32 *offset_to_nandc_reg(struct nandc_regs *regs, int offset)
360{
361 switch (offset) {
362 case NAND_FLASH_CMD:
363 return &regs->cmd;
364 case NAND_ADDR0:
365 return &regs->addr0;
366 case NAND_ADDR1:
367 return &regs->addr1;
368 case NAND_FLASH_CHIP_SELECT:
369 return &regs->chip_sel;
370 case NAND_EXEC_CMD:
371 return &regs->exec;
372 case NAND_FLASH_STATUS:
373 return &regs->clrflashstatus;
374 case NAND_DEV0_CFG0:
375 return &regs->cfg0;
376 case NAND_DEV0_CFG1:
377 return &regs->cfg1;
378 case NAND_DEV0_ECC_CFG:
379 return &regs->ecc_bch_cfg;
380 case NAND_READ_STATUS:
381 return &regs->clrreadstatus;
382 case NAND_DEV_CMD1:
383 return &regs->cmd1;
384 case NAND_DEV_CMD1_RESTORE:
385 return &regs->orig_cmd1;
386 case NAND_DEV_CMD_VLD:
387 return &regs->vld;
388 case NAND_DEV_CMD_VLD_RESTORE:
389 return &regs->orig_vld;
390 case NAND_EBI2_ECC_BUF_CFG:
391 return &regs->ecc_buf_cfg;
392 default:
393 return NULL;
394 }
395}
396
397static void nandc_set_reg(struct qcom_nand_controller *nandc, int offset,
398 u32 val)
399{
400 struct nandc_regs *regs = nandc->regs;
401 __le32 *reg;
402
403 reg = offset_to_nandc_reg(regs, offset);
404
405 if (reg)
406 *reg = cpu_to_le32(val);
407}
408
409/* helper to configure address register values */
410static void set_address(struct qcom_nand_host *host, u16 column, int page)
411{
412 struct nand_chip *chip = &host->chip;
413 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
414
415 if (chip->options & NAND_BUSWIDTH_16)
416 column >>= 1;
417
418 nandc_set_reg(nandc, NAND_ADDR0, page << 16 | column);
419 nandc_set_reg(nandc, NAND_ADDR1, page >> 16 & 0xff);
420}
421
422/*
423 * update_rw_regs: set up read/write register values, these will be
424 * written to the NAND controller registers via DMA
425 *
426 * @num_cw: number of steps for the read/write operation
427 * @read: read or write operation
428 */
429static void update_rw_regs(struct qcom_nand_host *host, int num_cw, bool read)
430{
431 struct nand_chip *chip = &host->chip;
432 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
433 u32 cmd, cfg0, cfg1, ecc_bch_cfg;
434
435 if (read) {
436 if (host->use_ecc)
437 cmd = PAGE_READ_WITH_ECC | PAGE_ACC | LAST_PAGE;
438 else
439 cmd = PAGE_READ | PAGE_ACC | LAST_PAGE;
440 } else {
441 cmd = PROGRAM_PAGE | PAGE_ACC | LAST_PAGE;
442 }
443
444 if (host->use_ecc) {
445 cfg0 = (host->cfg0 & ~(7U << CW_PER_PAGE)) |
446 (num_cw - 1) << CW_PER_PAGE;
447
448 cfg1 = host->cfg1;
449 ecc_bch_cfg = host->ecc_bch_cfg;
450 } else {
451 cfg0 = (host->cfg0_raw & ~(7U << CW_PER_PAGE)) |
452 (num_cw - 1) << CW_PER_PAGE;
453
454 cfg1 = host->cfg1_raw;
455 ecc_bch_cfg = 1 << ECC_CFG_ECC_DISABLE;
456 }
457
458 nandc_set_reg(nandc, NAND_FLASH_CMD, cmd);
459 nandc_set_reg(nandc, NAND_DEV0_CFG0, cfg0);
460 nandc_set_reg(nandc, NAND_DEV0_CFG1, cfg1);
461 nandc_set_reg(nandc, NAND_DEV0_ECC_CFG, ecc_bch_cfg);
462 nandc_set_reg(nandc, NAND_EBI2_ECC_BUF_CFG, host->ecc_buf_cfg);
463 nandc_set_reg(nandc, NAND_FLASH_STATUS, host->clrflashstatus);
464 nandc_set_reg(nandc, NAND_READ_STATUS, host->clrreadstatus);
465 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
466}
467
468static int prep_dma_desc(struct qcom_nand_controller *nandc, bool read,
469 int reg_off, const void *vaddr, int size,
470 bool flow_control)
471{
472 struct desc_info *desc;
473 struct dma_async_tx_descriptor *dma_desc;
474 struct scatterlist *sgl;
475 struct dma_slave_config slave_conf;
476 enum dma_transfer_direction dir_eng;
477 int ret;
478
479 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
480 if (!desc)
481 return -ENOMEM;
482
483 sgl = &desc->sgl;
484
485 sg_init_one(sgl, vaddr, size);
486
487 if (read) {
488 dir_eng = DMA_DEV_TO_MEM;
489 desc->dir = DMA_FROM_DEVICE;
490 } else {
491 dir_eng = DMA_MEM_TO_DEV;
492 desc->dir = DMA_TO_DEVICE;
493 }
494
495 ret = dma_map_sg(nandc->dev, sgl, 1, desc->dir);
496 if (ret == 0) {
497 ret = -ENOMEM;
498 goto err;
499 }
500
501 memset(&slave_conf, 0x00, sizeof(slave_conf));
502
503 slave_conf.device_fc = flow_control;
504 if (read) {
505 slave_conf.src_maxburst = 16;
506 slave_conf.src_addr = nandc->base_dma + reg_off;
507 slave_conf.slave_id = nandc->data_crci;
508 } else {
509 slave_conf.dst_maxburst = 16;
510 slave_conf.dst_addr = nandc->base_dma + reg_off;
511 slave_conf.slave_id = nandc->cmd_crci;
512 }
513
514 ret = dmaengine_slave_config(nandc->chan, &slave_conf);
515 if (ret) {
516 dev_err(nandc->dev, "failed to configure dma channel\n");
517 goto err;
518 }
519
520 dma_desc = dmaengine_prep_slave_sg(nandc->chan, sgl, 1, dir_eng, 0);
521 if (!dma_desc) {
522 dev_err(nandc->dev, "failed to prepare desc\n");
523 ret = -EINVAL;
524 goto err;
525 }
526
527 desc->dma_desc = dma_desc;
528
529 list_add_tail(&desc->node, &nandc->desc_list);
530
531 return 0;
532err:
533 kfree(desc);
534
535 return ret;
536}
537
538/*
539 * read_reg_dma: prepares a descriptor to read a given number of
540 * contiguous registers to the reg_read_buf pointer
541 *
542 * @first: offset of the first register in the contiguous block
543 * @num_regs: number of registers to read
544 */
545static int read_reg_dma(struct qcom_nand_controller *nandc, int first,
546 int num_regs)
547{
548 bool flow_control = false;
549 void *vaddr;
550 int size;
551
552 if (first == NAND_READ_ID || first == NAND_FLASH_STATUS)
553 flow_control = true;
554
555 size = num_regs * sizeof(u32);
556 vaddr = nandc->reg_read_buf + nandc->reg_read_pos;
557 nandc->reg_read_pos += num_regs;
558
559 return prep_dma_desc(nandc, true, first, vaddr, size, flow_control);
560}
561
562/*
563 * write_reg_dma: prepares a descriptor to write a given number of
564 * contiguous registers
565 *
566 * @first: offset of the first register in the contiguous block
567 * @num_regs: number of registers to write
568 */
569static int write_reg_dma(struct qcom_nand_controller *nandc, int first,
570 int num_regs)
571{
572 bool flow_control = false;
573 struct nandc_regs *regs = nandc->regs;
574 void *vaddr;
575 int size;
576
577 vaddr = offset_to_nandc_reg(regs, first);
578
579 if (first == NAND_FLASH_CMD)
580 flow_control = true;
581
582 if (first == NAND_DEV_CMD1_RESTORE)
583 first = NAND_DEV_CMD1;
584
585 if (first == NAND_DEV_CMD_VLD_RESTORE)
586 first = NAND_DEV_CMD_VLD;
587
588 size = num_regs * sizeof(u32);
589
590 return prep_dma_desc(nandc, false, first, vaddr, size, flow_control);
591}
592
593/*
594 * read_data_dma: prepares a DMA descriptor to transfer data from the
595 * controller's internal buffer to the buffer 'vaddr'
596 *
597 * @reg_off: offset within the controller's data buffer
598 * @vaddr: virtual address of the buffer we want to write to
599 * @size: DMA transaction size in bytes
600 */
601static int read_data_dma(struct qcom_nand_controller *nandc, int reg_off,
602 const u8 *vaddr, int size)
603{
604 return prep_dma_desc(nandc, true, reg_off, vaddr, size, false);
605}
606
607/*
608 * write_data_dma: prepares a DMA descriptor to transfer data from
609 * 'vaddr' to the controller's internal buffer
610 *
611 * @reg_off: offset within the controller's data buffer
612 * @vaddr: virtual address of the buffer we want to read from
613 * @size: DMA transaction size in bytes
614 */
615static int write_data_dma(struct qcom_nand_controller *nandc, int reg_off,
616 const u8 *vaddr, int size)
617{
618 return prep_dma_desc(nandc, false, reg_off, vaddr, size, false);
619}
620
621/*
Abhishek Sahubde43302017-07-19 17:17:55 +0530622 * Helper to prepare DMA descriptors for configuring registers
623 * before reading a NAND page.
Archit Tanejac76b78d2016-02-03 14:29:50 +0530624 */
Abhishek Sahubde43302017-07-19 17:17:55 +0530625static void config_nand_page_read(struct qcom_nand_controller *nandc)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530626{
Abhishek Sahubde43302017-07-19 17:17:55 +0530627 write_reg_dma(nandc, NAND_ADDR0, 2);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530628 write_reg_dma(nandc, NAND_DEV0_CFG0, 3);
629 write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1);
Abhishek Sahubde43302017-07-19 17:17:55 +0530630}
Archit Tanejac76b78d2016-02-03 14:29:50 +0530631
Abhishek Sahubde43302017-07-19 17:17:55 +0530632/*
633 * Helper to prepare DMA descriptors for configuring registers
634 * before reading each codeword in NAND page.
635 */
636static void config_nand_cw_read(struct qcom_nand_controller *nandc)
637{
638 write_reg_dma(nandc, NAND_FLASH_CMD, 1);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530639 write_reg_dma(nandc, NAND_EXEC_CMD, 1);
640
641 read_reg_dma(nandc, NAND_FLASH_STATUS, 2);
642 read_reg_dma(nandc, NAND_ERASED_CW_DETECT_STATUS, 1);
643}
644
645/*
Abhishek Sahubde43302017-07-19 17:17:55 +0530646 * Helper to prepare dma descriptors to configure registers needed for reading a
647 * single codeword in page
Archit Tanejac76b78d2016-02-03 14:29:50 +0530648 */
Abhishek Sahubde43302017-07-19 17:17:55 +0530649static void config_nand_single_cw_page_read(struct qcom_nand_controller *nandc)
650{
651 config_nand_page_read(nandc);
652 config_nand_cw_read(nandc);
653}
654
Abhishek Sahu77cc5362017-07-19 17:17:56 +0530655/*
656 * Helper to prepare DMA descriptors used to configure registers needed for
657 * before writing a NAND page.
658 */
659static void config_nand_page_write(struct qcom_nand_controller *nandc)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530660{
Abhishek Sahu77cc5362017-07-19 17:17:56 +0530661 write_reg_dma(nandc, NAND_ADDR0, 2);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530662 write_reg_dma(nandc, NAND_DEV0_CFG0, 3);
663 write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1);
664}
665
Abhishek Sahu77cc5362017-07-19 17:17:56 +0530666/*
667 * Helper to prepare DMA descriptors for configuring registers
668 * before writing each codeword in NAND page.
669 */
670static void config_nand_cw_write(struct qcom_nand_controller *nandc)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530671{
Abhishek Sahu77cc5362017-07-19 17:17:56 +0530672 write_reg_dma(nandc, NAND_FLASH_CMD, 1);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530673 write_reg_dma(nandc, NAND_EXEC_CMD, 1);
674
675 read_reg_dma(nandc, NAND_FLASH_STATUS, 1);
676
677 write_reg_dma(nandc, NAND_FLASH_STATUS, 1);
678 write_reg_dma(nandc, NAND_READ_STATUS, 1);
679}
680
681/*
682 * the following functions are used within chip->cmdfunc() to perform different
683 * NAND_CMD_* commands
684 */
685
686/* sets up descriptors for NAND_CMD_PARAM */
687static int nandc_param(struct qcom_nand_host *host)
688{
689 struct nand_chip *chip = &host->chip;
690 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
691
692 /*
693 * NAND_CMD_PARAM is called before we know much about the FLASH chip
694 * in use. we configure the controller to perform a raw read of 512
695 * bytes to read onfi params
696 */
697 nandc_set_reg(nandc, NAND_FLASH_CMD, PAGE_READ | PAGE_ACC | LAST_PAGE);
698 nandc_set_reg(nandc, NAND_ADDR0, 0);
699 nandc_set_reg(nandc, NAND_ADDR1, 0);
700 nandc_set_reg(nandc, NAND_DEV0_CFG0, 0 << CW_PER_PAGE
701 | 512 << UD_SIZE_BYTES
702 | 5 << NUM_ADDR_CYCLES
703 | 0 << SPARE_SIZE_BYTES);
704 nandc_set_reg(nandc, NAND_DEV0_CFG1, 7 << NAND_RECOVERY_CYCLES
705 | 0 << CS_ACTIVE_BSY
706 | 17 << BAD_BLOCK_BYTE_NUM
707 | 1 << BAD_BLOCK_IN_SPARE_AREA
708 | 2 << WR_RD_BSY_GAP
709 | 0 << WIDE_FLASH
710 | 1 << DEV0_CFG1_ECC_DISABLE);
711 nandc_set_reg(nandc, NAND_EBI2_ECC_BUF_CFG, 1 << ECC_CFG_ECC_DISABLE);
712
713 /* configure CMD1 and VLD for ONFI param probing */
714 nandc_set_reg(nandc, NAND_DEV_CMD_VLD,
Abhishek Sahud8a9b322017-08-11 17:09:16 +0530715 (nandc->vld & ~READ_START_VLD));
Archit Tanejac76b78d2016-02-03 14:29:50 +0530716 nandc_set_reg(nandc, NAND_DEV_CMD1,
717 (nandc->cmd1 & ~(0xFF << READ_ADDR))
718 | NAND_CMD_PARAM << READ_ADDR);
719
720 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
721
722 nandc_set_reg(nandc, NAND_DEV_CMD1_RESTORE, nandc->cmd1);
723 nandc_set_reg(nandc, NAND_DEV_CMD_VLD_RESTORE, nandc->vld);
724
725 write_reg_dma(nandc, NAND_DEV_CMD_VLD, 1);
726 write_reg_dma(nandc, NAND_DEV_CMD1, 1);
727
728 nandc->buf_count = 512;
729 memset(nandc->data_buffer, 0xff, nandc->buf_count);
730
Abhishek Sahubde43302017-07-19 17:17:55 +0530731 config_nand_single_cw_page_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530732
733 read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer,
734 nandc->buf_count);
735
736 /* restore CMD1 and VLD regs */
737 write_reg_dma(nandc, NAND_DEV_CMD1_RESTORE, 1);
738 write_reg_dma(nandc, NAND_DEV_CMD_VLD_RESTORE, 1);
739
740 return 0;
741}
742
743/* sets up descriptors for NAND_CMD_ERASE1 */
744static int erase_block(struct qcom_nand_host *host, int page_addr)
745{
746 struct nand_chip *chip = &host->chip;
747 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
748
749 nandc_set_reg(nandc, NAND_FLASH_CMD,
750 BLOCK_ERASE | PAGE_ACC | LAST_PAGE);
751 nandc_set_reg(nandc, NAND_ADDR0, page_addr);
752 nandc_set_reg(nandc, NAND_ADDR1, 0);
753 nandc_set_reg(nandc, NAND_DEV0_CFG0,
754 host->cfg0_raw & ~(7 << CW_PER_PAGE));
755 nandc_set_reg(nandc, NAND_DEV0_CFG1, host->cfg1_raw);
756 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
757 nandc_set_reg(nandc, NAND_FLASH_STATUS, host->clrflashstatus);
758 nandc_set_reg(nandc, NAND_READ_STATUS, host->clrreadstatus);
759
760 write_reg_dma(nandc, NAND_FLASH_CMD, 3);
761 write_reg_dma(nandc, NAND_DEV0_CFG0, 2);
762 write_reg_dma(nandc, NAND_EXEC_CMD, 1);
763
764 read_reg_dma(nandc, NAND_FLASH_STATUS, 1);
765
766 write_reg_dma(nandc, NAND_FLASH_STATUS, 1);
767 write_reg_dma(nandc, NAND_READ_STATUS, 1);
768
769 return 0;
770}
771
772/* sets up descriptors for NAND_CMD_READID */
773static int read_id(struct qcom_nand_host *host, int column)
774{
775 struct nand_chip *chip = &host->chip;
776 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
777
778 if (column == -1)
779 return 0;
780
781 nandc_set_reg(nandc, NAND_FLASH_CMD, FETCH_ID);
782 nandc_set_reg(nandc, NAND_ADDR0, column);
783 nandc_set_reg(nandc, NAND_ADDR1, 0);
784 nandc_set_reg(nandc, NAND_FLASH_CHIP_SELECT, DM_EN);
785 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
786
787 write_reg_dma(nandc, NAND_FLASH_CMD, 4);
788 write_reg_dma(nandc, NAND_EXEC_CMD, 1);
789
790 read_reg_dma(nandc, NAND_READ_ID, 1);
791
792 return 0;
793}
794
795/* sets up descriptors for NAND_CMD_RESET */
796static int reset(struct qcom_nand_host *host)
797{
798 struct nand_chip *chip = &host->chip;
799 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
800
801 nandc_set_reg(nandc, NAND_FLASH_CMD, RESET_DEVICE);
802 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
803
804 write_reg_dma(nandc, NAND_FLASH_CMD, 1);
805 write_reg_dma(nandc, NAND_EXEC_CMD, 1);
806
807 read_reg_dma(nandc, NAND_FLASH_STATUS, 1);
808
809 return 0;
810}
811
812/* helpers to submit/free our list of dma descriptors */
813static int submit_descs(struct qcom_nand_controller *nandc)
814{
815 struct desc_info *desc;
816 dma_cookie_t cookie = 0;
817
818 list_for_each_entry(desc, &nandc->desc_list, node)
819 cookie = dmaengine_submit(desc->dma_desc);
820
821 if (dma_sync_wait(nandc->chan, cookie) != DMA_COMPLETE)
822 return -ETIMEDOUT;
823
824 return 0;
825}
826
827static void free_descs(struct qcom_nand_controller *nandc)
828{
829 struct desc_info *desc, *n;
830
831 list_for_each_entry_safe(desc, n, &nandc->desc_list, node) {
832 list_del(&desc->node);
833 dma_unmap_sg(nandc->dev, &desc->sgl, 1, desc->dir);
834 kfree(desc);
835 }
836}
837
838/* reset the register read buffer for next NAND operation */
839static void clear_read_regs(struct qcom_nand_controller *nandc)
840{
841 nandc->reg_read_pos = 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530842}
843
844static void pre_command(struct qcom_nand_host *host, int command)
845{
846 struct nand_chip *chip = &host->chip;
847 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
848
849 nandc->buf_count = 0;
850 nandc->buf_start = 0;
851 host->use_ecc = false;
852 host->last_command = command;
853
854 clear_read_regs(nandc);
855}
856
857/*
858 * this is called after NAND_CMD_PAGEPROG and NAND_CMD_ERASE1 to set our
859 * privately maintained status byte, this status byte can be read after
860 * NAND_CMD_STATUS is called
861 */
862static void parse_erase_write_errors(struct qcom_nand_host *host, int command)
863{
864 struct nand_chip *chip = &host->chip;
865 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
866 struct nand_ecc_ctrl *ecc = &chip->ecc;
867 int num_cw;
868 int i;
869
870 num_cw = command == NAND_CMD_PAGEPROG ? ecc->steps : 1;
871
872 for (i = 0; i < num_cw; i++) {
873 u32 flash_status = le32_to_cpu(nandc->reg_read_buf[i]);
874
875 if (flash_status & FS_MPU_ERR)
876 host->status &= ~NAND_STATUS_WP;
877
878 if (flash_status & FS_OP_ERR || (i == (num_cw - 1) &&
879 (flash_status &
880 FS_DEVICE_STS_ERR)))
881 host->status |= NAND_STATUS_FAIL;
882 }
883}
884
885static void post_command(struct qcom_nand_host *host, int command)
886{
887 struct nand_chip *chip = &host->chip;
888 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
889
890 switch (command) {
891 case NAND_CMD_READID:
892 memcpy(nandc->data_buffer, nandc->reg_read_buf,
893 nandc->buf_count);
894 break;
895 case NAND_CMD_PAGEPROG:
896 case NAND_CMD_ERASE1:
897 parse_erase_write_errors(host, command);
898 break;
899 default:
900 break;
901 }
902}
903
904/*
905 * Implements chip->cmdfunc. It's only used for a limited set of commands.
906 * The rest of the commands wouldn't be called by upper layers. For example,
907 * NAND_CMD_READOOB would never be called because we have our own versions
908 * of read_oob ops for nand_ecc_ctrl.
909 */
910static void qcom_nandc_command(struct mtd_info *mtd, unsigned int command,
911 int column, int page_addr)
912{
913 struct nand_chip *chip = mtd_to_nand(mtd);
914 struct qcom_nand_host *host = to_qcom_nand_host(chip);
915 struct nand_ecc_ctrl *ecc = &chip->ecc;
916 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
917 bool wait = false;
918 int ret = 0;
919
920 pre_command(host, command);
921
922 switch (command) {
923 case NAND_CMD_RESET:
924 ret = reset(host);
925 wait = true;
926 break;
927
928 case NAND_CMD_READID:
929 nandc->buf_count = 4;
930 ret = read_id(host, column);
931 wait = true;
932 break;
933
934 case NAND_CMD_PARAM:
935 ret = nandc_param(host);
936 wait = true;
937 break;
938
939 case NAND_CMD_ERASE1:
940 ret = erase_block(host, page_addr);
941 wait = true;
942 break;
943
944 case NAND_CMD_READ0:
945 /* we read the entire page for now */
946 WARN_ON(column != 0);
947
948 host->use_ecc = true;
949 set_address(host, 0, page_addr);
950 update_rw_regs(host, ecc->steps, true);
951 break;
952
953 case NAND_CMD_SEQIN:
954 WARN_ON(column != 0);
955 set_address(host, 0, page_addr);
956 break;
957
958 case NAND_CMD_PAGEPROG:
959 case NAND_CMD_STATUS:
960 case NAND_CMD_NONE:
961 default:
962 break;
963 }
964
965 if (ret) {
966 dev_err(nandc->dev, "failure executing command %d\n",
967 command);
968 free_descs(nandc);
969 return;
970 }
971
972 if (wait) {
973 ret = submit_descs(nandc);
974 if (ret)
975 dev_err(nandc->dev,
976 "failure submitting descs for command %d\n",
977 command);
978 }
979
980 free_descs(nandc);
981
982 post_command(host, command);
983}
984
985/*
986 * when using BCH ECC, the HW flags an error in NAND_FLASH_STATUS if it read
987 * an erased CW, and reports an erased CW in NAND_ERASED_CW_DETECT_STATUS.
988 *
989 * when using RS ECC, the HW reports the same erros when reading an erased CW,
990 * but it notifies that it is an erased CW by placing special characters at
991 * certain offsets in the buffer.
992 *
993 * verify if the page is erased or not, and fix up the page for RS ECC by
994 * replacing the special characters with 0xff.
995 */
996static bool erased_chunk_check_and_fixup(u8 *data_buf, int data_len)
997{
998 u8 empty1, empty2;
999
1000 /*
1001 * an erased page flags an error in NAND_FLASH_STATUS, check if the page
1002 * is erased by looking for 0x54s at offsets 3 and 175 from the
1003 * beginning of each codeword
1004 */
1005
1006 empty1 = data_buf[3];
1007 empty2 = data_buf[175];
1008
1009 /*
1010 * if the erased codework markers, if they exist override them with
1011 * 0xffs
1012 */
1013 if ((empty1 == 0x54 && empty2 == 0xff) ||
1014 (empty1 == 0xff && empty2 == 0x54)) {
1015 data_buf[3] = 0xff;
1016 data_buf[175] = 0xff;
1017 }
1018
1019 /*
1020 * check if the entire chunk contains 0xffs or not. if it doesn't, then
1021 * restore the original values at the special offsets
1022 */
1023 if (memchr_inv(data_buf, 0xff, data_len)) {
1024 data_buf[3] = empty1;
1025 data_buf[175] = empty2;
1026
1027 return false;
1028 }
1029
1030 return true;
1031}
1032
1033struct read_stats {
1034 __le32 flash;
1035 __le32 buffer;
1036 __le32 erased_cw;
1037};
1038
1039/*
1040 * reads back status registers set by the controller to notify page read
1041 * errors. this is equivalent to what 'ecc->correct()' would do.
1042 */
1043static int parse_read_errors(struct qcom_nand_host *host, u8 *data_buf,
1044 u8 *oob_buf)
1045{
1046 struct nand_chip *chip = &host->chip;
1047 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1048 struct mtd_info *mtd = nand_to_mtd(chip);
1049 struct nand_ecc_ctrl *ecc = &chip->ecc;
1050 unsigned int max_bitflips = 0;
1051 struct read_stats *buf;
1052 int i;
1053
1054 buf = (struct read_stats *)nandc->reg_read_buf;
1055
1056 for (i = 0; i < ecc->steps; i++, buf++) {
1057 u32 flash, buffer, erased_cw;
1058 int data_len, oob_len;
1059
1060 if (i == (ecc->steps - 1)) {
1061 data_len = ecc->size - ((ecc->steps - 1) << 2);
1062 oob_len = ecc->steps << 2;
1063 } else {
1064 data_len = host->cw_data;
1065 oob_len = 0;
1066 }
1067
1068 flash = le32_to_cpu(buf->flash);
1069 buffer = le32_to_cpu(buf->buffer);
1070 erased_cw = le32_to_cpu(buf->erased_cw);
1071
1072 if (flash & (FS_OP_ERR | FS_MPU_ERR)) {
1073 bool erased;
1074
1075 /* ignore erased codeword errors */
1076 if (host->bch_enabled) {
1077 erased = (erased_cw & ERASED_CW) == ERASED_CW ?
1078 true : false;
1079 } else {
1080 erased = erased_chunk_check_and_fixup(data_buf,
1081 data_len);
1082 }
1083
1084 if (erased) {
1085 data_buf += data_len;
1086 if (oob_buf)
1087 oob_buf += oob_len + ecc->bytes;
1088 continue;
1089 }
1090
1091 if (buffer & BS_UNCORRECTABLE_BIT) {
1092 int ret, ecclen, extraooblen;
1093 void *eccbuf;
1094
1095 eccbuf = oob_buf ? oob_buf + oob_len : NULL;
1096 ecclen = oob_buf ? host->ecc_bytes_hw : 0;
1097 extraooblen = oob_buf ? oob_len : 0;
1098
1099 /*
1100 * make sure it isn't an erased page reported
1101 * as not-erased by HW because of a few bitflips
1102 */
1103 ret = nand_check_erased_ecc_chunk(data_buf,
1104 data_len, eccbuf, ecclen, oob_buf,
1105 extraooblen, ecc->strength);
1106 if (ret < 0) {
1107 mtd->ecc_stats.failed++;
1108 } else {
1109 mtd->ecc_stats.corrected += ret;
1110 max_bitflips =
1111 max_t(unsigned int, max_bitflips, ret);
1112 }
1113 }
1114 } else {
1115 unsigned int stat;
1116
1117 stat = buffer & BS_CORRECTABLE_ERR_MSK;
1118 mtd->ecc_stats.corrected += stat;
1119 max_bitflips = max(max_bitflips, stat);
1120 }
1121
1122 data_buf += data_len;
1123 if (oob_buf)
1124 oob_buf += oob_len + ecc->bytes;
1125 }
1126
1127 return max_bitflips;
1128}
1129
1130/*
1131 * helper to perform the actual page read operation, used by ecc->read_page(),
1132 * ecc->read_oob()
1133 */
1134static int read_page_ecc(struct qcom_nand_host *host, u8 *data_buf,
1135 u8 *oob_buf)
1136{
1137 struct nand_chip *chip = &host->chip;
1138 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1139 struct nand_ecc_ctrl *ecc = &chip->ecc;
1140 int i, ret;
1141
Abhishek Sahubde43302017-07-19 17:17:55 +05301142 config_nand_page_read(nandc);
1143
Archit Tanejac76b78d2016-02-03 14:29:50 +05301144 /* queue cmd descs for each codeword */
1145 for (i = 0; i < ecc->steps; i++) {
1146 int data_size, oob_size;
1147
1148 if (i == (ecc->steps - 1)) {
1149 data_size = ecc->size - ((ecc->steps - 1) << 2);
1150 oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
1151 host->spare_bytes;
1152 } else {
1153 data_size = host->cw_data;
1154 oob_size = host->ecc_bytes_hw + host->spare_bytes;
1155 }
1156
Abhishek Sahubde43302017-07-19 17:17:55 +05301157 config_nand_cw_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301158
1159 if (data_buf)
1160 read_data_dma(nandc, FLASH_BUF_ACC, data_buf,
1161 data_size);
1162
1163 /*
1164 * when ecc is enabled, the controller doesn't read the real
1165 * or dummy bad block markers in each chunk. To maintain a
1166 * consistent layout across RAW and ECC reads, we just
1167 * leave the real/dummy BBM offsets empty (i.e, filled with
1168 * 0xffs)
1169 */
1170 if (oob_buf) {
1171 int j;
1172
1173 for (j = 0; j < host->bbm_size; j++)
1174 *oob_buf++ = 0xff;
1175
1176 read_data_dma(nandc, FLASH_BUF_ACC + data_size,
1177 oob_buf, oob_size);
1178 }
1179
1180 if (data_buf)
1181 data_buf += data_size;
1182 if (oob_buf)
1183 oob_buf += oob_size;
1184 }
1185
1186 ret = submit_descs(nandc);
1187 if (ret)
1188 dev_err(nandc->dev, "failure to read page/oob\n");
1189
1190 free_descs(nandc);
1191
1192 return ret;
1193}
1194
1195/*
1196 * a helper that copies the last step/codeword of a page (containing free oob)
1197 * into our local buffer
1198 */
1199static int copy_last_cw(struct qcom_nand_host *host, int page)
1200{
1201 struct nand_chip *chip = &host->chip;
1202 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1203 struct nand_ecc_ctrl *ecc = &chip->ecc;
1204 int size;
1205 int ret;
1206
1207 clear_read_regs(nandc);
1208
1209 size = host->use_ecc ? host->cw_data : host->cw_size;
1210
1211 /* prepare a clean read buffer */
1212 memset(nandc->data_buffer, 0xff, size);
1213
1214 set_address(host, host->cw_size * (ecc->steps - 1), page);
1215 update_rw_regs(host, 1, true);
1216
Abhishek Sahubde43302017-07-19 17:17:55 +05301217 config_nand_single_cw_page_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301218
1219 read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer, size);
1220
1221 ret = submit_descs(nandc);
1222 if (ret)
1223 dev_err(nandc->dev, "failed to copy last codeword\n");
1224
1225 free_descs(nandc);
1226
1227 return ret;
1228}
1229
1230/* implements ecc->read_page() */
1231static int qcom_nandc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1232 uint8_t *buf, int oob_required, int page)
1233{
1234 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1235 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1236 u8 *data_buf, *oob_buf = NULL;
1237 int ret;
1238
1239 data_buf = buf;
1240 oob_buf = oob_required ? chip->oob_poi : NULL;
1241
1242 ret = read_page_ecc(host, data_buf, oob_buf);
1243 if (ret) {
1244 dev_err(nandc->dev, "failure to read page\n");
1245 return ret;
1246 }
1247
1248 return parse_read_errors(host, data_buf, oob_buf);
1249}
1250
1251/* implements ecc->read_page_raw() */
1252static int qcom_nandc_read_page_raw(struct mtd_info *mtd,
1253 struct nand_chip *chip, uint8_t *buf,
1254 int oob_required, int page)
1255{
1256 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1257 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1258 u8 *data_buf, *oob_buf;
1259 struct nand_ecc_ctrl *ecc = &chip->ecc;
1260 int i, ret;
1261
1262 data_buf = buf;
1263 oob_buf = chip->oob_poi;
1264
1265 host->use_ecc = false;
1266 update_rw_regs(host, ecc->steps, true);
Abhishek Sahubde43302017-07-19 17:17:55 +05301267 config_nand_page_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301268
1269 for (i = 0; i < ecc->steps; i++) {
1270 int data_size1, data_size2, oob_size1, oob_size2;
1271 int reg_off = FLASH_BUF_ACC;
1272
1273 data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
1274 oob_size1 = host->bbm_size;
1275
1276 if (i == (ecc->steps - 1)) {
1277 data_size2 = ecc->size - data_size1 -
1278 ((ecc->steps - 1) << 2);
1279 oob_size2 = (ecc->steps << 2) + host->ecc_bytes_hw +
1280 host->spare_bytes;
1281 } else {
1282 data_size2 = host->cw_data - data_size1;
1283 oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
1284 }
1285
Abhishek Sahubde43302017-07-19 17:17:55 +05301286 config_nand_cw_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301287
1288 read_data_dma(nandc, reg_off, data_buf, data_size1);
1289 reg_off += data_size1;
1290 data_buf += data_size1;
1291
1292 read_data_dma(nandc, reg_off, oob_buf, oob_size1);
1293 reg_off += oob_size1;
1294 oob_buf += oob_size1;
1295
1296 read_data_dma(nandc, reg_off, data_buf, data_size2);
1297 reg_off += data_size2;
1298 data_buf += data_size2;
1299
1300 read_data_dma(nandc, reg_off, oob_buf, oob_size2);
1301 oob_buf += oob_size2;
1302 }
1303
1304 ret = submit_descs(nandc);
1305 if (ret)
1306 dev_err(nandc->dev, "failure to read raw page\n");
1307
1308 free_descs(nandc);
1309
1310 return 0;
1311}
1312
1313/* implements ecc->read_oob() */
1314static int qcom_nandc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
1315 int page)
1316{
1317 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1318 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1319 struct nand_ecc_ctrl *ecc = &chip->ecc;
1320 int ret;
1321
1322 clear_read_regs(nandc);
1323
1324 host->use_ecc = true;
1325 set_address(host, 0, page);
1326 update_rw_regs(host, ecc->steps, true);
1327
1328 ret = read_page_ecc(host, NULL, chip->oob_poi);
1329 if (ret)
1330 dev_err(nandc->dev, "failure to read oob\n");
1331
1332 return ret;
1333}
1334
1335/* implements ecc->write_page() */
1336static int qcom_nandc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1337 const uint8_t *buf, int oob_required, int page)
1338{
1339 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1340 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1341 struct nand_ecc_ctrl *ecc = &chip->ecc;
1342 u8 *data_buf, *oob_buf;
1343 int i, ret;
1344
1345 clear_read_regs(nandc);
1346
1347 data_buf = (u8 *)buf;
1348 oob_buf = chip->oob_poi;
1349
1350 host->use_ecc = true;
1351 update_rw_regs(host, ecc->steps, false);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301352 config_nand_page_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301353
1354 for (i = 0; i < ecc->steps; i++) {
1355 int data_size, oob_size;
1356
1357 if (i == (ecc->steps - 1)) {
1358 data_size = ecc->size - ((ecc->steps - 1) << 2);
1359 oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
1360 host->spare_bytes;
1361 } else {
1362 data_size = host->cw_data;
1363 oob_size = ecc->bytes;
1364 }
1365
Archit Tanejac76b78d2016-02-03 14:29:50 +05301366
1367 write_data_dma(nandc, FLASH_BUF_ACC, data_buf, data_size);
1368
1369 /*
1370 * when ECC is enabled, we don't really need to write anything
1371 * to oob for the first n - 1 codewords since these oob regions
1372 * just contain ECC bytes that's written by the controller
1373 * itself. For the last codeword, we skip the bbm positions and
1374 * write to the free oob area.
1375 */
1376 if (i == (ecc->steps - 1)) {
1377 oob_buf += host->bbm_size;
1378
1379 write_data_dma(nandc, FLASH_BUF_ACC + data_size,
1380 oob_buf, oob_size);
1381 }
1382
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301383 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301384
1385 data_buf += data_size;
1386 oob_buf += oob_size;
1387 }
1388
1389 ret = submit_descs(nandc);
1390 if (ret)
1391 dev_err(nandc->dev, "failure to write page\n");
1392
1393 free_descs(nandc);
1394
1395 return ret;
1396}
1397
1398/* implements ecc->write_page_raw() */
1399static int qcom_nandc_write_page_raw(struct mtd_info *mtd,
1400 struct nand_chip *chip, const uint8_t *buf,
1401 int oob_required, int page)
1402{
1403 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1404 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1405 struct nand_ecc_ctrl *ecc = &chip->ecc;
1406 u8 *data_buf, *oob_buf;
1407 int i, ret;
1408
1409 clear_read_regs(nandc);
1410
1411 data_buf = (u8 *)buf;
1412 oob_buf = chip->oob_poi;
1413
1414 host->use_ecc = false;
1415 update_rw_regs(host, ecc->steps, false);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301416 config_nand_page_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301417
1418 for (i = 0; i < ecc->steps; i++) {
1419 int data_size1, data_size2, oob_size1, oob_size2;
1420 int reg_off = FLASH_BUF_ACC;
1421
1422 data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
1423 oob_size1 = host->bbm_size;
1424
1425 if (i == (ecc->steps - 1)) {
1426 data_size2 = ecc->size - data_size1 -
1427 ((ecc->steps - 1) << 2);
1428 oob_size2 = (ecc->steps << 2) + host->ecc_bytes_hw +
1429 host->spare_bytes;
1430 } else {
1431 data_size2 = host->cw_data - data_size1;
1432 oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
1433 }
1434
Archit Tanejac76b78d2016-02-03 14:29:50 +05301435 write_data_dma(nandc, reg_off, data_buf, data_size1);
1436 reg_off += data_size1;
1437 data_buf += data_size1;
1438
1439 write_data_dma(nandc, reg_off, oob_buf, oob_size1);
1440 reg_off += oob_size1;
1441 oob_buf += oob_size1;
1442
1443 write_data_dma(nandc, reg_off, data_buf, data_size2);
1444 reg_off += data_size2;
1445 data_buf += data_size2;
1446
1447 write_data_dma(nandc, reg_off, oob_buf, oob_size2);
1448 oob_buf += oob_size2;
1449
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301450 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301451 }
1452
1453 ret = submit_descs(nandc);
1454 if (ret)
1455 dev_err(nandc->dev, "failure to write raw page\n");
1456
1457 free_descs(nandc);
1458
1459 return ret;
1460}
1461
1462/*
1463 * implements ecc->write_oob()
1464 *
1465 * the NAND controller cannot write only data or only oob within a codeword,
1466 * since ecc is calculated for the combined codeword. we first copy the
1467 * entire contents for the last codeword(data + oob), replace the old oob
1468 * with the new one in chip->oob_poi, and then write the entire codeword.
1469 * this read-copy-write operation results in a slight performance loss.
1470 */
1471static int qcom_nandc_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
1472 int page)
1473{
1474 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1475 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1476 struct nand_ecc_ctrl *ecc = &chip->ecc;
1477 u8 *oob = chip->oob_poi;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301478 int data_size, oob_size;
1479 int ret, status = 0;
1480
1481 host->use_ecc = true;
1482
1483 ret = copy_last_cw(host, page);
1484 if (ret)
1485 return ret;
1486
1487 clear_read_regs(nandc);
1488
1489 /* calculate the data and oob size for the last codeword/step */
1490 data_size = ecc->size - ((ecc->steps - 1) << 2);
Boris Brezillonaa02fcf2016-03-18 17:53:31 +01001491 oob_size = mtd->oobavail;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301492
1493 /* override new oob content to last codeword */
Boris Brezillonaa02fcf2016-03-18 17:53:31 +01001494 mtd_ooblayout_get_databytes(mtd, nandc->data_buffer + data_size, oob,
1495 0, mtd->oobavail);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301496
1497 set_address(host, host->cw_size * (ecc->steps - 1), page);
1498 update_rw_regs(host, 1, false);
1499
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301500 config_nand_page_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301501 write_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer,
1502 data_size + oob_size);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301503 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301504
1505 ret = submit_descs(nandc);
1506
1507 free_descs(nandc);
1508
1509 if (ret) {
1510 dev_err(nandc->dev, "failure to write oob\n");
1511 return -EIO;
1512 }
1513
1514 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1515
1516 status = chip->waitfunc(mtd, chip);
1517
1518 return status & NAND_STATUS_FAIL ? -EIO : 0;
1519}
1520
1521static int qcom_nandc_block_bad(struct mtd_info *mtd, loff_t ofs)
1522{
1523 struct nand_chip *chip = mtd_to_nand(mtd);
1524 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1525 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1526 struct nand_ecc_ctrl *ecc = &chip->ecc;
1527 int page, ret, bbpos, bad = 0;
1528 u32 flash_status;
1529
1530 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
1531
1532 /*
1533 * configure registers for a raw sub page read, the address is set to
1534 * the beginning of the last codeword, we don't care about reading ecc
1535 * portion of oob. we just want the first few bytes from this codeword
1536 * that contains the BBM
1537 */
1538 host->use_ecc = false;
1539
1540 ret = copy_last_cw(host, page);
1541 if (ret)
1542 goto err;
1543
1544 flash_status = le32_to_cpu(nandc->reg_read_buf[0]);
1545
1546 if (flash_status & (FS_OP_ERR | FS_MPU_ERR)) {
1547 dev_warn(nandc->dev, "error when trying to read BBM\n");
1548 goto err;
1549 }
1550
1551 bbpos = mtd->writesize - host->cw_size * (ecc->steps - 1);
1552
1553 bad = nandc->data_buffer[bbpos] != 0xff;
1554
1555 if (chip->options & NAND_BUSWIDTH_16)
1556 bad = bad || (nandc->data_buffer[bbpos + 1] != 0xff);
1557err:
1558 return bad;
1559}
1560
1561static int qcom_nandc_block_markbad(struct mtd_info *mtd, loff_t ofs)
1562{
1563 struct nand_chip *chip = mtd_to_nand(mtd);
1564 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1565 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1566 struct nand_ecc_ctrl *ecc = &chip->ecc;
1567 int page, ret, status = 0;
1568
1569 clear_read_regs(nandc);
1570
1571 /*
1572 * to mark the BBM as bad, we flash the entire last codeword with 0s.
1573 * we don't care about the rest of the content in the codeword since
1574 * we aren't going to use this block again
1575 */
1576 memset(nandc->data_buffer, 0x00, host->cw_size);
1577
1578 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
1579
1580 /* prepare write */
1581 host->use_ecc = false;
1582 set_address(host, host->cw_size * (ecc->steps - 1), page);
1583 update_rw_regs(host, 1, false);
1584
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301585 config_nand_page_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301586 write_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer, host->cw_size);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301587 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301588
1589 ret = submit_descs(nandc);
1590
1591 free_descs(nandc);
1592
1593 if (ret) {
1594 dev_err(nandc->dev, "failure to update BBM\n");
1595 return -EIO;
1596 }
1597
1598 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1599
1600 status = chip->waitfunc(mtd, chip);
1601
1602 return status & NAND_STATUS_FAIL ? -EIO : 0;
1603}
1604
1605/*
1606 * the three functions below implement chip->read_byte(), chip->read_buf()
1607 * and chip->write_buf() respectively. these aren't used for
1608 * reading/writing page data, they are used for smaller data like reading
1609 * id, status etc
1610 */
1611static uint8_t qcom_nandc_read_byte(struct mtd_info *mtd)
1612{
1613 struct nand_chip *chip = mtd_to_nand(mtd);
1614 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1615 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1616 u8 *buf = nandc->data_buffer;
1617 u8 ret = 0x0;
1618
1619 if (host->last_command == NAND_CMD_STATUS) {
1620 ret = host->status;
1621
1622 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
1623
1624 return ret;
1625 }
1626
1627 if (nandc->buf_start < nandc->buf_count)
1628 ret = buf[nandc->buf_start++];
1629
1630 return ret;
1631}
1632
1633static void qcom_nandc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
1634{
1635 struct nand_chip *chip = mtd_to_nand(mtd);
1636 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1637 int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
1638
1639 memcpy(buf, nandc->data_buffer + nandc->buf_start, real_len);
1640 nandc->buf_start += real_len;
1641}
1642
1643static void qcom_nandc_write_buf(struct mtd_info *mtd, const uint8_t *buf,
1644 int len)
1645{
1646 struct nand_chip *chip = mtd_to_nand(mtd);
1647 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1648 int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
1649
1650 memcpy(nandc->data_buffer + nandc->buf_start, buf, real_len);
1651
1652 nandc->buf_start += real_len;
1653}
1654
1655/* we support only one external chip for now */
1656static void qcom_nandc_select_chip(struct mtd_info *mtd, int chipnr)
1657{
1658 struct nand_chip *chip = mtd_to_nand(mtd);
1659 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1660
1661 if (chipnr <= 0)
1662 return;
1663
1664 dev_warn(nandc->dev, "invalid chip select\n");
1665}
1666
1667/*
1668 * NAND controller page layout info
1669 *
1670 * Layout with ECC enabled:
1671 *
1672 * |----------------------| |---------------------------------|
1673 * | xx.......yy| | *********xx.......yy|
1674 * | DATA xx..ECC..yy| | DATA **SPARE**xx..ECC..yy|
1675 * | (516) xx.......yy| | (516-n*4) **(n*4)**xx.......yy|
1676 * | xx.......yy| | *********xx.......yy|
1677 * |----------------------| |---------------------------------|
1678 * codeword 1,2..n-1 codeword n
1679 * <---(528/532 Bytes)--> <-------(528/532 Bytes)--------->
1680 *
1681 * n = Number of codewords in the page
1682 * . = ECC bytes
1683 * * = Spare/free bytes
1684 * x = Unused byte(s)
1685 * y = Reserved byte(s)
1686 *
1687 * 2K page: n = 4, spare = 16 bytes
1688 * 4K page: n = 8, spare = 32 bytes
1689 * 8K page: n = 16, spare = 64 bytes
1690 *
1691 * the qcom nand controller operates at a sub page/codeword level. each
1692 * codeword is 528 and 532 bytes for 4 bit and 8 bit ECC modes respectively.
1693 * the number of ECC bytes vary based on the ECC strength and the bus width.
1694 *
1695 * the first n - 1 codewords contains 516 bytes of user data, the remaining
1696 * 12/16 bytes consist of ECC and reserved data. The nth codeword contains
1697 * both user data and spare(oobavail) bytes that sum up to 516 bytes.
1698 *
1699 * When we access a page with ECC enabled, the reserved bytes(s) are not
1700 * accessible at all. When reading, we fill up these unreadable positions
1701 * with 0xffs. When writing, the controller skips writing the inaccessible
1702 * bytes.
1703 *
1704 * Layout with ECC disabled:
1705 *
1706 * |------------------------------| |---------------------------------------|
1707 * | yy xx.......| | bb *********xx.......|
1708 * | DATA1 yy DATA2 xx..ECC..| | DATA1 bb DATA2 **SPARE**xx..ECC..|
1709 * | (size1) yy (size2) xx.......| | (size1) bb (size2) **(n*4)**xx.......|
1710 * | yy xx.......| | bb *********xx.......|
1711 * |------------------------------| |---------------------------------------|
1712 * codeword 1,2..n-1 codeword n
1713 * <-------(528/532 Bytes)------> <-----------(528/532 Bytes)----------->
1714 *
1715 * n = Number of codewords in the page
1716 * . = ECC bytes
1717 * * = Spare/free bytes
1718 * x = Unused byte(s)
1719 * y = Dummy Bad Bock byte(s)
1720 * b = Real Bad Block byte(s)
1721 * size1/size2 = function of codeword size and 'n'
1722 *
1723 * when the ECC block is disabled, one reserved byte (or two for 16 bit bus
1724 * width) is now accessible. For the first n - 1 codewords, these are dummy Bad
1725 * Block Markers. In the last codeword, this position contains the real BBM
1726 *
1727 * In order to have a consistent layout between RAW and ECC modes, we assume
1728 * the following OOB layout arrangement:
1729 *
1730 * |-----------| |--------------------|
1731 * |yyxx.......| |bb*********xx.......|
1732 * |yyxx..ECC..| |bb*FREEOOB*xx..ECC..|
1733 * |yyxx.......| |bb*********xx.......|
1734 * |yyxx.......| |bb*********xx.......|
1735 * |-----------| |--------------------|
1736 * first n - 1 nth OOB region
1737 * OOB regions
1738 *
1739 * n = Number of codewords in the page
1740 * . = ECC bytes
1741 * * = FREE OOB bytes
1742 * y = Dummy bad block byte(s) (inaccessible when ECC enabled)
1743 * x = Unused byte(s)
1744 * b = Real bad block byte(s) (inaccessible when ECC enabled)
1745 *
1746 * This layout is read as is when ECC is disabled. When ECC is enabled, the
1747 * inaccessible Bad Block byte(s) are ignored when we write to a page/oob,
1748 * and assumed as 0xffs when we read a page/oob. The ECC, unused and
Boris Brezillon421e81c2016-03-18 17:54:27 +01001749 * dummy/real bad block bytes are grouped as ecc bytes (i.e, ecc->bytes is
1750 * the sum of the three).
Archit Tanejac76b78d2016-02-03 14:29:50 +05301751 */
Boris Brezillon421e81c2016-03-18 17:54:27 +01001752static int qcom_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
1753 struct mtd_oob_region *oobregion)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301754{
Boris Brezillon421e81c2016-03-18 17:54:27 +01001755 struct nand_chip *chip = mtd_to_nand(mtd);
1756 struct qcom_nand_host *host = to_qcom_nand_host(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301757 struct nand_ecc_ctrl *ecc = &chip->ecc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301758
Boris Brezillon421e81c2016-03-18 17:54:27 +01001759 if (section > 1)
1760 return -ERANGE;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301761
Boris Brezillon421e81c2016-03-18 17:54:27 +01001762 if (!section) {
1763 oobregion->length = (ecc->bytes * (ecc->steps - 1)) +
1764 host->bbm_size;
1765 oobregion->offset = 0;
1766 } else {
1767 oobregion->length = host->ecc_bytes_hw + host->spare_bytes;
1768 oobregion->offset = mtd->oobsize - oobregion->length;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301769 }
1770
Boris Brezillon421e81c2016-03-18 17:54:27 +01001771 return 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301772}
1773
Boris Brezillon421e81c2016-03-18 17:54:27 +01001774static int qcom_nand_ooblayout_free(struct mtd_info *mtd, int section,
1775 struct mtd_oob_region *oobregion)
1776{
1777 struct nand_chip *chip = mtd_to_nand(mtd);
1778 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1779 struct nand_ecc_ctrl *ecc = &chip->ecc;
1780
1781 if (section)
1782 return -ERANGE;
1783
1784 oobregion->length = ecc->steps * 4;
1785 oobregion->offset = ((ecc->steps - 1) * ecc->bytes) + host->bbm_size;
1786
1787 return 0;
1788}
1789
1790static const struct mtd_ooblayout_ops qcom_nand_ooblayout_ops = {
1791 .ecc = qcom_nand_ooblayout_ecc,
1792 .free = qcom_nand_ooblayout_free,
1793};
1794
Archit Tanejac76b78d2016-02-03 14:29:50 +05301795static int qcom_nand_host_setup(struct qcom_nand_host *host)
1796{
1797 struct nand_chip *chip = &host->chip;
1798 struct mtd_info *mtd = nand_to_mtd(chip);
1799 struct nand_ecc_ctrl *ecc = &chip->ecc;
1800 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1801 int cwperpage, bad_block_byte;
1802 bool wide_bus;
1803 int ecc_mode = 1;
1804
1805 /*
1806 * the controller requires each step consists of 512 bytes of data.
1807 * bail out if DT has populated a wrong step size.
1808 */
1809 if (ecc->size != NANDC_STEP_SIZE) {
1810 dev_err(nandc->dev, "invalid ecc size\n");
1811 return -EINVAL;
1812 }
1813
1814 wide_bus = chip->options & NAND_BUSWIDTH_16 ? true : false;
1815
1816 if (ecc->strength >= 8) {
1817 /* 8 bit ECC defaults to BCH ECC on all platforms */
1818 host->bch_enabled = true;
1819 ecc_mode = 1;
1820
1821 if (wide_bus) {
1822 host->ecc_bytes_hw = 14;
1823 host->spare_bytes = 0;
1824 host->bbm_size = 2;
1825 } else {
1826 host->ecc_bytes_hw = 13;
1827 host->spare_bytes = 2;
1828 host->bbm_size = 1;
1829 }
1830 } else {
1831 /*
1832 * if the controller supports BCH for 4 bit ECC, the controller
1833 * uses lesser bytes for ECC. If RS is used, the ECC bytes is
1834 * always 10 bytes
1835 */
Abhishek Sahu58f1f222017-08-11 17:09:17 +05301836 if (nandc->props->ecc_modes & ECC_BCH_4BIT) {
Archit Tanejac76b78d2016-02-03 14:29:50 +05301837 /* BCH */
1838 host->bch_enabled = true;
1839 ecc_mode = 0;
1840
1841 if (wide_bus) {
1842 host->ecc_bytes_hw = 8;
1843 host->spare_bytes = 2;
1844 host->bbm_size = 2;
1845 } else {
1846 host->ecc_bytes_hw = 7;
1847 host->spare_bytes = 4;
1848 host->bbm_size = 1;
1849 }
1850 } else {
1851 /* RS */
1852 host->ecc_bytes_hw = 10;
1853
1854 if (wide_bus) {
1855 host->spare_bytes = 0;
1856 host->bbm_size = 2;
1857 } else {
1858 host->spare_bytes = 1;
1859 host->bbm_size = 1;
1860 }
1861 }
1862 }
1863
1864 /*
1865 * we consider ecc->bytes as the sum of all the non-data content in a
1866 * step. It gives us a clean representation of the oob area (even if
1867 * all the bytes aren't used for ECC).It is always 16 bytes for 8 bit
1868 * ECC and 12 bytes for 4 bit ECC
1869 */
1870 ecc->bytes = host->ecc_bytes_hw + host->spare_bytes + host->bbm_size;
1871
1872 ecc->read_page = qcom_nandc_read_page;
1873 ecc->read_page_raw = qcom_nandc_read_page_raw;
1874 ecc->read_oob = qcom_nandc_read_oob;
1875 ecc->write_page = qcom_nandc_write_page;
1876 ecc->write_page_raw = qcom_nandc_write_page_raw;
1877 ecc->write_oob = qcom_nandc_write_oob;
1878
1879 ecc->mode = NAND_ECC_HW;
1880
Boris Brezillon421e81c2016-03-18 17:54:27 +01001881 mtd_set_ooblayout(mtd, &qcom_nand_ooblayout_ops);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301882
1883 cwperpage = mtd->writesize / ecc->size;
1884
1885 /*
1886 * DATA_UD_BYTES varies based on whether the read/write command protects
1887 * spare data with ECC too. We protect spare data by default, so we set
1888 * it to main + spare data, which are 512 and 4 bytes respectively.
1889 */
1890 host->cw_data = 516;
1891
1892 /*
1893 * total bytes in a step, either 528 bytes for 4 bit ECC, or 532 bytes
1894 * for 8 bit ECC
1895 */
1896 host->cw_size = host->cw_data + ecc->bytes;
1897
1898 if (ecc->bytes * (mtd->writesize / ecc->size) > mtd->oobsize) {
1899 dev_err(nandc->dev, "ecc data doesn't fit in OOB area\n");
1900 return -EINVAL;
1901 }
1902
1903 bad_block_byte = mtd->writesize - host->cw_size * (cwperpage - 1) + 1;
1904
1905 host->cfg0 = (cwperpage - 1) << CW_PER_PAGE
1906 | host->cw_data << UD_SIZE_BYTES
1907 | 0 << DISABLE_STATUS_AFTER_WRITE
1908 | 5 << NUM_ADDR_CYCLES
1909 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_RS
1910 | 0 << STATUS_BFR_READ
1911 | 1 << SET_RD_MODE_AFTER_STATUS
1912 | host->spare_bytes << SPARE_SIZE_BYTES;
1913
1914 host->cfg1 = 7 << NAND_RECOVERY_CYCLES
1915 | 0 << CS_ACTIVE_BSY
1916 | bad_block_byte << BAD_BLOCK_BYTE_NUM
1917 | 0 << BAD_BLOCK_IN_SPARE_AREA
1918 | 2 << WR_RD_BSY_GAP
1919 | wide_bus << WIDE_FLASH
1920 | host->bch_enabled << ENABLE_BCH_ECC;
1921
1922 host->cfg0_raw = (cwperpage - 1) << CW_PER_PAGE
1923 | host->cw_size << UD_SIZE_BYTES
1924 | 5 << NUM_ADDR_CYCLES
1925 | 0 << SPARE_SIZE_BYTES;
1926
1927 host->cfg1_raw = 7 << NAND_RECOVERY_CYCLES
1928 | 0 << CS_ACTIVE_BSY
1929 | 17 << BAD_BLOCK_BYTE_NUM
1930 | 1 << BAD_BLOCK_IN_SPARE_AREA
1931 | 2 << WR_RD_BSY_GAP
1932 | wide_bus << WIDE_FLASH
1933 | 1 << DEV0_CFG1_ECC_DISABLE;
1934
Abhishek Sahu10777de2017-08-03 17:56:39 +02001935 host->ecc_bch_cfg = !host->bch_enabled << ECC_CFG_ECC_DISABLE
Archit Tanejac76b78d2016-02-03 14:29:50 +05301936 | 0 << ECC_SW_RESET
1937 | host->cw_data << ECC_NUM_DATA_BYTES
1938 | 1 << ECC_FORCE_CLK_OPEN
1939 | ecc_mode << ECC_MODE
1940 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_BCH;
1941
1942 host->ecc_buf_cfg = 0x203 << NUM_STEPS;
1943
1944 host->clrflashstatus = FS_READY_BSY_N;
1945 host->clrreadstatus = 0xc0;
1946
1947 dev_dbg(nandc->dev,
1948 "cfg0 %x cfg1 %x ecc_buf_cfg %x ecc_bch cfg %x cw_size %d cw_data %d strength %d parity_bytes %d steps %d\n",
1949 host->cfg0, host->cfg1, host->ecc_buf_cfg, host->ecc_bch_cfg,
1950 host->cw_size, host->cw_data, ecc->strength, ecc->bytes,
1951 cwperpage);
1952
1953 return 0;
1954}
1955
1956static int qcom_nandc_alloc(struct qcom_nand_controller *nandc)
1957{
1958 int ret;
1959
1960 ret = dma_set_coherent_mask(nandc->dev, DMA_BIT_MASK(32));
1961 if (ret) {
1962 dev_err(nandc->dev, "failed to set DMA mask\n");
1963 return ret;
1964 }
1965
1966 /*
1967 * we use the internal buffer for reading ONFI params, reading small
1968 * data like ID and status, and preforming read-copy-write operations
1969 * when writing to a codeword partially. 532 is the maximum possible
1970 * size of a codeword for our nand controller
1971 */
1972 nandc->buf_size = 532;
1973
1974 nandc->data_buffer = devm_kzalloc(nandc->dev, nandc->buf_size,
1975 GFP_KERNEL);
1976 if (!nandc->data_buffer)
1977 return -ENOMEM;
1978
1979 nandc->regs = devm_kzalloc(nandc->dev, sizeof(*nandc->regs),
1980 GFP_KERNEL);
1981 if (!nandc->regs)
1982 return -ENOMEM;
1983
1984 nandc->reg_read_buf = devm_kzalloc(nandc->dev,
1985 MAX_REG_RD * sizeof(*nandc->reg_read_buf),
1986 GFP_KERNEL);
1987 if (!nandc->reg_read_buf)
1988 return -ENOMEM;
1989
1990 nandc->chan = dma_request_slave_channel(nandc->dev, "rxtx");
1991 if (!nandc->chan) {
1992 dev_err(nandc->dev, "failed to request slave channel\n");
1993 return -ENODEV;
1994 }
1995
1996 INIT_LIST_HEAD(&nandc->desc_list);
1997 INIT_LIST_HEAD(&nandc->host_list);
1998
Marc Gonzalezd45bc582016-07-27 11:23:52 +02001999 nand_hw_control_init(&nandc->controller);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302000
2001 return 0;
2002}
2003
2004static void qcom_nandc_unalloc(struct qcom_nand_controller *nandc)
2005{
2006 dma_release_channel(nandc->chan);
2007}
2008
2009/* one time setup of a few nand controller registers */
2010static int qcom_nandc_setup(struct qcom_nand_controller *nandc)
2011{
2012 /* kill onenand */
2013 nandc_write(nandc, SFLASHC_BURST_CFG, 0);
Abhishek Sahud8a9b322017-08-11 17:09:16 +05302014 nandc_write(nandc, NAND_DEV_CMD_VLD, NAND_DEV_CMD_VLD_VAL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302015
2016 /* enable ADM DMA */
2017 nandc_write(nandc, NAND_FLASH_CHIP_SELECT, DM_EN);
2018
2019 /* save the original values of these registers */
2020 nandc->cmd1 = nandc_read(nandc, NAND_DEV_CMD1);
Abhishek Sahud8a9b322017-08-11 17:09:16 +05302021 nandc->vld = NAND_DEV_CMD_VLD_VAL;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302022
2023 return 0;
2024}
2025
2026static int qcom_nand_host_init(struct qcom_nand_controller *nandc,
2027 struct qcom_nand_host *host,
2028 struct device_node *dn)
2029{
2030 struct nand_chip *chip = &host->chip;
2031 struct mtd_info *mtd = nand_to_mtd(chip);
2032 struct device *dev = nandc->dev;
2033 int ret;
2034
2035 ret = of_property_read_u32(dn, "reg", &host->cs);
2036 if (ret) {
2037 dev_err(dev, "can't get chip-select\n");
2038 return -ENXIO;
2039 }
2040
2041 nand_set_flash_node(chip, dn);
2042 mtd->name = devm_kasprintf(dev, GFP_KERNEL, "qcom_nand.%d", host->cs);
2043 mtd->owner = THIS_MODULE;
2044 mtd->dev.parent = dev;
2045
2046 chip->cmdfunc = qcom_nandc_command;
2047 chip->select_chip = qcom_nandc_select_chip;
2048 chip->read_byte = qcom_nandc_read_byte;
2049 chip->read_buf = qcom_nandc_read_buf;
2050 chip->write_buf = qcom_nandc_write_buf;
Boris Brezillon4a78cc62017-05-26 17:10:15 +02002051 chip->onfi_set_features = nand_onfi_get_set_features_notsupp;
2052 chip->onfi_get_features = nand_onfi_get_set_features_notsupp;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302053
2054 /*
2055 * the bad block marker is readable only when we read the last codeword
2056 * of a page with ECC disabled. currently, the nand_base and nand_bbt
2057 * helpers don't allow us to read BB from a nand chip with ECC
2058 * disabled (MTD_OPS_PLACE_OOB is set by default). use the block_bad
2059 * and block_markbad helpers until we permanently switch to using
2060 * MTD_OPS_RAW for all drivers (with the help of badblockbits)
2061 */
2062 chip->block_bad = qcom_nandc_block_bad;
2063 chip->block_markbad = qcom_nandc_block_markbad;
2064
2065 chip->controller = &nandc->controller;
2066 chip->options |= NAND_NO_SUBPAGE_WRITE | NAND_USE_BOUNCE_BUFFER |
2067 NAND_SKIP_BBTSCAN;
2068
2069 /* set up initial status value */
2070 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
2071
2072 ret = nand_scan_ident(mtd, 1, NULL);
2073 if (ret)
2074 return ret;
2075
2076 ret = qcom_nand_host_setup(host);
Abhishek Sahu89f51272017-07-19 17:17:58 +05302077
2078 return ret;
2079}
2080
2081static int qcom_nand_mtd_register(struct qcom_nand_controller *nandc,
2082 struct qcom_nand_host *host,
2083 struct device_node *dn)
2084{
2085 struct nand_chip *chip = &host->chip;
2086 struct mtd_info *mtd = nand_to_mtd(chip);
2087 int ret;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302088
2089 ret = nand_scan_tail(mtd);
2090 if (ret)
2091 return ret;
2092
Abhishek Sahu89f51272017-07-19 17:17:58 +05302093 ret = mtd_device_register(mtd, NULL, 0);
2094 if (ret)
2095 nand_cleanup(mtd_to_nand(mtd));
2096
2097 return ret;
2098}
2099
2100static int qcom_probe_nand_devices(struct qcom_nand_controller *nandc)
2101{
2102 struct device *dev = nandc->dev;
2103 struct device_node *dn = dev->of_node, *child;
2104 struct qcom_nand_host *host, *tmp;
2105 int ret;
2106
2107 for_each_available_child_of_node(dn, child) {
2108 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
2109 if (!host) {
2110 of_node_put(child);
2111 return -ENOMEM;
2112 }
2113
2114 ret = qcom_nand_host_init(nandc, host, child);
2115 if (ret) {
2116 devm_kfree(dev, host);
2117 continue;
2118 }
2119
2120 list_add_tail(&host->node, &nandc->host_list);
2121 }
2122
2123 if (list_empty(&nandc->host_list))
2124 return -ENODEV;
2125
2126 list_for_each_entry_safe(host, tmp, &nandc->host_list, node) {
2127 ret = qcom_nand_mtd_register(nandc, host, child);
2128 if (ret) {
2129 list_del(&host->node);
2130 devm_kfree(dev, host);
2131 }
2132 }
2133
2134 if (list_empty(&nandc->host_list))
2135 return -ENODEV;
2136
2137 return 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302138}
2139
2140/* parse custom DT properties here */
2141static int qcom_nandc_parse_dt(struct platform_device *pdev)
2142{
2143 struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
2144 struct device_node *np = nandc->dev->of_node;
2145 int ret;
2146
2147 ret = of_property_read_u32(np, "qcom,cmd-crci", &nandc->cmd_crci);
2148 if (ret) {
2149 dev_err(nandc->dev, "command CRCI unspecified\n");
2150 return ret;
2151 }
2152
2153 ret = of_property_read_u32(np, "qcom,data-crci", &nandc->data_crci);
2154 if (ret) {
2155 dev_err(nandc->dev, "data CRCI unspecified\n");
2156 return ret;
2157 }
2158
2159 return 0;
2160}
2161
2162static int qcom_nandc_probe(struct platform_device *pdev)
2163{
2164 struct qcom_nand_controller *nandc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302165 const void *dev_data;
2166 struct device *dev = &pdev->dev;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302167 struct resource *res;
2168 int ret;
2169
2170 nandc = devm_kzalloc(&pdev->dev, sizeof(*nandc), GFP_KERNEL);
2171 if (!nandc)
2172 return -ENOMEM;
2173
2174 platform_set_drvdata(pdev, nandc);
2175 nandc->dev = dev;
2176
2177 dev_data = of_device_get_match_data(dev);
2178 if (!dev_data) {
2179 dev_err(&pdev->dev, "failed to get device data\n");
2180 return -ENODEV;
2181 }
2182
Abhishek Sahu58f1f222017-08-11 17:09:17 +05302183 nandc->props = dev_data;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302184
2185 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2186 nandc->base = devm_ioremap_resource(dev, res);
2187 if (IS_ERR(nandc->base))
2188 return PTR_ERR(nandc->base);
2189
2190 nandc->base_dma = phys_to_dma(dev, (phys_addr_t)res->start);
2191
2192 nandc->core_clk = devm_clk_get(dev, "core");
2193 if (IS_ERR(nandc->core_clk))
2194 return PTR_ERR(nandc->core_clk);
2195
2196 nandc->aon_clk = devm_clk_get(dev, "aon");
2197 if (IS_ERR(nandc->aon_clk))
2198 return PTR_ERR(nandc->aon_clk);
2199
2200 ret = qcom_nandc_parse_dt(pdev);
2201 if (ret)
2202 return ret;
2203
2204 ret = qcom_nandc_alloc(nandc);
2205 if (ret)
2206 return ret;
2207
2208 ret = clk_prepare_enable(nandc->core_clk);
2209 if (ret)
2210 goto err_core_clk;
2211
2212 ret = clk_prepare_enable(nandc->aon_clk);
2213 if (ret)
2214 goto err_aon_clk;
2215
2216 ret = qcom_nandc_setup(nandc);
2217 if (ret)
2218 goto err_setup;
2219
Abhishek Sahu89f51272017-07-19 17:17:58 +05302220 ret = qcom_probe_nand_devices(nandc);
2221 if (ret)
2222 goto err_setup;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302223
2224 return 0;
2225
Archit Tanejac76b78d2016-02-03 14:29:50 +05302226err_setup:
2227 clk_disable_unprepare(nandc->aon_clk);
2228err_aon_clk:
2229 clk_disable_unprepare(nandc->core_clk);
2230err_core_clk:
2231 qcom_nandc_unalloc(nandc);
2232
2233 return ret;
2234}
2235
2236static int qcom_nandc_remove(struct platform_device *pdev)
2237{
2238 struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
2239 struct qcom_nand_host *host;
2240
2241 list_for_each_entry(host, &nandc->host_list, node)
2242 nand_release(nand_to_mtd(&host->chip));
2243
2244 qcom_nandc_unalloc(nandc);
2245
2246 clk_disable_unprepare(nandc->aon_clk);
2247 clk_disable_unprepare(nandc->core_clk);
2248
2249 return 0;
2250}
2251
Abhishek Sahu58f1f222017-08-11 17:09:17 +05302252static const struct qcom_nandc_props ipq806x_nandc_props = {
2253 .ecc_modes = (ECC_RS_4BIT | ECC_BCH_8BIT),
2254};
Archit Tanejac76b78d2016-02-03 14:29:50 +05302255
2256/*
2257 * data will hold a struct pointer containing more differences once we support
2258 * more controller variants
2259 */
2260static const struct of_device_id qcom_nandc_of_match[] = {
Abhishek Sahu58f1f222017-08-11 17:09:17 +05302261 {
2262 .compatible = "qcom,ipq806x-nand",
2263 .data = &ipq806x_nandc_props,
Archit Tanejac76b78d2016-02-03 14:29:50 +05302264 },
2265 {}
2266};
2267MODULE_DEVICE_TABLE(of, qcom_nandc_of_match);
2268
2269static struct platform_driver qcom_nandc_driver = {
2270 .driver = {
2271 .name = "qcom-nandc",
2272 .of_match_table = qcom_nandc_of_match,
2273 },
2274 .probe = qcom_nandc_probe,
2275 .remove = qcom_nandc_remove,
2276};
2277module_platform_driver(qcom_nandc_driver);
2278
2279MODULE_AUTHOR("Archit Taneja <architt@codeaurora.org>");
2280MODULE_DESCRIPTION("Qualcomm NAND Controller driver");
2281MODULE_LICENSE("GPL v2");