2 * Copyright 2004-2008 Freescale Semiconductor, Inc. All Rights Reserved.
6 * The code contained herein is licensed under the GNU General Public
7 * License. You may obtain a copy of the GNU General Public License
8 * Version 2 or later at the following locations:
10 * http://www.opensource.org/licenses/gpl-license.html
11 * http://www.gnu.org/copyleft/gpl.html
18 #include <linux/mtd/mtd.h>
19 #include <linux/mtd/nand.h>
20 #include <asm/arch/imx-nand.h>
21 #include <asm/arch/imx-regs.h>
26 * Addresses for NFC registers
28 #define MAIN_AREA0 (host->regs + 0x000)
29 #define MAIN_AREA1 (host->regs + 0x200)
30 #define SPARE_AREA0 (host->regs + 0x1000)
31 #define NFC_BUF_SIZE (host->regs + 0x1E00)
32 #define NFC_BUF_ADDR (host->regs + 0x1E04)
33 #define NFC_FLASH_ADDR (host->regs + 0x1E06)
34 #define NFC_FLASH_CMD (host->regs + 0x1E08)
35 #define NFC_CONFIG (host->regs + 0x1E0A)
36 #define NFC_ECC_STATUS_RESULT (host->regs + 0x1E0C)
37 #define NFC_ECC_STATUS_RESULT_1 (host->regs + 0x1E0C)
38 #define NFC_ECC_STATUS_RESULT_2 (host->regs + 0x1E0E)
39 #define NFC_SPAS (host->regs + 0x1E10)
40 #define NFC_WRPROT (host->regs + 0x1E12)
41 #define NFC_UNLOCKSTART_BLKADDR (host->regs + 0x1E20)
42 #define NFC_UNLOCKEND_BLKADDR (host->regs + 0x1E22)
43 #define NFC_UNLOCKSTART_BLKADDR1 (host->regs + 0x1E24)
44 #define NFC_UNLOCKEND_BLKADDR1 (host->regs + 0x1E26)
45 #define NFC_UNLOCKSTART_BLKADDR2 (host->regs + 0x1E28)
46 #define NFC_UNLOCKEND_BLKADDR2 (host->regs + 0x1E2A)
47 #define NFC_UNLOCKSTART_BLKADDR3 (host->regs + 0x1E2C)
48 #define NFC_UNLOCKEND_BLKADDR3 (host->regs + 0x1E2E)
49 #define NFC_NF_WRPRST (host->regs + 0x1E18)
50 #define NFC_CONFIG1 (host->regs + 0x1E1A)
51 #define NFC_CONFIG2 (host->regs + 0x1E1C)
54 * Set INT to 0, Set 1 to specific operation bit, rest to 0 in LAUNCH_NFC Register for
60 #define NFC_OUTPUT 0x8
62 #define NFC_STATUS 0x20
65 #define NFC_OPS_STAT (1 << 15)
66 #define NFC_SP_EN (1 << 2)
67 #define NFC_ECC_EN (1 << 3)
68 #define NFC_INT_MSK (1 << 4)
69 #define NFC_BIG (1 << 5)
70 #define NFC_RST (1 << 6)
71 #define NFC_CE (1 << 7)
72 #define NFC_ONE_CYCLE (1 << 8)
73 #define NFC_BLS_LOCKED 0
74 #define NFC_BLS_LOCKED_DEFAULT 1
75 #define NFC_BLS_UNLCOKED 2
76 #define NFC_WPC_LOCK_TIGHT 1
77 #define NFC_WPC_LOCK (1 << 1)
78 #define NFC_WPC_UNLOCK (1 << 2)
79 #define NFC_FLASH_ADDR_SHIFT 0
80 #define NFC_UNLOCK_END_ADDR_SHIFT 0
82 #define NFC_ECC_MODE_4 (1<<0)
83 #define NFC_ECC_MODE_8 ~(1<<0)
85 #define NFC_SPAS_64 32
86 #define NFC_SPAS_128 64
87 #define NFC_SPAS_218 109
89 static inline void raw_write(unsigned long val
, void *reg
)
92 debug("wr: 0x%08x = 0x%08x\n", reg
, val
);
95 static inline unsigned long raw_read(void *reg
)
97 unsigned long val
= readw(reg
);
99 debug("rd: 0x%08x = 0x%08x\n", reg
, val
);
104 struct imx_nand_host
{
106 struct nand_chip nand
;
107 struct device_d
*dev
;
115 * Define delays in microsec for NAND device operations
117 #define TROP_US_DELAY 2000
123 * OOB placement block for use with hardware ecc generation
125 static struct nand_ecclayout nand_hw_eccoob_512
= {
127 .eccpos
= {7, 8, 9, 10, 11, 12, 13, 14, 15},
132 static struct nand_ecclayout nand_hw_eccoob_2k
= {
134 .eccpos
= {7, 8, 9, 10, 11, 12, 13, 14, 15},
139 static struct nand_ecclayout nand_hw_eccoob_4k
= {
141 .eccpos
= {7, 8, 9, 10, 11, 12, 13, 14, 15},
147 * @defgroup NAND_MTD NAND Flash MTD Driver for MXC processors
153 * @brief This file contains the hardware specific layer for NAND Flash on
159 static void memcpy32(void *trg
, const void *src
, int size
)
162 unsigned int *t
= trg
;
163 unsigned const int *s
= src
;
165 if ((ulong
)trg
& 0x3 || (ulong
)src
& 0x3 || size
& 0x3)
168 for (i
= 0; i
< (size
>> 2); i
++)
173 * Functions to transfer data to/from spare erea.
176 copy_spare(struct mtd_info
*mtd
, void *pbuf
, void *pspare
, int len
, int bfrom
)
179 u16 m
= mtd
->oobsize
;
180 u16 n
= mtd
->writesize
>> 9;
182 u8
*s
= (u8
*) pspare
;
184 j
= (m
/ n
>> 1) << 1;
187 for (i
= 0; i
< n
- 1; i
++)
188 memcpy32(&d
[i
* j
], &s
[i
* 64], j
);
190 /* the last section */
191 memcpy32(&d
[i
* j
], &s
[i
* 64], len
- i
* j
);
193 for (i
= 0; i
< n
- 1; i
++)
194 memcpy32(&s
[i
* 64], &d
[i
* j
], j
);
196 /* the last section */
197 memcpy32(&s
[i
* 64], &d
[i
* j
], len
- i
* j
);
202 * This function polls the NFC to wait for the basic operation to complete by
203 * checking the INT bit of config2 register.
205 * @param maxRetries number of retry attempts (separated by 1 us)
206 * @param useirq True if IRQ should be used rather than polling
208 static void wait_op_done(struct imx_nand_host
*host
, int maxRetries
)
210 while (maxRetries
> 0) {
212 if (raw_read(NFC_CONFIG2
) & NFC_OPS_STAT
) {
213 raw_write((raw_read(NFC_CONFIG2
) & ~NFC_OPS_STAT
), NFC_CONFIG2
);
220 printf("%s timeout\n", __func__
);
224 * This function issues the specified command to the NAND device and
225 * waits for completion.
227 * @param cmd command for NAND Flash
228 * @param useirq True if IRQ should be used rather than polling
230 static void send_cmd(struct mtd_info
*mtd
, u16 cmd
, int useirq
)
232 struct nand_chip
*nand_chip
= mtd
->priv
;
233 struct imx_nand_host
*host
= nand_chip
->priv
;
236 raw_write(cmd
, NFC_FLASH_CMD
);
238 /* send out command */
239 raw_write(NFC_CMD
, NFC_CONFIG2
);
241 wait_op_done(host
, TROP_US_DELAY
);
245 * This function sends an address (or partial address) to the
246 * NAND device. The address is used to select the source/destination for
249 * @param addr address to be written to NFC.
250 * @param useirq True if IRQ should be used rather than polling
252 static void send_addr(struct imx_nand_host
*host
, u16 addr
, int useirq
)
254 debug("%s: 0x%04x\n", __func__
, addr
);
257 raw_write((addr
<< NFC_FLASH_ADDR_SHIFT
), NFC_FLASH_ADDR
);
259 /* send out address */
260 raw_write(NFC_ADDR
, NFC_CONFIG2
);
262 wait_op_done(host
, TROP_US_DELAY
);
266 * This function requests the NFC to perform a read of the
267 * NAND device status and returns the current status.
269 * @return device status
271 static u16
get_dev_status(struct imx_nand_host
*host
)
273 raw_write(1, NFC_BUF_ADDR
);
275 /* Read status into main buffer */
276 raw_write(NFC_STATUS
, NFC_CONFIG2
);
278 wait_op_done(host
, TROP_US_DELAY
);
280 /* Status is placed in first word of main buffer */
281 /* get status, then recovery area 1 data */
282 return raw_read(MAIN_AREA1
);
285 static void mxc_nand_enable_hwecc(struct mtd_info
*mtd
, int mode
)
287 struct nand_chip
*nand_chip
= mtd
->priv
;
288 struct imx_nand_host
*host
= nand_chip
->priv
;
290 raw_write((raw_read(NFC_CONFIG1
) | NFC_ECC_EN
), NFC_CONFIG1
);
295 * Function to record the ECC corrected/uncorrected errors resulted
296 * after a page read. This NFC detects and corrects upto to 4 symbols
299 static int mxc_check_ecc_status(struct mtd_info
*mtd
)
301 struct nand_chip
*nand_chip
= mtd
->priv
;
302 struct imx_nand_host
*host
= nand_chip
->priv
;
306 u8 ecc_bit_mask
, err_limit
;
308 ecc_bit_mask
= (raw_read(NFC_CONFIG1
) & NFC_ECC_MODE_4
) ? 0x7 : 0xf;
309 err_limit
= raw_read(NFC_CONFIG1
) & NFC_ECC_MODE_4
? 0x4 : 0x8;
311 no_subpages
= mtd
->writesize
>> 9;
313 ecc_stat
= raw_read(NFC_ECC_STATUS_RESULT
);
315 err
= ecc_stat
& ecc_bit_mask
;
316 if (err
> err_limit
) {
317 mtd
->ecc_stats
.failed
++;
318 printk(KERN_WARNING
"UnCorrectable RS-ECC Error\n");
324 } while (--no_subpages
);
326 mtd
->ecc_stats
.corrected
+= ret
;
327 pr_debug("%d Symbol Correctable RS-ECC Error\n", ret
);
333 * Function to correct the detected errors. This NFC corrects all the errors
334 * detected. So this function just return 0.
336 static int mxc_nand_correct_data(struct mtd_info
*mtd
, u_char
* dat
,
337 u_char
* read_ecc
, u_char
* calc_ecc
)
343 * Function to calculate the ECC for the data to be stored in the Nand device.
344 * This NFC has a hardware RS(511,503) ECC engine together with the RS ECC
345 * CONTROL blocks are responsible for detection and correction of up to
346 * 8 symbols of 9 bits each in 528 byte page.
347 * So this function is just return 0.
350 static int mxc_nand_calculate_ecc(struct mtd_info
*mtd
, const u_char
*dat
,
357 * This function id is used to read the data buffer from the NAND Flash. To
358 * read the data from NAND Flash first the data output cycle is initiated by
359 * the NFC, which copies the data to RAMbuffer. This data of length \b len is
360 * then copied to buffer \b buf.
362 * @param mtd MTD structure for the NAND Flash
363 * @param buf data to be read from NAND Flash
364 * @param len number of bytes to be read
366 static void mxc_nand_read_buf(struct mtd_info
*mtd
, u_char
*buf
, int len
)
368 struct nand_chip
*nand_chip
= mtd
->priv
;
369 struct imx_nand_host
*host
= nand_chip
->priv
;
370 u16 col
= host
->col_addr
;
372 debug("%s: 0x%08x %d\n", __func__
, buf
, len
);
374 if (mtd
->writesize
) {
375 int j
= mtd
->writesize
- col
;
376 int n
= mtd
->oobsize
+ j
;
382 memcpy(buf
, &data_buf
[col
], j
);
383 memcpy(buf
+ j
, &oob_buf
[0], n
- j
);
385 memcpy(buf
, &data_buf
[col
], n
);
388 col
-= mtd
->writesize
;
389 memcpy(buf
, &oob_buf
[col
], len
);
396 /* At flash identify phase,
397 * mtd->writesize has not been
398 * set correctly, it should
399 * be zero.And len will less 2
401 memcpy(buf
, &data_buf
[col
], len
);
404 host
->col_addr
+= len
;
409 * This function reads byte from the NAND Flash
411 * @param mtd MTD structure for the NAND Flash
413 * @return data read from the NAND Flash
415 static uint8_t mxc_nand_read_byte(struct mtd_info
*mtd
)
417 struct nand_chip
*nand_chip
= mtd
->priv
;
418 struct imx_nand_host
*host
= nand_chip
->priv
;
421 /* Check for status request */
422 if (host
->status_request
)
423 return get_dev_status(host
) & 0xFF;
425 mxc_nand_read_buf(mtd
, &ret
, 1);
431 * This function reads word from the NAND Flash
433 * @param mtd MTD structure for the NAND Flash
435 * @return data read from the NAND Flash
437 static u16
mxc_nand_read_word(struct mtd_info
*mtd
)
441 mxc_nand_read_buf(mtd
, (uint8_t *)&ret
, sizeof(u16
));
447 * This function reads byte from the NAND Flash
449 * @param mtd MTD structure for the NAND Flash
451 * @return data read from the NAND Flash
453 static u_char
mxc_nand_read_byte16(struct mtd_info
*mtd
)
455 struct nand_chip
*nand_chip
= mtd
->priv
;
456 struct imx_nand_host
*host
= nand_chip
->priv
;
458 /* Check for status request */
459 if (host
->status_request
)
460 return get_dev_status(host
) & 0xFF;
462 return mxc_nand_read_word(mtd
) & 0xFF;
466 * This function writes data of length \b len from buffer \b buf to the NAND
467 * internal RAM buffer's MAIN area 0.
469 * @param mtd MTD structure for the NAND Flash
470 * @param buf data to be written to NAND Flash
471 * @param len number of bytes to be written
473 static void mxc_nand_write_buf(struct mtd_info
*mtd
,
474 const u_char
*buf
, int len
)
476 struct nand_chip
*nand_chip
= mtd
->priv
;
477 struct imx_nand_host
*host
= nand_chip
->priv
;
478 u16 col
= host
->col_addr
;
479 int j
= mtd
->writesize
- col
;
480 int n
= mtd
->oobsize
+ j
;
486 memcpy(&data_buf
[col
], buf
, j
);
487 memcpy(&oob_buf
[0], buf
+ j
, n
- j
);
489 memcpy(&data_buf
[col
], buf
, n
);
492 col
-= mtd
->writesize
;
493 memcpy(&oob_buf
[col
], buf
, len
);
501 * This function is used by the upper layer to verify the data in NAND Flash
502 * with the data in the \b buf.
504 * @param mtd MTD structure for the NAND Flash
505 * @param buf data to be verified
506 * @param len length of the data to be verified
508 * @return -EFAULT if error else 0
511 static int mxc_nand_verify_buf(struct mtd_info
*mtd
, const u_char
*buf
,
514 u_char
*s
= data_buf
;
516 const u_char
*p
= buf
;
518 for (; len
> 0; len
--) {
527 * This function is used by upper layer for select and deselect of the NAND
530 * @param mtd MTD structure for the NAND Flash
531 * @param chip val indicating select or deselect
533 static void mxc_nand_select_chip(struct mtd_info
*mtd
, int chip
)
538 * Function to perform the address cycles.
540 static void mxc_do_addr_cycle(struct mtd_info
*mtd
, int column
, int page_addr
)
542 struct nand_chip
*nand_chip
= mtd
->priv
;
543 struct imx_nand_host
*host
= nand_chip
->priv
;
544 u32 page_mask
= nand_chip
->pagemask
;
546 debug("%s: %d %d\n", __func__
, column
, page_addr
);
549 send_addr(host
, column
& 0xFF, 0);
550 if (mtd
->writesize
== 2048) {
551 /* another col addr cycle for 2k page */
552 send_addr(host
, (column
>> 8) & 0xF, 0);
553 } else if (mtd
->writesize
== 4096) {
554 /* another col addr cycle for 4k page */
555 send_addr(host
, (column
>> 8) & 0x1F, 0);
558 if (page_addr
!= -1) {
560 send_addr(host
, (page_addr
& 0xff), 0);
563 } while (page_mask
!= 0);
568 * This function is used by the upper layer to write command to NAND Flash for
569 * different operations to be carried out on NAND Flash
571 * @param mtd MTD structure for the NAND Flash
572 * @param command command for NAND Flash
573 * @param column column offset for the page read
574 * @param page_addr page to be read from NAND Flash
576 static void mxc_nand_command(struct mtd_info
*mtd
, unsigned command
,
577 int column
, int page_addr
)
579 struct nand_chip
*nand_chip
= mtd
->priv
;
580 struct imx_nand_host
*host
= nand_chip
->priv
;
582 debug("mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
583 command
, column
, page_addr
);
586 * Reset command state information
588 host
->status_request
= 0;
591 * Command pre-processing step
594 case NAND_CMD_STATUS
:
596 host
->status_request
= 1;
600 host
->col_addr
= column
;
603 case NAND_CMD_READOOB
:
604 host
->col_addr
= column
;
605 command
= NAND_CMD_READ0
;
611 /* FIXME: before send SEQIN command for
612 * partial write,We need read one page out.
613 * FSL NFC does not support partial write
614 * It alway send out 512+ecc+512+ecc ...
615 * for large page nand flash. But for small
616 * page nand flash, it did support SPARE
617 * ONLY operation. But to make driver
618 * simple. We take the same as large page,read
619 * whole page out and update. As for MLC nand
620 * NOP(num of operation) = 1. Partial written
621 * on one programed page is not allowed! We
622 * can't limit it on the driver, it need the
623 * upper layer applicaiton take care it
626 mxc_nand_command(mtd
, NAND_CMD_READ0
, 0, page_addr
);
629 host
->col_addr
= column
;
632 case NAND_CMD_PAGEPROG
:
633 /* FIXME:the NFC interal buffer
634 * access has some limitation, it
635 * does not allow byte access. To
636 * make the code simple and ease use
637 * not every time check the address
638 * alignment.Use the temp buffer
639 * to accomadate the data.since We
640 * know data_buf will be at leat 4
641 * byte alignment, so we can use
644 memcpy32(MAIN_AREA0
, data_buf
, mtd
->writesize
);
645 copy_spare(mtd
, oob_buf
, SPARE_AREA0
, mtd
->oobsize
, 0);
647 /* set ram buffer id */
648 raw_write(0, NFC_BUF_ADDR
);
650 /* transfer data from NFC ram to nand */
651 raw_write(NFC_INPUT
, NFC_CONFIG2
);
653 wait_op_done(host
, TROP_US_DELAY
);
657 case NAND_CMD_ERASE1
:
658 case NAND_CMD_ERASE2
:
662 send_cmd(mtd
, command
, 0);
664 mxc_do_addr_cycle(mtd
, column
, page_addr
);
667 * Command post-processing step
670 case NAND_CMD_READOOB
:
672 if (mtd
->writesize
> 512) {
673 /* send read confirm command */
674 send_cmd(mtd
, NAND_CMD_READSTART
, 1);
677 raw_write(0, NFC_BUF_ADDR
);
679 /* transfer data from nand to NFC ram */
680 raw_write(NFC_OUTPUT
, NFC_CONFIG2
);
682 wait_op_done(host
, TROP_US_DELAY
);
684 /* FIXME, the NFC interal buffer
685 * access has some limitation, it
686 * does not allow byte access. To
687 * make the code simple and ease use
688 * not every time check the address
689 * alignment.Use the temp buffer
690 * to accomadate the data.since We
691 * know data_buf will be at leat 4
692 * byte alignment, so we can use
695 memcpy32(data_buf
, MAIN_AREA0
, mtd
->writesize
);
696 copy_spare(mtd
, oob_buf
, SPARE_AREA0
, mtd
->oobsize
, 1);
700 case NAND_CMD_READID
:
701 raw_write(0, NFC_BUF_ADDR
);
703 /* Read ID into main buffer */
704 raw_write(NFC_ID
, NFC_CONFIG2
);
706 wait_op_done(host
, TROP_US_DELAY
);
708 host
->col_addr
= column
;
709 memcpy32(data_buf
, MAIN_AREA0
, 2048);
715 static int mxc_nand_read_oob(struct mtd_info
*mtd
,
716 struct nand_chip
*chip
, int page
, int sndcmd
)
719 chip
->cmdfunc(mtd
, NAND_CMD_READ0
, 0x00, page
);
723 memcpy32(chip
->oob_poi
, oob_buf
, mtd
->oobsize
);
728 static int mxc_nand_read_page(struct mtd_info
*mtd
, struct nand_chip
*chip
,
731 debug("%s: 0x%08x\n", __func__
, buf
);
733 mxc_check_ecc_status(mtd
);
735 memcpy32(buf
, data_buf
, mtd
->writesize
);
736 memcpy32(chip
->oob_poi
, oob_buf
, mtd
->oobsize
);
741 static void mxc_nand_write_page(struct mtd_info
*mtd
, struct nand_chip
*chip
,
744 memcpy32(data_buf
, buf
, mtd
->writesize
);
745 memcpy32(oob_buf
, chip
->oob_poi
, mtd
->oobsize
);
749 * We must provide a private bbt decriptor, because the settings from
750 * the generic one collide with our ECC hardware.
752 static uint8_t bbt_pattern
[] = { 'B', 'b', 't', '0' };
753 static uint8_t mirror_pattern
[] = { '1', 't', 'b', 'B' };
755 static struct nand_bbt_descr bbt_main_descr
= {
756 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
757 | NAND_BBT_2BIT
| NAND_BBT_VERSION
| NAND_BBT_PERCHIP
,
762 .pattern
= bbt_pattern
765 static struct nand_bbt_descr bbt_mirror_descr
= {
766 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
767 | NAND_BBT_2BIT
| NAND_BBT_VERSION
| NAND_BBT_PERCHIP
,
772 .pattern
= mirror_pattern
775 #ifdef CONFIG_ARCH_IMX25
777 #define RCSR_NFC_FMS (1 << 8)
778 #define RCSR_NFC_4K (1 << 9)
779 #define RCSR_NFC_16BIT_SEL (1 << 14)
781 static void __bare_init
mxc_nand_set_writesize(struct mtd_info
*mtd
, int writesize
)
785 rcsr
= readl(IMX_CCM_BASE
+ CCM_RCSR
);
786 rcsr
&= ~(RCSR_NFC_FMS
| RCSR_NFC_4K
);
788 if (writesize
== 2048)
789 rcsr
|= RCSR_NFC_FMS
;
790 if (writesize
== 4096)
791 rcsr
|= RCSR_NFC_FMS
| RCSR_NFC_4K
;
793 writel(rcsr
, IMX_CCM_BASE
+ CCM_RCSR
);
796 static void __bare_init
mxc_nand_set_datawidth(struct mtd_info
*mtd
, int datawidth
)
800 rcsr
= readl(IMX_CCM_BASE
+ CCM_RCSR
);
803 rcsr
|= RCSR_NFC_16BIT_SEL
;
805 rcsr
&= ~RCSR_NFC_16BIT_SEL
;
808 writel(rcsr
, IMX_CCM_BASE
+ CCM_RCSR
);
812 static void mxc_nfc_init(struct imx_nand_host
*host
)
814 /* Disable interrupt */
815 raw_write((raw_read(NFC_CONFIG1
) | NFC_INT_MSK
), NFC_CONFIG1
);
817 /* disable spare enable */
818 raw_write(raw_read(NFC_CONFIG1
) & ~NFC_SP_EN
, NFC_CONFIG1
);
820 /* Unlock the internal RAM Buffer */
821 raw_write(NFC_BLS_UNLCOKED
, NFC_CONFIG
);
823 /* Blocks to be unlocked */
824 raw_write(0x0, NFC_UNLOCKSTART_BLKADDR
);
825 raw_write(0xffff, NFC_UNLOCKEND_BLKADDR
);
827 /* Unlock Block Command for given address range */
828 raw_write(NFC_WPC_UNLOCK
, NFC_WRPROT
);
831 static int __init
imxnd_probe(struct device_d
*dev
)
833 struct nand_chip
*this;
834 struct mtd_info
*mtd
;
835 struct imx_nand_platform_data
*pdata
= dev
->platform_data
;
836 struct imx_nand_host
*host
;
841 data_buf
= xzalloc(NAND_MAX_PAGESIZE
);
842 oob_buf
= xzalloc(NAND_MAX_OOBSIZE
);
844 /* Allocate memory for MTD device structure and private data */
845 host
= kzalloc(sizeof(struct imx_nand_host
), GFP_KERNEL
);
847 printk(KERN_ERR
"%s: failed to allocate mtd_info\n",
854 /* structures must be linked */
859 /* NAND bus width determines access funtions used by upper layer */
860 if (pdata
->width
== 2) {
861 this->read_byte
= mxc_nand_read_byte16
;
862 this->options
|= NAND_BUSWIDTH_16
;
863 mxc_nand_set_datawidth(mtd
, 16);
865 mxc_nand_set_datawidth(mtd
, 8);
867 /* 50 us command delay time */
868 this->chip_delay
= 5;
871 this->cmdfunc
= mxc_nand_command
;
872 this->select_chip
= mxc_nand_select_chip
;
873 this->read_byte
= mxc_nand_read_byte
;
874 this->read_word
= mxc_nand_read_word
;
875 this->write_buf
= mxc_nand_write_buf
;
876 this->read_buf
= mxc_nand_read_buf
;
877 this->verify_buf
= mxc_nand_verify_buf
;
879 /* use flash based bbt */
880 this->bbt_td
= &bbt_main_descr
;
881 this->bbt_md
= &bbt_mirror_descr
;
883 /* update flash based bbt */
884 this->options
|= NAND_USE_FLASH_BBT
;
886 host
->regs
= (void __iomem
*)dev
->map_base
;
890 tmp
= readw(NFC_CONFIG1
);
892 writew(tmp
, NFC_CONFIG1
);
895 this->ecc
.read_page
= mxc_nand_read_page
;
896 this->ecc
.write_page
= mxc_nand_write_page
;
897 this->ecc
.read_oob
= mxc_nand_read_oob
;
898 this->ecc
.layout
= &nand_hw_eccoob_512
;
899 this->ecc
.calculate
= mxc_nand_calculate_ecc
;
900 this->ecc
.hwctl
= mxc_nand_enable_hwecc
;
901 this->ecc
.correct
= mxc_nand_correct_data
;
902 this->ecc
.mode
= NAND_ECC_HW
;
903 this->ecc
.size
= 512;
905 raw_write(raw_read(NFC_CONFIG1
) | NFC_ECC_EN
, NFC_CONFIG1
);
907 this->ecc
.mode
= NAND_ECC_SOFT
;
908 raw_write(raw_read(NFC_CONFIG1
) & ~NFC_ECC_EN
, NFC_CONFIG1
);
912 this->cmdfunc(mtd
, NAND_CMD_RESET
, -1, -1);
914 /* Scan to find existence of the device */
915 if (nand_scan_ident(mtd
, 1)) {
917 dev_err(dev
, "Unable to find any NAND device\n");
921 if (mtd
->writesize
== 2048) {
922 mxc_nand_set_writesize(mtd
, 2048);
923 raw_write(((raw_read(NFC_SPAS
) & 0xff00) | NFC_SPAS_64
), NFC_SPAS
);
924 raw_write((raw_read(NFC_CONFIG1
) | NFC_ECC_MODE_4
), NFC_CONFIG1
);
925 this->ecc
.layout
= &nand_hw_eccoob_2k
;
926 } else if (mtd
->writesize
== 4096) {
927 mxc_nand_set_writesize(mtd
, 4096);
928 raw_write(((raw_read(NFC_SPAS
) & 0xff00) | NFC_SPAS_128
), NFC_SPAS
);
929 raw_write((raw_read(NFC_CONFIG1
) | NFC_ECC_MODE_4
), NFC_CONFIG1
);
930 this->ecc
.layout
= &nand_hw_eccoob_4k
;
932 mxc_nand_set_writesize(mtd
, 512);
933 this->ecc
.layout
= &nand_hw_eccoob_512
;
936 if (nand_scan_tail(mtd
)) {
937 dev_err(dev
, "Unable to find any NAND device.\n");
944 #ifdef CONFIG_MXC_NAND_LOW_LEVEL_ERASE
945 /* Erase all the blocks of a NAND */
956 static struct driver_d imx_nand_driver
= {
958 .probe
= imxnd_probe
,
962 * Main initialization routine
963 * @return 0 if successful; non-zero otherwise
965 static int __init
imx_nand_init(void)
967 return register_driver(&imx_nand_driver
);
970 device_initcall(imx_nand_init
);
972 #ifdef CONFIG_NAND_IMX_BOOT
974 static void __bare_init noinline
boot_send_cmd(struct imx_nand_host
*host
, u16 cmd
)
977 raw_write(cmd
, NFC_FLASH_CMD
);
979 /* send out command */
980 raw_write(NFC_CMD
, NFC_CONFIG2
);
982 wait_op_done(host
, TROP_US_DELAY
);
985 static void noinline __bare_init
boot_send_addr(struct imx_nand_host
*host
, u16 addr
)
988 raw_write((addr
<< NFC_FLASH_ADDR_SHIFT
), NFC_FLASH_ADDR
);
990 /* send out address */
991 raw_write(NFC_ADDR
, NFC_CONFIG2
);
993 wait_op_done(host
, TROP_US_DELAY
);
996 static void __bare_init
boot_nfc_addr(struct imx_nand_host
*host
, u32 offs
, int pagesize
)
1000 boot_send_addr(host
, offs
& 0xff);
1001 boot_send_addr(host
, (offs
>> 9) & 0xff);
1002 boot_send_addr(host
, (offs
>> 17) & 0xff);
1003 boot_send_addr(host
, (offs
>> 25) & 0xff);
1006 boot_send_addr(host
, 0);
1007 boot_send_addr(host
, 0);
1008 boot_send_addr(host
, (offs
>> 11) & 0xff);
1009 boot_send_addr(host
, (offs
>> 19) & 0xff);
1010 boot_send_addr(host
, (offs
>> 27) & 0xff);
1013 boot_send_addr(host
, 0);
1014 boot_send_addr(host
, 0);
1015 boot_send_addr(host
, (offs
>> 12) & 0xff);
1016 boot_send_addr(host
, (offs
>> 20) & 0xff);
1017 boot_send_addr(host
, (offs
>> 27) & 0xff);
1022 boot_send_cmd(host
, NAND_CMD_READSTART
);
1025 static void __bare_init noinline
boot_send_read_page(struct imx_nand_host
*host
, u8 buf_id
)
1027 DEBUG(MTD_DEBUG_LEVEL3
, "%s(%d)\n", __FUNCTION__
, buf_id
);
1029 raw_write(buf_id
, NFC_BUF_ADDR
);
1031 /* transfer data from nand to NFC ram */
1032 raw_write(NFC_OUTPUT
, NFC_CONFIG2
);
1034 wait_op_done(host
, TROP_US_DELAY
);
1037 static int __bare_init
block_is_bad(struct imx_nand_host
*host
, u32 offs
, int pagesize
)
1039 boot_send_cmd(host
, NAND_CMD_READ0
);
1040 boot_nfc_addr(host
, offs
, pagesize
);
1041 boot_send_read_page(host
, 0);
1043 /* FIXME: copied from V1 driver. Is this correct? */
1044 return (raw_read(SPARE_AREA0
) & 0xff) == 0xff ? 0 : 1;
1047 void __bare_init
imx_nand_load_image(void *dest
, int size
, int pagesize
,
1050 struct imx_nand_host _host
;
1051 struct imx_nand_host
*host
= &_host
;
1055 host
->regs
= (void *)IMX_NAND_BASE
;
1057 debug("%s\n", __func__
);
1059 /* disable spare enable */
1060 raw_write(raw_read(NFC_CONFIG1
) & ~NFC_SP_EN
, NFC_CONFIG1
);
1062 /* Unlock the internal RAM Buffer */
1063 raw_write(NFC_BLS_UNLCOKED
, NFC_CONFIG
);
1065 /* Blocks to be unlocked */
1066 raw_write(0x0, NFC_UNLOCKSTART_BLKADDR
);
1067 raw_write(0xffff, NFC_UNLOCKEND_BLKADDR
);
1069 /* Unlock Block Command for given address range */
1070 raw_write(NFC_WPC_UNLOCK
, NFC_WRPROT
);
1072 if (readl(IMX_CCM_BASE
+ CCM_RCSR
) & (1 << 14))
1078 if (!block_is_bad(host
, block
* blocksize
, pagesize
)) {
1080 while (page
* pagesize
< blocksize
) {
1081 debug("page: %d block: %d dest: %p src "
1087 boot_send_cmd(host
, NAND_CMD_READ0
);
1088 boot_nfc_addr(host
, block
* blocksize
+
1089 page
* pagesize
, pagesize
);
1090 boot_send_read_page(host
, 0);
1092 memcpy32(dest
, MAIN_AREA0
, pagesize
);
1103 #ifdef IMX_NAND_BOOT_DEBUG
1104 #include <command.h>
1105 static int do_nand_boot_test(cmd_tbl_t
*cmdtp
, int argc
, char *argv
[])
1108 int size
, pagesize
, blocksize
;
1111 u_boot_cmd_usage(cmdtp
);
1115 dest
= (void *)strtoul_suffix(argv
[1], NULL
, 0);
1116 size
= strtoul_suffix(argv
[2], NULL
, 0);
1117 pagesize
= strtoul_suffix(argv
[3], NULL
, 0);
1118 blocksize
= strtoul_suffix(argv
[4], NULL
, 0);
1120 imx_nand_load_image(dest
, size
, pagesize
, blocksize
* 1024);
1125 static const __maybe_unused
char cmd_nand_boot_test_help
[] =
1126 "Usage: nand_boot_test <dest> <size> <pagesize> <blocksize in kiB>\n";
1128 U_BOOT_CMD_START(nand_boot_test
)
1129 .maxargs
= CONFIG_MAXARGS
,
1130 .cmd
= do_nand_boot_test
,
1131 .usage
= "load an image from NAND",
1132 U_BOOT_CMD_HELP(cmd_nand_boot_test_help
)