1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2016, The Linux Foundation. All rights reserved.
7 #include <linux/slab.h>
8 #include <linux/bitops.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/dmaengine.h>
11 #include <linux/module.h>
12 #include <linux/mtd/rawnand.h>
13 #include <linux/mtd/partitions.h>
15 #include <linux/of_device.h>
16 #include <linux/delay.h>
17 #include <linux/dma/qcom_bam_dma.h>
19 /* NANDc reg offsets */
20 #define NAND_FLASH_CMD 0x00
21 #define NAND_ADDR0 0x04
22 #define NAND_ADDR1 0x08
23 #define NAND_FLASH_CHIP_SELECT 0x0c
24 #define NAND_EXEC_CMD 0x10
25 #define NAND_FLASH_STATUS 0x14
26 #define NAND_BUFFER_STATUS 0x18
27 #define NAND_DEV0_CFG0 0x20
28 #define NAND_DEV0_CFG1 0x24
29 #define NAND_DEV0_ECC_CFG 0x28
30 #define NAND_AUTO_STATUS_EN 0x2c
31 #define NAND_DEV1_CFG0 0x30
32 #define NAND_DEV1_CFG1 0x34
33 #define NAND_READ_ID 0x40
34 #define NAND_READ_STATUS 0x44
35 #define NAND_DEV_CMD0 0xa0
36 #define NAND_DEV_CMD1 0xa4
37 #define NAND_DEV_CMD2 0xa8
38 #define NAND_DEV_CMD_VLD 0xac
39 #define SFLASHC_BURST_CFG 0xe0
40 #define NAND_ERASED_CW_DETECT_CFG 0xe8
41 #define NAND_ERASED_CW_DETECT_STATUS 0xec
42 #define NAND_EBI2_ECC_BUF_CFG 0xf0
43 #define FLASH_BUF_ACC 0x100
45 #define NAND_CTRL 0xf00
46 #define NAND_VERSION 0xf08
47 #define NAND_READ_LOCATION_0 0xf20
48 #define NAND_READ_LOCATION_1 0xf24
49 #define NAND_READ_LOCATION_2 0xf28
50 #define NAND_READ_LOCATION_3 0xf2c
51 #define NAND_READ_LOCATION_LAST_CW_0 0xf40
52 #define NAND_READ_LOCATION_LAST_CW_1 0xf44
53 #define NAND_READ_LOCATION_LAST_CW_2 0xf48
54 #define NAND_READ_LOCATION_LAST_CW_3 0xf4c
56 /* dummy register offsets, used by write_reg_dma */
57 #define NAND_DEV_CMD1_RESTORE 0xdead
58 #define NAND_DEV_CMD_VLD_RESTORE 0xbeef
60 /* NAND_FLASH_CMD bits */
61 #define PAGE_ACC BIT(4)
62 #define LAST_PAGE BIT(5)
64 /* NAND_FLASH_CHIP_SELECT bits */
65 #define NAND_DEV_SEL 0
68 /* NAND_FLASH_STATUS bits */
69 #define FS_OP_ERR BIT(4)
70 #define FS_READY_BSY_N BIT(5)
71 #define FS_MPU_ERR BIT(8)
72 #define FS_DEVICE_STS_ERR BIT(16)
73 #define FS_DEVICE_WP BIT(23)
75 /* NAND_BUFFER_STATUS bits */
76 #define BS_UNCORRECTABLE_BIT BIT(8)
77 #define BS_CORRECTABLE_ERR_MSK 0x1f
79 /* NAND_DEVn_CFG0 bits */
80 #define DISABLE_STATUS_AFTER_WRITE 4
82 #define UD_SIZE_BYTES 9
83 #define ECC_PARITY_SIZE_BYTES_RS 19
84 #define SPARE_SIZE_BYTES 23
85 #define NUM_ADDR_CYCLES 27
86 #define STATUS_BFR_READ 30
87 #define SET_RD_MODE_AFTER_STATUS 31
89 /* NAND_DEVn_CFG0 bits */
90 #define DEV0_CFG1_ECC_DISABLE 0
92 #define NAND_RECOVERY_CYCLES 2
93 #define CS_ACTIVE_BSY 5
94 #define BAD_BLOCK_BYTE_NUM 6
95 #define BAD_BLOCK_IN_SPARE_AREA 16
96 #define WR_RD_BSY_GAP 17
97 #define ENABLE_BCH_ECC 27
99 /* NAND_DEV0_ECC_CFG bits */
100 #define ECC_CFG_ECC_DISABLE 0
101 #define ECC_SW_RESET 1
103 #define ECC_PARITY_SIZE_BYTES_BCH 8
104 #define ECC_NUM_DATA_BYTES 16
105 #define ECC_FORCE_CLK_OPEN 30
107 /* NAND_DEV_CMD1 bits */
110 /* NAND_DEV_CMD_VLD bits */
111 #define READ_START_VLD BIT(0)
112 #define READ_STOP_VLD BIT(1)
113 #define WRITE_START_VLD BIT(2)
114 #define ERASE_START_VLD BIT(3)
115 #define SEQ_READ_START_VLD BIT(4)
117 /* NAND_EBI2_ECC_BUF_CFG bits */
120 /* NAND_ERASED_CW_DETECT_CFG bits */
121 #define ERASED_CW_ECC_MASK 1
122 #define AUTO_DETECT_RES 0
123 #define MASK_ECC (1 << ERASED_CW_ECC_MASK)
124 #define RESET_ERASED_DET (1 << AUTO_DETECT_RES)
125 #define ACTIVE_ERASED_DET (0 << AUTO_DETECT_RES)
126 #define CLR_ERASED_PAGE_DET (RESET_ERASED_DET | MASK_ECC)
127 #define SET_ERASED_PAGE_DET (ACTIVE_ERASED_DET | MASK_ECC)
129 /* NAND_ERASED_CW_DETECT_STATUS bits */
130 #define PAGE_ALL_ERASED BIT(7)
131 #define CODEWORD_ALL_ERASED BIT(6)
132 #define PAGE_ERASED BIT(5)
133 #define CODEWORD_ERASED BIT(4)
134 #define ERASED_PAGE (PAGE_ALL_ERASED | PAGE_ERASED)
135 #define ERASED_CW (CODEWORD_ALL_ERASED | CODEWORD_ERASED)
137 /* NAND_READ_LOCATION_n bits */
138 #define READ_LOCATION_OFFSET 0
139 #define READ_LOCATION_SIZE 16
140 #define READ_LOCATION_LAST 31
143 #define NAND_VERSION_MAJOR_MASK 0xf0000000
144 #define NAND_VERSION_MAJOR_SHIFT 28
145 #define NAND_VERSION_MINOR_MASK 0x0fff0000
146 #define NAND_VERSION_MINOR_SHIFT 16
149 #define OP_PAGE_READ 0x2
150 #define OP_PAGE_READ_WITH_ECC 0x3
151 #define OP_PAGE_READ_WITH_ECC_SPARE 0x4
152 #define OP_PAGE_READ_ONFI_READ 0x5
153 #define OP_PROGRAM_PAGE 0x6
154 #define OP_PAGE_PROGRAM_WITH_ECC 0x7
155 #define OP_PROGRAM_PAGE_SPARE 0x9
156 #define OP_BLOCK_ERASE 0xa
157 #define OP_FETCH_ID 0xb
158 #define OP_RESET_DEVICE 0xd
160 /* Default Value for NAND_DEV_CMD_VLD */
161 #define NAND_DEV_CMD_VLD_VAL (READ_START_VLD | WRITE_START_VLD | \
162 ERASE_START_VLD | SEQ_READ_START_VLD)
165 #define BAM_MODE_EN BIT(0)
168 * the NAND controller performs reads/writes with ECC in 516 byte chunks.
169 * the driver calls the chunks 'step' or 'codeword' interchangeably
171 #define NANDC_STEP_SIZE 512
174 * the largest page size we support is 8K, this will have 16 steps/codewords
177 #define MAX_NUM_STEPS (SZ_8K / NANDC_STEP_SIZE)
179 /* we read at most 3 registers per codeword scan */
180 #define MAX_REG_RD (3 * MAX_NUM_STEPS)
182 /* ECC modes supported by the controller */
183 #define ECC_NONE BIT(0)
184 #define ECC_RS_4BIT BIT(1)
185 #define ECC_BCH_4BIT BIT(2)
186 #define ECC_BCH_8BIT BIT(3)
188 #define nandc_set_read_loc_first(chip, reg, cw_offset, read_size, is_last_read_loc) \
189 nandc_set_reg(chip, reg, \
190 ((cw_offset) << READ_LOCATION_OFFSET) | \
191 ((read_size) << READ_LOCATION_SIZE) | \
192 ((is_last_read_loc) << READ_LOCATION_LAST))
194 #define nandc_set_read_loc_last(chip, reg, cw_offset, read_size, is_last_read_loc) \
195 nandc_set_reg(chip, reg, \
196 ((cw_offset) << READ_LOCATION_OFFSET) | \
197 ((read_size) << READ_LOCATION_SIZE) | \
198 ((is_last_read_loc) << READ_LOCATION_LAST))
200 * Returns the actual register address for all NAND_DEV_ registers
201 * (i.e. NAND_DEV_CMD0, NAND_DEV_CMD1, NAND_DEV_CMD2 and NAND_DEV_CMD_VLD)
203 #define dev_cmd_reg_addr(nandc, reg) ((nandc)->props->dev_cmd_reg_start + (reg))
205 /* Returns the NAND register physical address */
206 #define nandc_reg_phys(chip, offset) ((chip)->base_phys + (offset))
208 /* Returns the dma address for reg read buffer */
209 #define reg_buf_dma_addr(chip, vaddr) \
210 ((chip)->reg_read_dma + \
211 ((uint8_t *)(vaddr) - (uint8_t *)(chip)->reg_read_buf))
213 #define QPIC_PER_CW_CMD_ELEMENTS 32
214 #define QPIC_PER_CW_CMD_SGL 32
215 #define QPIC_PER_CW_DATA_SGL 8
217 #define QPIC_NAND_COMPLETION_TIMEOUT msecs_to_jiffies(2000)
220 * Flags used in DMA descriptor preparation helper functions
221 * (i.e. read_reg_dma/write_reg_dma/read_data_dma/write_data_dma)
223 /* Don't set the EOT in current tx BAM sgl */
224 #define NAND_BAM_NO_EOT BIT(0)
225 /* Set the NWD flag in current BAM sgl */
226 #define NAND_BAM_NWD BIT(1)
227 /* Finish writing in the current BAM sgl and start writing in another BAM sgl */
228 #define NAND_BAM_NEXT_SGL BIT(2)
230 * Erased codeword status is being used two times in single transfer so this
231 * flag will determine the current value of erased codeword status register
233 #define NAND_ERASED_CW_SET BIT(4)
236 * This data type corresponds to the BAM transaction which will be used for all
238 * @bam_ce - the array of BAM command elements
239 * @cmd_sgl - sgl for NAND BAM command pipe
240 * @data_sgl - sgl for NAND BAM consumer/producer pipe
241 * @bam_ce_pos - the index in bam_ce which is available for next sgl
242 * @bam_ce_start - the index in bam_ce which marks the start position ce
243 * for current sgl. It will be used for size calculation
245 * @cmd_sgl_pos - current index in command sgl.
246 * @cmd_sgl_start - start index in command sgl.
247 * @tx_sgl_pos - current index in data sgl for tx.
248 * @tx_sgl_start - start index in data sgl for tx.
249 * @rx_sgl_pos - current index in data sgl for rx.
250 * @rx_sgl_start - start index in data sgl for rx.
251 * @wait_second_completion - wait for second DMA desc completion before making
252 * the NAND transfer completion.
253 * @txn_done - completion for NAND transfer.
254 * @last_data_desc - last DMA desc in data channel (tx/rx).
255 * @last_cmd_desc - last DMA desc in command channel.
257 struct bam_transaction {
258 struct bam_cmd_element *bam_ce;
259 struct scatterlist *cmd_sgl;
260 struct scatterlist *data_sgl;
269 bool wait_second_completion;
270 struct completion txn_done;
271 struct dma_async_tx_descriptor *last_data_desc;
272 struct dma_async_tx_descriptor *last_cmd_desc;
276 * This data type corresponds to the nand dma descriptor
277 * @list - list for desc_info
278 * @dir - DMA transfer direction
279 * @adm_sgl - sgl which will be used for single sgl dma descriptor. Only used by
281 * @bam_sgl - sgl which will be used for dma descriptor. Only used by BAM
282 * @sgl_cnt - number of SGL in bam_sgl. Only used by BAM
283 * @dma_desc - low level DMA engine descriptor
286 struct list_head node;
288 enum dma_data_direction dir;
290 struct scatterlist adm_sgl;
292 struct scatterlist *bam_sgl;
296 struct dma_async_tx_descriptor *dma_desc;
300 * holds the current register values that we want to write. acts as a contiguous
301 * chunk of memory which we use to write the controller registers through DMA.
314 __le32 clrflashstatus;
315 __le32 clrreadstatus;
324 __le32 read_location0;
325 __le32 read_location1;
326 __le32 read_location2;
327 __le32 read_location3;
328 __le32 read_location_last0;
329 __le32 read_location_last1;
330 __le32 read_location_last2;
331 __le32 read_location_last3;
333 __le32 erased_cw_detect_cfg_clr;
334 __le32 erased_cw_detect_cfg_set;
338 * NAND controller data struct
340 * @controller: base controller structure
341 * @host_list: list containing all the chips attached to the
343 * @dev: parent device
345 * @base_phys: physical base address of controller registers
346 * @base_dma: dma base address of controller registers
347 * @core_clk: controller clock
348 * @aon_clk: another controller clock
351 * @cmd_crci: ADM DMA CRCI for command flow control
352 * @data_crci: ADM DMA CRCI for data flow control
353 * @desc_list: DMA descriptor list (list of desc_infos)
355 * @data_buffer: our local DMA buffer for page read/writes,
356 * used when we can't use the buffer provided
357 * by upper layers directly
358 * @buf_size/count/start: markers for chip->legacy.read_buf/write_buf
360 * @reg_read_buf: local buffer for reading back registers via DMA
361 * @reg_read_dma: contains dma address for register read buffer
362 * @reg_read_pos: marker for data read in reg_read_buf
364 * @regs: a contiguous chunk of memory for DMA register
365 * writes. contains the register values to be
366 * written to controller
367 * @cmd1/vld: some fixed controller register values
368 * @props: properties of current NAND controller,
369 * initialized via DT match data
370 * @max_cwperpage: maximum QPIC codewords required. calculated
371 * from all connected NAND devices pagesize
373 struct qcom_nand_controller {
374 struct nand_controller controller;
375 struct list_head host_list;
380 phys_addr_t base_phys;
383 struct clk *core_clk;
387 /* will be used only by QPIC for BAM DMA */
389 struct dma_chan *tx_chan;
390 struct dma_chan *rx_chan;
391 struct dma_chan *cmd_chan;
394 /* will be used only by EBI2 for ADM DMA */
396 struct dma_chan *chan;
397 unsigned int cmd_crci;
398 unsigned int data_crci;
402 struct list_head desc_list;
403 struct bam_transaction *bam_txn;
409 unsigned int max_cwperpage;
411 __le32 *reg_read_buf;
412 dma_addr_t reg_read_dma;
415 struct nandc_regs *regs;
418 const struct qcom_nandc_props *props;
422 * NAND chip structure
424 * @chip: base NAND chip structure
425 * @node: list node to add itself to host_list in
426 * qcom_nand_controller
428 * @cs: chip select value for this chip
429 * @cw_size: the number of bytes in a single step/codeword
430 * of a page, consisting of all data, ecc, spare
432 * @cw_data: the number of bytes within a codeword protected
434 * @use_ecc: request the controller to use ECC for the
435 * upcoming read/write
436 * @bch_enabled: flag to tell whether BCH ECC mode is used
437 * @ecc_bytes_hw: ECC bytes used by controller hardware for this
439 * @status: value to be returned if NAND_CMD_STATUS command
441 * @last_command: keeps track of last command on this chip. used
442 * for reading correct status
444 * @cfg0, cfg1, cfg0_raw..: NANDc register configurations needed for
445 * ecc/non-ecc mode for the current nand flash
448 struct qcom_nand_host {
449 struct nand_chip chip;
450 struct list_head node;
464 u32 cfg0_raw, cfg1_raw;
472 * This data type corresponds to the NAND controller properties which varies
473 * among different NAND controllers.
474 * @ecc_modes - ecc mode for NAND
475 * @is_bam - whether NAND controller is using BAM
476 * @is_qpic - whether NAND CTRL is part of qpic IP
477 * @qpic_v2 - flag to indicate QPIC IP version 2
478 * @dev_cmd_reg_start - NAND_DEV_CMD_* registers starting offset
480 struct qcom_nandc_props {
485 u32 dev_cmd_reg_start;
488 /* Frees the BAM transaction memory */
489 static void free_bam_transaction(struct qcom_nand_controller *nandc)
491 struct bam_transaction *bam_txn = nandc->bam_txn;
493 devm_kfree(nandc->dev, bam_txn);
496 /* Allocates and Initializes the BAM transaction */
497 static struct bam_transaction *
498 alloc_bam_transaction(struct qcom_nand_controller *nandc)
500 struct bam_transaction *bam_txn;
502 unsigned int num_cw = nandc->max_cwperpage;
506 sizeof(*bam_txn) + num_cw *
507 ((sizeof(*bam_txn->bam_ce) * QPIC_PER_CW_CMD_ELEMENTS) +
508 (sizeof(*bam_txn->cmd_sgl) * QPIC_PER_CW_CMD_SGL) +
509 (sizeof(*bam_txn->data_sgl) * QPIC_PER_CW_DATA_SGL));
511 bam_txn_buf = devm_kzalloc(nandc->dev, bam_txn_size, GFP_KERNEL);
515 bam_txn = bam_txn_buf;
516 bam_txn_buf += sizeof(*bam_txn);
518 bam_txn->bam_ce = bam_txn_buf;
520 sizeof(*bam_txn->bam_ce) * QPIC_PER_CW_CMD_ELEMENTS * num_cw;
522 bam_txn->cmd_sgl = bam_txn_buf;
524 sizeof(*bam_txn->cmd_sgl) * QPIC_PER_CW_CMD_SGL * num_cw;
526 bam_txn->data_sgl = bam_txn_buf;
528 init_completion(&bam_txn->txn_done);
533 /* Clears the BAM transaction indexes */
534 static void clear_bam_transaction(struct qcom_nand_controller *nandc)
536 struct bam_transaction *bam_txn = nandc->bam_txn;
538 if (!nandc->props->is_bam)
541 bam_txn->bam_ce_pos = 0;
542 bam_txn->bam_ce_start = 0;
543 bam_txn->cmd_sgl_pos = 0;
544 bam_txn->cmd_sgl_start = 0;
545 bam_txn->tx_sgl_pos = 0;
546 bam_txn->tx_sgl_start = 0;
547 bam_txn->rx_sgl_pos = 0;
548 bam_txn->rx_sgl_start = 0;
549 bam_txn->last_data_desc = NULL;
550 bam_txn->wait_second_completion = false;
552 sg_init_table(bam_txn->cmd_sgl, nandc->max_cwperpage *
553 QPIC_PER_CW_CMD_SGL);
554 sg_init_table(bam_txn->data_sgl, nandc->max_cwperpage *
555 QPIC_PER_CW_DATA_SGL);
557 reinit_completion(&bam_txn->txn_done);
560 /* Callback for DMA descriptor completion */
561 static void qpic_bam_dma_done(void *data)
563 struct bam_transaction *bam_txn = data;
566 * In case of data transfer with NAND, 2 callbacks will be generated.
567 * One for command channel and another one for data channel.
568 * If current transaction has data descriptors
569 * (i.e. wait_second_completion is true), then set this to false
570 * and wait for second DMA descriptor completion.
572 if (bam_txn->wait_second_completion)
573 bam_txn->wait_second_completion = false;
575 complete(&bam_txn->txn_done);
578 static inline struct qcom_nand_host *to_qcom_nand_host(struct nand_chip *chip)
580 return container_of(chip, struct qcom_nand_host, chip);
583 static inline struct qcom_nand_controller *
584 get_qcom_nand_controller(struct nand_chip *chip)
586 return container_of(chip->controller, struct qcom_nand_controller,
590 static inline u32 nandc_read(struct qcom_nand_controller *nandc, int offset)
592 return ioread32(nandc->base + offset);
595 static inline void nandc_write(struct qcom_nand_controller *nandc, int offset,
598 iowrite32(val, nandc->base + offset);
601 static inline void nandc_read_buffer_sync(struct qcom_nand_controller *nandc,
604 if (!nandc->props->is_bam)
608 dma_sync_single_for_cpu(nandc->dev, nandc->reg_read_dma,
610 sizeof(*nandc->reg_read_buf),
613 dma_sync_single_for_device(nandc->dev, nandc->reg_read_dma,
615 sizeof(*nandc->reg_read_buf),
619 static __le32 *offset_to_nandc_reg(struct nandc_regs *regs, int offset)
628 case NAND_FLASH_CHIP_SELECT:
629 return ®s->chip_sel;
632 case NAND_FLASH_STATUS:
633 return ®s->clrflashstatus;
638 case NAND_DEV0_ECC_CFG:
639 return ®s->ecc_bch_cfg;
640 case NAND_READ_STATUS:
641 return ®s->clrreadstatus;
644 case NAND_DEV_CMD1_RESTORE:
645 return ®s->orig_cmd1;
646 case NAND_DEV_CMD_VLD:
648 case NAND_DEV_CMD_VLD_RESTORE:
649 return ®s->orig_vld;
650 case NAND_EBI2_ECC_BUF_CFG:
651 return ®s->ecc_buf_cfg;
652 case NAND_READ_LOCATION_0:
653 return ®s->read_location0;
654 case NAND_READ_LOCATION_1:
655 return ®s->read_location1;
656 case NAND_READ_LOCATION_2:
657 return ®s->read_location2;
658 case NAND_READ_LOCATION_3:
659 return ®s->read_location3;
660 case NAND_READ_LOCATION_LAST_CW_0:
661 return ®s->read_location_last0;
662 case NAND_READ_LOCATION_LAST_CW_1:
663 return ®s->read_location_last1;
664 case NAND_READ_LOCATION_LAST_CW_2:
665 return ®s->read_location_last2;
666 case NAND_READ_LOCATION_LAST_CW_3:
667 return ®s->read_location_last3;
673 static void nandc_set_reg(struct nand_chip *chip, int offset,
676 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
677 struct nandc_regs *regs = nandc->regs;
680 reg = offset_to_nandc_reg(regs, offset);
683 *reg = cpu_to_le32(val);
686 /* Helper to check the code word, whether it is last cw or not */
687 static bool qcom_nandc_is_last_cw(struct nand_ecc_ctrl *ecc, int cw)
689 return cw == (ecc->steps - 1);
692 /* helper to configure location register values */
693 static void nandc_set_read_loc(struct nand_chip *chip, int cw, int reg,
694 int cw_offset, int read_size, int is_last_read_loc)
696 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
697 struct nand_ecc_ctrl *ecc = &chip->ecc;
698 int reg_base = NAND_READ_LOCATION_0;
700 if (nandc->props->qpic_v2 && qcom_nandc_is_last_cw(ecc, cw))
701 reg_base = NAND_READ_LOCATION_LAST_CW_0;
705 if (nandc->props->qpic_v2 && qcom_nandc_is_last_cw(ecc, cw))
706 return nandc_set_read_loc_last(chip, reg_base, cw_offset,
707 read_size, is_last_read_loc);
709 return nandc_set_read_loc_first(chip, reg_base, cw_offset,
710 read_size, is_last_read_loc);
713 /* helper to configure address register values */
714 static void set_address(struct qcom_nand_host *host, u16 column, int page)
716 struct nand_chip *chip = &host->chip;
718 if (chip->options & NAND_BUSWIDTH_16)
721 nandc_set_reg(chip, NAND_ADDR0, page << 16 | column);
722 nandc_set_reg(chip, NAND_ADDR1, page >> 16 & 0xff);
726 * update_rw_regs: set up read/write register values, these will be
727 * written to the NAND controller registers via DMA
729 * @num_cw: number of steps for the read/write operation
730 * @read: read or write operation
731 * @cw : which code word
733 static void update_rw_regs(struct qcom_nand_host *host, int num_cw, bool read, int cw)
735 struct nand_chip *chip = &host->chip;
736 u32 cmd, cfg0, cfg1, ecc_bch_cfg;
737 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
741 cmd = OP_PAGE_READ_WITH_ECC | PAGE_ACC | LAST_PAGE;
743 cmd = OP_PAGE_READ | PAGE_ACC | LAST_PAGE;
745 cmd = OP_PROGRAM_PAGE | PAGE_ACC | LAST_PAGE;
749 cfg0 = (host->cfg0 & ~(7U << CW_PER_PAGE)) |
750 (num_cw - 1) << CW_PER_PAGE;
753 ecc_bch_cfg = host->ecc_bch_cfg;
755 cfg0 = (host->cfg0_raw & ~(7U << CW_PER_PAGE)) |
756 (num_cw - 1) << CW_PER_PAGE;
758 cfg1 = host->cfg1_raw;
759 ecc_bch_cfg = 1 << ECC_CFG_ECC_DISABLE;
762 nandc_set_reg(chip, NAND_FLASH_CMD, cmd);
763 nandc_set_reg(chip, NAND_DEV0_CFG0, cfg0);
764 nandc_set_reg(chip, NAND_DEV0_CFG1, cfg1);
765 nandc_set_reg(chip, NAND_DEV0_ECC_CFG, ecc_bch_cfg);
766 if (!nandc->props->qpic_v2)
767 nandc_set_reg(chip, NAND_EBI2_ECC_BUF_CFG, host->ecc_buf_cfg);
768 nandc_set_reg(chip, NAND_FLASH_STATUS, host->clrflashstatus);
769 nandc_set_reg(chip, NAND_READ_STATUS, host->clrreadstatus);
770 nandc_set_reg(chip, NAND_EXEC_CMD, 1);
773 nandc_set_read_loc(chip, cw, 0, 0, host->use_ecc ?
774 host->cw_data : host->cw_size, 1);
778 * Maps the scatter gather list for DMA transfer and forms the DMA descriptor
779 * for BAM. This descriptor will be added in the NAND DMA descriptor queue
780 * which will be submitted to DMA engine.
782 static int prepare_bam_async_desc(struct qcom_nand_controller *nandc,
783 struct dma_chan *chan,
786 struct desc_info *desc;
787 struct scatterlist *sgl;
788 unsigned int sgl_cnt;
790 struct bam_transaction *bam_txn = nandc->bam_txn;
791 enum dma_transfer_direction dir_eng;
792 struct dma_async_tx_descriptor *dma_desc;
794 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
798 if (chan == nandc->cmd_chan) {
799 sgl = &bam_txn->cmd_sgl[bam_txn->cmd_sgl_start];
800 sgl_cnt = bam_txn->cmd_sgl_pos - bam_txn->cmd_sgl_start;
801 bam_txn->cmd_sgl_start = bam_txn->cmd_sgl_pos;
802 dir_eng = DMA_MEM_TO_DEV;
803 desc->dir = DMA_TO_DEVICE;
804 } else if (chan == nandc->tx_chan) {
805 sgl = &bam_txn->data_sgl[bam_txn->tx_sgl_start];
806 sgl_cnt = bam_txn->tx_sgl_pos - bam_txn->tx_sgl_start;
807 bam_txn->tx_sgl_start = bam_txn->tx_sgl_pos;
808 dir_eng = DMA_MEM_TO_DEV;
809 desc->dir = DMA_TO_DEVICE;
811 sgl = &bam_txn->data_sgl[bam_txn->rx_sgl_start];
812 sgl_cnt = bam_txn->rx_sgl_pos - bam_txn->rx_sgl_start;
813 bam_txn->rx_sgl_start = bam_txn->rx_sgl_pos;
814 dir_eng = DMA_DEV_TO_MEM;
815 desc->dir = DMA_FROM_DEVICE;
818 sg_mark_end(sgl + sgl_cnt - 1);
819 ret = dma_map_sg(nandc->dev, sgl, sgl_cnt, desc->dir);
821 dev_err(nandc->dev, "failure in mapping desc\n");
826 desc->sgl_cnt = sgl_cnt;
829 dma_desc = dmaengine_prep_slave_sg(chan, sgl, sgl_cnt, dir_eng,
833 dev_err(nandc->dev, "failure in prep desc\n");
834 dma_unmap_sg(nandc->dev, sgl, sgl_cnt, desc->dir);
839 desc->dma_desc = dma_desc;
841 /* update last data/command descriptor */
842 if (chan == nandc->cmd_chan)
843 bam_txn->last_cmd_desc = dma_desc;
845 bam_txn->last_data_desc = dma_desc;
847 list_add_tail(&desc->node, &nandc->desc_list);
853 * Prepares the command descriptor for BAM DMA which will be used for NAND
854 * register reads and writes. The command descriptor requires the command
855 * to be formed in command element type so this function uses the command
856 * element from bam transaction ce array and fills the same with required
857 * data. A single SGL can contain multiple command elements so
858 * NAND_BAM_NEXT_SGL will be used for starting the separate SGL
859 * after the current command element.
861 static int prep_bam_dma_desc_cmd(struct qcom_nand_controller *nandc, bool read,
862 int reg_off, const void *vaddr,
863 int size, unsigned int flags)
867 struct bam_cmd_element *bam_ce_buffer;
868 struct bam_transaction *bam_txn = nandc->bam_txn;
870 bam_ce_buffer = &bam_txn->bam_ce[bam_txn->bam_ce_pos];
872 /* fill the command desc */
873 for (i = 0; i < size; i++) {
875 bam_prep_ce(&bam_ce_buffer[i],
876 nandc_reg_phys(nandc, reg_off + 4 * i),
878 reg_buf_dma_addr(nandc,
879 (__le32 *)vaddr + i));
881 bam_prep_ce_le32(&bam_ce_buffer[i],
882 nandc_reg_phys(nandc, reg_off + 4 * i),
884 *((__le32 *)vaddr + i));
887 bam_txn->bam_ce_pos += size;
889 /* use the separate sgl after this command */
890 if (flags & NAND_BAM_NEXT_SGL) {
891 bam_ce_buffer = &bam_txn->bam_ce[bam_txn->bam_ce_start];
892 bam_ce_size = (bam_txn->bam_ce_pos -
893 bam_txn->bam_ce_start) *
894 sizeof(struct bam_cmd_element);
895 sg_set_buf(&bam_txn->cmd_sgl[bam_txn->cmd_sgl_pos],
896 bam_ce_buffer, bam_ce_size);
897 bam_txn->cmd_sgl_pos++;
898 bam_txn->bam_ce_start = bam_txn->bam_ce_pos;
900 if (flags & NAND_BAM_NWD) {
901 ret = prepare_bam_async_desc(nandc, nandc->cmd_chan,
913 * Prepares the data descriptor for BAM DMA which will be used for NAND
914 * data reads and writes.
916 static int prep_bam_dma_desc_data(struct qcom_nand_controller *nandc, bool read,
918 int size, unsigned int flags)
921 struct bam_transaction *bam_txn = nandc->bam_txn;
924 sg_set_buf(&bam_txn->data_sgl[bam_txn->rx_sgl_pos],
926 bam_txn->rx_sgl_pos++;
928 sg_set_buf(&bam_txn->data_sgl[bam_txn->tx_sgl_pos],
930 bam_txn->tx_sgl_pos++;
933 * BAM will only set EOT for DMA_PREP_INTERRUPT so if this flag
934 * is not set, form the DMA descriptor
936 if (!(flags & NAND_BAM_NO_EOT)) {
937 ret = prepare_bam_async_desc(nandc, nandc->tx_chan,
947 static int prep_adm_dma_desc(struct qcom_nand_controller *nandc, bool read,
948 int reg_off, const void *vaddr, int size,
951 struct desc_info *desc;
952 struct dma_async_tx_descriptor *dma_desc;
953 struct scatterlist *sgl;
954 struct dma_slave_config slave_conf;
955 enum dma_transfer_direction dir_eng;
958 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
962 sgl = &desc->adm_sgl;
964 sg_init_one(sgl, vaddr, size);
967 dir_eng = DMA_DEV_TO_MEM;
968 desc->dir = DMA_FROM_DEVICE;
970 dir_eng = DMA_MEM_TO_DEV;
971 desc->dir = DMA_TO_DEVICE;
974 ret = dma_map_sg(nandc->dev, sgl, 1, desc->dir);
980 memset(&slave_conf, 0x00, sizeof(slave_conf));
982 slave_conf.device_fc = flow_control;
984 slave_conf.src_maxburst = 16;
985 slave_conf.src_addr = nandc->base_dma + reg_off;
986 slave_conf.slave_id = nandc->data_crci;
988 slave_conf.dst_maxburst = 16;
989 slave_conf.dst_addr = nandc->base_dma + reg_off;
990 slave_conf.slave_id = nandc->cmd_crci;
993 ret = dmaengine_slave_config(nandc->chan, &slave_conf);
995 dev_err(nandc->dev, "failed to configure dma channel\n");
999 dma_desc = dmaengine_prep_slave_sg(nandc->chan, sgl, 1, dir_eng, 0);
1001 dev_err(nandc->dev, "failed to prepare desc\n");
1006 desc->dma_desc = dma_desc;
1008 list_add_tail(&desc->node, &nandc->desc_list);
1018 * read_reg_dma: prepares a descriptor to read a given number of
1019 * contiguous registers to the reg_read_buf pointer
1021 * @first: offset of the first register in the contiguous block
1022 * @num_regs: number of registers to read
1023 * @flags: flags to control DMA descriptor preparation
1025 static int read_reg_dma(struct qcom_nand_controller *nandc, int first,
1026 int num_regs, unsigned int flags)
1028 bool flow_control = false;
1031 vaddr = nandc->reg_read_buf + nandc->reg_read_pos;
1032 nandc->reg_read_pos += num_regs;
1034 if (first == NAND_DEV_CMD_VLD || first == NAND_DEV_CMD1)
1035 first = dev_cmd_reg_addr(nandc, first);
1037 if (nandc->props->is_bam)
1038 return prep_bam_dma_desc_cmd(nandc, true, first, vaddr,
1041 if (first == NAND_READ_ID || first == NAND_FLASH_STATUS)
1042 flow_control = true;
1044 return prep_adm_dma_desc(nandc, true, first, vaddr,
1045 num_regs * sizeof(u32), flow_control);
1049 * write_reg_dma: prepares a descriptor to write a given number of
1050 * contiguous registers
1052 * @first: offset of the first register in the contiguous block
1053 * @num_regs: number of registers to write
1054 * @flags: flags to control DMA descriptor preparation
1056 static int write_reg_dma(struct qcom_nand_controller *nandc, int first,
1057 int num_regs, unsigned int flags)
1059 bool flow_control = false;
1060 struct nandc_regs *regs = nandc->regs;
1063 vaddr = offset_to_nandc_reg(regs, first);
1065 if (first == NAND_ERASED_CW_DETECT_CFG) {
1066 if (flags & NAND_ERASED_CW_SET)
1067 vaddr = ®s->erased_cw_detect_cfg_set;
1069 vaddr = ®s->erased_cw_detect_cfg_clr;
1072 if (first == NAND_EXEC_CMD)
1073 flags |= NAND_BAM_NWD;
1075 if (first == NAND_DEV_CMD1_RESTORE || first == NAND_DEV_CMD1)
1076 first = dev_cmd_reg_addr(nandc, NAND_DEV_CMD1);
1078 if (first == NAND_DEV_CMD_VLD_RESTORE || first == NAND_DEV_CMD_VLD)
1079 first = dev_cmd_reg_addr(nandc, NAND_DEV_CMD_VLD);
1081 if (nandc->props->is_bam)
1082 return prep_bam_dma_desc_cmd(nandc, false, first, vaddr,
1085 if (first == NAND_FLASH_CMD)
1086 flow_control = true;
1088 return prep_adm_dma_desc(nandc, false, first, vaddr,
1089 num_regs * sizeof(u32), flow_control);
1093 * read_data_dma: prepares a DMA descriptor to transfer data from the
1094 * controller's internal buffer to the buffer 'vaddr'
1096 * @reg_off: offset within the controller's data buffer
1097 * @vaddr: virtual address of the buffer we want to write to
1098 * @size: DMA transaction size in bytes
1099 * @flags: flags to control DMA descriptor preparation
1101 static int read_data_dma(struct qcom_nand_controller *nandc, int reg_off,
1102 const u8 *vaddr, int size, unsigned int flags)
1104 if (nandc->props->is_bam)
1105 return prep_bam_dma_desc_data(nandc, true, vaddr, size, flags);
1107 return prep_adm_dma_desc(nandc, true, reg_off, vaddr, size, false);
1111 * write_data_dma: prepares a DMA descriptor to transfer data from
1112 * 'vaddr' to the controller's internal buffer
1114 * @reg_off: offset within the controller's data buffer
1115 * @vaddr: virtual address of the buffer we want to read from
1116 * @size: DMA transaction size in bytes
1117 * @flags: flags to control DMA descriptor preparation
1119 static int write_data_dma(struct qcom_nand_controller *nandc, int reg_off,
1120 const u8 *vaddr, int size, unsigned int flags)
1122 if (nandc->props->is_bam)
1123 return prep_bam_dma_desc_data(nandc, false, vaddr, size, flags);
1125 return prep_adm_dma_desc(nandc, false, reg_off, vaddr, size, false);
1129 * Helper to prepare DMA descriptors for configuring registers
1130 * before reading a NAND page.
1132 static void config_nand_page_read(struct nand_chip *chip)
1134 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1136 write_reg_dma(nandc, NAND_ADDR0, 2, 0);
1137 write_reg_dma(nandc, NAND_DEV0_CFG0, 3, 0);
1138 if (!nandc->props->qpic_v2)
1139 write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1, 0);
1140 write_reg_dma(nandc, NAND_ERASED_CW_DETECT_CFG, 1, 0);
1141 write_reg_dma(nandc, NAND_ERASED_CW_DETECT_CFG, 1,
1142 NAND_ERASED_CW_SET | NAND_BAM_NEXT_SGL);
1146 * Helper to prepare DMA descriptors for configuring registers
1147 * before reading each codeword in NAND page.
1150 config_nand_cw_read(struct nand_chip *chip, bool use_ecc, int cw)
1152 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1153 struct nand_ecc_ctrl *ecc = &chip->ecc;
1155 int reg = NAND_READ_LOCATION_0;
1157 if (nandc->props->qpic_v2 && qcom_nandc_is_last_cw(ecc, cw))
1158 reg = NAND_READ_LOCATION_LAST_CW_0;
1160 if (nandc->props->is_bam)
1161 write_reg_dma(nandc, reg, 4, NAND_BAM_NEXT_SGL);
1163 write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
1164 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
1167 read_reg_dma(nandc, NAND_FLASH_STATUS, 2, 0);
1168 read_reg_dma(nandc, NAND_ERASED_CW_DETECT_STATUS, 1,
1171 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
1176 * Helper to prepare dma descriptors to configure registers needed for reading a
1177 * single codeword in page
1180 config_nand_single_cw_page_read(struct nand_chip *chip,
1181 bool use_ecc, int cw)
1183 config_nand_page_read(chip);
1184 config_nand_cw_read(chip, use_ecc, cw);
1188 * Helper to prepare DMA descriptors used to configure registers needed for
1189 * before writing a NAND page.
1191 static void config_nand_page_write(struct nand_chip *chip)
1193 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1195 write_reg_dma(nandc, NAND_ADDR0, 2, 0);
1196 write_reg_dma(nandc, NAND_DEV0_CFG0, 3, 0);
1197 if (!nandc->props->qpic_v2)
1198 write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1,
1203 * Helper to prepare DMA descriptors for configuring registers
1204 * before writing each codeword in NAND page.
1206 static void config_nand_cw_write(struct nand_chip *chip)
1208 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1210 write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
1211 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
1213 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
1215 write_reg_dma(nandc, NAND_FLASH_STATUS, 1, 0);
1216 write_reg_dma(nandc, NAND_READ_STATUS, 1, NAND_BAM_NEXT_SGL);
1220 * the following functions are used within chip->legacy.cmdfunc() to
1221 * perform different NAND_CMD_* commands
1224 /* sets up descriptors for NAND_CMD_PARAM */
1225 static int nandc_param(struct qcom_nand_host *host)
1227 struct nand_chip *chip = &host->chip;
1228 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1231 * NAND_CMD_PARAM is called before we know much about the FLASH chip
1232 * in use. we configure the controller to perform a raw read of 512
1233 * bytes to read onfi params
1235 if (nandc->props->qpic_v2)
1236 nandc_set_reg(chip, NAND_FLASH_CMD, OP_PAGE_READ_ONFI_READ |
1237 PAGE_ACC | LAST_PAGE);
1239 nandc_set_reg(chip, NAND_FLASH_CMD, OP_PAGE_READ |
1240 PAGE_ACC | LAST_PAGE);
1242 nandc_set_reg(chip, NAND_ADDR0, 0);
1243 nandc_set_reg(chip, NAND_ADDR1, 0);
1244 nandc_set_reg(chip, NAND_DEV0_CFG0, 0 << CW_PER_PAGE
1245 | 512 << UD_SIZE_BYTES
1246 | 5 << NUM_ADDR_CYCLES
1247 | 0 << SPARE_SIZE_BYTES);
1248 nandc_set_reg(chip, NAND_DEV0_CFG1, 7 << NAND_RECOVERY_CYCLES
1249 | 0 << CS_ACTIVE_BSY
1250 | 17 << BAD_BLOCK_BYTE_NUM
1251 | 1 << BAD_BLOCK_IN_SPARE_AREA
1252 | 2 << WR_RD_BSY_GAP
1254 | 1 << DEV0_CFG1_ECC_DISABLE);
1255 if (!nandc->props->qpic_v2)
1256 nandc_set_reg(chip, NAND_EBI2_ECC_BUF_CFG, 1 << ECC_CFG_ECC_DISABLE);
1258 /* configure CMD1 and VLD for ONFI param probing in QPIC v1 */
1259 if (!nandc->props->qpic_v2) {
1260 nandc_set_reg(chip, NAND_DEV_CMD_VLD,
1261 (nandc->vld & ~READ_START_VLD));
1262 nandc_set_reg(chip, NAND_DEV_CMD1,
1263 (nandc->cmd1 & ~(0xFF << READ_ADDR))
1264 | NAND_CMD_PARAM << READ_ADDR);
1267 nandc_set_reg(chip, NAND_EXEC_CMD, 1);
1269 if (!nandc->props->qpic_v2) {
1270 nandc_set_reg(chip, NAND_DEV_CMD1_RESTORE, nandc->cmd1);
1271 nandc_set_reg(chip, NAND_DEV_CMD_VLD_RESTORE, nandc->vld);
1274 nandc_set_read_loc(chip, 0, 0, 0, 512, 1);
1276 if (!nandc->props->qpic_v2) {
1277 write_reg_dma(nandc, NAND_DEV_CMD_VLD, 1, 0);
1278 write_reg_dma(nandc, NAND_DEV_CMD1, 1, NAND_BAM_NEXT_SGL);
1281 nandc->buf_count = 512;
1282 memset(nandc->data_buffer, 0xff, nandc->buf_count);
1284 config_nand_single_cw_page_read(chip, false, 0);
1286 read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer,
1287 nandc->buf_count, 0);
1289 /* restore CMD1 and VLD regs */
1290 if (!nandc->props->qpic_v2) {
1291 write_reg_dma(nandc, NAND_DEV_CMD1_RESTORE, 1, 0);
1292 write_reg_dma(nandc, NAND_DEV_CMD_VLD_RESTORE, 1, NAND_BAM_NEXT_SGL);
1298 /* sets up descriptors for NAND_CMD_ERASE1 */
1299 static int erase_block(struct qcom_nand_host *host, int page_addr)
1301 struct nand_chip *chip = &host->chip;
1302 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1304 nandc_set_reg(chip, NAND_FLASH_CMD,
1305 OP_BLOCK_ERASE | PAGE_ACC | LAST_PAGE);
1306 nandc_set_reg(chip, NAND_ADDR0, page_addr);
1307 nandc_set_reg(chip, NAND_ADDR1, 0);
1308 nandc_set_reg(chip, NAND_DEV0_CFG0,
1309 host->cfg0_raw & ~(7 << CW_PER_PAGE));
1310 nandc_set_reg(chip, NAND_DEV0_CFG1, host->cfg1_raw);
1311 nandc_set_reg(chip, NAND_EXEC_CMD, 1);
1312 nandc_set_reg(chip, NAND_FLASH_STATUS, host->clrflashstatus);
1313 nandc_set_reg(chip, NAND_READ_STATUS, host->clrreadstatus);
1315 write_reg_dma(nandc, NAND_FLASH_CMD, 3, NAND_BAM_NEXT_SGL);
1316 write_reg_dma(nandc, NAND_DEV0_CFG0, 2, NAND_BAM_NEXT_SGL);
1317 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
1319 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
1321 write_reg_dma(nandc, NAND_FLASH_STATUS, 1, 0);
1322 write_reg_dma(nandc, NAND_READ_STATUS, 1, NAND_BAM_NEXT_SGL);
1327 /* sets up descriptors for NAND_CMD_READID */
1328 static int read_id(struct qcom_nand_host *host, int column)
1330 struct nand_chip *chip = &host->chip;
1331 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1336 nandc_set_reg(chip, NAND_FLASH_CMD, OP_FETCH_ID);
1337 nandc_set_reg(chip, NAND_ADDR0, column);
1338 nandc_set_reg(chip, NAND_ADDR1, 0);
1339 nandc_set_reg(chip, NAND_FLASH_CHIP_SELECT,
1340 nandc->props->is_bam ? 0 : DM_EN);
1341 nandc_set_reg(chip, NAND_EXEC_CMD, 1);
1343 write_reg_dma(nandc, NAND_FLASH_CMD, 4, NAND_BAM_NEXT_SGL);
1344 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
1346 read_reg_dma(nandc, NAND_READ_ID, 1, NAND_BAM_NEXT_SGL);
1351 /* sets up descriptors for NAND_CMD_RESET */
1352 static int reset(struct qcom_nand_host *host)
1354 struct nand_chip *chip = &host->chip;
1355 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1357 nandc_set_reg(chip, NAND_FLASH_CMD, OP_RESET_DEVICE);
1358 nandc_set_reg(chip, NAND_EXEC_CMD, 1);
1360 write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
1361 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
1363 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
1368 /* helpers to submit/free our list of dma descriptors */
1369 static int submit_descs(struct qcom_nand_controller *nandc)
1371 struct desc_info *desc;
1372 dma_cookie_t cookie = 0;
1373 struct bam_transaction *bam_txn = nandc->bam_txn;
1376 if (nandc->props->is_bam) {
1377 if (bam_txn->rx_sgl_pos > bam_txn->rx_sgl_start) {
1378 r = prepare_bam_async_desc(nandc, nandc->rx_chan, 0);
1383 if (bam_txn->tx_sgl_pos > bam_txn->tx_sgl_start) {
1384 r = prepare_bam_async_desc(nandc, nandc->tx_chan,
1385 DMA_PREP_INTERRUPT);
1390 if (bam_txn->cmd_sgl_pos > bam_txn->cmd_sgl_start) {
1391 r = prepare_bam_async_desc(nandc, nandc->cmd_chan,
1398 list_for_each_entry(desc, &nandc->desc_list, node)
1399 cookie = dmaengine_submit(desc->dma_desc);
1401 if (nandc->props->is_bam) {
1402 bam_txn->last_cmd_desc->callback = qpic_bam_dma_done;
1403 bam_txn->last_cmd_desc->callback_param = bam_txn;
1404 if (bam_txn->last_data_desc) {
1405 bam_txn->last_data_desc->callback = qpic_bam_dma_done;
1406 bam_txn->last_data_desc->callback_param = bam_txn;
1407 bam_txn->wait_second_completion = true;
1410 dma_async_issue_pending(nandc->tx_chan);
1411 dma_async_issue_pending(nandc->rx_chan);
1412 dma_async_issue_pending(nandc->cmd_chan);
1414 if (!wait_for_completion_timeout(&bam_txn->txn_done,
1415 QPIC_NAND_COMPLETION_TIMEOUT))
1418 if (dma_sync_wait(nandc->chan, cookie) != DMA_COMPLETE)
1425 static void free_descs(struct qcom_nand_controller *nandc)
1427 struct desc_info *desc, *n;
1429 list_for_each_entry_safe(desc, n, &nandc->desc_list, node) {
1430 list_del(&desc->node);
1432 if (nandc->props->is_bam)
1433 dma_unmap_sg(nandc->dev, desc->bam_sgl,
1434 desc->sgl_cnt, desc->dir);
1436 dma_unmap_sg(nandc->dev, &desc->adm_sgl, 1,
1443 /* reset the register read buffer for next NAND operation */
1444 static void clear_read_regs(struct qcom_nand_controller *nandc)
1446 nandc->reg_read_pos = 0;
1447 nandc_read_buffer_sync(nandc, false);
1450 static void pre_command(struct qcom_nand_host *host, int command)
1452 struct nand_chip *chip = &host->chip;
1453 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1455 nandc->buf_count = 0;
1456 nandc->buf_start = 0;
1457 host->use_ecc = false;
1458 host->last_command = command;
1460 clear_read_regs(nandc);
1462 if (command == NAND_CMD_RESET || command == NAND_CMD_READID ||
1463 command == NAND_CMD_PARAM || command == NAND_CMD_ERASE1)
1464 clear_bam_transaction(nandc);
1468 * this is called after NAND_CMD_PAGEPROG and NAND_CMD_ERASE1 to set our
1469 * privately maintained status byte, this status byte can be read after
1470 * NAND_CMD_STATUS is called
1472 static void parse_erase_write_errors(struct qcom_nand_host *host, int command)
1474 struct nand_chip *chip = &host->chip;
1475 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1476 struct nand_ecc_ctrl *ecc = &chip->ecc;
1480 num_cw = command == NAND_CMD_PAGEPROG ? ecc->steps : 1;
1481 nandc_read_buffer_sync(nandc, true);
1483 for (i = 0; i < num_cw; i++) {
1484 u32 flash_status = le32_to_cpu(nandc->reg_read_buf[i]);
1486 if (flash_status & FS_MPU_ERR)
1487 host->status &= ~NAND_STATUS_WP;
1489 if (flash_status & FS_OP_ERR || (i == (num_cw - 1) &&
1491 FS_DEVICE_STS_ERR)))
1492 host->status |= NAND_STATUS_FAIL;
1496 static void post_command(struct qcom_nand_host *host, int command)
1498 struct nand_chip *chip = &host->chip;
1499 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1502 case NAND_CMD_READID:
1503 nandc_read_buffer_sync(nandc, true);
1504 memcpy(nandc->data_buffer, nandc->reg_read_buf,
1507 case NAND_CMD_PAGEPROG:
1508 case NAND_CMD_ERASE1:
1509 parse_erase_write_errors(host, command);
1517 * Implements chip->legacy.cmdfunc. It's only used for a limited set of
1518 * commands. The rest of the commands wouldn't be called by upper layers.
1519 * For example, NAND_CMD_READOOB would never be called because we have our own
1520 * versions of read_oob ops for nand_ecc_ctrl.
1522 static void qcom_nandc_command(struct nand_chip *chip, unsigned int command,
1523 int column, int page_addr)
1525 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1526 struct nand_ecc_ctrl *ecc = &chip->ecc;
1527 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1531 pre_command(host, command);
1534 case NAND_CMD_RESET:
1539 case NAND_CMD_READID:
1540 nandc->buf_count = 4;
1541 ret = read_id(host, column);
1545 case NAND_CMD_PARAM:
1546 ret = nandc_param(host);
1550 case NAND_CMD_ERASE1:
1551 ret = erase_block(host, page_addr);
1555 case NAND_CMD_READ0:
1556 /* we read the entire page for now */
1557 WARN_ON(column != 0);
1559 host->use_ecc = true;
1560 set_address(host, 0, page_addr);
1561 update_rw_regs(host, ecc->steps, true, 0);
1564 case NAND_CMD_SEQIN:
1565 WARN_ON(column != 0);
1566 set_address(host, 0, page_addr);
1569 case NAND_CMD_PAGEPROG:
1570 case NAND_CMD_STATUS:
1577 dev_err(nandc->dev, "failure executing command %d\n",
1584 ret = submit_descs(nandc);
1587 "failure submitting descs for command %d\n",
1593 post_command(host, command);
1597 * when using BCH ECC, the HW flags an error in NAND_FLASH_STATUS if it read
1598 * an erased CW, and reports an erased CW in NAND_ERASED_CW_DETECT_STATUS.
1600 * when using RS ECC, the HW reports the same erros when reading an erased CW,
1601 * but it notifies that it is an erased CW by placing special characters at
1602 * certain offsets in the buffer.
1604 * verify if the page is erased or not, and fix up the page for RS ECC by
1605 * replacing the special characters with 0xff.
1607 static bool erased_chunk_check_and_fixup(u8 *data_buf, int data_len)
1612 * an erased page flags an error in NAND_FLASH_STATUS, check if the page
1613 * is erased by looking for 0x54s at offsets 3 and 175 from the
1614 * beginning of each codeword
1617 empty1 = data_buf[3];
1618 empty2 = data_buf[175];
1621 * if the erased codework markers, if they exist override them with
1624 if ((empty1 == 0x54 && empty2 == 0xff) ||
1625 (empty1 == 0xff && empty2 == 0x54)) {
1627 data_buf[175] = 0xff;
1631 * check if the entire chunk contains 0xffs or not. if it doesn't, then
1632 * restore the original values at the special offsets
1634 if (memchr_inv(data_buf, 0xff, data_len)) {
1635 data_buf[3] = empty1;
1636 data_buf[175] = empty2;
1650 /* reads back FLASH_STATUS register set by the controller */
1651 static int check_flash_errors(struct qcom_nand_host *host, int cw_cnt)
1653 struct nand_chip *chip = &host->chip;
1654 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1657 nandc_read_buffer_sync(nandc, true);
1659 for (i = 0; i < cw_cnt; i++) {
1660 u32 flash = le32_to_cpu(nandc->reg_read_buf[i]);
1662 if (flash & (FS_OP_ERR | FS_MPU_ERR))
1669 /* performs raw read for one codeword */
1671 qcom_nandc_read_cw_raw(struct mtd_info *mtd, struct nand_chip *chip,
1672 u8 *data_buf, u8 *oob_buf, int page, int cw)
1674 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1675 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1676 struct nand_ecc_ctrl *ecc = &chip->ecc;
1677 int data_size1, data_size2, oob_size1, oob_size2;
1678 int ret, reg_off = FLASH_BUF_ACC, read_loc = 0;
1680 nand_read_page_op(chip, page, 0, NULL, 0);
1681 host->use_ecc = false;
1683 clear_bam_transaction(nandc);
1684 set_address(host, host->cw_size * cw, page);
1685 update_rw_regs(host, 1, true, cw);
1686 config_nand_page_read(chip);
1688 data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
1689 oob_size1 = host->bbm_size;
1691 if (qcom_nandc_is_last_cw(ecc, cw)) {
1692 data_size2 = ecc->size - data_size1 -
1693 ((ecc->steps - 1) * 4);
1694 oob_size2 = (ecc->steps * 4) + host->ecc_bytes_hw +
1697 data_size2 = host->cw_data - data_size1;
1698 oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
1701 if (nandc->props->is_bam) {
1702 nandc_set_read_loc(chip, cw, 0, read_loc, data_size1, 0);
1703 read_loc += data_size1;
1705 nandc_set_read_loc(chip, cw, 1, read_loc, oob_size1, 0);
1706 read_loc += oob_size1;
1708 nandc_set_read_loc(chip, cw, 2, read_loc, data_size2, 0);
1709 read_loc += data_size2;
1711 nandc_set_read_loc(chip, cw, 3, read_loc, oob_size2, 1);
1714 config_nand_cw_read(chip, false, cw);
1716 read_data_dma(nandc, reg_off, data_buf, data_size1, 0);
1717 reg_off += data_size1;
1719 read_data_dma(nandc, reg_off, oob_buf, oob_size1, 0);
1720 reg_off += oob_size1;
1722 read_data_dma(nandc, reg_off, data_buf + data_size1, data_size2, 0);
1723 reg_off += data_size2;
1725 read_data_dma(nandc, reg_off, oob_buf + oob_size1, oob_size2, 0);
1727 ret = submit_descs(nandc);
1730 dev_err(nandc->dev, "failure to read raw cw %d\n", cw);
1734 return check_flash_errors(host, 1);
1738 * Bitflips can happen in erased codewords also so this function counts the
1739 * number of 0 in each CW for which ECC engine returns the uncorrectable
1740 * error. The page will be assumed as erased if this count is less than or
1741 * equal to the ecc->strength for each CW.
1743 * 1. Both DATA and OOB need to be checked for number of 0. The
1744 * top-level API can be called with only data buf or OOB buf so use
1745 * chip->data_buf if data buf is null and chip->oob_poi if oob buf
1746 * is null for copying the raw bytes.
1747 * 2. Perform raw read for all the CW which has uncorrectable errors.
1748 * 3. For each CW, check the number of 0 in cw_data and usable OOB bytes.
1749 * The BBM and spare bytes bit flip won’t affect the ECC so don’t check
1750 * the number of bitflips in this area.
1753 check_for_erased_page(struct qcom_nand_host *host, u8 *data_buf,
1754 u8 *oob_buf, unsigned long uncorrectable_cws,
1755 int page, unsigned int max_bitflips)
1757 struct nand_chip *chip = &host->chip;
1758 struct mtd_info *mtd = nand_to_mtd(chip);
1759 struct nand_ecc_ctrl *ecc = &chip->ecc;
1760 u8 *cw_data_buf, *cw_oob_buf;
1761 int cw, data_size, oob_size, ret = 0;
1764 data_buf = nand_get_data_buf(chip);
1767 nand_get_data_buf(chip);
1768 oob_buf = chip->oob_poi;
1771 for_each_set_bit(cw, &uncorrectable_cws, ecc->steps) {
1772 if (qcom_nandc_is_last_cw(ecc, cw)) {
1773 data_size = ecc->size - ((ecc->steps - 1) * 4);
1774 oob_size = (ecc->steps * 4) + host->ecc_bytes_hw;
1776 data_size = host->cw_data;
1777 oob_size = host->ecc_bytes_hw;
1780 /* determine starting buffer address for current CW */
1781 cw_data_buf = data_buf + (cw * host->cw_data);
1782 cw_oob_buf = oob_buf + (cw * ecc->bytes);
1784 ret = qcom_nandc_read_cw_raw(mtd, chip, cw_data_buf,
1785 cw_oob_buf, page, cw);
1790 * make sure it isn't an erased page reported
1791 * as not-erased by HW because of a few bitflips
1793 ret = nand_check_erased_ecc_chunk(cw_data_buf, data_size,
1794 cw_oob_buf + host->bbm_size,
1798 mtd->ecc_stats.failed++;
1800 mtd->ecc_stats.corrected += ret;
1801 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1805 return max_bitflips;
1809 * reads back status registers set by the controller to notify page read
1810 * errors. this is equivalent to what 'ecc->correct()' would do.
1812 static int parse_read_errors(struct qcom_nand_host *host, u8 *data_buf,
1813 u8 *oob_buf, int page)
1815 struct nand_chip *chip = &host->chip;
1816 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1817 struct mtd_info *mtd = nand_to_mtd(chip);
1818 struct nand_ecc_ctrl *ecc = &chip->ecc;
1819 unsigned int max_bitflips = 0, uncorrectable_cws = 0;
1820 struct read_stats *buf;
1821 bool flash_op_err = false, erased;
1823 u8 *data_buf_start = data_buf, *oob_buf_start = oob_buf;
1825 buf = (struct read_stats *)nandc->reg_read_buf;
1826 nandc_read_buffer_sync(nandc, true);
1828 for (i = 0; i < ecc->steps; i++, buf++) {
1829 u32 flash, buffer, erased_cw;
1830 int data_len, oob_len;
1832 if (qcom_nandc_is_last_cw(ecc, i)) {
1833 data_len = ecc->size - ((ecc->steps - 1) << 2);
1834 oob_len = ecc->steps << 2;
1836 data_len = host->cw_data;
1840 flash = le32_to_cpu(buf->flash);
1841 buffer = le32_to_cpu(buf->buffer);
1842 erased_cw = le32_to_cpu(buf->erased_cw);
1845 * Check ECC failure for each codeword. ECC failure can
1846 * happen in either of the following conditions
1847 * 1. If number of bitflips are greater than ECC engine
1849 * 2. If this codeword contains all 0xff for which erased
1850 * codeword detection check will be done.
1852 if ((flash & FS_OP_ERR) && (buffer & BS_UNCORRECTABLE_BIT)) {
1854 * For BCH ECC, ignore erased codeword errors, if
1855 * ERASED_CW bits are set.
1857 if (host->bch_enabled) {
1858 erased = (erased_cw & ERASED_CW) == ERASED_CW;
1860 * For RS ECC, HW reports the erased CW by placing
1861 * special characters at certain offsets in the buffer.
1862 * These special characters will be valid only if
1863 * complete page is read i.e. data_buf is not NULL.
1865 } else if (data_buf) {
1866 erased = erased_chunk_check_and_fixup(data_buf,
1873 uncorrectable_cws |= BIT(i);
1875 * Check if MPU or any other operational error (timeout,
1876 * device failure, etc.) happened for this codeword and
1877 * make flash_op_err true. If flash_op_err is set, then
1878 * EIO will be returned for page read.
1880 } else if (flash & (FS_OP_ERR | FS_MPU_ERR)) {
1881 flash_op_err = true;
1883 * No ECC or operational errors happened. Check the number of
1884 * bits corrected and update the ecc_stats.corrected.
1889 stat = buffer & BS_CORRECTABLE_ERR_MSK;
1890 mtd->ecc_stats.corrected += stat;
1891 max_bitflips = max(max_bitflips, stat);
1895 data_buf += data_len;
1897 oob_buf += oob_len + ecc->bytes;
1903 if (!uncorrectable_cws)
1904 return max_bitflips;
1906 return check_for_erased_page(host, data_buf_start, oob_buf_start,
1907 uncorrectable_cws, page,
1912 * helper to perform the actual page read operation, used by ecc->read_page(),
1915 static int read_page_ecc(struct qcom_nand_host *host, u8 *data_buf,
1916 u8 *oob_buf, int page)
1918 struct nand_chip *chip = &host->chip;
1919 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1920 struct nand_ecc_ctrl *ecc = &chip->ecc;
1921 u8 *data_buf_start = data_buf, *oob_buf_start = oob_buf;
1924 config_nand_page_read(chip);
1926 /* queue cmd descs for each codeword */
1927 for (i = 0; i < ecc->steps; i++) {
1928 int data_size, oob_size;
1930 if (qcom_nandc_is_last_cw(ecc, i)) {
1931 data_size = ecc->size - ((ecc->steps - 1) << 2);
1932 oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
1935 data_size = host->cw_data;
1936 oob_size = host->ecc_bytes_hw + host->spare_bytes;
1939 if (nandc->props->is_bam) {
1940 if (data_buf && oob_buf) {
1941 nandc_set_read_loc(chip, i, 0, 0, data_size, 0);
1942 nandc_set_read_loc(chip, i, 1, data_size,
1944 } else if (data_buf) {
1945 nandc_set_read_loc(chip, i, 0, 0, data_size, 1);
1947 nandc_set_read_loc(chip, i, 0, data_size,
1952 config_nand_cw_read(chip, true, i);
1955 read_data_dma(nandc, FLASH_BUF_ACC, data_buf,
1959 * when ecc is enabled, the controller doesn't read the real
1960 * or dummy bad block markers in each chunk. To maintain a
1961 * consistent layout across RAW and ECC reads, we just
1962 * leave the real/dummy BBM offsets empty (i.e, filled with
1968 for (j = 0; j < host->bbm_size; j++)
1971 read_data_dma(nandc, FLASH_BUF_ACC + data_size,
1972 oob_buf, oob_size, 0);
1976 data_buf += data_size;
1978 oob_buf += oob_size;
1981 ret = submit_descs(nandc);
1985 dev_err(nandc->dev, "failure to read page/oob\n");
1989 return parse_read_errors(host, data_buf_start, oob_buf_start, page);
1993 * a helper that copies the last step/codeword of a page (containing free oob)
1994 * into our local buffer
1996 static int copy_last_cw(struct qcom_nand_host *host, int page)
1998 struct nand_chip *chip = &host->chip;
1999 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2000 struct nand_ecc_ctrl *ecc = &chip->ecc;
2004 clear_read_regs(nandc);
2006 size = host->use_ecc ? host->cw_data : host->cw_size;
2008 /* prepare a clean read buffer */
2009 memset(nandc->data_buffer, 0xff, size);
2011 set_address(host, host->cw_size * (ecc->steps - 1), page);
2012 update_rw_regs(host, 1, true, ecc->steps - 1);
2014 config_nand_single_cw_page_read(chip, host->use_ecc, ecc->steps - 1);
2016 read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer, size, 0);
2018 ret = submit_descs(nandc);
2020 dev_err(nandc->dev, "failed to copy last codeword\n");
2027 /* implements ecc->read_page() */
2028 static int qcom_nandc_read_page(struct nand_chip *chip, uint8_t *buf,
2029 int oob_required, int page)
2031 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2032 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2033 u8 *data_buf, *oob_buf = NULL;
2035 nand_read_page_op(chip, page, 0, NULL, 0);
2037 oob_buf = oob_required ? chip->oob_poi : NULL;
2039 clear_bam_transaction(nandc);
2041 return read_page_ecc(host, data_buf, oob_buf, page);
2044 /* implements ecc->read_page_raw() */
2045 static int qcom_nandc_read_page_raw(struct nand_chip *chip, uint8_t *buf,
2046 int oob_required, int page)
2048 struct mtd_info *mtd = nand_to_mtd(chip);
2049 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2050 struct nand_ecc_ctrl *ecc = &chip->ecc;
2052 u8 *data_buf = buf, *oob_buf = chip->oob_poi;
2054 for (cw = 0; cw < ecc->steps; cw++) {
2055 ret = qcom_nandc_read_cw_raw(mtd, chip, data_buf, oob_buf,
2060 data_buf += host->cw_data;
2061 oob_buf += ecc->bytes;
2067 /* implements ecc->read_oob() */
2068 static int qcom_nandc_read_oob(struct nand_chip *chip, int page)
2070 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2071 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2072 struct nand_ecc_ctrl *ecc = &chip->ecc;
2074 clear_read_regs(nandc);
2075 clear_bam_transaction(nandc);
2077 host->use_ecc = true;
2078 set_address(host, 0, page);
2079 update_rw_regs(host, ecc->steps, true, 0);
2081 return read_page_ecc(host, NULL, chip->oob_poi, page);
2084 /* implements ecc->write_page() */
2085 static int qcom_nandc_write_page(struct nand_chip *chip, const uint8_t *buf,
2086 int oob_required, int page)
2088 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2089 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2090 struct nand_ecc_ctrl *ecc = &chip->ecc;
2091 u8 *data_buf, *oob_buf;
2094 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
2096 clear_read_regs(nandc);
2097 clear_bam_transaction(nandc);
2099 data_buf = (u8 *)buf;
2100 oob_buf = chip->oob_poi;
2102 host->use_ecc = true;
2103 update_rw_regs(host, ecc->steps, false, 0);
2104 config_nand_page_write(chip);
2106 for (i = 0; i < ecc->steps; i++) {
2107 int data_size, oob_size;
2109 if (qcom_nandc_is_last_cw(ecc, i)) {
2110 data_size = ecc->size - ((ecc->steps - 1) << 2);
2111 oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
2114 data_size = host->cw_data;
2115 oob_size = ecc->bytes;
2119 write_data_dma(nandc, FLASH_BUF_ACC, data_buf, data_size,
2120 i == (ecc->steps - 1) ? NAND_BAM_NO_EOT : 0);
2123 * when ECC is enabled, we don't really need to write anything
2124 * to oob for the first n - 1 codewords since these oob regions
2125 * just contain ECC bytes that's written by the controller
2126 * itself. For the last codeword, we skip the bbm positions and
2127 * write to the free oob area.
2129 if (qcom_nandc_is_last_cw(ecc, i)) {
2130 oob_buf += host->bbm_size;
2132 write_data_dma(nandc, FLASH_BUF_ACC + data_size,
2133 oob_buf, oob_size, 0);
2136 config_nand_cw_write(chip);
2138 data_buf += data_size;
2139 oob_buf += oob_size;
2142 ret = submit_descs(nandc);
2144 dev_err(nandc->dev, "failure to write page\n");
2149 ret = nand_prog_page_end_op(chip);
2154 /* implements ecc->write_page_raw() */
2155 static int qcom_nandc_write_page_raw(struct nand_chip *chip,
2156 const uint8_t *buf, int oob_required,
2159 struct mtd_info *mtd = nand_to_mtd(chip);
2160 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2161 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2162 struct nand_ecc_ctrl *ecc = &chip->ecc;
2163 u8 *data_buf, *oob_buf;
2166 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
2167 clear_read_regs(nandc);
2168 clear_bam_transaction(nandc);
2170 data_buf = (u8 *)buf;
2171 oob_buf = chip->oob_poi;
2173 host->use_ecc = false;
2174 update_rw_regs(host, ecc->steps, false, 0);
2175 config_nand_page_write(chip);
2177 for (i = 0; i < ecc->steps; i++) {
2178 int data_size1, data_size2, oob_size1, oob_size2;
2179 int reg_off = FLASH_BUF_ACC;
2181 data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
2182 oob_size1 = host->bbm_size;
2184 if (qcom_nandc_is_last_cw(ecc, i)) {
2185 data_size2 = ecc->size - data_size1 -
2186 ((ecc->steps - 1) << 2);
2187 oob_size2 = (ecc->steps << 2) + host->ecc_bytes_hw +
2190 data_size2 = host->cw_data - data_size1;
2191 oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
2194 write_data_dma(nandc, reg_off, data_buf, data_size1,
2196 reg_off += data_size1;
2197 data_buf += data_size1;
2199 write_data_dma(nandc, reg_off, oob_buf, oob_size1,
2201 reg_off += oob_size1;
2202 oob_buf += oob_size1;
2204 write_data_dma(nandc, reg_off, data_buf, data_size2,
2206 reg_off += data_size2;
2207 data_buf += data_size2;
2209 write_data_dma(nandc, reg_off, oob_buf, oob_size2, 0);
2210 oob_buf += oob_size2;
2212 config_nand_cw_write(chip);
2215 ret = submit_descs(nandc);
2217 dev_err(nandc->dev, "failure to write raw page\n");
2222 ret = nand_prog_page_end_op(chip);
2228 * implements ecc->write_oob()
2230 * the NAND controller cannot write only data or only OOB within a codeword
2231 * since ECC is calculated for the combined codeword. So update the OOB from
2232 * chip->oob_poi, and pad the data area with OxFF before writing.
2234 static int qcom_nandc_write_oob(struct nand_chip *chip, int page)
2236 struct mtd_info *mtd = nand_to_mtd(chip);
2237 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2238 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2239 struct nand_ecc_ctrl *ecc = &chip->ecc;
2240 u8 *oob = chip->oob_poi;
2241 int data_size, oob_size;
2244 host->use_ecc = true;
2245 clear_bam_transaction(nandc);
2247 /* calculate the data and oob size for the last codeword/step */
2248 data_size = ecc->size - ((ecc->steps - 1) << 2);
2249 oob_size = mtd->oobavail;
2251 memset(nandc->data_buffer, 0xff, host->cw_data);
2252 /* override new oob content to last codeword */
2253 mtd_ooblayout_get_databytes(mtd, nandc->data_buffer + data_size, oob,
2256 set_address(host, host->cw_size * (ecc->steps - 1), page);
2257 update_rw_regs(host, 1, false, 0);
2259 config_nand_page_write(chip);
2260 write_data_dma(nandc, FLASH_BUF_ACC,
2261 nandc->data_buffer, data_size + oob_size, 0);
2262 config_nand_cw_write(chip);
2264 ret = submit_descs(nandc);
2269 dev_err(nandc->dev, "failure to write oob\n");
2273 return nand_prog_page_end_op(chip);
2276 static int qcom_nandc_block_bad(struct nand_chip *chip, loff_t ofs)
2278 struct mtd_info *mtd = nand_to_mtd(chip);
2279 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2280 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2281 struct nand_ecc_ctrl *ecc = &chip->ecc;
2282 int page, ret, bbpos, bad = 0;
2284 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
2287 * configure registers for a raw sub page read, the address is set to
2288 * the beginning of the last codeword, we don't care about reading ecc
2289 * portion of oob. we just want the first few bytes from this codeword
2290 * that contains the BBM
2292 host->use_ecc = false;
2294 clear_bam_transaction(nandc);
2295 ret = copy_last_cw(host, page);
2299 if (check_flash_errors(host, 1)) {
2300 dev_warn(nandc->dev, "error when trying to read BBM\n");
2304 bbpos = mtd->writesize - host->cw_size * (ecc->steps - 1);
2306 bad = nandc->data_buffer[bbpos] != 0xff;
2308 if (chip->options & NAND_BUSWIDTH_16)
2309 bad = bad || (nandc->data_buffer[bbpos + 1] != 0xff);
2314 static int qcom_nandc_block_markbad(struct nand_chip *chip, loff_t ofs)
2316 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2317 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2318 struct nand_ecc_ctrl *ecc = &chip->ecc;
2321 clear_read_regs(nandc);
2322 clear_bam_transaction(nandc);
2325 * to mark the BBM as bad, we flash the entire last codeword with 0s.
2326 * we don't care about the rest of the content in the codeword since
2327 * we aren't going to use this block again
2329 memset(nandc->data_buffer, 0x00, host->cw_size);
2331 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
2334 host->use_ecc = false;
2335 set_address(host, host->cw_size * (ecc->steps - 1), page);
2336 update_rw_regs(host, 1, false, ecc->steps - 1);
2338 config_nand_page_write(chip);
2339 write_data_dma(nandc, FLASH_BUF_ACC,
2340 nandc->data_buffer, host->cw_size, 0);
2341 config_nand_cw_write(chip);
2343 ret = submit_descs(nandc);
2348 dev_err(nandc->dev, "failure to update BBM\n");
2352 return nand_prog_page_end_op(chip);
2356 * the three functions below implement chip->legacy.read_byte(),
2357 * chip->legacy.read_buf() and chip->legacy.write_buf() respectively. these
2358 * aren't used for reading/writing page data, they are used for smaller data
2359 * like reading id, status etc
2361 static uint8_t qcom_nandc_read_byte(struct nand_chip *chip)
2363 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2364 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2365 u8 *buf = nandc->data_buffer;
2368 if (host->last_command == NAND_CMD_STATUS) {
2371 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
2376 if (nandc->buf_start < nandc->buf_count)
2377 ret = buf[nandc->buf_start++];
2382 static void qcom_nandc_read_buf(struct nand_chip *chip, uint8_t *buf, int len)
2384 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2385 int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
2387 memcpy(buf, nandc->data_buffer + nandc->buf_start, real_len);
2388 nandc->buf_start += real_len;
2391 static void qcom_nandc_write_buf(struct nand_chip *chip, const uint8_t *buf,
2394 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2395 int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
2397 memcpy(nandc->data_buffer + nandc->buf_start, buf, real_len);
2399 nandc->buf_start += real_len;
2402 /* we support only one external chip for now */
2403 static void qcom_nandc_select_chip(struct nand_chip *chip, int chipnr)
2405 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2410 dev_warn(nandc->dev, "invalid chip select\n");
2414 * NAND controller page layout info
2416 * Layout with ECC enabled:
2418 * |----------------------| |---------------------------------|
2419 * | xx.......yy| | *********xx.......yy|
2420 * | DATA xx..ECC..yy| | DATA **SPARE**xx..ECC..yy|
2421 * | (516) xx.......yy| | (516-n*4) **(n*4)**xx.......yy|
2422 * | xx.......yy| | *********xx.......yy|
2423 * |----------------------| |---------------------------------|
2424 * codeword 1,2..n-1 codeword n
2425 * <---(528/532 Bytes)--> <-------(528/532 Bytes)--------->
2427 * n = Number of codewords in the page
2429 * * = Spare/free bytes
2430 * x = Unused byte(s)
2431 * y = Reserved byte(s)
2433 * 2K page: n = 4, spare = 16 bytes
2434 * 4K page: n = 8, spare = 32 bytes
2435 * 8K page: n = 16, spare = 64 bytes
2437 * the qcom nand controller operates at a sub page/codeword level. each
2438 * codeword is 528 and 532 bytes for 4 bit and 8 bit ECC modes respectively.
2439 * the number of ECC bytes vary based on the ECC strength and the bus width.
2441 * the first n - 1 codewords contains 516 bytes of user data, the remaining
2442 * 12/16 bytes consist of ECC and reserved data. The nth codeword contains
2443 * both user data and spare(oobavail) bytes that sum up to 516 bytes.
2445 * When we access a page with ECC enabled, the reserved bytes(s) are not
2446 * accessible at all. When reading, we fill up these unreadable positions
2447 * with 0xffs. When writing, the controller skips writing the inaccessible
2450 * Layout with ECC disabled:
2452 * |------------------------------| |---------------------------------------|
2453 * | yy xx.......| | bb *********xx.......|
2454 * | DATA1 yy DATA2 xx..ECC..| | DATA1 bb DATA2 **SPARE**xx..ECC..|
2455 * | (size1) yy (size2) xx.......| | (size1) bb (size2) **(n*4)**xx.......|
2456 * | yy xx.......| | bb *********xx.......|
2457 * |------------------------------| |---------------------------------------|
2458 * codeword 1,2..n-1 codeword n
2459 * <-------(528/532 Bytes)------> <-----------(528/532 Bytes)----------->
2461 * n = Number of codewords in the page
2463 * * = Spare/free bytes
2464 * x = Unused byte(s)
2465 * y = Dummy Bad Bock byte(s)
2466 * b = Real Bad Block byte(s)
2467 * size1/size2 = function of codeword size and 'n'
2469 * when the ECC block is disabled, one reserved byte (or two for 16 bit bus
2470 * width) is now accessible. For the first n - 1 codewords, these are dummy Bad
2471 * Block Markers. In the last codeword, this position contains the real BBM
2473 * In order to have a consistent layout between RAW and ECC modes, we assume
2474 * the following OOB layout arrangement:
2476 * |-----------| |--------------------|
2477 * |yyxx.......| |bb*********xx.......|
2478 * |yyxx..ECC..| |bb*FREEOOB*xx..ECC..|
2479 * |yyxx.......| |bb*********xx.......|
2480 * |yyxx.......| |bb*********xx.......|
2481 * |-----------| |--------------------|
2482 * first n - 1 nth OOB region
2485 * n = Number of codewords in the page
2487 * * = FREE OOB bytes
2488 * y = Dummy bad block byte(s) (inaccessible when ECC enabled)
2489 * x = Unused byte(s)
2490 * b = Real bad block byte(s) (inaccessible when ECC enabled)
2492 * This layout is read as is when ECC is disabled. When ECC is enabled, the
2493 * inaccessible Bad Block byte(s) are ignored when we write to a page/oob,
2494 * and assumed as 0xffs when we read a page/oob. The ECC, unused and
2495 * dummy/real bad block bytes are grouped as ecc bytes (i.e, ecc->bytes is
2496 * the sum of the three).
2498 static int qcom_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
2499 struct mtd_oob_region *oobregion)
2501 struct nand_chip *chip = mtd_to_nand(mtd);
2502 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2503 struct nand_ecc_ctrl *ecc = &chip->ecc;
2509 oobregion->length = (ecc->bytes * (ecc->steps - 1)) +
2511 oobregion->offset = 0;
2513 oobregion->length = host->ecc_bytes_hw + host->spare_bytes;
2514 oobregion->offset = mtd->oobsize - oobregion->length;
2520 static int qcom_nand_ooblayout_free(struct mtd_info *mtd, int section,
2521 struct mtd_oob_region *oobregion)
2523 struct nand_chip *chip = mtd_to_nand(mtd);
2524 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2525 struct nand_ecc_ctrl *ecc = &chip->ecc;
2530 oobregion->length = ecc->steps * 4;
2531 oobregion->offset = ((ecc->steps - 1) * ecc->bytes) + host->bbm_size;
2536 static const struct mtd_ooblayout_ops qcom_nand_ooblayout_ops = {
2537 .ecc = qcom_nand_ooblayout_ecc,
2538 .free = qcom_nand_ooblayout_free,
2542 qcom_nandc_calc_ecc_bytes(int step_size, int strength)
2544 return strength == 4 ? 12 : 16;
2546 NAND_ECC_CAPS_SINGLE(qcom_nandc_ecc_caps, qcom_nandc_calc_ecc_bytes,
2547 NANDC_STEP_SIZE, 4, 8);
2549 static int qcom_nand_attach_chip(struct nand_chip *chip)
2551 struct mtd_info *mtd = nand_to_mtd(chip);
2552 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2553 struct nand_ecc_ctrl *ecc = &chip->ecc;
2554 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2555 int cwperpage, bad_block_byte, ret;
2559 /* controller only supports 512 bytes data steps */
2560 ecc->size = NANDC_STEP_SIZE;
2561 wide_bus = chip->options & NAND_BUSWIDTH_16 ? true : false;
2562 cwperpage = mtd->writesize / NANDC_STEP_SIZE;
2565 * Each CW has 4 available OOB bytes which will be protected with ECC
2566 * so remaining bytes can be used for ECC.
2568 ret = nand_ecc_choose_conf(chip, &qcom_nandc_ecc_caps,
2569 mtd->oobsize - (cwperpage * 4));
2571 dev_err(nandc->dev, "No valid ECC settings possible\n");
2575 if (ecc->strength >= 8) {
2576 /* 8 bit ECC defaults to BCH ECC on all platforms */
2577 host->bch_enabled = true;
2581 host->ecc_bytes_hw = 14;
2582 host->spare_bytes = 0;
2585 host->ecc_bytes_hw = 13;
2586 host->spare_bytes = 2;
2591 * if the controller supports BCH for 4 bit ECC, the controller
2592 * uses lesser bytes for ECC. If RS is used, the ECC bytes is
2595 if (nandc->props->ecc_modes & ECC_BCH_4BIT) {
2597 host->bch_enabled = true;
2601 host->ecc_bytes_hw = 8;
2602 host->spare_bytes = 2;
2605 host->ecc_bytes_hw = 7;
2606 host->spare_bytes = 4;
2611 host->ecc_bytes_hw = 10;
2614 host->spare_bytes = 0;
2617 host->spare_bytes = 1;
2624 * we consider ecc->bytes as the sum of all the non-data content in a
2625 * step. It gives us a clean representation of the oob area (even if
2626 * all the bytes aren't used for ECC).It is always 16 bytes for 8 bit
2627 * ECC and 12 bytes for 4 bit ECC
2629 ecc->bytes = host->ecc_bytes_hw + host->spare_bytes + host->bbm_size;
2631 ecc->read_page = qcom_nandc_read_page;
2632 ecc->read_page_raw = qcom_nandc_read_page_raw;
2633 ecc->read_oob = qcom_nandc_read_oob;
2634 ecc->write_page = qcom_nandc_write_page;
2635 ecc->write_page_raw = qcom_nandc_write_page_raw;
2636 ecc->write_oob = qcom_nandc_write_oob;
2638 ecc->engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
2640 mtd_set_ooblayout(mtd, &qcom_nand_ooblayout_ops);
2642 nandc->max_cwperpage = max_t(unsigned int, nandc->max_cwperpage,
2646 * DATA_UD_BYTES varies based on whether the read/write command protects
2647 * spare data with ECC too. We protect spare data by default, so we set
2648 * it to main + spare data, which are 512 and 4 bytes respectively.
2650 host->cw_data = 516;
2653 * total bytes in a step, either 528 bytes for 4 bit ECC, or 532 bytes
2656 host->cw_size = host->cw_data + ecc->bytes;
2657 bad_block_byte = mtd->writesize - host->cw_size * (cwperpage - 1) + 1;
2659 host->cfg0 = (cwperpage - 1) << CW_PER_PAGE
2660 | host->cw_data << UD_SIZE_BYTES
2661 | 0 << DISABLE_STATUS_AFTER_WRITE
2662 | 5 << NUM_ADDR_CYCLES
2663 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_RS
2664 | 0 << STATUS_BFR_READ
2665 | 1 << SET_RD_MODE_AFTER_STATUS
2666 | host->spare_bytes << SPARE_SIZE_BYTES;
2668 host->cfg1 = 7 << NAND_RECOVERY_CYCLES
2669 | 0 << CS_ACTIVE_BSY
2670 | bad_block_byte << BAD_BLOCK_BYTE_NUM
2671 | 0 << BAD_BLOCK_IN_SPARE_AREA
2672 | 2 << WR_RD_BSY_GAP
2673 | wide_bus << WIDE_FLASH
2674 | host->bch_enabled << ENABLE_BCH_ECC;
2676 host->cfg0_raw = (cwperpage - 1) << CW_PER_PAGE
2677 | host->cw_size << UD_SIZE_BYTES
2678 | 5 << NUM_ADDR_CYCLES
2679 | 0 << SPARE_SIZE_BYTES;
2681 host->cfg1_raw = 7 << NAND_RECOVERY_CYCLES
2682 | 0 << CS_ACTIVE_BSY
2683 | 17 << BAD_BLOCK_BYTE_NUM
2684 | 1 << BAD_BLOCK_IN_SPARE_AREA
2685 | 2 << WR_RD_BSY_GAP
2686 | wide_bus << WIDE_FLASH
2687 | 1 << DEV0_CFG1_ECC_DISABLE;
2689 host->ecc_bch_cfg = !host->bch_enabled << ECC_CFG_ECC_DISABLE
2691 | host->cw_data << ECC_NUM_DATA_BYTES
2692 | 1 << ECC_FORCE_CLK_OPEN
2693 | ecc_mode << ECC_MODE
2694 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_BCH;
2696 if (!nandc->props->qpic_v2)
2697 host->ecc_buf_cfg = 0x203 << NUM_STEPS;
2699 host->clrflashstatus = FS_READY_BSY_N;
2700 host->clrreadstatus = 0xc0;
2701 nandc->regs->erased_cw_detect_cfg_clr =
2702 cpu_to_le32(CLR_ERASED_PAGE_DET);
2703 nandc->regs->erased_cw_detect_cfg_set =
2704 cpu_to_le32(SET_ERASED_PAGE_DET);
2707 "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",
2708 host->cfg0, host->cfg1, host->ecc_buf_cfg, host->ecc_bch_cfg,
2709 host->cw_size, host->cw_data, ecc->strength, ecc->bytes,
2715 static const struct nand_controller_ops qcom_nandc_ops = {
2716 .attach_chip = qcom_nand_attach_chip,
2719 static void qcom_nandc_unalloc(struct qcom_nand_controller *nandc)
2721 if (nandc->props->is_bam) {
2722 if (!dma_mapping_error(nandc->dev, nandc->reg_read_dma))
2723 dma_unmap_single(nandc->dev, nandc->reg_read_dma,
2725 sizeof(*nandc->reg_read_buf),
2729 dma_release_channel(nandc->tx_chan);
2732 dma_release_channel(nandc->rx_chan);
2734 if (nandc->cmd_chan)
2735 dma_release_channel(nandc->cmd_chan);
2738 dma_release_channel(nandc->chan);
2742 static int qcom_nandc_alloc(struct qcom_nand_controller *nandc)
2746 ret = dma_set_coherent_mask(nandc->dev, DMA_BIT_MASK(32));
2748 dev_err(nandc->dev, "failed to set DMA mask\n");
2753 * we use the internal buffer for reading ONFI params, reading small
2754 * data like ID and status, and preforming read-copy-write operations
2755 * when writing to a codeword partially. 532 is the maximum possible
2756 * size of a codeword for our nand controller
2758 nandc->buf_size = 532;
2760 nandc->data_buffer = devm_kzalloc(nandc->dev, nandc->buf_size,
2762 if (!nandc->data_buffer)
2765 nandc->regs = devm_kzalloc(nandc->dev, sizeof(*nandc->regs),
2770 nandc->reg_read_buf = devm_kcalloc(nandc->dev,
2771 MAX_REG_RD, sizeof(*nandc->reg_read_buf),
2773 if (!nandc->reg_read_buf)
2776 if (nandc->props->is_bam) {
2777 nandc->reg_read_dma =
2778 dma_map_single(nandc->dev, nandc->reg_read_buf,
2780 sizeof(*nandc->reg_read_buf),
2782 if (dma_mapping_error(nandc->dev, nandc->reg_read_dma)) {
2783 dev_err(nandc->dev, "failed to DMA MAP reg buffer\n");
2787 nandc->tx_chan = dma_request_chan(nandc->dev, "tx");
2788 if (IS_ERR(nandc->tx_chan)) {
2789 ret = PTR_ERR(nandc->tx_chan);
2790 nandc->tx_chan = NULL;
2791 dev_err_probe(nandc->dev, ret,
2792 "tx DMA channel request failed\n");
2796 nandc->rx_chan = dma_request_chan(nandc->dev, "rx");
2797 if (IS_ERR(nandc->rx_chan)) {
2798 ret = PTR_ERR(nandc->rx_chan);
2799 nandc->rx_chan = NULL;
2800 dev_err_probe(nandc->dev, ret,
2801 "rx DMA channel request failed\n");
2805 nandc->cmd_chan = dma_request_chan(nandc->dev, "cmd");
2806 if (IS_ERR(nandc->cmd_chan)) {
2807 ret = PTR_ERR(nandc->cmd_chan);
2808 nandc->cmd_chan = NULL;
2809 dev_err_probe(nandc->dev, ret,
2810 "cmd DMA channel request failed\n");
2815 * Initially allocate BAM transaction to read ONFI param page.
2816 * After detecting all the devices, this BAM transaction will
2817 * be freed and the next BAM tranasction will be allocated with
2818 * maximum codeword size
2820 nandc->max_cwperpage = 1;
2821 nandc->bam_txn = alloc_bam_transaction(nandc);
2822 if (!nandc->bam_txn) {
2824 "failed to allocate bam transaction\n");
2829 nandc->chan = dma_request_chan(nandc->dev, "rxtx");
2830 if (IS_ERR(nandc->chan)) {
2831 ret = PTR_ERR(nandc->chan);
2833 dev_err_probe(nandc->dev, ret,
2834 "rxtx DMA channel request failed\n");
2839 INIT_LIST_HEAD(&nandc->desc_list);
2840 INIT_LIST_HEAD(&nandc->host_list);
2842 nand_controller_init(&nandc->controller);
2843 nandc->controller.ops = &qcom_nandc_ops;
2847 qcom_nandc_unalloc(nandc);
2851 /* one time setup of a few nand controller registers */
2852 static int qcom_nandc_setup(struct qcom_nand_controller *nandc)
2857 if (!nandc->props->is_qpic)
2858 nandc_write(nandc, SFLASHC_BURST_CFG, 0);
2860 if (!nandc->props->qpic_v2)
2861 nandc_write(nandc, dev_cmd_reg_addr(nandc, NAND_DEV_CMD_VLD),
2862 NAND_DEV_CMD_VLD_VAL);
2864 /* enable ADM or BAM DMA */
2865 if (nandc->props->is_bam) {
2866 nand_ctrl = nandc_read(nandc, NAND_CTRL);
2869 *NAND_CTRL is an operational registers, and CPU
2870 * access to operational registers are read only
2871 * in BAM mode. So update the NAND_CTRL register
2872 * only if it is not in BAM mode. In most cases BAM
2873 * mode will be enabled in bootloader
2875 if (!(nand_ctrl & BAM_MODE_EN))
2876 nandc_write(nandc, NAND_CTRL, nand_ctrl | BAM_MODE_EN);
2878 nandc_write(nandc, NAND_FLASH_CHIP_SELECT, DM_EN);
2881 /* save the original values of these registers */
2882 if (!nandc->props->qpic_v2) {
2883 nandc->cmd1 = nandc_read(nandc, dev_cmd_reg_addr(nandc, NAND_DEV_CMD1));
2884 nandc->vld = NAND_DEV_CMD_VLD_VAL;
2890 static const char * const probes[] = { "cmdlinepart", "ofpart", "qcomsmem", NULL };
2892 static int qcom_nand_host_init_and_register(struct qcom_nand_controller *nandc,
2893 struct qcom_nand_host *host,
2894 struct device_node *dn)
2896 struct nand_chip *chip = &host->chip;
2897 struct mtd_info *mtd = nand_to_mtd(chip);
2898 struct device *dev = nandc->dev;
2901 ret = of_property_read_u32(dn, "reg", &host->cs);
2903 dev_err(dev, "can't get chip-select\n");
2907 nand_set_flash_node(chip, dn);
2908 mtd->name = devm_kasprintf(dev, GFP_KERNEL, "qcom_nand.%d", host->cs);
2912 mtd->owner = THIS_MODULE;
2913 mtd->dev.parent = dev;
2915 chip->legacy.cmdfunc = qcom_nandc_command;
2916 chip->legacy.select_chip = qcom_nandc_select_chip;
2917 chip->legacy.read_byte = qcom_nandc_read_byte;
2918 chip->legacy.read_buf = qcom_nandc_read_buf;
2919 chip->legacy.write_buf = qcom_nandc_write_buf;
2920 chip->legacy.set_features = nand_get_set_features_notsupp;
2921 chip->legacy.get_features = nand_get_set_features_notsupp;
2924 * the bad block marker is readable only when we read the last codeword
2925 * of a page with ECC disabled. currently, the nand_base and nand_bbt
2926 * helpers don't allow us to read BB from a nand chip with ECC
2927 * disabled (MTD_OPS_PLACE_OOB is set by default). use the block_bad
2928 * and block_markbad helpers until we permanently switch to using
2929 * MTD_OPS_RAW for all drivers (with the help of badblockbits)
2931 chip->legacy.block_bad = qcom_nandc_block_bad;
2932 chip->legacy.block_markbad = qcom_nandc_block_markbad;
2934 chip->controller = &nandc->controller;
2935 chip->options |= NAND_NO_SUBPAGE_WRITE | NAND_USES_DMA |
2938 /* set up initial status value */
2939 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
2941 ret = nand_scan(chip, 1);
2945 if (nandc->props->is_bam) {
2946 free_bam_transaction(nandc);
2947 nandc->bam_txn = alloc_bam_transaction(nandc);
2948 if (!nandc->bam_txn) {
2950 "failed to allocate bam transaction\n");
2956 ret = mtd_device_parse_register(mtd, probes, NULL, NULL, 0);
2963 static int qcom_probe_nand_devices(struct qcom_nand_controller *nandc)
2965 struct device *dev = nandc->dev;
2966 struct device_node *dn = dev->of_node, *child;
2967 struct qcom_nand_host *host;
2970 for_each_available_child_of_node(dn, child) {
2971 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
2977 ret = qcom_nand_host_init_and_register(nandc, host, child);
2979 devm_kfree(dev, host);
2983 list_add_tail(&host->node, &nandc->host_list);
2989 /* parse custom DT properties here */
2990 static int qcom_nandc_parse_dt(struct platform_device *pdev)
2992 struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
2993 struct device_node *np = nandc->dev->of_node;
2996 if (!nandc->props->is_bam) {
2997 ret = of_property_read_u32(np, "qcom,cmd-crci",
3000 dev_err(nandc->dev, "command CRCI unspecified\n");
3004 ret = of_property_read_u32(np, "qcom,data-crci",
3007 dev_err(nandc->dev, "data CRCI unspecified\n");
3015 static int qcom_nandc_probe(struct platform_device *pdev)
3017 struct qcom_nand_controller *nandc;
3018 const void *dev_data;
3019 struct device *dev = &pdev->dev;
3020 struct resource *res;
3023 nandc = devm_kzalloc(&pdev->dev, sizeof(*nandc), GFP_KERNEL);
3027 platform_set_drvdata(pdev, nandc);
3030 dev_data = of_device_get_match_data(dev);
3032 dev_err(&pdev->dev, "failed to get device data\n");
3036 nandc->props = dev_data;
3038 nandc->core_clk = devm_clk_get(dev, "core");
3039 if (IS_ERR(nandc->core_clk))
3040 return PTR_ERR(nandc->core_clk);
3042 nandc->aon_clk = devm_clk_get(dev, "aon");
3043 if (IS_ERR(nandc->aon_clk))
3044 return PTR_ERR(nandc->aon_clk);
3046 ret = qcom_nandc_parse_dt(pdev);
3050 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3051 nandc->base = devm_ioremap_resource(dev, res);
3052 if (IS_ERR(nandc->base))
3053 return PTR_ERR(nandc->base);
3055 nandc->base_phys = res->start;
3056 nandc->base_dma = dma_map_resource(dev, res->start,
3058 DMA_BIDIRECTIONAL, 0);
3059 if (dma_mapping_error(dev, nandc->base_dma))
3062 ret = qcom_nandc_alloc(nandc);
3064 goto err_nandc_alloc;
3066 ret = clk_prepare_enable(nandc->core_clk);
3070 ret = clk_prepare_enable(nandc->aon_clk);
3074 ret = qcom_nandc_setup(nandc);
3078 ret = qcom_probe_nand_devices(nandc);
3085 clk_disable_unprepare(nandc->aon_clk);
3087 clk_disable_unprepare(nandc->core_clk);
3089 qcom_nandc_unalloc(nandc);
3091 dma_unmap_resource(dev, res->start, resource_size(res),
3092 DMA_BIDIRECTIONAL, 0);
3097 static int qcom_nandc_remove(struct platform_device *pdev)
3099 struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
3100 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3101 struct qcom_nand_host *host;
3102 struct nand_chip *chip;
3105 list_for_each_entry(host, &nandc->host_list, node) {
3107 ret = mtd_device_unregister(nand_to_mtd(chip));
3112 qcom_nandc_unalloc(nandc);
3114 clk_disable_unprepare(nandc->aon_clk);
3115 clk_disable_unprepare(nandc->core_clk);
3117 dma_unmap_resource(&pdev->dev, nandc->base_dma, resource_size(res),
3118 DMA_BIDIRECTIONAL, 0);
3123 static const struct qcom_nandc_props ipq806x_nandc_props = {
3124 .ecc_modes = (ECC_RS_4BIT | ECC_BCH_8BIT),
3126 .dev_cmd_reg_start = 0x0,
3129 static const struct qcom_nandc_props ipq4019_nandc_props = {
3130 .ecc_modes = (ECC_BCH_4BIT | ECC_BCH_8BIT),
3133 .dev_cmd_reg_start = 0x0,
3136 static const struct qcom_nandc_props ipq8074_nandc_props = {
3137 .ecc_modes = (ECC_BCH_4BIT | ECC_BCH_8BIT),
3140 .dev_cmd_reg_start = 0x7000,
3143 static const struct qcom_nandc_props sdx55_nandc_props = {
3144 .ecc_modes = (ECC_BCH_4BIT | ECC_BCH_8BIT),
3148 .dev_cmd_reg_start = 0x7000,
3152 * data will hold a struct pointer containing more differences once we support
3153 * more controller variants
3155 static const struct of_device_id qcom_nandc_of_match[] = {
3157 .compatible = "qcom,ipq806x-nand",
3158 .data = &ipq806x_nandc_props,
3161 .compatible = "qcom,ipq4019-nand",
3162 .data = &ipq4019_nandc_props,
3165 .compatible = "qcom,ipq6018-nand",
3166 .data = &ipq8074_nandc_props,
3169 .compatible = "qcom,ipq8074-nand",
3170 .data = &ipq8074_nandc_props,
3173 .compatible = "qcom,sdx55-nand",
3174 .data = &sdx55_nandc_props,
3178 MODULE_DEVICE_TABLE(of, qcom_nandc_of_match);
3180 static struct platform_driver qcom_nandc_driver = {
3182 .name = "qcom-nandc",
3183 .of_match_table = qcom_nandc_of_match,
3185 .probe = qcom_nandc_probe,
3186 .remove = qcom_nandc_remove,
3188 module_platform_driver(qcom_nandc_driver);
3190 MODULE_AUTHOR("Archit Taneja <architt@codeaurora.org>");
3191 MODULE_DESCRIPTION("Qualcomm NAND Controller driver");
3192 MODULE_LICENSE("GPL v2");