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 <mach/generic.h>
28 #include <mach/imx-nand.h>
29 #include <mach/imx-regs.h>
35 #define nfc_is_v21() (cpu_is_mx25() || cpu_is_mx35())
36 #define nfc_is_v1() (cpu_is_mx31() || cpu_is_mx27() || cpu_is_mx21())
39 * Addresses for NFC registers
41 #define NFC_BUF_SIZE 0xE00
42 #define NFC_BUF_ADDR 0xE04
43 #define NFC_FLASH_ADDR 0xE06
44 #define NFC_FLASH_CMD 0xE08
45 #define NFC_CONFIG 0xE0A
46 #define NFC_ECC_STATUS_RESULT 0xE0C
47 #define NFC_RSLTMAIN_AREA 0xE0E
48 #define NFC_RSLTSPARE_AREA 0xE10
49 #define NFC_SPAS 0xe10
50 #define NFC_WRPROT 0xE12
51 #define NFC_V1_UNLOCKSTART_BLKADDR 0xe14
52 #define NFC_V1_UNLOCKEND_BLKADDR 0xe16
53 #define NFC_V21_UNLOCKSTART_BLKADDR 0xe20
54 #define NFC_V21_UNLOCKEND_BLKADDR 0xe22
55 #define NFC_NF_WRPRST 0xE18
56 #define NFC_CONFIG1 0xE1A
57 #define NFC_CONFIG2 0xE1C
60 * Addresses for NFC RAM BUFFER Main area 0
62 #define MAIN_AREA0 0x000
63 #define MAIN_AREA1 0x200
64 #define MAIN_AREA2 0x400
65 #define MAIN_AREA3 0x600
68 * Addresses for NFC SPARE BUFFER Spare area 0
70 #define SPARE_AREA0 0x800
71 #define SPARE_AREA1 0x810
72 #define SPARE_AREA2 0x820
73 #define SPARE_AREA3 0x830
76 * Set INT to 0, FCMD to 1, rest to 0 in NFC_CONFIG2 Register for Command
82 * Set INT to 0, FADD to 1, rest to 0 in NFC_CONFIG2 Register for Address
88 * Set INT to 0, FDI to 1, rest to 0 in NFC_CONFIG2 Register for Input
94 * Set INT to 0, FDO to 001, rest to 0 in NFC_CONFIG2 Register for Data Output
97 #define NFC_OUTPUT 0x8
100 * Set INT to 0, FD0 to 010, rest to 0 in NFC_CONFIG2 Register for Read ID
106 * Set INT to 0, FDO to 100, rest to 0 in NFC_CONFIG2 Register for Read Status
109 #define NFC_STATUS 0x20
112 * Set INT to 1, rest to 0 in NFC_CONFIG2 Register for Read Status
115 #define NFC_INT 0x8000
117 #define NFC_ECC_MODE (1 << 0)
118 #define NFC_SP_EN (1 << 2)
119 #define NFC_ECC_EN (1 << 3)
120 #define NFC_INT_MSK (1 << 4)
121 #define NFC_BIG (1 << 5)
122 #define NFC_RST (1 << 6)
123 #define NFC_CE (1 << 7)
124 #define NFC_ONE_CYCLE (1 << 8)
126 #define NFC_SPAS_16 8
127 #define NFC_SPAS_64 32
128 #define NFC_SPAS_128 64
129 #define NFC_SPAS_218 109
131 #ifdef CONFIG_NAND_IMX_BOOT
132 #define __nand_boot_init __bare_init
134 #define __nand_boot_init
137 struct imx_nand_host
{
139 struct nand_chip nand
;
140 struct mtd_partition
*parts
;
141 struct device_d
*dev
;
154 unsigned int buf_start
;
160 * OOB placement block for use with hardware ecc generation
162 static struct nand_ecclayout nandv1_hw_eccoob_smallpage
= {
164 .eccpos
= {6, 7, 8, 9, 10},
165 .oobfree
= {{0, 5}, {12, 4}}
168 static struct nand_ecclayout nandv1_hw_eccoob_largepage
= {
170 .eccpos
= {6, 7, 8, 9, 10, 22, 23, 24, 25, 26,
171 38, 39, 40, 41, 42, 54, 55, 56, 57, 58},
172 .oobfree
= {{2, 4}, {11, 10}, {27, 10}, {43, 10}, {59, 5}, }
175 /* OOB description for 512 byte pages with 16 byte OOB */
176 static struct nand_ecclayout nandv2_hw_eccoob_smallpage
= {
179 7, 8, 9, 10, 11, 12, 13, 14, 15
182 {.offset
= 0, .length
= 5}
186 /* OOB description for 2048 byte pages with 64 byte OOB */
187 static struct nand_ecclayout nandv2_hw_eccoob_largepage
= {
190 7, 8, 9, 10, 11, 12, 13, 14, 15,
191 23, 24, 25, 26, 27, 28, 29, 30, 31,
192 39, 40, 41, 42, 43, 44, 45, 46, 47,
193 55, 56, 57, 58, 59, 60, 61, 62, 63
196 {.offset
= 2, .length
= 4},
197 {.offset
= 16, .length
= 7},
198 {.offset
= 32, .length
= 7},
199 {.offset
= 48, .length
= 7}
203 static void memcpy32(void *trg
, const void *src
, int size
)
206 unsigned int *t
= trg
;
207 unsigned const int *s
= src
;
209 #ifdef CONFIG_ARM_OPTIMZED_STRING_FUNCTIONS
210 if (!((unsigned long)trg
& 0x3) && !((unsigned long)src
& 0x3))
211 memcpy(trg
, src
, size
);
214 for (i
= 0; i
< (size
>> 2); i
++)
219 * This function polls the NANDFC to wait for the basic operation to complete by
220 * checking the INT bit of config2 register.
222 * @param max_retries number of retry attempts (separated by 1 us)
223 * @param param parameter for debug
225 static void __nand_boot_init
wait_op_done(struct imx_nand_host
*host
)
230 /* This is a timeout of roughly 15ms on my system. We
231 * need about 2us, but be generous. Don't use udelay
232 * here as we might be here from nand booting.
234 for (i
= 0; i
< 100000; i
++) {
235 if (readw(host
->regs
+ NFC_CONFIG2
) & NFC_INT
) {
236 tmp
= readw(host
->regs
+ NFC_CONFIG2
);
238 writew(tmp
, host
->regs
+ NFC_CONFIG2
);
245 * This function issues the specified command to the NAND device and
246 * waits for completion.
248 * @param cmd command for NAND Flash
250 static void __nand_boot_init
send_cmd(struct imx_nand_host
*host
, u16 cmd
)
252 MTD_DEBUG(MTD_DEBUG_LEVEL3
, "send_cmd(host, 0x%x)\n", cmd
);
254 writew(cmd
, host
->regs
+ NFC_FLASH_CMD
);
255 writew(NFC_CMD
, host
->regs
+ NFC_CONFIG2
);
257 if (cpu_is_mx21() && (cmd
== NAND_CMD_RESET
)) {
258 /* Reset completion is indicated by NFC_CONFIG2 */
261 for (i
= 0; i
< 100000; i
++) {
262 if (readw(host
->regs
+ NFC_CONFIG2
) == 0) {
267 /* Wait for operation to complete */
272 * This function sends an address (or partial address) to the
273 * NAND device. The address is used to select the source/destination for
276 * @param addr address to be written to NFC.
277 * @param islast True if this is the last address cycle for command
279 static void __nand_boot_init noinline
send_addr(struct imx_nand_host
*host
, u16 addr
)
281 MTD_DEBUG(MTD_DEBUG_LEVEL3
, "send_addr(host, 0x%x %d)\n", addr
, islast
);
283 writew(addr
, host
->regs
+ NFC_FLASH_ADDR
);
284 writew(NFC_ADDR
, host
->regs
+ NFC_CONFIG2
);
286 /* Wait for operation to complete */
291 * This function requests the NANDFC to initate the transfer
292 * of data currently in the NANDFC RAM buffer to the NAND device.
294 * @param buf_id Specify Internal RAM Buffer number (0-3)
295 * @param spare_only set true if only the spare area is transferred
297 static void __nand_boot_init
send_page(struct imx_nand_host
*host
,
302 if (nfc_is_v1() && host
->pagesize_2k
)
307 for (i
= 0; i
< bufs
; i
++) {
308 /* NANDFC buffer 0 is used for page read/write */
309 writew(i
, host
->regs
+ NFC_BUF_ADDR
);
311 writew(ops
, host
->regs
+ NFC_CONFIG2
);
313 /* Wait for operation to complete */
319 * This function requests the NANDFC to perform a read of the
322 static void send_read_id(struct imx_nand_host
*host
)
324 struct nand_chip
*this = &host
->nand
;
327 /* NANDFC buffer 0 is used for device ID output */
328 writew(0x0, host
->regs
+ NFC_BUF_ADDR
);
330 /* Read ID into main buffer */
331 tmp
= readw(host
->regs
+ NFC_CONFIG1
);
333 writew(tmp
, host
->regs
+ NFC_CONFIG1
);
335 writew(NFC_ID
, host
->regs
+ NFC_CONFIG2
);
337 /* Wait for operation to complete */
340 if (this->options
& NAND_BUSWIDTH_16
) {
341 volatile u16
*mainbuf
= host
->main_area0
;
344 * Pack the every-other-byte result for 16-bit ID reads
345 * into every-byte as the generic code expects and various
349 mainbuf
[0] = (mainbuf
[0] & 0xff) | ((mainbuf
[1] & 0xff) << 8);
350 mainbuf
[1] = (mainbuf
[2] & 0xff) | ((mainbuf
[3] & 0xff) << 8);
351 mainbuf
[2] = (mainbuf
[4] & 0xff) | ((mainbuf
[5] & 0xff) << 8);
353 memcpy32(host
->data_buf
, host
->main_area0
, 16);
357 * This function requests the NANDFC to perform a read of the
358 * NAND device status and returns the current status.
360 * @return device status
362 static u16
get_dev_status(struct imx_nand_host
*host
)
364 volatile u16
*mainbuf
= host
->main_area1
;
367 /* Issue status request to NAND device */
369 /* store the main area1 first word, later do recovery */
370 store
= *((u32
*) mainbuf
);
372 * NANDFC buffer 1 is used for device status to prevent
373 * corruption of read/write buffer on status requests.
375 writew(1, host
->regs
+ NFC_BUF_ADDR
);
377 /* Read status into main buffer */
378 tmp
= readw(host
->regs
+ NFC_CONFIG1
);
380 writew(tmp
, host
->regs
+ NFC_CONFIG1
);
382 writew(NFC_STATUS
, host
->regs
+ NFC_CONFIG2
);
384 /* Wait for operation to complete */
387 /* Status is placed in first word of main buffer */
388 /* get status, then recovery area 1 data */
390 *((u32
*) mainbuf
) = store
;
396 * This function is used by upper layer to checks if device is ready
398 * @param mtd MTD structure for the NAND Flash
400 * @return 0 if device is busy else 1
402 static int imx_nand_dev_ready(struct mtd_info
*mtd
)
405 * NFC handles R/B internally.Therefore,this function
406 * always returns status as ready.
411 static void imx_nand_enable_hwecc(struct mtd_info
*mtd
, int mode
)
414 * If HW ECC is enabled, we turn it on during init. There is
415 * no need to enable again here.
419 static int imx_nand_correct_data(struct mtd_info
*mtd
, u_char
* dat
,
420 u_char
* read_ecc
, u_char
* calc_ecc
)
422 struct nand_chip
*nand_chip
= mtd
->priv
;
423 struct imx_nand_host
*host
= nand_chip
->priv
;
426 * 1-Bit errors are automatically corrected in HW. No need for
427 * additional correction. 2-Bit errors cannot be corrected by
428 * HW ECC, so we need to return failure
430 u16 ecc_status
= readw(host
->regs
+ NFC_ECC_STATUS_RESULT
);
432 if (((ecc_status
& 0x3) == 2) || ((ecc_status
>> 2) == 2)) {
433 MTD_DEBUG(MTD_DEBUG_LEVEL0
,
434 "MXC_NAND: HWECC uncorrectable 2-bit ECC error\n");
441 static int imx_nand_calculate_ecc(struct mtd_info
*mtd
, const u_char
* dat
,
448 * This function reads byte from the NAND Flash
450 * @param mtd MTD structure for the NAND Flash
452 * @return data read from the NAND Flash
454 static u_char
imx_nand_read_byte(struct mtd_info
*mtd
)
456 struct nand_chip
*nand_chip
= mtd
->priv
;
457 struct imx_nand_host
*host
= nand_chip
->priv
;
460 /* Check for status request */
461 if (host
->status_request
)
462 return get_dev_status(host
) & 0xFF;
464 ret
= *(uint8_t *)(host
->data_buf
+ host
->buf_start
);
471 * This function reads word from the NAND Flash
473 * @param mtd MTD structure for the NAND Flash
475 * @return data read from the NAND Flash
477 static u16
imx_nand_read_word(struct mtd_info
*mtd
)
479 struct nand_chip
*nand_chip
= mtd
->priv
;
480 struct imx_nand_host
*host
= nand_chip
->priv
;
483 ret
= *(uint16_t *)(host
->data_buf
+ host
->buf_start
);
484 host
->buf_start
+= 2;
490 * This function writes data of length \b len to buffer \b buf. The data to be
491 * written on NAND Flash is first copied to RAMbuffer. After the Data Input
492 * Operation by the NFC, the data is written to NAND Flash
494 * @param mtd MTD structure for the NAND Flash
495 * @param buf data to be written to NAND Flash
496 * @param len number of bytes to be written
498 static void imx_nand_write_buf(struct mtd_info
*mtd
,
499 const u_char
*buf
, int len
)
501 struct nand_chip
*nand_chip
= mtd
->priv
;
502 struct imx_nand_host
*host
= nand_chip
->priv
;
503 u16 col
= host
->buf_start
;
504 int n
= mtd
->oobsize
+ mtd
->writesize
- col
;
507 memcpy32(host
->data_buf
+ col
, buf
, n
);
509 host
->buf_start
+= n
;
513 * This function is used to read the data buffer from the NAND Flash. To
514 * read the data from NAND Flash first the data output cycle is initiated by
515 * the NFC, which copies the data to RAMbuffer. This data of length \b len is
516 * then copied to buffer \b buf.
518 * @param mtd MTD structure for the NAND Flash
519 * @param buf data to be read from NAND Flash
520 * @param len number of bytes to be read
522 static void imx_nand_read_buf(struct mtd_info
*mtd
, u_char
* buf
, int len
)
524 struct nand_chip
*nand_chip
= mtd
->priv
;
525 struct imx_nand_host
*host
= nand_chip
->priv
;
526 u16 col
= host
->buf_start
;
527 int n
= mtd
->oobsize
+ mtd
->writesize
- col
;
531 memcpy32(buf
, host
->data_buf
+ col
, len
);
533 host
->buf_start
+= len
;
537 * Function to transfer data to/from spare area.
539 static void copy_spare(struct mtd_info
*mtd
, int bfrom
)
541 struct nand_chip
*this = mtd
->priv
;
542 struct imx_nand_host
*host
= this->priv
;
544 u16 n
= mtd
->writesize
>> 9;
545 u8
*d
= host
->data_buf
+ mtd
->writesize
;
546 u8
*s
= host
->spare0
;
547 u16 t
= host
->spare_len
;
549 j
= (mtd
->oobsize
/ n
>> 1) << 1;
552 for (i
= 0; i
< n
- 1; i
++)
553 memcpy32(d
+ i
* j
, s
+ i
* t
, j
);
555 /* the last section */
556 memcpy32(d
+ i
* j
, s
+ i
* t
, mtd
->oobsize
- i
* j
);
558 for (i
= 0; i
< n
- 1; i
++)
559 memcpy32(&s
[i
* t
], &d
[i
* j
], j
);
561 /* the last section */
562 memcpy32(&s
[i
* t
], &d
[i
* j
], mtd
->oobsize
- i
* j
);
567 * This function is used by the upper layer to verify the data in NAND Flash
568 * with the data in the \b buf.
570 * @param mtd MTD structure for the NAND Flash
571 * @param buf data to be verified
572 * @param len length of the data to be verified
574 * @return -EFAULT if error else 0
578 imx_nand_verify_buf(struct mtd_info
*mtd
, const u_char
* buf
, int len
)
584 * This function is used by upper layer for select and deselect of the NAND
587 * @param mtd MTD structure for the NAND Flash
588 * @param chip val indicating select or deselect
590 static void imx_nand_select_chip(struct mtd_info
*mtd
, int chip
)
592 #ifdef CONFIG_MTD_NAND_MXC_FORCE_CE
596 MTD_DEBUG(MTD_DEBUG_LEVEL0
,
597 "ERROR: Illegal chip select (chip = %d)\n", chip
);
602 tmp
= readw(host
->regs
+ NFC_CONFIG1
);
604 writew(tmp
, host
->regs
+ NFC_CONFIG1
);
608 tmp
= readw(host
->regs
+ NFC_CONFIG1
);
610 writew(tmp
, host
->regs
+ NFC_CONFIG1
);
614 static void mxc_do_addr_cycle(struct mtd_info
*mtd
, int column
, int page_addr
)
616 struct nand_chip
*nand_chip
= mtd
->priv
;
617 struct imx_nand_host
*host
= nand_chip
->priv
;
620 * Write out column address, if necessary
624 * MXC NANDFC can only perform full page+spare or
625 * spare-only read/write. When the upper layers
626 * layers perform a read/write buf operation,
627 * we will used the saved column adress to index into
631 if (host
->pagesize_2k
)
632 /* another col addr cycle for 2k page */
637 * Write out page address, if necessary
639 if (page_addr
!= -1) {
640 send_addr(host
, (page_addr
& 0xff)); /* paddr_0 - p_addr_7 */
642 if (host
->pagesize_2k
) {
643 send_addr(host
, (page_addr
>> 8) & 0xFF);
644 if (mtd
->size
>= 0x10000000) {
645 send_addr(host
, (page_addr
>> 16) & 0xff);
648 /* One more address cycle for higher density devices */
649 if (mtd
->size
>= 0x4000000) {
650 /* paddr_8 - paddr_15 */
651 send_addr(host
, (page_addr
>> 8) & 0xff);
652 send_addr(host
, (page_addr
>> 16) & 0xff);
654 /* paddr_8 - paddr_15 */
655 send_addr(host
, (page_addr
>> 8) & 0xff);
661 * This function is used by the upper layer to write command to NAND Flash for
662 * different operations to be carried out on NAND Flash
664 * @param mtd MTD structure for the NAND Flash
665 * @param command command for NAND Flash
666 * @param column column offset for the page read
667 * @param page_addr page to be read from NAND Flash
669 static void imx_nand_command(struct mtd_info
*mtd
, unsigned command
,
670 int column
, int page_addr
)
672 struct nand_chip
*nand_chip
= mtd
->priv
;
673 struct imx_nand_host
*host
= nand_chip
->priv
;
675 MTD_DEBUG(MTD_DEBUG_LEVEL3
,
676 "imx_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
677 command
, column
, page_addr
);
680 * Reset command state information
682 host
->status_request
= 0;
685 * Command pre-processing step
689 case NAND_CMD_STATUS
:
691 host
->status_request
= 1;
692 send_cmd(host
, command
);
693 mxc_do_addr_cycle(mtd
, column
, page_addr
);
697 case NAND_CMD_READOOB
:
698 if (command
== NAND_CMD_READ0
)
699 host
->buf_start
= column
;
701 host
->buf_start
= column
+ mtd
->writesize
;
703 command
= NAND_CMD_READ0
;
705 send_cmd(host
, command
);
706 mxc_do_addr_cycle(mtd
, column
, page_addr
);
708 if (host
->pagesize_2k
)
709 /* send read confirm command */
710 send_cmd(host
, NAND_CMD_READSTART
);
712 send_page(host
, NFC_OUTPUT
);
714 memcpy32(host
->data_buf
, host
->main_area0
, mtd
->writesize
);
719 if (column
>= mtd
->writesize
) {
720 if (host
->pagesize_2k
) {
722 * FIXME: before send SEQIN command for write
723 * OOB, we must read one page out. For K9F1GXX
724 * has no READ1 command to set current HW
725 * pointer to spare area, we must write the
726 * whole page including OOB together.
728 /* call ourself to read a page */
729 imx_nand_command(mtd
, NAND_CMD_READ0
, 0,
732 host
->buf_start
= column
;
734 /* Set program pointer to spare region */
735 if (!host
->pagesize_2k
)
736 send_cmd(host
, NAND_CMD_READOOB
);
738 host
->buf_start
= column
;
740 /* Set program pointer to page start */
741 if (!host
->pagesize_2k
)
742 send_cmd(host
, NAND_CMD_READ0
);
744 send_cmd(host
, command
);
745 mxc_do_addr_cycle(mtd
, column
, page_addr
);
749 case NAND_CMD_PAGEPROG
:
750 memcpy32(host
->main_area0
, host
->data_buf
, mtd
->writesize
);
752 send_page(host
, NFC_INPUT
);
753 send_cmd(host
, command
);
754 mxc_do_addr_cycle(mtd
, column
, page_addr
);
757 case NAND_CMD_READID
:
758 send_cmd(host
, command
);
759 mxc_do_addr_cycle(mtd
, column
, page_addr
);
764 case NAND_CMD_ERASE1
:
765 case NAND_CMD_ERASE2
:
767 send_cmd(host
, command
);
768 mxc_do_addr_cycle(mtd
, column
, page_addr
);
773 #ifdef CONFIG_MXC_NAND_LOW_LEVEL_ERASE
774 static void imx_low_erase(struct mtd_info
*mtd
)
777 struct nand_chip
*this = mtd
->priv
;
778 unsigned int page_addr
, addr
;
781 MTD_DEBUG(MTD_DEBUG_LEVEL0
, "MXC_ND : imx_low_erase:Erasing NAND\n");
782 for (addr
= 0; addr
< this->chipsize
; addr
+= mtd
->erasesize
) {
783 page_addr
= addr
/ mtd
->writesize
;
784 imx_nand_command(mtd
, NAND_CMD_ERASE1
, -1, page_addr
);
785 imx_nand_command(mtd
, NAND_CMD_ERASE2
, -1, -1);
786 imx_nand_command(mtd
, NAND_CMD_STATUS
, -1, -1);
787 status
= imx_nand_read_byte(mtd
);
788 if (status
& NAND_STATUS_FAIL
) {
790 "ERASE FAILED(block = %d,status = 0x%x)\n",
791 addr
/ mtd
->erasesize
, status
);
799 * The generic flash bbt decriptors overlap with our ecc
800 * hardware, so define some i.MX specific ones.
802 static uint8_t bbt_pattern
[] = { 'B', 'b', 't', '0' };
803 static uint8_t mirror_pattern
[] = { '1', 't', 'b', 'B' };
805 static struct nand_bbt_descr bbt_main_descr
= {
806 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
807 | NAND_BBT_2BIT
| NAND_BBT_VERSION
| NAND_BBT_PERCHIP
,
812 .pattern
= bbt_pattern
,
815 static struct nand_bbt_descr bbt_mirror_descr
= {
816 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
817 | NAND_BBT_2BIT
| NAND_BBT_VERSION
| NAND_BBT_PERCHIP
,
822 .pattern
= mirror_pattern
,
826 * This function is called during the driver binding process.
828 * @param pdev the device structure used to store device specific
829 * information that is used by the suspend, resume and
832 * @return The function always returns 0.
835 static int __init
imxnd_probe(struct device_d
*dev
)
837 struct nand_chip
*this;
838 struct mtd_info
*mtd
;
839 struct imx_nand_platform_data
*pdata
= dev
->platform_data
;
840 struct imx_nand_host
*host
;
841 struct nand_ecclayout
*oob_smallpage
, *oob_largepage
;
845 #ifdef CONFIG_ARCH_IMX27
846 PCCR1
|= PCCR1_NFC_BAUDEN
;
848 #ifdef CONFIG_ARCH_IMX21
849 PCCR0
|= PCCR0_NFC_EN
;
851 /* Allocate memory for MTD device structure and private data */
852 host
= kzalloc(sizeof(struct imx_nand_host
) + NAND_MAX_PAGESIZE
+
853 NAND_MAX_OOBSIZE
, GFP_KERNEL
);
857 host
->data_buf
= (uint8_t *)(host
+ 1);
858 host
->base
= (void __iomem
*)dev
->map_base
;
860 host
->main_area0
= host
->base
;
861 host
->main_area1
= host
->base
+ 0x200;
864 host
->regs
= host
->base
+ 0x1000;
865 host
->spare0
= host
->base
+ 0x1000;
866 host
->spare_len
= 64;
867 oob_smallpage
= &nandv2_hw_eccoob_smallpage
;
868 oob_largepage
= &nandv2_hw_eccoob_largepage
;
869 } else if (nfc_is_v1()) {
870 host
->regs
= host
->base
;
871 host
->spare0
= host
->base
+ 0x800;
872 host
->spare_len
= 16;
873 oob_smallpage
= &nandv1_hw_eccoob_smallpage
;
874 oob_largepage
= &nandv1_hw_eccoob_largepage
;
878 /* structures must be linked */
883 /* 50 us command delay time */
884 this->chip_delay
= 5;
887 this->dev_ready
= imx_nand_dev_ready
;
888 this->cmdfunc
= imx_nand_command
;
889 this->select_chip
= imx_nand_select_chip
;
890 this->read_byte
= imx_nand_read_byte
;
891 this->read_word
= imx_nand_read_word
;
892 this->write_buf
= imx_nand_write_buf
;
893 this->read_buf
= imx_nand_read_buf
;
894 this->verify_buf
= imx_nand_verify_buf
;
896 host
->clk
= clk_get(&pdev
->dev
, "nfc_clk");
897 if (IS_ERR(host
->clk
))
900 clk_enable(host
->clk
);
903 tmp
= readw(host
->regs
+ NFC_CONFIG1
);
907 /* currently no support for 218 byte OOB with stronger ECC */
909 writew(tmp
, host
->regs
+ NFC_CONFIG1
);
912 this->ecc
.calculate
= imx_nand_calculate_ecc
;
913 this->ecc
.hwctl
= imx_nand_enable_hwecc
;
914 this->ecc
.correct
= imx_nand_correct_data
;
915 this->ecc
.mode
= NAND_ECC_HW
;
916 this->ecc
.size
= 512;
917 tmp
= readw(host
->regs
+ NFC_CONFIG1
);
919 writew(tmp
, host
->regs
+ NFC_CONFIG1
);
921 this->ecc
.size
= 512;
922 this->ecc
.mode
= NAND_ECC_SOFT
;
923 tmp
= readw(host
->regs
+ NFC_CONFIG1
);
925 writew(tmp
, host
->regs
+ NFC_CONFIG1
);
929 this->cmdfunc(mtd
, NAND_CMD_RESET
, -1, -1);
931 /* preset operation */
932 /* Unlock the internal RAM Buffer */
933 writew(0x2, host
->regs
+ NFC_CONFIG
);
935 /* Blocks to be unlocked */
937 writew(0x0, host
->regs
+ NFC_V21_UNLOCKSTART_BLKADDR
);
938 writew(0xffff, host
->regs
+ NFC_V21_UNLOCKEND_BLKADDR
);
940 } else if (nfc_is_v1()) {
941 writew(0x0, host
->regs
+ NFC_V1_UNLOCKSTART_BLKADDR
);
942 writew(0x4000, host
->regs
+ NFC_V1_UNLOCKEND_BLKADDR
);
946 /* Unlock Block Command for given address range */
947 writew(0x4, host
->regs
+ NFC_WRPROT
);
949 this->ecc
.layout
= oob_smallpage
;
951 /* NAND bus width determines access funtions used by upper layer */
952 if (pdata
->width
== 2) {
953 this->options
|= NAND_BUSWIDTH_16
;
954 this->ecc
.layout
= &nandv1_hw_eccoob_smallpage
;
955 imx_nand_set_layout(0, 16);
958 if (pdata
->flash_bbt
) {
959 this->bbt_td
= &bbt_main_descr
;
960 this->bbt_md
= &bbt_mirror_descr
;
961 /* update flash based bbt */
962 this->options
|= NAND_USE_FLASH_BBT
;
965 /* first scan to find the device and get the page size */
966 if (nand_scan_ident(mtd
, 1)) {
971 imx_nand_set_layout(mtd
->writesize
, pdata
->width
== 2 ? 16 : 8);
973 if (mtd
->writesize
== 2048) {
974 this->ecc
.layout
= oob_largepage
;
975 host
->pagesize_2k
= 1;
977 tmp
= readw(host
->regs
+ NFC_SPAS
);
980 writew(tmp
, host
->regs
+ NFC_SPAS
);
984 tmp
= readw(host
->regs
+ NFC_SPAS
);
987 writew(tmp
, host
->regs
+ NFC_SPAS
);
991 /* second phase scan */
992 if (nand_scan_tail(mtd
)) {
1010 static struct driver_d imx_nand_driver
= {
1012 .probe
= imxnd_probe
,
1015 #ifdef CONFIG_NAND_IMX_BOOT
1017 static void __nand_boot_init
nfc_addr(struct imx_nand_host
*host
, u32 offs
)
1019 if (host
->pagesize_2k
) {
1020 send_addr(host
, offs
& 0xff);
1021 send_addr(host
, offs
& 0xff);
1022 send_addr(host
, (offs
>> 11) & 0xff);
1023 send_addr(host
, (offs
>> 19) & 0xff);
1024 send_addr(host
, (offs
>> 27) & 0xff);
1026 send_addr(host
, offs
& 0xff);
1027 send_addr(host
, (offs
>> 9) & 0xff);
1028 send_addr(host
, (offs
>> 17) & 0xff);
1029 send_addr(host
, (offs
>> 25) & 0xff);
1033 static void __nand_boot_init
__memcpy32(void *trg
, const void *src
, int size
)
1036 unsigned int *t
= trg
;
1037 unsigned const int *s
= src
;
1039 for (i
= 0; i
< (size
>> 2); i
++)
1043 void __nand_boot_init
imx_nand_load_image(void *dest
, int size
)
1045 struct imx_nand_host host
;
1046 u32 tmp
, page
, block
, blocksize
, pagesize
;
1047 #ifdef CONFIG_ARCH_IMX21
1048 tmp
= readl(IMX_SYSTEM_CTL_BASE
+ 0x14);
1050 host
.pagesize_2k
= 1;
1052 host
.pagesize_2k
= 0;
1054 #ifdef CONFIG_ARCH_IMX27
1055 tmp
= readl(IMX_SYSTEM_CTL_BASE
+ 0x14);
1057 host
.pagesize_2k
= 1;
1059 host
.pagesize_2k
= 0;
1061 #ifdef CONFIG_ARCH_IMX31
1062 tmp
= readl(IMX_CCM_BASE
+ CCM_RCSR
);
1063 if (tmp
& RCSR_NFMS
)
1064 host
.pagesize_2k
= 1;
1066 host
.pagesize_2k
= 0;
1068 #ifdef CONFIG_ARCH_IMX35
1069 if (readl(IMX_CCM_BASE
+ CCM_RCSR
) & (1 << 8))
1070 host
.pagesize_2k
= 1;
1072 host
.pagesize_2k
= 0;
1074 if (host
.pagesize_2k
) {
1076 blocksize
= 128 * 1024;
1079 blocksize
= 16 * 1024;
1082 host
.base
= (void __iomem
*)IMX_NFC_BASE
;
1084 host
.regs
= host
.base
+ 0x1000;
1085 host
.spare0
= host
.base
+ 0x1000;
1086 host
.spare_len
= 64;
1087 } else if (nfc_is_v1()) {
1088 host
.regs
= host
.base
;
1089 host
.spare0
= host
.base
+ 0x800;
1090 host
.spare_len
= 16;
1093 send_cmd(&host
, NAND_CMD_RESET
);
1095 /* preset operation */
1096 /* Unlock the internal RAM Buffer */
1097 writew(0x2, host
.regs
+ NFC_CONFIG
);
1099 /* Unlock Block Command for given address range */
1100 writew(0x4, host
.regs
+ NFC_WRPROT
);
1102 tmp
= readw(host
.regs
+ NFC_CONFIG1
);
1103 tmp
|= NFC_ECC_EN
| NFC_INT_MSK
;
1105 /* currently no support for 218 byte OOB with stronger ECC */
1106 tmp
|= NFC_ECC_MODE
;
1108 writew(tmp
, host
.regs
+ NFC_CONFIG1
);
1111 if (host
.pagesize_2k
) {
1112 tmp
= readw(host
.regs
+ NFC_SPAS
);
1115 writew(tmp
, host
.regs
+ NFC_SPAS
);
1117 tmp
= readw(host
.regs
+ NFC_SPAS
);
1120 writew(tmp
, host
.regs
+ NFC_SPAS
);
1128 while (page
* pagesize
< blocksize
) {
1129 debug("page: %d block: %d dest: %p src "
1135 send_cmd(&host
, NAND_CMD_READ0
);
1136 nfc_addr(&host
, block
* blocksize
+
1138 if (host
.pagesize_2k
)
1139 send_cmd(&host
, NAND_CMD_READSTART
);
1140 send_page(&host
, NFC_OUTPUT
);
1143 if (host
.pagesize_2k
) {
1144 if ((readw(host
.spare0
) & 0xff)
1148 if ((readw(host
.spare0
+ 4) & 0xff00)
1153 __memcpy32(dest
, host
.base
, pagesize
);
1163 #define CONFIG_NAND_IMX_BOOT_DEBUG
1164 #ifdef CONFIG_NAND_IMX_BOOT_DEBUG
1165 #include <command.h>
1167 static int do_nand_boot_test(struct command
*cmdtp
, int argc
, char *argv
[])
1173 return COMMAND_ERROR_USAGE
;
1175 dest
= (void *)strtoul_suffix(argv
[1], NULL
, 0);
1176 size
= strtoul_suffix(argv
[2], NULL
, 0);
1178 imx_nand_load_image(dest
, size
);
1183 static const __maybe_unused
char cmd_nand_boot_test_help
[] =
1184 "Usage: nand_boot_test <dest> <size>\n"
1185 "This command loads the booloader from the NAND memory like the reset\n"
1186 "routine does. Its intended for development tests only";
1188 BAREBOX_CMD_START(nand_boot_test
)
1189 .cmd
= do_nand_boot_test
,
1190 .usage
= "load bootloader from NAND",
1191 BAREBOX_CMD_HELP(cmd_nand_boot_test_help
)
1195 #endif /* CONFIG_NAND_IMX_BOOT */
1198 * Main initialization routine
1199 * @return 0 if successful; non-zero otherwise
1201 static int __init
imx_nand_init(void)
1203 return register_driver(&imx_nand_driver
);
1206 device_initcall(imx_nand_init
);
1208 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1209 MODULE_DESCRIPTION("MXC NAND MTD driver");
1210 MODULE_LICENSE("GPL");