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 <linux/of_device.h>
38 #include <asm/mach/flash.h>
39 #include <linux/platform_data/mtd-mxc_nand.h>
41 #define DRIVER_NAME "mxc_nand"
43 /* Addresses for NFC registers */
44 #define NFC_V1_V2_BUF_SIZE (host->regs + 0x00)
45 #define NFC_V1_V2_BUF_ADDR (host->regs + 0x04)
46 #define NFC_V1_V2_FLASH_ADDR (host->regs + 0x06)
47 #define NFC_V1_V2_FLASH_CMD (host->regs + 0x08)
48 #define NFC_V1_V2_CONFIG (host->regs + 0x0a)
49 #define NFC_V1_V2_ECC_STATUS_RESULT (host->regs + 0x0c)
50 #define NFC_V1_V2_RSLTMAIN_AREA (host->regs + 0x0e)
51 #define NFC_V1_V2_RSLTSPARE_AREA (host->regs + 0x10)
52 #define NFC_V1_V2_WRPROT (host->regs + 0x12)
53 #define NFC_V1_UNLOCKSTART_BLKADDR (host->regs + 0x14)
54 #define NFC_V1_UNLOCKEND_BLKADDR (host->regs + 0x16)
55 #define NFC_V21_UNLOCKSTART_BLKADDR0 (host->regs + 0x20)
56 #define NFC_V21_UNLOCKSTART_BLKADDR1 (host->regs + 0x24)
57 #define NFC_V21_UNLOCKSTART_BLKADDR2 (host->regs + 0x28)
58 #define NFC_V21_UNLOCKSTART_BLKADDR3 (host->regs + 0x2c)
59 #define NFC_V21_UNLOCKEND_BLKADDR0 (host->regs + 0x22)
60 #define NFC_V21_UNLOCKEND_BLKADDR1 (host->regs + 0x26)
61 #define NFC_V21_UNLOCKEND_BLKADDR2 (host->regs + 0x2a)
62 #define NFC_V21_UNLOCKEND_BLKADDR3 (host->regs + 0x2e)
63 #define NFC_V1_V2_NF_WRPRST (host->regs + 0x18)
64 #define NFC_V1_V2_CONFIG1 (host->regs + 0x1a)
65 #define NFC_V1_V2_CONFIG2 (host->regs + 0x1c)
67 #define NFC_V2_CONFIG1_ECC_MODE_4 (1 << 0)
68 #define NFC_V1_V2_CONFIG1_SP_EN (1 << 2)
69 #define NFC_V1_V2_CONFIG1_ECC_EN (1 << 3)
70 #define NFC_V1_V2_CONFIG1_INT_MSK (1 << 4)
71 #define NFC_V1_V2_CONFIG1_BIG (1 << 5)
72 #define NFC_V1_V2_CONFIG1_RST (1 << 6)
73 #define NFC_V1_V2_CONFIG1_CE (1 << 7)
74 #define NFC_V2_CONFIG1_ONE_CYCLE (1 << 8)
75 #define NFC_V2_CONFIG1_PPB(x) (((x) & 0x3) << 9)
76 #define NFC_V2_CONFIG1_FP_INT (1 << 11)
78 #define NFC_V1_V2_CONFIG2_INT (1 << 15)
81 * Operation modes for the NFC. Valid for v1, v2 and v3
84 #define NFC_CMD (1 << 0)
85 #define NFC_ADDR (1 << 1)
86 #define NFC_INPUT (1 << 2)
87 #define NFC_OUTPUT (1 << 3)
88 #define NFC_ID (1 << 4)
89 #define NFC_STATUS (1 << 5)
91 #define NFC_V3_FLASH_CMD (host->regs_axi + 0x00)
92 #define NFC_V3_FLASH_ADDR0 (host->regs_axi + 0x04)
94 #define NFC_V3_CONFIG1 (host->regs_axi + 0x34)
95 #define NFC_V3_CONFIG1_SP_EN (1 << 0)
96 #define NFC_V3_CONFIG1_RBA(x) (((x) & 0x7 ) << 4)
98 #define NFC_V3_ECC_STATUS_RESULT (host->regs_axi + 0x38)
100 #define NFC_V3_LAUNCH (host->regs_axi + 0x40)
102 #define NFC_V3_WRPROT (host->regs_ip + 0x0)
103 #define NFC_V3_WRPROT_LOCK_TIGHT (1 << 0)
104 #define NFC_V3_WRPROT_LOCK (1 << 1)
105 #define NFC_V3_WRPROT_UNLOCK (1 << 2)
106 #define NFC_V3_WRPROT_BLS_UNLOCK (2 << 6)
108 #define NFC_V3_WRPROT_UNLOCK_BLK_ADD0 (host->regs_ip + 0x04)
110 #define NFC_V3_CONFIG2 (host->regs_ip + 0x24)
111 #define NFC_V3_CONFIG2_PS_512 (0 << 0)
112 #define NFC_V3_CONFIG2_PS_2048 (1 << 0)
113 #define NFC_V3_CONFIG2_PS_4096 (2 << 0)
114 #define NFC_V3_CONFIG2_ONE_CYCLE (1 << 2)
115 #define NFC_V3_CONFIG2_ECC_EN (1 << 3)
116 #define NFC_V3_CONFIG2_2CMD_PHASES (1 << 4)
117 #define NFC_V3_CONFIG2_NUM_ADDR_PHASE0 (1 << 5)
118 #define NFC_V3_CONFIG2_ECC_MODE_8 (1 << 6)
119 #define NFC_V3_CONFIG2_PPB(x, shift) (((x) & 0x3) << shift)
120 #define NFC_V3_CONFIG2_NUM_ADDR_PHASE1(x) (((x) & 0x3) << 12)
121 #define NFC_V3_CONFIG2_INT_MSK (1 << 15)
122 #define NFC_V3_CONFIG2_ST_CMD(x) (((x) & 0xff) << 24)
123 #define NFC_V3_CONFIG2_SPAS(x) (((x) & 0xff) << 16)
125 #define NFC_V3_CONFIG3 (host->regs_ip + 0x28)
126 #define NFC_V3_CONFIG3_ADD_OP(x) (((x) & 0x3) << 0)
127 #define NFC_V3_CONFIG3_FW8 (1 << 3)
128 #define NFC_V3_CONFIG3_SBB(x) (((x) & 0x7) << 8)
129 #define NFC_V3_CONFIG3_NUM_OF_DEVICES(x) (((x) & 0x7) << 12)
130 #define NFC_V3_CONFIG3_RBB_MODE (1 << 15)
131 #define NFC_V3_CONFIG3_NO_SDMA (1 << 20)
133 #define NFC_V3_IPC (host->regs_ip + 0x2C)
134 #define NFC_V3_IPC_CREQ (1 << 0)
135 #define NFC_V3_IPC_INT (1 << 31)
137 #define NFC_V3_DELAY_LINE (host->regs_ip + 0x34)
139 struct mxc_nand_host
;
141 struct mxc_nand_devtype_data
{
142 void (*preset
)(struct mtd_info
*);
143 void (*send_cmd
)(struct mxc_nand_host
*, uint16_t, int);
144 void (*send_addr
)(struct mxc_nand_host
*, uint16_t, int);
145 void (*send_page
)(struct mtd_info
*, unsigned int);
146 void (*send_read_id
)(struct mxc_nand_host
*);
147 uint16_t (*get_dev_status
)(struct mxc_nand_host
*);
148 int (*check_int
)(struct mxc_nand_host
*);
149 void (*irq_control
)(struct mxc_nand_host
*, int);
150 u32 (*get_ecc_status
)(struct mxc_nand_host
*);
151 const struct mtd_ooblayout_ops
*ooblayout
;
152 void (*select_chip
)(struct mtd_info
*mtd
, int chip
);
153 int (*correct_data
)(struct mtd_info
*mtd
, u_char
*dat
,
154 u_char
*read_ecc
, u_char
*calc_ecc
);
157 * On i.MX21 the CONFIG2:INT bit cannot be read if interrupts are masked
158 * (CONFIG1:INT_MSK is set). To handle this the driver uses
159 * enable_irq/disable_irq_nosync instead of CONFIG1:INT_MSK
161 int irqpending_quirk
;
165 size_t spare0_offset
;
174 struct mxc_nand_host
{
175 struct nand_chip nand
;
178 void __iomem
*spare0
;
179 void __iomem
*main_area0
;
183 void __iomem
*regs_axi
;
184 void __iomem
*regs_ip
;
193 struct completion op_completion
;
196 unsigned int buf_start
;
198 const struct mxc_nand_devtype_data
*devtype_data
;
199 struct mxc_nand_platform_data pdata
;
202 static const char * const part_probes
[] = {
203 "cmdlinepart", "RedBoot", "ofpart", NULL
};
205 static void memcpy32_fromio(void *trg
, const void __iomem
*src
, size_t size
)
209 const __iomem u32
*s
= src
;
211 for (i
= 0; i
< (size
>> 2); i
++)
212 *t
++ = __raw_readl(s
++);
215 static void memcpy16_fromio(void *trg
, const void __iomem
*src
, size_t size
)
219 const __iomem u16
*s
= src
;
221 /* We assume that src (IO) is always 32bit aligned */
222 if (PTR_ALIGN(trg
, 4) == trg
&& IS_ALIGNED(size
, 4)) {
223 memcpy32_fromio(trg
, src
, size
);
227 for (i
= 0; i
< (size
>> 1); i
++)
228 *t
++ = __raw_readw(s
++);
231 static inline void memcpy32_toio(void __iomem
*trg
, const void *src
, int size
)
233 /* __iowrite32_copy use 32bit size values so divide by 4 */
234 __iowrite32_copy(trg
, src
, size
/ 4);
237 static void memcpy16_toio(void __iomem
*trg
, const void *src
, int size
)
240 __iomem u16
*t
= trg
;
243 /* We assume that trg (IO) is always 32bit aligned */
244 if (PTR_ALIGN(src
, 4) == src
&& IS_ALIGNED(size
, 4)) {
245 memcpy32_toio(trg
, src
, size
);
249 for (i
= 0; i
< (size
>> 1); i
++)
250 __raw_writew(*s
++, t
++);
253 static int check_int_v3(struct mxc_nand_host
*host
)
257 tmp
= readl(NFC_V3_IPC
);
258 if (!(tmp
& NFC_V3_IPC_INT
))
261 tmp
&= ~NFC_V3_IPC_INT
;
262 writel(tmp
, NFC_V3_IPC
);
267 static int check_int_v1_v2(struct mxc_nand_host
*host
)
271 tmp
= readw(NFC_V1_V2_CONFIG2
);
272 if (!(tmp
& NFC_V1_V2_CONFIG2_INT
))
275 if (!host
->devtype_data
->irqpending_quirk
)
276 writew(tmp
& ~NFC_V1_V2_CONFIG2_INT
, NFC_V1_V2_CONFIG2
);
281 static void irq_control_v1_v2(struct mxc_nand_host
*host
, int activate
)
285 tmp
= readw(NFC_V1_V2_CONFIG1
);
288 tmp
&= ~NFC_V1_V2_CONFIG1_INT_MSK
;
290 tmp
|= NFC_V1_V2_CONFIG1_INT_MSK
;
292 writew(tmp
, NFC_V1_V2_CONFIG1
);
295 static void irq_control_v3(struct mxc_nand_host
*host
, int activate
)
299 tmp
= readl(NFC_V3_CONFIG2
);
302 tmp
&= ~NFC_V3_CONFIG2_INT_MSK
;
304 tmp
|= NFC_V3_CONFIG2_INT_MSK
;
306 writel(tmp
, NFC_V3_CONFIG2
);
309 static void irq_control(struct mxc_nand_host
*host
, int activate
)
311 if (host
->devtype_data
->irqpending_quirk
) {
313 enable_irq(host
->irq
);
315 disable_irq_nosync(host
->irq
);
317 host
->devtype_data
->irq_control(host
, activate
);
321 static u32
get_ecc_status_v1(struct mxc_nand_host
*host
)
323 return readw(NFC_V1_V2_ECC_STATUS_RESULT
);
326 static u32
get_ecc_status_v2(struct mxc_nand_host
*host
)
328 return readl(NFC_V1_V2_ECC_STATUS_RESULT
);
331 static u32
get_ecc_status_v3(struct mxc_nand_host
*host
)
333 return readl(NFC_V3_ECC_STATUS_RESULT
);
336 static irqreturn_t
mxc_nfc_irq(int irq
, void *dev_id
)
338 struct mxc_nand_host
*host
= dev_id
;
340 if (!host
->devtype_data
->check_int(host
))
343 irq_control(host
, 0);
345 complete(&host
->op_completion
);
350 /* This function polls the NANDFC to wait for the basic operation to
351 * complete by checking the INT bit of config2 register.
353 static int wait_op_done(struct mxc_nand_host
*host
, int useirq
)
358 * If operation is already complete, don't bother to setup an irq or a
361 if (host
->devtype_data
->check_int(host
))
365 unsigned long timeout
;
367 reinit_completion(&host
->op_completion
);
369 irq_control(host
, 1);
371 timeout
= wait_for_completion_timeout(&host
->op_completion
, HZ
);
372 if (!timeout
&& !host
->devtype_data
->check_int(host
)) {
373 dev_dbg(host
->dev
, "timeout waiting for irq\n");
377 int max_retries
= 8000;
383 done
= host
->devtype_data
->check_int(host
);
387 } while (--max_retries
);
390 dev_dbg(host
->dev
, "timeout polling for completion\n");
395 WARN_ONCE(ret
< 0, "timeout! useirq=%d\n", useirq
);
400 static void send_cmd_v3(struct mxc_nand_host
*host
, uint16_t cmd
, int useirq
)
403 writel(cmd
, NFC_V3_FLASH_CMD
);
405 /* send out command */
406 writel(NFC_CMD
, NFC_V3_LAUNCH
);
408 /* Wait for operation to complete */
409 wait_op_done(host
, useirq
);
412 /* This function issues the specified command to the NAND device and
413 * waits for completion. */
414 static void send_cmd_v1_v2(struct mxc_nand_host
*host
, uint16_t cmd
, int useirq
)
416 pr_debug("send_cmd(host, 0x%x, %d)\n", cmd
, useirq
);
418 writew(cmd
, NFC_V1_V2_FLASH_CMD
);
419 writew(NFC_CMD
, NFC_V1_V2_CONFIG2
);
421 if (host
->devtype_data
->irqpending_quirk
&& (cmd
== NAND_CMD_RESET
)) {
422 int max_retries
= 100;
423 /* Reset completion is indicated by NFC_CONFIG2 */
425 while (max_retries
-- > 0) {
426 if (readw(NFC_V1_V2_CONFIG2
) == 0) {
432 pr_debug("%s: RESET failed\n", __func__
);
434 /* Wait for operation to complete */
435 wait_op_done(host
, useirq
);
439 static void send_addr_v3(struct mxc_nand_host
*host
, uint16_t addr
, int islast
)
442 writel(addr
, NFC_V3_FLASH_ADDR0
);
444 /* send out address */
445 writel(NFC_ADDR
, NFC_V3_LAUNCH
);
447 wait_op_done(host
, 0);
450 /* This function sends an address (or partial address) to the
451 * NAND device. The address is used to select the source/destination for
453 static void send_addr_v1_v2(struct mxc_nand_host
*host
, uint16_t addr
, int islast
)
455 pr_debug("send_addr(host, 0x%x %d)\n", addr
, islast
);
457 writew(addr
, NFC_V1_V2_FLASH_ADDR
);
458 writew(NFC_ADDR
, NFC_V1_V2_CONFIG2
);
460 /* Wait for operation to complete */
461 wait_op_done(host
, islast
);
464 static void send_page_v3(struct mtd_info
*mtd
, unsigned int ops
)
466 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
467 struct mxc_nand_host
*host
= nand_get_controller_data(nand_chip
);
470 tmp
= readl(NFC_V3_CONFIG1
);
472 writel(tmp
, NFC_V3_CONFIG1
);
474 /* transfer data from NFC ram to nand */
475 writel(ops
, NFC_V3_LAUNCH
);
477 wait_op_done(host
, false);
480 static void send_page_v2(struct mtd_info
*mtd
, unsigned int ops
)
482 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
483 struct mxc_nand_host
*host
= nand_get_controller_data(nand_chip
);
485 /* NANDFC buffer 0 is used for page read/write */
486 writew(host
->active_cs
<< 4, NFC_V1_V2_BUF_ADDR
);
488 writew(ops
, NFC_V1_V2_CONFIG2
);
490 /* Wait for operation to complete */
491 wait_op_done(host
, true);
494 static void send_page_v1(struct mtd_info
*mtd
, unsigned int ops
)
496 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
497 struct mxc_nand_host
*host
= nand_get_controller_data(nand_chip
);
500 if (mtd
->writesize
> 512)
505 for (i
= 0; i
< bufs
; i
++) {
507 /* NANDFC buffer 0 is used for page read/write */
508 writew((host
->active_cs
<< 4) | i
, NFC_V1_V2_BUF_ADDR
);
510 writew(ops
, NFC_V1_V2_CONFIG2
);
512 /* Wait for operation to complete */
513 wait_op_done(host
, true);
517 static void send_read_id_v3(struct mxc_nand_host
*host
)
519 /* Read ID into main buffer */
520 writel(NFC_ID
, NFC_V3_LAUNCH
);
522 wait_op_done(host
, true);
524 memcpy32_fromio(host
->data_buf
, host
->main_area0
, 16);
527 /* Request the NANDFC to perform a read of the NAND device ID. */
528 static void send_read_id_v1_v2(struct mxc_nand_host
*host
)
530 /* NANDFC buffer 0 is used for device ID output */
531 writew(host
->active_cs
<< 4, NFC_V1_V2_BUF_ADDR
);
533 writew(NFC_ID
, NFC_V1_V2_CONFIG2
);
535 /* Wait for operation to complete */
536 wait_op_done(host
, true);
538 memcpy32_fromio(host
->data_buf
, host
->main_area0
, 16);
541 static uint16_t get_dev_status_v3(struct mxc_nand_host
*host
)
543 writew(NFC_STATUS
, NFC_V3_LAUNCH
);
544 wait_op_done(host
, true);
546 return readl(NFC_V3_CONFIG1
) >> 16;
549 /* This function requests the NANDFC to perform a read of the
550 * NAND device status and returns the current status. */
551 static uint16_t get_dev_status_v1_v2(struct mxc_nand_host
*host
)
553 void __iomem
*main_buf
= host
->main_area0
;
557 writew(host
->active_cs
<< 4, NFC_V1_V2_BUF_ADDR
);
560 * The device status is stored in main_area0. To
561 * prevent corruption of the buffer save the value
562 * and restore it afterwards.
564 store
= readl(main_buf
);
566 writew(NFC_STATUS
, NFC_V1_V2_CONFIG2
);
567 wait_op_done(host
, true);
569 ret
= readw(main_buf
);
571 writel(store
, main_buf
);
576 /* This functions is used by upper layer to checks if device is ready */
577 static int mxc_nand_dev_ready(struct mtd_info
*mtd
)
580 * NFC handles R/B internally. Therefore, this function
581 * always returns status as ready.
586 static void mxc_nand_enable_hwecc(struct mtd_info
*mtd
, int mode
)
589 * If HW ECC is enabled, we turn it on during init. There is
590 * no need to enable again here.
594 static int mxc_nand_correct_data_v1(struct mtd_info
*mtd
, u_char
*dat
,
595 u_char
*read_ecc
, u_char
*calc_ecc
)
597 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
598 struct mxc_nand_host
*host
= nand_get_controller_data(nand_chip
);
601 * 1-Bit errors are automatically corrected in HW. No need for
602 * additional correction. 2-Bit errors cannot be corrected by
603 * HW ECC, so we need to return failure
605 uint16_t ecc_status
= get_ecc_status_v1(host
);
607 if (((ecc_status
& 0x3) == 2) || ((ecc_status
>> 2) == 2)) {
608 pr_debug("MXC_NAND: HWECC uncorrectable 2-bit ECC error\n");
615 static int mxc_nand_correct_data_v2_v3(struct mtd_info
*mtd
, u_char
*dat
,
616 u_char
*read_ecc
, u_char
*calc_ecc
)
618 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
619 struct mxc_nand_host
*host
= nand_get_controller_data(nand_chip
);
623 u8 ecc_bit_mask
, err_limit
;
625 ecc_bit_mask
= (host
->eccsize
== 4) ? 0x7 : 0xf;
626 err_limit
= (host
->eccsize
== 4) ? 0x4 : 0x8;
628 no_subpages
= mtd
->writesize
>> 9;
630 ecc_stat
= host
->devtype_data
->get_ecc_status(host
);
633 err
= ecc_stat
& ecc_bit_mask
;
634 if (err
> err_limit
) {
635 printk(KERN_WARNING
"UnCorrectable RS-ECC Error\n");
641 } while (--no_subpages
);
643 pr_debug("%d Symbol Correctable RS-ECC Error\n", ret
);
648 static int mxc_nand_calculate_ecc(struct mtd_info
*mtd
, const u_char
*dat
,
654 static u_char
mxc_nand_read_byte(struct mtd_info
*mtd
)
656 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
657 struct mxc_nand_host
*host
= nand_get_controller_data(nand_chip
);
660 /* Check for status request */
661 if (host
->status_request
)
662 return host
->devtype_data
->get_dev_status(host
) & 0xFF;
664 if (nand_chip
->options
& NAND_BUSWIDTH_16
) {
665 /* only take the lower byte of each word */
666 ret
= *(uint16_t *)(host
->data_buf
+ host
->buf_start
);
668 host
->buf_start
+= 2;
670 ret
= *(uint8_t *)(host
->data_buf
+ host
->buf_start
);
674 pr_debug("%s: ret=0x%hhx (start=%u)\n", __func__
, ret
, host
->buf_start
);
678 static uint16_t mxc_nand_read_word(struct mtd_info
*mtd
)
680 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
681 struct mxc_nand_host
*host
= nand_get_controller_data(nand_chip
);
684 ret
= *(uint16_t *)(host
->data_buf
+ host
->buf_start
);
685 host
->buf_start
+= 2;
690 /* Write data of length len to buffer buf. The data to be
691 * written on NAND Flash is first copied to RAMbuffer. After the Data Input
692 * Operation by the NFC, the data is written to NAND Flash */
693 static void mxc_nand_write_buf(struct mtd_info
*mtd
,
694 const u_char
*buf
, int len
)
696 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
697 struct mxc_nand_host
*host
= nand_get_controller_data(nand_chip
);
698 u16 col
= host
->buf_start
;
699 int n
= mtd
->oobsize
+ mtd
->writesize
- col
;
703 memcpy(host
->data_buf
+ col
, buf
, n
);
705 host
->buf_start
+= n
;
708 /* Read the data buffer from the NAND Flash. To read the data from NAND
709 * Flash first the data output cycle is initiated by the NFC, which copies
710 * the data to RAMbuffer. This data of length len is then copied to buffer buf.
712 static void mxc_nand_read_buf(struct mtd_info
*mtd
, u_char
*buf
, int len
)
714 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
715 struct mxc_nand_host
*host
= nand_get_controller_data(nand_chip
);
716 u16 col
= host
->buf_start
;
717 int n
= mtd
->oobsize
+ mtd
->writesize
- col
;
721 memcpy(buf
, host
->data_buf
+ col
, n
);
723 host
->buf_start
+= n
;
726 /* This function is used by upper layer for select and
727 * deselect of the NAND chip */
728 static void mxc_nand_select_chip_v1_v3(struct mtd_info
*mtd
, int chip
)
730 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
731 struct mxc_nand_host
*host
= nand_get_controller_data(nand_chip
);
734 /* Disable the NFC clock */
736 clk_disable_unprepare(host
->clk
);
742 if (!host
->clk_act
) {
743 /* Enable the NFC clock */
744 clk_prepare_enable(host
->clk
);
749 static void mxc_nand_select_chip_v2(struct mtd_info
*mtd
, int chip
)
751 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
752 struct mxc_nand_host
*host
= nand_get_controller_data(nand_chip
);
755 /* Disable the NFC clock */
757 clk_disable_unprepare(host
->clk
);
763 if (!host
->clk_act
) {
764 /* Enable the NFC clock */
765 clk_prepare_enable(host
->clk
);
769 host
->active_cs
= chip
;
770 writew(host
->active_cs
<< 4, NFC_V1_V2_BUF_ADDR
);
774 * The controller splits a page into data chunks of 512 bytes + partial oob.
775 * There are writesize / 512 such chunks, the size of the partial oob parts is
776 * oobsize / #chunks rounded down to a multiple of 2. The last oob chunk then
777 * contains additionally the byte lost by rounding (if any).
778 * This function handles the needed shuffling between host->data_buf (which
779 * holds a page in natural order, i.e. writesize bytes data + oobsize bytes
780 * spare) and the NFC buffer.
782 static void copy_spare(struct mtd_info
*mtd
, bool bfrom
)
784 struct nand_chip
*this = mtd_to_nand(mtd
);
785 struct mxc_nand_host
*host
= nand_get_controller_data(this);
786 u16 i
, oob_chunk_size
;
787 u16 num_chunks
= mtd
->writesize
/ 512;
789 u8
*d
= host
->data_buf
+ mtd
->writesize
;
790 u8 __iomem
*s
= host
->spare0
;
791 u16 sparebuf_size
= host
->devtype_data
->spare_len
;
793 /* size of oob chunk for all but possibly the last one */
794 oob_chunk_size
= (host
->used_oobsize
/ num_chunks
) & ~1;
797 for (i
= 0; i
< num_chunks
- 1; i
++)
798 memcpy16_fromio(d
+ i
* oob_chunk_size
,
799 s
+ i
* sparebuf_size
,
803 memcpy16_fromio(d
+ i
* oob_chunk_size
,
804 s
+ i
* sparebuf_size
,
805 host
->used_oobsize
- i
* oob_chunk_size
);
807 for (i
= 0; i
< num_chunks
- 1; i
++)
808 memcpy16_toio(&s
[i
* sparebuf_size
],
809 &d
[i
* oob_chunk_size
],
813 memcpy16_toio(&s
[i
* sparebuf_size
],
814 &d
[i
* oob_chunk_size
],
815 host
->used_oobsize
- i
* oob_chunk_size
);
820 * MXC NANDFC can only perform full page+spare or spare-only read/write. When
821 * the upper layers perform a read/write buf operation, the saved column address
822 * is used to index into the full page. So usually this function is called with
823 * column == 0 (unless no column cycle is needed indicated by column == -1)
825 static void mxc_do_addr_cycle(struct mtd_info
*mtd
, int column
, int page_addr
)
827 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
828 struct mxc_nand_host
*host
= nand_get_controller_data(nand_chip
);
830 /* Write out column address, if necessary */
832 host
->devtype_data
->send_addr(host
, column
& 0xff,
834 if (mtd
->writesize
> 512)
835 /* another col addr cycle for 2k page */
836 host
->devtype_data
->send_addr(host
,
837 (column
>> 8) & 0xff,
841 /* Write out page address, if necessary */
842 if (page_addr
!= -1) {
843 /* paddr_0 - p_addr_7 */
844 host
->devtype_data
->send_addr(host
, (page_addr
& 0xff), false);
846 if (mtd
->writesize
> 512) {
847 if (mtd
->size
>= 0x10000000) {
848 /* paddr_8 - paddr_15 */
849 host
->devtype_data
->send_addr(host
,
850 (page_addr
>> 8) & 0xff,
852 host
->devtype_data
->send_addr(host
,
853 (page_addr
>> 16) & 0xff,
856 /* paddr_8 - paddr_15 */
857 host
->devtype_data
->send_addr(host
,
858 (page_addr
>> 8) & 0xff, true);
860 /* One more address cycle for higher density devices */
861 if (mtd
->size
>= 0x4000000) {
862 /* paddr_8 - paddr_15 */
863 host
->devtype_data
->send_addr(host
,
864 (page_addr
>> 8) & 0xff,
866 host
->devtype_data
->send_addr(host
,
867 (page_addr
>> 16) & 0xff,
870 /* paddr_8 - paddr_15 */
871 host
->devtype_data
->send_addr(host
,
872 (page_addr
>> 8) & 0xff, true);
877 static int mxc_v1_ooblayout_ecc(struct mtd_info
*mtd
, int section
,
878 struct mtd_oob_region
*oobregion
)
880 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
882 if (section
>= nand_chip
->ecc
.steps
)
885 oobregion
->offset
= (section
* 16) + 6;
886 oobregion
->length
= nand_chip
->ecc
.bytes
;
891 static int mxc_v1_ooblayout_free(struct mtd_info
*mtd
, int section
,
892 struct mtd_oob_region
*oobregion
)
894 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
896 if (section
> nand_chip
->ecc
.steps
)
900 if (mtd
->writesize
<= 512) {
901 oobregion
->offset
= 0;
902 oobregion
->length
= 5;
904 oobregion
->offset
= 2;
905 oobregion
->length
= 4;
908 oobregion
->offset
= ((section
- 1) * 16) +
909 nand_chip
->ecc
.bytes
+ 6;
910 if (section
< nand_chip
->ecc
.steps
)
911 oobregion
->length
= (section
* 16) + 6 -
914 oobregion
->length
= mtd
->oobsize
- oobregion
->offset
;
920 static const struct mtd_ooblayout_ops mxc_v1_ooblayout_ops
= {
921 .ecc
= mxc_v1_ooblayout_ecc
,
922 .free
= mxc_v1_ooblayout_free
,
925 static int mxc_v2_ooblayout_ecc(struct mtd_info
*mtd
, int section
,
926 struct mtd_oob_region
*oobregion
)
928 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
929 int stepsize
= nand_chip
->ecc
.bytes
== 9 ? 16 : 26;
931 if (section
>= nand_chip
->ecc
.steps
)
934 oobregion
->offset
= (section
* stepsize
) + 7;
935 oobregion
->length
= nand_chip
->ecc
.bytes
;
940 static int mxc_v2_ooblayout_free(struct mtd_info
*mtd
, int section
,
941 struct mtd_oob_region
*oobregion
)
943 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
944 int stepsize
= nand_chip
->ecc
.bytes
== 9 ? 16 : 26;
946 if (section
> nand_chip
->ecc
.steps
)
950 if (mtd
->writesize
<= 512) {
951 oobregion
->offset
= 0;
952 oobregion
->length
= 5;
954 oobregion
->offset
= 2;
955 oobregion
->length
= 4;
958 oobregion
->offset
= section
* stepsize
;
959 oobregion
->length
= 7;
965 static const struct mtd_ooblayout_ops mxc_v2_ooblayout_ops
= {
966 .ecc
= mxc_v2_ooblayout_ecc
,
967 .free
= mxc_v2_ooblayout_free
,
971 * v2 and v3 type controllers can do 4bit or 8bit ecc depending
972 * on how much oob the nand chip has. For 8bit ecc we need at least
973 * 26 bytes of oob data per 512 byte block.
975 static int get_eccsize(struct mtd_info
*mtd
)
977 int oobbytes_per_512
= 0;
979 oobbytes_per_512
= mtd
->oobsize
* 512 / mtd
->writesize
;
981 if (oobbytes_per_512
< 26)
987 static void preset_v1(struct mtd_info
*mtd
)
989 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
990 struct mxc_nand_host
*host
= nand_get_controller_data(nand_chip
);
991 uint16_t config1
= 0;
993 if (nand_chip
->ecc
.mode
== NAND_ECC_HW
&& mtd
->writesize
)
994 config1
|= NFC_V1_V2_CONFIG1_ECC_EN
;
996 if (!host
->devtype_data
->irqpending_quirk
)
997 config1
|= NFC_V1_V2_CONFIG1_INT_MSK
;
1001 writew(config1
, NFC_V1_V2_CONFIG1
);
1002 /* preset operation */
1004 /* Unlock the internal RAM Buffer */
1005 writew(0x2, NFC_V1_V2_CONFIG
);
1007 /* Blocks to be unlocked */
1008 writew(0x0, NFC_V1_UNLOCKSTART_BLKADDR
);
1009 writew(0xffff, NFC_V1_UNLOCKEND_BLKADDR
);
1011 /* Unlock Block Command for given address range */
1012 writew(0x4, NFC_V1_V2_WRPROT
);
1015 static void preset_v2(struct mtd_info
*mtd
)
1017 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
1018 struct mxc_nand_host
*host
= nand_get_controller_data(nand_chip
);
1019 uint16_t config1
= 0;
1021 config1
|= NFC_V2_CONFIG1_FP_INT
;
1023 if (!host
->devtype_data
->irqpending_quirk
)
1024 config1
|= NFC_V1_V2_CONFIG1_INT_MSK
;
1026 if (mtd
->writesize
) {
1027 uint16_t pages_per_block
= mtd
->erasesize
/ mtd
->writesize
;
1029 if (nand_chip
->ecc
.mode
== NAND_ECC_HW
)
1030 config1
|= NFC_V1_V2_CONFIG1_ECC_EN
;
1032 host
->eccsize
= get_eccsize(mtd
);
1033 if (host
->eccsize
== 4)
1034 config1
|= NFC_V2_CONFIG1_ECC_MODE_4
;
1036 config1
|= NFC_V2_CONFIG1_PPB(ffs(pages_per_block
) - 6);
1041 writew(config1
, NFC_V1_V2_CONFIG1
);
1042 /* preset operation */
1044 /* Unlock the internal RAM Buffer */
1045 writew(0x2, NFC_V1_V2_CONFIG
);
1047 /* Blocks to be unlocked */
1048 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR0
);
1049 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR1
);
1050 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR2
);
1051 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR3
);
1052 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR0
);
1053 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR1
);
1054 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR2
);
1055 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR3
);
1057 /* Unlock Block Command for given address range */
1058 writew(0x4, NFC_V1_V2_WRPROT
);
1061 static void preset_v3(struct mtd_info
*mtd
)
1063 struct nand_chip
*chip
= mtd_to_nand(mtd
);
1064 struct mxc_nand_host
*host
= nand_get_controller_data(chip
);
1065 uint32_t config2
, config3
;
1068 writel(NFC_V3_CONFIG1_RBA(0), NFC_V3_CONFIG1
);
1069 writel(NFC_V3_IPC_CREQ
, NFC_V3_IPC
);
1071 /* Unlock the internal RAM Buffer */
1072 writel(NFC_V3_WRPROT_BLS_UNLOCK
| NFC_V3_WRPROT_UNLOCK
,
1075 /* Blocks to be unlocked */
1076 for (i
= 0; i
< NAND_MAX_CHIPS
; i
++)
1077 writel(0xffff << 16, NFC_V3_WRPROT_UNLOCK_BLK_ADD0
+ (i
<< 2));
1079 writel(0, NFC_V3_IPC
);
1081 config2
= NFC_V3_CONFIG2_ONE_CYCLE
|
1082 NFC_V3_CONFIG2_2CMD_PHASES
|
1083 NFC_V3_CONFIG2_SPAS(mtd
->oobsize
>> 1) |
1084 NFC_V3_CONFIG2_ST_CMD(0x70) |
1085 NFC_V3_CONFIG2_INT_MSK
|
1086 NFC_V3_CONFIG2_NUM_ADDR_PHASE0
;
1088 addr_phases
= fls(chip
->pagemask
) >> 3;
1090 if (mtd
->writesize
== 2048) {
1091 config2
|= NFC_V3_CONFIG2_PS_2048
;
1092 config2
|= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases
);
1093 } else if (mtd
->writesize
== 4096) {
1094 config2
|= NFC_V3_CONFIG2_PS_4096
;
1095 config2
|= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases
);
1097 config2
|= NFC_V3_CONFIG2_PS_512
;
1098 config2
|= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases
- 1);
1101 if (mtd
->writesize
) {
1102 if (chip
->ecc
.mode
== NAND_ECC_HW
)
1103 config2
|= NFC_V3_CONFIG2_ECC_EN
;
1105 config2
|= NFC_V3_CONFIG2_PPB(
1106 ffs(mtd
->erasesize
/ mtd
->writesize
) - 6,
1107 host
->devtype_data
->ppb_shift
);
1108 host
->eccsize
= get_eccsize(mtd
);
1109 if (host
->eccsize
== 8)
1110 config2
|= NFC_V3_CONFIG2_ECC_MODE_8
;
1113 writel(config2
, NFC_V3_CONFIG2
);
1115 config3
= NFC_V3_CONFIG3_NUM_OF_DEVICES(0) |
1116 NFC_V3_CONFIG3_NO_SDMA
|
1117 NFC_V3_CONFIG3_RBB_MODE
|
1118 NFC_V3_CONFIG3_SBB(6) | /* Reset default */
1119 NFC_V3_CONFIG3_ADD_OP(0);
1121 if (!(chip
->options
& NAND_BUSWIDTH_16
))
1122 config3
|= NFC_V3_CONFIG3_FW8
;
1124 writel(config3
, NFC_V3_CONFIG3
);
1126 writel(0, NFC_V3_DELAY_LINE
);
1129 /* Used by the upper layer to write command to NAND Flash for
1130 * different operations to be carried out on NAND Flash */
1131 static void mxc_nand_command(struct mtd_info
*mtd
, unsigned command
,
1132 int column
, int page_addr
)
1134 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
1135 struct mxc_nand_host
*host
= nand_get_controller_data(nand_chip
);
1137 pr_debug("mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
1138 command
, column
, page_addr
);
1140 /* Reset command state information */
1141 host
->status_request
= false;
1143 /* Command pre-processing step */
1145 case NAND_CMD_RESET
:
1146 host
->devtype_data
->preset(mtd
);
1147 host
->devtype_data
->send_cmd(host
, command
, false);
1150 case NAND_CMD_STATUS
:
1151 host
->buf_start
= 0;
1152 host
->status_request
= true;
1154 host
->devtype_data
->send_cmd(host
, command
, true);
1155 WARN_ONCE(column
!= -1 || page_addr
!= -1,
1156 "Unexpected column/row value (cmd=%u, col=%d, row=%d)\n",
1157 command
, column
, page_addr
);
1158 mxc_do_addr_cycle(mtd
, column
, page_addr
);
1161 case NAND_CMD_READ0
:
1162 case NAND_CMD_READOOB
:
1163 if (command
== NAND_CMD_READ0
)
1164 host
->buf_start
= column
;
1166 host
->buf_start
= column
+ mtd
->writesize
;
1168 command
= NAND_CMD_READ0
; /* only READ0 is valid */
1170 host
->devtype_data
->send_cmd(host
, command
, false);
1171 WARN_ONCE(column
< 0,
1172 "Unexpected column/row value (cmd=%u, col=%d, row=%d)\n",
1173 command
, column
, page_addr
);
1174 mxc_do_addr_cycle(mtd
, 0, page_addr
);
1176 if (mtd
->writesize
> 512)
1177 host
->devtype_data
->send_cmd(host
,
1178 NAND_CMD_READSTART
, true);
1180 host
->devtype_data
->send_page(mtd
, NFC_OUTPUT
);
1182 memcpy32_fromio(host
->data_buf
, host
->main_area0
,
1184 copy_spare(mtd
, true);
1187 case NAND_CMD_SEQIN
:
1188 if (column
>= mtd
->writesize
)
1189 /* call ourself to read a page */
1190 mxc_nand_command(mtd
, NAND_CMD_READ0
, 0, page_addr
);
1192 host
->buf_start
= column
;
1194 host
->devtype_data
->send_cmd(host
, command
, false);
1195 WARN_ONCE(column
< -1,
1196 "Unexpected column/row value (cmd=%u, col=%d, row=%d)\n",
1197 command
, column
, page_addr
);
1198 mxc_do_addr_cycle(mtd
, 0, page_addr
);
1201 case NAND_CMD_PAGEPROG
:
1202 memcpy32_toio(host
->main_area0
, host
->data_buf
, mtd
->writesize
);
1203 copy_spare(mtd
, false);
1204 host
->devtype_data
->send_page(mtd
, NFC_INPUT
);
1205 host
->devtype_data
->send_cmd(host
, command
, true);
1206 WARN_ONCE(column
!= -1 || page_addr
!= -1,
1207 "Unexpected column/row value (cmd=%u, col=%d, row=%d)\n",
1208 command
, column
, page_addr
);
1209 mxc_do_addr_cycle(mtd
, column
, page_addr
);
1212 case NAND_CMD_READID
:
1213 host
->devtype_data
->send_cmd(host
, command
, true);
1214 mxc_do_addr_cycle(mtd
, column
, page_addr
);
1215 host
->devtype_data
->send_read_id(host
);
1216 host
->buf_start
= 0;
1219 case NAND_CMD_ERASE1
:
1220 case NAND_CMD_ERASE2
:
1221 host
->devtype_data
->send_cmd(host
, command
, false);
1222 WARN_ONCE(column
!= -1,
1223 "Unexpected column value (cmd=%u, col=%d)\n",
1225 mxc_do_addr_cycle(mtd
, column
, page_addr
);
1228 case NAND_CMD_PARAM
:
1229 host
->devtype_data
->send_cmd(host
, command
, false);
1230 mxc_do_addr_cycle(mtd
, column
, page_addr
);
1231 host
->devtype_data
->send_page(mtd
, NFC_OUTPUT
);
1232 memcpy32_fromio(host
->data_buf
, host
->main_area0
, 512);
1233 host
->buf_start
= 0;
1236 WARN_ONCE(1, "Unimplemented command (cmd=%u)\n",
1243 * The generic flash bbt decriptors overlap with our ecc
1244 * hardware, so define some i.MX specific ones.
1246 static uint8_t bbt_pattern
[] = { 'B', 'b', 't', '0' };
1247 static uint8_t mirror_pattern
[] = { '1', 't', 'b', 'B' };
1249 static struct nand_bbt_descr bbt_main_descr
= {
1250 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
1251 | NAND_BBT_2BIT
| NAND_BBT_VERSION
| NAND_BBT_PERCHIP
,
1256 .pattern
= bbt_pattern
,
1259 static struct nand_bbt_descr bbt_mirror_descr
= {
1260 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
1261 | NAND_BBT_2BIT
| NAND_BBT_VERSION
| NAND_BBT_PERCHIP
,
1266 .pattern
= mirror_pattern
,
1269 /* v1 + irqpending_quirk: i.MX21 */
1270 static const struct mxc_nand_devtype_data imx21_nand_devtype_data
= {
1271 .preset
= preset_v1
,
1272 .send_cmd
= send_cmd_v1_v2
,
1273 .send_addr
= send_addr_v1_v2
,
1274 .send_page
= send_page_v1
,
1275 .send_read_id
= send_read_id_v1_v2
,
1276 .get_dev_status
= get_dev_status_v1_v2
,
1277 .check_int
= check_int_v1_v2
,
1278 .irq_control
= irq_control_v1_v2
,
1279 .get_ecc_status
= get_ecc_status_v1
,
1280 .ooblayout
= &mxc_v1_ooblayout_ops
,
1281 .select_chip
= mxc_nand_select_chip_v1_v3
,
1282 .correct_data
= mxc_nand_correct_data_v1
,
1283 .irqpending_quirk
= 1,
1285 .regs_offset
= 0xe00,
1286 .spare0_offset
= 0x800,
1292 /* v1 + !irqpending_quirk: i.MX27, i.MX31 */
1293 static const struct mxc_nand_devtype_data imx27_nand_devtype_data
= {
1294 .preset
= preset_v1
,
1295 .send_cmd
= send_cmd_v1_v2
,
1296 .send_addr
= send_addr_v1_v2
,
1297 .send_page
= send_page_v1
,
1298 .send_read_id
= send_read_id_v1_v2
,
1299 .get_dev_status
= get_dev_status_v1_v2
,
1300 .check_int
= check_int_v1_v2
,
1301 .irq_control
= irq_control_v1_v2
,
1302 .get_ecc_status
= get_ecc_status_v1
,
1303 .ooblayout
= &mxc_v1_ooblayout_ops
,
1304 .select_chip
= mxc_nand_select_chip_v1_v3
,
1305 .correct_data
= mxc_nand_correct_data_v1
,
1306 .irqpending_quirk
= 0,
1308 .regs_offset
= 0xe00,
1309 .spare0_offset
= 0x800,
1316 /* v21: i.MX25, i.MX35 */
1317 static const struct mxc_nand_devtype_data imx25_nand_devtype_data
= {
1318 .preset
= preset_v2
,
1319 .send_cmd
= send_cmd_v1_v2
,
1320 .send_addr
= send_addr_v1_v2
,
1321 .send_page
= send_page_v2
,
1322 .send_read_id
= send_read_id_v1_v2
,
1323 .get_dev_status
= get_dev_status_v1_v2
,
1324 .check_int
= check_int_v1_v2
,
1325 .irq_control
= irq_control_v1_v2
,
1326 .get_ecc_status
= get_ecc_status_v2
,
1327 .ooblayout
= &mxc_v2_ooblayout_ops
,
1328 .select_chip
= mxc_nand_select_chip_v2
,
1329 .correct_data
= mxc_nand_correct_data_v2_v3
,
1330 .irqpending_quirk
= 0,
1332 .regs_offset
= 0x1e00,
1333 .spare0_offset
= 0x1000,
1341 static const struct mxc_nand_devtype_data imx51_nand_devtype_data
= {
1342 .preset
= preset_v3
,
1343 .send_cmd
= send_cmd_v3
,
1344 .send_addr
= send_addr_v3
,
1345 .send_page
= send_page_v3
,
1346 .send_read_id
= send_read_id_v3
,
1347 .get_dev_status
= get_dev_status_v3
,
1348 .check_int
= check_int_v3
,
1349 .irq_control
= irq_control_v3
,
1350 .get_ecc_status
= get_ecc_status_v3
,
1351 .ooblayout
= &mxc_v2_ooblayout_ops
,
1352 .select_chip
= mxc_nand_select_chip_v1_v3
,
1353 .correct_data
= mxc_nand_correct_data_v2_v3
,
1354 .irqpending_quirk
= 0,
1357 .spare0_offset
= 0x1000,
1358 .axi_offset
= 0x1e00,
1366 static const struct mxc_nand_devtype_data imx53_nand_devtype_data
= {
1367 .preset
= preset_v3
,
1368 .send_cmd
= send_cmd_v3
,
1369 .send_addr
= send_addr_v3
,
1370 .send_page
= send_page_v3
,
1371 .send_read_id
= send_read_id_v3
,
1372 .get_dev_status
= get_dev_status_v3
,
1373 .check_int
= check_int_v3
,
1374 .irq_control
= irq_control_v3
,
1375 .get_ecc_status
= get_ecc_status_v3
,
1376 .ooblayout
= &mxc_v2_ooblayout_ops
,
1377 .select_chip
= mxc_nand_select_chip_v1_v3
,
1378 .correct_data
= mxc_nand_correct_data_v2_v3
,
1379 .irqpending_quirk
= 0,
1382 .spare0_offset
= 0x1000,
1383 .axi_offset
= 0x1e00,
1390 static inline int is_imx21_nfc(struct mxc_nand_host
*host
)
1392 return host
->devtype_data
== &imx21_nand_devtype_data
;
1395 static inline int is_imx27_nfc(struct mxc_nand_host
*host
)
1397 return host
->devtype_data
== &imx27_nand_devtype_data
;
1400 static inline int is_imx25_nfc(struct mxc_nand_host
*host
)
1402 return host
->devtype_data
== &imx25_nand_devtype_data
;
1405 static inline int is_imx51_nfc(struct mxc_nand_host
*host
)
1407 return host
->devtype_data
== &imx51_nand_devtype_data
;
1410 static inline int is_imx53_nfc(struct mxc_nand_host
*host
)
1412 return host
->devtype_data
== &imx53_nand_devtype_data
;
1415 static const struct platform_device_id mxcnd_devtype
[] = {
1417 .name
= "imx21-nand",
1418 .driver_data
= (kernel_ulong_t
) &imx21_nand_devtype_data
,
1420 .name
= "imx27-nand",
1421 .driver_data
= (kernel_ulong_t
) &imx27_nand_devtype_data
,
1423 .name
= "imx25-nand",
1424 .driver_data
= (kernel_ulong_t
) &imx25_nand_devtype_data
,
1426 .name
= "imx51-nand",
1427 .driver_data
= (kernel_ulong_t
) &imx51_nand_devtype_data
,
1429 .name
= "imx53-nand",
1430 .driver_data
= (kernel_ulong_t
) &imx53_nand_devtype_data
,
1435 MODULE_DEVICE_TABLE(platform
, mxcnd_devtype
);
1437 #ifdef CONFIG_OF_MTD
1438 static const struct of_device_id mxcnd_dt_ids
[] = {
1440 .compatible
= "fsl,imx21-nand",
1441 .data
= &imx21_nand_devtype_data
,
1443 .compatible
= "fsl,imx27-nand",
1444 .data
= &imx27_nand_devtype_data
,
1446 .compatible
= "fsl,imx25-nand",
1447 .data
= &imx25_nand_devtype_data
,
1449 .compatible
= "fsl,imx51-nand",
1450 .data
= &imx51_nand_devtype_data
,
1452 .compatible
= "fsl,imx53-nand",
1453 .data
= &imx53_nand_devtype_data
,
1457 MODULE_DEVICE_TABLE(of
, mxcnd_dt_ids
);
1459 static int __init
mxcnd_probe_dt(struct mxc_nand_host
*host
)
1461 struct device_node
*np
= host
->dev
->of_node
;
1462 const struct of_device_id
*of_id
=
1463 of_match_device(mxcnd_dt_ids
, host
->dev
);
1468 host
->devtype_data
= of_id
->data
;
1473 static int __init
mxcnd_probe_dt(struct mxc_nand_host
*host
)
1479 static int mxcnd_probe(struct platform_device
*pdev
)
1481 struct nand_chip
*this;
1482 struct mtd_info
*mtd
;
1483 struct mxc_nand_host
*host
;
1484 struct resource
*res
;
1487 /* Allocate memory for MTD device structure and private data */
1488 host
= devm_kzalloc(&pdev
->dev
, sizeof(struct mxc_nand_host
),
1493 /* allocate a temporary buffer for the nand_scan_ident() */
1494 host
->data_buf
= devm_kzalloc(&pdev
->dev
, PAGE_SIZE
, GFP_KERNEL
);
1495 if (!host
->data_buf
)
1498 host
->dev
= &pdev
->dev
;
1499 /* structures must be linked */
1501 mtd
= nand_to_mtd(this);
1502 mtd
->dev
.parent
= &pdev
->dev
;
1503 mtd
->name
= DRIVER_NAME
;
1505 /* 50 us command delay time */
1506 this->chip_delay
= 5;
1508 nand_set_controller_data(this, host
);
1509 nand_set_flash_node(this, pdev
->dev
.of_node
),
1510 this->dev_ready
= mxc_nand_dev_ready
;
1511 this->cmdfunc
= mxc_nand_command
;
1512 this->read_byte
= mxc_nand_read_byte
;
1513 this->read_word
= mxc_nand_read_word
;
1514 this->write_buf
= mxc_nand_write_buf
;
1515 this->read_buf
= mxc_nand_read_buf
;
1517 host
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
1518 if (IS_ERR(host
->clk
))
1519 return PTR_ERR(host
->clk
);
1521 err
= mxcnd_probe_dt(host
);
1523 struct mxc_nand_platform_data
*pdata
=
1524 dev_get_platdata(&pdev
->dev
);
1526 host
->pdata
= *pdata
;
1527 host
->devtype_data
= (struct mxc_nand_devtype_data
*)
1528 pdev
->id_entry
->driver_data
;
1536 if (host
->devtype_data
->needs_ip
) {
1537 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1538 host
->regs_ip
= devm_ioremap_resource(&pdev
->dev
, res
);
1539 if (IS_ERR(host
->regs_ip
))
1540 return PTR_ERR(host
->regs_ip
);
1542 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1544 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1547 host
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
1548 if (IS_ERR(host
->base
))
1549 return PTR_ERR(host
->base
);
1551 host
->main_area0
= host
->base
;
1553 if (host
->devtype_data
->regs_offset
)
1554 host
->regs
= host
->base
+ host
->devtype_data
->regs_offset
;
1555 host
->spare0
= host
->base
+ host
->devtype_data
->spare0_offset
;
1556 if (host
->devtype_data
->axi_offset
)
1557 host
->regs_axi
= host
->base
+ host
->devtype_data
->axi_offset
;
1559 this->ecc
.bytes
= host
->devtype_data
->eccbytes
;
1560 host
->eccsize
= host
->devtype_data
->eccsize
;
1562 this->select_chip
= host
->devtype_data
->select_chip
;
1563 this->ecc
.size
= 512;
1564 mtd_set_ooblayout(mtd
, host
->devtype_data
->ooblayout
);
1566 if (host
->pdata
.hw_ecc
) {
1567 this->ecc
.mode
= NAND_ECC_HW
;
1569 this->ecc
.mode
= NAND_ECC_SOFT
;
1570 this->ecc
.algo
= NAND_ECC_HAMMING
;
1573 /* NAND bus width determines access functions used by upper layer */
1574 if (host
->pdata
.width
== 2)
1575 this->options
|= NAND_BUSWIDTH_16
;
1577 /* update flash based bbt */
1578 if (host
->pdata
.flash_bbt
)
1579 this->bbt_options
|= NAND_BBT_USE_FLASH
;
1581 init_completion(&host
->op_completion
);
1583 host
->irq
= platform_get_irq(pdev
, 0);
1588 * Use host->devtype_data->irq_control() here instead of irq_control()
1589 * because we must not disable_irq_nosync without having requested the
1592 host
->devtype_data
->irq_control(host
, 0);
1594 err
= devm_request_irq(&pdev
->dev
, host
->irq
, mxc_nfc_irq
,
1595 0, DRIVER_NAME
, host
);
1599 err
= clk_prepare_enable(host
->clk
);
1605 * Now that we "own" the interrupt make sure the interrupt mask bit is
1606 * cleared on i.MX21. Otherwise we can't read the interrupt status bit
1609 if (host
->devtype_data
->irqpending_quirk
) {
1610 disable_irq_nosync(host
->irq
);
1611 host
->devtype_data
->irq_control(host
, 1);
1614 /* first scan to find the device and get the page size */
1615 if (nand_scan_ident(mtd
, is_imx25_nfc(host
) ? 4 : 1, NULL
)) {
1620 switch (this->ecc
.mode
) {
1622 this->ecc
.calculate
= mxc_nand_calculate_ecc
;
1623 this->ecc
.hwctl
= mxc_nand_enable_hwecc
;
1624 this->ecc
.correct
= host
->devtype_data
->correct_data
;
1635 if (this->bbt_options
& NAND_BBT_USE_FLASH
) {
1636 this->bbt_td
= &bbt_main_descr
;
1637 this->bbt_md
= &bbt_mirror_descr
;
1640 /* allocate the right size buffer now */
1641 devm_kfree(&pdev
->dev
, (void *)host
->data_buf
);
1642 host
->data_buf
= devm_kzalloc(&pdev
->dev
, mtd
->writesize
+ mtd
->oobsize
,
1644 if (!host
->data_buf
) {
1649 /* Call preset again, with correct writesize this time */
1650 host
->devtype_data
->preset(mtd
);
1652 if (!this->ecc
.bytes
) {
1653 if (host
->eccsize
== 8)
1654 this->ecc
.bytes
= 18;
1655 else if (host
->eccsize
== 4)
1656 this->ecc
.bytes
= 9;
1660 * Experimentation shows that i.MX NFC can only handle up to 218 oob
1661 * bytes. Limit used_oobsize to 218 so as to not confuse copy_spare()
1662 * into copying invalid data to/from the spare IO buffer, as this
1663 * might cause ECC data corruption when doing sub-page write to a
1664 * partially written page.
1666 host
->used_oobsize
= min(mtd
->oobsize
, 218U);
1668 if (this->ecc
.mode
== NAND_ECC_HW
) {
1669 if (is_imx21_nfc(host
) || is_imx27_nfc(host
))
1670 this->ecc
.strength
= 1;
1672 this->ecc
.strength
= (host
->eccsize
== 4) ? 4 : 8;
1675 /* second phase scan */
1676 if (nand_scan_tail(mtd
)) {
1681 /* Register the partitions */
1682 mtd_device_parse_register(mtd
, part_probes
,
1685 host
->pdata
.nr_parts
);
1687 platform_set_drvdata(pdev
, host
);
1693 clk_disable_unprepare(host
->clk
);
1698 static int mxcnd_remove(struct platform_device
*pdev
)
1700 struct mxc_nand_host
*host
= platform_get_drvdata(pdev
);
1702 nand_release(nand_to_mtd(&host
->nand
));
1704 clk_disable_unprepare(host
->clk
);
1709 static struct platform_driver mxcnd_driver
= {
1711 .name
= DRIVER_NAME
,
1712 .of_match_table
= of_match_ptr(mxcnd_dt_ids
),
1714 .id_table
= mxcnd_devtype
,
1715 .probe
= mxcnd_probe
,
1716 .remove
= mxcnd_remove
,
1718 module_platform_driver(mxcnd_driver
);
1720 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1721 MODULE_DESCRIPTION("MXC NAND MTD driver");
1722 MODULE_LICENSE("GPL");