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) || defined(CONFIG_IMX8) || \
36 #define MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT 2
38 #define MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT 0
40 #define MXS_NAND_METADATA_SIZE 10
41 #define MXS_NAND_BITS_PER_ECC_LEVEL 13
43 #if !defined(CONFIG_SYS_CACHELINE_SIZE) || CONFIG_SYS_CACHELINE_SIZE < 32
44 #define MXS_NAND_COMMAND_BUFFER_SIZE 32
46 #define MXS_NAND_COMMAND_BUFFER_SIZE CONFIG_SYS_CACHELINE_SIZE
49 #define MXS_NAND_BCH_TIMEOUT 10000
51 struct nand_ecclayout fake_ecc_layout;
54 * Cache management functions
56 #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
57 static void mxs_nand_flush_data_buf(struct mxs_nand_info *info)
59 uint32_t addr = (uintptr_t)info->data_buf;
61 flush_dcache_range(addr, addr + info->data_buf_size);
64 static void mxs_nand_inval_data_buf(struct mxs_nand_info *info)
66 uint32_t addr = (uintptr_t)info->data_buf;
68 invalidate_dcache_range(addr, addr + info->data_buf_size);
71 static void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info)
73 uint32_t addr = (uintptr_t)info->cmd_buf;
75 flush_dcache_range(addr, addr + MXS_NAND_COMMAND_BUFFER_SIZE);
78 static inline void mxs_nand_flush_data_buf(struct mxs_nand_info *info) {}
79 static inline void mxs_nand_inval_data_buf(struct mxs_nand_info *info) {}
80 static inline void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info) {}
83 static struct mxs_dma_desc *mxs_nand_get_dma_desc(struct mxs_nand_info *info)
85 struct mxs_dma_desc *desc;
87 if (info->desc_index >= MXS_NAND_DMA_DESCRIPTOR_COUNT) {
88 printf("MXS NAND: Too many DMA descriptors requested\n");
92 desc = info->desc[info->desc_index];
98 static void mxs_nand_return_dma_descs(struct mxs_nand_info *info)
101 struct mxs_dma_desc *desc;
103 for (i = 0; i < info->desc_index; i++) {
104 desc = info->desc[i];
105 memset(desc, 0, sizeof(struct mxs_dma_desc));
106 desc->address = (dma_addr_t)desc;
109 info->desc_index = 0;
112 static uint32_t mxs_nand_aux_status_offset(void)
114 return (MXS_NAND_METADATA_SIZE + 0x3) & ~0x3;
117 static inline bool mxs_nand_bbm_in_data_chunk(struct bch_geometry *geo, struct mtd_info *mtd,
118 unsigned int *chunk_num)
122 if (geo->ecc_chunk0_size != geo->ecc_chunkn_size) {
123 dev_err(this->dev, "The size of chunk0 must equal to chunkn\n");
127 i = (mtd->writesize * 8 - MXS_NAND_METADATA_SIZE * 8) /
128 (geo->gf_len * geo->ecc_strength +
129 geo->ecc_chunkn_size * 8);
131 j = (mtd->writesize * 8 - MXS_NAND_METADATA_SIZE * 8) -
132 (geo->gf_len * geo->ecc_strength +
133 geo->ecc_chunkn_size * 8) * i;
135 if (j < geo->ecc_chunkn_size * 8) {
137 dev_dbg(this->dev, "Set ecc to %d and bbm in chunk %d\n",
138 geo->ecc_strength, *chunk_num);
145 static inline int mxs_nand_calc_ecc_layout_by_info(struct bch_geometry *geo,
146 struct mtd_info *mtd,
147 unsigned int ecc_strength,
148 unsigned int ecc_step)
150 struct nand_chip *chip = mtd_to_nand(mtd);
151 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
152 unsigned int block_mark_bit_offset;
165 geo->ecc_chunk0_size = ecc_step;
166 geo->ecc_chunkn_size = ecc_step;
167 geo->ecc_strength = round_up(ecc_strength, 2);
169 /* Keep the C >= O */
170 if (geo->ecc_chunkn_size < mtd->oobsize)
173 if (geo->ecc_strength > nand_info->max_ecc_strength_supported)
176 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunkn_size;
179 block_mark_bit_offset = mtd->writesize * 8 -
180 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
181 + MXS_NAND_METADATA_SIZE * 8);
183 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
184 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
189 static inline int mxs_nand_legacy_calc_ecc_layout(struct bch_geometry *geo,
190 struct mtd_info *mtd)
192 struct nand_chip *chip = mtd_to_nand(mtd);
193 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
194 unsigned int block_mark_bit_offset;
196 /* The default for the length of Galois Field. */
199 /* The default for chunk size. */
200 geo->ecc_chunk0_size = 512;
201 geo->ecc_chunkn_size = 512;
203 if (geo->ecc_chunkn_size < mtd->oobsize) {
205 geo->ecc_chunk0_size *= 2;
206 geo->ecc_chunkn_size *= 2;
209 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunkn_size;
212 * Determine the ECC layout with the formula:
213 * ECC bits per chunk = (total page spare data bits) /
214 * (bits per ECC level) / (chunks per page)
216 * total page spare data bits =
217 * (page oob size - meta data size) * (bits per byte)
219 geo->ecc_strength = ((mtd->oobsize - MXS_NAND_METADATA_SIZE) * 8)
220 / (geo->gf_len * geo->ecc_chunk_count);
222 geo->ecc_strength = min(round_down(geo->ecc_strength, 2),
223 nand_info->max_ecc_strength_supported);
225 block_mark_bit_offset = mtd->writesize * 8 -
226 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
227 + MXS_NAND_METADATA_SIZE * 8);
229 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
230 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
235 static inline int mxs_nand_calc_ecc_for_large_oob(struct bch_geometry *geo,
236 struct mtd_info *mtd)
238 struct nand_chip *chip = mtd_to_nand(mtd);
239 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
240 unsigned int block_mark_bit_offset;
241 unsigned int max_ecc;
242 unsigned int bbm_chunk;
245 /* sanity check for the minimum ecc nand required */
246 if (!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0))
248 geo->ecc_strength = chip->ecc_strength_ds;
250 /* calculate the maximum ecc platform can support*/
252 geo->ecc_chunk0_size = 1024;
253 geo->ecc_chunkn_size = 1024;
254 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunkn_size;
255 max_ecc = ((mtd->oobsize - MXS_NAND_METADATA_SIZE) * 8)
256 / (geo->gf_len * geo->ecc_chunk_count);
257 max_ecc = min(round_down(max_ecc, 2),
258 nand_info->max_ecc_strength_supported);
261 /* search a supported ecc strength that makes bbm */
262 /* located in data chunk */
263 geo->ecc_strength = chip->ecc_strength_ds;
264 while (!(geo->ecc_strength > max_ecc)) {
265 if (mxs_nand_bbm_in_data_chunk(geo, mtd, &bbm_chunk))
267 geo->ecc_strength += 2;
270 /* if none of them works, keep using the minimum ecc */
271 /* nand required but changing ecc page layout */
272 if (geo->ecc_strength > max_ecc) {
273 geo->ecc_strength = chip->ecc_strength_ds;
274 /* add extra ecc for meta data */
275 geo->ecc_chunk0_size = 0;
276 geo->ecc_chunk_count = (mtd->writesize / geo->ecc_chunkn_size) + 1;
277 geo->ecc_for_meta = 1;
278 /* check if oob can afford this extra ecc chunk */
279 if (mtd->oobsize * 8 < MXS_NAND_METADATA_SIZE * 8 +
280 geo->gf_len * geo->ecc_strength
281 * geo->ecc_chunk_count) {
282 printf("unsupported NAND chip with new layout\n");
286 /* calculate in which chunk bbm located */
287 bbm_chunk = (mtd->writesize * 8 - MXS_NAND_METADATA_SIZE * 8 -
288 geo->gf_len * geo->ecc_strength) /
289 (geo->gf_len * geo->ecc_strength +
290 geo->ecc_chunkn_size * 8) + 1;
293 /* calculate the number of ecc chunk behind the bbm */
294 i = (mtd->writesize / geo->ecc_chunkn_size) - bbm_chunk + 1;
296 block_mark_bit_offset = mtd->writesize * 8 -
297 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - i)
298 + MXS_NAND_METADATA_SIZE * 8);
300 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
301 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
307 * Wait for BCH complete IRQ and clear the IRQ
309 static int mxs_nand_wait_for_bch_complete(struct mxs_nand_info *nand_info)
311 int timeout = MXS_NAND_BCH_TIMEOUT;
314 ret = mxs_wait_mask_set(&nand_info->bch_regs->hw_bch_ctrl_reg,
315 BCH_CTRL_COMPLETE_IRQ, timeout);
317 writel(BCH_CTRL_COMPLETE_IRQ, &nand_info->bch_regs->hw_bch_ctrl_clr);
323 * This is the function that we install in the cmd_ctrl function pointer of the
324 * owning struct nand_chip. The only functions in the reference implementation
325 * that use these functions pointers are cmdfunc and select_chip.
327 * In this driver, we implement our own select_chip, so this function will only
328 * be called by the reference implementation's cmdfunc. For this reason, we can
329 * ignore the chip enable bit and concentrate only on sending bytes to the NAND
332 static void mxs_nand_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
334 struct nand_chip *nand = mtd_to_nand(mtd);
335 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
336 struct mxs_dma_desc *d;
337 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
341 * If this condition is true, something is _VERY_ wrong in MTD
344 if (nand_info->cmd_queue_len == MXS_NAND_COMMAND_BUFFER_SIZE) {
345 printf("MXS NAND: Command queue too long\n");
350 * Every operation begins with a command byte and a series of zero or
351 * more address bytes. These are distinguished by either the Address
352 * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
353 * asserted. When MTD is ready to execute the command, it will
354 * deasert both latch enables.
356 * Rather than run a separate DMA operation for every single byte, we
357 * queue them up and run a single DMA operation for the entire series
358 * of command and data bytes.
360 if (ctrl & (NAND_ALE | NAND_CLE)) {
361 if (data != NAND_CMD_NONE)
362 nand_info->cmd_buf[nand_info->cmd_queue_len++] = data;
367 * If control arrives here, MTD has deasserted both the ALE and CLE,
368 * which means it's ready to run an operation. Check if we have any
371 if (nand_info->cmd_queue_len == 0)
374 /* Compile the DMA descriptor -- a descriptor that sends command. */
375 d = mxs_nand_get_dma_desc(nand_info);
377 MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ |
378 MXS_DMA_DESC_CHAIN | MXS_DMA_DESC_DEC_SEM |
379 MXS_DMA_DESC_WAIT4END | (3 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
380 (nand_info->cmd_queue_len << MXS_DMA_DESC_BYTES_OFFSET);
382 d->cmd.address = (dma_addr_t)nand_info->cmd_buf;
384 d->cmd.pio_words[0] =
385 GPMI_CTRL0_COMMAND_MODE_WRITE |
386 GPMI_CTRL0_WORD_LENGTH |
387 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
388 GPMI_CTRL0_ADDRESS_NAND_CLE |
389 GPMI_CTRL0_ADDRESS_INCREMENT |
390 nand_info->cmd_queue_len;
392 mxs_dma_desc_append(channel, d);
395 mxs_nand_flush_cmd_buf(nand_info);
397 /* Execute the DMA chain. */
398 ret = mxs_dma_go(channel);
400 printf("MXS NAND: Error sending command\n");
402 mxs_nand_return_dma_descs(nand_info);
404 /* Reset the command queue. */
405 nand_info->cmd_queue_len = 0;
409 * Test if the NAND flash is ready.
411 static int mxs_nand_device_ready(struct mtd_info *mtd)
413 struct nand_chip *chip = mtd_to_nand(mtd);
414 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
417 tmp = readl(&nand_info->gpmi_regs->hw_gpmi_stat);
418 tmp >>= (GPMI_STAT_READY_BUSY_OFFSET + nand_info->cur_chip);
424 * Select the NAND chip.
426 static void mxs_nand_select_chip(struct mtd_info *mtd, int chip)
428 struct nand_chip *nand = mtd_to_nand(mtd);
429 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
431 nand_info->cur_chip = chip;
435 * Handle block mark swapping.
437 * Note that, when this function is called, it doesn't know whether it's
438 * swapping the block mark, or swapping it *back* -- but it doesn't matter
439 * because the the operation is the same.
441 static void mxs_nand_swap_block_mark(struct bch_geometry *geo,
442 uint8_t *data_buf, uint8_t *oob_buf)
444 uint32_t bit_offset = geo->block_mark_bit_offset;
445 uint32_t buf_offset = geo->block_mark_byte_offset;
451 * Get the byte from the data area that overlays the block mark. Since
452 * the ECC engine applies its own view to the bits in the page, the
453 * physical block mark won't (in general) appear on a byte boundary in
456 src = data_buf[buf_offset] >> bit_offset;
457 src |= data_buf[buf_offset + 1] << (8 - bit_offset);
463 data_buf[buf_offset] &= ~(0xff << bit_offset);
464 data_buf[buf_offset + 1] &= 0xff << bit_offset;
466 data_buf[buf_offset] |= dst << bit_offset;
467 data_buf[buf_offset + 1] |= dst >> (8 - bit_offset);
471 * Read data from NAND.
473 static void mxs_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int length)
475 struct nand_chip *nand = mtd_to_nand(mtd);
476 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
477 struct mxs_dma_desc *d;
478 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
481 if (length > NAND_MAX_PAGESIZE) {
482 printf("MXS NAND: DMA buffer too big\n");
487 printf("MXS NAND: DMA buffer is NULL\n");
491 /* Compile the DMA descriptor - a descriptor that reads data. */
492 d = mxs_nand_get_dma_desc(nand_info);
494 MXS_DMA_DESC_COMMAND_DMA_WRITE | MXS_DMA_DESC_IRQ |
495 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
496 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
497 (length << MXS_DMA_DESC_BYTES_OFFSET);
499 d->cmd.address = (dma_addr_t)nand_info->data_buf;
501 d->cmd.pio_words[0] =
502 GPMI_CTRL0_COMMAND_MODE_READ |
503 GPMI_CTRL0_WORD_LENGTH |
504 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
505 GPMI_CTRL0_ADDRESS_NAND_DATA |
508 mxs_dma_desc_append(channel, d);
511 * A DMA descriptor that waits for the command to end and the chip to
514 * I think we actually should *not* be waiting for the chip to become
515 * ready because, after all, we don't care. I think the original code
516 * did that and no one has re-thought it yet.
518 d = mxs_nand_get_dma_desc(nand_info);
520 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
521 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_DEC_SEM |
522 MXS_DMA_DESC_WAIT4END | (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
526 d->cmd.pio_words[0] =
527 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
528 GPMI_CTRL0_WORD_LENGTH |
529 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
530 GPMI_CTRL0_ADDRESS_NAND_DATA;
532 mxs_dma_desc_append(channel, d);
534 /* Invalidate caches */
535 mxs_nand_inval_data_buf(nand_info);
537 /* Execute the DMA chain. */
538 ret = mxs_dma_go(channel);
540 printf("MXS NAND: DMA read error\n");
544 /* Invalidate caches */
545 mxs_nand_inval_data_buf(nand_info);
547 memcpy(buf, nand_info->data_buf, length);
550 mxs_nand_return_dma_descs(nand_info);
554 * Write data to NAND.
556 static void mxs_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf,
559 struct nand_chip *nand = mtd_to_nand(mtd);
560 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
561 struct mxs_dma_desc *d;
562 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
565 if (length > NAND_MAX_PAGESIZE) {
566 printf("MXS NAND: DMA buffer too big\n");
571 printf("MXS NAND: DMA buffer is NULL\n");
575 memcpy(nand_info->data_buf, buf, length);
577 /* Compile the DMA descriptor - a descriptor that writes data. */
578 d = mxs_nand_get_dma_desc(nand_info);
580 MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ |
581 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
582 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
583 (length << MXS_DMA_DESC_BYTES_OFFSET);
585 d->cmd.address = (dma_addr_t)nand_info->data_buf;
587 d->cmd.pio_words[0] =
588 GPMI_CTRL0_COMMAND_MODE_WRITE |
589 GPMI_CTRL0_WORD_LENGTH |
590 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
591 GPMI_CTRL0_ADDRESS_NAND_DATA |
594 mxs_dma_desc_append(channel, d);
597 mxs_nand_flush_data_buf(nand_info);
599 /* Execute the DMA chain. */
600 ret = mxs_dma_go(channel);
602 printf("MXS NAND: DMA write error\n");
604 mxs_nand_return_dma_descs(nand_info);
608 * Read a single byte from NAND.
610 static uint8_t mxs_nand_read_byte(struct mtd_info *mtd)
613 mxs_nand_read_buf(mtd, &buf, 1);
617 static bool mxs_nand_erased_page(struct mtd_info *mtd, struct nand_chip *nand,
618 u8 *buf, int chunk, int page)
620 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
621 struct bch_geometry *geo = &nand_info->bch_geometry;
622 unsigned int flip_bits = 0, flip_bits_noecc = 0;
623 unsigned int threshold;
624 unsigned int base = geo->ecc_chunkn_size * chunk;
625 u32 *dma_buf = (u32 *)buf;
628 threshold = geo->gf_len / 2;
629 if (threshold > geo->ecc_strength)
630 threshold = geo->ecc_strength;
632 for (i = 0; i < geo->ecc_chunkn_size; i++) {
633 flip_bits += hweight8(~buf[base + i]);
634 if (flip_bits > threshold)
638 nand->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
639 nand->read_buf(mtd, buf, mtd->writesize);
641 for (i = 0; i < mtd->writesize / 4; i++) {
642 flip_bits_noecc += hweight32(~dma_buf[i]);
643 if (flip_bits_noecc > threshold)
647 mtd->ecc_stats.corrected += flip_bits;
649 memset(buf, 0xff, mtd->writesize);
651 printf("The page(%d) is an erased page(%d,%d,%d,%d).\n", page, chunk, threshold, flip_bits, flip_bits_noecc);
657 * Read a page from NAND.
659 static int mxs_nand_ecc_read_page(struct mtd_info *mtd, struct nand_chip *nand,
660 uint8_t *buf, int oob_required,
663 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
664 struct bch_geometry *geo = &nand_info->bch_geometry;
665 struct mxs_bch_regs *bch_regs = nand_info->bch_regs;
666 struct mxs_dma_desc *d;
667 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
668 uint32_t corrected = 0, failed = 0;
673 /* Compile the DMA descriptor - wait for ready. */
674 d = mxs_nand_get_dma_desc(nand_info);
676 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
677 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
678 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
682 d->cmd.pio_words[0] =
683 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
684 GPMI_CTRL0_WORD_LENGTH |
685 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
686 GPMI_CTRL0_ADDRESS_NAND_DATA;
688 mxs_dma_desc_append(channel, d);
690 /* Compile the DMA descriptor - enable the BCH block and read. */
691 d = mxs_nand_get_dma_desc(nand_info);
693 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
694 MXS_DMA_DESC_WAIT4END | (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
698 d->cmd.pio_words[0] =
699 GPMI_CTRL0_COMMAND_MODE_READ |
700 GPMI_CTRL0_WORD_LENGTH |
701 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
702 GPMI_CTRL0_ADDRESS_NAND_DATA |
703 (mtd->writesize + mtd->oobsize);
704 d->cmd.pio_words[1] = 0;
705 d->cmd.pio_words[2] =
706 GPMI_ECCCTRL_ENABLE_ECC |
707 GPMI_ECCCTRL_ECC_CMD_DECODE |
708 GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
709 d->cmd.pio_words[3] = mtd->writesize + mtd->oobsize;
710 d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
711 d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;
713 if ((is_mx7() || is_imx8m()) && nand_info->en_randomizer) {
714 d->cmd.pio_words[2] |= GPMI_ECCCTRL_RANDOMIZER_ENABLE |
715 GPMI_ECCCTRL_RANDOMIZER_TYPE2;
716 d->cmd.pio_words[3] |= (page % 256) << 16;
719 mxs_dma_desc_append(channel, d);
721 /* Compile the DMA descriptor - disable the BCH block. */
722 d = mxs_nand_get_dma_desc(nand_info);
724 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
725 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
726 (3 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
730 d->cmd.pio_words[0] =
731 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
732 GPMI_CTRL0_WORD_LENGTH |
733 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
734 GPMI_CTRL0_ADDRESS_NAND_DATA |
735 (mtd->writesize + mtd->oobsize);
736 d->cmd.pio_words[1] = 0;
737 d->cmd.pio_words[2] = 0;
739 mxs_dma_desc_append(channel, d);
741 /* Compile the DMA descriptor - deassert the NAND lock and interrupt. */
742 d = mxs_nand_get_dma_desc(nand_info);
744 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
745 MXS_DMA_DESC_DEC_SEM;
749 mxs_dma_desc_append(channel, d);
751 /* Invalidate caches */
752 mxs_nand_inval_data_buf(nand_info);
754 /* Execute the DMA chain. */
755 ret = mxs_dma_go(channel);
757 printf("MXS NAND: DMA read error\n");
761 ret = mxs_nand_wait_for_bch_complete(nand_info);
763 printf("MXS NAND: BCH read timeout\n");
767 mxs_nand_return_dma_descs(nand_info);
769 /* Invalidate caches */
770 mxs_nand_inval_data_buf(nand_info);
772 /* Read DMA completed, now do the mark swapping. */
773 mxs_nand_swap_block_mark(geo, nand_info->data_buf, nand_info->oob_buf);
775 /* Loop over status bytes, accumulating ECC status. */
776 status = nand_info->oob_buf + mxs_nand_aux_status_offset();
777 for (i = 0; i < geo->ecc_chunk_count; i++) {
778 if (status[i] == 0x00)
781 if (status[i] == 0xff) {
782 if (!nand_info->en_randomizer &&
783 (is_mx6dqp() || is_mx7() || is_mx6ul() ||
784 is_imx8() || is_imx8m()))
785 if (readl(&bch_regs->hw_bch_debug1))
790 if (status[i] == 0xfe) {
791 if (mxs_nand_erased_page(mtd, nand,
792 nand_info->data_buf, i, page))
798 corrected += status[i];
801 /* Propagate ECC status to the owning MTD. */
802 mtd->ecc_stats.failed += failed;
803 mtd->ecc_stats.corrected += corrected;
806 * It's time to deliver the OOB bytes. See mxs_nand_ecc_read_oob() for
807 * details about our policy for delivering the OOB.
809 * We fill the caller's buffer with set bits, and then copy the block
810 * mark to the caller's buffer. Note that, if block mark swapping was
811 * necessary, it has already been done, so we can rely on the first
812 * byte of the auxiliary buffer to contain the block mark.
814 memset(nand->oob_poi, 0xff, mtd->oobsize);
816 nand->oob_poi[0] = nand_info->oob_buf[0];
818 memcpy(buf, nand_info->data_buf, mtd->writesize);
821 memset(buf, 0xff, mtd->writesize);
823 mxs_nand_return_dma_descs(nand_info);
829 * Write a page to NAND.
831 static int mxs_nand_ecc_write_page(struct mtd_info *mtd,
832 struct nand_chip *nand, const uint8_t *buf,
833 int oob_required, int page)
835 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
836 struct bch_geometry *geo = &nand_info->bch_geometry;
837 struct mxs_dma_desc *d;
838 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
841 memcpy(nand_info->data_buf, buf, mtd->writesize);
842 memcpy(nand_info->oob_buf, nand->oob_poi, mtd->oobsize);
844 /* Handle block mark swapping. */
845 mxs_nand_swap_block_mark(geo, nand_info->data_buf, nand_info->oob_buf);
847 /* Compile the DMA descriptor - write data. */
848 d = mxs_nand_get_dma_desc(nand_info);
850 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
851 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
852 (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
856 d->cmd.pio_words[0] =
857 GPMI_CTRL0_COMMAND_MODE_WRITE |
858 GPMI_CTRL0_WORD_LENGTH |
859 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
860 GPMI_CTRL0_ADDRESS_NAND_DATA;
861 d->cmd.pio_words[1] = 0;
862 d->cmd.pio_words[2] =
863 GPMI_ECCCTRL_ENABLE_ECC |
864 GPMI_ECCCTRL_ECC_CMD_ENCODE |
865 GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
866 d->cmd.pio_words[3] = (mtd->writesize + mtd->oobsize);
867 d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
868 d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;
870 if ((is_mx7() || is_imx8m()) && nand_info->en_randomizer) {
871 d->cmd.pio_words[2] |= GPMI_ECCCTRL_RANDOMIZER_ENABLE |
872 GPMI_ECCCTRL_RANDOMIZER_TYPE2;
874 * Write NAND page number needed to be randomized
875 * to GPMI_ECCCOUNT register.
877 * The value is between 0-255. For additional details
878 * check 9.6.6.4 of i.MX7D Applications Processor reference
880 d->cmd.pio_words[3] |= (page % 256) << 16;
883 mxs_dma_desc_append(channel, d);
886 mxs_nand_flush_data_buf(nand_info);
888 /* Execute the DMA chain. */
889 ret = mxs_dma_go(channel);
891 printf("MXS NAND: DMA write error\n");
895 ret = mxs_nand_wait_for_bch_complete(nand_info);
897 printf("MXS NAND: BCH write timeout\n");
902 mxs_nand_return_dma_descs(nand_info);
907 * Read OOB from NAND.
909 * This function is a veneer that replaces the function originally installed by
910 * the NAND Flash MTD code.
912 static int mxs_nand_hook_read_oob(struct mtd_info *mtd, loff_t from,
913 struct mtd_oob_ops *ops)
915 struct nand_chip *chip = mtd_to_nand(mtd);
916 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
919 if (ops->mode == MTD_OPS_RAW)
920 nand_info->raw_oob_mode = 1;
922 nand_info->raw_oob_mode = 0;
924 ret = nand_info->hooked_read_oob(mtd, from, ops);
926 nand_info->raw_oob_mode = 0;
934 * This function is a veneer that replaces the function originally installed by
935 * the NAND Flash MTD code.
937 static int mxs_nand_hook_write_oob(struct mtd_info *mtd, loff_t to,
938 struct mtd_oob_ops *ops)
940 struct nand_chip *chip = mtd_to_nand(mtd);
941 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
944 if (ops->mode == MTD_OPS_RAW)
945 nand_info->raw_oob_mode = 1;
947 nand_info->raw_oob_mode = 0;
949 ret = nand_info->hooked_write_oob(mtd, to, ops);
951 nand_info->raw_oob_mode = 0;
957 * Mark a block bad in NAND.
959 * This function is a veneer that replaces the function originally installed by
960 * the NAND Flash MTD code.
962 static int mxs_nand_hook_block_markbad(struct mtd_info *mtd, loff_t ofs)
964 struct nand_chip *chip = mtd_to_nand(mtd);
965 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
968 nand_info->marking_block_bad = 1;
970 ret = nand_info->hooked_block_markbad(mtd, ofs);
972 nand_info->marking_block_bad = 0;
978 * There are several places in this driver where we have to handle the OOB and
979 * block marks. This is the function where things are the most complicated, so
980 * this is where we try to explain it all. All the other places refer back to
983 * These are the rules, in order of decreasing importance:
985 * 1) Nothing the caller does can be allowed to imperil the block mark, so all
986 * write operations take measures to protect it.
988 * 2) In read operations, the first byte of the OOB we return must reflect the
989 * true state of the block mark, no matter where that block mark appears in
992 * 3) ECC-based read operations return an OOB full of set bits (since we never
993 * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
996 * 4) "Raw" read operations return a direct view of the physical bytes in the
997 * page, using the conventional definition of which bytes are data and which
998 * are OOB. This gives the caller a way to see the actual, physical bytes
999 * in the page, without the distortions applied by our ECC engine.
1001 * What we do for this specific read operation depends on whether we're doing
1002 * "raw" read, or an ECC-based read.
1004 * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
1005 * easy. When reading a page, for example, the NAND Flash MTD code calls our
1006 * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
1007 * ECC-based or raw view of the page is implicit in which function it calls
1008 * (there is a similar pair of ECC-based/raw functions for writing).
1010 * Since MTD assumes the OOB is not covered by ECC, there is no pair of
1011 * ECC-based/raw functions for reading or or writing the OOB. The fact that the
1012 * caller wants an ECC-based or raw view of the page is not propagated down to
1015 * Since our OOB *is* covered by ECC, we need this information. So, we hook the
1016 * ecc.read_oob and ecc.write_oob function pointers in the owning
1017 * struct mtd_info with our own functions. These hook functions set the
1018 * raw_oob_mode field so that, when control finally arrives here, we'll know
1021 static int mxs_nand_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *nand,
1024 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
1027 * First, fill in the OOB buffer. If we're doing a raw read, we need to
1028 * get the bytes from the physical page. If we're not doing a raw read,
1029 * we need to fill the buffer with set bits.
1031 if (nand_info->raw_oob_mode) {
1033 * If control arrives here, we're doing a "raw" read. Send the
1034 * command to read the conventional OOB and read it.
1036 nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1037 nand->read_buf(mtd, nand->oob_poi, mtd->oobsize);
1040 * If control arrives here, we're not doing a "raw" read. Fill
1041 * the OOB buffer with set bits and correct the block mark.
1043 memset(nand->oob_poi, 0xff, mtd->oobsize);
1045 nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1046 mxs_nand_read_buf(mtd, nand->oob_poi, 1);
1054 * Write OOB data to NAND.
1056 static int mxs_nand_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *nand,
1059 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
1060 uint8_t block_mark = 0;
1063 * There are fundamental incompatibilities between the i.MX GPMI NFC and
1064 * the NAND Flash MTD model that make it essentially impossible to write
1065 * the out-of-band bytes.
1067 * We permit *ONE* exception. If the *intent* of writing the OOB is to
1068 * mark a block bad, we can do that.
1071 if (!nand_info->marking_block_bad) {
1072 printf("NXS NAND: Writing OOB isn't supported\n");
1076 /* Write the block mark. */
1077 nand->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1078 nand->write_buf(mtd, &block_mark, 1);
1079 nand->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1081 /* Check if it worked. */
1082 if (nand->waitfunc(mtd, nand) & NAND_STATUS_FAIL)
1089 * Claims all blocks are good.
1091 * In principle, this function is *only* called when the NAND Flash MTD system
1092 * isn't allowed to keep an in-memory bad block table, so it is forced to ask
1093 * the driver for bad block information.
1095 * In fact, we permit the NAND Flash MTD system to have an in-memory BBT, so
1096 * this function is *only* called when we take it away.
1098 * Thus, this function is only called when we want *all* blocks to look good,
1099 * so it *always* return success.
1101 static int mxs_nand_block_bad(struct mtd_info *mtd, loff_t ofs)
1106 static int mxs_nand_set_geometry(struct mtd_info *mtd, struct bch_geometry *geo)
1108 struct nand_chip *chip = mtd_to_nand(mtd);
1109 struct nand_chip *nand = mtd_to_nand(mtd);
1110 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
1112 if (chip->ecc_strength_ds > nand_info->max_ecc_strength_supported) {
1113 printf("unsupported NAND chip, minimum ecc required %d\n"
1114 , chip->ecc_strength_ds);
1118 if ((!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0) &&
1119 mtd->oobsize < 1024) || nand_info->legacy_bch_geometry) {
1120 dev_warn(this->dev, "use legacy bch geometry\n");
1121 return mxs_nand_legacy_calc_ecc_layout(geo, mtd);
1124 if (mtd->oobsize > 1024 || chip->ecc_step_ds < mtd->oobsize)
1125 return mxs_nand_calc_ecc_for_large_oob(geo, mtd);
1127 return mxs_nand_calc_ecc_layout_by_info(geo, mtd,
1128 chip->ecc_strength_ds, chip->ecc_step_ds);
1134 * At this point, the physical NAND Flash chips have been identified and
1135 * counted, so we know the physical geometry. This enables us to make some
1136 * important configuration decisions.
1138 * The return value of this function propagates directly back to this driver's
1139 * board_nand_init(). Anything other than zero will cause this driver to
1140 * tear everything down and declare failure.
1142 int mxs_nand_setup_ecc(struct mtd_info *mtd)
1144 struct nand_chip *nand = mtd_to_nand(mtd);
1145 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
1146 struct bch_geometry *geo = &nand_info->bch_geometry;
1147 struct mxs_bch_regs *bch_regs = nand_info->bch_regs;
1151 nand_info->en_randomizer = 0;
1152 nand_info->oobsize = mtd->oobsize;
1153 nand_info->writesize = mtd->writesize;
1155 ret = mxs_nand_set_geometry(mtd, geo);
1159 /* Configure BCH and set NFC geometry */
1160 mxs_reset_block(&bch_regs->hw_bch_ctrl_reg);
1162 /* Configure layout 0 */
1163 tmp = (geo->ecc_chunk_count - 1) << BCH_FLASHLAYOUT0_NBLOCKS_OFFSET;
1164 tmp |= MXS_NAND_METADATA_SIZE << BCH_FLASHLAYOUT0_META_SIZE_OFFSET;
1165 tmp |= (geo->ecc_strength >> 1) << BCH_FLASHLAYOUT0_ECC0_OFFSET;
1166 tmp |= geo->ecc_chunk0_size >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT;
1167 tmp |= (geo->gf_len == 14 ? 1 : 0) <<
1168 BCH_FLASHLAYOUT0_GF13_0_GF14_1_OFFSET;
1169 writel(tmp, &bch_regs->hw_bch_flash0layout0);
1170 nand_info->bch_flash0layout0 = tmp;
1172 tmp = (mtd->writesize + mtd->oobsize)
1173 << BCH_FLASHLAYOUT1_PAGE_SIZE_OFFSET;
1174 tmp |= (geo->ecc_strength >> 1) << BCH_FLASHLAYOUT1_ECCN_OFFSET;
1175 tmp |= geo->ecc_chunkn_size >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT;
1176 tmp |= (geo->gf_len == 14 ? 1 : 0) <<
1177 BCH_FLASHLAYOUT1_GF13_0_GF14_1_OFFSET;
1178 writel(tmp, &bch_regs->hw_bch_flash0layout1);
1179 nand_info->bch_flash0layout1 = tmp;
1181 /* Set erase threshold to ecc strength for mx6ul, mx6qp and mx7 */
1182 if (is_mx6dqp() || is_mx7() ||
1183 is_mx6ul() || is_imx8() || is_imx8m())
1184 writel(BCH_MODE_ERASE_THRESHOLD(geo->ecc_strength),
1185 &bch_regs->hw_bch_mode);
1187 /* Set *all* chip selects to use layout 0 */
1188 writel(0, &bch_regs->hw_bch_layoutselect);
1190 /* Enable BCH complete interrupt */
1191 writel(BCH_CTRL_COMPLETE_IRQ_EN, &bch_regs->hw_bch_ctrl_set);
1193 /* Hook some operations at the MTD level. */
1194 if (mtd->_read_oob != mxs_nand_hook_read_oob) {
1195 nand_info->hooked_read_oob = mtd->_read_oob;
1196 mtd->_read_oob = mxs_nand_hook_read_oob;
1199 if (mtd->_write_oob != mxs_nand_hook_write_oob) {
1200 nand_info->hooked_write_oob = mtd->_write_oob;
1201 mtd->_write_oob = mxs_nand_hook_write_oob;
1204 if (mtd->_block_markbad != mxs_nand_hook_block_markbad) {
1205 nand_info->hooked_block_markbad = mtd->_block_markbad;
1206 mtd->_block_markbad = mxs_nand_hook_block_markbad;
1213 * Allocate DMA buffers
1215 int mxs_nand_alloc_buffers(struct mxs_nand_info *nand_info)
1218 const int size = NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE;
1220 nand_info->data_buf_size = roundup(size, MXS_DMA_ALIGNMENT);
1223 buf = memalign(MXS_DMA_ALIGNMENT, nand_info->data_buf_size);
1225 printf("MXS NAND: Error allocating DMA buffers\n");
1229 memset(buf, 0, nand_info->data_buf_size);
1231 nand_info->data_buf = buf;
1232 nand_info->oob_buf = buf + NAND_MAX_PAGESIZE;
1233 /* Command buffers */
1234 nand_info->cmd_buf = memalign(MXS_DMA_ALIGNMENT,
1235 MXS_NAND_COMMAND_BUFFER_SIZE);
1236 if (!nand_info->cmd_buf) {
1238 printf("MXS NAND: Error allocating command buffers\n");
1241 memset(nand_info->cmd_buf, 0, MXS_NAND_COMMAND_BUFFER_SIZE);
1242 nand_info->cmd_queue_len = 0;
1248 * Initializes the NFC hardware.
1250 static int mxs_nand_init_dma(struct mxs_nand_info *info)
1252 int i = 0, j, ret = 0;
1254 info->desc = malloc(sizeof(struct mxs_dma_desc *) *
1255 MXS_NAND_DMA_DESCRIPTOR_COUNT);
1261 /* Allocate the DMA descriptors. */
1262 for (i = 0; i < MXS_NAND_DMA_DESCRIPTOR_COUNT; i++) {
1263 info->desc[i] = mxs_dma_desc_alloc();
1264 if (!info->desc[i]) {
1270 /* Init the DMA controller. */
1272 for (j = MXS_DMA_CHANNEL_AHB_APBH_GPMI0;
1273 j <= MXS_DMA_CHANNEL_AHB_APBH_GPMI7; j++) {
1274 ret = mxs_dma_init_channel(j);
1279 /* Reset the GPMI block. */
1280 mxs_reset_block(&info->gpmi_regs->hw_gpmi_ctrl0_reg);
1281 mxs_reset_block(&info->bch_regs->hw_bch_ctrl_reg);
1284 * Choose NAND mode, set IRQ polarity, disable write protection and
1287 clrsetbits_le32(&info->gpmi_regs->hw_gpmi_ctrl1,
1288 GPMI_CTRL1_GPMI_MODE,
1289 GPMI_CTRL1_ATA_IRQRDY_POLARITY | GPMI_CTRL1_DEV_RESET |
1290 GPMI_CTRL1_BCH_MODE);
1295 for (--j; j >= MXS_DMA_CHANNEL_AHB_APBH_GPMI0; j--)
1298 for (--i; i >= 0; i--)
1299 mxs_dma_desc_free(info->desc[i]);
1303 printf("MXS NAND: Unable to allocate DMA descriptors\n");
1307 int mxs_nand_init_spl(struct nand_chip *nand)
1309 struct mxs_nand_info *nand_info;
1312 nand_info = malloc(sizeof(struct mxs_nand_info));
1314 printf("MXS NAND: Failed to allocate private data\n");
1317 memset(nand_info, 0, sizeof(struct mxs_nand_info));
1319 nand_info->gpmi_regs = (struct mxs_gpmi_regs *)MXS_GPMI_BASE;
1320 nand_info->bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
1322 if (is_mx6sx() || is_mx7() || is_imx8() || is_imx8m())
1323 nand_info->max_ecc_strength_supported = 62;
1325 nand_info->max_ecc_strength_supported = 40;
1327 err = mxs_nand_alloc_buffers(nand_info);
1331 err = mxs_nand_init_dma(nand_info);
1335 nand_set_controller_data(nand, nand_info);
1337 nand->options |= NAND_NO_SUBPAGE_WRITE;
1339 nand->cmd_ctrl = mxs_nand_cmd_ctrl;
1340 nand->dev_ready = mxs_nand_device_ready;
1341 nand->select_chip = mxs_nand_select_chip;
1343 nand->read_byte = mxs_nand_read_byte;
1344 nand->read_buf = mxs_nand_read_buf;
1346 nand->ecc.read_page = mxs_nand_ecc_read_page;
1348 nand->ecc.mode = NAND_ECC_HW;
1353 int mxs_nand_init_ctrl(struct mxs_nand_info *nand_info)
1355 struct mtd_info *mtd;
1356 struct nand_chip *nand;
1359 nand = &nand_info->chip;
1360 mtd = nand_to_mtd(nand);
1361 err = mxs_nand_alloc_buffers(nand_info);
1365 err = mxs_nand_init_dma(nand_info);
1367 goto err_free_buffers;
1369 memset(&fake_ecc_layout, 0, sizeof(fake_ecc_layout));
1371 #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1372 nand->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
1375 nand_set_controller_data(nand, nand_info);
1376 nand->options |= NAND_NO_SUBPAGE_WRITE;
1379 nand->flash_node = dev_of_offset(nand_info->dev);
1381 nand->cmd_ctrl = mxs_nand_cmd_ctrl;
1383 nand->dev_ready = mxs_nand_device_ready;
1384 nand->select_chip = mxs_nand_select_chip;
1385 nand->block_bad = mxs_nand_block_bad;
1387 nand->read_byte = mxs_nand_read_byte;
1389 nand->read_buf = mxs_nand_read_buf;
1390 nand->write_buf = mxs_nand_write_buf;
1392 /* first scan to find the device and get the page size */
1393 if (nand_scan_ident(mtd, CONFIG_SYS_MAX_NAND_DEVICE, NULL))
1394 goto err_free_buffers;
1396 if (mxs_nand_setup_ecc(mtd))
1397 goto err_free_buffers;
1399 nand->ecc.read_page = mxs_nand_ecc_read_page;
1400 nand->ecc.write_page = mxs_nand_ecc_write_page;
1401 nand->ecc.read_oob = mxs_nand_ecc_read_oob;
1402 nand->ecc.write_oob = mxs_nand_ecc_write_oob;
1404 nand->ecc.layout = &fake_ecc_layout;
1405 nand->ecc.mode = NAND_ECC_HW;
1406 nand->ecc.size = nand_info->bch_geometry.ecc_chunkn_size;
1407 nand->ecc.strength = nand_info->bch_geometry.ecc_strength;
1409 /* second phase scan */
1410 err = nand_scan_tail(mtd);
1412 goto err_free_buffers;
1414 err = nand_register(0, mtd);
1416 goto err_free_buffers;
1421 free(nand_info->data_buf);
1422 free(nand_info->cmd_buf);
1427 #ifndef CONFIG_NAND_MXS_DT
1428 void board_nand_init(void)
1430 struct mxs_nand_info *nand_info;
1432 nand_info = malloc(sizeof(struct mxs_nand_info));
1434 printf("MXS NAND: Failed to allocate private data\n");
1437 memset(nand_info, 0, sizeof(struct mxs_nand_info));
1439 nand_info->gpmi_regs = (struct mxs_gpmi_regs *)MXS_GPMI_BASE;
1440 nand_info->bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
1442 /* Refer to Chapter 17 for i.MX6DQ, Chapter 18 for i.MX6SX */
1443 if (is_mx6sx() || is_mx7())
1444 nand_info->max_ecc_strength_supported = 62;
1446 nand_info->max_ecc_strength_supported = 40;
1448 #ifdef CONFIG_NAND_MXS_USE_MINIMUM_ECC
1449 nand_info->use_minimum_ecc = true;
1452 if (mxs_nand_init_ctrl(nand_info) < 0)
1463 * Read NAND layout for FCB block generation.
1465 void mxs_nand_get_layout(struct mtd_info *mtd, struct mxs_nand_layout *l)
1467 struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
1470 tmp = readl(&bch_regs->hw_bch_flash0layout0);
1471 l->nblocks = (tmp & BCH_FLASHLAYOUT0_NBLOCKS_MASK) >>
1472 BCH_FLASHLAYOUT0_NBLOCKS_OFFSET;
1473 l->meta_size = (tmp & BCH_FLASHLAYOUT0_META_SIZE_MASK) >>
1474 BCH_FLASHLAYOUT0_META_SIZE_OFFSET;
1476 tmp = readl(&bch_regs->hw_bch_flash0layout1);
1477 l->data0_size = 4 * ((tmp & BCH_FLASHLAYOUT0_DATA0_SIZE_MASK) >>
1478 BCH_FLASHLAYOUT0_DATA0_SIZE_OFFSET);
1479 l->ecc0 = (tmp & BCH_FLASHLAYOUT0_ECC0_MASK) >>
1480 BCH_FLASHLAYOUT0_ECC0_OFFSET;
1481 l->datan_size = 4 * ((tmp & BCH_FLASHLAYOUT1_DATAN_SIZE_MASK) >>
1482 BCH_FLASHLAYOUT1_DATAN_SIZE_OFFSET);
1483 l->eccn = (tmp & BCH_FLASHLAYOUT1_ECCN_MASK) >>
1484 BCH_FLASHLAYOUT1_ECCN_OFFSET;
1485 l->gf_len = (tmp & BCH_FLASHLAYOUT1_GF13_0_GF14_1_MASK) >>
1486 BCH_FLASHLAYOUT1_GF13_0_GF14_1_OFFSET;
1490 * Set BCH to specific layout used by ROM bootloader to read FCB.
1492 void mxs_nand_mode_fcb(struct mtd_info *mtd)
1495 struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
1496 struct nand_chip *nand = mtd_to_nand(mtd);
1497 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
1499 nand_info->en_randomizer = 1;
1501 mtd->writesize = 1024;
1502 mtd->oobsize = 1862 - 1024;
1505 tmp = 7 << BCH_FLASHLAYOUT0_NBLOCKS_OFFSET;
1506 /* 32 bytes for metadata */
1507 tmp |= 32 << BCH_FLASHLAYOUT0_META_SIZE_OFFSET;
1508 /* using ECC62 level to be performed */
1509 tmp |= 0x1F << BCH_FLASHLAYOUT0_ECC0_OFFSET;
1510 /* 0x20 * 4 bytes of the data0 block */
1511 tmp |= 0x20 << BCH_FLASHLAYOUT0_DATA0_SIZE_OFFSET;
1512 tmp |= 0 << BCH_FLASHLAYOUT0_GF13_0_GF14_1_OFFSET;
1513 writel(tmp, &bch_regs->hw_bch_flash0layout0);
1515 /* 1024 for data + 838 for OOB */
1516 tmp = 1862 << BCH_FLASHLAYOUT1_PAGE_SIZE_OFFSET;
1517 /* using ECC62 level to be performed */
1518 tmp |= 0x1F << BCH_FLASHLAYOUT1_ECCN_OFFSET;
1519 /* 0x20 * 4 bytes of the data0 block */
1520 tmp |= 0x20 << BCH_FLASHLAYOUT1_DATAN_SIZE_OFFSET;
1521 tmp |= 0 << BCH_FLASHLAYOUT1_GF13_0_GF14_1_OFFSET;
1522 writel(tmp, &bch_regs->hw_bch_flash0layout1);
1526 * Restore BCH to normal settings.
1528 void mxs_nand_mode_normal(struct mtd_info *mtd)
1530 struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
1531 struct nand_chip *nand = mtd_to_nand(mtd);
1532 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
1534 nand_info->en_randomizer = 0;
1536 mtd->writesize = nand_info->writesize;
1537 mtd->oobsize = nand_info->oobsize;
1539 writel(nand_info->bch_flash0layout0, &bch_regs->hw_bch_flash0layout0);
1540 writel(nand_info->bch_flash0layout1, &bch_regs->hw_bch_flash0layout1);
1543 uint32_t mxs_nand_mark_byte_offset(struct mtd_info *mtd)
1545 struct nand_chip *chip = mtd_to_nand(mtd);
1546 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
1547 struct bch_geometry *geo = &nand_info->bch_geometry;
1549 return geo->block_mark_byte_offset;
1552 uint32_t mxs_nand_mark_bit_offset(struct mtd_info *mtd)
1554 struct nand_chip *chip = mtd_to_nand(mtd);
1555 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
1556 struct bch_geometry *geo = &nand_info->bch_geometry;
1558 return geo->block_mark_bit_offset;