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.42 2004/11/16 18:29:03 dwmw2 Exp $
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/sched.h>
25 #include <linux/delay.h>
26 #include <linux/rslib.h>
29 #include <linux/mtd/mtd.h>
30 #include <linux/mtd/nand.h>
31 #include <linux/mtd/doc2000.h>
32 #include <linux/mtd/compatmac.h>
33 #include <linux/mtd/partitions.h>
34 #include <linux/mtd/inftl.h>
36 /* Where to look for the devices? */
37 #ifndef CONFIG_MTD_DISKONCHIP_PROBE_ADDRESS
38 #define CONFIG_MTD_DISKONCHIP_PROBE_ADDRESS 0
41 static unsigned long __initdata doc_locations[] = {
42 #if defined (__alpha__) || defined(__i386__) || defined(__x86_64__)
43 #ifdef CONFIG_MTD_DISKONCHIP_PROBE_HIGH
44 0xfffc8000, 0xfffca000, 0xfffcc000, 0xfffce000,
45 0xfffd0000, 0xfffd2000, 0xfffd4000, 0xfffd6000,
46 0xfffd8000, 0xfffda000, 0xfffdc000, 0xfffde000,
47 0xfffe0000, 0xfffe2000, 0xfffe4000, 0xfffe6000,
48 0xfffe8000, 0xfffea000, 0xfffec000, 0xfffee000,
49 #else /* CONFIG_MTD_DOCPROBE_HIGH */
50 0xc8000, 0xca000, 0xcc000, 0xce000,
51 0xd0000, 0xd2000, 0xd4000, 0xd6000,
52 0xd8000, 0xda000, 0xdc000, 0xde000,
53 0xe0000, 0xe2000, 0xe4000, 0xe6000,
54 0xe8000, 0xea000, 0xec000, 0xee000,
55 #endif /* CONFIG_MTD_DOCPROBE_HIGH */
56 #elif defined(__PPC__)
58 #elif defined(CONFIG_MOMENCO_OCELOT)
61 #elif defined(CONFIG_MOMENCO_OCELOT_G) || defined (CONFIG_MOMENCO_OCELOT_C)
64 #warning Unknown architecture for DiskOnChip. No default probe locations defined
68 static struct mtd_info *doclist = NULL;
71 void __iomem *virtadr;
72 unsigned long physadr;
75 int chips_per_floor; /* The number of chips detected on each floor */
80 struct mtd_info *nextdoc;
83 /* Max number of eraseblocks to scan (from start of device) for the (I)NFTL
84 MediaHeader. The spec says to just keep going, I think, but that's just
86 #define MAX_MEDIAHEADER_SCAN 8
88 /* This is the syndrome computed by the HW ecc generator upon reading an empty
89 page, one with all 0xff for data and stored ecc code. */
90 static u_char empty_read_syndrome[6] = { 0x26, 0xff, 0x6d, 0x47, 0x73, 0x7a };
91 /* This is the ecc value computed by the HW ecc generator upon writing an empty
92 page, one with all 0xff for data. */
93 static u_char empty_write_ecc[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
95 #define INFTL_BBT_RESERVED_BLOCKS 4
97 #define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
98 #define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
99 #define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
101 static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd);
102 static void doc200x_select_chip(struct mtd_info *mtd, int chip);
105 module_param(debug, int, 0);
107 static int try_dword=1;
108 module_param(try_dword, int, 0);
110 static int no_ecc_failures=0;
111 module_param(no_ecc_failures, int, 0);
113 #ifdef CONFIG_MTD_PARTITIONS
114 static int no_autopart=0;
115 module_param(no_autopart, int, 0);
118 #ifdef MTD_NAND_DISKONCHIP_BBTWRITE
119 static int inftl_bbt_write=1;
121 static int inftl_bbt_write=0;
123 module_param(inftl_bbt_write, int, 0);
125 static unsigned long doc_config_location = CONFIG_MTD_DISKONCHIP_PROBE_ADDRESS;
126 module_param(doc_config_location, ulong, 0);
127 MODULE_PARM_DESC(doc_config_location, "Physical memory address at which to probe for DiskOnChip");
130 /* Sector size for HW ECC */
131 #define SECTOR_SIZE 512
132 /* The sector bytes are packed into NB_DATA 10 bit words */
133 #define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / 10)
134 /* Number of roots */
136 /* First consective root */
138 /* Number of symbols */
141 /* the Reed Solomon control structure */
142 static struct rs_control *rs_decoder;
145 * The HW decoder in the DoC ASIC's provides us a error syndrome,
146 * which we must convert to a standard syndrom usable by the generic
147 * Reed-Solomon library code.
149 * Fabrice Bellard figured this out in the old docecc code. I added
150 * some comments, improved a minor bit and converted it to make use
151 * of the generic Reed-Solomon libary. tglx
153 static int doc_ecc_decode (struct rs_control *rs, uint8_t *data, uint8_t *ecc)
155 int i, j, nerr, errpos[8];
157 uint16_t ds[4], s[5], tmp, errval[8], syn[4];
159 /* Convert the ecc bytes into words */
160 ds[0] = ((ecc[4] & 0xff) >> 0) | ((ecc[5] & 0x03) << 8);
161 ds[1] = ((ecc[5] & 0xfc) >> 2) | ((ecc[2] & 0x0f) << 6);
162 ds[2] = ((ecc[2] & 0xf0) >> 4) | ((ecc[3] & 0x3f) << 4);
163 ds[3] = ((ecc[3] & 0xc0) >> 6) | ((ecc[0] & 0xff) << 2);
166 /* Initialize the syndrom buffer */
167 for (i = 0; i < NROOTS; i++)
171 * s[i] = ds[3]x^3 + ds[2]x^2 + ds[1]x^1 + ds[0]
172 * where x = alpha^(FCR + i)
174 for(j = 1; j < NROOTS; j++) {
177 tmp = rs->index_of[ds[j]];
178 for(i = 0; i < NROOTS; i++)
179 s[i] ^= rs->alpha_to[rs_modnn(rs, tmp + (FCR + i) * j)];
182 /* Calc s[i] = s[i] / alpha^(v + i) */
183 for (i = 0; i < NROOTS; i++) {
185 syn[i] = rs_modnn(rs, rs->index_of[s[i]] + (NN - FCR - i));
187 /* Call the decoder library */
188 nerr = decode_rs16(rs, NULL, NULL, 1019, syn, 0, errpos, 0, errval);
190 /* Incorrectable errors ? */
195 * Correct the errors. The bitpositions are a bit of magic,
196 * but they are given by the design of the de/encoder circuit
199 for(i = 0;i < nerr; i++) {
200 int index, bitpos, pos = 1015 - errpos[i];
202 if (pos >= NB_DATA && pos < 1019)
205 /* extract bit position (MSB first) */
206 pos = 10 * (NB_DATA - 1 - pos) - 6;
207 /* now correct the following 10 bits. At most two bytes
208 can be modified since pos is even */
209 index = (pos >> 3) ^ 1;
211 if ((index >= 0 && index < SECTOR_SIZE) ||
212 index == (SECTOR_SIZE + 1)) {
213 val = (uint8_t) (errval[i] >> (2 + bitpos));
215 if (index < SECTOR_SIZE)
218 index = ((pos >> 3) + 1) ^ 1;
219 bitpos = (bitpos + 10) & 7;
222 if ((index >= 0 && index < SECTOR_SIZE) ||
223 index == (SECTOR_SIZE + 1)) {
224 val = (uint8_t)(errval[i] << (8 - bitpos));
226 if (index < SECTOR_SIZE)
231 /* If the parity is wrong, no rescue possible */
232 return parity ? -1 : nerr;
235 static void DoC_Delay(struct doc_priv *doc, unsigned short cycles)
240 for (i = 0; i < cycles; i++) {
241 if (DoC_is_Millennium(doc))
242 dummy = ReadDOC(doc->virtadr, NOP);
243 else if (DoC_is_MillenniumPlus(doc))
244 dummy = ReadDOC(doc->virtadr, Mplus_NOP);
246 dummy = ReadDOC(doc->virtadr, DOCStatus);
251 #define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
253 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
254 static int _DoC_WaitReady(struct doc_priv *doc)
256 void __iomem *docptr = doc->virtadr;
257 unsigned long timeo = jiffies + (HZ * 10);
259 if(debug) printk("_DoC_WaitReady...\n");
260 /* Out-of-line routine to wait for chip response */
261 if (DoC_is_MillenniumPlus(doc)) {
262 while ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
263 if (time_after(jiffies, timeo)) {
264 printk("_DoC_WaitReady timed out.\n");
271 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
272 if (time_after(jiffies, timeo)) {
273 printk("_DoC_WaitReady timed out.\n");
284 static inline int DoC_WaitReady(struct doc_priv *doc)
286 void __iomem *docptr = doc->virtadr;
289 if (DoC_is_MillenniumPlus(doc)) {
292 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
293 /* Call the out-of-line routine to wait */
294 ret = _DoC_WaitReady(doc);
298 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
299 /* Call the out-of-line routine to wait */
300 ret = _DoC_WaitReady(doc);
304 if(debug) printk("DoC_WaitReady OK\n");
308 static void doc2000_write_byte(struct mtd_info *mtd, u_char datum)
310 struct nand_chip *this = mtd->priv;
311 struct doc_priv *doc = (void *)this->priv;
312 void __iomem *docptr = doc->virtadr;
314 if(debug)printk("write_byte %02x\n", datum);
315 WriteDOC(datum, docptr, CDSNSlowIO);
316 WriteDOC(datum, docptr, 2k_CDSN_IO);
319 static u_char doc2000_read_byte(struct mtd_info *mtd)
321 struct nand_chip *this = mtd->priv;
322 struct doc_priv *doc = (void *)this->priv;
323 void __iomem *docptr = doc->virtadr;
326 ReadDOC(docptr, CDSNSlowIO);
328 ret = ReadDOC(docptr, 2k_CDSN_IO);
329 if (debug) printk("read_byte returns %02x\n", ret);
333 static void doc2000_writebuf(struct mtd_info *mtd,
334 const u_char *buf, int len)
336 struct nand_chip *this = mtd->priv;
337 struct doc_priv *doc = (void *)this->priv;
338 void __iomem *docptr = doc->virtadr;
340 if (debug)printk("writebuf of %d bytes: ", len);
341 for (i=0; i < len; i++) {
342 WriteDOC_(buf[i], docptr, DoC_2k_CDSN_IO + i);
344 printk("%02x ", buf[i]);
346 if (debug) printk("\n");
349 static void doc2000_readbuf(struct mtd_info *mtd,
350 u_char *buf, int len)
352 struct nand_chip *this = mtd->priv;
353 struct doc_priv *doc = (void *)this->priv;
354 void __iomem *docptr = doc->virtadr;
357 if (debug)printk("readbuf of %d bytes: ", len);
359 for (i=0; i < len; i++) {
360 buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
364 static void doc2000_readbuf_dword(struct mtd_info *mtd,
365 u_char *buf, int len)
367 struct nand_chip *this = mtd->priv;
368 struct doc_priv *doc = (void *)this->priv;
369 void __iomem *docptr = doc->virtadr;
372 if (debug) printk("readbuf_dword of %d bytes: ", len);
374 if (unlikely((((unsigned long)buf)|len) & 3)) {
375 for (i=0; i < len; i++) {
376 *(uint8_t *)(&buf[i]) = ReadDOC(docptr, 2k_CDSN_IO + i);
379 for (i=0; i < len; i+=4) {
380 *(uint32_t*)(&buf[i]) = readl(docptr + DoC_2k_CDSN_IO + i);
385 static int doc2000_verifybuf(struct mtd_info *mtd,
386 const u_char *buf, int len)
388 struct nand_chip *this = mtd->priv;
389 struct doc_priv *doc = (void *)this->priv;
390 void __iomem *docptr = doc->virtadr;
393 for (i=0; i < len; i++)
394 if (buf[i] != ReadDOC(docptr, 2k_CDSN_IO))
399 static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr)
401 struct nand_chip *this = mtd->priv;
402 struct doc_priv *doc = (void *)this->priv;
405 doc200x_select_chip(mtd, nr);
406 doc200x_hwcontrol(mtd, NAND_CTL_SETCLE);
407 this->write_byte(mtd, NAND_CMD_READID);
408 doc200x_hwcontrol(mtd, NAND_CTL_CLRCLE);
409 doc200x_hwcontrol(mtd, NAND_CTL_SETALE);
410 this->write_byte(mtd, 0);
411 doc200x_hwcontrol(mtd, NAND_CTL_CLRALE);
413 ret = this->read_byte(mtd) << 8;
414 ret |= this->read_byte(mtd);
416 if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) {
417 /* First chip probe. See if we get same results by 32-bit access */
422 void __iomem *docptr = doc->virtadr;
424 doc200x_hwcontrol(mtd, NAND_CTL_SETCLE);
425 doc2000_write_byte(mtd, NAND_CMD_READID);
426 doc200x_hwcontrol(mtd, NAND_CTL_CLRCLE);
427 doc200x_hwcontrol(mtd, NAND_CTL_SETALE);
428 doc2000_write_byte(mtd, 0);
429 doc200x_hwcontrol(mtd, NAND_CTL_CLRALE);
431 ident.dword = readl(docptr + DoC_2k_CDSN_IO);
432 if (((ident.byte[0] << 8) | ident.byte[1]) == ret) {
433 printk(KERN_INFO "DiskOnChip 2000 responds to DWORD access\n");
434 this->read_buf = &doc2000_readbuf_dword;
441 static void __init doc2000_count_chips(struct mtd_info *mtd)
443 struct nand_chip *this = mtd->priv;
444 struct doc_priv *doc = (void *)this->priv;
448 /* Max 4 chips per floor on DiskOnChip 2000 */
449 doc->chips_per_floor = 4;
451 /* Find out what the first chip is */
452 mfrid = doc200x_ident_chip(mtd, 0);
454 /* Find how many chips in each floor. */
455 for (i = 1; i < 4; i++) {
456 if (doc200x_ident_chip(mtd, i) != mfrid)
459 doc->chips_per_floor = i;
460 printk(KERN_DEBUG "Detected %d chips per floor.\n", i);
463 static int doc200x_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
465 struct doc_priv *doc = (void *)this->priv;
470 this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
472 status = (int)this->read_byte(mtd);
477 static void doc2001_write_byte(struct mtd_info *mtd, u_char datum)
479 struct nand_chip *this = mtd->priv;
480 struct doc_priv *doc = (void *)this->priv;
481 void __iomem *docptr = doc->virtadr;
483 WriteDOC(datum, docptr, CDSNSlowIO);
484 WriteDOC(datum, docptr, Mil_CDSN_IO);
485 WriteDOC(datum, docptr, WritePipeTerm);
488 static u_char doc2001_read_byte(struct mtd_info *mtd)
490 struct nand_chip *this = mtd->priv;
491 struct doc_priv *doc = (void *)this->priv;
492 void __iomem *docptr = doc->virtadr;
494 //ReadDOC(docptr, CDSNSlowIO);
495 /* 11.4.5 -- delay twice to allow extended length cycle */
497 ReadDOC(docptr, ReadPipeInit);
498 //return ReadDOC(docptr, Mil_CDSN_IO);
499 return ReadDOC(docptr, LastDataRead);
502 static void doc2001_writebuf(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 void __iomem *docptr = doc->virtadr;
510 for (i=0; i < len; i++)
511 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
512 /* Terminate write pipeline */
513 WriteDOC(0x00, docptr, WritePipeTerm);
516 static void doc2001_readbuf(struct mtd_info *mtd,
517 u_char *buf, int len)
519 struct nand_chip *this = mtd->priv;
520 struct doc_priv *doc = (void *)this->priv;
521 void __iomem *docptr = doc->virtadr;
524 /* Start read pipeline */
525 ReadDOC(docptr, ReadPipeInit);
527 for (i=0; i < len-1; i++)
528 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
530 /* Terminate read pipeline */
531 buf[i] = ReadDOC(docptr, LastDataRead);
534 static int doc2001_verifybuf(struct mtd_info *mtd,
535 const u_char *buf, int len)
537 struct nand_chip *this = mtd->priv;
538 struct doc_priv *doc = (void *)this->priv;
539 void __iomem *docptr = doc->virtadr;
542 /* Start read pipeline */
543 ReadDOC(docptr, ReadPipeInit);
545 for (i=0; i < len-1; i++)
546 if (buf[i] != ReadDOC(docptr, Mil_CDSN_IO)) {
547 ReadDOC(docptr, LastDataRead);
550 if (buf[i] != ReadDOC(docptr, LastDataRead))
555 static u_char doc2001plus_read_byte(struct mtd_info *mtd)
557 struct nand_chip *this = mtd->priv;
558 struct doc_priv *doc = (void *)this->priv;
559 void __iomem *docptr = doc->virtadr;
562 ReadDOC(docptr, Mplus_ReadPipeInit);
563 ReadDOC(docptr, Mplus_ReadPipeInit);
564 ret = ReadDOC(docptr, Mplus_LastDataRead);
565 if (debug) printk("read_byte returns %02x\n", ret);
569 static void doc2001plus_writebuf(struct mtd_info *mtd,
570 const u_char *buf, int len)
572 struct nand_chip *this = mtd->priv;
573 struct doc_priv *doc = (void *)this->priv;
574 void __iomem *docptr = doc->virtadr;
577 if (debug)printk("writebuf of %d bytes: ", len);
578 for (i=0; i < len; i++) {
579 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
581 printk("%02x ", buf[i]);
583 if (debug) printk("\n");
586 static void doc2001plus_readbuf(struct mtd_info *mtd,
587 u_char *buf, int len)
589 struct nand_chip *this = mtd->priv;
590 struct doc_priv *doc = (void *)this->priv;
591 void __iomem *docptr = doc->virtadr;
594 if (debug)printk("readbuf of %d bytes: ", len);
596 /* Start read pipeline */
597 ReadDOC(docptr, Mplus_ReadPipeInit);
598 ReadDOC(docptr, Mplus_ReadPipeInit);
600 for (i=0; i < len-2; i++) {
601 buf[i] = ReadDOC(docptr, Mil_CDSN_IO);
603 printk("%02x ", buf[i]);
606 /* Terminate read pipeline */
607 buf[len-2] = ReadDOC(docptr, Mplus_LastDataRead);
609 printk("%02x ", buf[len-2]);
610 buf[len-1] = ReadDOC(docptr, Mplus_LastDataRead);
612 printk("%02x ", buf[len-1]);
613 if (debug) printk("\n");
616 static int doc2001plus_verifybuf(struct mtd_info *mtd,
617 const u_char *buf, int len)
619 struct nand_chip *this = mtd->priv;
620 struct doc_priv *doc = (void *)this->priv;
621 void __iomem *docptr = doc->virtadr;
624 if (debug)printk("verifybuf of %d bytes: ", len);
626 /* Start read pipeline */
627 ReadDOC(docptr, Mplus_ReadPipeInit);
628 ReadDOC(docptr, Mplus_ReadPipeInit);
630 for (i=0; i < len-2; i++)
631 if (buf[i] != ReadDOC(docptr, Mil_CDSN_IO)) {
632 ReadDOC(docptr, Mplus_LastDataRead);
633 ReadDOC(docptr, Mplus_LastDataRead);
636 if (buf[len-2] != ReadDOC(docptr, Mplus_LastDataRead))
638 if (buf[len-1] != ReadDOC(docptr, Mplus_LastDataRead))
643 static void doc2001plus_select_chip(struct mtd_info *mtd, int chip)
645 struct nand_chip *this = mtd->priv;
646 struct doc_priv *doc = (void *)this->priv;
647 void __iomem *docptr = doc->virtadr;
650 if(debug)printk("select chip (%d)\n", chip);
653 /* Disable flash internally */
654 WriteDOC(0, docptr, Mplus_FlashSelect);
658 floor = chip / doc->chips_per_floor;
659 chip -= (floor * doc->chips_per_floor);
661 /* Assert ChipEnable and deassert WriteProtect */
662 WriteDOC((DOC_FLASH_CE), docptr, Mplus_FlashSelect);
663 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
666 doc->curfloor = floor;
669 static void doc200x_select_chip(struct mtd_info *mtd, int chip)
671 struct nand_chip *this = mtd->priv;
672 struct doc_priv *doc = (void *)this->priv;
673 void __iomem *docptr = doc->virtadr;
676 if(debug)printk("select chip (%d)\n", chip);
681 floor = chip / doc->chips_per_floor;
682 chip -= (floor * doc->chips_per_floor);
684 /* 11.4.4 -- deassert CE before changing chip */
685 doc200x_hwcontrol(mtd, NAND_CTL_CLRNCE);
687 WriteDOC(floor, docptr, FloorSelect);
688 WriteDOC(chip, docptr, CDSNDeviceSelect);
690 doc200x_hwcontrol(mtd, NAND_CTL_SETNCE);
693 doc->curfloor = floor;
696 static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd)
698 struct nand_chip *this = mtd->priv;
699 struct doc_priv *doc = (void *)this->priv;
700 void __iomem *docptr = doc->virtadr;
703 case NAND_CTL_SETNCE:
704 doc->CDSNControl |= CDSN_CTRL_CE;
706 case NAND_CTL_CLRNCE:
707 doc->CDSNControl &= ~CDSN_CTRL_CE;
709 case NAND_CTL_SETCLE:
710 doc->CDSNControl |= CDSN_CTRL_CLE;
712 case NAND_CTL_CLRCLE:
713 doc->CDSNControl &= ~CDSN_CTRL_CLE;
715 case NAND_CTL_SETALE:
716 doc->CDSNControl |= CDSN_CTRL_ALE;
718 case NAND_CTL_CLRALE:
719 doc->CDSNControl &= ~CDSN_CTRL_ALE;
722 doc->CDSNControl |= CDSN_CTRL_WP;
725 doc->CDSNControl &= ~CDSN_CTRL_WP;
728 if (debug)printk("hwcontrol(%d): %02x\n", cmd, doc->CDSNControl);
729 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
730 /* 11.4.3 -- 4 NOPs after CSDNControl write */
734 static void doc2001plus_command (struct mtd_info *mtd, unsigned command, int column, int page_addr)
736 struct nand_chip *this = mtd->priv;
737 struct doc_priv *doc = (void *)this->priv;
738 void __iomem *docptr = doc->virtadr;
741 * Must terminate write pipeline before sending any commands
744 if (command == NAND_CMD_PAGEPROG) {
745 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
746 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
750 * Write out the command to the device.
752 if (command == NAND_CMD_SEQIN) {
755 if (column >= mtd->oobblock) {
757 column -= mtd->oobblock;
758 readcmd = NAND_CMD_READOOB;
759 } else if (column < 256) {
760 /* First 256 bytes --> READ0 */
761 readcmd = NAND_CMD_READ0;
764 readcmd = NAND_CMD_READ1;
766 WriteDOC(readcmd, docptr, Mplus_FlashCmd);
768 WriteDOC(command, docptr, Mplus_FlashCmd);
769 WriteDOC(0, docptr, Mplus_WritePipeTerm);
770 WriteDOC(0, docptr, Mplus_WritePipeTerm);
772 if (column != -1 || page_addr != -1) {
773 /* Serially input address */
775 /* Adjust columns for 16 bit buswidth */
776 if (this->options & NAND_BUSWIDTH_16)
778 WriteDOC(column, docptr, Mplus_FlashAddress);
780 if (page_addr != -1) {
781 WriteDOC((unsigned char) (page_addr & 0xff), docptr, Mplus_FlashAddress);
782 WriteDOC((unsigned char) ((page_addr >> 8) & 0xff), docptr, Mplus_FlashAddress);
783 /* One more address cycle for higher density devices */
784 if (this->chipsize & 0x0c000000) {
785 WriteDOC((unsigned char) ((page_addr >> 16) & 0x0f), docptr, Mplus_FlashAddress);
786 printk("high density\n");
789 WriteDOC(0, docptr, Mplus_WritePipeTerm);
790 WriteDOC(0, docptr, Mplus_WritePipeTerm);
792 if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 || command == NAND_CMD_READOOB || command == NAND_CMD_READID)
793 WriteDOC(0, docptr, Mplus_FlashControl);
797 * program and erase have their own busy handlers
798 * status and sequential in needs no delay
802 case NAND_CMD_PAGEPROG:
803 case NAND_CMD_ERASE1:
804 case NAND_CMD_ERASE2:
806 case NAND_CMD_STATUS:
812 udelay(this->chip_delay);
813 WriteDOC(NAND_CMD_STATUS, docptr, Mplus_FlashCmd);
814 WriteDOC(0, docptr, Mplus_WritePipeTerm);
815 WriteDOC(0, docptr, Mplus_WritePipeTerm);
816 while ( !(this->read_byte(mtd) & 0x40));
819 /* This applies to read commands */
822 * If we don't have access to the busy pin, we apply the given
825 if (!this->dev_ready) {
826 udelay (this->chip_delay);
831 /* Apply this short delay always to ensure that we do wait tWB in
832 * any case on any machine. */
834 /* wait until command is processed */
835 while (!this->dev_ready(mtd));
838 static int doc200x_dev_ready(struct mtd_info *mtd)
840 struct nand_chip *this = mtd->priv;
841 struct doc_priv *doc = (void *)this->priv;
842 void __iomem *docptr = doc->virtadr;
844 if (DoC_is_MillenniumPlus(doc)) {
845 /* 11.4.2 -- must NOP four times before checking FR/B# */
847 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
849 printk("not ready\n");
852 if (debug)printk("was ready\n");
855 /* 11.4.2 -- must NOP four times before checking FR/B# */
857 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
859 printk("not ready\n");
862 /* 11.4.2 -- Must NOP twice if it's ready */
864 if (debug)printk("was ready\n");
869 static int doc200x_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
871 /* This is our last resort if we couldn't find or create a BBT. Just
872 pretend all blocks are good. */
876 static void doc200x_enable_hwecc(struct mtd_info *mtd, int mode)
878 struct nand_chip *this = mtd->priv;
879 struct doc_priv *doc = (void *)this->priv;
880 void __iomem *docptr = doc->virtadr;
882 /* Prime the ECC engine */
885 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
886 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
889 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
890 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
895 static void doc2001plus_enable_hwecc(struct mtd_info *mtd, int mode)
897 struct nand_chip *this = mtd->priv;
898 struct doc_priv *doc = (void *)this->priv;
899 void __iomem *docptr = doc->virtadr;
901 /* Prime the ECC engine */
904 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
905 WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
908 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
909 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
914 /* This code is only called on write */
915 static int doc200x_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
916 unsigned char *ecc_code)
918 struct nand_chip *this = mtd->priv;
919 struct doc_priv *doc = (void *)this->priv;
920 void __iomem *docptr = doc->virtadr;
924 /* flush the pipeline */
925 if (DoC_is_2000(doc)) {
926 WriteDOC(doc->CDSNControl & ~CDSN_CTRL_FLASH_IO, docptr, CDSNControl);
927 WriteDOC(0, docptr, 2k_CDSN_IO);
928 WriteDOC(0, docptr, 2k_CDSN_IO);
929 WriteDOC(0, docptr, 2k_CDSN_IO);
930 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
931 } else if (DoC_is_MillenniumPlus(doc)) {
932 WriteDOC(0, docptr, Mplus_NOP);
933 WriteDOC(0, docptr, Mplus_NOP);
934 WriteDOC(0, docptr, Mplus_NOP);
936 WriteDOC(0, docptr, NOP);
937 WriteDOC(0, docptr, NOP);
938 WriteDOC(0, docptr, NOP);
941 for (i = 0; i < 6; i++) {
942 if (DoC_is_MillenniumPlus(doc))
943 ecc_code[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
945 ecc_code[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
946 if (ecc_code[i] != empty_write_ecc[i])
949 if (DoC_is_MillenniumPlus(doc))
950 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
952 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
954 /* If emptymatch=1, we might have an all-0xff data buffer. Check. */
956 /* Note: this somewhat expensive test should not be triggered
957 often. It could be optimized away by examining the data in
958 the writebuf routine, and remembering the result. */
959 for (i = 0; i < 512; i++) {
960 if (dat[i] == 0xff) continue;
965 /* If emptymatch still =1, we do have an all-0xff data buffer.
966 Return all-0xff ecc value instead of the computed one, so
967 it'll look just like a freshly-erased page. */
968 if (emptymatch) memset(ecc_code, 0xff, 6);
973 static int doc200x_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc)
976 struct nand_chip *this = mtd->priv;
977 struct doc_priv *doc = (void *)this->priv;
978 void __iomem *docptr = doc->virtadr;
979 volatile u_char dummy;
982 /* flush the pipeline */
983 if (DoC_is_2000(doc)) {
984 dummy = ReadDOC(docptr, 2k_ECCStatus);
985 dummy = ReadDOC(docptr, 2k_ECCStatus);
986 dummy = ReadDOC(docptr, 2k_ECCStatus);
987 } else if (DoC_is_MillenniumPlus(doc)) {
988 dummy = ReadDOC(docptr, Mplus_ECCConf);
989 dummy = ReadDOC(docptr, Mplus_ECCConf);
990 dummy = ReadDOC(docptr, Mplus_ECCConf);
992 dummy = ReadDOC(docptr, ECCConf);
993 dummy = ReadDOC(docptr, ECCConf);
994 dummy = ReadDOC(docptr, ECCConf);
997 /* Error occured ? */
999 for (i = 0; i < 6; i++) {
1000 if (DoC_is_MillenniumPlus(doc))
1001 calc_ecc[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
1003 calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
1004 if (calc_ecc[i] != empty_read_syndrome[i])
1007 /* If emptymatch=1, the read syndrome is consistent with an
1008 all-0xff data and stored ecc block. Check the stored ecc. */
1010 for (i = 0; i < 6; i++) {
1011 if (read_ecc[i] == 0xff) continue;
1016 /* If emptymatch still =1, check the data block. */
1018 /* Note: this somewhat expensive test should not be triggered
1019 often. It could be optimized away by examining the data in
1020 the readbuf routine, and remembering the result. */
1021 for (i = 0; i < 512; i++) {
1022 if (dat[i] == 0xff) continue;
1027 /* If emptymatch still =1, this is almost certainly a freshly-
1028 erased block, in which case the ECC will not come out right.
1029 We'll suppress the error and tell the caller everything's
1030 OK. Because it is. */
1031 if (!emptymatch) ret = doc_ecc_decode (rs_decoder, dat, calc_ecc);
1033 printk(KERN_ERR "doc200x_correct_data corrected %d errors\n", ret);
1035 if (DoC_is_MillenniumPlus(doc))
1036 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
1038 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
1039 if (no_ecc_failures && (ret == -1)) {
1040 printk(KERN_ERR "suppressing ECC failure\n");
1046 //u_char mydatabuf[528];
1048 static struct nand_oobinfo doc200x_oobinfo = {
1049 .useecc = MTD_NANDECC_AUTOPLACE,
1051 .eccpos = {0, 1, 2, 3, 4, 5},
1052 .oobfree = { {8, 8} }
1055 /* Find the (I)NFTL Media Header, and optionally also the mirror media header.
1056 On sucessful return, buf will contain a copy of the media header for
1057 further processing. id is the string to scan for, and will presumably be
1058 either "ANAND" or "BNAND". If findmirror=1, also look for the mirror media
1059 header. The page #s of the found media headers are placed in mh0_page and
1060 mh1_page in the DOC private structure. */
1061 static int __init find_media_headers(struct mtd_info *mtd, u_char *buf,
1062 const char *id, int findmirror)
1064 struct nand_chip *this = mtd->priv;
1065 struct doc_priv *doc = (void *)this->priv;
1066 unsigned offs, end = (MAX_MEDIAHEADER_SCAN << this->phys_erase_shift);
1070 end = min(end, mtd->size); // paranoia
1071 for (offs = 0; offs < end; offs += mtd->erasesize) {
1072 ret = mtd->read(mtd, offs, mtd->oobblock, &retlen, buf);
1073 if (retlen != mtd->oobblock) continue;
1075 printk(KERN_WARNING "ECC error scanning DOC at 0x%x\n",
1078 if (memcmp(buf, id, 6)) continue;
1079 printk(KERN_INFO "Found DiskOnChip %s Media Header at 0x%x\n", id, offs);
1080 if (doc->mh0_page == -1) {
1081 doc->mh0_page = offs >> this->page_shift;
1082 if (!findmirror) return 1;
1085 doc->mh1_page = offs >> this->page_shift;
1088 if (doc->mh0_page == -1) {
1089 printk(KERN_WARNING "DiskOnChip %s Media Header not found.\n", id);
1092 /* Only one mediaheader was found. We want buf to contain a
1093 mediaheader on return, so we'll have to re-read the one we found. */
1094 offs = doc->mh0_page << this->page_shift;
1095 ret = mtd->read(mtd, offs, mtd->oobblock, &retlen, buf);
1096 if (retlen != mtd->oobblock) {
1097 /* Insanity. Give up. */
1098 printk(KERN_ERR "Read DiskOnChip Media Header once, but can't reread it???\n");
1104 static inline int __init nftl_partscan(struct mtd_info *mtd,
1105 struct mtd_partition *parts)
1107 struct nand_chip *this = mtd->priv;
1108 struct doc_priv *doc = (void *)this->priv;
1111 struct NFTLMediaHeader *mh;
1112 const unsigned psize = 1 << this->page_shift;
1113 unsigned blocks, maxblocks;
1114 int offs, numheaders;
1116 buf = kmalloc(mtd->oobblock, GFP_KERNEL);
1118 printk(KERN_ERR "DiskOnChip mediaheader kmalloc failed!\n");
1121 if (!(numheaders=find_media_headers(mtd, buf, "ANAND", 1))) goto out;
1122 mh = (struct NFTLMediaHeader *) buf;
1124 //#ifdef CONFIG_MTD_DEBUG_VERBOSE
1125 // if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
1126 printk(KERN_INFO " DataOrgID = %s\n"
1127 " NumEraseUnits = %d\n"
1128 " FirstPhysicalEUN = %d\n"
1129 " FormattedSize = %d\n"
1130 " UnitSizeFactor = %d\n",
1131 mh->DataOrgID, mh->NumEraseUnits,
1132 mh->FirstPhysicalEUN, mh->FormattedSize,
1133 mh->UnitSizeFactor);
1136 blocks = mtd->size >> this->phys_erase_shift;
1137 maxblocks = min(32768U, mtd->erasesize - psize);
1139 if (mh->UnitSizeFactor == 0x00) {
1140 /* Auto-determine UnitSizeFactor. The constraints are:
1141 - There can be at most 32768 virtual blocks.
1142 - There can be at most (virtual block size - page size)
1143 virtual blocks (because MediaHeader+BBT must fit in 1).
1145 mh->UnitSizeFactor = 0xff;
1146 while (blocks > maxblocks) {
1148 maxblocks = min(32768U, (maxblocks << 1) + psize);
1149 mh->UnitSizeFactor--;
1151 printk(KERN_WARNING "UnitSizeFactor=0x00 detected. Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor);
1154 /* NOTE: The lines below modify internal variables of the NAND and MTD
1155 layers; variables with have already been configured by nand_scan.
1156 Unfortunately, we didn't know before this point what these values
1157 should be. Thus, this code is somewhat dependant on the exact
1158 implementation of the NAND layer. */
1159 if (mh->UnitSizeFactor != 0xff) {
1160 this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
1161 mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
1162 printk(KERN_INFO "Setting virtual erase size to %d\n", mtd->erasesize);
1163 blocks = mtd->size >> this->bbt_erase_shift;
1164 maxblocks = min(32768U, mtd->erasesize - psize);
1167 if (blocks > maxblocks) {
1168 printk(KERN_ERR "UnitSizeFactor of 0x%02x is inconsistent with device size. Aborting.\n", mh->UnitSizeFactor);
1172 /* Skip past the media headers. */
1173 offs = max(doc->mh0_page, doc->mh1_page);
1174 offs <<= this->page_shift;
1175 offs += mtd->erasesize;
1177 //parts[0].name = " DiskOnChip Boot / Media Header partition";
1178 //parts[0].offset = 0;
1179 //parts[0].size = offs;
1181 parts[0].name = " DiskOnChip BDTL partition";
1182 parts[0].offset = offs;
1183 parts[0].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift;
1185 offs += parts[0].size;
1186 if (offs < mtd->size) {
1187 parts[1].name = " DiskOnChip Remainder partition";
1188 parts[1].offset = offs;
1189 parts[1].size = mtd->size - offs;
1199 /* This is a stripped-down copy of the code in inftlmount.c */
1200 static inline int __init inftl_partscan(struct mtd_info *mtd,
1201 struct mtd_partition *parts)
1203 struct nand_chip *this = mtd->priv;
1204 struct doc_priv *doc = (void *)this->priv;
1207 struct INFTLMediaHeader *mh;
1208 struct INFTLPartition *ip;
1211 int vshift, lastvunit = 0;
1213 int end = mtd->size;
1215 if (inftl_bbt_write)
1216 end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift);
1218 buf = kmalloc(mtd->oobblock, GFP_KERNEL);
1220 printk(KERN_ERR "DiskOnChip mediaheader kmalloc failed!\n");
1224 if (!find_media_headers(mtd, buf, "BNAND", 0)) goto out;
1225 doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift);
1226 mh = (struct INFTLMediaHeader *) buf;
1228 mh->NoOfBootImageBlocks = le32_to_cpu(mh->NoOfBootImageBlocks);
1229 mh->NoOfBinaryPartitions = le32_to_cpu(mh->NoOfBinaryPartitions);
1230 mh->NoOfBDTLPartitions = le32_to_cpu(mh->NoOfBDTLPartitions);
1231 mh->BlockMultiplierBits = le32_to_cpu(mh->BlockMultiplierBits);
1232 mh->FormatFlags = le32_to_cpu(mh->FormatFlags);
1233 mh->PercentUsed = le32_to_cpu(mh->PercentUsed);
1235 //#ifdef CONFIG_MTD_DEBUG_VERBOSE
1236 // if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
1237 printk(KERN_INFO " bootRecordID = %s\n"
1238 " NoOfBootImageBlocks = %d\n"
1239 " NoOfBinaryPartitions = %d\n"
1240 " NoOfBDTLPartitions = %d\n"
1241 " BlockMultiplerBits = %d\n"
1242 " FormatFlgs = %d\n"
1243 " OsakVersion = %d.%d.%d.%d\n"
1244 " PercentUsed = %d\n",
1245 mh->bootRecordID, mh->NoOfBootImageBlocks,
1246 mh->NoOfBinaryPartitions,
1247 mh->NoOfBDTLPartitions,
1248 mh->BlockMultiplierBits, mh->FormatFlags,
1249 ((unsigned char *) &mh->OsakVersion)[0] & 0xf,
1250 ((unsigned char *) &mh->OsakVersion)[1] & 0xf,
1251 ((unsigned char *) &mh->OsakVersion)[2] & 0xf,
1252 ((unsigned char *) &mh->OsakVersion)[3] & 0xf,
1256 vshift = this->phys_erase_shift + mh->BlockMultiplierBits;
1258 blocks = mtd->size >> vshift;
1259 if (blocks > 32768) {
1260 printk(KERN_ERR "BlockMultiplierBits=%d is inconsistent with device size. Aborting.\n", mh->BlockMultiplierBits);
1264 blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift);
1265 if (inftl_bbt_write && (blocks > mtd->erasesize)) {
1266 printk(KERN_ERR "Writeable BBTs spanning more than one erase block are not yet supported. FIX ME!\n");
1270 /* Scan the partitions */
1271 for (i = 0; (i < 4); i++) {
1272 ip = &(mh->Partitions[i]);
1273 ip->virtualUnits = le32_to_cpu(ip->virtualUnits);
1274 ip->firstUnit = le32_to_cpu(ip->firstUnit);
1275 ip->lastUnit = le32_to_cpu(ip->lastUnit);
1276 ip->flags = le32_to_cpu(ip->flags);
1277 ip->spareUnits = le32_to_cpu(ip->spareUnits);
1278 ip->Reserved0 = le32_to_cpu(ip->Reserved0);
1280 //#ifdef CONFIG_MTD_DEBUG_VERBOSE
1281 // if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
1282 printk(KERN_INFO " PARTITION[%d] ->\n"
1283 " virtualUnits = %d\n"
1287 " spareUnits = %d\n",
1288 i, ip->virtualUnits, ip->firstUnit,
1289 ip->lastUnit, ip->flags,
1294 if ((i == 0) && (ip->firstUnit > 0)) {
1295 parts[0].name = " DiskOnChip IPL / Media Header partition";
1296 parts[0].offset = 0;
1297 parts[0].size = mtd->erasesize * ip->firstUnit;
1302 if (ip->flags & INFTL_BINARY)
1303 parts[numparts].name = " DiskOnChip BDK partition";
1305 parts[numparts].name = " DiskOnChip BDTL partition";
1306 parts[numparts].offset = ip->firstUnit << vshift;
1307 parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift;
1309 if (ip->lastUnit > lastvunit) lastvunit = ip->lastUnit;
1310 if (ip->flags & INFTL_LAST) break;
1313 if ((lastvunit << vshift) < end) {
1314 parts[numparts].name = " DiskOnChip Remainder partition";
1315 parts[numparts].offset = lastvunit << vshift;
1316 parts[numparts].size = end - parts[numparts].offset;
1325 static int __init nftl_scan_bbt(struct mtd_info *mtd)
1328 struct nand_chip *this = mtd->priv;
1329 struct doc_priv *doc = (void *)this->priv;
1330 struct mtd_partition parts[2];
1332 memset((char *) parts, 0, sizeof(parts));
1333 /* On NFTL, we have to find the media headers before we can read the
1334 BBTs, since they're stored in the media header eraseblocks. */
1335 numparts = nftl_partscan(mtd, parts);
1336 if (!numparts) return -EIO;
1337 this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1338 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1340 this->bbt_td->veroffs = 7;
1341 this->bbt_td->pages[0] = doc->mh0_page + 1;
1342 if (doc->mh1_page != -1) {
1343 this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1344 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1346 this->bbt_md->veroffs = 7;
1347 this->bbt_md->pages[0] = doc->mh1_page + 1;
1349 this->bbt_md = NULL;
1352 /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
1353 At least as nand_bbt.c is currently written. */
1354 if ((ret = nand_scan_bbt(mtd, NULL)))
1356 add_mtd_device(mtd);
1357 #ifdef CONFIG_MTD_PARTITIONS
1359 add_mtd_partitions(mtd, parts, numparts);
1364 static int __init inftl_scan_bbt(struct mtd_info *mtd)
1367 struct nand_chip *this = mtd->priv;
1368 struct doc_priv *doc = (void *)this->priv;
1369 struct mtd_partition parts[5];
1371 if (this->numchips > doc->chips_per_floor) {
1372 printk(KERN_ERR "Multi-floor INFTL devices not yet supported.\n");
1376 if (DoC_is_MillenniumPlus(doc)) {
1377 this->bbt_td->options = NAND_BBT_2BIT | NAND_BBT_ABSPAGE;
1378 if (inftl_bbt_write)
1379 this->bbt_td->options |= NAND_BBT_WRITE;
1380 this->bbt_td->pages[0] = 2;
1381 this->bbt_md = NULL;
1383 this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT |
1385 if (inftl_bbt_write)
1386 this->bbt_td->options |= NAND_BBT_WRITE;
1387 this->bbt_td->offs = 8;
1388 this->bbt_td->len = 8;
1389 this->bbt_td->veroffs = 7;
1390 this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1391 this->bbt_td->reserved_block_code = 0x01;
1392 this->bbt_td->pattern = "MSYS_BBT";
1394 this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT |
1396 if (inftl_bbt_write)
1397 this->bbt_md->options |= NAND_BBT_WRITE;
1398 this->bbt_md->offs = 8;
1399 this->bbt_md->len = 8;
1400 this->bbt_md->veroffs = 7;
1401 this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1402 this->bbt_md->reserved_block_code = 0x01;
1403 this->bbt_md->pattern = "TBB_SYSM";
1406 /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
1407 At least as nand_bbt.c is currently written. */
1408 if ((ret = nand_scan_bbt(mtd, NULL)))
1410 memset((char *) parts, 0, sizeof(parts));
1411 numparts = inftl_partscan(mtd, parts);
1412 /* At least for now, require the INFTL Media Header. We could probably
1413 do without it for non-INFTL use, since all it gives us is
1414 autopartitioning, but I want to give it more thought. */
1415 if (!numparts) return -EIO;
1416 add_mtd_device(mtd);
1417 #ifdef CONFIG_MTD_PARTITIONS
1419 add_mtd_partitions(mtd, parts, numparts);
1424 static inline int __init doc2000_init(struct mtd_info *mtd)
1426 struct nand_chip *this = mtd->priv;
1427 struct doc_priv *doc = (void *)this->priv;
1429 this->write_byte = doc2000_write_byte;
1430 this->read_byte = doc2000_read_byte;
1431 this->write_buf = doc2000_writebuf;
1432 this->read_buf = doc2000_readbuf;
1433 this->verify_buf = doc2000_verifybuf;
1434 this->scan_bbt = nftl_scan_bbt;
1436 doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
1437 doc2000_count_chips(mtd);
1438 mtd->name = "DiskOnChip 2000 (NFTL Model)";
1439 return (4 * doc->chips_per_floor);
1442 static inline int __init doc2001_init(struct mtd_info *mtd)
1444 struct nand_chip *this = mtd->priv;
1445 struct doc_priv *doc = (void *)this->priv;
1447 this->write_byte = doc2001_write_byte;
1448 this->read_byte = doc2001_read_byte;
1449 this->write_buf = doc2001_writebuf;
1450 this->read_buf = doc2001_readbuf;
1451 this->verify_buf = doc2001_verifybuf;
1453 ReadDOC(doc->virtadr, ChipID);
1454 ReadDOC(doc->virtadr, ChipID);
1455 ReadDOC(doc->virtadr, ChipID);
1456 if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) {
1457 /* It's not a Millennium; it's one of the newer
1458 DiskOnChip 2000 units with a similar ASIC.
1459 Treat it like a Millennium, except that it
1460 can have multiple chips. */
1461 doc2000_count_chips(mtd);
1462 mtd->name = "DiskOnChip 2000 (INFTL Model)";
1463 this->scan_bbt = inftl_scan_bbt;
1464 return (4 * doc->chips_per_floor);
1466 /* Bog-standard Millennium */
1467 doc->chips_per_floor = 1;
1468 mtd->name = "DiskOnChip Millennium";
1469 this->scan_bbt = nftl_scan_bbt;
1474 static inline int __init doc2001plus_init(struct mtd_info *mtd)
1476 struct nand_chip *this = mtd->priv;
1477 struct doc_priv *doc = (void *)this->priv;
1479 this->write_byte = NULL;
1480 this->read_byte = doc2001plus_read_byte;
1481 this->write_buf = doc2001plus_writebuf;
1482 this->read_buf = doc2001plus_readbuf;
1483 this->verify_buf = doc2001plus_verifybuf;
1484 this->scan_bbt = inftl_scan_bbt;
1485 this->hwcontrol = NULL;
1486 this->select_chip = doc2001plus_select_chip;
1487 this->cmdfunc = doc2001plus_command;
1488 this->enable_hwecc = doc2001plus_enable_hwecc;
1490 doc->chips_per_floor = 1;
1491 mtd->name = "DiskOnChip Millennium Plus";
1496 static inline int __init doc_probe(unsigned long physadr)
1498 unsigned char ChipID;
1499 struct mtd_info *mtd;
1500 struct nand_chip *nand;
1501 struct doc_priv *doc;
1502 void __iomem *virtadr;
1503 unsigned char save_control;
1504 unsigned char tmp, tmpb, tmpc;
1505 int reg, len, numchips;
1508 virtadr = ioremap(physadr, DOC_IOREMAP_LEN);
1510 printk(KERN_ERR "Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n", DOC_IOREMAP_LEN, physadr);
1514 /* It's not possible to cleanly detect the DiskOnChip - the
1515 * bootup procedure will put the device into reset mode, and
1516 * it's not possible to talk to it without actually writing
1517 * to the DOCControl register. So we store the current contents
1518 * of the DOCControl register's location, in case we later decide
1519 * that it's not a DiskOnChip, and want to put it back how we
1522 save_control = ReadDOC(virtadr, DOCControl);
1524 /* Reset the DiskOnChip ASIC */
1525 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET,
1526 virtadr, DOCControl);
1527 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET,
1528 virtadr, DOCControl);
1530 /* Enable the DiskOnChip ASIC */
1531 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL,
1532 virtadr, DOCControl);
1533 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL,
1534 virtadr, DOCControl);
1536 ChipID = ReadDOC(virtadr, ChipID);
1539 case DOC_ChipID_Doc2k:
1540 reg = DoC_2k_ECCStatus;
1542 case DOC_ChipID_DocMil:
1545 case DOC_ChipID_DocMilPlus16:
1546 case DOC_ChipID_DocMilPlus32:
1548 /* Possible Millennium Plus, need to do more checks */
1549 /* Possibly release from power down mode */
1550 for (tmp = 0; (tmp < 4); tmp++)
1551 ReadDOC(virtadr, Mplus_Power);
1553 /* Reset the Millennium Plus ASIC */
1554 tmp = DOC_MODE_RESET | DOC_MODE_MDWREN | DOC_MODE_RST_LAT |
1556 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1557 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1560 /* Enable the Millennium Plus ASIC */
1561 tmp = DOC_MODE_NORMAL | DOC_MODE_MDWREN | DOC_MODE_RST_LAT |
1563 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1564 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1567 ChipID = ReadDOC(virtadr, ChipID);
1570 case DOC_ChipID_DocMilPlus16:
1571 reg = DoC_Mplus_Toggle;
1573 case DOC_ChipID_DocMilPlus32:
1574 printk(KERN_ERR "DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
1585 /* Check the TOGGLE bit in the ECC register */
1586 tmp = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1587 tmpb = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1588 tmpc = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1589 if ((tmp == tmpb) || (tmp != tmpc)) {
1590 printk(KERN_WARNING "Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr);
1595 for (mtd = doclist; mtd; mtd = doc->nextdoc) {
1596 unsigned char oldval;
1597 unsigned char newval;
1599 doc = (void *)nand->priv;
1600 /* Use the alias resolution register to determine if this is
1601 in fact the same DOC aliased to a new address. If writes
1602 to one chip's alias resolution register change the value on
1603 the other chip, they're the same chip. */
1604 if (ChipID == DOC_ChipID_DocMilPlus16) {
1605 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1606 newval = ReadDOC(virtadr, Mplus_AliasResolution);
1608 oldval = ReadDOC(doc->virtadr, AliasResolution);
1609 newval = ReadDOC(virtadr, AliasResolution);
1611 if (oldval != newval)
1613 if (ChipID == DOC_ChipID_DocMilPlus16) {
1614 WriteDOC(~newval, virtadr, Mplus_AliasResolution);
1615 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1616 WriteDOC(newval, virtadr, Mplus_AliasResolution); // restore it
1618 WriteDOC(~newval, virtadr, AliasResolution);
1619 oldval = ReadDOC(doc->virtadr, AliasResolution);
1620 WriteDOC(newval, virtadr, AliasResolution); // restore it
1623 if (oldval == newval) {
1624 printk(KERN_DEBUG "Found alias of DOC at 0x%lx to 0x%lx\n", doc->physadr, physadr);
1629 printk(KERN_NOTICE "DiskOnChip found at 0x%lx\n", physadr);
1631 len = sizeof(struct mtd_info) +
1632 sizeof(struct nand_chip) +
1633 sizeof(struct doc_priv) +
1634 (2 * sizeof(struct nand_bbt_descr));
1635 mtd = kmalloc(len, GFP_KERNEL);
1637 printk(KERN_ERR "DiskOnChip kmalloc (%d bytes) failed!\n", len);
1641 memset(mtd, 0, len);
1643 nand = (struct nand_chip *) (mtd + 1);
1644 doc = (struct doc_priv *) (nand + 1);
1645 nand->bbt_td = (struct nand_bbt_descr *) (doc + 1);
1646 nand->bbt_md = nand->bbt_td + 1;
1648 mtd->priv = (void *) nand;
1649 mtd->owner = THIS_MODULE;
1651 nand->priv = (void *) doc;
1652 nand->select_chip = doc200x_select_chip;
1653 nand->hwcontrol = doc200x_hwcontrol;
1654 nand->dev_ready = doc200x_dev_ready;
1655 nand->waitfunc = doc200x_wait;
1656 nand->block_bad = doc200x_block_bad;
1657 nand->enable_hwecc = doc200x_enable_hwecc;
1658 nand->calculate_ecc = doc200x_calculate_ecc;
1659 nand->correct_data = doc200x_correct_data;
1661 nand->autooob = &doc200x_oobinfo;
1662 nand->eccmode = NAND_ECC_HW6_512;
1663 nand->options = NAND_USE_FLASH_BBT | NAND_HWECC_SYNDROME;
1665 doc->physadr = physadr;
1666 doc->virtadr = virtadr;
1667 doc->ChipID = ChipID;
1672 doc->nextdoc = doclist;
1674 if (ChipID == DOC_ChipID_Doc2k)
1675 numchips = doc2000_init(mtd);
1676 else if (ChipID == DOC_ChipID_DocMilPlus16)
1677 numchips = doc2001plus_init(mtd);
1679 numchips = doc2001_init(mtd);
1681 if ((ret = nand_scan(mtd, numchips))) {
1682 /* DBB note: i believe nand_release is necessary here, as
1683 buffers may have been allocated in nand_base. Check with
1685 /* nand_release will call del_mtd_device, but we haven't yet
1686 added it. This is handled without incident by
1687 del_mtd_device, as far as I can tell. */
1698 /* Put back the contents of the DOCControl register, in case it's not
1699 actually a DiskOnChip. */
1700 WriteDOC(save_control, virtadr, DOCControl);
1702 iounmap((void *)virtadr);
1706 static void release_nanddoc(void)
1708 struct mtd_info *mtd, *nextmtd;
1709 struct nand_chip *nand;
1710 struct doc_priv *doc;
1712 for (mtd = doclist; mtd; mtd = nextmtd) {
1714 doc = (void *)nand->priv;
1716 nextmtd = doc->nextdoc;
1718 iounmap((void *)doc->virtadr);
1723 static int __init init_nanddoc(void)
1727 /* We could create the decoder on demand, if memory is a concern.
1728 * This way we have it handy, if an error happens
1730 * Symbolsize is 10 (bits)
1731 * Primitve polynomial is x^10+x^3+1
1732 * first consecutive root is 510
1733 * primitve element to generate roots = 1
1734 * generator polinomial degree = 4
1736 rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS);
1738 printk (KERN_ERR "DiskOnChip: Could not create a RS decoder\n");
1742 if (doc_config_location) {
1743 printk(KERN_INFO "Using configured DiskOnChip probe address 0x%lx\n", doc_config_location);
1744 ret = doc_probe(doc_config_location);
1748 for (i=0; (doc_locations[i] != 0xffffffff); i++) {
1749 doc_probe(doc_locations[i]);
1752 /* No banner message any more. Print a message if no DiskOnChip
1753 found, so the user knows we at least tried. */
1755 printk(KERN_INFO "No valid DiskOnChip devices found\n");
1761 free_rs(rs_decoder);
1765 static void __exit cleanup_nanddoc(void)
1767 /* Cleanup the nand/DoC resources */
1770 /* Free the reed solomon resources */
1772 free_rs(rs_decoder);
1776 module_init(init_nanddoc);
1777 module_exit(cleanup_nanddoc);
1779 MODULE_LICENSE("GPL");
1780 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1781 MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver\n");