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.
16 #include <linux/mtd/mtd.h>
17 #include <linux/mtd/rawnand.h>
18 #include <linux/types.h>
21 #include <linux/errno.h>
23 #include <asm/arch/clock.h>
24 #include <asm/arch/imx-regs.h>
25 #include <asm/mach-imx/regs-bch.h>
26 #include <asm/mach-imx/regs-gpmi.h>
27 #include <asm/arch/sys_proto.h>
28 #include <asm/mach-imx/dma.h>
31 #define MXS_NAND_DMA_DESCRIPTOR_COUNT 4
33 #define MXS_NAND_CHUNK_DATA_CHUNK_SIZE 512
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 mxs_nand_info {
51 struct nand_chip chip;
54 uint32_t cmd_queue_len;
55 uint32_t data_buf_size;
61 uint8_t marking_block_bad;
64 /* Functions with altered behaviour */
65 int (*hooked_read_oob)(struct mtd_info *mtd,
66 loff_t from, struct mtd_oob_ops *ops);
67 int (*hooked_write_oob)(struct mtd_info *mtd,
68 loff_t to, struct mtd_oob_ops *ops);
69 int (*hooked_block_markbad)(struct mtd_info *mtd,
73 struct mxs_dma_desc **desc;
77 struct nand_ecclayout fake_ecc_layout;
78 static int chunk_data_size = MXS_NAND_CHUNK_DATA_CHUNK_SIZE;
79 static int galois_field = 13;
82 * Cache management functions
84 #ifndef CONFIG_SYS_DCACHE_OFF
85 static void mxs_nand_flush_data_buf(struct mxs_nand_info *info)
87 uint32_t addr = (uint32_t)info->data_buf;
89 flush_dcache_range(addr, addr + info->data_buf_size);
92 static void mxs_nand_inval_data_buf(struct mxs_nand_info *info)
94 uint32_t addr = (uint32_t)info->data_buf;
96 invalidate_dcache_range(addr, addr + info->data_buf_size);
99 static void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info)
101 uint32_t addr = (uint32_t)info->cmd_buf;
103 flush_dcache_range(addr, addr + MXS_NAND_COMMAND_BUFFER_SIZE);
106 static inline void mxs_nand_flush_data_buf(struct mxs_nand_info *info) {}
107 static inline void mxs_nand_inval_data_buf(struct mxs_nand_info *info) {}
108 static inline void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info) {}
111 static struct mxs_dma_desc *mxs_nand_get_dma_desc(struct mxs_nand_info *info)
113 struct mxs_dma_desc *desc;
115 if (info->desc_index >= MXS_NAND_DMA_DESCRIPTOR_COUNT) {
116 printf("MXS NAND: Too many DMA descriptors requested\n");
120 desc = info->desc[info->desc_index];
126 static void mxs_nand_return_dma_descs(struct mxs_nand_info *info)
129 struct mxs_dma_desc *desc;
131 for (i = 0; i < info->desc_index; i++) {
132 desc = info->desc[i];
133 memset(desc, 0, sizeof(struct mxs_dma_desc));
134 desc->address = (dma_addr_t)desc;
137 info->desc_index = 0;
140 static uint32_t mxs_nand_ecc_chunk_cnt(uint32_t page_data_size)
142 return page_data_size / chunk_data_size;
145 static uint32_t mxs_nand_ecc_size_in_bits(uint32_t ecc_strength)
147 return ecc_strength * galois_field;
150 static uint32_t mxs_nand_aux_status_offset(void)
152 return (MXS_NAND_METADATA_SIZE + 0x3) & ~0x3;
155 static inline uint32_t mxs_nand_get_ecc_strength(uint32_t page_data_size,
156 uint32_t page_oob_size)
159 int max_ecc_strength_supported;
161 /* Refer to Chapter 17 for i.MX6DQ, Chapter 18 for i.MX6SX */
162 if (is_mx6sx() || is_mx7())
163 max_ecc_strength_supported = 62;
165 max_ecc_strength_supported = 40;
168 * Determine the ECC layout with the formula:
169 * ECC bits per chunk = (total page spare data bits) /
170 * (bits per ECC level) / (chunks per page)
172 * total page spare data bits =
173 * (page oob size - meta data size) * (bits per byte)
175 ecc_strength = ((page_oob_size - MXS_NAND_METADATA_SIZE) * 8)
177 mxs_nand_ecc_chunk_cnt(page_data_size));
179 return min(round_down(ecc_strength, 2), max_ecc_strength_supported);
182 static inline uint32_t mxs_nand_get_mark_offset(uint32_t page_data_size,
183 uint32_t ecc_strength)
185 uint32_t chunk_data_size_in_bits;
186 uint32_t chunk_ecc_size_in_bits;
187 uint32_t chunk_total_size_in_bits;
188 uint32_t block_mark_chunk_number;
189 uint32_t block_mark_chunk_bit_offset;
190 uint32_t block_mark_bit_offset;
192 chunk_data_size_in_bits = chunk_data_size * 8;
193 chunk_ecc_size_in_bits = mxs_nand_ecc_size_in_bits(ecc_strength);
195 chunk_total_size_in_bits =
196 chunk_data_size_in_bits + chunk_ecc_size_in_bits;
198 /* Compute the bit offset of the block mark within the physical page. */
199 block_mark_bit_offset = page_data_size * 8;
201 /* Subtract the metadata bits. */
202 block_mark_bit_offset -= MXS_NAND_METADATA_SIZE * 8;
205 * Compute the chunk number (starting at zero) in which the block mark
208 block_mark_chunk_number =
209 block_mark_bit_offset / chunk_total_size_in_bits;
212 * Compute the bit offset of the block mark within its chunk, and
215 block_mark_chunk_bit_offset = block_mark_bit_offset -
216 (block_mark_chunk_number * chunk_total_size_in_bits);
218 if (block_mark_chunk_bit_offset > chunk_data_size_in_bits)
222 * Now that we know the chunk number in which the block mark appears,
223 * we can subtract all the ECC bits that appear before it.
225 block_mark_bit_offset -=
226 block_mark_chunk_number * chunk_ecc_size_in_bits;
228 return block_mark_bit_offset;
231 static uint32_t mxs_nand_mark_byte_offset(struct mtd_info *mtd)
233 uint32_t ecc_strength;
234 ecc_strength = mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize);
235 return mxs_nand_get_mark_offset(mtd->writesize, ecc_strength) >> 3;
238 static uint32_t mxs_nand_mark_bit_offset(struct mtd_info *mtd)
240 uint32_t ecc_strength;
241 ecc_strength = mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize);
242 return mxs_nand_get_mark_offset(mtd->writesize, ecc_strength) & 0x7;
246 * Wait for BCH complete IRQ and clear the IRQ
248 static int mxs_nand_wait_for_bch_complete(void)
250 struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
251 int timeout = MXS_NAND_BCH_TIMEOUT;
254 ret = mxs_wait_mask_set(&bch_regs->hw_bch_ctrl_reg,
255 BCH_CTRL_COMPLETE_IRQ, timeout);
257 writel(BCH_CTRL_COMPLETE_IRQ, &bch_regs->hw_bch_ctrl_clr);
263 * This is the function that we install in the cmd_ctrl function pointer of the
264 * owning struct nand_chip. The only functions in the reference implementation
265 * that use these functions pointers are cmdfunc and select_chip.
267 * In this driver, we implement our own select_chip, so this function will only
268 * be called by the reference implementation's cmdfunc. For this reason, we can
269 * ignore the chip enable bit and concentrate only on sending bytes to the NAND
272 static void mxs_nand_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
274 struct nand_chip *nand = mtd_to_nand(mtd);
275 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
276 struct mxs_dma_desc *d;
277 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
281 * If this condition is true, something is _VERY_ wrong in MTD
284 if (nand_info->cmd_queue_len == MXS_NAND_COMMAND_BUFFER_SIZE) {
285 printf("MXS NAND: Command queue too long\n");
290 * Every operation begins with a command byte and a series of zero or
291 * more address bytes. These are distinguished by either the Address
292 * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
293 * asserted. When MTD is ready to execute the command, it will
294 * deasert both latch enables.
296 * Rather than run a separate DMA operation for every single byte, we
297 * queue them up and run a single DMA operation for the entire series
298 * of command and data bytes.
300 if (ctrl & (NAND_ALE | NAND_CLE)) {
301 if (data != NAND_CMD_NONE)
302 nand_info->cmd_buf[nand_info->cmd_queue_len++] = data;
307 * If control arrives here, MTD has deasserted both the ALE and CLE,
308 * which means it's ready to run an operation. Check if we have any
311 if (nand_info->cmd_queue_len == 0)
314 /* Compile the DMA descriptor -- a descriptor that sends command. */
315 d = mxs_nand_get_dma_desc(nand_info);
317 MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ |
318 MXS_DMA_DESC_CHAIN | MXS_DMA_DESC_DEC_SEM |
319 MXS_DMA_DESC_WAIT4END | (3 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
320 (nand_info->cmd_queue_len << MXS_DMA_DESC_BYTES_OFFSET);
322 d->cmd.address = (dma_addr_t)nand_info->cmd_buf;
324 d->cmd.pio_words[0] =
325 GPMI_CTRL0_COMMAND_MODE_WRITE |
326 GPMI_CTRL0_WORD_LENGTH |
327 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
328 GPMI_CTRL0_ADDRESS_NAND_CLE |
329 GPMI_CTRL0_ADDRESS_INCREMENT |
330 nand_info->cmd_queue_len;
332 mxs_dma_desc_append(channel, d);
335 mxs_nand_flush_cmd_buf(nand_info);
337 /* Execute the DMA chain. */
338 ret = mxs_dma_go(channel);
340 printf("MXS NAND: Error sending command\n");
342 mxs_nand_return_dma_descs(nand_info);
344 /* Reset the command queue. */
345 nand_info->cmd_queue_len = 0;
349 * Test if the NAND flash is ready.
351 static int mxs_nand_device_ready(struct mtd_info *mtd)
353 struct nand_chip *chip = mtd_to_nand(mtd);
354 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
355 struct mxs_gpmi_regs *gpmi_regs =
356 (struct mxs_gpmi_regs *)MXS_GPMI_BASE;
359 tmp = readl(&gpmi_regs->hw_gpmi_stat);
360 tmp >>= (GPMI_STAT_READY_BUSY_OFFSET + nand_info->cur_chip);
366 * Select the NAND chip.
368 static void mxs_nand_select_chip(struct mtd_info *mtd, int chip)
370 struct nand_chip *nand = mtd_to_nand(mtd);
371 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
373 nand_info->cur_chip = chip;
377 * Handle block mark swapping.
379 * Note that, when this function is called, it doesn't know whether it's
380 * swapping the block mark, or swapping it *back* -- but it doesn't matter
381 * because the the operation is the same.
383 static void mxs_nand_swap_block_mark(struct mtd_info *mtd,
384 uint8_t *data_buf, uint8_t *oob_buf)
392 bit_offset = mxs_nand_mark_bit_offset(mtd);
393 buf_offset = mxs_nand_mark_byte_offset(mtd);
396 * Get the byte from the data area that overlays the block mark. Since
397 * the ECC engine applies its own view to the bits in the page, the
398 * physical block mark won't (in general) appear on a byte boundary in
401 src = data_buf[buf_offset] >> bit_offset;
402 src |= data_buf[buf_offset + 1] << (8 - bit_offset);
408 data_buf[buf_offset] &= ~(0xff << bit_offset);
409 data_buf[buf_offset + 1] &= 0xff << bit_offset;
411 data_buf[buf_offset] |= dst << bit_offset;
412 data_buf[buf_offset + 1] |= dst >> (8 - bit_offset);
416 * Read data from NAND.
418 static void mxs_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int length)
420 struct nand_chip *nand = mtd_to_nand(mtd);
421 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
422 struct mxs_dma_desc *d;
423 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
426 if (length > NAND_MAX_PAGESIZE) {
427 printf("MXS NAND: DMA buffer too big\n");
432 printf("MXS NAND: DMA buffer is NULL\n");
436 /* Compile the DMA descriptor - a descriptor that reads data. */
437 d = mxs_nand_get_dma_desc(nand_info);
439 MXS_DMA_DESC_COMMAND_DMA_WRITE | MXS_DMA_DESC_IRQ |
440 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
441 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
442 (length << MXS_DMA_DESC_BYTES_OFFSET);
444 d->cmd.address = (dma_addr_t)nand_info->data_buf;
446 d->cmd.pio_words[0] =
447 GPMI_CTRL0_COMMAND_MODE_READ |
448 GPMI_CTRL0_WORD_LENGTH |
449 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
450 GPMI_CTRL0_ADDRESS_NAND_DATA |
453 mxs_dma_desc_append(channel, d);
456 * A DMA descriptor that waits for the command to end and the chip to
459 * I think we actually should *not* be waiting for the chip to become
460 * ready because, after all, we don't care. I think the original code
461 * did that and no one has re-thought it yet.
463 d = mxs_nand_get_dma_desc(nand_info);
465 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
466 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_DEC_SEM |
467 MXS_DMA_DESC_WAIT4END | (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
471 d->cmd.pio_words[0] =
472 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
473 GPMI_CTRL0_WORD_LENGTH |
474 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
475 GPMI_CTRL0_ADDRESS_NAND_DATA;
477 mxs_dma_desc_append(channel, d);
479 /* Invalidate caches */
480 mxs_nand_inval_data_buf(nand_info);
482 /* Execute the DMA chain. */
483 ret = mxs_dma_go(channel);
485 printf("MXS NAND: DMA read error\n");
489 /* Invalidate caches */
490 mxs_nand_inval_data_buf(nand_info);
492 memcpy(buf, nand_info->data_buf, length);
495 mxs_nand_return_dma_descs(nand_info);
499 * Write data to NAND.
501 static void mxs_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf,
504 struct nand_chip *nand = mtd_to_nand(mtd);
505 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
506 struct mxs_dma_desc *d;
507 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
510 if (length > NAND_MAX_PAGESIZE) {
511 printf("MXS NAND: DMA buffer too big\n");
516 printf("MXS NAND: DMA buffer is NULL\n");
520 memcpy(nand_info->data_buf, buf, length);
522 /* Compile the DMA descriptor - a descriptor that writes data. */
523 d = mxs_nand_get_dma_desc(nand_info);
525 MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ |
526 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
527 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
528 (length << MXS_DMA_DESC_BYTES_OFFSET);
530 d->cmd.address = (dma_addr_t)nand_info->data_buf;
532 d->cmd.pio_words[0] =
533 GPMI_CTRL0_COMMAND_MODE_WRITE |
534 GPMI_CTRL0_WORD_LENGTH |
535 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
536 GPMI_CTRL0_ADDRESS_NAND_DATA |
539 mxs_dma_desc_append(channel, d);
542 mxs_nand_flush_data_buf(nand_info);
544 /* Execute the DMA chain. */
545 ret = mxs_dma_go(channel);
547 printf("MXS NAND: DMA write error\n");
549 mxs_nand_return_dma_descs(nand_info);
553 * Read a single byte from NAND.
555 static uint8_t mxs_nand_read_byte(struct mtd_info *mtd)
558 mxs_nand_read_buf(mtd, &buf, 1);
563 * Read a page from NAND.
565 static int mxs_nand_ecc_read_page(struct mtd_info *mtd, struct nand_chip *nand,
566 uint8_t *buf, int oob_required,
569 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
570 struct mxs_dma_desc *d;
571 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
572 uint32_t corrected = 0, failed = 0;
576 /* Compile the DMA descriptor - wait for ready. */
577 d = mxs_nand_get_dma_desc(nand_info);
579 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
580 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
581 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
585 d->cmd.pio_words[0] =
586 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
587 GPMI_CTRL0_WORD_LENGTH |
588 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
589 GPMI_CTRL0_ADDRESS_NAND_DATA;
591 mxs_dma_desc_append(channel, d);
593 /* Compile the DMA descriptor - enable the BCH block and read. */
594 d = mxs_nand_get_dma_desc(nand_info);
596 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
597 MXS_DMA_DESC_WAIT4END | (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
601 d->cmd.pio_words[0] =
602 GPMI_CTRL0_COMMAND_MODE_READ |
603 GPMI_CTRL0_WORD_LENGTH |
604 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
605 GPMI_CTRL0_ADDRESS_NAND_DATA |
606 (mtd->writesize + mtd->oobsize);
607 d->cmd.pio_words[1] = 0;
608 d->cmd.pio_words[2] =
609 GPMI_ECCCTRL_ENABLE_ECC |
610 GPMI_ECCCTRL_ECC_CMD_DECODE |
611 GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
612 d->cmd.pio_words[3] = mtd->writesize + mtd->oobsize;
613 d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
614 d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;
616 mxs_dma_desc_append(channel, d);
618 /* Compile the DMA descriptor - disable the BCH block. */
619 d = mxs_nand_get_dma_desc(nand_info);
621 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
622 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
623 (3 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
627 d->cmd.pio_words[0] =
628 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
629 GPMI_CTRL0_WORD_LENGTH |
630 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
631 GPMI_CTRL0_ADDRESS_NAND_DATA |
632 (mtd->writesize + mtd->oobsize);
633 d->cmd.pio_words[1] = 0;
634 d->cmd.pio_words[2] = 0;
636 mxs_dma_desc_append(channel, d);
638 /* Compile the DMA descriptor - deassert the NAND lock and interrupt. */
639 d = mxs_nand_get_dma_desc(nand_info);
641 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
642 MXS_DMA_DESC_DEC_SEM;
646 mxs_dma_desc_append(channel, d);
648 /* Invalidate caches */
649 mxs_nand_inval_data_buf(nand_info);
651 /* Execute the DMA chain. */
652 ret = mxs_dma_go(channel);
654 printf("MXS NAND: DMA read error\n");
658 ret = mxs_nand_wait_for_bch_complete();
660 printf("MXS NAND: BCH read timeout\n");
664 /* Invalidate caches */
665 mxs_nand_inval_data_buf(nand_info);
667 /* Read DMA completed, now do the mark swapping. */
668 mxs_nand_swap_block_mark(mtd, nand_info->data_buf, nand_info->oob_buf);
670 /* Loop over status bytes, accumulating ECC status. */
671 status = nand_info->oob_buf + mxs_nand_aux_status_offset();
672 for (i = 0; i < mxs_nand_ecc_chunk_cnt(mtd->writesize); i++) {
673 if (status[i] == 0x00)
676 if (status[i] == 0xff)
679 if (status[i] == 0xfe) {
684 corrected += status[i];
687 /* Propagate ECC status to the owning MTD. */
688 mtd->ecc_stats.failed += failed;
689 mtd->ecc_stats.corrected += corrected;
692 * It's time to deliver the OOB bytes. See mxs_nand_ecc_read_oob() for
693 * details about our policy for delivering the OOB.
695 * We fill the caller's buffer with set bits, and then copy the block
696 * mark to the caller's buffer. Note that, if block mark swapping was
697 * necessary, it has already been done, so we can rely on the first
698 * byte of the auxiliary buffer to contain the block mark.
700 memset(nand->oob_poi, 0xff, mtd->oobsize);
702 nand->oob_poi[0] = nand_info->oob_buf[0];
704 memcpy(buf, nand_info->data_buf, mtd->writesize);
707 mxs_nand_return_dma_descs(nand_info);
713 * Write a page to NAND.
715 static int mxs_nand_ecc_write_page(struct mtd_info *mtd,
716 struct nand_chip *nand, const uint8_t *buf,
717 int oob_required, int page)
719 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
720 struct mxs_dma_desc *d;
721 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
724 memcpy(nand_info->data_buf, buf, mtd->writesize);
725 memcpy(nand_info->oob_buf, nand->oob_poi, mtd->oobsize);
727 /* Handle block mark swapping. */
728 mxs_nand_swap_block_mark(mtd, nand_info->data_buf, nand_info->oob_buf);
730 /* Compile the DMA descriptor - write data. */
731 d = mxs_nand_get_dma_desc(nand_info);
733 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
734 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
735 (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
739 d->cmd.pio_words[0] =
740 GPMI_CTRL0_COMMAND_MODE_WRITE |
741 GPMI_CTRL0_WORD_LENGTH |
742 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
743 GPMI_CTRL0_ADDRESS_NAND_DATA;
744 d->cmd.pio_words[1] = 0;
745 d->cmd.pio_words[2] =
746 GPMI_ECCCTRL_ENABLE_ECC |
747 GPMI_ECCCTRL_ECC_CMD_ENCODE |
748 GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
749 d->cmd.pio_words[3] = (mtd->writesize + mtd->oobsize);
750 d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
751 d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;
753 mxs_dma_desc_append(channel, d);
756 mxs_nand_flush_data_buf(nand_info);
758 /* Execute the DMA chain. */
759 ret = mxs_dma_go(channel);
761 printf("MXS NAND: DMA write error\n");
765 ret = mxs_nand_wait_for_bch_complete();
767 printf("MXS NAND: BCH write timeout\n");
772 mxs_nand_return_dma_descs(nand_info);
777 * Read OOB from NAND.
779 * This function is a veneer that replaces the function originally installed by
780 * the NAND Flash MTD code.
782 static int mxs_nand_hook_read_oob(struct mtd_info *mtd, loff_t from,
783 struct mtd_oob_ops *ops)
785 struct nand_chip *chip = mtd_to_nand(mtd);
786 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
789 if (ops->mode == MTD_OPS_RAW)
790 nand_info->raw_oob_mode = 1;
792 nand_info->raw_oob_mode = 0;
794 ret = nand_info->hooked_read_oob(mtd, from, ops);
796 nand_info->raw_oob_mode = 0;
804 * This function is a veneer that replaces the function originally installed by
805 * the NAND Flash MTD code.
807 static int mxs_nand_hook_write_oob(struct mtd_info *mtd, loff_t to,
808 struct mtd_oob_ops *ops)
810 struct nand_chip *chip = mtd_to_nand(mtd);
811 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
814 if (ops->mode == MTD_OPS_RAW)
815 nand_info->raw_oob_mode = 1;
817 nand_info->raw_oob_mode = 0;
819 ret = nand_info->hooked_write_oob(mtd, to, ops);
821 nand_info->raw_oob_mode = 0;
827 * Mark a block bad in NAND.
829 * This function is a veneer that replaces the function originally installed by
830 * the NAND Flash MTD code.
832 static int mxs_nand_hook_block_markbad(struct mtd_info *mtd, loff_t ofs)
834 struct nand_chip *chip = mtd_to_nand(mtd);
835 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
838 nand_info->marking_block_bad = 1;
840 ret = nand_info->hooked_block_markbad(mtd, ofs);
842 nand_info->marking_block_bad = 0;
848 * There are several places in this driver where we have to handle the OOB and
849 * block marks. This is the function where things are the most complicated, so
850 * this is where we try to explain it all. All the other places refer back to
853 * These are the rules, in order of decreasing importance:
855 * 1) Nothing the caller does can be allowed to imperil the block mark, so all
856 * write operations take measures to protect it.
858 * 2) In read operations, the first byte of the OOB we return must reflect the
859 * true state of the block mark, no matter where that block mark appears in
862 * 3) ECC-based read operations return an OOB full of set bits (since we never
863 * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
866 * 4) "Raw" read operations return a direct view of the physical bytes in the
867 * page, using the conventional definition of which bytes are data and which
868 * are OOB. This gives the caller a way to see the actual, physical bytes
869 * in the page, without the distortions applied by our ECC engine.
871 * What we do for this specific read operation depends on whether we're doing
872 * "raw" read, or an ECC-based read.
874 * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
875 * easy. When reading a page, for example, the NAND Flash MTD code calls our
876 * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
877 * ECC-based or raw view of the page is implicit in which function it calls
878 * (there is a similar pair of ECC-based/raw functions for writing).
880 * Since MTD assumes the OOB is not covered by ECC, there is no pair of
881 * ECC-based/raw functions for reading or or writing the OOB. The fact that the
882 * caller wants an ECC-based or raw view of the page is not propagated down to
885 * Since our OOB *is* covered by ECC, we need this information. So, we hook the
886 * ecc.read_oob and ecc.write_oob function pointers in the owning
887 * struct mtd_info with our own functions. These hook functions set the
888 * raw_oob_mode field so that, when control finally arrives here, we'll know
891 static int mxs_nand_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *nand,
894 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
897 * First, fill in the OOB buffer. If we're doing a raw read, we need to
898 * get the bytes from the physical page. If we're not doing a raw read,
899 * we need to fill the buffer with set bits.
901 if (nand_info->raw_oob_mode) {
903 * If control arrives here, we're doing a "raw" read. Send the
904 * command to read the conventional OOB and read it.
906 nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
907 nand->read_buf(mtd, nand->oob_poi, mtd->oobsize);
910 * If control arrives here, we're not doing a "raw" read. Fill
911 * the OOB buffer with set bits and correct the block mark.
913 memset(nand->oob_poi, 0xff, mtd->oobsize);
915 nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
916 mxs_nand_read_buf(mtd, nand->oob_poi, 1);
924 * Write OOB data to NAND.
926 static int mxs_nand_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *nand,
929 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
930 uint8_t block_mark = 0;
933 * There are fundamental incompatibilities between the i.MX GPMI NFC and
934 * the NAND Flash MTD model that make it essentially impossible to write
935 * the out-of-band bytes.
937 * We permit *ONE* exception. If the *intent* of writing the OOB is to
938 * mark a block bad, we can do that.
941 if (!nand_info->marking_block_bad) {
942 printf("NXS NAND: Writing OOB isn't supported\n");
946 /* Write the block mark. */
947 nand->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
948 nand->write_buf(mtd, &block_mark, 1);
949 nand->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
951 /* Check if it worked. */
952 if (nand->waitfunc(mtd, nand) & NAND_STATUS_FAIL)
959 * Claims all blocks are good.
961 * In principle, this function is *only* called when the NAND Flash MTD system
962 * isn't allowed to keep an in-memory bad block table, so it is forced to ask
963 * the driver for bad block information.
965 * In fact, we permit the NAND Flash MTD system to have an in-memory BBT, so
966 * this function is *only* called when we take it away.
968 * Thus, this function is only called when we want *all* blocks to look good,
969 * so it *always* return success.
971 static int mxs_nand_block_bad(struct mtd_info *mtd, loff_t ofs)
977 * At this point, the physical NAND Flash chips have been identified and
978 * counted, so we know the physical geometry. This enables us to make some
979 * important configuration decisions.
981 * The return value of this function propagates directly back to this driver's
982 * board_nand_init(). Anything other than zero will cause this driver to
983 * tear everything down and declare failure.
985 int mxs_nand_setup_ecc(struct mtd_info *mtd)
987 struct nand_chip *nand = mtd_to_nand(mtd);
988 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
989 struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
992 if (mtd->oobsize > MXS_NAND_CHUNK_DATA_CHUNK_SIZE) {
994 chunk_data_size = MXS_NAND_CHUNK_DATA_CHUNK_SIZE * 2;
997 if (mtd->oobsize > chunk_data_size) {
998 printf("Not support the NAND chips whose oob size is larger then %d bytes!\n", chunk_data_size);
1002 /* Configure BCH and set NFC geometry */
1003 mxs_reset_block(&bch_regs->hw_bch_ctrl_reg);
1005 /* Configure layout 0 */
1006 tmp = (mxs_nand_ecc_chunk_cnt(mtd->writesize) - 1)
1007 << BCH_FLASHLAYOUT0_NBLOCKS_OFFSET;
1008 tmp |= MXS_NAND_METADATA_SIZE << BCH_FLASHLAYOUT0_META_SIZE_OFFSET;
1009 tmp |= (mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize) >> 1)
1010 << BCH_FLASHLAYOUT0_ECC0_OFFSET;
1011 tmp |= chunk_data_size >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT;
1012 tmp |= (14 == galois_field ? 1 : 0) <<
1013 BCH_FLASHLAYOUT0_GF13_0_GF14_1_OFFSET;
1014 writel(tmp, &bch_regs->hw_bch_flash0layout0);
1016 tmp = (mtd->writesize + mtd->oobsize)
1017 << BCH_FLASHLAYOUT1_PAGE_SIZE_OFFSET;
1018 tmp |= (mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize) >> 1)
1019 << BCH_FLASHLAYOUT1_ECCN_OFFSET;
1020 tmp |= chunk_data_size >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT;
1021 tmp |= (14 == galois_field ? 1 : 0) <<
1022 BCH_FLASHLAYOUT1_GF13_0_GF14_1_OFFSET;
1023 writel(tmp, &bch_regs->hw_bch_flash0layout1);
1025 /* Set *all* chip selects to use layout 0 */
1026 writel(0, &bch_regs->hw_bch_layoutselect);
1028 /* Enable BCH complete interrupt */
1029 writel(BCH_CTRL_COMPLETE_IRQ_EN, &bch_regs->hw_bch_ctrl_set);
1031 /* Hook some operations at the MTD level. */
1032 if (mtd->_read_oob != mxs_nand_hook_read_oob) {
1033 nand_info->hooked_read_oob = mtd->_read_oob;
1034 mtd->_read_oob = mxs_nand_hook_read_oob;
1037 if (mtd->_write_oob != mxs_nand_hook_write_oob) {
1038 nand_info->hooked_write_oob = mtd->_write_oob;
1039 mtd->_write_oob = mxs_nand_hook_write_oob;
1042 if (mtd->_block_markbad != mxs_nand_hook_block_markbad) {
1043 nand_info->hooked_block_markbad = mtd->_block_markbad;
1044 mtd->_block_markbad = mxs_nand_hook_block_markbad;
1051 * Allocate DMA buffers
1053 int mxs_nand_alloc_buffers(struct mxs_nand_info *nand_info)
1056 const int size = NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE;
1058 nand_info->data_buf_size = roundup(size, MXS_DMA_ALIGNMENT);
1061 buf = memalign(MXS_DMA_ALIGNMENT, nand_info->data_buf_size);
1063 printf("MXS NAND: Error allocating DMA buffers\n");
1067 memset(buf, 0, nand_info->data_buf_size);
1069 nand_info->data_buf = buf;
1070 nand_info->oob_buf = buf + NAND_MAX_PAGESIZE;
1071 /* Command buffers */
1072 nand_info->cmd_buf = memalign(MXS_DMA_ALIGNMENT,
1073 MXS_NAND_COMMAND_BUFFER_SIZE);
1074 if (!nand_info->cmd_buf) {
1076 printf("MXS NAND: Error allocating command buffers\n");
1079 memset(nand_info->cmd_buf, 0, MXS_NAND_COMMAND_BUFFER_SIZE);
1080 nand_info->cmd_queue_len = 0;
1086 * Initializes the NFC hardware.
1088 int mxs_nand_init(struct mxs_nand_info *info)
1090 struct mxs_gpmi_regs *gpmi_regs =
1091 (struct mxs_gpmi_regs *)MXS_GPMI_BASE;
1092 struct mxs_bch_regs *bch_regs =
1093 (struct mxs_bch_regs *)MXS_BCH_BASE;
1094 int i = 0, j, ret = 0;
1096 info->desc = malloc(sizeof(struct mxs_dma_desc *) *
1097 MXS_NAND_DMA_DESCRIPTOR_COUNT);
1103 /* Allocate the DMA descriptors. */
1104 for (i = 0; i < MXS_NAND_DMA_DESCRIPTOR_COUNT; i++) {
1105 info->desc[i] = mxs_dma_desc_alloc();
1106 if (!info->desc[i]) {
1112 /* Init the DMA controller. */
1114 for (j = MXS_DMA_CHANNEL_AHB_APBH_GPMI0;
1115 j <= MXS_DMA_CHANNEL_AHB_APBH_GPMI7; j++) {
1116 ret = mxs_dma_init_channel(j);
1121 /* Reset the GPMI block. */
1122 mxs_reset_block(&gpmi_regs->hw_gpmi_ctrl0_reg);
1123 mxs_reset_block(&bch_regs->hw_bch_ctrl_reg);
1126 * Choose NAND mode, set IRQ polarity, disable write protection and
1129 clrsetbits_le32(&gpmi_regs->hw_gpmi_ctrl1,
1130 GPMI_CTRL1_GPMI_MODE,
1131 GPMI_CTRL1_ATA_IRQRDY_POLARITY | GPMI_CTRL1_DEV_RESET |
1132 GPMI_CTRL1_BCH_MODE);
1137 for (--j; j >= MXS_DMA_CHANNEL_AHB_APBH_GPMI0; j--)
1140 for (--i; i >= 0; i--)
1141 mxs_dma_desc_free(info->desc[i]);
1145 printf("MXS NAND: Unable to allocate DMA descriptors\n");
1149 int mxs_nand_init_spl(struct nand_chip *nand)
1151 struct mxs_nand_info *nand_info;
1154 nand_info = malloc(sizeof(struct mxs_nand_info));
1156 printf("MXS NAND: Failed to allocate private data\n");
1159 memset(nand_info, 0, sizeof(struct mxs_nand_info));
1161 err = mxs_nand_alloc_buffers(nand_info);
1165 err = mxs_nand_init(nand_info);
1169 nand_set_controller_data(nand, nand_info);
1171 nand->options |= NAND_NO_SUBPAGE_WRITE;
1173 nand->cmd_ctrl = mxs_nand_cmd_ctrl;
1174 nand->dev_ready = mxs_nand_device_ready;
1175 nand->select_chip = mxs_nand_select_chip;
1177 nand->read_byte = mxs_nand_read_byte;
1178 nand->read_buf = mxs_nand_read_buf;
1180 nand->ecc.read_page = mxs_nand_ecc_read_page;
1182 nand->ecc.mode = NAND_ECC_HW;
1183 nand->ecc.bytes = 9;
1184 nand->ecc.size = 512;
1185 nand->ecc.strength = 8;
1190 void board_nand_init(void)
1192 struct mtd_info *mtd;
1193 struct mxs_nand_info *nand_info;
1194 struct nand_chip *nand;
1197 nand_info = malloc(sizeof(struct mxs_nand_info));
1199 printf("MXS NAND: Failed to allocate private data\n");
1202 memset(nand_info, 0, sizeof(struct mxs_nand_info));
1204 nand = &nand_info->chip;
1205 mtd = nand_to_mtd(nand);
1206 err = mxs_nand_alloc_buffers(nand_info);
1210 err = mxs_nand_init(nand_info);
1214 memset(&fake_ecc_layout, 0, sizeof(fake_ecc_layout));
1216 #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1217 nand->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
1220 nand_set_controller_data(nand, nand_info);
1221 nand->options |= NAND_NO_SUBPAGE_WRITE;
1223 nand->cmd_ctrl = mxs_nand_cmd_ctrl;
1225 nand->dev_ready = mxs_nand_device_ready;
1226 nand->select_chip = mxs_nand_select_chip;
1227 nand->block_bad = mxs_nand_block_bad;
1229 nand->read_byte = mxs_nand_read_byte;
1231 nand->read_buf = mxs_nand_read_buf;
1232 nand->write_buf = mxs_nand_write_buf;
1234 /* first scan to find the device and get the page size */
1235 if (nand_scan_ident(mtd, CONFIG_SYS_MAX_NAND_DEVICE, NULL))
1238 if (mxs_nand_setup_ecc(mtd))
1241 nand->ecc.read_page = mxs_nand_ecc_read_page;
1242 nand->ecc.write_page = mxs_nand_ecc_write_page;
1243 nand->ecc.read_oob = mxs_nand_ecc_read_oob;
1244 nand->ecc.write_oob = mxs_nand_ecc_write_oob;
1246 nand->ecc.layout = &fake_ecc_layout;
1247 nand->ecc.mode = NAND_ECC_HW;
1248 nand->ecc.bytes = 9;
1249 nand->ecc.size = 512;
1250 nand->ecc.strength = 8;
1252 /* second phase scan */
1253 err = nand_scan_tail(mtd);
1257 err = nand_register(0, mtd);
1264 free(nand_info->data_buf);
1265 free(nand_info->cmd_buf);