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
22 #if !defined(CONFIG_NAND_LEGACY)
24 #include <linux/kernel.h>
25 #include <linux/init.h>
26 #include <linux/sched.h>
27 #include <linux/delay.h>
28 #include <linux/rslib.h>
29 #include <linux/moduleparam.h>
32 #include <linux/mtd/mtd.h>
33 #include <linux/mtd/nand.h>
34 #include <linux/mtd/doc2000.h>
35 #include <linux/mtd/compatmac.h>
36 #include <linux/mtd/partitions.h>
37 #include <linux/mtd/inftl.h>
39 /* Where to look for the devices? */
40 #ifndef CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS
41 #define CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS 0
44 static unsigned long __initdata doc_locations[] = {
45 #if defined (__alpha__) || defined(__i386__) || defined(__x86_64__)
46 #ifdef CONFIG_MTD_NAND_DISKONCHIP_PROBE_HIGH
47 0xfffc8000, 0xfffca000, 0xfffcc000, 0xfffce000,
48 0xfffd0000, 0xfffd2000, 0xfffd4000, 0xfffd6000,
49 0xfffd8000, 0xfffda000, 0xfffdc000, 0xfffde000,
50 0xfffe0000, 0xfffe2000, 0xfffe4000, 0xfffe6000,
51 0xfffe8000, 0xfffea000, 0xfffec000, 0xfffee000,
52 #else /* CONFIG_MTD_DOCPROBE_HIGH */
53 0xc8000, 0xca000, 0xcc000, 0xce000,
54 0xd0000, 0xd2000, 0xd4000, 0xd6000,
55 0xd8000, 0xda000, 0xdc000, 0xde000,
56 0xe0000, 0xe2000, 0xe4000, 0xe6000,
57 0xe8000, 0xea000, 0xec000, 0xee000,
58 #endif /* CONFIG_MTD_DOCPROBE_HIGH */
60 #warning Unknown architecture for DiskOnChip. No default probe locations defined
64 static struct mtd_info *doclist = NULL;
67 void __iomem *virtadr;
68 unsigned long physadr;
71 int chips_per_floor; /* The number of chips detected on each floor */
76 struct mtd_info *nextdoc;
79 /* This is the syndrome computed by the HW ecc generator upon reading an empty
80 page, one with all 0xff for data and stored ecc code. */
81 static u_char empty_read_syndrome[6] = { 0x26, 0xff, 0x6d, 0x47, 0x73, 0x7a };
83 /* This is the ecc value computed by the HW ecc generator upon writing an empty
84 page, one with all 0xff for data. */
85 static u_char empty_write_ecc[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
87 #define INFTL_BBT_RESERVED_BLOCKS 4
89 #define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
90 #define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
91 #define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
93 static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
94 unsigned int bitmask);
95 static void doc200x_select_chip(struct mtd_info *mtd, int chip);
98 module_param(debug, int, 0);
100 static int try_dword = 1;
101 module_param(try_dword, int, 0);
103 static int no_ecc_failures = 0;
104 module_param(no_ecc_failures, int, 0);
106 static int no_autopart = 0;
107 module_param(no_autopart, int, 0);
109 static int show_firmware_partition = 0;
110 module_param(show_firmware_partition, int, 0);
112 #ifdef CONFIG_MTD_NAND_DISKONCHIP_BBTWRITE
113 static int inftl_bbt_write = 1;
115 static int inftl_bbt_write = 0;
117 module_param(inftl_bbt_write, int, 0);
119 static unsigned long doc_config_location = CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS;
120 module_param(doc_config_location, ulong, 0);
121 MODULE_PARM_DESC(doc_config_location, "Physical memory address at which to probe for DiskOnChip");
123 /* Sector size for HW ECC */
124 #define SECTOR_SIZE 512
125 /* The sector bytes are packed into NB_DATA 10 bit words */
126 #define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / 10)
127 /* Number of roots */
129 /* First consective root */
131 /* Number of symbols */
134 /* the Reed Solomon control structure */
135 static struct rs_control *rs_decoder;
138 * The HW decoder in the DoC ASIC's provides us a error syndrome,
139 * which we must convert to a standard syndrom usable by the generic
140 * Reed-Solomon library code.
142 * Fabrice Bellard figured this out in the old docecc code. I added
143 * some comments, improved a minor bit and converted it to make use
144 * of the generic Reed-Solomon libary. tglx
146 static int doc_ecc_decode(struct rs_control *rs, uint8_t *data, uint8_t *ecc)
148 int i, j, nerr, errpos[8];
150 uint16_t ds[4], s[5], tmp, errval[8], syn[4];
152 /* Convert the ecc bytes into words */
153 ds[0] = ((ecc[4] & 0xff) >> 0) | ((ecc[5] & 0x03) << 8);
154 ds[1] = ((ecc[5] & 0xfc) >> 2) | ((ecc[2] & 0x0f) << 6);
155 ds[2] = ((ecc[2] & 0xf0) >> 4) | ((ecc[3] & 0x3f) << 4);
156 ds[3] = ((ecc[3] & 0xc0) >> 6) | ((ecc[0] & 0xff) << 2);
159 /* Initialize the syndrom buffer */
160 for (i = 0; i < NROOTS; i++)
164 * s[i] = ds[3]x^3 + ds[2]x^2 + ds[1]x^1 + ds[0]
165 * where x = alpha^(FCR + i)
167 for (j = 1; j < NROOTS; j++) {
170 tmp = rs->index_of[ds[j]];
171 for (i = 0; i < NROOTS; i++)
172 s[i] ^= rs->alpha_to[rs_modnn(rs, tmp + (FCR + i) * j)];
175 /* Calc s[i] = s[i] / alpha^(v + i) */
176 for (i = 0; i < NROOTS; i++) {
178 syn[i] = rs_modnn(rs, rs->index_of[s[i]] + (NN - FCR - i));
180 /* Call the decoder library */
181 nerr = decode_rs16(rs, NULL, NULL, 1019, syn, 0, errpos, 0, errval);
183 /* Incorrectable errors ? */
188 * Correct the errors. The bitpositions are a bit of magic,
189 * but they are given by the design of the de/encoder circuit
192 for (i = 0; i < nerr; i++) {
193 int index, bitpos, pos = 1015 - errpos[i];
195 if (pos >= NB_DATA && pos < 1019)
198 /* extract bit position (MSB first) */
199 pos = 10 * (NB_DATA - 1 - pos) - 6;
200 /* now correct the following 10 bits. At most two bytes
201 can be modified since pos is even */
202 index = (pos >> 3) ^ 1;
204 if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
205 val = (uint8_t) (errval[i] >> (2 + bitpos));
207 if (index < SECTOR_SIZE)
210 index = ((pos >> 3) + 1) ^ 1;
211 bitpos = (bitpos + 10) & 7;
214 if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
215 val = (uint8_t) (errval[i] << (8 - bitpos));
217 if (index < SECTOR_SIZE)
222 /* If the parity is wrong, no rescue possible */
223 return parity ? -EBADMSG : nerr;
226 static void DoC_Delay(struct doc_priv *doc, unsigned short cycles)
231 for (i = 0; i < cycles; i++) {
232 if (DoC_is_Millennium(doc))
233 dummy = ReadDOC(doc->virtadr, NOP);
234 else if (DoC_is_MillenniumPlus(doc))
235 dummy = ReadDOC(doc->virtadr, Mplus_NOP);
237 dummy = ReadDOC(doc->virtadr, DOCStatus);
242 #define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
244 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
245 static int _DoC_WaitReady(struct doc_priv *doc)
247 void __iomem *docptr = doc->virtadr;
248 unsigned long timeo = jiffies + (HZ * 10);
251 printk("_DoC_WaitReady...\n");
252 /* Out-of-line routine to wait for chip response */
253 if (DoC_is_MillenniumPlus(doc)) {
254 while ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
255 if (time_after(jiffies, timeo)) {
256 printk("_DoC_WaitReady timed out.\n");
263 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
264 if (time_after(jiffies, timeo)) {
265 printk("_DoC_WaitReady timed out.\n");
276 static inline int DoC_WaitReady(struct doc_priv *doc)
278 void __iomem *docptr = doc->virtadr;
281 if (DoC_is_MillenniumPlus(doc)) {
284 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
285 /* Call the out-of-line routine to wait */
286 ret = _DoC_WaitReady(doc);
290 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
291 /* Call the out-of-line routine to wait */
292 ret = _DoC_WaitReady(doc);
297 printk("DoC_WaitReady OK\n");
301 static void doc2000_write_byte(struct mtd_info *mtd, u_char datum)
303 struct nand_chip *this = mtd->priv;
304 struct doc_priv *doc = this->priv;
305 void __iomem *docptr = doc->virtadr;
308 printk("write_byte %02x\n", datum);
309 WriteDOC(datum, docptr, CDSNSlowIO);
310 WriteDOC(datum, docptr, 2k_CDSN_IO);
313 static u_char doc2000_read_byte(struct mtd_info *mtd)
315 struct nand_chip *this = mtd->priv;
316 struct doc_priv *doc = this->priv;
317 void __iomem *docptr = doc->virtadr;
320 ReadDOC(docptr, CDSNSlowIO);
322 ret = ReadDOC(docptr, 2k_CDSN_IO);
324 printk("read_byte returns %02x\n", ret);
328 static void doc2000_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
330 struct nand_chip *this = mtd->priv;
331 struct doc_priv *doc = this->priv;
332 void __iomem *docptr = doc->virtadr;
335 printk("writebuf of %d bytes: ", len);
336 for (i = 0; i < len; i++) {
337 WriteDOC_(buf[i], docptr, DoC_2k_CDSN_IO + i);
339 printk("%02x ", buf[i]);
345 static void doc2000_readbuf(struct mtd_info *mtd, u_char *buf, int len)
347 struct nand_chip *this = mtd->priv;
348 struct doc_priv *doc = this->priv;
349 void __iomem *docptr = doc->virtadr;
353 printk("readbuf of %d bytes: ", len);
355 for (i = 0; i < len; i++) {
356 buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
360 static void doc2000_readbuf_dword(struct mtd_info *mtd,
361 u_char *buf, int len)
363 struct nand_chip *this = mtd->priv;
364 struct doc_priv *doc = this->priv;
365 void __iomem *docptr = doc->virtadr;
369 printk("readbuf_dword of %d bytes: ", len);
371 if (unlikely((((unsigned long)buf) | len) & 3)) {
372 for (i = 0; i < len; i++) {
373 *(uint8_t *) (&buf[i]) = ReadDOC(docptr, 2k_CDSN_IO + i);
376 for (i = 0; i < len; i += 4) {
377 *(uint32_t*) (&buf[i]) = readl(docptr + DoC_2k_CDSN_IO + i);
382 static int doc2000_verifybuf(struct mtd_info *mtd, const u_char *buf, int len)
384 struct nand_chip *this = mtd->priv;
385 struct doc_priv *doc = this->priv;
386 void __iomem *docptr = doc->virtadr;
389 for (i = 0; i < len; i++)
390 if (buf[i] != ReadDOC(docptr, 2k_CDSN_IO))
395 static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr)
397 struct nand_chip *this = mtd->priv;
398 struct doc_priv *doc = this->priv;
401 doc200x_select_chip(mtd, nr);
402 doc200x_hwcontrol(mtd, NAND_CMD_READID,
403 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
404 doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
405 doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
407 /* We cant' use dev_ready here, but at least we wait for the
408 * command to complete
412 ret = this->read_byte(mtd) << 8;
413 ret |= this->read_byte(mtd);
415 if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) {
416 /* First chip probe. See if we get same results by 32-bit access */
421 void __iomem *docptr = doc->virtadr;
423 doc200x_hwcontrol(mtd, NAND_CMD_READID,
424 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
425 doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
426 doc200x_hwcontrol(mtd, NAND_CMD_NONE,
427 NAND_NCE | NAND_CTRL_CHANGE);
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 = 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)
465 struct doc_priv *doc = 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 = 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 = 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, const u_char *buf, int len)
504 struct nand_chip *this = mtd->priv;
505 struct doc_priv *doc = this->priv;
506 void __iomem *docptr = doc->virtadr;
509 for (i = 0; i < len; i++)
510 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
511 /* Terminate write pipeline */
512 WriteDOC(0x00, docptr, WritePipeTerm);
515 static void doc2001_readbuf(struct mtd_info *mtd, u_char *buf, int len)
517 struct nand_chip *this = mtd->priv;
518 struct doc_priv *doc = this->priv;
519 void __iomem *docptr = doc->virtadr;
522 /* Start read pipeline */
523 ReadDOC(docptr, ReadPipeInit);
525 for (i = 0; i < len - 1; i++)
526 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
528 /* Terminate read pipeline */
529 buf[i] = ReadDOC(docptr, LastDataRead);
532 static int doc2001_verifybuf(struct mtd_info *mtd, const u_char *buf, int len)
534 struct nand_chip *this = mtd->priv;
535 struct doc_priv *doc = this->priv;
536 void __iomem *docptr = doc->virtadr;
539 /* Start read pipeline */
540 ReadDOC(docptr, ReadPipeInit);
542 for (i = 0; i < len - 1; i++)
543 if (buf[i] != ReadDOC(docptr, Mil_CDSN_IO)) {
544 ReadDOC(docptr, LastDataRead);
547 if (buf[i] != ReadDOC(docptr, LastDataRead))
552 static u_char doc2001plus_read_byte(struct mtd_info *mtd)
554 struct nand_chip *this = mtd->priv;
555 struct doc_priv *doc = this->priv;
556 void __iomem *docptr = doc->virtadr;
559 ReadDOC(docptr, Mplus_ReadPipeInit);
560 ReadDOC(docptr, Mplus_ReadPipeInit);
561 ret = ReadDOC(docptr, Mplus_LastDataRead);
563 printk("read_byte returns %02x\n", ret);
567 static void doc2001plus_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
569 struct nand_chip *this = mtd->priv;
570 struct doc_priv *doc = this->priv;
571 void __iomem *docptr = doc->virtadr;
575 printk("writebuf of %d bytes: ", len);
576 for (i = 0; i < len; i++) {
577 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
579 printk("%02x ", buf[i]);
585 static void doc2001plus_readbuf(struct mtd_info *mtd, u_char *buf, int len)
587 struct nand_chip *this = mtd->priv;
588 struct doc_priv *doc = this->priv;
589 void __iomem *docptr = doc->virtadr;
593 printk("readbuf of %d bytes: ", len);
595 /* Start read pipeline */
596 ReadDOC(docptr, Mplus_ReadPipeInit);
597 ReadDOC(docptr, Mplus_ReadPipeInit);
599 for (i = 0; i < len - 2; i++) {
600 buf[i] = ReadDOC(docptr, Mil_CDSN_IO);
602 printk("%02x ", buf[i]);
605 /* Terminate read pipeline */
606 buf[len - 2] = ReadDOC(docptr, Mplus_LastDataRead);
608 printk("%02x ", buf[len - 2]);
609 buf[len - 1] = ReadDOC(docptr, Mplus_LastDataRead);
611 printk("%02x ", buf[len - 1]);
616 static int doc2001plus_verifybuf(struct mtd_info *mtd, const u_char *buf, int len)
618 struct nand_chip *this = mtd->priv;
619 struct doc_priv *doc = this->priv;
620 void __iomem *docptr = doc->virtadr;
624 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 = this->priv;
647 void __iomem *docptr = doc->virtadr;
651 printk("select chip (%d)\n", chip);
654 /* Disable flash internally */
655 WriteDOC(0, docptr, Mplus_FlashSelect);
659 floor = chip / doc->chips_per_floor;
660 chip -= (floor * doc->chips_per_floor);
662 /* Assert ChipEnable and deassert WriteProtect */
663 WriteDOC((DOC_FLASH_CE), docptr, Mplus_FlashSelect);
664 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
667 doc->curfloor = floor;
670 static void doc200x_select_chip(struct mtd_info *mtd, int chip)
672 struct nand_chip *this = mtd->priv;
673 struct doc_priv *doc = this->priv;
674 void __iomem *docptr = doc->virtadr;
678 printk("select chip (%d)\n", chip);
683 floor = chip / doc->chips_per_floor;
684 chip -= (floor * doc->chips_per_floor);
686 /* 11.4.4 -- deassert CE before changing chip */
687 doc200x_hwcontrol(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
689 WriteDOC(floor, docptr, FloorSelect);
690 WriteDOC(chip, docptr, CDSNDeviceSelect);
692 doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
695 doc->curfloor = floor;
698 #define CDSN_CTRL_MSK (CDSN_CTRL_CE | CDSN_CTRL_CLE | CDSN_CTRL_ALE)
700 static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
703 struct nand_chip *this = mtd->priv;
704 struct doc_priv *doc = this->priv;
705 void __iomem *docptr = doc->virtadr;
707 if (ctrl & NAND_CTRL_CHANGE) {
708 doc->CDSNControl &= ~CDSN_CTRL_MSK;
709 doc->CDSNControl |= ctrl & CDSN_CTRL_MSK;
711 printk("hwcontrol(%d): %02x\n", cmd, doc->CDSNControl);
712 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
713 /* 11.4.3 -- 4 NOPs after CSDNControl write */
716 if (cmd != NAND_CMD_NONE) {
717 if (DoC_is_2000(doc))
718 doc2000_write_byte(mtd, cmd);
720 doc2001_write_byte(mtd, cmd);
724 static void doc2001plus_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
726 struct nand_chip *this = mtd->priv;
727 struct doc_priv *doc = this->priv;
728 void __iomem *docptr = doc->virtadr;
731 * Must terminate write pipeline before sending any commands
734 if (command == NAND_CMD_PAGEPROG) {
735 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
736 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
740 * Write out the command to the device.
742 if (command == NAND_CMD_SEQIN) {
745 if (column >= mtd->writesize) {
747 column -= mtd->writesize;
748 readcmd = NAND_CMD_READOOB;
749 } else if (column < 256) {
750 /* First 256 bytes --> READ0 */
751 readcmd = NAND_CMD_READ0;
754 readcmd = NAND_CMD_READ1;
756 WriteDOC(readcmd, docptr, Mplus_FlashCmd);
758 WriteDOC(command, docptr, Mplus_FlashCmd);
759 WriteDOC(0, docptr, Mplus_WritePipeTerm);
760 WriteDOC(0, docptr, Mplus_WritePipeTerm);
762 if (column != -1 || page_addr != -1) {
763 /* Serially input address */
765 /* Adjust columns for 16 bit buswidth */
766 if (this->options & NAND_BUSWIDTH_16)
768 WriteDOC(column, docptr, Mplus_FlashAddress);
770 if (page_addr != -1) {
771 WriteDOC((unsigned char)(page_addr & 0xff), docptr, Mplus_FlashAddress);
772 WriteDOC((unsigned char)((page_addr >> 8) & 0xff), docptr, Mplus_FlashAddress);
773 /* One more address cycle for higher density devices */
774 if (this->chipsize & 0x0c000000) {
775 WriteDOC((unsigned char)((page_addr >> 16) & 0x0f), docptr, Mplus_FlashAddress);
776 printk("high density\n");
779 WriteDOC(0, docptr, Mplus_WritePipeTerm);
780 WriteDOC(0, docptr, Mplus_WritePipeTerm);
782 if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
783 command == NAND_CMD_READOOB || command == NAND_CMD_READID)
784 WriteDOC(0, docptr, Mplus_FlashControl);
788 * program and erase have their own busy handlers
789 * status and sequential in needs no delay
793 case NAND_CMD_PAGEPROG:
794 case NAND_CMD_ERASE1:
795 case NAND_CMD_ERASE2:
797 case NAND_CMD_STATUS:
803 udelay(this->chip_delay);
804 WriteDOC(NAND_CMD_STATUS, docptr, Mplus_FlashCmd);
805 WriteDOC(0, docptr, Mplus_WritePipeTerm);
806 WriteDOC(0, docptr, Mplus_WritePipeTerm);
807 while (!(this->read_byte(mtd) & 0x40)) ;
810 /* This applies to read commands */
813 * If we don't have access to the busy pin, we apply the given
816 if (!this->dev_ready) {
817 udelay(this->chip_delay);
822 /* Apply this short delay always to ensure that we do wait tWB in
823 * any case on any machine. */
825 /* wait until command is processed */
826 while (!this->dev_ready(mtd)) ;
829 static int doc200x_dev_ready(struct mtd_info *mtd)
831 struct nand_chip *this = mtd->priv;
832 struct doc_priv *doc = this->priv;
833 void __iomem *docptr = doc->virtadr;
835 if (DoC_is_MillenniumPlus(doc)) {
836 /* 11.4.2 -- must NOP four times before checking FR/B# */
838 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
840 printk("not ready\n");
844 printk("was ready\n");
847 /* 11.4.2 -- must NOP four times before checking FR/B# */
849 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
851 printk("not ready\n");
854 /* 11.4.2 -- Must NOP twice if it's ready */
857 printk("was ready\n");
862 static int doc200x_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
864 /* This is our last resort if we couldn't find or create a BBT. Just
865 pretend all blocks are good. */
869 static void doc200x_enable_hwecc(struct mtd_info *mtd, int mode)
871 struct nand_chip *this = mtd->priv;
872 struct doc_priv *doc = this->priv;
873 void __iomem *docptr = doc->virtadr;
875 /* Prime the ECC engine */
878 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
879 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
882 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
883 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
888 static void doc2001plus_enable_hwecc(struct mtd_info *mtd, int mode)
890 struct nand_chip *this = mtd->priv;
891 struct doc_priv *doc = this->priv;
892 void __iomem *docptr = doc->virtadr;
894 /* Prime the ECC engine */
897 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
898 WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
901 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
902 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
907 /* This code is only called on write */
908 static int doc200x_calculate_ecc(struct mtd_info *mtd, const u_char *dat, unsigned char *ecc_code)
910 struct nand_chip *this = mtd->priv;
911 struct doc_priv *doc = this->priv;
912 void __iomem *docptr = doc->virtadr;
916 /* flush the pipeline */
917 if (DoC_is_2000(doc)) {
918 WriteDOC(doc->CDSNControl & ~CDSN_CTRL_FLASH_IO, docptr, CDSNControl);
919 WriteDOC(0, docptr, 2k_CDSN_IO);
920 WriteDOC(0, docptr, 2k_CDSN_IO);
921 WriteDOC(0, docptr, 2k_CDSN_IO);
922 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
923 } else if (DoC_is_MillenniumPlus(doc)) {
924 WriteDOC(0, docptr, Mplus_NOP);
925 WriteDOC(0, docptr, Mplus_NOP);
926 WriteDOC(0, docptr, Mplus_NOP);
928 WriteDOC(0, docptr, NOP);
929 WriteDOC(0, docptr, NOP);
930 WriteDOC(0, docptr, NOP);
933 for (i = 0; i < 6; i++) {
934 if (DoC_is_MillenniumPlus(doc))
935 ecc_code[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
937 ecc_code[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
938 if (ecc_code[i] != empty_write_ecc[i])
941 if (DoC_is_MillenniumPlus(doc))
942 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
944 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
946 /* If emptymatch=1, we might have an all-0xff data buffer. Check. */
948 /* Note: this somewhat expensive test should not be triggered
949 often. It could be optimized away by examining the data in
950 the writebuf routine, and remembering the result. */
951 for (i = 0; i < 512; i++) {
958 /* If emptymatch still =1, we do have an all-0xff data buffer.
959 Return all-0xff ecc value instead of the computed one, so
960 it'll look just like a freshly-erased page. */
962 memset(ecc_code, 0xff, 6);
967 static int doc200x_correct_data(struct mtd_info *mtd, u_char *dat,
968 u_char *read_ecc, u_char *isnull)
971 struct nand_chip *this = mtd->priv;
972 struct doc_priv *doc = this->priv;
973 void __iomem *docptr = doc->virtadr;
975 volatile u_char dummy;
978 /* flush the pipeline */
979 if (DoC_is_2000(doc)) {
980 dummy = ReadDOC(docptr, 2k_ECCStatus);
981 dummy = ReadDOC(docptr, 2k_ECCStatus);
982 dummy = ReadDOC(docptr, 2k_ECCStatus);
983 } else if (DoC_is_MillenniumPlus(doc)) {
984 dummy = ReadDOC(docptr, Mplus_ECCConf);
985 dummy = ReadDOC(docptr, Mplus_ECCConf);
986 dummy = ReadDOC(docptr, Mplus_ECCConf);
988 dummy = ReadDOC(docptr, ECCConf);
989 dummy = ReadDOC(docptr, ECCConf);
990 dummy = ReadDOC(docptr, ECCConf);
993 /* Error occured ? */
995 for (i = 0; i < 6; i++) {
996 if (DoC_is_MillenniumPlus(doc))
997 calc_ecc[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
999 calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
1000 if (calc_ecc[i] != empty_read_syndrome[i])
1003 /* If emptymatch=1, the read syndrome is consistent with an
1004 all-0xff data and stored ecc block. Check the stored ecc. */
1006 for (i = 0; i < 6; i++) {
1007 if (read_ecc[i] == 0xff)
1013 /* If emptymatch still =1, check the data block. */
1015 /* Note: this somewhat expensive test should not be triggered
1016 often. It could be optimized away by examining the data in
1017 the readbuf routine, and remembering the result. */
1018 for (i = 0; i < 512; i++) {
1025 /* If emptymatch still =1, this is almost certainly a freshly-
1026 erased block, in which case the ECC will not come out right.
1027 We'll suppress the error and tell the caller everything's
1028 OK. Because it is. */
1030 ret = doc_ecc_decode(rs_decoder, dat, calc_ecc);
1032 printk(KERN_ERR "doc200x_correct_data corrected %d errors\n", ret);
1034 if (DoC_is_MillenniumPlus(doc))
1035 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
1037 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
1038 if (no_ecc_failures && (ret == -EBADMSG)) {
1039 printk(KERN_ERR "suppressing ECC failure\n");
1045 /*u_char mydatabuf[528]; */
1047 /* The strange out-of-order .oobfree list below is a (possibly unneeded)
1048 * attempt to retain compatibility. It used to read:
1049 * .oobfree = { {8, 8} }
1050 * Since that leaves two bytes unusable, it was changed. But the following
1051 * scheme might affect existing jffs2 installs by moving the cleanmarker:
1052 * .oobfree = { {6, 10} }
1053 * jffs2 seems to handle the above gracefully, but the current scheme seems
1054 * safer. The only problem with it is that any code that parses oobfree must
1055 * be able to handle out-of-order segments.
1057 static struct nand_ecclayout doc200x_oobinfo = {
1059 .eccpos = {0, 1, 2, 3, 4, 5},
1060 .oobfree = {{8, 8}, {6, 2}}
1063 /* Find the (I)NFTL Media Header, and optionally also the mirror media header.
1064 On sucessful return, buf will contain a copy of the media header for
1065 further processing. id is the string to scan for, and will presumably be
1066 either "ANAND" or "BNAND". If findmirror=1, also look for the mirror media
1067 header. The page #s of the found media headers are placed in mh0_page and
1068 mh1_page in the DOC private structure. */
1069 static int __init find_media_headers(struct mtd_info *mtd, u_char *buf, const char *id, int findmirror)
1071 struct nand_chip *this = mtd->priv;
1072 struct doc_priv *doc = this->priv;
1077 for (offs = 0; offs < mtd->size; offs += mtd->erasesize) {
1078 ret = mtd->read(mtd, offs, mtd->writesize, &retlen, buf);
1079 if (retlen != mtd->writesize)
1082 printk(KERN_WARNING "ECC error scanning DOC at 0x%x\n", offs);
1084 if (memcmp(buf, id, 6))
1086 printk(KERN_INFO "Found DiskOnChip %s Media Header at 0x%x\n", id, offs);
1087 if (doc->mh0_page == -1) {
1088 doc->mh0_page = offs >> this->page_shift;
1093 doc->mh1_page = offs >> this->page_shift;
1096 if (doc->mh0_page == -1) {
1097 printk(KERN_WARNING "DiskOnChip %s Media Header not found.\n", id);
1100 /* Only one mediaheader was found. We want buf to contain a
1101 mediaheader on return, so we'll have to re-read the one we found. */
1102 offs = doc->mh0_page << this->page_shift;
1103 ret = mtd->read(mtd, offs, mtd->writesize, &retlen, buf);
1104 if (retlen != mtd->writesize) {
1105 /* Insanity. Give up. */
1106 printk(KERN_ERR "Read DiskOnChip Media Header once, but can't reread it???\n");
1112 static inline int __init nftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
1114 struct nand_chip *this = mtd->priv;
1115 struct doc_priv *doc = this->priv;
1118 struct NFTLMediaHeader *mh;
1119 const unsigned psize = 1 << this->page_shift;
1121 unsigned blocks, maxblocks;
1122 int offs, numheaders;
1124 buf = kmalloc(mtd->writesize, GFP_KERNEL);
1126 printk(KERN_ERR "DiskOnChip mediaheader kmalloc failed!\n");
1129 if (!(numheaders = find_media_headers(mtd, buf, "ANAND", 1)))
1131 mh = (struct NFTLMediaHeader *)buf;
1133 le16_to_cpus(&mh->NumEraseUnits);
1134 le16_to_cpus(&mh->FirstPhysicalEUN);
1135 le32_to_cpus(&mh->FormattedSize);
1137 printk(KERN_INFO " DataOrgID = %s\n"
1138 " NumEraseUnits = %d\n"
1139 " FirstPhysicalEUN = %d\n"
1140 " FormattedSize = %d\n"
1141 " UnitSizeFactor = %d\n",
1142 mh->DataOrgID, mh->NumEraseUnits,
1143 mh->FirstPhysicalEUN, mh->FormattedSize,
1144 mh->UnitSizeFactor);
1146 blocks = mtd->size >> this->phys_erase_shift;
1147 maxblocks = min(32768U, mtd->erasesize - psize);
1149 if (mh->UnitSizeFactor == 0x00) {
1150 /* Auto-determine UnitSizeFactor. The constraints are:
1151 - There can be at most 32768 virtual blocks.
1152 - There can be at most (virtual block size - page size)
1153 virtual blocks (because MediaHeader+BBT must fit in 1).
1155 mh->UnitSizeFactor = 0xff;
1156 while (blocks > maxblocks) {
1158 maxblocks = min(32768U, (maxblocks << 1) + psize);
1159 mh->UnitSizeFactor--;
1161 printk(KERN_WARNING "UnitSizeFactor=0x00 detected. Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor);
1164 /* NOTE: The lines below modify internal variables of the NAND and MTD
1165 layers; variables with have already been configured by nand_scan.
1166 Unfortunately, we didn't know before this point what these values
1167 should be. Thus, this code is somewhat dependant on the exact
1168 implementation of the NAND layer. */
1169 if (mh->UnitSizeFactor != 0xff) {
1170 this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
1171 mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
1172 printk(KERN_INFO "Setting virtual erase size to %d\n", mtd->erasesize);
1173 blocks = mtd->size >> this->bbt_erase_shift;
1174 maxblocks = min(32768U, mtd->erasesize - psize);
1177 if (blocks > maxblocks) {
1178 printk(KERN_ERR "UnitSizeFactor of 0x%02x is inconsistent with device size. Aborting.\n", mh->UnitSizeFactor);
1182 /* Skip past the media headers. */
1183 offs = max(doc->mh0_page, doc->mh1_page);
1184 offs <<= this->page_shift;
1185 offs += mtd->erasesize;
1187 if (show_firmware_partition == 1) {
1188 parts[0].name = " DiskOnChip Firmware / Media Header partition";
1189 parts[0].offset = 0;
1190 parts[0].size = offs;
1194 parts[numparts].name = " DiskOnChip BDTL partition";
1195 parts[numparts].offset = offs;
1196 parts[numparts].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift;
1198 offs += parts[numparts].size;
1201 if (offs < mtd->size) {
1202 parts[numparts].name = " DiskOnChip Remainder partition";
1203 parts[numparts].offset = offs;
1204 parts[numparts].size = mtd->size - offs;
1214 /* This is a stripped-down copy of the code in inftlmount.c */
1215 static inline int __init inftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
1217 struct nand_chip *this = mtd->priv;
1218 struct doc_priv *doc = this->priv;
1221 struct INFTLMediaHeader *mh;
1222 struct INFTLPartition *ip;
1225 int vshift, lastvunit = 0;
1227 int end = mtd->size;
1229 if (inftl_bbt_write)
1230 end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift);
1232 buf = kmalloc(mtd->writesize, GFP_KERNEL);
1234 printk(KERN_ERR "DiskOnChip mediaheader kmalloc failed!\n");
1238 if (!find_media_headers(mtd, buf, "BNAND", 0))
1240 doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift);
1241 mh = (struct INFTLMediaHeader *)buf;
1243 le32_to_cpus(&mh->NoOfBootImageBlocks);
1244 le32_to_cpus(&mh->NoOfBinaryPartitions);
1245 le32_to_cpus(&mh->NoOfBDTLPartitions);
1246 le32_to_cpus(&mh->BlockMultiplierBits);
1247 le32_to_cpus(&mh->FormatFlags);
1248 le32_to_cpus(&mh->PercentUsed);
1250 printk(KERN_INFO " bootRecordID = %s\n"
1251 " NoOfBootImageBlocks = %d\n"
1252 " NoOfBinaryPartitions = %d\n"
1253 " NoOfBDTLPartitions = %d\n"
1254 " BlockMultiplerBits = %d\n"
1255 " FormatFlgs = %d\n"
1256 " OsakVersion = %d.%d.%d.%d\n"
1257 " PercentUsed = %d\n",
1258 mh->bootRecordID, mh->NoOfBootImageBlocks,
1259 mh->NoOfBinaryPartitions,
1260 mh->NoOfBDTLPartitions,
1261 mh->BlockMultiplierBits, mh->FormatFlags,
1262 ((unsigned char *) &mh->OsakVersion)[0] & 0xf,
1263 ((unsigned char *) &mh->OsakVersion)[1] & 0xf,
1264 ((unsigned char *) &mh->OsakVersion)[2] & 0xf,
1265 ((unsigned char *) &mh->OsakVersion)[3] & 0xf,
1268 vshift = this->phys_erase_shift + mh->BlockMultiplierBits;
1270 blocks = mtd->size >> vshift;
1271 if (blocks > 32768) {
1272 printk(KERN_ERR "BlockMultiplierBits=%d is inconsistent with device size. Aborting.\n", mh->BlockMultiplierBits);
1276 blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift);
1277 if (inftl_bbt_write && (blocks > mtd->erasesize)) {
1278 printk(KERN_ERR "Writeable BBTs spanning more than one erase block are not yet supported. FIX ME!\n");
1282 /* Scan the partitions */
1283 for (i = 0; (i < 4); i++) {
1284 ip = &(mh->Partitions[i]);
1285 le32_to_cpus(&ip->virtualUnits);
1286 le32_to_cpus(&ip->firstUnit);
1287 le32_to_cpus(&ip->lastUnit);
1288 le32_to_cpus(&ip->flags);
1289 le32_to_cpus(&ip->spareUnits);
1290 le32_to_cpus(&ip->Reserved0);
1292 printk(KERN_INFO " PARTITION[%d] ->\n"
1293 " virtualUnits = %d\n"
1297 " spareUnits = %d\n",
1298 i, ip->virtualUnits, ip->firstUnit,
1299 ip->lastUnit, ip->flags,
1302 if ((show_firmware_partition == 1) &&
1303 (i == 0) && (ip->firstUnit > 0)) {
1304 parts[0].name = " DiskOnChip IPL / Media Header partition";
1305 parts[0].offset = 0;
1306 parts[0].size = mtd->erasesize * ip->firstUnit;
1310 if (ip->flags & INFTL_BINARY)
1311 parts[numparts].name = " DiskOnChip BDK partition";
1313 parts[numparts].name = " DiskOnChip BDTL partition";
1314 parts[numparts].offset = ip->firstUnit << vshift;
1315 parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift;
1317 if (ip->lastUnit > lastvunit)
1318 lastvunit = ip->lastUnit;
1319 if (ip->flags & INFTL_LAST)
1323 if ((lastvunit << vshift) < end) {
1324 parts[numparts].name = " DiskOnChip Remainder partition";
1325 parts[numparts].offset = lastvunit << vshift;
1326 parts[numparts].size = end - parts[numparts].offset;
1335 static int __init nftl_scan_bbt(struct mtd_info *mtd)
1338 struct nand_chip *this = mtd->priv;
1339 struct doc_priv *doc = this->priv;
1340 struct mtd_partition parts[2];
1342 memset((char *)parts, 0, sizeof(parts));
1343 /* On NFTL, we have to find the media headers before we can read the
1344 BBTs, since they're stored in the media header eraseblocks. */
1345 numparts = nftl_partscan(mtd, parts);
1348 this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1349 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1351 this->bbt_td->veroffs = 7;
1352 this->bbt_td->pages[0] = doc->mh0_page + 1;
1353 if (doc->mh1_page != -1) {
1354 this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1355 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1357 this->bbt_md->veroffs = 7;
1358 this->bbt_md->pages[0] = doc->mh1_page + 1;
1360 this->bbt_md = NULL;
1363 /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
1364 At least as nand_bbt.c is currently written. */
1365 if ((ret = nand_scan_bbt(mtd, NULL)))
1367 add_mtd_device(mtd);
1368 #ifdef CONFIG_MTD_PARTITIONS
1370 add_mtd_partitions(mtd, parts, numparts);
1375 static int __init inftl_scan_bbt(struct mtd_info *mtd)
1378 struct nand_chip *this = mtd->priv;
1379 struct doc_priv *doc = this->priv;
1380 struct mtd_partition parts[5];
1382 if (this->numchips > doc->chips_per_floor) {
1383 printk(KERN_ERR "Multi-floor INFTL devices not yet supported.\n");
1387 if (DoC_is_MillenniumPlus(doc)) {
1388 this->bbt_td->options = NAND_BBT_2BIT | NAND_BBT_ABSPAGE;
1389 if (inftl_bbt_write)
1390 this->bbt_td->options |= NAND_BBT_WRITE;
1391 this->bbt_td->pages[0] = 2;
1392 this->bbt_md = NULL;
1394 this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
1395 if (inftl_bbt_write)
1396 this->bbt_td->options |= NAND_BBT_WRITE;
1397 this->bbt_td->offs = 8;
1398 this->bbt_td->len = 8;
1399 this->bbt_td->veroffs = 7;
1400 this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1401 this->bbt_td->reserved_block_code = 0x01;
1402 this->bbt_td->pattern = "MSYS_BBT";
1404 this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
1405 if (inftl_bbt_write)
1406 this->bbt_md->options |= NAND_BBT_WRITE;
1407 this->bbt_md->offs = 8;
1408 this->bbt_md->len = 8;
1409 this->bbt_md->veroffs = 7;
1410 this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1411 this->bbt_md->reserved_block_code = 0x01;
1412 this->bbt_md->pattern = "TBB_SYSM";
1415 /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
1416 At least as nand_bbt.c is currently written. */
1417 if ((ret = nand_scan_bbt(mtd, NULL)))
1419 memset((char *)parts, 0, sizeof(parts));
1420 numparts = inftl_partscan(mtd, parts);
1421 /* At least for now, require the INFTL Media Header. We could probably
1422 do without it for non-INFTL use, since all it gives us is
1423 autopartitioning, but I want to give it more thought. */
1426 add_mtd_device(mtd);
1427 #ifdef CONFIG_MTD_PARTITIONS
1429 add_mtd_partitions(mtd, parts, numparts);
1434 static inline int __init doc2000_init(struct mtd_info *mtd)
1436 struct nand_chip *this = mtd->priv;
1437 struct doc_priv *doc = this->priv;
1439 this->read_byte = doc2000_read_byte;
1440 this->write_buf = doc2000_writebuf;
1441 this->read_buf = doc2000_readbuf;
1442 this->verify_buf = doc2000_verifybuf;
1443 this->scan_bbt = nftl_scan_bbt;
1445 doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
1446 doc2000_count_chips(mtd);
1447 mtd->name = "DiskOnChip 2000 (NFTL Model)";
1448 return (4 * doc->chips_per_floor);
1451 static inline int __init doc2001_init(struct mtd_info *mtd)
1453 struct nand_chip *this = mtd->priv;
1454 struct doc_priv *doc = this->priv;
1456 this->read_byte = doc2001_read_byte;
1457 this->write_buf = doc2001_writebuf;
1458 this->read_buf = doc2001_readbuf;
1459 this->verify_buf = doc2001_verifybuf;
1461 ReadDOC(doc->virtadr, ChipID);
1462 ReadDOC(doc->virtadr, ChipID);
1463 ReadDOC(doc->virtadr, ChipID);
1464 if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) {
1465 /* It's not a Millennium; it's one of the newer
1466 DiskOnChip 2000 units with a similar ASIC.
1467 Treat it like a Millennium, except that it
1468 can have multiple chips. */
1469 doc2000_count_chips(mtd);
1470 mtd->name = "DiskOnChip 2000 (INFTL Model)";
1471 this->scan_bbt = inftl_scan_bbt;
1472 return (4 * doc->chips_per_floor);
1474 /* Bog-standard Millennium */
1475 doc->chips_per_floor = 1;
1476 mtd->name = "DiskOnChip Millennium";
1477 this->scan_bbt = nftl_scan_bbt;
1482 static inline int __init doc2001plus_init(struct mtd_info *mtd)
1484 struct nand_chip *this = mtd->priv;
1485 struct doc_priv *doc = this->priv;
1487 this->read_byte = doc2001plus_read_byte;
1488 this->write_buf = doc2001plus_writebuf;
1489 this->read_buf = doc2001plus_readbuf;
1490 this->verify_buf = doc2001plus_verifybuf;
1491 this->scan_bbt = inftl_scan_bbt;
1492 this->cmd_ctrl = NULL;
1493 this->select_chip = doc2001plus_select_chip;
1494 this->cmdfunc = doc2001plus_command;
1495 this->ecc.hwctl = doc2001plus_enable_hwecc;
1497 doc->chips_per_floor = 1;
1498 mtd->name = "DiskOnChip Millennium Plus";
1503 static int __init doc_probe(unsigned long physadr)
1505 unsigned char ChipID;
1506 struct mtd_info *mtd;
1507 struct nand_chip *nand;
1508 struct doc_priv *doc;
1509 void __iomem *virtadr;
1510 unsigned char save_control;
1511 unsigned char tmp, tmpb, tmpc;
1512 int reg, len, numchips;
1515 virtadr = ioremap(physadr, DOC_IOREMAP_LEN);
1517 printk(KERN_ERR "Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n", DOC_IOREMAP_LEN, physadr);
1521 /* It's not possible to cleanly detect the DiskOnChip - the
1522 * bootup procedure will put the device into reset mode, and
1523 * it's not possible to talk to it without actually writing
1524 * to the DOCControl register. So we store the current contents
1525 * of the DOCControl register's location, in case we later decide
1526 * that it's not a DiskOnChip, and want to put it back how we
1529 save_control = ReadDOC(virtadr, DOCControl);
1531 /* Reset the DiskOnChip ASIC */
1532 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
1533 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
1535 /* Enable the DiskOnChip ASIC */
1536 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
1537 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
1539 ChipID = ReadDOC(virtadr, ChipID);
1542 case DOC_ChipID_Doc2k:
1543 reg = DoC_2k_ECCStatus;
1545 case DOC_ChipID_DocMil:
1548 case DOC_ChipID_DocMilPlus16:
1549 case DOC_ChipID_DocMilPlus32:
1551 /* Possible Millennium Plus, need to do more checks */
1552 /* Possibly release from power down mode */
1553 for (tmp = 0; (tmp < 4); tmp++)
1554 ReadDOC(virtadr, Mplus_Power);
1556 /* Reset the Millennium Plus ASIC */
1557 tmp = DOC_MODE_RESET | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
1558 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1559 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1562 /* Enable the Millennium Plus ASIC */
1563 tmp = DOC_MODE_NORMAL | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
1564 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1565 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1568 ChipID = ReadDOC(virtadr, ChipID);
1571 case DOC_ChipID_DocMilPlus16:
1572 reg = DoC_Mplus_Toggle;
1574 case DOC_ChipID_DocMilPlus32:
1575 printk(KERN_ERR "DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
1586 /* Check the TOGGLE bit in the ECC register */
1587 tmp = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1588 tmpb = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1589 tmpc = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1590 if ((tmp == tmpb) || (tmp != tmpc)) {
1591 printk(KERN_WARNING "Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr);
1596 for (mtd = doclist; mtd; mtd = doc->nextdoc) {
1597 unsigned char oldval;
1598 unsigned char newval;
1601 /* Use the alias resolution register to determine if this is
1602 in fact the same DOC aliased to a new address. If writes
1603 to one chip's alias resolution register change the value on
1604 the other chip, they're the same chip. */
1605 if (ChipID == DOC_ChipID_DocMilPlus16) {
1606 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1607 newval = ReadDOC(virtadr, Mplus_AliasResolution);
1609 oldval = ReadDOC(doc->virtadr, AliasResolution);
1610 newval = ReadDOC(virtadr, AliasResolution);
1612 if (oldval != newval)
1614 if (ChipID == DOC_ChipID_DocMilPlus16) {
1615 WriteDOC(~newval, virtadr, Mplus_AliasResolution);
1616 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1617 WriteDOC(newval, virtadr, Mplus_AliasResolution); /* restore it */
1619 WriteDOC(~newval, virtadr, AliasResolution);
1620 oldval = ReadDOC(doc->virtadr, AliasResolution);
1621 WriteDOC(newval, virtadr, AliasResolution); /* restore it */
1624 if (oldval == newval) {
1625 printk(KERN_DEBUG "Found alias of DOC at 0x%lx to 0x%lx\n", doc->physadr, physadr);
1630 printk(KERN_NOTICE "DiskOnChip found at 0x%lx\n", physadr);
1632 len = sizeof(struct mtd_info) +
1633 sizeof(struct nand_chip) + sizeof(struct doc_priv) + (2 * sizeof(struct nand_bbt_descr));
1634 mtd = kzalloc(len, GFP_KERNEL);
1636 printk(KERN_ERR "DiskOnChip kmalloc (%d bytes) failed!\n", len);
1641 nand = (struct nand_chip *) (mtd + 1);
1642 doc = (struct doc_priv *) (nand + 1);
1643 nand->bbt_td = (struct nand_bbt_descr *) (doc + 1);
1644 nand->bbt_md = nand->bbt_td + 1;
1647 mtd->owner = THIS_MODULE;
1650 nand->select_chip = doc200x_select_chip;
1651 nand->cmd_ctrl = doc200x_hwcontrol;
1652 nand->dev_ready = doc200x_dev_ready;
1653 nand->waitfunc = doc200x_wait;
1654 nand->block_bad = doc200x_block_bad;
1655 nand->ecc.hwctl = doc200x_enable_hwecc;
1656 nand->ecc.calculate = doc200x_calculate_ecc;
1657 nand->ecc.correct = doc200x_correct_data;
1659 nand->ecc.layout = &doc200x_oobinfo;
1660 nand->ecc.mode = NAND_ECC_HW_SYNDROME;
1661 nand->ecc.size = 512;
1662 nand->ecc.bytes = 6;
1663 nand->options = NAND_USE_FLASH_BBT;
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);
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) {
1716 nextmtd = doc->nextdoc;
1718 iounmap(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");