2 * drivers/mtd/nand/diskonchip.c
4 * (C) 2003 Red Hat, Inc.
5 * (C) 2004 Dan Brown <dan_brown@ieee.org>
6 * (C) 2004 Kalev Lember <kalev@smartlink.ee>
8 * Author: David Woodhouse <dwmw2@infradead.org>
9 * Additional Diskonchip 2000 and Millennium support by Dan Brown <dan_brown@ieee.org>
10 * Diskonchip Millennium Plus support by Kalev Lember <kalev@smartlink.ee>
12 * Error correction code lifted from the old docecc code
13 * Author: Fabrice Bellard (fabrice.bellard@netgem.com)
14 * Copyright (C) 2000 Netgem S.A.
15 * converted to the generic Reed-Solomon library by Thomas Gleixner <tglx@linutronix.de>
17 * Interface to generic NAND code for M-Systems DiskOnChip devices
19 * $Id: diskonchip.c,v 1.45 2005/01/05 18:05:14 dwmw2 Exp $
24 #ifdef CFG_NAND_LEGACY
25 #error CFG_NAND_LEGACY defined in a file not using the legacy NAND support!
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/sched.h>
31 #include <linux/delay.h>
32 #include <linux/rslib.h>
33 #include <linux/moduleparam.h>
36 #include <linux/mtd/mtd.h>
37 #include <linux/mtd/nand.h>
38 #include <linux/mtd/doc2000.h>
39 #include <linux/mtd/compatmac.h>
40 #include <linux/mtd/partitions.h>
41 #include <linux/mtd/inftl.h>
43 /* Where to look for the devices? */
44 #ifndef CONFIG_MTD_DISKONCHIP_PROBE_ADDRESS
45 #define CONFIG_MTD_DISKONCHIP_PROBE_ADDRESS 0
48 static unsigned long __initdata doc_locations[] = {
49 #if defined (__alpha__) || defined(__i386__) || defined(__x86_64__)
50 #ifdef CONFIG_MTD_DISKONCHIP_PROBE_HIGH
51 0xfffc8000, 0xfffca000, 0xfffcc000, 0xfffce000,
52 0xfffd0000, 0xfffd2000, 0xfffd4000, 0xfffd6000,
53 0xfffd8000, 0xfffda000, 0xfffdc000, 0xfffde000,
54 0xfffe0000, 0xfffe2000, 0xfffe4000, 0xfffe6000,
55 0xfffe8000, 0xfffea000, 0xfffec000, 0xfffee000,
56 #else /* CONFIG_MTD_DOCPROBE_HIGH */
57 0xc8000, 0xca000, 0xcc000, 0xce000,
58 0xd0000, 0xd2000, 0xd4000, 0xd6000,
59 0xd8000, 0xda000, 0xdc000, 0xde000,
60 0xe0000, 0xe2000, 0xe4000, 0xe6000,
61 0xe8000, 0xea000, 0xec000, 0xee000,
62 #endif /* CONFIG_MTD_DOCPROBE_HIGH */
63 #elif defined(__PPC__)
65 #elif defined(CONFIG_MOMENCO_OCELOT)
68 #elif defined(CONFIG_MOMENCO_OCELOT_G) || defined (CONFIG_MOMENCO_OCELOT_C)
71 #warning Unknown architecture for DiskOnChip. No default probe locations defined
75 static struct mtd_info *doclist = NULL;
78 void __iomem *virtadr;
79 unsigned long physadr;
82 int chips_per_floor; /* The number of chips detected on each floor */
87 struct mtd_info *nextdoc;
90 /* Max number of eraseblocks to scan (from start of device) for the (I)NFTL
91 MediaHeader. The spec says to just keep going, I think, but that's just
93 #define MAX_MEDIAHEADER_SCAN 8
95 /* This is the syndrome computed by the HW ecc generator upon reading an empty
96 page, one with all 0xff for data and stored ecc code. */
97 static u_char empty_read_syndrome[6] = { 0x26, 0xff, 0x6d, 0x47, 0x73, 0x7a };
98 /* This is the ecc value computed by the HW ecc generator upon writing an empty
99 page, one with all 0xff for data. */
100 static u_char empty_write_ecc[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
102 #define INFTL_BBT_RESERVED_BLOCKS 4
104 #define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
105 #define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
106 #define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
108 static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd);
109 static void doc200x_select_chip(struct mtd_info *mtd, int chip);
112 module_param(debug, int, 0);
114 static int try_dword=1;
115 module_param(try_dword, int, 0);
117 static int no_ecc_failures=0;
118 module_param(no_ecc_failures, int, 0);
120 #ifdef CONFIG_MTD_PARTITIONS
121 static int no_autopart=0;
122 module_param(no_autopart, int, 0);
125 #ifdef MTD_NAND_DISKONCHIP_BBTWRITE
126 static int inftl_bbt_write=1;
128 static int inftl_bbt_write=0;
130 module_param(inftl_bbt_write, int, 0);
132 static unsigned long doc_config_location = CONFIG_MTD_DISKONCHIP_PROBE_ADDRESS;
133 module_param(doc_config_location, ulong, 0);
134 MODULE_PARM_DESC(doc_config_location, "Physical memory address at which to probe for DiskOnChip");
137 /* Sector size for HW ECC */
138 #define SECTOR_SIZE 512
139 /* The sector bytes are packed into NB_DATA 10 bit words */
140 #define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / 10)
141 /* Number of roots */
143 /* First consective root */
145 /* Number of symbols */
148 /* the Reed Solomon control structure */
149 static struct rs_control *rs_decoder;
152 * The HW decoder in the DoC ASIC's provides us a error syndrome,
153 * which we must convert to a standard syndrom usable by the generic
154 * Reed-Solomon library code.
156 * Fabrice Bellard figured this out in the old docecc code. I added
157 * some comments, improved a minor bit and converted it to make use
158 * of the generic Reed-Solomon libary. tglx
160 static int doc_ecc_decode (struct rs_control *rs, uint8_t *data, uint8_t *ecc)
162 int i, j, nerr, errpos[8];
164 uint16_t ds[4], s[5], tmp, errval[8], syn[4];
166 /* Convert the ecc bytes into words */
167 ds[0] = ((ecc[4] & 0xff) >> 0) | ((ecc[5] & 0x03) << 8);
168 ds[1] = ((ecc[5] & 0xfc) >> 2) | ((ecc[2] & 0x0f) << 6);
169 ds[2] = ((ecc[2] & 0xf0) >> 4) | ((ecc[3] & 0x3f) << 4);
170 ds[3] = ((ecc[3] & 0xc0) >> 6) | ((ecc[0] & 0xff) << 2);
173 /* Initialize the syndrom buffer */
174 for (i = 0; i < NROOTS; i++)
178 * s[i] = ds[3]x^3 + ds[2]x^2 + ds[1]x^1 + ds[0]
179 * where x = alpha^(FCR + i)
181 for(j = 1; j < NROOTS; j++) {
184 tmp = rs->index_of[ds[j]];
185 for(i = 0; i < NROOTS; i++)
186 s[i] ^= rs->alpha_to[rs_modnn(rs, tmp + (FCR + i) * j)];
189 /* Calc s[i] = s[i] / alpha^(v + i) */
190 for (i = 0; i < NROOTS; i++) {
192 syn[i] = rs_modnn(rs, rs->index_of[s[i]] + (NN - FCR - i));
194 /* Call the decoder library */
195 nerr = decode_rs16(rs, NULL, NULL, 1019, syn, 0, errpos, 0, errval);
197 /* Incorrectable errors ? */
202 * Correct the errors. The bitpositions are a bit of magic,
203 * but they are given by the design of the de/encoder circuit
206 for(i = 0;i < nerr; i++) {
207 int index, bitpos, pos = 1015 - errpos[i];
209 if (pos >= NB_DATA && pos < 1019)
212 /* extract bit position (MSB first) */
213 pos = 10 * (NB_DATA - 1 - pos) - 6;
214 /* now correct the following 10 bits. At most two bytes
215 can be modified since pos is even */
216 index = (pos >> 3) ^ 1;
218 if ((index >= 0 && index < SECTOR_SIZE) ||
219 index == (SECTOR_SIZE + 1)) {
220 val = (uint8_t) (errval[i] >> (2 + bitpos));
222 if (index < SECTOR_SIZE)
225 index = ((pos >> 3) + 1) ^ 1;
226 bitpos = (bitpos + 10) & 7;
229 if ((index >= 0 && index < SECTOR_SIZE) ||
230 index == (SECTOR_SIZE + 1)) {
231 val = (uint8_t)(errval[i] << (8 - bitpos));
233 if (index < SECTOR_SIZE)
238 /* If the parity is wrong, no rescue possible */
239 return parity ? -1 : nerr;
242 static void DoC_Delay(struct doc_priv *doc, unsigned short cycles)
247 for (i = 0; i < cycles; i++) {
248 if (DoC_is_Millennium(doc))
249 dummy = ReadDOC(doc->virtadr, NOP);
250 else if (DoC_is_MillenniumPlus(doc))
251 dummy = ReadDOC(doc->virtadr, Mplus_NOP);
253 dummy = ReadDOC(doc->virtadr, DOCStatus);
258 #define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
260 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
261 static int _DoC_WaitReady(struct doc_priv *doc)
263 void __iomem *docptr = doc->virtadr;
264 unsigned long timeo = jiffies + (HZ * 10);
266 if(debug) printk("_DoC_WaitReady...\n");
267 /* Out-of-line routine to wait for chip response */
268 if (DoC_is_MillenniumPlus(doc)) {
269 while ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
270 if (time_after(jiffies, timeo)) {
271 printk("_DoC_WaitReady timed out.\n");
278 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
279 if (time_after(jiffies, timeo)) {
280 printk("_DoC_WaitReady timed out.\n");
291 static inline int DoC_WaitReady(struct doc_priv *doc)
293 void __iomem *docptr = doc->virtadr;
296 if (DoC_is_MillenniumPlus(doc)) {
299 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
300 /* Call the out-of-line routine to wait */
301 ret = _DoC_WaitReady(doc);
305 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
306 /* Call the out-of-line routine to wait */
307 ret = _DoC_WaitReady(doc);
311 if(debug) printk("DoC_WaitReady OK\n");
315 static void doc2000_write_byte(struct mtd_info *mtd, u_char datum)
317 struct nand_chip *this = mtd->priv;
318 struct doc_priv *doc = this->priv;
319 void __iomem *docptr = doc->virtadr;
321 if(debug)printk("write_byte %02x\n", datum);
322 WriteDOC(datum, docptr, CDSNSlowIO);
323 WriteDOC(datum, docptr, 2k_CDSN_IO);
326 static u_char doc2000_read_byte(struct mtd_info *mtd)
328 struct nand_chip *this = mtd->priv;
329 struct doc_priv *doc = this->priv;
330 void __iomem *docptr = doc->virtadr;
333 ReadDOC(docptr, CDSNSlowIO);
335 ret = ReadDOC(docptr, 2k_CDSN_IO);
336 if (debug) printk("read_byte returns %02x\n", ret);
340 static void doc2000_writebuf(struct mtd_info *mtd,
341 const u_char *buf, int len)
343 struct nand_chip *this = mtd->priv;
344 struct doc_priv *doc = this->priv;
345 void __iomem *docptr = doc->virtadr;
347 if (debug)printk("writebuf of %d bytes: ", len);
348 for (i=0; i < len; i++) {
349 WriteDOC_(buf[i], docptr, DoC_2k_CDSN_IO + i);
351 printk("%02x ", buf[i]);
353 if (debug) printk("\n");
356 static void doc2000_readbuf(struct mtd_info *mtd,
357 u_char *buf, int len)
359 struct nand_chip *this = mtd->priv;
360 struct doc_priv *doc = this->priv;
361 void __iomem *docptr = doc->virtadr;
364 if (debug)printk("readbuf of %d bytes: ", len);
366 for (i=0; i < len; i++) {
367 buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
371 static void doc2000_readbuf_dword(struct mtd_info *mtd,
372 u_char *buf, int len)
374 struct nand_chip *this = mtd->priv;
375 struct doc_priv *doc = this->priv;
376 void __iomem *docptr = doc->virtadr;
379 if (debug) printk("readbuf_dword of %d bytes: ", len);
381 if (unlikely((((unsigned long)buf)|len) & 3)) {
382 for (i=0; i < len; i++) {
383 *(uint8_t *)(&buf[i]) = ReadDOC(docptr, 2k_CDSN_IO + i);
386 for (i=0; i < len; i+=4) {
387 *(uint32_t*)(&buf[i]) = readl(docptr + DoC_2k_CDSN_IO + i);
392 static int doc2000_verifybuf(struct mtd_info *mtd,
393 const u_char *buf, int len)
395 struct nand_chip *this = mtd->priv;
396 struct doc_priv *doc = this->priv;
397 void __iomem *docptr = doc->virtadr;
400 for (i=0; i < len; i++)
401 if (buf[i] != ReadDOC(docptr, 2k_CDSN_IO))
406 static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr)
408 struct nand_chip *this = mtd->priv;
409 struct doc_priv *doc = this->priv;
412 doc200x_select_chip(mtd, nr);
413 doc200x_hwcontrol(mtd, NAND_CTL_SETCLE);
414 this->write_byte(mtd, NAND_CMD_READID);
415 doc200x_hwcontrol(mtd, NAND_CTL_CLRCLE);
416 doc200x_hwcontrol(mtd, NAND_CTL_SETALE);
417 this->write_byte(mtd, 0);
418 doc200x_hwcontrol(mtd, NAND_CTL_CLRALE);
420 ret = this->read_byte(mtd) << 8;
421 ret |= this->read_byte(mtd);
423 if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) {
424 /* First chip probe. See if we get same results by 32-bit access */
429 void __iomem *docptr = doc->virtadr;
431 doc200x_hwcontrol(mtd, NAND_CTL_SETCLE);
432 doc2000_write_byte(mtd, NAND_CMD_READID);
433 doc200x_hwcontrol(mtd, NAND_CTL_CLRCLE);
434 doc200x_hwcontrol(mtd, NAND_CTL_SETALE);
435 doc2000_write_byte(mtd, 0);
436 doc200x_hwcontrol(mtd, NAND_CTL_CLRALE);
438 ident.dword = readl(docptr + DoC_2k_CDSN_IO);
439 if (((ident.byte[0] << 8) | ident.byte[1]) == ret) {
440 printk(KERN_INFO "DiskOnChip 2000 responds to DWORD access\n");
441 this->read_buf = &doc2000_readbuf_dword;
448 static void __init doc2000_count_chips(struct mtd_info *mtd)
450 struct nand_chip *this = mtd->priv;
451 struct doc_priv *doc = this->priv;
455 /* Max 4 chips per floor on DiskOnChip 2000 */
456 doc->chips_per_floor = 4;
458 /* Find out what the first chip is */
459 mfrid = doc200x_ident_chip(mtd, 0);
461 /* Find how many chips in each floor. */
462 for (i = 1; i < 4; i++) {
463 if (doc200x_ident_chip(mtd, i) != mfrid)
466 doc->chips_per_floor = i;
467 printk(KERN_DEBUG "Detected %d chips per floor.\n", i);
470 static int doc200x_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
472 struct doc_priv *doc = this->priv;
477 this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
479 status = (int)this->read_byte(mtd);
484 static void doc2001_write_byte(struct mtd_info *mtd, u_char datum)
486 struct nand_chip *this = mtd->priv;
487 struct doc_priv *doc = this->priv;
488 void __iomem *docptr = doc->virtadr;
490 WriteDOC(datum, docptr, CDSNSlowIO);
491 WriteDOC(datum, docptr, Mil_CDSN_IO);
492 WriteDOC(datum, docptr, WritePipeTerm);
495 static u_char doc2001_read_byte(struct mtd_info *mtd)
497 struct nand_chip *this = mtd->priv;
498 struct doc_priv *doc = this->priv;
499 void __iomem *docptr = doc->virtadr;
501 /*ReadDOC(docptr, CDSNSlowIO); */
502 /* 11.4.5 -- delay twice to allow extended length cycle */
504 ReadDOC(docptr, ReadPipeInit);
505 /*return ReadDOC(docptr, Mil_CDSN_IO); */
506 return ReadDOC(docptr, LastDataRead);
509 static void doc2001_writebuf(struct mtd_info *mtd,
510 const u_char *buf, int len)
512 struct nand_chip *this = mtd->priv;
513 struct doc_priv *doc = this->priv;
514 void __iomem *docptr = doc->virtadr;
517 for (i=0; i < len; i++)
518 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
519 /* Terminate write pipeline */
520 WriteDOC(0x00, docptr, WritePipeTerm);
523 static void doc2001_readbuf(struct mtd_info *mtd,
524 u_char *buf, int len)
526 struct nand_chip *this = mtd->priv;
527 struct doc_priv *doc = this->priv;
528 void __iomem *docptr = doc->virtadr;
531 /* Start read pipeline */
532 ReadDOC(docptr, ReadPipeInit);
534 for (i=0; i < len-1; i++)
535 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
537 /* Terminate read pipeline */
538 buf[i] = ReadDOC(docptr, LastDataRead);
541 static int doc2001_verifybuf(struct mtd_info *mtd,
542 const u_char *buf, int len)
544 struct nand_chip *this = mtd->priv;
545 struct doc_priv *doc = this->priv;
546 void __iomem *docptr = doc->virtadr;
549 /* Start read pipeline */
550 ReadDOC(docptr, ReadPipeInit);
552 for (i=0; i < len-1; i++)
553 if (buf[i] != ReadDOC(docptr, Mil_CDSN_IO)) {
554 ReadDOC(docptr, LastDataRead);
557 if (buf[i] != ReadDOC(docptr, LastDataRead))
562 static u_char doc2001plus_read_byte(struct mtd_info *mtd)
564 struct nand_chip *this = mtd->priv;
565 struct doc_priv *doc = this->priv;
566 void __iomem *docptr = doc->virtadr;
569 ReadDOC(docptr, Mplus_ReadPipeInit);
570 ReadDOC(docptr, Mplus_ReadPipeInit);
571 ret = ReadDOC(docptr, Mplus_LastDataRead);
572 if (debug) printk("read_byte returns %02x\n", ret);
576 static void doc2001plus_writebuf(struct mtd_info *mtd,
577 const u_char *buf, int len)
579 struct nand_chip *this = mtd->priv;
580 struct doc_priv *doc = this->priv;
581 void __iomem *docptr = doc->virtadr;
584 if (debug)printk("writebuf of %d bytes: ", len);
585 for (i=0; i < len; i++) {
586 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
588 printk("%02x ", buf[i]);
590 if (debug) printk("\n");
593 static void doc2001plus_readbuf(struct mtd_info *mtd,
594 u_char *buf, int len)
596 struct nand_chip *this = mtd->priv;
597 struct doc_priv *doc = this->priv;
598 void __iomem *docptr = doc->virtadr;
601 if (debug)printk("readbuf of %d bytes: ", len);
603 /* Start read pipeline */
604 ReadDOC(docptr, Mplus_ReadPipeInit);
605 ReadDOC(docptr, Mplus_ReadPipeInit);
607 for (i=0; i < len-2; i++) {
608 buf[i] = ReadDOC(docptr, Mil_CDSN_IO);
610 printk("%02x ", buf[i]);
613 /* Terminate read pipeline */
614 buf[len-2] = ReadDOC(docptr, Mplus_LastDataRead);
616 printk("%02x ", buf[len-2]);
617 buf[len-1] = ReadDOC(docptr, Mplus_LastDataRead);
619 printk("%02x ", buf[len-1]);
620 if (debug) printk("\n");
623 static int doc2001plus_verifybuf(struct mtd_info *mtd,
624 const u_char *buf, int len)
626 struct nand_chip *this = mtd->priv;
627 struct doc_priv *doc = this->priv;
628 void __iomem *docptr = doc->virtadr;
631 if (debug)printk("verifybuf of %d bytes: ", len);
633 /* Start read pipeline */
634 ReadDOC(docptr, Mplus_ReadPipeInit);
635 ReadDOC(docptr, Mplus_ReadPipeInit);
637 for (i=0; i < len-2; i++)
638 if (buf[i] != ReadDOC(docptr, Mil_CDSN_IO)) {
639 ReadDOC(docptr, Mplus_LastDataRead);
640 ReadDOC(docptr, Mplus_LastDataRead);
643 if (buf[len-2] != ReadDOC(docptr, Mplus_LastDataRead))
645 if (buf[len-1] != ReadDOC(docptr, Mplus_LastDataRead))
650 static void doc2001plus_select_chip(struct mtd_info *mtd, int chip)
652 struct nand_chip *this = mtd->priv;
653 struct doc_priv *doc = this->priv;
654 void __iomem *docptr = doc->virtadr;
657 if(debug)printk("select chip (%d)\n", chip);
660 /* Disable flash internally */
661 WriteDOC(0, docptr, Mplus_FlashSelect);
665 floor = chip / doc->chips_per_floor;
666 chip -= (floor * doc->chips_per_floor);
668 /* Assert ChipEnable and deassert WriteProtect */
669 WriteDOC((DOC_FLASH_CE), docptr, Mplus_FlashSelect);
670 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
673 doc->curfloor = floor;
676 static void doc200x_select_chip(struct mtd_info *mtd, int chip)
678 struct nand_chip *this = mtd->priv;
679 struct doc_priv *doc = this->priv;
680 void __iomem *docptr = doc->virtadr;
683 if(debug)printk("select chip (%d)\n", chip);
688 floor = chip / doc->chips_per_floor;
689 chip -= (floor * doc->chips_per_floor);
691 /* 11.4.4 -- deassert CE before changing chip */
692 doc200x_hwcontrol(mtd, NAND_CTL_CLRNCE);
694 WriteDOC(floor, docptr, FloorSelect);
695 WriteDOC(chip, docptr, CDSNDeviceSelect);
697 doc200x_hwcontrol(mtd, NAND_CTL_SETNCE);
700 doc->curfloor = floor;
703 static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd)
705 struct nand_chip *this = mtd->priv;
706 struct doc_priv *doc = this->priv;
707 void __iomem *docptr = doc->virtadr;
710 case NAND_CTL_SETNCE:
711 doc->CDSNControl |= CDSN_CTRL_CE;
713 case NAND_CTL_CLRNCE:
714 doc->CDSNControl &= ~CDSN_CTRL_CE;
716 case NAND_CTL_SETCLE:
717 doc->CDSNControl |= CDSN_CTRL_CLE;
719 case NAND_CTL_CLRCLE:
720 doc->CDSNControl &= ~CDSN_CTRL_CLE;
722 case NAND_CTL_SETALE:
723 doc->CDSNControl |= CDSN_CTRL_ALE;
725 case NAND_CTL_CLRALE:
726 doc->CDSNControl &= ~CDSN_CTRL_ALE;
729 doc->CDSNControl |= CDSN_CTRL_WP;
732 doc->CDSNControl &= ~CDSN_CTRL_WP;
735 if (debug)printk("hwcontrol(%d): %02x\n", cmd, doc->CDSNControl);
736 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
737 /* 11.4.3 -- 4 NOPs after CSDNControl write */
741 static void doc2001plus_command (struct mtd_info *mtd, unsigned command, int column, int page_addr)
743 struct nand_chip *this = mtd->priv;
744 struct doc_priv *doc = this->priv;
745 void __iomem *docptr = doc->virtadr;
748 * Must terminate write pipeline before sending any commands
751 if (command == NAND_CMD_PAGEPROG) {
752 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
753 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
757 * Write out the command to the device.
759 if (command == NAND_CMD_SEQIN) {
762 if (column >= mtd->oobblock) {
764 column -= mtd->oobblock;
765 readcmd = NAND_CMD_READOOB;
766 } else if (column < 256) {
767 /* First 256 bytes --> READ0 */
768 readcmd = NAND_CMD_READ0;
771 readcmd = NAND_CMD_READ1;
773 WriteDOC(readcmd, docptr, Mplus_FlashCmd);
775 WriteDOC(command, docptr, Mplus_FlashCmd);
776 WriteDOC(0, docptr, Mplus_WritePipeTerm);
777 WriteDOC(0, docptr, Mplus_WritePipeTerm);
779 if (column != -1 || page_addr != -1) {
780 /* Serially input address */
782 /* Adjust columns for 16 bit buswidth */
783 if (this->options & NAND_BUSWIDTH_16)
785 WriteDOC(column, docptr, Mplus_FlashAddress);
787 if (page_addr != -1) {
788 WriteDOC((unsigned char) (page_addr & 0xff), docptr, Mplus_FlashAddress);
789 WriteDOC((unsigned char) ((page_addr >> 8) & 0xff), docptr, Mplus_FlashAddress);
790 /* One more address cycle for higher density devices */
791 if (this->chipsize & 0x0c000000) {
792 WriteDOC((unsigned char) ((page_addr >> 16) & 0x0f), docptr, Mplus_FlashAddress);
793 printk("high density\n");
796 WriteDOC(0, docptr, Mplus_WritePipeTerm);
797 WriteDOC(0, docptr, Mplus_WritePipeTerm);
799 if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 || command == NAND_CMD_READOOB || command == NAND_CMD_READID)
800 WriteDOC(0, docptr, Mplus_FlashControl);
804 * program and erase have their own busy handlers
805 * status and sequential in needs no delay
809 case NAND_CMD_PAGEPROG:
810 case NAND_CMD_ERASE1:
811 case NAND_CMD_ERASE2:
813 case NAND_CMD_STATUS:
819 udelay(this->chip_delay);
820 WriteDOC(NAND_CMD_STATUS, docptr, Mplus_FlashCmd);
821 WriteDOC(0, docptr, Mplus_WritePipeTerm);
822 WriteDOC(0, docptr, Mplus_WritePipeTerm);
823 while ( !(this->read_byte(mtd) & 0x40));
826 /* This applies to read commands */
829 * If we don't have access to the busy pin, we apply the given
832 if (!this->dev_ready) {
833 udelay (this->chip_delay);
838 /* Apply this short delay always to ensure that we do wait tWB in
839 * any case on any machine. */
841 /* wait until command is processed */
842 while (!this->dev_ready(mtd));
845 static int doc200x_dev_ready(struct mtd_info *mtd)
847 struct nand_chip *this = mtd->priv;
848 struct doc_priv *doc = this->priv;
849 void __iomem *docptr = doc->virtadr;
851 if (DoC_is_MillenniumPlus(doc)) {
852 /* 11.4.2 -- must NOP four times before checking FR/B# */
854 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
856 printk("not ready\n");
859 if (debug)printk("was ready\n");
862 /* 11.4.2 -- must NOP four times before checking FR/B# */
864 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
866 printk("not ready\n");
869 /* 11.4.2 -- Must NOP twice if it's ready */
871 if (debug)printk("was ready\n");
876 static int doc200x_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
878 /* This is our last resort if we couldn't find or create a BBT. Just
879 pretend all blocks are good. */
883 static void doc200x_enable_hwecc(struct mtd_info *mtd, int mode)
885 struct nand_chip *this = mtd->priv;
886 struct doc_priv *doc = this->priv;
887 void __iomem *docptr = doc->virtadr;
889 /* Prime the ECC engine */
892 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
893 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
896 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
897 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
902 static void doc2001plus_enable_hwecc(struct mtd_info *mtd, int mode)
904 struct nand_chip *this = mtd->priv;
905 struct doc_priv *doc = this->priv;
906 void __iomem *docptr = doc->virtadr;
908 /* Prime the ECC engine */
911 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
912 WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
915 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
916 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
921 /* This code is only called on write */
922 static int doc200x_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
923 unsigned char *ecc_code)
925 struct nand_chip *this = mtd->priv;
926 struct doc_priv *doc = this->priv;
927 void __iomem *docptr = doc->virtadr;
931 /* flush the pipeline */
932 if (DoC_is_2000(doc)) {
933 WriteDOC(doc->CDSNControl & ~CDSN_CTRL_FLASH_IO, docptr, CDSNControl);
934 WriteDOC(0, docptr, 2k_CDSN_IO);
935 WriteDOC(0, docptr, 2k_CDSN_IO);
936 WriteDOC(0, docptr, 2k_CDSN_IO);
937 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
938 } else if (DoC_is_MillenniumPlus(doc)) {
939 WriteDOC(0, docptr, Mplus_NOP);
940 WriteDOC(0, docptr, Mplus_NOP);
941 WriteDOC(0, docptr, Mplus_NOP);
943 WriteDOC(0, docptr, NOP);
944 WriteDOC(0, docptr, NOP);
945 WriteDOC(0, docptr, NOP);
948 for (i = 0; i < 6; i++) {
949 if (DoC_is_MillenniumPlus(doc))
950 ecc_code[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
952 ecc_code[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
953 if (ecc_code[i] != empty_write_ecc[i])
956 if (DoC_is_MillenniumPlus(doc))
957 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
959 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
961 /* If emptymatch=1, we might have an all-0xff data buffer. Check. */
963 /* Note: this somewhat expensive test should not be triggered
964 often. It could be optimized away by examining the data in
965 the writebuf routine, and remembering the result. */
966 for (i = 0; i < 512; i++) {
967 if (dat[i] == 0xff) continue;
972 /* If emptymatch still =1, we do have an all-0xff data buffer.
973 Return all-0xff ecc value instead of the computed one, so
974 it'll look just like a freshly-erased page. */
975 if (emptymatch) memset(ecc_code, 0xff, 6);
980 static int doc200x_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc)
983 struct nand_chip *this = mtd->priv;
984 struct doc_priv *doc = this->priv;
985 void __iomem *docptr = doc->virtadr;
986 volatile u_char dummy;
989 /* flush the pipeline */
990 if (DoC_is_2000(doc)) {
991 dummy = ReadDOC(docptr, 2k_ECCStatus);
992 dummy = ReadDOC(docptr, 2k_ECCStatus);
993 dummy = ReadDOC(docptr, 2k_ECCStatus);
994 } else if (DoC_is_MillenniumPlus(doc)) {
995 dummy = ReadDOC(docptr, Mplus_ECCConf);
996 dummy = ReadDOC(docptr, Mplus_ECCConf);
997 dummy = ReadDOC(docptr, Mplus_ECCConf);
999 dummy = ReadDOC(docptr, ECCConf);
1000 dummy = ReadDOC(docptr, ECCConf);
1001 dummy = ReadDOC(docptr, ECCConf);
1004 /* Error occured ? */
1006 for (i = 0; i < 6; i++) {
1007 if (DoC_is_MillenniumPlus(doc))
1008 calc_ecc[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
1010 calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
1011 if (calc_ecc[i] != empty_read_syndrome[i])
1014 /* If emptymatch=1, the read syndrome is consistent with an
1015 all-0xff data and stored ecc block. Check the stored ecc. */
1017 for (i = 0; i < 6; i++) {
1018 if (read_ecc[i] == 0xff) continue;
1023 /* If emptymatch still =1, check the data block. */
1025 /* Note: this somewhat expensive test should not be triggered
1026 often. It could be optimized away by examining the data in
1027 the readbuf routine, and remembering the result. */
1028 for (i = 0; i < 512; i++) {
1029 if (dat[i] == 0xff) continue;
1034 /* If emptymatch still =1, this is almost certainly a freshly-
1035 erased block, in which case the ECC will not come out right.
1036 We'll suppress the error and tell the caller everything's
1037 OK. Because it is. */
1038 if (!emptymatch) ret = doc_ecc_decode (rs_decoder, dat, calc_ecc);
1040 printk(KERN_ERR "doc200x_correct_data corrected %d errors\n", ret);
1042 if (DoC_is_MillenniumPlus(doc))
1043 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
1045 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
1046 if (no_ecc_failures && (ret == -1)) {
1047 printk(KERN_ERR "suppressing ECC failure\n");
1053 /*u_char mydatabuf[528]; */
1055 static struct nand_oobinfo doc200x_oobinfo = {
1056 .useecc = MTD_NANDECC_AUTOPLACE,
1058 .eccpos = {0, 1, 2, 3, 4, 5},
1059 .oobfree = { {8, 8} }
1062 /* Find the (I)NFTL Media Header, and optionally also the mirror media header.
1063 On sucessful return, buf will contain a copy of the media header for
1064 further processing. id is the string to scan for, and will presumably be
1065 either "ANAND" or "BNAND". If findmirror=1, also look for the mirror media
1066 header. The page #s of the found media headers are placed in mh0_page and
1067 mh1_page in the DOC private structure. */
1068 static int __init find_media_headers(struct mtd_info *mtd, u_char *buf,
1069 const char *id, int findmirror)
1071 struct nand_chip *this = mtd->priv;
1072 struct doc_priv *doc = this->priv;
1073 unsigned offs, end = (MAX_MEDIAHEADER_SCAN << this->phys_erase_shift);
1077 end = min(end, mtd->size); /* paranoia */
1078 for (offs = 0; offs < end; offs += mtd->erasesize) {
1079 ret = mtd->read(mtd, offs, mtd->oobblock, &retlen, buf);
1080 if (retlen != mtd->oobblock) continue;
1082 printk(KERN_WARNING "ECC error scanning DOC at 0x%x\n",
1085 if (memcmp(buf, id, 6)) continue;
1086 printk(KERN_INFO "Found DiskOnChip %s Media Header at 0x%x\n", id, offs);
1087 if (doc->mh0_page == -1) {
1088 doc->mh0_page = offs >> this->page_shift;
1089 if (!findmirror) return 1;
1092 doc->mh1_page = offs >> this->page_shift;
1095 if (doc->mh0_page == -1) {
1096 printk(KERN_WARNING "DiskOnChip %s Media Header not found.\n", id);
1099 /* Only one mediaheader was found. We want buf to contain a
1100 mediaheader on return, so we'll have to re-read the one we found. */
1101 offs = doc->mh0_page << this->page_shift;
1102 ret = mtd->read(mtd, offs, mtd->oobblock, &retlen, buf);
1103 if (retlen != mtd->oobblock) {
1104 /* Insanity. Give up. */
1105 printk(KERN_ERR "Read DiskOnChip Media Header once, but can't reread it???\n");
1111 static inline int __init nftl_partscan(struct mtd_info *mtd,
1112 struct mtd_partition *parts)
1114 struct nand_chip *this = mtd->priv;
1115 struct doc_priv *doc = this->priv;
1118 struct NFTLMediaHeader *mh;
1119 const unsigned psize = 1 << this->page_shift;
1120 unsigned blocks, maxblocks;
1121 int offs, numheaders;
1123 buf = kmalloc(mtd->oobblock, GFP_KERNEL);
1125 printk(KERN_ERR "DiskOnChip mediaheader kmalloc failed!\n");
1128 if (!(numheaders=find_media_headers(mtd, buf, "ANAND", 1))) goto out;
1129 mh = (struct NFTLMediaHeader *) buf;
1131 /*#ifdef CONFIG_MTD_DEBUG_VERBOSE */
1132 /* if (CONFIG_MTD_DEBUG_VERBOSE >= 2) */
1133 printk(KERN_INFO " DataOrgID = %s\n"
1134 " NumEraseUnits = %d\n"
1135 " FirstPhysicalEUN = %d\n"
1136 " FormattedSize = %d\n"
1137 " UnitSizeFactor = %d\n",
1138 mh->DataOrgID, mh->NumEraseUnits,
1139 mh->FirstPhysicalEUN, mh->FormattedSize,
1140 mh->UnitSizeFactor);
1143 blocks = mtd->size >> this->phys_erase_shift;
1144 maxblocks = min(32768U, mtd->erasesize - psize);
1146 if (mh->UnitSizeFactor == 0x00) {
1147 /* Auto-determine UnitSizeFactor. The constraints are:
1148 - There can be at most 32768 virtual blocks.
1149 - There can be at most (virtual block size - page size)
1150 virtual blocks (because MediaHeader+BBT must fit in 1).
1152 mh->UnitSizeFactor = 0xff;
1153 while (blocks > maxblocks) {
1155 maxblocks = min(32768U, (maxblocks << 1) + psize);
1156 mh->UnitSizeFactor--;
1158 printk(KERN_WARNING "UnitSizeFactor=0x00 detected. Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor);
1161 /* NOTE: The lines below modify internal variables of the NAND and MTD
1162 layers; variables with have already been configured by nand_scan.
1163 Unfortunately, we didn't know before this point what these values
1164 should be. Thus, this code is somewhat dependant on the exact
1165 implementation of the NAND layer. */
1166 if (mh->UnitSizeFactor != 0xff) {
1167 this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
1168 mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
1169 printk(KERN_INFO "Setting virtual erase size to %d\n", mtd->erasesize);
1170 blocks = mtd->size >> this->bbt_erase_shift;
1171 maxblocks = min(32768U, mtd->erasesize - psize);
1174 if (blocks > maxblocks) {
1175 printk(KERN_ERR "UnitSizeFactor of 0x%02x is inconsistent with device size. Aborting.\n", mh->UnitSizeFactor);
1179 /* Skip past the media headers. */
1180 offs = max(doc->mh0_page, doc->mh1_page);
1181 offs <<= this->page_shift;
1182 offs += mtd->erasesize;
1184 /*parts[0].name = " DiskOnChip Boot / Media Header partition"; */
1185 /*parts[0].offset = 0; */
1186 /*parts[0].size = offs; */
1188 parts[0].name = " DiskOnChip BDTL partition";
1189 parts[0].offset = offs;
1190 parts[0].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift;
1192 offs += parts[0].size;
1193 if (offs < mtd->size) {
1194 parts[1].name = " DiskOnChip Remainder partition";
1195 parts[1].offset = offs;
1196 parts[1].size = mtd->size - offs;
1206 /* This is a stripped-down copy of the code in inftlmount.c */
1207 static inline int __init inftl_partscan(struct mtd_info *mtd,
1208 struct mtd_partition *parts)
1210 struct nand_chip *this = mtd->priv;
1211 struct doc_priv *doc = this->priv;
1214 struct INFTLMediaHeader *mh;
1215 struct INFTLPartition *ip;
1218 int vshift, lastvunit = 0;
1220 int end = mtd->size;
1222 if (inftl_bbt_write)
1223 end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift);
1225 buf = kmalloc(mtd->oobblock, GFP_KERNEL);
1227 printk(KERN_ERR "DiskOnChip mediaheader kmalloc failed!\n");
1231 if (!find_media_headers(mtd, buf, "BNAND", 0)) goto out;
1232 doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift);
1233 mh = (struct INFTLMediaHeader *) buf;
1235 mh->NoOfBootImageBlocks = le32_to_cpu(mh->NoOfBootImageBlocks);
1236 mh->NoOfBinaryPartitions = le32_to_cpu(mh->NoOfBinaryPartitions);
1237 mh->NoOfBDTLPartitions = le32_to_cpu(mh->NoOfBDTLPartitions);
1238 mh->BlockMultiplierBits = le32_to_cpu(mh->BlockMultiplierBits);
1239 mh->FormatFlags = le32_to_cpu(mh->FormatFlags);
1240 mh->PercentUsed = le32_to_cpu(mh->PercentUsed);
1242 /*#ifdef CONFIG_MTD_DEBUG_VERBOSE */
1243 /* if (CONFIG_MTD_DEBUG_VERBOSE >= 2) */
1244 printk(KERN_INFO " bootRecordID = %s\n"
1245 " NoOfBootImageBlocks = %d\n"
1246 " NoOfBinaryPartitions = %d\n"
1247 " NoOfBDTLPartitions = %d\n"
1248 " BlockMultiplerBits = %d\n"
1249 " FormatFlgs = %d\n"
1250 " OsakVersion = %d.%d.%d.%d\n"
1251 " PercentUsed = %d\n",
1252 mh->bootRecordID, mh->NoOfBootImageBlocks,
1253 mh->NoOfBinaryPartitions,
1254 mh->NoOfBDTLPartitions,
1255 mh->BlockMultiplierBits, mh->FormatFlags,
1256 ((unsigned char *) &mh->OsakVersion)[0] & 0xf,
1257 ((unsigned char *) &mh->OsakVersion)[1] & 0xf,
1258 ((unsigned char *) &mh->OsakVersion)[2] & 0xf,
1259 ((unsigned char *) &mh->OsakVersion)[3] & 0xf,
1263 vshift = this->phys_erase_shift + mh->BlockMultiplierBits;
1265 blocks = mtd->size >> vshift;
1266 if (blocks > 32768) {
1267 printk(KERN_ERR "BlockMultiplierBits=%d is inconsistent with device size. Aborting.\n", mh->BlockMultiplierBits);
1271 blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift);
1272 if (inftl_bbt_write && (blocks > mtd->erasesize)) {
1273 printk(KERN_ERR "Writeable BBTs spanning more than one erase block are not yet supported. FIX ME!\n");
1277 /* Scan the partitions */
1278 for (i = 0; (i < 4); i++) {
1279 ip = &(mh->Partitions[i]);
1280 ip->virtualUnits = le32_to_cpu(ip->virtualUnits);
1281 ip->firstUnit = le32_to_cpu(ip->firstUnit);
1282 ip->lastUnit = le32_to_cpu(ip->lastUnit);
1283 ip->flags = le32_to_cpu(ip->flags);
1284 ip->spareUnits = le32_to_cpu(ip->spareUnits);
1285 ip->Reserved0 = le32_to_cpu(ip->Reserved0);
1287 /*#ifdef CONFIG_MTD_DEBUG_VERBOSE */
1288 /* if (CONFIG_MTD_DEBUG_VERBOSE >= 2) */
1289 printk(KERN_INFO " PARTITION[%d] ->\n"
1290 " virtualUnits = %d\n"
1294 " spareUnits = %d\n",
1295 i, ip->virtualUnits, ip->firstUnit,
1296 ip->lastUnit, ip->flags,
1301 if ((i == 0) && (ip->firstUnit > 0)) {
1302 parts[0].name = " DiskOnChip IPL / Media Header partition";
1303 parts[0].offset = 0;
1304 parts[0].size = mtd->erasesize * ip->firstUnit;
1309 if (ip->flags & INFTL_BINARY)
1310 parts[numparts].name = " DiskOnChip BDK partition";
1312 parts[numparts].name = " DiskOnChip BDTL partition";
1313 parts[numparts].offset = ip->firstUnit << vshift;
1314 parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift;
1316 if (ip->lastUnit > lastvunit) lastvunit = ip->lastUnit;
1317 if (ip->flags & INFTL_LAST) break;
1320 if ((lastvunit << vshift) < end) {
1321 parts[numparts].name = " DiskOnChip Remainder partition";
1322 parts[numparts].offset = lastvunit << vshift;
1323 parts[numparts].size = end - parts[numparts].offset;
1332 static int __init nftl_scan_bbt(struct mtd_info *mtd)
1335 struct nand_chip *this = mtd->priv;
1336 struct doc_priv *doc = this->priv;
1337 struct mtd_partition parts[2];
1339 memset((char *) parts, 0, sizeof(parts));
1340 /* On NFTL, we have to find the media headers before we can read the
1341 BBTs, since they're stored in the media header eraseblocks. */
1342 numparts = nftl_partscan(mtd, parts);
1343 if (!numparts) return -EIO;
1344 this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1345 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1347 this->bbt_td->veroffs = 7;
1348 this->bbt_td->pages[0] = doc->mh0_page + 1;
1349 if (doc->mh1_page != -1) {
1350 this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1351 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1353 this->bbt_md->veroffs = 7;
1354 this->bbt_md->pages[0] = doc->mh1_page + 1;
1356 this->bbt_md = NULL;
1359 /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
1360 At least as nand_bbt.c is currently written. */
1361 if ((ret = nand_scan_bbt(mtd, NULL)))
1363 add_mtd_device(mtd);
1364 #ifdef CONFIG_MTD_PARTITIONS
1366 add_mtd_partitions(mtd, parts, numparts);
1371 static int __init inftl_scan_bbt(struct mtd_info *mtd)
1374 struct nand_chip *this = mtd->priv;
1375 struct doc_priv *doc = this->priv;
1376 struct mtd_partition parts[5];
1378 if (this->numchips > doc->chips_per_floor) {
1379 printk(KERN_ERR "Multi-floor INFTL devices not yet supported.\n");
1383 if (DoC_is_MillenniumPlus(doc)) {
1384 this->bbt_td->options = NAND_BBT_2BIT | NAND_BBT_ABSPAGE;
1385 if (inftl_bbt_write)
1386 this->bbt_td->options |= NAND_BBT_WRITE;
1387 this->bbt_td->pages[0] = 2;
1388 this->bbt_md = NULL;
1390 this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT |
1392 if (inftl_bbt_write)
1393 this->bbt_td->options |= NAND_BBT_WRITE;
1394 this->bbt_td->offs = 8;
1395 this->bbt_td->len = 8;
1396 this->bbt_td->veroffs = 7;
1397 this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1398 this->bbt_td->reserved_block_code = 0x01;
1399 this->bbt_td->pattern = "MSYS_BBT";
1401 this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT |
1403 if (inftl_bbt_write)
1404 this->bbt_md->options |= NAND_BBT_WRITE;
1405 this->bbt_md->offs = 8;
1406 this->bbt_md->len = 8;
1407 this->bbt_md->veroffs = 7;
1408 this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1409 this->bbt_md->reserved_block_code = 0x01;
1410 this->bbt_md->pattern = "TBB_SYSM";
1413 /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
1414 At least as nand_bbt.c is currently written. */
1415 if ((ret = nand_scan_bbt(mtd, NULL)))
1417 memset((char *) parts, 0, sizeof(parts));
1418 numparts = inftl_partscan(mtd, parts);
1419 /* At least for now, require the INFTL Media Header. We could probably
1420 do without it for non-INFTL use, since all it gives us is
1421 autopartitioning, but I want to give it more thought. */
1422 if (!numparts) return -EIO;
1423 add_mtd_device(mtd);
1424 #ifdef CONFIG_MTD_PARTITIONS
1426 add_mtd_partitions(mtd, parts, numparts);
1431 static inline int __init doc2000_init(struct mtd_info *mtd)
1433 struct nand_chip *this = mtd->priv;
1434 struct doc_priv *doc = this->priv;
1436 this->write_byte = doc2000_write_byte;
1437 this->read_byte = doc2000_read_byte;
1438 this->write_buf = doc2000_writebuf;
1439 this->read_buf = doc2000_readbuf;
1440 this->verify_buf = doc2000_verifybuf;
1441 this->scan_bbt = nftl_scan_bbt;
1443 doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
1444 doc2000_count_chips(mtd);
1445 mtd->name = "DiskOnChip 2000 (NFTL Model)";
1446 return (4 * doc->chips_per_floor);
1449 static inline int __init doc2001_init(struct mtd_info *mtd)
1451 struct nand_chip *this = mtd->priv;
1452 struct doc_priv *doc = this->priv;
1454 this->write_byte = doc2001_write_byte;
1455 this->read_byte = doc2001_read_byte;
1456 this->write_buf = doc2001_writebuf;
1457 this->read_buf = doc2001_readbuf;
1458 this->verify_buf = doc2001_verifybuf;
1460 ReadDOC(doc->virtadr, ChipID);
1461 ReadDOC(doc->virtadr, ChipID);
1462 ReadDOC(doc->virtadr, ChipID);
1463 if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) {
1464 /* It's not a Millennium; it's one of the newer
1465 DiskOnChip 2000 units with a similar ASIC.
1466 Treat it like a Millennium, except that it
1467 can have multiple chips. */
1468 doc2000_count_chips(mtd);
1469 mtd->name = "DiskOnChip 2000 (INFTL Model)";
1470 this->scan_bbt = inftl_scan_bbt;
1471 return (4 * doc->chips_per_floor);
1473 /* Bog-standard Millennium */
1474 doc->chips_per_floor = 1;
1475 mtd->name = "DiskOnChip Millennium";
1476 this->scan_bbt = nftl_scan_bbt;
1481 static inline int __init doc2001plus_init(struct mtd_info *mtd)
1483 struct nand_chip *this = mtd->priv;
1484 struct doc_priv *doc = this->priv;
1486 this->write_byte = NULL;
1487 this->read_byte = doc2001plus_read_byte;
1488 this->write_buf = doc2001plus_writebuf;
1489 this->read_buf = doc2001plus_readbuf;
1490 this->verify_buf = doc2001plus_verifybuf;
1491 this->scan_bbt = inftl_scan_bbt;
1492 this->hwcontrol = NULL;
1493 this->select_chip = doc2001plus_select_chip;
1494 this->cmdfunc = doc2001plus_command;
1495 this->enable_hwecc = doc2001plus_enable_hwecc;
1497 doc->chips_per_floor = 1;
1498 mtd->name = "DiskOnChip Millennium Plus";
1503 static inline int __init doc_probe(unsigned long physadr)
1505 unsigned char ChipID;
1506 struct mtd_info *mtd;
1507 struct nand_chip *nand;
1508 struct doc_priv *doc;
1509 void __iomem *virtadr;
1510 unsigned char save_control;
1511 unsigned char tmp, tmpb, tmpc;
1512 int reg, len, numchips;
1515 virtadr = ioremap(physadr, DOC_IOREMAP_LEN);
1517 printk(KERN_ERR "Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n", DOC_IOREMAP_LEN, physadr);
1521 /* It's not possible to cleanly detect the DiskOnChip - the
1522 * bootup procedure will put the device into reset mode, and
1523 * it's not possible to talk to it without actually writing
1524 * to the DOCControl register. So we store the current contents
1525 * of the DOCControl register's location, in case we later decide
1526 * that it's not a DiskOnChip, and want to put it back how we
1529 save_control = ReadDOC(virtadr, DOCControl);
1531 /* Reset the DiskOnChip ASIC */
1532 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET,
1533 virtadr, DOCControl);
1534 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET,
1535 virtadr, DOCControl);
1537 /* Enable the DiskOnChip ASIC */
1538 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL,
1539 virtadr, DOCControl);
1540 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL,
1541 virtadr, DOCControl);
1543 ChipID = ReadDOC(virtadr, ChipID);
1546 case DOC_ChipID_Doc2k:
1547 reg = DoC_2k_ECCStatus;
1549 case DOC_ChipID_DocMil:
1552 case DOC_ChipID_DocMilPlus16:
1553 case DOC_ChipID_DocMilPlus32:
1555 /* Possible Millennium Plus, need to do more checks */
1556 /* Possibly release from power down mode */
1557 for (tmp = 0; (tmp < 4); tmp++)
1558 ReadDOC(virtadr, Mplus_Power);
1560 /* Reset the Millennium Plus ASIC */
1561 tmp = DOC_MODE_RESET | DOC_MODE_MDWREN | DOC_MODE_RST_LAT |
1563 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1564 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1567 /* Enable the Millennium Plus ASIC */
1568 tmp = DOC_MODE_NORMAL | DOC_MODE_MDWREN | DOC_MODE_RST_LAT |
1570 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1571 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1574 ChipID = ReadDOC(virtadr, ChipID);
1577 case DOC_ChipID_DocMilPlus16:
1578 reg = DoC_Mplus_Toggle;
1580 case DOC_ChipID_DocMilPlus32:
1581 printk(KERN_ERR "DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
1592 /* Check the TOGGLE bit in the ECC register */
1593 tmp = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1594 tmpb = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1595 tmpc = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1596 if ((tmp == tmpb) || (tmp != tmpc)) {
1597 printk(KERN_WARNING "Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr);
1602 for (mtd = doclist; mtd; mtd = doc->nextdoc) {
1603 unsigned char oldval;
1604 unsigned char newval;
1607 /* Use the alias resolution register to determine if this is
1608 in fact the same DOC aliased to a new address. If writes
1609 to one chip's alias resolution register change the value on
1610 the other chip, they're the same chip. */
1611 if (ChipID == DOC_ChipID_DocMilPlus16) {
1612 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1613 newval = ReadDOC(virtadr, Mplus_AliasResolution);
1615 oldval = ReadDOC(doc->virtadr, AliasResolution);
1616 newval = ReadDOC(virtadr, AliasResolution);
1618 if (oldval != newval)
1620 if (ChipID == DOC_ChipID_DocMilPlus16) {
1621 WriteDOC(~newval, virtadr, Mplus_AliasResolution);
1622 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1623 WriteDOC(newval, virtadr, Mplus_AliasResolution); /* restore it */
1625 WriteDOC(~newval, virtadr, AliasResolution);
1626 oldval = ReadDOC(doc->virtadr, AliasResolution);
1627 WriteDOC(newval, virtadr, AliasResolution); /* restore it */
1630 if (oldval == newval) {
1631 printk(KERN_DEBUG "Found alias of DOC at 0x%lx to 0x%lx\n", doc->physadr, physadr);
1636 printk(KERN_NOTICE "DiskOnChip found at 0x%lx\n", physadr);
1638 len = sizeof(struct mtd_info) +
1639 sizeof(struct nand_chip) +
1640 sizeof(struct doc_priv) +
1641 (2 * sizeof(struct nand_bbt_descr));
1642 mtd = kmalloc(len, GFP_KERNEL);
1644 printk(KERN_ERR "DiskOnChip kmalloc (%d bytes) failed!\n", len);
1648 memset(mtd, 0, len);
1650 nand = (struct nand_chip *) (mtd + 1);
1651 doc = (struct doc_priv *) (nand + 1);
1652 nand->bbt_td = (struct nand_bbt_descr *) (doc + 1);
1653 nand->bbt_md = nand->bbt_td + 1;
1656 mtd->owner = THIS_MODULE;
1659 nand->select_chip = doc200x_select_chip;
1660 nand->hwcontrol = doc200x_hwcontrol;
1661 nand->dev_ready = doc200x_dev_ready;
1662 nand->waitfunc = doc200x_wait;
1663 nand->block_bad = doc200x_block_bad;
1664 nand->enable_hwecc = doc200x_enable_hwecc;
1665 nand->calculate_ecc = doc200x_calculate_ecc;
1666 nand->correct_data = doc200x_correct_data;
1668 nand->autooob = &doc200x_oobinfo;
1669 nand->eccmode = NAND_ECC_HW6_512;
1670 nand->options = NAND_USE_FLASH_BBT | NAND_HWECC_SYNDROME;
1672 doc->physadr = physadr;
1673 doc->virtadr = virtadr;
1674 doc->ChipID = ChipID;
1679 doc->nextdoc = doclist;
1681 if (ChipID == DOC_ChipID_Doc2k)
1682 numchips = doc2000_init(mtd);
1683 else if (ChipID == DOC_ChipID_DocMilPlus16)
1684 numchips = doc2001plus_init(mtd);
1686 numchips = doc2001_init(mtd);
1688 if ((ret = nand_scan(mtd, numchips))) {
1689 /* DBB note: i believe nand_release is necessary here, as
1690 buffers may have been allocated in nand_base. Check with
1692 /* nand_release will call del_mtd_device, but we haven't yet
1693 added it. This is handled without incident by
1694 del_mtd_device, as far as I can tell. */
1705 /* Put back the contents of the DOCControl register, in case it's not
1706 actually a DiskOnChip. */
1707 WriteDOC(save_control, virtadr, DOCControl);
1713 static void release_nanddoc(void)
1715 struct mtd_info *mtd, *nextmtd;
1716 struct nand_chip *nand;
1717 struct doc_priv *doc;
1719 for (mtd = doclist; mtd; mtd = nextmtd) {
1723 nextmtd = doc->nextdoc;
1725 iounmap(doc->virtadr);
1730 static int __init init_nanddoc(void)
1734 /* We could create the decoder on demand, if memory is a concern.
1735 * This way we have it handy, if an error happens
1737 * Symbolsize is 10 (bits)
1738 * Primitve polynomial is x^10+x^3+1
1739 * first consecutive root is 510
1740 * primitve element to generate roots = 1
1741 * generator polinomial degree = 4
1743 rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS);
1745 printk (KERN_ERR "DiskOnChip: Could not create a RS decoder\n");
1749 if (doc_config_location) {
1750 printk(KERN_INFO "Using configured DiskOnChip probe address 0x%lx\n", doc_config_location);
1751 ret = doc_probe(doc_config_location);
1755 for (i=0; (doc_locations[i] != 0xffffffff); i++) {
1756 doc_probe(doc_locations[i]);
1759 /* No banner message any more. Print a message if no DiskOnChip
1760 found, so the user knows we at least tried. */
1762 printk(KERN_INFO "No valid DiskOnChip devices found\n");
1768 free_rs(rs_decoder);
1772 static void __exit cleanup_nanddoc(void)
1774 /* Cleanup the nand/DoC resources */
1777 /* Free the reed solomon resources */
1779 free_rs(rs_decoder);
1783 module_init(init_nanddoc);
1784 module_exit(cleanup_nanddoc);
1786 MODULE_LICENSE("GPL");
1787 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1788 MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver\n");