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>
27 /* NANDc reg offsets */
28 #define NAND_FLASH_CMD 0x00
29 #define NAND_ADDR0 0x04
30 #define NAND_ADDR1 0x08
31 #define NAND_FLASH_CHIP_SELECT 0x0c
32 #define NAND_EXEC_CMD 0x10
33 #define NAND_FLASH_STATUS 0x14
34 #define NAND_BUFFER_STATUS 0x18
35 #define NAND_DEV0_CFG0 0x20
36 #define NAND_DEV0_CFG1 0x24
37 #define NAND_DEV0_ECC_CFG 0x28
38 #define NAND_DEV1_ECC_CFG 0x2c
39 #define NAND_DEV1_CFG0 0x30
40 #define NAND_DEV1_CFG1 0x34
41 #define NAND_READ_ID 0x40
42 #define NAND_READ_STATUS 0x44
43 #define NAND_DEV_CMD0 0xa0
44 #define NAND_DEV_CMD1 0xa4
45 #define NAND_DEV_CMD2 0xa8
46 #define NAND_DEV_CMD_VLD 0xac
47 #define SFLASHC_BURST_CFG 0xe0
48 #define NAND_ERASED_CW_DETECT_CFG 0xe8
49 #define NAND_ERASED_CW_DETECT_STATUS 0xec
50 #define NAND_EBI2_ECC_BUF_CFG 0xf0
51 #define FLASH_BUF_ACC 0x100
53 #define NAND_CTRL 0xf00
54 #define NAND_VERSION 0xf08
55 #define NAND_READ_LOCATION_0 0xf20
56 #define NAND_READ_LOCATION_1 0xf24
57 #define NAND_READ_LOCATION_2 0xf28
58 #define NAND_READ_LOCATION_3 0xf2c
60 /* dummy register offsets, used by write_reg_dma */
61 #define NAND_DEV_CMD1_RESTORE 0xdead
62 #define NAND_DEV_CMD_VLD_RESTORE 0xbeef
64 /* NAND_FLASH_CMD bits */
65 #define PAGE_ACC BIT(4)
66 #define LAST_PAGE BIT(5)
68 /* NAND_FLASH_CHIP_SELECT bits */
69 #define NAND_DEV_SEL 0
72 /* NAND_FLASH_STATUS bits */
73 #define FS_OP_ERR BIT(4)
74 #define FS_READY_BSY_N BIT(5)
75 #define FS_MPU_ERR BIT(8)
76 #define FS_DEVICE_STS_ERR BIT(16)
77 #define FS_DEVICE_WP BIT(23)
79 /* NAND_BUFFER_STATUS bits */
80 #define BS_UNCORRECTABLE_BIT BIT(8)
81 #define BS_CORRECTABLE_ERR_MSK 0x1f
83 /* NAND_DEVn_CFG0 bits */
84 #define DISABLE_STATUS_AFTER_WRITE 4
86 #define UD_SIZE_BYTES 9
87 #define ECC_PARITY_SIZE_BYTES_RS 19
88 #define SPARE_SIZE_BYTES 23
89 #define NUM_ADDR_CYCLES 27
90 #define STATUS_BFR_READ 30
91 #define SET_RD_MODE_AFTER_STATUS 31
93 /* NAND_DEVn_CFG0 bits */
94 #define DEV0_CFG1_ECC_DISABLE 0
96 #define NAND_RECOVERY_CYCLES 2
97 #define CS_ACTIVE_BSY 5
98 #define BAD_BLOCK_BYTE_NUM 6
99 #define BAD_BLOCK_IN_SPARE_AREA 16
100 #define WR_RD_BSY_GAP 17
101 #define ENABLE_BCH_ECC 27
103 /* NAND_DEV0_ECC_CFG bits */
104 #define ECC_CFG_ECC_DISABLE 0
105 #define ECC_SW_RESET 1
107 #define ECC_PARITY_SIZE_BYTES_BCH 8
108 #define ECC_NUM_DATA_BYTES 16
109 #define ECC_FORCE_CLK_OPEN 30
111 /* NAND_DEV_CMD1 bits */
114 /* NAND_DEV_CMD_VLD bits */
115 #define READ_START_VLD BIT(0)
116 #define READ_STOP_VLD BIT(1)
117 #define WRITE_START_VLD BIT(2)
118 #define ERASE_START_VLD BIT(3)
119 #define SEQ_READ_START_VLD BIT(4)
121 /* NAND_EBI2_ECC_BUF_CFG bits */
124 /* NAND_ERASED_CW_DETECT_CFG bits */
125 #define ERASED_CW_ECC_MASK 1
126 #define AUTO_DETECT_RES 0
127 #define MASK_ECC (1 << ERASED_CW_ECC_MASK)
128 #define RESET_ERASED_DET (1 << AUTO_DETECT_RES)
129 #define ACTIVE_ERASED_DET (0 << AUTO_DETECT_RES)
130 #define CLR_ERASED_PAGE_DET (RESET_ERASED_DET | MASK_ECC)
131 #define SET_ERASED_PAGE_DET (ACTIVE_ERASED_DET | MASK_ECC)
133 /* NAND_ERASED_CW_DETECT_STATUS bits */
134 #define PAGE_ALL_ERASED BIT(7)
135 #define CODEWORD_ALL_ERASED BIT(6)
136 #define PAGE_ERASED BIT(5)
137 #define CODEWORD_ERASED BIT(4)
138 #define ERASED_PAGE (PAGE_ALL_ERASED | PAGE_ERASED)
139 #define ERASED_CW (CODEWORD_ALL_ERASED | CODEWORD_ERASED)
141 /* NAND_READ_LOCATION_n bits */
142 #define READ_LOCATION_OFFSET 0
143 #define READ_LOCATION_SIZE 16
144 #define READ_LOCATION_LAST 31
147 #define NAND_VERSION_MAJOR_MASK 0xf0000000
148 #define NAND_VERSION_MAJOR_SHIFT 28
149 #define NAND_VERSION_MINOR_MASK 0x0fff0000
150 #define NAND_VERSION_MINOR_SHIFT 16
153 #define OP_PAGE_READ 0x2
154 #define OP_PAGE_READ_WITH_ECC 0x3
155 #define OP_PAGE_READ_WITH_ECC_SPARE 0x4
156 #define OP_PROGRAM_PAGE 0x6
157 #define OP_PAGE_PROGRAM_WITH_ECC 0x7
158 #define OP_PROGRAM_PAGE_SPARE 0x9
159 #define OP_BLOCK_ERASE 0xa
160 #define OP_FETCH_ID 0xb
161 #define OP_RESET_DEVICE 0xd
163 /* Default Value for NAND_DEV_CMD_VLD */
164 #define NAND_DEV_CMD_VLD_VAL (READ_START_VLD | WRITE_START_VLD | \
165 ERASE_START_VLD | SEQ_READ_START_VLD)
168 #define BAM_MODE_EN BIT(0)
171 * the NAND controller performs reads/writes with ECC in 516 byte chunks.
172 * the driver calls the chunks 'step' or 'codeword' interchangeably
174 #define NANDC_STEP_SIZE 512
177 * the largest page size we support is 8K, this will have 16 steps/codewords
180 #define MAX_NUM_STEPS (SZ_8K / NANDC_STEP_SIZE)
182 /* we read at most 3 registers per codeword scan */
183 #define MAX_REG_RD (3 * MAX_NUM_STEPS)
185 /* ECC modes supported by the controller */
186 #define ECC_NONE BIT(0)
187 #define ECC_RS_4BIT BIT(1)
188 #define ECC_BCH_4BIT BIT(2)
189 #define ECC_BCH_8BIT BIT(3)
191 #define nandc_set_read_loc(nandc, reg, offset, size, is_last) \
192 nandc_set_reg(nandc, NAND_READ_LOCATION_##reg, \
193 ((offset) << READ_LOCATION_OFFSET) | \
194 ((size) << READ_LOCATION_SIZE) | \
195 ((is_last) << READ_LOCATION_LAST))
198 * Returns the actual register address for all NAND_DEV_ registers
199 * (i.e. NAND_DEV_CMD0, NAND_DEV_CMD1, NAND_DEV_CMD2 and NAND_DEV_CMD_VLD)
201 #define dev_cmd_reg_addr(nandc, reg) ((nandc)->props->dev_cmd_reg_start + (reg))
203 /* Returns the NAND register physical address */
204 #define nandc_reg_phys(chip, offset) ((chip)->base_phys + (offset))
206 /* Returns the dma address for reg read buffer */
207 #define reg_buf_dma_addr(chip, vaddr) \
208 ((chip)->reg_read_dma + \
209 ((uint8_t *)(vaddr) - (uint8_t *)(chip)->reg_read_buf))
211 #define QPIC_PER_CW_CMD_ELEMENTS 32
212 #define QPIC_PER_CW_CMD_SGL 32
213 #define QPIC_PER_CW_DATA_SGL 8
215 #define QPIC_NAND_COMPLETION_TIMEOUT msecs_to_jiffies(2000)
218 * Flags used in DMA descriptor preparation helper functions
219 * (i.e. read_reg_dma/write_reg_dma/read_data_dma/write_data_dma)
221 /* Don't set the EOT in current tx BAM sgl */
222 #define NAND_BAM_NO_EOT BIT(0)
223 /* Set the NWD flag in current BAM sgl */
224 #define NAND_BAM_NWD BIT(1)
225 /* Finish writing in the current BAM sgl and start writing in another BAM sgl */
226 #define NAND_BAM_NEXT_SGL BIT(2)
228 * Erased codeword status is being used two times in single transfer so this
229 * flag will determine the current value of erased codeword status register
231 #define NAND_ERASED_CW_SET BIT(4)
234 * This data type corresponds to the BAM transaction which will be used for all
236 * @bam_ce - the array of BAM command elements
237 * @cmd_sgl - sgl for NAND BAM command pipe
238 * @data_sgl - sgl for NAND BAM consumer/producer pipe
239 * @bam_ce_pos - the index in bam_ce which is available for next sgl
240 * @bam_ce_start - the index in bam_ce which marks the start position ce
241 * for current sgl. It will be used for size calculation
243 * @cmd_sgl_pos - current index in command sgl.
244 * @cmd_sgl_start - start index in command sgl.
245 * @tx_sgl_pos - current index in data sgl for tx.
246 * @tx_sgl_start - start index in data sgl for tx.
247 * @rx_sgl_pos - current index in data sgl for rx.
248 * @rx_sgl_start - start index in data sgl for rx.
249 * @wait_second_completion - wait for second DMA desc completion before making
250 * the NAND transfer completion.
251 * @txn_done - completion for NAND transfer.
252 * @last_data_desc - last DMA desc in data channel (tx/rx).
253 * @last_cmd_desc - last DMA desc in command channel.
255 struct bam_transaction
{
256 struct bam_cmd_element
*bam_ce
;
257 struct scatterlist
*cmd_sgl
;
258 struct scatterlist
*data_sgl
;
267 bool wait_second_completion
;
268 struct completion txn_done
;
269 struct dma_async_tx_descriptor
*last_data_desc
;
270 struct dma_async_tx_descriptor
*last_cmd_desc
;
274 * This data type corresponds to the nand dma descriptor
275 * @list - list for desc_info
276 * @dir - DMA transfer direction
277 * @adm_sgl - sgl which will be used for single sgl dma descriptor. Only used by
279 * @bam_sgl - sgl which will be used for dma descriptor. Only used by BAM
280 * @sgl_cnt - number of SGL in bam_sgl. Only used by BAM
281 * @dma_desc - low level DMA engine descriptor
284 struct list_head node
;
286 enum dma_data_direction dir
;
288 struct scatterlist adm_sgl
;
290 struct scatterlist
*bam_sgl
;
294 struct dma_async_tx_descriptor
*dma_desc
;
298 * holds the current register values that we want to write. acts as a contiguous
299 * chunk of memory which we use to write the controller registers through DMA.
312 __le32 clrflashstatus
;
313 __le32 clrreadstatus
;
322 __le32 read_location0
;
323 __le32 read_location1
;
324 __le32 read_location2
;
325 __le32 read_location3
;
327 __le32 erased_cw_detect_cfg_clr
;
328 __le32 erased_cw_detect_cfg_set
;
332 * NAND controller data struct
334 * @controller: base controller structure
335 * @host_list: list containing all the chips attached to the
337 * @dev: parent device
339 * @base_phys: physical base address of controller registers
340 * @base_dma: dma base address of controller registers
341 * @core_clk: controller clock
342 * @aon_clk: another controller clock
345 * @cmd_crci: ADM DMA CRCI for command flow control
346 * @data_crci: ADM DMA CRCI for data flow control
347 * @desc_list: DMA descriptor list (list of desc_infos)
349 * @data_buffer: our local DMA buffer for page read/writes,
350 * used when we can't use the buffer provided
351 * by upper layers directly
352 * @buf_size/count/start: markers for chip->read_buf/write_buf functions
353 * @reg_read_buf: local buffer for reading back registers via DMA
354 * @reg_read_dma: contains dma address for register read buffer
355 * @reg_read_pos: marker for data read in reg_read_buf
357 * @regs: a contiguous chunk of memory for DMA register
358 * writes. contains the register values to be
359 * written to controller
360 * @cmd1/vld: some fixed controller register values
361 * @props: properties of current NAND controller,
362 * initialized via DT match data
363 * @max_cwperpage: maximum QPIC codewords required. calculated
364 * from all connected NAND devices pagesize
366 struct qcom_nand_controller
{
367 struct nand_controller controller
;
368 struct list_head host_list
;
373 phys_addr_t base_phys
;
376 struct clk
*core_clk
;
380 /* will be used only by QPIC for BAM DMA */
382 struct dma_chan
*tx_chan
;
383 struct dma_chan
*rx_chan
;
384 struct dma_chan
*cmd_chan
;
387 /* will be used only by EBI2 for ADM DMA */
389 struct dma_chan
*chan
;
390 unsigned int cmd_crci
;
391 unsigned int data_crci
;
395 struct list_head desc_list
;
396 struct bam_transaction
*bam_txn
;
402 unsigned int max_cwperpage
;
404 __le32
*reg_read_buf
;
405 dma_addr_t reg_read_dma
;
408 struct nandc_regs
*regs
;
411 const struct qcom_nandc_props
*props
;
415 * NAND chip structure
417 * @chip: base NAND chip structure
418 * @node: list node to add itself to host_list in
419 * qcom_nand_controller
421 * @cs: chip select value for this chip
422 * @cw_size: the number of bytes in a single step/codeword
423 * of a page, consisting of all data, ecc, spare
425 * @cw_data: the number of bytes within a codeword protected
427 * @use_ecc: request the controller to use ECC for the
428 * upcoming read/write
429 * @bch_enabled: flag to tell whether BCH ECC mode is used
430 * @ecc_bytes_hw: ECC bytes used by controller hardware for this
432 * @status: value to be returned if NAND_CMD_STATUS command
434 * @last_command: keeps track of last command on this chip. used
435 * for reading correct status
437 * @cfg0, cfg1, cfg0_raw..: NANDc register configurations needed for
438 * ecc/non-ecc mode for the current nand flash
441 struct qcom_nand_host
{
442 struct nand_chip chip
;
443 struct list_head node
;
457 u32 cfg0_raw
, cfg1_raw
;
465 * This data type corresponds to the NAND controller properties which varies
466 * among different NAND controllers.
467 * @ecc_modes - ecc mode for NAND
468 * @is_bam - whether NAND controller is using BAM
469 * @dev_cmd_reg_start - NAND_DEV_CMD_* registers starting offset
471 struct qcom_nandc_props
{
474 u32 dev_cmd_reg_start
;
477 /* Frees the BAM transaction memory */
478 static void free_bam_transaction(struct qcom_nand_controller
*nandc
)
480 struct bam_transaction
*bam_txn
= nandc
->bam_txn
;
482 devm_kfree(nandc
->dev
, bam_txn
);
485 /* Allocates and Initializes the BAM transaction */
486 static struct bam_transaction
*
487 alloc_bam_transaction(struct qcom_nand_controller
*nandc
)
489 struct bam_transaction
*bam_txn
;
491 unsigned int num_cw
= nandc
->max_cwperpage
;
495 sizeof(*bam_txn
) + num_cw
*
496 ((sizeof(*bam_txn
->bam_ce
) * QPIC_PER_CW_CMD_ELEMENTS
) +
497 (sizeof(*bam_txn
->cmd_sgl
) * QPIC_PER_CW_CMD_SGL
) +
498 (sizeof(*bam_txn
->data_sgl
) * QPIC_PER_CW_DATA_SGL
));
500 bam_txn_buf
= devm_kzalloc(nandc
->dev
, bam_txn_size
, GFP_KERNEL
);
504 bam_txn
= bam_txn_buf
;
505 bam_txn_buf
+= sizeof(*bam_txn
);
507 bam_txn
->bam_ce
= bam_txn_buf
;
509 sizeof(*bam_txn
->bam_ce
) * QPIC_PER_CW_CMD_ELEMENTS
* num_cw
;
511 bam_txn
->cmd_sgl
= bam_txn_buf
;
513 sizeof(*bam_txn
->cmd_sgl
) * QPIC_PER_CW_CMD_SGL
* num_cw
;
515 bam_txn
->data_sgl
= bam_txn_buf
;
517 init_completion(&bam_txn
->txn_done
);
522 /* Clears the BAM transaction indexes */
523 static void clear_bam_transaction(struct qcom_nand_controller
*nandc
)
525 struct bam_transaction
*bam_txn
= nandc
->bam_txn
;
527 if (!nandc
->props
->is_bam
)
530 bam_txn
->bam_ce_pos
= 0;
531 bam_txn
->bam_ce_start
= 0;
532 bam_txn
->cmd_sgl_pos
= 0;
533 bam_txn
->cmd_sgl_start
= 0;
534 bam_txn
->tx_sgl_pos
= 0;
535 bam_txn
->tx_sgl_start
= 0;
536 bam_txn
->rx_sgl_pos
= 0;
537 bam_txn
->rx_sgl_start
= 0;
538 bam_txn
->last_data_desc
= NULL
;
539 bam_txn
->wait_second_completion
= false;
541 sg_init_table(bam_txn
->cmd_sgl
, nandc
->max_cwperpage
*
542 QPIC_PER_CW_CMD_SGL
);
543 sg_init_table(bam_txn
->data_sgl
, nandc
->max_cwperpage
*
544 QPIC_PER_CW_DATA_SGL
);
546 reinit_completion(&bam_txn
->txn_done
);
549 /* Callback for DMA descriptor completion */
550 static void qpic_bam_dma_done(void *data
)
552 struct bam_transaction
*bam_txn
= data
;
555 * In case of data transfer with NAND, 2 callbacks will be generated.
556 * One for command channel and another one for data channel.
557 * If current transaction has data descriptors
558 * (i.e. wait_second_completion is true), then set this to false
559 * and wait for second DMA descriptor completion.
561 if (bam_txn
->wait_second_completion
)
562 bam_txn
->wait_second_completion
= false;
564 complete(&bam_txn
->txn_done
);
567 static inline struct qcom_nand_host
*to_qcom_nand_host(struct nand_chip
*chip
)
569 return container_of(chip
, struct qcom_nand_host
, chip
);
572 static inline struct qcom_nand_controller
*
573 get_qcom_nand_controller(struct nand_chip
*chip
)
575 return container_of(chip
->controller
, struct qcom_nand_controller
,
579 static inline u32
nandc_read(struct qcom_nand_controller
*nandc
, int offset
)
581 return ioread32(nandc
->base
+ offset
);
584 static inline void nandc_write(struct qcom_nand_controller
*nandc
, int offset
,
587 iowrite32(val
, nandc
->base
+ offset
);
590 static inline void nandc_read_buffer_sync(struct qcom_nand_controller
*nandc
,
593 if (!nandc
->props
->is_bam
)
597 dma_sync_single_for_cpu(nandc
->dev
, nandc
->reg_read_dma
,
599 sizeof(*nandc
->reg_read_buf
),
602 dma_sync_single_for_device(nandc
->dev
, nandc
->reg_read_dma
,
604 sizeof(*nandc
->reg_read_buf
),
608 static __le32
*offset_to_nandc_reg(struct nandc_regs
*regs
, int offset
)
617 case NAND_FLASH_CHIP_SELECT
:
618 return ®s
->chip_sel
;
621 case NAND_FLASH_STATUS
:
622 return ®s
->clrflashstatus
;
627 case NAND_DEV0_ECC_CFG
:
628 return ®s
->ecc_bch_cfg
;
629 case NAND_READ_STATUS
:
630 return ®s
->clrreadstatus
;
633 case NAND_DEV_CMD1_RESTORE
:
634 return ®s
->orig_cmd1
;
635 case NAND_DEV_CMD_VLD
:
637 case NAND_DEV_CMD_VLD_RESTORE
:
638 return ®s
->orig_vld
;
639 case NAND_EBI2_ECC_BUF_CFG
:
640 return ®s
->ecc_buf_cfg
;
641 case NAND_READ_LOCATION_0
:
642 return ®s
->read_location0
;
643 case NAND_READ_LOCATION_1
:
644 return ®s
->read_location1
;
645 case NAND_READ_LOCATION_2
:
646 return ®s
->read_location2
;
647 case NAND_READ_LOCATION_3
:
648 return ®s
->read_location3
;
654 static void nandc_set_reg(struct qcom_nand_controller
*nandc
, int offset
,
657 struct nandc_regs
*regs
= nandc
->regs
;
660 reg
= offset_to_nandc_reg(regs
, offset
);
663 *reg
= cpu_to_le32(val
);
666 /* helper to configure address register values */
667 static void set_address(struct qcom_nand_host
*host
, u16 column
, int page
)
669 struct nand_chip
*chip
= &host
->chip
;
670 struct qcom_nand_controller
*nandc
= get_qcom_nand_controller(chip
);
672 if (chip
->options
& NAND_BUSWIDTH_16
)
675 nandc_set_reg(nandc
, NAND_ADDR0
, page
<< 16 | column
);
676 nandc_set_reg(nandc
, NAND_ADDR1
, page
>> 16 & 0xff);
680 * update_rw_regs: set up read/write register values, these will be
681 * written to the NAND controller registers via DMA
683 * @num_cw: number of steps for the read/write operation
684 * @read: read or write operation
686 static void update_rw_regs(struct qcom_nand_host
*host
, int num_cw
, bool read
)
688 struct nand_chip
*chip
= &host
->chip
;
689 struct qcom_nand_controller
*nandc
= get_qcom_nand_controller(chip
);
690 u32 cmd
, cfg0
, cfg1
, ecc_bch_cfg
;
694 cmd
= OP_PAGE_READ_WITH_ECC
| PAGE_ACC
| LAST_PAGE
;
696 cmd
= OP_PAGE_READ
| PAGE_ACC
| LAST_PAGE
;
698 cmd
= OP_PROGRAM_PAGE
| PAGE_ACC
| LAST_PAGE
;
702 cfg0
= (host
->cfg0
& ~(7U << CW_PER_PAGE
)) |
703 (num_cw
- 1) << CW_PER_PAGE
;
706 ecc_bch_cfg
= host
->ecc_bch_cfg
;
708 cfg0
= (host
->cfg0_raw
& ~(7U << CW_PER_PAGE
)) |
709 (num_cw
- 1) << CW_PER_PAGE
;
711 cfg1
= host
->cfg1_raw
;
712 ecc_bch_cfg
= 1 << ECC_CFG_ECC_DISABLE
;
715 nandc_set_reg(nandc
, NAND_FLASH_CMD
, cmd
);
716 nandc_set_reg(nandc
, NAND_DEV0_CFG0
, cfg0
);
717 nandc_set_reg(nandc
, NAND_DEV0_CFG1
, cfg1
);
718 nandc_set_reg(nandc
, NAND_DEV0_ECC_CFG
, ecc_bch_cfg
);
719 nandc_set_reg(nandc
, NAND_EBI2_ECC_BUF_CFG
, host
->ecc_buf_cfg
);
720 nandc_set_reg(nandc
, NAND_FLASH_STATUS
, host
->clrflashstatus
);
721 nandc_set_reg(nandc
, NAND_READ_STATUS
, host
->clrreadstatus
);
722 nandc_set_reg(nandc
, NAND_EXEC_CMD
, 1);
725 nandc_set_read_loc(nandc
, 0, 0, host
->use_ecc
?
726 host
->cw_data
: host
->cw_size
, 1);
730 * Maps the scatter gather list for DMA transfer and forms the DMA descriptor
731 * for BAM. This descriptor will be added in the NAND DMA descriptor queue
732 * which will be submitted to DMA engine.
734 static int prepare_bam_async_desc(struct qcom_nand_controller
*nandc
,
735 struct dma_chan
*chan
,
738 struct desc_info
*desc
;
739 struct scatterlist
*sgl
;
740 unsigned int sgl_cnt
;
742 struct bam_transaction
*bam_txn
= nandc
->bam_txn
;
743 enum dma_transfer_direction dir_eng
;
744 struct dma_async_tx_descriptor
*dma_desc
;
746 desc
= kzalloc(sizeof(*desc
), GFP_KERNEL
);
750 if (chan
== nandc
->cmd_chan
) {
751 sgl
= &bam_txn
->cmd_sgl
[bam_txn
->cmd_sgl_start
];
752 sgl_cnt
= bam_txn
->cmd_sgl_pos
- bam_txn
->cmd_sgl_start
;
753 bam_txn
->cmd_sgl_start
= bam_txn
->cmd_sgl_pos
;
754 dir_eng
= DMA_MEM_TO_DEV
;
755 desc
->dir
= DMA_TO_DEVICE
;
756 } else if (chan
== nandc
->tx_chan
) {
757 sgl
= &bam_txn
->data_sgl
[bam_txn
->tx_sgl_start
];
758 sgl_cnt
= bam_txn
->tx_sgl_pos
- bam_txn
->tx_sgl_start
;
759 bam_txn
->tx_sgl_start
= bam_txn
->tx_sgl_pos
;
760 dir_eng
= DMA_MEM_TO_DEV
;
761 desc
->dir
= DMA_TO_DEVICE
;
763 sgl
= &bam_txn
->data_sgl
[bam_txn
->rx_sgl_start
];
764 sgl_cnt
= bam_txn
->rx_sgl_pos
- bam_txn
->rx_sgl_start
;
765 bam_txn
->rx_sgl_start
= bam_txn
->rx_sgl_pos
;
766 dir_eng
= DMA_DEV_TO_MEM
;
767 desc
->dir
= DMA_FROM_DEVICE
;
770 sg_mark_end(sgl
+ sgl_cnt
- 1);
771 ret
= dma_map_sg(nandc
->dev
, sgl
, sgl_cnt
, desc
->dir
);
773 dev_err(nandc
->dev
, "failure in mapping desc\n");
778 desc
->sgl_cnt
= sgl_cnt
;
781 dma_desc
= dmaengine_prep_slave_sg(chan
, sgl
, sgl_cnt
, dir_eng
,
785 dev_err(nandc
->dev
, "failure in prep desc\n");
786 dma_unmap_sg(nandc
->dev
, sgl
, sgl_cnt
, desc
->dir
);
791 desc
->dma_desc
= dma_desc
;
793 /* update last data/command descriptor */
794 if (chan
== nandc
->cmd_chan
)
795 bam_txn
->last_cmd_desc
= dma_desc
;
797 bam_txn
->last_data_desc
= dma_desc
;
799 list_add_tail(&desc
->node
, &nandc
->desc_list
);
805 * Prepares the command descriptor for BAM DMA which will be used for NAND
806 * register reads and writes. The command descriptor requires the command
807 * to be formed in command element type so this function uses the command
808 * element from bam transaction ce array and fills the same with required
809 * data. A single SGL can contain multiple command elements so
810 * NAND_BAM_NEXT_SGL will be used for starting the separate SGL
811 * after the current command element.
813 static int prep_bam_dma_desc_cmd(struct qcom_nand_controller
*nandc
, bool read
,
814 int reg_off
, const void *vaddr
,
815 int size
, unsigned int flags
)
819 struct bam_cmd_element
*bam_ce_buffer
;
820 struct bam_transaction
*bam_txn
= nandc
->bam_txn
;
822 bam_ce_buffer
= &bam_txn
->bam_ce
[bam_txn
->bam_ce_pos
];
824 /* fill the command desc */
825 for (i
= 0; i
< size
; i
++) {
827 bam_prep_ce(&bam_ce_buffer
[i
],
828 nandc_reg_phys(nandc
, reg_off
+ 4 * i
),
830 reg_buf_dma_addr(nandc
,
831 (__le32
*)vaddr
+ i
));
833 bam_prep_ce_le32(&bam_ce_buffer
[i
],
834 nandc_reg_phys(nandc
, reg_off
+ 4 * i
),
836 *((__le32
*)vaddr
+ i
));
839 bam_txn
->bam_ce_pos
+= size
;
841 /* use the separate sgl after this command */
842 if (flags
& NAND_BAM_NEXT_SGL
) {
843 bam_ce_buffer
= &bam_txn
->bam_ce
[bam_txn
->bam_ce_start
];
844 bam_ce_size
= (bam_txn
->bam_ce_pos
-
845 bam_txn
->bam_ce_start
) *
846 sizeof(struct bam_cmd_element
);
847 sg_set_buf(&bam_txn
->cmd_sgl
[bam_txn
->cmd_sgl_pos
],
848 bam_ce_buffer
, bam_ce_size
);
849 bam_txn
->cmd_sgl_pos
++;
850 bam_txn
->bam_ce_start
= bam_txn
->bam_ce_pos
;
852 if (flags
& NAND_BAM_NWD
) {
853 ret
= prepare_bam_async_desc(nandc
, nandc
->cmd_chan
,
865 * Prepares the data descriptor for BAM DMA which will be used for NAND
866 * data reads and writes.
868 static int prep_bam_dma_desc_data(struct qcom_nand_controller
*nandc
, bool read
,
870 int size
, unsigned int flags
)
873 struct bam_transaction
*bam_txn
= nandc
->bam_txn
;
876 sg_set_buf(&bam_txn
->data_sgl
[bam_txn
->rx_sgl_pos
],
878 bam_txn
->rx_sgl_pos
++;
880 sg_set_buf(&bam_txn
->data_sgl
[bam_txn
->tx_sgl_pos
],
882 bam_txn
->tx_sgl_pos
++;
885 * BAM will only set EOT for DMA_PREP_INTERRUPT so if this flag
886 * is not set, form the DMA descriptor
888 if (!(flags
& NAND_BAM_NO_EOT
)) {
889 ret
= prepare_bam_async_desc(nandc
, nandc
->tx_chan
,
899 static int prep_adm_dma_desc(struct qcom_nand_controller
*nandc
, bool read
,
900 int reg_off
, const void *vaddr
, int size
,
903 struct desc_info
*desc
;
904 struct dma_async_tx_descriptor
*dma_desc
;
905 struct scatterlist
*sgl
;
906 struct dma_slave_config slave_conf
;
907 enum dma_transfer_direction dir_eng
;
910 desc
= kzalloc(sizeof(*desc
), GFP_KERNEL
);
914 sgl
= &desc
->adm_sgl
;
916 sg_init_one(sgl
, vaddr
, size
);
919 dir_eng
= DMA_DEV_TO_MEM
;
920 desc
->dir
= DMA_FROM_DEVICE
;
922 dir_eng
= DMA_MEM_TO_DEV
;
923 desc
->dir
= DMA_TO_DEVICE
;
926 ret
= dma_map_sg(nandc
->dev
, sgl
, 1, desc
->dir
);
932 memset(&slave_conf
, 0x00, sizeof(slave_conf
));
934 slave_conf
.device_fc
= flow_control
;
936 slave_conf
.src_maxburst
= 16;
937 slave_conf
.src_addr
= nandc
->base_dma
+ reg_off
;
938 slave_conf
.slave_id
= nandc
->data_crci
;
940 slave_conf
.dst_maxburst
= 16;
941 slave_conf
.dst_addr
= nandc
->base_dma
+ reg_off
;
942 slave_conf
.slave_id
= nandc
->cmd_crci
;
945 ret
= dmaengine_slave_config(nandc
->chan
, &slave_conf
);
947 dev_err(nandc
->dev
, "failed to configure dma channel\n");
951 dma_desc
= dmaengine_prep_slave_sg(nandc
->chan
, sgl
, 1, dir_eng
, 0);
953 dev_err(nandc
->dev
, "failed to prepare desc\n");
958 desc
->dma_desc
= dma_desc
;
960 list_add_tail(&desc
->node
, &nandc
->desc_list
);
970 * read_reg_dma: prepares a descriptor to read a given number of
971 * contiguous registers to the reg_read_buf pointer
973 * @first: offset of the first register in the contiguous block
974 * @num_regs: number of registers to read
975 * @flags: flags to control DMA descriptor preparation
977 static int read_reg_dma(struct qcom_nand_controller
*nandc
, int first
,
978 int num_regs
, unsigned int flags
)
980 bool flow_control
= false;
983 vaddr
= nandc
->reg_read_buf
+ nandc
->reg_read_pos
;
984 nandc
->reg_read_pos
+= num_regs
;
986 if (first
== NAND_DEV_CMD_VLD
|| first
== NAND_DEV_CMD1
)
987 first
= dev_cmd_reg_addr(nandc
, first
);
989 if (nandc
->props
->is_bam
)
990 return prep_bam_dma_desc_cmd(nandc
, true, first
, vaddr
,
993 if (first
== NAND_READ_ID
|| first
== NAND_FLASH_STATUS
)
996 return prep_adm_dma_desc(nandc
, true, first
, vaddr
,
997 num_regs
* sizeof(u32
), flow_control
);
1001 * write_reg_dma: prepares a descriptor to write a given number of
1002 * contiguous registers
1004 * @first: offset of the first register in the contiguous block
1005 * @num_regs: number of registers to write
1006 * @flags: flags to control DMA descriptor preparation
1008 static int write_reg_dma(struct qcom_nand_controller
*nandc
, int first
,
1009 int num_regs
, unsigned int flags
)
1011 bool flow_control
= false;
1012 struct nandc_regs
*regs
= nandc
->regs
;
1015 vaddr
= offset_to_nandc_reg(regs
, first
);
1017 if (first
== NAND_ERASED_CW_DETECT_CFG
) {
1018 if (flags
& NAND_ERASED_CW_SET
)
1019 vaddr
= ®s
->erased_cw_detect_cfg_set
;
1021 vaddr
= ®s
->erased_cw_detect_cfg_clr
;
1024 if (first
== NAND_EXEC_CMD
)
1025 flags
|= NAND_BAM_NWD
;
1027 if (first
== NAND_DEV_CMD1_RESTORE
|| first
== NAND_DEV_CMD1
)
1028 first
= dev_cmd_reg_addr(nandc
, NAND_DEV_CMD1
);
1030 if (first
== NAND_DEV_CMD_VLD_RESTORE
|| first
== NAND_DEV_CMD_VLD
)
1031 first
= dev_cmd_reg_addr(nandc
, NAND_DEV_CMD_VLD
);
1033 if (nandc
->props
->is_bam
)
1034 return prep_bam_dma_desc_cmd(nandc
, false, first
, vaddr
,
1037 if (first
== NAND_FLASH_CMD
)
1038 flow_control
= true;
1040 return prep_adm_dma_desc(nandc
, false, first
, vaddr
,
1041 num_regs
* sizeof(u32
), flow_control
);
1045 * read_data_dma: prepares a DMA descriptor to transfer data from the
1046 * controller's internal buffer to the buffer 'vaddr'
1048 * @reg_off: offset within the controller's data buffer
1049 * @vaddr: virtual address of the buffer we want to write to
1050 * @size: DMA transaction size in bytes
1051 * @flags: flags to control DMA descriptor preparation
1053 static int read_data_dma(struct qcom_nand_controller
*nandc
, int reg_off
,
1054 const u8
*vaddr
, int size
, unsigned int flags
)
1056 if (nandc
->props
->is_bam
)
1057 return prep_bam_dma_desc_data(nandc
, true, vaddr
, size
, flags
);
1059 return prep_adm_dma_desc(nandc
, true, reg_off
, vaddr
, size
, false);
1063 * write_data_dma: prepares a DMA descriptor to transfer data from
1064 * 'vaddr' to the controller's internal buffer
1066 * @reg_off: offset within the controller's data buffer
1067 * @vaddr: virtual address of the buffer we want to read from
1068 * @size: DMA transaction size in bytes
1069 * @flags: flags to control DMA descriptor preparation
1071 static int write_data_dma(struct qcom_nand_controller
*nandc
, int reg_off
,
1072 const u8
*vaddr
, int size
, unsigned int flags
)
1074 if (nandc
->props
->is_bam
)
1075 return prep_bam_dma_desc_data(nandc
, false, vaddr
, size
, flags
);
1077 return prep_adm_dma_desc(nandc
, false, reg_off
, vaddr
, size
, false);
1081 * Helper to prepare DMA descriptors for configuring registers
1082 * before reading a NAND page.
1084 static void config_nand_page_read(struct qcom_nand_controller
*nandc
)
1086 write_reg_dma(nandc
, NAND_ADDR0
, 2, 0);
1087 write_reg_dma(nandc
, NAND_DEV0_CFG0
, 3, 0);
1088 write_reg_dma(nandc
, NAND_EBI2_ECC_BUF_CFG
, 1, 0);
1089 write_reg_dma(nandc
, NAND_ERASED_CW_DETECT_CFG
, 1, 0);
1090 write_reg_dma(nandc
, NAND_ERASED_CW_DETECT_CFG
, 1,
1091 NAND_ERASED_CW_SET
| NAND_BAM_NEXT_SGL
);
1095 * Helper to prepare DMA descriptors for configuring registers
1096 * before reading each codeword in NAND page.
1099 config_nand_cw_read(struct qcom_nand_controller
*nandc
, bool use_ecc
)
1101 if (nandc
->props
->is_bam
)
1102 write_reg_dma(nandc
, NAND_READ_LOCATION_0
, 4,
1105 write_reg_dma(nandc
, NAND_FLASH_CMD
, 1, NAND_BAM_NEXT_SGL
);
1106 write_reg_dma(nandc
, NAND_EXEC_CMD
, 1, NAND_BAM_NEXT_SGL
);
1109 read_reg_dma(nandc
, NAND_FLASH_STATUS
, 2, 0);
1110 read_reg_dma(nandc
, NAND_ERASED_CW_DETECT_STATUS
, 1,
1113 read_reg_dma(nandc
, NAND_FLASH_STATUS
, 1, NAND_BAM_NEXT_SGL
);
1118 * Helper to prepare dma descriptors to configure registers needed for reading a
1119 * single codeword in page
1122 config_nand_single_cw_page_read(struct qcom_nand_controller
*nandc
,
1125 config_nand_page_read(nandc
);
1126 config_nand_cw_read(nandc
, use_ecc
);
1130 * Helper to prepare DMA descriptors used to configure registers needed for
1131 * before writing a NAND page.
1133 static void config_nand_page_write(struct qcom_nand_controller
*nandc
)
1135 write_reg_dma(nandc
, NAND_ADDR0
, 2, 0);
1136 write_reg_dma(nandc
, NAND_DEV0_CFG0
, 3, 0);
1137 write_reg_dma(nandc
, NAND_EBI2_ECC_BUF_CFG
, 1,
1142 * Helper to prepare DMA descriptors for configuring registers
1143 * before writing each codeword in NAND page.
1145 static void config_nand_cw_write(struct qcom_nand_controller
*nandc
)
1147 write_reg_dma(nandc
, NAND_FLASH_CMD
, 1, NAND_BAM_NEXT_SGL
);
1148 write_reg_dma(nandc
, NAND_EXEC_CMD
, 1, NAND_BAM_NEXT_SGL
);
1150 read_reg_dma(nandc
, NAND_FLASH_STATUS
, 1, NAND_BAM_NEXT_SGL
);
1152 write_reg_dma(nandc
, NAND_FLASH_STATUS
, 1, 0);
1153 write_reg_dma(nandc
, NAND_READ_STATUS
, 1, NAND_BAM_NEXT_SGL
);
1157 * the following functions are used within chip->cmdfunc() to perform different
1158 * NAND_CMD_* commands
1161 /* sets up descriptors for NAND_CMD_PARAM */
1162 static int nandc_param(struct qcom_nand_host
*host
)
1164 struct nand_chip
*chip
= &host
->chip
;
1165 struct qcom_nand_controller
*nandc
= get_qcom_nand_controller(chip
);
1168 * NAND_CMD_PARAM is called before we know much about the FLASH chip
1169 * in use. we configure the controller to perform a raw read of 512
1170 * bytes to read onfi params
1172 nandc_set_reg(nandc
, NAND_FLASH_CMD
, OP_PAGE_READ
| PAGE_ACC
| LAST_PAGE
);
1173 nandc_set_reg(nandc
, NAND_ADDR0
, 0);
1174 nandc_set_reg(nandc
, NAND_ADDR1
, 0);
1175 nandc_set_reg(nandc
, NAND_DEV0_CFG0
, 0 << CW_PER_PAGE
1176 | 512 << UD_SIZE_BYTES
1177 | 5 << NUM_ADDR_CYCLES
1178 | 0 << SPARE_SIZE_BYTES
);
1179 nandc_set_reg(nandc
, NAND_DEV0_CFG1
, 7 << NAND_RECOVERY_CYCLES
1180 | 0 << CS_ACTIVE_BSY
1181 | 17 << BAD_BLOCK_BYTE_NUM
1182 | 1 << BAD_BLOCK_IN_SPARE_AREA
1183 | 2 << WR_RD_BSY_GAP
1185 | 1 << DEV0_CFG1_ECC_DISABLE
);
1186 nandc_set_reg(nandc
, NAND_EBI2_ECC_BUF_CFG
, 1 << ECC_CFG_ECC_DISABLE
);
1188 /* configure CMD1 and VLD for ONFI param probing */
1189 nandc_set_reg(nandc
, NAND_DEV_CMD_VLD
,
1190 (nandc
->vld
& ~READ_START_VLD
));
1191 nandc_set_reg(nandc
, NAND_DEV_CMD1
,
1192 (nandc
->cmd1
& ~(0xFF << READ_ADDR
))
1193 | NAND_CMD_PARAM
<< READ_ADDR
);
1195 nandc_set_reg(nandc
, NAND_EXEC_CMD
, 1);
1197 nandc_set_reg(nandc
, NAND_DEV_CMD1_RESTORE
, nandc
->cmd1
);
1198 nandc_set_reg(nandc
, NAND_DEV_CMD_VLD_RESTORE
, nandc
->vld
);
1199 nandc_set_read_loc(nandc
, 0, 0, 512, 1);
1201 write_reg_dma(nandc
, NAND_DEV_CMD_VLD
, 1, 0);
1202 write_reg_dma(nandc
, NAND_DEV_CMD1
, 1, NAND_BAM_NEXT_SGL
);
1204 nandc
->buf_count
= 512;
1205 memset(nandc
->data_buffer
, 0xff, nandc
->buf_count
);
1207 config_nand_single_cw_page_read(nandc
, false);
1209 read_data_dma(nandc
, FLASH_BUF_ACC
, nandc
->data_buffer
,
1210 nandc
->buf_count
, 0);
1212 /* restore CMD1 and VLD regs */
1213 write_reg_dma(nandc
, NAND_DEV_CMD1_RESTORE
, 1, 0);
1214 write_reg_dma(nandc
, NAND_DEV_CMD_VLD_RESTORE
, 1, NAND_BAM_NEXT_SGL
);
1219 /* sets up descriptors for NAND_CMD_ERASE1 */
1220 static int erase_block(struct qcom_nand_host
*host
, int page_addr
)
1222 struct nand_chip
*chip
= &host
->chip
;
1223 struct qcom_nand_controller
*nandc
= get_qcom_nand_controller(chip
);
1225 nandc_set_reg(nandc
, NAND_FLASH_CMD
,
1226 OP_BLOCK_ERASE
| PAGE_ACC
| LAST_PAGE
);
1227 nandc_set_reg(nandc
, NAND_ADDR0
, page_addr
);
1228 nandc_set_reg(nandc
, NAND_ADDR1
, 0);
1229 nandc_set_reg(nandc
, NAND_DEV0_CFG0
,
1230 host
->cfg0_raw
& ~(7 << CW_PER_PAGE
));
1231 nandc_set_reg(nandc
, NAND_DEV0_CFG1
, host
->cfg1_raw
);
1232 nandc_set_reg(nandc
, NAND_EXEC_CMD
, 1);
1233 nandc_set_reg(nandc
, NAND_FLASH_STATUS
, host
->clrflashstatus
);
1234 nandc_set_reg(nandc
, NAND_READ_STATUS
, host
->clrreadstatus
);
1236 write_reg_dma(nandc
, NAND_FLASH_CMD
, 3, NAND_BAM_NEXT_SGL
);
1237 write_reg_dma(nandc
, NAND_DEV0_CFG0
, 2, NAND_BAM_NEXT_SGL
);
1238 write_reg_dma(nandc
, NAND_EXEC_CMD
, 1, NAND_BAM_NEXT_SGL
);
1240 read_reg_dma(nandc
, NAND_FLASH_STATUS
, 1, NAND_BAM_NEXT_SGL
);
1242 write_reg_dma(nandc
, NAND_FLASH_STATUS
, 1, 0);
1243 write_reg_dma(nandc
, NAND_READ_STATUS
, 1, NAND_BAM_NEXT_SGL
);
1248 /* sets up descriptors for NAND_CMD_READID */
1249 static int read_id(struct qcom_nand_host
*host
, int column
)
1251 struct nand_chip
*chip
= &host
->chip
;
1252 struct qcom_nand_controller
*nandc
= get_qcom_nand_controller(chip
);
1257 nandc_set_reg(nandc
, NAND_FLASH_CMD
, OP_FETCH_ID
);
1258 nandc_set_reg(nandc
, NAND_ADDR0
, column
);
1259 nandc_set_reg(nandc
, NAND_ADDR1
, 0);
1260 nandc_set_reg(nandc
, NAND_FLASH_CHIP_SELECT
,
1261 nandc
->props
->is_bam
? 0 : DM_EN
);
1262 nandc_set_reg(nandc
, NAND_EXEC_CMD
, 1);
1264 write_reg_dma(nandc
, NAND_FLASH_CMD
, 4, NAND_BAM_NEXT_SGL
);
1265 write_reg_dma(nandc
, NAND_EXEC_CMD
, 1, NAND_BAM_NEXT_SGL
);
1267 read_reg_dma(nandc
, NAND_READ_ID
, 1, NAND_BAM_NEXT_SGL
);
1272 /* sets up descriptors for NAND_CMD_RESET */
1273 static int reset(struct qcom_nand_host
*host
)
1275 struct nand_chip
*chip
= &host
->chip
;
1276 struct qcom_nand_controller
*nandc
= get_qcom_nand_controller(chip
);
1278 nandc_set_reg(nandc
, NAND_FLASH_CMD
, OP_RESET_DEVICE
);
1279 nandc_set_reg(nandc
, NAND_EXEC_CMD
, 1);
1281 write_reg_dma(nandc
, NAND_FLASH_CMD
, 1, NAND_BAM_NEXT_SGL
);
1282 write_reg_dma(nandc
, NAND_EXEC_CMD
, 1, NAND_BAM_NEXT_SGL
);
1284 read_reg_dma(nandc
, NAND_FLASH_STATUS
, 1, NAND_BAM_NEXT_SGL
);
1289 /* helpers to submit/free our list of dma descriptors */
1290 static int submit_descs(struct qcom_nand_controller
*nandc
)
1292 struct desc_info
*desc
;
1293 dma_cookie_t cookie
= 0;
1294 struct bam_transaction
*bam_txn
= nandc
->bam_txn
;
1297 if (nandc
->props
->is_bam
) {
1298 if (bam_txn
->rx_sgl_pos
> bam_txn
->rx_sgl_start
) {
1299 r
= prepare_bam_async_desc(nandc
, nandc
->rx_chan
, 0);
1304 if (bam_txn
->tx_sgl_pos
> bam_txn
->tx_sgl_start
) {
1305 r
= prepare_bam_async_desc(nandc
, nandc
->tx_chan
,
1306 DMA_PREP_INTERRUPT
);
1311 if (bam_txn
->cmd_sgl_pos
> bam_txn
->cmd_sgl_start
) {
1312 r
= prepare_bam_async_desc(nandc
, nandc
->cmd_chan
,
1319 list_for_each_entry(desc
, &nandc
->desc_list
, node
)
1320 cookie
= dmaengine_submit(desc
->dma_desc
);
1322 if (nandc
->props
->is_bam
) {
1323 bam_txn
->last_cmd_desc
->callback
= qpic_bam_dma_done
;
1324 bam_txn
->last_cmd_desc
->callback_param
= bam_txn
;
1325 if (bam_txn
->last_data_desc
) {
1326 bam_txn
->last_data_desc
->callback
= qpic_bam_dma_done
;
1327 bam_txn
->last_data_desc
->callback_param
= bam_txn
;
1328 bam_txn
->wait_second_completion
= true;
1331 dma_async_issue_pending(nandc
->tx_chan
);
1332 dma_async_issue_pending(nandc
->rx_chan
);
1333 dma_async_issue_pending(nandc
->cmd_chan
);
1335 if (!wait_for_completion_timeout(&bam_txn
->txn_done
,
1336 QPIC_NAND_COMPLETION_TIMEOUT
))
1339 if (dma_sync_wait(nandc
->chan
, cookie
) != DMA_COMPLETE
)
1346 static void free_descs(struct qcom_nand_controller
*nandc
)
1348 struct desc_info
*desc
, *n
;
1350 list_for_each_entry_safe(desc
, n
, &nandc
->desc_list
, node
) {
1351 list_del(&desc
->node
);
1353 if (nandc
->props
->is_bam
)
1354 dma_unmap_sg(nandc
->dev
, desc
->bam_sgl
,
1355 desc
->sgl_cnt
, desc
->dir
);
1357 dma_unmap_sg(nandc
->dev
, &desc
->adm_sgl
, 1,
1364 /* reset the register read buffer for next NAND operation */
1365 static void clear_read_regs(struct qcom_nand_controller
*nandc
)
1367 nandc
->reg_read_pos
= 0;
1368 nandc_read_buffer_sync(nandc
, false);
1371 static void pre_command(struct qcom_nand_host
*host
, int command
)
1373 struct nand_chip
*chip
= &host
->chip
;
1374 struct qcom_nand_controller
*nandc
= get_qcom_nand_controller(chip
);
1376 nandc
->buf_count
= 0;
1377 nandc
->buf_start
= 0;
1378 host
->use_ecc
= false;
1379 host
->last_command
= command
;
1381 clear_read_regs(nandc
);
1383 if (command
== NAND_CMD_RESET
|| command
== NAND_CMD_READID
||
1384 command
== NAND_CMD_PARAM
|| command
== NAND_CMD_ERASE1
)
1385 clear_bam_transaction(nandc
);
1389 * this is called after NAND_CMD_PAGEPROG and NAND_CMD_ERASE1 to set our
1390 * privately maintained status byte, this status byte can be read after
1391 * NAND_CMD_STATUS is called
1393 static void parse_erase_write_errors(struct qcom_nand_host
*host
, int command
)
1395 struct nand_chip
*chip
= &host
->chip
;
1396 struct qcom_nand_controller
*nandc
= get_qcom_nand_controller(chip
);
1397 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
1401 num_cw
= command
== NAND_CMD_PAGEPROG
? ecc
->steps
: 1;
1402 nandc_read_buffer_sync(nandc
, true);
1404 for (i
= 0; i
< num_cw
; i
++) {
1405 u32 flash_status
= le32_to_cpu(nandc
->reg_read_buf
[i
]);
1407 if (flash_status
& FS_MPU_ERR
)
1408 host
->status
&= ~NAND_STATUS_WP
;
1410 if (flash_status
& FS_OP_ERR
|| (i
== (num_cw
- 1) &&
1412 FS_DEVICE_STS_ERR
)))
1413 host
->status
|= NAND_STATUS_FAIL
;
1417 static void post_command(struct qcom_nand_host
*host
, int command
)
1419 struct nand_chip
*chip
= &host
->chip
;
1420 struct qcom_nand_controller
*nandc
= get_qcom_nand_controller(chip
);
1423 case NAND_CMD_READID
:
1424 nandc_read_buffer_sync(nandc
, true);
1425 memcpy(nandc
->data_buffer
, nandc
->reg_read_buf
,
1428 case NAND_CMD_PAGEPROG
:
1429 case NAND_CMD_ERASE1
:
1430 parse_erase_write_errors(host
, command
);
1438 * Implements chip->cmdfunc. It's only used for a limited set of commands.
1439 * The rest of the commands wouldn't be called by upper layers. For example,
1440 * NAND_CMD_READOOB would never be called because we have our own versions
1441 * of read_oob ops for nand_ecc_ctrl.
1443 static void qcom_nandc_command(struct mtd_info
*mtd
, unsigned int command
,
1444 int column
, int page_addr
)
1446 struct nand_chip
*chip
= mtd_to_nand(mtd
);
1447 struct qcom_nand_host
*host
= to_qcom_nand_host(chip
);
1448 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
1449 struct qcom_nand_controller
*nandc
= get_qcom_nand_controller(chip
);
1453 pre_command(host
, command
);
1456 case NAND_CMD_RESET
:
1461 case NAND_CMD_READID
:
1462 nandc
->buf_count
= 4;
1463 ret
= read_id(host
, column
);
1467 case NAND_CMD_PARAM
:
1468 ret
= nandc_param(host
);
1472 case NAND_CMD_ERASE1
:
1473 ret
= erase_block(host
, page_addr
);
1477 case NAND_CMD_READ0
:
1478 /* we read the entire page for now */
1479 WARN_ON(column
!= 0);
1481 host
->use_ecc
= true;
1482 set_address(host
, 0, page_addr
);
1483 update_rw_regs(host
, ecc
->steps
, true);
1486 case NAND_CMD_SEQIN
:
1487 WARN_ON(column
!= 0);
1488 set_address(host
, 0, page_addr
);
1491 case NAND_CMD_PAGEPROG
:
1492 case NAND_CMD_STATUS
:
1499 dev_err(nandc
->dev
, "failure executing command %d\n",
1506 ret
= submit_descs(nandc
);
1509 "failure submitting descs for command %d\n",
1515 post_command(host
, command
);
1519 * when using BCH ECC, the HW flags an error in NAND_FLASH_STATUS if it read
1520 * an erased CW, and reports an erased CW in NAND_ERASED_CW_DETECT_STATUS.
1522 * when using RS ECC, the HW reports the same erros when reading an erased CW,
1523 * but it notifies that it is an erased CW by placing special characters at
1524 * certain offsets in the buffer.
1526 * verify if the page is erased or not, and fix up the page for RS ECC by
1527 * replacing the special characters with 0xff.
1529 static bool erased_chunk_check_and_fixup(u8
*data_buf
, int data_len
)
1534 * an erased page flags an error in NAND_FLASH_STATUS, check if the page
1535 * is erased by looking for 0x54s at offsets 3 and 175 from the
1536 * beginning of each codeword
1539 empty1
= data_buf
[3];
1540 empty2
= data_buf
[175];
1543 * if the erased codework markers, if they exist override them with
1546 if ((empty1
== 0x54 && empty2
== 0xff) ||
1547 (empty1
== 0xff && empty2
== 0x54)) {
1549 data_buf
[175] = 0xff;
1553 * check if the entire chunk contains 0xffs or not. if it doesn't, then
1554 * restore the original values at the special offsets
1556 if (memchr_inv(data_buf
, 0xff, data_len
)) {
1557 data_buf
[3] = empty1
;
1558 data_buf
[175] = empty2
;
1572 /* reads back FLASH_STATUS register set by the controller */
1573 static int check_flash_errors(struct qcom_nand_host
*host
, int cw_cnt
)
1575 struct nand_chip
*chip
= &host
->chip
;
1576 struct qcom_nand_controller
*nandc
= get_qcom_nand_controller(chip
);
1579 for (i
= 0; i
< cw_cnt
; i
++) {
1580 u32 flash
= le32_to_cpu(nandc
->reg_read_buf
[i
]);
1582 if (flash
& (FS_OP_ERR
| FS_MPU_ERR
))
1589 /* performs raw read for one codeword */
1591 qcom_nandc_read_cw_raw(struct mtd_info
*mtd
, struct nand_chip
*chip
,
1592 u8
*data_buf
, u8
*oob_buf
, int page
, int cw
)
1594 struct qcom_nand_host
*host
= to_qcom_nand_host(chip
);
1595 struct qcom_nand_controller
*nandc
= get_qcom_nand_controller(chip
);
1596 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
1597 int data_size1
, data_size2
, oob_size1
, oob_size2
;
1598 int ret
, reg_off
= FLASH_BUF_ACC
, read_loc
= 0;
1600 nand_read_page_op(chip
, page
, 0, NULL
, 0);
1601 host
->use_ecc
= false;
1603 clear_bam_transaction(nandc
);
1604 set_address(host
, host
->cw_size
* cw
, page
);
1605 update_rw_regs(host
, 1, true);
1606 config_nand_page_read(nandc
);
1608 data_size1
= mtd
->writesize
- host
->cw_size
* (ecc
->steps
- 1);
1609 oob_size1
= host
->bbm_size
;
1611 if (cw
== (ecc
->steps
- 1)) {
1612 data_size2
= ecc
->size
- data_size1
-
1613 ((ecc
->steps
- 1) * 4);
1614 oob_size2
= (ecc
->steps
* 4) + host
->ecc_bytes_hw
+
1617 data_size2
= host
->cw_data
- data_size1
;
1618 oob_size2
= host
->ecc_bytes_hw
+ host
->spare_bytes
;
1621 if (nandc
->props
->is_bam
) {
1622 nandc_set_read_loc(nandc
, 0, read_loc
, data_size1
, 0);
1623 read_loc
+= data_size1
;
1625 nandc_set_read_loc(nandc
, 1, read_loc
, oob_size1
, 0);
1626 read_loc
+= oob_size1
;
1628 nandc_set_read_loc(nandc
, 2, read_loc
, data_size2
, 0);
1629 read_loc
+= data_size2
;
1631 nandc_set_read_loc(nandc
, 3, read_loc
, oob_size2
, 1);
1634 config_nand_cw_read(nandc
, false);
1636 read_data_dma(nandc
, reg_off
, data_buf
, data_size1
, 0);
1637 reg_off
+= data_size1
;
1639 read_data_dma(nandc
, reg_off
, oob_buf
, oob_size1
, 0);
1640 reg_off
+= oob_size1
;
1642 read_data_dma(nandc
, reg_off
, data_buf
+ data_size1
, data_size2
, 0);
1643 reg_off
+= data_size2
;
1645 read_data_dma(nandc
, reg_off
, oob_buf
+ oob_size1
, oob_size2
, 0);
1647 ret
= submit_descs(nandc
);
1650 dev_err(nandc
->dev
, "failure to read raw cw %d\n", cw
);
1654 return check_flash_errors(host
, 1);
1658 * Bitflips can happen in erased codewords also so this function counts the
1659 * number of 0 in each CW for which ECC engine returns the uncorrectable
1660 * error. The page will be assumed as erased if this count is less than or
1661 * equal to the ecc->strength for each CW.
1663 * 1. Both DATA and OOB need to be checked for number of 0. The
1664 * top-level API can be called with only data buf or OOB buf so use
1665 * chip->data_buf if data buf is null and chip->oob_poi if oob buf
1666 * is null for copying the raw bytes.
1667 * 2. Perform raw read for all the CW which has uncorrectable errors.
1668 * 3. For each CW, check the number of 0 in cw_data and usable OOB bytes.
1669 * The BBM and spare bytes bit flip won’t affect the ECC so don’t check
1670 * the number of bitflips in this area.
1673 check_for_erased_page(struct qcom_nand_host
*host
, u8
*data_buf
,
1674 u8
*oob_buf
, unsigned long uncorrectable_cws
,
1675 int page
, unsigned int max_bitflips
)
1677 struct nand_chip
*chip
= &host
->chip
;
1678 struct mtd_info
*mtd
= nand_to_mtd(chip
);
1679 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
1680 u8
*cw_data_buf
, *cw_oob_buf
;
1681 int cw
, data_size
, oob_size
, ret
= 0;
1684 data_buf
= chip
->data_buf
;
1689 oob_buf
= chip
->oob_poi
;
1693 for_each_set_bit(cw
, &uncorrectable_cws
, ecc
->steps
) {
1694 if (cw
== (ecc
->steps
- 1)) {
1695 data_size
= ecc
->size
- ((ecc
->steps
- 1) * 4);
1696 oob_size
= (ecc
->steps
* 4) + host
->ecc_bytes_hw
;
1698 data_size
= host
->cw_data
;
1699 oob_size
= host
->ecc_bytes_hw
;
1702 /* determine starting buffer address for current CW */
1703 cw_data_buf
= data_buf
+ (cw
* host
->cw_data
);
1704 cw_oob_buf
= oob_buf
+ (cw
* ecc
->bytes
);
1706 ret
= qcom_nandc_read_cw_raw(mtd
, chip
, cw_data_buf
,
1707 cw_oob_buf
, page
, cw
);
1712 * make sure it isn't an erased page reported
1713 * as not-erased by HW because of a few bitflips
1715 ret
= nand_check_erased_ecc_chunk(cw_data_buf
, data_size
,
1716 cw_oob_buf
+ host
->bbm_size
,
1720 mtd
->ecc_stats
.failed
++;
1722 mtd
->ecc_stats
.corrected
+= ret
;
1723 max_bitflips
= max_t(unsigned int, max_bitflips
, ret
);
1727 return max_bitflips
;
1731 * reads back status registers set by the controller to notify page read
1732 * errors. this is equivalent to what 'ecc->correct()' would do.
1734 static int parse_read_errors(struct qcom_nand_host
*host
, u8
*data_buf
,
1735 u8
*oob_buf
, int page
)
1737 struct nand_chip
*chip
= &host
->chip
;
1738 struct qcom_nand_controller
*nandc
= get_qcom_nand_controller(chip
);
1739 struct mtd_info
*mtd
= nand_to_mtd(chip
);
1740 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
1741 unsigned int max_bitflips
= 0, uncorrectable_cws
= 0;
1742 struct read_stats
*buf
;
1743 bool flash_op_err
= false, erased
;
1745 u8
*data_buf_start
= data_buf
, *oob_buf_start
= oob_buf
;
1747 buf
= (struct read_stats
*)nandc
->reg_read_buf
;
1748 nandc_read_buffer_sync(nandc
, true);
1750 for (i
= 0; i
< ecc
->steps
; i
++, buf
++) {
1751 u32 flash
, buffer
, erased_cw
;
1752 int data_len
, oob_len
;
1754 if (i
== (ecc
->steps
- 1)) {
1755 data_len
= ecc
->size
- ((ecc
->steps
- 1) << 2);
1756 oob_len
= ecc
->steps
<< 2;
1758 data_len
= host
->cw_data
;
1762 flash
= le32_to_cpu(buf
->flash
);
1763 buffer
= le32_to_cpu(buf
->buffer
);
1764 erased_cw
= le32_to_cpu(buf
->erased_cw
);
1767 * Check ECC failure for each codeword. ECC failure can
1768 * happen in either of the following conditions
1769 * 1. If number of bitflips are greater than ECC engine
1771 * 2. If this codeword contains all 0xff for which erased
1772 * codeword detection check will be done.
1774 if ((flash
& FS_OP_ERR
) && (buffer
& BS_UNCORRECTABLE_BIT
)) {
1776 * For BCH ECC, ignore erased codeword errors, if
1777 * ERASED_CW bits are set.
1779 if (host
->bch_enabled
) {
1780 erased
= (erased_cw
& ERASED_CW
) == ERASED_CW
?
1783 * For RS ECC, HW reports the erased CW by placing
1784 * special characters at certain offsets in the buffer.
1785 * These special characters will be valid only if
1786 * complete page is read i.e. data_buf is not NULL.
1788 } else if (data_buf
) {
1789 erased
= erased_chunk_check_and_fixup(data_buf
,
1796 uncorrectable_cws
|= BIT(i
);
1798 * Check if MPU or any other operational error (timeout,
1799 * device failure, etc.) happened for this codeword and
1800 * make flash_op_err true. If flash_op_err is set, then
1801 * EIO will be returned for page read.
1803 } else if (flash
& (FS_OP_ERR
| FS_MPU_ERR
)) {
1804 flash_op_err
= true;
1806 * No ECC or operational errors happened. Check the number of
1807 * bits corrected and update the ecc_stats.corrected.
1812 stat
= buffer
& BS_CORRECTABLE_ERR_MSK
;
1813 mtd
->ecc_stats
.corrected
+= stat
;
1814 max_bitflips
= max(max_bitflips
, stat
);
1818 data_buf
+= data_len
;
1820 oob_buf
+= oob_len
+ ecc
->bytes
;
1826 if (!uncorrectable_cws
)
1827 return max_bitflips
;
1829 return check_for_erased_page(host
, data_buf_start
, oob_buf_start
,
1830 uncorrectable_cws
, page
,
1835 * helper to perform the actual page read operation, used by ecc->read_page(),
1838 static int read_page_ecc(struct qcom_nand_host
*host
, u8
*data_buf
,
1839 u8
*oob_buf
, int page
)
1841 struct nand_chip
*chip
= &host
->chip
;
1842 struct qcom_nand_controller
*nandc
= get_qcom_nand_controller(chip
);
1843 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
1844 u8
*data_buf_start
= data_buf
, *oob_buf_start
= oob_buf
;
1847 config_nand_page_read(nandc
);
1849 /* queue cmd descs for each codeword */
1850 for (i
= 0; i
< ecc
->steps
; i
++) {
1851 int data_size
, oob_size
;
1853 if (i
== (ecc
->steps
- 1)) {
1854 data_size
= ecc
->size
- ((ecc
->steps
- 1) << 2);
1855 oob_size
= (ecc
->steps
<< 2) + host
->ecc_bytes_hw
+
1858 data_size
= host
->cw_data
;
1859 oob_size
= host
->ecc_bytes_hw
+ host
->spare_bytes
;
1862 if (nandc
->props
->is_bam
) {
1863 if (data_buf
&& oob_buf
) {
1864 nandc_set_read_loc(nandc
, 0, 0, data_size
, 0);
1865 nandc_set_read_loc(nandc
, 1, data_size
,
1867 } else if (data_buf
) {
1868 nandc_set_read_loc(nandc
, 0, 0, data_size
, 1);
1870 nandc_set_read_loc(nandc
, 0, data_size
,
1875 config_nand_cw_read(nandc
, true);
1878 read_data_dma(nandc
, FLASH_BUF_ACC
, data_buf
,
1882 * when ecc is enabled, the controller doesn't read the real
1883 * or dummy bad block markers in each chunk. To maintain a
1884 * consistent layout across RAW and ECC reads, we just
1885 * leave the real/dummy BBM offsets empty (i.e, filled with
1891 for (j
= 0; j
< host
->bbm_size
; j
++)
1894 read_data_dma(nandc
, FLASH_BUF_ACC
+ data_size
,
1895 oob_buf
, oob_size
, 0);
1899 data_buf
+= data_size
;
1901 oob_buf
+= oob_size
;
1904 ret
= submit_descs(nandc
);
1908 dev_err(nandc
->dev
, "failure to read page/oob\n");
1912 return parse_read_errors(host
, data_buf_start
, oob_buf_start
, page
);
1916 * a helper that copies the last step/codeword of a page (containing free oob)
1917 * into our local buffer
1919 static int copy_last_cw(struct qcom_nand_host
*host
, int page
)
1921 struct nand_chip
*chip
= &host
->chip
;
1922 struct qcom_nand_controller
*nandc
= get_qcom_nand_controller(chip
);
1923 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
1927 clear_read_regs(nandc
);
1929 size
= host
->use_ecc
? host
->cw_data
: host
->cw_size
;
1931 /* prepare a clean read buffer */
1932 memset(nandc
->data_buffer
, 0xff, size
);
1934 set_address(host
, host
->cw_size
* (ecc
->steps
- 1), page
);
1935 update_rw_regs(host
, 1, true);
1937 config_nand_single_cw_page_read(nandc
, host
->use_ecc
);
1939 read_data_dma(nandc
, FLASH_BUF_ACC
, nandc
->data_buffer
, size
, 0);
1941 ret
= submit_descs(nandc
);
1943 dev_err(nandc
->dev
, "failed to copy last codeword\n");
1950 /* implements ecc->read_page() */
1951 static int qcom_nandc_read_page(struct mtd_info
*mtd
, struct nand_chip
*chip
,
1952 uint8_t *buf
, int oob_required
, int page
)
1954 struct qcom_nand_host
*host
= to_qcom_nand_host(chip
);
1955 struct qcom_nand_controller
*nandc
= get_qcom_nand_controller(chip
);
1956 u8
*data_buf
, *oob_buf
= NULL
;
1958 nand_read_page_op(chip
, page
, 0, NULL
, 0);
1960 oob_buf
= oob_required
? chip
->oob_poi
: NULL
;
1962 clear_bam_transaction(nandc
);
1964 return read_page_ecc(host
, data_buf
, oob_buf
, page
);
1967 /* implements ecc->read_page_raw() */
1968 static int qcom_nandc_read_page_raw(struct mtd_info
*mtd
,
1969 struct nand_chip
*chip
, uint8_t *buf
,
1970 int oob_required
, int page
)
1972 struct qcom_nand_host
*host
= to_qcom_nand_host(chip
);
1973 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
1975 u8
*data_buf
= buf
, *oob_buf
= chip
->oob_poi
;
1977 for (cw
= 0; cw
< ecc
->steps
; cw
++) {
1978 ret
= qcom_nandc_read_cw_raw(mtd
, chip
, data_buf
, oob_buf
,
1983 data_buf
+= host
->cw_data
;
1984 oob_buf
+= ecc
->bytes
;
1990 /* implements ecc->read_oob() */
1991 static int qcom_nandc_read_oob(struct mtd_info
*mtd
, struct nand_chip
*chip
,
1994 struct qcom_nand_host
*host
= to_qcom_nand_host(chip
);
1995 struct qcom_nand_controller
*nandc
= get_qcom_nand_controller(chip
);
1996 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
1998 clear_read_regs(nandc
);
1999 clear_bam_transaction(nandc
);
2001 host
->use_ecc
= true;
2002 set_address(host
, 0, page
);
2003 update_rw_regs(host
, ecc
->steps
, true);
2005 return read_page_ecc(host
, NULL
, chip
->oob_poi
, page
);
2008 /* implements ecc->write_page() */
2009 static int qcom_nandc_write_page(struct mtd_info
*mtd
, struct nand_chip
*chip
,
2010 const uint8_t *buf
, int oob_required
, int page
)
2012 struct qcom_nand_host
*host
= to_qcom_nand_host(chip
);
2013 struct qcom_nand_controller
*nandc
= get_qcom_nand_controller(chip
);
2014 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
2015 u8
*data_buf
, *oob_buf
;
2018 nand_prog_page_begin_op(chip
, page
, 0, NULL
, 0);
2020 clear_read_regs(nandc
);
2021 clear_bam_transaction(nandc
);
2023 data_buf
= (u8
*)buf
;
2024 oob_buf
= chip
->oob_poi
;
2026 host
->use_ecc
= true;
2027 update_rw_regs(host
, ecc
->steps
, false);
2028 config_nand_page_write(nandc
);
2030 for (i
= 0; i
< ecc
->steps
; i
++) {
2031 int data_size
, oob_size
;
2033 if (i
== (ecc
->steps
- 1)) {
2034 data_size
= ecc
->size
- ((ecc
->steps
- 1) << 2);
2035 oob_size
= (ecc
->steps
<< 2) + host
->ecc_bytes_hw
+
2038 data_size
= host
->cw_data
;
2039 oob_size
= ecc
->bytes
;
2043 write_data_dma(nandc
, FLASH_BUF_ACC
, data_buf
, data_size
,
2044 i
== (ecc
->steps
- 1) ? NAND_BAM_NO_EOT
: 0);
2047 * when ECC is enabled, we don't really need to write anything
2048 * to oob for the first n - 1 codewords since these oob regions
2049 * just contain ECC bytes that's written by the controller
2050 * itself. For the last codeword, we skip the bbm positions and
2051 * write to the free oob area.
2053 if (i
== (ecc
->steps
- 1)) {
2054 oob_buf
+= host
->bbm_size
;
2056 write_data_dma(nandc
, FLASH_BUF_ACC
+ data_size
,
2057 oob_buf
, oob_size
, 0);
2060 config_nand_cw_write(nandc
);
2062 data_buf
+= data_size
;
2063 oob_buf
+= oob_size
;
2066 ret
= submit_descs(nandc
);
2068 dev_err(nandc
->dev
, "failure to write page\n");
2073 ret
= nand_prog_page_end_op(chip
);
2078 /* implements ecc->write_page_raw() */
2079 static int qcom_nandc_write_page_raw(struct mtd_info
*mtd
,
2080 struct nand_chip
*chip
, const uint8_t *buf
,
2081 int oob_required
, int page
)
2083 struct qcom_nand_host
*host
= to_qcom_nand_host(chip
);
2084 struct qcom_nand_controller
*nandc
= get_qcom_nand_controller(chip
);
2085 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
2086 u8
*data_buf
, *oob_buf
;
2089 nand_prog_page_begin_op(chip
, page
, 0, NULL
, 0);
2090 clear_read_regs(nandc
);
2091 clear_bam_transaction(nandc
);
2093 data_buf
= (u8
*)buf
;
2094 oob_buf
= chip
->oob_poi
;
2096 host
->use_ecc
= false;
2097 update_rw_regs(host
, ecc
->steps
, false);
2098 config_nand_page_write(nandc
);
2100 for (i
= 0; i
< ecc
->steps
; i
++) {
2101 int data_size1
, data_size2
, oob_size1
, oob_size2
;
2102 int reg_off
= FLASH_BUF_ACC
;
2104 data_size1
= mtd
->writesize
- host
->cw_size
* (ecc
->steps
- 1);
2105 oob_size1
= host
->bbm_size
;
2107 if (i
== (ecc
->steps
- 1)) {
2108 data_size2
= ecc
->size
- data_size1
-
2109 ((ecc
->steps
- 1) << 2);
2110 oob_size2
= (ecc
->steps
<< 2) + host
->ecc_bytes_hw
+
2113 data_size2
= host
->cw_data
- data_size1
;
2114 oob_size2
= host
->ecc_bytes_hw
+ host
->spare_bytes
;
2117 write_data_dma(nandc
, reg_off
, data_buf
, data_size1
,
2119 reg_off
+= data_size1
;
2120 data_buf
+= data_size1
;
2122 write_data_dma(nandc
, reg_off
, oob_buf
, oob_size1
,
2124 reg_off
+= oob_size1
;
2125 oob_buf
+= oob_size1
;
2127 write_data_dma(nandc
, reg_off
, data_buf
, data_size2
,
2129 reg_off
+= data_size2
;
2130 data_buf
+= data_size2
;
2132 write_data_dma(nandc
, reg_off
, oob_buf
, oob_size2
, 0);
2133 oob_buf
+= oob_size2
;
2135 config_nand_cw_write(nandc
);
2138 ret
= submit_descs(nandc
);
2140 dev_err(nandc
->dev
, "failure to write raw page\n");
2145 ret
= nand_prog_page_end_op(chip
);
2151 * implements ecc->write_oob()
2153 * the NAND controller cannot write only data or only OOB within a codeword
2154 * since ECC is calculated for the combined codeword. So update the OOB from
2155 * chip->oob_poi, and pad the data area with OxFF before writing.
2157 static int qcom_nandc_write_oob(struct mtd_info
*mtd
, struct nand_chip
*chip
,
2160 struct qcom_nand_host
*host
= to_qcom_nand_host(chip
);
2161 struct qcom_nand_controller
*nandc
= get_qcom_nand_controller(chip
);
2162 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
2163 u8
*oob
= chip
->oob_poi
;
2164 int data_size
, oob_size
;
2167 host
->use_ecc
= true;
2168 clear_bam_transaction(nandc
);
2170 /* calculate the data and oob size for the last codeword/step */
2171 data_size
= ecc
->size
- ((ecc
->steps
- 1) << 2);
2172 oob_size
= mtd
->oobavail
;
2174 memset(nandc
->data_buffer
, 0xff, host
->cw_data
);
2175 /* override new oob content to last codeword */
2176 mtd_ooblayout_get_databytes(mtd
, nandc
->data_buffer
+ data_size
, oob
,
2179 set_address(host
, host
->cw_size
* (ecc
->steps
- 1), page
);
2180 update_rw_regs(host
, 1, false);
2182 config_nand_page_write(nandc
);
2183 write_data_dma(nandc
, FLASH_BUF_ACC
,
2184 nandc
->data_buffer
, data_size
+ oob_size
, 0);
2185 config_nand_cw_write(nandc
);
2187 ret
= submit_descs(nandc
);
2192 dev_err(nandc
->dev
, "failure to write oob\n");
2196 return nand_prog_page_end_op(chip
);
2199 static int qcom_nandc_block_bad(struct mtd_info
*mtd
, loff_t ofs
)
2201 struct nand_chip
*chip
= mtd_to_nand(mtd
);
2202 struct qcom_nand_host
*host
= to_qcom_nand_host(chip
);
2203 struct qcom_nand_controller
*nandc
= get_qcom_nand_controller(chip
);
2204 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
2205 int page
, ret
, bbpos
, bad
= 0;
2207 page
= (int)(ofs
>> chip
->page_shift
) & chip
->pagemask
;
2210 * configure registers for a raw sub page read, the address is set to
2211 * the beginning of the last codeword, we don't care about reading ecc
2212 * portion of oob. we just want the first few bytes from this codeword
2213 * that contains the BBM
2215 host
->use_ecc
= false;
2217 clear_bam_transaction(nandc
);
2218 ret
= copy_last_cw(host
, page
);
2222 if (check_flash_errors(host
, 1)) {
2223 dev_warn(nandc
->dev
, "error when trying to read BBM\n");
2227 bbpos
= mtd
->writesize
- host
->cw_size
* (ecc
->steps
- 1);
2229 bad
= nandc
->data_buffer
[bbpos
] != 0xff;
2231 if (chip
->options
& NAND_BUSWIDTH_16
)
2232 bad
= bad
|| (nandc
->data_buffer
[bbpos
+ 1] != 0xff);
2237 static int qcom_nandc_block_markbad(struct mtd_info
*mtd
, loff_t ofs
)
2239 struct nand_chip
*chip
= mtd_to_nand(mtd
);
2240 struct qcom_nand_host
*host
= to_qcom_nand_host(chip
);
2241 struct qcom_nand_controller
*nandc
= get_qcom_nand_controller(chip
);
2242 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
2245 clear_read_regs(nandc
);
2246 clear_bam_transaction(nandc
);
2249 * to mark the BBM as bad, we flash the entire last codeword with 0s.
2250 * we don't care about the rest of the content in the codeword since
2251 * we aren't going to use this block again
2253 memset(nandc
->data_buffer
, 0x00, host
->cw_size
);
2255 page
= (int)(ofs
>> chip
->page_shift
) & chip
->pagemask
;
2258 host
->use_ecc
= false;
2259 set_address(host
, host
->cw_size
* (ecc
->steps
- 1), page
);
2260 update_rw_regs(host
, 1, false);
2262 config_nand_page_write(nandc
);
2263 write_data_dma(nandc
, FLASH_BUF_ACC
,
2264 nandc
->data_buffer
, host
->cw_size
, 0);
2265 config_nand_cw_write(nandc
);
2267 ret
= submit_descs(nandc
);
2272 dev_err(nandc
->dev
, "failure to update BBM\n");
2276 return nand_prog_page_end_op(chip
);
2280 * the three functions below implement chip->read_byte(), chip->read_buf()
2281 * and chip->write_buf() respectively. these aren't used for
2282 * reading/writing page data, they are used for smaller data like reading
2285 static uint8_t qcom_nandc_read_byte(struct mtd_info
*mtd
)
2287 struct nand_chip
*chip
= mtd_to_nand(mtd
);
2288 struct qcom_nand_host
*host
= to_qcom_nand_host(chip
);
2289 struct qcom_nand_controller
*nandc
= get_qcom_nand_controller(chip
);
2290 u8
*buf
= nandc
->data_buffer
;
2293 if (host
->last_command
== NAND_CMD_STATUS
) {
2296 host
->status
= NAND_STATUS_READY
| NAND_STATUS_WP
;
2301 if (nandc
->buf_start
< nandc
->buf_count
)
2302 ret
= buf
[nandc
->buf_start
++];
2307 static void qcom_nandc_read_buf(struct mtd_info
*mtd
, uint8_t *buf
, int len
)
2309 struct nand_chip
*chip
= mtd_to_nand(mtd
);
2310 struct qcom_nand_controller
*nandc
= get_qcom_nand_controller(chip
);
2311 int real_len
= min_t(size_t, len
, nandc
->buf_count
- nandc
->buf_start
);
2313 memcpy(buf
, nandc
->data_buffer
+ nandc
->buf_start
, real_len
);
2314 nandc
->buf_start
+= real_len
;
2317 static void qcom_nandc_write_buf(struct mtd_info
*mtd
, const uint8_t *buf
,
2320 struct nand_chip
*chip
= mtd_to_nand(mtd
);
2321 struct qcom_nand_controller
*nandc
= get_qcom_nand_controller(chip
);
2322 int real_len
= min_t(size_t, len
, nandc
->buf_count
- nandc
->buf_start
);
2324 memcpy(nandc
->data_buffer
+ nandc
->buf_start
, buf
, real_len
);
2326 nandc
->buf_start
+= real_len
;
2329 /* we support only one external chip for now */
2330 static void qcom_nandc_select_chip(struct mtd_info
*mtd
, int chipnr
)
2332 struct nand_chip
*chip
= mtd_to_nand(mtd
);
2333 struct qcom_nand_controller
*nandc
= get_qcom_nand_controller(chip
);
2338 dev_warn(nandc
->dev
, "invalid chip select\n");
2342 * NAND controller page layout info
2344 * Layout with ECC enabled:
2346 * |----------------------| |---------------------------------|
2347 * | xx.......yy| | *********xx.......yy|
2348 * | DATA xx..ECC..yy| | DATA **SPARE**xx..ECC..yy|
2349 * | (516) xx.......yy| | (516-n*4) **(n*4)**xx.......yy|
2350 * | xx.......yy| | *********xx.......yy|
2351 * |----------------------| |---------------------------------|
2352 * codeword 1,2..n-1 codeword n
2353 * <---(528/532 Bytes)--> <-------(528/532 Bytes)--------->
2355 * n = Number of codewords in the page
2357 * * = Spare/free bytes
2358 * x = Unused byte(s)
2359 * y = Reserved byte(s)
2361 * 2K page: n = 4, spare = 16 bytes
2362 * 4K page: n = 8, spare = 32 bytes
2363 * 8K page: n = 16, spare = 64 bytes
2365 * the qcom nand controller operates at a sub page/codeword level. each
2366 * codeword is 528 and 532 bytes for 4 bit and 8 bit ECC modes respectively.
2367 * the number of ECC bytes vary based on the ECC strength and the bus width.
2369 * the first n - 1 codewords contains 516 bytes of user data, the remaining
2370 * 12/16 bytes consist of ECC and reserved data. The nth codeword contains
2371 * both user data and spare(oobavail) bytes that sum up to 516 bytes.
2373 * When we access a page with ECC enabled, the reserved bytes(s) are not
2374 * accessible at all. When reading, we fill up these unreadable positions
2375 * with 0xffs. When writing, the controller skips writing the inaccessible
2378 * Layout with ECC disabled:
2380 * |------------------------------| |---------------------------------------|
2381 * | yy xx.......| | bb *********xx.......|
2382 * | DATA1 yy DATA2 xx..ECC..| | DATA1 bb DATA2 **SPARE**xx..ECC..|
2383 * | (size1) yy (size2) xx.......| | (size1) bb (size2) **(n*4)**xx.......|
2384 * | yy xx.......| | bb *********xx.......|
2385 * |------------------------------| |---------------------------------------|
2386 * codeword 1,2..n-1 codeword n
2387 * <-------(528/532 Bytes)------> <-----------(528/532 Bytes)----------->
2389 * n = Number of codewords in the page
2391 * * = Spare/free bytes
2392 * x = Unused byte(s)
2393 * y = Dummy Bad Bock byte(s)
2394 * b = Real Bad Block byte(s)
2395 * size1/size2 = function of codeword size and 'n'
2397 * when the ECC block is disabled, one reserved byte (or two for 16 bit bus
2398 * width) is now accessible. For the first n - 1 codewords, these are dummy Bad
2399 * Block Markers. In the last codeword, this position contains the real BBM
2401 * In order to have a consistent layout between RAW and ECC modes, we assume
2402 * the following OOB layout arrangement:
2404 * |-----------| |--------------------|
2405 * |yyxx.......| |bb*********xx.......|
2406 * |yyxx..ECC..| |bb*FREEOOB*xx..ECC..|
2407 * |yyxx.......| |bb*********xx.......|
2408 * |yyxx.......| |bb*********xx.......|
2409 * |-----------| |--------------------|
2410 * first n - 1 nth OOB region
2413 * n = Number of codewords in the page
2415 * * = FREE OOB bytes
2416 * y = Dummy bad block byte(s) (inaccessible when ECC enabled)
2417 * x = Unused byte(s)
2418 * b = Real bad block byte(s) (inaccessible when ECC enabled)
2420 * This layout is read as is when ECC is disabled. When ECC is enabled, the
2421 * inaccessible Bad Block byte(s) are ignored when we write to a page/oob,
2422 * and assumed as 0xffs when we read a page/oob. The ECC, unused and
2423 * dummy/real bad block bytes are grouped as ecc bytes (i.e, ecc->bytes is
2424 * the sum of the three).
2426 static int qcom_nand_ooblayout_ecc(struct mtd_info
*mtd
, int section
,
2427 struct mtd_oob_region
*oobregion
)
2429 struct nand_chip
*chip
= mtd_to_nand(mtd
);
2430 struct qcom_nand_host
*host
= to_qcom_nand_host(chip
);
2431 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
2437 oobregion
->length
= (ecc
->bytes
* (ecc
->steps
- 1)) +
2439 oobregion
->offset
= 0;
2441 oobregion
->length
= host
->ecc_bytes_hw
+ host
->spare_bytes
;
2442 oobregion
->offset
= mtd
->oobsize
- oobregion
->length
;
2448 static int qcom_nand_ooblayout_free(struct mtd_info
*mtd
, int section
,
2449 struct mtd_oob_region
*oobregion
)
2451 struct nand_chip
*chip
= mtd_to_nand(mtd
);
2452 struct qcom_nand_host
*host
= to_qcom_nand_host(chip
);
2453 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
2458 oobregion
->length
= ecc
->steps
* 4;
2459 oobregion
->offset
= ((ecc
->steps
- 1) * ecc
->bytes
) + host
->bbm_size
;
2464 static const struct mtd_ooblayout_ops qcom_nand_ooblayout_ops
= {
2465 .ecc
= qcom_nand_ooblayout_ecc
,
2466 .free
= qcom_nand_ooblayout_free
,
2470 qcom_nandc_calc_ecc_bytes(int step_size
, int strength
)
2472 return strength
== 4 ? 12 : 16;
2474 NAND_ECC_CAPS_SINGLE(qcom_nandc_ecc_caps
, qcom_nandc_calc_ecc_bytes
,
2475 NANDC_STEP_SIZE
, 4, 8);
2477 static int qcom_nand_attach_chip(struct nand_chip
*chip
)
2479 struct mtd_info
*mtd
= nand_to_mtd(chip
);
2480 struct qcom_nand_host
*host
= to_qcom_nand_host(chip
);
2481 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
2482 struct qcom_nand_controller
*nandc
= get_qcom_nand_controller(chip
);
2483 int cwperpage
, bad_block_byte
, ret
;
2487 /* controller only supports 512 bytes data steps */
2488 ecc
->size
= NANDC_STEP_SIZE
;
2489 wide_bus
= chip
->options
& NAND_BUSWIDTH_16
? true : false;
2490 cwperpage
= mtd
->writesize
/ NANDC_STEP_SIZE
;
2493 * Each CW has 4 available OOB bytes which will be protected with ECC
2494 * so remaining bytes can be used for ECC.
2496 ret
= nand_ecc_choose_conf(chip
, &qcom_nandc_ecc_caps
,
2497 mtd
->oobsize
- (cwperpage
* 4));
2499 dev_err(nandc
->dev
, "No valid ECC settings possible\n");
2503 if (ecc
->strength
>= 8) {
2504 /* 8 bit ECC defaults to BCH ECC on all platforms */
2505 host
->bch_enabled
= true;
2509 host
->ecc_bytes_hw
= 14;
2510 host
->spare_bytes
= 0;
2513 host
->ecc_bytes_hw
= 13;
2514 host
->spare_bytes
= 2;
2519 * if the controller supports BCH for 4 bit ECC, the controller
2520 * uses lesser bytes for ECC. If RS is used, the ECC bytes is
2523 if (nandc
->props
->ecc_modes
& ECC_BCH_4BIT
) {
2525 host
->bch_enabled
= true;
2529 host
->ecc_bytes_hw
= 8;
2530 host
->spare_bytes
= 2;
2533 host
->ecc_bytes_hw
= 7;
2534 host
->spare_bytes
= 4;
2539 host
->ecc_bytes_hw
= 10;
2542 host
->spare_bytes
= 0;
2545 host
->spare_bytes
= 1;
2552 * we consider ecc->bytes as the sum of all the non-data content in a
2553 * step. It gives us a clean representation of the oob area (even if
2554 * all the bytes aren't used for ECC).It is always 16 bytes for 8 bit
2555 * ECC and 12 bytes for 4 bit ECC
2557 ecc
->bytes
= host
->ecc_bytes_hw
+ host
->spare_bytes
+ host
->bbm_size
;
2559 ecc
->read_page
= qcom_nandc_read_page
;
2560 ecc
->read_page_raw
= qcom_nandc_read_page_raw
;
2561 ecc
->read_oob
= qcom_nandc_read_oob
;
2562 ecc
->write_page
= qcom_nandc_write_page
;
2563 ecc
->write_page_raw
= qcom_nandc_write_page_raw
;
2564 ecc
->write_oob
= qcom_nandc_write_oob
;
2566 ecc
->mode
= NAND_ECC_HW
;
2568 mtd_set_ooblayout(mtd
, &qcom_nand_ooblayout_ops
);
2570 nandc
->max_cwperpage
= max_t(unsigned int, nandc
->max_cwperpage
,
2574 * DATA_UD_BYTES varies based on whether the read/write command protects
2575 * spare data with ECC too. We protect spare data by default, so we set
2576 * it to main + spare data, which are 512 and 4 bytes respectively.
2578 host
->cw_data
= 516;
2581 * total bytes in a step, either 528 bytes for 4 bit ECC, or 532 bytes
2584 host
->cw_size
= host
->cw_data
+ ecc
->bytes
;
2585 bad_block_byte
= mtd
->writesize
- host
->cw_size
* (cwperpage
- 1) + 1;
2587 host
->cfg0
= (cwperpage
- 1) << CW_PER_PAGE
2588 | host
->cw_data
<< UD_SIZE_BYTES
2589 | 0 << DISABLE_STATUS_AFTER_WRITE
2590 | 5 << NUM_ADDR_CYCLES
2591 | host
->ecc_bytes_hw
<< ECC_PARITY_SIZE_BYTES_RS
2592 | 0 << STATUS_BFR_READ
2593 | 1 << SET_RD_MODE_AFTER_STATUS
2594 | host
->spare_bytes
<< SPARE_SIZE_BYTES
;
2596 host
->cfg1
= 7 << NAND_RECOVERY_CYCLES
2597 | 0 << CS_ACTIVE_BSY
2598 | bad_block_byte
<< BAD_BLOCK_BYTE_NUM
2599 | 0 << BAD_BLOCK_IN_SPARE_AREA
2600 | 2 << WR_RD_BSY_GAP
2601 | wide_bus
<< WIDE_FLASH
2602 | host
->bch_enabled
<< ENABLE_BCH_ECC
;
2604 host
->cfg0_raw
= (cwperpage
- 1) << CW_PER_PAGE
2605 | host
->cw_size
<< UD_SIZE_BYTES
2606 | 5 << NUM_ADDR_CYCLES
2607 | 0 << SPARE_SIZE_BYTES
;
2609 host
->cfg1_raw
= 7 << NAND_RECOVERY_CYCLES
2610 | 0 << CS_ACTIVE_BSY
2611 | 17 << BAD_BLOCK_BYTE_NUM
2612 | 1 << BAD_BLOCK_IN_SPARE_AREA
2613 | 2 << WR_RD_BSY_GAP
2614 | wide_bus
<< WIDE_FLASH
2615 | 1 << DEV0_CFG1_ECC_DISABLE
;
2617 host
->ecc_bch_cfg
= !host
->bch_enabled
<< ECC_CFG_ECC_DISABLE
2619 | host
->cw_data
<< ECC_NUM_DATA_BYTES
2620 | 1 << ECC_FORCE_CLK_OPEN
2621 | ecc_mode
<< ECC_MODE
2622 | host
->ecc_bytes_hw
<< ECC_PARITY_SIZE_BYTES_BCH
;
2624 host
->ecc_buf_cfg
= 0x203 << NUM_STEPS
;
2626 host
->clrflashstatus
= FS_READY_BSY_N
;
2627 host
->clrreadstatus
= 0xc0;
2628 nandc
->regs
->erased_cw_detect_cfg_clr
=
2629 cpu_to_le32(CLR_ERASED_PAGE_DET
);
2630 nandc
->regs
->erased_cw_detect_cfg_set
=
2631 cpu_to_le32(SET_ERASED_PAGE_DET
);
2634 "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",
2635 host
->cfg0
, host
->cfg1
, host
->ecc_buf_cfg
, host
->ecc_bch_cfg
,
2636 host
->cw_size
, host
->cw_data
, ecc
->strength
, ecc
->bytes
,
2642 static const struct nand_controller_ops qcom_nandc_ops
= {
2643 .attach_chip
= qcom_nand_attach_chip
,
2646 static int qcom_nandc_alloc(struct qcom_nand_controller
*nandc
)
2650 ret
= dma_set_coherent_mask(nandc
->dev
, DMA_BIT_MASK(32));
2652 dev_err(nandc
->dev
, "failed to set DMA mask\n");
2657 * we use the internal buffer for reading ONFI params, reading small
2658 * data like ID and status, and preforming read-copy-write operations
2659 * when writing to a codeword partially. 532 is the maximum possible
2660 * size of a codeword for our nand controller
2662 nandc
->buf_size
= 532;
2664 nandc
->data_buffer
= devm_kzalloc(nandc
->dev
, nandc
->buf_size
,
2666 if (!nandc
->data_buffer
)
2669 nandc
->regs
= devm_kzalloc(nandc
->dev
, sizeof(*nandc
->regs
),
2674 nandc
->reg_read_buf
= devm_kcalloc(nandc
->dev
,
2675 MAX_REG_RD
, sizeof(*nandc
->reg_read_buf
),
2677 if (!nandc
->reg_read_buf
)
2680 if (nandc
->props
->is_bam
) {
2681 nandc
->reg_read_dma
=
2682 dma_map_single(nandc
->dev
, nandc
->reg_read_buf
,
2684 sizeof(*nandc
->reg_read_buf
),
2686 if (dma_mapping_error(nandc
->dev
, nandc
->reg_read_dma
)) {
2687 dev_err(nandc
->dev
, "failed to DMA MAP reg buffer\n");
2691 nandc
->tx_chan
= dma_request_slave_channel(nandc
->dev
, "tx");
2692 if (!nandc
->tx_chan
) {
2693 dev_err(nandc
->dev
, "failed to request tx channel\n");
2697 nandc
->rx_chan
= dma_request_slave_channel(nandc
->dev
, "rx");
2698 if (!nandc
->rx_chan
) {
2699 dev_err(nandc
->dev
, "failed to request rx channel\n");
2703 nandc
->cmd_chan
= dma_request_slave_channel(nandc
->dev
, "cmd");
2704 if (!nandc
->cmd_chan
) {
2705 dev_err(nandc
->dev
, "failed to request cmd channel\n");
2710 * Initially allocate BAM transaction to read ONFI param page.
2711 * After detecting all the devices, this BAM transaction will
2712 * be freed and the next BAM tranasction will be allocated with
2713 * maximum codeword size
2715 nandc
->max_cwperpage
= 1;
2716 nandc
->bam_txn
= alloc_bam_transaction(nandc
);
2717 if (!nandc
->bam_txn
) {
2719 "failed to allocate bam transaction\n");
2723 nandc
->chan
= dma_request_slave_channel(nandc
->dev
, "rxtx");
2726 "failed to request slave channel\n");
2731 INIT_LIST_HEAD(&nandc
->desc_list
);
2732 INIT_LIST_HEAD(&nandc
->host_list
);
2734 nand_controller_init(&nandc
->controller
);
2735 nandc
->controller
.ops
= &qcom_nandc_ops
;
2740 static void qcom_nandc_unalloc(struct qcom_nand_controller
*nandc
)
2742 if (nandc
->props
->is_bam
) {
2743 if (!dma_mapping_error(nandc
->dev
, nandc
->reg_read_dma
))
2744 dma_unmap_single(nandc
->dev
, nandc
->reg_read_dma
,
2746 sizeof(*nandc
->reg_read_buf
),
2750 dma_release_channel(nandc
->tx_chan
);
2753 dma_release_channel(nandc
->rx_chan
);
2755 if (nandc
->cmd_chan
)
2756 dma_release_channel(nandc
->cmd_chan
);
2759 dma_release_channel(nandc
->chan
);
2763 /* one time setup of a few nand controller registers */
2764 static int qcom_nandc_setup(struct qcom_nand_controller
*nandc
)
2769 nandc_write(nandc
, SFLASHC_BURST_CFG
, 0);
2770 nandc_write(nandc
, dev_cmd_reg_addr(nandc
, NAND_DEV_CMD_VLD
),
2771 NAND_DEV_CMD_VLD_VAL
);
2773 /* enable ADM or BAM DMA */
2774 if (nandc
->props
->is_bam
) {
2775 nand_ctrl
= nandc_read(nandc
, NAND_CTRL
);
2776 nandc_write(nandc
, NAND_CTRL
, nand_ctrl
| BAM_MODE_EN
);
2778 nandc_write(nandc
, NAND_FLASH_CHIP_SELECT
, DM_EN
);
2781 /* save the original values of these registers */
2782 nandc
->cmd1
= nandc_read(nandc
, dev_cmd_reg_addr(nandc
, NAND_DEV_CMD1
));
2783 nandc
->vld
= NAND_DEV_CMD_VLD_VAL
;
2788 static int qcom_nand_host_init_and_register(struct qcom_nand_controller
*nandc
,
2789 struct qcom_nand_host
*host
,
2790 struct device_node
*dn
)
2792 struct nand_chip
*chip
= &host
->chip
;
2793 struct mtd_info
*mtd
= nand_to_mtd(chip
);
2794 struct device
*dev
= nandc
->dev
;
2797 ret
= of_property_read_u32(dn
, "reg", &host
->cs
);
2799 dev_err(dev
, "can't get chip-select\n");
2803 nand_set_flash_node(chip
, dn
);
2804 mtd
->name
= devm_kasprintf(dev
, GFP_KERNEL
, "qcom_nand.%d", host
->cs
);
2808 mtd
->owner
= THIS_MODULE
;
2809 mtd
->dev
.parent
= dev
;
2811 chip
->cmdfunc
= qcom_nandc_command
;
2812 chip
->select_chip
= qcom_nandc_select_chip
;
2813 chip
->read_byte
= qcom_nandc_read_byte
;
2814 chip
->read_buf
= qcom_nandc_read_buf
;
2815 chip
->write_buf
= qcom_nandc_write_buf
;
2816 chip
->set_features
= nand_get_set_features_notsupp
;
2817 chip
->get_features
= nand_get_set_features_notsupp
;
2820 * the bad block marker is readable only when we read the last codeword
2821 * of a page with ECC disabled. currently, the nand_base and nand_bbt
2822 * helpers don't allow us to read BB from a nand chip with ECC
2823 * disabled (MTD_OPS_PLACE_OOB is set by default). use the block_bad
2824 * and block_markbad helpers until we permanently switch to using
2825 * MTD_OPS_RAW for all drivers (with the help of badblockbits)
2827 chip
->block_bad
= qcom_nandc_block_bad
;
2828 chip
->block_markbad
= qcom_nandc_block_markbad
;
2830 chip
->controller
= &nandc
->controller
;
2831 chip
->options
|= NAND_NO_SUBPAGE_WRITE
| NAND_USE_BOUNCE_BUFFER
|
2834 /* set up initial status value */
2835 host
->status
= NAND_STATUS_READY
| NAND_STATUS_WP
;
2837 ret
= nand_scan(chip
, 1);
2841 if (nandc
->props
->is_bam
) {
2842 free_bam_transaction(nandc
);
2843 nandc
->bam_txn
= alloc_bam_transaction(nandc
);
2844 if (!nandc
->bam_txn
) {
2846 "failed to allocate bam transaction\n");
2851 ret
= mtd_device_register(mtd
, NULL
, 0);
2858 static int qcom_probe_nand_devices(struct qcom_nand_controller
*nandc
)
2860 struct device
*dev
= nandc
->dev
;
2861 struct device_node
*dn
= dev
->of_node
, *child
;
2862 struct qcom_nand_host
*host
;
2865 for_each_available_child_of_node(dn
, child
) {
2866 host
= devm_kzalloc(dev
, sizeof(*host
), GFP_KERNEL
);
2872 ret
= qcom_nand_host_init_and_register(nandc
, host
, child
);
2874 devm_kfree(dev
, host
);
2878 list_add_tail(&host
->node
, &nandc
->host_list
);
2881 if (list_empty(&nandc
->host_list
))
2887 /* parse custom DT properties here */
2888 static int qcom_nandc_parse_dt(struct platform_device
*pdev
)
2890 struct qcom_nand_controller
*nandc
= platform_get_drvdata(pdev
);
2891 struct device_node
*np
= nandc
->dev
->of_node
;
2894 if (!nandc
->props
->is_bam
) {
2895 ret
= of_property_read_u32(np
, "qcom,cmd-crci",
2898 dev_err(nandc
->dev
, "command CRCI unspecified\n");
2902 ret
= of_property_read_u32(np
, "qcom,data-crci",
2905 dev_err(nandc
->dev
, "data CRCI unspecified\n");
2913 static int qcom_nandc_probe(struct platform_device
*pdev
)
2915 struct qcom_nand_controller
*nandc
;
2916 const void *dev_data
;
2917 struct device
*dev
= &pdev
->dev
;
2918 struct resource
*res
;
2921 nandc
= devm_kzalloc(&pdev
->dev
, sizeof(*nandc
), GFP_KERNEL
);
2925 platform_set_drvdata(pdev
, nandc
);
2928 dev_data
= of_device_get_match_data(dev
);
2930 dev_err(&pdev
->dev
, "failed to get device data\n");
2934 nandc
->props
= dev_data
;
2936 nandc
->core_clk
= devm_clk_get(dev
, "core");
2937 if (IS_ERR(nandc
->core_clk
))
2938 return PTR_ERR(nandc
->core_clk
);
2940 nandc
->aon_clk
= devm_clk_get(dev
, "aon");
2941 if (IS_ERR(nandc
->aon_clk
))
2942 return PTR_ERR(nandc
->aon_clk
);
2944 ret
= qcom_nandc_parse_dt(pdev
);
2948 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2949 nandc
->base
= devm_ioremap_resource(dev
, res
);
2950 if (IS_ERR(nandc
->base
))
2951 return PTR_ERR(nandc
->base
);
2953 nandc
->base_phys
= res
->start
;
2954 nandc
->base_dma
= dma_map_resource(dev
, res
->start
,
2956 DMA_BIDIRECTIONAL
, 0);
2957 if (!nandc
->base_dma
)
2960 ret
= qcom_nandc_alloc(nandc
);
2962 goto err_nandc_alloc
;
2964 ret
= clk_prepare_enable(nandc
->core_clk
);
2968 ret
= clk_prepare_enable(nandc
->aon_clk
);
2972 ret
= qcom_nandc_setup(nandc
);
2976 ret
= qcom_probe_nand_devices(nandc
);
2983 clk_disable_unprepare(nandc
->aon_clk
);
2985 clk_disable_unprepare(nandc
->core_clk
);
2987 qcom_nandc_unalloc(nandc
);
2989 dma_unmap_resource(dev
, res
->start
, resource_size(res
),
2990 DMA_BIDIRECTIONAL
, 0);
2995 static int qcom_nandc_remove(struct platform_device
*pdev
)
2997 struct qcom_nand_controller
*nandc
= platform_get_drvdata(pdev
);
2998 struct resource
*res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2999 struct qcom_nand_host
*host
;
3001 list_for_each_entry(host
, &nandc
->host_list
, node
)
3002 nand_release(&host
->chip
);
3005 qcom_nandc_unalloc(nandc
);
3007 clk_disable_unprepare(nandc
->aon_clk
);
3008 clk_disable_unprepare(nandc
->core_clk
);
3010 dma_unmap_resource(&pdev
->dev
, nandc
->base_dma
, resource_size(res
),
3011 DMA_BIDIRECTIONAL
, 0);
3016 static const struct qcom_nandc_props ipq806x_nandc_props
= {
3017 .ecc_modes
= (ECC_RS_4BIT
| ECC_BCH_8BIT
),
3019 .dev_cmd_reg_start
= 0x0,
3022 static const struct qcom_nandc_props ipq4019_nandc_props
= {
3023 .ecc_modes
= (ECC_BCH_4BIT
| ECC_BCH_8BIT
),
3025 .dev_cmd_reg_start
= 0x0,
3028 static const struct qcom_nandc_props ipq8074_nandc_props
= {
3029 .ecc_modes
= (ECC_BCH_4BIT
| ECC_BCH_8BIT
),
3031 .dev_cmd_reg_start
= 0x7000,
3035 * data will hold a struct pointer containing more differences once we support
3036 * more controller variants
3038 static const struct of_device_id qcom_nandc_of_match
[] = {
3040 .compatible
= "qcom,ipq806x-nand",
3041 .data
= &ipq806x_nandc_props
,
3044 .compatible
= "qcom,ipq4019-nand",
3045 .data
= &ipq4019_nandc_props
,
3048 .compatible
= "qcom,ipq8074-nand",
3049 .data
= &ipq8074_nandc_props
,
3053 MODULE_DEVICE_TABLE(of
, qcom_nandc_of_match
);
3055 static struct platform_driver qcom_nandc_driver
= {
3057 .name
= "qcom-nandc",
3058 .of_match_table
= qcom_nandc_of_match
,
3060 .probe
= qcom_nandc_probe
,
3061 .remove
= qcom_nandc_remove
,
3063 module_platform_driver(qcom_nandc_driver
);
3065 MODULE_AUTHOR("Archit Taneja <architt@codeaurora.org>");
3066 MODULE_DESCRIPTION("Qualcomm NAND Controller driver");
3067 MODULE_LICENSE("GPL v2");