2 * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
3 * Copyright 2008 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
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.
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>
32 #define NFMS (*((volatile u32 *)(IMX_SYSTEM_CTL_BASE + 0x14)))
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
78 * Set INT to 0, FADD to 1, rest to 0 in NFC_CONFIG2 Register for Address
84 * Set INT to 0, FDI to 1, rest to 0 in NFC_CONFIG2 Register for Input
90 * Set INT to 0, FDO to 001, rest to 0 in NFC_CONFIG2 Register for Data Output
93 #define NFC_OUTPUT 0x8
96 * Set INT to 0, FD0 to 010, rest to 0 in NFC_CONFIG2 Register for Read ID
102 * Set INT to 0, FDO to 100, rest to 0 in NFC_CONFIG2 Register for Read Status
105 #define NFC_STATUS 0x20
108 * Set INT to 1, rest to 0 in NFC_CONFIG2 Register for Read Status
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
124 #define __nand_boot_init
127 struct imx_nand_host
{
129 struct nand_chip nand
;
130 struct mtd_partition
*parts
;
131 struct device_d
*dev
;
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
= {
157 .eccpos
= {6, 7, 8, 9, 10},
158 .oobfree
= {{0, 5}, {11, 5}}
161 static struct nand_ecclayout nand_hw_eccoob_16
= {
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
)
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
);
187 writew(tmp
, host
->regs
+ NFC_CONFIG2
);
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
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
,
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
);
249 config1
|= NFC_SP_EN
;
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
);
280 config1
|= NFC_SP_EN
;
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
296 static void send_read_id(struct imx_nand_host
*host
)
298 struct nand_chip
*this = &host
->nand
;
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
);
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
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
;
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
);
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 */
363 *((u32
*) mainbuf
) = store
;
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.
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");
414 static int imx_nand_calculate_ecc(struct mtd_info
*mtd
, const u_char
* dat
,
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
;
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
];
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;
456 /* Update saved column address */
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
;
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);
489 p
= (host
->regs
+ SPARE_AREA0
) + ((col
- mtd
->writesize
) >> 1);
493 ret
= (rdword
>> 8) & 0xff;
495 ret
|= (rdword
<< 8) & 0xff00;
499 /* Update saved column address */
500 host
->col_addr
= col
+ 2;
505 static void __nand_boot_init
memcpy32(void *trg
, const void *src
, int size
)
508 unsigned int *t
= trg
;
509 unsigned const int *s
= src
;
511 for (i
= 0; i
< (size
>> 2); i
++)
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
;
534 MTD_DEBUG(MTD_DEBUG_LEVEL3
,
535 "imx_nand_write_buf(col = %d, len = %d)\n", host
->col_addr
,
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
;
547 MTD_DEBUG(MTD_DEBUG_LEVEL3
,
548 "%s:%d: col = %d, n = %d\n", __FUNCTION__
, __LINE__
, col
, n
);
552 if (col
< mtd
->writesize
)
553 p
= (volatile u32
*)((ulong
) (host
->regs
+ MAIN_AREA0
)
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__
,
562 if (((col
| (int)&buf
[i
]) & 3) || n
< 16) {
565 if (col
& 3 || n
< 4)
571 data
= (data
& 0xffffff00) |
578 data
= (data
& 0xffff00ff) |
585 data
= (data
& 0xff00ffff) |
592 data
= (data
& 0x00ffffff) |
601 int m
= mtd
->writesize
- col
;
603 if (col
>= mtd
->writesize
)
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
);
615 memcpy32((void *)(p
), &buf
[i
], 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
;
645 MTD_DEBUG(MTD_DEBUG_LEVEL3
,
646 "imx_nand_read_buf(col = %d, len = %d)\n", host
->col_addr
,
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
;
660 if (col
< mtd
->writesize
)
661 p
= (volatile u32
*)((ulong
) (host
->regs
+ MAIN_AREA0
)
664 p
= (volatile u32
*)((ulong
) (host
->regs
+ SPARE_AREA0
)
665 - mtd
->writesize
+ (col
& ~3));
666 if (((col
| (int)&buf
[i
]) & 3) || n
< 16) {
672 buf
[i
++] = (u8
) (data
);
678 buf
[i
++] = (u8
) (data
>> 8);
684 buf
[i
++] = (u8
) (data
>> 16);
690 buf
[i
++] = (u8
) (data
>> 24);
696 int m
= mtd
->writesize
- col
;
697 if (col
>= mtd
->writesize
)
701 #ifdef CONFIG_ARM_OPTIMZED_STRING_FUNCTIONS
702 memcpy(&buf
[i
], (void *)p
, m
);
704 memcpy32(&buf
[i
], (void *)(p
), 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
728 imx_nand_verify_buf(struct mtd_info
*mtd
, const u_char
* buf
, int len
)
734 * This function is used by upper layer for select and deselect of the NAND
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
746 MTD_DEBUG(MTD_DEBUG_LEVEL0
,
747 "ERROR: Illegal chip select (chip = %d)\n", chip
);
752 tmp
= readw(host
->regs
+ NFC_CONFIG1
);
754 writew(tmp
, host
->regs
+ NFC_CONFIG1
);
758 tmp
= readw(host
->regs
+ NFC_CONFIG1
);
760 writew(tmp
, host
->regs
+ NFC_CONFIG1
);
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
793 case NAND_CMD_STATUS
:
795 host
->status_request
= 1;
799 host
->col_addr
= column
;
800 host
->spare_only
= 0;
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
;
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,
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
);
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
);
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
);
851 case NAND_CMD_ERASE1
:
856 * Write out the command to the device.
858 send_cmd(host
, command
);
861 * Write out column address, if necessary
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
872 if (host
->pagesize_2k
)
873 /* another col addr cycle for 2k page */
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);
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);
895 /* paddr_8 - paddr_15 */
896 send_addr(host
, (page_addr
>> 8) & 0xff);
901 * Command post-processing step
908 case NAND_CMD_READOOB
:
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
);
919 send_read_page(host
, 0, host
->spare_only
);
923 case NAND_CMD_READID
:
928 case NAND_CMD_PAGEPROG
:
931 case NAND_CMD_STATUS
:
934 case NAND_CMD_ERASE2
:
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
;
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
) {
956 "ERASE FAILED(block = %d,status = 0x%x)\n",
957 addr
/ mtd
->erasesize
, status
);
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
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
;
981 #ifdef CONFIG_ARCH_IMX27
982 PCCR1
|= PCCR1_NFC_BAUDEN
;
984 /* Allocate memory for MTD device structure and private data */
985 host
= kzalloc(sizeof(struct imx_nand_host
), GFP_KERNEL
);
990 /* structures must be linked */
995 /* 50 us command delay time */
996 this->chip_delay
= 5;
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
;
1008 host
->clk
= clk_get(&pdev
->dev
, "nfc_clk");
1009 if (IS_ERR(host
->clk
))
1012 clk_enable(host
->clk
);
1015 host
->regs
= (void __iomem
*)dev
->map_base
;
1017 tmp
= readw(host
->regs
+ NFC_CONFIG1
);
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
);
1031 writew(tmp
, host
->regs
+ NFC_CONFIG1
);
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
);
1039 writew(tmp
, host
->regs
+ NFC_CONFIG1
);
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");
1074 add_mtd_device(mtd
);
1076 #ifdef CONFIG_MXC_NAND_LOW_LEVEL_ERASE
1077 /* Erase all the blocks of a NAND */
1092 static struct driver_d imx_nand_driver
= {
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
,
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
);
1140 writew(tmp
, host
.regs
+ NFC_CONFIG1
);
1145 if (!block_is_bad(&host
, block
* blocksize
)) {
1147 while (page
* pagesize
< blocksize
) {
1148 debug("page: %d block: %d dest: %p src "
1154 send_cmd(&host
, NAND_CMD_READ0
);
1155 nfc_addr(&host
, block
* blocksize
+
1157 send_read_page(&host
, 0, 0);
1159 memcpy32(dest
, host
.regs
, 512);
1166 debug("skip bad block at 0x%08x\n", block
* blocksize
);
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
[])
1177 int size
, pagesize
, blocksize
;
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
);
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
)
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");