2 * Copyright (c) 2016, The Linux Foundation. All rights reserved.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/clk.h>
15 #include <linux/slab.h>
16 #include <linux/bitops.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/dmaengine.h>
19 #include <linux/module.h>
20 #include <linux/mtd/rawnand.h>
21 #include <linux/mtd/partitions.h>
23 #include <linux/of_device.h>
24 #include <linux/delay.h>
25 #include <linux/dma/qcom_bam_dma.h>
26 #include <linux/dma-direct.h> /* XXX: drivers shall never use this directly! */
28 /* NANDc reg offsets */
29 #define NAND_FLASH_CMD 0x00
30 #define NAND_ADDR0 0x04
31 #define NAND_ADDR1 0x08
32 #define NAND_FLASH_CHIP_SELECT 0x0c
33 #define NAND_EXEC_CMD 0x10
34 #define NAND_FLASH_STATUS 0x14
35 #define NAND_BUFFER_STATUS 0x18
36 #define NAND_DEV0_CFG0 0x20
37 #define NAND_DEV0_CFG1 0x24
38 #define NAND_DEV0_ECC_CFG 0x28
39 #define NAND_DEV1_ECC_CFG 0x2c
40 #define NAND_DEV1_CFG0 0x30
41 #define NAND_DEV1_CFG1 0x34
42 #define NAND_READ_ID 0x40
43 #define NAND_READ_STATUS 0x44
44 #define NAND_DEV_CMD0 0xa0
45 #define NAND_DEV_CMD1 0xa4
46 #define NAND_DEV_CMD2 0xa8
47 #define NAND_DEV_CMD_VLD 0xac
48 #define SFLASHC_BURST_CFG 0xe0
49 #define NAND_ERASED_CW_DETECT_CFG 0xe8
50 #define NAND_ERASED_CW_DETECT_STATUS 0xec
51 #define NAND_EBI2_ECC_BUF_CFG 0xf0
52 #define FLASH_BUF_ACC 0x100
54 #define NAND_CTRL 0xf00
55 #define NAND_VERSION 0xf08
56 #define NAND_READ_LOCATION_0 0xf20
57 #define NAND_READ_LOCATION_1 0xf24
58 #define NAND_READ_LOCATION_2 0xf28
59 #define NAND_READ_LOCATION_3 0xf2c
61 /* dummy register offsets, used by write_reg_dma */
62 #define NAND_DEV_CMD1_RESTORE 0xdead
63 #define NAND_DEV_CMD_VLD_RESTORE 0xbeef
65 /* NAND_FLASH_CMD bits */
66 #define PAGE_ACC BIT(4)
67 #define LAST_PAGE BIT(5)
69 /* NAND_FLASH_CHIP_SELECT bits */
70 #define NAND_DEV_SEL 0
73 /* NAND_FLASH_STATUS bits */
74 #define FS_OP_ERR BIT(4)
75 #define FS_READY_BSY_N BIT(5)
76 #define FS_MPU_ERR BIT(8)
77 #define FS_DEVICE_STS_ERR BIT(16)
78 #define FS_DEVICE_WP BIT(23)
80 /* NAND_BUFFER_STATUS bits */
81 #define BS_UNCORRECTABLE_BIT BIT(8)
82 #define BS_CORRECTABLE_ERR_MSK 0x1f
84 /* NAND_DEVn_CFG0 bits */
85 #define DISABLE_STATUS_AFTER_WRITE 4
87 #define UD_SIZE_BYTES 9
88 #define ECC_PARITY_SIZE_BYTES_RS 19
89 #define SPARE_SIZE_BYTES 23
90 #define NUM_ADDR_CYCLES 27
91 #define STATUS_BFR_READ 30
92 #define SET_RD_MODE_AFTER_STATUS 31
94 /* NAND_DEVn_CFG0 bits */
95 #define DEV0_CFG1_ECC_DISABLE 0
97 #define NAND_RECOVERY_CYCLES 2
98 #define CS_ACTIVE_BSY 5
99 #define BAD_BLOCK_BYTE_NUM 6
100 #define BAD_BLOCK_IN_SPARE_AREA 16
101 #define WR_RD_BSY_GAP 17
102 #define ENABLE_BCH_ECC 27
104 /* NAND_DEV0_ECC_CFG bits */
105 #define ECC_CFG_ECC_DISABLE 0
106 #define ECC_SW_RESET 1
108 #define ECC_PARITY_SIZE_BYTES_BCH 8
109 #define ECC_NUM_DATA_BYTES 16
110 #define ECC_FORCE_CLK_OPEN 30
112 /* NAND_DEV_CMD1 bits */
115 /* NAND_DEV_CMD_VLD bits */
116 #define READ_START_VLD BIT(0)
117 #define READ_STOP_VLD BIT(1)
118 #define WRITE_START_VLD BIT(2)
119 #define ERASE_START_VLD BIT(3)
120 #define SEQ_READ_START_VLD BIT(4)
122 /* NAND_EBI2_ECC_BUF_CFG bits */
125 /* NAND_ERASED_CW_DETECT_CFG bits */
126 #define ERASED_CW_ECC_MASK 1
127 #define AUTO_DETECT_RES 0
128 #define MASK_ECC (1 << ERASED_CW_ECC_MASK)
129 #define RESET_ERASED_DET (1 << AUTO_DETECT_RES)
130 #define ACTIVE_ERASED_DET (0 << AUTO_DETECT_RES)
131 #define CLR_ERASED_PAGE_DET (RESET_ERASED_DET | MASK_ECC)
132 #define SET_ERASED_PAGE_DET (ACTIVE_ERASED_DET | MASK_ECC)
134 /* NAND_ERASED_CW_DETECT_STATUS bits */
135 #define PAGE_ALL_ERASED BIT(7)
136 #define CODEWORD_ALL_ERASED BIT(6)
137 #define PAGE_ERASED BIT(5)
138 #define CODEWORD_ERASED BIT(4)
139 #define ERASED_PAGE (PAGE_ALL_ERASED | PAGE_ERASED)
140 #define ERASED_CW (CODEWORD_ALL_ERASED | CODEWORD_ERASED)
142 /* NAND_READ_LOCATION_n bits */
143 #define READ_LOCATION_OFFSET 0
144 #define READ_LOCATION_SIZE 16
145 #define READ_LOCATION_LAST 31
148 #define NAND_VERSION_MAJOR_MASK 0xf0000000
149 #define NAND_VERSION_MAJOR_SHIFT 28
150 #define NAND_VERSION_MINOR_MASK 0x0fff0000
151 #define NAND_VERSION_MINOR_SHIFT 16
154 #define PAGE_READ 0x2
155 #define PAGE_READ_WITH_ECC 0x3
156 #define PAGE_READ_WITH_ECC_SPARE 0x4
157 #define PROGRAM_PAGE 0x6
158 #define PAGE_PROGRAM_WITH_ECC 0x7
159 #define PROGRAM_PAGE_SPARE 0x9
160 #define BLOCK_ERASE 0xa
162 #define RESET_DEVICE 0xd
164 /* Default Value for NAND_DEV_CMD_VLD */
165 #define NAND_DEV_CMD_VLD_VAL (READ_START_VLD | WRITE_START_VLD | \
166 ERASE_START_VLD | SEQ_READ_START_VLD)
169 #define BAM_MODE_EN BIT(0)
172 * the NAND controller performs reads/writes with ECC in 516 byte chunks.
173 * the driver calls the chunks 'step' or 'codeword' interchangeably
175 #define NANDC_STEP_SIZE 512
178 * the largest page size we support is 8K, this will have 16 steps/codewords
181 #define MAX_NUM_STEPS (SZ_8K / NANDC_STEP_SIZE)
183 /* we read at most 3 registers per codeword scan */
184 #define MAX_REG_RD (3 * MAX_NUM_STEPS)
186 /* ECC modes supported by the controller */
187 #define ECC_NONE BIT(0)
188 #define ECC_RS_4BIT BIT(1)
189 #define ECC_BCH_4BIT BIT(2)
190 #define ECC_BCH_8BIT BIT(3)
192 #define nandc_set_read_loc(nandc, reg, offset, size, is_last) \
193 nandc_set_reg(nandc, NAND_READ_LOCATION_##reg, \
194 ((offset) << READ_LOCATION_OFFSET) | \
195 ((size) << READ_LOCATION_SIZE) | \
196 ((is_last) << READ_LOCATION_LAST))
199 * Returns the actual register address for all NAND_DEV_ registers
200 * (i.e. NAND_DEV_CMD0, NAND_DEV_CMD1, NAND_DEV_CMD2 and NAND_DEV_CMD_VLD)
202 #define dev_cmd_reg_addr(nandc, reg) ((nandc)->props->dev_cmd_reg_start + (reg))
204 /* Returns the NAND register physical address */
205 #define nandc_reg_phys(chip, offset) ((chip)->base_phys + (offset))
207 /* Returns the dma address for reg read buffer */
208 #define reg_buf_dma_addr(chip, vaddr) \
209 ((chip)->reg_read_dma + \
210 ((uint8_t *)(vaddr) - (uint8_t *)(chip)->reg_read_buf))
212 #define QPIC_PER_CW_CMD_ELEMENTS 32
213 #define QPIC_PER_CW_CMD_SGL 32
214 #define QPIC_PER_CW_DATA_SGL 8
217 * Flags used in DMA descriptor preparation helper functions
218 * (i.e. read_reg_dma/write_reg_dma/read_data_dma/write_data_dma)
220 /* Don't set the EOT in current tx BAM sgl */
221 #define NAND_BAM_NO_EOT BIT(0)
222 /* Set the NWD flag in current BAM sgl */
223 #define NAND_BAM_NWD BIT(1)
224 /* Finish writing in the current BAM sgl and start writing in another BAM sgl */
225 #define NAND_BAM_NEXT_SGL BIT(2)
227 * Erased codeword status is being used two times in single transfer so this
228 * flag will determine the current value of erased codeword status register
230 #define NAND_ERASED_CW_SET BIT(4)
233 * This data type corresponds to the BAM transaction which will be used for all
235 * @bam_ce - the array of BAM command elements
236 * @cmd_sgl - sgl for NAND BAM command pipe
237 * @data_sgl - sgl for NAND BAM consumer/producer pipe
238 * @bam_ce_pos - the index in bam_ce which is available for next sgl
239 * @bam_ce_start - the index in bam_ce which marks the start position ce
240 * for current sgl. It will be used for size calculation
242 * @cmd_sgl_pos - current index in command sgl.
243 * @cmd_sgl_start - start index in command sgl.
244 * @tx_sgl_pos - current index in data sgl for tx.
245 * @tx_sgl_start - start index in data sgl for tx.
246 * @rx_sgl_pos - current index in data sgl for rx.
247 * @rx_sgl_start - start index in data sgl for rx.
249 struct bam_transaction {
250 struct bam_cmd_element *bam_ce;
251 struct scatterlist *cmd_sgl;
252 struct scatterlist *data_sgl;
264 * This data type corresponds to the nand dma descriptor
265 * @list - list for desc_info
266 * @dir - DMA transfer direction
267 * @adm_sgl - sgl which will be used for single sgl dma descriptor. Only used by
269 * @bam_sgl - sgl which will be used for dma descriptor. Only used by BAM
270 * @sgl_cnt - number of SGL in bam_sgl. Only used by BAM
271 * @dma_desc - low level DMA engine descriptor
274 struct list_head node;
276 enum dma_data_direction dir;
278 struct scatterlist adm_sgl;
280 struct scatterlist *bam_sgl;
284 struct dma_async_tx_descriptor *dma_desc;
288 * holds the current register values that we want to write. acts as a contiguous
289 * chunk of memory which we use to write the controller registers through DMA.
302 __le32 clrflashstatus;
303 __le32 clrreadstatus;
312 __le32 read_location0;
313 __le32 read_location1;
314 __le32 read_location2;
315 __le32 read_location3;
317 __le32 erased_cw_detect_cfg_clr;
318 __le32 erased_cw_detect_cfg_set;
322 * NAND controller data struct
324 * @controller: base controller structure
325 * @host_list: list containing all the chips attached to the
327 * @dev: parent device
329 * @base_phys: physical base address of controller registers
330 * @base_dma: dma base address of controller registers
331 * @core_clk: controller clock
332 * @aon_clk: another controller clock
335 * @cmd_crci: ADM DMA CRCI for command flow control
336 * @data_crci: ADM DMA CRCI for data flow control
337 * @desc_list: DMA descriptor list (list of desc_infos)
339 * @data_buffer: our local DMA buffer for page read/writes,
340 * used when we can't use the buffer provided
341 * by upper layers directly
342 * @buf_size/count/start: markers for chip->read_buf/write_buf functions
343 * @reg_read_buf: local buffer for reading back registers via DMA
344 * @reg_read_dma: contains dma address for register read buffer
345 * @reg_read_pos: marker for data read in reg_read_buf
347 * @regs: a contiguous chunk of memory for DMA register
348 * writes. contains the register values to be
349 * written to controller
350 * @cmd1/vld: some fixed controller register values
351 * @props: properties of current NAND controller,
352 * initialized via DT match data
353 * @max_cwperpage: maximum QPIC codewords required. calculated
354 * from all connected NAND devices pagesize
356 struct qcom_nand_controller {
357 struct nand_hw_control controller;
358 struct list_head host_list;
363 phys_addr_t base_phys;
366 struct clk *core_clk;
370 /* will be used only by QPIC for BAM DMA */
372 struct dma_chan *tx_chan;
373 struct dma_chan *rx_chan;
374 struct dma_chan *cmd_chan;
377 /* will be used only by EBI2 for ADM DMA */
379 struct dma_chan *chan;
380 unsigned int cmd_crci;
381 unsigned int data_crci;
385 struct list_head desc_list;
386 struct bam_transaction *bam_txn;
392 unsigned int max_cwperpage;
394 __le32 *reg_read_buf;
395 dma_addr_t reg_read_dma;
398 struct nandc_regs *regs;
401 const struct qcom_nandc_props *props;
405 * NAND chip structure
407 * @chip: base NAND chip structure
408 * @node: list node to add itself to host_list in
409 * qcom_nand_controller
411 * @cs: chip select value for this chip
412 * @cw_size: the number of bytes in a single step/codeword
413 * of a page, consisting of all data, ecc, spare
415 * @cw_data: the number of bytes within a codeword protected
417 * @use_ecc: request the controller to use ECC for the
418 * upcoming read/write
419 * @bch_enabled: flag to tell whether BCH ECC mode is used
420 * @ecc_bytes_hw: ECC bytes used by controller hardware for this
422 * @status: value to be returned if NAND_CMD_STATUS command
424 * @last_command: keeps track of last command on this chip. used
425 * for reading correct status
427 * @cfg0, cfg1, cfg0_raw..: NANDc register configurations needed for
428 * ecc/non-ecc mode for the current nand flash
431 struct qcom_nand_host {
432 struct nand_chip chip;
433 struct list_head node;
447 u32 cfg0_raw, cfg1_raw;
455 * This data type corresponds to the NAND controller properties which varies
456 * among different NAND controllers.
457 * @ecc_modes - ecc mode for NAND
458 * @is_bam - whether NAND controller is using BAM
459 * @dev_cmd_reg_start - NAND_DEV_CMD_* registers starting offset
461 struct qcom_nandc_props {
464 u32 dev_cmd_reg_start;
467 /* Frees the BAM transaction memory */
468 static void free_bam_transaction(struct qcom_nand_controller *nandc)
470 struct bam_transaction *bam_txn = nandc->bam_txn;
472 devm_kfree(nandc->dev, bam_txn);
475 /* Allocates and Initializes the BAM transaction */
476 static struct bam_transaction *
477 alloc_bam_transaction(struct qcom_nand_controller *nandc)
479 struct bam_transaction *bam_txn;
481 unsigned int num_cw = nandc->max_cwperpage;
485 sizeof(*bam_txn) + num_cw *
486 ((sizeof(*bam_txn->bam_ce) * QPIC_PER_CW_CMD_ELEMENTS) +
487 (sizeof(*bam_txn->cmd_sgl) * QPIC_PER_CW_CMD_SGL) +
488 (sizeof(*bam_txn->data_sgl) * QPIC_PER_CW_DATA_SGL));
490 bam_txn_buf = devm_kzalloc(nandc->dev, bam_txn_size, GFP_KERNEL);
494 bam_txn = bam_txn_buf;
495 bam_txn_buf += sizeof(*bam_txn);
497 bam_txn->bam_ce = bam_txn_buf;
499 sizeof(*bam_txn->bam_ce) * QPIC_PER_CW_CMD_ELEMENTS * num_cw;
501 bam_txn->cmd_sgl = bam_txn_buf;
503 sizeof(*bam_txn->cmd_sgl) * QPIC_PER_CW_CMD_SGL * num_cw;
505 bam_txn->data_sgl = bam_txn_buf;
510 /* Clears the BAM transaction indexes */
511 static void clear_bam_transaction(struct qcom_nand_controller *nandc)
513 struct bam_transaction *bam_txn = nandc->bam_txn;
515 if (!nandc->props->is_bam)
518 bam_txn->bam_ce_pos = 0;
519 bam_txn->bam_ce_start = 0;
520 bam_txn->cmd_sgl_pos = 0;
521 bam_txn->cmd_sgl_start = 0;
522 bam_txn->tx_sgl_pos = 0;
523 bam_txn->tx_sgl_start = 0;
524 bam_txn->rx_sgl_pos = 0;
525 bam_txn->rx_sgl_start = 0;
527 sg_init_table(bam_txn->cmd_sgl, nandc->max_cwperpage *
528 QPIC_PER_CW_CMD_SGL);
529 sg_init_table(bam_txn->data_sgl, nandc->max_cwperpage *
530 QPIC_PER_CW_DATA_SGL);
533 static inline struct qcom_nand_host *to_qcom_nand_host(struct nand_chip *chip)
535 return container_of(chip, struct qcom_nand_host, chip);
538 static inline struct qcom_nand_controller *
539 get_qcom_nand_controller(struct nand_chip *chip)
541 return container_of(chip->controller, struct qcom_nand_controller,
545 static inline u32 nandc_read(struct qcom_nand_controller *nandc, int offset)
547 return ioread32(nandc->base + offset);
550 static inline void nandc_write(struct qcom_nand_controller *nandc, int offset,
553 iowrite32(val, nandc->base + offset);
556 static inline void nandc_read_buffer_sync(struct qcom_nand_controller *nandc,
559 if (!nandc->props->is_bam)
563 dma_sync_single_for_cpu(nandc->dev, nandc->reg_read_dma,
565 sizeof(*nandc->reg_read_buf),
568 dma_sync_single_for_device(nandc->dev, nandc->reg_read_dma,
570 sizeof(*nandc->reg_read_buf),
574 static __le32 *offset_to_nandc_reg(struct nandc_regs *regs, int offset)
583 case NAND_FLASH_CHIP_SELECT:
584 return ®s->chip_sel;
587 case NAND_FLASH_STATUS:
588 return ®s->clrflashstatus;
593 case NAND_DEV0_ECC_CFG:
594 return ®s->ecc_bch_cfg;
595 case NAND_READ_STATUS:
596 return ®s->clrreadstatus;
599 case NAND_DEV_CMD1_RESTORE:
600 return ®s->orig_cmd1;
601 case NAND_DEV_CMD_VLD:
603 case NAND_DEV_CMD_VLD_RESTORE:
604 return ®s->orig_vld;
605 case NAND_EBI2_ECC_BUF_CFG:
606 return ®s->ecc_buf_cfg;
607 case NAND_READ_LOCATION_0:
608 return ®s->read_location0;
609 case NAND_READ_LOCATION_1:
610 return ®s->read_location1;
611 case NAND_READ_LOCATION_2:
612 return ®s->read_location2;
613 case NAND_READ_LOCATION_3:
614 return ®s->read_location3;
620 static void nandc_set_reg(struct qcom_nand_controller *nandc, int offset,
623 struct nandc_regs *regs = nandc->regs;
626 reg = offset_to_nandc_reg(regs, offset);
629 *reg = cpu_to_le32(val);
632 /* helper to configure address register values */
633 static void set_address(struct qcom_nand_host *host, u16 column, int page)
635 struct nand_chip *chip = &host->chip;
636 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
638 if (chip->options & NAND_BUSWIDTH_16)
641 nandc_set_reg(nandc, NAND_ADDR0, page << 16 | column);
642 nandc_set_reg(nandc, NAND_ADDR1, page >> 16 & 0xff);
646 * update_rw_regs: set up read/write register values, these will be
647 * written to the NAND controller registers via DMA
649 * @num_cw: number of steps for the read/write operation
650 * @read: read or write operation
652 static void update_rw_regs(struct qcom_nand_host *host, int num_cw, bool read)
654 struct nand_chip *chip = &host->chip;
655 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
656 u32 cmd, cfg0, cfg1, ecc_bch_cfg;
660 cmd = PAGE_READ_WITH_ECC | PAGE_ACC | LAST_PAGE;
662 cmd = PAGE_READ | PAGE_ACC | LAST_PAGE;
664 cmd = PROGRAM_PAGE | PAGE_ACC | LAST_PAGE;
668 cfg0 = (host->cfg0 & ~(7U << CW_PER_PAGE)) |
669 (num_cw - 1) << CW_PER_PAGE;
672 ecc_bch_cfg = host->ecc_bch_cfg;
674 cfg0 = (host->cfg0_raw & ~(7U << CW_PER_PAGE)) |
675 (num_cw - 1) << CW_PER_PAGE;
677 cfg1 = host->cfg1_raw;
678 ecc_bch_cfg = 1 << ECC_CFG_ECC_DISABLE;
681 nandc_set_reg(nandc, NAND_FLASH_CMD, cmd);
682 nandc_set_reg(nandc, NAND_DEV0_CFG0, cfg0);
683 nandc_set_reg(nandc, NAND_DEV0_CFG1, cfg1);
684 nandc_set_reg(nandc, NAND_DEV0_ECC_CFG, ecc_bch_cfg);
685 nandc_set_reg(nandc, NAND_EBI2_ECC_BUF_CFG, host->ecc_buf_cfg);
686 nandc_set_reg(nandc, NAND_FLASH_STATUS, host->clrflashstatus);
687 nandc_set_reg(nandc, NAND_READ_STATUS, host->clrreadstatus);
688 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
691 nandc_set_read_loc(nandc, 0, 0, host->use_ecc ?
692 host->cw_data : host->cw_size, 1);
696 * Maps the scatter gather list for DMA transfer and forms the DMA descriptor
697 * for BAM. This descriptor will be added in the NAND DMA descriptor queue
698 * which will be submitted to DMA engine.
700 static int prepare_bam_async_desc(struct qcom_nand_controller *nandc,
701 struct dma_chan *chan,
704 struct desc_info *desc;
705 struct scatterlist *sgl;
706 unsigned int sgl_cnt;
708 struct bam_transaction *bam_txn = nandc->bam_txn;
709 enum dma_transfer_direction dir_eng;
710 struct dma_async_tx_descriptor *dma_desc;
712 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
716 if (chan == nandc->cmd_chan) {
717 sgl = &bam_txn->cmd_sgl[bam_txn->cmd_sgl_start];
718 sgl_cnt = bam_txn->cmd_sgl_pos - bam_txn->cmd_sgl_start;
719 bam_txn->cmd_sgl_start = bam_txn->cmd_sgl_pos;
720 dir_eng = DMA_MEM_TO_DEV;
721 desc->dir = DMA_TO_DEVICE;
722 } else if (chan == nandc->tx_chan) {
723 sgl = &bam_txn->data_sgl[bam_txn->tx_sgl_start];
724 sgl_cnt = bam_txn->tx_sgl_pos - bam_txn->tx_sgl_start;
725 bam_txn->tx_sgl_start = bam_txn->tx_sgl_pos;
726 dir_eng = DMA_MEM_TO_DEV;
727 desc->dir = DMA_TO_DEVICE;
729 sgl = &bam_txn->data_sgl[bam_txn->rx_sgl_start];
730 sgl_cnt = bam_txn->rx_sgl_pos - bam_txn->rx_sgl_start;
731 bam_txn->rx_sgl_start = bam_txn->rx_sgl_pos;
732 dir_eng = DMA_DEV_TO_MEM;
733 desc->dir = DMA_FROM_DEVICE;
736 sg_mark_end(sgl + sgl_cnt - 1);
737 ret = dma_map_sg(nandc->dev, sgl, sgl_cnt, desc->dir);
739 dev_err(nandc->dev, "failure in mapping desc\n");
744 desc->sgl_cnt = sgl_cnt;
747 dma_desc = dmaengine_prep_slave_sg(chan, sgl, sgl_cnt, dir_eng,
751 dev_err(nandc->dev, "failure in prep desc\n");
752 dma_unmap_sg(nandc->dev, sgl, sgl_cnt, desc->dir);
757 desc->dma_desc = dma_desc;
759 list_add_tail(&desc->node, &nandc->desc_list);
765 * Prepares the command descriptor for BAM DMA which will be used for NAND
766 * register reads and writes. The command descriptor requires the command
767 * to be formed in command element type so this function uses the command
768 * element from bam transaction ce array and fills the same with required
769 * data. A single SGL can contain multiple command elements so
770 * NAND_BAM_NEXT_SGL will be used for starting the separate SGL
771 * after the current command element.
773 static int prep_bam_dma_desc_cmd(struct qcom_nand_controller *nandc, bool read,
774 int reg_off, const void *vaddr,
775 int size, unsigned int flags)
779 struct bam_cmd_element *bam_ce_buffer;
780 struct bam_transaction *bam_txn = nandc->bam_txn;
782 bam_ce_buffer = &bam_txn->bam_ce[bam_txn->bam_ce_pos];
784 /* fill the command desc */
785 for (i = 0; i < size; i++) {
787 bam_prep_ce(&bam_ce_buffer[i],
788 nandc_reg_phys(nandc, reg_off + 4 * i),
790 reg_buf_dma_addr(nandc,
791 (__le32 *)vaddr + i));
793 bam_prep_ce_le32(&bam_ce_buffer[i],
794 nandc_reg_phys(nandc, reg_off + 4 * i),
796 *((__le32 *)vaddr + i));
799 bam_txn->bam_ce_pos += size;
801 /* use the separate sgl after this command */
802 if (flags & NAND_BAM_NEXT_SGL) {
803 bam_ce_buffer = &bam_txn->bam_ce[bam_txn->bam_ce_start];
804 bam_ce_size = (bam_txn->bam_ce_pos -
805 bam_txn->bam_ce_start) *
806 sizeof(struct bam_cmd_element);
807 sg_set_buf(&bam_txn->cmd_sgl[bam_txn->cmd_sgl_pos],
808 bam_ce_buffer, bam_ce_size);
809 bam_txn->cmd_sgl_pos++;
810 bam_txn->bam_ce_start = bam_txn->bam_ce_pos;
812 if (flags & NAND_BAM_NWD) {
813 ret = prepare_bam_async_desc(nandc, nandc->cmd_chan,
825 * Prepares the data descriptor for BAM DMA which will be used for NAND
826 * data reads and writes.
828 static int prep_bam_dma_desc_data(struct qcom_nand_controller *nandc, bool read,
830 int size, unsigned int flags)
833 struct bam_transaction *bam_txn = nandc->bam_txn;
836 sg_set_buf(&bam_txn->data_sgl[bam_txn->rx_sgl_pos],
838 bam_txn->rx_sgl_pos++;
840 sg_set_buf(&bam_txn->data_sgl[bam_txn->tx_sgl_pos],
842 bam_txn->tx_sgl_pos++;
845 * BAM will only set EOT for DMA_PREP_INTERRUPT so if this flag
846 * is not set, form the DMA descriptor
848 if (!(flags & NAND_BAM_NO_EOT)) {
849 ret = prepare_bam_async_desc(nandc, nandc->tx_chan,
859 static int prep_adm_dma_desc(struct qcom_nand_controller *nandc, bool read,
860 int reg_off, const void *vaddr, int size,
863 struct desc_info *desc;
864 struct dma_async_tx_descriptor *dma_desc;
865 struct scatterlist *sgl;
866 struct dma_slave_config slave_conf;
867 enum dma_transfer_direction dir_eng;
870 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
874 sgl = &desc->adm_sgl;
876 sg_init_one(sgl, vaddr, size);
879 dir_eng = DMA_DEV_TO_MEM;
880 desc->dir = DMA_FROM_DEVICE;
882 dir_eng = DMA_MEM_TO_DEV;
883 desc->dir = DMA_TO_DEVICE;
886 ret = dma_map_sg(nandc->dev, sgl, 1, desc->dir);
892 memset(&slave_conf, 0x00, sizeof(slave_conf));
894 slave_conf.device_fc = flow_control;
896 slave_conf.src_maxburst = 16;
897 slave_conf.src_addr = nandc->base_dma + reg_off;
898 slave_conf.slave_id = nandc->data_crci;
900 slave_conf.dst_maxburst = 16;
901 slave_conf.dst_addr = nandc->base_dma + reg_off;
902 slave_conf.slave_id = nandc->cmd_crci;
905 ret = dmaengine_slave_config(nandc->chan, &slave_conf);
907 dev_err(nandc->dev, "failed to configure dma channel\n");
911 dma_desc = dmaengine_prep_slave_sg(nandc->chan, sgl, 1, dir_eng, 0);
913 dev_err(nandc->dev, "failed to prepare desc\n");
918 desc->dma_desc = dma_desc;
920 list_add_tail(&desc->node, &nandc->desc_list);
930 * read_reg_dma: prepares a descriptor to read a given number of
931 * contiguous registers to the reg_read_buf pointer
933 * @first: offset of the first register in the contiguous block
934 * @num_regs: number of registers to read
935 * @flags: flags to control DMA descriptor preparation
937 static int read_reg_dma(struct qcom_nand_controller *nandc, int first,
938 int num_regs, unsigned int flags)
940 bool flow_control = false;
943 vaddr = nandc->reg_read_buf + nandc->reg_read_pos;
944 nandc->reg_read_pos += num_regs;
946 if (first == NAND_DEV_CMD_VLD || first == NAND_DEV_CMD1)
947 first = dev_cmd_reg_addr(nandc, first);
949 if (nandc->props->is_bam)
950 return prep_bam_dma_desc_cmd(nandc, true, first, vaddr,
953 if (first == NAND_READ_ID || first == NAND_FLASH_STATUS)
956 return prep_adm_dma_desc(nandc, true, first, vaddr,
957 num_regs * sizeof(u32), flow_control);
961 * write_reg_dma: prepares a descriptor to write a given number of
962 * contiguous registers
964 * @first: offset of the first register in the contiguous block
965 * @num_regs: number of registers to write
966 * @flags: flags to control DMA descriptor preparation
968 static int write_reg_dma(struct qcom_nand_controller *nandc, int first,
969 int num_regs, unsigned int flags)
971 bool flow_control = false;
972 struct nandc_regs *regs = nandc->regs;
975 vaddr = offset_to_nandc_reg(regs, first);
977 if (first == NAND_ERASED_CW_DETECT_CFG) {
978 if (flags & NAND_ERASED_CW_SET)
979 vaddr = ®s->erased_cw_detect_cfg_set;
981 vaddr = ®s->erased_cw_detect_cfg_clr;
984 if (first == NAND_EXEC_CMD)
985 flags |= NAND_BAM_NWD;
987 if (first == NAND_DEV_CMD1_RESTORE || first == NAND_DEV_CMD1)
988 first = dev_cmd_reg_addr(nandc, NAND_DEV_CMD1);
990 if (first == NAND_DEV_CMD_VLD_RESTORE || first == NAND_DEV_CMD_VLD)
991 first = dev_cmd_reg_addr(nandc, NAND_DEV_CMD_VLD);
993 if (nandc->props->is_bam)
994 return prep_bam_dma_desc_cmd(nandc, false, first, vaddr,
997 if (first == NAND_FLASH_CMD)
1000 return prep_adm_dma_desc(nandc, false, first, vaddr,
1001 num_regs * sizeof(u32), flow_control);
1005 * read_data_dma: prepares a DMA descriptor to transfer data from the
1006 * controller's internal buffer to the buffer 'vaddr'
1008 * @reg_off: offset within the controller's data buffer
1009 * @vaddr: virtual address of the buffer we want to write to
1010 * @size: DMA transaction size in bytes
1011 * @flags: flags to control DMA descriptor preparation
1013 static int read_data_dma(struct qcom_nand_controller *nandc, int reg_off,
1014 const u8 *vaddr, int size, unsigned int flags)
1016 if (nandc->props->is_bam)
1017 return prep_bam_dma_desc_data(nandc, true, vaddr, size, flags);
1019 return prep_adm_dma_desc(nandc, true, reg_off, vaddr, size, false);
1023 * write_data_dma: prepares a DMA descriptor to transfer data from
1024 * 'vaddr' to the controller's internal buffer
1026 * @reg_off: offset within the controller's data buffer
1027 * @vaddr: virtual address of the buffer we want to read from
1028 * @size: DMA transaction size in bytes
1029 * @flags: flags to control DMA descriptor preparation
1031 static int write_data_dma(struct qcom_nand_controller *nandc, int reg_off,
1032 const u8 *vaddr, int size, unsigned int flags)
1034 if (nandc->props->is_bam)
1035 return prep_bam_dma_desc_data(nandc, false, vaddr, size, flags);
1037 return prep_adm_dma_desc(nandc, false, reg_off, vaddr, size, false);
1041 * Helper to prepare DMA descriptors for configuring registers
1042 * before reading a NAND page.
1044 static void config_nand_page_read(struct qcom_nand_controller *nandc)
1046 write_reg_dma(nandc, NAND_ADDR0, 2, 0);
1047 write_reg_dma(nandc, NAND_DEV0_CFG0, 3, 0);
1048 write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1, 0);
1049 write_reg_dma(nandc, NAND_ERASED_CW_DETECT_CFG, 1, 0);
1050 write_reg_dma(nandc, NAND_ERASED_CW_DETECT_CFG, 1,
1051 NAND_ERASED_CW_SET | NAND_BAM_NEXT_SGL);
1055 * Helper to prepare DMA descriptors for configuring registers
1056 * before reading each codeword in NAND page.
1058 static void config_nand_cw_read(struct qcom_nand_controller *nandc)
1060 if (nandc->props->is_bam)
1061 write_reg_dma(nandc, NAND_READ_LOCATION_0, 4,
1064 write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
1065 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
1067 read_reg_dma(nandc, NAND_FLASH_STATUS, 2, 0);
1068 read_reg_dma(nandc, NAND_ERASED_CW_DETECT_STATUS, 1,
1073 * Helper to prepare dma descriptors to configure registers needed for reading a
1074 * single codeword in page
1076 static void config_nand_single_cw_page_read(struct qcom_nand_controller *nandc)
1078 config_nand_page_read(nandc);
1079 config_nand_cw_read(nandc);
1083 * Helper to prepare DMA descriptors used to configure registers needed for
1084 * before writing a NAND page.
1086 static void config_nand_page_write(struct qcom_nand_controller *nandc)
1088 write_reg_dma(nandc, NAND_ADDR0, 2, 0);
1089 write_reg_dma(nandc, NAND_DEV0_CFG0, 3, 0);
1090 write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1,
1095 * Helper to prepare DMA descriptors for configuring registers
1096 * before writing each codeword in NAND page.
1098 static void config_nand_cw_write(struct qcom_nand_controller *nandc)
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);
1103 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
1105 write_reg_dma(nandc, NAND_FLASH_STATUS, 1, 0);
1106 write_reg_dma(nandc, NAND_READ_STATUS, 1, NAND_BAM_NEXT_SGL);
1110 * the following functions are used within chip->cmdfunc() to perform different
1111 * NAND_CMD_* commands
1114 /* sets up descriptors for NAND_CMD_PARAM */
1115 static int nandc_param(struct qcom_nand_host *host)
1117 struct nand_chip *chip = &host->chip;
1118 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1121 * NAND_CMD_PARAM is called before we know much about the FLASH chip
1122 * in use. we configure the controller to perform a raw read of 512
1123 * bytes to read onfi params
1125 nandc_set_reg(nandc, NAND_FLASH_CMD, PAGE_READ | PAGE_ACC | LAST_PAGE);
1126 nandc_set_reg(nandc, NAND_ADDR0, 0);
1127 nandc_set_reg(nandc, NAND_ADDR1, 0);
1128 nandc_set_reg(nandc, NAND_DEV0_CFG0, 0 << CW_PER_PAGE
1129 | 512 << UD_SIZE_BYTES
1130 | 5 << NUM_ADDR_CYCLES
1131 | 0 << SPARE_SIZE_BYTES);
1132 nandc_set_reg(nandc, NAND_DEV0_CFG1, 7 << NAND_RECOVERY_CYCLES
1133 | 0 << CS_ACTIVE_BSY
1134 | 17 << BAD_BLOCK_BYTE_NUM
1135 | 1 << BAD_BLOCK_IN_SPARE_AREA
1136 | 2 << WR_RD_BSY_GAP
1138 | 1 << DEV0_CFG1_ECC_DISABLE);
1139 nandc_set_reg(nandc, NAND_EBI2_ECC_BUF_CFG, 1 << ECC_CFG_ECC_DISABLE);
1141 /* configure CMD1 and VLD for ONFI param probing */
1142 nandc_set_reg(nandc, NAND_DEV_CMD_VLD,
1143 (nandc->vld & ~READ_START_VLD));
1144 nandc_set_reg(nandc, NAND_DEV_CMD1,
1145 (nandc->cmd1 & ~(0xFF << READ_ADDR))
1146 | NAND_CMD_PARAM << READ_ADDR);
1148 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1150 nandc_set_reg(nandc, NAND_DEV_CMD1_RESTORE, nandc->cmd1);
1151 nandc_set_reg(nandc, NAND_DEV_CMD_VLD_RESTORE, nandc->vld);
1152 nandc_set_read_loc(nandc, 0, 0, 512, 1);
1154 write_reg_dma(nandc, NAND_DEV_CMD_VLD, 1, 0);
1155 write_reg_dma(nandc, NAND_DEV_CMD1, 1, NAND_BAM_NEXT_SGL);
1157 nandc->buf_count = 512;
1158 memset(nandc->data_buffer, 0xff, nandc->buf_count);
1160 config_nand_single_cw_page_read(nandc);
1162 read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer,
1163 nandc->buf_count, 0);
1165 /* restore CMD1 and VLD regs */
1166 write_reg_dma(nandc, NAND_DEV_CMD1_RESTORE, 1, 0);
1167 write_reg_dma(nandc, NAND_DEV_CMD_VLD_RESTORE, 1, NAND_BAM_NEXT_SGL);
1172 /* sets up descriptors for NAND_CMD_ERASE1 */
1173 static int erase_block(struct qcom_nand_host *host, int page_addr)
1175 struct nand_chip *chip = &host->chip;
1176 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1178 nandc_set_reg(nandc, NAND_FLASH_CMD,
1179 BLOCK_ERASE | PAGE_ACC | LAST_PAGE);
1180 nandc_set_reg(nandc, NAND_ADDR0, page_addr);
1181 nandc_set_reg(nandc, NAND_ADDR1, 0);
1182 nandc_set_reg(nandc, NAND_DEV0_CFG0,
1183 host->cfg0_raw & ~(7 << CW_PER_PAGE));
1184 nandc_set_reg(nandc, NAND_DEV0_CFG1, host->cfg1_raw);
1185 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1186 nandc_set_reg(nandc, NAND_FLASH_STATUS, host->clrflashstatus);
1187 nandc_set_reg(nandc, NAND_READ_STATUS, host->clrreadstatus);
1189 write_reg_dma(nandc, NAND_FLASH_CMD, 3, NAND_BAM_NEXT_SGL);
1190 write_reg_dma(nandc, NAND_DEV0_CFG0, 2, NAND_BAM_NEXT_SGL);
1191 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
1193 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
1195 write_reg_dma(nandc, NAND_FLASH_STATUS, 1, 0);
1196 write_reg_dma(nandc, NAND_READ_STATUS, 1, NAND_BAM_NEXT_SGL);
1201 /* sets up descriptors for NAND_CMD_READID */
1202 static int read_id(struct qcom_nand_host *host, int column)
1204 struct nand_chip *chip = &host->chip;
1205 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1210 nandc_set_reg(nandc, NAND_FLASH_CMD, FETCH_ID);
1211 nandc_set_reg(nandc, NAND_ADDR0, column);
1212 nandc_set_reg(nandc, NAND_ADDR1, 0);
1213 nandc_set_reg(nandc, NAND_FLASH_CHIP_SELECT,
1214 nandc->props->is_bam ? 0 : DM_EN);
1215 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1217 write_reg_dma(nandc, NAND_FLASH_CMD, 4, NAND_BAM_NEXT_SGL);
1218 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
1220 read_reg_dma(nandc, NAND_READ_ID, 1, NAND_BAM_NEXT_SGL);
1225 /* sets up descriptors for NAND_CMD_RESET */
1226 static int reset(struct qcom_nand_host *host)
1228 struct nand_chip *chip = &host->chip;
1229 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1231 nandc_set_reg(nandc, NAND_FLASH_CMD, RESET_DEVICE);
1232 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1234 write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
1235 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
1237 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
1242 /* helpers to submit/free our list of dma descriptors */
1243 static int submit_descs(struct qcom_nand_controller *nandc)
1245 struct desc_info *desc;
1246 dma_cookie_t cookie = 0;
1247 struct bam_transaction *bam_txn = nandc->bam_txn;
1250 if (nandc->props->is_bam) {
1251 if (bam_txn->rx_sgl_pos > bam_txn->rx_sgl_start) {
1252 r = prepare_bam_async_desc(nandc, nandc->rx_chan, 0);
1257 if (bam_txn->tx_sgl_pos > bam_txn->tx_sgl_start) {
1258 r = prepare_bam_async_desc(nandc, nandc->tx_chan,
1259 DMA_PREP_INTERRUPT);
1264 if (bam_txn->cmd_sgl_pos > bam_txn->cmd_sgl_start) {
1265 r = prepare_bam_async_desc(nandc, nandc->cmd_chan,
1272 list_for_each_entry(desc, &nandc->desc_list, node)
1273 cookie = dmaengine_submit(desc->dma_desc);
1275 if (nandc->props->is_bam) {
1276 dma_async_issue_pending(nandc->tx_chan);
1277 dma_async_issue_pending(nandc->rx_chan);
1279 if (dma_sync_wait(nandc->cmd_chan, cookie) != DMA_COMPLETE)
1282 if (dma_sync_wait(nandc->chan, cookie) != DMA_COMPLETE)
1289 static void free_descs(struct qcom_nand_controller *nandc)
1291 struct desc_info *desc, *n;
1293 list_for_each_entry_safe(desc, n, &nandc->desc_list, node) {
1294 list_del(&desc->node);
1296 if (nandc->props->is_bam)
1297 dma_unmap_sg(nandc->dev, desc->bam_sgl,
1298 desc->sgl_cnt, desc->dir);
1300 dma_unmap_sg(nandc->dev, &desc->adm_sgl, 1,
1307 /* reset the register read buffer for next NAND operation */
1308 static void clear_read_regs(struct qcom_nand_controller *nandc)
1310 nandc->reg_read_pos = 0;
1311 nandc_read_buffer_sync(nandc, false);
1314 static void pre_command(struct qcom_nand_host *host, int command)
1316 struct nand_chip *chip = &host->chip;
1317 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1319 nandc->buf_count = 0;
1320 nandc->buf_start = 0;
1321 host->use_ecc = false;
1322 host->last_command = command;
1324 clear_read_regs(nandc);
1326 if (command == NAND_CMD_RESET || command == NAND_CMD_READID ||
1327 command == NAND_CMD_PARAM || command == NAND_CMD_ERASE1)
1328 clear_bam_transaction(nandc);
1332 * this is called after NAND_CMD_PAGEPROG and NAND_CMD_ERASE1 to set our
1333 * privately maintained status byte, this status byte can be read after
1334 * NAND_CMD_STATUS is called
1336 static void parse_erase_write_errors(struct qcom_nand_host *host, int command)
1338 struct nand_chip *chip = &host->chip;
1339 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1340 struct nand_ecc_ctrl *ecc = &chip->ecc;
1344 num_cw = command == NAND_CMD_PAGEPROG ? ecc->steps : 1;
1345 nandc_read_buffer_sync(nandc, true);
1347 for (i = 0; i < num_cw; i++) {
1348 u32 flash_status = le32_to_cpu(nandc->reg_read_buf[i]);
1350 if (flash_status & FS_MPU_ERR)
1351 host->status &= ~NAND_STATUS_WP;
1353 if (flash_status & FS_OP_ERR || (i == (num_cw - 1) &&
1355 FS_DEVICE_STS_ERR)))
1356 host->status |= NAND_STATUS_FAIL;
1360 static void post_command(struct qcom_nand_host *host, int command)
1362 struct nand_chip *chip = &host->chip;
1363 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1366 case NAND_CMD_READID:
1367 nandc_read_buffer_sync(nandc, true);
1368 memcpy(nandc->data_buffer, nandc->reg_read_buf,
1371 case NAND_CMD_PAGEPROG:
1372 case NAND_CMD_ERASE1:
1373 parse_erase_write_errors(host, command);
1381 * Implements chip->cmdfunc. It's only used for a limited set of commands.
1382 * The rest of the commands wouldn't be called by upper layers. For example,
1383 * NAND_CMD_READOOB would never be called because we have our own versions
1384 * of read_oob ops for nand_ecc_ctrl.
1386 static void qcom_nandc_command(struct mtd_info *mtd, unsigned int command,
1387 int column, int page_addr)
1389 struct nand_chip *chip = mtd_to_nand(mtd);
1390 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1391 struct nand_ecc_ctrl *ecc = &chip->ecc;
1392 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1396 pre_command(host, command);
1399 case NAND_CMD_RESET:
1404 case NAND_CMD_READID:
1405 nandc->buf_count = 4;
1406 ret = read_id(host, column);
1410 case NAND_CMD_PARAM:
1411 ret = nandc_param(host);
1415 case NAND_CMD_ERASE1:
1416 ret = erase_block(host, page_addr);
1420 case NAND_CMD_READ0:
1421 /* we read the entire page for now */
1422 WARN_ON(column != 0);
1424 host->use_ecc = true;
1425 set_address(host, 0, page_addr);
1426 update_rw_regs(host, ecc->steps, true);
1429 case NAND_CMD_SEQIN:
1430 WARN_ON(column != 0);
1431 set_address(host, 0, page_addr);
1434 case NAND_CMD_PAGEPROG:
1435 case NAND_CMD_STATUS:
1442 dev_err(nandc->dev, "failure executing command %d\n",
1449 ret = submit_descs(nandc);
1452 "failure submitting descs for command %d\n",
1458 post_command(host, command);
1462 * when using BCH ECC, the HW flags an error in NAND_FLASH_STATUS if it read
1463 * an erased CW, and reports an erased CW in NAND_ERASED_CW_DETECT_STATUS.
1465 * when using RS ECC, the HW reports the same erros when reading an erased CW,
1466 * but it notifies that it is an erased CW by placing special characters at
1467 * certain offsets in the buffer.
1469 * verify if the page is erased or not, and fix up the page for RS ECC by
1470 * replacing the special characters with 0xff.
1472 static bool erased_chunk_check_and_fixup(u8 *data_buf, int data_len)
1477 * an erased page flags an error in NAND_FLASH_STATUS, check if the page
1478 * is erased by looking for 0x54s at offsets 3 and 175 from the
1479 * beginning of each codeword
1482 empty1 = data_buf[3];
1483 empty2 = data_buf[175];
1486 * if the erased codework markers, if they exist override them with
1489 if ((empty1 == 0x54 && empty2 == 0xff) ||
1490 (empty1 == 0xff && empty2 == 0x54)) {
1492 data_buf[175] = 0xff;
1496 * check if the entire chunk contains 0xffs or not. if it doesn't, then
1497 * restore the original values at the special offsets
1499 if (memchr_inv(data_buf, 0xff, data_len)) {
1500 data_buf[3] = empty1;
1501 data_buf[175] = empty2;
1516 * reads back status registers set by the controller to notify page read
1517 * errors. this is equivalent to what 'ecc->correct()' would do.
1519 static int parse_read_errors(struct qcom_nand_host *host, u8 *data_buf,
1522 struct nand_chip *chip = &host->chip;
1523 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1524 struct mtd_info *mtd = nand_to_mtd(chip);
1525 struct nand_ecc_ctrl *ecc = &chip->ecc;
1526 unsigned int max_bitflips = 0;
1527 struct read_stats *buf;
1530 buf = (struct read_stats *)nandc->reg_read_buf;
1531 nandc_read_buffer_sync(nandc, true);
1533 for (i = 0; i < ecc->steps; i++, buf++) {
1534 u32 flash, buffer, erased_cw;
1535 int data_len, oob_len;
1537 if (i == (ecc->steps - 1)) {
1538 data_len = ecc->size - ((ecc->steps - 1) << 2);
1539 oob_len = ecc->steps << 2;
1541 data_len = host->cw_data;
1545 flash = le32_to_cpu(buf->flash);
1546 buffer = le32_to_cpu(buf->buffer);
1547 erased_cw = le32_to_cpu(buf->erased_cw);
1549 if (flash & (FS_OP_ERR | FS_MPU_ERR)) {
1552 /* ignore erased codeword errors */
1553 if (host->bch_enabled) {
1554 erased = (erased_cw & ERASED_CW) == ERASED_CW ?
1557 erased = erased_chunk_check_and_fixup(data_buf,
1562 data_buf += data_len;
1564 oob_buf += oob_len + ecc->bytes;
1568 if (buffer & BS_UNCORRECTABLE_BIT) {
1569 int ret, ecclen, extraooblen;
1572 eccbuf = oob_buf ? oob_buf + oob_len : NULL;
1573 ecclen = oob_buf ? host->ecc_bytes_hw : 0;
1574 extraooblen = oob_buf ? oob_len : 0;
1577 * make sure it isn't an erased page reported
1578 * as not-erased by HW because of a few bitflips
1580 ret = nand_check_erased_ecc_chunk(data_buf,
1581 data_len, eccbuf, ecclen, oob_buf,
1582 extraooblen, ecc->strength);
1584 mtd->ecc_stats.failed++;
1586 mtd->ecc_stats.corrected += ret;
1588 max_t(unsigned int, max_bitflips, ret);
1594 stat = buffer & BS_CORRECTABLE_ERR_MSK;
1595 mtd->ecc_stats.corrected += stat;
1596 max_bitflips = max(max_bitflips, stat);
1599 data_buf += data_len;
1601 oob_buf += oob_len + ecc->bytes;
1604 return max_bitflips;
1608 * helper to perform the actual page read operation, used by ecc->read_page(),
1611 static int read_page_ecc(struct qcom_nand_host *host, u8 *data_buf,
1614 struct nand_chip *chip = &host->chip;
1615 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1616 struct nand_ecc_ctrl *ecc = &chip->ecc;
1619 config_nand_page_read(nandc);
1621 /* queue cmd descs for each codeword */
1622 for (i = 0; i < ecc->steps; i++) {
1623 int data_size, oob_size;
1625 if (i == (ecc->steps - 1)) {
1626 data_size = ecc->size - ((ecc->steps - 1) << 2);
1627 oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
1630 data_size = host->cw_data;
1631 oob_size = host->ecc_bytes_hw + host->spare_bytes;
1634 if (nandc->props->is_bam) {
1635 if (data_buf && oob_buf) {
1636 nandc_set_read_loc(nandc, 0, 0, data_size, 0);
1637 nandc_set_read_loc(nandc, 1, data_size,
1639 } else if (data_buf) {
1640 nandc_set_read_loc(nandc, 0, 0, data_size, 1);
1642 nandc_set_read_loc(nandc, 0, data_size,
1647 config_nand_cw_read(nandc);
1650 read_data_dma(nandc, FLASH_BUF_ACC, data_buf,
1654 * when ecc is enabled, the controller doesn't read the real
1655 * or dummy bad block markers in each chunk. To maintain a
1656 * consistent layout across RAW and ECC reads, we just
1657 * leave the real/dummy BBM offsets empty (i.e, filled with
1663 for (j = 0; j < host->bbm_size; j++)
1666 read_data_dma(nandc, FLASH_BUF_ACC + data_size,
1667 oob_buf, oob_size, 0);
1671 data_buf += data_size;
1673 oob_buf += oob_size;
1676 ret = submit_descs(nandc);
1678 dev_err(nandc->dev, "failure to read page/oob\n");
1686 * a helper that copies the last step/codeword of a page (containing free oob)
1687 * into our local buffer
1689 static int copy_last_cw(struct qcom_nand_host *host, int page)
1691 struct nand_chip *chip = &host->chip;
1692 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1693 struct nand_ecc_ctrl *ecc = &chip->ecc;
1697 clear_read_regs(nandc);
1699 size = host->use_ecc ? host->cw_data : host->cw_size;
1701 /* prepare a clean read buffer */
1702 memset(nandc->data_buffer, 0xff, size);
1704 set_address(host, host->cw_size * (ecc->steps - 1), page);
1705 update_rw_regs(host, 1, true);
1707 config_nand_single_cw_page_read(nandc);
1709 read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer, size, 0);
1711 ret = submit_descs(nandc);
1713 dev_err(nandc->dev, "failed to copy last codeword\n");
1720 /* implements ecc->read_page() */
1721 static int qcom_nandc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1722 uint8_t *buf, int oob_required, int page)
1724 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1725 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1726 u8 *data_buf, *oob_buf = NULL;
1729 nand_read_page_op(chip, page, 0, NULL, 0);
1731 oob_buf = oob_required ? chip->oob_poi : NULL;
1733 clear_bam_transaction(nandc);
1734 ret = read_page_ecc(host, data_buf, oob_buf);
1736 dev_err(nandc->dev, "failure to read page\n");
1740 return parse_read_errors(host, data_buf, oob_buf);
1743 /* implements ecc->read_page_raw() */
1744 static int qcom_nandc_read_page_raw(struct mtd_info *mtd,
1745 struct nand_chip *chip, uint8_t *buf,
1746 int oob_required, int page)
1748 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1749 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1750 u8 *data_buf, *oob_buf;
1751 struct nand_ecc_ctrl *ecc = &chip->ecc;
1755 nand_read_page_op(chip, page, 0, NULL, 0);
1757 oob_buf = chip->oob_poi;
1759 host->use_ecc = false;
1761 clear_bam_transaction(nandc);
1762 update_rw_regs(host, ecc->steps, true);
1763 config_nand_page_read(nandc);
1765 for (i = 0; i < ecc->steps; i++) {
1766 int data_size1, data_size2, oob_size1, oob_size2;
1767 int reg_off = FLASH_BUF_ACC;
1769 data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
1770 oob_size1 = host->bbm_size;
1772 if (i == (ecc->steps - 1)) {
1773 data_size2 = ecc->size - data_size1 -
1774 ((ecc->steps - 1) << 2);
1775 oob_size2 = (ecc->steps << 2) + host->ecc_bytes_hw +
1778 data_size2 = host->cw_data - data_size1;
1779 oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
1782 if (nandc->props->is_bam) {
1784 nandc_set_read_loc(nandc, 0, read_loc, data_size1, 0);
1785 read_loc += data_size1;
1787 nandc_set_read_loc(nandc, 1, read_loc, oob_size1, 0);
1788 read_loc += oob_size1;
1790 nandc_set_read_loc(nandc, 2, read_loc, data_size2, 0);
1791 read_loc += data_size2;
1793 nandc_set_read_loc(nandc, 3, read_loc, oob_size2, 1);
1796 config_nand_cw_read(nandc);
1798 read_data_dma(nandc, reg_off, data_buf, data_size1, 0);
1799 reg_off += data_size1;
1800 data_buf += data_size1;
1802 read_data_dma(nandc, reg_off, oob_buf, oob_size1, 0);
1803 reg_off += oob_size1;
1804 oob_buf += oob_size1;
1806 read_data_dma(nandc, reg_off, data_buf, data_size2, 0);
1807 reg_off += data_size2;
1808 data_buf += data_size2;
1810 read_data_dma(nandc, reg_off, oob_buf, oob_size2, 0);
1811 oob_buf += oob_size2;
1814 ret = submit_descs(nandc);
1816 dev_err(nandc->dev, "failure to read raw page\n");
1823 /* implements ecc->read_oob() */
1824 static int qcom_nandc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
1827 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1828 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1829 struct nand_ecc_ctrl *ecc = &chip->ecc;
1832 clear_read_regs(nandc);
1833 clear_bam_transaction(nandc);
1835 host->use_ecc = true;
1836 set_address(host, 0, page);
1837 update_rw_regs(host, ecc->steps, true);
1839 ret = read_page_ecc(host, NULL, chip->oob_poi);
1841 dev_err(nandc->dev, "failure to read oob\n");
1846 /* implements ecc->write_page() */
1847 static int qcom_nandc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1848 const uint8_t *buf, int oob_required, int page)
1850 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1851 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1852 struct nand_ecc_ctrl *ecc = &chip->ecc;
1853 u8 *data_buf, *oob_buf;
1856 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
1858 clear_read_regs(nandc);
1859 clear_bam_transaction(nandc);
1861 data_buf = (u8 *)buf;
1862 oob_buf = chip->oob_poi;
1864 host->use_ecc = true;
1865 update_rw_regs(host, ecc->steps, false);
1866 config_nand_page_write(nandc);
1868 for (i = 0; i < ecc->steps; i++) {
1869 int data_size, oob_size;
1871 if (i == (ecc->steps - 1)) {
1872 data_size = ecc->size - ((ecc->steps - 1) << 2);
1873 oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
1876 data_size = host->cw_data;
1877 oob_size = ecc->bytes;
1881 write_data_dma(nandc, FLASH_BUF_ACC, data_buf, data_size,
1882 i == (ecc->steps - 1) ? NAND_BAM_NO_EOT : 0);
1885 * when ECC is enabled, we don't really need to write anything
1886 * to oob for the first n - 1 codewords since these oob regions
1887 * just contain ECC bytes that's written by the controller
1888 * itself. For the last codeword, we skip the bbm positions and
1889 * write to the free oob area.
1891 if (i == (ecc->steps - 1)) {
1892 oob_buf += host->bbm_size;
1894 write_data_dma(nandc, FLASH_BUF_ACC + data_size,
1895 oob_buf, oob_size, 0);
1898 config_nand_cw_write(nandc);
1900 data_buf += data_size;
1901 oob_buf += oob_size;
1904 ret = submit_descs(nandc);
1906 dev_err(nandc->dev, "failure to write page\n");
1911 ret = nand_prog_page_end_op(chip);
1916 /* implements ecc->write_page_raw() */
1917 static int qcom_nandc_write_page_raw(struct mtd_info *mtd,
1918 struct nand_chip *chip, const uint8_t *buf,
1919 int oob_required, int page)
1921 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1922 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1923 struct nand_ecc_ctrl *ecc = &chip->ecc;
1924 u8 *data_buf, *oob_buf;
1927 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
1928 clear_read_regs(nandc);
1929 clear_bam_transaction(nandc);
1931 data_buf = (u8 *)buf;
1932 oob_buf = chip->oob_poi;
1934 host->use_ecc = false;
1935 update_rw_regs(host, ecc->steps, false);
1936 config_nand_page_write(nandc);
1938 for (i = 0; i < ecc->steps; i++) {
1939 int data_size1, data_size2, oob_size1, oob_size2;
1940 int reg_off = FLASH_BUF_ACC;
1942 data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
1943 oob_size1 = host->bbm_size;
1945 if (i == (ecc->steps - 1)) {
1946 data_size2 = ecc->size - data_size1 -
1947 ((ecc->steps - 1) << 2);
1948 oob_size2 = (ecc->steps << 2) + host->ecc_bytes_hw +
1951 data_size2 = host->cw_data - data_size1;
1952 oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
1955 write_data_dma(nandc, reg_off, data_buf, data_size1,
1957 reg_off += data_size1;
1958 data_buf += data_size1;
1960 write_data_dma(nandc, reg_off, oob_buf, oob_size1,
1962 reg_off += oob_size1;
1963 oob_buf += oob_size1;
1965 write_data_dma(nandc, reg_off, data_buf, data_size2,
1967 reg_off += data_size2;
1968 data_buf += data_size2;
1970 write_data_dma(nandc, reg_off, oob_buf, oob_size2, 0);
1971 oob_buf += oob_size2;
1973 config_nand_cw_write(nandc);
1976 ret = submit_descs(nandc);
1978 dev_err(nandc->dev, "failure to write raw page\n");
1983 ret = nand_prog_page_end_op(chip);
1989 * implements ecc->write_oob()
1991 * the NAND controller cannot write only data or only oob within a codeword,
1992 * since ecc is calculated for the combined codeword. we first copy the
1993 * entire contents for the last codeword(data + oob), replace the old oob
1994 * with the new one in chip->oob_poi, and then write the entire codeword.
1995 * this read-copy-write operation results in a slight performance loss.
1997 static int qcom_nandc_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
2000 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2001 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2002 struct nand_ecc_ctrl *ecc = &chip->ecc;
2003 u8 *oob = chip->oob_poi;
2004 int data_size, oob_size;
2007 host->use_ecc = true;
2009 clear_bam_transaction(nandc);
2010 ret = copy_last_cw(host, page);
2014 clear_read_regs(nandc);
2015 clear_bam_transaction(nandc);
2017 /* calculate the data and oob size for the last codeword/step */
2018 data_size = ecc->size - ((ecc->steps - 1) << 2);
2019 oob_size = mtd->oobavail;
2021 /* override new oob content to last codeword */
2022 mtd_ooblayout_get_databytes(mtd, nandc->data_buffer + data_size, oob,
2025 set_address(host, host->cw_size * (ecc->steps - 1), page);
2026 update_rw_regs(host, 1, false);
2028 config_nand_page_write(nandc);
2029 write_data_dma(nandc, FLASH_BUF_ACC,
2030 nandc->data_buffer, data_size + oob_size, 0);
2031 config_nand_cw_write(nandc);
2033 ret = submit_descs(nandc);
2038 dev_err(nandc->dev, "failure to write oob\n");
2042 return nand_prog_page_end_op(chip);
2045 static int qcom_nandc_block_bad(struct mtd_info *mtd, loff_t ofs)
2047 struct nand_chip *chip = mtd_to_nand(mtd);
2048 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2049 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2050 struct nand_ecc_ctrl *ecc = &chip->ecc;
2051 int page, ret, bbpos, bad = 0;
2054 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
2057 * configure registers for a raw sub page read, the address is set to
2058 * the beginning of the last codeword, we don't care about reading ecc
2059 * portion of oob. we just want the first few bytes from this codeword
2060 * that contains the BBM
2062 host->use_ecc = false;
2064 clear_bam_transaction(nandc);
2065 ret = copy_last_cw(host, page);
2069 flash_status = le32_to_cpu(nandc->reg_read_buf[0]);
2071 if (flash_status & (FS_OP_ERR | FS_MPU_ERR)) {
2072 dev_warn(nandc->dev, "error when trying to read BBM\n");
2076 bbpos = mtd->writesize - host->cw_size * (ecc->steps - 1);
2078 bad = nandc->data_buffer[bbpos] != 0xff;
2080 if (chip->options & NAND_BUSWIDTH_16)
2081 bad = bad || (nandc->data_buffer[bbpos + 1] != 0xff);
2086 static int qcom_nandc_block_markbad(struct mtd_info *mtd, loff_t ofs)
2088 struct nand_chip *chip = mtd_to_nand(mtd);
2089 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2090 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2091 struct nand_ecc_ctrl *ecc = &chip->ecc;
2094 clear_read_regs(nandc);
2095 clear_bam_transaction(nandc);
2098 * to mark the BBM as bad, we flash the entire last codeword with 0s.
2099 * we don't care about the rest of the content in the codeword since
2100 * we aren't going to use this block again
2102 memset(nandc->data_buffer, 0x00, host->cw_size);
2104 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
2107 host->use_ecc = false;
2108 set_address(host, host->cw_size * (ecc->steps - 1), page);
2109 update_rw_regs(host, 1, false);
2111 config_nand_page_write(nandc);
2112 write_data_dma(nandc, FLASH_BUF_ACC,
2113 nandc->data_buffer, host->cw_size, 0);
2114 config_nand_cw_write(nandc);
2116 ret = submit_descs(nandc);
2121 dev_err(nandc->dev, "failure to update BBM\n");
2125 return nand_prog_page_end_op(chip);
2129 * the three functions below implement chip->read_byte(), chip->read_buf()
2130 * and chip->write_buf() respectively. these aren't used for
2131 * reading/writing page data, they are used for smaller data like reading
2134 static uint8_t qcom_nandc_read_byte(struct mtd_info *mtd)
2136 struct nand_chip *chip = mtd_to_nand(mtd);
2137 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2138 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2139 u8 *buf = nandc->data_buffer;
2142 if (host->last_command == NAND_CMD_STATUS) {
2145 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
2150 if (nandc->buf_start < nandc->buf_count)
2151 ret = buf[nandc->buf_start++];
2156 static void qcom_nandc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
2158 struct nand_chip *chip = mtd_to_nand(mtd);
2159 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2160 int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
2162 memcpy(buf, nandc->data_buffer + nandc->buf_start, real_len);
2163 nandc->buf_start += real_len;
2166 static void qcom_nandc_write_buf(struct mtd_info *mtd, const uint8_t *buf,
2169 struct nand_chip *chip = mtd_to_nand(mtd);
2170 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2171 int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
2173 memcpy(nandc->data_buffer + nandc->buf_start, buf, real_len);
2175 nandc->buf_start += real_len;
2178 /* we support only one external chip for now */
2179 static void qcom_nandc_select_chip(struct mtd_info *mtd, int chipnr)
2181 struct nand_chip *chip = mtd_to_nand(mtd);
2182 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2187 dev_warn(nandc->dev, "invalid chip select\n");
2191 * NAND controller page layout info
2193 * Layout with ECC enabled:
2195 * |----------------------| |---------------------------------|
2196 * | xx.......yy| | *********xx.......yy|
2197 * | DATA xx..ECC..yy| | DATA **SPARE**xx..ECC..yy|
2198 * | (516) xx.......yy| | (516-n*4) **(n*4)**xx.......yy|
2199 * | xx.......yy| | *********xx.......yy|
2200 * |----------------------| |---------------------------------|
2201 * codeword 1,2..n-1 codeword n
2202 * <---(528/532 Bytes)--> <-------(528/532 Bytes)--------->
2204 * n = Number of codewords in the page
2206 * * = Spare/free bytes
2207 * x = Unused byte(s)
2208 * y = Reserved byte(s)
2210 * 2K page: n = 4, spare = 16 bytes
2211 * 4K page: n = 8, spare = 32 bytes
2212 * 8K page: n = 16, spare = 64 bytes
2214 * the qcom nand controller operates at a sub page/codeword level. each
2215 * codeword is 528 and 532 bytes for 4 bit and 8 bit ECC modes respectively.
2216 * the number of ECC bytes vary based on the ECC strength and the bus width.
2218 * the first n - 1 codewords contains 516 bytes of user data, the remaining
2219 * 12/16 bytes consist of ECC and reserved data. The nth codeword contains
2220 * both user data and spare(oobavail) bytes that sum up to 516 bytes.
2222 * When we access a page with ECC enabled, the reserved bytes(s) are not
2223 * accessible at all. When reading, we fill up these unreadable positions
2224 * with 0xffs. When writing, the controller skips writing the inaccessible
2227 * Layout with ECC disabled:
2229 * |------------------------------| |---------------------------------------|
2230 * | yy xx.......| | bb *********xx.......|
2231 * | DATA1 yy DATA2 xx..ECC..| | DATA1 bb DATA2 **SPARE**xx..ECC..|
2232 * | (size1) yy (size2) xx.......| | (size1) bb (size2) **(n*4)**xx.......|
2233 * | yy xx.......| | bb *********xx.......|
2234 * |------------------------------| |---------------------------------------|
2235 * codeword 1,2..n-1 codeword n
2236 * <-------(528/532 Bytes)------> <-----------(528/532 Bytes)----------->
2238 * n = Number of codewords in the page
2240 * * = Spare/free bytes
2241 * x = Unused byte(s)
2242 * y = Dummy Bad Bock byte(s)
2243 * b = Real Bad Block byte(s)
2244 * size1/size2 = function of codeword size and 'n'
2246 * when the ECC block is disabled, one reserved byte (or two for 16 bit bus
2247 * width) is now accessible. For the first n - 1 codewords, these are dummy Bad
2248 * Block Markers. In the last codeword, this position contains the real BBM
2250 * In order to have a consistent layout between RAW and ECC modes, we assume
2251 * the following OOB layout arrangement:
2253 * |-----------| |--------------------|
2254 * |yyxx.......| |bb*********xx.......|
2255 * |yyxx..ECC..| |bb*FREEOOB*xx..ECC..|
2256 * |yyxx.......| |bb*********xx.......|
2257 * |yyxx.......| |bb*********xx.......|
2258 * |-----------| |--------------------|
2259 * first n - 1 nth OOB region
2262 * n = Number of codewords in the page
2264 * * = FREE OOB bytes
2265 * y = Dummy bad block byte(s) (inaccessible when ECC enabled)
2266 * x = Unused byte(s)
2267 * b = Real bad block byte(s) (inaccessible when ECC enabled)
2269 * This layout is read as is when ECC is disabled. When ECC is enabled, the
2270 * inaccessible Bad Block byte(s) are ignored when we write to a page/oob,
2271 * and assumed as 0xffs when we read a page/oob. The ECC, unused and
2272 * dummy/real bad block bytes are grouped as ecc bytes (i.e, ecc->bytes is
2273 * the sum of the three).
2275 static int qcom_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
2276 struct mtd_oob_region *oobregion)
2278 struct nand_chip *chip = mtd_to_nand(mtd);
2279 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2280 struct nand_ecc_ctrl *ecc = &chip->ecc;
2286 oobregion->length = (ecc->bytes * (ecc->steps - 1)) +
2288 oobregion->offset = 0;
2290 oobregion->length = host->ecc_bytes_hw + host->spare_bytes;
2291 oobregion->offset = mtd->oobsize - oobregion->length;
2297 static int qcom_nand_ooblayout_free(struct mtd_info *mtd, int section,
2298 struct mtd_oob_region *oobregion)
2300 struct nand_chip *chip = mtd_to_nand(mtd);
2301 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2302 struct nand_ecc_ctrl *ecc = &chip->ecc;
2307 oobregion->length = ecc->steps * 4;
2308 oobregion->offset = ((ecc->steps - 1) * ecc->bytes) + host->bbm_size;
2313 static const struct mtd_ooblayout_ops qcom_nand_ooblayout_ops = {
2314 .ecc = qcom_nand_ooblayout_ecc,
2315 .free = qcom_nand_ooblayout_free,
2318 static int qcom_nand_host_setup(struct qcom_nand_host *host)
2320 struct nand_chip *chip = &host->chip;
2321 struct mtd_info *mtd = nand_to_mtd(chip);
2322 struct nand_ecc_ctrl *ecc = &chip->ecc;
2323 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2324 int cwperpage, bad_block_byte;
2329 * the controller requires each step consists of 512 bytes of data.
2330 * bail out if DT has populated a wrong step size.
2332 if (ecc->size != NANDC_STEP_SIZE) {
2333 dev_err(nandc->dev, "invalid ecc size\n");
2337 wide_bus = chip->options & NAND_BUSWIDTH_16 ? true : false;
2339 if (ecc->strength >= 8) {
2340 /* 8 bit ECC defaults to BCH ECC on all platforms */
2341 host->bch_enabled = true;
2345 host->ecc_bytes_hw = 14;
2346 host->spare_bytes = 0;
2349 host->ecc_bytes_hw = 13;
2350 host->spare_bytes = 2;
2355 * if the controller supports BCH for 4 bit ECC, the controller
2356 * uses lesser bytes for ECC. If RS is used, the ECC bytes is
2359 if (nandc->props->ecc_modes & ECC_BCH_4BIT) {
2361 host->bch_enabled = true;
2365 host->ecc_bytes_hw = 8;
2366 host->spare_bytes = 2;
2369 host->ecc_bytes_hw = 7;
2370 host->spare_bytes = 4;
2375 host->ecc_bytes_hw = 10;
2378 host->spare_bytes = 0;
2381 host->spare_bytes = 1;
2388 * we consider ecc->bytes as the sum of all the non-data content in a
2389 * step. It gives us a clean representation of the oob area (even if
2390 * all the bytes aren't used for ECC).It is always 16 bytes for 8 bit
2391 * ECC and 12 bytes for 4 bit ECC
2393 ecc->bytes = host->ecc_bytes_hw + host->spare_bytes + host->bbm_size;
2395 ecc->read_page = qcom_nandc_read_page;
2396 ecc->read_page_raw = qcom_nandc_read_page_raw;
2397 ecc->read_oob = qcom_nandc_read_oob;
2398 ecc->write_page = qcom_nandc_write_page;
2399 ecc->write_page_raw = qcom_nandc_write_page_raw;
2400 ecc->write_oob = qcom_nandc_write_oob;
2402 ecc->mode = NAND_ECC_HW;
2404 mtd_set_ooblayout(mtd, &qcom_nand_ooblayout_ops);
2406 cwperpage = mtd->writesize / ecc->size;
2407 nandc->max_cwperpage = max_t(unsigned int, nandc->max_cwperpage,
2411 * DATA_UD_BYTES varies based on whether the read/write command protects
2412 * spare data with ECC too. We protect spare data by default, so we set
2413 * it to main + spare data, which are 512 and 4 bytes respectively.
2415 host->cw_data = 516;
2418 * total bytes in a step, either 528 bytes for 4 bit ECC, or 532 bytes
2421 host->cw_size = host->cw_data + ecc->bytes;
2423 if (ecc->bytes * (mtd->writesize / ecc->size) > mtd->oobsize) {
2424 dev_err(nandc->dev, "ecc data doesn't fit in OOB area\n");
2428 bad_block_byte = mtd->writesize - host->cw_size * (cwperpage - 1) + 1;
2430 host->cfg0 = (cwperpage - 1) << CW_PER_PAGE
2431 | host->cw_data << UD_SIZE_BYTES
2432 | 0 << DISABLE_STATUS_AFTER_WRITE
2433 | 5 << NUM_ADDR_CYCLES
2434 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_RS
2435 | 0 << STATUS_BFR_READ
2436 | 1 << SET_RD_MODE_AFTER_STATUS
2437 | host->spare_bytes << SPARE_SIZE_BYTES;
2439 host->cfg1 = 7 << NAND_RECOVERY_CYCLES
2440 | 0 << CS_ACTIVE_BSY
2441 | bad_block_byte << BAD_BLOCK_BYTE_NUM
2442 | 0 << BAD_BLOCK_IN_SPARE_AREA
2443 | 2 << WR_RD_BSY_GAP
2444 | wide_bus << WIDE_FLASH
2445 | host->bch_enabled << ENABLE_BCH_ECC;
2447 host->cfg0_raw = (cwperpage - 1) << CW_PER_PAGE
2448 | host->cw_size << UD_SIZE_BYTES
2449 | 5 << NUM_ADDR_CYCLES
2450 | 0 << SPARE_SIZE_BYTES;
2452 host->cfg1_raw = 7 << NAND_RECOVERY_CYCLES
2453 | 0 << CS_ACTIVE_BSY
2454 | 17 << BAD_BLOCK_BYTE_NUM
2455 | 1 << BAD_BLOCK_IN_SPARE_AREA
2456 | 2 << WR_RD_BSY_GAP
2457 | wide_bus << WIDE_FLASH
2458 | 1 << DEV0_CFG1_ECC_DISABLE;
2460 host->ecc_bch_cfg = !host->bch_enabled << ECC_CFG_ECC_DISABLE
2462 | host->cw_data << ECC_NUM_DATA_BYTES
2463 | 1 << ECC_FORCE_CLK_OPEN
2464 | ecc_mode << ECC_MODE
2465 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_BCH;
2467 host->ecc_buf_cfg = 0x203 << NUM_STEPS;
2469 host->clrflashstatus = FS_READY_BSY_N;
2470 host->clrreadstatus = 0xc0;
2471 nandc->regs->erased_cw_detect_cfg_clr =
2472 cpu_to_le32(CLR_ERASED_PAGE_DET);
2473 nandc->regs->erased_cw_detect_cfg_set =
2474 cpu_to_le32(SET_ERASED_PAGE_DET);
2477 "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",
2478 host->cfg0, host->cfg1, host->ecc_buf_cfg, host->ecc_bch_cfg,
2479 host->cw_size, host->cw_data, ecc->strength, ecc->bytes,
2485 static int qcom_nandc_alloc(struct qcom_nand_controller *nandc)
2489 ret = dma_set_coherent_mask(nandc->dev, DMA_BIT_MASK(32));
2491 dev_err(nandc->dev, "failed to set DMA mask\n");
2496 * we use the internal buffer for reading ONFI params, reading small
2497 * data like ID and status, and preforming read-copy-write operations
2498 * when writing to a codeword partially. 532 is the maximum possible
2499 * size of a codeword for our nand controller
2501 nandc->buf_size = 532;
2503 nandc->data_buffer = devm_kzalloc(nandc->dev, nandc->buf_size,
2505 if (!nandc->data_buffer)
2508 nandc->regs = devm_kzalloc(nandc->dev, sizeof(*nandc->regs),
2513 nandc->reg_read_buf = devm_kzalloc(nandc->dev,
2514 MAX_REG_RD * sizeof(*nandc->reg_read_buf),
2516 if (!nandc->reg_read_buf)
2519 if (nandc->props->is_bam) {
2520 nandc->reg_read_dma =
2521 dma_map_single(nandc->dev, nandc->reg_read_buf,
2523 sizeof(*nandc->reg_read_buf),
2525 if (dma_mapping_error(nandc->dev, nandc->reg_read_dma)) {
2526 dev_err(nandc->dev, "failed to DMA MAP reg buffer\n");
2530 nandc->tx_chan = dma_request_slave_channel(nandc->dev, "tx");
2531 if (!nandc->tx_chan) {
2532 dev_err(nandc->dev, "failed to request tx channel\n");
2536 nandc->rx_chan = dma_request_slave_channel(nandc->dev, "rx");
2537 if (!nandc->rx_chan) {
2538 dev_err(nandc->dev, "failed to request rx channel\n");
2542 nandc->cmd_chan = dma_request_slave_channel(nandc->dev, "cmd");
2543 if (!nandc->cmd_chan) {
2544 dev_err(nandc->dev, "failed to request cmd channel\n");
2549 * Initially allocate BAM transaction to read ONFI param page.
2550 * After detecting all the devices, this BAM transaction will
2551 * be freed and the next BAM tranasction will be allocated with
2552 * maximum codeword size
2554 nandc->max_cwperpage = 1;
2555 nandc->bam_txn = alloc_bam_transaction(nandc);
2556 if (!nandc->bam_txn) {
2558 "failed to allocate bam transaction\n");
2562 nandc->chan = dma_request_slave_channel(nandc->dev, "rxtx");
2565 "failed to request slave channel\n");
2570 INIT_LIST_HEAD(&nandc->desc_list);
2571 INIT_LIST_HEAD(&nandc->host_list);
2573 nand_hw_control_init(&nandc->controller);
2578 static void qcom_nandc_unalloc(struct qcom_nand_controller *nandc)
2580 if (nandc->props->is_bam) {
2581 if (!dma_mapping_error(nandc->dev, nandc->reg_read_dma))
2582 dma_unmap_single(nandc->dev, nandc->reg_read_dma,
2584 sizeof(*nandc->reg_read_buf),
2588 dma_release_channel(nandc->tx_chan);
2591 dma_release_channel(nandc->rx_chan);
2593 if (nandc->cmd_chan)
2594 dma_release_channel(nandc->cmd_chan);
2597 dma_release_channel(nandc->chan);
2601 /* one time setup of a few nand controller registers */
2602 static int qcom_nandc_setup(struct qcom_nand_controller *nandc)
2607 nandc_write(nandc, SFLASHC_BURST_CFG, 0);
2608 nandc_write(nandc, dev_cmd_reg_addr(nandc, NAND_DEV_CMD_VLD),
2609 NAND_DEV_CMD_VLD_VAL);
2611 /* enable ADM or BAM DMA */
2612 if (nandc->props->is_bam) {
2613 nand_ctrl = nandc_read(nandc, NAND_CTRL);
2614 nandc_write(nandc, NAND_CTRL, nand_ctrl | BAM_MODE_EN);
2616 nandc_write(nandc, NAND_FLASH_CHIP_SELECT, DM_EN);
2619 /* save the original values of these registers */
2620 nandc->cmd1 = nandc_read(nandc, dev_cmd_reg_addr(nandc, NAND_DEV_CMD1));
2621 nandc->vld = NAND_DEV_CMD_VLD_VAL;
2626 static int qcom_nand_host_init(struct qcom_nand_controller *nandc,
2627 struct qcom_nand_host *host,
2628 struct device_node *dn)
2630 struct nand_chip *chip = &host->chip;
2631 struct mtd_info *mtd = nand_to_mtd(chip);
2632 struct device *dev = nandc->dev;
2635 ret = of_property_read_u32(dn, "reg", &host->cs);
2637 dev_err(dev, "can't get chip-select\n");
2641 nand_set_flash_node(chip, dn);
2642 mtd->name = devm_kasprintf(dev, GFP_KERNEL, "qcom_nand.%d", host->cs);
2646 mtd->owner = THIS_MODULE;
2647 mtd->dev.parent = dev;
2649 chip->cmdfunc = qcom_nandc_command;
2650 chip->select_chip = qcom_nandc_select_chip;
2651 chip->read_byte = qcom_nandc_read_byte;
2652 chip->read_buf = qcom_nandc_read_buf;
2653 chip->write_buf = qcom_nandc_write_buf;
2654 chip->onfi_set_features = nand_onfi_get_set_features_notsupp;
2655 chip->onfi_get_features = nand_onfi_get_set_features_notsupp;
2658 * the bad block marker is readable only when we read the last codeword
2659 * of a page with ECC disabled. currently, the nand_base and nand_bbt
2660 * helpers don't allow us to read BB from a nand chip with ECC
2661 * disabled (MTD_OPS_PLACE_OOB is set by default). use the block_bad
2662 * and block_markbad helpers until we permanently switch to using
2663 * MTD_OPS_RAW for all drivers (with the help of badblockbits)
2665 chip->block_bad = qcom_nandc_block_bad;
2666 chip->block_markbad = qcom_nandc_block_markbad;
2668 chip->controller = &nandc->controller;
2669 chip->options |= NAND_NO_SUBPAGE_WRITE | NAND_USE_BOUNCE_BUFFER |
2672 /* set up initial status value */
2673 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
2675 ret = nand_scan_ident(mtd, 1, NULL);
2679 ret = qcom_nand_host_setup(host);
2684 static int qcom_nand_mtd_register(struct qcom_nand_controller *nandc,
2685 struct qcom_nand_host *host,
2686 struct device_node *dn)
2688 struct nand_chip *chip = &host->chip;
2689 struct mtd_info *mtd = nand_to_mtd(chip);
2692 ret = nand_scan_tail(mtd);
2696 ret = mtd_device_register(mtd, NULL, 0);
2698 nand_cleanup(mtd_to_nand(mtd));
2703 static int qcom_probe_nand_devices(struct qcom_nand_controller *nandc)
2705 struct device *dev = nandc->dev;
2706 struct device_node *dn = dev->of_node, *child;
2707 struct qcom_nand_host *host, *tmp;
2710 for_each_available_child_of_node(dn, child) {
2711 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
2717 ret = qcom_nand_host_init(nandc, host, child);
2719 devm_kfree(dev, host);
2723 list_add_tail(&host->node, &nandc->host_list);
2726 if (list_empty(&nandc->host_list))
2729 if (nandc->props->is_bam) {
2730 free_bam_transaction(nandc);
2731 nandc->bam_txn = alloc_bam_transaction(nandc);
2732 if (!nandc->bam_txn) {
2734 "failed to allocate bam transaction\n");
2739 list_for_each_entry_safe(host, tmp, &nandc->host_list, node) {
2740 ret = qcom_nand_mtd_register(nandc, host, child);
2742 list_del(&host->node);
2743 devm_kfree(dev, host);
2747 if (list_empty(&nandc->host_list))
2753 /* parse custom DT properties here */
2754 static int qcom_nandc_parse_dt(struct platform_device *pdev)
2756 struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
2757 struct device_node *np = nandc->dev->of_node;
2760 if (!nandc->props->is_bam) {
2761 ret = of_property_read_u32(np, "qcom,cmd-crci",
2764 dev_err(nandc->dev, "command CRCI unspecified\n");
2768 ret = of_property_read_u32(np, "qcom,data-crci",
2771 dev_err(nandc->dev, "data CRCI unspecified\n");
2779 static int qcom_nandc_probe(struct platform_device *pdev)
2781 struct qcom_nand_controller *nandc;
2782 const void *dev_data;
2783 struct device *dev = &pdev->dev;
2784 struct resource *res;
2787 nandc = devm_kzalloc(&pdev->dev, sizeof(*nandc), GFP_KERNEL);
2791 platform_set_drvdata(pdev, nandc);
2794 dev_data = of_device_get_match_data(dev);
2796 dev_err(&pdev->dev, "failed to get device data\n");
2800 nandc->props = dev_data;
2802 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2803 nandc->base = devm_ioremap_resource(dev, res);
2804 if (IS_ERR(nandc->base))
2805 return PTR_ERR(nandc->base);
2807 nandc->base_phys = res->start;
2808 nandc->base_dma = phys_to_dma(dev, (phys_addr_t)res->start);
2810 nandc->core_clk = devm_clk_get(dev, "core");
2811 if (IS_ERR(nandc->core_clk))
2812 return PTR_ERR(nandc->core_clk);
2814 nandc->aon_clk = devm_clk_get(dev, "aon");
2815 if (IS_ERR(nandc->aon_clk))
2816 return PTR_ERR(nandc->aon_clk);
2818 ret = qcom_nandc_parse_dt(pdev);
2822 ret = qcom_nandc_alloc(nandc);
2826 ret = clk_prepare_enable(nandc->core_clk);
2830 ret = clk_prepare_enable(nandc->aon_clk);
2834 ret = qcom_nandc_setup(nandc);
2838 ret = qcom_probe_nand_devices(nandc);
2845 clk_disable_unprepare(nandc->aon_clk);
2847 clk_disable_unprepare(nandc->core_clk);
2849 qcom_nandc_unalloc(nandc);
2854 static int qcom_nandc_remove(struct platform_device *pdev)
2856 struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
2857 struct qcom_nand_host *host;
2859 list_for_each_entry(host, &nandc->host_list, node)
2860 nand_release(nand_to_mtd(&host->chip));
2862 qcom_nandc_unalloc(nandc);
2864 clk_disable_unprepare(nandc->aon_clk);
2865 clk_disable_unprepare(nandc->core_clk);
2870 static const struct qcom_nandc_props ipq806x_nandc_props = {
2871 .ecc_modes = (ECC_RS_4BIT | ECC_BCH_8BIT),
2873 .dev_cmd_reg_start = 0x0,
2876 static const struct qcom_nandc_props ipq4019_nandc_props = {
2877 .ecc_modes = (ECC_BCH_4BIT | ECC_BCH_8BIT),
2879 .dev_cmd_reg_start = 0x0,
2882 static const struct qcom_nandc_props ipq8074_nandc_props = {
2883 .ecc_modes = (ECC_BCH_4BIT | ECC_BCH_8BIT),
2885 .dev_cmd_reg_start = 0x7000,
2889 * data will hold a struct pointer containing more differences once we support
2890 * more controller variants
2892 static const struct of_device_id qcom_nandc_of_match[] = {
2894 .compatible = "qcom,ipq806x-nand",
2895 .data = &ipq806x_nandc_props,
2898 .compatible = "qcom,ipq4019-nand",
2899 .data = &ipq4019_nandc_props,
2902 .compatible = "qcom,ipq8074-nand",
2903 .data = &ipq8074_nandc_props,
2907 MODULE_DEVICE_TABLE(of, qcom_nandc_of_match);
2909 static struct platform_driver qcom_nandc_driver = {
2911 .name = "qcom-nandc",
2912 .of_match_table = qcom_nandc_of_match,
2914 .probe = qcom_nandc_probe,
2915 .remove = qcom_nandc_remove,
2917 module_platform_driver(qcom_nandc_driver);
2919 MODULE_AUTHOR("Archit Taneja <architt@codeaurora.org>");
2920 MODULE_DESCRIPTION("Qualcomm NAND Controller driver");
2921 MODULE_LICENSE("GPL v2");