2 * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
3 * Copyright 2008 Sascha Hauer, kernel@pengutronix.de
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 #include <linux/delay.h>
21 #include <linux/slab.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/mtd/mtd.h>
25 #include <linux/mtd/nand.h>
26 #include <linux/mtd/partitions.h>
27 #include <linux/interrupt.h>
28 #include <linux/device.h>
29 #include <linux/platform_device.h>
30 #include <linux/clk.h>
31 #include <linux/err.h>
33 #include <linux/irq.h>
34 #include <linux/completion.h>
36 #include <asm/mach/flash.h>
37 #include <mach/mxc_nand.h>
38 #include <mach/hardware.h>
40 #define DRIVER_NAME "mxc_nand"
42 #define nfc_is_v21() (cpu_is_mx25() || cpu_is_mx35())
43 #define nfc_is_v1() (cpu_is_mx31() || cpu_is_mx27() || cpu_is_mx21())
44 #define nfc_is_v3_2() cpu_is_mx51()
45 #define nfc_is_v3() nfc_is_v3_2()
47 /* Addresses for NFC registers */
48 #define NFC_V1_V2_BUF_SIZE (host->regs + 0x00)
49 #define NFC_V1_V2_BUF_ADDR (host->regs + 0x04)
50 #define NFC_V1_V2_FLASH_ADDR (host->regs + 0x06)
51 #define NFC_V1_V2_FLASH_CMD (host->regs + 0x08)
52 #define NFC_V1_V2_CONFIG (host->regs + 0x0a)
53 #define NFC_V1_V2_ECC_STATUS_RESULT (host->regs + 0x0c)
54 #define NFC_V1_V2_RSLTMAIN_AREA (host->regs + 0x0e)
55 #define NFC_V1_V2_RSLTSPARE_AREA (host->regs + 0x10)
56 #define NFC_V1_V2_WRPROT (host->regs + 0x12)
57 #define NFC_V1_UNLOCKSTART_BLKADDR (host->regs + 0x14)
58 #define NFC_V1_UNLOCKEND_BLKADDR (host->regs + 0x16)
59 #define NFC_V21_UNLOCKSTART_BLKADDR (host->regs + 0x20)
60 #define NFC_V21_UNLOCKEND_BLKADDR (host->regs + 0x22)
61 #define NFC_V1_V2_NF_WRPRST (host->regs + 0x18)
62 #define NFC_V1_V2_CONFIG1 (host->regs + 0x1a)
63 #define NFC_V1_V2_CONFIG2 (host->regs + 0x1c)
65 #define NFC_V2_CONFIG1_ECC_MODE_4 (1 << 0)
66 #define NFC_V1_V2_CONFIG1_SP_EN (1 << 2)
67 #define NFC_V1_V2_CONFIG1_ECC_EN (1 << 3)
68 #define NFC_V1_V2_CONFIG1_INT_MSK (1 << 4)
69 #define NFC_V1_V2_CONFIG1_BIG (1 << 5)
70 #define NFC_V1_V2_CONFIG1_RST (1 << 6)
71 #define NFC_V1_V2_CONFIG1_CE (1 << 7)
72 #define NFC_V2_CONFIG1_ONE_CYCLE (1 << 8)
73 #define NFC_V2_CONFIG1_PPB(x) (((x) & 0x3) << 9)
74 #define NFC_V2_CONFIG1_FP_INT (1 << 11)
76 #define NFC_V1_V2_CONFIG2_INT (1 << 15)
79 * Operation modes for the NFC. Valid for v1, v2 and v3
82 #define NFC_CMD (1 << 0)
83 #define NFC_ADDR (1 << 1)
84 #define NFC_INPUT (1 << 2)
85 #define NFC_OUTPUT (1 << 3)
86 #define NFC_ID (1 << 4)
87 #define NFC_STATUS (1 << 5)
89 #define NFC_V3_FLASH_CMD (host->regs_axi + 0x00)
90 #define NFC_V3_FLASH_ADDR0 (host->regs_axi + 0x04)
92 #define NFC_V3_CONFIG1 (host->regs_axi + 0x34)
93 #define NFC_V3_CONFIG1_SP_EN (1 << 0)
94 #define NFC_V3_CONFIG1_RBA(x) (((x) & 0x7 ) << 4)
96 #define NFC_V3_ECC_STATUS_RESULT (host->regs_axi + 0x38)
98 #define NFC_V3_LAUNCH (host->regs_axi + 0x40)
100 #define NFC_V3_WRPROT (host->regs_ip + 0x0)
101 #define NFC_V3_WRPROT_LOCK_TIGHT (1 << 0)
102 #define NFC_V3_WRPROT_LOCK (1 << 1)
103 #define NFC_V3_WRPROT_UNLOCK (1 << 2)
104 #define NFC_V3_WRPROT_BLS_UNLOCK (2 << 6)
106 #define NFC_V3_WRPROT_UNLOCK_BLK_ADD0 (host->regs_ip + 0x04)
108 #define NFC_V3_CONFIG2 (host->regs_ip + 0x24)
109 #define NFC_V3_CONFIG2_PS_512 (0 << 0)
110 #define NFC_V3_CONFIG2_PS_2048 (1 << 0)
111 #define NFC_V3_CONFIG2_PS_4096 (2 << 0)
112 #define NFC_V3_CONFIG2_ONE_CYCLE (1 << 2)
113 #define NFC_V3_CONFIG2_ECC_EN (1 << 3)
114 #define NFC_V3_CONFIG2_2CMD_PHASES (1 << 4)
115 #define NFC_V3_CONFIG2_NUM_ADDR_PHASE0 (1 << 5)
116 #define NFC_V3_CONFIG2_ECC_MODE_8 (1 << 6)
117 #define NFC_V3_CONFIG2_PPB(x) (((x) & 0x3) << 7)
118 #define NFC_V3_CONFIG2_NUM_ADDR_PHASE1(x) (((x) & 0x3) << 12)
119 #define NFC_V3_CONFIG2_INT_MSK (1 << 15)
120 #define NFC_V3_CONFIG2_ST_CMD(x) (((x) & 0xff) << 24)
121 #define NFC_V3_CONFIG2_SPAS(x) (((x) & 0xff) << 16)
123 #define NFC_V3_CONFIG3 (host->regs_ip + 0x28)
124 #define NFC_V3_CONFIG3_ADD_OP(x) (((x) & 0x3) << 0)
125 #define NFC_V3_CONFIG3_FW8 (1 << 3)
126 #define NFC_V3_CONFIG3_SBB(x) (((x) & 0x7) << 8)
127 #define NFC_V3_CONFIG3_NUM_OF_DEVICES(x) (((x) & 0x7) << 12)
128 #define NFC_V3_CONFIG3_RBB_MODE (1 << 15)
129 #define NFC_V3_CONFIG3_NO_SDMA (1 << 20)
131 #define NFC_V3_IPC (host->regs_ip + 0x2C)
132 #define NFC_V3_IPC_CREQ (1 << 0)
133 #define NFC_V3_IPC_INT (1 << 31)
135 #define NFC_V3_DELAY_LINE (host->regs_ip + 0x34)
137 struct mxc_nand_host
{
139 struct nand_chip nand
;
140 struct mtd_partition
*parts
;
148 void __iomem
*regs_axi
;
149 void __iomem
*regs_ip
;
156 struct completion op_completion
;
159 unsigned int buf_start
;
162 void (*preset
)(struct mtd_info
*);
163 void (*send_cmd
)(struct mxc_nand_host
*, uint16_t, int);
164 void (*send_addr
)(struct mxc_nand_host
*, uint16_t, int);
165 void (*send_page
)(struct mtd_info
*, unsigned int);
166 void (*send_read_id
)(struct mxc_nand_host
*);
167 uint16_t (*get_dev_status
)(struct mxc_nand_host
*);
168 int (*check_int
)(struct mxc_nand_host
*);
169 void (*irq_control
)(struct mxc_nand_host
*, int);
172 /* OOB placement block for use with hardware ecc generation */
173 static struct nand_ecclayout nandv1_hw_eccoob_smallpage
= {
175 .eccpos
= {6, 7, 8, 9, 10},
176 .oobfree
= {{0, 5}, {12, 4}, }
179 static struct nand_ecclayout nandv1_hw_eccoob_largepage
= {
181 .eccpos
= {6, 7, 8, 9, 10, 22, 23, 24, 25, 26,
182 38, 39, 40, 41, 42, 54, 55, 56, 57, 58},
183 .oobfree
= {{2, 4}, {11, 10}, {27, 10}, {43, 10}, {59, 5}, }
186 /* OOB description for 512 byte pages with 16 byte OOB */
187 static struct nand_ecclayout nandv2_hw_eccoob_smallpage
= {
190 7, 8, 9, 10, 11, 12, 13, 14, 15
193 {.offset
= 0, .length
= 5}
197 /* OOB description for 2048 byte pages with 64 byte OOB */
198 static struct nand_ecclayout nandv2_hw_eccoob_largepage
= {
201 7, 8, 9, 10, 11, 12, 13, 14, 15,
202 23, 24, 25, 26, 27, 28, 29, 30, 31,
203 39, 40, 41, 42, 43, 44, 45, 46, 47,
204 55, 56, 57, 58, 59, 60, 61, 62, 63
207 {.offset
= 2, .length
= 4},
208 {.offset
= 16, .length
= 7},
209 {.offset
= 32, .length
= 7},
210 {.offset
= 48, .length
= 7}
214 /* OOB description for 4096 byte pages with 128 byte OOB */
215 static struct nand_ecclayout nandv2_hw_eccoob_4k
= {
218 7, 8, 9, 10, 11, 12, 13, 14, 15,
219 23, 24, 25, 26, 27, 28, 29, 30, 31,
220 39, 40, 41, 42, 43, 44, 45, 46, 47,
221 55, 56, 57, 58, 59, 60, 61, 62, 63,
222 71, 72, 73, 74, 75, 76, 77, 78, 79,
223 87, 88, 89, 90, 91, 92, 93, 94, 95,
224 103, 104, 105, 106, 107, 108, 109, 110, 111,
225 119, 120, 121, 122, 123, 124, 125, 126, 127,
228 {.offset
= 2, .length
= 4},
229 {.offset
= 16, .length
= 7},
230 {.offset
= 32, .length
= 7},
231 {.offset
= 48, .length
= 7},
232 {.offset
= 64, .length
= 7},
233 {.offset
= 80, .length
= 7},
234 {.offset
= 96, .length
= 7},
235 {.offset
= 112, .length
= 7},
239 #ifdef CONFIG_MTD_PARTITIONS
240 static const char *part_probes
[] = { "RedBoot", "cmdlinepart", NULL
};
243 static irqreturn_t
mxc_nfc_irq(int irq
, void *dev_id
)
245 struct mxc_nand_host
*host
= dev_id
;
247 if (!host
->check_int(host
))
250 host
->irq_control(host
, 0);
252 complete(&host
->op_completion
);
257 static int check_int_v3(struct mxc_nand_host
*host
)
261 tmp
= readl(NFC_V3_IPC
);
262 if (!(tmp
& NFC_V3_IPC_INT
))
265 tmp
&= ~NFC_V3_IPC_INT
;
266 writel(tmp
, NFC_V3_IPC
);
271 static int check_int_v1_v2(struct mxc_nand_host
*host
)
275 tmp
= readw(NFC_V1_V2_CONFIG2
);
276 if (!(tmp
& NFC_V1_V2_CONFIG2_INT
))
280 writew(tmp
& ~NFC_V1_V2_CONFIG2_INT
, NFC_V1_V2_CONFIG2
);
286 * It has been observed that the i.MX21 cannot read the CONFIG2:INT bit
287 * if interrupts are masked (CONFIG1:INT_MSK is set). To handle this, the
288 * driver can enable/disable the irq line rather than simply masking the
291 static void irq_control_mx21(struct mxc_nand_host
*host
, int activate
)
294 enable_irq(host
->irq
);
296 disable_irq_nosync(host
->irq
);
299 static void irq_control_v1_v2(struct mxc_nand_host
*host
, int activate
)
303 tmp
= readw(NFC_V1_V2_CONFIG1
);
306 tmp
&= ~NFC_V1_V2_CONFIG1_INT_MSK
;
308 tmp
|= NFC_V1_V2_CONFIG1_INT_MSK
;
310 writew(tmp
, NFC_V1_V2_CONFIG1
);
313 static void irq_control_v3(struct mxc_nand_host
*host
, int activate
)
317 tmp
= readl(NFC_V3_CONFIG2
);
320 tmp
&= ~NFC_V3_CONFIG2_INT_MSK
;
322 tmp
|= NFC_V3_CONFIG2_INT_MSK
;
324 writel(tmp
, NFC_V3_CONFIG2
);
327 /* This function polls the NANDFC to wait for the basic operation to
328 * complete by checking the INT bit of config2 register.
330 static void wait_op_done(struct mxc_nand_host
*host
, int useirq
)
332 int max_retries
= 8000;
335 if (!host
->check_int(host
)) {
336 INIT_COMPLETION(host
->op_completion
);
337 host
->irq_control(host
, 1);
338 wait_for_completion(&host
->op_completion
);
341 while (max_retries
-- > 0) {
342 if (host
->check_int(host
))
348 DEBUG(MTD_DEBUG_LEVEL0
, "%s: INT not set\n",
353 static void send_cmd_v3(struct mxc_nand_host
*host
, uint16_t cmd
, int useirq
)
356 writel(cmd
, NFC_V3_FLASH_CMD
);
358 /* send out command */
359 writel(NFC_CMD
, NFC_V3_LAUNCH
);
361 /* Wait for operation to complete */
362 wait_op_done(host
, useirq
);
365 /* This function issues the specified command to the NAND device and
366 * waits for completion. */
367 static void send_cmd_v1_v2(struct mxc_nand_host
*host
, uint16_t cmd
, int useirq
)
369 DEBUG(MTD_DEBUG_LEVEL3
, "send_cmd(host, 0x%x, %d)\n", cmd
, useirq
);
371 writew(cmd
, NFC_V1_V2_FLASH_CMD
);
372 writew(NFC_CMD
, NFC_V1_V2_CONFIG2
);
374 if (cpu_is_mx21() && (cmd
== NAND_CMD_RESET
)) {
375 int max_retries
= 100;
376 /* Reset completion is indicated by NFC_CONFIG2 */
378 while (max_retries
-- > 0) {
379 if (readw(NFC_V1_V2_CONFIG2
) == 0) {
385 DEBUG(MTD_DEBUG_LEVEL0
, "%s: RESET failed\n",
388 /* Wait for operation to complete */
389 wait_op_done(host
, useirq
);
393 static void send_addr_v3(struct mxc_nand_host
*host
, uint16_t addr
, int islast
)
396 writel(addr
, NFC_V3_FLASH_ADDR0
);
398 /* send out address */
399 writel(NFC_ADDR
, NFC_V3_LAUNCH
);
401 wait_op_done(host
, 0);
404 /* This function sends an address (or partial address) to the
405 * NAND device. The address is used to select the source/destination for
407 static void send_addr_v1_v2(struct mxc_nand_host
*host
, uint16_t addr
, int islast
)
409 DEBUG(MTD_DEBUG_LEVEL3
, "send_addr(host, 0x%x %d)\n", addr
, islast
);
411 writew(addr
, NFC_V1_V2_FLASH_ADDR
);
412 writew(NFC_ADDR
, NFC_V1_V2_CONFIG2
);
414 /* Wait for operation to complete */
415 wait_op_done(host
, islast
);
418 static void send_page_v3(struct mtd_info
*mtd
, unsigned int ops
)
420 struct nand_chip
*nand_chip
= mtd
->priv
;
421 struct mxc_nand_host
*host
= nand_chip
->priv
;
424 tmp
= readl(NFC_V3_CONFIG1
);
426 writel(tmp
, NFC_V3_CONFIG1
);
428 /* transfer data from NFC ram to nand */
429 writel(ops
, NFC_V3_LAUNCH
);
431 wait_op_done(host
, false);
434 static void send_page_v1_v2(struct mtd_info
*mtd
, unsigned int ops
)
436 struct nand_chip
*nand_chip
= mtd
->priv
;
437 struct mxc_nand_host
*host
= nand_chip
->priv
;
440 if (nfc_is_v1() && mtd
->writesize
> 512)
445 for (i
= 0; i
< bufs
; i
++) {
447 /* NANDFC buffer 0 is used for page read/write */
448 writew(i
, NFC_V1_V2_BUF_ADDR
);
450 writew(ops
, NFC_V1_V2_CONFIG2
);
452 /* Wait for operation to complete */
453 wait_op_done(host
, true);
457 static void send_read_id_v3(struct mxc_nand_host
*host
)
459 /* Read ID into main buffer */
460 writel(NFC_ID
, NFC_V3_LAUNCH
);
462 wait_op_done(host
, true);
464 memcpy(host
->data_buf
, host
->main_area0
, 16);
467 /* Request the NANDFC to perform a read of the NAND device ID. */
468 static void send_read_id_v1_v2(struct mxc_nand_host
*host
)
470 struct nand_chip
*this = &host
->nand
;
472 /* NANDFC buffer 0 is used for device ID output */
473 writew(0x0, NFC_V1_V2_BUF_ADDR
);
475 writew(NFC_ID
, NFC_V1_V2_CONFIG2
);
477 /* Wait for operation to complete */
478 wait_op_done(host
, true);
480 memcpy(host
->data_buf
, host
->main_area0
, 16);
482 if (this->options
& NAND_BUSWIDTH_16
) {
483 /* compress the ID info */
484 host
->data_buf
[1] = host
->data_buf
[2];
485 host
->data_buf
[2] = host
->data_buf
[4];
486 host
->data_buf
[3] = host
->data_buf
[6];
487 host
->data_buf
[4] = host
->data_buf
[8];
488 host
->data_buf
[5] = host
->data_buf
[10];
492 static uint16_t get_dev_status_v3(struct mxc_nand_host
*host
)
494 writew(NFC_STATUS
, NFC_V3_LAUNCH
);
495 wait_op_done(host
, true);
497 return readl(NFC_V3_CONFIG1
) >> 16;
500 /* This function requests the NANDFC to perform a read of the
501 * NAND device status and returns the current status. */
502 static uint16_t get_dev_status_v1_v2(struct mxc_nand_host
*host
)
504 void __iomem
*main_buf
= host
->main_area0
;
508 writew(0x0, NFC_V1_V2_BUF_ADDR
);
511 * The device status is stored in main_area0. To
512 * prevent corruption of the buffer save the value
513 * and restore it afterwards.
515 store
= readl(main_buf
);
517 writew(NFC_STATUS
, NFC_V1_V2_CONFIG2
);
518 wait_op_done(host
, true);
520 ret
= readw(main_buf
);
522 writel(store
, main_buf
);
527 /* This functions is used by upper layer to checks if device is ready */
528 static int mxc_nand_dev_ready(struct mtd_info
*mtd
)
531 * NFC handles R/B internally. Therefore, this function
532 * always returns status as ready.
537 static void mxc_nand_enable_hwecc(struct mtd_info
*mtd
, int mode
)
540 * If HW ECC is enabled, we turn it on during init. There is
541 * no need to enable again here.
545 static int mxc_nand_correct_data_v1(struct mtd_info
*mtd
, u_char
*dat
,
546 u_char
*read_ecc
, u_char
*calc_ecc
)
548 struct nand_chip
*nand_chip
= mtd
->priv
;
549 struct mxc_nand_host
*host
= nand_chip
->priv
;
552 * 1-Bit errors are automatically corrected in HW. No need for
553 * additional correction. 2-Bit errors cannot be corrected by
554 * HW ECC, so we need to return failure
556 uint16_t ecc_status
= readw(NFC_V1_V2_ECC_STATUS_RESULT
);
558 if (((ecc_status
& 0x3) == 2) || ((ecc_status
>> 2) == 2)) {
559 DEBUG(MTD_DEBUG_LEVEL0
,
560 "MXC_NAND: HWECC uncorrectable 2-bit ECC error\n");
567 static int mxc_nand_correct_data_v2_v3(struct mtd_info
*mtd
, u_char
*dat
,
568 u_char
*read_ecc
, u_char
*calc_ecc
)
570 struct nand_chip
*nand_chip
= mtd
->priv
;
571 struct mxc_nand_host
*host
= nand_chip
->priv
;
575 u8 ecc_bit_mask
, err_limit
;
577 ecc_bit_mask
= (host
->eccsize
== 4) ? 0x7 : 0xf;
578 err_limit
= (host
->eccsize
== 4) ? 0x4 : 0x8;
580 no_subpages
= mtd
->writesize
>> 9;
583 ecc_stat
= readl(NFC_V1_V2_ECC_STATUS_RESULT
);
585 ecc_stat
= readl(NFC_V3_ECC_STATUS_RESULT
);
588 err
= ecc_stat
& ecc_bit_mask
;
589 if (err
> err_limit
) {
590 printk(KERN_WARNING
"UnCorrectable RS-ECC Error\n");
596 } while (--no_subpages
);
598 mtd
->ecc_stats
.corrected
+= ret
;
599 pr_debug("%d Symbol Correctable RS-ECC Error\n", ret
);
604 static int mxc_nand_calculate_ecc(struct mtd_info
*mtd
, const u_char
*dat
,
610 static u_char
mxc_nand_read_byte(struct mtd_info
*mtd
)
612 struct nand_chip
*nand_chip
= mtd
->priv
;
613 struct mxc_nand_host
*host
= nand_chip
->priv
;
616 /* Check for status request */
617 if (host
->status_request
)
618 return host
->get_dev_status(host
) & 0xFF;
620 ret
= *(uint8_t *)(host
->data_buf
+ host
->buf_start
);
626 static uint16_t mxc_nand_read_word(struct mtd_info
*mtd
)
628 struct nand_chip
*nand_chip
= mtd
->priv
;
629 struct mxc_nand_host
*host
= nand_chip
->priv
;
632 ret
= *(uint16_t *)(host
->data_buf
+ host
->buf_start
);
633 host
->buf_start
+= 2;
638 /* Write data of length len to buffer buf. The data to be
639 * written on NAND Flash is first copied to RAMbuffer. After the Data Input
640 * Operation by the NFC, the data is written to NAND Flash */
641 static void mxc_nand_write_buf(struct mtd_info
*mtd
,
642 const u_char
*buf
, int len
)
644 struct nand_chip
*nand_chip
= mtd
->priv
;
645 struct mxc_nand_host
*host
= nand_chip
->priv
;
646 u16 col
= host
->buf_start
;
647 int n
= mtd
->oobsize
+ mtd
->writesize
- col
;
651 memcpy(host
->data_buf
+ col
, buf
, n
);
653 host
->buf_start
+= n
;
656 /* Read the data buffer from the NAND Flash. To read the data from NAND
657 * Flash first the data output cycle is initiated by the NFC, which copies
658 * the data to RAMbuffer. This data of length len is then copied to buffer buf.
660 static void mxc_nand_read_buf(struct mtd_info
*mtd
, u_char
*buf
, int len
)
662 struct nand_chip
*nand_chip
= mtd
->priv
;
663 struct mxc_nand_host
*host
= nand_chip
->priv
;
664 u16 col
= host
->buf_start
;
665 int n
= mtd
->oobsize
+ mtd
->writesize
- col
;
669 memcpy(buf
, host
->data_buf
+ col
, n
);
671 host
->buf_start
+= n
;
674 /* Used by the upper layer to verify the data in NAND Flash
675 * with the data in the buf. */
676 static int mxc_nand_verify_buf(struct mtd_info
*mtd
,
677 const u_char
*buf
, int len
)
682 /* This function is used by upper layer for select and
683 * deselect of the NAND chip */
684 static void mxc_nand_select_chip(struct mtd_info
*mtd
, int chip
)
686 struct nand_chip
*nand_chip
= mtd
->priv
;
687 struct mxc_nand_host
*host
= nand_chip
->priv
;
691 /* Disable the NFC clock */
693 clk_disable(host
->clk
);
698 /* Enable the NFC clock */
699 if (!host
->clk_act
) {
700 clk_enable(host
->clk
);
711 * Function to transfer data to/from spare area.
713 static void copy_spare(struct mtd_info
*mtd
, bool bfrom
)
715 struct nand_chip
*this = mtd
->priv
;
716 struct mxc_nand_host
*host
= this->priv
;
718 u16 n
= mtd
->writesize
>> 9;
719 u8
*d
= host
->data_buf
+ mtd
->writesize
;
720 u8
*s
= host
->spare0
;
721 u16 t
= host
->spare_len
;
723 j
= (mtd
->oobsize
/ n
>> 1) << 1;
726 for (i
= 0; i
< n
- 1; i
++)
727 memcpy(d
+ i
* j
, s
+ i
* t
, j
);
729 /* the last section */
730 memcpy(d
+ i
* j
, s
+ i
* t
, mtd
->oobsize
- i
* j
);
732 for (i
= 0; i
< n
- 1; i
++)
733 memcpy(&s
[i
* t
], &d
[i
* j
], j
);
735 /* the last section */
736 memcpy(&s
[i
* t
], &d
[i
* j
], mtd
->oobsize
- i
* j
);
740 static void mxc_do_addr_cycle(struct mtd_info
*mtd
, int column
, int page_addr
)
742 struct nand_chip
*nand_chip
= mtd
->priv
;
743 struct mxc_nand_host
*host
= nand_chip
->priv
;
745 /* Write out column address, if necessary */
748 * MXC NANDFC can only perform full page+spare or
749 * spare-only read/write. When the upper layers
750 * perform a read/write buf operation, the saved column
751 * address is used to index into the full page.
753 host
->send_addr(host
, 0, page_addr
== -1);
754 if (mtd
->writesize
> 512)
755 /* another col addr cycle for 2k page */
756 host
->send_addr(host
, 0, false);
759 /* Write out page address, if necessary */
760 if (page_addr
!= -1) {
761 /* paddr_0 - p_addr_7 */
762 host
->send_addr(host
, (page_addr
& 0xff), false);
764 if (mtd
->writesize
> 512) {
765 if (mtd
->size
>= 0x10000000) {
766 /* paddr_8 - paddr_15 */
767 host
->send_addr(host
, (page_addr
>> 8) & 0xff, false);
768 host
->send_addr(host
, (page_addr
>> 16) & 0xff, true);
770 /* paddr_8 - paddr_15 */
771 host
->send_addr(host
, (page_addr
>> 8) & 0xff, true);
773 /* One more address cycle for higher density devices */
774 if (mtd
->size
>= 0x4000000) {
775 /* paddr_8 - paddr_15 */
776 host
->send_addr(host
, (page_addr
>> 8) & 0xff, false);
777 host
->send_addr(host
, (page_addr
>> 16) & 0xff, true);
779 /* paddr_8 - paddr_15 */
780 host
->send_addr(host
, (page_addr
>> 8) & 0xff, true);
786 * v2 and v3 type controllers can do 4bit or 8bit ecc depending
787 * on how much oob the nand chip has. For 8bit ecc we need at least
788 * 26 bytes of oob data per 512 byte block.
790 static int get_eccsize(struct mtd_info
*mtd
)
792 int oobbytes_per_512
= 0;
794 oobbytes_per_512
= mtd
->oobsize
* 512 / mtd
->writesize
;
796 if (oobbytes_per_512
< 26)
802 static void preset_v1_v2(struct mtd_info
*mtd
)
804 struct nand_chip
*nand_chip
= mtd
->priv
;
805 struct mxc_nand_host
*host
= nand_chip
->priv
;
806 uint16_t config1
= 0;
808 if (nand_chip
->ecc
.mode
== NAND_ECC_HW
)
809 config1
|= NFC_V1_V2_CONFIG1_ECC_EN
;
812 config1
|= NFC_V2_CONFIG1_FP_INT
;
815 config1
|= NFC_V1_V2_CONFIG1_INT_MSK
;
817 if (nfc_is_v21() && mtd
->writesize
) {
818 uint16_t pages_per_block
= mtd
->erasesize
/ mtd
->writesize
;
820 host
->eccsize
= get_eccsize(mtd
);
821 if (host
->eccsize
== 4)
822 config1
|= NFC_V2_CONFIG1_ECC_MODE_4
;
824 config1
|= NFC_V2_CONFIG1_PPB(ffs(pages_per_block
) - 6);
829 writew(config1
, NFC_V1_V2_CONFIG1
);
830 /* preset operation */
832 /* Unlock the internal RAM Buffer */
833 writew(0x2, NFC_V1_V2_CONFIG
);
835 /* Blocks to be unlocked */
837 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR
);
838 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR
);
839 } else if (nfc_is_v1()) {
840 writew(0x0, NFC_V1_UNLOCKSTART_BLKADDR
);
841 writew(0x4000, NFC_V1_UNLOCKEND_BLKADDR
);
845 /* Unlock Block Command for given address range */
846 writew(0x4, NFC_V1_V2_WRPROT
);
849 static void preset_v3(struct mtd_info
*mtd
)
851 struct nand_chip
*chip
= mtd
->priv
;
852 struct mxc_nand_host
*host
= chip
->priv
;
853 uint32_t config2
, config3
;
856 writel(NFC_V3_CONFIG1_RBA(0), NFC_V3_CONFIG1
);
857 writel(NFC_V3_IPC_CREQ
, NFC_V3_IPC
);
859 /* Unlock the internal RAM Buffer */
860 writel(NFC_V3_WRPROT_BLS_UNLOCK
| NFC_V3_WRPROT_UNLOCK
,
863 /* Blocks to be unlocked */
864 for (i
= 0; i
< NAND_MAX_CHIPS
; i
++)
865 writel(0x0 | (0xffff << 16),
866 NFC_V3_WRPROT_UNLOCK_BLK_ADD0
+ (i
<< 2));
868 writel(0, NFC_V3_IPC
);
870 config2
= NFC_V3_CONFIG2_ONE_CYCLE
|
871 NFC_V3_CONFIG2_2CMD_PHASES
|
872 NFC_V3_CONFIG2_SPAS(mtd
->oobsize
>> 1) |
873 NFC_V3_CONFIG2_ST_CMD(0x70) |
874 NFC_V3_CONFIG2_INT_MSK
|
875 NFC_V3_CONFIG2_NUM_ADDR_PHASE0
;
877 if (chip
->ecc
.mode
== NAND_ECC_HW
)
878 config2
|= NFC_V3_CONFIG2_ECC_EN
;
880 addr_phases
= fls(chip
->pagemask
) >> 3;
882 if (mtd
->writesize
== 2048) {
883 config2
|= NFC_V3_CONFIG2_PS_2048
;
884 config2
|= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases
);
885 } else if (mtd
->writesize
== 4096) {
886 config2
|= NFC_V3_CONFIG2_PS_4096
;
887 config2
|= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases
);
889 config2
|= NFC_V3_CONFIG2_PS_512
;
890 config2
|= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases
- 1);
893 if (mtd
->writesize
) {
894 config2
|= NFC_V3_CONFIG2_PPB(ffs(mtd
->erasesize
/ mtd
->writesize
) - 6);
895 host
->eccsize
= get_eccsize(mtd
);
896 if (host
->eccsize
== 8)
897 config2
|= NFC_V3_CONFIG2_ECC_MODE_8
;
900 writel(config2
, NFC_V3_CONFIG2
);
902 config3
= NFC_V3_CONFIG3_NUM_OF_DEVICES(0) |
903 NFC_V3_CONFIG3_NO_SDMA
|
904 NFC_V3_CONFIG3_RBB_MODE
|
905 NFC_V3_CONFIG3_SBB(6) | /* Reset default */
906 NFC_V3_CONFIG3_ADD_OP(0);
908 if (!(chip
->options
& NAND_BUSWIDTH_16
))
909 config3
|= NFC_V3_CONFIG3_FW8
;
911 writel(config3
, NFC_V3_CONFIG3
);
913 writel(0, NFC_V3_DELAY_LINE
);
916 /* Used by the upper layer to write command to NAND Flash for
917 * different operations to be carried out on NAND Flash */
918 static void mxc_nand_command(struct mtd_info
*mtd
, unsigned command
,
919 int column
, int page_addr
)
921 struct nand_chip
*nand_chip
= mtd
->priv
;
922 struct mxc_nand_host
*host
= nand_chip
->priv
;
924 DEBUG(MTD_DEBUG_LEVEL3
,
925 "mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
926 command
, column
, page_addr
);
928 /* Reset command state information */
929 host
->status_request
= false;
931 /* Command pre-processing step */
935 host
->send_cmd(host
, command
, false);
938 case NAND_CMD_STATUS
:
940 host
->status_request
= true;
942 host
->send_cmd(host
, command
, true);
943 mxc_do_addr_cycle(mtd
, column
, page_addr
);
947 case NAND_CMD_READOOB
:
948 if (command
== NAND_CMD_READ0
)
949 host
->buf_start
= column
;
951 host
->buf_start
= column
+ mtd
->writesize
;
953 command
= NAND_CMD_READ0
; /* only READ0 is valid */
955 host
->send_cmd(host
, command
, false);
956 mxc_do_addr_cycle(mtd
, column
, page_addr
);
958 if (mtd
->writesize
> 512)
959 host
->send_cmd(host
, NAND_CMD_READSTART
, true);
961 host
->send_page(mtd
, NFC_OUTPUT
);
963 memcpy(host
->data_buf
, host
->main_area0
, mtd
->writesize
);
964 copy_spare(mtd
, true);
968 if (column
>= mtd
->writesize
)
969 /* call ourself to read a page */
970 mxc_nand_command(mtd
, NAND_CMD_READ0
, 0, page_addr
);
972 host
->buf_start
= column
;
974 host
->send_cmd(host
, command
, false);
975 mxc_do_addr_cycle(mtd
, column
, page_addr
);
978 case NAND_CMD_PAGEPROG
:
979 memcpy(host
->main_area0
, host
->data_buf
, mtd
->writesize
);
980 copy_spare(mtd
, false);
981 host
->send_page(mtd
, NFC_INPUT
);
982 host
->send_cmd(host
, command
, true);
983 mxc_do_addr_cycle(mtd
, column
, page_addr
);
986 case NAND_CMD_READID
:
987 host
->send_cmd(host
, command
, true);
988 mxc_do_addr_cycle(mtd
, column
, page_addr
);
989 host
->send_read_id(host
);
990 host
->buf_start
= column
;
993 case NAND_CMD_ERASE1
:
994 case NAND_CMD_ERASE2
:
995 host
->send_cmd(host
, command
, false);
996 mxc_do_addr_cycle(mtd
, column
, page_addr
);
1003 * The generic flash bbt decriptors overlap with our ecc
1004 * hardware, so define some i.MX specific ones.
1006 static uint8_t bbt_pattern
[] = { 'B', 'b', 't', '0' };
1007 static uint8_t mirror_pattern
[] = { '1', 't', 'b', 'B' };
1009 static struct nand_bbt_descr bbt_main_descr
= {
1010 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
1011 | NAND_BBT_2BIT
| NAND_BBT_VERSION
| NAND_BBT_PERCHIP
,
1016 .pattern
= bbt_pattern
,
1019 static struct nand_bbt_descr bbt_mirror_descr
= {
1020 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
1021 | NAND_BBT_2BIT
| NAND_BBT_VERSION
| NAND_BBT_PERCHIP
,
1026 .pattern
= mirror_pattern
,
1029 static int __init
mxcnd_probe(struct platform_device
*pdev
)
1031 struct nand_chip
*this;
1032 struct mtd_info
*mtd
;
1033 struct mxc_nand_platform_data
*pdata
= pdev
->dev
.platform_data
;
1034 struct mxc_nand_host
*host
;
1035 struct resource
*res
;
1036 int err
= 0, __maybe_unused nr_parts
= 0;
1037 struct nand_ecclayout
*oob_smallpage
, *oob_largepage
;
1039 /* Allocate memory for MTD device structure and private data */
1040 host
= kzalloc(sizeof(struct mxc_nand_host
) + NAND_MAX_PAGESIZE
+
1041 NAND_MAX_OOBSIZE
, GFP_KERNEL
);
1045 host
->data_buf
= (uint8_t *)(host
+ 1);
1047 host
->dev
= &pdev
->dev
;
1048 /* structures must be linked */
1052 mtd
->owner
= THIS_MODULE
;
1053 mtd
->dev
.parent
= &pdev
->dev
;
1054 mtd
->name
= DRIVER_NAME
;
1056 /* 50 us command delay time */
1057 this->chip_delay
= 5;
1060 this->dev_ready
= mxc_nand_dev_ready
;
1061 this->cmdfunc
= mxc_nand_command
;
1062 this->select_chip
= mxc_nand_select_chip
;
1063 this->read_byte
= mxc_nand_read_byte
;
1064 this->read_word
= mxc_nand_read_word
;
1065 this->write_buf
= mxc_nand_write_buf
;
1066 this->read_buf
= mxc_nand_read_buf
;
1067 this->verify_buf
= mxc_nand_verify_buf
;
1069 host
->clk
= clk_get(&pdev
->dev
, "nfc");
1070 if (IS_ERR(host
->clk
)) {
1071 err
= PTR_ERR(host
->clk
);
1075 clk_enable(host
->clk
);
1078 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1084 host
->base
= ioremap(res
->start
, resource_size(res
));
1090 host
->main_area0
= host
->base
;
1092 if (nfc_is_v1() || nfc_is_v21()) {
1093 host
->preset
= preset_v1_v2
;
1094 host
->send_cmd
= send_cmd_v1_v2
;
1095 host
->send_addr
= send_addr_v1_v2
;
1096 host
->send_page
= send_page_v1_v2
;
1097 host
->send_read_id
= send_read_id_v1_v2
;
1098 host
->get_dev_status
= get_dev_status_v1_v2
;
1099 host
->check_int
= check_int_v1_v2
;
1101 host
->irq_control
= irq_control_mx21
;
1103 host
->irq_control
= irq_control_v1_v2
;
1107 host
->regs
= host
->base
+ 0x1e00;
1108 host
->spare0
= host
->base
+ 0x1000;
1109 host
->spare_len
= 64;
1110 oob_smallpage
= &nandv2_hw_eccoob_smallpage
;
1111 oob_largepage
= &nandv2_hw_eccoob_largepage
;
1112 this->ecc
.bytes
= 9;
1113 } else if (nfc_is_v1()) {
1114 host
->regs
= host
->base
+ 0xe00;
1115 host
->spare0
= host
->base
+ 0x800;
1116 host
->spare_len
= 16;
1117 oob_smallpage
= &nandv1_hw_eccoob_smallpage
;
1118 oob_largepage
= &nandv1_hw_eccoob_largepage
;
1119 this->ecc
.bytes
= 3;
1121 } else if (nfc_is_v3_2()) {
1122 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1127 host
->regs_ip
= ioremap(res
->start
, resource_size(res
));
1128 if (!host
->regs_ip
) {
1132 host
->regs_axi
= host
->base
+ 0x1e00;
1133 host
->spare0
= host
->base
+ 0x1000;
1134 host
->spare_len
= 64;
1135 host
->preset
= preset_v3
;
1136 host
->send_cmd
= send_cmd_v3
;
1137 host
->send_addr
= send_addr_v3
;
1138 host
->send_page
= send_page_v3
;
1139 host
->send_read_id
= send_read_id_v3
;
1140 host
->check_int
= check_int_v3
;
1141 host
->get_dev_status
= get_dev_status_v3
;
1142 host
->irq_control
= irq_control_v3
;
1143 oob_smallpage
= &nandv2_hw_eccoob_smallpage
;
1144 oob_largepage
= &nandv2_hw_eccoob_largepage
;
1148 this->ecc
.size
= 512;
1149 this->ecc
.layout
= oob_smallpage
;
1151 if (pdata
->hw_ecc
) {
1152 this->ecc
.calculate
= mxc_nand_calculate_ecc
;
1153 this->ecc
.hwctl
= mxc_nand_enable_hwecc
;
1155 this->ecc
.correct
= mxc_nand_correct_data_v1
;
1157 this->ecc
.correct
= mxc_nand_correct_data_v2_v3
;
1158 this->ecc
.mode
= NAND_ECC_HW
;
1160 this->ecc
.mode
= NAND_ECC_SOFT
;
1163 /* NAND bus width determines access funtions used by upper layer */
1164 if (pdata
->width
== 2)
1165 this->options
|= NAND_BUSWIDTH_16
;
1167 if (pdata
->flash_bbt
) {
1168 this->bbt_td
= &bbt_main_descr
;
1169 this->bbt_md
= &bbt_mirror_descr
;
1170 /* update flash based bbt */
1171 this->options
|= NAND_USE_FLASH_BBT
;
1174 init_completion(&host
->op_completion
);
1176 host
->irq
= platform_get_irq(pdev
, 0);
1179 * mask the interrupt. For i.MX21 explicitely call
1180 * irq_control_v1_v2 to use the mask bit. We can't call
1181 * disable_irq_nosync() for an interrupt we do not own yet.
1184 irq_control_v1_v2(host
, 0);
1186 host
->irq_control(host
, 0);
1188 err
= request_irq(host
->irq
, mxc_nfc_irq
, IRQF_DISABLED
, DRIVER_NAME
, host
);
1192 host
->irq_control(host
, 0);
1195 * Now that the interrupt is disabled make sure the interrupt
1196 * mask bit is cleared on i.MX21. Otherwise we can't read
1197 * the interrupt status bit on this machine.
1200 irq_control_v1_v2(host
, 1);
1202 /* first scan to find the device and get the page size */
1203 if (nand_scan_ident(mtd
, 1, NULL
)) {
1208 /* Call preset again, with correct writesize this time */
1211 if (mtd
->writesize
== 2048)
1212 this->ecc
.layout
= oob_largepage
;
1213 if (nfc_is_v21() && mtd
->writesize
== 4096)
1214 this->ecc
.layout
= &nandv2_hw_eccoob_4k
;
1216 /* second phase scan */
1217 if (nand_scan_tail(mtd
)) {
1222 /* Register the partitions */
1223 #ifdef CONFIG_MTD_PARTITIONS
1225 parse_mtd_partitions(mtd
, part_probes
, &host
->parts
, 0);
1227 add_mtd_partitions(mtd
, host
->parts
, nr_parts
);
1228 else if (pdata
->parts
)
1229 add_mtd_partitions(mtd
, pdata
->parts
, pdata
->nr_parts
);
1233 pr_info("Registering %s as whole device\n", mtd
->name
);
1234 add_mtd_device(mtd
);
1237 platform_set_drvdata(pdev
, host
);
1242 free_irq(host
->irq
, host
);
1245 iounmap(host
->regs_ip
);
1246 iounmap(host
->base
);
1255 static int __devexit
mxcnd_remove(struct platform_device
*pdev
)
1257 struct mxc_nand_host
*host
= platform_get_drvdata(pdev
);
1261 platform_set_drvdata(pdev
, NULL
);
1263 nand_release(&host
->mtd
);
1264 free_irq(host
->irq
, host
);
1266 iounmap(host
->regs_ip
);
1267 iounmap(host
->base
);
1273 static struct platform_driver mxcnd_driver
= {
1275 .name
= DRIVER_NAME
,
1277 .remove
= __devexit_p(mxcnd_remove
),
1280 static int __init
mxc_nd_init(void)
1282 return platform_driver_probe(&mxcnd_driver
, mxcnd_probe
);
1285 static void __exit
mxc_nd_cleanup(void)
1287 /* Unregister the device structure */
1288 platform_driver_unregister(&mxcnd_driver
);
1291 module_init(mxc_nd_init
);
1292 module_exit(mxc_nd_cleanup
);
1294 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1295 MODULE_DESCRIPTION("MXC NAND MTD driver");
1296 MODULE_LICENSE("GPL");