1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
4 * Copyright 2008 Sascha Hauer, kernel@pengutronix.de
7 #include <linux/delay.h>
8 #include <linux/slab.h>
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/mtd/mtd.h>
12 #include <linux/mtd/rawnand.h>
13 #include <linux/mtd/partitions.h>
14 #include <linux/interrupt.h>
15 #include <linux/device.h>
16 #include <linux/platform_device.h>
17 #include <linux/clk.h>
18 #include <linux/err.h>
20 #include <linux/irq.h>
21 #include <linux/completion.h>
24 #define DRIVER_NAME "mxc_nand"
26 /* Addresses for NFC registers */
27 #define NFC_V1_V2_BUF_SIZE (host->regs + 0x00)
28 #define NFC_V1_V2_BUF_ADDR (host->regs + 0x04)
29 #define NFC_V1_V2_FLASH_ADDR (host->regs + 0x06)
30 #define NFC_V1_V2_FLASH_CMD (host->regs + 0x08)
31 #define NFC_V1_V2_CONFIG (host->regs + 0x0a)
32 #define NFC_V1_V2_ECC_STATUS_RESULT (host->regs + 0x0c)
33 #define NFC_V1_V2_RSLTMAIN_AREA (host->regs + 0x0e)
34 #define NFC_V21_RSLTSPARE_AREA (host->regs + 0x10)
35 #define NFC_V1_V2_WRPROT (host->regs + 0x12)
36 #define NFC_V1_UNLOCKSTART_BLKADDR (host->regs + 0x14)
37 #define NFC_V1_UNLOCKEND_BLKADDR (host->regs + 0x16)
38 #define NFC_V21_UNLOCKSTART_BLKADDR0 (host->regs + 0x20)
39 #define NFC_V21_UNLOCKSTART_BLKADDR1 (host->regs + 0x24)
40 #define NFC_V21_UNLOCKSTART_BLKADDR2 (host->regs + 0x28)
41 #define NFC_V21_UNLOCKSTART_BLKADDR3 (host->regs + 0x2c)
42 #define NFC_V21_UNLOCKEND_BLKADDR0 (host->regs + 0x22)
43 #define NFC_V21_UNLOCKEND_BLKADDR1 (host->regs + 0x26)
44 #define NFC_V21_UNLOCKEND_BLKADDR2 (host->regs + 0x2a)
45 #define NFC_V21_UNLOCKEND_BLKADDR3 (host->regs + 0x2e)
46 #define NFC_V1_V2_NF_WRPRST (host->regs + 0x18)
47 #define NFC_V1_V2_CONFIG1 (host->regs + 0x1a)
48 #define NFC_V1_V2_CONFIG2 (host->regs + 0x1c)
50 #define NFC_V2_CONFIG1_ECC_MODE_4 (1 << 0)
51 #define NFC_V1_V2_CONFIG1_SP_EN (1 << 2)
52 #define NFC_V1_V2_CONFIG1_ECC_EN (1 << 3)
53 #define NFC_V1_V2_CONFIG1_INT_MSK (1 << 4)
54 #define NFC_V1_V2_CONFIG1_BIG (1 << 5)
55 #define NFC_V1_V2_CONFIG1_RST (1 << 6)
56 #define NFC_V1_V2_CONFIG1_CE (1 << 7)
57 #define NFC_V2_CONFIG1_ONE_CYCLE (1 << 8)
58 #define NFC_V2_CONFIG1_PPB(x) (((x) & 0x3) << 9)
59 #define NFC_V2_CONFIG1_FP_INT (1 << 11)
61 #define NFC_V1_V2_CONFIG2_INT (1 << 15)
64 * Operation modes for the NFC. Valid for v1, v2 and v3
67 #define NFC_CMD (1 << 0)
68 #define NFC_ADDR (1 << 1)
69 #define NFC_INPUT (1 << 2)
70 #define NFC_OUTPUT (1 << 3)
71 #define NFC_ID (1 << 4)
72 #define NFC_STATUS (1 << 5)
74 #define NFC_V3_FLASH_CMD (host->regs_axi + 0x00)
75 #define NFC_V3_FLASH_ADDR0 (host->regs_axi + 0x04)
77 #define NFC_V3_CONFIG1 (host->regs_axi + 0x34)
78 #define NFC_V3_CONFIG1_SP_EN (1 << 0)
79 #define NFC_V3_CONFIG1_RBA(x) (((x) & 0x7 ) << 4)
81 #define NFC_V3_ECC_STATUS_RESULT (host->regs_axi + 0x38)
83 #define NFC_V3_LAUNCH (host->regs_axi + 0x40)
85 #define NFC_V3_WRPROT (host->regs_ip + 0x0)
86 #define NFC_V3_WRPROT_LOCK_TIGHT (1 << 0)
87 #define NFC_V3_WRPROT_LOCK (1 << 1)
88 #define NFC_V3_WRPROT_UNLOCK (1 << 2)
89 #define NFC_V3_WRPROT_BLS_UNLOCK (2 << 6)
91 #define NFC_V3_WRPROT_UNLOCK_BLK_ADD0 (host->regs_ip + 0x04)
93 #define NFC_V3_CONFIG2 (host->regs_ip + 0x24)
94 #define NFC_V3_CONFIG2_PS_512 (0 << 0)
95 #define NFC_V3_CONFIG2_PS_2048 (1 << 0)
96 #define NFC_V3_CONFIG2_PS_4096 (2 << 0)
97 #define NFC_V3_CONFIG2_ONE_CYCLE (1 << 2)
98 #define NFC_V3_CONFIG2_ECC_EN (1 << 3)
99 #define NFC_V3_CONFIG2_2CMD_PHASES (1 << 4)
100 #define NFC_V3_CONFIG2_NUM_ADDR_PHASE0 (1 << 5)
101 #define NFC_V3_CONFIG2_ECC_MODE_8 (1 << 6)
102 #define NFC_V3_CONFIG2_PPB(x, shift) (((x) & 0x3) << shift)
103 #define NFC_V3_CONFIG2_NUM_ADDR_PHASE1(x) (((x) & 0x3) << 12)
104 #define NFC_V3_CONFIG2_INT_MSK (1 << 15)
105 #define NFC_V3_CONFIG2_ST_CMD(x) (((x) & 0xff) << 24)
106 #define NFC_V3_CONFIG2_SPAS(x) (((x) & 0xff) << 16)
108 #define NFC_V3_CONFIG3 (host->regs_ip + 0x28)
109 #define NFC_V3_CONFIG3_ADD_OP(x) (((x) & 0x3) << 0)
110 #define NFC_V3_CONFIG3_FW8 (1 << 3)
111 #define NFC_V3_CONFIG3_SBB(x) (((x) & 0x7) << 8)
112 #define NFC_V3_CONFIG3_NUM_OF_DEVICES(x) (((x) & 0x7) << 12)
113 #define NFC_V3_CONFIG3_RBB_MODE (1 << 15)
114 #define NFC_V3_CONFIG3_NO_SDMA (1 << 20)
116 #define NFC_V3_IPC (host->regs_ip + 0x2C)
117 #define NFC_V3_IPC_CREQ (1 << 0)
118 #define NFC_V3_IPC_INT (1 << 31)
120 #define NFC_V3_DELAY_LINE (host->regs_ip + 0x34)
122 struct mxc_nand_host;
124 struct mxc_nand_devtype_data {
125 void (*preset)(struct mtd_info *);
126 int (*read_page)(struct nand_chip *chip, void *buf, void *oob, bool ecc,
128 void (*send_cmd)(struct mxc_nand_host *, uint16_t, int);
129 void (*send_addr)(struct mxc_nand_host *, uint16_t, int);
130 void (*send_page)(struct mtd_info *, unsigned int);
131 void (*send_read_id)(struct mxc_nand_host *);
132 uint16_t (*get_dev_status)(struct mxc_nand_host *);
133 int (*check_int)(struct mxc_nand_host *);
134 void (*irq_control)(struct mxc_nand_host *, int);
135 u32 (*get_ecc_status)(struct mxc_nand_host *);
136 const struct mtd_ooblayout_ops *ooblayout;
137 void (*select_chip)(struct nand_chip *chip, int cs);
138 int (*setup_interface)(struct nand_chip *chip, int csline,
139 const struct nand_interface_config *conf);
140 void (*enable_hwecc)(struct nand_chip *chip, bool enable);
143 * On i.MX21 the CONFIG2:INT bit cannot be read if interrupts are masked
144 * (CONFIG1:INT_MSK is set). To handle this the driver uses
145 * enable_irq/disable_irq_nosync instead of CONFIG1:INT_MSK
147 int irqpending_quirk;
151 size_t spare0_offset;
160 struct mxc_nand_host {
161 struct nand_chip nand;
164 void __iomem *spare0;
165 void __iomem *main_area0;
169 void __iomem *regs_axi;
170 void __iomem *regs_ip;
179 struct completion op_completion;
182 unsigned int buf_start;
184 const struct mxc_nand_devtype_data *devtype_data;
187 static const char * const part_probes[] = {
188 "cmdlinepart", "RedBoot", "ofpart", NULL };
190 static void memcpy32_fromio(void *trg, const void __iomem *src, size_t size)
194 const __iomem u32 *s = src;
196 for (i = 0; i < (size >> 2); i++)
197 *t++ = __raw_readl(s++);
200 static void memcpy16_fromio(void *trg, const void __iomem *src, size_t size)
204 const __iomem u16 *s = src;
206 /* We assume that src (IO) is always 32bit aligned */
207 if (PTR_ALIGN(trg, 4) == trg && IS_ALIGNED(size, 4)) {
208 memcpy32_fromio(trg, src, size);
212 for (i = 0; i < (size >> 1); i++)
213 *t++ = __raw_readw(s++);
216 static inline void memcpy32_toio(void __iomem *trg, const void *src, int size)
218 /* __iowrite32_copy use 32bit size values so divide by 4 */
219 __iowrite32_copy(trg, src, size / 4);
222 static void memcpy16_toio(void __iomem *trg, const void *src, int size)
225 __iomem u16 *t = trg;
228 /* We assume that trg (IO) is always 32bit aligned */
229 if (PTR_ALIGN(src, 4) == src && IS_ALIGNED(size, 4)) {
230 memcpy32_toio(trg, src, size);
234 for (i = 0; i < (size >> 1); i++)
235 __raw_writew(*s++, t++);
239 * The controller splits a page into data chunks of 512 bytes + partial oob.
240 * There are writesize / 512 such chunks, the size of the partial oob parts is
241 * oobsize / #chunks rounded down to a multiple of 2. The last oob chunk then
242 * contains additionally the byte lost by rounding (if any).
243 * This function handles the needed shuffling between host->data_buf (which
244 * holds a page in natural order, i.e. writesize bytes data + oobsize bytes
245 * spare) and the NFC buffer.
247 static void copy_spare(struct mtd_info *mtd, bool bfrom, void *buf)
249 struct nand_chip *this = mtd_to_nand(mtd);
250 struct mxc_nand_host *host = nand_get_controller_data(this);
251 u16 i, oob_chunk_size;
252 u16 num_chunks = mtd->writesize / 512;
255 u8 __iomem *s = host->spare0;
256 u16 sparebuf_size = host->devtype_data->spare_len;
258 /* size of oob chunk for all but possibly the last one */
259 oob_chunk_size = (host->used_oobsize / num_chunks) & ~1;
262 for (i = 0; i < num_chunks - 1; i++)
263 memcpy16_fromio(d + i * oob_chunk_size,
264 s + i * sparebuf_size,
268 memcpy16_fromio(d + i * oob_chunk_size,
269 s + i * sparebuf_size,
270 host->used_oobsize - i * oob_chunk_size);
272 for (i = 0; i < num_chunks - 1; i++)
273 memcpy16_toio(&s[i * sparebuf_size],
274 &d[i * oob_chunk_size],
278 memcpy16_toio(&s[i * sparebuf_size],
279 &d[i * oob_chunk_size],
280 host->used_oobsize - i * oob_chunk_size);
285 * MXC NANDFC can only perform full page+spare or spare-only read/write. When
286 * the upper layers perform a read/write buf operation, the saved column address
287 * is used to index into the full page. So usually this function is called with
288 * column == 0 (unless no column cycle is needed indicated by column == -1)
290 static void mxc_do_addr_cycle(struct mtd_info *mtd, int column, int page_addr)
292 struct nand_chip *nand_chip = mtd_to_nand(mtd);
293 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
295 /* Write out column address, if necessary */
297 host->devtype_data->send_addr(host, column & 0xff,
299 if (mtd->writesize > 512)
300 /* another col addr cycle for 2k page */
301 host->devtype_data->send_addr(host,
302 (column >> 8) & 0xff,
306 /* Write out page address, if necessary */
307 if (page_addr != -1) {
308 /* paddr_0 - p_addr_7 */
309 host->devtype_data->send_addr(host, (page_addr & 0xff), false);
311 if (mtd->writesize > 512) {
312 if (mtd->size >= 0x10000000) {
313 /* paddr_8 - paddr_15 */
314 host->devtype_data->send_addr(host,
315 (page_addr >> 8) & 0xff,
317 host->devtype_data->send_addr(host,
318 (page_addr >> 16) & 0xff,
321 /* paddr_8 - paddr_15 */
322 host->devtype_data->send_addr(host,
323 (page_addr >> 8) & 0xff, true);
325 if (nand_chip->options & NAND_ROW_ADDR_3) {
326 /* paddr_8 - paddr_15 */
327 host->devtype_data->send_addr(host,
328 (page_addr >> 8) & 0xff,
330 host->devtype_data->send_addr(host,
331 (page_addr >> 16) & 0xff,
334 /* paddr_8 - paddr_15 */
335 host->devtype_data->send_addr(host,
336 (page_addr >> 8) & 0xff, true);
341 static int check_int_v3(struct mxc_nand_host *host)
345 tmp = readl(NFC_V3_IPC);
346 if (!(tmp & NFC_V3_IPC_INT))
349 tmp &= ~NFC_V3_IPC_INT;
350 writel(tmp, NFC_V3_IPC);
355 static int check_int_v1_v2(struct mxc_nand_host *host)
359 tmp = readw(NFC_V1_V2_CONFIG2);
360 if (!(tmp & NFC_V1_V2_CONFIG2_INT))
363 if (!host->devtype_data->irqpending_quirk)
364 writew(tmp & ~NFC_V1_V2_CONFIG2_INT, NFC_V1_V2_CONFIG2);
369 static void irq_control_v1_v2(struct mxc_nand_host *host, int activate)
373 tmp = readw(NFC_V1_V2_CONFIG1);
376 tmp &= ~NFC_V1_V2_CONFIG1_INT_MSK;
378 tmp |= NFC_V1_V2_CONFIG1_INT_MSK;
380 writew(tmp, NFC_V1_V2_CONFIG1);
383 static void irq_control_v3(struct mxc_nand_host *host, int activate)
387 tmp = readl(NFC_V3_CONFIG2);
390 tmp &= ~NFC_V3_CONFIG2_INT_MSK;
392 tmp |= NFC_V3_CONFIG2_INT_MSK;
394 writel(tmp, NFC_V3_CONFIG2);
397 static void irq_control(struct mxc_nand_host *host, int activate)
399 if (host->devtype_data->irqpending_quirk) {
401 enable_irq(host->irq);
403 disable_irq_nosync(host->irq);
405 host->devtype_data->irq_control(host, activate);
409 static u32 get_ecc_status_v1(struct mxc_nand_host *host)
411 return readw(NFC_V1_V2_ECC_STATUS_RESULT);
414 static u32 get_ecc_status_v2(struct mxc_nand_host *host)
416 return readl(NFC_V1_V2_ECC_STATUS_RESULT);
419 static u32 get_ecc_status_v3(struct mxc_nand_host *host)
421 return readl(NFC_V3_ECC_STATUS_RESULT);
424 static irqreturn_t mxc_nfc_irq(int irq, void *dev_id)
426 struct mxc_nand_host *host = dev_id;
428 if (!host->devtype_data->check_int(host))
431 irq_control(host, 0);
433 complete(&host->op_completion);
438 /* This function polls the NANDFC to wait for the basic operation to
439 * complete by checking the INT bit of config2 register.
441 static int wait_op_done(struct mxc_nand_host *host, int useirq)
446 * If operation is already complete, don't bother to setup an irq or a
449 if (host->devtype_data->check_int(host))
453 unsigned long timeout;
455 reinit_completion(&host->op_completion);
457 irq_control(host, 1);
459 timeout = wait_for_completion_timeout(&host->op_completion, HZ);
460 if (!timeout && !host->devtype_data->check_int(host)) {
461 dev_dbg(host->dev, "timeout waiting for irq\n");
465 int max_retries = 8000;
471 done = host->devtype_data->check_int(host);
475 } while (--max_retries);
478 dev_dbg(host->dev, "timeout polling for completion\n");
483 WARN_ONCE(ret < 0, "timeout! useirq=%d\n", useirq);
488 static void send_cmd_v3(struct mxc_nand_host *host, uint16_t cmd, int useirq)
491 writel(cmd, NFC_V3_FLASH_CMD);
493 /* send out command */
494 writel(NFC_CMD, NFC_V3_LAUNCH);
496 /* Wait for operation to complete */
497 wait_op_done(host, useirq);
500 /* This function issues the specified command to the NAND device and
501 * waits for completion. */
502 static void send_cmd_v1_v2(struct mxc_nand_host *host, uint16_t cmd, int useirq)
504 dev_dbg(host->dev, "send_cmd(host, 0x%x, %d)\n", cmd, useirq);
506 writew(cmd, NFC_V1_V2_FLASH_CMD);
507 writew(NFC_CMD, NFC_V1_V2_CONFIG2);
509 if (host->devtype_data->irqpending_quirk && (cmd == NAND_CMD_RESET)) {
510 int max_retries = 100;
511 /* Reset completion is indicated by NFC_CONFIG2 */
513 while (max_retries-- > 0) {
514 if (readw(NFC_V1_V2_CONFIG2) == 0) {
520 dev_dbg(host->dev, "%s: RESET failed\n", __func__);
522 /* Wait for operation to complete */
523 wait_op_done(host, useirq);
527 static void send_addr_v3(struct mxc_nand_host *host, uint16_t addr, int islast)
530 writel(addr, NFC_V3_FLASH_ADDR0);
532 /* send out address */
533 writel(NFC_ADDR, NFC_V3_LAUNCH);
535 wait_op_done(host, 0);
538 /* This function sends an address (or partial address) to the
539 * NAND device. The address is used to select the source/destination for
541 static void send_addr_v1_v2(struct mxc_nand_host *host, uint16_t addr, int islast)
543 dev_dbg(host->dev, "send_addr(host, 0x%x %d)\n", addr, islast);
545 writew(addr, NFC_V1_V2_FLASH_ADDR);
546 writew(NFC_ADDR, NFC_V1_V2_CONFIG2);
548 /* Wait for operation to complete */
549 wait_op_done(host, islast);
552 static void send_page_v3(struct mtd_info *mtd, unsigned int ops)
554 struct nand_chip *nand_chip = mtd_to_nand(mtd);
555 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
558 tmp = readl(NFC_V3_CONFIG1);
560 writel(tmp, NFC_V3_CONFIG1);
562 /* transfer data from NFC ram to nand */
563 writel(ops, NFC_V3_LAUNCH);
565 wait_op_done(host, false);
568 static void send_page_v2(struct mtd_info *mtd, unsigned int ops)
570 struct nand_chip *nand_chip = mtd_to_nand(mtd);
571 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
573 /* NANDFC buffer 0 is used for page read/write */
574 writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
576 writew(ops, NFC_V1_V2_CONFIG2);
578 /* Wait for operation to complete */
579 wait_op_done(host, true);
582 static void send_page_v1(struct mtd_info *mtd, unsigned int ops)
584 struct nand_chip *nand_chip = mtd_to_nand(mtd);
585 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
588 if (mtd->writesize > 512)
593 for (i = 0; i < bufs; i++) {
595 /* NANDFC buffer 0 is used for page read/write */
596 writew((host->active_cs << 4) | i, NFC_V1_V2_BUF_ADDR);
598 writew(ops, NFC_V1_V2_CONFIG2);
600 /* Wait for operation to complete */
601 wait_op_done(host, true);
605 static void send_read_id_v3(struct mxc_nand_host *host)
607 /* Read ID into main buffer */
608 writel(NFC_ID, NFC_V3_LAUNCH);
610 wait_op_done(host, true);
612 memcpy32_fromio(host->data_buf, host->main_area0, 16);
615 /* Request the NANDFC to perform a read of the NAND device ID. */
616 static void send_read_id_v1_v2(struct mxc_nand_host *host)
618 /* NANDFC buffer 0 is used for device ID output */
619 writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
621 writew(NFC_ID, NFC_V1_V2_CONFIG2);
623 /* Wait for operation to complete */
624 wait_op_done(host, true);
626 memcpy32_fromio(host->data_buf, host->main_area0, 16);
629 static uint16_t get_dev_status_v3(struct mxc_nand_host *host)
631 writew(NFC_STATUS, NFC_V3_LAUNCH);
632 wait_op_done(host, true);
634 return readl(NFC_V3_CONFIG1) >> 16;
637 /* This function requests the NANDFC to perform a read of the
638 * NAND device status and returns the current status. */
639 static uint16_t get_dev_status_v1_v2(struct mxc_nand_host *host)
641 void __iomem *main_buf = host->main_area0;
645 writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
648 * The device status is stored in main_area0. To
649 * prevent corruption of the buffer save the value
650 * and restore it afterwards.
652 store = readl(main_buf);
654 writew(NFC_STATUS, NFC_V1_V2_CONFIG2);
655 wait_op_done(host, true);
657 ret = readw(main_buf);
659 writel(store, main_buf);
664 static void mxc_nand_enable_hwecc_v1_v2(struct nand_chip *chip, bool enable)
666 struct mxc_nand_host *host = nand_get_controller_data(chip);
669 if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST)
672 config1 = readw(NFC_V1_V2_CONFIG1);
675 config1 |= NFC_V1_V2_CONFIG1_ECC_EN;
677 config1 &= ~NFC_V1_V2_CONFIG1_ECC_EN;
679 writew(config1, NFC_V1_V2_CONFIG1);
682 static void mxc_nand_enable_hwecc_v3(struct nand_chip *chip, bool enable)
684 struct mxc_nand_host *host = nand_get_controller_data(chip);
687 if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST)
690 config2 = readl(NFC_V3_CONFIG2);
693 config2 |= NFC_V3_CONFIG2_ECC_EN;
695 config2 &= ~NFC_V3_CONFIG2_ECC_EN;
697 writel(config2, NFC_V3_CONFIG2);
700 /* This functions is used by upper layer to checks if device is ready */
701 static int mxc_nand_dev_ready(struct nand_chip *chip)
704 * NFC handles R/B internally. Therefore, this function
705 * always returns status as ready.
710 static int mxc_nand_read_page_v1(struct nand_chip *chip, void *buf, void *oob,
713 struct mtd_info *mtd = nand_to_mtd(chip);
714 struct mxc_nand_host *host = nand_get_controller_data(chip);
715 unsigned int bitflips_corrected = 0;
719 host->devtype_data->enable_hwecc(chip, ecc);
721 host->devtype_data->send_cmd(host, NAND_CMD_READ0, false);
722 mxc_do_addr_cycle(mtd, 0, page);
724 if (mtd->writesize > 512)
725 host->devtype_data->send_cmd(host, NAND_CMD_READSTART, true);
727 no_subpages = mtd->writesize >> 9;
729 for (i = 0; i < no_subpages; i++) {
732 /* NANDFC buffer 0 is used for page read/write */
733 writew((host->active_cs << 4) | i, NFC_V1_V2_BUF_ADDR);
735 writew(NFC_OUTPUT, NFC_V1_V2_CONFIG2);
737 /* Wait for operation to complete */
738 wait_op_done(host, true);
740 ecc_stats = get_ecc_status_v1(host);
745 switch (ecc_stats & 0x3) {
750 mtd->ecc_stats.corrected++;
751 bitflips_corrected = 1;
754 mtd->ecc_stats.failed++;
761 memcpy32_fromio(buf, host->main_area0, mtd->writesize);
763 copy_spare(mtd, true, oob);
765 return bitflips_corrected;
768 static int mxc_nand_read_page_v2_v3(struct nand_chip *chip, void *buf,
769 void *oob, bool ecc, int page)
771 struct mtd_info *mtd = nand_to_mtd(chip);
772 struct mxc_nand_host *host = nand_get_controller_data(chip);
773 unsigned int max_bitflips = 0;
776 u8 ecc_bit_mask, err_limit;
778 host->devtype_data->enable_hwecc(chip, ecc);
780 host->devtype_data->send_cmd(host, NAND_CMD_READ0, false);
781 mxc_do_addr_cycle(mtd, 0, page);
783 if (mtd->writesize > 512)
784 host->devtype_data->send_cmd(host,
785 NAND_CMD_READSTART, true);
787 host->devtype_data->send_page(mtd, NFC_OUTPUT);
790 memcpy32_fromio(buf, host->main_area0, mtd->writesize);
792 copy_spare(mtd, true, oob);
794 ecc_bit_mask = (host->eccsize == 4) ? 0x7 : 0xf;
795 err_limit = (host->eccsize == 4) ? 0x4 : 0x8;
797 no_subpages = mtd->writesize >> 9;
799 ecc_stat = host->devtype_data->get_ecc_status(host);
802 err = ecc_stat & ecc_bit_mask;
803 if (err > err_limit) {
804 mtd->ecc_stats.failed++;
806 mtd->ecc_stats.corrected += err;
807 max_bitflips = max_t(unsigned int, max_bitflips, err);
811 } while (--no_subpages);
816 static int mxc_nand_read_page(struct nand_chip *chip, uint8_t *buf,
817 int oob_required, int page)
819 struct mxc_nand_host *host = nand_get_controller_data(chip);
823 oob_buf = chip->oob_poi;
827 return host->devtype_data->read_page(chip, buf, oob_buf, 1, page);
830 static int mxc_nand_read_page_raw(struct nand_chip *chip, uint8_t *buf,
831 int oob_required, int page)
833 struct mxc_nand_host *host = nand_get_controller_data(chip);
837 oob_buf = chip->oob_poi;
841 return host->devtype_data->read_page(chip, buf, oob_buf, 0, page);
844 static int mxc_nand_read_oob(struct nand_chip *chip, int page)
846 struct mxc_nand_host *host = nand_get_controller_data(chip);
848 return host->devtype_data->read_page(chip, NULL, chip->oob_poi, 0,
852 static int mxc_nand_write_page(struct nand_chip *chip, const uint8_t *buf,
855 struct mtd_info *mtd = nand_to_mtd(chip);
856 struct mxc_nand_host *host = nand_get_controller_data(chip);
858 host->devtype_data->enable_hwecc(chip, ecc);
860 host->devtype_data->send_cmd(host, NAND_CMD_SEQIN, false);
861 mxc_do_addr_cycle(mtd, 0, page);
863 memcpy32_toio(host->main_area0, buf, mtd->writesize);
864 copy_spare(mtd, false, chip->oob_poi);
866 host->devtype_data->send_page(mtd, NFC_INPUT);
867 host->devtype_data->send_cmd(host, NAND_CMD_PAGEPROG, true);
868 mxc_do_addr_cycle(mtd, 0, page);
873 static int mxc_nand_write_page_ecc(struct nand_chip *chip, const uint8_t *buf,
874 int oob_required, int page)
876 return mxc_nand_write_page(chip, buf, true, page);
879 static int mxc_nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
880 int oob_required, int page)
882 return mxc_nand_write_page(chip, buf, false, page);
885 static int mxc_nand_write_oob(struct nand_chip *chip, int page)
887 struct mtd_info *mtd = nand_to_mtd(chip);
888 struct mxc_nand_host *host = nand_get_controller_data(chip);
890 memset(host->data_buf, 0xff, mtd->writesize);
892 return mxc_nand_write_page(chip, host->data_buf, false, page);
895 static u_char mxc_nand_read_byte(struct nand_chip *nand_chip)
897 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
900 /* Check for status request */
901 if (host->status_request)
902 return host->devtype_data->get_dev_status(host) & 0xFF;
904 if (nand_chip->options & NAND_BUSWIDTH_16) {
905 /* only take the lower byte of each word */
906 ret = *(uint16_t *)(host->data_buf + host->buf_start);
908 host->buf_start += 2;
910 ret = *(uint8_t *)(host->data_buf + host->buf_start);
914 dev_dbg(host->dev, "%s: ret=0x%hhx (start=%u)\n", __func__, ret, host->buf_start);
918 /* Write data of length len to buffer buf. The data to be
919 * written on NAND Flash is first copied to RAMbuffer. After the Data Input
920 * Operation by the NFC, the data is written to NAND Flash */
921 static void mxc_nand_write_buf(struct nand_chip *nand_chip, const u_char *buf,
924 struct mtd_info *mtd = nand_to_mtd(nand_chip);
925 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
926 u16 col = host->buf_start;
927 int n = mtd->oobsize + mtd->writesize - col;
931 memcpy(host->data_buf + col, buf, n);
933 host->buf_start += n;
936 /* Read the data buffer from the NAND Flash. To read the data from NAND
937 * Flash first the data output cycle is initiated by the NFC, which copies
938 * the data to RAMbuffer. This data of length len is then copied to buffer buf.
940 static void mxc_nand_read_buf(struct nand_chip *nand_chip, u_char *buf,
943 struct mtd_info *mtd = nand_to_mtd(nand_chip);
944 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
945 u16 col = host->buf_start;
946 int n = mtd->oobsize + mtd->writesize - col;
950 memcpy(buf, host->data_buf + col, n);
952 host->buf_start += n;
955 /* This function is used by upper layer for select and
956 * deselect of the NAND chip */
957 static void mxc_nand_select_chip_v1_v3(struct nand_chip *nand_chip, int chip)
959 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
962 /* Disable the NFC clock */
964 clk_disable_unprepare(host->clk);
970 if (!host->clk_act) {
971 /* Enable the NFC clock */
972 clk_prepare_enable(host->clk);
977 static void mxc_nand_select_chip_v2(struct nand_chip *nand_chip, int chip)
979 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
982 /* Disable the NFC clock */
984 clk_disable_unprepare(host->clk);
990 if (!host->clk_act) {
991 /* Enable the NFC clock */
992 clk_prepare_enable(host->clk);
996 host->active_cs = chip;
997 writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
1000 #define MXC_V1_ECCBYTES 5
1002 static int mxc_v1_ooblayout_ecc(struct mtd_info *mtd, int section,
1003 struct mtd_oob_region *oobregion)
1005 struct nand_chip *nand_chip = mtd_to_nand(mtd);
1007 if (section >= nand_chip->ecc.steps)
1010 oobregion->offset = (section * 16) + 6;
1011 oobregion->length = MXC_V1_ECCBYTES;
1016 static int mxc_v1_ooblayout_free(struct mtd_info *mtd, int section,
1017 struct mtd_oob_region *oobregion)
1019 struct nand_chip *nand_chip = mtd_to_nand(mtd);
1021 if (section > nand_chip->ecc.steps)
1025 if (mtd->writesize <= 512) {
1026 oobregion->offset = 0;
1027 oobregion->length = 5;
1029 oobregion->offset = 2;
1030 oobregion->length = 4;
1033 oobregion->offset = ((section - 1) * 16) + MXC_V1_ECCBYTES + 6;
1034 if (section < nand_chip->ecc.steps)
1035 oobregion->length = (section * 16) + 6 -
1038 oobregion->length = mtd->oobsize - oobregion->offset;
1044 static const struct mtd_ooblayout_ops mxc_v1_ooblayout_ops = {
1045 .ecc = mxc_v1_ooblayout_ecc,
1046 .free = mxc_v1_ooblayout_free,
1049 static int mxc_v2_ooblayout_ecc(struct mtd_info *mtd, int section,
1050 struct mtd_oob_region *oobregion)
1052 struct nand_chip *nand_chip = mtd_to_nand(mtd);
1053 int stepsize = nand_chip->ecc.bytes == 9 ? 16 : 26;
1055 if (section >= nand_chip->ecc.steps)
1058 oobregion->offset = (section * stepsize) + 7;
1059 oobregion->length = nand_chip->ecc.bytes;
1064 static int mxc_v2_ooblayout_free(struct mtd_info *mtd, int section,
1065 struct mtd_oob_region *oobregion)
1067 struct nand_chip *nand_chip = mtd_to_nand(mtd);
1068 int stepsize = nand_chip->ecc.bytes == 9 ? 16 : 26;
1070 if (section >= nand_chip->ecc.steps)
1074 if (mtd->writesize <= 512) {
1075 oobregion->offset = 0;
1076 oobregion->length = 5;
1078 oobregion->offset = 2;
1079 oobregion->length = 4;
1082 oobregion->offset = section * stepsize;
1083 oobregion->length = 7;
1089 static const struct mtd_ooblayout_ops mxc_v2_ooblayout_ops = {
1090 .ecc = mxc_v2_ooblayout_ecc,
1091 .free = mxc_v2_ooblayout_free,
1095 * v2 and v3 type controllers can do 4bit or 8bit ecc depending
1096 * on how much oob the nand chip has. For 8bit ecc we need at least
1097 * 26 bytes of oob data per 512 byte block.
1099 static int get_eccsize(struct mtd_info *mtd)
1101 int oobbytes_per_512 = 0;
1103 oobbytes_per_512 = mtd->oobsize * 512 / mtd->writesize;
1105 if (oobbytes_per_512 < 26)
1111 static void preset_v1(struct mtd_info *mtd)
1113 struct nand_chip *nand_chip = mtd_to_nand(mtd);
1114 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
1115 uint16_t config1 = 0;
1117 if (nand_chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST &&
1119 config1 |= NFC_V1_V2_CONFIG1_ECC_EN;
1121 if (!host->devtype_data->irqpending_quirk)
1122 config1 |= NFC_V1_V2_CONFIG1_INT_MSK;
1126 writew(config1, NFC_V1_V2_CONFIG1);
1127 /* preset operation */
1129 /* Unlock the internal RAM Buffer */
1130 writew(0x2, NFC_V1_V2_CONFIG);
1132 /* Blocks to be unlocked */
1133 writew(0x0, NFC_V1_UNLOCKSTART_BLKADDR);
1134 writew(0xffff, NFC_V1_UNLOCKEND_BLKADDR);
1136 /* Unlock Block Command for given address range */
1137 writew(0x4, NFC_V1_V2_WRPROT);
1140 static int mxc_nand_v2_setup_interface(struct nand_chip *chip, int csline,
1141 const struct nand_interface_config *conf)
1143 struct mxc_nand_host *host = nand_get_controller_data(chip);
1144 int tRC_min_ns, tRC_ps, ret;
1145 unsigned long rate, rate_round;
1146 const struct nand_sdr_timings *timings;
1149 timings = nand_get_sdr_timings(conf);
1150 if (IS_ERR(timings))
1153 config1 = readw(NFC_V1_V2_CONFIG1);
1155 tRC_min_ns = timings->tRC_min / 1000;
1156 rate = 1000000000 / tRC_min_ns;
1159 * For tRC < 30ns we have to use EDO mode. In this case the controller
1160 * does one access per clock cycle. Otherwise the controller does one
1161 * access in two clock cycles, thus we have to double the rate to the
1164 if (tRC_min_ns < 30) {
1165 rate_round = clk_round_rate(host->clk, rate);
1166 config1 |= NFC_V2_CONFIG1_ONE_CYCLE;
1167 tRC_ps = 1000000000 / (rate_round / 1000);
1170 rate_round = clk_round_rate(host->clk, rate);
1171 config1 &= ~NFC_V2_CONFIG1_ONE_CYCLE;
1172 tRC_ps = 1000000000 / (rate_round / 1000 / 2);
1176 * The timing values compared against are from the i.MX25 Automotive
1177 * datasheet, Table 50. NFC Timing Parameters
1179 if (timings->tCLS_min > tRC_ps - 1000 ||
1180 timings->tCLH_min > tRC_ps - 2000 ||
1181 timings->tCS_min > tRC_ps - 1000 ||
1182 timings->tCH_min > tRC_ps - 2000 ||
1183 timings->tWP_min > tRC_ps - 1500 ||
1184 timings->tALS_min > tRC_ps ||
1185 timings->tALH_min > tRC_ps - 3000 ||
1186 timings->tDS_min > tRC_ps ||
1187 timings->tDH_min > tRC_ps - 5000 ||
1188 timings->tWC_min > 2 * tRC_ps ||
1189 timings->tWH_min > tRC_ps - 2500 ||
1190 timings->tRR_min > 6 * tRC_ps ||
1191 timings->tRP_min > 3 * tRC_ps / 2 ||
1192 timings->tRC_min > 2 * tRC_ps ||
1193 timings->tREH_min > (tRC_ps / 2) - 2500) {
1194 dev_dbg(host->dev, "Timing out of bounds\n");
1198 if (csline == NAND_DATA_IFACE_CHECK_ONLY)
1201 ret = clk_set_rate(host->clk, rate);
1205 writew(config1, NFC_V1_V2_CONFIG1);
1207 dev_dbg(host->dev, "Setting rate to %ldHz, %s mode\n", rate_round,
1208 config1 & NFC_V2_CONFIG1_ONE_CYCLE ? "One cycle (EDO)" :
1214 static void preset_v2(struct mtd_info *mtd)
1216 struct nand_chip *nand_chip = mtd_to_nand(mtd);
1217 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
1218 uint16_t config1 = 0;
1220 config1 |= NFC_V2_CONFIG1_FP_INT;
1222 if (!host->devtype_data->irqpending_quirk)
1223 config1 |= NFC_V1_V2_CONFIG1_INT_MSK;
1225 if (mtd->writesize) {
1226 uint16_t pages_per_block = mtd->erasesize / mtd->writesize;
1228 if (nand_chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST)
1229 config1 |= NFC_V1_V2_CONFIG1_ECC_EN;
1231 host->eccsize = get_eccsize(mtd);
1232 if (host->eccsize == 4)
1233 config1 |= NFC_V2_CONFIG1_ECC_MODE_4;
1235 config1 |= NFC_V2_CONFIG1_PPB(ffs(pages_per_block) - 6);
1240 writew(config1, NFC_V1_V2_CONFIG1);
1241 /* preset operation */
1243 /* spare area size in 16-bit half-words */
1244 writew(mtd->oobsize / 2, NFC_V21_RSLTSPARE_AREA);
1246 /* Unlock the internal RAM Buffer */
1247 writew(0x2, NFC_V1_V2_CONFIG);
1249 /* Blocks to be unlocked */
1250 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR0);
1251 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR1);
1252 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR2);
1253 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR3);
1254 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR0);
1255 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR1);
1256 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR2);
1257 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR3);
1259 /* Unlock Block Command for given address range */
1260 writew(0x4, NFC_V1_V2_WRPROT);
1263 static void preset_v3(struct mtd_info *mtd)
1265 struct nand_chip *chip = mtd_to_nand(mtd);
1266 struct mxc_nand_host *host = nand_get_controller_data(chip);
1267 uint32_t config2, config3;
1270 writel(NFC_V3_CONFIG1_RBA(0), NFC_V3_CONFIG1);
1271 writel(NFC_V3_IPC_CREQ, NFC_V3_IPC);
1273 /* Unlock the internal RAM Buffer */
1274 writel(NFC_V3_WRPROT_BLS_UNLOCK | NFC_V3_WRPROT_UNLOCK,
1277 /* Blocks to be unlocked */
1278 for (i = 0; i < NAND_MAX_CHIPS; i++)
1279 writel(0xffff << 16, NFC_V3_WRPROT_UNLOCK_BLK_ADD0 + (i << 2));
1281 writel(0, NFC_V3_IPC);
1283 config2 = NFC_V3_CONFIG2_ONE_CYCLE |
1284 NFC_V3_CONFIG2_2CMD_PHASES |
1285 NFC_V3_CONFIG2_SPAS(mtd->oobsize >> 1) |
1286 NFC_V3_CONFIG2_ST_CMD(0x70) |
1287 NFC_V3_CONFIG2_INT_MSK |
1288 NFC_V3_CONFIG2_NUM_ADDR_PHASE0;
1290 addr_phases = fls(chip->pagemask) >> 3;
1292 if (mtd->writesize == 2048) {
1293 config2 |= NFC_V3_CONFIG2_PS_2048;
1294 config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
1295 } else if (mtd->writesize == 4096) {
1296 config2 |= NFC_V3_CONFIG2_PS_4096;
1297 config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
1299 config2 |= NFC_V3_CONFIG2_PS_512;
1300 config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases - 1);
1303 if (mtd->writesize) {
1304 if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST)
1305 config2 |= NFC_V3_CONFIG2_ECC_EN;
1307 config2 |= NFC_V3_CONFIG2_PPB(
1308 ffs(mtd->erasesize / mtd->writesize) - 6,
1309 host->devtype_data->ppb_shift);
1310 host->eccsize = get_eccsize(mtd);
1311 if (host->eccsize == 8)
1312 config2 |= NFC_V3_CONFIG2_ECC_MODE_8;
1315 writel(config2, NFC_V3_CONFIG2);
1317 config3 = NFC_V3_CONFIG3_NUM_OF_DEVICES(0) |
1318 NFC_V3_CONFIG3_NO_SDMA |
1319 NFC_V3_CONFIG3_RBB_MODE |
1320 NFC_V3_CONFIG3_SBB(6) | /* Reset default */
1321 NFC_V3_CONFIG3_ADD_OP(0);
1323 if (!(chip->options & NAND_BUSWIDTH_16))
1324 config3 |= NFC_V3_CONFIG3_FW8;
1326 writel(config3, NFC_V3_CONFIG3);
1328 writel(0, NFC_V3_DELAY_LINE);
1331 /* Used by the upper layer to write command to NAND Flash for
1332 * different operations to be carried out on NAND Flash */
1333 static void mxc_nand_command(struct nand_chip *nand_chip, unsigned command,
1334 int column, int page_addr)
1336 struct mtd_info *mtd = nand_to_mtd(nand_chip);
1337 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
1339 dev_dbg(host->dev, "mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
1340 command, column, page_addr);
1342 /* Reset command state information */
1343 host->status_request = false;
1345 /* Command pre-processing step */
1347 case NAND_CMD_RESET:
1348 host->devtype_data->preset(mtd);
1349 host->devtype_data->send_cmd(host, command, false);
1352 case NAND_CMD_STATUS:
1353 host->buf_start = 0;
1354 host->status_request = true;
1356 host->devtype_data->send_cmd(host, command, true);
1357 WARN_ONCE(column != -1 || page_addr != -1,
1358 "Unexpected column/row value (cmd=%u, col=%d, row=%d)\n",
1359 command, column, page_addr);
1360 mxc_do_addr_cycle(mtd, column, page_addr);
1363 case NAND_CMD_READID:
1364 host->devtype_data->send_cmd(host, command, true);
1365 mxc_do_addr_cycle(mtd, column, page_addr);
1366 host->devtype_data->send_read_id(host);
1367 host->buf_start = 0;
1370 case NAND_CMD_ERASE1:
1371 case NAND_CMD_ERASE2:
1372 host->devtype_data->send_cmd(host, command, false);
1373 WARN_ONCE(column != -1,
1374 "Unexpected column value (cmd=%u, col=%d)\n",
1376 mxc_do_addr_cycle(mtd, column, page_addr);
1379 case NAND_CMD_PARAM:
1380 host->devtype_data->send_cmd(host, command, false);
1381 mxc_do_addr_cycle(mtd, column, page_addr);
1382 host->devtype_data->send_page(mtd, NFC_OUTPUT);
1383 memcpy32_fromio(host->data_buf, host->main_area0, 512);
1384 host->buf_start = 0;
1387 WARN_ONCE(1, "Unimplemented command (cmd=%u)\n",
1393 static int mxc_nand_set_features(struct nand_chip *chip, int addr,
1394 u8 *subfeature_param)
1396 struct mtd_info *mtd = nand_to_mtd(chip);
1397 struct mxc_nand_host *host = nand_get_controller_data(chip);
1400 host->buf_start = 0;
1402 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
1403 chip->legacy.write_byte(chip, subfeature_param[i]);
1405 memcpy32_toio(host->main_area0, host->data_buf, mtd->writesize);
1406 host->devtype_data->send_cmd(host, NAND_CMD_SET_FEATURES, false);
1407 mxc_do_addr_cycle(mtd, addr, -1);
1408 host->devtype_data->send_page(mtd, NFC_INPUT);
1413 static int mxc_nand_get_features(struct nand_chip *chip, int addr,
1414 u8 *subfeature_param)
1416 struct mtd_info *mtd = nand_to_mtd(chip);
1417 struct mxc_nand_host *host = nand_get_controller_data(chip);
1420 host->devtype_data->send_cmd(host, NAND_CMD_GET_FEATURES, false);
1421 mxc_do_addr_cycle(mtd, addr, -1);
1422 host->devtype_data->send_page(mtd, NFC_OUTPUT);
1423 memcpy32_fromio(host->data_buf, host->main_area0, 512);
1424 host->buf_start = 0;
1426 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
1427 *subfeature_param++ = chip->legacy.read_byte(chip);
1433 * The generic flash bbt descriptors overlap with our ecc
1434 * hardware, so define some i.MX specific ones.
1436 static uint8_t bbt_pattern[] = { 'B', 'b', 't', '0' };
1437 static uint8_t mirror_pattern[] = { '1', 't', 'b', 'B' };
1439 static struct nand_bbt_descr bbt_main_descr = {
1440 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1441 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1446 .pattern = bbt_pattern,
1449 static struct nand_bbt_descr bbt_mirror_descr = {
1450 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1451 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1456 .pattern = mirror_pattern,
1459 /* v1 + irqpending_quirk: i.MX21 */
1460 static const struct mxc_nand_devtype_data imx21_nand_devtype_data = {
1461 .preset = preset_v1,
1462 .read_page = mxc_nand_read_page_v1,
1463 .send_cmd = send_cmd_v1_v2,
1464 .send_addr = send_addr_v1_v2,
1465 .send_page = send_page_v1,
1466 .send_read_id = send_read_id_v1_v2,
1467 .get_dev_status = get_dev_status_v1_v2,
1468 .check_int = check_int_v1_v2,
1469 .irq_control = irq_control_v1_v2,
1470 .get_ecc_status = get_ecc_status_v1,
1471 .ooblayout = &mxc_v1_ooblayout_ops,
1472 .select_chip = mxc_nand_select_chip_v1_v3,
1473 .enable_hwecc = mxc_nand_enable_hwecc_v1_v2,
1474 .irqpending_quirk = 1,
1476 .regs_offset = 0xe00,
1477 .spare0_offset = 0x800,
1483 /* v1 + !irqpending_quirk: i.MX27, i.MX31 */
1484 static const struct mxc_nand_devtype_data imx27_nand_devtype_data = {
1485 .preset = preset_v1,
1486 .read_page = mxc_nand_read_page_v1,
1487 .send_cmd = send_cmd_v1_v2,
1488 .send_addr = send_addr_v1_v2,
1489 .send_page = send_page_v1,
1490 .send_read_id = send_read_id_v1_v2,
1491 .get_dev_status = get_dev_status_v1_v2,
1492 .check_int = check_int_v1_v2,
1493 .irq_control = irq_control_v1_v2,
1494 .get_ecc_status = get_ecc_status_v1,
1495 .ooblayout = &mxc_v1_ooblayout_ops,
1496 .select_chip = mxc_nand_select_chip_v1_v3,
1497 .enable_hwecc = mxc_nand_enable_hwecc_v1_v2,
1498 .irqpending_quirk = 0,
1500 .regs_offset = 0xe00,
1501 .spare0_offset = 0x800,
1508 /* v21: i.MX25, i.MX35 */
1509 static const struct mxc_nand_devtype_data imx25_nand_devtype_data = {
1510 .preset = preset_v2,
1511 .read_page = mxc_nand_read_page_v2_v3,
1512 .send_cmd = send_cmd_v1_v2,
1513 .send_addr = send_addr_v1_v2,
1514 .send_page = send_page_v2,
1515 .send_read_id = send_read_id_v1_v2,
1516 .get_dev_status = get_dev_status_v1_v2,
1517 .check_int = check_int_v1_v2,
1518 .irq_control = irq_control_v1_v2,
1519 .get_ecc_status = get_ecc_status_v2,
1520 .ooblayout = &mxc_v2_ooblayout_ops,
1521 .select_chip = mxc_nand_select_chip_v2,
1522 .setup_interface = mxc_nand_v2_setup_interface,
1523 .enable_hwecc = mxc_nand_enable_hwecc_v1_v2,
1524 .irqpending_quirk = 0,
1526 .regs_offset = 0x1e00,
1527 .spare0_offset = 0x1000,
1535 static const struct mxc_nand_devtype_data imx51_nand_devtype_data = {
1536 .preset = preset_v3,
1537 .read_page = mxc_nand_read_page_v2_v3,
1538 .send_cmd = send_cmd_v3,
1539 .send_addr = send_addr_v3,
1540 .send_page = send_page_v3,
1541 .send_read_id = send_read_id_v3,
1542 .get_dev_status = get_dev_status_v3,
1543 .check_int = check_int_v3,
1544 .irq_control = irq_control_v3,
1545 .get_ecc_status = get_ecc_status_v3,
1546 .ooblayout = &mxc_v2_ooblayout_ops,
1547 .select_chip = mxc_nand_select_chip_v1_v3,
1548 .enable_hwecc = mxc_nand_enable_hwecc_v3,
1549 .irqpending_quirk = 0,
1552 .spare0_offset = 0x1000,
1553 .axi_offset = 0x1e00,
1561 static const struct mxc_nand_devtype_data imx53_nand_devtype_data = {
1562 .preset = preset_v3,
1563 .read_page = mxc_nand_read_page_v2_v3,
1564 .send_cmd = send_cmd_v3,
1565 .send_addr = send_addr_v3,
1566 .send_page = send_page_v3,
1567 .send_read_id = send_read_id_v3,
1568 .get_dev_status = get_dev_status_v3,
1569 .check_int = check_int_v3,
1570 .irq_control = irq_control_v3,
1571 .get_ecc_status = get_ecc_status_v3,
1572 .ooblayout = &mxc_v2_ooblayout_ops,
1573 .select_chip = mxc_nand_select_chip_v1_v3,
1574 .enable_hwecc = mxc_nand_enable_hwecc_v3,
1575 .irqpending_quirk = 0,
1578 .spare0_offset = 0x1000,
1579 .axi_offset = 0x1e00,
1586 static inline int is_imx21_nfc(struct mxc_nand_host *host)
1588 return host->devtype_data == &imx21_nand_devtype_data;
1591 static inline int is_imx27_nfc(struct mxc_nand_host *host)
1593 return host->devtype_data == &imx27_nand_devtype_data;
1596 static inline int is_imx25_nfc(struct mxc_nand_host *host)
1598 return host->devtype_data == &imx25_nand_devtype_data;
1601 static const struct of_device_id mxcnd_dt_ids[] = {
1602 { .compatible = "fsl,imx21-nand", .data = &imx21_nand_devtype_data, },
1603 { .compatible = "fsl,imx27-nand", .data = &imx27_nand_devtype_data, },
1604 { .compatible = "fsl,imx25-nand", .data = &imx25_nand_devtype_data, },
1605 { .compatible = "fsl,imx51-nand", .data = &imx51_nand_devtype_data, },
1606 { .compatible = "fsl,imx53-nand", .data = &imx53_nand_devtype_data, },
1609 MODULE_DEVICE_TABLE(of, mxcnd_dt_ids);
1611 static int mxcnd_attach_chip(struct nand_chip *chip)
1613 struct mtd_info *mtd = nand_to_mtd(chip);
1614 struct mxc_nand_host *host = nand_get_controller_data(chip);
1615 struct device *dev = mtd->dev.parent;
1617 chip->ecc.bytes = host->devtype_data->eccbytes;
1618 host->eccsize = host->devtype_data->eccsize;
1619 chip->ecc.size = 512;
1620 mtd_set_ooblayout(mtd, host->devtype_data->ooblayout);
1622 switch (chip->ecc.engine_type) {
1623 case NAND_ECC_ENGINE_TYPE_ON_HOST:
1624 chip->ecc.read_page = mxc_nand_read_page;
1625 chip->ecc.read_page_raw = mxc_nand_read_page_raw;
1626 chip->ecc.read_oob = mxc_nand_read_oob;
1627 chip->ecc.write_page = mxc_nand_write_page_ecc;
1628 chip->ecc.write_page_raw = mxc_nand_write_page_raw;
1629 chip->ecc.write_oob = mxc_nand_write_oob;
1632 case NAND_ECC_ENGINE_TYPE_SOFT:
1639 if (chip->bbt_options & NAND_BBT_USE_FLASH) {
1640 chip->bbt_td = &bbt_main_descr;
1641 chip->bbt_md = &bbt_mirror_descr;
1644 /* Allocate the right size buffer now */
1645 devm_kfree(dev, (void *)host->data_buf);
1646 host->data_buf = devm_kzalloc(dev, mtd->writesize + mtd->oobsize,
1648 if (!host->data_buf)
1651 /* Call preset again, with correct writesize chip time */
1652 host->devtype_data->preset(mtd);
1654 if (!chip->ecc.bytes) {
1655 if (host->eccsize == 8)
1656 chip->ecc.bytes = 18;
1657 else if (host->eccsize == 4)
1658 chip->ecc.bytes = 9;
1662 * Experimentation shows that i.MX NFC can only handle up to 218 oob
1663 * bytes. Limit used_oobsize to 218 so as to not confuse copy_spare()
1664 * into copying invalid data to/from the spare IO buffer, as this
1665 * might cause ECC data corruption when doing sub-page write to a
1666 * partially written page.
1668 host->used_oobsize = min(mtd->oobsize, 218U);
1670 if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST) {
1671 if (is_imx21_nfc(host) || is_imx27_nfc(host))
1672 chip->ecc.strength = 1;
1674 chip->ecc.strength = (host->eccsize == 4) ? 4 : 8;
1680 static int mxcnd_setup_interface(struct nand_chip *chip, int chipnr,
1681 const struct nand_interface_config *conf)
1683 struct mxc_nand_host *host = nand_get_controller_data(chip);
1685 return host->devtype_data->setup_interface(chip, chipnr, conf);
1688 static const struct nand_controller_ops mxcnd_controller_ops = {
1689 .attach_chip = mxcnd_attach_chip,
1690 .setup_interface = mxcnd_setup_interface,
1693 static int mxcnd_probe(struct platform_device *pdev)
1695 struct nand_chip *this;
1696 struct mtd_info *mtd;
1697 struct mxc_nand_host *host;
1700 /* Allocate memory for MTD device structure and private data */
1701 host = devm_kzalloc(&pdev->dev, sizeof(struct mxc_nand_host),
1706 /* allocate a temporary buffer for the nand_scan_ident() */
1707 host->data_buf = devm_kzalloc(&pdev->dev, PAGE_SIZE, GFP_KERNEL);
1708 if (!host->data_buf)
1711 host->dev = &pdev->dev;
1712 /* structures must be linked */
1714 mtd = nand_to_mtd(this);
1715 mtd->dev.parent = &pdev->dev;
1716 mtd->name = DRIVER_NAME;
1718 /* 50 us command delay time */
1719 this->legacy.chip_delay = 5;
1721 nand_set_controller_data(this, host);
1722 nand_set_flash_node(this, pdev->dev.of_node);
1723 this->legacy.dev_ready = mxc_nand_dev_ready;
1724 this->legacy.cmdfunc = mxc_nand_command;
1725 this->legacy.read_byte = mxc_nand_read_byte;
1726 this->legacy.write_buf = mxc_nand_write_buf;
1727 this->legacy.read_buf = mxc_nand_read_buf;
1728 this->legacy.set_features = mxc_nand_set_features;
1729 this->legacy.get_features = mxc_nand_get_features;
1731 host->clk = devm_clk_get(&pdev->dev, NULL);
1732 if (IS_ERR(host->clk))
1733 return PTR_ERR(host->clk);
1735 host->devtype_data = device_get_match_data(&pdev->dev);
1737 if (!host->devtype_data->setup_interface)
1738 this->options |= NAND_KEEP_TIMINGS;
1740 if (host->devtype_data->needs_ip) {
1741 host->regs_ip = devm_platform_ioremap_resource(pdev, 0);
1742 if (IS_ERR(host->regs_ip))
1743 return PTR_ERR(host->regs_ip);
1745 host->base = devm_platform_ioremap_resource(pdev, 1);
1747 host->base = devm_platform_ioremap_resource(pdev, 0);
1750 if (IS_ERR(host->base))
1751 return PTR_ERR(host->base);
1753 host->main_area0 = host->base;
1755 if (host->devtype_data->regs_offset)
1756 host->regs = host->base + host->devtype_data->regs_offset;
1757 host->spare0 = host->base + host->devtype_data->spare0_offset;
1758 if (host->devtype_data->axi_offset)
1759 host->regs_axi = host->base + host->devtype_data->axi_offset;
1761 this->legacy.select_chip = host->devtype_data->select_chip;
1763 init_completion(&host->op_completion);
1765 host->irq = platform_get_irq(pdev, 0);
1770 * Use host->devtype_data->irq_control() here instead of irq_control()
1771 * because we must not disable_irq_nosync without having requested the
1774 host->devtype_data->irq_control(host, 0);
1776 err = devm_request_irq(&pdev->dev, host->irq, mxc_nfc_irq,
1777 0, DRIVER_NAME, host);
1781 err = clk_prepare_enable(host->clk);
1787 * Now that we "own" the interrupt make sure the interrupt mask bit is
1788 * cleared on i.MX21. Otherwise we can't read the interrupt status bit
1791 if (host->devtype_data->irqpending_quirk) {
1792 disable_irq_nosync(host->irq);
1793 host->devtype_data->irq_control(host, 1);
1796 /* Scan the NAND device */
1797 this->legacy.dummy_controller.ops = &mxcnd_controller_ops;
1798 err = nand_scan(this, is_imx25_nfc(host) ? 4 : 1);
1802 /* Register the partitions */
1803 err = mtd_device_parse_register(mtd, part_probes, NULL, NULL, 0);
1807 platform_set_drvdata(pdev, host);
1815 clk_disable_unprepare(host->clk);
1820 static void mxcnd_remove(struct platform_device *pdev)
1822 struct mxc_nand_host *host = platform_get_drvdata(pdev);
1823 struct nand_chip *chip = &host->nand;
1826 ret = mtd_device_unregister(nand_to_mtd(chip));
1830 clk_disable_unprepare(host->clk);
1833 static struct platform_driver mxcnd_driver = {
1835 .name = DRIVER_NAME,
1836 .of_match_table = mxcnd_dt_ids,
1838 .probe = mxcnd_probe,
1839 .remove_new = mxcnd_remove,
1841 module_platform_driver(mxcnd_driver);
1843 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1844 MODULE_DESCRIPTION("MXC NAND MTD driver");
1845 MODULE_LICENSE("GPL");