2 * Copyright 2004-2007 Freescale Semiconductor, Inc.
3 * Copyright 2008 Sascha Hauer, kernel@pengutronix.de
4 * Copyright 2009 Ilya Yanok, <yanok@emcraft.com>
6 * SPDX-License-Identifier: GPL-2.0+
11 #include <linux/err.h>
13 #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX35) || \
14 defined(CONFIG_MX51) || defined(CONFIG_MX53)
15 #include <asm/arch/imx-regs.h>
19 #define DRIVER_NAME "mxc_nand"
21 struct mxc_nand_host {
22 struct nand_chip *nand;
24 struct mxc_nand_regs __iomem *regs;
26 struct mxc_nand_ip_regs __iomem *ip_regs;
33 unsigned int page_addr;
36 static struct mxc_nand_host mxc_host;
37 static struct mxc_nand_host *host = &mxc_host;
39 /* Define delays in microsec for NAND device operations */
40 #define TROP_US_DELAY 2000
41 /* Macros to get byte and bit positions of ECC */
42 #define COLPOS(x) ((x) >> 3)
43 #define BITPOS(x) ((x) & 0xf)
45 /* Define single bit Error positions in Main & Spare area */
46 #define MAIN_SINGLEBIT_ERROR 0x4
47 #define SPARE_SINGLEBIT_ERROR 0x1
49 /* OOB placement block for use with hardware ecc generation */
50 #if defined(MXC_NFC_V1)
51 #ifndef CONFIG_SYS_NAND_LARGEPAGE
52 static struct nand_ecclayout nand_hw_eccoob = {
54 .eccpos = {6, 7, 8, 9, 10},
55 .oobfree = { {0, 5}, {11, 5}, }
58 static struct nand_ecclayout nand_hw_eccoob2k = {
66 .oobfree = { {2, 4}, {11, 11}, {27, 11}, {43, 11}, {59, 5} },
69 #elif defined(MXC_NFC_V2_1) || defined(MXC_NFC_V3_2)
70 #ifndef CONFIG_SYS_NAND_LARGEPAGE
71 static struct nand_ecclayout nand_hw_eccoob = {
73 .eccpos = {7, 8, 9, 10, 11, 12, 13, 14, 15},
77 static struct nand_ecclayout nand_hw_eccoob2k = {
80 7, 8, 9, 10, 11, 12, 13, 14, 15,
81 23, 24, 25, 26, 27, 28, 29, 30, 31,
82 39, 40, 41, 42, 43, 44, 45, 46, 47,
83 55, 56, 57, 58, 59, 60, 61, 62, 63,
85 .oobfree = { {2, 5}, {16, 7}, {32, 7}, {48, 7} },
90 static int is_16bit_nand(void)
92 #if defined(CONFIG_SYS_NAND_BUSWIDTH_16BIT)
99 static uint32_t *mxc_nand_memcpy32(uint32_t *dest, uint32_t *source, size_t size)
105 __raw_writel(__raw_readl(source++), d++);
110 * This function polls the NANDFC to wait for the basic operation to
111 * complete by checking the INT bit.
113 static void wait_op_done(struct mxc_nand_host *host, int max_retries,
118 while (max_retries-- > 0) {
119 #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
120 tmp = readnfc(&host->regs->config2);
121 if (tmp & NFC_V1_V2_CONFIG2_INT) {
122 tmp &= ~NFC_V1_V2_CONFIG2_INT;
123 writenfc(tmp, &host->regs->config2);
124 #elif defined(MXC_NFC_V3_2)
125 tmp = readnfc(&host->ip_regs->ipc);
126 if (tmp & NFC_V3_IPC_INT) {
127 tmp &= ~NFC_V3_IPC_INT;
128 writenfc(tmp, &host->ip_regs->ipc);
134 if (max_retries < 0) {
135 pr_debug("%s(%d): INT not set\n",
141 * This function issues the specified command to the NAND device and
142 * waits for completion.
144 static void send_cmd(struct mxc_nand_host *host, uint16_t cmd)
146 pr_debug("send_cmd(host, 0x%x)\n", cmd);
148 writenfc(cmd, &host->regs->flash_cmd);
149 writenfc(NFC_CMD, &host->regs->operation);
151 /* Wait for operation to complete */
152 wait_op_done(host, TROP_US_DELAY, cmd);
156 * This function sends an address (or partial address) to the
157 * NAND device. The address is used to select the source/destination for
160 static void send_addr(struct mxc_nand_host *host, uint16_t addr)
162 pr_debug("send_addr(host, 0x%x)\n", addr);
164 writenfc(addr, &host->regs->flash_addr);
165 writenfc(NFC_ADDR, &host->regs->operation);
167 /* Wait for operation to complete */
168 wait_op_done(host, TROP_US_DELAY, addr);
172 * This function requests the NANDFC to initiate the transfer
173 * of data currently in the NANDFC RAM buffer to the NAND device.
175 static void send_prog_page(struct mxc_nand_host *host, uint8_t buf_id,
179 pr_debug("send_prog_page (%d)\n", spare_only);
181 if (is_mxc_nfc_21() || is_mxc_nfc_32()) {
184 * The controller copies the 64 bytes of spare data from
185 * the first 16 bytes of each of the 4 64 byte spare buffers.
186 * Copy the contiguous data starting in spare_area[0] to
187 * the four spare area buffers.
189 for (i = 1; i < 4; i++) {
190 void __iomem *src = &host->regs->spare_area[0][i * 16];
191 void __iomem *dst = &host->regs->spare_area[i][0];
193 mxc_nand_memcpy32(dst, src, 16);
197 #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
198 writenfc(buf_id, &host->regs->buf_addr);
199 #elif defined(MXC_NFC_V3_2)
200 uint32_t tmp = readnfc(&host->regs->config1);
201 tmp &= ~NFC_V3_CONFIG1_RBA_MASK;
202 tmp |= NFC_V3_CONFIG1_RBA(buf_id);
203 writenfc(tmp, &host->regs->config1);
206 /* Configure spare or page+spare access */
207 if (!host->pagesize_2k) {
208 uint32_t config1 = readnfc(&host->regs->config1);
210 config1 |= NFC_CONFIG1_SP_EN;
212 config1 &= ~NFC_CONFIG1_SP_EN;
213 writenfc(config1, &host->regs->config1);
216 writenfc(NFC_INPUT, &host->regs->operation);
218 /* Wait for operation to complete */
219 wait_op_done(host, TROP_US_DELAY, spare_only);
223 * Requests NANDFC to initiate the transfer of data from the
224 * NAND device into in the NANDFC ram buffer.
226 static void send_read_page(struct mxc_nand_host *host, uint8_t buf_id,
229 pr_debug("send_read_page (%d)\n", spare_only);
231 #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
232 writenfc(buf_id, &host->regs->buf_addr);
233 #elif defined(MXC_NFC_V3_2)
234 uint32_t tmp = readnfc(&host->regs->config1);
235 tmp &= ~NFC_V3_CONFIG1_RBA_MASK;
236 tmp |= NFC_V3_CONFIG1_RBA(buf_id);
237 writenfc(tmp, &host->regs->config1);
240 /* Configure spare or page+spare access */
241 if (!host->pagesize_2k) {
242 uint32_t config1 = readnfc(&host->regs->config1);
244 config1 |= NFC_CONFIG1_SP_EN;
246 config1 &= ~NFC_CONFIG1_SP_EN;
247 writenfc(config1, &host->regs->config1);
250 writenfc(NFC_OUTPUT, &host->regs->operation);
252 /* Wait for operation to complete */
253 wait_op_done(host, TROP_US_DELAY, spare_only);
255 if (is_mxc_nfc_21() || is_mxc_nfc_32()) {
259 * The controller copies the 64 bytes of spare data to
260 * the first 16 bytes of each of the 4 spare buffers.
261 * Make the data contiguous starting in spare_area[0].
263 for (i = 1; i < 4; i++) {
264 void __iomem *src = &host->regs->spare_area[i][0];
265 void __iomem *dst = &host->regs->spare_area[0][i * 16];
267 mxc_nand_memcpy32(dst, src, 16);
272 /* Request the NANDFC to perform a read of the NAND device ID. */
273 static void send_read_id(struct mxc_nand_host *host)
277 #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
278 /* NANDFC buffer 0 is used for device ID output */
279 writenfc(0x0, &host->regs->buf_addr);
280 #elif defined(MXC_NFC_V3_2)
281 tmp = readnfc(&host->regs->config1);
282 tmp &= ~NFC_V3_CONFIG1_RBA_MASK;
283 writenfc(tmp, &host->regs->config1);
286 /* Read ID into main buffer */
287 tmp = readnfc(&host->regs->config1);
288 tmp &= ~NFC_CONFIG1_SP_EN;
289 writenfc(tmp, &host->regs->config1);
291 writenfc(NFC_ID, &host->regs->operation);
293 /* Wait for operation to complete */
294 wait_op_done(host, TROP_US_DELAY, 0);
298 * This function requests the NANDFC to perform a read of the
299 * NAND device status and returns the current status.
301 static uint16_t get_dev_status(struct mxc_nand_host *host)
303 #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
304 void __iomem *main_buf = host->regs->main_area[1];
308 /* Issue status request to NAND device */
310 #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
311 /* store the main area1 first word, later do recovery */
312 store = readl(main_buf);
313 /* NANDFC buffer 1 is used for device status */
314 writenfc(1, &host->regs->buf_addr);
317 /* Read status into main buffer */
318 tmp = readnfc(&host->regs->config1);
319 tmp &= ~NFC_CONFIG1_SP_EN;
320 writenfc(tmp, &host->regs->config1);
322 writenfc(NFC_STATUS, &host->regs->operation);
324 /* Wait for operation to complete */
325 wait_op_done(host, TROP_US_DELAY, 0);
327 #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
329 * Status is placed in first word of main buffer
330 * get status, then recovery area 1 data
332 ret = readw(main_buf);
333 writel(store, main_buf);
334 #elif defined(MXC_NFC_V3_2)
335 ret = readnfc(&host->regs->config1) >> 16;
341 /* This function is used by upper layer to checks if device is ready */
342 static int mxc_nand_dev_ready(struct mtd_info *mtd)
345 * NFC handles R/B internally. Therefore, this function
346 * always returns status as ready.
351 static void _mxc_nand_enable_hwecc(struct mtd_info *mtd, int on)
353 struct nand_chip *nand_chip = mtd_to_nand(mtd);
354 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
355 #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
356 uint16_t tmp = readnfc(&host->regs->config1);
359 tmp |= NFC_V1_V2_CONFIG1_ECC_EN;
361 tmp &= ~NFC_V1_V2_CONFIG1_ECC_EN;
362 writenfc(tmp, &host->regs->config1);
363 #elif defined(MXC_NFC_V3_2)
364 uint32_t tmp = readnfc(&host->ip_regs->config2);
367 tmp |= NFC_V3_CONFIG2_ECC_EN;
369 tmp &= ~NFC_V3_CONFIG2_ECC_EN;
370 writenfc(tmp, &host->ip_regs->config2);
374 #ifdef CONFIG_MXC_NAND_HWECC
375 static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode)
378 * If HW ECC is enabled, we turn it on during init. There is
379 * no need to enable again here.
383 #if defined(MXC_NFC_V2_1) || defined(MXC_NFC_V3_2)
384 static int mxc_nand_read_oob_syndrome(struct mtd_info *mtd,
385 struct nand_chip *chip,
388 struct mxc_nand_host *host = nand_get_controller_data(chip);
389 uint8_t *buf = chip->oob_poi;
390 int length = mtd->oobsize;
391 int eccpitch = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
392 uint8_t *bufpoi = buf;
395 pr_debug("%s: Reading OOB area of page %u to oob %p\n",
396 __func__, page, buf);
398 chip->cmdfunc(mtd, NAND_CMD_READOOB, mtd->writesize, page);
399 for (i = 0; i < chip->ecc.steps; i++) {
400 toread = min_t(int, length, chip->ecc.prepad);
402 chip->read_buf(mtd, bufpoi, toread);
406 bufpoi += chip->ecc.bytes;
407 host->col_addr += chip->ecc.bytes;
408 length -= chip->ecc.bytes;
410 toread = min_t(int, length, chip->ecc.postpad);
412 chip->read_buf(mtd, bufpoi, toread);
418 chip->read_buf(mtd, bufpoi, length);
420 _mxc_nand_enable_hwecc(mtd, 0);
421 chip->cmdfunc(mtd, NAND_CMD_READOOB,
422 mtd->writesize + chip->ecc.prepad, page);
423 bufpoi = buf + chip->ecc.prepad;
424 length = mtd->oobsize - chip->ecc.prepad;
425 for (i = 0; i < chip->ecc.steps; i++) {
426 toread = min_t(int, length, chip->ecc.bytes);
427 chip->read_buf(mtd, bufpoi, toread);
430 host->col_addr += chip->ecc.postpad + chip->ecc.prepad;
432 _mxc_nand_enable_hwecc(mtd, 1);
436 static int mxc_nand_read_page_raw_syndrome(struct mtd_info *mtd,
437 struct nand_chip *chip,
442 struct mxc_nand_host *host = nand_get_controller_data(chip);
443 int eccsize = chip->ecc.size;
444 int eccbytes = chip->ecc.bytes;
445 int eccpitch = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
446 uint8_t *oob = chip->oob_poi;
450 _mxc_nand_enable_hwecc(mtd, 0);
451 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
453 for (n = 0, steps = chip->ecc.steps; steps > 0; n++, steps--) {
454 host->col_addr = n * eccsize;
455 chip->read_buf(mtd, buf, eccsize);
458 host->col_addr = mtd->writesize + n * eccpitch;
459 if (chip->ecc.prepad) {
460 chip->read_buf(mtd, oob, chip->ecc.prepad);
461 oob += chip->ecc.prepad;
464 chip->read_buf(mtd, oob, eccbytes);
467 if (chip->ecc.postpad) {
468 chip->read_buf(mtd, oob, chip->ecc.postpad);
469 oob += chip->ecc.postpad;
473 size = mtd->oobsize - (oob - chip->oob_poi);
475 chip->read_buf(mtd, oob, size);
476 _mxc_nand_enable_hwecc(mtd, 1);
481 static int mxc_nand_read_page_syndrome(struct mtd_info *mtd,
482 struct nand_chip *chip,
487 struct mxc_nand_host *host = nand_get_controller_data(chip);
488 int n, eccsize = chip->ecc.size;
489 int eccbytes = chip->ecc.bytes;
490 int eccpitch = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
491 int eccsteps = chip->ecc.steps;
493 uint8_t *oob = chip->oob_poi;
495 pr_debug("Reading page %u to buf %p oob %p\n",
498 /* first read the data area and the available portion of OOB */
499 for (n = 0; eccsteps; n++, eccsteps--, p += eccsize) {
502 host->col_addr = n * eccsize;
504 chip->read_buf(mtd, p, eccsize);
506 host->col_addr = mtd->writesize + n * eccpitch;
508 if (chip->ecc.prepad) {
509 chip->read_buf(mtd, oob, chip->ecc.prepad);
510 oob += chip->ecc.prepad;
513 stat = chip->ecc.correct(mtd, p, oob, NULL);
516 mtd->ecc_stats.failed++;
518 mtd->ecc_stats.corrected += stat;
521 if (chip->ecc.postpad) {
522 chip->read_buf(mtd, oob, chip->ecc.postpad);
523 oob += chip->ecc.postpad;
527 /* Calculate remaining oob bytes */
528 n = mtd->oobsize - (oob - chip->oob_poi);
530 chip->read_buf(mtd, oob, n);
532 /* Then switch ECC off and read the OOB area to get the ECC code */
533 _mxc_nand_enable_hwecc(mtd, 0);
534 chip->cmdfunc(mtd, NAND_CMD_READOOB, mtd->writesize, page);
535 eccsteps = chip->ecc.steps;
536 oob = chip->oob_poi + chip->ecc.prepad;
537 for (n = 0; eccsteps; n++, eccsteps--, p += eccsize) {
538 host->col_addr = mtd->writesize +
541 chip->read_buf(mtd, oob, eccbytes);
542 oob += eccbytes + chip->ecc.postpad;
544 _mxc_nand_enable_hwecc(mtd, 1);
548 static int mxc_nand_write_oob_syndrome(struct mtd_info *mtd,
549 struct nand_chip *chip, int page)
551 struct mxc_nand_host *host = nand_get_controller_data(chip);
552 int eccpitch = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
553 int length = mtd->oobsize;
554 int i, len, status, steps = chip->ecc.steps;
555 const uint8_t *bufpoi = chip->oob_poi;
557 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
558 for (i = 0; i < steps; i++) {
559 len = min_t(int, length, eccpitch);
561 chip->write_buf(mtd, bufpoi, len);
564 host->col_addr += chip->ecc.prepad + chip->ecc.postpad;
567 chip->write_buf(mtd, bufpoi, length);
569 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
570 status = chip->waitfunc(mtd, chip);
571 return status & NAND_STATUS_FAIL ? -EIO : 0;
574 static int mxc_nand_write_page_raw_syndrome(struct mtd_info *mtd,
575 struct nand_chip *chip,
577 int oob_required, int page)
579 struct mxc_nand_host *host = nand_get_controller_data(chip);
580 int eccsize = chip->ecc.size;
581 int eccbytes = chip->ecc.bytes;
582 int eccpitch = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
583 uint8_t *oob = chip->oob_poi;
587 for (n = 0, steps = chip->ecc.steps; steps > 0; n++, steps--) {
588 host->col_addr = n * eccsize;
589 chip->write_buf(mtd, buf, eccsize);
592 host->col_addr = mtd->writesize + n * eccpitch;
594 if (chip->ecc.prepad) {
595 chip->write_buf(mtd, oob, chip->ecc.prepad);
596 oob += chip->ecc.prepad;
599 host->col_addr += eccbytes;
602 if (chip->ecc.postpad) {
603 chip->write_buf(mtd, oob, chip->ecc.postpad);
604 oob += chip->ecc.postpad;
608 size = mtd->oobsize - (oob - chip->oob_poi);
610 chip->write_buf(mtd, oob, size);
614 static int mxc_nand_write_page_syndrome(struct mtd_info *mtd,
615 struct nand_chip *chip,
617 int oob_required, int page)
619 struct mxc_nand_host *host = nand_get_controller_data(chip);
620 int i, n, eccsize = chip->ecc.size;
621 int eccbytes = chip->ecc.bytes;
622 int eccpitch = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
623 int eccsteps = chip->ecc.steps;
624 const uint8_t *p = buf;
625 uint8_t *oob = chip->oob_poi;
627 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
631 n++, eccsteps--, i += eccbytes, p += eccsize) {
632 host->col_addr = n * eccsize;
634 chip->write_buf(mtd, p, eccsize);
636 host->col_addr = mtd->writesize + n * eccpitch;
638 if (chip->ecc.prepad) {
639 chip->write_buf(mtd, oob, chip->ecc.prepad);
640 oob += chip->ecc.prepad;
643 chip->write_buf(mtd, oob, eccbytes);
646 if (chip->ecc.postpad) {
647 chip->write_buf(mtd, oob, chip->ecc.postpad);
648 oob += chip->ecc.postpad;
652 /* Calculate remaining oob bytes */
653 i = mtd->oobsize - (oob - chip->oob_poi);
655 chip->write_buf(mtd, oob, i);
659 static int mxc_nand_correct_data(struct mtd_info *mtd, u_char *dat,
660 u_char *read_ecc, u_char *calc_ecc)
662 struct nand_chip *nand_chip = mtd_to_nand(mtd);
663 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
664 uint32_t ecc_status = readl(&host->regs->ecc_status_result);
665 int subpages = mtd->writesize / nand_chip->subpagesize;
666 int pg2blk_shift = nand_chip->phys_erase_shift -
667 nand_chip->page_shift;
670 if ((ecc_status & 0xf) > 4) {
671 static int last_bad = -1;
673 if (last_bad != host->page_addr >> pg2blk_shift) {
674 last_bad = host->page_addr >> pg2blk_shift;
676 "MXC_NAND: HWECC uncorrectable ECC error"
677 " in block %u page %u subpage %d\n",
678 last_bad, host->page_addr,
679 mtd->writesize / nand_chip->subpagesize
686 } while (subpages > 0);
691 #define mxc_nand_read_page_syndrome NULL
692 #define mxc_nand_read_page_raw_syndrome NULL
693 #define mxc_nand_read_oob_syndrome NULL
694 #define mxc_nand_write_page_syndrome NULL
695 #define mxc_nand_write_page_raw_syndrome NULL
696 #define mxc_nand_write_oob_syndrome NULL
698 static int mxc_nand_correct_data(struct mtd_info *mtd, u_char *dat,
699 u_char *read_ecc, u_char *calc_ecc)
701 struct nand_chip *nand_chip = mtd_to_nand(mtd);
702 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
705 * 1-Bit errors are automatically corrected in HW. No need for
706 * additional correction. 2-Bit errors cannot be corrected by
707 * HW ECC, so we need to return failure
709 uint16_t ecc_status = readnfc(&host->regs->ecc_status_result);
711 if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) {
712 pr_debug("MXC_NAND: HWECC uncorrectable 2-bit ECC error\n");
720 static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
727 static u_char mxc_nand_read_byte(struct mtd_info *mtd)
729 struct nand_chip *nand_chip = mtd_to_nand(mtd);
730 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
733 uint16_t __iomem *main_buf =
734 (uint16_t __iomem *)host->regs->main_area[0];
735 uint16_t __iomem *spare_buf =
736 (uint16_t __iomem *)host->regs->spare_area[0];
742 /* Check for status request */
743 if (host->status_request)
744 return get_dev_status(host) & 0xFF;
746 /* Get column for 16-bit access */
747 col = host->col_addr >> 1;
749 /* If we are accessing the spare region */
750 if (host->spare_only)
751 nfc_word.word = readw(&spare_buf[col]);
753 nfc_word.word = readw(&main_buf[col]);
755 /* Pick upper/lower byte of word from RAM buffer */
756 ret = nfc_word.bytes[host->col_addr & 0x1];
758 /* Update saved column address */
759 if (nand_chip->options & NAND_BUSWIDTH_16)
767 static uint16_t mxc_nand_read_word(struct mtd_info *mtd)
769 struct nand_chip *nand_chip = mtd_to_nand(mtd);
770 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
774 pr_debug("mxc_nand_read_word(col = %d)\n", host->col_addr);
776 col = host->col_addr;
777 /* Adjust saved column address */
778 if (col < mtd->writesize && host->spare_only)
779 col += mtd->writesize;
781 if (col < mtd->writesize) {
782 p = (uint16_t __iomem *)(host->regs->main_area[0] +
785 p = (uint16_t __iomem *)(host->regs->spare_area[0] +
786 ((col - mtd->writesize) >> 1));
795 nfc_word[0].word = readw(p);
796 nfc_word[1].word = readw(p + 1);
798 nfc_word[2].bytes[0] = nfc_word[0].bytes[1];
799 nfc_word[2].bytes[1] = nfc_word[1].bytes[0];
801 ret = nfc_word[2].word;
806 /* Update saved column address */
807 host->col_addr = col + 2;
813 * Write data of length len to buffer buf. The data to be
814 * written on NAND Flash is first copied to RAMbuffer. After the Data Input
815 * Operation by the NFC, the data is written to NAND Flash
817 static void mxc_nand_write_buf(struct mtd_info *mtd,
818 const u_char *buf, int len)
820 struct nand_chip *nand_chip = mtd_to_nand(mtd);
821 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
824 pr_debug("mxc_nand_write_buf(col = %d, len = %d)\n", host->col_addr,
827 col = host->col_addr;
829 /* Adjust saved column address */
830 if (col < mtd->writesize && host->spare_only)
831 col += mtd->writesize;
833 n = mtd->writesize + mtd->oobsize - col;
836 pr_debug("%s:%d: col = %d, n = %d\n", __func__, __LINE__, col, n);
841 if (col < mtd->writesize) {
842 p = host->regs->main_area[0] + (col & ~3);
844 p = host->regs->spare_area[0] -
845 mtd->writesize + (col & ~3);
848 pr_debug("%s:%d: p = %p\n", __func__,
851 if (((col | (unsigned long)&buf[i]) & 3) || n < 4) {
857 nfc_word.word = readl(p);
858 nfc_word.bytes[col & 3] = buf[i++];
862 writel(nfc_word.word, p);
864 int m = mtd->writesize - col;
866 if (col >= mtd->writesize)
871 pr_debug("%s:%d: n = %d, m = %d, i = %d, col = %d\n",
872 __func__, __LINE__, n, m, i, col);
874 mxc_nand_memcpy32(p, (uint32_t *)&buf[i], m);
880 /* Update saved column address */
881 host->col_addr = col;
885 * Read the data buffer from the NAND Flash. To read the data from NAND
886 * Flash first the data output cycle is initiated by the NFC, which copies
887 * the data to RAMbuffer. This data of length len is then copied to buffer buf.
889 static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
891 struct nand_chip *nand_chip = mtd_to_nand(mtd);
892 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
895 pr_debug("mxc_nand_read_buf(col = %d, len = %d)\n", host->col_addr,
898 col = host->col_addr;
900 /* Adjust saved column address */
901 if (col < mtd->writesize && host->spare_only)
902 col += mtd->writesize;
904 n = mtd->writesize + mtd->oobsize - col;
910 if (col < mtd->writesize) {
911 p = host->regs->main_area[0] + (col & ~3);
913 p = host->regs->spare_area[0] -
914 mtd->writesize + (col & ~3);
917 if (((col | (int)&buf[i]) & 3) || n < 4) {
923 nfc_word.word = readl(p);
924 buf[i++] = nfc_word.bytes[col & 3];
928 int m = mtd->writesize - col;
930 if (col >= mtd->writesize)
934 mxc_nand_memcpy32((uint32_t *)&buf[i], p, m);
941 /* Update saved column address */
942 host->col_addr = col;
946 * This function is used by upper layer for select and
947 * deselect of the NAND chip
949 static void mxc_nand_select_chip(struct mtd_info *mtd, int chip)
951 struct nand_chip *nand_chip = mtd_to_nand(mtd);
952 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
956 /* TODO: Disable the NFC clock */
961 /* TODO: Enable the NFC clock */
972 * Used by the upper layer to write command to NAND Flash for
973 * different operations to be carried out on NAND Flash
975 void mxc_nand_command(struct mtd_info *mtd, unsigned command,
976 int column, int page_addr)
978 struct nand_chip *nand_chip = mtd_to_nand(mtd);
979 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
981 pr_debug("mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
982 command, column, page_addr);
984 /* Reset command state information */
985 host->status_request = false;
987 /* Command pre-processing step */
990 case NAND_CMD_STATUS:
992 host->status_request = true;
996 host->page_addr = page_addr;
997 host->col_addr = column;
998 host->spare_only = false;
1001 case NAND_CMD_READOOB:
1002 host->col_addr = column;
1003 host->spare_only = true;
1004 if (host->pagesize_2k)
1005 command = NAND_CMD_READ0; /* only READ0 is valid */
1008 case NAND_CMD_SEQIN:
1009 if (column >= mtd->writesize) {
1011 * before sending SEQIN command for partial write,
1012 * we need read one page out. FSL NFC does not support
1013 * partial write. It always sends out 512+ecc+512+ecc
1014 * for large page nand flash. But for small page nand
1015 * flash, it does support SPARE ONLY operation.
1017 if (host->pagesize_2k) {
1018 /* call ourself to read a page */
1019 mxc_nand_command(mtd, NAND_CMD_READ0, 0,
1023 host->col_addr = column - mtd->writesize;
1024 host->spare_only = true;
1026 /* Set program pointer to spare region */
1027 if (!host->pagesize_2k)
1028 send_cmd(host, NAND_CMD_READOOB);
1030 host->spare_only = false;
1031 host->col_addr = column;
1033 /* Set program pointer to page start */
1034 if (!host->pagesize_2k)
1035 send_cmd(host, NAND_CMD_READ0);
1039 case NAND_CMD_PAGEPROG:
1040 send_prog_page(host, 0, host->spare_only);
1042 if (host->pagesize_2k && is_mxc_nfc_1()) {
1043 /* data in 4 areas */
1044 send_prog_page(host, 1, host->spare_only);
1045 send_prog_page(host, 2, host->spare_only);
1046 send_prog_page(host, 3, host->spare_only);
1052 /* Write out the command to the device. */
1053 send_cmd(host, command);
1055 /* Write out column address, if necessary */
1058 * MXC NANDFC can only perform full page+spare or
1059 * spare-only read/write. When the upper layers perform
1060 * a read/write buffer operation, we will use the saved
1061 * column address to index into the full page.
1064 if (host->pagesize_2k)
1065 /* another col addr cycle for 2k page */
1069 /* Write out page address, if necessary */
1070 if (page_addr != -1) {
1071 u32 page_mask = nand_chip->pagemask;
1073 send_addr(host, page_addr & 0xFF);
1076 } while (page_mask);
1079 /* Command post-processing step */
1082 case NAND_CMD_RESET:
1085 case NAND_CMD_READOOB:
1086 case NAND_CMD_READ0:
1087 if (host->pagesize_2k) {
1088 /* send read confirm command */
1089 send_cmd(host, NAND_CMD_READSTART);
1090 /* read for each AREA */
1091 send_read_page(host, 0, host->spare_only);
1092 if (is_mxc_nfc_1()) {
1093 send_read_page(host, 1, host->spare_only);
1094 send_read_page(host, 2, host->spare_only);
1095 send_read_page(host, 3, host->spare_only);
1098 send_read_page(host, 0, host->spare_only);
1102 case NAND_CMD_READID:
1107 case NAND_CMD_PAGEPROG:
1110 case NAND_CMD_STATUS:
1113 case NAND_CMD_ERASE2:
1118 #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1120 static u8 bbt_pattern[] = {'B', 'b', 't', '0' };
1121 static u8 mirror_pattern[] = {'1', 't', 'b', 'B' };
1123 static struct nand_bbt_descr bbt_main_descr = {
1124 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
1125 NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1130 .pattern = bbt_pattern,
1133 static struct nand_bbt_descr bbt_mirror_descr = {
1134 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
1135 NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1140 .pattern = mirror_pattern,
1145 int board_nand_init(struct nand_chip *this)
1147 struct mtd_info *mtd;
1148 #if defined(MXC_NFC_V2_1) || defined(MXC_NFC_V3_2)
1152 #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1153 this->bbt_options |= NAND_BBT_USE_FLASH;
1154 this->bbt_td = &bbt_main_descr;
1155 this->bbt_md = &bbt_mirror_descr;
1158 /* structures must be linked */
1162 /* 5 us command delay time */
1163 this->chip_delay = 5;
1165 nand_set_controller_data(this, host);
1166 this->dev_ready = mxc_nand_dev_ready;
1167 this->cmdfunc = mxc_nand_command;
1168 this->select_chip = mxc_nand_select_chip;
1169 this->read_byte = mxc_nand_read_byte;
1170 this->read_word = mxc_nand_read_word;
1171 this->write_buf = mxc_nand_write_buf;
1172 this->read_buf = mxc_nand_read_buf;
1174 host->regs = (struct mxc_nand_regs __iomem *)CONFIG_MXC_NAND_REGS_BASE;
1177 (struct mxc_nand_ip_regs __iomem *)CONFIG_MXC_NAND_IP_REGS_BASE;
1181 #ifdef CONFIG_MXC_NAND_HWECC
1182 this->ecc.calculate = mxc_nand_calculate_ecc;
1183 this->ecc.hwctl = mxc_nand_enable_hwecc;
1184 this->ecc.correct = mxc_nand_correct_data;
1185 if (is_mxc_nfc_21() || is_mxc_nfc_32()) {
1186 this->ecc.mode = NAND_ECC_HW_SYNDROME;
1187 this->ecc.read_page = mxc_nand_read_page_syndrome;
1188 this->ecc.read_page_raw = mxc_nand_read_page_raw_syndrome;
1189 this->ecc.read_oob = mxc_nand_read_oob_syndrome;
1190 this->ecc.write_page = mxc_nand_write_page_syndrome;
1191 this->ecc.write_page_raw = mxc_nand_write_page_raw_syndrome;
1192 this->ecc.write_oob = mxc_nand_write_oob_syndrome;
1193 this->ecc.bytes = 9;
1194 this->ecc.prepad = 7;
1196 this->ecc.mode = NAND_ECC_HW;
1200 this->ecc.strength = 1;
1202 this->ecc.strength = 4;
1204 host->pagesize_2k = 0;
1206 this->ecc.size = 512;
1207 _mxc_nand_enable_hwecc(mtd, 1);
1209 this->ecc.layout = &nand_soft_eccoob;
1210 this->ecc.mode = NAND_ECC_SOFT;
1211 _mxc_nand_enable_hwecc(mtd, 0);
1214 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1216 /* NAND bus width determines access functions used by upper layer */
1217 if (is_16bit_nand())
1218 this->options |= NAND_BUSWIDTH_16;
1220 #ifdef CONFIG_SYS_NAND_LARGEPAGE
1221 host->pagesize_2k = 1;
1222 this->ecc.layout = &nand_hw_eccoob2k;
1224 host->pagesize_2k = 0;
1225 this->ecc.layout = &nand_hw_eccoob;
1228 #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
1230 tmp = readnfc(&host->regs->config1);
1231 tmp |= NFC_V2_CONFIG1_ONE_CYCLE;
1232 tmp |= NFC_V2_CONFIG1_ECC_MODE_4;
1233 writenfc(tmp, &host->regs->config1);
1234 if (host->pagesize_2k)
1235 writenfc(64/2, &host->regs->spare_area_size);
1237 writenfc(16/2, &host->regs->spare_area_size);
1242 * Unlock the internal RAM Buffer
1244 writenfc(0x2, &host->regs->config);
1246 /* Blocks to be unlocked */
1247 writenfc(0x0, &host->regs->unlockstart_blkaddr);
1248 /* Originally (Freescale LTIB 2.6.21) 0x4000 was written to the
1249 * unlockend_blkaddr, but the magic 0x4000 does not always work
1250 * when writing more than some 32 megabytes (on 2k page nands)
1251 * However 0xFFFF doesn't seem to have this kind
1252 * of limitation (tried it back and forth several times).
1253 * The linux kernel driver sets this to 0xFFFF for the v2 controller
1254 * only, but probably this was not tested there for v1.
1255 * The very same limitation seems to apply to this kernel driver.
1256 * This might be NAND chip specific and the i.MX31 datasheet is
1257 * extremely vague about the semantics of this register.
1259 writenfc(0xFFFF, &host->regs->unlockend_blkaddr);
1261 /* Unlock Block Command for given address range */
1262 writenfc(0x4, &host->regs->wrprot);
1263 #elif defined(MXC_NFC_V3_2)
1264 writenfc(NFC_V3_CONFIG1_RBA(0), &host->regs->config1);
1265 writenfc(NFC_V3_IPC_CREQ, &host->ip_regs->ipc);
1267 /* Unlock the internal RAM Buffer */
1268 writenfc(NFC_V3_WRPROT_BLS_UNLOCK | NFC_V3_WRPROT_UNLOCK,
1269 &host->ip_regs->wrprot);
1271 /* Blocks to be unlocked */
1272 for (tmp = 0; tmp < CONFIG_SYS_NAND_MAX_CHIPS; tmp++)
1273 writenfc(0x0 | 0xFFFF << 16,
1274 &host->ip_regs->wrprot_unlock_blkaddr[tmp]);
1276 writenfc(0, &host->ip_regs->ipc);
1278 tmp = readnfc(&host->ip_regs->config2);
1279 tmp &= ~(NFC_V3_CONFIG2_SPAS_MASK | NFC_V3_CONFIG2_EDC_MASK |
1280 NFC_V3_CONFIG2_ECC_MODE_8 | NFC_V3_CONFIG2_PS_MASK);
1281 tmp |= NFC_V3_CONFIG2_ONE_CYCLE;
1283 if (host->pagesize_2k) {
1284 tmp |= NFC_V3_CONFIG2_SPAS(64/2);
1285 tmp |= NFC_V3_CONFIG2_PS_2048;
1287 tmp |= NFC_V3_CONFIG2_SPAS(16/2);
1288 tmp |= NFC_V3_CONFIG2_PS_512;
1291 writenfc(tmp, &host->ip_regs->config2);
1293 tmp = NFC_V3_CONFIG3_NUM_OF_DEVS(0) |
1294 NFC_V3_CONFIG3_NO_SDMA |
1295 NFC_V3_CONFIG3_RBB_MODE |
1296 NFC_V3_CONFIG3_SBB(6) | /* Reset default */
1297 NFC_V3_CONFIG3_ADD_OP(0);
1299 if (!(this->options & NAND_BUSWIDTH_16))
1300 tmp |= NFC_V3_CONFIG3_FW8;
1302 writenfc(tmp, &host->ip_regs->config3);
1304 writenfc(0, &host->ip_regs->delay_line);