1 // SPDX-License-Identifier: GPL-2.0+
3 * Freescale i.MX28 NAND flash driver
5 * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
6 * on behalf of DENX Software Engineering GmbH
8 * Based on code from LTIB:
9 * Freescale GPMI NFC NAND Flash Driver
11 * Copyright (C) 2010 Freescale Semiconductor, Inc.
12 * Copyright (C) 2008 Embedded Alley Solutions, Inc.
13 * Copyright 2017-2019 NXP
19 #include <linux/mtd/rawnand.h>
20 #include <linux/sizes.h>
21 #include <linux/types.h>
23 #include <linux/errno.h>
25 #include <asm/arch/clock.h>
26 #include <asm/arch/imx-regs.h>
27 #include <asm/mach-imx/regs-bch.h>
28 #include <asm/mach-imx/regs-gpmi.h>
29 #include <asm/arch/sys_proto.h>
32 #define MXS_NAND_DMA_DESCRIPTOR_COUNT 4
34 #if (defined(CONFIG_MX6) || defined(CONFIG_MX7))
35 #define MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT 2
37 #define MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT 0
39 #define MXS_NAND_METADATA_SIZE 10
40 #define MXS_NAND_BITS_PER_ECC_LEVEL 13
42 #if !defined(CONFIG_SYS_CACHELINE_SIZE) || CONFIG_SYS_CACHELINE_SIZE < 32
43 #define MXS_NAND_COMMAND_BUFFER_SIZE 32
45 #define MXS_NAND_COMMAND_BUFFER_SIZE CONFIG_SYS_CACHELINE_SIZE
48 #define MXS_NAND_BCH_TIMEOUT 10000
50 struct nand_ecclayout fake_ecc_layout;
53 * Cache management functions
55 #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
56 static void mxs_nand_flush_data_buf(struct mxs_nand_info *info)
58 uint32_t addr = (uint32_t)info->data_buf;
60 flush_dcache_range(addr, addr + info->data_buf_size);
63 static void mxs_nand_inval_data_buf(struct mxs_nand_info *info)
65 uint32_t addr = (uint32_t)info->data_buf;
67 invalidate_dcache_range(addr, addr + info->data_buf_size);
70 static void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info)
72 uint32_t addr = (uint32_t)info->cmd_buf;
74 flush_dcache_range(addr, addr + MXS_NAND_COMMAND_BUFFER_SIZE);
77 static inline void mxs_nand_flush_data_buf(struct mxs_nand_info *info) {}
78 static inline void mxs_nand_inval_data_buf(struct mxs_nand_info *info) {}
79 static inline void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info) {}
82 static struct mxs_dma_desc *mxs_nand_get_dma_desc(struct mxs_nand_info *info)
84 struct mxs_dma_desc *desc;
86 if (info->desc_index >= MXS_NAND_DMA_DESCRIPTOR_COUNT) {
87 printf("MXS NAND: Too many DMA descriptors requested\n");
91 desc = info->desc[info->desc_index];
97 static void mxs_nand_return_dma_descs(struct mxs_nand_info *info)
100 struct mxs_dma_desc *desc;
102 for (i = 0; i < info->desc_index; i++) {
103 desc = info->desc[i];
104 memset(desc, 0, sizeof(struct mxs_dma_desc));
105 desc->address = (dma_addr_t)desc;
108 info->desc_index = 0;
111 static uint32_t mxs_nand_aux_status_offset(void)
113 return (MXS_NAND_METADATA_SIZE + 0x3) & ~0x3;
116 static inline bool mxs_nand_bbm_in_data_chunk(struct bch_geometry *geo, struct mtd_info *mtd,
117 unsigned int *chunk_num)
121 if (geo->ecc_chunk0_size != geo->ecc_chunkn_size) {
122 dev_err(this->dev, "The size of chunk0 must equal to chunkn\n");
126 i = (mtd->writesize * 8 - MXS_NAND_METADATA_SIZE * 8) /
127 (geo->gf_len * geo->ecc_strength +
128 geo->ecc_chunkn_size * 8);
130 j = (mtd->writesize * 8 - MXS_NAND_METADATA_SIZE * 8) -
131 (geo->gf_len * geo->ecc_strength +
132 geo->ecc_chunkn_size * 8) * i;
134 if (j < geo->ecc_chunkn_size * 8) {
136 dev_dbg(this->dev, "Set ecc to %d and bbm in chunk %d\n",
137 geo->ecc_strength, *chunk_num);
144 static inline int mxs_nand_calc_ecc_layout_by_info(struct bch_geometry *geo,
145 struct mtd_info *mtd,
146 unsigned int ecc_strength,
147 unsigned int ecc_step)
149 struct nand_chip *chip = mtd_to_nand(mtd);
150 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
151 unsigned int block_mark_bit_offset;
164 geo->ecc_chunk0_size = ecc_step;
165 geo->ecc_chunkn_size = ecc_step;
166 geo->ecc_strength = round_up(ecc_strength, 2);
168 /* Keep the C >= O */
169 if (geo->ecc_chunkn_size < mtd->oobsize)
172 if (geo->ecc_strength > nand_info->max_ecc_strength_supported)
175 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunkn_size;
178 block_mark_bit_offset = mtd->writesize * 8 -
179 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
180 + MXS_NAND_METADATA_SIZE * 8);
182 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
183 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
188 static inline int mxs_nand_legacy_calc_ecc_layout(struct bch_geometry *geo,
189 struct mtd_info *mtd)
191 struct nand_chip *chip = mtd_to_nand(mtd);
192 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
193 unsigned int block_mark_bit_offset;
195 /* The default for the length of Galois Field. */
198 /* The default for chunk size. */
199 geo->ecc_chunk0_size = 512;
200 geo->ecc_chunkn_size = 512;
202 if (geo->ecc_chunkn_size < mtd->oobsize) {
204 geo->ecc_chunk0_size *= 2;
205 geo->ecc_chunkn_size *= 2;
208 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunkn_size;
211 * Determine the ECC layout with the formula:
212 * ECC bits per chunk = (total page spare data bits) /
213 * (bits per ECC level) / (chunks per page)
215 * total page spare data bits =
216 * (page oob size - meta data size) * (bits per byte)
218 geo->ecc_strength = ((mtd->oobsize - MXS_NAND_METADATA_SIZE) * 8)
219 / (geo->gf_len * geo->ecc_chunk_count);
221 geo->ecc_strength = min(round_down(geo->ecc_strength, 2),
222 nand_info->max_ecc_strength_supported);
224 block_mark_bit_offset = mtd->writesize * 8 -
225 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
226 + MXS_NAND_METADATA_SIZE * 8);
228 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
229 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
234 static inline int mxs_nand_calc_ecc_for_large_oob(struct bch_geometry *geo,
235 struct mtd_info *mtd)
237 struct nand_chip *chip = mtd_to_nand(mtd);
238 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
239 unsigned int block_mark_bit_offset;
240 unsigned int max_ecc;
241 unsigned int bbm_chunk;
244 /* sanity check for the minimum ecc nand required */
245 if (!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0))
247 geo->ecc_strength = chip->ecc_strength_ds;
249 /* calculate the maximum ecc platform can support*/
251 geo->ecc_chunk0_size = 1024;
252 geo->ecc_chunkn_size = 1024;
253 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunkn_size;
254 max_ecc = ((mtd->oobsize - MXS_NAND_METADATA_SIZE) * 8)
255 / (geo->gf_len * geo->ecc_chunk_count);
256 max_ecc = min(round_down(max_ecc, 2),
257 nand_info->max_ecc_strength_supported);
260 /* search a supported ecc strength that makes bbm */
261 /* located in data chunk */
262 geo->ecc_strength = chip->ecc_strength_ds;
263 while (!(geo->ecc_strength > max_ecc)) {
264 if (mxs_nand_bbm_in_data_chunk(geo, mtd, &bbm_chunk))
266 geo->ecc_strength += 2;
269 /* if none of them works, keep using the minimum ecc */
270 /* nand required but changing ecc page layout */
271 if (geo->ecc_strength > max_ecc) {
272 geo->ecc_strength = chip->ecc_strength_ds;
273 /* add extra ecc for meta data */
274 geo->ecc_chunk0_size = 0;
275 geo->ecc_chunk_count = (mtd->writesize / geo->ecc_chunkn_size) + 1;
276 geo->ecc_for_meta = 1;
277 /* check if oob can afford this extra ecc chunk */
278 if (mtd->oobsize * 8 < MXS_NAND_METADATA_SIZE * 8 +
279 geo->gf_len * geo->ecc_strength
280 * geo->ecc_chunk_count) {
281 printf("unsupported NAND chip with new layout\n");
285 /* calculate in which chunk bbm located */
286 bbm_chunk = (mtd->writesize * 8 - MXS_NAND_METADATA_SIZE * 8 -
287 geo->gf_len * geo->ecc_strength) /
288 (geo->gf_len * geo->ecc_strength +
289 geo->ecc_chunkn_size * 8) + 1;
292 /* calculate the number of ecc chunk behind the bbm */
293 i = (mtd->writesize / geo->ecc_chunkn_size) - bbm_chunk + 1;
295 block_mark_bit_offset = mtd->writesize * 8 -
296 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - i)
297 + MXS_NAND_METADATA_SIZE * 8);
299 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
300 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
306 * Wait for BCH complete IRQ and clear the IRQ
308 static int mxs_nand_wait_for_bch_complete(struct mxs_nand_info *nand_info)
310 int timeout = MXS_NAND_BCH_TIMEOUT;
313 ret = mxs_wait_mask_set(&nand_info->bch_regs->hw_bch_ctrl_reg,
314 BCH_CTRL_COMPLETE_IRQ, timeout);
316 writel(BCH_CTRL_COMPLETE_IRQ, &nand_info->bch_regs->hw_bch_ctrl_clr);
322 * This is the function that we install in the cmd_ctrl function pointer of the
323 * owning struct nand_chip. The only functions in the reference implementation
324 * that use these functions pointers are cmdfunc and select_chip.
326 * In this driver, we implement our own select_chip, so this function will only
327 * be called by the reference implementation's cmdfunc. For this reason, we can
328 * ignore the chip enable bit and concentrate only on sending bytes to the NAND
331 static void mxs_nand_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
333 struct nand_chip *nand = mtd_to_nand(mtd);
334 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
335 struct mxs_dma_desc *d;
336 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
340 * If this condition is true, something is _VERY_ wrong in MTD
343 if (nand_info->cmd_queue_len == MXS_NAND_COMMAND_BUFFER_SIZE) {
344 printf("MXS NAND: Command queue too long\n");
349 * Every operation begins with a command byte and a series of zero or
350 * more address bytes. These are distinguished by either the Address
351 * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
352 * asserted. When MTD is ready to execute the command, it will
353 * deasert both latch enables.
355 * Rather than run a separate DMA operation for every single byte, we
356 * queue them up and run a single DMA operation for the entire series
357 * of command and data bytes.
359 if (ctrl & (NAND_ALE | NAND_CLE)) {
360 if (data != NAND_CMD_NONE)
361 nand_info->cmd_buf[nand_info->cmd_queue_len++] = data;
366 * If control arrives here, MTD has deasserted both the ALE and CLE,
367 * which means it's ready to run an operation. Check if we have any
370 if (nand_info->cmd_queue_len == 0)
373 /* Compile the DMA descriptor -- a descriptor that sends command. */
374 d = mxs_nand_get_dma_desc(nand_info);
376 MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ |
377 MXS_DMA_DESC_CHAIN | MXS_DMA_DESC_DEC_SEM |
378 MXS_DMA_DESC_WAIT4END | (3 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
379 (nand_info->cmd_queue_len << MXS_DMA_DESC_BYTES_OFFSET);
381 d->cmd.address = (dma_addr_t)nand_info->cmd_buf;
383 d->cmd.pio_words[0] =
384 GPMI_CTRL0_COMMAND_MODE_WRITE |
385 GPMI_CTRL0_WORD_LENGTH |
386 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
387 GPMI_CTRL0_ADDRESS_NAND_CLE |
388 GPMI_CTRL0_ADDRESS_INCREMENT |
389 nand_info->cmd_queue_len;
391 mxs_dma_desc_append(channel, d);
394 mxs_nand_flush_cmd_buf(nand_info);
396 /* Execute the DMA chain. */
397 ret = mxs_dma_go(channel);
399 printf("MXS NAND: Error sending command\n");
401 mxs_nand_return_dma_descs(nand_info);
403 /* Reset the command queue. */
404 nand_info->cmd_queue_len = 0;
408 * Test if the NAND flash is ready.
410 static int mxs_nand_device_ready(struct mtd_info *mtd)
412 struct nand_chip *chip = mtd_to_nand(mtd);
413 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
416 tmp = readl(&nand_info->gpmi_regs->hw_gpmi_stat);
417 tmp >>= (GPMI_STAT_READY_BUSY_OFFSET + nand_info->cur_chip);
423 * Select the NAND chip.
425 static void mxs_nand_select_chip(struct mtd_info *mtd, int chip)
427 struct nand_chip *nand = mtd_to_nand(mtd);
428 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
430 nand_info->cur_chip = chip;
434 * Handle block mark swapping.
436 * Note that, when this function is called, it doesn't know whether it's
437 * swapping the block mark, or swapping it *back* -- but it doesn't matter
438 * because the the operation is the same.
440 static void mxs_nand_swap_block_mark(struct bch_geometry *geo,
441 uint8_t *data_buf, uint8_t *oob_buf)
443 uint32_t bit_offset = geo->block_mark_bit_offset;
444 uint32_t buf_offset = geo->block_mark_byte_offset;
450 * Get the byte from the data area that overlays the block mark. Since
451 * the ECC engine applies its own view to the bits in the page, the
452 * physical block mark won't (in general) appear on a byte boundary in
455 src = data_buf[buf_offset] >> bit_offset;
456 src |= data_buf[buf_offset + 1] << (8 - bit_offset);
462 data_buf[buf_offset] &= ~(0xff << bit_offset);
463 data_buf[buf_offset + 1] &= 0xff << bit_offset;
465 data_buf[buf_offset] |= dst << bit_offset;
466 data_buf[buf_offset + 1] |= dst >> (8 - bit_offset);
470 * Read data from NAND.
472 static void mxs_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int length)
474 struct nand_chip *nand = mtd_to_nand(mtd);
475 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
476 struct mxs_dma_desc *d;
477 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
480 if (length > NAND_MAX_PAGESIZE) {
481 printf("MXS NAND: DMA buffer too big\n");
486 printf("MXS NAND: DMA buffer is NULL\n");
490 /* Compile the DMA descriptor - a descriptor that reads data. */
491 d = mxs_nand_get_dma_desc(nand_info);
493 MXS_DMA_DESC_COMMAND_DMA_WRITE | MXS_DMA_DESC_IRQ |
494 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
495 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
496 (length << MXS_DMA_DESC_BYTES_OFFSET);
498 d->cmd.address = (dma_addr_t)nand_info->data_buf;
500 d->cmd.pio_words[0] =
501 GPMI_CTRL0_COMMAND_MODE_READ |
502 GPMI_CTRL0_WORD_LENGTH |
503 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
504 GPMI_CTRL0_ADDRESS_NAND_DATA |
507 mxs_dma_desc_append(channel, d);
510 * A DMA descriptor that waits for the command to end and the chip to
513 * I think we actually should *not* be waiting for the chip to become
514 * ready because, after all, we don't care. I think the original code
515 * did that and no one has re-thought it yet.
517 d = mxs_nand_get_dma_desc(nand_info);
519 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
520 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_DEC_SEM |
521 MXS_DMA_DESC_WAIT4END | (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
525 d->cmd.pio_words[0] =
526 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
527 GPMI_CTRL0_WORD_LENGTH |
528 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
529 GPMI_CTRL0_ADDRESS_NAND_DATA;
531 mxs_dma_desc_append(channel, d);
533 /* Invalidate caches */
534 mxs_nand_inval_data_buf(nand_info);
536 /* Execute the DMA chain. */
537 ret = mxs_dma_go(channel);
539 printf("MXS NAND: DMA read error\n");
543 /* Invalidate caches */
544 mxs_nand_inval_data_buf(nand_info);
546 memcpy(buf, nand_info->data_buf, length);
549 mxs_nand_return_dma_descs(nand_info);
553 * Write data to NAND.
555 static void mxs_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf,
558 struct nand_chip *nand = mtd_to_nand(mtd);
559 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
560 struct mxs_dma_desc *d;
561 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
564 if (length > NAND_MAX_PAGESIZE) {
565 printf("MXS NAND: DMA buffer too big\n");
570 printf("MXS NAND: DMA buffer is NULL\n");
574 memcpy(nand_info->data_buf, buf, length);
576 /* Compile the DMA descriptor - a descriptor that writes data. */
577 d = mxs_nand_get_dma_desc(nand_info);
579 MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ |
580 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
581 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
582 (length << MXS_DMA_DESC_BYTES_OFFSET);
584 d->cmd.address = (dma_addr_t)nand_info->data_buf;
586 d->cmd.pio_words[0] =
587 GPMI_CTRL0_COMMAND_MODE_WRITE |
588 GPMI_CTRL0_WORD_LENGTH |
589 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
590 GPMI_CTRL0_ADDRESS_NAND_DATA |
593 mxs_dma_desc_append(channel, d);
596 mxs_nand_flush_data_buf(nand_info);
598 /* Execute the DMA chain. */
599 ret = mxs_dma_go(channel);
601 printf("MXS NAND: DMA write error\n");
603 mxs_nand_return_dma_descs(nand_info);
607 * Read a single byte from NAND.
609 static uint8_t mxs_nand_read_byte(struct mtd_info *mtd)
612 mxs_nand_read_buf(mtd, &buf, 1);
616 static bool mxs_nand_erased_page(struct mtd_info *mtd, struct nand_chip *nand,
617 u8 *buf, int chunk, int page)
619 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
620 struct bch_geometry *geo = &nand_info->bch_geometry;
621 unsigned int flip_bits = 0, flip_bits_noecc = 0;
622 unsigned int threshold;
623 unsigned int base = geo->ecc_chunkn_size * chunk;
624 u32 *dma_buf = (u32 *)buf;
627 threshold = geo->gf_len / 2;
628 if (threshold > geo->ecc_strength)
629 threshold = geo->ecc_strength;
631 for (i = 0; i < geo->ecc_chunkn_size; i++) {
632 flip_bits += hweight8(~buf[base + i]);
633 if (flip_bits > threshold)
637 nand->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
638 nand->read_buf(mtd, buf, mtd->writesize);
640 for (i = 0; i < mtd->writesize / 4; i++) {
641 flip_bits_noecc += hweight32(~dma_buf[i]);
642 if (flip_bits_noecc > threshold)
646 mtd->ecc_stats.corrected += flip_bits;
648 memset(buf, 0xff, mtd->writesize);
650 printf("The page(%d) is an erased page(%d,%d,%d,%d).\n", page, chunk, threshold, flip_bits, flip_bits_noecc);
656 * Read a page from NAND.
658 static int mxs_nand_ecc_read_page(struct mtd_info *mtd, struct nand_chip *nand,
659 uint8_t *buf, int oob_required,
662 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
663 struct bch_geometry *geo = &nand_info->bch_geometry;
664 struct mxs_bch_regs *bch_regs = nand_info->bch_regs;
665 struct mxs_dma_desc *d;
666 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
667 uint32_t corrected = 0, failed = 0;
672 /* Compile the DMA descriptor - wait for ready. */
673 d = mxs_nand_get_dma_desc(nand_info);
675 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
676 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
677 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
681 d->cmd.pio_words[0] =
682 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
683 GPMI_CTRL0_WORD_LENGTH |
684 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
685 GPMI_CTRL0_ADDRESS_NAND_DATA;
687 mxs_dma_desc_append(channel, d);
689 /* Compile the DMA descriptor - enable the BCH block and read. */
690 d = mxs_nand_get_dma_desc(nand_info);
692 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
693 MXS_DMA_DESC_WAIT4END | (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
697 d->cmd.pio_words[0] =
698 GPMI_CTRL0_COMMAND_MODE_READ |
699 GPMI_CTRL0_WORD_LENGTH |
700 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
701 GPMI_CTRL0_ADDRESS_NAND_DATA |
702 (mtd->writesize + mtd->oobsize);
703 d->cmd.pio_words[1] = 0;
704 d->cmd.pio_words[2] =
705 GPMI_ECCCTRL_ENABLE_ECC |
706 GPMI_ECCCTRL_ECC_CMD_DECODE |
707 GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
708 d->cmd.pio_words[3] = mtd->writesize + mtd->oobsize;
709 d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
710 d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;
712 mxs_dma_desc_append(channel, d);
714 /* Compile the DMA descriptor - disable the BCH block. */
715 d = mxs_nand_get_dma_desc(nand_info);
717 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
718 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
719 (3 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
723 d->cmd.pio_words[0] =
724 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
725 GPMI_CTRL0_WORD_LENGTH |
726 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
727 GPMI_CTRL0_ADDRESS_NAND_DATA |
728 (mtd->writesize + mtd->oobsize);
729 d->cmd.pio_words[1] = 0;
730 d->cmd.pio_words[2] = 0;
732 mxs_dma_desc_append(channel, d);
734 /* Compile the DMA descriptor - deassert the NAND lock and interrupt. */
735 d = mxs_nand_get_dma_desc(nand_info);
737 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
738 MXS_DMA_DESC_DEC_SEM;
742 mxs_dma_desc_append(channel, d);
744 /* Invalidate caches */
745 mxs_nand_inval_data_buf(nand_info);
747 /* Execute the DMA chain. */
748 ret = mxs_dma_go(channel);
750 printf("MXS NAND: DMA read error\n");
754 ret = mxs_nand_wait_for_bch_complete(nand_info);
756 printf("MXS NAND: BCH read timeout\n");
760 mxs_nand_return_dma_descs(nand_info);
762 /* Invalidate caches */
763 mxs_nand_inval_data_buf(nand_info);
765 /* Read DMA completed, now do the mark swapping. */
766 mxs_nand_swap_block_mark(geo, nand_info->data_buf, nand_info->oob_buf);
768 /* Loop over status bytes, accumulating ECC status. */
769 status = nand_info->oob_buf + mxs_nand_aux_status_offset();
770 for (i = 0; i < geo->ecc_chunk_count; i++) {
771 if (status[i] == 0x00)
774 if (status[i] == 0xff) {
775 if (is_mx6dqp() || is_mx7() ||
777 if (readl(&bch_regs->hw_bch_debug1))
782 if (status[i] == 0xfe) {
783 if (mxs_nand_erased_page(mtd, nand,
784 nand_info->data_buf, i, page))
790 corrected += status[i];
793 /* Propagate ECC status to the owning MTD. */
794 mtd->ecc_stats.failed += failed;
795 mtd->ecc_stats.corrected += corrected;
798 * It's time to deliver the OOB bytes. See mxs_nand_ecc_read_oob() for
799 * details about our policy for delivering the OOB.
801 * We fill the caller's buffer with set bits, and then copy the block
802 * mark to the caller's buffer. Note that, if block mark swapping was
803 * necessary, it has already been done, so we can rely on the first
804 * byte of the auxiliary buffer to contain the block mark.
806 memset(nand->oob_poi, 0xff, mtd->oobsize);
808 nand->oob_poi[0] = nand_info->oob_buf[0];
810 memcpy(buf, nand_info->data_buf, mtd->writesize);
813 memset(buf, 0xff, mtd->writesize);
815 mxs_nand_return_dma_descs(nand_info);
821 * Write a page to NAND.
823 static int mxs_nand_ecc_write_page(struct mtd_info *mtd,
824 struct nand_chip *nand, const uint8_t *buf,
825 int oob_required, int page)
827 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
828 struct bch_geometry *geo = &nand_info->bch_geometry;
829 struct mxs_dma_desc *d;
830 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
833 memcpy(nand_info->data_buf, buf, mtd->writesize);
834 memcpy(nand_info->oob_buf, nand->oob_poi, mtd->oobsize);
836 /* Handle block mark swapping. */
837 mxs_nand_swap_block_mark(geo, nand_info->data_buf, nand_info->oob_buf);
839 /* Compile the DMA descriptor - write data. */
840 d = mxs_nand_get_dma_desc(nand_info);
842 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
843 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
844 (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
848 d->cmd.pio_words[0] =
849 GPMI_CTRL0_COMMAND_MODE_WRITE |
850 GPMI_CTRL0_WORD_LENGTH |
851 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
852 GPMI_CTRL0_ADDRESS_NAND_DATA;
853 d->cmd.pio_words[1] = 0;
854 d->cmd.pio_words[2] =
855 GPMI_ECCCTRL_ENABLE_ECC |
856 GPMI_ECCCTRL_ECC_CMD_ENCODE |
857 GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
858 d->cmd.pio_words[3] = (mtd->writesize + mtd->oobsize);
859 d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
860 d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;
862 if (is_mx7() && nand_info->en_randomizer) {
863 d->cmd.pio_words[2] |= GPMI_ECCCTRL_RANDOMIZER_ENABLE |
864 GPMI_ECCCTRL_RANDOMIZER_TYPE2;
866 * Write NAND page number needed to be randomized
867 * to GPMI_ECCCOUNT register.
869 * The value is between 0-255. For additional details
870 * check 9.6.6.4 of i.MX7D Applications Processor reference
872 d->cmd.pio_words[3] |= (page % 255) << 16;
875 mxs_dma_desc_append(channel, d);
878 mxs_nand_flush_data_buf(nand_info);
880 /* Execute the DMA chain. */
881 ret = mxs_dma_go(channel);
883 printf("MXS NAND: DMA write error\n");
887 ret = mxs_nand_wait_for_bch_complete(nand_info);
889 printf("MXS NAND: BCH write timeout\n");
894 mxs_nand_return_dma_descs(nand_info);
899 * Read OOB from NAND.
901 * This function is a veneer that replaces the function originally installed by
902 * the NAND Flash MTD code.
904 static int mxs_nand_hook_read_oob(struct mtd_info *mtd, loff_t from,
905 struct mtd_oob_ops *ops)
907 struct nand_chip *chip = mtd_to_nand(mtd);
908 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
911 if (ops->mode == MTD_OPS_RAW)
912 nand_info->raw_oob_mode = 1;
914 nand_info->raw_oob_mode = 0;
916 ret = nand_info->hooked_read_oob(mtd, from, ops);
918 nand_info->raw_oob_mode = 0;
926 * This function is a veneer that replaces the function originally installed by
927 * the NAND Flash MTD code.
929 static int mxs_nand_hook_write_oob(struct mtd_info *mtd, loff_t to,
930 struct mtd_oob_ops *ops)
932 struct nand_chip *chip = mtd_to_nand(mtd);
933 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
936 if (ops->mode == MTD_OPS_RAW)
937 nand_info->raw_oob_mode = 1;
939 nand_info->raw_oob_mode = 0;
941 ret = nand_info->hooked_write_oob(mtd, to, ops);
943 nand_info->raw_oob_mode = 0;
949 * Mark a block bad in NAND.
951 * This function is a veneer that replaces the function originally installed by
952 * the NAND Flash MTD code.
954 static int mxs_nand_hook_block_markbad(struct mtd_info *mtd, loff_t ofs)
956 struct nand_chip *chip = mtd_to_nand(mtd);
957 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
960 nand_info->marking_block_bad = 1;
962 ret = nand_info->hooked_block_markbad(mtd, ofs);
964 nand_info->marking_block_bad = 0;
970 * There are several places in this driver where we have to handle the OOB and
971 * block marks. This is the function where things are the most complicated, so
972 * this is where we try to explain it all. All the other places refer back to
975 * These are the rules, in order of decreasing importance:
977 * 1) Nothing the caller does can be allowed to imperil the block mark, so all
978 * write operations take measures to protect it.
980 * 2) In read operations, the first byte of the OOB we return must reflect the
981 * true state of the block mark, no matter where that block mark appears in
984 * 3) ECC-based read operations return an OOB full of set bits (since we never
985 * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
988 * 4) "Raw" read operations return a direct view of the physical bytes in the
989 * page, using the conventional definition of which bytes are data and which
990 * are OOB. This gives the caller a way to see the actual, physical bytes
991 * in the page, without the distortions applied by our ECC engine.
993 * What we do for this specific read operation depends on whether we're doing
994 * "raw" read, or an ECC-based read.
996 * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
997 * easy. When reading a page, for example, the NAND Flash MTD code calls our
998 * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
999 * ECC-based or raw view of the page is implicit in which function it calls
1000 * (there is a similar pair of ECC-based/raw functions for writing).
1002 * Since MTD assumes the OOB is not covered by ECC, there is no pair of
1003 * ECC-based/raw functions for reading or or writing the OOB. The fact that the
1004 * caller wants an ECC-based or raw view of the page is not propagated down to
1007 * Since our OOB *is* covered by ECC, we need this information. So, we hook the
1008 * ecc.read_oob and ecc.write_oob function pointers in the owning
1009 * struct mtd_info with our own functions. These hook functions set the
1010 * raw_oob_mode field so that, when control finally arrives here, we'll know
1013 static int mxs_nand_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *nand,
1016 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
1019 * First, fill in the OOB buffer. If we're doing a raw read, we need to
1020 * get the bytes from the physical page. If we're not doing a raw read,
1021 * we need to fill the buffer with set bits.
1023 if (nand_info->raw_oob_mode) {
1025 * If control arrives here, we're doing a "raw" read. Send the
1026 * command to read the conventional OOB and read it.
1028 nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1029 nand->read_buf(mtd, nand->oob_poi, mtd->oobsize);
1032 * If control arrives here, we're not doing a "raw" read. Fill
1033 * the OOB buffer with set bits and correct the block mark.
1035 memset(nand->oob_poi, 0xff, mtd->oobsize);
1037 nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1038 mxs_nand_read_buf(mtd, nand->oob_poi, 1);
1046 * Write OOB data to NAND.
1048 static int mxs_nand_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *nand,
1051 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
1052 uint8_t block_mark = 0;
1055 * There are fundamental incompatibilities between the i.MX GPMI NFC and
1056 * the NAND Flash MTD model that make it essentially impossible to write
1057 * the out-of-band bytes.
1059 * We permit *ONE* exception. If the *intent* of writing the OOB is to
1060 * mark a block bad, we can do that.
1063 if (!nand_info->marking_block_bad) {
1064 printf("NXS NAND: Writing OOB isn't supported\n");
1068 /* Write the block mark. */
1069 nand->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1070 nand->write_buf(mtd, &block_mark, 1);
1071 nand->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1073 /* Check if it worked. */
1074 if (nand->waitfunc(mtd, nand) & NAND_STATUS_FAIL)
1081 * Claims all blocks are good.
1083 * In principle, this function is *only* called when the NAND Flash MTD system
1084 * isn't allowed to keep an in-memory bad block table, so it is forced to ask
1085 * the driver for bad block information.
1087 * In fact, we permit the NAND Flash MTD system to have an in-memory BBT, so
1088 * this function is *only* called when we take it away.
1090 * Thus, this function is only called when we want *all* blocks to look good,
1091 * so it *always* return success.
1093 static int mxs_nand_block_bad(struct mtd_info *mtd, loff_t ofs)
1098 static int mxs_nand_set_geometry(struct mtd_info *mtd, struct bch_geometry *geo)
1100 struct nand_chip *chip = mtd_to_nand(mtd);
1101 struct nand_chip *nand = mtd_to_nand(mtd);
1102 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
1104 if (chip->ecc_strength_ds > nand_info->max_ecc_strength_supported) {
1105 printf("unsupported NAND chip, minimum ecc required %d\n"
1106 , chip->ecc_strength_ds);
1110 if ((!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0) &&
1111 mtd->oobsize < 1024) || nand_info->legacy_bch_geometry) {
1112 dev_warn(this->dev, "use legacy bch geometry\n");
1113 return mxs_nand_legacy_calc_ecc_layout(geo, mtd);
1116 if (mtd->oobsize > 1024 || chip->ecc_step_ds < mtd->oobsize)
1117 return mxs_nand_calc_ecc_for_large_oob(geo, mtd);
1119 return mxs_nand_calc_ecc_layout_by_info(geo, mtd,
1120 chip->ecc_strength_ds, chip->ecc_step_ds);
1126 * At this point, the physical NAND Flash chips have been identified and
1127 * counted, so we know the physical geometry. This enables us to make some
1128 * important configuration decisions.
1130 * The return value of this function propagates directly back to this driver's
1131 * board_nand_init(). Anything other than zero will cause this driver to
1132 * tear everything down and declare failure.
1134 int mxs_nand_setup_ecc(struct mtd_info *mtd)
1136 struct nand_chip *nand = mtd_to_nand(mtd);
1137 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
1138 struct bch_geometry *geo = &nand_info->bch_geometry;
1139 struct mxs_bch_regs *bch_regs = nand_info->bch_regs;
1143 nand_info->en_randomizer = 0;
1144 nand_info->oobsize = mtd->oobsize;
1145 nand_info->writesize = mtd->writesize;
1147 ret = mxs_nand_set_geometry(mtd, geo);
1151 /* Configure BCH and set NFC geometry */
1152 mxs_reset_block(&bch_regs->hw_bch_ctrl_reg);
1154 /* Configure layout 0 */
1155 tmp = (geo->ecc_chunk_count - 1) << BCH_FLASHLAYOUT0_NBLOCKS_OFFSET;
1156 tmp |= MXS_NAND_METADATA_SIZE << BCH_FLASHLAYOUT0_META_SIZE_OFFSET;
1157 tmp |= (geo->ecc_strength >> 1) << BCH_FLASHLAYOUT0_ECC0_OFFSET;
1158 tmp |= geo->ecc_chunk0_size >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT;
1159 tmp |= (geo->gf_len == 14 ? 1 : 0) <<
1160 BCH_FLASHLAYOUT0_GF13_0_GF14_1_OFFSET;
1161 writel(tmp, &bch_regs->hw_bch_flash0layout0);
1162 nand_info->bch_flash0layout0 = tmp;
1164 tmp = (mtd->writesize + mtd->oobsize)
1165 << BCH_FLASHLAYOUT1_PAGE_SIZE_OFFSET;
1166 tmp |= (geo->ecc_strength >> 1) << BCH_FLASHLAYOUT1_ECCN_OFFSET;
1167 tmp |= geo->ecc_chunkn_size >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT;
1168 tmp |= (geo->gf_len == 14 ? 1 : 0) <<
1169 BCH_FLASHLAYOUT1_GF13_0_GF14_1_OFFSET;
1170 writel(tmp, &bch_regs->hw_bch_flash0layout1);
1171 nand_info->bch_flash0layout1 = tmp;
1173 /* Set erase threshold to ecc strength for mx6ul, mx6qp and mx7 */
1174 if (is_mx6dqp() || is_mx7() ||
1176 writel(BCH_MODE_ERASE_THRESHOLD(geo->ecc_strength),
1177 &bch_regs->hw_bch_mode);
1179 /* Set *all* chip selects to use layout 0 */
1180 writel(0, &bch_regs->hw_bch_layoutselect);
1182 /* Enable BCH complete interrupt */
1183 writel(BCH_CTRL_COMPLETE_IRQ_EN, &bch_regs->hw_bch_ctrl_set);
1185 /* Hook some operations at the MTD level. */
1186 if (mtd->_read_oob != mxs_nand_hook_read_oob) {
1187 nand_info->hooked_read_oob = mtd->_read_oob;
1188 mtd->_read_oob = mxs_nand_hook_read_oob;
1191 if (mtd->_write_oob != mxs_nand_hook_write_oob) {
1192 nand_info->hooked_write_oob = mtd->_write_oob;
1193 mtd->_write_oob = mxs_nand_hook_write_oob;
1196 if (mtd->_block_markbad != mxs_nand_hook_block_markbad) {
1197 nand_info->hooked_block_markbad = mtd->_block_markbad;
1198 mtd->_block_markbad = mxs_nand_hook_block_markbad;
1205 * Allocate DMA buffers
1207 int mxs_nand_alloc_buffers(struct mxs_nand_info *nand_info)
1210 const int size = NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE;
1212 nand_info->data_buf_size = roundup(size, MXS_DMA_ALIGNMENT);
1215 buf = memalign(MXS_DMA_ALIGNMENT, nand_info->data_buf_size);
1217 printf("MXS NAND: Error allocating DMA buffers\n");
1221 memset(buf, 0, nand_info->data_buf_size);
1223 nand_info->data_buf = buf;
1224 nand_info->oob_buf = buf + NAND_MAX_PAGESIZE;
1225 /* Command buffers */
1226 nand_info->cmd_buf = memalign(MXS_DMA_ALIGNMENT,
1227 MXS_NAND_COMMAND_BUFFER_SIZE);
1228 if (!nand_info->cmd_buf) {
1230 printf("MXS NAND: Error allocating command buffers\n");
1233 memset(nand_info->cmd_buf, 0, MXS_NAND_COMMAND_BUFFER_SIZE);
1234 nand_info->cmd_queue_len = 0;
1240 * Initializes the NFC hardware.
1242 static int mxs_nand_init_dma(struct mxs_nand_info *info)
1244 int i = 0, j, ret = 0;
1246 info->desc = malloc(sizeof(struct mxs_dma_desc *) *
1247 MXS_NAND_DMA_DESCRIPTOR_COUNT);
1253 /* Allocate the DMA descriptors. */
1254 for (i = 0; i < MXS_NAND_DMA_DESCRIPTOR_COUNT; i++) {
1255 info->desc[i] = mxs_dma_desc_alloc();
1256 if (!info->desc[i]) {
1262 /* Init the DMA controller. */
1264 for (j = MXS_DMA_CHANNEL_AHB_APBH_GPMI0;
1265 j <= MXS_DMA_CHANNEL_AHB_APBH_GPMI7; j++) {
1266 ret = mxs_dma_init_channel(j);
1271 /* Reset the GPMI block. */
1272 mxs_reset_block(&info->gpmi_regs->hw_gpmi_ctrl0_reg);
1273 mxs_reset_block(&info->bch_regs->hw_bch_ctrl_reg);
1276 * Choose NAND mode, set IRQ polarity, disable write protection and
1279 clrsetbits_le32(&info->gpmi_regs->hw_gpmi_ctrl1,
1280 GPMI_CTRL1_GPMI_MODE,
1281 GPMI_CTRL1_ATA_IRQRDY_POLARITY | GPMI_CTRL1_DEV_RESET |
1282 GPMI_CTRL1_BCH_MODE);
1287 for (--j; j >= MXS_DMA_CHANNEL_AHB_APBH_GPMI0; j--)
1290 for (--i; i >= 0; i--)
1291 mxs_dma_desc_free(info->desc[i]);
1295 printf("MXS NAND: Unable to allocate DMA descriptors\n");
1299 int mxs_nand_init_spl(struct nand_chip *nand)
1301 struct mxs_nand_info *nand_info;
1304 nand_info = malloc(sizeof(struct mxs_nand_info));
1306 printf("MXS NAND: Failed to allocate private data\n");
1309 memset(nand_info, 0, sizeof(struct mxs_nand_info));
1311 nand_info->gpmi_regs = (struct mxs_gpmi_regs *)MXS_GPMI_BASE;
1312 nand_info->bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
1314 if (is_mx6sx() || is_mx7())
1315 nand_info->max_ecc_strength_supported = 62;
1317 nand_info->max_ecc_strength_supported = 40;
1319 err = mxs_nand_alloc_buffers(nand_info);
1323 err = mxs_nand_init_dma(nand_info);
1327 nand_set_controller_data(nand, nand_info);
1329 nand->options |= NAND_NO_SUBPAGE_WRITE;
1331 nand->cmd_ctrl = mxs_nand_cmd_ctrl;
1332 nand->dev_ready = mxs_nand_device_ready;
1333 nand->select_chip = mxs_nand_select_chip;
1335 nand->read_byte = mxs_nand_read_byte;
1336 nand->read_buf = mxs_nand_read_buf;
1338 nand->ecc.read_page = mxs_nand_ecc_read_page;
1340 nand->ecc.mode = NAND_ECC_HW;
1345 int mxs_nand_init_ctrl(struct mxs_nand_info *nand_info)
1347 struct mtd_info *mtd;
1348 struct nand_chip *nand;
1351 nand = &nand_info->chip;
1352 mtd = nand_to_mtd(nand);
1353 err = mxs_nand_alloc_buffers(nand_info);
1357 err = mxs_nand_init_dma(nand_info);
1359 goto err_free_buffers;
1361 memset(&fake_ecc_layout, 0, sizeof(fake_ecc_layout));
1363 #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1364 nand->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
1367 nand_set_controller_data(nand, nand_info);
1368 nand->options |= NAND_NO_SUBPAGE_WRITE;
1371 nand->flash_node = dev_of_offset(nand_info->dev);
1373 nand->cmd_ctrl = mxs_nand_cmd_ctrl;
1375 nand->dev_ready = mxs_nand_device_ready;
1376 nand->select_chip = mxs_nand_select_chip;
1377 nand->block_bad = mxs_nand_block_bad;
1379 nand->read_byte = mxs_nand_read_byte;
1381 nand->read_buf = mxs_nand_read_buf;
1382 nand->write_buf = mxs_nand_write_buf;
1384 /* first scan to find the device and get the page size */
1385 if (nand_scan_ident(mtd, CONFIG_SYS_MAX_NAND_DEVICE, NULL))
1386 goto err_free_buffers;
1388 if (mxs_nand_setup_ecc(mtd))
1389 goto err_free_buffers;
1391 nand->ecc.read_page = mxs_nand_ecc_read_page;
1392 nand->ecc.write_page = mxs_nand_ecc_write_page;
1393 nand->ecc.read_oob = mxs_nand_ecc_read_oob;
1394 nand->ecc.write_oob = mxs_nand_ecc_write_oob;
1396 nand->ecc.layout = &fake_ecc_layout;
1397 nand->ecc.mode = NAND_ECC_HW;
1398 nand->ecc.size = nand_info->bch_geometry.ecc_chunkn_size;
1399 nand->ecc.strength = nand_info->bch_geometry.ecc_strength;
1401 /* second phase scan */
1402 err = nand_scan_tail(mtd);
1404 goto err_free_buffers;
1406 err = nand_register(0, mtd);
1408 goto err_free_buffers;
1413 free(nand_info->data_buf);
1414 free(nand_info->cmd_buf);
1419 #ifndef CONFIG_NAND_MXS_DT
1420 void board_nand_init(void)
1422 struct mxs_nand_info *nand_info;
1424 nand_info = malloc(sizeof(struct mxs_nand_info));
1426 printf("MXS NAND: Failed to allocate private data\n");
1429 memset(nand_info, 0, sizeof(struct mxs_nand_info));
1431 nand_info->gpmi_regs = (struct mxs_gpmi_regs *)MXS_GPMI_BASE;
1432 nand_info->bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
1434 /* Refer to Chapter 17 for i.MX6DQ, Chapter 18 for i.MX6SX */
1435 if (is_mx6sx() || is_mx7())
1436 nand_info->max_ecc_strength_supported = 62;
1438 nand_info->max_ecc_strength_supported = 40;
1440 #ifdef CONFIG_NAND_MXS_USE_MINIMUM_ECC
1441 nand_info->use_minimum_ecc = true;
1444 if (mxs_nand_init_ctrl(nand_info) < 0)
1455 * Read NAND layout for FCB block generation.
1457 void mxs_nand_get_layout(struct mtd_info *mtd, struct mxs_nand_layout *l)
1459 struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
1462 tmp = readl(&bch_regs->hw_bch_flash0layout0);
1463 l->nblocks = (tmp & BCH_FLASHLAYOUT0_NBLOCKS_MASK) >>
1464 BCH_FLASHLAYOUT0_NBLOCKS_OFFSET;
1465 l->meta_size = (tmp & BCH_FLASHLAYOUT0_META_SIZE_MASK) >>
1466 BCH_FLASHLAYOUT0_META_SIZE_OFFSET;
1468 tmp = readl(&bch_regs->hw_bch_flash0layout1);
1469 l->data0_size = 4 * ((tmp & BCH_FLASHLAYOUT0_DATA0_SIZE_MASK) >>
1470 BCH_FLASHLAYOUT0_DATA0_SIZE_OFFSET);
1471 l->ecc0 = (tmp & BCH_FLASHLAYOUT0_ECC0_MASK) >>
1472 BCH_FLASHLAYOUT0_ECC0_OFFSET;
1473 l->datan_size = 4 * ((tmp & BCH_FLASHLAYOUT1_DATAN_SIZE_MASK) >>
1474 BCH_FLASHLAYOUT1_DATAN_SIZE_OFFSET);
1475 l->eccn = (tmp & BCH_FLASHLAYOUT1_ECCN_MASK) >>
1476 BCH_FLASHLAYOUT1_ECCN_OFFSET;
1480 * Set BCH to specific layout used by ROM bootloader to read FCB.
1482 void mxs_nand_mode_fcb(struct mtd_info *mtd)
1485 struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
1486 struct nand_chip *nand = mtd_to_nand(mtd);
1487 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
1489 nand_info->en_randomizer = 1;
1491 mtd->writesize = 1024;
1492 mtd->oobsize = 1862 - 1024;
1495 tmp = 7 << BCH_FLASHLAYOUT0_NBLOCKS_OFFSET;
1496 /* 32 bytes for metadata */
1497 tmp |= 32 << BCH_FLASHLAYOUT0_META_SIZE_OFFSET;
1498 /* using ECC62 level to be performed */
1499 tmp |= 0x1F << BCH_FLASHLAYOUT0_ECC0_OFFSET;
1500 /* 0x20 * 4 bytes of the data0 block */
1501 tmp |= 0x20 << BCH_FLASHLAYOUT0_DATA0_SIZE_OFFSET;
1502 tmp |= 0 << BCH_FLASHLAYOUT0_GF13_0_GF14_1_OFFSET;
1503 writel(tmp, &bch_regs->hw_bch_flash0layout0);
1505 /* 1024 for data + 838 for OOB */
1506 tmp = 1862 << BCH_FLASHLAYOUT1_PAGE_SIZE_OFFSET;
1507 /* using ECC62 level to be performed */
1508 tmp |= 0x1F << BCH_FLASHLAYOUT1_ECCN_OFFSET;
1509 /* 0x20 * 4 bytes of the data0 block */
1510 tmp |= 0x20 << BCH_FLASHLAYOUT1_DATAN_SIZE_OFFSET;
1511 tmp |= 0 << BCH_FLASHLAYOUT1_GF13_0_GF14_1_OFFSET;
1512 writel(tmp, &bch_regs->hw_bch_flash0layout1);
1516 * Restore BCH to normal settings.
1518 void mxs_nand_mode_normal(struct mtd_info *mtd)
1520 struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
1521 struct nand_chip *nand = mtd_to_nand(mtd);
1522 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
1524 nand_info->en_randomizer = 0;
1526 mtd->writesize = nand_info->writesize;
1527 mtd->oobsize = nand_info->oobsize;
1529 writel(nand_info->bch_flash0layout0, &bch_regs->hw_bch_flash0layout0);
1530 writel(nand_info->bch_flash0layout1, &bch_regs->hw_bch_flash0layout1);
1533 uint32_t mxs_nand_mark_byte_offset(struct mtd_info *mtd)
1535 struct nand_chip *chip = mtd_to_nand(mtd);
1536 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
1537 struct bch_geometry *geo = &nand_info->bch_geometry;
1539 return geo->block_mark_byte_offset;
1542 uint32_t mxs_nand_mark_bit_offset(struct mtd_info *mtd)
1544 struct nand_chip *chip = mtd_to_nand(mtd);
1545 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
1546 struct bch_geometry *geo = &nand_info->bch_geometry;
1548 return geo->block_mark_bit_offset;