Linux 4.9.89
[linux/fpc-iii.git] / drivers / mtd / nand / qcom_nandc.c
blob6f0fd1512ad29b9420467b4d73f13f3e0795608a
1 /*
2 * Copyright (c) 2016, The Linux Foundation. All rights reserved.
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.
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.
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>
24 #include <linux/delay.h>
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
52 #define NAND_CTRL 0xf00
53 #define NAND_VERSION 0xf08
54 #define NAND_READ_LOCATION_0 0xf20
55 #define NAND_READ_LOCATION_1 0xf24
57 /* dummy register offsets, used by write_reg_dma */
58 #define NAND_DEV_CMD1_RESTORE 0xdead
59 #define NAND_DEV_CMD_VLD_RESTORE 0xbeef
61 /* NAND_FLASH_CMD bits */
62 #define PAGE_ACC BIT(4)
63 #define LAST_PAGE BIT(5)
65 /* NAND_FLASH_CHIP_SELECT bits */
66 #define NAND_DEV_SEL 0
67 #define DM_EN BIT(2)
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)
76 /* NAND_BUFFER_STATUS bits */
77 #define BS_UNCORRECTABLE_BIT BIT(8)
78 #define BS_CORRECTABLE_ERR_MSK 0x1f
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
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
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
108 /* NAND_DEV_CMD1 bits */
109 #define READ_ADDR 0
111 /* NAND_DEV_CMD_VLD bits */
112 #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)
118 /* NAND_EBI2_ECC_BUF_CFG bits */
119 #define NUM_STEPS 0
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)
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)
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
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
155 /* 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)
160 * the NAND controller performs reads/writes with ECC in 516 byte chunks.
161 * the driver calls the chunks 'step' or 'codeword' interchangeably
163 #define NANDC_STEP_SIZE 512
166 * the largest page size we support is 8K, this will have 16 steps/codewords
167 * of 512 bytes each
169 #define MAX_NUM_STEPS (SZ_8K / NANDC_STEP_SIZE)
171 /* we read at most 3 registers per codeword scan */
172 #define MAX_REG_RD (3 * MAX_NUM_STEPS)
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)
180 struct desc_info {
181 struct list_head node;
183 enum dma_data_direction dir;
184 struct scatterlist sgl;
185 struct dma_async_tx_descriptor *dma_desc;
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.
192 struct nandc_regs {
193 __le32 cmd;
194 __le32 addr0;
195 __le32 addr1;
196 __le32 chip_sel;
197 __le32 exec;
199 __le32 cfg0;
200 __le32 cfg1;
201 __le32 ecc_bch_cfg;
203 __le32 clrflashstatus;
204 __le32 clrreadstatus;
206 __le32 cmd1;
207 __le32 vld;
209 __le32 orig_cmd1;
210 __le32 orig_vld;
212 __le32 ecc_buf_cfg;
216 * NAND controller data struct
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
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)
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
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
243 * @ecc_modes: supported ECC modes by the current controller,
244 * initialized via DT match data
246 struct qcom_nand_controller {
247 struct nand_hw_control controller;
248 struct list_head host_list;
250 struct device *dev;
252 void __iomem *base;
253 dma_addr_t base_dma;
255 struct clk *core_clk;
256 struct clk *aon_clk;
258 struct dma_chan *chan;
259 unsigned int cmd_crci;
260 unsigned int data_crci;
261 struct list_head desc_list;
263 u8 *data_buffer;
264 int buf_size;
265 int buf_count;
266 int buf_start;
268 __le32 *reg_read_buf;
269 int reg_read_pos;
271 struct nandc_regs *regs;
273 u32 cmd1, vld;
274 u32 ecc_modes;
278 * NAND chip structure
280 * @chip: base NAND chip structure
281 * @node: list node to add itself to host_list in
282 * qcom_nand_controller
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
300 * @cfg0, cfg1, cfg0_raw..: NANDc register configurations needed for
301 * ecc/non-ecc mode for the current nand flash
302 * device
304 struct qcom_nand_host {
305 struct nand_chip chip;
306 struct list_head node;
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;
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;
327 static inline struct qcom_nand_host *to_qcom_nand_host(struct nand_chip *chip)
329 return container_of(chip, struct qcom_nand_host, chip);
332 static inline struct qcom_nand_controller *
333 get_qcom_nand_controller(struct nand_chip *chip)
335 return container_of(chip->controller, struct qcom_nand_controller,
336 controller);
339 static inline u32 nandc_read(struct qcom_nand_controller *nandc, int offset)
341 return ioread32(nandc->base + offset);
344 static inline void nandc_write(struct qcom_nand_controller *nandc, int offset,
345 u32 val)
347 iowrite32(val, nandc->base + offset);
350 static __le32 *offset_to_nandc_reg(struct nandc_regs *regs, int offset)
352 switch (offset) {
353 case NAND_FLASH_CMD:
354 return &regs->cmd;
355 case NAND_ADDR0:
356 return &regs->addr0;
357 case NAND_ADDR1:
358 return &regs->addr1;
359 case NAND_FLASH_CHIP_SELECT:
360 return &regs->chip_sel;
361 case NAND_EXEC_CMD:
362 return &regs->exec;
363 case NAND_FLASH_STATUS:
364 return &regs->clrflashstatus;
365 case NAND_DEV0_CFG0:
366 return &regs->cfg0;
367 case NAND_DEV0_CFG1:
368 return &regs->cfg1;
369 case NAND_DEV0_ECC_CFG:
370 return &regs->ecc_bch_cfg;
371 case NAND_READ_STATUS:
372 return &regs->clrreadstatus;
373 case NAND_DEV_CMD1:
374 return &regs->cmd1;
375 case NAND_DEV_CMD1_RESTORE:
376 return &regs->orig_cmd1;
377 case NAND_DEV_CMD_VLD:
378 return &regs->vld;
379 case NAND_DEV_CMD_VLD_RESTORE:
380 return &regs->orig_vld;
381 case NAND_EBI2_ECC_BUF_CFG:
382 return &regs->ecc_buf_cfg;
383 default:
384 return NULL;
388 static void nandc_set_reg(struct qcom_nand_controller *nandc, int offset,
389 u32 val)
391 struct nandc_regs *regs = nandc->regs;
392 __le32 *reg;
394 reg = offset_to_nandc_reg(regs, offset);
396 if (reg)
397 *reg = cpu_to_le32(val);
400 /* helper to configure address register values */
401 static void set_address(struct qcom_nand_host *host, u16 column, int page)
403 struct nand_chip *chip = &host->chip;
404 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
406 if (chip->options & NAND_BUSWIDTH_16)
407 column >>= 1;
409 nandc_set_reg(nandc, NAND_ADDR0, page << 16 | column);
410 nandc_set_reg(nandc, NAND_ADDR1, page >> 16 & 0xff);
414 * update_rw_regs: set up read/write register values, these will be
415 * written to the NAND controller registers via DMA
417 * @num_cw: number of steps for the read/write operation
418 * @read: read or write operation
420 static void update_rw_regs(struct qcom_nand_host *host, int num_cw, bool read)
422 struct nand_chip *chip = &host->chip;
423 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
424 u32 cmd, cfg0, cfg1, ecc_bch_cfg;
426 if (read) {
427 if (host->use_ecc)
428 cmd = PAGE_READ_WITH_ECC | PAGE_ACC | LAST_PAGE;
429 else
430 cmd = PAGE_READ | PAGE_ACC | LAST_PAGE;
431 } else {
432 cmd = PROGRAM_PAGE | PAGE_ACC | LAST_PAGE;
435 if (host->use_ecc) {
436 cfg0 = (host->cfg0 & ~(7U << CW_PER_PAGE)) |
437 (num_cw - 1) << CW_PER_PAGE;
439 cfg1 = host->cfg1;
440 ecc_bch_cfg = host->ecc_bch_cfg;
441 } else {
442 cfg0 = (host->cfg0_raw & ~(7U << CW_PER_PAGE)) |
443 (num_cw - 1) << CW_PER_PAGE;
445 cfg1 = host->cfg1_raw;
446 ecc_bch_cfg = 1 << ECC_CFG_ECC_DISABLE;
449 nandc_set_reg(nandc, NAND_FLASH_CMD, cmd);
450 nandc_set_reg(nandc, NAND_DEV0_CFG0, cfg0);
451 nandc_set_reg(nandc, NAND_DEV0_CFG1, cfg1);
452 nandc_set_reg(nandc, NAND_DEV0_ECC_CFG, ecc_bch_cfg);
453 nandc_set_reg(nandc, NAND_EBI2_ECC_BUF_CFG, host->ecc_buf_cfg);
454 nandc_set_reg(nandc, NAND_FLASH_STATUS, host->clrflashstatus);
455 nandc_set_reg(nandc, NAND_READ_STATUS, host->clrreadstatus);
456 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
459 static int prep_dma_desc(struct qcom_nand_controller *nandc, bool read,
460 int reg_off, const void *vaddr, int size,
461 bool flow_control)
463 struct desc_info *desc;
464 struct dma_async_tx_descriptor *dma_desc;
465 struct scatterlist *sgl;
466 struct dma_slave_config slave_conf;
467 enum dma_transfer_direction dir_eng;
468 int ret;
470 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
471 if (!desc)
472 return -ENOMEM;
474 sgl = &desc->sgl;
476 sg_init_one(sgl, vaddr, size);
478 if (read) {
479 dir_eng = DMA_DEV_TO_MEM;
480 desc->dir = DMA_FROM_DEVICE;
481 } else {
482 dir_eng = DMA_MEM_TO_DEV;
483 desc->dir = DMA_TO_DEVICE;
486 ret = dma_map_sg(nandc->dev, sgl, 1, desc->dir);
487 if (ret == 0) {
488 ret = -ENOMEM;
489 goto err;
492 memset(&slave_conf, 0x00, sizeof(slave_conf));
494 slave_conf.device_fc = flow_control;
495 if (read) {
496 slave_conf.src_maxburst = 16;
497 slave_conf.src_addr = nandc->base_dma + reg_off;
498 slave_conf.slave_id = nandc->data_crci;
499 } else {
500 slave_conf.dst_maxburst = 16;
501 slave_conf.dst_addr = nandc->base_dma + reg_off;
502 slave_conf.slave_id = nandc->cmd_crci;
505 ret = dmaengine_slave_config(nandc->chan, &slave_conf);
506 if (ret) {
507 dev_err(nandc->dev, "failed to configure dma channel\n");
508 goto err;
511 dma_desc = dmaengine_prep_slave_sg(nandc->chan, sgl, 1, dir_eng, 0);
512 if (!dma_desc) {
513 dev_err(nandc->dev, "failed to prepare desc\n");
514 ret = -EINVAL;
515 goto err;
518 desc->dma_desc = dma_desc;
520 list_add_tail(&desc->node, &nandc->desc_list);
522 return 0;
523 err:
524 kfree(desc);
526 return ret;
530 * read_reg_dma: prepares a descriptor to read a given number of
531 * contiguous registers to the reg_read_buf pointer
533 * @first: offset of the first register in the contiguous block
534 * @num_regs: number of registers to read
536 static int read_reg_dma(struct qcom_nand_controller *nandc, int first,
537 int num_regs)
539 bool flow_control = false;
540 void *vaddr;
541 int size;
543 if (first == NAND_READ_ID || first == NAND_FLASH_STATUS)
544 flow_control = true;
546 size = num_regs * sizeof(u32);
547 vaddr = nandc->reg_read_buf + nandc->reg_read_pos;
548 nandc->reg_read_pos += num_regs;
550 return prep_dma_desc(nandc, true, first, vaddr, size, flow_control);
554 * write_reg_dma: prepares a descriptor to write a given number of
555 * contiguous registers
557 * @first: offset of the first register in the contiguous block
558 * @num_regs: number of registers to write
560 static int write_reg_dma(struct qcom_nand_controller *nandc, int first,
561 int num_regs)
563 bool flow_control = false;
564 struct nandc_regs *regs = nandc->regs;
565 void *vaddr;
566 int size;
568 vaddr = offset_to_nandc_reg(regs, first);
570 if (first == NAND_FLASH_CMD)
571 flow_control = true;
573 if (first == NAND_DEV_CMD1_RESTORE)
574 first = NAND_DEV_CMD1;
576 if (first == NAND_DEV_CMD_VLD_RESTORE)
577 first = NAND_DEV_CMD_VLD;
579 size = num_regs * sizeof(u32);
581 return prep_dma_desc(nandc, false, first, vaddr, size, flow_control);
585 * read_data_dma: prepares a DMA descriptor to transfer data from the
586 * controller's internal buffer to the buffer 'vaddr'
588 * @reg_off: offset within the controller's data buffer
589 * @vaddr: virtual address of the buffer we want to write to
590 * @size: DMA transaction size in bytes
592 static int read_data_dma(struct qcom_nand_controller *nandc, int reg_off,
593 const u8 *vaddr, int size)
595 return prep_dma_desc(nandc, true, reg_off, vaddr, size, false);
599 * write_data_dma: prepares a DMA descriptor to transfer data from
600 * 'vaddr' to the controller's internal buffer
602 * @reg_off: offset within the controller's data buffer
603 * @vaddr: virtual address of the buffer we want to read from
604 * @size: DMA transaction size in bytes
606 static int write_data_dma(struct qcom_nand_controller *nandc, int reg_off,
607 const u8 *vaddr, int size)
609 return prep_dma_desc(nandc, false, reg_off, vaddr, size, false);
613 * helper to prepare dma descriptors to configure registers needed for reading a
614 * codeword/step in a page
616 static void config_cw_read(struct qcom_nand_controller *nandc)
618 write_reg_dma(nandc, NAND_FLASH_CMD, 3);
619 write_reg_dma(nandc, NAND_DEV0_CFG0, 3);
620 write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1);
622 write_reg_dma(nandc, NAND_EXEC_CMD, 1);
624 read_reg_dma(nandc, NAND_FLASH_STATUS, 2);
625 read_reg_dma(nandc, NAND_ERASED_CW_DETECT_STATUS, 1);
629 * helpers to prepare dma descriptors used to configure registers needed for
630 * writing a codeword/step in a page
632 static void config_cw_write_pre(struct qcom_nand_controller *nandc)
634 write_reg_dma(nandc, NAND_FLASH_CMD, 3);
635 write_reg_dma(nandc, NAND_DEV0_CFG0, 3);
636 write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1);
639 static void config_cw_write_post(struct qcom_nand_controller *nandc)
641 write_reg_dma(nandc, NAND_EXEC_CMD, 1);
643 read_reg_dma(nandc, NAND_FLASH_STATUS, 1);
645 write_reg_dma(nandc, NAND_FLASH_STATUS, 1);
646 write_reg_dma(nandc, NAND_READ_STATUS, 1);
650 * the following functions are used within chip->cmdfunc() to perform different
651 * NAND_CMD_* commands
654 /* sets up descriptors for NAND_CMD_PARAM */
655 static int nandc_param(struct qcom_nand_host *host)
657 struct nand_chip *chip = &host->chip;
658 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
661 * NAND_CMD_PARAM is called before we know much about the FLASH chip
662 * in use. we configure the controller to perform a raw read of 512
663 * bytes to read onfi params
665 nandc_set_reg(nandc, NAND_FLASH_CMD, PAGE_READ | PAGE_ACC | LAST_PAGE);
666 nandc_set_reg(nandc, NAND_ADDR0, 0);
667 nandc_set_reg(nandc, NAND_ADDR1, 0);
668 nandc_set_reg(nandc, NAND_DEV0_CFG0, 0 << CW_PER_PAGE
669 | 512 << UD_SIZE_BYTES
670 | 5 << NUM_ADDR_CYCLES
671 | 0 << SPARE_SIZE_BYTES);
672 nandc_set_reg(nandc, NAND_DEV0_CFG1, 7 << NAND_RECOVERY_CYCLES
673 | 0 << CS_ACTIVE_BSY
674 | 17 << BAD_BLOCK_BYTE_NUM
675 | 1 << BAD_BLOCK_IN_SPARE_AREA
676 | 2 << WR_RD_BSY_GAP
677 | 0 << WIDE_FLASH
678 | 1 << DEV0_CFG1_ECC_DISABLE);
679 nandc_set_reg(nandc, NAND_EBI2_ECC_BUF_CFG, 1 << ECC_CFG_ECC_DISABLE);
681 /* configure CMD1 and VLD for ONFI param probing */
682 nandc_set_reg(nandc, NAND_DEV_CMD_VLD,
683 (nandc->vld & ~READ_START_VLD));
684 nandc_set_reg(nandc, NAND_DEV_CMD1,
685 (nandc->cmd1 & ~(0xFF << READ_ADDR))
686 | NAND_CMD_PARAM << READ_ADDR);
688 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
690 nandc_set_reg(nandc, NAND_DEV_CMD1_RESTORE, nandc->cmd1);
691 nandc_set_reg(nandc, NAND_DEV_CMD_VLD_RESTORE, nandc->vld);
693 write_reg_dma(nandc, NAND_DEV_CMD_VLD, 1);
694 write_reg_dma(nandc, NAND_DEV_CMD1, 1);
696 nandc->buf_count = 512;
697 memset(nandc->data_buffer, 0xff, nandc->buf_count);
699 config_cw_read(nandc);
701 read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer,
702 nandc->buf_count);
704 /* restore CMD1 and VLD regs */
705 write_reg_dma(nandc, NAND_DEV_CMD1_RESTORE, 1);
706 write_reg_dma(nandc, NAND_DEV_CMD_VLD_RESTORE, 1);
708 return 0;
711 /* sets up descriptors for NAND_CMD_ERASE1 */
712 static int erase_block(struct qcom_nand_host *host, int page_addr)
714 struct nand_chip *chip = &host->chip;
715 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
717 nandc_set_reg(nandc, NAND_FLASH_CMD,
718 BLOCK_ERASE | PAGE_ACC | LAST_PAGE);
719 nandc_set_reg(nandc, NAND_ADDR0, page_addr);
720 nandc_set_reg(nandc, NAND_ADDR1, 0);
721 nandc_set_reg(nandc, NAND_DEV0_CFG0,
722 host->cfg0_raw & ~(7 << CW_PER_PAGE));
723 nandc_set_reg(nandc, NAND_DEV0_CFG1, host->cfg1_raw);
724 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
725 nandc_set_reg(nandc, NAND_FLASH_STATUS, host->clrflashstatus);
726 nandc_set_reg(nandc, NAND_READ_STATUS, host->clrreadstatus);
728 write_reg_dma(nandc, NAND_FLASH_CMD, 3);
729 write_reg_dma(nandc, NAND_DEV0_CFG0, 2);
730 write_reg_dma(nandc, NAND_EXEC_CMD, 1);
732 read_reg_dma(nandc, NAND_FLASH_STATUS, 1);
734 write_reg_dma(nandc, NAND_FLASH_STATUS, 1);
735 write_reg_dma(nandc, NAND_READ_STATUS, 1);
737 return 0;
740 /* sets up descriptors for NAND_CMD_READID */
741 static int read_id(struct qcom_nand_host *host, int column)
743 struct nand_chip *chip = &host->chip;
744 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
746 if (column == -1)
747 return 0;
749 nandc_set_reg(nandc, NAND_FLASH_CMD, FETCH_ID);
750 nandc_set_reg(nandc, NAND_ADDR0, column);
751 nandc_set_reg(nandc, NAND_ADDR1, 0);
752 nandc_set_reg(nandc, NAND_FLASH_CHIP_SELECT, DM_EN);
753 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
755 write_reg_dma(nandc, NAND_FLASH_CMD, 4);
756 write_reg_dma(nandc, NAND_EXEC_CMD, 1);
758 read_reg_dma(nandc, NAND_READ_ID, 1);
760 return 0;
763 /* sets up descriptors for NAND_CMD_RESET */
764 static int reset(struct qcom_nand_host *host)
766 struct nand_chip *chip = &host->chip;
767 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
769 nandc_set_reg(nandc, NAND_FLASH_CMD, RESET_DEVICE);
770 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
772 write_reg_dma(nandc, NAND_FLASH_CMD, 1);
773 write_reg_dma(nandc, NAND_EXEC_CMD, 1);
775 read_reg_dma(nandc, NAND_FLASH_STATUS, 1);
777 return 0;
780 /* helpers to submit/free our list of dma descriptors */
781 static int submit_descs(struct qcom_nand_controller *nandc)
783 struct desc_info *desc;
784 dma_cookie_t cookie = 0;
786 list_for_each_entry(desc, &nandc->desc_list, node)
787 cookie = dmaengine_submit(desc->dma_desc);
789 if (dma_sync_wait(nandc->chan, cookie) != DMA_COMPLETE)
790 return -ETIMEDOUT;
792 return 0;
795 static void free_descs(struct qcom_nand_controller *nandc)
797 struct desc_info *desc, *n;
799 list_for_each_entry_safe(desc, n, &nandc->desc_list, node) {
800 list_del(&desc->node);
801 dma_unmap_sg(nandc->dev, &desc->sgl, 1, desc->dir);
802 kfree(desc);
806 /* reset the register read buffer for next NAND operation */
807 static void clear_read_regs(struct qcom_nand_controller *nandc)
809 nandc->reg_read_pos = 0;
810 memset(nandc->reg_read_buf, 0,
811 MAX_REG_RD * sizeof(*nandc->reg_read_buf));
814 static void pre_command(struct qcom_nand_host *host, int command)
816 struct nand_chip *chip = &host->chip;
817 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
819 nandc->buf_count = 0;
820 nandc->buf_start = 0;
821 host->use_ecc = false;
822 host->last_command = command;
824 clear_read_regs(nandc);
828 * this is called after NAND_CMD_PAGEPROG and NAND_CMD_ERASE1 to set our
829 * privately maintained status byte, this status byte can be read after
830 * NAND_CMD_STATUS is called
832 static void parse_erase_write_errors(struct qcom_nand_host *host, int command)
834 struct nand_chip *chip = &host->chip;
835 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
836 struct nand_ecc_ctrl *ecc = &chip->ecc;
837 int num_cw;
838 int i;
840 num_cw = command == NAND_CMD_PAGEPROG ? ecc->steps : 1;
842 for (i = 0; i < num_cw; i++) {
843 u32 flash_status = le32_to_cpu(nandc->reg_read_buf[i]);
845 if (flash_status & FS_MPU_ERR)
846 host->status &= ~NAND_STATUS_WP;
848 if (flash_status & FS_OP_ERR || (i == (num_cw - 1) &&
849 (flash_status &
850 FS_DEVICE_STS_ERR)))
851 host->status |= NAND_STATUS_FAIL;
855 static void post_command(struct qcom_nand_host *host, int command)
857 struct nand_chip *chip = &host->chip;
858 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
860 switch (command) {
861 case NAND_CMD_READID:
862 memcpy(nandc->data_buffer, nandc->reg_read_buf,
863 nandc->buf_count);
864 break;
865 case NAND_CMD_PAGEPROG:
866 case NAND_CMD_ERASE1:
867 parse_erase_write_errors(host, command);
868 break;
869 default:
870 break;
875 * Implements chip->cmdfunc. It's only used for a limited set of commands.
876 * The rest of the commands wouldn't be called by upper layers. For example,
877 * NAND_CMD_READOOB would never be called because we have our own versions
878 * of read_oob ops for nand_ecc_ctrl.
880 static void qcom_nandc_command(struct mtd_info *mtd, unsigned int command,
881 int column, int page_addr)
883 struct nand_chip *chip = mtd_to_nand(mtd);
884 struct qcom_nand_host *host = to_qcom_nand_host(chip);
885 struct nand_ecc_ctrl *ecc = &chip->ecc;
886 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
887 bool wait = false;
888 int ret = 0;
890 pre_command(host, command);
892 switch (command) {
893 case NAND_CMD_RESET:
894 ret = reset(host);
895 wait = true;
896 break;
898 case NAND_CMD_READID:
899 nandc->buf_count = 4;
900 ret = read_id(host, column);
901 wait = true;
902 break;
904 case NAND_CMD_PARAM:
905 ret = nandc_param(host);
906 wait = true;
907 break;
909 case NAND_CMD_ERASE1:
910 ret = erase_block(host, page_addr);
911 wait = true;
912 break;
914 case NAND_CMD_READ0:
915 /* we read the entire page for now */
916 WARN_ON(column != 0);
918 host->use_ecc = true;
919 set_address(host, 0, page_addr);
920 update_rw_regs(host, ecc->steps, true);
921 break;
923 case NAND_CMD_SEQIN:
924 WARN_ON(column != 0);
925 set_address(host, 0, page_addr);
926 break;
928 case NAND_CMD_PAGEPROG:
929 case NAND_CMD_STATUS:
930 case NAND_CMD_NONE:
931 default:
932 break;
935 if (ret) {
936 dev_err(nandc->dev, "failure executing command %d\n",
937 command);
938 free_descs(nandc);
939 return;
942 if (wait) {
943 ret = submit_descs(nandc);
944 if (ret)
945 dev_err(nandc->dev,
946 "failure submitting descs for command %d\n",
947 command);
950 free_descs(nandc);
952 post_command(host, command);
956 * when using BCH ECC, the HW flags an error in NAND_FLASH_STATUS if it read
957 * an erased CW, and reports an erased CW in NAND_ERASED_CW_DETECT_STATUS.
959 * when using RS ECC, the HW reports the same erros when reading an erased CW,
960 * but it notifies that it is an erased CW by placing special characters at
961 * certain offsets in the buffer.
963 * verify if the page is erased or not, and fix up the page for RS ECC by
964 * replacing the special characters with 0xff.
966 static bool erased_chunk_check_and_fixup(u8 *data_buf, int data_len)
968 u8 empty1, empty2;
971 * an erased page flags an error in NAND_FLASH_STATUS, check if the page
972 * is erased by looking for 0x54s at offsets 3 and 175 from the
973 * beginning of each codeword
976 empty1 = data_buf[3];
977 empty2 = data_buf[175];
980 * if the erased codework markers, if they exist override them with
981 * 0xffs
983 if ((empty1 == 0x54 && empty2 == 0xff) ||
984 (empty1 == 0xff && empty2 == 0x54)) {
985 data_buf[3] = 0xff;
986 data_buf[175] = 0xff;
990 * check if the entire chunk contains 0xffs or not. if it doesn't, then
991 * restore the original values at the special offsets
993 if (memchr_inv(data_buf, 0xff, data_len)) {
994 data_buf[3] = empty1;
995 data_buf[175] = empty2;
997 return false;
1000 return true;
1003 struct read_stats {
1004 __le32 flash;
1005 __le32 buffer;
1006 __le32 erased_cw;
1010 * reads back status registers set by the controller to notify page read
1011 * errors. this is equivalent to what 'ecc->correct()' would do.
1013 static int parse_read_errors(struct qcom_nand_host *host, u8 *data_buf,
1014 u8 *oob_buf)
1016 struct nand_chip *chip = &host->chip;
1017 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1018 struct mtd_info *mtd = nand_to_mtd(chip);
1019 struct nand_ecc_ctrl *ecc = &chip->ecc;
1020 unsigned int max_bitflips = 0;
1021 struct read_stats *buf;
1022 int i;
1024 buf = (struct read_stats *)nandc->reg_read_buf;
1026 for (i = 0; i < ecc->steps; i++, buf++) {
1027 u32 flash, buffer, erased_cw;
1028 int data_len, oob_len;
1030 if (i == (ecc->steps - 1)) {
1031 data_len = ecc->size - ((ecc->steps - 1) << 2);
1032 oob_len = ecc->steps << 2;
1033 } else {
1034 data_len = host->cw_data;
1035 oob_len = 0;
1038 flash = le32_to_cpu(buf->flash);
1039 buffer = le32_to_cpu(buf->buffer);
1040 erased_cw = le32_to_cpu(buf->erased_cw);
1042 if (flash & (FS_OP_ERR | FS_MPU_ERR)) {
1043 bool erased;
1045 /* ignore erased codeword errors */
1046 if (host->bch_enabled) {
1047 erased = (erased_cw & ERASED_CW) == ERASED_CW ?
1048 true : false;
1049 } else {
1050 erased = erased_chunk_check_and_fixup(data_buf,
1051 data_len);
1054 if (erased) {
1055 data_buf += data_len;
1056 if (oob_buf)
1057 oob_buf += oob_len + ecc->bytes;
1058 continue;
1061 if (buffer & BS_UNCORRECTABLE_BIT) {
1062 int ret, ecclen, extraooblen;
1063 void *eccbuf;
1065 eccbuf = oob_buf ? oob_buf + oob_len : NULL;
1066 ecclen = oob_buf ? host->ecc_bytes_hw : 0;
1067 extraooblen = oob_buf ? oob_len : 0;
1070 * make sure it isn't an erased page reported
1071 * as not-erased by HW because of a few bitflips
1073 ret = nand_check_erased_ecc_chunk(data_buf,
1074 data_len, eccbuf, ecclen, oob_buf,
1075 extraooblen, ecc->strength);
1076 if (ret < 0) {
1077 mtd->ecc_stats.failed++;
1078 } else {
1079 mtd->ecc_stats.corrected += ret;
1080 max_bitflips =
1081 max_t(unsigned int, max_bitflips, ret);
1084 } else {
1085 unsigned int stat;
1087 stat = buffer & BS_CORRECTABLE_ERR_MSK;
1088 mtd->ecc_stats.corrected += stat;
1089 max_bitflips = max(max_bitflips, stat);
1092 data_buf += data_len;
1093 if (oob_buf)
1094 oob_buf += oob_len + ecc->bytes;
1097 return max_bitflips;
1101 * helper to perform the actual page read operation, used by ecc->read_page(),
1102 * ecc->read_oob()
1104 static int read_page_ecc(struct qcom_nand_host *host, u8 *data_buf,
1105 u8 *oob_buf)
1107 struct nand_chip *chip = &host->chip;
1108 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1109 struct nand_ecc_ctrl *ecc = &chip->ecc;
1110 int i, ret;
1112 /* queue cmd descs for each codeword */
1113 for (i = 0; i < ecc->steps; i++) {
1114 int data_size, oob_size;
1116 if (i == (ecc->steps - 1)) {
1117 data_size = ecc->size - ((ecc->steps - 1) << 2);
1118 oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
1119 host->spare_bytes;
1120 } else {
1121 data_size = host->cw_data;
1122 oob_size = host->ecc_bytes_hw + host->spare_bytes;
1125 config_cw_read(nandc);
1127 if (data_buf)
1128 read_data_dma(nandc, FLASH_BUF_ACC, data_buf,
1129 data_size);
1132 * when ecc is enabled, the controller doesn't read the real
1133 * or dummy bad block markers in each chunk. To maintain a
1134 * consistent layout across RAW and ECC reads, we just
1135 * leave the real/dummy BBM offsets empty (i.e, filled with
1136 * 0xffs)
1138 if (oob_buf) {
1139 int j;
1141 for (j = 0; j < host->bbm_size; j++)
1142 *oob_buf++ = 0xff;
1144 read_data_dma(nandc, FLASH_BUF_ACC + data_size,
1145 oob_buf, oob_size);
1148 if (data_buf)
1149 data_buf += data_size;
1150 if (oob_buf)
1151 oob_buf += oob_size;
1154 ret = submit_descs(nandc);
1155 if (ret)
1156 dev_err(nandc->dev, "failure to read page/oob\n");
1158 free_descs(nandc);
1160 return ret;
1164 * a helper that copies the last step/codeword of a page (containing free oob)
1165 * into our local buffer
1167 static int copy_last_cw(struct qcom_nand_host *host, int page)
1169 struct nand_chip *chip = &host->chip;
1170 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1171 struct nand_ecc_ctrl *ecc = &chip->ecc;
1172 int size;
1173 int ret;
1175 clear_read_regs(nandc);
1177 size = host->use_ecc ? host->cw_data : host->cw_size;
1179 /* prepare a clean read buffer */
1180 memset(nandc->data_buffer, 0xff, size);
1182 set_address(host, host->cw_size * (ecc->steps - 1), page);
1183 update_rw_regs(host, 1, true);
1185 config_cw_read(nandc);
1187 read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer, size);
1189 ret = submit_descs(nandc);
1190 if (ret)
1191 dev_err(nandc->dev, "failed to copy last codeword\n");
1193 free_descs(nandc);
1195 return ret;
1198 /* implements ecc->read_page() */
1199 static int qcom_nandc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1200 uint8_t *buf, int oob_required, int page)
1202 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1203 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1204 u8 *data_buf, *oob_buf = NULL;
1205 int ret;
1207 data_buf = buf;
1208 oob_buf = oob_required ? chip->oob_poi : NULL;
1210 ret = read_page_ecc(host, data_buf, oob_buf);
1211 if (ret) {
1212 dev_err(nandc->dev, "failure to read page\n");
1213 return ret;
1216 return parse_read_errors(host, data_buf, oob_buf);
1219 /* implements ecc->read_page_raw() */
1220 static int qcom_nandc_read_page_raw(struct mtd_info *mtd,
1221 struct nand_chip *chip, uint8_t *buf,
1222 int oob_required, int page)
1224 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1225 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1226 u8 *data_buf, *oob_buf;
1227 struct nand_ecc_ctrl *ecc = &chip->ecc;
1228 int i, ret;
1230 data_buf = buf;
1231 oob_buf = chip->oob_poi;
1233 host->use_ecc = false;
1234 update_rw_regs(host, ecc->steps, true);
1236 for (i = 0; i < ecc->steps; i++) {
1237 int data_size1, data_size2, oob_size1, oob_size2;
1238 int reg_off = FLASH_BUF_ACC;
1240 data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
1241 oob_size1 = host->bbm_size;
1243 if (i == (ecc->steps - 1)) {
1244 data_size2 = ecc->size - data_size1 -
1245 ((ecc->steps - 1) << 2);
1246 oob_size2 = (ecc->steps << 2) + host->ecc_bytes_hw +
1247 host->spare_bytes;
1248 } else {
1249 data_size2 = host->cw_data - data_size1;
1250 oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
1253 config_cw_read(nandc);
1255 read_data_dma(nandc, reg_off, data_buf, data_size1);
1256 reg_off += data_size1;
1257 data_buf += data_size1;
1259 read_data_dma(nandc, reg_off, oob_buf, oob_size1);
1260 reg_off += oob_size1;
1261 oob_buf += oob_size1;
1263 read_data_dma(nandc, reg_off, data_buf, data_size2);
1264 reg_off += data_size2;
1265 data_buf += data_size2;
1267 read_data_dma(nandc, reg_off, oob_buf, oob_size2);
1268 oob_buf += oob_size2;
1271 ret = submit_descs(nandc);
1272 if (ret)
1273 dev_err(nandc->dev, "failure to read raw page\n");
1275 free_descs(nandc);
1277 return 0;
1280 /* implements ecc->read_oob() */
1281 static int qcom_nandc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
1282 int page)
1284 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1285 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1286 struct nand_ecc_ctrl *ecc = &chip->ecc;
1287 int ret;
1289 clear_read_regs(nandc);
1291 host->use_ecc = true;
1292 set_address(host, 0, page);
1293 update_rw_regs(host, ecc->steps, true);
1295 ret = read_page_ecc(host, NULL, chip->oob_poi);
1296 if (ret)
1297 dev_err(nandc->dev, "failure to read oob\n");
1299 return ret;
1302 /* implements ecc->write_page() */
1303 static int qcom_nandc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1304 const uint8_t *buf, int oob_required, int page)
1306 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1307 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1308 struct nand_ecc_ctrl *ecc = &chip->ecc;
1309 u8 *data_buf, *oob_buf;
1310 int i, ret;
1312 clear_read_regs(nandc);
1314 data_buf = (u8 *)buf;
1315 oob_buf = chip->oob_poi;
1317 host->use_ecc = true;
1318 update_rw_regs(host, ecc->steps, false);
1320 for (i = 0; i < ecc->steps; i++) {
1321 int data_size, oob_size;
1323 if (i == (ecc->steps - 1)) {
1324 data_size = ecc->size - ((ecc->steps - 1) << 2);
1325 oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
1326 host->spare_bytes;
1327 } else {
1328 data_size = host->cw_data;
1329 oob_size = ecc->bytes;
1332 config_cw_write_pre(nandc);
1334 write_data_dma(nandc, FLASH_BUF_ACC, data_buf, data_size);
1337 * when ECC is enabled, we don't really need to write anything
1338 * to oob for the first n - 1 codewords since these oob regions
1339 * just contain ECC bytes that's written by the controller
1340 * itself. For the last codeword, we skip the bbm positions and
1341 * write to the free oob area.
1343 if (i == (ecc->steps - 1)) {
1344 oob_buf += host->bbm_size;
1346 write_data_dma(nandc, FLASH_BUF_ACC + data_size,
1347 oob_buf, oob_size);
1350 config_cw_write_post(nandc);
1352 data_buf += data_size;
1353 oob_buf += oob_size;
1356 ret = submit_descs(nandc);
1357 if (ret)
1358 dev_err(nandc->dev, "failure to write page\n");
1360 free_descs(nandc);
1362 return ret;
1365 /* implements ecc->write_page_raw() */
1366 static int qcom_nandc_write_page_raw(struct mtd_info *mtd,
1367 struct nand_chip *chip, const uint8_t *buf,
1368 int oob_required, int page)
1370 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1371 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1372 struct nand_ecc_ctrl *ecc = &chip->ecc;
1373 u8 *data_buf, *oob_buf;
1374 int i, ret;
1376 clear_read_regs(nandc);
1378 data_buf = (u8 *)buf;
1379 oob_buf = chip->oob_poi;
1381 host->use_ecc = false;
1382 update_rw_regs(host, ecc->steps, false);
1384 for (i = 0; i < ecc->steps; i++) {
1385 int data_size1, data_size2, oob_size1, oob_size2;
1386 int reg_off = FLASH_BUF_ACC;
1388 data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
1389 oob_size1 = host->bbm_size;
1391 if (i == (ecc->steps - 1)) {
1392 data_size2 = ecc->size - data_size1 -
1393 ((ecc->steps - 1) << 2);
1394 oob_size2 = (ecc->steps << 2) + host->ecc_bytes_hw +
1395 host->spare_bytes;
1396 } else {
1397 data_size2 = host->cw_data - data_size1;
1398 oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
1401 config_cw_write_pre(nandc);
1403 write_data_dma(nandc, reg_off, data_buf, data_size1);
1404 reg_off += data_size1;
1405 data_buf += data_size1;
1407 write_data_dma(nandc, reg_off, oob_buf, oob_size1);
1408 reg_off += oob_size1;
1409 oob_buf += oob_size1;
1411 write_data_dma(nandc, reg_off, data_buf, data_size2);
1412 reg_off += data_size2;
1413 data_buf += data_size2;
1415 write_data_dma(nandc, reg_off, oob_buf, oob_size2);
1416 oob_buf += oob_size2;
1418 config_cw_write_post(nandc);
1421 ret = submit_descs(nandc);
1422 if (ret)
1423 dev_err(nandc->dev, "failure to write raw page\n");
1425 free_descs(nandc);
1427 return ret;
1431 * implements ecc->write_oob()
1433 * the NAND controller cannot write only data or only oob within a codeword,
1434 * since ecc is calculated for the combined codeword. we first copy the
1435 * entire contents for the last codeword(data + oob), replace the old oob
1436 * with the new one in chip->oob_poi, and then write the entire codeword.
1437 * this read-copy-write operation results in a slight performance loss.
1439 static int qcom_nandc_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
1440 int page)
1442 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1443 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1444 struct nand_ecc_ctrl *ecc = &chip->ecc;
1445 u8 *oob = chip->oob_poi;
1446 int data_size, oob_size;
1447 int ret, status = 0;
1449 host->use_ecc = true;
1451 ret = copy_last_cw(host, page);
1452 if (ret)
1453 return ret;
1455 clear_read_regs(nandc);
1457 /* calculate the data and oob size for the last codeword/step */
1458 data_size = ecc->size - ((ecc->steps - 1) << 2);
1459 oob_size = mtd->oobavail;
1461 /* override new oob content to last codeword */
1462 mtd_ooblayout_get_databytes(mtd, nandc->data_buffer + data_size, oob,
1463 0, mtd->oobavail);
1465 set_address(host, host->cw_size * (ecc->steps - 1), page);
1466 update_rw_regs(host, 1, false);
1468 config_cw_write_pre(nandc);
1469 write_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer,
1470 data_size + oob_size);
1471 config_cw_write_post(nandc);
1473 ret = submit_descs(nandc);
1475 free_descs(nandc);
1477 if (ret) {
1478 dev_err(nandc->dev, "failure to write oob\n");
1479 return -EIO;
1482 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1484 status = chip->waitfunc(mtd, chip);
1486 return status & NAND_STATUS_FAIL ? -EIO : 0;
1489 static int qcom_nandc_block_bad(struct mtd_info *mtd, loff_t ofs)
1491 struct nand_chip *chip = mtd_to_nand(mtd);
1492 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1493 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1494 struct nand_ecc_ctrl *ecc = &chip->ecc;
1495 int page, ret, bbpos, bad = 0;
1496 u32 flash_status;
1498 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
1501 * configure registers for a raw sub page read, the address is set to
1502 * the beginning of the last codeword, we don't care about reading ecc
1503 * portion of oob. we just want the first few bytes from this codeword
1504 * that contains the BBM
1506 host->use_ecc = false;
1508 ret = copy_last_cw(host, page);
1509 if (ret)
1510 goto err;
1512 flash_status = le32_to_cpu(nandc->reg_read_buf[0]);
1514 if (flash_status & (FS_OP_ERR | FS_MPU_ERR)) {
1515 dev_warn(nandc->dev, "error when trying to read BBM\n");
1516 goto err;
1519 bbpos = mtd->writesize - host->cw_size * (ecc->steps - 1);
1521 bad = nandc->data_buffer[bbpos] != 0xff;
1523 if (chip->options & NAND_BUSWIDTH_16)
1524 bad = bad || (nandc->data_buffer[bbpos + 1] != 0xff);
1525 err:
1526 return bad;
1529 static int qcom_nandc_block_markbad(struct mtd_info *mtd, loff_t ofs)
1531 struct nand_chip *chip = mtd_to_nand(mtd);
1532 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1533 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1534 struct nand_ecc_ctrl *ecc = &chip->ecc;
1535 int page, ret, status = 0;
1537 clear_read_regs(nandc);
1540 * to mark the BBM as bad, we flash the entire last codeword with 0s.
1541 * we don't care about the rest of the content in the codeword since
1542 * we aren't going to use this block again
1544 memset(nandc->data_buffer, 0x00, host->cw_size);
1546 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
1548 /* prepare write */
1549 host->use_ecc = false;
1550 set_address(host, host->cw_size * (ecc->steps - 1), page);
1551 update_rw_regs(host, 1, false);
1553 config_cw_write_pre(nandc);
1554 write_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer, host->cw_size);
1555 config_cw_write_post(nandc);
1557 ret = submit_descs(nandc);
1559 free_descs(nandc);
1561 if (ret) {
1562 dev_err(nandc->dev, "failure to update BBM\n");
1563 return -EIO;
1566 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1568 status = chip->waitfunc(mtd, chip);
1570 return status & NAND_STATUS_FAIL ? -EIO : 0;
1574 * the three functions below implement chip->read_byte(), chip->read_buf()
1575 * and chip->write_buf() respectively. these aren't used for
1576 * reading/writing page data, they are used for smaller data like reading
1577 * id, status etc
1579 static uint8_t qcom_nandc_read_byte(struct mtd_info *mtd)
1581 struct nand_chip *chip = mtd_to_nand(mtd);
1582 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1583 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1584 u8 *buf = nandc->data_buffer;
1585 u8 ret = 0x0;
1587 if (host->last_command == NAND_CMD_STATUS) {
1588 ret = host->status;
1590 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
1592 return ret;
1595 if (nandc->buf_start < nandc->buf_count)
1596 ret = buf[nandc->buf_start++];
1598 return ret;
1601 static void qcom_nandc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
1603 struct nand_chip *chip = mtd_to_nand(mtd);
1604 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1605 int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
1607 memcpy(buf, nandc->data_buffer + nandc->buf_start, real_len);
1608 nandc->buf_start += real_len;
1611 static void qcom_nandc_write_buf(struct mtd_info *mtd, const uint8_t *buf,
1612 int len)
1614 struct nand_chip *chip = mtd_to_nand(mtd);
1615 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1616 int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
1618 memcpy(nandc->data_buffer + nandc->buf_start, buf, real_len);
1620 nandc->buf_start += real_len;
1623 /* we support only one external chip for now */
1624 static void qcom_nandc_select_chip(struct mtd_info *mtd, int chipnr)
1626 struct nand_chip *chip = mtd_to_nand(mtd);
1627 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1629 if (chipnr <= 0)
1630 return;
1632 dev_warn(nandc->dev, "invalid chip select\n");
1636 * NAND controller page layout info
1638 * Layout with ECC enabled:
1640 * |----------------------| |---------------------------------|
1641 * | xx.......yy| | *********xx.......yy|
1642 * | DATA xx..ECC..yy| | DATA **SPARE**xx..ECC..yy|
1643 * | (516) xx.......yy| | (516-n*4) **(n*4)**xx.......yy|
1644 * | xx.......yy| | *********xx.......yy|
1645 * |----------------------| |---------------------------------|
1646 * codeword 1,2..n-1 codeword n
1647 * <---(528/532 Bytes)--> <-------(528/532 Bytes)--------->
1649 * n = Number of codewords in the page
1650 * . = ECC bytes
1651 * * = Spare/free bytes
1652 * x = Unused byte(s)
1653 * y = Reserved byte(s)
1655 * 2K page: n = 4, spare = 16 bytes
1656 * 4K page: n = 8, spare = 32 bytes
1657 * 8K page: n = 16, spare = 64 bytes
1659 * the qcom nand controller operates at a sub page/codeword level. each
1660 * codeword is 528 and 532 bytes for 4 bit and 8 bit ECC modes respectively.
1661 * the number of ECC bytes vary based on the ECC strength and the bus width.
1663 * the first n - 1 codewords contains 516 bytes of user data, the remaining
1664 * 12/16 bytes consist of ECC and reserved data. The nth codeword contains
1665 * both user data and spare(oobavail) bytes that sum up to 516 bytes.
1667 * When we access a page with ECC enabled, the reserved bytes(s) are not
1668 * accessible at all. When reading, we fill up these unreadable positions
1669 * with 0xffs. When writing, the controller skips writing the inaccessible
1670 * bytes.
1672 * Layout with ECC disabled:
1674 * |------------------------------| |---------------------------------------|
1675 * | yy xx.......| | bb *********xx.......|
1676 * | DATA1 yy DATA2 xx..ECC..| | DATA1 bb DATA2 **SPARE**xx..ECC..|
1677 * | (size1) yy (size2) xx.......| | (size1) bb (size2) **(n*4)**xx.......|
1678 * | yy xx.......| | bb *********xx.......|
1679 * |------------------------------| |---------------------------------------|
1680 * codeword 1,2..n-1 codeword n
1681 * <-------(528/532 Bytes)------> <-----------(528/532 Bytes)----------->
1683 * n = Number of codewords in the page
1684 * . = ECC bytes
1685 * * = Spare/free bytes
1686 * x = Unused byte(s)
1687 * y = Dummy Bad Bock byte(s)
1688 * b = Real Bad Block byte(s)
1689 * size1/size2 = function of codeword size and 'n'
1691 * when the ECC block is disabled, one reserved byte (or two for 16 bit bus
1692 * width) is now accessible. For the first n - 1 codewords, these are dummy Bad
1693 * Block Markers. In the last codeword, this position contains the real BBM
1695 * In order to have a consistent layout between RAW and ECC modes, we assume
1696 * the following OOB layout arrangement:
1698 * |-----------| |--------------------|
1699 * |yyxx.......| |bb*********xx.......|
1700 * |yyxx..ECC..| |bb*FREEOOB*xx..ECC..|
1701 * |yyxx.......| |bb*********xx.......|
1702 * |yyxx.......| |bb*********xx.......|
1703 * |-----------| |--------------------|
1704 * first n - 1 nth OOB region
1705 * OOB regions
1707 * n = Number of codewords in the page
1708 * . = ECC bytes
1709 * * = FREE OOB bytes
1710 * y = Dummy bad block byte(s) (inaccessible when ECC enabled)
1711 * x = Unused byte(s)
1712 * b = Real bad block byte(s) (inaccessible when ECC enabled)
1714 * This layout is read as is when ECC is disabled. When ECC is enabled, the
1715 * inaccessible Bad Block byte(s) are ignored when we write to a page/oob,
1716 * and assumed as 0xffs when we read a page/oob. The ECC, unused and
1717 * dummy/real bad block bytes are grouped as ecc bytes (i.e, ecc->bytes is
1718 * the sum of the three).
1720 static int qcom_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
1721 struct mtd_oob_region *oobregion)
1723 struct nand_chip *chip = mtd_to_nand(mtd);
1724 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1725 struct nand_ecc_ctrl *ecc = &chip->ecc;
1727 if (section > 1)
1728 return -ERANGE;
1730 if (!section) {
1731 oobregion->length = (ecc->bytes * (ecc->steps - 1)) +
1732 host->bbm_size;
1733 oobregion->offset = 0;
1734 } else {
1735 oobregion->length = host->ecc_bytes_hw + host->spare_bytes;
1736 oobregion->offset = mtd->oobsize - oobregion->length;
1739 return 0;
1742 static int qcom_nand_ooblayout_free(struct mtd_info *mtd, int section,
1743 struct mtd_oob_region *oobregion)
1745 struct nand_chip *chip = mtd_to_nand(mtd);
1746 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1747 struct nand_ecc_ctrl *ecc = &chip->ecc;
1749 if (section)
1750 return -ERANGE;
1752 oobregion->length = ecc->steps * 4;
1753 oobregion->offset = ((ecc->steps - 1) * ecc->bytes) + host->bbm_size;
1755 return 0;
1758 static const struct mtd_ooblayout_ops qcom_nand_ooblayout_ops = {
1759 .ecc = qcom_nand_ooblayout_ecc,
1760 .free = qcom_nand_ooblayout_free,
1763 static int qcom_nand_host_setup(struct qcom_nand_host *host)
1765 struct nand_chip *chip = &host->chip;
1766 struct mtd_info *mtd = nand_to_mtd(chip);
1767 struct nand_ecc_ctrl *ecc = &chip->ecc;
1768 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1769 int cwperpage, bad_block_byte;
1770 bool wide_bus;
1771 int ecc_mode = 1;
1774 * the controller requires each step consists of 512 bytes of data.
1775 * bail out if DT has populated a wrong step size.
1777 if (ecc->size != NANDC_STEP_SIZE) {
1778 dev_err(nandc->dev, "invalid ecc size\n");
1779 return -EINVAL;
1782 wide_bus = chip->options & NAND_BUSWIDTH_16 ? true : false;
1784 if (ecc->strength >= 8) {
1785 /* 8 bit ECC defaults to BCH ECC on all platforms */
1786 host->bch_enabled = true;
1787 ecc_mode = 1;
1789 if (wide_bus) {
1790 host->ecc_bytes_hw = 14;
1791 host->spare_bytes = 0;
1792 host->bbm_size = 2;
1793 } else {
1794 host->ecc_bytes_hw = 13;
1795 host->spare_bytes = 2;
1796 host->bbm_size = 1;
1798 } else {
1800 * if the controller supports BCH for 4 bit ECC, the controller
1801 * uses lesser bytes for ECC. If RS is used, the ECC bytes is
1802 * always 10 bytes
1804 if (nandc->ecc_modes & ECC_BCH_4BIT) {
1805 /* BCH */
1806 host->bch_enabled = true;
1807 ecc_mode = 0;
1809 if (wide_bus) {
1810 host->ecc_bytes_hw = 8;
1811 host->spare_bytes = 2;
1812 host->bbm_size = 2;
1813 } else {
1814 host->ecc_bytes_hw = 7;
1815 host->spare_bytes = 4;
1816 host->bbm_size = 1;
1818 } else {
1819 /* RS */
1820 host->ecc_bytes_hw = 10;
1822 if (wide_bus) {
1823 host->spare_bytes = 0;
1824 host->bbm_size = 2;
1825 } else {
1826 host->spare_bytes = 1;
1827 host->bbm_size = 1;
1833 * we consider ecc->bytes as the sum of all the non-data content in a
1834 * step. It gives us a clean representation of the oob area (even if
1835 * all the bytes aren't used for ECC).It is always 16 bytes for 8 bit
1836 * ECC and 12 bytes for 4 bit ECC
1838 ecc->bytes = host->ecc_bytes_hw + host->spare_bytes + host->bbm_size;
1840 ecc->read_page = qcom_nandc_read_page;
1841 ecc->read_page_raw = qcom_nandc_read_page_raw;
1842 ecc->read_oob = qcom_nandc_read_oob;
1843 ecc->write_page = qcom_nandc_write_page;
1844 ecc->write_page_raw = qcom_nandc_write_page_raw;
1845 ecc->write_oob = qcom_nandc_write_oob;
1847 ecc->mode = NAND_ECC_HW;
1849 mtd_set_ooblayout(mtd, &qcom_nand_ooblayout_ops);
1851 cwperpage = mtd->writesize / ecc->size;
1854 * DATA_UD_BYTES varies based on whether the read/write command protects
1855 * spare data with ECC too. We protect spare data by default, so we set
1856 * it to main + spare data, which are 512 and 4 bytes respectively.
1858 host->cw_data = 516;
1861 * total bytes in a step, either 528 bytes for 4 bit ECC, or 532 bytes
1862 * for 8 bit ECC
1864 host->cw_size = host->cw_data + ecc->bytes;
1866 if (ecc->bytes * (mtd->writesize / ecc->size) > mtd->oobsize) {
1867 dev_err(nandc->dev, "ecc data doesn't fit in OOB area\n");
1868 return -EINVAL;
1871 bad_block_byte = mtd->writesize - host->cw_size * (cwperpage - 1) + 1;
1873 host->cfg0 = (cwperpage - 1) << CW_PER_PAGE
1874 | host->cw_data << UD_SIZE_BYTES
1875 | 0 << DISABLE_STATUS_AFTER_WRITE
1876 | 5 << NUM_ADDR_CYCLES
1877 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_RS
1878 | 0 << STATUS_BFR_READ
1879 | 1 << SET_RD_MODE_AFTER_STATUS
1880 | host->spare_bytes << SPARE_SIZE_BYTES;
1882 host->cfg1 = 7 << NAND_RECOVERY_CYCLES
1883 | 0 << CS_ACTIVE_BSY
1884 | bad_block_byte << BAD_BLOCK_BYTE_NUM
1885 | 0 << BAD_BLOCK_IN_SPARE_AREA
1886 | 2 << WR_RD_BSY_GAP
1887 | wide_bus << WIDE_FLASH
1888 | host->bch_enabled << ENABLE_BCH_ECC;
1890 host->cfg0_raw = (cwperpage - 1) << CW_PER_PAGE
1891 | host->cw_size << UD_SIZE_BYTES
1892 | 5 << NUM_ADDR_CYCLES
1893 | 0 << SPARE_SIZE_BYTES;
1895 host->cfg1_raw = 7 << NAND_RECOVERY_CYCLES
1896 | 0 << CS_ACTIVE_BSY
1897 | 17 << BAD_BLOCK_BYTE_NUM
1898 | 1 << BAD_BLOCK_IN_SPARE_AREA
1899 | 2 << WR_RD_BSY_GAP
1900 | wide_bus << WIDE_FLASH
1901 | 1 << DEV0_CFG1_ECC_DISABLE;
1903 host->ecc_bch_cfg = !host->bch_enabled << ECC_CFG_ECC_DISABLE
1904 | 0 << ECC_SW_RESET
1905 | host->cw_data << ECC_NUM_DATA_BYTES
1906 | 1 << ECC_FORCE_CLK_OPEN
1907 | ecc_mode << ECC_MODE
1908 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_BCH;
1910 host->ecc_buf_cfg = 0x203 << NUM_STEPS;
1912 host->clrflashstatus = FS_READY_BSY_N;
1913 host->clrreadstatus = 0xc0;
1915 dev_dbg(nandc->dev,
1916 "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",
1917 host->cfg0, host->cfg1, host->ecc_buf_cfg, host->ecc_bch_cfg,
1918 host->cw_size, host->cw_data, ecc->strength, ecc->bytes,
1919 cwperpage);
1921 return 0;
1924 static int qcom_nandc_alloc(struct qcom_nand_controller *nandc)
1926 int ret;
1928 ret = dma_set_coherent_mask(nandc->dev, DMA_BIT_MASK(32));
1929 if (ret) {
1930 dev_err(nandc->dev, "failed to set DMA mask\n");
1931 return ret;
1935 * we use the internal buffer for reading ONFI params, reading small
1936 * data like ID and status, and preforming read-copy-write operations
1937 * when writing to a codeword partially. 532 is the maximum possible
1938 * size of a codeword for our nand controller
1940 nandc->buf_size = 532;
1942 nandc->data_buffer = devm_kzalloc(nandc->dev, nandc->buf_size,
1943 GFP_KERNEL);
1944 if (!nandc->data_buffer)
1945 return -ENOMEM;
1947 nandc->regs = devm_kzalloc(nandc->dev, sizeof(*nandc->regs),
1948 GFP_KERNEL);
1949 if (!nandc->regs)
1950 return -ENOMEM;
1952 nandc->reg_read_buf = devm_kzalloc(nandc->dev,
1953 MAX_REG_RD * sizeof(*nandc->reg_read_buf),
1954 GFP_KERNEL);
1955 if (!nandc->reg_read_buf)
1956 return -ENOMEM;
1958 nandc->chan = dma_request_slave_channel(nandc->dev, "rxtx");
1959 if (!nandc->chan) {
1960 dev_err(nandc->dev, "failed to request slave channel\n");
1961 return -ENODEV;
1964 INIT_LIST_HEAD(&nandc->desc_list);
1965 INIT_LIST_HEAD(&nandc->host_list);
1967 nand_hw_control_init(&nandc->controller);
1969 return 0;
1972 static void qcom_nandc_unalloc(struct qcom_nand_controller *nandc)
1974 dma_release_channel(nandc->chan);
1977 /* one time setup of a few nand controller registers */
1978 static int qcom_nandc_setup(struct qcom_nand_controller *nandc)
1980 /* kill onenand */
1981 nandc_write(nandc, SFLASHC_BURST_CFG, 0);
1982 nandc_write(nandc, NAND_DEV_CMD_VLD, NAND_DEV_CMD_VLD_VAL);
1984 /* enable ADM DMA */
1985 nandc_write(nandc, NAND_FLASH_CHIP_SELECT, DM_EN);
1987 /* save the original values of these registers */
1988 nandc->cmd1 = nandc_read(nandc, NAND_DEV_CMD1);
1989 nandc->vld = NAND_DEV_CMD_VLD_VAL;
1991 return 0;
1994 static int qcom_nand_host_init(struct qcom_nand_controller *nandc,
1995 struct qcom_nand_host *host,
1996 struct device_node *dn)
1998 struct nand_chip *chip = &host->chip;
1999 struct mtd_info *mtd = nand_to_mtd(chip);
2000 struct device *dev = nandc->dev;
2001 int ret;
2003 ret = of_property_read_u32(dn, "reg", &host->cs);
2004 if (ret) {
2005 dev_err(dev, "can't get chip-select\n");
2006 return -ENXIO;
2009 nand_set_flash_node(chip, dn);
2010 mtd->name = devm_kasprintf(dev, GFP_KERNEL, "qcom_nand.%d", host->cs);
2011 mtd->owner = THIS_MODULE;
2012 mtd->dev.parent = dev;
2014 chip->cmdfunc = qcom_nandc_command;
2015 chip->select_chip = qcom_nandc_select_chip;
2016 chip->read_byte = qcom_nandc_read_byte;
2017 chip->read_buf = qcom_nandc_read_buf;
2018 chip->write_buf = qcom_nandc_write_buf;
2021 * the bad block marker is readable only when we read the last codeword
2022 * of a page with ECC disabled. currently, the nand_base and nand_bbt
2023 * helpers don't allow us to read BB from a nand chip with ECC
2024 * disabled (MTD_OPS_PLACE_OOB is set by default). use the block_bad
2025 * and block_markbad helpers until we permanently switch to using
2026 * MTD_OPS_RAW for all drivers (with the help of badblockbits)
2028 chip->block_bad = qcom_nandc_block_bad;
2029 chip->block_markbad = qcom_nandc_block_markbad;
2031 chip->controller = &nandc->controller;
2032 chip->options |= NAND_NO_SUBPAGE_WRITE | NAND_USE_BOUNCE_BUFFER |
2033 NAND_SKIP_BBTSCAN;
2035 /* set up initial status value */
2036 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
2038 ret = nand_scan_ident(mtd, 1, NULL);
2039 if (ret)
2040 return ret;
2042 ret = qcom_nand_host_setup(host);
2043 if (ret)
2044 return ret;
2046 ret = nand_scan_tail(mtd);
2047 if (ret)
2048 return ret;
2050 return mtd_device_register(mtd, NULL, 0);
2053 /* parse custom DT properties here */
2054 static int qcom_nandc_parse_dt(struct platform_device *pdev)
2056 struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
2057 struct device_node *np = nandc->dev->of_node;
2058 int ret;
2060 ret = of_property_read_u32(np, "qcom,cmd-crci", &nandc->cmd_crci);
2061 if (ret) {
2062 dev_err(nandc->dev, "command CRCI unspecified\n");
2063 return ret;
2066 ret = of_property_read_u32(np, "qcom,data-crci", &nandc->data_crci);
2067 if (ret) {
2068 dev_err(nandc->dev, "data CRCI unspecified\n");
2069 return ret;
2072 return 0;
2075 static int qcom_nandc_probe(struct platform_device *pdev)
2077 struct qcom_nand_controller *nandc;
2078 struct qcom_nand_host *host;
2079 const void *dev_data;
2080 struct device *dev = &pdev->dev;
2081 struct device_node *dn = dev->of_node, *child;
2082 struct resource *res;
2083 int ret;
2085 nandc = devm_kzalloc(&pdev->dev, sizeof(*nandc), GFP_KERNEL);
2086 if (!nandc)
2087 return -ENOMEM;
2089 platform_set_drvdata(pdev, nandc);
2090 nandc->dev = dev;
2092 dev_data = of_device_get_match_data(dev);
2093 if (!dev_data) {
2094 dev_err(&pdev->dev, "failed to get device data\n");
2095 return -ENODEV;
2098 nandc->ecc_modes = (unsigned long)dev_data;
2100 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2101 nandc->base = devm_ioremap_resource(dev, res);
2102 if (IS_ERR(nandc->base))
2103 return PTR_ERR(nandc->base);
2105 nandc->base_dma = phys_to_dma(dev, (phys_addr_t)res->start);
2107 nandc->core_clk = devm_clk_get(dev, "core");
2108 if (IS_ERR(nandc->core_clk))
2109 return PTR_ERR(nandc->core_clk);
2111 nandc->aon_clk = devm_clk_get(dev, "aon");
2112 if (IS_ERR(nandc->aon_clk))
2113 return PTR_ERR(nandc->aon_clk);
2115 ret = qcom_nandc_parse_dt(pdev);
2116 if (ret)
2117 return ret;
2119 ret = qcom_nandc_alloc(nandc);
2120 if (ret)
2121 return ret;
2123 ret = clk_prepare_enable(nandc->core_clk);
2124 if (ret)
2125 goto err_core_clk;
2127 ret = clk_prepare_enable(nandc->aon_clk);
2128 if (ret)
2129 goto err_aon_clk;
2131 ret = qcom_nandc_setup(nandc);
2132 if (ret)
2133 goto err_setup;
2135 for_each_available_child_of_node(dn, child) {
2136 if (of_device_is_compatible(child, "qcom,nandcs")) {
2137 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
2138 if (!host) {
2139 of_node_put(child);
2140 ret = -ENOMEM;
2141 goto err_cs_init;
2144 ret = qcom_nand_host_init(nandc, host, child);
2145 if (ret) {
2146 devm_kfree(dev, host);
2147 continue;
2150 list_add_tail(&host->node, &nandc->host_list);
2154 if (list_empty(&nandc->host_list)) {
2155 ret = -ENODEV;
2156 goto err_cs_init;
2159 return 0;
2161 err_cs_init:
2162 list_for_each_entry(host, &nandc->host_list, node)
2163 nand_release(nand_to_mtd(&host->chip));
2164 err_setup:
2165 clk_disable_unprepare(nandc->aon_clk);
2166 err_aon_clk:
2167 clk_disable_unprepare(nandc->core_clk);
2168 err_core_clk:
2169 qcom_nandc_unalloc(nandc);
2171 return ret;
2174 static int qcom_nandc_remove(struct platform_device *pdev)
2176 struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
2177 struct qcom_nand_host *host;
2179 list_for_each_entry(host, &nandc->host_list, node)
2180 nand_release(nand_to_mtd(&host->chip));
2182 qcom_nandc_unalloc(nandc);
2184 clk_disable_unprepare(nandc->aon_clk);
2185 clk_disable_unprepare(nandc->core_clk);
2187 return 0;
2190 #define EBI2_NANDC_ECC_MODES (ECC_RS_4BIT | ECC_BCH_8BIT)
2193 * data will hold a struct pointer containing more differences once we support
2194 * more controller variants
2196 static const struct of_device_id qcom_nandc_of_match[] = {
2197 { .compatible = "qcom,ipq806x-nand",
2198 .data = (void *)EBI2_NANDC_ECC_MODES,
2202 MODULE_DEVICE_TABLE(of, qcom_nandc_of_match);
2204 static struct platform_driver qcom_nandc_driver = {
2205 .driver = {
2206 .name = "qcom-nandc",
2207 .of_match_table = qcom_nandc_of_match,
2209 .probe = qcom_nandc_probe,
2210 .remove = qcom_nandc_remove,
2212 module_platform_driver(qcom_nandc_driver);
2214 MODULE_AUTHOR("Archit Taneja <architt@codeaurora.org>");
2215 MODULE_DESCRIPTION("Qualcomm NAND Controller driver");
2216 MODULE_LICENSE("GPL v2");