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 * Interface to generic NAND code for M-Systems DiskOnChip devices
14 * $Id: diskonchip.c,v 1.34 2004/08/09 19:41:12 dbrown Exp $
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/sched.h>
20 #include <linux/delay.h>
23 #include <linux/mtd/mtd.h>
24 #include <linux/mtd/nand.h>
25 #include <linux/mtd/doc2000.h>
26 #include <linux/mtd/compatmac.h>
27 #include <linux/mtd/partitions.h>
28 #include <linux/mtd/inftl.h>
30 /* Where to look for the devices? */
31 #ifndef CONFIG_MTD_DISKONCHIP_PROBE_ADDRESS
32 #define CONFIG_MTD_DISKONCHIP_PROBE_ADDRESS 0
35 static unsigned long __initdata doc_locations[] = {
36 #if defined (__alpha__) || defined(__i386__) || defined(__x86_64__)
37 #ifdef CONFIG_MTD_DISKONCHIP_PROBE_HIGH
38 0xfffc8000, 0xfffca000, 0xfffcc000, 0xfffce000,
39 0xfffd0000, 0xfffd2000, 0xfffd4000, 0xfffd6000,
40 0xfffd8000, 0xfffda000, 0xfffdc000, 0xfffde000,
41 0xfffe0000, 0xfffe2000, 0xfffe4000, 0xfffe6000,
42 0xfffe8000, 0xfffea000, 0xfffec000, 0xfffee000,
43 #else /* CONFIG_MTD_DOCPROBE_HIGH */
44 0xc8000, 0xca000, 0xcc000, 0xce000,
45 0xd0000, 0xd2000, 0xd4000, 0xd6000,
46 0xd8000, 0xda000, 0xdc000, 0xde000,
47 0xe0000, 0xe2000, 0xe4000, 0xe6000,
48 0xe8000, 0xea000, 0xec000, 0xee000,
49 #endif /* CONFIG_MTD_DOCPROBE_HIGH */
50 #elif defined(__PPC__)
52 #elif defined(CONFIG_MOMENCO_OCELOT)
55 #elif defined(CONFIG_MOMENCO_OCELOT_G) || defined (CONFIG_MOMENCO_OCELOT_C)
58 #warning Unknown architecture for DiskOnChip. No default probe locations defined
62 static struct mtd_info *doclist = NULL;
65 unsigned long virtadr;
66 unsigned long physadr;
69 int chips_per_floor; /* The number of chips detected on each floor */
74 struct mtd_info *nextdoc;
77 /* Max number of eraseblocks to scan (from start of device) for the (I)NFTL
78 MediaHeader. The spec says to just keep going, I think, but that's just
80 #define MAX_MEDIAHEADER_SCAN 8
82 /* This is the syndrome computed by the HW ecc generator upon reading an empty
83 page, one with all 0xff for data and stored ecc code. */
84 static u_char empty_read_syndrome[6] = { 0x26, 0xff, 0x6d, 0x47, 0x73, 0x7a };
85 /* This is the ecc value computed by the HW ecc generator upon writing an empty
86 page, one with all 0xff for data. */
87 static u_char empty_write_ecc[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
89 #define INFTL_BBT_RESERVED_BLOCKS 4
91 #define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
92 #define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
93 #define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
95 static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd);
96 static void doc200x_select_chip(struct mtd_info *mtd, int chip);
99 MODULE_PARM(debug, "i");
101 static int try_dword=1;
102 MODULE_PARM(try_dword, "i");
104 static int no_ecc_failures=0;
105 MODULE_PARM(no_ecc_failures, "i");
107 static int no_autopart=0;
108 MODULE_PARM(no_autopart, "i");
110 #ifdef MTD_NAND_DISKONCHIP_BBTWRITE
111 static int inftl_bbt_write=1;
113 static int inftl_bbt_write=0;
115 MODULE_PARM(inftl_bbt_write, "i");
117 static unsigned long doc_config_location = CONFIG_MTD_DISKONCHIP_PROBE_ADDRESS;
118 MODULE_PARM(doc_config_location, "l");
119 MODULE_PARM_DESC(doc_config_location, "Physical memory address at which to probe for DiskOnChip");
121 static void DoC_Delay(struct doc_priv *doc, unsigned short cycles)
126 for (i = 0; i < cycles; i++) {
127 if (DoC_is_Millennium(doc))
128 dummy = ReadDOC(doc->virtadr, NOP);
129 else if (DoC_is_MillenniumPlus(doc))
130 dummy = ReadDOC(doc->virtadr, Mplus_NOP);
132 dummy = ReadDOC(doc->virtadr, DOCStatus);
137 #define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
139 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
140 static int _DoC_WaitReady(struct doc_priv *doc)
142 unsigned long docptr = doc->virtadr;
143 unsigned long timeo = jiffies + (HZ * 10);
145 if(debug) printk("_DoC_WaitReady...\n");
146 /* Out-of-line routine to wait for chip response */
147 if (DoC_is_MillenniumPlus(doc)) {
148 while ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
149 if (time_after(jiffies, timeo)) {
150 printk("_DoC_WaitReady timed out.\n");
157 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
158 if (time_after(jiffies, timeo)) {
159 printk("_DoC_WaitReady timed out.\n");
170 static inline int DoC_WaitReady(struct doc_priv *doc)
172 unsigned long docptr = doc->virtadr;
175 if (DoC_is_MillenniumPlus(doc)) {
178 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
179 /* Call the out-of-line routine to wait */
180 ret = _DoC_WaitReady(doc);
184 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
185 /* Call the out-of-line routine to wait */
186 ret = _DoC_WaitReady(doc);
190 if(debug) printk("DoC_WaitReady OK\n");
194 static void doc2000_write_byte(struct mtd_info *mtd, u_char datum)
196 struct nand_chip *this = mtd->priv;
197 struct doc_priv *doc = (void *)this->priv;
198 unsigned long docptr = doc->virtadr;
200 if(debug)printk("write_byte %02x\n", datum);
201 WriteDOC(datum, docptr, CDSNSlowIO);
202 WriteDOC(datum, docptr, 2k_CDSN_IO);
205 static u_char doc2000_read_byte(struct mtd_info *mtd)
207 struct nand_chip *this = mtd->priv;
208 struct doc_priv *doc = (void *)this->priv;
209 unsigned long docptr = doc->virtadr;
212 ReadDOC(docptr, CDSNSlowIO);
214 ret = ReadDOC(docptr, 2k_CDSN_IO);
215 if (debug) printk("read_byte returns %02x\n", ret);
219 static void doc2000_writebuf(struct mtd_info *mtd,
220 const u_char *buf, int len)
222 struct nand_chip *this = mtd->priv;
223 struct doc_priv *doc = (void *)this->priv;
224 unsigned long docptr = doc->virtadr;
226 if (debug)printk("writebuf of %d bytes: ", len);
227 for (i=0; i < len; i++) {
228 WriteDOC_(buf[i], docptr, DoC_2k_CDSN_IO + i);
230 printk("%02x ", buf[i]);
232 if (debug) printk("\n");
235 static void doc2000_readbuf(struct mtd_info *mtd,
236 u_char *buf, int len)
238 struct nand_chip *this = mtd->priv;
239 struct doc_priv *doc = (void *)this->priv;
240 unsigned long docptr = doc->virtadr;
243 if (debug)printk("readbuf of %d bytes: ", len);
245 for (i=0; i < len; i++) {
246 buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
250 static void doc2000_readbuf_dword(struct mtd_info *mtd,
251 u_char *buf, int len)
253 struct nand_chip *this = mtd->priv;
254 struct doc_priv *doc = (void *)this->priv;
255 unsigned long docptr = doc->virtadr;
258 if (debug) printk("readbuf_dword of %d bytes: ", len);
260 if (unlikely((((unsigned long)buf)|len) & 3)) {
261 for (i=0; i < len; i++) {
262 *(uint8_t *)(&buf[i]) = ReadDOC(docptr, 2k_CDSN_IO + i);
265 for (i=0; i < len; i+=4) {
266 *(uint32_t*)(&buf[i]) = readl(docptr + DoC_2k_CDSN_IO + i);
271 static int doc2000_verifybuf(struct mtd_info *mtd,
272 const u_char *buf, int len)
274 struct nand_chip *this = mtd->priv;
275 struct doc_priv *doc = (void *)this->priv;
276 unsigned long docptr = doc->virtadr;
279 for (i=0; i < len; i++)
280 if (buf[i] != ReadDOC(docptr, 2k_CDSN_IO))
285 static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr)
287 struct nand_chip *this = mtd->priv;
288 struct doc_priv *doc = (void *)this->priv;
291 doc200x_select_chip(mtd, nr);
292 doc200x_hwcontrol(mtd, NAND_CTL_SETCLE);
293 this->write_byte(mtd, NAND_CMD_READID);
294 doc200x_hwcontrol(mtd, NAND_CTL_CLRCLE);
295 doc200x_hwcontrol(mtd, NAND_CTL_SETALE);
296 this->write_byte(mtd, 0);
297 doc200x_hwcontrol(mtd, NAND_CTL_CLRALE);
299 ret = this->read_byte(mtd) << 8;
300 ret |= this->read_byte(mtd);
302 if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) {
303 /* First chip probe. See if we get same results by 32-bit access */
308 unsigned long docptr = doc->virtadr;
310 doc200x_hwcontrol(mtd, NAND_CTL_SETCLE);
311 doc2000_write_byte(mtd, NAND_CMD_READID);
312 doc200x_hwcontrol(mtd, NAND_CTL_CLRCLE);
313 doc200x_hwcontrol(mtd, NAND_CTL_SETALE);
314 doc2000_write_byte(mtd, 0);
315 doc200x_hwcontrol(mtd, NAND_CTL_CLRALE);
317 ident.dword = readl(docptr + DoC_2k_CDSN_IO);
318 if (((ident.byte[0] << 8) | ident.byte[1]) == ret) {
319 printk(KERN_INFO "DiskOnChip 2000 responds to DWORD access\n");
320 this->read_buf = &doc2000_readbuf_dword;
327 static void __init doc2000_count_chips(struct mtd_info *mtd)
329 struct nand_chip *this = mtd->priv;
330 struct doc_priv *doc = (void *)this->priv;
334 /* Max 4 chips per floor on DiskOnChip 2000 */
335 doc->chips_per_floor = 4;
337 /* Find out what the first chip is */
338 mfrid = doc200x_ident_chip(mtd, 0);
340 /* Find how many chips in each floor. */
341 for (i = 1; i < 4; i++) {
342 if (doc200x_ident_chip(mtd, i) != mfrid)
345 doc->chips_per_floor = i;
346 printk(KERN_DEBUG "Detected %d chips per floor.\n", i);
349 static int doc200x_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
351 struct doc_priv *doc = (void *)this->priv;
356 this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
358 status = (int)this->read_byte(mtd);
363 static void doc2001_write_byte(struct mtd_info *mtd, u_char datum)
365 struct nand_chip *this = mtd->priv;
366 struct doc_priv *doc = (void *)this->priv;
367 unsigned long docptr = doc->virtadr;
369 WriteDOC(datum, docptr, CDSNSlowIO);
370 WriteDOC(datum, docptr, Mil_CDSN_IO);
371 WriteDOC(datum, docptr, WritePipeTerm);
374 static u_char doc2001_read_byte(struct mtd_info *mtd)
376 struct nand_chip *this = mtd->priv;
377 struct doc_priv *doc = (void *)this->priv;
378 unsigned long docptr = doc->virtadr;
380 //ReadDOC(docptr, CDSNSlowIO);
381 /* 11.4.5 -- delay twice to allow extended length cycle */
383 ReadDOC(docptr, ReadPipeInit);
384 //return ReadDOC(docptr, Mil_CDSN_IO);
385 return ReadDOC(docptr, LastDataRead);
388 static void doc2001_writebuf(struct mtd_info *mtd,
389 const u_char *buf, int len)
391 struct nand_chip *this = mtd->priv;
392 struct doc_priv *doc = (void *)this->priv;
393 unsigned long docptr = doc->virtadr;
396 for (i=0; i < len; i++)
397 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
398 /* Terminate write pipeline */
399 WriteDOC(0x00, docptr, WritePipeTerm);
402 static void doc2001_readbuf(struct mtd_info *mtd,
403 u_char *buf, int len)
405 struct nand_chip *this = mtd->priv;
406 struct doc_priv *doc = (void *)this->priv;
407 unsigned long docptr = doc->virtadr;
410 /* Start read pipeline */
411 ReadDOC(docptr, ReadPipeInit);
413 for (i=0; i < len-1; i++)
414 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
416 /* Terminate read pipeline */
417 buf[i] = ReadDOC(docptr, LastDataRead);
420 static int doc2001_verifybuf(struct mtd_info *mtd,
421 const u_char *buf, int len)
423 struct nand_chip *this = mtd->priv;
424 struct doc_priv *doc = (void *)this->priv;
425 unsigned long docptr = doc->virtadr;
428 /* Start read pipeline */
429 ReadDOC(docptr, ReadPipeInit);
431 for (i=0; i < len-1; i++)
432 if (buf[i] != ReadDOC(docptr, Mil_CDSN_IO)) {
433 ReadDOC(docptr, LastDataRead);
436 if (buf[i] != ReadDOC(docptr, LastDataRead))
441 static u_char doc2001plus_read_byte(struct mtd_info *mtd)
443 struct nand_chip *this = mtd->priv;
444 struct doc_priv *doc = (void *)this->priv;
445 unsigned long docptr = doc->virtadr;
448 ReadDOC(docptr, Mplus_ReadPipeInit);
449 ReadDOC(docptr, Mplus_ReadPipeInit);
450 ret = ReadDOC(docptr, Mplus_LastDataRead);
451 if (debug) printk("read_byte returns %02x\n", ret);
455 static void doc2001plus_writebuf(struct mtd_info *mtd,
456 const u_char *buf, int len)
458 struct nand_chip *this = mtd->priv;
459 struct doc_priv *doc = (void *)this->priv;
460 unsigned long docptr = doc->virtadr;
463 if (debug)printk("writebuf of %d bytes: ", len);
464 for (i=0; i < len; i++) {
465 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
467 printk("%02x ", buf[i]);
469 if (debug) printk("\n");
472 static void doc2001plus_readbuf(struct mtd_info *mtd,
473 u_char *buf, int len)
475 struct nand_chip *this = mtd->priv;
476 struct doc_priv *doc = (void *)this->priv;
477 unsigned long docptr = doc->virtadr;
480 if (debug)printk("readbuf of %d bytes: ", len);
482 /* Start read pipeline */
483 ReadDOC(docptr, Mplus_ReadPipeInit);
484 ReadDOC(docptr, Mplus_ReadPipeInit);
486 for (i=0; i < len-2; i++) {
487 buf[i] = ReadDOC(docptr, Mil_CDSN_IO);
489 printk("%02x ", buf[i]);
492 /* Terminate read pipeline */
493 buf[len-2] = ReadDOC(docptr, Mplus_LastDataRead);
495 printk("%02x ", buf[len-2]);
496 buf[len-1] = ReadDOC(docptr, Mplus_LastDataRead);
498 printk("%02x ", buf[len-1]);
499 if (debug) printk("\n");
502 static int doc2001plus_verifybuf(struct mtd_info *mtd,
503 const u_char *buf, int len)
505 struct nand_chip *this = mtd->priv;
506 struct doc_priv *doc = (void *)this->priv;
507 unsigned long docptr = doc->virtadr;
510 if (debug)printk("verifybuf of %d bytes: ", len);
512 /* Start read pipeline */
513 ReadDOC(docptr, Mplus_ReadPipeInit);
514 ReadDOC(docptr, Mplus_ReadPipeInit);
516 for (i=0; i < len-2; i++)
517 if (buf[i] != ReadDOC(docptr, Mil_CDSN_IO)) {
518 ReadDOC(docptr, Mplus_LastDataRead);
519 ReadDOC(docptr, Mplus_LastDataRead);
522 if (buf[len-2] != ReadDOC(docptr, Mplus_LastDataRead))
524 if (buf[len-1] != ReadDOC(docptr, Mplus_LastDataRead))
529 static void doc2001plus_select_chip(struct mtd_info *mtd, int chip)
531 struct nand_chip *this = mtd->priv;
532 struct doc_priv *doc = (void *)this->priv;
533 unsigned long docptr = doc->virtadr;
536 if(debug)printk("select chip (%d)\n", chip);
539 /* Disable flash internally */
540 WriteDOC(0, docptr, Mplus_FlashSelect);
544 floor = chip / doc->chips_per_floor;
545 chip -= (floor * doc->chips_per_floor);
547 /* Assert ChipEnable and deassert WriteProtect */
548 WriteDOC((DOC_FLASH_CE), docptr, Mplus_FlashSelect);
549 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
552 doc->curfloor = floor;
555 static void doc200x_select_chip(struct mtd_info *mtd, int chip)
557 struct nand_chip *this = mtd->priv;
558 struct doc_priv *doc = (void *)this->priv;
559 unsigned long docptr = doc->virtadr;
562 if(debug)printk("select chip (%d)\n", chip);
567 floor = chip / doc->chips_per_floor;
568 chip -= (floor * doc->chips_per_floor);
570 /* 11.4.4 -- deassert CE before changing chip */
571 doc200x_hwcontrol(mtd, NAND_CTL_CLRNCE);
573 WriteDOC(floor, docptr, FloorSelect);
574 WriteDOC(chip, docptr, CDSNDeviceSelect);
576 doc200x_hwcontrol(mtd, NAND_CTL_SETNCE);
579 doc->curfloor = floor;
582 static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd)
584 struct nand_chip *this = mtd->priv;
585 struct doc_priv *doc = (void *)this->priv;
586 unsigned long docptr = doc->virtadr;
589 case NAND_CTL_SETNCE:
590 doc->CDSNControl |= CDSN_CTRL_CE;
592 case NAND_CTL_CLRNCE:
593 doc->CDSNControl &= ~CDSN_CTRL_CE;
595 case NAND_CTL_SETCLE:
596 doc->CDSNControl |= CDSN_CTRL_CLE;
598 case NAND_CTL_CLRCLE:
599 doc->CDSNControl &= ~CDSN_CTRL_CLE;
601 case NAND_CTL_SETALE:
602 doc->CDSNControl |= CDSN_CTRL_ALE;
604 case NAND_CTL_CLRALE:
605 doc->CDSNControl &= ~CDSN_CTRL_ALE;
608 doc->CDSNControl |= CDSN_CTRL_WP;
611 doc->CDSNControl &= ~CDSN_CTRL_WP;
614 if (debug)printk("hwcontrol(%d): %02x\n", cmd, doc->CDSNControl);
615 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
616 /* 11.4.3 -- 4 NOPs after CSDNControl write */
620 static void doc2001plus_command (struct mtd_info *mtd, unsigned command, int column, int page_addr)
622 struct nand_chip *this = mtd->priv;
623 struct doc_priv *doc = (void *)this->priv;
624 unsigned long docptr = doc->virtadr;
627 * Must terminate write pipeline before sending any commands
630 if (command == NAND_CMD_PAGEPROG) {
631 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
632 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
636 * Write out the command to the device.
638 if (command == NAND_CMD_SEQIN) {
641 if (column >= mtd->oobblock) {
643 column -= mtd->oobblock;
644 readcmd = NAND_CMD_READOOB;
645 } else if (column < 256) {
646 /* First 256 bytes --> READ0 */
647 readcmd = NAND_CMD_READ0;
650 readcmd = NAND_CMD_READ1;
652 WriteDOC(readcmd, docptr, Mplus_FlashCmd);
654 WriteDOC(command, docptr, Mplus_FlashCmd);
655 WriteDOC(0, docptr, Mplus_WritePipeTerm);
656 WriteDOC(0, docptr, Mplus_WritePipeTerm);
658 if (column != -1 || page_addr != -1) {
659 /* Serially input address */
661 /* Adjust columns for 16 bit buswidth */
662 if (this->options & NAND_BUSWIDTH_16)
664 WriteDOC(column, docptr, Mplus_FlashAddress);
666 if (page_addr != -1) {
667 WriteDOC((unsigned char) (page_addr & 0xff), docptr, Mplus_FlashAddress);
668 WriteDOC((unsigned char) ((page_addr >> 8) & 0xff), docptr, Mplus_FlashAddress);
669 /* One more address cycle for higher density devices */
670 if (this->chipsize & 0x0c000000) {
671 WriteDOC((unsigned char) ((page_addr >> 16) & 0x0f), docptr, Mplus_FlashAddress);
672 printk("high density\n");
675 WriteDOC(0, docptr, Mplus_WritePipeTerm);
676 WriteDOC(0, docptr, Mplus_WritePipeTerm);
678 if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 || command == NAND_CMD_READOOB || command == NAND_CMD_READID)
679 WriteDOC(0, docptr, Mplus_FlashControl);
683 * program and erase have their own busy handlers
684 * status and sequential in needs no delay
688 case NAND_CMD_PAGEPROG:
689 case NAND_CMD_ERASE1:
690 case NAND_CMD_ERASE2:
692 case NAND_CMD_STATUS:
698 udelay(this->chip_delay);
699 WriteDOC(NAND_CMD_STATUS, docptr, Mplus_FlashCmd);
700 WriteDOC(0, docptr, Mplus_WritePipeTerm);
701 WriteDOC(0, docptr, Mplus_WritePipeTerm);
702 while ( !(this->read_byte(mtd) & 0x40));
705 /* This applies to read commands */
708 * If we don't have access to the busy pin, we apply the given
711 if (!this->dev_ready) {
712 udelay (this->chip_delay);
717 /* Apply this short delay always to ensure that we do wait tWB in
718 * any case on any machine. */
720 /* wait until command is processed */
721 while (!this->dev_ready(mtd));
724 static int doc200x_dev_ready(struct mtd_info *mtd)
726 struct nand_chip *this = mtd->priv;
727 struct doc_priv *doc = (void *)this->priv;
728 unsigned long docptr = doc->virtadr;
730 if (DoC_is_MillenniumPlus(doc)) {
731 /* 11.4.2 -- must NOP four times before checking FR/B# */
733 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
735 printk("not ready\n");
738 if (debug)printk("was ready\n");
741 /* 11.4.2 -- must NOP four times before checking FR/B# */
743 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
745 printk("not ready\n");
748 /* 11.4.2 -- Must NOP twice if it's ready */
750 if (debug)printk("was ready\n");
755 static int doc200x_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
757 /* This is our last resort if we couldn't find or create a BBT. Just
758 pretend all blocks are good. */
762 static void doc200x_enable_hwecc(struct mtd_info *mtd, int mode)
764 struct nand_chip *this = mtd->priv;
765 struct doc_priv *doc = (void *)this->priv;
766 unsigned long docptr = doc->virtadr;
768 /* Prime the ECC engine */
771 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
772 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
775 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
776 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
781 static void doc2001plus_enable_hwecc(struct mtd_info *mtd, int mode)
783 struct nand_chip *this = mtd->priv;
784 struct doc_priv *doc = (void *)this->priv;
785 unsigned long docptr = doc->virtadr;
787 /* Prime the ECC engine */
790 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
791 WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
794 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
795 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
800 /* This code is only called on write */
801 static int doc200x_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
802 unsigned char *ecc_code)
804 struct nand_chip *this = mtd->priv;
805 struct doc_priv *doc = (void *)this->priv;
806 unsigned long docptr = doc->virtadr;
810 /* flush the pipeline */
811 if (DoC_is_2000(doc)) {
812 WriteDOC(doc->CDSNControl & ~CDSN_CTRL_FLASH_IO, docptr, CDSNControl);
813 WriteDOC(0, docptr, 2k_CDSN_IO);
814 WriteDOC(0, docptr, 2k_CDSN_IO);
815 WriteDOC(0, docptr, 2k_CDSN_IO);
816 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
817 } else if (DoC_is_MillenniumPlus(doc)) {
818 WriteDOC(0, docptr, Mplus_NOP);
819 WriteDOC(0, docptr, Mplus_NOP);
820 WriteDOC(0, docptr, Mplus_NOP);
822 WriteDOC(0, docptr, NOP);
823 WriteDOC(0, docptr, NOP);
824 WriteDOC(0, docptr, NOP);
827 for (i = 0; i < 6; i++) {
828 if (DoC_is_MillenniumPlus(doc))
829 ecc_code[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
831 ecc_code[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
832 if (ecc_code[i] != empty_write_ecc[i])
835 if (DoC_is_MillenniumPlus(doc))
836 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
838 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
840 /* If emptymatch=1, we might have an all-0xff data buffer. Check. */
842 /* Note: this somewhat expensive test should not be triggered
843 often. It could be optimized away by examining the data in
844 the writebuf routine, and remembering the result. */
845 for (i = 0; i < 512; i++) {
846 if (dat[i] == 0xff) continue;
851 /* If emptymatch still =1, we do have an all-0xff data buffer.
852 Return all-0xff ecc value instead of the computed one, so
853 it'll look just like a freshly-erased page. */
854 if (emptymatch) memset(ecc_code, 0xff, 6);
859 static int doc200x_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc)
862 struct nand_chip *this = mtd->priv;
863 struct doc_priv *doc = (void *)this->priv;
864 unsigned long docptr = doc->virtadr;
865 volatile u_char dummy;
868 /* flush the pipeline */
869 if (DoC_is_2000(doc)) {
870 dummy = ReadDOC(docptr, 2k_ECCStatus);
871 dummy = ReadDOC(docptr, 2k_ECCStatus);
872 dummy = ReadDOC(docptr, 2k_ECCStatus);
873 } else if (DoC_is_MillenniumPlus(doc)) {
874 dummy = ReadDOC(docptr, Mplus_ECCConf);
875 dummy = ReadDOC(docptr, Mplus_ECCConf);
876 dummy = ReadDOC(docptr, Mplus_ECCConf);
878 dummy = ReadDOC(docptr, ECCConf);
879 dummy = ReadDOC(docptr, ECCConf);
880 dummy = ReadDOC(docptr, ECCConf);
883 /* Error occured ? */
885 for (i = 0; i < 6; i++) {
886 if (DoC_is_MillenniumPlus(doc))
887 calc_ecc[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
889 calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
890 if (calc_ecc[i] != empty_read_syndrome[i])
893 /* If emptymatch=1, the read syndrome is consistent with an
894 all-0xff data and stored ecc block. Check the stored ecc. */
896 for (i = 0; i < 6; i++) {
897 if (read_ecc[i] == 0xff) continue;
902 /* If emptymatch still =1, check the data block. */
904 /* Note: this somewhat expensive test should not be triggered
905 often. It could be optimized away by examining the data in
906 the readbuf routine, and remembering the result. */
907 for (i = 0; i < 512; i++) {
908 if (dat[i] == 0xff) continue;
913 /* If emptymatch still =1, this is almost certainly a freshly-
914 erased block, in which case the ECC will not come out right.
915 We'll suppress the error and tell the caller everything's
916 OK. Because it is. */
917 if (!emptymatch) ret = doc_decode_ecc (dat, calc_ecc);
919 printk(KERN_ERR "doc200x_correct_data corrected %d errors\n", ret);
921 if (DoC_is_MillenniumPlus(doc))
922 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
924 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
925 if (no_ecc_failures && (ret == -1)) {
926 printk(KERN_ERR "suppressing ECC failure\n");
932 //u_char mydatabuf[528];
934 static struct nand_oobinfo doc200x_oobinfo = {
935 .useecc = MTD_NANDECC_AUTOPLACE,
937 .eccpos = {0, 1, 2, 3, 4, 5},
938 .oobfree = { {8, 8} }
941 /* Find the (I)NFTL Media Header, and optionally also the mirror media header.
942 On sucessful return, buf will contain a copy of the media header for
943 further processing. id is the string to scan for, and will presumably be
944 either "ANAND" or "BNAND". If findmirror=1, also look for the mirror media
945 header. The page #s of the found media headers are placed in mh0_page and
946 mh1_page in the DOC private structure. */
947 static int __init find_media_headers(struct mtd_info *mtd, u_char *buf,
948 const char *id, int findmirror)
950 struct nand_chip *this = mtd->priv;
951 struct doc_priv *doc = (void *)this->priv;
952 unsigned offs, end = (MAX_MEDIAHEADER_SCAN << this->phys_erase_shift);
956 end = min(end, mtd->size); // paranoia
957 for (offs = 0; offs < end; offs += mtd->erasesize) {
958 ret = mtd->read(mtd, offs, mtd->oobblock, &retlen, buf);
959 if (retlen != mtd->oobblock) continue;
961 printk(KERN_WARNING "ECC error scanning DOC at 0x%x\n",
964 if (memcmp(buf, id, 6)) continue;
965 printk(KERN_INFO "Found DiskOnChip %s Media Header at 0x%x\n", id, offs);
966 if (doc->mh0_page == -1) {
967 doc->mh0_page = offs >> this->page_shift;
968 if (!findmirror) return 1;
971 doc->mh1_page = offs >> this->page_shift;
974 if (doc->mh0_page == -1) {
975 printk(KERN_WARNING "DiskOnChip %s Media Header not found.\n", id);
978 /* Only one mediaheader was found. We want buf to contain a
979 mediaheader on return, so we'll have to re-read the one we found. */
980 offs = doc->mh0_page << this->page_shift;
981 ret = mtd->read(mtd, offs, mtd->oobblock, &retlen, buf);
982 if (retlen != mtd->oobblock) {
983 /* Insanity. Give up. */
984 printk(KERN_ERR "Read DiskOnChip Media Header once, but can't reread it???\n");
990 static inline int __init nftl_partscan(struct mtd_info *mtd,
991 struct mtd_partition *parts)
993 struct nand_chip *this = mtd->priv;
994 struct doc_priv *doc = (void *)this->priv;
997 struct NFTLMediaHeader *mh;
998 const unsigned psize = 1 << this->page_shift;
999 unsigned blocks, maxblocks;
1000 int offs, numheaders;
1002 buf = kmalloc(mtd->oobblock, GFP_KERNEL);
1004 printk(KERN_ERR "DiskOnChip mediaheader kmalloc failed!\n");
1007 if (!(numheaders=find_media_headers(mtd, buf, "ANAND", 1))) goto out;
1008 mh = (struct NFTLMediaHeader *) buf;
1010 //#ifdef CONFIG_MTD_DEBUG_VERBOSE
1011 // if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
1012 printk(KERN_INFO " DataOrgID = %s\n"
1013 " NumEraseUnits = %d\n"
1014 " FirstPhysicalEUN = %d\n"
1015 " FormattedSize = %d\n"
1016 " UnitSizeFactor = %d\n",
1017 mh->DataOrgID, mh->NumEraseUnits,
1018 mh->FirstPhysicalEUN, mh->FormattedSize,
1019 mh->UnitSizeFactor);
1022 blocks = mtd->size >> this->phys_erase_shift;
1023 maxblocks = min(32768U, mtd->erasesize - psize);
1025 if (mh->UnitSizeFactor == 0x00) {
1026 /* Auto-determine UnitSizeFactor. The constraints are:
1027 - There can be at most 32768 virtual blocks.
1028 - There can be at most (virtual block size - page size)
1029 virtual blocks (because MediaHeader+BBT must fit in 1).
1031 mh->UnitSizeFactor = 0xff;
1032 while (blocks > maxblocks) {
1034 maxblocks = min(32768U, (maxblocks << 1) + psize);
1035 mh->UnitSizeFactor--;
1037 printk(KERN_WARNING "UnitSizeFactor=0x00 detected. Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor);
1040 /* NOTE: The lines below modify internal variables of the NAND and MTD
1041 layers; variables with have already been configured by nand_scan.
1042 Unfortunately, we didn't know before this point what these values
1043 should be. Thus, this code is somewhat dependant on the exact
1044 implementation of the NAND layer. */
1045 if (mh->UnitSizeFactor != 0xff) {
1046 this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
1047 mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
1048 printk(KERN_INFO "Setting virtual erase size to %d\n", mtd->erasesize);
1049 blocks = mtd->size >> this->bbt_erase_shift;
1050 maxblocks = min(32768U, mtd->erasesize - psize);
1053 if (blocks > maxblocks) {
1054 printk(KERN_ERR "UnitSizeFactor of 0x%02x is inconsistent with device size. Aborting.\n", mh->UnitSizeFactor);
1058 /* Skip past the media headers. */
1059 offs = max(doc->mh0_page, doc->mh1_page);
1060 offs <<= this->page_shift;
1061 offs += mtd->erasesize;
1063 //parts[0].name = " DiskOnChip Boot / Media Header partition";
1064 //parts[0].offset = 0;
1065 //parts[0].size = offs;
1067 parts[0].name = " DiskOnChip BDTL partition";
1068 parts[0].offset = offs;
1069 parts[0].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift;
1071 offs += parts[0].size;
1072 if (offs < mtd->size) {
1073 parts[1].name = " DiskOnChip Remainder partition";
1074 parts[1].offset = offs;
1075 parts[1].size = mtd->size - offs;
1085 /* This is a stripped-down copy of the code in inftlmount.c */
1086 static inline int __init inftl_partscan(struct mtd_info *mtd,
1087 struct mtd_partition *parts)
1089 struct nand_chip *this = mtd->priv;
1090 struct doc_priv *doc = (void *)this->priv;
1093 struct INFTLMediaHeader *mh;
1094 struct INFTLPartition *ip;
1097 int vshift, lastvunit = 0;
1099 int end = mtd->size;
1101 if (inftl_bbt_write)
1102 end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift);
1104 buf = kmalloc(mtd->oobblock, GFP_KERNEL);
1106 printk(KERN_ERR "DiskOnChip mediaheader kmalloc failed!\n");
1110 if (!find_media_headers(mtd, buf, "BNAND", 0)) goto out;
1111 doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift);
1112 mh = (struct INFTLMediaHeader *) buf;
1114 mh->NoOfBootImageBlocks = le32_to_cpu(mh->NoOfBootImageBlocks);
1115 mh->NoOfBinaryPartitions = le32_to_cpu(mh->NoOfBinaryPartitions);
1116 mh->NoOfBDTLPartitions = le32_to_cpu(mh->NoOfBDTLPartitions);
1117 mh->BlockMultiplierBits = le32_to_cpu(mh->BlockMultiplierBits);
1118 mh->FormatFlags = le32_to_cpu(mh->FormatFlags);
1119 mh->PercentUsed = le32_to_cpu(mh->PercentUsed);
1121 //#ifdef CONFIG_MTD_DEBUG_VERBOSE
1122 // if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
1123 printk(KERN_INFO " bootRecordID = %s\n"
1124 " NoOfBootImageBlocks = %d\n"
1125 " NoOfBinaryPartitions = %d\n"
1126 " NoOfBDTLPartitions = %d\n"
1127 " BlockMultiplerBits = %d\n"
1128 " FormatFlgs = %d\n"
1129 " OsakVersion = %d.%d.%d.%d\n"
1130 " PercentUsed = %d\n",
1131 mh->bootRecordID, mh->NoOfBootImageBlocks,
1132 mh->NoOfBinaryPartitions,
1133 mh->NoOfBDTLPartitions,
1134 mh->BlockMultiplierBits, mh->FormatFlags,
1135 ((unsigned char *) &mh->OsakVersion)[0] & 0xf,
1136 ((unsigned char *) &mh->OsakVersion)[1] & 0xf,
1137 ((unsigned char *) &mh->OsakVersion)[2] & 0xf,
1138 ((unsigned char *) &mh->OsakVersion)[3] & 0xf,
1142 vshift = this->phys_erase_shift + mh->BlockMultiplierBits;
1144 blocks = mtd->size >> vshift;
1145 if (blocks > 32768) {
1146 printk(KERN_ERR "BlockMultiplierBits=%d is inconsistent with device size. Aborting.\n", mh->BlockMultiplierBits);
1150 blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift);
1151 if (inftl_bbt_write && (blocks > mtd->erasesize)) {
1152 printk(KERN_ERR "Writeable BBTs spanning more than one erase block are not yet supported. FIX ME!\n");
1156 /* Scan the partitions */
1157 for (i = 0; (i < 4); i++) {
1158 ip = &(mh->Partitions[i]);
1159 ip->virtualUnits = le32_to_cpu(ip->virtualUnits);
1160 ip->firstUnit = le32_to_cpu(ip->firstUnit);
1161 ip->lastUnit = le32_to_cpu(ip->lastUnit);
1162 ip->flags = le32_to_cpu(ip->flags);
1163 ip->spareUnits = le32_to_cpu(ip->spareUnits);
1164 ip->Reserved0 = le32_to_cpu(ip->Reserved0);
1166 //#ifdef CONFIG_MTD_DEBUG_VERBOSE
1167 // if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
1168 printk(KERN_INFO " PARTITION[%d] ->\n"
1169 " virtualUnits = %d\n"
1173 " spareUnits = %d\n",
1174 i, ip->virtualUnits, ip->firstUnit,
1175 ip->lastUnit, ip->flags,
1180 if ((i == 0) && (ip->firstUnit > 0)) {
1181 parts[0].name = " DiskOnChip IPL / Media Header partition";
1182 parts[0].offset = 0;
1183 parts[0].size = mtd->erasesize * ip->firstUnit;
1188 if (ip->flags & INFTL_BINARY)
1189 parts[numparts].name = " DiskOnChip BDK partition";
1191 parts[numparts].name = " DiskOnChip BDTL partition";
1192 parts[numparts].offset = ip->firstUnit << vshift;
1193 parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift;
1195 if (ip->lastUnit > lastvunit) lastvunit = ip->lastUnit;
1196 if (ip->flags & INFTL_LAST) break;
1199 if ((lastvunit << vshift) < end) {
1200 parts[numparts].name = " DiskOnChip Remainder partition";
1201 parts[numparts].offset = lastvunit << vshift;
1202 parts[numparts].size = end - parts[numparts].offset;
1211 static int __init nftl_scan_bbt(struct mtd_info *mtd)
1214 struct nand_chip *this = mtd->priv;
1215 struct doc_priv *doc = (void *)this->priv;
1216 struct mtd_partition parts[2];
1218 memset((char *) parts, 0, sizeof(parts));
1219 /* On NFTL, we have to find the media headers before we can read the
1220 BBTs, since they're stored in the media header eraseblocks. */
1221 numparts = nftl_partscan(mtd, parts);
1222 if (!numparts) return -EIO;
1223 this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1224 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1226 this->bbt_td->veroffs = 7;
1227 this->bbt_td->pages[0] = doc->mh0_page + 1;
1228 if (doc->mh1_page != -1) {
1229 this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1230 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1232 this->bbt_md->veroffs = 7;
1233 this->bbt_md->pages[0] = doc->mh1_page + 1;
1235 this->bbt_md = NULL;
1238 /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
1239 At least as nand_bbt.c is currently written. */
1240 if ((ret = nand_scan_bbt(mtd, NULL)))
1242 add_mtd_device(mtd);
1243 #ifdef CONFIG_MTD_PARTITIONS
1245 add_mtd_partitions(mtd, parts, numparts);
1250 static int __init inftl_scan_bbt(struct mtd_info *mtd)
1253 struct nand_chip *this = mtd->priv;
1254 struct doc_priv *doc = (void *)this->priv;
1255 struct mtd_partition parts[5];
1257 if (this->numchips > doc->chips_per_floor) {
1258 printk(KERN_ERR "Multi-floor INFTL devices not yet supported.\n");
1262 if (DoC_is_MillenniumPlus(doc)) {
1263 this->bbt_td->options = NAND_BBT_2BIT | NAND_BBT_ABSPAGE;
1264 if (inftl_bbt_write)
1265 this->bbt_td->options |= NAND_BBT_WRITE;
1266 this->bbt_td->pages[0] = 2;
1267 this->bbt_md = NULL;
1269 this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT |
1271 if (inftl_bbt_write)
1272 this->bbt_td->options |= NAND_BBT_WRITE;
1273 this->bbt_td->offs = 8;
1274 this->bbt_td->len = 8;
1275 this->bbt_td->veroffs = 7;
1276 this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1277 this->bbt_td->reserved_block_code = 0x01;
1278 this->bbt_td->pattern = "MSYS_BBT";
1280 this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT |
1282 if (inftl_bbt_write)
1283 this->bbt_md->options |= NAND_BBT_WRITE;
1284 this->bbt_md->offs = 8;
1285 this->bbt_md->len = 8;
1286 this->bbt_md->veroffs = 7;
1287 this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1288 this->bbt_md->reserved_block_code = 0x01;
1289 this->bbt_md->pattern = "TBB_SYSM";
1292 /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
1293 At least as nand_bbt.c is currently written. */
1294 if ((ret = nand_scan_bbt(mtd, NULL)))
1296 memset((char *) parts, 0, sizeof(parts));
1297 numparts = inftl_partscan(mtd, parts);
1298 /* At least for now, require the INFTL Media Header. We could probably
1299 do without it for non-INFTL use, since all it gives us is
1300 autopartitioning, but I want to give it more thought. */
1301 if (!numparts) return -EIO;
1302 add_mtd_device(mtd);
1303 #ifdef CONFIG_MTD_PARTITIONS
1305 add_mtd_partitions(mtd, parts, numparts);
1310 static inline int __init doc2000_init(struct mtd_info *mtd)
1312 struct nand_chip *this = mtd->priv;
1313 struct doc_priv *doc = (void *)this->priv;
1315 this->write_byte = doc2000_write_byte;
1316 this->read_byte = doc2000_read_byte;
1317 this->write_buf = doc2000_writebuf;
1318 this->read_buf = doc2000_readbuf;
1319 this->verify_buf = doc2000_verifybuf;
1320 this->scan_bbt = nftl_scan_bbt;
1322 doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
1323 doc2000_count_chips(mtd);
1324 mtd->name = "DiskOnChip 2000 (NFTL Model)";
1325 return (4 * doc->chips_per_floor);
1328 static inline int __init doc2001_init(struct mtd_info *mtd)
1330 struct nand_chip *this = mtd->priv;
1331 struct doc_priv *doc = (void *)this->priv;
1333 this->write_byte = doc2001_write_byte;
1334 this->read_byte = doc2001_read_byte;
1335 this->write_buf = doc2001_writebuf;
1336 this->read_buf = doc2001_readbuf;
1337 this->verify_buf = doc2001_verifybuf;
1339 ReadDOC(doc->virtadr, ChipID);
1340 ReadDOC(doc->virtadr, ChipID);
1341 ReadDOC(doc->virtadr, ChipID);
1342 if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) {
1343 /* It's not a Millennium; it's one of the newer
1344 DiskOnChip 2000 units with a similar ASIC.
1345 Treat it like a Millennium, except that it
1346 can have multiple chips. */
1347 doc2000_count_chips(mtd);
1348 mtd->name = "DiskOnChip 2000 (INFTL Model)";
1349 this->scan_bbt = inftl_scan_bbt;
1350 return (4 * doc->chips_per_floor);
1352 /* Bog-standard Millennium */
1353 doc->chips_per_floor = 1;
1354 mtd->name = "DiskOnChip Millennium";
1355 this->scan_bbt = nftl_scan_bbt;
1360 static inline int __init doc2001plus_init(struct mtd_info *mtd)
1362 struct nand_chip *this = mtd->priv;
1363 struct doc_priv *doc = (void *)this->priv;
1365 this->write_byte = NULL;
1366 this->read_byte = doc2001plus_read_byte;
1367 this->write_buf = doc2001plus_writebuf;
1368 this->read_buf = doc2001plus_readbuf;
1369 this->verify_buf = doc2001plus_verifybuf;
1370 this->scan_bbt = inftl_scan_bbt;
1371 this->hwcontrol = NULL;
1372 this->select_chip = doc2001plus_select_chip;
1373 this->cmdfunc = doc2001plus_command;
1374 this->enable_hwecc = doc2001plus_enable_hwecc;
1376 doc->chips_per_floor = 1;
1377 mtd->name = "DiskOnChip Millennium Plus";
1382 static inline int __init doc_probe(unsigned long physadr)
1384 unsigned char ChipID;
1385 struct mtd_info *mtd;
1386 struct nand_chip *nand;
1387 struct doc_priv *doc;
1388 unsigned long virtadr;
1389 unsigned char save_control;
1390 unsigned char tmp, tmpb, tmpc;
1391 int reg, len, numchips;
1394 virtadr = (unsigned long)ioremap(physadr, DOC_IOREMAP_LEN);
1396 printk(KERN_ERR "Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n", DOC_IOREMAP_LEN, physadr);
1400 /* It's not possible to cleanly detect the DiskOnChip - the
1401 * bootup procedure will put the device into reset mode, and
1402 * it's not possible to talk to it without actually writing
1403 * to the DOCControl register. So we store the current contents
1404 * of the DOCControl register's location, in case we later decide
1405 * that it's not a DiskOnChip, and want to put it back how we
1408 save_control = ReadDOC(virtadr, DOCControl);
1410 /* Reset the DiskOnChip ASIC */
1411 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET,
1412 virtadr, DOCControl);
1413 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET,
1414 virtadr, DOCControl);
1416 /* Enable the DiskOnChip ASIC */
1417 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL,
1418 virtadr, DOCControl);
1419 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL,
1420 virtadr, DOCControl);
1422 ChipID = ReadDOC(virtadr, ChipID);
1425 case DOC_ChipID_Doc2k:
1426 reg = DoC_2k_ECCStatus;
1428 case DOC_ChipID_DocMil:
1431 case DOC_ChipID_DocMilPlus16:
1432 case DOC_ChipID_DocMilPlus32:
1434 /* Possible Millennium Plus, need to do more checks */
1435 /* Possibly release from power down mode */
1436 for (tmp = 0; (tmp < 4); tmp++)
1437 ReadDOC(virtadr, Mplus_Power);
1439 /* Reset the Millennium Plus ASIC */
1440 tmp = DOC_MODE_RESET | DOC_MODE_MDWREN | DOC_MODE_RST_LAT |
1442 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1443 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1446 /* Enable the Millennium Plus ASIC */
1447 tmp = DOC_MODE_NORMAL | DOC_MODE_MDWREN | DOC_MODE_RST_LAT |
1449 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1450 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1453 ChipID = ReadDOC(virtadr, ChipID);
1456 case DOC_ChipID_DocMilPlus16:
1457 reg = DoC_Mplus_Toggle;
1459 case DOC_ChipID_DocMilPlus32:
1460 printk(KERN_ERR "DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
1471 /* Check the TOGGLE bit in the ECC register */
1472 tmp = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1473 tmpb = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1474 tmpc = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1475 if ((tmp == tmpb) || (tmp != tmpc)) {
1476 printk(KERN_WARNING "Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr);
1481 for (mtd = doclist; mtd; mtd = doc->nextdoc) {
1482 unsigned char oldval;
1483 unsigned char newval;
1485 doc = (void *)nand->priv;
1486 /* Use the alias resolution register to determine if this is
1487 in fact the same DOC aliased to a new address. If writes
1488 to one chip's alias resolution register change the value on
1489 the other chip, they're the same chip. */
1490 if (ChipID == DOC_ChipID_DocMilPlus16) {
1491 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1492 newval = ReadDOC(virtadr, Mplus_AliasResolution);
1494 oldval = ReadDOC(doc->virtadr, AliasResolution);
1495 newval = ReadDOC(virtadr, AliasResolution);
1497 if (oldval != newval)
1499 if (ChipID == DOC_ChipID_DocMilPlus16) {
1500 WriteDOC(~newval, virtadr, Mplus_AliasResolution);
1501 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1502 WriteDOC(newval, virtadr, Mplus_AliasResolution); // restore it
1504 WriteDOC(~newval, virtadr, AliasResolution);
1505 oldval = ReadDOC(doc->virtadr, AliasResolution);
1506 WriteDOC(newval, virtadr, AliasResolution); // restore it
1509 if (oldval == newval) {
1510 printk(KERN_DEBUG "Found alias of DOC at 0x%lx to 0x%lx\n", doc->physadr, physadr);
1515 printk(KERN_NOTICE "DiskOnChip found at 0x%lx\n", physadr);
1517 len = sizeof(struct mtd_info) +
1518 sizeof(struct nand_chip) +
1519 sizeof(struct doc_priv) +
1520 (2 * sizeof(struct nand_bbt_descr));
1521 mtd = kmalloc(len, GFP_KERNEL);
1523 printk(KERN_ERR "DiskOnChip kmalloc (%d bytes) failed!\n", len);
1527 memset(mtd, 0, len);
1529 nand = (struct nand_chip *) (mtd + 1);
1530 doc = (struct doc_priv *) (nand + 1);
1531 nand->bbt_td = (struct nand_bbt_descr *) (doc + 1);
1532 nand->bbt_md = nand->bbt_td + 1;
1534 mtd->priv = (void *) nand;
1535 mtd->owner = THIS_MODULE;
1537 nand->priv = (void *) doc;
1538 nand->select_chip = doc200x_select_chip;
1539 nand->hwcontrol = doc200x_hwcontrol;
1540 nand->dev_ready = doc200x_dev_ready;
1541 nand->waitfunc = doc200x_wait;
1542 nand->block_bad = doc200x_block_bad;
1543 nand->enable_hwecc = doc200x_enable_hwecc;
1544 nand->calculate_ecc = doc200x_calculate_ecc;
1545 nand->correct_data = doc200x_correct_data;
1547 nand->autooob = &doc200x_oobinfo;
1548 nand->eccmode = NAND_ECC_HW6_512;
1549 nand->options = NAND_USE_FLASH_BBT | NAND_HWECC_SYNDROME;
1551 doc->physadr = physadr;
1552 doc->virtadr = virtadr;
1553 doc->ChipID = ChipID;
1558 doc->nextdoc = doclist;
1560 if (ChipID == DOC_ChipID_Doc2k)
1561 numchips = doc2000_init(mtd);
1562 else if (ChipID == DOC_ChipID_DocMilPlus16)
1563 numchips = doc2001plus_init(mtd);
1565 numchips = doc2001_init(mtd);
1567 if ((ret = nand_scan(mtd, numchips))) {
1568 /* DBB note: i believe nand_release is necessary here, as
1569 buffers may have been allocated in nand_base. Check with
1571 /* nand_release will call del_mtd_device, but we haven't yet
1572 added it. This is handled without incident by
1573 del_mtd_device, as far as I can tell. */
1584 /* Put back the contents of the DOCControl register, in case it's not
1585 actually a DiskOnChip. */
1586 WriteDOC(save_control, virtadr, DOCControl);
1588 iounmap((void *)virtadr);
1592 int __init init_nanddoc(void)
1596 if (doc_config_location) {
1597 printk(KERN_INFO "Using configured DiskOnChip probe address 0x%lx\n", doc_config_location);
1598 return doc_probe(doc_config_location);
1600 for (i=0; (doc_locations[i] != 0xffffffff); i++) {
1601 doc_probe(doc_locations[i]);
1604 /* No banner message any more. Print a message if no DiskOnChip
1605 found, so the user knows we at least tried. */
1607 printk(KERN_INFO "No valid DiskOnChip devices found\n");
1613 void __exit cleanup_nanddoc(void)
1615 struct mtd_info *mtd, *nextmtd;
1616 struct nand_chip *nand;
1617 struct doc_priv *doc;
1619 for (mtd = doclist; mtd; mtd = nextmtd) {
1621 doc = (void *)nand->priv;
1623 nextmtd = doc->nextdoc;
1625 iounmap((void *)doc->virtadr);
1630 module_init(init_nanddoc);
1631 module_exit(cleanup_nanddoc);
1633 MODULE_LICENSE("GPL");
1634 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1635 MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver\n");