2 * Copyright 2004-2007 Freescale Semiconductor, Inc.
3 * Copyright 2008 Sascha Hauer, kernel@pengutronix.de
4 * Copyright 2009 Ilya Yanok, <yanok@emcraft.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23 #include <linux/err.h>
25 #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX35)
26 #include <asm/arch/imx-regs.h>
29 #define DRIVER_NAME "mxc_nand"
32 * TODO: Use same register defs here as nand_spl mxc nand driver.
35 * Register map and bit definitions for the Freescale NAND Flash Controller
36 * present in various i.MX devices.
38 * MX31 and MX27 have version 1 which has
39 * 4 512 byte main buffers and
40 * 4 16 byte spare buffers
41 * to support up to 2K byte pagesize nand.
42 * Reading or writing a 2K page requires 4 FDI/FDO cycles.
44 * MX25 has version 1.1 which has
45 * 8 512 byte main buffers and
46 * 8 64 byte spare buffers
47 * to support up to 4K byte pagesize nand.
48 * Reading or writing a 2K or 4K page requires only 1 FDI/FDO cycle.
49 * Also some of registers are moved and/or changed meaning as seen below.
51 #if defined(CONFIG_MX31) || defined(CONFIG_MX27)
53 #elif defined(CONFIG_MX25) || defined(CONFIG_MX35)
56 #warning "MXC NFC version not defined"
59 #if defined(MXC_NFC_V1)
60 #define NAND_MXC_NR_BUFS 4
61 #define NAND_MXC_SPARE_BUF_SIZE 16
62 #define NAND_MXC_REG_OFFSET 0xe00
63 #define is_mxc_nfc_11() 0
64 #elif defined(MXC_NFC_V1_1)
65 #define NAND_MXC_NR_BUFS 8
66 #define NAND_MXC_SPARE_BUF_SIZE 64
67 #define NAND_MXC_REG_OFFSET 0x1e00
68 #define is_mxc_nfc_11() 1
70 #error "define CONFIG_NAND_MXC_VXXX to use mtd mxc nand driver"
73 uint8_t main_area[NAND_MXC_NR_BUFS][0x200];
74 uint8_t spare_area[NAND_MXC_NR_BUFS][NAND_MXC_SPARE_BUF_SIZE];
76 * reserved size is offset of nfc registers
77 * minus total main and spare sizes
79 uint8_t reserved1[NAND_MXC_REG_OFFSET
80 - NAND_MXC_NR_BUFS * (512 + NAND_MXC_SPARE_BUF_SIZE)];
81 #if defined(MXC_NFC_V1)
82 uint16_t nfc_buf_size;
84 uint16_t nfc_buf_addr;
85 uint16_t nfc_flash_addr;
86 uint16_t nfc_flash_cmd;
88 uint16_t nfc_ecc_status_result;
89 uint16_t nfc_rsltmain_area;
90 uint16_t nfc_rsltspare_area;
92 uint16_t nfc_unlockstart_blkaddr;
93 uint16_t nfc_unlockend_blkaddr;
94 uint16_t nfc_nf_wrprst;
97 #elif defined(MXC_NFC_V1_1)
98 uint16_t reserved2[2];
99 uint16_t nfc_buf_addr;
100 uint16_t nfc_flash_addr;
101 uint16_t nfc_flash_cmd;
103 uint16_t nfc_ecc_status_result;
104 uint16_t nfc_ecc_status_result2;
105 uint16_t nfc_spare_area_size;
107 uint16_t reserved3[2];
108 uint16_t nfc_nf_wrprst;
109 uint16_t nfc_config1;
110 uint16_t nfc_config2;
112 uint16_t nfc_unlockstart_blkaddr;
113 uint16_t nfc_unlockend_blkaddr;
114 uint16_t nfc_unlockstart_blkaddr1;
115 uint16_t nfc_unlockend_blkaddr1;
116 uint16_t nfc_unlockstart_blkaddr2;
117 uint16_t nfc_unlockend_blkaddr2;
118 uint16_t nfc_unlockstart_blkaddr3;
119 uint16_t nfc_unlockend_blkaddr3;
124 * Set INT to 0, FCMD to 1, rest to 0 in NFC_CONFIG2 Register
125 * for Command operation
130 * Set INT to 0, FADD to 1, rest to 0 in NFC_CONFIG2 Register
131 * for Address operation
136 * Set INT to 0, FDI to 1, rest to 0 in NFC_CONFIG2 Register
137 * for Input operation
139 #define NFC_INPUT 0x4
142 * Set INT to 0, FDO to 001, rest to 0 in NFC_CONFIG2 Register
143 * for Data Output operation
145 #define NFC_OUTPUT 0x8
148 * Set INT to 0, FD0 to 010, rest to 0 in NFC_CONFIG2 Register
149 * for Read ID operation
154 * Set INT to 0, FDO to 100, rest to 0 in NFC_CONFIG2 Register
155 * for Read Status operation
157 #define NFC_STATUS 0x20
160 * Set INT to 1, rest to 0 in NFC_CONFIG2 Register for Read
163 #define NFC_INT 0x8000
166 #define NFC_4_8N_ECC (1 << 0)
168 #define NFC_4_8N_ECC 0
170 #define NFC_SP_EN (1 << 2)
171 #define NFC_ECC_EN (1 << 3)
172 #define NFC_BIG (1 << 5)
173 #define NFC_RST (1 << 6)
174 #define NFC_CE (1 << 7)
175 #define NFC_ONE_CYCLE (1 << 8)
177 typedef enum {false, true} bool;
179 struct mxc_nand_host {
181 struct nand_chip *nand;
183 struct nfc_regs __iomem *regs;
189 unsigned int page_addr;
192 static struct mxc_nand_host mxc_host;
193 static struct mxc_nand_host *host = &mxc_host;
195 /* Define delays in microsec for NAND device operations */
196 #define TROP_US_DELAY 2000
197 /* Macros to get byte and bit positions of ECC */
198 #define COLPOS(x) ((x) >> 3)
199 #define BITPOS(x) ((x) & 0xf)
201 /* Define single bit Error positions in Main & Spare area */
202 #define MAIN_SINGLEBIT_ERROR 0x4
203 #define SPARE_SINGLEBIT_ERROR 0x1
205 /* OOB placement block for use with hardware ecc generation */
206 #if defined(MXC_NFC_V1)
207 #ifndef CONFIG_SYS_NAND_LARGEPAGE
208 static struct nand_ecclayout nand_hw_eccoob = {
210 .eccpos = {6, 7, 8, 9, 10},
211 .oobfree = { {0, 5}, {11, 5}, }
214 static struct nand_ecclayout nand_hw_eccoob2k = {
222 .oobfree = { {2, 4}, {11, 11}, {27, 11}, {43, 11}, {59, 5} },
225 #elif defined(MXC_NFC_V1_1)
226 #ifndef CONFIG_SYS_NAND_LARGEPAGE
227 static struct nand_ecclayout nand_hw_eccoob = {
229 .eccpos = {7, 8, 9, 10, 11, 12, 13, 14, 15},
230 .oobfree = { {2, 5} }
233 static struct nand_ecclayout nand_hw_eccoob2k = {
236 7, 8, 9, 10, 11, 12, 13, 14, 15,
237 23, 24, 25, 26, 27, 28, 29, 30, 31,
238 39, 40, 41, 42, 43, 44, 45, 46, 47,
239 55, 56, 57, 58, 59, 60, 61, 62, 63,
241 .oobfree = { {2, 5}, {16, 7}, {32, 7}, {48, 7} },
247 static int is_16bit_nand(void)
249 struct system_control_regs *sc_regs =
250 (struct system_control_regs *)IMX_SYSTEM_CTL_BASE;
252 if (readl(&sc_regs->fmcr) & NF_16BIT_SEL)
257 #elif defined(CONFIG_MX31)
258 static int is_16bit_nand(void)
260 struct clock_control_regs *sc_regs =
261 (struct clock_control_regs *)CCM_BASE;
263 if (readl(&sc_regs->rcsr) & CCM_RCSR_NF16B)
268 #elif defined(CONFIG_MX25) || defined(CONFIG_MX35)
269 static int is_16bit_nand(void)
271 struct ccm_regs *ccm =
272 (struct ccm_regs *)IMX_CCM_BASE;
274 if (readl(&ccm->rcsr) & CCM_RCSR_NF_16BIT_SEL)
280 #warning "8/16 bit NAND autodetection not supported"
281 static int is_16bit_nand(void)
287 static uint32_t *mxc_nand_memcpy32(uint32_t *dest, uint32_t *source, size_t size)
293 __raw_writel(__raw_readl(source++), d++);
298 * This function polls the NANDFC to wait for the basic operation to
299 * complete by checking the INT bit of config2 register.
301 static void wait_op_done(struct mxc_nand_host *host, int max_retries,
306 while (max_retries-- > 0) {
307 if (readw(&host->regs->nfc_config2) & NFC_INT) {
308 tmp = readw(&host->regs->nfc_config2);
310 writew(tmp, &host->regs->nfc_config2);
315 if (max_retries < 0) {
316 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s(%d): INT not set\n",
322 * This function issues the specified command to the NAND device and
323 * waits for completion.
325 static void send_cmd(struct mxc_nand_host *host, uint16_t cmd)
327 MTDDEBUG(MTD_DEBUG_LEVEL3, "send_cmd(host, 0x%x)\n", cmd);
329 writew(cmd, &host->regs->nfc_flash_cmd);
330 writew(NFC_CMD, &host->regs->nfc_config2);
332 /* Wait for operation to complete */
333 wait_op_done(host, TROP_US_DELAY, cmd);
337 * This function sends an address (or partial address) to the
338 * NAND device. The address is used to select the source/destination for
341 static void send_addr(struct mxc_nand_host *host, uint16_t addr)
343 MTDDEBUG(MTD_DEBUG_LEVEL3, "send_addr(host, 0x%x)\n", addr);
345 writew(addr, &host->regs->nfc_flash_addr);
346 writew(NFC_ADDR, &host->regs->nfc_config2);
348 /* Wait for operation to complete */
349 wait_op_done(host, TROP_US_DELAY, addr);
353 * This function requests the NANDFC to initiate the transfer
354 * of data currently in the NANDFC RAM buffer to the NAND device.
356 static void send_prog_page(struct mxc_nand_host *host, uint8_t buf_id,
360 MTDDEBUG(MTD_DEBUG_LEVEL1, "send_prog_page (%d)\n", spare_only);
362 if (is_mxc_nfc_11()) {
365 * The controller copies the 64 bytes of spare data from
366 * the first 16 bytes of each of the 4 64 byte spare buffers.
367 * Copy the contiguous data starting in spare_area[0] to
368 * the four spare area buffers.
370 for (i = 1; i < 4; i++) {
371 void __iomem *src = &host->regs->spare_area[0][i * 16];
372 void __iomem *dst = &host->regs->spare_area[i][0];
374 mxc_nand_memcpy32(dst, src, 16);
378 writew(buf_id, &host->regs->nfc_buf_addr);
380 /* Configure spare or page+spare access */
381 if (!host->pagesize_2k) {
382 uint16_t config1 = readw(&host->regs->nfc_config1);
384 config1 |= NFC_SP_EN;
386 config1 &= ~(NFC_SP_EN);
387 writew(config1, &host->regs->nfc_config1);
390 writew(NFC_INPUT, &host->regs->nfc_config2);
392 /* Wait for operation to complete */
393 wait_op_done(host, TROP_US_DELAY, spare_only);
397 * Requests NANDFC to initiate the transfer of data from the
398 * NAND device into in the NANDFC ram buffer.
400 static void send_read_page(struct mxc_nand_host *host, uint8_t buf_id,
403 MTDDEBUG(MTD_DEBUG_LEVEL3, "send_read_page (%d)\n", spare_only);
405 writew(buf_id, &host->regs->nfc_buf_addr);
407 /* Configure spare or page+spare access */
408 if (!host->pagesize_2k) {
409 uint32_t config1 = readw(&host->regs->nfc_config1);
411 config1 |= NFC_SP_EN;
413 config1 &= ~NFC_SP_EN;
414 writew(config1, &host->regs->nfc_config1);
417 writew(NFC_OUTPUT, &host->regs->nfc_config2);
419 /* Wait for operation to complete */
420 wait_op_done(host, TROP_US_DELAY, spare_only);
422 if (is_mxc_nfc_11()) {
426 * The controller copies the 64 bytes of spare data to
427 * the first 16 bytes of each of the 4 spare buffers.
428 * Make the data contiguous starting in spare_area[0].
430 for (i = 1; i < 4; i++) {
431 void __iomem *src = &host->regs->spare_area[i][0];
432 void __iomem *dst = &host->regs->spare_area[0][i * 16];
434 mxc_nand_memcpy32(dst, src, 16);
439 /* Request the NANDFC to perform a read of the NAND device ID. */
440 static void send_read_id(struct mxc_nand_host *host)
444 /* NANDFC buffer 0 is used for device ID output */
445 writew(0x0, &host->regs->nfc_buf_addr);
447 /* Read ID into main buffer */
448 tmp = readw(&host->regs->nfc_config1);
450 writew(tmp, &host->regs->nfc_config1);
452 writew(NFC_ID, &host->regs->nfc_config2);
454 /* Wait for operation to complete */
455 wait_op_done(host, TROP_US_DELAY, 0);
459 * This function requests the NANDFC to perform a read of the
460 * NAND device status and returns the current status.
462 static uint16_t get_dev_status(struct mxc_nand_host *host)
464 void __iomem *main_buf = host->regs->main_area[1];
467 /* Issue status request to NAND device */
469 /* store the main area1 first word, later do recovery */
470 store = readl(main_buf);
471 /* NANDFC buffer 1 is used for device status */
472 writew(1, &host->regs->nfc_buf_addr);
474 /* Read status into main buffer */
475 tmp = readw(&host->regs->nfc_config1);
477 writew(tmp, &host->regs->nfc_config1);
479 writew(NFC_STATUS, &host->regs->nfc_config2);
481 /* Wait for operation to complete */
482 wait_op_done(host, TROP_US_DELAY, 0);
485 * Status is placed in first word of main buffer
486 * get status, then recovery area 1 data
488 ret = readw(main_buf);
489 writel(store, main_buf);
494 /* This function is used by upper layer to checks if device is ready */
495 static int mxc_nand_dev_ready(struct mtd_info *mtd)
498 * NFC handles R/B internally. Therefore, this function
499 * always returns status as ready.
504 #ifdef CONFIG_MXC_NAND_HWECC
505 static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode)
508 * If HW ECC is enabled, we turn it on during init. There is
509 * no need to enable again here.
514 static void _mxc_nand_enable_hwecc(struct mtd_info *mtd, int on)
516 struct nand_chip *nand_chip = mtd->priv;
517 struct mxc_nand_host *host = nand_chip->priv;
518 uint16_t tmp = readw(&host->regs->nfc_config1);
524 writew(tmp, &host->regs->nfc_config1);
527 static int mxc_nand_read_oob_syndrome(struct mtd_info *mtd,
528 struct nand_chip *chip,
529 int page, int sndcmd)
531 struct mxc_nand_host *host = chip->priv;
532 uint8_t *buf = chip->oob_poi;
533 int length = mtd->oobsize;
534 int eccpitch = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
535 uint8_t *bufpoi = buf;
538 MTDDEBUG(MTD_DEBUG_LEVEL0,
539 "%s: Reading OOB area of page %u to oob %p\n",
540 __FUNCTION__, host->page_addr, buf);
542 chip->cmdfunc(mtd, NAND_CMD_READOOB, mtd->writesize, page);
543 for (i = 0; i < chip->ecc.steps; i++) {
544 toread = min_t(int, length, chip->ecc.prepad);
546 chip->read_buf(mtd, bufpoi, toread);
550 bufpoi += chip->ecc.bytes;
551 host->col_addr += chip->ecc.bytes;
552 length -= chip->ecc.bytes;
554 toread = min_t(int, length, chip->ecc.postpad);
556 chip->read_buf(mtd, bufpoi, toread);
562 chip->read_buf(mtd, bufpoi, length);
564 _mxc_nand_enable_hwecc(mtd, 0);
565 chip->cmdfunc(mtd, NAND_CMD_READOOB,
566 mtd->writesize + chip->ecc.prepad, page);
567 bufpoi = buf + chip->ecc.prepad;
568 length = mtd->oobsize - chip->ecc.prepad;
569 for (i = 0; i < chip->ecc.steps; i++) {
570 toread = min_t(int, length, chip->ecc.bytes);
571 chip->read_buf(mtd, bufpoi, toread);
574 host->col_addr += chip->ecc.postpad + chip->ecc.prepad;
576 _mxc_nand_enable_hwecc(mtd, 1);
580 static int mxc_nand_read_page_raw_syndrome(struct mtd_info *mtd,
581 struct nand_chip *chip,
585 struct mxc_nand_host *host = chip->priv;
586 int eccsize = chip->ecc.size;
587 int eccbytes = chip->ecc.bytes;
588 int eccpitch = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
589 uint8_t *oob = chip->oob_poi;
593 _mxc_nand_enable_hwecc(mtd, 0);
594 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, host->page_addr);
596 for (n = 0, steps = chip->ecc.steps; steps > 0; n++, steps--) {
597 host->col_addr = n * eccsize;
598 chip->read_buf(mtd, buf, eccsize);
601 host->col_addr = mtd->writesize + n * eccpitch;
602 if (chip->ecc.prepad) {
603 chip->read_buf(mtd, oob, chip->ecc.prepad);
604 oob += chip->ecc.prepad;
607 chip->read_buf(mtd, oob, eccbytes);
610 if (chip->ecc.postpad) {
611 chip->read_buf(mtd, oob, chip->ecc.postpad);
612 oob += chip->ecc.postpad;
616 size = mtd->oobsize - (oob - chip->oob_poi);
618 chip->read_buf(mtd, oob, size);
619 _mxc_nand_enable_hwecc(mtd, 0);
624 static int mxc_nand_read_page_syndrome(struct mtd_info *mtd,
625 struct nand_chip *chip,
629 struct mxc_nand_host *host = chip->priv;
630 int n, eccsize = chip->ecc.size;
631 int eccbytes = chip->ecc.bytes;
632 int eccpitch = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
633 int eccsteps = chip->ecc.steps;
635 uint8_t *oob = chip->oob_poi;
637 MTDDEBUG(MTD_DEBUG_LEVEL1, "Reading page %u to buf %p oob %p\n",
638 host->page_addr, buf, oob);
640 /* first read the data area and the available portion of OOB */
641 for (n = 0; eccsteps; n++, eccsteps--, p += eccsize) {
644 host->col_addr = n * eccsize;
646 chip->read_buf(mtd, p, eccsize);
648 host->col_addr = mtd->writesize + n * eccpitch;
650 if (chip->ecc.prepad) {
651 chip->read_buf(mtd, oob, chip->ecc.prepad);
652 oob += chip->ecc.prepad;
655 stat = chip->ecc.correct(mtd, p, oob, NULL);
658 mtd->ecc_stats.failed++;
660 mtd->ecc_stats.corrected += stat;
663 if (chip->ecc.postpad) {
664 chip->read_buf(mtd, oob, chip->ecc.postpad);
665 oob += chip->ecc.postpad;
669 /* Calculate remaining oob bytes */
670 n = mtd->oobsize - (oob - chip->oob_poi);
672 chip->read_buf(mtd, oob, n);
674 /* Then switch ECC off and read the OOB area to get the ECC code */
675 _mxc_nand_enable_hwecc(mtd, 0);
676 chip->cmdfunc(mtd, NAND_CMD_READOOB, mtd->writesize, host->page_addr);
677 eccsteps = chip->ecc.steps;
678 oob = chip->oob_poi + chip->ecc.prepad;
679 for (n = 0; eccsteps; n++, eccsteps--, p += eccsize) {
680 host->col_addr = mtd->writesize +
683 chip->read_buf(mtd, oob, eccbytes);
684 oob += eccbytes + chip->ecc.postpad;
686 _mxc_nand_enable_hwecc(mtd, 1);
690 static int mxc_nand_write_oob_syndrome(struct mtd_info *mtd,
691 struct nand_chip *chip, int page)
693 struct mxc_nand_host *host = chip->priv;
694 int eccpitch = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
695 int length = mtd->oobsize;
696 int i, len, status, steps = chip->ecc.steps;
697 const uint8_t *bufpoi = chip->oob_poi;
699 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
700 for (i = 0; i < steps; i++) {
701 len = min_t(int, length, eccpitch);
703 chip->write_buf(mtd, bufpoi, len);
706 host->col_addr += chip->ecc.prepad + chip->ecc.postpad;
709 chip->write_buf(mtd, bufpoi, length);
711 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
712 status = chip->waitfunc(mtd, chip);
713 return status & NAND_STATUS_FAIL ? -EIO : 0;
716 static void mxc_nand_write_page_raw_syndrome(struct mtd_info *mtd,
717 struct nand_chip *chip,
720 struct mxc_nand_host *host = chip->priv;
721 int eccsize = chip->ecc.size;
722 int eccbytes = chip->ecc.bytes;
723 int eccpitch = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
724 uint8_t *oob = chip->oob_poi;
728 for (n = 0, steps = chip->ecc.steps; steps > 0; n++, steps--) {
729 host->col_addr = n * eccsize;
730 chip->write_buf(mtd, buf, eccsize);
733 host->col_addr = mtd->writesize + n * eccpitch;
735 if (chip->ecc.prepad) {
736 chip->write_buf(mtd, oob, chip->ecc.prepad);
737 oob += chip->ecc.prepad;
740 host->col_addr += eccbytes;
743 if (chip->ecc.postpad) {
744 chip->write_buf(mtd, oob, chip->ecc.postpad);
745 oob += chip->ecc.postpad;
749 size = mtd->oobsize - (oob - chip->oob_poi);
751 chip->write_buf(mtd, oob, size);
754 static void mxc_nand_write_page_syndrome(struct mtd_info *mtd,
755 struct nand_chip *chip,
758 struct mxc_nand_host *host = chip->priv;
759 int i, n, eccsize = chip->ecc.size;
760 int eccbytes = chip->ecc.bytes;
761 int eccpitch = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
762 int eccsteps = chip->ecc.steps;
763 const uint8_t *p = buf;
764 uint8_t *oob = chip->oob_poi;
766 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
770 n++, eccsteps--, i += eccbytes, p += eccsize) {
771 host->col_addr = n * eccsize;
773 chip->write_buf(mtd, p, eccsize);
775 host->col_addr = mtd->writesize + n * eccpitch;
777 if (chip->ecc.prepad) {
778 chip->write_buf(mtd, oob, chip->ecc.prepad);
779 oob += chip->ecc.prepad;
782 chip->write_buf(mtd, oob, eccbytes);
785 if (chip->ecc.postpad) {
786 chip->write_buf(mtd, oob, chip->ecc.postpad);
787 oob += chip->ecc.postpad;
791 /* Calculate remaining oob bytes */
792 i = mtd->oobsize - (oob - chip->oob_poi);
794 chip->write_buf(mtd, oob, i);
797 static int mxc_nand_correct_data(struct mtd_info *mtd, u_char *dat,
798 u_char *read_ecc, u_char *calc_ecc)
800 struct nand_chip *nand_chip = mtd->priv;
801 struct mxc_nand_host *host = nand_chip->priv;
802 uint16_t ecc_status = readw(&host->regs->nfc_ecc_status_result);
803 int subpages = mtd->writesize / nand_chip->subpagesize;
804 int pg2blk_shift = nand_chip->phys_erase_shift -
805 nand_chip->page_shift;
808 if ((ecc_status & 0xf) > 4) {
809 static int last_bad = -1;
811 if (last_bad != host->page_addr >> pg2blk_shift) {
812 last_bad = host->page_addr >> pg2blk_shift;
814 "MXC_NAND: HWECC uncorrectable ECC error"
815 " in block %u page %u subpage %d\n",
816 last_bad, host->page_addr,
817 mtd->writesize / nand_chip->subpagesize
824 } while (subpages > 0);
829 #define mxc_nand_read_page_syndrome NULL
830 #define mxc_nand_read_page_raw_syndrome NULL
831 #define mxc_nand_read_oob_syndrome NULL
832 #define mxc_nand_write_page_syndrome NULL
833 #define mxc_nand_write_page_raw_syndrome NULL
834 #define mxc_nand_write_oob_syndrome NULL
835 #define mxc_nfc_11_nand_correct_data NULL
837 static int mxc_nand_correct_data(struct mtd_info *mtd, u_char *dat,
838 u_char *read_ecc, u_char *calc_ecc)
840 struct nand_chip *nand_chip = mtd->priv;
841 struct mxc_nand_host *host = nand_chip->priv;
844 * 1-Bit errors are automatically corrected in HW. No need for
845 * additional correction. 2-Bit errors cannot be corrected by
846 * HW ECC, so we need to return failure
848 uint16_t ecc_status = readw(&host->regs->nfc_ecc_status_result);
850 if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) {
851 MTDDEBUG(MTD_DEBUG_LEVEL0,
852 "MXC_NAND: HWECC uncorrectable 2-bit ECC error\n");
860 static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
867 static u_char mxc_nand_read_byte(struct mtd_info *mtd)
869 struct nand_chip *nand_chip = mtd->priv;
870 struct mxc_nand_host *host = nand_chip->priv;
873 uint16_t __iomem *main_buf =
874 (uint16_t __iomem *)host->regs->main_area[0];
875 uint16_t __iomem *spare_buf =
876 (uint16_t __iomem *)host->regs->spare_area[0];
882 /* Check for status request */
883 if (host->status_request)
884 return get_dev_status(host) & 0xFF;
886 /* Get column for 16-bit access */
887 col = host->col_addr >> 1;
889 /* If we are accessing the spare region */
890 if (host->spare_only)
891 nfc_word.word = readw(&spare_buf[col]);
893 nfc_word.word = readw(&main_buf[col]);
895 /* Pick upper/lower byte of word from RAM buffer */
896 ret = nfc_word.bytes[host->col_addr & 0x1];
898 /* Update saved column address */
899 if (nand_chip->options & NAND_BUSWIDTH_16)
907 static uint16_t mxc_nand_read_word(struct mtd_info *mtd)
909 struct nand_chip *nand_chip = mtd->priv;
910 struct mxc_nand_host *host = nand_chip->priv;
914 MTDDEBUG(MTD_DEBUG_LEVEL3,
915 "mxc_nand_read_word(col = %d)\n", host->col_addr);
917 col = host->col_addr;
918 /* Adjust saved column address */
919 if (col < mtd->writesize && host->spare_only)
920 col += mtd->writesize;
922 if (col < mtd->writesize) {
923 p = (uint16_t __iomem *)(host->regs->main_area[0] +
926 p = (uint16_t __iomem *)(host->regs->spare_area[0] +
927 ((col - mtd->writesize) >> 1));
936 nfc_word[0].word = readw(p);
937 nfc_word[1].word = readw(p + 1);
939 nfc_word[2].bytes[0] = nfc_word[0].bytes[1];
940 nfc_word[2].bytes[1] = nfc_word[1].bytes[0];
942 ret = nfc_word[2].word;
947 /* Update saved column address */
948 host->col_addr = col + 2;
954 * Write data of length len to buffer buf. The data to be
955 * written on NAND Flash is first copied to RAMbuffer. After the Data Input
956 * Operation by the NFC, the data is written to NAND Flash
958 static void mxc_nand_write_buf(struct mtd_info *mtd,
959 const u_char *buf, int len)
961 struct nand_chip *nand_chip = mtd->priv;
962 struct mxc_nand_host *host = nand_chip->priv;
965 MTDDEBUG(MTD_DEBUG_LEVEL3,
966 "mxc_nand_write_buf(col = %d, len = %d)\n", host->col_addr,
969 col = host->col_addr;
971 /* Adjust saved column address */
972 if (col < mtd->writesize && host->spare_only)
973 col += mtd->writesize;
975 n = mtd->writesize + mtd->oobsize - col;
978 MTDDEBUG(MTD_DEBUG_LEVEL3,
979 "%s:%d: col = %d, n = %d\n", __func__, __LINE__, col, n);
984 if (col < mtd->writesize) {
985 p = host->regs->main_area[0] + (col & ~3);
987 p = host->regs->spare_area[0] -
988 mtd->writesize + (col & ~3);
991 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s:%d: p = %p\n", __func__,
994 if (((col | (unsigned long)&buf[i]) & 3) || n < 4) {
1000 nfc_word.word = readl(p);
1001 nfc_word.bytes[col & 3] = buf[i++];
1005 writel(nfc_word.word, p);
1007 int m = mtd->writesize - col;
1009 if (col >= mtd->writesize)
1014 MTDDEBUG(MTD_DEBUG_LEVEL3,
1015 "%s:%d: n = %d, m = %d, i = %d, col = %d\n",
1016 __func__, __LINE__, n, m, i, col);
1018 mxc_nand_memcpy32(p, (uint32_t *)&buf[i], m);
1024 /* Update saved column address */
1025 host->col_addr = col;
1029 * Read the data buffer from the NAND Flash. To read the data from NAND
1030 * Flash first the data output cycle is initiated by the NFC, which copies
1031 * the data to RAMbuffer. This data of length len is then copied to buffer buf.
1033 static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
1035 struct nand_chip *nand_chip = mtd->priv;
1036 struct mxc_nand_host *host = nand_chip->priv;
1039 MTDDEBUG(MTD_DEBUG_LEVEL3,
1040 "mxc_nand_read_buf(col = %d, len = %d)\n", host->col_addr, len);
1042 col = host->col_addr;
1044 /* Adjust saved column address */
1045 if (col < mtd->writesize && host->spare_only)
1046 col += mtd->writesize;
1048 n = mtd->writesize + mtd->oobsize - col;
1054 if (col < mtd->writesize) {
1055 p = host->regs->main_area[0] + (col & ~3);
1057 p = host->regs->spare_area[0] -
1058 mtd->writesize + (col & ~3);
1061 if (((col | (int)&buf[i]) & 3) || n < 4) {
1067 nfc_word.word = readl(p);
1068 buf[i++] = nfc_word.bytes[col & 3];
1072 int m = mtd->writesize - col;
1074 if (col >= mtd->writesize)
1078 mxc_nand_memcpy32((uint32_t *)&buf[i], p, m);
1085 /* Update saved column address */
1086 host->col_addr = col;
1090 * Used by the upper layer to verify the data in NAND Flash
1091 * with the data in the buf.
1093 static int mxc_nand_verify_buf(struct mtd_info *mtd,
1094 const u_char *buf, int len)
1100 bsize = min(len, 256);
1101 mxc_nand_read_buf(mtd, tmp, bsize);
1103 if (memcmp(buf, tmp, bsize))
1114 * This function is used by upper layer for select and
1115 * deselect of the NAND chip
1117 static void mxc_nand_select_chip(struct mtd_info *mtd, int chip)
1119 struct nand_chip *nand_chip = mtd->priv;
1120 struct mxc_nand_host *host = nand_chip->priv;
1124 /* TODO: Disable the NFC clock */
1129 /* TODO: Enable the NFC clock */
1140 * Used by the upper layer to write command to NAND Flash for
1141 * different operations to be carried out on NAND Flash
1143 void mxc_nand_command(struct mtd_info *mtd, unsigned command,
1144 int column, int page_addr)
1146 struct nand_chip *nand_chip = mtd->priv;
1147 struct mxc_nand_host *host = nand_chip->priv;
1149 MTDDEBUG(MTD_DEBUG_LEVEL3,
1150 "mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
1151 command, column, page_addr);
1153 /* Reset command state information */
1154 host->status_request = false;
1156 /* Command pre-processing step */
1159 case NAND_CMD_STATUS:
1161 host->status_request = true;
1164 case NAND_CMD_READ0:
1165 host->page_addr = page_addr;
1166 host->col_addr = column;
1167 host->spare_only = false;
1170 case NAND_CMD_READOOB:
1171 host->col_addr = column;
1172 host->spare_only = true;
1173 if (host->pagesize_2k)
1174 command = NAND_CMD_READ0; /* only READ0 is valid */
1177 case NAND_CMD_SEQIN:
1178 if (column >= mtd->writesize) {
1180 * before sending SEQIN command for partial write,
1181 * we need read one page out. FSL NFC does not support
1182 * partial write. It always sends out 512+ecc+512+ecc
1183 * for large page nand flash. But for small page nand
1184 * flash, it does support SPARE ONLY operation.
1186 if (host->pagesize_2k) {
1187 /* call ourself to read a page */
1188 mxc_nand_command(mtd, NAND_CMD_READ0, 0,
1192 host->col_addr = column - mtd->writesize;
1193 host->spare_only = true;
1195 /* Set program pointer to spare region */
1196 if (!host->pagesize_2k)
1197 send_cmd(host, NAND_CMD_READOOB);
1199 host->spare_only = false;
1200 host->col_addr = column;
1202 /* Set program pointer to page start */
1203 if (!host->pagesize_2k)
1204 send_cmd(host, NAND_CMD_READ0);
1208 case NAND_CMD_PAGEPROG:
1209 send_prog_page(host, 0, host->spare_only);
1211 if (host->pagesize_2k && !is_mxc_nfc_11()) {
1212 /* data in 4 areas */
1213 send_prog_page(host, 1, host->spare_only);
1214 send_prog_page(host, 2, host->spare_only);
1215 send_prog_page(host, 3, host->spare_only);
1221 /* Write out the command to the device. */
1222 send_cmd(host, command);
1224 /* Write out column address, if necessary */
1227 * MXC NANDFC can only perform full page+spare or
1228 * spare-only read/write. When the upper layers perform
1229 * a read/write buffer operation, we will use the saved
1230 * column address to index into the full page.
1233 if (host->pagesize_2k)
1234 /* another col addr cycle for 2k page */
1238 /* Write out page address, if necessary */
1239 if (page_addr != -1) {
1240 u32 page_mask = nand_chip->pagemask;
1242 send_addr(host, page_addr & 0xFF);
1245 } while (page_mask);
1248 /* Command post-processing step */
1251 case NAND_CMD_RESET:
1254 case NAND_CMD_READOOB:
1255 case NAND_CMD_READ0:
1256 if (host->pagesize_2k) {
1257 /* send read confirm command */
1258 send_cmd(host, NAND_CMD_READSTART);
1259 /* read for each AREA */
1260 send_read_page(host, 0, host->spare_only);
1261 if (!is_mxc_nfc_11()) {
1262 send_read_page(host, 1, host->spare_only);
1263 send_read_page(host, 2, host->spare_only);
1264 send_read_page(host, 3, host->spare_only);
1267 send_read_page(host, 0, host->spare_only);
1271 case NAND_CMD_READID:
1276 case NAND_CMD_PAGEPROG:
1279 case NAND_CMD_STATUS:
1282 case NAND_CMD_ERASE2:
1288 static void mxc_setup_config1(void)
1292 tmp = readw(&host->regs->nfc_config1);
1293 tmp |= NFC_ONE_CYCLE;
1294 tmp |= NFC_4_8N_ECC;
1295 writew(tmp, &host->regs->nfc_config1);
1296 if (host->pagesize_2k)
1297 writew(64/2, &host->regs->nfc_spare_area_size);
1299 writew(16/2, &host->regs->nfc_spare_area_size);
1302 #define mxc_setup_config1()
1305 #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1307 static u8 bbt_pattern[] = {'B', 'b', 't', '0' };
1308 static u8 mirror_pattern[] = {'1', 't', 'b', 'B' };
1310 static struct nand_bbt_descr bbt_main_descr = {
1311 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
1312 NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1317 .pattern = bbt_pattern,
1320 static struct nand_bbt_descr bbt_mirror_descr = {
1321 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
1322 NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1327 .pattern = mirror_pattern,
1332 int board_nand_init(struct nand_chip *this)
1334 struct mtd_info *mtd;
1338 #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1339 this->options |= NAND_USE_FLASH_BBT;
1340 this->bbt_td = &bbt_main_descr;
1341 this->bbt_md = &bbt_mirror_descr;
1344 /* structures must be linked */
1349 /* 5 us command delay time */
1350 this->chip_delay = 5;
1353 this->dev_ready = mxc_nand_dev_ready;
1354 this->cmdfunc = mxc_nand_command;
1355 this->select_chip = mxc_nand_select_chip;
1356 this->read_byte = mxc_nand_read_byte;
1357 this->read_word = mxc_nand_read_word;
1358 this->write_buf = mxc_nand_write_buf;
1359 this->read_buf = mxc_nand_read_buf;
1360 this->verify_buf = mxc_nand_verify_buf;
1362 host->regs = (struct nfc_regs __iomem *)CONFIG_MXC_NAND_REGS_BASE;
1365 #ifdef CONFIG_MXC_NAND_HWECC
1366 this->ecc.calculate = mxc_nand_calculate_ecc;
1367 this->ecc.hwctl = mxc_nand_enable_hwecc;
1368 this->ecc.correct = mxc_nand_correct_data;
1369 if (is_mxc_nfc_11()) {
1370 this->ecc.mode = NAND_ECC_HW_SYNDROME;
1371 this->ecc.read_page = mxc_nand_read_page_syndrome;
1372 this->ecc.read_page_raw = mxc_nand_read_page_raw_syndrome;
1373 this->ecc.read_oob = mxc_nand_read_oob_syndrome;
1374 this->ecc.write_page = mxc_nand_write_page_syndrome;
1375 this->ecc.write_page_raw = mxc_nand_write_page_raw_syndrome;
1376 this->ecc.write_oob = mxc_nand_write_oob_syndrome;
1377 this->ecc.bytes = 9;
1378 this->ecc.prepad = 7;
1380 this->ecc.mode = NAND_ECC_HW;
1383 host->pagesize_2k = 0;
1385 this->ecc.size = 512;
1386 tmp = readw(&host->regs->nfc_config1);
1388 writew(tmp, &host->regs->nfc_config1);
1390 this->ecc.layout = &nand_soft_eccoob;
1391 this->ecc.mode = NAND_ECC_SOFT;
1392 tmp = readw(&host->regs->nfc_config1);
1394 writew(tmp, &host->regs->nfc_config1);
1397 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1401 * Unlock the internal RAM Buffer
1403 writew(0x2, &host->regs->nfc_config);
1405 /* Blocks to be unlocked */
1406 writew(0x0, &host->regs->nfc_unlockstart_blkaddr);
1407 /* Originally (Freescale LTIB 2.6.21) 0x4000 was written to the
1408 * unlockend_blkaddr, but the magic 0x4000 does not always work
1409 * when writing more than some 32 megabytes (on 2k page nands)
1410 * However 0xFFFF doesn't seem to have this kind
1411 * of limitation (tried it back and forth several times).
1412 * The linux kernel driver sets this to 0xFFFF for the v2 controller
1413 * only, but probably this was not tested there for v1.
1414 * The very same limitation seems to apply to this kernel driver.
1415 * This might be NAND chip specific and the i.MX31 datasheet is
1416 * extremely vague about the semantics of this register.
1418 writew(0xFFFF, &host->regs->nfc_unlockend_blkaddr);
1420 /* Unlock Block Command for given address range */
1421 writew(0x4, &host->regs->nfc_wrprot);
1423 /* NAND bus width determines access functions used by upper layer */
1424 if (is_16bit_nand())
1425 this->options |= NAND_BUSWIDTH_16;
1427 #ifdef CONFIG_SYS_NAND_LARGEPAGE
1428 host->pagesize_2k = 1;
1429 this->ecc.layout = &nand_hw_eccoob2k;
1431 host->pagesize_2k = 0;
1432 this->ecc.layout = &nand_hw_eccoob;
1434 mxc_setup_config1();