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_DEV1_ECC_CFG 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
52 /* dummy register offsets, used by write_reg_dma */
53 #define NAND_DEV_CMD1_RESTORE 0xdead
54 #define NAND_DEV_CMD_VLD_RESTORE 0xbeef
56 /* NAND_FLASH_CMD bits */
57 #define PAGE_ACC BIT(4)
58 #define LAST_PAGE BIT(5)
60 /* NAND_FLASH_CHIP_SELECT bits */
61 #define NAND_DEV_SEL 0
64 /* NAND_FLASH_STATUS bits */
65 #define FS_OP_ERR BIT(4)
66 #define FS_READY_BSY_N BIT(5)
67 #define FS_MPU_ERR BIT(8)
68 #define FS_DEVICE_STS_ERR BIT(16)
69 #define FS_DEVICE_WP BIT(23)
71 /* NAND_BUFFER_STATUS bits */
72 #define BS_UNCORRECTABLE_BIT BIT(8)
73 #define BS_CORRECTABLE_ERR_MSK 0x1f
75 /* NAND_DEVn_CFG0 bits */
76 #define DISABLE_STATUS_AFTER_WRITE 4
78 #define UD_SIZE_BYTES 9
79 #define ECC_PARITY_SIZE_BYTES_RS 19
80 #define SPARE_SIZE_BYTES 23
81 #define NUM_ADDR_CYCLES 27
82 #define STATUS_BFR_READ 30
83 #define SET_RD_MODE_AFTER_STATUS 31
85 /* NAND_DEVn_CFG0 bits */
86 #define DEV0_CFG1_ECC_DISABLE 0
88 #define NAND_RECOVERY_CYCLES 2
89 #define CS_ACTIVE_BSY 5
90 #define BAD_BLOCK_BYTE_NUM 6
91 #define BAD_BLOCK_IN_SPARE_AREA 16
92 #define WR_RD_BSY_GAP 17
93 #define ENABLE_BCH_ECC 27
95 /* NAND_DEV0_ECC_CFG bits */
96 #define ECC_CFG_ECC_DISABLE 0
97 #define ECC_SW_RESET 1
99 #define ECC_PARITY_SIZE_BYTES_BCH 8
100 #define ECC_NUM_DATA_BYTES 16
101 #define ECC_FORCE_CLK_OPEN 30
103 /* NAND_DEV_CMD1 bits */
106 /* NAND_DEV_CMD_VLD bits */
107 #define READ_START_VLD BIT(0)
108 #define READ_STOP_VLD BIT(1)
109 #define WRITE_START_VLD BIT(2)
110 #define ERASE_START_VLD BIT(3)
111 #define SEQ_READ_START_VLD BIT(4)
113 /* NAND_EBI2_ECC_BUF_CFG bits */
116 /* NAND_ERASED_CW_DETECT_CFG bits */
117 #define ERASED_CW_ECC_MASK 1
118 #define AUTO_DETECT_RES 0
119 #define MASK_ECC (1 << ERASED_CW_ECC_MASK)
120 #define RESET_ERASED_DET (1 << AUTO_DETECT_RES)
121 #define ACTIVE_ERASED_DET (0 << AUTO_DETECT_RES)
122 #define CLR_ERASED_PAGE_DET (RESET_ERASED_DET | MASK_ECC)
123 #define SET_ERASED_PAGE_DET (ACTIVE_ERASED_DET | MASK_ECC)
125 /* NAND_ERASED_CW_DETECT_STATUS bits */
126 #define PAGE_ALL_ERASED BIT(7)
127 #define CODEWORD_ALL_ERASED BIT(6)
128 #define PAGE_ERASED BIT(5)
129 #define CODEWORD_ERASED BIT(4)
130 #define ERASED_PAGE (PAGE_ALL_ERASED | PAGE_ERASED)
131 #define ERASED_CW (CODEWORD_ALL_ERASED | CODEWORD_ERASED)
133 /* NAND_READ_LOCATION_n bits */
134 #define READ_LOCATION_OFFSET 0
135 #define READ_LOCATION_SIZE 16
136 #define READ_LOCATION_LAST 31
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
145 #define OP_PAGE_READ 0x2
146 #define OP_PAGE_READ_WITH_ECC 0x3
147 #define OP_PAGE_READ_WITH_ECC_SPARE 0x4
148 #define OP_PROGRAM_PAGE 0x6
149 #define OP_PAGE_PROGRAM_WITH_ECC 0x7
150 #define OP_PROGRAM_PAGE_SPARE 0x9
151 #define OP_BLOCK_ERASE 0xa
152 #define OP_FETCH_ID 0xb
153 #define OP_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 #define BAM_MODE_EN BIT(0)
163 * the NAND controller performs reads/writes with ECC in 516 byte chunks.
164 * the driver calls the chunks 'step' or 'codeword' interchangeably
166 #define NANDC_STEP_SIZE 512
169 * the largest page size we support is 8K, this will have 16 steps/codewords
172 #define MAX_NUM_STEPS (SZ_8K / NANDC_STEP_SIZE)
174 /* we read at most 3 registers per codeword scan */
175 #define MAX_REG_RD (3 * MAX_NUM_STEPS)
177 /* ECC modes supported by the controller */
178 #define ECC_NONE BIT(0)
179 #define ECC_RS_4BIT BIT(1)
180 #define ECC_BCH_4BIT BIT(2)
181 #define ECC_BCH_8BIT BIT(3)
183 #define nandc_set_read_loc(nandc, reg, offset, size, is_last) \
184 nandc_set_reg(nandc, NAND_READ_LOCATION_##reg, \
185 ((offset) << READ_LOCATION_OFFSET) | \
186 ((size) << READ_LOCATION_SIZE) | \
187 ((is_last) << READ_LOCATION_LAST))
190 * Returns the actual register address for all NAND_DEV_ registers
191 * (i.e. NAND_DEV_CMD0, NAND_DEV_CMD1, NAND_DEV_CMD2 and NAND_DEV_CMD_VLD)
193 #define dev_cmd_reg_addr(nandc, reg) ((nandc)->props->dev_cmd_reg_start + (reg))
195 /* Returns the NAND register physical address */
196 #define nandc_reg_phys(chip, offset) ((chip)->base_phys + (offset))
198 /* Returns the dma address for reg read buffer */
199 #define reg_buf_dma_addr(chip, vaddr) \
200 ((chip)->reg_read_dma + \
201 ((uint8_t *)(vaddr) - (uint8_t *)(chip)->reg_read_buf))
203 #define QPIC_PER_CW_CMD_ELEMENTS 32
204 #define QPIC_PER_CW_CMD_SGL 32
205 #define QPIC_PER_CW_DATA_SGL 8
207 #define QPIC_NAND_COMPLETION_TIMEOUT msecs_to_jiffies(2000)
210 * Flags used in DMA descriptor preparation helper functions
211 * (i.e. read_reg_dma/write_reg_dma/read_data_dma/write_data_dma)
213 /* Don't set the EOT in current tx BAM sgl */
214 #define NAND_BAM_NO_EOT BIT(0)
215 /* Set the NWD flag in current BAM sgl */
216 #define NAND_BAM_NWD BIT(1)
217 /* Finish writing in the current BAM sgl and start writing in another BAM sgl */
218 #define NAND_BAM_NEXT_SGL BIT(2)
220 * Erased codeword status is being used two times in single transfer so this
221 * flag will determine the current value of erased codeword status register
223 #define NAND_ERASED_CW_SET BIT(4)
226 * This data type corresponds to the BAM transaction which will be used for all
228 * @bam_ce - the array of BAM command elements
229 * @cmd_sgl - sgl for NAND BAM command pipe
230 * @data_sgl - sgl for NAND BAM consumer/producer pipe
231 * @bam_ce_pos - the index in bam_ce which is available for next sgl
232 * @bam_ce_start - the index in bam_ce which marks the start position ce
233 * for current sgl. It will be used for size calculation
235 * @cmd_sgl_pos - current index in command sgl.
236 * @cmd_sgl_start - start index in command sgl.
237 * @tx_sgl_pos - current index in data sgl for tx.
238 * @tx_sgl_start - start index in data sgl for tx.
239 * @rx_sgl_pos - current index in data sgl for rx.
240 * @rx_sgl_start - start index in data sgl for rx.
241 * @wait_second_completion - wait for second DMA desc completion before making
242 * the NAND transfer completion.
243 * @txn_done - completion for NAND transfer.
244 * @last_data_desc - last DMA desc in data channel (tx/rx).
245 * @last_cmd_desc - last DMA desc in command channel.
247 struct bam_transaction {
248 struct bam_cmd_element *bam_ce;
249 struct scatterlist *cmd_sgl;
250 struct scatterlist *data_sgl;
259 bool wait_second_completion;
260 struct completion txn_done;
261 struct dma_async_tx_descriptor *last_data_desc;
262 struct dma_async_tx_descriptor *last_cmd_desc;
266 * This data type corresponds to the nand dma descriptor
267 * @list - list for desc_info
268 * @dir - DMA transfer direction
269 * @adm_sgl - sgl which will be used for single sgl dma descriptor. Only used by
271 * @bam_sgl - sgl which will be used for dma descriptor. Only used by BAM
272 * @sgl_cnt - number of SGL in bam_sgl. Only used by BAM
273 * @dma_desc - low level DMA engine descriptor
276 struct list_head node;
278 enum dma_data_direction dir;
280 struct scatterlist adm_sgl;
282 struct scatterlist *bam_sgl;
286 struct dma_async_tx_descriptor *dma_desc;
290 * holds the current register values that we want to write. acts as a contiguous
291 * chunk of memory which we use to write the controller registers through DMA.
304 __le32 clrflashstatus;
305 __le32 clrreadstatus;
314 __le32 read_location0;
315 __le32 read_location1;
316 __le32 read_location2;
317 __le32 read_location3;
319 __le32 erased_cw_detect_cfg_clr;
320 __le32 erased_cw_detect_cfg_set;
324 * NAND controller data struct
326 * @controller: base controller structure
327 * @host_list: list containing all the chips attached to the
329 * @dev: parent device
331 * @base_phys: physical base address of controller registers
332 * @base_dma: dma base address of controller registers
333 * @core_clk: controller clock
334 * @aon_clk: another controller clock
337 * @cmd_crci: ADM DMA CRCI for command flow control
338 * @data_crci: ADM DMA CRCI for data flow control
339 * @desc_list: DMA descriptor list (list of desc_infos)
341 * @data_buffer: our local DMA buffer for page read/writes,
342 * used when we can't use the buffer provided
343 * by upper layers directly
344 * @buf_size/count/start: markers for chip->legacy.read_buf/write_buf
346 * @reg_read_buf: local buffer for reading back registers via DMA
347 * @reg_read_dma: contains dma address for register read buffer
348 * @reg_read_pos: marker for data read in reg_read_buf
350 * @regs: a contiguous chunk of memory for DMA register
351 * writes. contains the register values to be
352 * written to controller
353 * @cmd1/vld: some fixed controller register values
354 * @props: properties of current NAND controller,
355 * initialized via DT match data
356 * @max_cwperpage: maximum QPIC codewords required. calculated
357 * from all connected NAND devices pagesize
359 struct qcom_nand_controller {
360 struct nand_controller controller;
361 struct list_head host_list;
366 phys_addr_t base_phys;
369 struct clk *core_clk;
373 /* will be used only by QPIC for BAM DMA */
375 struct dma_chan *tx_chan;
376 struct dma_chan *rx_chan;
377 struct dma_chan *cmd_chan;
380 /* will be used only by EBI2 for ADM DMA */
382 struct dma_chan *chan;
383 unsigned int cmd_crci;
384 unsigned int data_crci;
388 struct list_head desc_list;
389 struct bam_transaction *bam_txn;
395 unsigned int max_cwperpage;
397 __le32 *reg_read_buf;
398 dma_addr_t reg_read_dma;
401 struct nandc_regs *regs;
404 const struct qcom_nandc_props *props;
408 * NAND chip structure
410 * @chip: base NAND chip structure
411 * @node: list node to add itself to host_list in
412 * qcom_nand_controller
414 * @cs: chip select value for this chip
415 * @cw_size: the number of bytes in a single step/codeword
416 * of a page, consisting of all data, ecc, spare
418 * @cw_data: the number of bytes within a codeword protected
420 * @use_ecc: request the controller to use ECC for the
421 * upcoming read/write
422 * @bch_enabled: flag to tell whether BCH ECC mode is used
423 * @ecc_bytes_hw: ECC bytes used by controller hardware for this
425 * @status: value to be returned if NAND_CMD_STATUS command
427 * @last_command: keeps track of last command on this chip. used
428 * for reading correct status
430 * @cfg0, cfg1, cfg0_raw..: NANDc register configurations needed for
431 * ecc/non-ecc mode for the current nand flash
434 struct qcom_nand_host {
435 struct nand_chip chip;
436 struct list_head node;
450 u32 cfg0_raw, cfg1_raw;
458 * This data type corresponds to the NAND controller properties which varies
459 * among different NAND controllers.
460 * @ecc_modes - ecc mode for NAND
461 * @is_bam - whether NAND controller is using BAM
462 * @is_qpic - whether NAND CTRL is part of qpic IP
463 * @dev_cmd_reg_start - NAND_DEV_CMD_* registers starting offset
465 struct qcom_nandc_props {
469 u32 dev_cmd_reg_start;
472 /* Frees the BAM transaction memory */
473 static void free_bam_transaction(struct qcom_nand_controller *nandc)
475 struct bam_transaction *bam_txn = nandc->bam_txn;
477 devm_kfree(nandc->dev, bam_txn);
480 /* Allocates and Initializes the BAM transaction */
481 static struct bam_transaction *
482 alloc_bam_transaction(struct qcom_nand_controller *nandc)
484 struct bam_transaction *bam_txn;
486 unsigned int num_cw = nandc->max_cwperpage;
490 sizeof(*bam_txn) + num_cw *
491 ((sizeof(*bam_txn->bam_ce) * QPIC_PER_CW_CMD_ELEMENTS) +
492 (sizeof(*bam_txn->cmd_sgl) * QPIC_PER_CW_CMD_SGL) +
493 (sizeof(*bam_txn->data_sgl) * QPIC_PER_CW_DATA_SGL));
495 bam_txn_buf = devm_kzalloc(nandc->dev, bam_txn_size, GFP_KERNEL);
499 bam_txn = bam_txn_buf;
500 bam_txn_buf += sizeof(*bam_txn);
502 bam_txn->bam_ce = bam_txn_buf;
504 sizeof(*bam_txn->bam_ce) * QPIC_PER_CW_CMD_ELEMENTS * num_cw;
506 bam_txn->cmd_sgl = bam_txn_buf;
508 sizeof(*bam_txn->cmd_sgl) * QPIC_PER_CW_CMD_SGL * num_cw;
510 bam_txn->data_sgl = bam_txn_buf;
512 init_completion(&bam_txn->txn_done);
517 /* Clears the BAM transaction indexes */
518 static void clear_bam_transaction(struct qcom_nand_controller *nandc)
520 struct bam_transaction *bam_txn = nandc->bam_txn;
522 if (!nandc->props->is_bam)
525 bam_txn->bam_ce_pos = 0;
526 bam_txn->bam_ce_start = 0;
527 bam_txn->cmd_sgl_pos = 0;
528 bam_txn->cmd_sgl_start = 0;
529 bam_txn->tx_sgl_pos = 0;
530 bam_txn->tx_sgl_start = 0;
531 bam_txn->rx_sgl_pos = 0;
532 bam_txn->rx_sgl_start = 0;
533 bam_txn->last_data_desc = NULL;
534 bam_txn->wait_second_completion = false;
536 sg_init_table(bam_txn->cmd_sgl, nandc->max_cwperpage *
537 QPIC_PER_CW_CMD_SGL);
538 sg_init_table(bam_txn->data_sgl, nandc->max_cwperpage *
539 QPIC_PER_CW_DATA_SGL);
541 reinit_completion(&bam_txn->txn_done);
544 /* Callback for DMA descriptor completion */
545 static void qpic_bam_dma_done(void *data)
547 struct bam_transaction *bam_txn = data;
550 * In case of data transfer with NAND, 2 callbacks will be generated.
551 * One for command channel and another one for data channel.
552 * If current transaction has data descriptors
553 * (i.e. wait_second_completion is true), then set this to false
554 * and wait for second DMA descriptor completion.
556 if (bam_txn->wait_second_completion)
557 bam_txn->wait_second_completion = false;
559 complete(&bam_txn->txn_done);
562 static inline struct qcom_nand_host *to_qcom_nand_host(struct nand_chip *chip)
564 return container_of(chip, struct qcom_nand_host, chip);
567 static inline struct qcom_nand_controller *
568 get_qcom_nand_controller(struct nand_chip *chip)
570 return container_of(chip->controller, struct qcom_nand_controller,
574 static inline u32 nandc_read(struct qcom_nand_controller *nandc, int offset)
576 return ioread32(nandc->base + offset);
579 static inline void nandc_write(struct qcom_nand_controller *nandc, int offset,
582 iowrite32(val, nandc->base + offset);
585 static inline void nandc_read_buffer_sync(struct qcom_nand_controller *nandc,
588 if (!nandc->props->is_bam)
592 dma_sync_single_for_cpu(nandc->dev, nandc->reg_read_dma,
594 sizeof(*nandc->reg_read_buf),
597 dma_sync_single_for_device(nandc->dev, nandc->reg_read_dma,
599 sizeof(*nandc->reg_read_buf),
603 static __le32 *offset_to_nandc_reg(struct nandc_regs *regs, int offset)
612 case NAND_FLASH_CHIP_SELECT:
613 return ®s->chip_sel;
616 case NAND_FLASH_STATUS:
617 return ®s->clrflashstatus;
622 case NAND_DEV0_ECC_CFG:
623 return ®s->ecc_bch_cfg;
624 case NAND_READ_STATUS:
625 return ®s->clrreadstatus;
628 case NAND_DEV_CMD1_RESTORE:
629 return ®s->orig_cmd1;
630 case NAND_DEV_CMD_VLD:
632 case NAND_DEV_CMD_VLD_RESTORE:
633 return ®s->orig_vld;
634 case NAND_EBI2_ECC_BUF_CFG:
635 return ®s->ecc_buf_cfg;
636 case NAND_READ_LOCATION_0:
637 return ®s->read_location0;
638 case NAND_READ_LOCATION_1:
639 return ®s->read_location1;
640 case NAND_READ_LOCATION_2:
641 return ®s->read_location2;
642 case NAND_READ_LOCATION_3:
643 return ®s->read_location3;
649 static void nandc_set_reg(struct qcom_nand_controller *nandc, int offset,
652 struct nandc_regs *regs = nandc->regs;
655 reg = offset_to_nandc_reg(regs, offset);
658 *reg = cpu_to_le32(val);
661 /* helper to configure address register values */
662 static void set_address(struct qcom_nand_host *host, u16 column, int page)
664 struct nand_chip *chip = &host->chip;
665 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
667 if (chip->options & NAND_BUSWIDTH_16)
670 nandc_set_reg(nandc, NAND_ADDR0, page << 16 | column);
671 nandc_set_reg(nandc, NAND_ADDR1, page >> 16 & 0xff);
675 * update_rw_regs: set up read/write register values, these will be
676 * written to the NAND controller registers via DMA
678 * @num_cw: number of steps for the read/write operation
679 * @read: read or write operation
681 static void update_rw_regs(struct qcom_nand_host *host, int num_cw, bool read)
683 struct nand_chip *chip = &host->chip;
684 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
685 u32 cmd, cfg0, cfg1, ecc_bch_cfg;
689 cmd = OP_PAGE_READ_WITH_ECC | PAGE_ACC | LAST_PAGE;
691 cmd = OP_PAGE_READ | PAGE_ACC | LAST_PAGE;
693 cmd = OP_PROGRAM_PAGE | PAGE_ACC | LAST_PAGE;
697 cfg0 = (host->cfg0 & ~(7U << CW_PER_PAGE)) |
698 (num_cw - 1) << CW_PER_PAGE;
701 ecc_bch_cfg = host->ecc_bch_cfg;
703 cfg0 = (host->cfg0_raw & ~(7U << CW_PER_PAGE)) |
704 (num_cw - 1) << CW_PER_PAGE;
706 cfg1 = host->cfg1_raw;
707 ecc_bch_cfg = 1 << ECC_CFG_ECC_DISABLE;
710 nandc_set_reg(nandc, NAND_FLASH_CMD, cmd);
711 nandc_set_reg(nandc, NAND_DEV0_CFG0, cfg0);
712 nandc_set_reg(nandc, NAND_DEV0_CFG1, cfg1);
713 nandc_set_reg(nandc, NAND_DEV0_ECC_CFG, ecc_bch_cfg);
714 nandc_set_reg(nandc, NAND_EBI2_ECC_BUF_CFG, host->ecc_buf_cfg);
715 nandc_set_reg(nandc, NAND_FLASH_STATUS, host->clrflashstatus);
716 nandc_set_reg(nandc, NAND_READ_STATUS, host->clrreadstatus);
717 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
720 nandc_set_read_loc(nandc, 0, 0, host->use_ecc ?
721 host->cw_data : host->cw_size, 1);
725 * Maps the scatter gather list for DMA transfer and forms the DMA descriptor
726 * for BAM. This descriptor will be added in the NAND DMA descriptor queue
727 * which will be submitted to DMA engine.
729 static int prepare_bam_async_desc(struct qcom_nand_controller *nandc,
730 struct dma_chan *chan,
733 struct desc_info *desc;
734 struct scatterlist *sgl;
735 unsigned int sgl_cnt;
737 struct bam_transaction *bam_txn = nandc->bam_txn;
738 enum dma_transfer_direction dir_eng;
739 struct dma_async_tx_descriptor *dma_desc;
741 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
745 if (chan == nandc->cmd_chan) {
746 sgl = &bam_txn->cmd_sgl[bam_txn->cmd_sgl_start];
747 sgl_cnt = bam_txn->cmd_sgl_pos - bam_txn->cmd_sgl_start;
748 bam_txn->cmd_sgl_start = bam_txn->cmd_sgl_pos;
749 dir_eng = DMA_MEM_TO_DEV;
750 desc->dir = DMA_TO_DEVICE;
751 } else if (chan == nandc->tx_chan) {
752 sgl = &bam_txn->data_sgl[bam_txn->tx_sgl_start];
753 sgl_cnt = bam_txn->tx_sgl_pos - bam_txn->tx_sgl_start;
754 bam_txn->tx_sgl_start = bam_txn->tx_sgl_pos;
755 dir_eng = DMA_MEM_TO_DEV;
756 desc->dir = DMA_TO_DEVICE;
758 sgl = &bam_txn->data_sgl[bam_txn->rx_sgl_start];
759 sgl_cnt = bam_txn->rx_sgl_pos - bam_txn->rx_sgl_start;
760 bam_txn->rx_sgl_start = bam_txn->rx_sgl_pos;
761 dir_eng = DMA_DEV_TO_MEM;
762 desc->dir = DMA_FROM_DEVICE;
765 sg_mark_end(sgl + sgl_cnt - 1);
766 ret = dma_map_sg(nandc->dev, sgl, sgl_cnt, desc->dir);
768 dev_err(nandc->dev, "failure in mapping desc\n");
773 desc->sgl_cnt = sgl_cnt;
776 dma_desc = dmaengine_prep_slave_sg(chan, sgl, sgl_cnt, dir_eng,
780 dev_err(nandc->dev, "failure in prep desc\n");
781 dma_unmap_sg(nandc->dev, sgl, sgl_cnt, desc->dir);
786 desc->dma_desc = dma_desc;
788 /* update last data/command descriptor */
789 if (chan == nandc->cmd_chan)
790 bam_txn->last_cmd_desc = dma_desc;
792 bam_txn->last_data_desc = dma_desc;
794 list_add_tail(&desc->node, &nandc->desc_list);
800 * Prepares the command descriptor for BAM DMA which will be used for NAND
801 * register reads and writes. The command descriptor requires the command
802 * to be formed in command element type so this function uses the command
803 * element from bam transaction ce array and fills the same with required
804 * data. A single SGL can contain multiple command elements so
805 * NAND_BAM_NEXT_SGL will be used for starting the separate SGL
806 * after the current command element.
808 static int prep_bam_dma_desc_cmd(struct qcom_nand_controller *nandc, bool read,
809 int reg_off, const void *vaddr,
810 int size, unsigned int flags)
814 struct bam_cmd_element *bam_ce_buffer;
815 struct bam_transaction *bam_txn = nandc->bam_txn;
817 bam_ce_buffer = &bam_txn->bam_ce[bam_txn->bam_ce_pos];
819 /* fill the command desc */
820 for (i = 0; i < size; i++) {
822 bam_prep_ce(&bam_ce_buffer[i],
823 nandc_reg_phys(nandc, reg_off + 4 * i),
825 reg_buf_dma_addr(nandc,
826 (__le32 *)vaddr + i));
828 bam_prep_ce_le32(&bam_ce_buffer[i],
829 nandc_reg_phys(nandc, reg_off + 4 * i),
831 *((__le32 *)vaddr + i));
834 bam_txn->bam_ce_pos += size;
836 /* use the separate sgl after this command */
837 if (flags & NAND_BAM_NEXT_SGL) {
838 bam_ce_buffer = &bam_txn->bam_ce[bam_txn->bam_ce_start];
839 bam_ce_size = (bam_txn->bam_ce_pos -
840 bam_txn->bam_ce_start) *
841 sizeof(struct bam_cmd_element);
842 sg_set_buf(&bam_txn->cmd_sgl[bam_txn->cmd_sgl_pos],
843 bam_ce_buffer, bam_ce_size);
844 bam_txn->cmd_sgl_pos++;
845 bam_txn->bam_ce_start = bam_txn->bam_ce_pos;
847 if (flags & NAND_BAM_NWD) {
848 ret = prepare_bam_async_desc(nandc, nandc->cmd_chan,
860 * Prepares the data descriptor for BAM DMA which will be used for NAND
861 * data reads and writes.
863 static int prep_bam_dma_desc_data(struct qcom_nand_controller *nandc, bool read,
865 int size, unsigned int flags)
868 struct bam_transaction *bam_txn = nandc->bam_txn;
871 sg_set_buf(&bam_txn->data_sgl[bam_txn->rx_sgl_pos],
873 bam_txn->rx_sgl_pos++;
875 sg_set_buf(&bam_txn->data_sgl[bam_txn->tx_sgl_pos],
877 bam_txn->tx_sgl_pos++;
880 * BAM will only set EOT for DMA_PREP_INTERRUPT so if this flag
881 * is not set, form the DMA descriptor
883 if (!(flags & NAND_BAM_NO_EOT)) {
884 ret = prepare_bam_async_desc(nandc, nandc->tx_chan,
894 static int prep_adm_dma_desc(struct qcom_nand_controller *nandc, bool read,
895 int reg_off, const void *vaddr, int size,
898 struct desc_info *desc;
899 struct dma_async_tx_descriptor *dma_desc;
900 struct scatterlist *sgl;
901 struct dma_slave_config slave_conf;
902 enum dma_transfer_direction dir_eng;
905 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
909 sgl = &desc->adm_sgl;
911 sg_init_one(sgl, vaddr, size);
914 dir_eng = DMA_DEV_TO_MEM;
915 desc->dir = DMA_FROM_DEVICE;
917 dir_eng = DMA_MEM_TO_DEV;
918 desc->dir = DMA_TO_DEVICE;
921 ret = dma_map_sg(nandc->dev, sgl, 1, desc->dir);
927 memset(&slave_conf, 0x00, sizeof(slave_conf));
929 slave_conf.device_fc = flow_control;
931 slave_conf.src_maxburst = 16;
932 slave_conf.src_addr = nandc->base_dma + reg_off;
933 slave_conf.slave_id = nandc->data_crci;
935 slave_conf.dst_maxburst = 16;
936 slave_conf.dst_addr = nandc->base_dma + reg_off;
937 slave_conf.slave_id = nandc->cmd_crci;
940 ret = dmaengine_slave_config(nandc->chan, &slave_conf);
942 dev_err(nandc->dev, "failed to configure dma channel\n");
946 dma_desc = dmaengine_prep_slave_sg(nandc->chan, sgl, 1, dir_eng, 0);
948 dev_err(nandc->dev, "failed to prepare desc\n");
953 desc->dma_desc = dma_desc;
955 list_add_tail(&desc->node, &nandc->desc_list);
965 * read_reg_dma: prepares a descriptor to read a given number of
966 * contiguous registers to the reg_read_buf pointer
968 * @first: offset of the first register in the contiguous block
969 * @num_regs: number of registers to read
970 * @flags: flags to control DMA descriptor preparation
972 static int read_reg_dma(struct qcom_nand_controller *nandc, int first,
973 int num_regs, unsigned int flags)
975 bool flow_control = false;
978 vaddr = nandc->reg_read_buf + nandc->reg_read_pos;
979 nandc->reg_read_pos += num_regs;
981 if (first == NAND_DEV_CMD_VLD || first == NAND_DEV_CMD1)
982 first = dev_cmd_reg_addr(nandc, first);
984 if (nandc->props->is_bam)
985 return prep_bam_dma_desc_cmd(nandc, true, first, vaddr,
988 if (first == NAND_READ_ID || first == NAND_FLASH_STATUS)
991 return prep_adm_dma_desc(nandc, true, first, vaddr,
992 num_regs * sizeof(u32), flow_control);
996 * write_reg_dma: prepares a descriptor to write a given number of
997 * contiguous registers
999 * @first: offset of the first register in the contiguous block
1000 * @num_regs: number of registers to write
1001 * @flags: flags to control DMA descriptor preparation
1003 static int write_reg_dma(struct qcom_nand_controller *nandc, int first,
1004 int num_regs, unsigned int flags)
1006 bool flow_control = false;
1007 struct nandc_regs *regs = nandc->regs;
1010 vaddr = offset_to_nandc_reg(regs, first);
1012 if (first == NAND_ERASED_CW_DETECT_CFG) {
1013 if (flags & NAND_ERASED_CW_SET)
1014 vaddr = ®s->erased_cw_detect_cfg_set;
1016 vaddr = ®s->erased_cw_detect_cfg_clr;
1019 if (first == NAND_EXEC_CMD)
1020 flags |= NAND_BAM_NWD;
1022 if (first == NAND_DEV_CMD1_RESTORE || first == NAND_DEV_CMD1)
1023 first = dev_cmd_reg_addr(nandc, NAND_DEV_CMD1);
1025 if (first == NAND_DEV_CMD_VLD_RESTORE || first == NAND_DEV_CMD_VLD)
1026 first = dev_cmd_reg_addr(nandc, NAND_DEV_CMD_VLD);
1028 if (nandc->props->is_bam)
1029 return prep_bam_dma_desc_cmd(nandc, false, first, vaddr,
1032 if (first == NAND_FLASH_CMD)
1033 flow_control = true;
1035 return prep_adm_dma_desc(nandc, false, first, vaddr,
1036 num_regs * sizeof(u32), flow_control);
1040 * read_data_dma: prepares a DMA descriptor to transfer data from the
1041 * controller's internal buffer to the buffer 'vaddr'
1043 * @reg_off: offset within the controller's data buffer
1044 * @vaddr: virtual address of the buffer we want to write to
1045 * @size: DMA transaction size in bytes
1046 * @flags: flags to control DMA descriptor preparation
1048 static int read_data_dma(struct qcom_nand_controller *nandc, int reg_off,
1049 const u8 *vaddr, int size, unsigned int flags)
1051 if (nandc->props->is_bam)
1052 return prep_bam_dma_desc_data(nandc, true, vaddr, size, flags);
1054 return prep_adm_dma_desc(nandc, true, reg_off, vaddr, size, false);
1058 * write_data_dma: prepares a DMA descriptor to transfer data from
1059 * 'vaddr' to the controller's internal buffer
1061 * @reg_off: offset within the controller's data buffer
1062 * @vaddr: virtual address of the buffer we want to read from
1063 * @size: DMA transaction size in bytes
1064 * @flags: flags to control DMA descriptor preparation
1066 static int write_data_dma(struct qcom_nand_controller *nandc, int reg_off,
1067 const u8 *vaddr, int size, unsigned int flags)
1069 if (nandc->props->is_bam)
1070 return prep_bam_dma_desc_data(nandc, false, vaddr, size, flags);
1072 return prep_adm_dma_desc(nandc, false, reg_off, vaddr, size, false);
1076 * Helper to prepare DMA descriptors for configuring registers
1077 * before reading a NAND page.
1079 static void config_nand_page_read(struct qcom_nand_controller *nandc)
1081 write_reg_dma(nandc, NAND_ADDR0, 2, 0);
1082 write_reg_dma(nandc, NAND_DEV0_CFG0, 3, 0);
1083 write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1, 0);
1084 write_reg_dma(nandc, NAND_ERASED_CW_DETECT_CFG, 1, 0);
1085 write_reg_dma(nandc, NAND_ERASED_CW_DETECT_CFG, 1,
1086 NAND_ERASED_CW_SET | NAND_BAM_NEXT_SGL);
1090 * Helper to prepare DMA descriptors for configuring registers
1091 * before reading each codeword in NAND page.
1094 config_nand_cw_read(struct qcom_nand_controller *nandc, bool use_ecc)
1096 if (nandc->props->is_bam)
1097 write_reg_dma(nandc, NAND_READ_LOCATION_0, 4,
1100 write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
1101 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
1104 read_reg_dma(nandc, NAND_FLASH_STATUS, 2, 0);
1105 read_reg_dma(nandc, NAND_ERASED_CW_DETECT_STATUS, 1,
1108 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
1113 * Helper to prepare dma descriptors to configure registers needed for reading a
1114 * single codeword in page
1117 config_nand_single_cw_page_read(struct qcom_nand_controller *nandc,
1120 config_nand_page_read(nandc);
1121 config_nand_cw_read(nandc, use_ecc);
1125 * Helper to prepare DMA descriptors used to configure registers needed for
1126 * before writing a NAND page.
1128 static void config_nand_page_write(struct qcom_nand_controller *nandc)
1130 write_reg_dma(nandc, NAND_ADDR0, 2, 0);
1131 write_reg_dma(nandc, NAND_DEV0_CFG0, 3, 0);
1132 write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1,
1137 * Helper to prepare DMA descriptors for configuring registers
1138 * before writing each codeword in NAND page.
1140 static void config_nand_cw_write(struct qcom_nand_controller *nandc)
1142 write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
1143 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
1145 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
1147 write_reg_dma(nandc, NAND_FLASH_STATUS, 1, 0);
1148 write_reg_dma(nandc, NAND_READ_STATUS, 1, NAND_BAM_NEXT_SGL);
1152 * the following functions are used within chip->legacy.cmdfunc() to
1153 * perform different NAND_CMD_* commands
1156 /* sets up descriptors for NAND_CMD_PARAM */
1157 static int nandc_param(struct qcom_nand_host *host)
1159 struct nand_chip *chip = &host->chip;
1160 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1163 * NAND_CMD_PARAM is called before we know much about the FLASH chip
1164 * in use. we configure the controller to perform a raw read of 512
1165 * bytes to read onfi params
1167 nandc_set_reg(nandc, NAND_FLASH_CMD, OP_PAGE_READ | PAGE_ACC | LAST_PAGE);
1168 nandc_set_reg(nandc, NAND_ADDR0, 0);
1169 nandc_set_reg(nandc, NAND_ADDR1, 0);
1170 nandc_set_reg(nandc, NAND_DEV0_CFG0, 0 << CW_PER_PAGE
1171 | 512 << UD_SIZE_BYTES
1172 | 5 << NUM_ADDR_CYCLES
1173 | 0 << SPARE_SIZE_BYTES);
1174 nandc_set_reg(nandc, NAND_DEV0_CFG1, 7 << NAND_RECOVERY_CYCLES
1175 | 0 << CS_ACTIVE_BSY
1176 | 17 << BAD_BLOCK_BYTE_NUM
1177 | 1 << BAD_BLOCK_IN_SPARE_AREA
1178 | 2 << WR_RD_BSY_GAP
1180 | 1 << DEV0_CFG1_ECC_DISABLE);
1181 nandc_set_reg(nandc, NAND_EBI2_ECC_BUF_CFG, 1 << ECC_CFG_ECC_DISABLE);
1183 /* configure CMD1 and VLD for ONFI param probing */
1184 nandc_set_reg(nandc, NAND_DEV_CMD_VLD,
1185 (nandc->vld & ~READ_START_VLD));
1186 nandc_set_reg(nandc, NAND_DEV_CMD1,
1187 (nandc->cmd1 & ~(0xFF << READ_ADDR))
1188 | NAND_CMD_PARAM << READ_ADDR);
1190 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1192 nandc_set_reg(nandc, NAND_DEV_CMD1_RESTORE, nandc->cmd1);
1193 nandc_set_reg(nandc, NAND_DEV_CMD_VLD_RESTORE, nandc->vld);
1194 nandc_set_read_loc(nandc, 0, 0, 512, 1);
1196 write_reg_dma(nandc, NAND_DEV_CMD_VLD, 1, 0);
1197 write_reg_dma(nandc, NAND_DEV_CMD1, 1, NAND_BAM_NEXT_SGL);
1199 nandc->buf_count = 512;
1200 memset(nandc->data_buffer, 0xff, nandc->buf_count);
1202 config_nand_single_cw_page_read(nandc, false);
1204 read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer,
1205 nandc->buf_count, 0);
1207 /* restore CMD1 and VLD regs */
1208 write_reg_dma(nandc, NAND_DEV_CMD1_RESTORE, 1, 0);
1209 write_reg_dma(nandc, NAND_DEV_CMD_VLD_RESTORE, 1, NAND_BAM_NEXT_SGL);
1214 /* sets up descriptors for NAND_CMD_ERASE1 */
1215 static int erase_block(struct qcom_nand_host *host, int page_addr)
1217 struct nand_chip *chip = &host->chip;
1218 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1220 nandc_set_reg(nandc, NAND_FLASH_CMD,
1221 OP_BLOCK_ERASE | PAGE_ACC | LAST_PAGE);
1222 nandc_set_reg(nandc, NAND_ADDR0, page_addr);
1223 nandc_set_reg(nandc, NAND_ADDR1, 0);
1224 nandc_set_reg(nandc, NAND_DEV0_CFG0,
1225 host->cfg0_raw & ~(7 << CW_PER_PAGE));
1226 nandc_set_reg(nandc, NAND_DEV0_CFG1, host->cfg1_raw);
1227 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1228 nandc_set_reg(nandc, NAND_FLASH_STATUS, host->clrflashstatus);
1229 nandc_set_reg(nandc, NAND_READ_STATUS, host->clrreadstatus);
1231 write_reg_dma(nandc, NAND_FLASH_CMD, 3, NAND_BAM_NEXT_SGL);
1232 write_reg_dma(nandc, NAND_DEV0_CFG0, 2, NAND_BAM_NEXT_SGL);
1233 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
1235 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
1237 write_reg_dma(nandc, NAND_FLASH_STATUS, 1, 0);
1238 write_reg_dma(nandc, NAND_READ_STATUS, 1, NAND_BAM_NEXT_SGL);
1243 /* sets up descriptors for NAND_CMD_READID */
1244 static int read_id(struct qcom_nand_host *host, int column)
1246 struct nand_chip *chip = &host->chip;
1247 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1252 nandc_set_reg(nandc, NAND_FLASH_CMD, OP_FETCH_ID);
1253 nandc_set_reg(nandc, NAND_ADDR0, column);
1254 nandc_set_reg(nandc, NAND_ADDR1, 0);
1255 nandc_set_reg(nandc, NAND_FLASH_CHIP_SELECT,
1256 nandc->props->is_bam ? 0 : DM_EN);
1257 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1259 write_reg_dma(nandc, NAND_FLASH_CMD, 4, NAND_BAM_NEXT_SGL);
1260 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
1262 read_reg_dma(nandc, NAND_READ_ID, 1, NAND_BAM_NEXT_SGL);
1267 /* sets up descriptors for NAND_CMD_RESET */
1268 static int reset(struct qcom_nand_host *host)
1270 struct nand_chip *chip = &host->chip;
1271 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1273 nandc_set_reg(nandc, NAND_FLASH_CMD, OP_RESET_DEVICE);
1274 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1276 write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
1277 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
1279 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
1284 /* helpers to submit/free our list of dma descriptors */
1285 static int submit_descs(struct qcom_nand_controller *nandc)
1287 struct desc_info *desc;
1288 dma_cookie_t cookie = 0;
1289 struct bam_transaction *bam_txn = nandc->bam_txn;
1292 if (nandc->props->is_bam) {
1293 if (bam_txn->rx_sgl_pos > bam_txn->rx_sgl_start) {
1294 r = prepare_bam_async_desc(nandc, nandc->rx_chan, 0);
1299 if (bam_txn->tx_sgl_pos > bam_txn->tx_sgl_start) {
1300 r = prepare_bam_async_desc(nandc, nandc->tx_chan,
1301 DMA_PREP_INTERRUPT);
1306 if (bam_txn->cmd_sgl_pos > bam_txn->cmd_sgl_start) {
1307 r = prepare_bam_async_desc(nandc, nandc->cmd_chan,
1314 list_for_each_entry(desc, &nandc->desc_list, node)
1315 cookie = dmaengine_submit(desc->dma_desc);
1317 if (nandc->props->is_bam) {
1318 bam_txn->last_cmd_desc->callback = qpic_bam_dma_done;
1319 bam_txn->last_cmd_desc->callback_param = bam_txn;
1320 if (bam_txn->last_data_desc) {
1321 bam_txn->last_data_desc->callback = qpic_bam_dma_done;
1322 bam_txn->last_data_desc->callback_param = bam_txn;
1323 bam_txn->wait_second_completion = true;
1326 dma_async_issue_pending(nandc->tx_chan);
1327 dma_async_issue_pending(nandc->rx_chan);
1328 dma_async_issue_pending(nandc->cmd_chan);
1330 if (!wait_for_completion_timeout(&bam_txn->txn_done,
1331 QPIC_NAND_COMPLETION_TIMEOUT))
1334 if (dma_sync_wait(nandc->chan, cookie) != DMA_COMPLETE)
1341 static void free_descs(struct qcom_nand_controller *nandc)
1343 struct desc_info *desc, *n;
1345 list_for_each_entry_safe(desc, n, &nandc->desc_list, node) {
1346 list_del(&desc->node);
1348 if (nandc->props->is_bam)
1349 dma_unmap_sg(nandc->dev, desc->bam_sgl,
1350 desc->sgl_cnt, desc->dir);
1352 dma_unmap_sg(nandc->dev, &desc->adm_sgl, 1,
1359 /* reset the register read buffer for next NAND operation */
1360 static void clear_read_regs(struct qcom_nand_controller *nandc)
1362 nandc->reg_read_pos = 0;
1363 nandc_read_buffer_sync(nandc, false);
1366 static void pre_command(struct qcom_nand_host *host, int command)
1368 struct nand_chip *chip = &host->chip;
1369 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1371 nandc->buf_count = 0;
1372 nandc->buf_start = 0;
1373 host->use_ecc = false;
1374 host->last_command = command;
1376 clear_read_regs(nandc);
1378 if (command == NAND_CMD_RESET || command == NAND_CMD_READID ||
1379 command == NAND_CMD_PARAM || command == NAND_CMD_ERASE1)
1380 clear_bam_transaction(nandc);
1384 * this is called after NAND_CMD_PAGEPROG and NAND_CMD_ERASE1 to set our
1385 * privately maintained status byte, this status byte can be read after
1386 * NAND_CMD_STATUS is called
1388 static void parse_erase_write_errors(struct qcom_nand_host *host, int command)
1390 struct nand_chip *chip = &host->chip;
1391 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1392 struct nand_ecc_ctrl *ecc = &chip->ecc;
1396 num_cw = command == NAND_CMD_PAGEPROG ? ecc->steps : 1;
1397 nandc_read_buffer_sync(nandc, true);
1399 for (i = 0; i < num_cw; i++) {
1400 u32 flash_status = le32_to_cpu(nandc->reg_read_buf[i]);
1402 if (flash_status & FS_MPU_ERR)
1403 host->status &= ~NAND_STATUS_WP;
1405 if (flash_status & FS_OP_ERR || (i == (num_cw - 1) &&
1407 FS_DEVICE_STS_ERR)))
1408 host->status |= NAND_STATUS_FAIL;
1412 static void post_command(struct qcom_nand_host *host, int command)
1414 struct nand_chip *chip = &host->chip;
1415 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1418 case NAND_CMD_READID:
1419 nandc_read_buffer_sync(nandc, true);
1420 memcpy(nandc->data_buffer, nandc->reg_read_buf,
1423 case NAND_CMD_PAGEPROG:
1424 case NAND_CMD_ERASE1:
1425 parse_erase_write_errors(host, command);
1433 * Implements chip->legacy.cmdfunc. It's only used for a limited set of
1434 * commands. The rest of the commands wouldn't be called by upper layers.
1435 * For example, NAND_CMD_READOOB would never be called because we have our own
1436 * versions of read_oob ops for nand_ecc_ctrl.
1438 static void qcom_nandc_command(struct nand_chip *chip, unsigned int command,
1439 int column, int page_addr)
1441 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1442 struct nand_ecc_ctrl *ecc = &chip->ecc;
1443 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1447 pre_command(host, command);
1450 case NAND_CMD_RESET:
1455 case NAND_CMD_READID:
1456 nandc->buf_count = 4;
1457 ret = read_id(host, column);
1461 case NAND_CMD_PARAM:
1462 ret = nandc_param(host);
1466 case NAND_CMD_ERASE1:
1467 ret = erase_block(host, page_addr);
1471 case NAND_CMD_READ0:
1472 /* we read the entire page for now */
1473 WARN_ON(column != 0);
1475 host->use_ecc = true;
1476 set_address(host, 0, page_addr);
1477 update_rw_regs(host, ecc->steps, true);
1480 case NAND_CMD_SEQIN:
1481 WARN_ON(column != 0);
1482 set_address(host, 0, page_addr);
1485 case NAND_CMD_PAGEPROG:
1486 case NAND_CMD_STATUS:
1493 dev_err(nandc->dev, "failure executing command %d\n",
1500 ret = submit_descs(nandc);
1503 "failure submitting descs for command %d\n",
1509 post_command(host, command);
1513 * when using BCH ECC, the HW flags an error in NAND_FLASH_STATUS if it read
1514 * an erased CW, and reports an erased CW in NAND_ERASED_CW_DETECT_STATUS.
1516 * when using RS ECC, the HW reports the same erros when reading an erased CW,
1517 * but it notifies that it is an erased CW by placing special characters at
1518 * certain offsets in the buffer.
1520 * verify if the page is erased or not, and fix up the page for RS ECC by
1521 * replacing the special characters with 0xff.
1523 static bool erased_chunk_check_and_fixup(u8 *data_buf, int data_len)
1528 * an erased page flags an error in NAND_FLASH_STATUS, check if the page
1529 * is erased by looking for 0x54s at offsets 3 and 175 from the
1530 * beginning of each codeword
1533 empty1 = data_buf[3];
1534 empty2 = data_buf[175];
1537 * if the erased codework markers, if they exist override them with
1540 if ((empty1 == 0x54 && empty2 == 0xff) ||
1541 (empty1 == 0xff && empty2 == 0x54)) {
1543 data_buf[175] = 0xff;
1547 * check if the entire chunk contains 0xffs or not. if it doesn't, then
1548 * restore the original values at the special offsets
1550 if (memchr_inv(data_buf, 0xff, data_len)) {
1551 data_buf[3] = empty1;
1552 data_buf[175] = empty2;
1566 /* reads back FLASH_STATUS register set by the controller */
1567 static int check_flash_errors(struct qcom_nand_host *host, int cw_cnt)
1569 struct nand_chip *chip = &host->chip;
1570 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1573 nandc_read_buffer_sync(nandc, true);
1575 for (i = 0; i < cw_cnt; i++) {
1576 u32 flash = le32_to_cpu(nandc->reg_read_buf[i]);
1578 if (flash & (FS_OP_ERR | FS_MPU_ERR))
1585 /* performs raw read for one codeword */
1587 qcom_nandc_read_cw_raw(struct mtd_info *mtd, struct nand_chip *chip,
1588 u8 *data_buf, u8 *oob_buf, int page, int cw)
1590 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1591 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1592 struct nand_ecc_ctrl *ecc = &chip->ecc;
1593 int data_size1, data_size2, oob_size1, oob_size2;
1594 int ret, reg_off = FLASH_BUF_ACC, read_loc = 0;
1596 nand_read_page_op(chip, page, 0, NULL, 0);
1597 host->use_ecc = false;
1599 clear_bam_transaction(nandc);
1600 set_address(host, host->cw_size * cw, page);
1601 update_rw_regs(host, 1, true);
1602 config_nand_page_read(nandc);
1604 data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
1605 oob_size1 = host->bbm_size;
1607 if (cw == (ecc->steps - 1)) {
1608 data_size2 = ecc->size - data_size1 -
1609 ((ecc->steps - 1) * 4);
1610 oob_size2 = (ecc->steps * 4) + host->ecc_bytes_hw +
1613 data_size2 = host->cw_data - data_size1;
1614 oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
1617 if (nandc->props->is_bam) {
1618 nandc_set_read_loc(nandc, 0, read_loc, data_size1, 0);
1619 read_loc += data_size1;
1621 nandc_set_read_loc(nandc, 1, read_loc, oob_size1, 0);
1622 read_loc += oob_size1;
1624 nandc_set_read_loc(nandc, 2, read_loc, data_size2, 0);
1625 read_loc += data_size2;
1627 nandc_set_read_loc(nandc, 3, read_loc, oob_size2, 1);
1630 config_nand_cw_read(nandc, false);
1632 read_data_dma(nandc, reg_off, data_buf, data_size1, 0);
1633 reg_off += data_size1;
1635 read_data_dma(nandc, reg_off, oob_buf, oob_size1, 0);
1636 reg_off += oob_size1;
1638 read_data_dma(nandc, reg_off, data_buf + data_size1, data_size2, 0);
1639 reg_off += data_size2;
1641 read_data_dma(nandc, reg_off, oob_buf + oob_size1, oob_size2, 0);
1643 ret = submit_descs(nandc);
1646 dev_err(nandc->dev, "failure to read raw cw %d\n", cw);
1650 return check_flash_errors(host, 1);
1654 * Bitflips can happen in erased codewords also so this function counts the
1655 * number of 0 in each CW for which ECC engine returns the uncorrectable
1656 * error. The page will be assumed as erased if this count is less than or
1657 * equal to the ecc->strength for each CW.
1659 * 1. Both DATA and OOB need to be checked for number of 0. The
1660 * top-level API can be called with only data buf or OOB buf so use
1661 * chip->data_buf if data buf is null and chip->oob_poi if oob buf
1662 * is null for copying the raw bytes.
1663 * 2. Perform raw read for all the CW which has uncorrectable errors.
1664 * 3. For each CW, check the number of 0 in cw_data and usable OOB bytes.
1665 * The BBM and spare bytes bit flip won’t affect the ECC so don’t check
1666 * the number of bitflips in this area.
1669 check_for_erased_page(struct qcom_nand_host *host, u8 *data_buf,
1670 u8 *oob_buf, unsigned long uncorrectable_cws,
1671 int page, unsigned int max_bitflips)
1673 struct nand_chip *chip = &host->chip;
1674 struct mtd_info *mtd = nand_to_mtd(chip);
1675 struct nand_ecc_ctrl *ecc = &chip->ecc;
1676 u8 *cw_data_buf, *cw_oob_buf;
1677 int cw, data_size, oob_size, ret = 0;
1680 data_buf = nand_get_data_buf(chip);
1683 nand_get_data_buf(chip);
1684 oob_buf = chip->oob_poi;
1687 for_each_set_bit(cw, &uncorrectable_cws, ecc->steps) {
1688 if (cw == (ecc->steps - 1)) {
1689 data_size = ecc->size - ((ecc->steps - 1) * 4);
1690 oob_size = (ecc->steps * 4) + host->ecc_bytes_hw;
1692 data_size = host->cw_data;
1693 oob_size = host->ecc_bytes_hw;
1696 /* determine starting buffer address for current CW */
1697 cw_data_buf = data_buf + (cw * host->cw_data);
1698 cw_oob_buf = oob_buf + (cw * ecc->bytes);
1700 ret = qcom_nandc_read_cw_raw(mtd, chip, cw_data_buf,
1701 cw_oob_buf, page, cw);
1706 * make sure it isn't an erased page reported
1707 * as not-erased by HW because of a few bitflips
1709 ret = nand_check_erased_ecc_chunk(cw_data_buf, data_size,
1710 cw_oob_buf + host->bbm_size,
1714 mtd->ecc_stats.failed++;
1716 mtd->ecc_stats.corrected += ret;
1717 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1721 return max_bitflips;
1725 * reads back status registers set by the controller to notify page read
1726 * errors. this is equivalent to what 'ecc->correct()' would do.
1728 static int parse_read_errors(struct qcom_nand_host *host, u8 *data_buf,
1729 u8 *oob_buf, int page)
1731 struct nand_chip *chip = &host->chip;
1732 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1733 struct mtd_info *mtd = nand_to_mtd(chip);
1734 struct nand_ecc_ctrl *ecc = &chip->ecc;
1735 unsigned int max_bitflips = 0, uncorrectable_cws = 0;
1736 struct read_stats *buf;
1737 bool flash_op_err = false, erased;
1739 u8 *data_buf_start = data_buf, *oob_buf_start = oob_buf;
1741 buf = (struct read_stats *)nandc->reg_read_buf;
1742 nandc_read_buffer_sync(nandc, true);
1744 for (i = 0; i < ecc->steps; i++, buf++) {
1745 u32 flash, buffer, erased_cw;
1746 int data_len, oob_len;
1748 if (i == (ecc->steps - 1)) {
1749 data_len = ecc->size - ((ecc->steps - 1) << 2);
1750 oob_len = ecc->steps << 2;
1752 data_len = host->cw_data;
1756 flash = le32_to_cpu(buf->flash);
1757 buffer = le32_to_cpu(buf->buffer);
1758 erased_cw = le32_to_cpu(buf->erased_cw);
1761 * Check ECC failure for each codeword. ECC failure can
1762 * happen in either of the following conditions
1763 * 1. If number of bitflips are greater than ECC engine
1765 * 2. If this codeword contains all 0xff for which erased
1766 * codeword detection check will be done.
1768 if ((flash & FS_OP_ERR) && (buffer & BS_UNCORRECTABLE_BIT)) {
1770 * For BCH ECC, ignore erased codeword errors, if
1771 * ERASED_CW bits are set.
1773 if (host->bch_enabled) {
1774 erased = (erased_cw & ERASED_CW) == ERASED_CW ?
1777 * For RS ECC, HW reports the erased CW by placing
1778 * special characters at certain offsets in the buffer.
1779 * These special characters will be valid only if
1780 * complete page is read i.e. data_buf is not NULL.
1782 } else if (data_buf) {
1783 erased = erased_chunk_check_and_fixup(data_buf,
1790 uncorrectable_cws |= BIT(i);
1792 * Check if MPU or any other operational error (timeout,
1793 * device failure, etc.) happened for this codeword and
1794 * make flash_op_err true. If flash_op_err is set, then
1795 * EIO will be returned for page read.
1797 } else if (flash & (FS_OP_ERR | FS_MPU_ERR)) {
1798 flash_op_err = true;
1800 * No ECC or operational errors happened. Check the number of
1801 * bits corrected and update the ecc_stats.corrected.
1806 stat = buffer & BS_CORRECTABLE_ERR_MSK;
1807 mtd->ecc_stats.corrected += stat;
1808 max_bitflips = max(max_bitflips, stat);
1812 data_buf += data_len;
1814 oob_buf += oob_len + ecc->bytes;
1820 if (!uncorrectable_cws)
1821 return max_bitflips;
1823 return check_for_erased_page(host, data_buf_start, oob_buf_start,
1824 uncorrectable_cws, page,
1829 * helper to perform the actual page read operation, used by ecc->read_page(),
1832 static int read_page_ecc(struct qcom_nand_host *host, u8 *data_buf,
1833 u8 *oob_buf, int page)
1835 struct nand_chip *chip = &host->chip;
1836 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1837 struct nand_ecc_ctrl *ecc = &chip->ecc;
1838 u8 *data_buf_start = data_buf, *oob_buf_start = oob_buf;
1841 config_nand_page_read(nandc);
1843 /* queue cmd descs for each codeword */
1844 for (i = 0; i < ecc->steps; i++) {
1845 int data_size, oob_size;
1847 if (i == (ecc->steps - 1)) {
1848 data_size = ecc->size - ((ecc->steps - 1) << 2);
1849 oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
1852 data_size = host->cw_data;
1853 oob_size = host->ecc_bytes_hw + host->spare_bytes;
1856 if (nandc->props->is_bam) {
1857 if (data_buf && oob_buf) {
1858 nandc_set_read_loc(nandc, 0, 0, data_size, 0);
1859 nandc_set_read_loc(nandc, 1, data_size,
1861 } else if (data_buf) {
1862 nandc_set_read_loc(nandc, 0, 0, data_size, 1);
1864 nandc_set_read_loc(nandc, 0, data_size,
1869 config_nand_cw_read(nandc, true);
1872 read_data_dma(nandc, FLASH_BUF_ACC, data_buf,
1876 * when ecc is enabled, the controller doesn't read the real
1877 * or dummy bad block markers in each chunk. To maintain a
1878 * consistent layout across RAW and ECC reads, we just
1879 * leave the real/dummy BBM offsets empty (i.e, filled with
1885 for (j = 0; j < host->bbm_size; j++)
1888 read_data_dma(nandc, FLASH_BUF_ACC + data_size,
1889 oob_buf, oob_size, 0);
1893 data_buf += data_size;
1895 oob_buf += oob_size;
1898 ret = submit_descs(nandc);
1902 dev_err(nandc->dev, "failure to read page/oob\n");
1906 return parse_read_errors(host, data_buf_start, oob_buf_start, page);
1910 * a helper that copies the last step/codeword of a page (containing free oob)
1911 * into our local buffer
1913 static int copy_last_cw(struct qcom_nand_host *host, int page)
1915 struct nand_chip *chip = &host->chip;
1916 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1917 struct nand_ecc_ctrl *ecc = &chip->ecc;
1921 clear_read_regs(nandc);
1923 size = host->use_ecc ? host->cw_data : host->cw_size;
1925 /* prepare a clean read buffer */
1926 memset(nandc->data_buffer, 0xff, size);
1928 set_address(host, host->cw_size * (ecc->steps - 1), page);
1929 update_rw_regs(host, 1, true);
1931 config_nand_single_cw_page_read(nandc, host->use_ecc);
1933 read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer, size, 0);
1935 ret = submit_descs(nandc);
1937 dev_err(nandc->dev, "failed to copy last codeword\n");
1944 /* implements ecc->read_page() */
1945 static int qcom_nandc_read_page(struct nand_chip *chip, uint8_t *buf,
1946 int oob_required, int page)
1948 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1949 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1950 u8 *data_buf, *oob_buf = NULL;
1952 nand_read_page_op(chip, page, 0, NULL, 0);
1954 oob_buf = oob_required ? chip->oob_poi : NULL;
1956 clear_bam_transaction(nandc);
1958 return read_page_ecc(host, data_buf, oob_buf, page);
1961 /* implements ecc->read_page_raw() */
1962 static int qcom_nandc_read_page_raw(struct nand_chip *chip, uint8_t *buf,
1963 int oob_required, int page)
1965 struct mtd_info *mtd = nand_to_mtd(chip);
1966 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1967 struct nand_ecc_ctrl *ecc = &chip->ecc;
1969 u8 *data_buf = buf, *oob_buf = chip->oob_poi;
1971 for (cw = 0; cw < ecc->steps; cw++) {
1972 ret = qcom_nandc_read_cw_raw(mtd, chip, data_buf, oob_buf,
1977 data_buf += host->cw_data;
1978 oob_buf += ecc->bytes;
1984 /* implements ecc->read_oob() */
1985 static int qcom_nandc_read_oob(struct nand_chip *chip, int page)
1987 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1988 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1989 struct nand_ecc_ctrl *ecc = &chip->ecc;
1991 clear_read_regs(nandc);
1992 clear_bam_transaction(nandc);
1994 host->use_ecc = true;
1995 set_address(host, 0, page);
1996 update_rw_regs(host, ecc->steps, true);
1998 return read_page_ecc(host, NULL, chip->oob_poi, page);
2001 /* implements ecc->write_page() */
2002 static int qcom_nandc_write_page(struct nand_chip *chip, const uint8_t *buf,
2003 int oob_required, int page)
2005 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2006 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2007 struct nand_ecc_ctrl *ecc = &chip->ecc;
2008 u8 *data_buf, *oob_buf;
2011 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
2013 clear_read_regs(nandc);
2014 clear_bam_transaction(nandc);
2016 data_buf = (u8 *)buf;
2017 oob_buf = chip->oob_poi;
2019 host->use_ecc = true;
2020 update_rw_regs(host, ecc->steps, false);
2021 config_nand_page_write(nandc);
2023 for (i = 0; i < ecc->steps; i++) {
2024 int data_size, oob_size;
2026 if (i == (ecc->steps - 1)) {
2027 data_size = ecc->size - ((ecc->steps - 1) << 2);
2028 oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
2031 data_size = host->cw_data;
2032 oob_size = ecc->bytes;
2036 write_data_dma(nandc, FLASH_BUF_ACC, data_buf, data_size,
2037 i == (ecc->steps - 1) ? NAND_BAM_NO_EOT : 0);
2040 * when ECC is enabled, we don't really need to write anything
2041 * to oob for the first n - 1 codewords since these oob regions
2042 * just contain ECC bytes that's written by the controller
2043 * itself. For the last codeword, we skip the bbm positions and
2044 * write to the free oob area.
2046 if (i == (ecc->steps - 1)) {
2047 oob_buf += host->bbm_size;
2049 write_data_dma(nandc, FLASH_BUF_ACC + data_size,
2050 oob_buf, oob_size, 0);
2053 config_nand_cw_write(nandc);
2055 data_buf += data_size;
2056 oob_buf += oob_size;
2059 ret = submit_descs(nandc);
2061 dev_err(nandc->dev, "failure to write page\n");
2066 ret = nand_prog_page_end_op(chip);
2071 /* implements ecc->write_page_raw() */
2072 static int qcom_nandc_write_page_raw(struct nand_chip *chip,
2073 const uint8_t *buf, int oob_required,
2076 struct mtd_info *mtd = nand_to_mtd(chip);
2077 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2078 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2079 struct nand_ecc_ctrl *ecc = &chip->ecc;
2080 u8 *data_buf, *oob_buf;
2083 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
2084 clear_read_regs(nandc);
2085 clear_bam_transaction(nandc);
2087 data_buf = (u8 *)buf;
2088 oob_buf = chip->oob_poi;
2090 host->use_ecc = false;
2091 update_rw_regs(host, ecc->steps, false);
2092 config_nand_page_write(nandc);
2094 for (i = 0; i < ecc->steps; i++) {
2095 int data_size1, data_size2, oob_size1, oob_size2;
2096 int reg_off = FLASH_BUF_ACC;
2098 data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
2099 oob_size1 = host->bbm_size;
2101 if (i == (ecc->steps - 1)) {
2102 data_size2 = ecc->size - data_size1 -
2103 ((ecc->steps - 1) << 2);
2104 oob_size2 = (ecc->steps << 2) + host->ecc_bytes_hw +
2107 data_size2 = host->cw_data - data_size1;
2108 oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
2111 write_data_dma(nandc, reg_off, data_buf, data_size1,
2113 reg_off += data_size1;
2114 data_buf += data_size1;
2116 write_data_dma(nandc, reg_off, oob_buf, oob_size1,
2118 reg_off += oob_size1;
2119 oob_buf += oob_size1;
2121 write_data_dma(nandc, reg_off, data_buf, data_size2,
2123 reg_off += data_size2;
2124 data_buf += data_size2;
2126 write_data_dma(nandc, reg_off, oob_buf, oob_size2, 0);
2127 oob_buf += oob_size2;
2129 config_nand_cw_write(nandc);
2132 ret = submit_descs(nandc);
2134 dev_err(nandc->dev, "failure to write raw page\n");
2139 ret = nand_prog_page_end_op(chip);
2145 * implements ecc->write_oob()
2147 * the NAND controller cannot write only data or only OOB within a codeword
2148 * since ECC is calculated for the combined codeword. So update the OOB from
2149 * chip->oob_poi, and pad the data area with OxFF before writing.
2151 static int qcom_nandc_write_oob(struct nand_chip *chip, int page)
2153 struct mtd_info *mtd = nand_to_mtd(chip);
2154 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2155 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2156 struct nand_ecc_ctrl *ecc = &chip->ecc;
2157 u8 *oob = chip->oob_poi;
2158 int data_size, oob_size;
2161 host->use_ecc = true;
2162 clear_bam_transaction(nandc);
2164 /* calculate the data and oob size for the last codeword/step */
2165 data_size = ecc->size - ((ecc->steps - 1) << 2);
2166 oob_size = mtd->oobavail;
2168 memset(nandc->data_buffer, 0xff, host->cw_data);
2169 /* override new oob content to last codeword */
2170 mtd_ooblayout_get_databytes(mtd, nandc->data_buffer + data_size, oob,
2173 set_address(host, host->cw_size * (ecc->steps - 1), page);
2174 update_rw_regs(host, 1, false);
2176 config_nand_page_write(nandc);
2177 write_data_dma(nandc, FLASH_BUF_ACC,
2178 nandc->data_buffer, data_size + oob_size, 0);
2179 config_nand_cw_write(nandc);
2181 ret = submit_descs(nandc);
2186 dev_err(nandc->dev, "failure to write oob\n");
2190 return nand_prog_page_end_op(chip);
2193 static int qcom_nandc_block_bad(struct nand_chip *chip, loff_t ofs)
2195 struct mtd_info *mtd = nand_to_mtd(chip);
2196 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2197 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2198 struct nand_ecc_ctrl *ecc = &chip->ecc;
2199 int page, ret, bbpos, bad = 0;
2201 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
2204 * configure registers for a raw sub page read, the address is set to
2205 * the beginning of the last codeword, we don't care about reading ecc
2206 * portion of oob. we just want the first few bytes from this codeword
2207 * that contains the BBM
2209 host->use_ecc = false;
2211 clear_bam_transaction(nandc);
2212 ret = copy_last_cw(host, page);
2216 if (check_flash_errors(host, 1)) {
2217 dev_warn(nandc->dev, "error when trying to read BBM\n");
2221 bbpos = mtd->writesize - host->cw_size * (ecc->steps - 1);
2223 bad = nandc->data_buffer[bbpos] != 0xff;
2225 if (chip->options & NAND_BUSWIDTH_16)
2226 bad = bad || (nandc->data_buffer[bbpos + 1] != 0xff);
2231 static int qcom_nandc_block_markbad(struct nand_chip *chip, loff_t ofs)
2233 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2234 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2235 struct nand_ecc_ctrl *ecc = &chip->ecc;
2238 clear_read_regs(nandc);
2239 clear_bam_transaction(nandc);
2242 * to mark the BBM as bad, we flash the entire last codeword with 0s.
2243 * we don't care about the rest of the content in the codeword since
2244 * we aren't going to use this block again
2246 memset(nandc->data_buffer, 0x00, host->cw_size);
2248 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
2251 host->use_ecc = false;
2252 set_address(host, host->cw_size * (ecc->steps - 1), page);
2253 update_rw_regs(host, 1, false);
2255 config_nand_page_write(nandc);
2256 write_data_dma(nandc, FLASH_BUF_ACC,
2257 nandc->data_buffer, host->cw_size, 0);
2258 config_nand_cw_write(nandc);
2260 ret = submit_descs(nandc);
2265 dev_err(nandc->dev, "failure to update BBM\n");
2269 return nand_prog_page_end_op(chip);
2273 * the three functions below implement chip->legacy.read_byte(),
2274 * chip->legacy.read_buf() and chip->legacy.write_buf() respectively. these
2275 * aren't used for reading/writing page data, they are used for smaller data
2276 * like reading id, status etc
2278 static uint8_t qcom_nandc_read_byte(struct nand_chip *chip)
2280 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2281 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2282 u8 *buf = nandc->data_buffer;
2285 if (host->last_command == NAND_CMD_STATUS) {
2288 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
2293 if (nandc->buf_start < nandc->buf_count)
2294 ret = buf[nandc->buf_start++];
2299 static void qcom_nandc_read_buf(struct nand_chip *chip, uint8_t *buf, int len)
2301 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2302 int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
2304 memcpy(buf, nandc->data_buffer + nandc->buf_start, real_len);
2305 nandc->buf_start += real_len;
2308 static void qcom_nandc_write_buf(struct nand_chip *chip, const uint8_t *buf,
2311 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2312 int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
2314 memcpy(nandc->data_buffer + nandc->buf_start, buf, real_len);
2316 nandc->buf_start += real_len;
2319 /* we support only one external chip for now */
2320 static void qcom_nandc_select_chip(struct nand_chip *chip, int chipnr)
2322 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2327 dev_warn(nandc->dev, "invalid chip select\n");
2331 * NAND controller page layout info
2333 * Layout with ECC enabled:
2335 * |----------------------| |---------------------------------|
2336 * | xx.......yy| | *********xx.......yy|
2337 * | DATA xx..ECC..yy| | DATA **SPARE**xx..ECC..yy|
2338 * | (516) xx.......yy| | (516-n*4) **(n*4)**xx.......yy|
2339 * | xx.......yy| | *********xx.......yy|
2340 * |----------------------| |---------------------------------|
2341 * codeword 1,2..n-1 codeword n
2342 * <---(528/532 Bytes)--> <-------(528/532 Bytes)--------->
2344 * n = Number of codewords in the page
2346 * * = Spare/free bytes
2347 * x = Unused byte(s)
2348 * y = Reserved byte(s)
2350 * 2K page: n = 4, spare = 16 bytes
2351 * 4K page: n = 8, spare = 32 bytes
2352 * 8K page: n = 16, spare = 64 bytes
2354 * the qcom nand controller operates at a sub page/codeword level. each
2355 * codeword is 528 and 532 bytes for 4 bit and 8 bit ECC modes respectively.
2356 * the number of ECC bytes vary based on the ECC strength and the bus width.
2358 * the first n - 1 codewords contains 516 bytes of user data, the remaining
2359 * 12/16 bytes consist of ECC and reserved data. The nth codeword contains
2360 * both user data and spare(oobavail) bytes that sum up to 516 bytes.
2362 * When we access a page with ECC enabled, the reserved bytes(s) are not
2363 * accessible at all. When reading, we fill up these unreadable positions
2364 * with 0xffs. When writing, the controller skips writing the inaccessible
2367 * Layout with ECC disabled:
2369 * |------------------------------| |---------------------------------------|
2370 * | yy xx.......| | bb *********xx.......|
2371 * | DATA1 yy DATA2 xx..ECC..| | DATA1 bb DATA2 **SPARE**xx..ECC..|
2372 * | (size1) yy (size2) xx.......| | (size1) bb (size2) **(n*4)**xx.......|
2373 * | yy xx.......| | bb *********xx.......|
2374 * |------------------------------| |---------------------------------------|
2375 * codeword 1,2..n-1 codeword n
2376 * <-------(528/532 Bytes)------> <-----------(528/532 Bytes)----------->
2378 * n = Number of codewords in the page
2380 * * = Spare/free bytes
2381 * x = Unused byte(s)
2382 * y = Dummy Bad Bock byte(s)
2383 * b = Real Bad Block byte(s)
2384 * size1/size2 = function of codeword size and 'n'
2386 * when the ECC block is disabled, one reserved byte (or two for 16 bit bus
2387 * width) is now accessible. For the first n - 1 codewords, these are dummy Bad
2388 * Block Markers. In the last codeword, this position contains the real BBM
2390 * In order to have a consistent layout between RAW and ECC modes, we assume
2391 * the following OOB layout arrangement:
2393 * |-----------| |--------------------|
2394 * |yyxx.......| |bb*********xx.......|
2395 * |yyxx..ECC..| |bb*FREEOOB*xx..ECC..|
2396 * |yyxx.......| |bb*********xx.......|
2397 * |yyxx.......| |bb*********xx.......|
2398 * |-----------| |--------------------|
2399 * first n - 1 nth OOB region
2402 * n = Number of codewords in the page
2404 * * = FREE OOB bytes
2405 * y = Dummy bad block byte(s) (inaccessible when ECC enabled)
2406 * x = Unused byte(s)
2407 * b = Real bad block byte(s) (inaccessible when ECC enabled)
2409 * This layout is read as is when ECC is disabled. When ECC is enabled, the
2410 * inaccessible Bad Block byte(s) are ignored when we write to a page/oob,
2411 * and assumed as 0xffs when we read a page/oob. The ECC, unused and
2412 * dummy/real bad block bytes are grouped as ecc bytes (i.e, ecc->bytes is
2413 * the sum of the three).
2415 static int qcom_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
2416 struct mtd_oob_region *oobregion)
2418 struct nand_chip *chip = mtd_to_nand(mtd);
2419 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2420 struct nand_ecc_ctrl *ecc = &chip->ecc;
2426 oobregion->length = (ecc->bytes * (ecc->steps - 1)) +
2428 oobregion->offset = 0;
2430 oobregion->length = host->ecc_bytes_hw + host->spare_bytes;
2431 oobregion->offset = mtd->oobsize - oobregion->length;
2437 static int qcom_nand_ooblayout_free(struct mtd_info *mtd, int section,
2438 struct mtd_oob_region *oobregion)
2440 struct nand_chip *chip = mtd_to_nand(mtd);
2441 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2442 struct nand_ecc_ctrl *ecc = &chip->ecc;
2447 oobregion->length = ecc->steps * 4;
2448 oobregion->offset = ((ecc->steps - 1) * ecc->bytes) + host->bbm_size;
2453 static const struct mtd_ooblayout_ops qcom_nand_ooblayout_ops = {
2454 .ecc = qcom_nand_ooblayout_ecc,
2455 .free = qcom_nand_ooblayout_free,
2459 qcom_nandc_calc_ecc_bytes(int step_size, int strength)
2461 return strength == 4 ? 12 : 16;
2463 NAND_ECC_CAPS_SINGLE(qcom_nandc_ecc_caps, qcom_nandc_calc_ecc_bytes,
2464 NANDC_STEP_SIZE, 4, 8);
2466 static int qcom_nand_attach_chip(struct nand_chip *chip)
2468 struct mtd_info *mtd = nand_to_mtd(chip);
2469 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2470 struct nand_ecc_ctrl *ecc = &chip->ecc;
2471 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2472 int cwperpage, bad_block_byte, ret;
2476 /* controller only supports 512 bytes data steps */
2477 ecc->size = NANDC_STEP_SIZE;
2478 wide_bus = chip->options & NAND_BUSWIDTH_16 ? true : false;
2479 cwperpage = mtd->writesize / NANDC_STEP_SIZE;
2482 * Each CW has 4 available OOB bytes which will be protected with ECC
2483 * so remaining bytes can be used for ECC.
2485 ret = nand_ecc_choose_conf(chip, &qcom_nandc_ecc_caps,
2486 mtd->oobsize - (cwperpage * 4));
2488 dev_err(nandc->dev, "No valid ECC settings possible\n");
2492 if (ecc->strength >= 8) {
2493 /* 8 bit ECC defaults to BCH ECC on all platforms */
2494 host->bch_enabled = true;
2498 host->ecc_bytes_hw = 14;
2499 host->spare_bytes = 0;
2502 host->ecc_bytes_hw = 13;
2503 host->spare_bytes = 2;
2508 * if the controller supports BCH for 4 bit ECC, the controller
2509 * uses lesser bytes for ECC. If RS is used, the ECC bytes is
2512 if (nandc->props->ecc_modes & ECC_BCH_4BIT) {
2514 host->bch_enabled = true;
2518 host->ecc_bytes_hw = 8;
2519 host->spare_bytes = 2;
2522 host->ecc_bytes_hw = 7;
2523 host->spare_bytes = 4;
2528 host->ecc_bytes_hw = 10;
2531 host->spare_bytes = 0;
2534 host->spare_bytes = 1;
2541 * we consider ecc->bytes as the sum of all the non-data content in a
2542 * step. It gives us a clean representation of the oob area (even if
2543 * all the bytes aren't used for ECC).It is always 16 bytes for 8 bit
2544 * ECC and 12 bytes for 4 bit ECC
2546 ecc->bytes = host->ecc_bytes_hw + host->spare_bytes + host->bbm_size;
2548 ecc->read_page = qcom_nandc_read_page;
2549 ecc->read_page_raw = qcom_nandc_read_page_raw;
2550 ecc->read_oob = qcom_nandc_read_oob;
2551 ecc->write_page = qcom_nandc_write_page;
2552 ecc->write_page_raw = qcom_nandc_write_page_raw;
2553 ecc->write_oob = qcom_nandc_write_oob;
2555 ecc->engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
2557 mtd_set_ooblayout(mtd, &qcom_nand_ooblayout_ops);
2559 nandc->max_cwperpage = max_t(unsigned int, nandc->max_cwperpage,
2563 * DATA_UD_BYTES varies based on whether the read/write command protects
2564 * spare data with ECC too. We protect spare data by default, so we set
2565 * it to main + spare data, which are 512 and 4 bytes respectively.
2567 host->cw_data = 516;
2570 * total bytes in a step, either 528 bytes for 4 bit ECC, or 532 bytes
2573 host->cw_size = host->cw_data + ecc->bytes;
2574 bad_block_byte = mtd->writesize - host->cw_size * (cwperpage - 1) + 1;
2576 host->cfg0 = (cwperpage - 1) << CW_PER_PAGE
2577 | host->cw_data << UD_SIZE_BYTES
2578 | 0 << DISABLE_STATUS_AFTER_WRITE
2579 | 5 << NUM_ADDR_CYCLES
2580 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_RS
2581 | 0 << STATUS_BFR_READ
2582 | 1 << SET_RD_MODE_AFTER_STATUS
2583 | host->spare_bytes << SPARE_SIZE_BYTES;
2585 host->cfg1 = 7 << NAND_RECOVERY_CYCLES
2586 | 0 << CS_ACTIVE_BSY
2587 | bad_block_byte << BAD_BLOCK_BYTE_NUM
2588 | 0 << BAD_BLOCK_IN_SPARE_AREA
2589 | 2 << WR_RD_BSY_GAP
2590 | wide_bus << WIDE_FLASH
2591 | host->bch_enabled << ENABLE_BCH_ECC;
2593 host->cfg0_raw = (cwperpage - 1) << CW_PER_PAGE
2594 | host->cw_size << UD_SIZE_BYTES
2595 | 5 << NUM_ADDR_CYCLES
2596 | 0 << SPARE_SIZE_BYTES;
2598 host->cfg1_raw = 7 << NAND_RECOVERY_CYCLES
2599 | 0 << CS_ACTIVE_BSY
2600 | 17 << BAD_BLOCK_BYTE_NUM
2601 | 1 << BAD_BLOCK_IN_SPARE_AREA
2602 | 2 << WR_RD_BSY_GAP
2603 | wide_bus << WIDE_FLASH
2604 | 1 << DEV0_CFG1_ECC_DISABLE;
2606 host->ecc_bch_cfg = !host->bch_enabled << ECC_CFG_ECC_DISABLE
2608 | host->cw_data << ECC_NUM_DATA_BYTES
2609 | 1 << ECC_FORCE_CLK_OPEN
2610 | ecc_mode << ECC_MODE
2611 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_BCH;
2613 host->ecc_buf_cfg = 0x203 << NUM_STEPS;
2615 host->clrflashstatus = FS_READY_BSY_N;
2616 host->clrreadstatus = 0xc0;
2617 nandc->regs->erased_cw_detect_cfg_clr =
2618 cpu_to_le32(CLR_ERASED_PAGE_DET);
2619 nandc->regs->erased_cw_detect_cfg_set =
2620 cpu_to_le32(SET_ERASED_PAGE_DET);
2623 "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",
2624 host->cfg0, host->cfg1, host->ecc_buf_cfg, host->ecc_bch_cfg,
2625 host->cw_size, host->cw_data, ecc->strength, ecc->bytes,
2631 static const struct nand_controller_ops qcom_nandc_ops = {
2632 .attach_chip = qcom_nand_attach_chip,
2635 static void qcom_nandc_unalloc(struct qcom_nand_controller *nandc)
2637 if (nandc->props->is_bam) {
2638 if (!dma_mapping_error(nandc->dev, nandc->reg_read_dma))
2639 dma_unmap_single(nandc->dev, nandc->reg_read_dma,
2641 sizeof(*nandc->reg_read_buf),
2645 dma_release_channel(nandc->tx_chan);
2648 dma_release_channel(nandc->rx_chan);
2650 if (nandc->cmd_chan)
2651 dma_release_channel(nandc->cmd_chan);
2654 dma_release_channel(nandc->chan);
2658 static int qcom_nandc_alloc(struct qcom_nand_controller *nandc)
2662 ret = dma_set_coherent_mask(nandc->dev, DMA_BIT_MASK(32));
2664 dev_err(nandc->dev, "failed to set DMA mask\n");
2669 * we use the internal buffer for reading ONFI params, reading small
2670 * data like ID and status, and preforming read-copy-write operations
2671 * when writing to a codeword partially. 532 is the maximum possible
2672 * size of a codeword for our nand controller
2674 nandc->buf_size = 532;
2676 nandc->data_buffer = devm_kzalloc(nandc->dev, nandc->buf_size,
2678 if (!nandc->data_buffer)
2681 nandc->regs = devm_kzalloc(nandc->dev, sizeof(*nandc->regs),
2686 nandc->reg_read_buf = devm_kcalloc(nandc->dev,
2687 MAX_REG_RD, sizeof(*nandc->reg_read_buf),
2689 if (!nandc->reg_read_buf)
2692 if (nandc->props->is_bam) {
2693 nandc->reg_read_dma =
2694 dma_map_single(nandc->dev, nandc->reg_read_buf,
2696 sizeof(*nandc->reg_read_buf),
2698 if (dma_mapping_error(nandc->dev, nandc->reg_read_dma)) {
2699 dev_err(nandc->dev, "failed to DMA MAP reg buffer\n");
2703 nandc->tx_chan = dma_request_chan(nandc->dev, "tx");
2704 if (IS_ERR(nandc->tx_chan)) {
2705 ret = PTR_ERR(nandc->tx_chan);
2706 nandc->tx_chan = NULL;
2707 dev_err_probe(nandc->dev, ret,
2708 "tx DMA channel request failed\n");
2712 nandc->rx_chan = dma_request_chan(nandc->dev, "rx");
2713 if (IS_ERR(nandc->rx_chan)) {
2714 ret = PTR_ERR(nandc->rx_chan);
2715 nandc->rx_chan = NULL;
2716 dev_err_probe(nandc->dev, ret,
2717 "rx DMA channel request failed\n");
2721 nandc->cmd_chan = dma_request_chan(nandc->dev, "cmd");
2722 if (IS_ERR(nandc->cmd_chan)) {
2723 ret = PTR_ERR(nandc->cmd_chan);
2724 nandc->cmd_chan = NULL;
2725 dev_err_probe(nandc->dev, ret,
2726 "cmd DMA channel request failed\n");
2731 * Initially allocate BAM transaction to read ONFI param page.
2732 * After detecting all the devices, this BAM transaction will
2733 * be freed and the next BAM tranasction will be allocated with
2734 * maximum codeword size
2736 nandc->max_cwperpage = 1;
2737 nandc->bam_txn = alloc_bam_transaction(nandc);
2738 if (!nandc->bam_txn) {
2740 "failed to allocate bam transaction\n");
2745 nandc->chan = dma_request_chan(nandc->dev, "rxtx");
2746 if (IS_ERR(nandc->chan)) {
2747 ret = PTR_ERR(nandc->chan);
2749 dev_err_probe(nandc->dev, ret,
2750 "rxtx DMA channel request failed\n");
2755 INIT_LIST_HEAD(&nandc->desc_list);
2756 INIT_LIST_HEAD(&nandc->host_list);
2758 nand_controller_init(&nandc->controller);
2759 nandc->controller.ops = &qcom_nandc_ops;
2763 qcom_nandc_unalloc(nandc);
2767 /* one time setup of a few nand controller registers */
2768 static int qcom_nandc_setup(struct qcom_nand_controller *nandc)
2773 if (!nandc->props->is_qpic)
2774 nandc_write(nandc, SFLASHC_BURST_CFG, 0);
2775 nandc_write(nandc, dev_cmd_reg_addr(nandc, NAND_DEV_CMD_VLD),
2776 NAND_DEV_CMD_VLD_VAL);
2778 /* enable ADM or BAM DMA */
2779 if (nandc->props->is_bam) {
2780 nand_ctrl = nandc_read(nandc, NAND_CTRL);
2783 *NAND_CTRL is an operational registers, and CPU
2784 * access to operational registers are read only
2785 * in BAM mode. So update the NAND_CTRL register
2786 * only if it is not in BAM mode. In most cases BAM
2787 * mode will be enabled in bootloader
2789 if (!(nand_ctrl & BAM_MODE_EN))
2790 nandc_write(nandc, NAND_CTRL, nand_ctrl | BAM_MODE_EN);
2792 nandc_write(nandc, NAND_FLASH_CHIP_SELECT, DM_EN);
2795 /* save the original values of these registers */
2796 nandc->cmd1 = nandc_read(nandc, dev_cmd_reg_addr(nandc, NAND_DEV_CMD1));
2797 nandc->vld = NAND_DEV_CMD_VLD_VAL;
2802 static int qcom_nand_host_init_and_register(struct qcom_nand_controller *nandc,
2803 struct qcom_nand_host *host,
2804 struct device_node *dn)
2806 struct nand_chip *chip = &host->chip;
2807 struct mtd_info *mtd = nand_to_mtd(chip);
2808 struct device *dev = nandc->dev;
2811 ret = of_property_read_u32(dn, "reg", &host->cs);
2813 dev_err(dev, "can't get chip-select\n");
2817 nand_set_flash_node(chip, dn);
2818 mtd->name = devm_kasprintf(dev, GFP_KERNEL, "qcom_nand.%d", host->cs);
2822 mtd->owner = THIS_MODULE;
2823 mtd->dev.parent = dev;
2825 chip->legacy.cmdfunc = qcom_nandc_command;
2826 chip->legacy.select_chip = qcom_nandc_select_chip;
2827 chip->legacy.read_byte = qcom_nandc_read_byte;
2828 chip->legacy.read_buf = qcom_nandc_read_buf;
2829 chip->legacy.write_buf = qcom_nandc_write_buf;
2830 chip->legacy.set_features = nand_get_set_features_notsupp;
2831 chip->legacy.get_features = nand_get_set_features_notsupp;
2834 * the bad block marker is readable only when we read the last codeword
2835 * of a page with ECC disabled. currently, the nand_base and nand_bbt
2836 * helpers don't allow us to read BB from a nand chip with ECC
2837 * disabled (MTD_OPS_PLACE_OOB is set by default). use the block_bad
2838 * and block_markbad helpers until we permanently switch to using
2839 * MTD_OPS_RAW for all drivers (with the help of badblockbits)
2841 chip->legacy.block_bad = qcom_nandc_block_bad;
2842 chip->legacy.block_markbad = qcom_nandc_block_markbad;
2844 chip->controller = &nandc->controller;
2845 chip->options |= NAND_NO_SUBPAGE_WRITE | NAND_USES_DMA |
2848 /* set up initial status value */
2849 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
2851 ret = nand_scan(chip, 1);
2855 if (nandc->props->is_bam) {
2856 free_bam_transaction(nandc);
2857 nandc->bam_txn = alloc_bam_transaction(nandc);
2858 if (!nandc->bam_txn) {
2860 "failed to allocate bam transaction\n");
2865 ret = mtd_device_register(mtd, NULL, 0);
2872 static int qcom_probe_nand_devices(struct qcom_nand_controller *nandc)
2874 struct device *dev = nandc->dev;
2875 struct device_node *dn = dev->of_node, *child;
2876 struct qcom_nand_host *host;
2879 for_each_available_child_of_node(dn, child) {
2880 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
2886 ret = qcom_nand_host_init_and_register(nandc, host, child);
2888 devm_kfree(dev, host);
2892 list_add_tail(&host->node, &nandc->host_list);
2895 if (list_empty(&nandc->host_list))
2901 /* parse custom DT properties here */
2902 static int qcom_nandc_parse_dt(struct platform_device *pdev)
2904 struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
2905 struct device_node *np = nandc->dev->of_node;
2908 if (!nandc->props->is_bam) {
2909 ret = of_property_read_u32(np, "qcom,cmd-crci",
2912 dev_err(nandc->dev, "command CRCI unspecified\n");
2916 ret = of_property_read_u32(np, "qcom,data-crci",
2919 dev_err(nandc->dev, "data CRCI unspecified\n");
2927 static int qcom_nandc_probe(struct platform_device *pdev)
2929 struct qcom_nand_controller *nandc;
2930 const void *dev_data;
2931 struct device *dev = &pdev->dev;
2932 struct resource *res;
2935 nandc = devm_kzalloc(&pdev->dev, sizeof(*nandc), GFP_KERNEL);
2939 platform_set_drvdata(pdev, nandc);
2942 dev_data = of_device_get_match_data(dev);
2944 dev_err(&pdev->dev, "failed to get device data\n");
2948 nandc->props = dev_data;
2950 nandc->core_clk = devm_clk_get(dev, "core");
2951 if (IS_ERR(nandc->core_clk))
2952 return PTR_ERR(nandc->core_clk);
2954 nandc->aon_clk = devm_clk_get(dev, "aon");
2955 if (IS_ERR(nandc->aon_clk))
2956 return PTR_ERR(nandc->aon_clk);
2958 ret = qcom_nandc_parse_dt(pdev);
2962 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2963 nandc->base = devm_ioremap_resource(dev, res);
2964 if (IS_ERR(nandc->base))
2965 return PTR_ERR(nandc->base);
2967 nandc->base_phys = res->start;
2968 nandc->base_dma = dma_map_resource(dev, res->start,
2970 DMA_BIDIRECTIONAL, 0);
2971 if (!nandc->base_dma)
2974 ret = qcom_nandc_alloc(nandc);
2976 goto err_nandc_alloc;
2978 ret = clk_prepare_enable(nandc->core_clk);
2982 ret = clk_prepare_enable(nandc->aon_clk);
2986 ret = qcom_nandc_setup(nandc);
2990 ret = qcom_probe_nand_devices(nandc);
2997 clk_disable_unprepare(nandc->aon_clk);
2999 clk_disable_unprepare(nandc->core_clk);
3001 qcom_nandc_unalloc(nandc);
3003 dma_unmap_resource(dev, res->start, resource_size(res),
3004 DMA_BIDIRECTIONAL, 0);
3009 static int qcom_nandc_remove(struct platform_device *pdev)
3011 struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
3012 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3013 struct qcom_nand_host *host;
3014 struct nand_chip *chip;
3017 list_for_each_entry(host, &nandc->host_list, node) {
3019 ret = mtd_device_unregister(nand_to_mtd(chip));
3024 qcom_nandc_unalloc(nandc);
3026 clk_disable_unprepare(nandc->aon_clk);
3027 clk_disable_unprepare(nandc->core_clk);
3029 dma_unmap_resource(&pdev->dev, nandc->base_dma, resource_size(res),
3030 DMA_BIDIRECTIONAL, 0);
3035 static const struct qcom_nandc_props ipq806x_nandc_props = {
3036 .ecc_modes = (ECC_RS_4BIT | ECC_BCH_8BIT),
3038 .dev_cmd_reg_start = 0x0,
3041 static const struct qcom_nandc_props ipq4019_nandc_props = {
3042 .ecc_modes = (ECC_BCH_4BIT | ECC_BCH_8BIT),
3045 .dev_cmd_reg_start = 0x0,
3048 static const struct qcom_nandc_props ipq8074_nandc_props = {
3049 .ecc_modes = (ECC_BCH_4BIT | ECC_BCH_8BIT),
3052 .dev_cmd_reg_start = 0x7000,
3056 * data will hold a struct pointer containing more differences once we support
3057 * more controller variants
3059 static const struct of_device_id qcom_nandc_of_match[] = {
3061 .compatible = "qcom,ipq806x-nand",
3062 .data = &ipq806x_nandc_props,
3065 .compatible = "qcom,ipq4019-nand",
3066 .data = &ipq4019_nandc_props,
3069 .compatible = "qcom,ipq8074-nand",
3070 .data = &ipq8074_nandc_props,
3074 MODULE_DEVICE_TABLE(of, qcom_nandc_of_match);
3076 static struct platform_driver qcom_nandc_driver = {
3078 .name = "qcom-nandc",
3079 .of_match_table = qcom_nandc_of_match,
3081 .probe = qcom_nandc_probe,
3082 .remove = qcom_nandc_remove,
3084 module_platform_driver(qcom_nandc_driver);
3086 MODULE_AUTHOR("Archit Taneja <architt@codeaurora.org>");
3087 MODULE_DESCRIPTION("Qualcomm NAND Controller driver");
3088 MODULE_LICENSE("GPL v2");