imx-nand : remove wrong part of commit ca326e88827e08e723046a21b18e74d41d15ace7
[barebox-mini2440.git] / drivers / nand / nand_imx.c
blobee8f0f427bde768c08a78bc33a5241280918436c
1 /*
2 * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
3 * Copyright 2008 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
4 */
6 /*
7 * The code contained herein is licensed under the GNU General Public
8 * License. You may obtain a copy of the GNU General Public License
9 * Version 2 or later at the following locations:
11 * http://www.opensource.org/licenses/gpl-license.html
12 * http://www.gnu.org/copyleft/gpl.html
16 * MX21 Hardware contains a bug which causes HW ECC to fail for two
17 * consecutive read pages containing 1bit Errors (See MX21 Chip Erata,
18 * Erratum 16). Use software ECC for this chip.
21 #include <common.h>
22 #include <driver.h>
23 #include <malloc.h>
24 #include <init.h>
25 #include <linux/mtd/mtd.h>
26 #include <linux/mtd/nand.h>
27 #include <asm/arch/imx-nand.h>
28 #include <asm/arch/imx-regs.h>
29 #include <asm/io.h>
30 #include <errno.h>
32 #define NFMS (*((volatile u32 *)(IMX_SYSTEM_CTL_BASE + 0x14)))
33 #define NFMS_BIT 5
35 #define DVR_VER "2.0"
38 * Addresses for NFC registers
40 #define NFC_BUF_SIZE 0xE00
41 #define NFC_BUF_ADDR 0xE04
42 #define NFC_FLASH_ADDR 0xE06
43 #define NFC_FLASH_CMD 0xE08
44 #define NFC_CONFIG 0xE0A
45 #define NFC_ECC_STATUS_RESULT 0xE0C
46 #define NFC_RSLTMAIN_AREA 0xE0E
47 #define NFC_RSLTSPARE_AREA 0xE10
48 #define NFC_WRPROT 0xE12
49 #define NFC_UNLOCKSTART_BLKADDR 0xE14
50 #define NFC_UNLOCKEND_BLKADDR 0xE16
51 #define NFC_NF_WRPRST 0xE18
52 #define NFC_CONFIG1 0xE1A
53 #define NFC_CONFIG2 0xE1C
56 * Addresses for NFC RAM BUFFER Main area 0
58 #define MAIN_AREA0 0x000
59 #define MAIN_AREA1 0x200
60 #define MAIN_AREA2 0x400
61 #define MAIN_AREA3 0x600
64 * Addresses for NFC SPARE BUFFER Spare area 0
66 #define SPARE_AREA0 0x800
67 #define SPARE_AREA1 0x810
68 #define SPARE_AREA2 0x820
69 #define SPARE_AREA3 0x830
72 * Set INT to 0, FCMD to 1, rest to 0 in NFC_CONFIG2 Register for Command
73 * operation
75 #define NFC_CMD 0x1
78 * Set INT to 0, FADD to 1, rest to 0 in NFC_CONFIG2 Register for Address
79 * operation
81 #define NFC_ADDR 0x2
84 * Set INT to 0, FDI to 1, rest to 0 in NFC_CONFIG2 Register for Input
85 * operation
87 #define NFC_INPUT 0x4
90 * Set INT to 0, FDO to 001, rest to 0 in NFC_CONFIG2 Register for Data Output
91 * operation
93 #define NFC_OUTPUT 0x8
96 * Set INT to 0, FD0 to 010, rest to 0 in NFC_CONFIG2 Register for Read ID
97 * operation
99 #define NFC_ID 0x10
102 * Set INT to 0, FDO to 100, rest to 0 in NFC_CONFIG2 Register for Read Status
103 * operation
105 #define NFC_STATUS 0x20
108 * Set INT to 1, rest to 0 in NFC_CONFIG2 Register for Read Status
109 * operation
111 #define NFC_INT 0x8000
113 #define NFC_SP_EN (1 << 2)
114 #define NFC_ECC_EN (1 << 3)
115 #define NFC_INT_MSK (1 << 4)
116 #define NFC_BIG (1 << 5)
117 #define NFC_RST (1 << 6)
118 #define NFC_CE (1 << 7)
119 #define NFC_ONE_CYCLE (1 << 8)
121 #ifdef CONFIG_NAND_IMX_BOOT
122 #define __nand_boot_init __bare_init
123 #else
124 #define __nand_boot_init
125 #endif
127 struct imx_nand_host {
128 struct mtd_info mtd;
129 struct nand_chip nand;
130 struct mtd_partition *parts;
131 struct device_d *dev;
133 void __iomem *regs;
134 int spare_only;
135 int status_request;
136 u16 col_addr;
137 struct clk *clk;
139 int pagesize_2k;
143 * Macros to get byte and bit positions of ECC
145 #define COLPOS(x) ((x) >> 3)
146 #define BITPOS(x) ((x)& 0xf)
148 /* Define single bit Error positions in Main & Spare area */
149 #define MAIN_SINGLEBIT_ERROR 0x4
150 #define SPARE_SINGLEBIT_ERROR 0x1
153 * OOB placement block for use with hardware ecc generation
155 static struct nand_ecclayout nand_hw_eccoob_8 = {
156 .eccbytes = 5,
157 .eccpos = {6, 7, 8, 9, 10},
158 .oobfree = {{0, 5}, {11, 5}}
161 static struct nand_ecclayout nand_hw_eccoob_16 = {
162 .eccbytes = 5,
163 .eccpos = {6, 7, 8, 9, 10},
164 .oobfree = {{0, 6}, {12, 4}}
168 * This function polls the NANDFC to wait for the basic operation to complete by
169 * checking the INT bit of config2 register.
171 * @param max_retries number of retry attempts (separated by 1 us)
172 * @param param parameter for debug
174 static void __nand_boot_init wait_op_done(struct imx_nand_host *host, u16 param)
176 u32 tmp;
177 int i;
179 /* This is a timeout of roughly 15ms on my system. We
180 * need about 2us, but be generous. Don't use udelay
181 * here as we might be here from nand booting.
183 for (i = 0; i < 100000; i++) {
184 if (readw(host->regs + NFC_CONFIG2) & NFC_INT) {
185 tmp = readw(host->regs + NFC_CONFIG2);
186 tmp &= ~NFC_INT;
187 writew(tmp, host->regs + NFC_CONFIG2);
188 return;
194 * This function issues the specified command to the NAND device and
195 * waits for completion.
197 * @param cmd command for NAND Flash
199 static void __nand_boot_init send_cmd(struct imx_nand_host *host, u16 cmd)
201 MTD_DEBUG(MTD_DEBUG_LEVEL3, "send_cmd(host, 0x%x)\n", cmd);
203 writew(cmd, host->regs + NFC_FLASH_CMD);
204 writew(NFC_CMD, host->regs + NFC_CONFIG2);
206 /* Wait for operation to complete */
207 wait_op_done(host, cmd);
211 * This function sends an address (or partial address) to the
212 * NAND device. The address is used to select the source/destination for
213 * a NAND command.
215 * @param addr address to be written to NFC.
216 * @param islast True if this is the last address cycle for command
218 static void __nand_boot_init send_addr(struct imx_nand_host *host, u16 addr)
220 MTD_DEBUG(MTD_DEBUG_LEVEL3, "send_addr(host, 0x%x %d)\n", addr, islast);
222 writew(addr, host->regs + NFC_FLASH_ADDR);
223 writew(NFC_ADDR, host->regs + NFC_CONFIG2);
225 /* Wait for operation to complete */
226 wait_op_done(host, addr);
230 * This function requests the NANDFC to initate the transfer
231 * of data currently in the NANDFC RAM buffer to the NAND device.
233 * @param buf_id Specify Internal RAM Buffer number (0-3)
234 * @param spare_only set true if only the spare area is transferred
236 static void send_prog_page(struct imx_nand_host *host, u8 buf_id,
237 int spare_only)
239 MTD_DEBUG(MTD_DEBUG_LEVEL3, "send_prog_page (%d)\n", spare_only);
241 /* NANDFC buffer 0 is used for page read/write */
243 writew(buf_id, host->regs + NFC_BUF_ADDR);
245 /* Configure spare or page+spare access */
246 if (!host->pagesize_2k) {
247 u16 config1 = readw(host->regs + NFC_CONFIG1);
248 if (spare_only)
249 config1 |= NFC_SP_EN;
250 else
251 config1 &= ~(NFC_SP_EN);
252 writew(config1, host->regs + NFC_CONFIG1);
255 writew(NFC_INPUT, host->regs + NFC_CONFIG2);
257 /* Wait for operation to complete */
258 wait_op_done(host, spare_only);
262 * This function requests the NANDFC to initate the transfer
263 * of data from the NAND device into in the NANDFC ram buffer.
265 * @param buf_id Specify Internal RAM Buffer number (0-3)
266 * @param spare_only set true if only the spare area is transferred
268 static void __nand_boot_init send_read_page(struct imx_nand_host *host,
269 u8 buf_id, int spare_only)
271 MTD_DEBUG(MTD_DEBUG_LEVEL3, "send_read_page (%d)\n", spare_only);
273 /* NANDFC buffer 0 is used for page read/write */
274 writew(buf_id, host->regs + NFC_BUF_ADDR);
276 /* Configure spare or page+spare access */
277 if (!host->pagesize_2k) {
278 u32 config1 = readw(host->regs + NFC_CONFIG1);
279 if (spare_only)
280 config1 |= NFC_SP_EN;
281 else
282 config1 &= ~NFC_SP_EN;
283 writew(config1, host->regs + NFC_CONFIG1);
286 writew(NFC_OUTPUT, host->regs + NFC_CONFIG2);
288 /* Wait for operation to complete */
289 wait_op_done(host, spare_only);
293 * This function requests the NANDFC to perform a read of the
294 * NAND device ID.
296 static void send_read_id(struct imx_nand_host *host)
298 struct nand_chip *this = &host->nand;
299 u16 tmp;
301 /* NANDFC buffer 0 is used for device ID output */
302 writew(0x0, host->regs + NFC_BUF_ADDR);
304 /* Read ID into main buffer */
305 tmp = readw(host->regs + NFC_CONFIG1);
306 tmp &= ~NFC_SP_EN;
307 writew(tmp, host->regs + NFC_CONFIG1);
309 writew(NFC_ID, host->regs + NFC_CONFIG2);
311 /* Wait for operation to complete */
312 wait_op_done(host, 0);
314 if (this->options & NAND_BUSWIDTH_16) {
315 volatile u16 *mainbuf = host->regs + MAIN_AREA0;
318 * Pack the every-other-byte result for 16-bit ID reads
319 * into every-byte as the generic code expects and various
320 * chips implement.
323 mainbuf[0] = (mainbuf[0] & 0xff) | ((mainbuf[1] & 0xff) << 8);
324 mainbuf[1] = (mainbuf[2] & 0xff) | ((mainbuf[3] & 0xff) << 8);
325 mainbuf[2] = (mainbuf[4] & 0xff) | ((mainbuf[5] & 0xff) << 8);
330 * This function requests the NANDFC to perform a read of the
331 * NAND device status and returns the current status.
333 * @return device status
335 static u16 get_dev_status(struct imx_nand_host *host)
337 volatile u16 *mainbuf = host->regs + MAIN_AREA1;
338 u32 store;
339 u16 ret, tmp;
340 /* Issue status request to NAND device */
342 /* store the main area1 first word, later do recovery */
343 store = *((u32 *) mainbuf);
345 * NANDFC buffer 1 is used for device status to prevent
346 * corruption of read/write buffer on status requests.
348 writew(1, host->regs + NFC_BUF_ADDR);
350 /* Read status into main buffer */
351 tmp = readw(host->regs + NFC_CONFIG1);
352 tmp &= ~NFC_SP_EN;
353 writew(tmp, host->regs + NFC_CONFIG1);
355 writew(NFC_STATUS, host->regs + NFC_CONFIG2);
357 /* Wait for operation to complete */
358 wait_op_done(host, 0);
360 /* Status is placed in first word of main buffer */
361 /* get status, then recovery area 1 data */
362 ret = mainbuf[0];
363 *((u32 *) mainbuf) = store;
365 return ret;
369 * This function is used by upper layer to checks if device is ready
371 * @param mtd MTD structure for the NAND Flash
373 * @return 0 if device is busy else 1
375 static int imx_nand_dev_ready(struct mtd_info *mtd)
378 * NFC handles R/B internally.Therefore,this function
379 * always returns status as ready.
381 return 1;
384 static void imx_nand_enable_hwecc(struct mtd_info *mtd, int mode)
387 * If HW ECC is enabled, we turn it on during init. There is
388 * no need to enable again here.
392 static int imx_nand_correct_data(struct mtd_info *mtd, u_char * dat,
393 u_char * read_ecc, u_char * calc_ecc)
395 struct nand_chip *nand_chip = mtd->priv;
396 struct imx_nand_host *host = nand_chip->priv;
399 * 1-Bit errors are automatically corrected in HW. No need for
400 * additional correction. 2-Bit errors cannot be corrected by
401 * HW ECC, so we need to return failure
403 u16 ecc_status = readw(host->regs + NFC_ECC_STATUS_RESULT);
405 if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) {
406 MTD_DEBUG(MTD_DEBUG_LEVEL0,
407 "MXC_NAND: HWECC uncorrectable 2-bit ECC error\n");
408 return -1;
411 return 0;
414 static int imx_nand_calculate_ecc(struct mtd_info *mtd, const u_char * dat,
415 u_char * ecc_code)
417 return 0;
421 * This function reads byte from the NAND Flash
423 * @param mtd MTD structure for the NAND Flash
425 * @return data read from the NAND Flash
427 static u_char imx_nand_read_byte(struct mtd_info *mtd)
429 struct nand_chip *nand_chip = mtd->priv;
430 struct imx_nand_host *host = nand_chip->priv;
432 u_char ret = 0;
433 u16 col, rdword;
434 volatile u16 *mainbuf = host->regs + MAIN_AREA0;
435 volatile u16 *sparebuf = host->regs + SPARE_AREA0;
437 /* Check for status request */
438 if (host->status_request)
439 return get_dev_status(host) & 0xFF;
441 /* Get column for 16-bit access */
442 col = host->col_addr >> 1;
444 /* If we are accessing the spare region */
445 if (host->spare_only)
446 rdword = sparebuf[col];
447 else
448 rdword = mainbuf[col];
450 /* Pick upper/lower byte of word from RAM buffer */
451 if (host->col_addr & 0x1)
452 ret = (rdword >> 8) & 0xFF;
453 else
454 ret = rdword & 0xFF;
456 /* Update saved column address */
457 host->col_addr++;
459 return ret;
463 * This function reads word from the NAND Flash
465 * @param mtd MTD structure for the NAND Flash
467 * @return data read from the NAND Flash
469 static u16 imx_nand_read_word(struct mtd_info *mtd)
471 struct nand_chip *nand_chip = mtd->priv;
472 struct imx_nand_host *host = nand_chip->priv;
474 u16 col;
475 u16 rdword, ret;
476 volatile u16 *p;
478 MTD_DEBUG(MTD_DEBUG_LEVEL3,
479 "imx_nand_read_word(col = %d)\n", host->col_addr);
481 col = host->col_addr;
482 /* Adjust saved column address */
483 if (col < mtd->writesize && host->spare_only)
484 col += mtd->writesize;
486 if (col < mtd->writesize)
487 p = (host->regs + MAIN_AREA0) + (col >> 1);
488 else
489 p = (host->regs + SPARE_AREA0) + ((col - mtd->writesize) >> 1);
491 if (col & 1) {
492 rdword = *p;
493 ret = (rdword >> 8) & 0xff;
494 rdword = *(p + 1);
495 ret |= (rdword << 8) & 0xff00;
496 } else
497 ret = *p;
499 /* Update saved column address */
500 host->col_addr = col + 2;
502 return ret;
505 static void __nand_boot_init memcpy32(void *trg, const void *src, int size)
507 int i;
508 unsigned int *t = trg;
509 unsigned const int *s = src;
511 for (i = 0; i < (size >> 2); i++)
512 *t++ = *s++;
516 * This function writes data of length \b len to buffer \b buf. The data to be
517 * written on NAND Flash is first copied to RAMbuffer. After the Data Input
518 * Operation by the NFC, the data is written to NAND Flash
520 * @param mtd MTD structure for the NAND Flash
521 * @param buf data to be written to NAND Flash
522 * @param len number of bytes to be written
524 static void imx_nand_write_buf(struct mtd_info *mtd,
525 const u_char *buf, int len)
527 struct nand_chip *nand_chip = mtd->priv;
528 struct imx_nand_host *host = nand_chip->priv;
530 int n;
531 int col;
532 int i = 0;
534 MTD_DEBUG(MTD_DEBUG_LEVEL3,
535 "imx_nand_write_buf(col = %d, len = %d)\n", host->col_addr,
536 len);
538 col = host->col_addr;
540 /* Adjust saved column address */
541 if (col < mtd->writesize && host->spare_only)
542 col += mtd->writesize;
544 n = mtd->writesize + mtd->oobsize - col;
545 n = min(len, n);
547 MTD_DEBUG(MTD_DEBUG_LEVEL3,
548 "%s:%d: col = %d, n = %d\n", __FUNCTION__, __LINE__, col, n);
550 while (n) {
551 volatile u32 *p;
552 if (col < mtd->writesize)
553 p = (volatile u32 *)((ulong) (host->regs + MAIN_AREA0)
554 + (col & ~3));
555 else
556 p = (volatile u32 *)((ulong) (host->regs + SPARE_AREA0)
557 - mtd->writesize + (col & ~3));
559 MTD_DEBUG(MTD_DEBUG_LEVEL3, "%s:%d: p = %p\n", __FUNCTION__,
560 __LINE__, p);
562 if (((col | (int)&buf[i]) & 3) || n < 16) {
563 u32 data = 0;
565 if (col & 3 || n < 4)
566 data = *p;
568 switch (col & 3) {
569 case 0:
570 if (n) {
571 data = (data & 0xffffff00) |
572 (buf[i++] << 0);
573 n--;
574 col++;
576 case 1:
577 if (n) {
578 data = (data & 0xffff00ff) |
579 (buf[i++] << 8);
580 n--;
581 col++;
583 case 2:
584 if (n) {
585 data = (data & 0xff00ffff) |
586 (buf[i++] << 16);
587 n--;
588 col++;
590 case 3:
591 if (n) {
592 data = (data & 0x00ffffff) |
593 (buf[i++] << 24);
594 n--;
595 col++;
599 *p = data;
600 } else {
601 int m = mtd->writesize - col;
603 if (col >= mtd->writesize)
604 m += mtd->oobsize;
606 m = min(n, m) & ~3;
608 MTD_DEBUG(MTD_DEBUG_LEVEL3,
609 "%s:%d: n = %d, m = %d, i = %d, col = %d\n",
610 __FUNCTION__, __LINE__, n, m, i, col);
612 #ifdef CONFIG_ARM_OPTIMZED_STRING_FUNCTIONS
613 memcpy((void *)(p), &buf[i], m);
614 #else
615 memcpy32((void *)(p), &buf[i], m);
616 #endif
617 col += m;
618 i += m;
619 n -= m;
622 /* Update saved column address */
623 host->col_addr = col;
627 * This function is used to read the data buffer from the NAND Flash. To
628 * read the data from NAND Flash first the data output cycle is initiated by
629 * the NFC, which copies the data to RAMbuffer. This data of length \b len is
630 * then copied to buffer \b buf.
632 * @param mtd MTD structure for the NAND Flash
633 * @param buf data to be read from NAND Flash
634 * @param len number of bytes to be read
636 static void imx_nand_read_buf(struct mtd_info *mtd, u_char * buf, int len)
638 struct nand_chip *nand_chip = mtd->priv;
639 struct imx_nand_host *host = nand_chip->priv;
641 int n;
642 int col;
643 int i = 0;
645 MTD_DEBUG(MTD_DEBUG_LEVEL3,
646 "imx_nand_read_buf(col = %d, len = %d)\n", host->col_addr,
647 len);
649 col = host->col_addr;
650 /* Adjust saved column address */
651 if (col < mtd->writesize && host->spare_only)
652 col += mtd->writesize;
654 n = mtd->writesize + mtd->oobsize - col;
655 n = min(len, n);
657 while (n) {
658 volatile u32 *p;
660 if (col < mtd->writesize)
661 p = (volatile u32 *)((ulong) (host->regs + MAIN_AREA0)
662 + (col & ~3));
663 else
664 p = (volatile u32 *)((ulong) (host->regs + SPARE_AREA0)
665 - mtd->writesize + (col & ~3));
666 if (((col | (int)&buf[i]) & 3) || n < 16) {
667 u32 data;
668 data = *p;
669 switch (col & 3) {
670 case 0:
671 if (n) {
672 buf[i++] = (u8) (data);
673 n--;
674 col++;
676 case 1:
677 if (n) {
678 buf[i++] = (u8) (data >> 8);
679 n--;
680 col++;
682 case 2:
683 if (n) {
684 buf[i++] = (u8) (data >> 16);
685 n--;
686 col++;
688 case 3:
689 if (n) {
690 buf[i++] = (u8) (data >> 24);
691 n--;
692 col++;
695 } else {
696 int m = mtd->writesize - col;
697 if (col >= mtd->writesize)
698 m += mtd->oobsize;
700 m = min(n, m) & ~3;
701 #ifdef CONFIG_ARM_OPTIMZED_STRING_FUNCTIONS
702 memcpy(&buf[i], (void *)p, m);
703 #else
704 memcpy32(&buf[i], (void *)(p), m);
705 #endif
706 col += m;
707 i += m;
708 n -= m;
711 /* Update saved column address */
712 host->col_addr = col;
717 * This function is used by the upper layer to verify the data in NAND Flash
718 * with the data in the \b buf.
720 * @param mtd MTD structure for the NAND Flash
721 * @param buf data to be verified
722 * @param len length of the data to be verified
724 * @return -EFAULT if error else 0
727 static int
728 imx_nand_verify_buf(struct mtd_info *mtd, const u_char * buf, int len)
730 return -EFAULT;
734 * This function is used by upper layer for select and deselect of the NAND
735 * chip
737 * @param mtd MTD structure for the NAND Flash
738 * @param chip val indicating select or deselect
740 static void imx_nand_select_chip(struct mtd_info *mtd, int chip)
742 #ifdef CONFIG_MTD_NAND_MXC_FORCE_CE
743 u16 tmp;
745 if (chip > 0) {
746 MTD_DEBUG(MTD_DEBUG_LEVEL0,
747 "ERROR: Illegal chip select (chip = %d)\n", chip);
748 return;
751 if (chip == -1) {
752 tmp = readw(host->regs + NFC_CONFIG1);
753 tmp &= ~NFC_CE;
754 writew(tmp, host->regs + NFC_CONFIG1);
755 return;
758 tmp = readw(host->regs + NFC_CONFIG1);
759 tmp |= NFC_CE;
760 writew(tmp, host->regs + NFC_CONFIG1);
761 #endif
765 * This function is used by the upper layer to write command to NAND Flash for
766 * different operations to be carried out on NAND Flash
768 * @param mtd MTD structure for the NAND Flash
769 * @param command command for NAND Flash
770 * @param column column offset for the page read
771 * @param page_addr page to be read from NAND Flash
773 static void imx_nand_command(struct mtd_info *mtd, unsigned command,
774 int column, int page_addr)
776 struct nand_chip *nand_chip = mtd->priv;
777 struct imx_nand_host *host = nand_chip->priv;
779 MTD_DEBUG(MTD_DEBUG_LEVEL3,
780 "imx_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
781 command, column, page_addr);
784 * Reset command state information
786 host->status_request = 0;
789 * Command pre-processing step
791 switch (command) {
793 case NAND_CMD_STATUS:
794 host->col_addr = 0;
795 host->status_request = 1;
796 break;
798 case NAND_CMD_READ0:
799 host->col_addr = column;
800 host->spare_only = 0;
801 break;
803 case NAND_CMD_READOOB:
804 host->col_addr = column;
805 host->spare_only = 1;
806 if (host->pagesize_2k)
807 /* only READ0 is valid */
808 command = NAND_CMD_READ0;
809 break;
811 case NAND_CMD_SEQIN:
812 if (column >= mtd->writesize) {
813 if (host->pagesize_2k) {
815 * FIXME: before send SEQIN command for write
816 * OOB, we must read one page out. For K9F1GXX
817 * has no READ1 command to set current HW
818 * pointer to spare area, we must write the
819 * whole page including OOB together.
821 /* call ourself to read a page */
822 imx_nand_command(mtd, NAND_CMD_READ0, 0,
823 page_addr);
825 host->col_addr = column - mtd->writesize;
826 host->spare_only = 1;
827 /* Set program pointer to spare region */
828 if (!host->pagesize_2k)
829 send_cmd(host, NAND_CMD_READOOB);
830 } else {
831 host->spare_only = 0;
832 host->col_addr = column;
833 /* Set program pointer to page start */
834 if (!host->pagesize_2k)
835 send_cmd(host, NAND_CMD_READ0);
837 break;
839 case NAND_CMD_PAGEPROG:
840 send_prog_page(host, 0, host->spare_only);
842 if (host->pagesize_2k) {
843 /* data in 4 areas datas */
844 send_prog_page(host, 1, host->spare_only);
845 send_prog_page(host, 2, host->spare_only);
846 send_prog_page(host, 3, host->spare_only);
849 break;
851 case NAND_CMD_ERASE1:
852 break;
856 * Write out the command to the device.
858 send_cmd(host, command);
861 * Write out column address, if necessary
863 if (column != -1) {
865 * MXC NANDFC can only perform full page+spare or
866 * spare-only read/write. When the upper layers
867 * layers perform a read/write buf operation,
868 * we will used the saved column adress to index into
869 * the full page.
871 send_addr(host, 0);
872 if (host->pagesize_2k)
873 /* another col addr cycle for 2k page */
874 send_addr(host, 0);
878 * Write out page address, if necessary
880 if (page_addr != -1) {
881 send_addr(host, (page_addr & 0xff)); /* paddr_0 - p_addr_7 */
883 if (host->pagesize_2k) {
884 send_addr(host, (page_addr >> 8) & 0xFF);
885 if (mtd->size >= 0x10000000) {
886 send_addr(host, (page_addr >> 16) & 0xff);
888 } else {
889 /* One more address cycle for higher density devices */
890 if (mtd->size >= 0x4000000) {
891 /* paddr_8 - paddr_15 */
892 send_addr(host, (page_addr >> 8) & 0xff);
893 send_addr(host, (page_addr >> 16) & 0xff);
894 } else
895 /* paddr_8 - paddr_15 */
896 send_addr(host, (page_addr >> 8) & 0xff);
901 * Command post-processing step
903 switch (command) {
905 case NAND_CMD_RESET:
906 break;
908 case NAND_CMD_READOOB:
909 case NAND_CMD_READ0:
910 if (host->pagesize_2k) {
911 /* send read confirm command */
912 send_cmd(host, NAND_CMD_READSTART);
913 /* read for each AREA */
914 send_read_page(host, 0, host->spare_only);
915 send_read_page(host, 1, host->spare_only);
916 send_read_page(host, 2, host->spare_only);
917 send_read_page(host, 3, host->spare_only);
918 } else {
919 send_read_page(host, 0, host->spare_only);
921 break;
923 case NAND_CMD_READID:
924 host->col_addr = 0;
925 send_read_id(host);
926 break;
928 case NAND_CMD_PAGEPROG:
929 break;
931 case NAND_CMD_STATUS:
932 break;
934 case NAND_CMD_ERASE2:
935 break;
939 #ifdef CONFIG_MXC_NAND_LOW_LEVEL_ERASE
940 static void imx_low_erase(struct mtd_info *mtd)
943 struct nand_chip *this = mtd->priv;
944 unsigned int page_addr, addr;
945 u_char status;
947 MTD_DEBUG(MTD_DEBUG_LEVEL0, "MXC_ND : imx_low_erase:Erasing NAND\n");
948 for (addr = 0; addr < this->chipsize; addr += mtd->erasesize) {
949 page_addr = addr / mtd->writesize;
950 imx_nand_command(mtd, NAND_CMD_ERASE1, -1, page_addr);
951 imx_nand_command(mtd, NAND_CMD_ERASE2, -1, -1);
952 imx_nand_command(mtd, NAND_CMD_STATUS, -1, -1);
953 status = imx_nand_read_byte(mtd);
954 if (status & NAND_STATUS_FAIL) {
955 printk(KERN_ERR
956 "ERASE FAILED(block = %d,status = 0x%x)\n",
957 addr / mtd->erasesize, status);
962 #endif
964 * This function is called during the driver binding process.
966 * @param pdev the device structure used to store device specific
967 * information that is used by the suspend, resume and
968 * remove functions
970 * @return The function always returns 0.
973 static int __init imxnd_probe(struct device_d *dev)
975 struct nand_chip *this;
976 struct mtd_info *mtd;
977 struct imx_nand_platform_data *pdata = dev->platform_data;
978 struct imx_nand_host *host;
979 u16 tmp;
980 int err = 0;
981 #ifdef CONFIG_ARCH_IMX27
982 PCCR1 |= PCCR1_NFC_BAUDEN;
983 #endif
984 /* Allocate memory for MTD device structure and private data */
985 host = kzalloc(sizeof(struct imx_nand_host), GFP_KERNEL);
986 if (!host)
987 return -ENOMEM;
989 host->dev = dev;
990 /* structures must be linked */
991 this = &host->nand;
992 mtd = &host->mtd;
993 mtd->priv = this;
995 /* 50 us command delay time */
996 this->chip_delay = 5;
998 this->priv = host;
999 this->dev_ready = imx_nand_dev_ready;
1000 this->cmdfunc = imx_nand_command;
1001 this->select_chip = imx_nand_select_chip;
1002 this->read_byte = imx_nand_read_byte;
1003 this->read_word = imx_nand_read_word;
1004 this->write_buf = imx_nand_write_buf;
1005 this->read_buf = imx_nand_read_buf;
1006 this->verify_buf = imx_nand_verify_buf;
1007 #if 0
1008 host->clk = clk_get(&pdev->dev, "nfc_clk");
1009 if (IS_ERR(host->clk))
1010 goto eclk;
1012 clk_enable(host->clk);
1013 #endif
1015 host->regs = (void __iomem *)dev->map_base;
1017 tmp = readw(host->regs + NFC_CONFIG1);
1018 tmp |= NFC_INT_MSK;
1019 writew(tmp, host->regs + NFC_CONFIG1);
1021 if (pdata->hw_ecc) {
1022 this->ecc.calculate = imx_nand_calculate_ecc;
1023 this->ecc.hwctl = imx_nand_enable_hwecc;
1024 this->ecc.correct = imx_nand_correct_data;
1025 this->ecc.mode = NAND_ECC_HW;
1026 this->ecc.size = 512;
1027 this->ecc.bytes = 3;
1028 this->ecc.layout = &nand_hw_eccoob_8;
1029 tmp = readw(host->regs + NFC_CONFIG1);
1030 tmp |= NFC_ECC_EN;
1031 writew(tmp, host->regs + NFC_CONFIG1);
1032 } else {
1033 this->ecc.size = 512;
1034 this->ecc.bytes = 3;
1035 this->ecc.layout = &nand_hw_eccoob_8;
1036 this->ecc.mode = NAND_ECC_SOFT;
1037 tmp = readw(host->regs + NFC_CONFIG1);
1038 tmp &= ~NFC_ECC_EN;
1039 writew(tmp, host->regs + NFC_CONFIG1);
1042 /* Reset NAND */
1043 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1045 /* preset operation */
1046 /* Unlock the internal RAM Buffer */
1047 writew(0x2, host->regs + NFC_CONFIG);
1049 /* Blocks to be unlocked */
1050 writew(0x0, host->regs + NFC_UNLOCKSTART_BLKADDR);
1051 writew(0x4000, host->regs + NFC_UNLOCKEND_BLKADDR);
1053 /* Unlock Block Command for given address range */
1054 writew(0x4, host->regs + NFC_WRPROT);
1056 /* NAND bus width determines access funtions used by upper layer */
1057 if (pdata->width == 2) {
1058 this->options |= NAND_BUSWIDTH_16;
1059 this->ecc.layout = &nand_hw_eccoob_16;
1062 host->pagesize_2k = 0;
1064 this->options |= NAND_SKIP_BBTSCAN;
1066 /* Scan to find existence of the device */
1067 if (nand_scan(mtd, 1)) {
1068 MTD_DEBUG(MTD_DEBUG_LEVEL0,
1069 "MXC_ND: Unable to find any NAND device.\n");
1070 err = -ENXIO;
1071 goto escan;
1074 add_mtd_device(mtd);
1076 #ifdef CONFIG_MXC_NAND_LOW_LEVEL_ERASE
1077 /* Erase all the blocks of a NAND */
1078 imx_low_erase(mtd);
1079 #endif
1081 dev->priv = host;
1083 return 0;
1085 escan:
1086 kfree(host);
1088 return err;
1092 static struct driver_d imx_nand_driver = {
1093 .name = "imx_nand",
1094 .probe = imxnd_probe,
1097 #ifdef CONFIG_NAND_IMX_BOOT
1099 static void __nand_boot_init nfc_addr(struct imx_nand_host *host, u32 offs)
1101 send_addr(host, offs & 0xff);
1102 send_addr(host, (offs >> 9) & 0xff);
1103 send_addr(host, (offs >> 17) & 0xff);
1104 send_addr(host, (offs >> 25) & 0xff);
1107 static int __nand_boot_init block_is_bad(struct imx_nand_host *host, u32 offs)
1109 send_cmd(host, NAND_CMD_READOOB);
1110 nfc_addr(host, offs);
1111 send_read_page(host, 0, 1);
1113 return (readw(host->regs + SPARE_AREA0 + 4) & 0xff) == 0xff ? 0 : 1;
1116 void __nand_boot_init imx_nand_load_image(void *dest, int size, int pagesize,
1117 int blocksize)
1119 struct imx_nand_host host;
1120 u32 tmp, page, block;
1122 host.regs = (void __iomem *)IMX_NFC_BASE;
1123 host.pagesize_2k = (pagesize == 2048);
1125 send_cmd(&host, NAND_CMD_RESET);
1127 /* preset operation */
1128 /* Unlock the internal RAM Buffer */
1129 writew(0x2, host.regs + NFC_CONFIG);
1131 /* Blocks to be unlocked */
1132 writew(0x0, host.regs + NFC_UNLOCKSTART_BLKADDR);
1133 writew(0x4000, host.regs + NFC_UNLOCKEND_BLKADDR);
1135 /* Unlock Block Command for given address range */
1136 writew(0x4, host.regs + NFC_WRPROT);
1138 tmp = readw(host.regs + NFC_CONFIG1);
1139 tmp |= NFC_ECC_EN;
1140 writew(tmp, host.regs + NFC_CONFIG1);
1142 block = page = 0;
1144 while (1) {
1145 if (!block_is_bad(&host, block * blocksize)) {
1146 page = 0;
1147 while (page * pagesize < blocksize) {
1148 debug("page: %d block: %d dest: %p src "
1149 "0x%08x\n",
1150 page, block, dest,
1151 block * blocksize +
1152 page * pagesize);
1154 send_cmd(&host, NAND_CMD_READ0);
1155 nfc_addr(&host, block * blocksize +
1156 page * pagesize);
1157 send_read_page(&host, 0, 0);
1158 page++;
1159 memcpy32(dest, host.regs, 512);
1160 dest += pagesize;
1161 size -= pagesize;
1162 if (size <= 0)
1163 return;
1165 } else
1166 debug("skip bad block at 0x%08x\n", block * blocksize);
1167 block++;
1170 #define CONFIG_NAND_IMX_BOOT_DEBUG
1171 #ifdef CONFIG_NAND_IMX_BOOT_DEBUG
1172 #include <command.h>
1174 static int do_nand_boot_test(cmd_tbl_t *cmdtp, int argc, char *argv[])
1176 void *dest;
1177 int size, pagesize, blocksize;
1179 if (argc < 4)
1180 return COMMAND_ERROR_USAGE;
1182 dest = (void *)strtoul_suffix(argv[1], NULL, 0);
1183 size = strtoul_suffix(argv[2], NULL, 0);
1184 pagesize = strtoul_suffix(argv[3], NULL, 0);
1185 blocksize = strtoul_suffix(argv[4], NULL, 0);
1187 imx_nand_load_image(dest, size, pagesize, blocksize);
1189 return 0;
1192 static const __maybe_unused char cmd_nand_boot_test_help[] =
1193 "Usage: nand_boot_test <dest> <size> <pagesize> <blocksize>\n";
1195 U_BOOT_CMD_START(nand_boot_test)
1196 .cmd = do_nand_boot_test,
1197 .usage = "list a file or directory",
1198 U_BOOT_CMD_HELP(cmd_nand_boot_test_help)
1199 U_BOOT_CMD_END
1200 #endif
1202 #endif /* CONFIG_NAND_IMX_BOOT */
1205 * Main initialization routine
1206 * @return 0 if successful; non-zero otherwise
1208 static int __init imx_nand_init(void)
1210 return register_driver(&imx_nand_driver);
1213 device_initcall(imx_nand_init);
1215 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1216 MODULE_DESCRIPTION("MXC NAND MTD driver");
1217 MODULE_LICENSE("GPL");