1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2004-2007 Freescale Semiconductor, Inc.
4 * Copyright 2008 Sascha Hauer, kernel@pengutronix.de
5 * Copyright 2009 Ilya Yanok, <yanok@emcraft.com>
10 #include <linux/err.h>
12 #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX35) || \
13 defined(CONFIG_MX51) || defined(CONFIG_MX53)
14 #include <asm/arch/imx-regs.h>
18 #define DRIVER_NAME "mxc_nand"
20 struct mxc_nand_host {
21 struct nand_chip *nand;
23 struct mxc_nand_regs __iomem *regs;
25 struct mxc_nand_ip_regs __iomem *ip_regs;
32 unsigned int page_addr;
35 static struct mxc_nand_host mxc_host;
36 static struct mxc_nand_host *host = &mxc_host;
38 /* Define delays in microsec for NAND device operations */
39 #define TROP_US_DELAY 2000
40 /* Macros to get byte and bit positions of ECC */
41 #define COLPOS(x) ((x) >> 3)
42 #define BITPOS(x) ((x) & 0xf)
44 /* Define single bit Error positions in Main & Spare area */
45 #define MAIN_SINGLEBIT_ERROR 0x4
46 #define SPARE_SINGLEBIT_ERROR 0x1
48 /* OOB placement block for use with hardware ecc generation */
49 #if defined(MXC_NFC_V1)
50 #ifndef CONFIG_SYS_NAND_LARGEPAGE
51 static struct nand_ecclayout nand_hw_eccoob = {
53 .eccpos = {6, 7, 8, 9, 10},
54 .oobfree = { {0, 5}, {11, 5}, }
57 static struct nand_ecclayout nand_hw_eccoob2k = {
65 .oobfree = { {2, 4}, {11, 11}, {27, 11}, {43, 11}, {59, 5} },
68 #elif defined(MXC_NFC_V2_1) || defined(MXC_NFC_V3_2)
69 #ifndef CONFIG_SYS_NAND_LARGEPAGE
70 static struct nand_ecclayout nand_hw_eccoob = {
72 .eccpos = {7, 8, 9, 10, 11, 12, 13, 14, 15},
76 static struct nand_ecclayout nand_hw_eccoob2k = {
79 7, 8, 9, 10, 11, 12, 13, 14, 15,
80 23, 24, 25, 26, 27, 28, 29, 30, 31,
81 39, 40, 41, 42, 43, 44, 45, 46, 47,
82 55, 56, 57, 58, 59, 60, 61, 62, 63,
84 .oobfree = { {2, 5}, {16, 7}, {32, 7}, {48, 7} },
89 static int is_16bit_nand(void)
91 #if defined(CONFIG_SYS_NAND_BUSWIDTH_16BIT)
98 static uint32_t *mxc_nand_memcpy32(uint32_t *dest, uint32_t *source, size_t size)
104 __raw_writel(__raw_readl(source++), d++);
109 * This function polls the NANDFC to wait for the basic operation to
110 * complete by checking the INT bit.
112 static void wait_op_done(struct mxc_nand_host *host, int max_retries,
117 while (max_retries-- > 0) {
118 #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
119 tmp = readnfc(&host->regs->config2);
120 if (tmp & NFC_V1_V2_CONFIG2_INT) {
121 tmp &= ~NFC_V1_V2_CONFIG2_INT;
122 writenfc(tmp, &host->regs->config2);
123 #elif defined(MXC_NFC_V3_2)
124 tmp = readnfc(&host->ip_regs->ipc);
125 if (tmp & NFC_V3_IPC_INT) {
126 tmp &= ~NFC_V3_IPC_INT;
127 writenfc(tmp, &host->ip_regs->ipc);
133 if (max_retries < 0) {
134 pr_debug("%s(%d): INT not set\n",
140 * This function issues the specified command to the NAND device and
141 * waits for completion.
143 static void send_cmd(struct mxc_nand_host *host, uint16_t cmd)
145 pr_debug("send_cmd(host, 0x%x)\n", cmd);
147 writenfc(cmd, &host->regs->flash_cmd);
148 writenfc(NFC_CMD, &host->regs->operation);
150 /* Wait for operation to complete */
151 wait_op_done(host, TROP_US_DELAY, cmd);
155 * This function sends an address (or partial address) to the
156 * NAND device. The address is used to select the source/destination for
159 static void send_addr(struct mxc_nand_host *host, uint16_t addr)
161 pr_debug("send_addr(host, 0x%x)\n", addr);
163 writenfc(addr, &host->regs->flash_addr);
164 writenfc(NFC_ADDR, &host->regs->operation);
166 /* Wait for operation to complete */
167 wait_op_done(host, TROP_US_DELAY, addr);
171 * This function requests the NANDFC to initiate the transfer
172 * of data currently in the NANDFC RAM buffer to the NAND device.
174 static void send_prog_page(struct mxc_nand_host *host, uint8_t buf_id,
178 pr_debug("send_prog_page (%d)\n", spare_only);
180 if (is_mxc_nfc_21() || is_mxc_nfc_32()) {
183 * The controller copies the 64 bytes of spare data from
184 * the first 16 bytes of each of the 4 64 byte spare buffers.
185 * Copy the contiguous data starting in spare_area[0] to
186 * the four spare area buffers.
188 for (i = 1; i < 4; i++) {
189 void __iomem *src = &host->regs->spare_area[0][i * 16];
190 void __iomem *dst = &host->regs->spare_area[i][0];
192 mxc_nand_memcpy32(dst, src, 16);
196 #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
197 writenfc(buf_id, &host->regs->buf_addr);
198 #elif defined(MXC_NFC_V3_2)
199 uint32_t tmp = readnfc(&host->regs->config1);
200 tmp &= ~NFC_V3_CONFIG1_RBA_MASK;
201 tmp |= NFC_V3_CONFIG1_RBA(buf_id);
202 writenfc(tmp, &host->regs->config1);
205 /* Configure spare or page+spare access */
206 if (!host->pagesize_2k) {
207 uint32_t config1 = readnfc(&host->regs->config1);
209 config1 |= NFC_CONFIG1_SP_EN;
211 config1 &= ~NFC_CONFIG1_SP_EN;
212 writenfc(config1, &host->regs->config1);
215 writenfc(NFC_INPUT, &host->regs->operation);
217 /* Wait for operation to complete */
218 wait_op_done(host, TROP_US_DELAY, spare_only);
222 * Requests NANDFC to initiate the transfer of data from the
223 * NAND device into in the NANDFC ram buffer.
225 static void send_read_page(struct mxc_nand_host *host, uint8_t buf_id,
228 pr_debug("send_read_page (%d)\n", spare_only);
230 #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
231 writenfc(buf_id, &host->regs->buf_addr);
232 #elif defined(MXC_NFC_V3_2)
233 uint32_t tmp = readnfc(&host->regs->config1);
234 tmp &= ~NFC_V3_CONFIG1_RBA_MASK;
235 tmp |= NFC_V3_CONFIG1_RBA(buf_id);
236 writenfc(tmp, &host->regs->config1);
239 /* Configure spare or page+spare access */
240 if (!host->pagesize_2k) {
241 uint32_t config1 = readnfc(&host->regs->config1);
243 config1 |= NFC_CONFIG1_SP_EN;
245 config1 &= ~NFC_CONFIG1_SP_EN;
246 writenfc(config1, &host->regs->config1);
249 writenfc(NFC_OUTPUT, &host->regs->operation);
251 /* Wait for operation to complete */
252 wait_op_done(host, TROP_US_DELAY, spare_only);
254 if (is_mxc_nfc_21() || is_mxc_nfc_32()) {
258 * The controller copies the 64 bytes of spare data to
259 * the first 16 bytes of each of the 4 spare buffers.
260 * Make the data contiguous starting in spare_area[0].
262 for (i = 1; i < 4; i++) {
263 void __iomem *src = &host->regs->spare_area[i][0];
264 void __iomem *dst = &host->regs->spare_area[0][i * 16];
266 mxc_nand_memcpy32(dst, src, 16);
271 /* Request the NANDFC to perform a read of the NAND device ID. */
272 static void send_read_id(struct mxc_nand_host *host)
276 #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
277 /* NANDFC buffer 0 is used for device ID output */
278 writenfc(0x0, &host->regs->buf_addr);
279 #elif defined(MXC_NFC_V3_2)
280 tmp = readnfc(&host->regs->config1);
281 tmp &= ~NFC_V3_CONFIG1_RBA_MASK;
282 writenfc(tmp, &host->regs->config1);
285 /* Read ID into main buffer */
286 tmp = readnfc(&host->regs->config1);
287 tmp &= ~NFC_CONFIG1_SP_EN;
288 writenfc(tmp, &host->regs->config1);
290 writenfc(NFC_ID, &host->regs->operation);
292 /* Wait for operation to complete */
293 wait_op_done(host, TROP_US_DELAY, 0);
297 * This function requests the NANDFC to perform a read of the
298 * NAND device status and returns the current status.
300 static uint16_t get_dev_status(struct mxc_nand_host *host)
302 #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
303 void __iomem *main_buf = host->regs->main_area[1];
307 /* Issue status request to NAND device */
309 #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
310 /* store the main area1 first word, later do recovery */
311 store = readl(main_buf);
312 /* NANDFC buffer 1 is used for device status */
313 writenfc(1, &host->regs->buf_addr);
316 /* Read status into main buffer */
317 tmp = readnfc(&host->regs->config1);
318 tmp &= ~NFC_CONFIG1_SP_EN;
319 writenfc(tmp, &host->regs->config1);
321 writenfc(NFC_STATUS, &host->regs->operation);
323 /* Wait for operation to complete */
324 wait_op_done(host, TROP_US_DELAY, 0);
326 #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
328 * Status is placed in first word of main buffer
329 * get status, then recovery area 1 data
331 ret = readw(main_buf);
332 writel(store, main_buf);
333 #elif defined(MXC_NFC_V3_2)
334 ret = readnfc(&host->regs->config1) >> 16;
340 /* This function is used by upper layer to checks if device is ready */
341 static int mxc_nand_dev_ready(struct mtd_info *mtd)
344 * NFC handles R/B internally. Therefore, this function
345 * always returns status as ready.
350 static void _mxc_nand_enable_hwecc(struct mtd_info *mtd, int on)
352 struct nand_chip *nand_chip = mtd_to_nand(mtd);
353 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
354 #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
355 uint16_t tmp = readnfc(&host->regs->config1);
358 tmp |= NFC_V1_V2_CONFIG1_ECC_EN;
360 tmp &= ~NFC_V1_V2_CONFIG1_ECC_EN;
361 writenfc(tmp, &host->regs->config1);
362 #elif defined(MXC_NFC_V3_2)
363 uint32_t tmp = readnfc(&host->ip_regs->config2);
366 tmp |= NFC_V3_CONFIG2_ECC_EN;
368 tmp &= ~NFC_V3_CONFIG2_ECC_EN;
369 writenfc(tmp, &host->ip_regs->config2);
373 #ifdef CONFIG_MXC_NAND_HWECC
374 static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode)
377 * If HW ECC is enabled, we turn it on during init. There is
378 * no need to enable again here.
382 #if defined(MXC_NFC_V2_1) || defined(MXC_NFC_V3_2)
383 static int mxc_nand_read_oob_syndrome(struct mtd_info *mtd,
384 struct nand_chip *chip,
387 struct mxc_nand_host *host = nand_get_controller_data(chip);
388 uint8_t *buf = chip->oob_poi;
389 int length = mtd->oobsize;
390 int eccpitch = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
391 uint8_t *bufpoi = buf;
394 pr_debug("%s: Reading OOB area of page %u to oob %p\n",
395 __func__, page, buf);
397 chip->cmdfunc(mtd, NAND_CMD_READOOB, mtd->writesize, page);
398 for (i = 0; i < chip->ecc.steps; i++) {
399 toread = min_t(int, length, chip->ecc.prepad);
401 chip->read_buf(mtd, bufpoi, toread);
405 bufpoi += chip->ecc.bytes;
406 host->col_addr += chip->ecc.bytes;
407 length -= chip->ecc.bytes;
409 toread = min_t(int, length, chip->ecc.postpad);
411 chip->read_buf(mtd, bufpoi, toread);
417 chip->read_buf(mtd, bufpoi, length);
419 _mxc_nand_enable_hwecc(mtd, 0);
420 chip->cmdfunc(mtd, NAND_CMD_READOOB,
421 mtd->writesize + chip->ecc.prepad, page);
422 bufpoi = buf + chip->ecc.prepad;
423 length = mtd->oobsize - chip->ecc.prepad;
424 for (i = 0; i < chip->ecc.steps; i++) {
425 toread = min_t(int, length, chip->ecc.bytes);
426 chip->read_buf(mtd, bufpoi, toread);
429 host->col_addr += chip->ecc.postpad + chip->ecc.prepad;
431 _mxc_nand_enable_hwecc(mtd, 1);
435 static int mxc_nand_read_page_raw_syndrome(struct mtd_info *mtd,
436 struct nand_chip *chip,
441 struct mxc_nand_host *host = nand_get_controller_data(chip);
442 int eccsize = chip->ecc.size;
443 int eccbytes = chip->ecc.bytes;
444 int eccpitch = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
445 uint8_t *oob = chip->oob_poi;
449 _mxc_nand_enable_hwecc(mtd, 0);
450 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
452 for (n = 0, steps = chip->ecc.steps; steps > 0; n++, steps--) {
453 host->col_addr = n * eccsize;
454 chip->read_buf(mtd, buf, eccsize);
457 host->col_addr = mtd->writesize + n * eccpitch;
458 if (chip->ecc.prepad) {
459 chip->read_buf(mtd, oob, chip->ecc.prepad);
460 oob += chip->ecc.prepad;
463 chip->read_buf(mtd, oob, eccbytes);
466 if (chip->ecc.postpad) {
467 chip->read_buf(mtd, oob, chip->ecc.postpad);
468 oob += chip->ecc.postpad;
472 size = mtd->oobsize - (oob - chip->oob_poi);
474 chip->read_buf(mtd, oob, size);
475 _mxc_nand_enable_hwecc(mtd, 1);
480 static int mxc_nand_read_page_syndrome(struct mtd_info *mtd,
481 struct nand_chip *chip,
486 struct mxc_nand_host *host = nand_get_controller_data(chip);
487 int n, eccsize = chip->ecc.size;
488 int eccbytes = chip->ecc.bytes;
489 int eccpitch = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
490 int eccsteps = chip->ecc.steps;
492 uint8_t *oob = chip->oob_poi;
494 pr_debug("Reading page %u to buf %p oob %p\n",
497 /* first read the data area and the available portion of OOB */
498 for (n = 0; eccsteps; n++, eccsteps--, p += eccsize) {
501 host->col_addr = n * eccsize;
503 chip->read_buf(mtd, p, eccsize);
505 host->col_addr = mtd->writesize + n * eccpitch;
507 if (chip->ecc.prepad) {
508 chip->read_buf(mtd, oob, chip->ecc.prepad);
509 oob += chip->ecc.prepad;
512 stat = chip->ecc.correct(mtd, p, oob, NULL);
515 mtd->ecc_stats.failed++;
517 mtd->ecc_stats.corrected += stat;
520 if (chip->ecc.postpad) {
521 chip->read_buf(mtd, oob, chip->ecc.postpad);
522 oob += chip->ecc.postpad;
526 /* Calculate remaining oob bytes */
527 n = mtd->oobsize - (oob - chip->oob_poi);
529 chip->read_buf(mtd, oob, n);
531 /* Then switch ECC off and read the OOB area to get the ECC code */
532 _mxc_nand_enable_hwecc(mtd, 0);
533 chip->cmdfunc(mtd, NAND_CMD_READOOB, mtd->writesize, page);
534 eccsteps = chip->ecc.steps;
535 oob = chip->oob_poi + chip->ecc.prepad;
536 for (n = 0; eccsteps; n++, eccsteps--, p += eccsize) {
537 host->col_addr = mtd->writesize +
540 chip->read_buf(mtd, oob, eccbytes);
541 oob += eccbytes + chip->ecc.postpad;
543 _mxc_nand_enable_hwecc(mtd, 1);
547 static int mxc_nand_write_oob_syndrome(struct mtd_info *mtd,
548 struct nand_chip *chip, int page)
550 struct mxc_nand_host *host = nand_get_controller_data(chip);
551 int eccpitch = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
552 int length = mtd->oobsize;
553 int i, len, status, steps = chip->ecc.steps;
554 const uint8_t *bufpoi = chip->oob_poi;
556 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
557 for (i = 0; i < steps; i++) {
558 len = min_t(int, length, eccpitch);
560 chip->write_buf(mtd, bufpoi, len);
563 host->col_addr += chip->ecc.prepad + chip->ecc.postpad;
566 chip->write_buf(mtd, bufpoi, length);
568 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
569 status = chip->waitfunc(mtd, chip);
570 return status & NAND_STATUS_FAIL ? -EIO : 0;
573 static int mxc_nand_write_page_raw_syndrome(struct mtd_info *mtd,
574 struct nand_chip *chip,
576 int oob_required, int page)
578 struct mxc_nand_host *host = nand_get_controller_data(chip);
579 int eccsize = chip->ecc.size;
580 int eccbytes = chip->ecc.bytes;
581 int eccpitch = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
582 uint8_t *oob = chip->oob_poi;
586 for (n = 0, steps = chip->ecc.steps; steps > 0; n++, steps--) {
587 host->col_addr = n * eccsize;
588 chip->write_buf(mtd, buf, eccsize);
591 host->col_addr = mtd->writesize + n * eccpitch;
593 if (chip->ecc.prepad) {
594 chip->write_buf(mtd, oob, chip->ecc.prepad);
595 oob += chip->ecc.prepad;
598 host->col_addr += eccbytes;
601 if (chip->ecc.postpad) {
602 chip->write_buf(mtd, oob, chip->ecc.postpad);
603 oob += chip->ecc.postpad;
607 size = mtd->oobsize - (oob - chip->oob_poi);
609 chip->write_buf(mtd, oob, size);
613 static int mxc_nand_write_page_syndrome(struct mtd_info *mtd,
614 struct nand_chip *chip,
616 int oob_required, int page)
618 struct mxc_nand_host *host = nand_get_controller_data(chip);
619 int i, n, eccsize = chip->ecc.size;
620 int eccbytes = chip->ecc.bytes;
621 int eccpitch = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
622 int eccsteps = chip->ecc.steps;
623 const uint8_t *p = buf;
624 uint8_t *oob = chip->oob_poi;
626 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
630 n++, eccsteps--, i += eccbytes, p += eccsize) {
631 host->col_addr = n * eccsize;
633 chip->write_buf(mtd, p, eccsize);
635 host->col_addr = mtd->writesize + n * eccpitch;
637 if (chip->ecc.prepad) {
638 chip->write_buf(mtd, oob, chip->ecc.prepad);
639 oob += chip->ecc.prepad;
642 chip->write_buf(mtd, oob, eccbytes);
645 if (chip->ecc.postpad) {
646 chip->write_buf(mtd, oob, chip->ecc.postpad);
647 oob += chip->ecc.postpad;
651 /* Calculate remaining oob bytes */
652 i = mtd->oobsize - (oob - chip->oob_poi);
654 chip->write_buf(mtd, oob, i);
658 static int mxc_nand_correct_data(struct mtd_info *mtd, u_char *dat,
659 u_char *read_ecc, u_char *calc_ecc)
661 struct nand_chip *nand_chip = mtd_to_nand(mtd);
662 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
663 uint32_t ecc_status = readl(&host->regs->ecc_status_result);
664 int subpages = mtd->writesize / nand_chip->subpagesize;
665 int pg2blk_shift = nand_chip->phys_erase_shift -
666 nand_chip->page_shift;
669 if ((ecc_status & 0xf) > 4) {
670 static int last_bad = -1;
672 if (last_bad != host->page_addr >> pg2blk_shift) {
673 last_bad = host->page_addr >> pg2blk_shift;
675 "MXC_NAND: HWECC uncorrectable ECC error"
676 " in block %u page %u subpage %d\n",
677 last_bad, host->page_addr,
678 mtd->writesize / nand_chip->subpagesize
685 } while (subpages > 0);
690 #define mxc_nand_read_page_syndrome NULL
691 #define mxc_nand_read_page_raw_syndrome NULL
692 #define mxc_nand_read_oob_syndrome NULL
693 #define mxc_nand_write_page_syndrome NULL
694 #define mxc_nand_write_page_raw_syndrome NULL
695 #define mxc_nand_write_oob_syndrome NULL
697 static int mxc_nand_correct_data(struct mtd_info *mtd, u_char *dat,
698 u_char *read_ecc, u_char *calc_ecc)
700 struct nand_chip *nand_chip = mtd_to_nand(mtd);
701 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
704 * 1-Bit errors are automatically corrected in HW. No need for
705 * additional correction. 2-Bit errors cannot be corrected by
706 * HW ECC, so we need to return failure
708 uint16_t ecc_status = readnfc(&host->regs->ecc_status_result);
710 if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) {
711 pr_debug("MXC_NAND: HWECC uncorrectable 2-bit ECC error\n");
719 static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
726 static u_char mxc_nand_read_byte(struct mtd_info *mtd)
728 struct nand_chip *nand_chip = mtd_to_nand(mtd);
729 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
732 uint16_t __iomem *main_buf =
733 (uint16_t __iomem *)host->regs->main_area[0];
734 uint16_t __iomem *spare_buf =
735 (uint16_t __iomem *)host->regs->spare_area[0];
741 /* Check for status request */
742 if (host->status_request)
743 return get_dev_status(host) & 0xFF;
745 /* Get column for 16-bit access */
746 col = host->col_addr >> 1;
748 /* If we are accessing the spare region */
749 if (host->spare_only)
750 nfc_word.word = readw(&spare_buf[col]);
752 nfc_word.word = readw(&main_buf[col]);
754 /* Pick upper/lower byte of word from RAM buffer */
755 ret = nfc_word.bytes[host->col_addr & 0x1];
757 /* Update saved column address */
758 if (nand_chip->options & NAND_BUSWIDTH_16)
766 static uint16_t mxc_nand_read_word(struct mtd_info *mtd)
768 struct nand_chip *nand_chip = mtd_to_nand(mtd);
769 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
773 pr_debug("mxc_nand_read_word(col = %d)\n", host->col_addr);
775 col = host->col_addr;
776 /* Adjust saved column address */
777 if (col < mtd->writesize && host->spare_only)
778 col += mtd->writesize;
780 if (col < mtd->writesize) {
781 p = (uint16_t __iomem *)(host->regs->main_area[0] +
784 p = (uint16_t __iomem *)(host->regs->spare_area[0] +
785 ((col - mtd->writesize) >> 1));
794 nfc_word[0].word = readw(p);
795 nfc_word[1].word = readw(p + 1);
797 nfc_word[2].bytes[0] = nfc_word[0].bytes[1];
798 nfc_word[2].bytes[1] = nfc_word[1].bytes[0];
800 ret = nfc_word[2].word;
805 /* Update saved column address */
806 host->col_addr = col + 2;
812 * Write data of length len to buffer buf. The data to be
813 * written on NAND Flash is first copied to RAMbuffer. After the Data Input
814 * Operation by the NFC, the data is written to NAND Flash
816 static void mxc_nand_write_buf(struct mtd_info *mtd,
817 const u_char *buf, int len)
819 struct nand_chip *nand_chip = mtd_to_nand(mtd);
820 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
823 pr_debug("mxc_nand_write_buf(col = %d, len = %d)\n", host->col_addr,
826 col = host->col_addr;
828 /* Adjust saved column address */
829 if (col < mtd->writesize && host->spare_only)
830 col += mtd->writesize;
832 n = mtd->writesize + mtd->oobsize - col;
835 pr_debug("%s:%d: col = %d, n = %d\n", __func__, __LINE__, col, n);
840 if (col < mtd->writesize) {
841 p = host->regs->main_area[0] + (col & ~3);
843 p = host->regs->spare_area[0] -
844 mtd->writesize + (col & ~3);
847 pr_debug("%s:%d: p = %p\n", __func__,
850 if (((col | (unsigned long)&buf[i]) & 3) || n < 4) {
856 nfc_word.word = readl(p);
857 nfc_word.bytes[col & 3] = buf[i++];
861 writel(nfc_word.word, p);
863 int m = mtd->writesize - col;
865 if (col >= mtd->writesize)
870 pr_debug("%s:%d: n = %d, m = %d, i = %d, col = %d\n",
871 __func__, __LINE__, n, m, i, col);
873 mxc_nand_memcpy32(p, (uint32_t *)&buf[i], m);
879 /* Update saved column address */
880 host->col_addr = col;
884 * Read the data buffer from the NAND Flash. To read the data from NAND
885 * Flash first the data output cycle is initiated by the NFC, which copies
886 * the data to RAMbuffer. This data of length len is then copied to buffer buf.
888 static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
890 struct nand_chip *nand_chip = mtd_to_nand(mtd);
891 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
894 pr_debug("mxc_nand_read_buf(col = %d, len = %d)\n", host->col_addr,
897 col = host->col_addr;
899 /* Adjust saved column address */
900 if (col < mtd->writesize && host->spare_only)
901 col += mtd->writesize;
903 n = mtd->writesize + mtd->oobsize - col;
909 if (col < mtd->writesize) {
910 p = host->regs->main_area[0] + (col & ~3);
912 p = host->regs->spare_area[0] -
913 mtd->writesize + (col & ~3);
916 if (((col | (int)&buf[i]) & 3) || n < 4) {
922 nfc_word.word = readl(p);
923 buf[i++] = nfc_word.bytes[col & 3];
927 int m = mtd->writesize - col;
929 if (col >= mtd->writesize)
933 mxc_nand_memcpy32((uint32_t *)&buf[i], p, m);
940 /* Update saved column address */
941 host->col_addr = col;
945 * This function is used by upper layer for select and
946 * deselect of the NAND chip
948 static void mxc_nand_select_chip(struct mtd_info *mtd, int chip)
950 struct nand_chip *nand_chip = mtd_to_nand(mtd);
951 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
955 /* TODO: Disable the NFC clock */
960 /* TODO: Enable the NFC clock */
971 * Used by the upper layer to write command to NAND Flash for
972 * different operations to be carried out on NAND Flash
974 void mxc_nand_command(struct mtd_info *mtd, unsigned command,
975 int column, int page_addr)
977 struct nand_chip *nand_chip = mtd_to_nand(mtd);
978 struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
980 pr_debug("mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
981 command, column, page_addr);
983 /* Reset command state information */
984 host->status_request = false;
986 /* Command pre-processing step */
989 case NAND_CMD_STATUS:
991 host->status_request = true;
995 host->page_addr = page_addr;
996 host->col_addr = column;
997 host->spare_only = false;
1000 case NAND_CMD_READOOB:
1001 host->col_addr = column;
1002 host->spare_only = true;
1003 if (host->pagesize_2k)
1004 command = NAND_CMD_READ0; /* only READ0 is valid */
1007 case NAND_CMD_SEQIN:
1008 if (column >= mtd->writesize) {
1010 * before sending SEQIN command for partial write,
1011 * we need read one page out. FSL NFC does not support
1012 * partial write. It always sends out 512+ecc+512+ecc
1013 * for large page nand flash. But for small page nand
1014 * flash, it does support SPARE ONLY operation.
1016 if (host->pagesize_2k) {
1017 /* call ourself to read a page */
1018 mxc_nand_command(mtd, NAND_CMD_READ0, 0,
1022 host->col_addr = column - mtd->writesize;
1023 host->spare_only = true;
1025 /* Set program pointer to spare region */
1026 if (!host->pagesize_2k)
1027 send_cmd(host, NAND_CMD_READOOB);
1029 host->spare_only = false;
1030 host->col_addr = column;
1032 /* Set program pointer to page start */
1033 if (!host->pagesize_2k)
1034 send_cmd(host, NAND_CMD_READ0);
1038 case NAND_CMD_PAGEPROG:
1039 send_prog_page(host, 0, host->spare_only);
1041 if (host->pagesize_2k && is_mxc_nfc_1()) {
1042 /* data in 4 areas */
1043 send_prog_page(host, 1, host->spare_only);
1044 send_prog_page(host, 2, host->spare_only);
1045 send_prog_page(host, 3, host->spare_only);
1051 /* Write out the command to the device. */
1052 send_cmd(host, command);
1054 /* Write out column address, if necessary */
1057 * MXC NANDFC can only perform full page+spare or
1058 * spare-only read/write. When the upper layers perform
1059 * a read/write buffer operation, we will use the saved
1060 * column address to index into the full page.
1063 if (host->pagesize_2k)
1064 /* another col addr cycle for 2k page */
1068 /* Write out page address, if necessary */
1069 if (page_addr != -1) {
1070 u32 page_mask = nand_chip->pagemask;
1072 send_addr(host, page_addr & 0xFF);
1075 } while (page_mask);
1078 /* Command post-processing step */
1081 case NAND_CMD_RESET:
1084 case NAND_CMD_READOOB:
1085 case NAND_CMD_READ0:
1086 if (host->pagesize_2k) {
1087 /* send read confirm command */
1088 send_cmd(host, NAND_CMD_READSTART);
1089 /* read for each AREA */
1090 send_read_page(host, 0, host->spare_only);
1091 if (is_mxc_nfc_1()) {
1092 send_read_page(host, 1, host->spare_only);
1093 send_read_page(host, 2, host->spare_only);
1094 send_read_page(host, 3, host->spare_only);
1097 send_read_page(host, 0, host->spare_only);
1101 case NAND_CMD_READID:
1106 case NAND_CMD_PAGEPROG:
1109 case NAND_CMD_STATUS:
1112 case NAND_CMD_ERASE2:
1117 #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1119 static u8 bbt_pattern[] = {'B', 'b', 't', '0' };
1120 static u8 mirror_pattern[] = {'1', 't', 'b', 'B' };
1122 static struct nand_bbt_descr bbt_main_descr = {
1123 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
1124 NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1129 .pattern = bbt_pattern,
1132 static struct nand_bbt_descr bbt_mirror_descr = {
1133 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
1134 NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1139 .pattern = mirror_pattern,
1144 int board_nand_init(struct nand_chip *this)
1146 struct mtd_info *mtd;
1147 #if defined(MXC_NFC_V2_1) || defined(MXC_NFC_V3_2)
1151 #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1152 this->bbt_options |= NAND_BBT_USE_FLASH;
1153 this->bbt_td = &bbt_main_descr;
1154 this->bbt_md = &bbt_mirror_descr;
1157 /* structures must be linked */
1161 /* 5 us command delay time */
1162 this->chip_delay = 5;
1164 nand_set_controller_data(this, host);
1165 this->dev_ready = mxc_nand_dev_ready;
1166 this->cmdfunc = mxc_nand_command;
1167 this->select_chip = mxc_nand_select_chip;
1168 this->read_byte = mxc_nand_read_byte;
1169 this->read_word = mxc_nand_read_word;
1170 this->write_buf = mxc_nand_write_buf;
1171 this->read_buf = mxc_nand_read_buf;
1173 host->regs = (struct mxc_nand_regs __iomem *)CONFIG_MXC_NAND_REGS_BASE;
1176 (struct mxc_nand_ip_regs __iomem *)CONFIG_MXC_NAND_IP_REGS_BASE;
1180 #ifdef CONFIG_MXC_NAND_HWECC
1181 this->ecc.calculate = mxc_nand_calculate_ecc;
1182 this->ecc.hwctl = mxc_nand_enable_hwecc;
1183 this->ecc.correct = mxc_nand_correct_data;
1184 if (is_mxc_nfc_21() || is_mxc_nfc_32()) {
1185 this->ecc.mode = NAND_ECC_HW_SYNDROME;
1186 this->ecc.read_page = mxc_nand_read_page_syndrome;
1187 this->ecc.read_page_raw = mxc_nand_read_page_raw_syndrome;
1188 this->ecc.read_oob = mxc_nand_read_oob_syndrome;
1189 this->ecc.write_page = mxc_nand_write_page_syndrome;
1190 this->ecc.write_page_raw = mxc_nand_write_page_raw_syndrome;
1191 this->ecc.write_oob = mxc_nand_write_oob_syndrome;
1192 this->ecc.bytes = 9;
1193 this->ecc.prepad = 7;
1195 this->ecc.mode = NAND_ECC_HW;
1199 this->ecc.strength = 1;
1201 this->ecc.strength = 4;
1203 host->pagesize_2k = 0;
1205 this->ecc.size = 512;
1206 _mxc_nand_enable_hwecc(mtd, 1);
1208 this->ecc.layout = &nand_soft_eccoob;
1209 this->ecc.mode = NAND_ECC_SOFT;
1210 _mxc_nand_enable_hwecc(mtd, 0);
1213 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1215 /* NAND bus width determines access functions used by upper layer */
1216 if (is_16bit_nand())
1217 this->options |= NAND_BUSWIDTH_16;
1219 #ifdef CONFIG_SYS_NAND_LARGEPAGE
1220 host->pagesize_2k = 1;
1221 this->ecc.layout = &nand_hw_eccoob2k;
1223 host->pagesize_2k = 0;
1224 this->ecc.layout = &nand_hw_eccoob;
1227 #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
1229 tmp = readnfc(&host->regs->config1);
1230 tmp |= NFC_V2_CONFIG1_ONE_CYCLE;
1231 tmp |= NFC_V2_CONFIG1_ECC_MODE_4;
1232 writenfc(tmp, &host->regs->config1);
1233 if (host->pagesize_2k)
1234 writenfc(64/2, &host->regs->spare_area_size);
1236 writenfc(16/2, &host->regs->spare_area_size);
1241 * Unlock the internal RAM Buffer
1243 writenfc(0x2, &host->regs->config);
1245 /* Blocks to be unlocked */
1246 writenfc(0x0, &host->regs->unlockstart_blkaddr);
1247 /* Originally (Freescale LTIB 2.6.21) 0x4000 was written to the
1248 * unlockend_blkaddr, but the magic 0x4000 does not always work
1249 * when writing more than some 32 megabytes (on 2k page nands)
1250 * However 0xFFFF doesn't seem to have this kind
1251 * of limitation (tried it back and forth several times).
1252 * The linux kernel driver sets this to 0xFFFF for the v2 controller
1253 * only, but probably this was not tested there for v1.
1254 * The very same limitation seems to apply to this kernel driver.
1255 * This might be NAND chip specific and the i.MX31 datasheet is
1256 * extremely vague about the semantics of this register.
1258 writenfc(0xFFFF, &host->regs->unlockend_blkaddr);
1260 /* Unlock Block Command for given address range */
1261 writenfc(0x4, &host->regs->wrprot);
1262 #elif defined(MXC_NFC_V3_2)
1263 writenfc(NFC_V3_CONFIG1_RBA(0), &host->regs->config1);
1264 writenfc(NFC_V3_IPC_CREQ, &host->ip_regs->ipc);
1266 /* Unlock the internal RAM Buffer */
1267 writenfc(NFC_V3_WRPROT_BLS_UNLOCK | NFC_V3_WRPROT_UNLOCK,
1268 &host->ip_regs->wrprot);
1270 /* Blocks to be unlocked */
1271 for (tmp = 0; tmp < CONFIG_SYS_NAND_MAX_CHIPS; tmp++)
1272 writenfc(0x0 | 0xFFFF << 16,
1273 &host->ip_regs->wrprot_unlock_blkaddr[tmp]);
1275 writenfc(0, &host->ip_regs->ipc);
1277 tmp = readnfc(&host->ip_regs->config2);
1278 tmp &= ~(NFC_V3_CONFIG2_SPAS_MASK | NFC_V3_CONFIG2_EDC_MASK |
1279 NFC_V3_CONFIG2_ECC_MODE_8 | NFC_V3_CONFIG2_PS_MASK);
1280 tmp |= NFC_V3_CONFIG2_ONE_CYCLE;
1282 if (host->pagesize_2k) {
1283 tmp |= NFC_V3_CONFIG2_SPAS(64/2);
1284 tmp |= NFC_V3_CONFIG2_PS_2048;
1286 tmp |= NFC_V3_CONFIG2_SPAS(16/2);
1287 tmp |= NFC_V3_CONFIG2_PS_512;
1290 writenfc(tmp, &host->ip_regs->config2);
1292 tmp = NFC_V3_CONFIG3_NUM_OF_DEVS(0) |
1293 NFC_V3_CONFIG3_NO_SDMA |
1294 NFC_V3_CONFIG3_RBB_MODE |
1295 NFC_V3_CONFIG3_SBB(6) | /* Reset default */
1296 NFC_V3_CONFIG3_ADD_OP(0);
1298 if (!(this->options & NAND_BUSWIDTH_16))
1299 tmp |= NFC_V3_CONFIG3_FW8;
1301 writenfc(tmp, &host->ip_regs->config3);
1303 writenfc(0, &host->ip_regs->delay_line);