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.
18 #include <linux/mtd/rawnand.h>
19 #include <linux/sizes.h>
20 #include <linux/types.h>
22 #include <linux/errno.h>
24 #include <asm/arch/clock.h>
25 #include <asm/arch/imx-regs.h>
26 #include <asm/mach-imx/regs-bch.h>
27 #include <asm/mach-imx/regs-gpmi.h>
28 #include <asm/arch/sys_proto.h>
31 #define MXS_NAND_DMA_DESCRIPTOR_COUNT 4
33 #if (defined(CONFIG_MX6) || defined(CONFIG_MX7))
34 #define MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT 2
36 #define MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT 0
38 #define MXS_NAND_METADATA_SIZE 10
39 #define MXS_NAND_BITS_PER_ECC_LEVEL 13
41 #if !defined(CONFIG_SYS_CACHELINE_SIZE) || CONFIG_SYS_CACHELINE_SIZE < 32
42 #define MXS_NAND_COMMAND_BUFFER_SIZE 32
44 #define MXS_NAND_COMMAND_BUFFER_SIZE CONFIG_SYS_CACHELINE_SIZE
47 #define MXS_NAND_BCH_TIMEOUT 10000
49 struct nand_ecclayout fake_ecc_layout;
52 * Cache management functions
54 #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
55 static void mxs_nand_flush_data_buf(struct mxs_nand_info *info)
57 uint32_t addr = (uint32_t)info->data_buf;
59 flush_dcache_range(addr, addr + info->data_buf_size);
62 static void mxs_nand_inval_data_buf(struct mxs_nand_info *info)
64 uint32_t addr = (uint32_t)info->data_buf;
66 invalidate_dcache_range(addr, addr + info->data_buf_size);
69 static void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info)
71 uint32_t addr = (uint32_t)info->cmd_buf;
73 flush_dcache_range(addr, addr + MXS_NAND_COMMAND_BUFFER_SIZE);
76 static inline void mxs_nand_flush_data_buf(struct mxs_nand_info *info) {}
77 static inline void mxs_nand_inval_data_buf(struct mxs_nand_info *info) {}
78 static inline void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info) {}
81 static struct mxs_dma_desc *mxs_nand_get_dma_desc(struct mxs_nand_info *info)
83 struct mxs_dma_desc *desc;
85 if (info->desc_index >= MXS_NAND_DMA_DESCRIPTOR_COUNT) {
86 printf("MXS NAND: Too many DMA descriptors requested\n");
90 desc = info->desc[info->desc_index];
96 static void mxs_nand_return_dma_descs(struct mxs_nand_info *info)
99 struct mxs_dma_desc *desc;
101 for (i = 0; i < info->desc_index; i++) {
102 desc = info->desc[i];
103 memset(desc, 0, sizeof(struct mxs_dma_desc));
104 desc->address = (dma_addr_t)desc;
107 info->desc_index = 0;
110 static uint32_t mxs_nand_aux_status_offset(void)
112 return (MXS_NAND_METADATA_SIZE + 0x3) & ~0x3;
115 static inline bool mxs_nand_bbm_in_data_chunk(struct bch_geometry *geo, struct mtd_info *mtd,
116 unsigned int *chunk_num)
120 if (geo->ecc_chunk0_size != geo->ecc_chunkn_size) {
121 dev_err(this->dev, "The size of chunk0 must equal to chunkn\n");
125 i = (mtd->writesize * 8 - MXS_NAND_METADATA_SIZE * 8) /
126 (geo->gf_len * geo->ecc_strength +
127 geo->ecc_chunkn_size * 8);
129 j = (mtd->writesize * 8 - MXS_NAND_METADATA_SIZE * 8) -
130 (geo->gf_len * geo->ecc_strength +
131 geo->ecc_chunkn_size * 8) * i;
133 if (j < geo->ecc_chunkn_size * 8) {
135 dev_dbg(this->dev, "Set ecc to %d and bbm in chunk %d\n",
136 geo->ecc_strength, *chunk_num);
143 static inline int mxs_nand_calc_ecc_layout_by_info(struct bch_geometry *geo,
144 struct mtd_info *mtd,
145 unsigned int ecc_strength,
146 unsigned int ecc_step)
148 struct nand_chip *chip = mtd_to_nand(mtd);
149 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
150 unsigned int block_mark_bit_offset;
163 geo->ecc_chunk0_size = ecc_step;
164 geo->ecc_chunkn_size = ecc_step;
165 geo->ecc_strength = round_up(ecc_strength, 2);
167 /* Keep the C >= O */
168 if (geo->ecc_chunkn_size < mtd->oobsize)
171 if (geo->ecc_strength > nand_info->max_ecc_strength_supported)
174 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunkn_size;
177 block_mark_bit_offset = mtd->writesize * 8 -
178 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
179 + MXS_NAND_METADATA_SIZE * 8);
181 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
182 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
187 static inline int mxs_nand_legacy_calc_ecc_layout(struct bch_geometry *geo,
188 struct mtd_info *mtd)
190 struct nand_chip *chip = mtd_to_nand(mtd);
191 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
192 unsigned int block_mark_bit_offset;
194 /* The default for the length of Galois Field. */
197 /* The default for chunk size. */
198 geo->ecc_chunk0_size = 512;
199 geo->ecc_chunkn_size = 512;
201 if (geo->ecc_chunkn_size < mtd->oobsize) {
203 geo->ecc_chunk0_size *= 2;
204 geo->ecc_chunkn_size *= 2;
207 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunkn_size;
210 * Determine the ECC layout with the formula:
211 * ECC bits per chunk = (total page spare data bits) /
212 * (bits per ECC level) / (chunks per page)
214 * total page spare data bits =
215 * (page oob size - meta data size) * (bits per byte)
217 geo->ecc_strength = ((mtd->oobsize - MXS_NAND_METADATA_SIZE) * 8)
218 / (geo->gf_len * geo->ecc_chunk_count);
220 geo->ecc_strength = min(round_down(geo->ecc_strength, 2),
221 nand_info->max_ecc_strength_supported);
223 block_mark_bit_offset = mtd->writesize * 8 -
224 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
225 + MXS_NAND_METADATA_SIZE * 8);
227 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
228 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
233 static inline int mxs_nand_calc_ecc_for_large_oob(struct bch_geometry *geo,
234 struct mtd_info *mtd)
236 struct nand_chip *chip = mtd_to_nand(mtd);
237 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
238 unsigned int block_mark_bit_offset;
239 unsigned int max_ecc;
240 unsigned int bbm_chunk;
243 /* sanity check for the minimum ecc nand required */
244 if (!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0))
246 geo->ecc_strength = chip->ecc_strength_ds;
248 /* calculate the maximum ecc platform can support*/
250 geo->ecc_chunk0_size = 1024;
251 geo->ecc_chunkn_size = 1024;
252 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunkn_size;
253 max_ecc = ((mtd->oobsize - MXS_NAND_METADATA_SIZE) * 8)
254 / (geo->gf_len * geo->ecc_chunk_count);
255 max_ecc = min(round_down(max_ecc, 2),
256 nand_info->max_ecc_strength_supported);
259 /* search a supported ecc strength that makes bbm */
260 /* located in data chunk */
261 geo->ecc_strength = chip->ecc_strength_ds;
262 while (!(geo->ecc_strength > max_ecc)) {
263 if (mxs_nand_bbm_in_data_chunk(geo, mtd, &bbm_chunk))
265 geo->ecc_strength += 2;
268 /* if none of them works, keep using the minimum ecc */
269 /* nand required but changing ecc page layout */
270 if (geo->ecc_strength > max_ecc) {
271 geo->ecc_strength = chip->ecc_strength_ds;
272 /* add extra ecc for meta data */
273 geo->ecc_chunk0_size = 0;
274 geo->ecc_chunk_count = (mtd->writesize / geo->ecc_chunkn_size) + 1;
275 geo->ecc_for_meta = 1;
276 /* check if oob can afford this extra ecc chunk */
277 if (mtd->oobsize * 8 < MXS_NAND_METADATA_SIZE * 8 +
278 geo->gf_len * geo->ecc_strength
279 * geo->ecc_chunk_count) {
280 printf("unsupported NAND chip with new layout\n");
284 /* calculate in which chunk bbm located */
285 bbm_chunk = (mtd->writesize * 8 - MXS_NAND_METADATA_SIZE * 8 -
286 geo->gf_len * geo->ecc_strength) /
287 (geo->gf_len * geo->ecc_strength +
288 geo->ecc_chunkn_size * 8) + 1;
291 /* calculate the number of ecc chunk behind the bbm */
292 i = (mtd->writesize / geo->ecc_chunkn_size) - bbm_chunk + 1;
294 block_mark_bit_offset = mtd->writesize * 8 -
295 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - i)
296 + MXS_NAND_METADATA_SIZE * 8);
298 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
299 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
305 * Wait for BCH complete IRQ and clear the IRQ
307 static int mxs_nand_wait_for_bch_complete(struct mxs_nand_info *nand_info)
309 int timeout = MXS_NAND_BCH_TIMEOUT;
312 ret = mxs_wait_mask_set(&nand_info->bch_regs->hw_bch_ctrl_reg,
313 BCH_CTRL_COMPLETE_IRQ, timeout);
315 writel(BCH_CTRL_COMPLETE_IRQ, &nand_info->bch_regs->hw_bch_ctrl_clr);
321 * This is the function that we install in the cmd_ctrl function pointer of the
322 * owning struct nand_chip. The only functions in the reference implementation
323 * that use these functions pointers are cmdfunc and select_chip.
325 * In this driver, we implement our own select_chip, so this function will only
326 * be called by the reference implementation's cmdfunc. For this reason, we can
327 * ignore the chip enable bit and concentrate only on sending bytes to the NAND
330 static void mxs_nand_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
332 struct nand_chip *nand = mtd_to_nand(mtd);
333 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
334 struct mxs_dma_desc *d;
335 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
339 * If this condition is true, something is _VERY_ wrong in MTD
342 if (nand_info->cmd_queue_len == MXS_NAND_COMMAND_BUFFER_SIZE) {
343 printf("MXS NAND: Command queue too long\n");
348 * Every operation begins with a command byte and a series of zero or
349 * more address bytes. These are distinguished by either the Address
350 * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
351 * asserted. When MTD is ready to execute the command, it will
352 * deasert both latch enables.
354 * Rather than run a separate DMA operation for every single byte, we
355 * queue them up and run a single DMA operation for the entire series
356 * of command and data bytes.
358 if (ctrl & (NAND_ALE | NAND_CLE)) {
359 if (data != NAND_CMD_NONE)
360 nand_info->cmd_buf[nand_info->cmd_queue_len++] = data;
365 * If control arrives here, MTD has deasserted both the ALE and CLE,
366 * which means it's ready to run an operation. Check if we have any
369 if (nand_info->cmd_queue_len == 0)
372 /* Compile the DMA descriptor -- a descriptor that sends command. */
373 d = mxs_nand_get_dma_desc(nand_info);
375 MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ |
376 MXS_DMA_DESC_CHAIN | MXS_DMA_DESC_DEC_SEM |
377 MXS_DMA_DESC_WAIT4END | (3 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
378 (nand_info->cmd_queue_len << MXS_DMA_DESC_BYTES_OFFSET);
380 d->cmd.address = (dma_addr_t)nand_info->cmd_buf;
382 d->cmd.pio_words[0] =
383 GPMI_CTRL0_COMMAND_MODE_WRITE |
384 GPMI_CTRL0_WORD_LENGTH |
385 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
386 GPMI_CTRL0_ADDRESS_NAND_CLE |
387 GPMI_CTRL0_ADDRESS_INCREMENT |
388 nand_info->cmd_queue_len;
390 mxs_dma_desc_append(channel, d);
393 mxs_nand_flush_cmd_buf(nand_info);
395 /* Execute the DMA chain. */
396 ret = mxs_dma_go(channel);
398 printf("MXS NAND: Error sending command\n");
400 mxs_nand_return_dma_descs(nand_info);
402 /* Reset the command queue. */
403 nand_info->cmd_queue_len = 0;
407 * Test if the NAND flash is ready.
409 static int mxs_nand_device_ready(struct mtd_info *mtd)
411 struct nand_chip *chip = mtd_to_nand(mtd);
412 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
415 tmp = readl(&nand_info->gpmi_regs->hw_gpmi_stat);
416 tmp >>= (GPMI_STAT_READY_BUSY_OFFSET + nand_info->cur_chip);
422 * Select the NAND chip.
424 static void mxs_nand_select_chip(struct mtd_info *mtd, int chip)
426 struct nand_chip *nand = mtd_to_nand(mtd);
427 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
429 nand_info->cur_chip = chip;
433 * Handle block mark swapping.
435 * Note that, when this function is called, it doesn't know whether it's
436 * swapping the block mark, or swapping it *back* -- but it doesn't matter
437 * because the the operation is the same.
439 static void mxs_nand_swap_block_mark(struct bch_geometry *geo,
440 uint8_t *data_buf, uint8_t *oob_buf)
442 uint32_t bit_offset = geo->block_mark_bit_offset;
443 uint32_t buf_offset = geo->block_mark_byte_offset;
449 * Get the byte from the data area that overlays the block mark. Since
450 * the ECC engine applies its own view to the bits in the page, the
451 * physical block mark won't (in general) appear on a byte boundary in
454 src = data_buf[buf_offset] >> bit_offset;
455 src |= data_buf[buf_offset + 1] << (8 - bit_offset);
461 data_buf[buf_offset] &= ~(0xff << bit_offset);
462 data_buf[buf_offset + 1] &= 0xff << bit_offset;
464 data_buf[buf_offset] |= dst << bit_offset;
465 data_buf[buf_offset + 1] |= dst >> (8 - bit_offset);
469 * Read data from NAND.
471 static void mxs_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int length)
473 struct nand_chip *nand = mtd_to_nand(mtd);
474 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
475 struct mxs_dma_desc *d;
476 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
479 if (length > NAND_MAX_PAGESIZE) {
480 printf("MXS NAND: DMA buffer too big\n");
485 printf("MXS NAND: DMA buffer is NULL\n");
489 /* Compile the DMA descriptor - a descriptor that reads data. */
490 d = mxs_nand_get_dma_desc(nand_info);
492 MXS_DMA_DESC_COMMAND_DMA_WRITE | MXS_DMA_DESC_IRQ |
493 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
494 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
495 (length << MXS_DMA_DESC_BYTES_OFFSET);
497 d->cmd.address = (dma_addr_t)nand_info->data_buf;
499 d->cmd.pio_words[0] =
500 GPMI_CTRL0_COMMAND_MODE_READ |
501 GPMI_CTRL0_WORD_LENGTH |
502 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
503 GPMI_CTRL0_ADDRESS_NAND_DATA |
506 mxs_dma_desc_append(channel, d);
509 * A DMA descriptor that waits for the command to end and the chip to
512 * I think we actually should *not* be waiting for the chip to become
513 * ready because, after all, we don't care. I think the original code
514 * did that and no one has re-thought it yet.
516 d = mxs_nand_get_dma_desc(nand_info);
518 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
519 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_DEC_SEM |
520 MXS_DMA_DESC_WAIT4END | (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
524 d->cmd.pio_words[0] =
525 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
526 GPMI_CTRL0_WORD_LENGTH |
527 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
528 GPMI_CTRL0_ADDRESS_NAND_DATA;
530 mxs_dma_desc_append(channel, d);
532 /* Invalidate caches */
533 mxs_nand_inval_data_buf(nand_info);
535 /* Execute the DMA chain. */
536 ret = mxs_dma_go(channel);
538 printf("MXS NAND: DMA read error\n");
542 /* Invalidate caches */
543 mxs_nand_inval_data_buf(nand_info);
545 memcpy(buf, nand_info->data_buf, length);
548 mxs_nand_return_dma_descs(nand_info);
552 * Write data to NAND.
554 static void mxs_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf,
557 struct nand_chip *nand = mtd_to_nand(mtd);
558 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
559 struct mxs_dma_desc *d;
560 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
563 if (length > NAND_MAX_PAGESIZE) {
564 printf("MXS NAND: DMA buffer too big\n");
569 printf("MXS NAND: DMA buffer is NULL\n");
573 memcpy(nand_info->data_buf, buf, length);
575 /* Compile the DMA descriptor - a descriptor that writes data. */
576 d = mxs_nand_get_dma_desc(nand_info);
578 MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ |
579 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
580 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
581 (length << MXS_DMA_DESC_BYTES_OFFSET);
583 d->cmd.address = (dma_addr_t)nand_info->data_buf;
585 d->cmd.pio_words[0] =
586 GPMI_CTRL0_COMMAND_MODE_WRITE |
587 GPMI_CTRL0_WORD_LENGTH |
588 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
589 GPMI_CTRL0_ADDRESS_NAND_DATA |
592 mxs_dma_desc_append(channel, d);
595 mxs_nand_flush_data_buf(nand_info);
597 /* Execute the DMA chain. */
598 ret = mxs_dma_go(channel);
600 printf("MXS NAND: DMA write error\n");
602 mxs_nand_return_dma_descs(nand_info);
606 * Read a single byte from NAND.
608 static uint8_t mxs_nand_read_byte(struct mtd_info *mtd)
611 mxs_nand_read_buf(mtd, &buf, 1);
615 static bool mxs_nand_erased_page(struct mtd_info *mtd, struct nand_chip *nand,
616 u8 *buf, int chunk, int page)
618 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
619 struct bch_geometry *geo = &nand_info->bch_geometry;
620 unsigned int flip_bits = 0, flip_bits_noecc = 0;
621 unsigned int threshold;
622 unsigned int base = geo->ecc_chunkn_size * chunk;
623 u32 *dma_buf = (u32 *)buf;
626 threshold = geo->gf_len / 2;
627 if (threshold > geo->ecc_strength)
628 threshold = geo->ecc_strength;
630 for (i = 0; i < geo->ecc_chunkn_size; i++) {
631 flip_bits += hweight8(~buf[base + i]);
632 if (flip_bits > threshold)
636 nand->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
637 nand->read_buf(mtd, buf, mtd->writesize);
639 for (i = 0; i < mtd->writesize / 4; i++) {
640 flip_bits_noecc += hweight32(~dma_buf[i]);
641 if (flip_bits_noecc > threshold)
645 mtd->ecc_stats.corrected += flip_bits;
647 memset(buf, 0xff, mtd->writesize);
649 printf("The page(%d) is an erased page(%d,%d,%d,%d).\n", page, chunk, threshold, flip_bits, flip_bits_noecc);
655 * Read a page from NAND.
657 static int mxs_nand_ecc_read_page(struct mtd_info *mtd, struct nand_chip *nand,
658 uint8_t *buf, int oob_required,
661 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
662 struct bch_geometry *geo = &nand_info->bch_geometry;
663 struct mxs_dma_desc *d;
664 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
665 uint32_t corrected = 0, failed = 0;
669 /* Compile the DMA descriptor - wait for ready. */
670 d = mxs_nand_get_dma_desc(nand_info);
672 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
673 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
674 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
678 d->cmd.pio_words[0] =
679 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
680 GPMI_CTRL0_WORD_LENGTH |
681 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
682 GPMI_CTRL0_ADDRESS_NAND_DATA;
684 mxs_dma_desc_append(channel, d);
686 /* Compile the DMA descriptor - enable the BCH block and read. */
687 d = mxs_nand_get_dma_desc(nand_info);
689 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
690 MXS_DMA_DESC_WAIT4END | (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
694 d->cmd.pio_words[0] =
695 GPMI_CTRL0_COMMAND_MODE_READ |
696 GPMI_CTRL0_WORD_LENGTH |
697 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
698 GPMI_CTRL0_ADDRESS_NAND_DATA |
699 (mtd->writesize + mtd->oobsize);
700 d->cmd.pio_words[1] = 0;
701 d->cmd.pio_words[2] =
702 GPMI_ECCCTRL_ENABLE_ECC |
703 GPMI_ECCCTRL_ECC_CMD_DECODE |
704 GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
705 d->cmd.pio_words[3] = mtd->writesize + mtd->oobsize;
706 d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
707 d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;
709 mxs_dma_desc_append(channel, d);
711 /* Compile the DMA descriptor - disable the BCH block. */
712 d = mxs_nand_get_dma_desc(nand_info);
714 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
715 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
716 (3 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
720 d->cmd.pio_words[0] =
721 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
722 GPMI_CTRL0_WORD_LENGTH |
723 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
724 GPMI_CTRL0_ADDRESS_NAND_DATA |
725 (mtd->writesize + mtd->oobsize);
726 d->cmd.pio_words[1] = 0;
727 d->cmd.pio_words[2] = 0;
729 mxs_dma_desc_append(channel, d);
731 /* Compile the DMA descriptor - deassert the NAND lock and interrupt. */
732 d = mxs_nand_get_dma_desc(nand_info);
734 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
735 MXS_DMA_DESC_DEC_SEM;
739 mxs_dma_desc_append(channel, d);
741 /* Invalidate caches */
742 mxs_nand_inval_data_buf(nand_info);
744 /* Execute the DMA chain. */
745 ret = mxs_dma_go(channel);
747 printf("MXS NAND: DMA read error\n");
751 ret = mxs_nand_wait_for_bch_complete(nand_info);
753 printf("MXS NAND: BCH read timeout\n");
757 mxs_nand_return_dma_descs(nand_info);
759 /* Invalidate caches */
760 mxs_nand_inval_data_buf(nand_info);
762 /* Read DMA completed, now do the mark swapping. */
763 mxs_nand_swap_block_mark(geo, nand_info->data_buf, nand_info->oob_buf);
765 /* Loop over status bytes, accumulating ECC status. */
766 status = nand_info->oob_buf + mxs_nand_aux_status_offset();
767 for (i = 0; i < geo->ecc_chunk_count; i++) {
768 if (status[i] == 0x00)
771 if (status[i] == 0xff)
774 if (status[i] == 0xfe) {
775 if (mxs_nand_erased_page(mtd, nand,
776 nand_info->data_buf, i, page))
782 corrected += status[i];
785 /* Propagate ECC status to the owning MTD. */
786 mtd->ecc_stats.failed += failed;
787 mtd->ecc_stats.corrected += corrected;
790 * It's time to deliver the OOB bytes. See mxs_nand_ecc_read_oob() for
791 * details about our policy for delivering the OOB.
793 * We fill the caller's buffer with set bits, and then copy the block
794 * mark to the caller's buffer. Note that, if block mark swapping was
795 * necessary, it has already been done, so we can rely on the first
796 * byte of the auxiliary buffer to contain the block mark.
798 memset(nand->oob_poi, 0xff, mtd->oobsize);
800 nand->oob_poi[0] = nand_info->oob_buf[0];
802 memcpy(buf, nand_info->data_buf, mtd->writesize);
805 mxs_nand_return_dma_descs(nand_info);
811 * Write a page to NAND.
813 static int mxs_nand_ecc_write_page(struct mtd_info *mtd,
814 struct nand_chip *nand, const uint8_t *buf,
815 int oob_required, int page)
817 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
818 struct bch_geometry *geo = &nand_info->bch_geometry;
819 struct mxs_dma_desc *d;
820 uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
823 memcpy(nand_info->data_buf, buf, mtd->writesize);
824 memcpy(nand_info->oob_buf, nand->oob_poi, mtd->oobsize);
826 /* Handle block mark swapping. */
827 mxs_nand_swap_block_mark(geo, nand_info->data_buf, nand_info->oob_buf);
829 /* Compile the DMA descriptor - write data. */
830 d = mxs_nand_get_dma_desc(nand_info);
832 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
833 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
834 (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
838 d->cmd.pio_words[0] =
839 GPMI_CTRL0_COMMAND_MODE_WRITE |
840 GPMI_CTRL0_WORD_LENGTH |
841 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
842 GPMI_CTRL0_ADDRESS_NAND_DATA;
843 d->cmd.pio_words[1] = 0;
844 d->cmd.pio_words[2] =
845 GPMI_ECCCTRL_ENABLE_ECC |
846 GPMI_ECCCTRL_ECC_CMD_ENCODE |
847 GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
848 d->cmd.pio_words[3] = (mtd->writesize + mtd->oobsize);
849 d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
850 d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;
852 if (is_mx7() && nand_info->en_randomizer) {
853 d->cmd.pio_words[2] |= GPMI_ECCCTRL_RANDOMIZER_ENABLE |
854 GPMI_ECCCTRL_RANDOMIZER_TYPE2;
856 * Write NAND page number needed to be randomized
857 * to GPMI_ECCCOUNT register.
859 * The value is between 0-255. For additional details
860 * check 9.6.6.4 of i.MX7D Applications Processor reference
862 d->cmd.pio_words[3] |= (page % 255) << 16;
865 mxs_dma_desc_append(channel, d);
868 mxs_nand_flush_data_buf(nand_info);
870 /* Execute the DMA chain. */
871 ret = mxs_dma_go(channel);
873 printf("MXS NAND: DMA write error\n");
877 ret = mxs_nand_wait_for_bch_complete(nand_info);
879 printf("MXS NAND: BCH write timeout\n");
884 mxs_nand_return_dma_descs(nand_info);
889 * Read OOB from NAND.
891 * This function is a veneer that replaces the function originally installed by
892 * the NAND Flash MTD code.
894 static int mxs_nand_hook_read_oob(struct mtd_info *mtd, loff_t from,
895 struct mtd_oob_ops *ops)
897 struct nand_chip *chip = mtd_to_nand(mtd);
898 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
901 if (ops->mode == MTD_OPS_RAW)
902 nand_info->raw_oob_mode = 1;
904 nand_info->raw_oob_mode = 0;
906 ret = nand_info->hooked_read_oob(mtd, from, ops);
908 nand_info->raw_oob_mode = 0;
916 * This function is a veneer that replaces the function originally installed by
917 * the NAND Flash MTD code.
919 static int mxs_nand_hook_write_oob(struct mtd_info *mtd, loff_t to,
920 struct mtd_oob_ops *ops)
922 struct nand_chip *chip = mtd_to_nand(mtd);
923 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
926 if (ops->mode == MTD_OPS_RAW)
927 nand_info->raw_oob_mode = 1;
929 nand_info->raw_oob_mode = 0;
931 ret = nand_info->hooked_write_oob(mtd, to, ops);
933 nand_info->raw_oob_mode = 0;
939 * Mark a block bad in NAND.
941 * This function is a veneer that replaces the function originally installed by
942 * the NAND Flash MTD code.
944 static int mxs_nand_hook_block_markbad(struct mtd_info *mtd, loff_t ofs)
946 struct nand_chip *chip = mtd_to_nand(mtd);
947 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
950 nand_info->marking_block_bad = 1;
952 ret = nand_info->hooked_block_markbad(mtd, ofs);
954 nand_info->marking_block_bad = 0;
960 * There are several places in this driver where we have to handle the OOB and
961 * block marks. This is the function where things are the most complicated, so
962 * this is where we try to explain it all. All the other places refer back to
965 * These are the rules, in order of decreasing importance:
967 * 1) Nothing the caller does can be allowed to imperil the block mark, so all
968 * write operations take measures to protect it.
970 * 2) In read operations, the first byte of the OOB we return must reflect the
971 * true state of the block mark, no matter where that block mark appears in
974 * 3) ECC-based read operations return an OOB full of set bits (since we never
975 * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
978 * 4) "Raw" read operations return a direct view of the physical bytes in the
979 * page, using the conventional definition of which bytes are data and which
980 * are OOB. This gives the caller a way to see the actual, physical bytes
981 * in the page, without the distortions applied by our ECC engine.
983 * What we do for this specific read operation depends on whether we're doing
984 * "raw" read, or an ECC-based read.
986 * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
987 * easy. When reading a page, for example, the NAND Flash MTD code calls our
988 * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
989 * ECC-based or raw view of the page is implicit in which function it calls
990 * (there is a similar pair of ECC-based/raw functions for writing).
992 * Since MTD assumes the OOB is not covered by ECC, there is no pair of
993 * ECC-based/raw functions for reading or or writing the OOB. The fact that the
994 * caller wants an ECC-based or raw view of the page is not propagated down to
997 * Since our OOB *is* covered by ECC, we need this information. So, we hook the
998 * ecc.read_oob and ecc.write_oob function pointers in the owning
999 * struct mtd_info with our own functions. These hook functions set the
1000 * raw_oob_mode field so that, when control finally arrives here, we'll know
1003 static int mxs_nand_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *nand,
1006 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
1009 * First, fill in the OOB buffer. If we're doing a raw read, we need to
1010 * get the bytes from the physical page. If we're not doing a raw read,
1011 * we need to fill the buffer with set bits.
1013 if (nand_info->raw_oob_mode) {
1015 * If control arrives here, we're doing a "raw" read. Send the
1016 * command to read the conventional OOB and read it.
1018 nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1019 nand->read_buf(mtd, nand->oob_poi, mtd->oobsize);
1022 * If control arrives here, we're not doing a "raw" read. Fill
1023 * the OOB buffer with set bits and correct the block mark.
1025 memset(nand->oob_poi, 0xff, mtd->oobsize);
1027 nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1028 mxs_nand_read_buf(mtd, nand->oob_poi, 1);
1036 * Write OOB data to NAND.
1038 static int mxs_nand_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *nand,
1041 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
1042 uint8_t block_mark = 0;
1045 * There are fundamental incompatibilities between the i.MX GPMI NFC and
1046 * the NAND Flash MTD model that make it essentially impossible to write
1047 * the out-of-band bytes.
1049 * We permit *ONE* exception. If the *intent* of writing the OOB is to
1050 * mark a block bad, we can do that.
1053 if (!nand_info->marking_block_bad) {
1054 printf("NXS NAND: Writing OOB isn't supported\n");
1058 /* Write the block mark. */
1059 nand->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1060 nand->write_buf(mtd, &block_mark, 1);
1061 nand->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1063 /* Check if it worked. */
1064 if (nand->waitfunc(mtd, nand) & NAND_STATUS_FAIL)
1071 * Claims all blocks are good.
1073 * In principle, this function is *only* called when the NAND Flash MTD system
1074 * isn't allowed to keep an in-memory bad block table, so it is forced to ask
1075 * the driver for bad block information.
1077 * In fact, we permit the NAND Flash MTD system to have an in-memory BBT, so
1078 * this function is *only* called when we take it away.
1080 * Thus, this function is only called when we want *all* blocks to look good,
1081 * so it *always* return success.
1083 static int mxs_nand_block_bad(struct mtd_info *mtd, loff_t ofs)
1088 static int mxs_nand_set_geometry(struct mtd_info *mtd, struct bch_geometry *geo)
1090 struct nand_chip *chip = mtd_to_nand(mtd);
1091 struct nand_chip *nand = mtd_to_nand(mtd);
1092 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
1094 if (chip->ecc_strength_ds > nand_info->max_ecc_strength_supported) {
1095 printf("unsupported NAND chip, minimum ecc required %d\n"
1096 , chip->ecc_strength_ds);
1100 if ((!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0) &&
1101 mtd->oobsize < 1024) || nand_info->legacy_bch_geometry) {
1102 dev_warn(this->dev, "use legacy bch geometry\n");
1103 return mxs_nand_legacy_calc_ecc_layout(geo, mtd);
1106 if (mtd->oobsize > 1024 || chip->ecc_step_ds < mtd->oobsize)
1107 return mxs_nand_calc_ecc_for_large_oob(geo, mtd);
1109 return mxs_nand_calc_ecc_layout_by_info(geo, mtd,
1110 chip->ecc_strength_ds, chip->ecc_step_ds);
1116 * At this point, the physical NAND Flash chips have been identified and
1117 * counted, so we know the physical geometry. This enables us to make some
1118 * important configuration decisions.
1120 * The return value of this function propagates directly back to this driver's
1121 * board_nand_init(). Anything other than zero will cause this driver to
1122 * tear everything down and declare failure.
1124 int mxs_nand_setup_ecc(struct mtd_info *mtd)
1126 struct nand_chip *nand = mtd_to_nand(mtd);
1127 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
1128 struct bch_geometry *geo = &nand_info->bch_geometry;
1129 struct mxs_bch_regs *bch_regs = nand_info->bch_regs;
1133 nand_info->en_randomizer = 0;
1134 nand_info->oobsize = mtd->oobsize;
1135 nand_info->writesize = mtd->writesize;
1137 ret = mxs_nand_set_geometry(mtd, geo);
1141 /* Configure BCH and set NFC geometry */
1142 mxs_reset_block(&bch_regs->hw_bch_ctrl_reg);
1144 /* Configure layout 0 */
1145 tmp = (geo->ecc_chunk_count - 1) << BCH_FLASHLAYOUT0_NBLOCKS_OFFSET;
1146 tmp |= MXS_NAND_METADATA_SIZE << BCH_FLASHLAYOUT0_META_SIZE_OFFSET;
1147 tmp |= (geo->ecc_strength >> 1) << BCH_FLASHLAYOUT0_ECC0_OFFSET;
1148 tmp |= geo->ecc_chunk0_size >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT;
1149 tmp |= (geo->gf_len == 14 ? 1 : 0) <<
1150 BCH_FLASHLAYOUT0_GF13_0_GF14_1_OFFSET;
1151 writel(tmp, &bch_regs->hw_bch_flash0layout0);
1152 nand_info->bch_flash0layout0 = tmp;
1154 tmp = (mtd->writesize + mtd->oobsize)
1155 << BCH_FLASHLAYOUT1_PAGE_SIZE_OFFSET;
1156 tmp |= (geo->ecc_strength >> 1) << BCH_FLASHLAYOUT1_ECCN_OFFSET;
1157 tmp |= geo->ecc_chunkn_size >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT;
1158 tmp |= (geo->gf_len == 14 ? 1 : 0) <<
1159 BCH_FLASHLAYOUT1_GF13_0_GF14_1_OFFSET;
1160 writel(tmp, &bch_regs->hw_bch_flash0layout1);
1161 nand_info->bch_flash0layout1 = tmp;
1163 /* Set *all* chip selects to use layout 0 */
1164 writel(0, &bch_regs->hw_bch_layoutselect);
1166 /* Enable BCH complete interrupt */
1167 writel(BCH_CTRL_COMPLETE_IRQ_EN, &bch_regs->hw_bch_ctrl_set);
1169 /* Hook some operations at the MTD level. */
1170 if (mtd->_read_oob != mxs_nand_hook_read_oob) {
1171 nand_info->hooked_read_oob = mtd->_read_oob;
1172 mtd->_read_oob = mxs_nand_hook_read_oob;
1175 if (mtd->_write_oob != mxs_nand_hook_write_oob) {
1176 nand_info->hooked_write_oob = mtd->_write_oob;
1177 mtd->_write_oob = mxs_nand_hook_write_oob;
1180 if (mtd->_block_markbad != mxs_nand_hook_block_markbad) {
1181 nand_info->hooked_block_markbad = mtd->_block_markbad;
1182 mtd->_block_markbad = mxs_nand_hook_block_markbad;
1189 * Allocate DMA buffers
1191 int mxs_nand_alloc_buffers(struct mxs_nand_info *nand_info)
1194 const int size = NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE;
1196 nand_info->data_buf_size = roundup(size, MXS_DMA_ALIGNMENT);
1199 buf = memalign(MXS_DMA_ALIGNMENT, nand_info->data_buf_size);
1201 printf("MXS NAND: Error allocating DMA buffers\n");
1205 memset(buf, 0, nand_info->data_buf_size);
1207 nand_info->data_buf = buf;
1208 nand_info->oob_buf = buf + NAND_MAX_PAGESIZE;
1209 /* Command buffers */
1210 nand_info->cmd_buf = memalign(MXS_DMA_ALIGNMENT,
1211 MXS_NAND_COMMAND_BUFFER_SIZE);
1212 if (!nand_info->cmd_buf) {
1214 printf("MXS NAND: Error allocating command buffers\n");
1217 memset(nand_info->cmd_buf, 0, MXS_NAND_COMMAND_BUFFER_SIZE);
1218 nand_info->cmd_queue_len = 0;
1224 * Initializes the NFC hardware.
1226 static int mxs_nand_init_dma(struct mxs_nand_info *info)
1228 int i = 0, j, ret = 0;
1230 info->desc = malloc(sizeof(struct mxs_dma_desc *) *
1231 MXS_NAND_DMA_DESCRIPTOR_COUNT);
1237 /* Allocate the DMA descriptors. */
1238 for (i = 0; i < MXS_NAND_DMA_DESCRIPTOR_COUNT; i++) {
1239 info->desc[i] = mxs_dma_desc_alloc();
1240 if (!info->desc[i]) {
1246 /* Init the DMA controller. */
1248 for (j = MXS_DMA_CHANNEL_AHB_APBH_GPMI0;
1249 j <= MXS_DMA_CHANNEL_AHB_APBH_GPMI7; j++) {
1250 ret = mxs_dma_init_channel(j);
1255 /* Reset the GPMI block. */
1256 mxs_reset_block(&info->gpmi_regs->hw_gpmi_ctrl0_reg);
1257 mxs_reset_block(&info->bch_regs->hw_bch_ctrl_reg);
1260 * Choose NAND mode, set IRQ polarity, disable write protection and
1263 clrsetbits_le32(&info->gpmi_regs->hw_gpmi_ctrl1,
1264 GPMI_CTRL1_GPMI_MODE,
1265 GPMI_CTRL1_ATA_IRQRDY_POLARITY | GPMI_CTRL1_DEV_RESET |
1266 GPMI_CTRL1_BCH_MODE);
1271 for (--j; j >= MXS_DMA_CHANNEL_AHB_APBH_GPMI0; j--)
1274 for (--i; i >= 0; i--)
1275 mxs_dma_desc_free(info->desc[i]);
1279 printf("MXS NAND: Unable to allocate DMA descriptors\n");
1283 int mxs_nand_init_spl(struct nand_chip *nand)
1285 struct mxs_nand_info *nand_info;
1288 nand_info = malloc(sizeof(struct mxs_nand_info));
1290 printf("MXS NAND: Failed to allocate private data\n");
1293 memset(nand_info, 0, sizeof(struct mxs_nand_info));
1295 nand_info->gpmi_regs = (struct mxs_gpmi_regs *)MXS_GPMI_BASE;
1296 nand_info->bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
1298 if (is_mx6sx() || is_mx7())
1299 nand_info->max_ecc_strength_supported = 62;
1301 nand_info->max_ecc_strength_supported = 40;
1303 err = mxs_nand_alloc_buffers(nand_info);
1307 err = mxs_nand_init_dma(nand_info);
1311 nand_set_controller_data(nand, nand_info);
1313 nand->options |= NAND_NO_SUBPAGE_WRITE;
1315 nand->cmd_ctrl = mxs_nand_cmd_ctrl;
1316 nand->dev_ready = mxs_nand_device_ready;
1317 nand->select_chip = mxs_nand_select_chip;
1319 nand->read_byte = mxs_nand_read_byte;
1320 nand->read_buf = mxs_nand_read_buf;
1322 nand->ecc.read_page = mxs_nand_ecc_read_page;
1324 nand->ecc.mode = NAND_ECC_HW;
1329 int mxs_nand_init_ctrl(struct mxs_nand_info *nand_info)
1331 struct mtd_info *mtd;
1332 struct nand_chip *nand;
1335 nand = &nand_info->chip;
1336 mtd = nand_to_mtd(nand);
1337 err = mxs_nand_alloc_buffers(nand_info);
1341 err = mxs_nand_init_dma(nand_info);
1343 goto err_free_buffers;
1345 memset(&fake_ecc_layout, 0, sizeof(fake_ecc_layout));
1347 #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1348 nand->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
1351 nand_set_controller_data(nand, nand_info);
1352 nand->options |= NAND_NO_SUBPAGE_WRITE;
1355 nand->flash_node = dev_of_offset(nand_info->dev);
1357 nand->cmd_ctrl = mxs_nand_cmd_ctrl;
1359 nand->dev_ready = mxs_nand_device_ready;
1360 nand->select_chip = mxs_nand_select_chip;
1361 nand->block_bad = mxs_nand_block_bad;
1363 nand->read_byte = mxs_nand_read_byte;
1365 nand->read_buf = mxs_nand_read_buf;
1366 nand->write_buf = mxs_nand_write_buf;
1368 /* first scan to find the device and get the page size */
1369 if (nand_scan_ident(mtd, CONFIG_SYS_MAX_NAND_DEVICE, NULL))
1370 goto err_free_buffers;
1372 if (mxs_nand_setup_ecc(mtd))
1373 goto err_free_buffers;
1375 nand->ecc.read_page = mxs_nand_ecc_read_page;
1376 nand->ecc.write_page = mxs_nand_ecc_write_page;
1377 nand->ecc.read_oob = mxs_nand_ecc_read_oob;
1378 nand->ecc.write_oob = mxs_nand_ecc_write_oob;
1380 nand->ecc.layout = &fake_ecc_layout;
1381 nand->ecc.mode = NAND_ECC_HW;
1382 nand->ecc.size = nand_info->bch_geometry.ecc_chunkn_size;
1383 nand->ecc.strength = nand_info->bch_geometry.ecc_strength;
1385 /* second phase scan */
1386 err = nand_scan_tail(mtd);
1388 goto err_free_buffers;
1390 err = nand_register(0, mtd);
1392 goto err_free_buffers;
1397 free(nand_info->data_buf);
1398 free(nand_info->cmd_buf);
1403 #ifndef CONFIG_NAND_MXS_DT
1404 void board_nand_init(void)
1406 struct mxs_nand_info *nand_info;
1408 nand_info = malloc(sizeof(struct mxs_nand_info));
1410 printf("MXS NAND: Failed to allocate private data\n");
1413 memset(nand_info, 0, sizeof(struct mxs_nand_info));
1415 nand_info->gpmi_regs = (struct mxs_gpmi_regs *)MXS_GPMI_BASE;
1416 nand_info->bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
1418 /* Refer to Chapter 17 for i.MX6DQ, Chapter 18 for i.MX6SX */
1419 if (is_mx6sx() || is_mx7())
1420 nand_info->max_ecc_strength_supported = 62;
1422 nand_info->max_ecc_strength_supported = 40;
1424 #ifdef CONFIG_NAND_MXS_USE_MINIMUM_ECC
1425 nand_info->use_minimum_ecc = true;
1428 if (mxs_nand_init_ctrl(nand_info) < 0)
1439 * Read NAND layout for FCB block generation.
1441 void mxs_nand_get_layout(struct mtd_info *mtd, struct mxs_nand_layout *l)
1443 struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
1446 tmp = readl(&bch_regs->hw_bch_flash0layout0);
1447 l->nblocks = (tmp & BCH_FLASHLAYOUT0_NBLOCKS_MASK) >>
1448 BCH_FLASHLAYOUT0_NBLOCKS_OFFSET;
1449 l->meta_size = (tmp & BCH_FLASHLAYOUT0_META_SIZE_MASK) >>
1450 BCH_FLASHLAYOUT0_META_SIZE_OFFSET;
1452 tmp = readl(&bch_regs->hw_bch_flash0layout1);
1453 l->data0_size = 4 * ((tmp & BCH_FLASHLAYOUT0_DATA0_SIZE_MASK) >>
1454 BCH_FLASHLAYOUT0_DATA0_SIZE_OFFSET);
1455 l->ecc0 = (tmp & BCH_FLASHLAYOUT0_ECC0_MASK) >>
1456 BCH_FLASHLAYOUT0_ECC0_OFFSET;
1457 l->datan_size = 4 * ((tmp & BCH_FLASHLAYOUT1_DATAN_SIZE_MASK) >>
1458 BCH_FLASHLAYOUT1_DATAN_SIZE_OFFSET);
1459 l->eccn = (tmp & BCH_FLASHLAYOUT1_ECCN_MASK) >>
1460 BCH_FLASHLAYOUT1_ECCN_OFFSET;
1464 * Set BCH to specific layout used by ROM bootloader to read FCB.
1466 void mxs_nand_mode_fcb(struct mtd_info *mtd)
1469 struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
1470 struct nand_chip *nand = mtd_to_nand(mtd);
1471 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
1473 nand_info->en_randomizer = 1;
1475 mtd->writesize = 1024;
1476 mtd->oobsize = 1862 - 1024;
1479 tmp = 7 << BCH_FLASHLAYOUT0_NBLOCKS_OFFSET;
1480 /* 32 bytes for metadata */
1481 tmp |= 32 << BCH_FLASHLAYOUT0_META_SIZE_OFFSET;
1482 /* using ECC62 level to be performed */
1483 tmp |= 0x1F << BCH_FLASHLAYOUT0_ECC0_OFFSET;
1484 /* 0x20 * 4 bytes of the data0 block */
1485 tmp |= 0x20 << BCH_FLASHLAYOUT0_DATA0_SIZE_OFFSET;
1486 tmp |= 0 << BCH_FLASHLAYOUT0_GF13_0_GF14_1_OFFSET;
1487 writel(tmp, &bch_regs->hw_bch_flash0layout0);
1489 /* 1024 for data + 838 for OOB */
1490 tmp = 1862 << BCH_FLASHLAYOUT1_PAGE_SIZE_OFFSET;
1491 /* using ECC62 level to be performed */
1492 tmp |= 0x1F << BCH_FLASHLAYOUT1_ECCN_OFFSET;
1493 /* 0x20 * 4 bytes of the data0 block */
1494 tmp |= 0x20 << BCH_FLASHLAYOUT1_DATAN_SIZE_OFFSET;
1495 tmp |= 0 << BCH_FLASHLAYOUT1_GF13_0_GF14_1_OFFSET;
1496 writel(tmp, &bch_regs->hw_bch_flash0layout1);
1500 * Restore BCH to normal settings.
1502 void mxs_nand_mode_normal(struct mtd_info *mtd)
1504 struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
1505 struct nand_chip *nand = mtd_to_nand(mtd);
1506 struct mxs_nand_info *nand_info = nand_get_controller_data(nand);
1508 nand_info->en_randomizer = 0;
1510 mtd->writesize = nand_info->writesize;
1511 mtd->oobsize = nand_info->oobsize;
1513 writel(nand_info->bch_flash0layout0, &bch_regs->hw_bch_flash0layout0);
1514 writel(nand_info->bch_flash0layout1, &bch_regs->hw_bch_flash0layout1);
1517 uint32_t mxs_nand_mark_byte_offset(struct mtd_info *mtd)
1519 struct nand_chip *chip = mtd_to_nand(mtd);
1520 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
1521 struct bch_geometry *geo = &nand_info->bch_geometry;
1523 return geo->block_mark_byte_offset;
1526 uint32_t mxs_nand_mark_bit_offset(struct mtd_info *mtd)
1528 struct nand_chip *chip = mtd_to_nand(mtd);
1529 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
1530 struct bch_geometry *geo = &nand_info->bch_geometry;
1532 return geo->block_mark_bit_offset;