1 From a5fc495c8dc199ffa997d43331693a5b7ee07270 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Sun, 17 Nov 2013 17:41:46 +0100
4 Subject: [PATCH] ralink: add mt7620 nand driver
6 Signed-off-by: John Crispin <blogic@openwrt.org>
8 drivers/mtd/maps/Kconfig | 4 +
9 drivers/mtd/maps/Makefile | 2 +
10 drivers/mtd/maps/ralink_nand.c | 2136 ++++++++++++++++++++++++++++++++++++++++
11 drivers/mtd/maps/ralink_nand.h | 232 +++++
12 drivers/mtd/nand/Makefile | 2 +-
13 5 files changed, 2375 insertions(+), 1 deletion(-)
14 create mode 100644 drivers/mtd/maps/ralink_nand.c
15 create mode 100644 drivers/mtd/maps/ralink_nand.h
17 --- a/drivers/mtd/maps/Kconfig
18 +++ b/drivers/mtd/maps/Kconfig
19 @@ -424,4 +424,8 @@ config MTD_LATCH_ADDR
21 If compiled as a module, it will be called latch-addr-flash.
23 +config MTD_NAND_MT7620
24 + tristate "Support for NAND on Mediatek MT7620"
25 + depends on RALINK && SOC_MT7620
28 --- a/drivers/mtd/maps/Makefile
29 +++ b/drivers/mtd/maps/Makefile
30 @@ -46,3 +46,5 @@ obj-$(CONFIG_MTD_VMU) += vmu-flash.o
31 obj-$(CONFIG_MTD_GPIO_ADDR) += gpio-addr-flash.o
32 obj-$(CONFIG_MTD_LATCH_ADDR) += latch-addr-flash.o
33 obj-$(CONFIG_MTD_LANTIQ) += lantiq-flash.o
34 +obj-$(CONFIG_MTD_NAND_MT7620) += ralink_nand.o
37 +++ b/drivers/mtd/maps/ralink_nand.c
40 +#include <linux/device.h>
42 +#include <linux/slab.h>
43 +#include <linux/mtd/mtd.h>
44 +#include <linux/delay.h>
45 +#include <linux/module.h>
46 +#include <linux/interrupt.h>
47 +#include <linux/dma-mapping.h>
48 +#include <linux/mtd/partitions.h>
50 +#include <linux/delay.h>
51 +#include <linux/sched.h>
52 +#include <linux/of.h>
53 +#include <linux/platform_device.h>
55 +#include "ralink_nand.h"
56 +#ifdef RANDOM_GEN_BAD_BLOCK
57 +#include <linux/random.h>
60 +#define LARGE_MTD_BOOT_PART_SIZE (CFG_BLOCKSIZE<<2)
61 +#define LARGE_MTD_CONFIG_PART_SIZE (CFG_BLOCKSIZE<<2)
62 +#define LARGE_MTD_FACTORY_PART_SIZE (CFG_BLOCKSIZE<<1)
65 +#define BLOCK_ALIGNED(a) ((a) & (CFG_BLOCKSIZE - 1))
67 +#define READ_STATUS_RETRY 1000
69 +struct mtd_info *ranfc_mtd = NULL;
73 +static int ranfc_bbt = 1;
74 +#if defined (WORKAROUND_RX_BUF_OV)
75 +static int ranfc_verify = 1;
77 +static u32 nand_addrlen;
80 +module_param(ranfc_debug, int, 0644);
81 +module_param(ranfc_bbt, int, 0644);
82 +module_param(ranfc_verify, int, 0644);
86 +#define ra_dbg(args...) do { if (ranfc_debug) printk(args); } while(0)
88 +#define ra_dbg(args...)
91 +#define CLEAR_INT_STATUS() ra_outl(NFC_INT_ST, ra_inl(NFC_INT_ST))
92 +#define NFC_TRANS_DONE() (ra_inl(NFC_INT_ST) & INT_ST_ND_DONE)
94 +int is_nand_page_2048 = 0;
95 +const unsigned int nand_size_map[2][3] = {{25, 30, 30}, {20, 27, 30}};
97 +static int nfc_wait_ready(int snooze_ms);
99 +static const char * const mtk_probe_types[] = { "cmdlinepart", "ofpart", NULL };
104 +static int nfc_chip_reset(void)
108 + //ra_dbg("%s:\n", __func__);
110 + // reset nand flash
111 + ra_outl(NFC_CMD1, 0x0);
112 + ra_outl(NFC_CMD2, 0xff);
113 + ra_outl(NFC_ADDR, 0x0);
114 + ra_outl(NFC_CONF, 0x0411);
116 + status = nfc_wait_ready(5); //erase wait 5us
117 + if (status & NAND_STATUS_FAIL) {
118 + printk("%s: fail \n", __func__);
121 + return (int)(status & NAND_STATUS_FAIL);
128 + * clear NFC and flash chip.
130 +static int nfc_all_reset(void)
134 + ra_dbg("%s: \n", __func__);
136 + // reset controller
137 + ra_outl(NFC_CTRL, ra_inl(NFC_CTRL) | 0x02); //clear data buffer
138 + ra_outl(NFC_CTRL, ra_inl(NFC_CTRL) & ~0x02); //clear data buffer
140 + CLEAR_INT_STATUS();
142 + retry = READ_STATUS_RETRY;
143 + while ((ra_inl(NFC_INT_ST) & 0x02) != 0x02 && retry--);
145 + printk("nfc_all_reset: clean buffer fail \n");
149 + retry = READ_STATUS_RETRY;
150 + while ((ra_inl(NFC_STATUS) & 0x1) != 0x0 && retry--) { //fixme, controller is busy ?
159 +/** NOTICE: only called by nfc_wait_ready().
160 + * @return -1, nfc can not get transction done
163 +static int _nfc_read_status(char *status)
165 + unsigned long cmd1, conf;
166 + int int_st, nfc_st;
170 + conf = 0x000101 | (1 << 20);
172 + //fixme, should we check nfc status?
173 + CLEAR_INT_STATUS();
175 + ra_outl(NFC_CMD1, cmd1);
176 + ra_outl(NFC_CONF, conf);
179 + * 1. since we have no wired ready signal, directly
180 + * calling this function is not gurantee to read right status under ready state.
181 + * 2. the other side, we can not determine how long to become ready, this timeout retry is nonsense.
182 + * 3. SUGGESTION: call nfc_read_status() from nfc_wait_ready(),
183 + * that is aware about caller (in sementics) and has snooze plused nfc ND_DONE.
185 + retry = READ_STATUS_RETRY;
187 + nfc_st = ra_inl(NFC_STATUS);
188 + int_st = ra_inl(NFC_INT_ST);
191 + } while (!(int_st & INT_ST_RX_BUF_RDY) && retry--);
193 + if (!(int_st & INT_ST_RX_BUF_RDY)) {
194 + printk("nfc_read_status: NFC fail, int_st(%x), retry:%x. nfc:%x, reset nfc and flash. \n",
195 + int_st, retry, nfc_st);
197 + *status = NAND_STATUS_FAIL;
201 + *status = (char)(le32_to_cpu(ra_inl(NFC_DATA)) & 0x0ff);
206 + * @return !0, chip protect.
207 + * @return 0, chip not protected.
209 +static int nfc_check_wp(void)
211 + /* Check the WP bit */
212 +#if !defined CONFIG_NOT_SUPPORT_WP
213 + return !!(ra_inl(NFC_CTRL) & 0x01);
218 + ret = _nfc_read_status(&result);
219 + //FIXME, if ret < 0
221 + return !(result & NAND_STATUS_WP);
225 +#if !defined CONFIG_NOT_SUPPORT_RB
227 + * @return !0, chip ready.
228 + * @return 0, chip busy.
230 +static int nfc_device_ready(void)
232 + /* Check the ready */
233 + return !!(ra_inl(NFC_STATUS) & 0x04);
239 + * generic function to get data from flash.
240 + * @return data length reading from flash.
242 +static int _ra_nand_pull_data(char *buf, int len, int use_gdma)
244 +#ifdef RW_DATA_BY_BYTE
247 + __u32 *p = (__u32 *)buf;
250 + unsigned int ret_data;
253 + // receive data by use_gdma
255 + //if (_ra_nand_dma_pull((unsigned long)p, len)) {
257 + printk("%s: fail \n", __func__);
258 + len = -1; //return error
264 + //fixme: retry count size?
265 + retry = READ_STATUS_RETRY;
268 + int_st = ra_inl(NFC_INT_ST);
269 + if (int_st & INT_ST_RX_BUF_RDY) {
271 + ret_data = ra_inl(NFC_DATA);
272 + ra_outl(NFC_INT_ST, INT_ST_RX_BUF_RDY);
273 +#ifdef RW_DATA_BY_BYTE
274 + ret_size = sizeof(unsigned int);
275 + ret_size = min(ret_size, len);
277 + while (ret_size-- > 0) {
278 + //nfc is little endian
279 + *p++ = ret_data & 0x0ff;
283 + ret_size = min(len, 4);
288 + __u8 *q = (__u8 *)p;
289 + while (ret_size-- > 0) {
290 + *q++ = ret_data & 0x0ff;
296 + retry = READ_STATUS_RETRY;
298 + else if (int_st & INT_ST_ND_DONE) {
308 +#ifdef RW_DATA_BY_BYTE
309 + return (int)(p - buf);
311 + return ((int)p - (int)buf);
316 + * generic function to put data into flash.
317 + * @return data length writing into flash.
319 +static int _ra_nand_push_data(char *buf, int len, int use_gdma)
321 +#ifdef RW_DATA_BY_BYTE
324 + __u32 *p = (__u32 *)buf;
327 + unsigned int tx_data = 0;
328 + int tx_size, iter = 0;
330 + // receive data by use_gdma
332 + //if (_ra_nand_dma_push((unsigned long)p, len))
335 + printk("%s: fail \n", __func__);
340 + retry = READ_STATUS_RETRY;
342 + int_st = ra_inl(NFC_INT_ST);
343 + if (int_st & INT_ST_TX_BUF_RDY) {
344 +#ifdef RW_DATA_BY_BYTE
345 + tx_size = min(len, (int)sizeof(unsigned long));
346 + for (iter = 0; iter < tx_size; iter++) {
347 + tx_data |= (*p++ << (8*iter));
350 + tx_size = min(len, 4);
354 + __u8 *q = (__u8 *)p;
355 + for (iter = 0; iter < tx_size; iter++)
356 + tx_data |= (*q++ << (8*iter));
360 + ra_outl(NFC_INT_ST, INT_ST_TX_BUF_RDY);
361 + ra_outl(NFC_DATA, tx_data);
363 + retry = READ_STATUS_RETRY;
365 + else if (int_st & INT_ST_ND_DONE) {
371 + ra_dbg("%s p:%p buf:%p \n", __func__, p, buf);
378 +#ifdef RW_DATA_BY_BYTE
379 + return (int)(p - buf);
381 + return ((int)p - (int)buf);
386 +static int nfc_select_chip(struct ra_nand_chip *ra, int chipnr)
388 +#if (CONFIG_NUMCHIPS == 1)
389 + if (!(chipnr < CONFIG_NUMCHIPS))
397 +/** @return -1: chip_select fail
398 + * 0 : both CE and WP==0 are OK
399 + * 1 : CE OK and WP==1
401 +static int nfc_enable_chip(struct ra_nand_chip *ra, unsigned int offs, int read_only)
403 + int chipnr = offs >> ra->chip_shift;
405 + ra_dbg("%s: offs:%x read_only:%x \n", __func__, offs, read_only);
407 + chipnr = nfc_select_chip(ra, chipnr);
409 + printk("%s: chip select error, offs(%x)\n", __func__, offs);
414 + return nfc_check_wp();
419 +/** wait nand chip becomeing ready and return queried status.
420 + * @param snooze: sleep time in ms unit before polling device ready.
421 + * @return status of nand chip
422 + * @return NAN_STATUS_FAIL if something unexpected.
424 +static int nfc_wait_ready(int snooze_ms)
430 + if (snooze_ms == 0)
433 + schedule_timeout(snooze_ms * HZ / 1000);
435 + snooze_ms = retry = snooze_ms *1000000 / 100 ; // ndelay(100)
437 + while (!NFC_TRANS_DONE() && retry--) {
438 + if (!cond_resched())
442 + if (!NFC_TRANS_DONE()) {
443 + printk("nfc_wait_ready: no transaction done \n");
444 + return NAND_STATUS_FAIL;
447 +#if !defined (CONFIG_NOT_SUPPORT_RB)
449 + while(!(status = nfc_device_ready()) && retry--) {
454 + printk("nfc_wait_ready: no device ready. \n");
455 + return NAND_STATUS_FAIL;
458 + _nfc_read_status(&status);
463 + _nfc_read_status(&status);
464 + if (status & NAND_STATUS_READY)
469 + printk("nfc_wait_ready 2: no device ready, status(%x). \n", status);
476 + * return 0: erase OK
477 + * return -EIO: fail
479 +int nfc_erase_block(struct ra_nand_chip *ra, int row_addr)
481 + unsigned long cmd1, cmd2, bus_addr, conf;
486 + bus_addr = row_addr;
487 + conf = 0x00511 | ((CFG_ROW_ADDR_CYCLE)<<16);
490 + ra_dbg("%s: cmd1: %lx, cmd2:%lx bus_addr: %lx, conf: %lx \n",
491 + __func__, cmd1, cmd2, bus_addr, conf);
493 + //fixme, should we check nfc status?
494 + CLEAR_INT_STATUS();
496 + ra_outl(NFC_CMD1, cmd1);
497 + ra_outl(NFC_CMD2, cmd2);
498 + ra_outl(NFC_ADDR, bus_addr);
499 + ra_outl(NFC_CONF, conf);
501 + status = nfc_wait_ready(3); //erase wait 3ms
502 + if (status & NAND_STATUS_FAIL) {
503 + printk("%s: fail \n", __func__);
511 +static inline int _nfc_read_raw_data(int cmd1, int cmd2, int bus_addr, int bus_addr2, int conf, char *buf, int len, int flags)
515 + CLEAR_INT_STATUS();
516 + ra_outl(NFC_CMD1, cmd1);
517 + ra_outl(NFC_CMD2, cmd2);
518 + ra_outl(NFC_ADDR, bus_addr);
519 +#if defined (CONFIG_RALINK_RT6855) || defined (CONFIG_RALINK_RT6855A) || \
520 + defined (CONFIG_RALINK_MT7620) || defined (CONFIG_RALINK_MT7621)
521 + ra_outl(NFC_ADDR2, bus_addr2);
523 + ra_outl(NFC_CONF, conf);
525 + ret = _ra_nand_pull_data(buf, len, 0);
527 + ra_dbg("%s: ret:%x (%x) \n", __func__, ret, len);
528 + return NAND_STATUS_FAIL;
531 + //FIXME, this section is not necessary
532 + ret = nfc_wait_ready(0); //wait ready
533 + /* to prevent the DATA FIFO 's old data from next operation */
534 + ra_outl(NFC_CTRL, ra_inl(NFC_CTRL) | 0x02); //clear data buffer
535 + ra_outl(NFC_CTRL, ra_inl(NFC_CTRL) & ~0x02); //clear data buffer
537 + if (ret & NAND_STATUS_FAIL) {
538 + printk("%s: fail \n", __func__);
539 + return NAND_STATUS_FAIL;
545 +static inline int _nfc_write_raw_data(int cmd1, int cmd3, int bus_addr, int bus_addr2, int conf, char *buf, int len, int flags)
549 + CLEAR_INT_STATUS();
550 + ra_outl(NFC_CMD1, cmd1);
551 + ra_outl(NFC_CMD3, cmd3);
552 + ra_outl(NFC_ADDR, bus_addr);
553 +#if defined (CONFIG_RALINK_RT6855) || defined (CONFIG_RALINK_RT6855A) || \
554 + defined (CONFIG_RALINK_MT7620) || defined (CONFIG_RALINK_MT7621)
555 + ra_outl(NFC_ADDR2, bus_addr2);
557 + ra_outl(NFC_CONF, conf);
559 + ret = _ra_nand_push_data(buf, len, 0);
561 + ra_dbg("%s: ret:%x (%x) \n", __func__, ret, len);
562 + return NAND_STATUS_FAIL;
565 + ret = nfc_wait_ready(1); //write wait 1ms
566 + /* to prevent the DATA FIFO 's old data from next operation */
567 + ra_outl(NFC_CTRL, ra_inl(NFC_CTRL) | 0x02); //clear data buffer
568 + ra_outl(NFC_CTRL, ra_inl(NFC_CTRL) & ~0x02); //clear data buffer
570 + if (ret & NAND_STATUS_FAIL) {
571 + printk("%s: fail \n", __func__);
572 + return NAND_STATUS_FAIL;
582 +int nfc_read_oob(struct ra_nand_chip *ra, int page, unsigned int offs, char *buf, int len, int flags)
584 + unsigned int cmd1 = 0, cmd2 = 0, conf = 0;
585 + unsigned int bus_addr = 0, bus_addr2 = 0;
586 + unsigned int ecc_en;
590 + int pages_perblock = 1<<(ra->erase_shift - ra->page_shift);
591 + // constrain of nfc read function
593 +#if defined (WORKAROUND_RX_BUF_OV)
594 + BUG_ON (len > 60); //problem of rx-buffer overrun
596 + BUG_ON (offs >> ra->oob_shift); //page boundry
597 + BUG_ON ((unsigned int)(((offs + len) >> ra->oob_shift) + page) >
598 + ((page + pages_perblock) & ~(pages_perblock-1))); //block boundry
600 + use_gdma = flags & FLAG_USE_GDMA;
601 + ecc_en = flags & FLAG_ECC_EN;
602 + bus_addr = (page << (CFG_COLUMN_ADDR_CYCLE*8)) | (offs & ((1<<CFG_COLUMN_ADDR_CYCLE*8) - 1));
604 + if (is_nand_page_2048) {
605 + bus_addr += CFG_PAGESIZE;
606 + bus_addr2 = page >> (CFG_COLUMN_ADDR_CYCLE*8);
609 + conf = 0x000511| ((CFG_ADDR_CYCLE)<<16) | (len << 20);
613 + conf = 0x000141| ((CFG_ADDR_CYCLE)<<16) | (len << 20);
620 + ra_dbg("%s: cmd1:%x, bus_addr:%x, conf:%x, len:%x, flag:%x\n",
621 + __func__, cmd1, bus_addr, conf, len, flags);
623 + status = _nfc_read_raw_data(cmd1, cmd2, bus_addr, bus_addr2, conf, buf, len, flags);
624 + if (status & NAND_STATUS_FAIL) {
625 + printk("%s: fail\n", __func__);
636 +int nfc_write_oob(struct ra_nand_chip *ra, int page, unsigned int offs, char *buf, int len, int flags)
638 + unsigned int cmd1 = 0, cmd3=0, conf = 0;
639 + unsigned int bus_addr = 0, bus_addr2 = 0;
643 + int pages_perblock = 1<<(ra->erase_shift - ra->page_shift);
644 + // constrain of nfc read function
646 + BUG_ON (offs >> ra->oob_shift); //page boundry
647 + BUG_ON ((unsigned int)(((offs + len) >> ra->oob_shift) + page) >
648 + ((page + pages_perblock) & ~(pages_perblock-1))); //block boundry
650 + use_gdma = flags & FLAG_USE_GDMA;
651 + bus_addr = (page << (CFG_COLUMN_ADDR_CYCLE*8)) | (offs & ((1<<CFG_COLUMN_ADDR_CYCLE*8) - 1));
653 + if (is_nand_page_2048) {
656 + bus_addr += CFG_PAGESIZE;
657 + bus_addr2 = page >> (CFG_COLUMN_ADDR_CYCLE*8);
658 + conf = 0x001123 | ((CFG_ADDR_CYCLE)<<16) | ((len) << 20);
663 + conf = 0x001223 | ((CFG_ADDR_CYCLE)<<16) | ((len) << 20);
669 + ra_dbg("%s: cmd1: %x, cmd3: %x bus_addr: %x, conf: %x, len:%x\n",
670 + __func__, cmd1, cmd3, bus_addr, conf, len);
672 + status = _nfc_write_raw_data(cmd1, cmd3, bus_addr, bus_addr2, conf, buf, len, flags);
673 + if (status & NAND_STATUS_FAIL) {
674 + printk("%s: fail \n", __func__);
682 +int nfc_read_page(struct ra_nand_chip *ra, char *buf, int page, int flags);
683 +int nfc_write_page(struct ra_nand_chip *ra, char *buf, int page, int flags);
686 +#if !defined (WORKAROUND_RX_BUF_OV)
687 +static int one_bit_correction(char *ecc, char *expected, int *bytes, int *bits);
688 +int nfc_ecc_verify(struct ra_nand_chip *ra, char *buf, int page, int mode)
694 + //ra_dbg("%s, page:%x mode:%d\n", __func__, page, mode);
696 + if (mode == FL_WRITING) {
697 + int len = CFG_PAGESIZE + CFG_PAGE_OOBSIZE;
698 + int conf = 0x000141| ((CFG_ADDR_CYCLE)<<16) | (len << 20);
699 + conf |= (1<<3); //(ecc_en)
700 + //conf |= (1<<2); // (use_gdma)
702 + p = ra->readback_buffers;
703 + ret = nfc_read_page(ra, ra->readback_buffers, page, FLAG_ECC_EN);
707 + //FIXME, double comfirm
708 + printk("%s: read back fail, try again \n",__func__);
709 + ret = nfc_read_page(ra, ra->readback_buffers, page, FLAG_ECC_EN);
711 + printk("\t%s: read back fail agian \n",__func__);
715 + else if (mode == FL_READING) {
723 + if (!is_nand_page_2048) {
724 + ecc = ra_inl(NFC_ECC);
725 + if (ecc == 0) //clean page.
728 + for (i=0; i<CONFIG_ECC_BYTES; i++) {
729 + int eccpos = CONFIG_ECC_OFFSET + i;
730 + if (*(p + eccpos) != (char)0xff)
732 + if (i == CONFIG_ECC_BYTES - 1) {
733 + printk("skip ecc 0xff at page %x\n", page);
737 + for (i=0; i<CONFIG_ECC_BYTES; i++) {
738 + int eccpos = CONFIG_ECC_OFFSET + i;
739 + if (*(p + eccpos) != *(e + i)) {
740 + printk("%s mode:%s, invalid ecc, page: %x read:%x %x %x, ecc:%x \n",
741 + __func__, (mode == FL_READING)?"read":"write", page,
742 + *(p+ CONFIG_ECC_OFFSET), *(p+ CONFIG_ECC_OFFSET+1), *(p+ CONFIG_ECC_OFFSET +2), ecc);
747 +#if defined (CONFIG_RALINK_RT6855) || defined (CONFIG_RALINK_RT6855A) || \
748 + defined (CONFIG_RALINK_MT7620) || defined (CONFIG_RALINK_MT7621)
750 + int ecc2, ecc3, ecc4, qsz;
751 + char *e2, *e3, *e4;
752 + int correction_flag = 0;
753 + ecc = ra_inl(NFC_ECC_P1);
754 + ecc2 = ra_inl(NFC_ECC_P2);
755 + ecc3 = ra_inl(NFC_ECC_P3);
756 + ecc4 = ra_inl(NFC_ECC_P4);
761 + qsz = CFG_PAGE_OOBSIZE / 4;
762 + if (ecc == 0 && ecc2 == 0 && ecc3 == 0 && ecc4 == 0)
764 + for (i=0; i<CONFIG_ECC_BYTES; i++) {
765 + int eccpos = CONFIG_ECC_OFFSET + i;
766 + if (*(p + eccpos) != (char)0xff)
768 + else if (*(p + eccpos + qsz) != (char)0xff)
770 + else if (*(p + eccpos + qsz*2) != (char)0xff)
772 + else if (*(p + eccpos + qsz*3) != (char)0xff)
774 + if (i == CONFIG_ECC_BYTES - 1) {
775 + printk("skip ecc 0xff at page %x\n", page);
779 + for (i=0; i<CONFIG_ECC_BYTES; i++) {
780 + int eccpos = CONFIG_ECC_OFFSET + i;
781 + if (*(p + eccpos) != *(e + i)) {
782 + printk("%s mode:%s, invalid ecc, page: %x read:%x %x %x, ecc:%x \n",
783 + __func__, (mode == FL_READING)?"read":"write", page,
784 + *(p+ CONFIG_ECC_OFFSET), *(p+ CONFIG_ECC_OFFSET+1), *(p+ CONFIG_ECC_OFFSET +2), ecc);
785 + correction_flag |= 0x1;
787 + if (*(p + eccpos + qsz) != *(e2 + i)) {
788 + printk("%s mode:%s, invalid ecc2, page: %x read:%x %x %x, ecc2:%x \n",
789 + __func__, (mode == FL_READING)?"read":"write", page,
790 + *(p+CONFIG_ECC_OFFSET+qsz), *(p+ CONFIG_ECC_OFFSET+1+qsz), *(p+ CONFIG_ECC_OFFSET+2+qsz), ecc2);
791 + correction_flag |= 0x2;
793 + if (*(p + eccpos + qsz*2) != *(e3 + i)) {
794 + printk("%s mode:%s, invalid ecc3, page: %x read:%x %x %x, ecc3:%x \n",
795 + __func__, (mode == FL_READING)?"read":"write", page,
796 + *(p+CONFIG_ECC_OFFSET+qsz*2), *(p+ CONFIG_ECC_OFFSET+1+qsz*2), *(p+ CONFIG_ECC_OFFSET+2+qsz*2), ecc3);
797 + correction_flag |= 0x4;
799 + if (*(p + eccpos + qsz*3) != *(e4 + i)) {
800 + printk("%s mode:%s, invalid ecc4, page: %x read:%x %x %x, ecc4:%x \n",
801 + __func__, (mode == FL_READING)?"read":"write", page,
802 + *(p+CONFIG_ECC_OFFSET+qsz*3), *(p+ CONFIG_ECC_OFFSET+1+qsz*3), *(p+ CONFIG_ECC_OFFSET+2+qsz*3), ecc4);
803 + correction_flag |= 0x8;
807 + if (correction_flag)
809 + printk("trying to do correction!\n");
810 + if (correction_flag & 0x1)
813 + char *pBuf = p - CFG_PAGESIZE;
815 + if (one_bit_correction(p + CONFIG_ECC_OFFSET, e, &bytes, &bits) == 0)
817 + pBuf[bytes] = pBuf[bytes] ^ (1 << bits);
818 + printk("1. correct byte %d, bit %d!\n", bytes, bits);
822 + printk("failed to correct!\n");
827 + if (correction_flag & 0x2)
830 + char *pBuf = (p - CFG_PAGESIZE) + CFG_PAGESIZE/4;
832 + if (one_bit_correction((p + CONFIG_ECC_OFFSET + qsz), e2, &bytes, &bits) == 0)
834 + pBuf[bytes] = pBuf[bytes] ^ (1 << bits);
835 + printk("2. correct byte %d, bit %d!\n", bytes, bits);
839 + printk("failed to correct!\n");
843 + if (correction_flag & 0x4)
846 + char *pBuf = (p - CFG_PAGESIZE) + CFG_PAGESIZE/2;
848 + if (one_bit_correction((p + CONFIG_ECC_OFFSET + qsz * 2), e3, &bytes, &bits) == 0)
850 + pBuf[bytes] = pBuf[bytes] ^ (1 << bits);
851 + printk("3. correct byte %d, bit %d!\n", bytes, bits);
855 + printk("failed to correct!\n");
859 + if (correction_flag & 0x8)
862 + char *pBuf = (p - CFG_PAGESIZE) + CFG_PAGESIZE*3/4;
864 + if (one_bit_correction((p + CONFIG_ECC_OFFSET + qsz * 3), e4, &bytes, &bits) == 0)
866 + pBuf[bytes] = pBuf[bytes] ^ (1 << bits);
867 + printk("4. correct byte %d, bit %d!\n", bytes, bits);
871 + printk("failed to correct!\n");
887 +void ranfc_dump(void)
890 + for (i=0; i<11; i++) {
893 + printk("%x: %x \n", NFC_BASE + i*4, ra_inl(NFC_BASE + i*4));
898 + * @return 0, ecc OK or corrected.
899 + * @return NAND_STATUS_FAIL, ecc fail.
902 +int nfc_ecc_verify(struct ra_nand_chip *ra, char *buf, int page, int mode)
908 + if (ranfc_verify == 0)
911 + ra_dbg("%s, page:%x mode:%d\n", __func__, page, mode);
913 + if (mode == FL_WRITING) { // read back and memcmp
914 + ret = nfc_read_page(ra, ra->readback_buffers, page, FLAG_NONE);
915 + if (ret != 0) //double comfirm
916 + ret = nfc_read_page(ra, ra->readback_buffers, page, FLAG_NONE);
919 + printk("%s: mode:%x read back fail \n", __func__, mode);
922 + return memcmp(buf, ra->readback_buffers, 1<<ra->page_shift);
925 + if (mode == FL_READING) {
927 + if (ra->sandbox_page == 0)
930 + ret = nfc_write_page(ra, buf, ra->sandbox_page, FLAG_USE_GDMA | FLAG_ECC_EN);
932 + printk("%s, fail write sandbox_page \n", __func__);
937 + * The following command is actually not 'write' command to drive NFC to write flash.
938 + * However, it can make NFC to calculate ECC, that will be used to compare with original ones.
941 + unsigned int conf = 0x001223| (CFG_ADDR_CYCLE<<16) | (0x200 << 20) | (1<<3) | (1<<2);
942 + _nfc_write_raw_data(0xff, 0xff, ra->sandbox_page<<ra->page_shift, conf, buf, 0x200, FLAG_USE_GDMA);
945 + ecc = ra_inl(NFC_ECC);
946 + if (ecc == 0) //clean page.
949 + p = buf + (1<<ra->page_shift);
950 + for (i=0; i<CONFIG_ECC_BYTES; i++) {
951 + int eccpos = CONFIG_ECC_OFFSET + i;
952 + if (*(p + eccpos) != *(e + i)) {
953 + printk("%s mode:%s, invalid ecc, page: %x read:%x %x %x, write:%x \n",
954 + __func__, (mode == FL_READING)?"read":"write", page,
955 + *(p+ CONFIG_ECC_OFFSET), *(p+ CONFIG_ECC_OFFSET+1), *(p+ CONFIG_ECC_OFFSET +2), ecc);
957 + for (i=0; i<528; i++)
958 + printk("%-2x \n", *(buf + i));
973 + * @return -EIO, writing size is less than a page
976 +int nfc_read_page(struct ra_nand_chip *ra, char *buf, int page, int flags)
978 + unsigned int cmd1 = 0, cmd2 = 0, conf = 0;
979 + unsigned int bus_addr = 0, bus_addr2 = 0;
980 + unsigned int ecc_en;
985 + use_gdma = flags & FLAG_USE_GDMA;
986 + ecc_en = flags & FLAG_ECC_EN;
988 + page = page & (CFG_CHIPSIZE - 1); // chip boundary
989 + size = CFG_PAGESIZE + CFG_PAGE_OOBSIZE; //add oobsize
994 +#if defined (WORKAROUND_RX_BUF_OV)
995 + len = min(60, size);
999 + bus_addr = (page << (CFG_COLUMN_ADDR_CYCLE*8)) | (offs & ((1<<CFG_COLUMN_ADDR_CYCLE*8)-1));
1000 + if (is_nand_page_2048) {
1001 + bus_addr2 = page >> (CFG_COLUMN_ADDR_CYCLE*8);
1004 + conf = 0x000511| ((CFG_ADDR_CYCLE)<<16) | (len << 20);
1007 + if (offs & ~(CFG_PAGESIZE-1))
1009 + else if (offs & ~((1<<CFG_COLUMN_ADDR_CYCLE*8)-1))
1014 + conf = 0x000141| ((CFG_ADDR_CYCLE)<<16) | (len << 20);
1016 +#if !defined (WORKAROUND_RX_BUF_OV)
1023 + status = _nfc_read_raw_data(cmd1, cmd2, bus_addr, bus_addr2, conf, buf+offs, len, flags);
1024 + if (status & NAND_STATUS_FAIL) {
1025 + printk("%s: fail \n", __func__);
1033 + // verify and correct ecc
1034 + if ((flags & (FLAG_VERIFY | FLAG_ECC_EN)) == (FLAG_VERIFY | FLAG_ECC_EN)) {
1035 + status = nfc_ecc_verify(ra, buf, page, FL_READING);
1036 + if (status != 0) {
1037 + printk("%s: fail, buf:%x, page:%x, flag:%x\n",
1038 + __func__, (unsigned int)buf, page, flags);
1043 + // fix,e not yet support
1044 + ra->buffers_page = -1; //cached
1052 + * @return -EIO, fail to write
1055 +int nfc_write_page(struct ra_nand_chip *ra, char *buf, int page, int flags)
1057 + unsigned int cmd1 = 0, cmd3, conf = 0;
1058 + unsigned int bus_addr = 0, bus_addr2 = 0;
1059 + unsigned int ecc_en;
1063 + uint8_t *oob = buf + (1<<ra->page_shift);
1065 + use_gdma = flags & FLAG_USE_GDMA;
1066 + ecc_en = flags & FLAG_ECC_EN;
1068 + oob[ra->badblockpos] = 0xff; //tag as good block.
1069 + ra->buffers_page = -1; //cached
1071 + page = page & (CFG_CHIPSIZE-1); //chip boundary
1072 + size = CFG_PAGESIZE + CFG_PAGE_OOBSIZE; //add oobsize
1073 + bus_addr = (page << (CFG_COLUMN_ADDR_CYCLE*8)); //write_page always write from offset 0.
1075 + if (is_nand_page_2048) {
1076 + bus_addr2 = page >> (CFG_COLUMN_ADDR_CYCLE*8);
1079 + conf = 0x001123| ((CFG_ADDR_CYCLE)<<16) | (size << 20);
1084 + conf = 0x001223| ((CFG_ADDR_CYCLE)<<16) | (size << 20);
1087 + conf |= (1<<3); //enable ecc
1092 + ra_dbg("nfc_write_page: cmd1: %x, cmd3: %x bus_addr: %x, conf: %x, len:%x\n",
1093 + cmd1, cmd3, bus_addr, conf, size);
1095 + status = _nfc_write_raw_data(cmd1, cmd3, bus_addr, bus_addr2, conf, buf, size, flags);
1096 + if (status & NAND_STATUS_FAIL) {
1097 + printk("%s: fail \n", __func__);
1102 + if (flags & FLAG_VERIFY) { // verify and correct ecc
1103 + status = nfc_ecc_verify(ra, buf, page, FL_WRITING);
1105 +#ifdef RANDOM_GEN_BAD_BLOCK
1106 + if (((random32() & 0x1ff) == 0x0) && (page >= 0x100)) // randomly create bad block
1108 + printk("hmm... create a bad block at page %x\n", (bus_addr >> 16));
1113 + if (status != 0) {
1114 + printk("%s: ecc_verify fail: ret:%x \n", __func__, status);
1115 + oob[ra->badblockpos] = 0x33;
1116 + page -= page % (CFG_BLOCKSIZE/CFG_PAGESIZE);
1117 + printk("create a bad block at page %x\n", page);
1118 + if (!is_nand_page_2048)
1119 + status = nfc_write_oob(ra, page, ra->badblockpos, oob+ra->badblockpos, 1, flags);
1122 + status = _nfc_write_raw_data(cmd1, cmd3, bus_addr, bus_addr2, conf, buf, size, flags);
1123 + nfc_write_oob(ra, page, 0, oob, 16, FLAG_NONE);
1130 + ra->buffers_page = page; //cached
1136 +/*************************************************************
1137 + * nand internal process
1138 + *************************************************************/
1141 + * nand_release_device - [GENERIC] release chip
1142 + * @mtd: MTD device structure
1144 + * Deselect, release chip lock and wake up anyone waiting on the device
1146 +static void nand_release_device(struct ra_nand_chip *ra)
1148 + /* De-select the NAND device */
1149 + nfc_select_chip(ra, -1);
1151 + /* Release the controller and the chip */
1152 + ra->state = FL_READY;
1154 + mutex_unlock(ra->controller);
1158 + * nand_get_device - [GENERIC] Get chip for selected access
1159 + * @chip: the nand chip descriptor
1160 + * @mtd: MTD device structure
1161 + * @new_state: the state which is requested
1163 + * Get the device and lock it for exclusive access
1166 +nand_get_device(struct ra_nand_chip *ra, int new_state)
1170 + ret = mutex_lock_interruptible(ra->controller);
1172 + ra->state = new_state;
1180 +/*************************************************************
1181 + * nand internal process
1182 + *************************************************************/
1184 +int nand_bbt_get(struct ra_nand_chip *ra, int block)
1187 + bits = block * BBTTAG_BITS;
1192 + return (ra->bbt[byte] >> bits) & BBTTAG_BITS_MASK;
1195 +int nand_bbt_set(struct ra_nand_chip *ra, int block, int tag)
1198 + bits = block * BBTTAG_BITS;
1203 + // If previous tag is bad, dont overwrite it
1204 + if (((ra->bbt[byte] >> bits) & BBTTAG_BITS_MASK) == BBT_TAG_BAD)
1206 + return BBT_TAG_BAD;
1209 + ra->bbt[byte] = (ra->bbt[byte] & ~(BBTTAG_BITS_MASK << bits)) | ((tag & BBTTAG_BITS_MASK) << bits);
1215 + * nand_block_checkbad - [GENERIC] Check if a block is marked bad
1216 + * @mtd: MTD device structure
1217 + * @ofs: offset from device start
1219 + * Check, if the block is bad. Either by reading the bad block table or
1220 + * calling of the scan function.
1222 +int nand_block_checkbad(struct ra_nand_chip *ra, loff_t offs)
1227 + char *str[]= {"UNK", "RES", "BAD", "GOOD"};
1229 + if (ranfc_bbt == 0)
1233 + // align with chip
1235 + offs = offs & ((1<<ra->chip_shift) -1);
1237 + page = offs >> ra->page_shift;
1238 + block = offs >> ra->erase_shift;
1241 + tag = nand_bbt_get(ra, block);
1243 + if (tag == BBT_TAG_UNKNOWN) {
1244 + ret = nfc_read_oob(ra, page, ra->badblockpos, (char*)&tag, 1, FLAG_NONE);
1246 + tag = ((le32_to_cpu(tag) & 0x0ff) == 0x0ff) ? BBT_TAG_GOOD : BBT_TAG_BAD;
1248 + tag = BBT_TAG_BAD;
1250 + nand_bbt_set(ra, block, tag);
1253 + if (tag != BBT_TAG_GOOD) {
1254 + printk("%s: offs:%x tag: %s \n", __func__, (unsigned int)offs, str[tag]);
1265 + * nand_block_markbad -
1267 +int nand_block_markbad(struct ra_nand_chip *ra, loff_t offs)
1274 + // align with chip
1275 + ra_dbg("%s offs: %x \n", __func__, (int)offs);
1277 + offs = offs & ((1<<ra->chip_shift) -1);
1279 + page = offs >> ra->page_shift;
1280 + block = offs >> ra->erase_shift;
1282 + tag = nand_bbt_get(ra, block);
1284 + if (tag == BBT_TAG_BAD) {
1285 + printk("%s: mark repeatedly \n", __func__);
1291 + ret = nfc_read_page(ra, ra->buffers, page, FLAG_NONE);
1293 + printk("%s: fail to read bad block tag \n", __func__);
1297 + ecc = &ra->buffers[(1<<ra->page_shift)+ra->badblockpos];
1298 + if (*ecc == (char)0x0ff) {
1301 + ret = nfc_write_page(ra, ra->buffers, page, FLAG_USE_GDMA);
1303 + printk("%s: fail to write bad block tag \n", __func__);
1309 + nand_bbt_set(ra, block, tag);
1315 +#if defined (WORKAROUND_RX_BUF_OV)
1317 + * to find a bad block for ecc verify of read_page
1319 +unsigned int nand_bbt_find_sandbox(struct ra_nand_chip *ra)
1322 + int chipsize = 1 << ra->chip_shift;
1323 + int blocksize = 1 << ra->erase_shift;
1326 + while (offs < chipsize) {
1327 + if (nand_block_checkbad(ra, offs)) //scan and verify the unknown tag
1329 + offs += blocksize;
1332 + if (offs >= chipsize) {
1333 + offs = chipsize - blocksize;
1336 + nand_bbt_set(ra, (unsigned int)offs>>ra->erase_shift, BBT_TAG_RES); // tag bbt only, instead of update badblockpos of flash.
1337 + return (offs >> ra->page_shift);
1344 + * nand_erase_nand - [Internal] erase block(s)
1345 + * @mtd: MTD device structure
1346 + * @instr: erase instruction
1347 + * @allowbbt: allow erasing the bbt area
1349 + * Erase one ore more blocks
1351 +int _nand_erase_nand(struct ra_nand_chip *ra, struct erase_info *instr)
1353 + int page, len, status, ret;
1354 + unsigned int addr, blocksize = 1<<ra->erase_shift;
1356 + ra_dbg("%s: start:%x, len:%x \n", __func__,
1357 + (unsigned int)instr->addr, (unsigned int)instr->len);
1359 +//#define BLOCK_ALIGNED(a) ((a) & (blocksize - 1)) // already defined
1361 + if (BLOCK_ALIGNED(instr->addr) || BLOCK_ALIGNED(instr->len)) {
1362 + ra_dbg("%s: erase block not aligned, addr:%x len:%x\n", __func__, instr->addr, instr->len);
1366 + instr->fail_addr = 0xffffffff;
1369 + addr = instr->addr;
1370 + instr->state = MTD_ERASING;
1374 + page = (int)(addr >> ra->page_shift);
1376 + /* select device and check wp */
1377 + if (nfc_enable_chip(ra, addr, 0)) {
1378 + printk("%s: nand is write protected \n", __func__);
1379 + instr->state = MTD_ERASE_FAILED;
1383 + /* if we have a bad block, we do not erase bad blocks */
1384 + if (nand_block_checkbad(ra, addr)) {
1385 + printk(KERN_WARNING "nand_erase: attempt to erase a "
1386 + "bad block at 0x%08x\n", addr);
1387 + instr->state = MTD_ERASE_FAILED;
1392 + * Invalidate the page cache, if we erase the block which
1393 + * contains the current cached page
1395 + if (BLOCK_ALIGNED(addr) == BLOCK_ALIGNED(ra->buffers_page << ra->page_shift))
1396 + ra->buffers_page = -1;
1398 + status = nfc_erase_block(ra, page);
1399 + /* See if block erase succeeded */
1401 + printk("%s: failed erase, page 0x%08x\n", __func__, page);
1402 + instr->state = MTD_ERASE_FAILED;
1403 + instr->fail_addr = (page << ra->page_shift);
1408 + /* Increment page address and decrement length */
1410 + addr += blocksize;
1413 + instr->state = MTD_ERASE_DONE;
1417 + ret = ((instr->state == MTD_ERASE_DONE) ? 0 : -EIO);
1418 + /* Do call back function */
1420 + mtd_erase_callback(instr);
1423 + nand_bbt_set(ra, addr >> ra->erase_shift, BBT_TAG_BAD);
1426 + /* Return more or less happy */
1431 +nand_write_oob_buf(struct ra_nand_chip *ra, uint8_t *buf, uint8_t *oob, size_t size,
1432 + int mode, int ooboffs)
1434 + size_t oobsize = 1<<ra->oob_shift;
1435 + struct nand_oobfree *free;
1436 + uint32_t woffs = ooboffs;
1439 + ra_dbg("%s: size:%x, mode:%x, offs:%x \n", __func__, size, mode, ooboffs);
1442 + case MTD_OPS_PLACE_OOB:
1444 + if (ooboffs > oobsize)
1447 + size = min(size, oobsize - ooboffs);
1448 + memcpy(buf + ooboffs, oob, size);
1452 + case MTD_OPS_AUTO_OOB:
1453 + if (ooboffs > ra->oob->oobavail)
1457 + for(free = ra->oob->oobfree; free->length && size; free++) {
1458 + int wlen = free->length - woffs;
1461 + /* Write request not from offset 0 ? */
1467 + bytes = min_t(size_t, size, wlen);
1468 + memcpy (buf + free->offset + woffs, oob, bytes);
1485 +static int nand_read_oob_buf(struct ra_nand_chip *ra, uint8_t *oob, size_t size,
1486 + int mode, int ooboffs)
1488 + size_t oobsize = 1<<ra->oob_shift;
1489 + uint8_t *buf = ra->buffers + (1<<ra->page_shift);
1492 + ra_dbg("%s: size:%x, mode:%x, offs:%x \n", __func__, size, mode, ooboffs);
1495 + case MTD_OPS_PLACE_OOB:
1497 + if (ooboffs > oobsize)
1500 + size = min(size, oobsize - ooboffs);
1501 + memcpy(oob, buf + ooboffs, size);
1504 + case MTD_OPS_AUTO_OOB: {
1505 + struct nand_oobfree *free;
1506 + uint32_t woffs = ooboffs;
1508 + if (ooboffs > ra->oob->oobavail)
1511 + size = min(size, ra->oob->oobavail - ooboffs);
1512 + for(free = ra->oob->oobfree; free->length && size; free++) {
1513 + int wlen = free->length - woffs;
1516 + /* Write request not from offset 0 ? */
1522 + bytes = min_t(size_t, size, wlen);
1523 + memcpy (oob, buf + free->offset + woffs, bytes);
1539 + * nand_do_write_ops - [Internal] NAND write with ECC
1540 + * @mtd: MTD device structure
1541 + * @to: offset to write to
1542 + * @ops: oob operations description structure
1544 + * NAND write with ECC
1546 +static int nand_do_write_ops(struct ra_nand_chip *ra, loff_t to,
1547 + struct mtd_oob_ops *ops)
1550 + uint32_t datalen = ops->len;
1551 + uint32_t ooblen = ops->ooblen;
1552 + uint8_t *oob = ops->oobbuf;
1553 + uint8_t *data = ops->datbuf;
1554 + int pagesize = (1<<ra->page_shift);
1555 + int pagemask = (pagesize -1);
1556 + int oobsize = 1<<ra->oob_shift;
1558 + //int i = 0; //for ra_dbg only
1560 + ra_dbg("%s: to:%x, ops data:%p, oob:%p datalen:%x ooblen:%x, ooboffs:%x oobmode:%x \n",
1561 + __func__, (unsigned int)to, data, oob, datalen, ooblen, ops->ooboffs, ops->mode);
1564 + ops->oobretlen = 0;
1567 + /* Invalidate the page cache, when we write to the cached page */
1568 + ra->buffers_page = -1;
1574 + // oob sequential (burst) write
1575 + if (datalen == 0 && ooblen) {
1576 + int len = ((ooblen + ops->ooboffs) + (ra->oob->oobavail - 1)) / ra->oob->oobavail * oobsize;
1578 + /* select chip, and check if it is write protected */
1579 + if (nfc_enable_chip(ra, addr, 0))
1582 + //FIXME, need sanity check of block boundary
1583 + page = (int)((to & ((1<<ra->chip_shift)-1)) >> ra->page_shift); //chip boundary
1584 + memset(ra->buffers, 0x0ff, pagesize);
1585 + //fixme, should we reserve the original content?
1586 + if (ops->mode == MTD_OPS_AUTO_OOB) {
1587 + nfc_read_oob(ra, page, 0, ra->buffers, len, FLAG_NONE);
1592 + nand_write_oob_buf(ra, ra->buffers, oob, ooblen, ops->mode, ops->ooboffs);
1593 + // write out buffer to chip
1594 + nfc_write_oob(ra, page, 0, ra->buffers, len, FLAG_USE_GDMA);
1597 + ops->oobretlen = ooblen;
1601 + // data sequential (burst) write
1602 + if (datalen && ooblen == 0) {
1603 + // ranfc can not support write_data_burst, since hw-ecc and fifo constraints..
1607 + while(datalen || ooblen) {
1613 + ra_dbg("%s (%d): addr:%x, ops data:%p, oob:%p datalen:%x ooblen:%x, ooboffs:%x \n",
1614 + __func__, i++, (unsigned int)addr, data, oob, datalen, ooblen, ops->ooboffs);
1616 + page = (int)((addr & ((1<<ra->chip_shift)-1)) >> ra->page_shift); //chip boundary
1618 + /* select chip, and check if it is write protected */
1619 + if (nfc_enable_chip(ra, addr, 0))
1623 + if (ops->mode == MTD_OPS_AUTO_OOB) {
1624 + //fixme, this path is not yet varified
1625 + nfc_read_oob(ra, page, 0, ra->buffers + pagesize, oobsize, FLAG_NONE);
1627 + if (oob && ooblen > 0) {
1628 + len = nand_write_oob_buf(ra, ra->buffers + pagesize, oob, ooblen, ops->mode, ops->ooboffs);
1633 + ops->oobretlen += len;
1638 + offs = addr & pagemask;
1639 + len = min_t(size_t, datalen, pagesize - offs);
1640 + if (data && len > 0) {
1641 + memcpy(ra->buffers + offs, data, len); // we can not sure ops->buf wether is DMA-able.
1645 + ops->retlen += len;
1647 + ecc_en = FLAG_ECC_EN;
1649 + ret = nfc_write_page(ra, ra->buffers, page, FLAG_USE_GDMA | FLAG_VERIFY |
1650 + ((ops->mode == MTD_OPS_RAW || ops->mode == MTD_OPS_PLACE_OOB) ? 0 : ecc_en ));
1652 + nand_bbt_set(ra, addr >> ra->erase_shift, BBT_TAG_BAD);
1656 + nand_bbt_set(ra, addr >> ra->erase_shift, BBT_TAG_GOOD);
1658 + addr = (page+1) << ra->page_shift;
1665 + * nand_do_read_ops - [Internal] Read data with ECC
1667 + * @mtd: MTD device structure
1668 + * @from: offset to read from
1669 + * @ops: oob ops structure
1671 + * Internal function. Called with chip held.
1673 +static int nand_do_read_ops(struct ra_nand_chip *ra, loff_t from,
1674 + struct mtd_oob_ops *ops)
1677 + uint32_t datalen = ops->len;
1678 + uint32_t ooblen = ops->ooblen;
1679 + uint8_t *oob = ops->oobbuf;
1680 + uint8_t *data = ops->datbuf;
1681 + int pagesize = (1<<ra->page_shift);
1682 + int pagemask = (pagesize -1);
1683 + loff_t addr = from;
1684 + //int i = 0; //for ra_dbg only
1686 + ra_dbg("%s: addr:%x, ops data:%p, oob:%p datalen:%x ooblen:%x, ooboffs:%x \n",
1687 + __func__, (unsigned int)addr, data, oob, datalen, ooblen, ops->ooboffs);
1690 + ops->oobretlen = 0;
1695 + while(datalen || ooblen) {
1700 + ra_dbg("%s (%d): addr:%x, ops data:%p, oob:%p datalen:%x ooblen:%x, ooboffs:%x \n",
1701 + __func__, i++, (unsigned int)addr, data, oob, datalen, ooblen, ops->ooboffs);
1703 + if (nfc_enable_chip(ra, addr, 1) < 0)
1706 + page = (int)((addr & ((1<<ra->chip_shift)-1)) >> ra->page_shift);
1708 + ret = nfc_read_page(ra, ra->buffers, page, FLAG_VERIFY |
1709 + ((ops->mode == MTD_OPS_RAW || ops->mode == MTD_OPS_PLACE_OOB) ? 0: FLAG_ECC_EN ));
1710 + //FIXME, something strange here, some page needs 2 more tries to guarantee read success.
1712 + printk("read again:\n");
1713 + ret = nfc_read_page(ra, ra->buffers, page, FLAG_VERIFY |
1714 + ((ops->mode == MTD_OPS_RAW || ops->mode == MTD_OPS_PLACE_OOB) ? 0: FLAG_ECC_EN ));
1717 + printk("read again fail \n");
1718 + nand_bbt_set(ra, addr >> ra->erase_shift, BBT_TAG_BAD);
1719 + if ((ret != -EUCLEAN) && (ret != -EBADMSG)) {
1723 + /* ecc verification fail, but data need to be returned. */
1727 + printk(" read agian susccess \n");
1732 + if (oob && ooblen > 0) {
1733 + len = nand_read_oob_buf(ra, oob, ooblen, ops->mode, ops->ooboffs);
1735 + printk("nand_read_oob_buf: fail return %x \n", len);
1740 + ops->oobretlen += len;
1745 + offs = addr & pagemask;
1746 + len = min_t(size_t, datalen, pagesize - offs);
1747 + if (data && len > 0) {
1748 + memcpy(data, ra->buffers + offs, len); // we can not sure ops->buf wether is DMA-able.
1752 + ops->retlen += len;
1758 + nand_bbt_set(ra, addr >> ra->erase_shift, BBT_TAG_GOOD);
1759 + // address go further to next page, instead of increasing of length of write. This avoids some special cases wrong.
1760 + addr = (page+1) << ra->page_shift;
1766 +ramtd_nand_erase(struct mtd_info *mtd, struct erase_info *instr)
1768 + struct ra_nand_chip *ra = (struct ra_nand_chip *)mtd->priv;
1771 + ra_dbg("%s: start:%x, len:%x \n", __func__,
1772 + (unsigned int)instr->addr, (unsigned int)instr->len);
1774 + nand_get_device(ra, FL_ERASING);
1775 + ret = _nand_erase_nand((struct ra_nand_chip *)mtd->priv, instr);
1776 + nand_release_device(ra);
1782 +ramtd_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
1783 + size_t *retlen, const uint8_t *buf)
1785 + struct ra_nand_chip *ra = mtd->priv;
1786 + struct mtd_oob_ops ops;
1789 + ra_dbg("%s: to 0x%x len=0x%x\n", __func__, to, len);
1791 + if ((to + len) > mtd->size)
1797 + nand_get_device(ra, FL_WRITING);
1799 + memset(&ops, 0, sizeof(ops));
1801 + ops.datbuf = (uint8_t *)buf;
1802 + ops.oobbuf = NULL;
1803 + ops.mode = MTD_OPS_AUTO_OOB;
1805 + ret = nand_do_write_ops(ra, to, &ops);
1807 + *retlen = ops.retlen;
1809 + nand_release_device(ra);
1815 +ramtd_nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1816 + size_t *retlen, uint8_t *buf)
1819 + struct ra_nand_chip *ra = mtd->priv;
1821 + struct mtd_oob_ops ops;
1823 + ra_dbg("%s: mtd:%p from:%x, len:%x, buf:%p \n", __func__, mtd, (unsigned int)from, len, buf);
1825 + /* Do not allow reads past end of device */
1826 + if ((from + len) > mtd->size)
1831 + nand_get_device(ra, FL_READING);
1833 + memset(&ops, 0, sizeof(ops));
1836 + ops.oobbuf = NULL;
1837 + ops.mode = MTD_OPS_AUTO_OOB;
1839 + ret = nand_do_read_ops(ra, from, &ops);
1841 + *retlen = ops.retlen;
1843 + nand_release_device(ra);
1850 +ramtd_nand_readoob(struct mtd_info *mtd, loff_t from,
1851 + struct mtd_oob_ops *ops)
1853 + struct ra_nand_chip *ra = mtd->priv;
1856 + ra_dbg("%s: \n", __func__);
1858 + nand_get_device(ra, FL_READING);
1860 + ret = nand_do_read_ops(ra, from, ops);
1862 + nand_release_device(ra);
1868 +ramtd_nand_writeoob(struct mtd_info *mtd, loff_t to,
1869 + struct mtd_oob_ops *ops)
1871 + struct ra_nand_chip *ra = mtd->priv;
1874 + nand_get_device(ra, FL_READING);
1875 + ret = nand_do_write_ops(ra, to, ops);
1876 + nand_release_device(ra);
1882 +ramtd_nand_block_isbad(struct mtd_info *mtd, loff_t offs)
1884 + if (offs > mtd->size)
1887 + return nand_block_checkbad((struct ra_nand_chip *)mtd->priv, offs);
1891 +ramtd_nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
1893 + struct ra_nand_chip *ra = mtd->priv;
1896 + ra_dbg("%s: \n", __func__);
1897 + nand_get_device(ra, FL_WRITING);
1898 + ret = nand_block_markbad(ra, ofs);
1899 + nand_release_device(ra);
1904 +// 1-bit error detection
1905 +static int one_bit_correction(char *ecc1, char *ecc2, int *bytes, int *bits)
1907 + // check if ecc and expected are all valid
1908 + char *p, nibble, crumb;
1909 + int i, xor, iecc1 = 0, iecc2 = 0;
1911 + printk("correction : %x %x %x\n", ecc1[0], ecc1[1], ecc1[2]);
1912 + printk("correction : %x %x %x\n", ecc2[0], ecc2[1], ecc2[2]);
1915 + for (i = 0; i < CONFIG_ECC_BYTES; i++)
1917 + nibble = *(p+i) & 0xf;
1918 + if ((nibble != 0x0) && (nibble != 0xf) && (nibble != 0x3) && (nibble != 0xc) &&
1919 + (nibble != 0x5) && (nibble != 0xa) && (nibble != 0x6) && (nibble != 0x9))
1921 + nibble = ((*(p+i)) >> 4) & 0xf;
1922 + if ((nibble != 0x0) && (nibble != 0xf) && (nibble != 0x3) && (nibble != 0xc) &&
1923 + (nibble != 0x5) && (nibble != 0xa) && (nibble != 0x6) && (nibble != 0x9))
1928 + for (i = 0; i < CONFIG_ECC_BYTES; i++)
1930 + nibble = *(p+i) & 0xf;
1931 + if ((nibble != 0x0) && (nibble != 0xf) && (nibble != 0x3) && (nibble != 0xc) &&
1932 + (nibble != 0x5) && (nibble != 0xa) && (nibble != 0x6) && (nibble != 0x9))
1934 + nibble = ((*(p+i)) >> 4) & 0xf;
1935 + if ((nibble != 0x0) && (nibble != 0xf) && (nibble != 0x3) && (nibble != 0xc) &&
1936 + (nibble != 0x5) && (nibble != 0xa) && (nibble != 0x6) && (nibble != 0x9))
1940 + memcpy(&iecc1, ecc1, 3);
1941 + memcpy(&iecc2, ecc2, 3);
1943 + xor = iecc1 ^ iecc2;
1944 + printk("xor = %x (%x %x)\n", xor, iecc1, iecc2);
1947 + for (i = 0; i < 9; i++)
1949 + crumb = (xor >> (2*i)) & 0x3;
1950 + if ((crumb == 0x0) || (crumb == 0x3))
1953 + *bytes += (1 << i);
1957 + for (i = 0; i < 3; i++)
1959 + crumb = (xor >> (18 + 2*i)) & 0x3;
1960 + if ((crumb == 0x0) || (crumb == 0x3))
1963 + *bits += (1 << i);
1971 +/************************************************************
1972 + * the init/exit section.
1975 +static struct nand_ecclayout ra_oob_layout = {
1976 + .eccbytes = CONFIG_ECC_BYTES,
1977 + .eccpos = {5, 6, 7},
1979 + {.offset = 0, .length = 4},
1980 + {.offset = 8, .length = 8},
1981 + {.offset = 0, .length = 0}
1983 +#define RA_CHIP_OOB_AVAIL (4+8)
1984 + .oobavail = RA_CHIP_OOB_AVAIL,
1985 + // 5th byte is bad-block flag.
1989 +mtk_nand_probe(struct platform_device *pdev)
1991 + struct mtd_part_parser_data ppdata;
1992 + struct ra_nand_chip *ra;
1993 + int alloc_size, bbt_size, buffers_size, reg, err;
1994 + unsigned char chip_mode = 12;
1996 +/* if(ra_check_flash_type()!=BOOT_FROM_NAND) {
2000 + //FIXME: config 512 or 2048-byte page according to HWCONF
2001 +#if defined (CONFIG_RALINK_RT6855A)
2002 + reg = ra_inl(RALINK_SYSCTL_BASE+0x8c);
2003 + chip_mode = ((reg>>28) & 0x3)|(((reg>>22) & 0x3)<<2);
2004 + if (chip_mode == 1) {
2005 + printk("! nand 2048\n");
2006 + ra_or(NFC_CONF1, 1);
2007 + is_nand_page_2048 = 1;
2011 + printk("! nand 512\n");
2012 + ra_and(NFC_CONF1, ~1);
2013 + is_nand_page_2048 = 0;
2016 +#elif (defined (CONFIG_RALINK_MT7620) || defined (CONFIG_RALINK_RT6855))
2017 + ra_outl(RALINK_SYSCTL_BASE+0x60, ra_inl(RALINK_SYSCTL_BASE+0x60) & ~(0x3<<18));
2018 + reg = ra_inl(RALINK_SYSCTL_BASE+0x10);
2019 + chip_mode = (reg & 0x0F);
2020 + if((chip_mode==1)||(chip_mode==11)) {
2021 + ra_or(NFC_CONF1, 1);
2022 + is_nand_page_2048 = 1;
2023 + nand_addrlen = ((chip_mode!=11) ? 4 : 5);
2024 + printk("!!! nand page size = 2048, addr len=%d\n", nand_addrlen);
2027 + ra_and(NFC_CONF1, ~1);
2028 + is_nand_page_2048 = 0;
2029 + nand_addrlen = ((chip_mode!=10) ? 3 : 4);
2030 + printk("!!! nand page size = 512, addr len=%d\n", nand_addrlen);
2033 + is_nand_page_2048 = 0;
2035 + printk("!!! nand page size = 512, addr len=%d\n", nand_addrlen);
2038 +#if defined (CONFIG_RALINK_RT6855A) || defined (CONFIG_RALINK_MT7620) || defined (CONFIG_RALINK_RT6855)
2039 + //config ECC location
2040 + ra_and(NFC_CONF1, 0xfff000ff);
2041 + ra_or(NFC_CONF1, ((CONFIG_ECC_OFFSET + 2) << 16) +
2042 + ((CONFIG_ECC_OFFSET + 1) << 12) +
2043 + (CONFIG_ECC_OFFSET << 8));
2046 +#define ALIGNE_16(a) (((unsigned long)(a)+15) & ~15)
2047 + buffers_size = ALIGNE_16((1<<CONFIG_PAGE_SIZE_BIT) + (1<<CONFIG_OOBSIZE_PER_PAGE_BIT)); //ra->buffers
2048 + bbt_size = BBTTAG_BITS * (1<<(CONFIG_CHIP_SIZE_BIT - (CONFIG_PAGE_SIZE_BIT + CONFIG_NUMPAGE_PER_BLOCK_BIT))) / 8; //ra->bbt
2049 + bbt_size = ALIGNE_16(bbt_size);
2051 + alloc_size = buffers_size + bbt_size;
2052 + alloc_size += buffers_size; //for ra->readback_buffers
2053 + alloc_size += sizeof(*ra);
2054 + alloc_size += sizeof(*ranfc_mtd);
2056 + //make sure gpio-0 is input
2057 + ra_outl(RALINK_PIO_BASE+0x24, ra_inl(RALINK_PIO_BASE+0x24) & ~0x01);
2059 + ra = (struct ra_nand_chip *)kzalloc(alloc_size, GFP_KERNEL | GFP_DMA);
2061 + printk("%s: mem alloc fail \n", __func__);
2064 + memset(ra, 0, alloc_size);
2067 + ra->buffers = (char *)((char *)ra + sizeof(*ra));
2068 + ra->readback_buffers = ra->buffers + buffers_size;
2069 + ra->bbt = ra->readback_buffers + buffers_size;
2070 + ranfc_mtd = (struct mtd_info *)(ra->bbt + bbt_size);
2073 + ra->numchips = CONFIG_NUMCHIPS;
2074 + ra->chip_shift = CONFIG_CHIP_SIZE_BIT;
2075 + ra->page_shift = CONFIG_PAGE_SIZE_BIT;
2076 + ra->oob_shift = CONFIG_OOBSIZE_PER_PAGE_BIT;
2077 + ra->erase_shift = (CONFIG_PAGE_SIZE_BIT + CONFIG_NUMPAGE_PER_BLOCK_BIT);
2078 + ra->badblockpos = CONFIG_BAD_BLOCK_POS;
2079 + ra_oob_layout.eccpos[0] = CONFIG_ECC_OFFSET;
2080 + ra_oob_layout.eccpos[1] = CONFIG_ECC_OFFSET + 1;
2081 + ra_oob_layout.eccpos[2] = CONFIG_ECC_OFFSET + 2;
2082 + ra->oob = &ra_oob_layout;
2083 + ra->buffers_page = -1;
2085 +#if defined (WORKAROUND_RX_BUF_OV)
2086 + if (ranfc_verify) {
2087 + ra->sandbox_page = nand_bbt_find_sandbox(ra);
2090 + ra_outl(NFC_CTRL, ra_inl(NFC_CTRL) | 0x01); //set wp to high
2093 + ranfc_mtd->type = MTD_NANDFLASH;
2094 + ranfc_mtd->flags = MTD_CAP_NANDFLASH;
2095 + ranfc_mtd->size = CONFIG_NUMCHIPS * CFG_CHIPSIZE;
2096 + ranfc_mtd->erasesize = CFG_BLOCKSIZE;
2097 + ranfc_mtd->writesize = CFG_PAGESIZE;
2098 + ranfc_mtd->oobsize = CFG_PAGE_OOBSIZE;
2099 + ranfc_mtd->oobavail = RA_CHIP_OOB_AVAIL;
2100 + ranfc_mtd->name = "ra_nfc";
2101 + //ranfc_mtd->index
2102 + ranfc_mtd->ecclayout = &ra_oob_layout;
2103 + //ranfc_mtd->numberaseregions
2104 + //ranfc_mtd->eraseregions
2105 + //ranfc_mtd->bansize
2106 + ranfc_mtd->_erase = ramtd_nand_erase;
2107 + //ranfc_mtd->point
2108 + //ranfc_mtd->unpoint
2109 + ranfc_mtd->_read = ramtd_nand_read;
2110 + ranfc_mtd->_write = ramtd_nand_write;
2111 + ranfc_mtd->_read_oob = ramtd_nand_readoob;
2112 + ranfc_mtd->_write_oob = ramtd_nand_writeoob;
2113 + //ranfc_mtd->get_fact_prot_info; ranfc_mtd->read_fact_prot_reg;
2114 + //ranfc_mtd->get_user_prot_info; ranfc_mtd->read_user_prot_reg;
2115 + //ranfc_mtd->write_user_prot_reg; ranfc_mtd->lock_user_prot_reg;
2116 + //ranfc_mtd->writev; ranfc_mtd->sync; ranfc_mtd->lock; ranfc_mtd->unlock; ranfc_mtd->suspend; ranfc_mtd->resume;
2117 + ranfc_mtd->_block_isbad = ramtd_nand_block_isbad;
2118 + ranfc_mtd->_block_markbad = ramtd_nand_block_markbad;
2119 + //ranfc_mtd->reboot_notifier
2120 + //ranfc_mtd->ecc_stats;
2123 + //ranfc_mtd->get_device; ranfc_mtd->put_device
2124 + ranfc_mtd->priv = ra;
2126 + ranfc_mtd->owner = THIS_MODULE;
2127 + ra->controller = &ra->hwcontrol;
2128 + mutex_init(ra->controller);
2130 + printk("%s: alloc %x, at %p , btt(%p, %x), ranfc_mtd:%p\n",
2131 + __func__ , alloc_size, ra, ra->bbt, bbt_size, ranfc_mtd);
2133 + ppdata.of_node = pdev->dev.of_node;
2134 + err = mtd_device_parse_register(ranfc_mtd, mtk_probe_types,
2135 + &ppdata, NULL, 0);
2141 +mtk_nand_remove(struct platform_device *pdev)
2143 + struct ra_nand_chip *ra;
2146 + ra = (struct ra_nand_chip *)ranfc_mtd->priv;
2148 + /* Deregister partitions */
2149 + //del_mtd_partitions(ranfc_mtd);
2155 +static const struct of_device_id mtk_nand_match[] = {
2156 + { .compatible = "mtk,mt7620-nand" },
2159 +MODULE_DEVICE_TABLE(of, mtk_nand_match);
2161 +static struct platform_driver mtk_nand_driver = {
2162 + .probe = mtk_nand_probe,
2163 + .remove = mtk_nand_remove,
2165 + .name = "mt7620_nand",
2166 + .owner = THIS_MODULE,
2167 + .of_match_table = mtk_nand_match,
2171 +module_platform_driver(mtk_nand_driver);
2174 +MODULE_LICENSE("GPL");
2176 +++ b/drivers/mtd/maps/ralink_nand.h
2178 +#ifndef RT2880_NAND_H
2179 +#define RT2880_NAND_H
2181 +#include <linux/mtd/mtd.h>
2183 +//#include "gdma.h"
2185 +#define RALINK_SYSCTL_BASE 0xB0000000
2186 +#define RALINK_PIO_BASE 0xB0000600
2187 +#define RALINK_NAND_CTRL_BASE 0xB0000810
2188 +#define CONFIG_RALINK_MT7620
2190 +#define SKIP_BAD_BLOCK
2191 +//#define RANDOM_GEN_BAD_BLOCK
2193 +#define ra_inl(addr) (*(volatile unsigned int *)(addr))
2194 +#define ra_outl(addr, value) (*(volatile unsigned int *)(addr) = (value))
2195 +#define ra_aor(addr, a_mask, o_value) ra_outl(addr, (ra_inl(addr) & (a_mask)) | (o_value))
2196 +#define ra_and(addr, a_mask) ra_aor(addr, a_mask, 0)
2197 +#define ra_or(addr, o_value) ra_aor(addr, -1, o_value)
2200 +#define CONFIG_NUMCHIPS 1
2201 +#define CONFIG_NOT_SUPPORT_WP //rt3052 has no WP signal for chip.
2202 +//#define CONFIG_NOT_SUPPORT_RB
2204 +extern int is_nand_page_2048;
2205 +extern const unsigned int nand_size_map[2][3];
2208 +// chip geometry: SAMSUNG small size 32MB.
2209 +#define CONFIG_CHIP_SIZE_BIT (nand_size_map[is_nand_page_2048][nand_addrlen-3]) //! (1<<NAND_SIZE_BYTE) MB
2210 +//#define CONFIG_CHIP_SIZE_BIT (is_nand_page_2048? 29 : 25) //! (1<<NAND_SIZE_BYTE) MB
2211 +#define CONFIG_PAGE_SIZE_BIT (is_nand_page_2048? 11 : 9) //! (1<<PAGE_SIZE) MB
2212 +//#define CONFIG_SUBPAGE_BIT 1 //! these bits will be compensate by command cycle
2213 +#define CONFIG_NUMPAGE_PER_BLOCK_BIT (is_nand_page_2048? 6 : 5) //! order of number of pages a block.
2214 +#define CONFIG_OOBSIZE_PER_PAGE_BIT (is_nand_page_2048? 6 : 4) //! byte number of oob a page.
2215 +#define CONFIG_BAD_BLOCK_POS (is_nand_page_2048? 0 : 4) //! offset of byte to denote bad block.
2216 +#define CONFIG_ECC_BYTES 3 //! ecc has 3 bytes
2217 +#define CONFIG_ECC_OFFSET (is_nand_page_2048? 6 : 5) //! ecc starts from offset 5.
2219 +//this section should not be modified.
2220 +//#define CFG_COLUMN_ADDR_MASK ((1 << (CONFIG_PAGE_SIZE_BIT - CONFIG_SUBPAGE_BIT)) - 1)
2221 +//#define CFG_COLUMN_ADDR_CYCLE (((CONFIG_PAGE_SIZE_BIT - CONFIG_SUBPAGE_BIT) + 7)/8)
2222 +//#define CFG_ROW_ADDR_CYCLE ((CONFIG_CHIP_SIZE_BIT - CONFIG_PAGE_SIZE_BIT + 7)/8)
2223 +//#define CFG_ADDR_CYCLE (CFG_COLUMN_ADDR_CYCLE + CFG_ROW_ADDR_CYCLE)
2225 +#define CFG_COLUMN_ADDR_CYCLE (is_nand_page_2048? 2 : 1)
2226 +#define CFG_ROW_ADDR_CYCLE (nand_addrlen - CFG_COLUMN_ADDR_CYCLE)
2227 +#define CFG_ADDR_CYCLE (CFG_COLUMN_ADDR_CYCLE + CFG_ROW_ADDR_CYCLE)
2229 +#define CFG_CHIPSIZE (1 << ((CONFIG_CHIP_SIZE_BIT>=32)? 31 : CONFIG_CHIP_SIZE_BIT))
2230 +//#define CFG_CHIPSIZE (1 << CONFIG_CHIP_SIZE_BIT)
2231 +#define CFG_PAGESIZE (1 << CONFIG_PAGE_SIZE_BIT)
2232 +#define CFG_BLOCKSIZE (CFG_PAGESIZE << CONFIG_NUMPAGE_PER_BLOCK_BIT)
2233 +#define CFG_NUMPAGE (1 << (CONFIG_CHIP_SIZE_BIT - CONFIG_PAGE_SIZE_BIT))
2234 +#define CFG_NUMBLOCK (CFG_NUMPAGE >> CONFIG_NUMPAGE_PER_BLOCK_BIT)
2235 +#define CFG_BLOCK_OOBSIZE (1 << (CONFIG_OOBSIZE_PER_PAGE_BIT + CONFIG_NUMPAGE_PER_BLOCK_BIT))
2236 +#define CFG_PAGE_OOBSIZE (1 << CONFIG_OOBSIZE_PER_PAGE_BIT)
2238 +#define NAND_BLOCK_ALIGN(addr) ((addr) & (CFG_BLOCKSIZE-1))
2239 +#define NAND_PAGE_ALIGN(addr) ((addr) & (CFG_PAGESIZE-1))
2242 +#define NFC_BASE RALINK_NAND_CTRL_BASE
2243 +#define NFC_CTRL (NFC_BASE + 0x0)
2244 +#define NFC_CONF (NFC_BASE + 0x4)
2245 +#define NFC_CMD1 (NFC_BASE + 0x8)
2246 +#define NFC_CMD2 (NFC_BASE + 0xc)
2247 +#define NFC_CMD3 (NFC_BASE + 0x10)
2248 +#define NFC_ADDR (NFC_BASE + 0x14)
2249 +#define NFC_DATA (NFC_BASE + 0x18)
2250 +#if defined (CONFIG_RALINK_RT6855) || defined (CONFIG_RALINK_RT6855A) || \
2251 + defined (CONFIG_RALINK_MT7620) || defined (CONFIG_RALINK_MT7621)
2252 +#define NFC_ECC (NFC_BASE + 0x30)
2254 +#define NFC_ECC (NFC_BASE + 0x1c)
2256 +#define NFC_STATUS (NFC_BASE + 0x20)
2257 +#define NFC_INT_EN (NFC_BASE + 0x24)
2258 +#define NFC_INT_ST (NFC_BASE + 0x28)
2259 +#if defined (CONFIG_RALINK_RT6855) || defined (CONFIG_RALINK_RT6855A) || \
2260 + defined (CONFIG_RALINK_MT7620) || defined (CONFIG_RALINK_MT7621)
2261 +#define NFC_CONF1 (NFC_BASE + 0x2c)
2262 +#define NFC_ECC_P1 (NFC_BASE + 0x30)
2263 +#define NFC_ECC_P2 (NFC_BASE + 0x34)
2264 +#define NFC_ECC_P3 (NFC_BASE + 0x38)
2265 +#define NFC_ECC_P4 (NFC_BASE + 0x3c)
2266 +#define NFC_ECC_ERR1 (NFC_BASE + 0x40)
2267 +#define NFC_ECC_ERR2 (NFC_BASE + 0x44)
2268 +#define NFC_ECC_ERR3 (NFC_BASE + 0x48)
2269 +#define NFC_ECC_ERR4 (NFC_BASE + 0x4c)
2270 +#define NFC_ADDR2 (NFC_BASE + 0x50)
2274 + INT_ST_ND_DONE = 1<<0,
2275 + INT_ST_TX_BUF_RDY = 1<<1,
2276 + INT_ST_RX_BUF_RDY = 1<<2,
2277 + INT_ST_ECC_ERR = 1<<3,
2278 + INT_ST_TX_TRAS_ERR = 1<<4,
2279 + INT_ST_RX_TRAS_ERR = 1<<5,
2280 + INT_ST_TX_KICK_ERR = 1<<6,
2281 + INT_ST_RX_KICK_ERR = 1<<7
2285 +//#define WORKAROUND_RX_BUF_OV 1
2288 +/*************************************************************
2289 + * stolen from nand.h
2290 + *************************************************************/
2293 + * Standard NAND flash commands
2295 +#define NAND_CMD_READ0 0
2296 +#define NAND_CMD_READ1 1
2297 +#define NAND_CMD_RNDOUT 5
2298 +#define NAND_CMD_PAGEPROG 0x10
2299 +#define NAND_CMD_READOOB 0x50
2300 +#define NAND_CMD_ERASE1 0x60
2301 +#define NAND_CMD_STATUS 0x70
2302 +#define NAND_CMD_STATUS_MULTI 0x71
2303 +#define NAND_CMD_SEQIN 0x80
2304 +#define NAND_CMD_RNDIN 0x85
2305 +#define NAND_CMD_READID 0x90
2306 +#define NAND_CMD_ERASE2 0xd0
2307 +#define NAND_CMD_RESET 0xff
2309 +/* Extended commands for large page devices */
2310 +#define NAND_CMD_READSTART 0x30
2311 +#define NAND_CMD_RNDOUTSTART 0xE0
2312 +#define NAND_CMD_CACHEDPROG 0x15
2314 +/* Extended commands for AG-AND device */
2316 + * Note: the command for NAND_CMD_DEPLETE1 is really 0x00 but
2317 + * there is no way to distinguish that from NAND_CMD_READ0
2318 + * until the remaining sequence of commands has been completed
2319 + * so add a high order bit and mask it off in the command.
2321 +#define NAND_CMD_DEPLETE1 0x100
2322 +#define NAND_CMD_DEPLETE2 0x38
2323 +#define NAND_CMD_STATUS_MULTI 0x71
2324 +#define NAND_CMD_STATUS_ERROR 0x72
2325 +/* multi-bank error status (banks 0-3) */
2326 +#define NAND_CMD_STATUS_ERROR0 0x73
2327 +#define NAND_CMD_STATUS_ERROR1 0x74
2328 +#define NAND_CMD_STATUS_ERROR2 0x75
2329 +#define NAND_CMD_STATUS_ERROR3 0x76
2330 +#define NAND_CMD_STATUS_RESET 0x7f
2331 +#define NAND_CMD_STATUS_CLEAR 0xff
2333 +#define NAND_CMD_NONE -1
2336 +#define NAND_STATUS_FAIL 0x01
2337 +#define NAND_STATUS_FAIL_N1 0x02
2338 +#define NAND_STATUS_TRUE_READY 0x20
2339 +#define NAND_STATUS_READY 0x40
2340 +#define NAND_STATUS_WP 0x80
2352 +/*************************************************************/
2356 +typedef enum _ra_flags {
2358 + FLAG_ECC_EN = (1<<0),
2359 + FLAG_USE_GDMA = (1<<1),
2360 + FLAG_VERIFY = (1<<2),
2364 +#define BBTTAG_BITS 2
2365 +#define BBTTAG_BITS_MASK ((1<<BBTTAG_BITS) -1)
2367 + BBT_TAG_UNKNOWN = 0, //2'b01
2368 + BBT_TAG_GOOD = 3, //2'b11
2369 + BBT_TAG_BAD = 2, //2'b10
2370 + BBT_TAG_RES = 1, //2'b01
2373 +struct ra_nand_chip {
2380 +#if !defined (__UBOOT__)
2381 + struct mutex hwcontrol;
2382 + struct mutex *controller;
2384 + struct nand_ecclayout *oob;
2386 + unsigned int buffers_page;
2387 + char *buffers; //[CFG_PAGESIZE + CFG_PAGE_OOBSIZE];
2388 + char *readback_buffers;
2389 + unsigned char *bbt;
2390 +#if defined (WORKAROUND_RX_BUF_OV)
2391 + unsigned int sandbox_page; // steal a page (block) for read ECC verification
2399 +int nand_dma_sync(void);
2400 +void release_dma_buf(void);
2401 +int set_gdma_ch(unsigned long dst,
2402 + unsigned long src, unsigned int len, int burst_size,
2403 + int soft_mode, int src_req_type, int dst_req_type,
2404 + int src_burst_mode, int dst_burst_mode);