Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / mtd / nand / diskonchip.c
blob02135c3ac29a0372f19a05784cb7da8e90f38245
1 /*
2 * drivers/mtd/nand/diskonchip.c
4 * (C) 2003 Red Hat, Inc.
5 * (C) 2004 Dan Brown <dan_brown@ieee.org>
6 * (C) 2004 Kalev Lember <kalev@smartlink.ee>
8 * Author: David Woodhouse <dwmw2@infradead.org>
9 * Additional Diskonchip 2000 and Millennium support by Dan Brown <dan_brown@ieee.org>
10 * Diskonchip Millennium Plus support by Kalev Lember <kalev@smartlink.ee>
12 * Error correction code lifted from the old docecc code
13 * Author: Fabrice Bellard (fabrice.bellard@netgem.com)
14 * Copyright (C) 2000 Netgem S.A.
15 * converted to the generic Reed-Solomon library by Thomas Gleixner <tglx@linutronix.de>
17 * Interface to generic NAND code for M-Systems DiskOnChip devices
19 * $Id: diskonchip.c,v 1.45 2005/01/05 18:05:14 dwmw2 Exp $
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>
27 #include <linux/moduleparam.h>
28 #include <asm/io.h>
30 #include <linux/mtd/mtd.h>
31 #include <linux/mtd/nand.h>
32 #include <linux/mtd/doc2000.h>
33 #include <linux/mtd/compatmac.h>
34 #include <linux/mtd/partitions.h>
35 #include <linux/mtd/inftl.h>
37 /* Where to look for the devices? */
38 #ifndef CONFIG_MTD_DISKONCHIP_PROBE_ADDRESS
39 #define CONFIG_MTD_DISKONCHIP_PROBE_ADDRESS 0
40 #endif
42 static unsigned long __initdata doc_locations[] = {
43 #if defined (__alpha__) || defined(__i386__) || defined(__x86_64__)
44 #ifdef CONFIG_MTD_DISKONCHIP_PROBE_HIGH
45 0xfffc8000, 0xfffca000, 0xfffcc000, 0xfffce000,
46 0xfffd0000, 0xfffd2000, 0xfffd4000, 0xfffd6000,
47 0xfffd8000, 0xfffda000, 0xfffdc000, 0xfffde000,
48 0xfffe0000, 0xfffe2000, 0xfffe4000, 0xfffe6000,
49 0xfffe8000, 0xfffea000, 0xfffec000, 0xfffee000,
50 #else /* CONFIG_MTD_DOCPROBE_HIGH */
51 0xc8000, 0xca000, 0xcc000, 0xce000,
52 0xd0000, 0xd2000, 0xd4000, 0xd6000,
53 0xd8000, 0xda000, 0xdc000, 0xde000,
54 0xe0000, 0xe2000, 0xe4000, 0xe6000,
55 0xe8000, 0xea000, 0xec000, 0xee000,
56 #endif /* CONFIG_MTD_DOCPROBE_HIGH */
57 #elif defined(__PPC__)
58 0xe4000000,
59 #elif defined(CONFIG_MOMENCO_OCELOT)
60 0x2f000000,
61 0xff000000,
62 #elif defined(CONFIG_MOMENCO_OCELOT_G) || defined (CONFIG_MOMENCO_OCELOT_C)
63 0xff000000,
64 ##else
65 #warning Unknown architecture for DiskOnChip. No default probe locations defined
66 #endif
67 0xffffffff };
69 static struct mtd_info *doclist = NULL;
71 struct doc_priv {
72 void __iomem *virtadr;
73 unsigned long physadr;
74 u_char ChipID;
75 u_char CDSNControl;
76 int chips_per_floor; /* The number of chips detected on each floor */
77 int curfloor;
78 int curchip;
79 int mh0_page;
80 int mh1_page;
81 struct mtd_info *nextdoc;
84 /* Max number of eraseblocks to scan (from start of device) for the (I)NFTL
85 MediaHeader. The spec says to just keep going, I think, but that's just
86 silly. */
87 #define MAX_MEDIAHEADER_SCAN 8
89 /* This is the syndrome computed by the HW ecc generator upon reading an empty
90 page, one with all 0xff for data and stored ecc code. */
91 static u_char empty_read_syndrome[6] = { 0x26, 0xff, 0x6d, 0x47, 0x73, 0x7a };
92 /* This is the ecc value computed by the HW ecc generator upon writing an empty
93 page, one with all 0xff for data. */
94 static u_char empty_write_ecc[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
96 #define INFTL_BBT_RESERVED_BLOCKS 4
98 #define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
99 #define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
100 #define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
102 static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd);
103 static void doc200x_select_chip(struct mtd_info *mtd, int chip);
105 static int debug=0;
106 module_param(debug, int, 0);
108 static int try_dword=1;
109 module_param(try_dword, int, 0);
111 static int no_ecc_failures=0;
112 module_param(no_ecc_failures, int, 0);
114 #ifdef CONFIG_MTD_PARTITIONS
115 static int no_autopart=0;
116 module_param(no_autopart, int, 0);
117 #endif
119 #ifdef MTD_NAND_DISKONCHIP_BBTWRITE
120 static int inftl_bbt_write=1;
121 #else
122 static int inftl_bbt_write=0;
123 #endif
124 module_param(inftl_bbt_write, int, 0);
126 static unsigned long doc_config_location = CONFIG_MTD_DISKONCHIP_PROBE_ADDRESS;
127 module_param(doc_config_location, ulong, 0);
128 MODULE_PARM_DESC(doc_config_location, "Physical memory address at which to probe for DiskOnChip");
131 /* Sector size for HW ECC */
132 #define SECTOR_SIZE 512
133 /* The sector bytes are packed into NB_DATA 10 bit words */
134 #define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / 10)
135 /* Number of roots */
136 #define NROOTS 4
137 /* First consective root */
138 #define FCR 510
139 /* Number of symbols */
140 #define NN 1023
142 /* the Reed Solomon control structure */
143 static struct rs_control *rs_decoder;
146 * The HW decoder in the DoC ASIC's provides us a error syndrome,
147 * which we must convert to a standard syndrom usable by the generic
148 * Reed-Solomon library code.
150 * Fabrice Bellard figured this out in the old docecc code. I added
151 * some comments, improved a minor bit and converted it to make use
152 * of the generic Reed-Solomon libary. tglx
154 static int doc_ecc_decode (struct rs_control *rs, uint8_t *data, uint8_t *ecc)
156 int i, j, nerr, errpos[8];
157 uint8_t parity;
158 uint16_t ds[4], s[5], tmp, errval[8], syn[4];
160 /* Convert the ecc bytes into words */
161 ds[0] = ((ecc[4] & 0xff) >> 0) | ((ecc[5] & 0x03) << 8);
162 ds[1] = ((ecc[5] & 0xfc) >> 2) | ((ecc[2] & 0x0f) << 6);
163 ds[2] = ((ecc[2] & 0xf0) >> 4) | ((ecc[3] & 0x3f) << 4);
164 ds[3] = ((ecc[3] & 0xc0) >> 6) | ((ecc[0] & 0xff) << 2);
165 parity = ecc[1];
167 /* Initialize the syndrom buffer */
168 for (i = 0; i < NROOTS; i++)
169 s[i] = ds[0];
171 * Evaluate
172 * s[i] = ds[3]x^3 + ds[2]x^2 + ds[1]x^1 + ds[0]
173 * where x = alpha^(FCR + i)
175 for(j = 1; j < NROOTS; j++) {
176 if(ds[j] == 0)
177 continue;
178 tmp = rs->index_of[ds[j]];
179 for(i = 0; i < NROOTS; i++)
180 s[i] ^= rs->alpha_to[rs_modnn(rs, tmp + (FCR + i) * j)];
183 /* Calc s[i] = s[i] / alpha^(v + i) */
184 for (i = 0; i < NROOTS; i++) {
185 if (syn[i])
186 syn[i] = rs_modnn(rs, rs->index_of[s[i]] + (NN - FCR - i));
188 /* Call the decoder library */
189 nerr = decode_rs16(rs, NULL, NULL, 1019, syn, 0, errpos, 0, errval);
191 /* Incorrectable errors ? */
192 if (nerr < 0)
193 return nerr;
196 * Correct the errors. The bitpositions are a bit of magic,
197 * but they are given by the design of the de/encoder circuit
198 * in the DoC ASIC's.
200 for(i = 0;i < nerr; i++) {
201 int index, bitpos, pos = 1015 - errpos[i];
202 uint8_t val;
203 if (pos >= NB_DATA && pos < 1019)
204 continue;
205 if (pos < NB_DATA) {
206 /* extract bit position (MSB first) */
207 pos = 10 * (NB_DATA - 1 - pos) - 6;
208 /* now correct the following 10 bits. At most two bytes
209 can be modified since pos is even */
210 index = (pos >> 3) ^ 1;
211 bitpos = pos & 7;
212 if ((index >= 0 && index < SECTOR_SIZE) ||
213 index == (SECTOR_SIZE + 1)) {
214 val = (uint8_t) (errval[i] >> (2 + bitpos));
215 parity ^= val;
216 if (index < SECTOR_SIZE)
217 data[index] ^= val;
219 index = ((pos >> 3) + 1) ^ 1;
220 bitpos = (bitpos + 10) & 7;
221 if (bitpos == 0)
222 bitpos = 8;
223 if ((index >= 0 && index < SECTOR_SIZE) ||
224 index == (SECTOR_SIZE + 1)) {
225 val = (uint8_t)(errval[i] << (8 - bitpos));
226 parity ^= val;
227 if (index < SECTOR_SIZE)
228 data[index] ^= val;
232 /* If the parity is wrong, no rescue possible */
233 return parity ? -1 : nerr;
236 static void DoC_Delay(struct doc_priv *doc, unsigned short cycles)
238 volatile char dummy;
239 int i;
241 for (i = 0; i < cycles; i++) {
242 if (DoC_is_Millennium(doc))
243 dummy = ReadDOC(doc->virtadr, NOP);
244 else if (DoC_is_MillenniumPlus(doc))
245 dummy = ReadDOC(doc->virtadr, Mplus_NOP);
246 else
247 dummy = ReadDOC(doc->virtadr, DOCStatus);
252 #define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
254 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
255 static int _DoC_WaitReady(struct doc_priv *doc)
257 void __iomem *docptr = doc->virtadr;
258 unsigned long timeo = jiffies + (HZ * 10);
260 if(debug) printk("_DoC_WaitReady...\n");
261 /* Out-of-line routine to wait for chip response */
262 if (DoC_is_MillenniumPlus(doc)) {
263 while ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
264 if (time_after(jiffies, timeo)) {
265 printk("_DoC_WaitReady timed out.\n");
266 return -EIO;
268 udelay(1);
269 cond_resched();
271 } else {
272 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
273 if (time_after(jiffies, timeo)) {
274 printk("_DoC_WaitReady timed out.\n");
275 return -EIO;
277 udelay(1);
278 cond_resched();
282 return 0;
285 static inline int DoC_WaitReady(struct doc_priv *doc)
287 void __iomem *docptr = doc->virtadr;
288 int ret = 0;
290 if (DoC_is_MillenniumPlus(doc)) {
291 DoC_Delay(doc, 4);
293 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
294 /* Call the out-of-line routine to wait */
295 ret = _DoC_WaitReady(doc);
296 } else {
297 DoC_Delay(doc, 4);
299 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
300 /* Call the out-of-line routine to wait */
301 ret = _DoC_WaitReady(doc);
302 DoC_Delay(doc, 2);
305 if(debug) printk("DoC_WaitReady OK\n");
306 return ret;
309 static void doc2000_write_byte(struct mtd_info *mtd, u_char datum)
311 struct nand_chip *this = mtd->priv;
312 struct doc_priv *doc = this->priv;
313 void __iomem *docptr = doc->virtadr;
315 if(debug)printk("write_byte %02x\n", datum);
316 WriteDOC(datum, docptr, CDSNSlowIO);
317 WriteDOC(datum, docptr, 2k_CDSN_IO);
320 static u_char doc2000_read_byte(struct mtd_info *mtd)
322 struct nand_chip *this = mtd->priv;
323 struct doc_priv *doc = this->priv;
324 void __iomem *docptr = doc->virtadr;
325 u_char ret;
327 ReadDOC(docptr, CDSNSlowIO);
328 DoC_Delay(doc, 2);
329 ret = ReadDOC(docptr, 2k_CDSN_IO);
330 if (debug) printk("read_byte returns %02x\n", ret);
331 return ret;
334 static void doc2000_writebuf(struct mtd_info *mtd,
335 const u_char *buf, int len)
337 struct nand_chip *this = mtd->priv;
338 struct doc_priv *doc = this->priv;
339 void __iomem *docptr = doc->virtadr;
340 int i;
341 if (debug)printk("writebuf of %d bytes: ", len);
342 for (i=0; i < len; i++) {
343 WriteDOC_(buf[i], docptr, DoC_2k_CDSN_IO + i);
344 if (debug && i < 16)
345 printk("%02x ", buf[i]);
347 if (debug) printk("\n");
350 static void doc2000_readbuf(struct mtd_info *mtd,
351 u_char *buf, int len)
353 struct nand_chip *this = mtd->priv;
354 struct doc_priv *doc = this->priv;
355 void __iomem *docptr = doc->virtadr;
356 int i;
358 if (debug)printk("readbuf of %d bytes: ", len);
360 for (i=0; i < len; i++) {
361 buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
365 static void doc2000_readbuf_dword(struct mtd_info *mtd,
366 u_char *buf, int len)
368 struct nand_chip *this = mtd->priv;
369 struct doc_priv *doc = this->priv;
370 void __iomem *docptr = doc->virtadr;
371 int i;
373 if (debug) printk("readbuf_dword of %d bytes: ", len);
375 if (unlikely((((unsigned long)buf)|len) & 3)) {
376 for (i=0; i < len; i++) {
377 *(uint8_t *)(&buf[i]) = ReadDOC(docptr, 2k_CDSN_IO + i);
379 } else {
380 for (i=0; i < len; i+=4) {
381 *(uint32_t*)(&buf[i]) = readl(docptr + DoC_2k_CDSN_IO + i);
386 static int doc2000_verifybuf(struct mtd_info *mtd,
387 const u_char *buf, int len)
389 struct nand_chip *this = mtd->priv;
390 struct doc_priv *doc = this->priv;
391 void __iomem *docptr = doc->virtadr;
392 int i;
394 for (i=0; i < len; i++)
395 if (buf[i] != ReadDOC(docptr, 2k_CDSN_IO))
396 return -EFAULT;
397 return 0;
400 static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr)
402 struct nand_chip *this = mtd->priv;
403 struct doc_priv *doc = this->priv;
404 uint16_t ret;
406 doc200x_select_chip(mtd, nr);
407 doc200x_hwcontrol(mtd, NAND_CTL_SETCLE);
408 this->write_byte(mtd, NAND_CMD_READID);
409 doc200x_hwcontrol(mtd, NAND_CTL_CLRCLE);
410 doc200x_hwcontrol(mtd, NAND_CTL_SETALE);
411 this->write_byte(mtd, 0);
412 doc200x_hwcontrol(mtd, NAND_CTL_CLRALE);
414 ret = this->read_byte(mtd) << 8;
415 ret |= this->read_byte(mtd);
417 if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) {
418 /* First chip probe. See if we get same results by 32-bit access */
419 union {
420 uint32_t dword;
421 uint8_t byte[4];
422 } ident;
423 void __iomem *docptr = doc->virtadr;
425 doc200x_hwcontrol(mtd, NAND_CTL_SETCLE);
426 doc2000_write_byte(mtd, NAND_CMD_READID);
427 doc200x_hwcontrol(mtd, NAND_CTL_CLRCLE);
428 doc200x_hwcontrol(mtd, NAND_CTL_SETALE);
429 doc2000_write_byte(mtd, 0);
430 doc200x_hwcontrol(mtd, NAND_CTL_CLRALE);
432 ident.dword = readl(docptr + DoC_2k_CDSN_IO);
433 if (((ident.byte[0] << 8) | ident.byte[1]) == ret) {
434 printk(KERN_INFO "DiskOnChip 2000 responds to DWORD access\n");
435 this->read_buf = &doc2000_readbuf_dword;
439 return ret;
442 static void __init doc2000_count_chips(struct mtd_info *mtd)
444 struct nand_chip *this = mtd->priv;
445 struct doc_priv *doc = this->priv;
446 uint16_t mfrid;
447 int i;
449 /* Max 4 chips per floor on DiskOnChip 2000 */
450 doc->chips_per_floor = 4;
452 /* Find out what the first chip is */
453 mfrid = doc200x_ident_chip(mtd, 0);
455 /* Find how many chips in each floor. */
456 for (i = 1; i < 4; i++) {
457 if (doc200x_ident_chip(mtd, i) != mfrid)
458 break;
460 doc->chips_per_floor = i;
461 printk(KERN_DEBUG "Detected %d chips per floor.\n", i);
464 static int doc200x_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
466 struct doc_priv *doc = this->priv;
468 int status;
470 DoC_WaitReady(doc);
471 this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
472 DoC_WaitReady(doc);
473 status = (int)this->read_byte(mtd);
475 return status;
478 static void doc2001_write_byte(struct mtd_info *mtd, u_char datum)
480 struct nand_chip *this = mtd->priv;
481 struct doc_priv *doc = this->priv;
482 void __iomem *docptr = doc->virtadr;
484 WriteDOC(datum, docptr, CDSNSlowIO);
485 WriteDOC(datum, docptr, Mil_CDSN_IO);
486 WriteDOC(datum, docptr, WritePipeTerm);
489 static u_char doc2001_read_byte(struct mtd_info *mtd)
491 struct nand_chip *this = mtd->priv;
492 struct doc_priv *doc = this->priv;
493 void __iomem *docptr = doc->virtadr;
495 //ReadDOC(docptr, CDSNSlowIO);
496 /* 11.4.5 -- delay twice to allow extended length cycle */
497 DoC_Delay(doc, 2);
498 ReadDOC(docptr, ReadPipeInit);
499 //return ReadDOC(docptr, Mil_CDSN_IO);
500 return ReadDOC(docptr, LastDataRead);
503 static void doc2001_writebuf(struct mtd_info *mtd,
504 const u_char *buf, int len)
506 struct nand_chip *this = mtd->priv;
507 struct doc_priv *doc = this->priv;
508 void __iomem *docptr = doc->virtadr;
509 int i;
511 for (i=0; i < len; i++)
512 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
513 /* Terminate write pipeline */
514 WriteDOC(0x00, docptr, WritePipeTerm);
517 static void doc2001_readbuf(struct mtd_info *mtd,
518 u_char *buf, int len)
520 struct nand_chip *this = mtd->priv;
521 struct doc_priv *doc = this->priv;
522 void __iomem *docptr = doc->virtadr;
523 int i;
525 /* Start read pipeline */
526 ReadDOC(docptr, ReadPipeInit);
528 for (i=0; i < len-1; i++)
529 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
531 /* Terminate read pipeline */
532 buf[i] = ReadDOC(docptr, LastDataRead);
535 static int doc2001_verifybuf(struct mtd_info *mtd,
536 const u_char *buf, int len)
538 struct nand_chip *this = mtd->priv;
539 struct doc_priv *doc = this->priv;
540 void __iomem *docptr = doc->virtadr;
541 int i;
543 /* Start read pipeline */
544 ReadDOC(docptr, ReadPipeInit);
546 for (i=0; i < len-1; i++)
547 if (buf[i] != ReadDOC(docptr, Mil_CDSN_IO)) {
548 ReadDOC(docptr, LastDataRead);
549 return i;
551 if (buf[i] != ReadDOC(docptr, LastDataRead))
552 return i;
553 return 0;
556 static u_char doc2001plus_read_byte(struct mtd_info *mtd)
558 struct nand_chip *this = mtd->priv;
559 struct doc_priv *doc = this->priv;
560 void __iomem *docptr = doc->virtadr;
561 u_char ret;
563 ReadDOC(docptr, Mplus_ReadPipeInit);
564 ReadDOC(docptr, Mplus_ReadPipeInit);
565 ret = ReadDOC(docptr, Mplus_LastDataRead);
566 if (debug) printk("read_byte returns %02x\n", ret);
567 return ret;
570 static void doc2001plus_writebuf(struct mtd_info *mtd,
571 const u_char *buf, int len)
573 struct nand_chip *this = mtd->priv;
574 struct doc_priv *doc = this->priv;
575 void __iomem *docptr = doc->virtadr;
576 int i;
578 if (debug)printk("writebuf of %d bytes: ", len);
579 for (i=0; i < len; i++) {
580 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
581 if (debug && i < 16)
582 printk("%02x ", buf[i]);
584 if (debug) printk("\n");
587 static void doc2001plus_readbuf(struct mtd_info *mtd,
588 u_char *buf, int len)
590 struct nand_chip *this = mtd->priv;
591 struct doc_priv *doc = this->priv;
592 void __iomem *docptr = doc->virtadr;
593 int i;
595 if (debug)printk("readbuf of %d bytes: ", len);
597 /* Start read pipeline */
598 ReadDOC(docptr, Mplus_ReadPipeInit);
599 ReadDOC(docptr, Mplus_ReadPipeInit);
601 for (i=0; i < len-2; i++) {
602 buf[i] = ReadDOC(docptr, Mil_CDSN_IO);
603 if (debug && i < 16)
604 printk("%02x ", buf[i]);
607 /* Terminate read pipeline */
608 buf[len-2] = ReadDOC(docptr, Mplus_LastDataRead);
609 if (debug && i < 16)
610 printk("%02x ", buf[len-2]);
611 buf[len-1] = ReadDOC(docptr, Mplus_LastDataRead);
612 if (debug && i < 16)
613 printk("%02x ", buf[len-1]);
614 if (debug) printk("\n");
617 static int doc2001plus_verifybuf(struct mtd_info *mtd,
618 const u_char *buf, int len)
620 struct nand_chip *this = mtd->priv;
621 struct doc_priv *doc = this->priv;
622 void __iomem *docptr = doc->virtadr;
623 int i;
625 if (debug)printk("verifybuf of %d bytes: ", len);
627 /* Start read pipeline */
628 ReadDOC(docptr, Mplus_ReadPipeInit);
629 ReadDOC(docptr, Mplus_ReadPipeInit);
631 for (i=0; i < len-2; i++)
632 if (buf[i] != ReadDOC(docptr, Mil_CDSN_IO)) {
633 ReadDOC(docptr, Mplus_LastDataRead);
634 ReadDOC(docptr, Mplus_LastDataRead);
635 return i;
637 if (buf[len-2] != ReadDOC(docptr, Mplus_LastDataRead))
638 return len-2;
639 if (buf[len-1] != ReadDOC(docptr, Mplus_LastDataRead))
640 return len-1;
641 return 0;
644 static void doc2001plus_select_chip(struct mtd_info *mtd, int chip)
646 struct nand_chip *this = mtd->priv;
647 struct doc_priv *doc = this->priv;
648 void __iomem *docptr = doc->virtadr;
649 int floor = 0;
651 if(debug)printk("select chip (%d)\n", chip);
653 if (chip == -1) {
654 /* Disable flash internally */
655 WriteDOC(0, docptr, Mplus_FlashSelect);
656 return;
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);
666 doc->curchip = chip;
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;
675 int floor = 0;
677 if(debug)printk("select chip (%d)\n", chip);
679 if (chip == -1)
680 return;
682 floor = chip / doc->chips_per_floor;
683 chip -= (floor * doc->chips_per_floor);
685 /* 11.4.4 -- deassert CE before changing chip */
686 doc200x_hwcontrol(mtd, NAND_CTL_CLRNCE);
688 WriteDOC(floor, docptr, FloorSelect);
689 WriteDOC(chip, docptr, CDSNDeviceSelect);
691 doc200x_hwcontrol(mtd, NAND_CTL_SETNCE);
693 doc->curchip = chip;
694 doc->curfloor = floor;
697 static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd)
699 struct nand_chip *this = mtd->priv;
700 struct doc_priv *doc = this->priv;
701 void __iomem *docptr = doc->virtadr;
703 switch(cmd) {
704 case NAND_CTL_SETNCE:
705 doc->CDSNControl |= CDSN_CTRL_CE;
706 break;
707 case NAND_CTL_CLRNCE:
708 doc->CDSNControl &= ~CDSN_CTRL_CE;
709 break;
710 case NAND_CTL_SETCLE:
711 doc->CDSNControl |= CDSN_CTRL_CLE;
712 break;
713 case NAND_CTL_CLRCLE:
714 doc->CDSNControl &= ~CDSN_CTRL_CLE;
715 break;
716 case NAND_CTL_SETALE:
717 doc->CDSNControl |= CDSN_CTRL_ALE;
718 break;
719 case NAND_CTL_CLRALE:
720 doc->CDSNControl &= ~CDSN_CTRL_ALE;
721 break;
722 case NAND_CTL_SETWP:
723 doc->CDSNControl |= CDSN_CTRL_WP;
724 break;
725 case NAND_CTL_CLRWP:
726 doc->CDSNControl &= ~CDSN_CTRL_WP;
727 break;
729 if (debug)printk("hwcontrol(%d): %02x\n", cmd, doc->CDSNControl);
730 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
731 /* 11.4.3 -- 4 NOPs after CSDNControl write */
732 DoC_Delay(doc, 4);
735 static void doc2001plus_command (struct mtd_info *mtd, unsigned command, int column, int page_addr)
737 struct nand_chip *this = mtd->priv;
738 struct doc_priv *doc = this->priv;
739 void __iomem *docptr = doc->virtadr;
742 * Must terminate write pipeline before sending any commands
743 * to the device.
745 if (command == NAND_CMD_PAGEPROG) {
746 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
747 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
751 * Write out the command to the device.
753 if (command == NAND_CMD_SEQIN) {
754 int readcmd;
756 if (column >= mtd->oobblock) {
757 /* OOB area */
758 column -= mtd->oobblock;
759 readcmd = NAND_CMD_READOOB;
760 } else if (column < 256) {
761 /* First 256 bytes --> READ0 */
762 readcmd = NAND_CMD_READ0;
763 } else {
764 column -= 256;
765 readcmd = NAND_CMD_READ1;
767 WriteDOC(readcmd, docptr, Mplus_FlashCmd);
769 WriteDOC(command, docptr, Mplus_FlashCmd);
770 WriteDOC(0, docptr, Mplus_WritePipeTerm);
771 WriteDOC(0, docptr, Mplus_WritePipeTerm);
773 if (column != -1 || page_addr != -1) {
774 /* Serially input address */
775 if (column != -1) {
776 /* Adjust columns for 16 bit buswidth */
777 if (this->options & NAND_BUSWIDTH_16)
778 column >>= 1;
779 WriteDOC(column, docptr, Mplus_FlashAddress);
781 if (page_addr != -1) {
782 WriteDOC((unsigned char) (page_addr & 0xff), docptr, Mplus_FlashAddress);
783 WriteDOC((unsigned char) ((page_addr >> 8) & 0xff), docptr, Mplus_FlashAddress);
784 /* One more address cycle for higher density devices */
785 if (this->chipsize & 0x0c000000) {
786 WriteDOC((unsigned char) ((page_addr >> 16) & 0x0f), docptr, Mplus_FlashAddress);
787 printk("high density\n");
790 WriteDOC(0, docptr, Mplus_WritePipeTerm);
791 WriteDOC(0, docptr, Mplus_WritePipeTerm);
792 /* deassert ALE */
793 if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 || command == NAND_CMD_READOOB || command == NAND_CMD_READID)
794 WriteDOC(0, docptr, Mplus_FlashControl);
798 * program and erase have their own busy handlers
799 * status and sequential in needs no delay
801 switch (command) {
803 case NAND_CMD_PAGEPROG:
804 case NAND_CMD_ERASE1:
805 case NAND_CMD_ERASE2:
806 case NAND_CMD_SEQIN:
807 case NAND_CMD_STATUS:
808 return;
810 case NAND_CMD_RESET:
811 if (this->dev_ready)
812 break;
813 udelay(this->chip_delay);
814 WriteDOC(NAND_CMD_STATUS, docptr, Mplus_FlashCmd);
815 WriteDOC(0, docptr, Mplus_WritePipeTerm);
816 WriteDOC(0, docptr, Mplus_WritePipeTerm);
817 while ( !(this->read_byte(mtd) & 0x40));
818 return;
820 /* This applies to read commands */
821 default:
823 * If we don't have access to the busy pin, we apply the given
824 * command delay
826 if (!this->dev_ready) {
827 udelay (this->chip_delay);
828 return;
832 /* Apply this short delay always to ensure that we do wait tWB in
833 * any case on any machine. */
834 ndelay (100);
835 /* wait until command is processed */
836 while (!this->dev_ready(mtd));
839 static int doc200x_dev_ready(struct mtd_info *mtd)
841 struct nand_chip *this = mtd->priv;
842 struct doc_priv *doc = this->priv;
843 void __iomem *docptr = doc->virtadr;
845 if (DoC_is_MillenniumPlus(doc)) {
846 /* 11.4.2 -- must NOP four times before checking FR/B# */
847 DoC_Delay(doc, 4);
848 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
849 if(debug)
850 printk("not ready\n");
851 return 0;
853 if (debug)printk("was ready\n");
854 return 1;
855 } else {
856 /* 11.4.2 -- must NOP four times before checking FR/B# */
857 DoC_Delay(doc, 4);
858 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
859 if(debug)
860 printk("not ready\n");
861 return 0;
863 /* 11.4.2 -- Must NOP twice if it's ready */
864 DoC_Delay(doc, 2);
865 if (debug)printk("was ready\n");
866 return 1;
870 static int doc200x_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
872 /* This is our last resort if we couldn't find or create a BBT. Just
873 pretend all blocks are good. */
874 return 0;
877 static void doc200x_enable_hwecc(struct mtd_info *mtd, int mode)
879 struct nand_chip *this = mtd->priv;
880 struct doc_priv *doc = this->priv;
881 void __iomem *docptr = doc->virtadr;
883 /* Prime the ECC engine */
884 switch(mode) {
885 case NAND_ECC_READ:
886 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
887 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
888 break;
889 case NAND_ECC_WRITE:
890 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
891 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
892 break;
896 static void doc2001plus_enable_hwecc(struct mtd_info *mtd, int mode)
898 struct nand_chip *this = mtd->priv;
899 struct doc_priv *doc = this->priv;
900 void __iomem *docptr = doc->virtadr;
902 /* Prime the ECC engine */
903 switch(mode) {
904 case NAND_ECC_READ:
905 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
906 WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
907 break;
908 case NAND_ECC_WRITE:
909 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
910 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
911 break;
915 /* This code is only called on write */
916 static int doc200x_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
917 unsigned char *ecc_code)
919 struct nand_chip *this = mtd->priv;
920 struct doc_priv *doc = this->priv;
921 void __iomem *docptr = doc->virtadr;
922 int i;
923 int emptymatch = 1;
925 /* flush the pipeline */
926 if (DoC_is_2000(doc)) {
927 WriteDOC(doc->CDSNControl & ~CDSN_CTRL_FLASH_IO, docptr, CDSNControl);
928 WriteDOC(0, docptr, 2k_CDSN_IO);
929 WriteDOC(0, docptr, 2k_CDSN_IO);
930 WriteDOC(0, docptr, 2k_CDSN_IO);
931 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
932 } else if (DoC_is_MillenniumPlus(doc)) {
933 WriteDOC(0, docptr, Mplus_NOP);
934 WriteDOC(0, docptr, Mplus_NOP);
935 WriteDOC(0, docptr, Mplus_NOP);
936 } else {
937 WriteDOC(0, docptr, NOP);
938 WriteDOC(0, docptr, NOP);
939 WriteDOC(0, docptr, NOP);
942 for (i = 0; i < 6; i++) {
943 if (DoC_is_MillenniumPlus(doc))
944 ecc_code[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
945 else
946 ecc_code[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
947 if (ecc_code[i] != empty_write_ecc[i])
948 emptymatch = 0;
950 if (DoC_is_MillenniumPlus(doc))
951 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
952 else
953 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
954 #if 0
955 /* If emptymatch=1, we might have an all-0xff data buffer. Check. */
956 if (emptymatch) {
957 /* Note: this somewhat expensive test should not be triggered
958 often. It could be optimized away by examining the data in
959 the writebuf routine, and remembering the result. */
960 for (i = 0; i < 512; i++) {
961 if (dat[i] == 0xff) continue;
962 emptymatch = 0;
963 break;
966 /* If emptymatch still =1, we do have an all-0xff data buffer.
967 Return all-0xff ecc value instead of the computed one, so
968 it'll look just like a freshly-erased page. */
969 if (emptymatch) memset(ecc_code, 0xff, 6);
970 #endif
971 return 0;
974 static int doc200x_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc)
976 int i, ret = 0;
977 struct nand_chip *this = mtd->priv;
978 struct doc_priv *doc = this->priv;
979 void __iomem *docptr = doc->virtadr;
980 volatile u_char dummy;
981 int emptymatch = 1;
983 /* flush the pipeline */
984 if (DoC_is_2000(doc)) {
985 dummy = ReadDOC(docptr, 2k_ECCStatus);
986 dummy = ReadDOC(docptr, 2k_ECCStatus);
987 dummy = ReadDOC(docptr, 2k_ECCStatus);
988 } else if (DoC_is_MillenniumPlus(doc)) {
989 dummy = ReadDOC(docptr, Mplus_ECCConf);
990 dummy = ReadDOC(docptr, Mplus_ECCConf);
991 dummy = ReadDOC(docptr, Mplus_ECCConf);
992 } else {
993 dummy = ReadDOC(docptr, ECCConf);
994 dummy = ReadDOC(docptr, ECCConf);
995 dummy = ReadDOC(docptr, ECCConf);
998 /* Error occured ? */
999 if (dummy & 0x80) {
1000 for (i = 0; i < 6; i++) {
1001 if (DoC_is_MillenniumPlus(doc))
1002 calc_ecc[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
1003 else
1004 calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
1005 if (calc_ecc[i] != empty_read_syndrome[i])
1006 emptymatch = 0;
1008 /* If emptymatch=1, the read syndrome is consistent with an
1009 all-0xff data and stored ecc block. Check the stored ecc. */
1010 if (emptymatch) {
1011 for (i = 0; i < 6; i++) {
1012 if (read_ecc[i] == 0xff) continue;
1013 emptymatch = 0;
1014 break;
1017 /* If emptymatch still =1, check the data block. */
1018 if (emptymatch) {
1019 /* Note: this somewhat expensive test should not be triggered
1020 often. It could be optimized away by examining the data in
1021 the readbuf routine, and remembering the result. */
1022 for (i = 0; i < 512; i++) {
1023 if (dat[i] == 0xff) continue;
1024 emptymatch = 0;
1025 break;
1028 /* If emptymatch still =1, this is almost certainly a freshly-
1029 erased block, in which case the ECC will not come out right.
1030 We'll suppress the error and tell the caller everything's
1031 OK. Because it is. */
1032 if (!emptymatch) ret = doc_ecc_decode (rs_decoder, dat, calc_ecc);
1033 if (ret > 0)
1034 printk(KERN_ERR "doc200x_correct_data corrected %d errors\n", ret);
1036 if (DoC_is_MillenniumPlus(doc))
1037 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
1038 else
1039 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
1040 if (no_ecc_failures && (ret == -1)) {
1041 printk(KERN_ERR "suppressing ECC failure\n");
1042 ret = 0;
1044 return ret;
1047 //u_char mydatabuf[528];
1049 static struct nand_oobinfo doc200x_oobinfo = {
1050 .useecc = MTD_NANDECC_AUTOPLACE,
1051 .eccbytes = 6,
1052 .eccpos = {0, 1, 2, 3, 4, 5},
1053 .oobfree = { {8, 8} }
1056 /* Find the (I)NFTL Media Header, and optionally also the mirror media header.
1057 On sucessful return, buf will contain a copy of the media header for
1058 further processing. id is the string to scan for, and will presumably be
1059 either "ANAND" or "BNAND". If findmirror=1, also look for the mirror media
1060 header. The page #s of the found media headers are placed in mh0_page and
1061 mh1_page in the DOC private structure. */
1062 static int __init find_media_headers(struct mtd_info *mtd, u_char *buf,
1063 const char *id, int findmirror)
1065 struct nand_chip *this = mtd->priv;
1066 struct doc_priv *doc = this->priv;
1067 unsigned offs, end = (MAX_MEDIAHEADER_SCAN << this->phys_erase_shift);
1068 int ret;
1069 size_t retlen;
1071 end = min(end, mtd->size); // paranoia
1072 for (offs = 0; offs < end; offs += mtd->erasesize) {
1073 ret = mtd->read(mtd, offs, mtd->oobblock, &retlen, buf);
1074 if (retlen != mtd->oobblock) continue;
1075 if (ret) {
1076 printk(KERN_WARNING "ECC error scanning DOC at 0x%x\n",
1077 offs);
1079 if (memcmp(buf, id, 6)) continue;
1080 printk(KERN_INFO "Found DiskOnChip %s Media Header at 0x%x\n", id, offs);
1081 if (doc->mh0_page == -1) {
1082 doc->mh0_page = offs >> this->page_shift;
1083 if (!findmirror) return 1;
1084 continue;
1086 doc->mh1_page = offs >> this->page_shift;
1087 return 2;
1089 if (doc->mh0_page == -1) {
1090 printk(KERN_WARNING "DiskOnChip %s Media Header not found.\n", id);
1091 return 0;
1093 /* Only one mediaheader was found. We want buf to contain a
1094 mediaheader on return, so we'll have to re-read the one we found. */
1095 offs = doc->mh0_page << this->page_shift;
1096 ret = mtd->read(mtd, offs, mtd->oobblock, &retlen, buf);
1097 if (retlen != mtd->oobblock) {
1098 /* Insanity. Give up. */
1099 printk(KERN_ERR "Read DiskOnChip Media Header once, but can't reread it???\n");
1100 return 0;
1102 return 1;
1105 static inline int __init nftl_partscan(struct mtd_info *mtd,
1106 struct mtd_partition *parts)
1108 struct nand_chip *this = mtd->priv;
1109 struct doc_priv *doc = this->priv;
1110 int ret = 0;
1111 u_char *buf;
1112 struct NFTLMediaHeader *mh;
1113 const unsigned psize = 1 << this->page_shift;
1114 unsigned blocks, maxblocks;
1115 int offs, numheaders;
1117 buf = kmalloc(mtd->oobblock, GFP_KERNEL);
1118 if (!buf) {
1119 printk(KERN_ERR "DiskOnChip mediaheader kmalloc failed!\n");
1120 return 0;
1122 if (!(numheaders=find_media_headers(mtd, buf, "ANAND", 1))) goto out;
1123 mh = (struct NFTLMediaHeader *) buf;
1125 //#ifdef CONFIG_MTD_DEBUG_VERBOSE
1126 // if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
1127 printk(KERN_INFO " DataOrgID = %s\n"
1128 " NumEraseUnits = %d\n"
1129 " FirstPhysicalEUN = %d\n"
1130 " FormattedSize = %d\n"
1131 " UnitSizeFactor = %d\n",
1132 mh->DataOrgID, mh->NumEraseUnits,
1133 mh->FirstPhysicalEUN, mh->FormattedSize,
1134 mh->UnitSizeFactor);
1135 //#endif
1137 blocks = mtd->size >> this->phys_erase_shift;
1138 maxblocks = min(32768U, mtd->erasesize - psize);
1140 if (mh->UnitSizeFactor == 0x00) {
1141 /* Auto-determine UnitSizeFactor. The constraints are:
1142 - There can be at most 32768 virtual blocks.
1143 - There can be at most (virtual block size - page size)
1144 virtual blocks (because MediaHeader+BBT must fit in 1).
1146 mh->UnitSizeFactor = 0xff;
1147 while (blocks > maxblocks) {
1148 blocks >>= 1;
1149 maxblocks = min(32768U, (maxblocks << 1) + psize);
1150 mh->UnitSizeFactor--;
1152 printk(KERN_WARNING "UnitSizeFactor=0x00 detected. Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor);
1155 /* NOTE: The lines below modify internal variables of the NAND and MTD
1156 layers; variables with have already been configured by nand_scan.
1157 Unfortunately, we didn't know before this point what these values
1158 should be. Thus, this code is somewhat dependant on the exact
1159 implementation of the NAND layer. */
1160 if (mh->UnitSizeFactor != 0xff) {
1161 this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
1162 mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
1163 printk(KERN_INFO "Setting virtual erase size to %d\n", mtd->erasesize);
1164 blocks = mtd->size >> this->bbt_erase_shift;
1165 maxblocks = min(32768U, mtd->erasesize - psize);
1168 if (blocks > maxblocks) {
1169 printk(KERN_ERR "UnitSizeFactor of 0x%02x is inconsistent with device size. Aborting.\n", mh->UnitSizeFactor);
1170 goto out;
1173 /* Skip past the media headers. */
1174 offs = max(doc->mh0_page, doc->mh1_page);
1175 offs <<= this->page_shift;
1176 offs += mtd->erasesize;
1178 //parts[0].name = " DiskOnChip Boot / Media Header partition";
1179 //parts[0].offset = 0;
1180 //parts[0].size = offs;
1182 parts[0].name = " DiskOnChip BDTL partition";
1183 parts[0].offset = offs;
1184 parts[0].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift;
1186 offs += parts[0].size;
1187 if (offs < mtd->size) {
1188 parts[1].name = " DiskOnChip Remainder partition";
1189 parts[1].offset = offs;
1190 parts[1].size = mtd->size - offs;
1191 ret = 2;
1192 goto out;
1194 ret = 1;
1195 out:
1196 kfree(buf);
1197 return ret;
1200 /* This is a stripped-down copy of the code in inftlmount.c */
1201 static inline int __init inftl_partscan(struct mtd_info *mtd,
1202 struct mtd_partition *parts)
1204 struct nand_chip *this = mtd->priv;
1205 struct doc_priv *doc = this->priv;
1206 int ret = 0;
1207 u_char *buf;
1208 struct INFTLMediaHeader *mh;
1209 struct INFTLPartition *ip;
1210 int numparts = 0;
1211 int blocks;
1212 int vshift, lastvunit = 0;
1213 int i;
1214 int end = mtd->size;
1216 if (inftl_bbt_write)
1217 end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift);
1219 buf = kmalloc(mtd->oobblock, GFP_KERNEL);
1220 if (!buf) {
1221 printk(KERN_ERR "DiskOnChip mediaheader kmalloc failed!\n");
1222 return 0;
1225 if (!find_media_headers(mtd, buf, "BNAND", 0)) goto out;
1226 doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift);
1227 mh = (struct INFTLMediaHeader *) buf;
1229 mh->NoOfBootImageBlocks = le32_to_cpu(mh->NoOfBootImageBlocks);
1230 mh->NoOfBinaryPartitions = le32_to_cpu(mh->NoOfBinaryPartitions);
1231 mh->NoOfBDTLPartitions = le32_to_cpu(mh->NoOfBDTLPartitions);
1232 mh->BlockMultiplierBits = le32_to_cpu(mh->BlockMultiplierBits);
1233 mh->FormatFlags = le32_to_cpu(mh->FormatFlags);
1234 mh->PercentUsed = le32_to_cpu(mh->PercentUsed);
1236 //#ifdef CONFIG_MTD_DEBUG_VERBOSE
1237 // if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
1238 printk(KERN_INFO " bootRecordID = %s\n"
1239 " NoOfBootImageBlocks = %d\n"
1240 " NoOfBinaryPartitions = %d\n"
1241 " NoOfBDTLPartitions = %d\n"
1242 " BlockMultiplerBits = %d\n"
1243 " FormatFlgs = %d\n"
1244 " OsakVersion = %d.%d.%d.%d\n"
1245 " PercentUsed = %d\n",
1246 mh->bootRecordID, mh->NoOfBootImageBlocks,
1247 mh->NoOfBinaryPartitions,
1248 mh->NoOfBDTLPartitions,
1249 mh->BlockMultiplierBits, mh->FormatFlags,
1250 ((unsigned char *) &mh->OsakVersion)[0] & 0xf,
1251 ((unsigned char *) &mh->OsakVersion)[1] & 0xf,
1252 ((unsigned char *) &mh->OsakVersion)[2] & 0xf,
1253 ((unsigned char *) &mh->OsakVersion)[3] & 0xf,
1254 mh->PercentUsed);
1255 //#endif
1257 vshift = this->phys_erase_shift + mh->BlockMultiplierBits;
1259 blocks = mtd->size >> vshift;
1260 if (blocks > 32768) {
1261 printk(KERN_ERR "BlockMultiplierBits=%d is inconsistent with device size. Aborting.\n", mh->BlockMultiplierBits);
1262 goto out;
1265 blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift);
1266 if (inftl_bbt_write && (blocks > mtd->erasesize)) {
1267 printk(KERN_ERR "Writeable BBTs spanning more than one erase block are not yet supported. FIX ME!\n");
1268 goto out;
1271 /* Scan the partitions */
1272 for (i = 0; (i < 4); i++) {
1273 ip = &(mh->Partitions[i]);
1274 ip->virtualUnits = le32_to_cpu(ip->virtualUnits);
1275 ip->firstUnit = le32_to_cpu(ip->firstUnit);
1276 ip->lastUnit = le32_to_cpu(ip->lastUnit);
1277 ip->flags = le32_to_cpu(ip->flags);
1278 ip->spareUnits = le32_to_cpu(ip->spareUnits);
1279 ip->Reserved0 = le32_to_cpu(ip->Reserved0);
1281 //#ifdef CONFIG_MTD_DEBUG_VERBOSE
1282 // if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
1283 printk(KERN_INFO " PARTITION[%d] ->\n"
1284 " virtualUnits = %d\n"
1285 " firstUnit = %d\n"
1286 " lastUnit = %d\n"
1287 " flags = 0x%x\n"
1288 " spareUnits = %d\n",
1289 i, ip->virtualUnits, ip->firstUnit,
1290 ip->lastUnit, ip->flags,
1291 ip->spareUnits);
1292 //#endif
1295 if ((i == 0) && (ip->firstUnit > 0)) {
1296 parts[0].name = " DiskOnChip IPL / Media Header partition";
1297 parts[0].offset = 0;
1298 parts[0].size = mtd->erasesize * ip->firstUnit;
1299 numparts = 1;
1303 if (ip->flags & INFTL_BINARY)
1304 parts[numparts].name = " DiskOnChip BDK partition";
1305 else
1306 parts[numparts].name = " DiskOnChip BDTL partition";
1307 parts[numparts].offset = ip->firstUnit << vshift;
1308 parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift;
1309 numparts++;
1310 if (ip->lastUnit > lastvunit) lastvunit = ip->lastUnit;
1311 if (ip->flags & INFTL_LAST) break;
1313 lastvunit++;
1314 if ((lastvunit << vshift) < end) {
1315 parts[numparts].name = " DiskOnChip Remainder partition";
1316 parts[numparts].offset = lastvunit << vshift;
1317 parts[numparts].size = end - parts[numparts].offset;
1318 numparts++;
1320 ret = numparts;
1321 out:
1322 kfree(buf);
1323 return ret;
1326 static int __init nftl_scan_bbt(struct mtd_info *mtd)
1328 int ret, numparts;
1329 struct nand_chip *this = mtd->priv;
1330 struct doc_priv *doc = this->priv;
1331 struct mtd_partition parts[2];
1333 memset((char *) parts, 0, sizeof(parts));
1334 /* On NFTL, we have to find the media headers before we can read the
1335 BBTs, since they're stored in the media header eraseblocks. */
1336 numparts = nftl_partscan(mtd, parts);
1337 if (!numparts) return -EIO;
1338 this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1339 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1340 NAND_BBT_VERSION;
1341 this->bbt_td->veroffs = 7;
1342 this->bbt_td->pages[0] = doc->mh0_page + 1;
1343 if (doc->mh1_page != -1) {
1344 this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1345 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1346 NAND_BBT_VERSION;
1347 this->bbt_md->veroffs = 7;
1348 this->bbt_md->pages[0] = doc->mh1_page + 1;
1349 } else {
1350 this->bbt_md = NULL;
1353 /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
1354 At least as nand_bbt.c is currently written. */
1355 if ((ret = nand_scan_bbt(mtd, NULL)))
1356 return ret;
1357 add_mtd_device(mtd);
1358 #ifdef CONFIG_MTD_PARTITIONS
1359 if (!no_autopart)
1360 add_mtd_partitions(mtd, parts, numparts);
1361 #endif
1362 return 0;
1365 static int __init inftl_scan_bbt(struct mtd_info *mtd)
1367 int ret, numparts;
1368 struct nand_chip *this = mtd->priv;
1369 struct doc_priv *doc = this->priv;
1370 struct mtd_partition parts[5];
1372 if (this->numchips > doc->chips_per_floor) {
1373 printk(KERN_ERR "Multi-floor INFTL devices not yet supported.\n");
1374 return -EIO;
1377 if (DoC_is_MillenniumPlus(doc)) {
1378 this->bbt_td->options = NAND_BBT_2BIT | NAND_BBT_ABSPAGE;
1379 if (inftl_bbt_write)
1380 this->bbt_td->options |= NAND_BBT_WRITE;
1381 this->bbt_td->pages[0] = 2;
1382 this->bbt_md = NULL;
1383 } else {
1384 this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT |
1385 NAND_BBT_VERSION;
1386 if (inftl_bbt_write)
1387 this->bbt_td->options |= NAND_BBT_WRITE;
1388 this->bbt_td->offs = 8;
1389 this->bbt_td->len = 8;
1390 this->bbt_td->veroffs = 7;
1391 this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1392 this->bbt_td->reserved_block_code = 0x01;
1393 this->bbt_td->pattern = "MSYS_BBT";
1395 this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT |
1396 NAND_BBT_VERSION;
1397 if (inftl_bbt_write)
1398 this->bbt_md->options |= NAND_BBT_WRITE;
1399 this->bbt_md->offs = 8;
1400 this->bbt_md->len = 8;
1401 this->bbt_md->veroffs = 7;
1402 this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1403 this->bbt_md->reserved_block_code = 0x01;
1404 this->bbt_md->pattern = "TBB_SYSM";
1407 /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
1408 At least as nand_bbt.c is currently written. */
1409 if ((ret = nand_scan_bbt(mtd, NULL)))
1410 return ret;
1411 memset((char *) parts, 0, sizeof(parts));
1412 numparts = inftl_partscan(mtd, parts);
1413 /* At least for now, require the INFTL Media Header. We could probably
1414 do without it for non-INFTL use, since all it gives us is
1415 autopartitioning, but I want to give it more thought. */
1416 if (!numparts) return -EIO;
1417 add_mtd_device(mtd);
1418 #ifdef CONFIG_MTD_PARTITIONS
1419 if (!no_autopart)
1420 add_mtd_partitions(mtd, parts, numparts);
1421 #endif
1422 return 0;
1425 static inline int __init doc2000_init(struct mtd_info *mtd)
1427 struct nand_chip *this = mtd->priv;
1428 struct doc_priv *doc = this->priv;
1430 this->write_byte = doc2000_write_byte;
1431 this->read_byte = doc2000_read_byte;
1432 this->write_buf = doc2000_writebuf;
1433 this->read_buf = doc2000_readbuf;
1434 this->verify_buf = doc2000_verifybuf;
1435 this->scan_bbt = nftl_scan_bbt;
1437 doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
1438 doc2000_count_chips(mtd);
1439 mtd->name = "DiskOnChip 2000 (NFTL Model)";
1440 return (4 * doc->chips_per_floor);
1443 static inline int __init doc2001_init(struct mtd_info *mtd)
1445 struct nand_chip *this = mtd->priv;
1446 struct doc_priv *doc = this->priv;
1448 this->write_byte = doc2001_write_byte;
1449 this->read_byte = doc2001_read_byte;
1450 this->write_buf = doc2001_writebuf;
1451 this->read_buf = doc2001_readbuf;
1452 this->verify_buf = doc2001_verifybuf;
1454 ReadDOC(doc->virtadr, ChipID);
1455 ReadDOC(doc->virtadr, ChipID);
1456 ReadDOC(doc->virtadr, ChipID);
1457 if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) {
1458 /* It's not a Millennium; it's one of the newer
1459 DiskOnChip 2000 units with a similar ASIC.
1460 Treat it like a Millennium, except that it
1461 can have multiple chips. */
1462 doc2000_count_chips(mtd);
1463 mtd->name = "DiskOnChip 2000 (INFTL Model)";
1464 this->scan_bbt = inftl_scan_bbt;
1465 return (4 * doc->chips_per_floor);
1466 } else {
1467 /* Bog-standard Millennium */
1468 doc->chips_per_floor = 1;
1469 mtd->name = "DiskOnChip Millennium";
1470 this->scan_bbt = nftl_scan_bbt;
1471 return 1;
1475 static inline int __init doc2001plus_init(struct mtd_info *mtd)
1477 struct nand_chip *this = mtd->priv;
1478 struct doc_priv *doc = this->priv;
1480 this->write_byte = NULL;
1481 this->read_byte = doc2001plus_read_byte;
1482 this->write_buf = doc2001plus_writebuf;
1483 this->read_buf = doc2001plus_readbuf;
1484 this->verify_buf = doc2001plus_verifybuf;
1485 this->scan_bbt = inftl_scan_bbt;
1486 this->hwcontrol = NULL;
1487 this->select_chip = doc2001plus_select_chip;
1488 this->cmdfunc = doc2001plus_command;
1489 this->enable_hwecc = doc2001plus_enable_hwecc;
1491 doc->chips_per_floor = 1;
1492 mtd->name = "DiskOnChip Millennium Plus";
1494 return 1;
1497 static inline int __init doc_probe(unsigned long physadr)
1499 unsigned char ChipID;
1500 struct mtd_info *mtd;
1501 struct nand_chip *nand;
1502 struct doc_priv *doc;
1503 void __iomem *virtadr;
1504 unsigned char save_control;
1505 unsigned char tmp, tmpb, tmpc;
1506 int reg, len, numchips;
1507 int ret = 0;
1509 virtadr = ioremap(physadr, DOC_IOREMAP_LEN);
1510 if (!virtadr) {
1511 printk(KERN_ERR "Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n", DOC_IOREMAP_LEN, physadr);
1512 return -EIO;
1515 /* It's not possible to cleanly detect the DiskOnChip - the
1516 * bootup procedure will put the device into reset mode, and
1517 * it's not possible to talk to it without actually writing
1518 * to the DOCControl register. So we store the current contents
1519 * of the DOCControl register's location, in case we later decide
1520 * that it's not a DiskOnChip, and want to put it back how we
1521 * found it.
1523 save_control = ReadDOC(virtadr, DOCControl);
1525 /* Reset the DiskOnChip ASIC */
1526 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET,
1527 virtadr, DOCControl);
1528 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET,
1529 virtadr, DOCControl);
1531 /* Enable the DiskOnChip ASIC */
1532 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL,
1533 virtadr, DOCControl);
1534 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL,
1535 virtadr, DOCControl);
1537 ChipID = ReadDOC(virtadr, ChipID);
1539 switch(ChipID) {
1540 case DOC_ChipID_Doc2k:
1541 reg = DoC_2k_ECCStatus;
1542 break;
1543 case DOC_ChipID_DocMil:
1544 reg = DoC_ECCConf;
1545 break;
1546 case DOC_ChipID_DocMilPlus16:
1547 case DOC_ChipID_DocMilPlus32:
1548 case 0:
1549 /* Possible Millennium Plus, need to do more checks */
1550 /* Possibly release from power down mode */
1551 for (tmp = 0; (tmp < 4); tmp++)
1552 ReadDOC(virtadr, Mplus_Power);
1554 /* Reset the Millennium Plus ASIC */
1555 tmp = DOC_MODE_RESET | DOC_MODE_MDWREN | DOC_MODE_RST_LAT |
1556 DOC_MODE_BDECT;
1557 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1558 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1560 mdelay(1);
1561 /* Enable the Millennium Plus ASIC */
1562 tmp = DOC_MODE_NORMAL | DOC_MODE_MDWREN | DOC_MODE_RST_LAT |
1563 DOC_MODE_BDECT;
1564 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1565 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1566 mdelay(1);
1568 ChipID = ReadDOC(virtadr, ChipID);
1570 switch (ChipID) {
1571 case DOC_ChipID_DocMilPlus16:
1572 reg = DoC_Mplus_Toggle;
1573 break;
1574 case DOC_ChipID_DocMilPlus32:
1575 printk(KERN_ERR "DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
1576 default:
1577 ret = -ENODEV;
1578 goto notfound;
1580 break;
1582 default:
1583 ret = -ENODEV;
1584 goto notfound;
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);
1592 ret = -ENODEV;
1593 goto notfound;
1596 for (mtd = doclist; mtd; mtd = doc->nextdoc) {
1597 unsigned char oldval;
1598 unsigned char newval;
1599 nand = mtd->priv;
1600 doc = nand->priv;
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);
1608 } else {
1609 oldval = ReadDOC(doc->virtadr, AliasResolution);
1610 newval = ReadDOC(virtadr, AliasResolution);
1612 if (oldval != newval)
1613 continue;
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
1618 } else {
1619 WriteDOC(~newval, virtadr, AliasResolution);
1620 oldval = ReadDOC(doc->virtadr, AliasResolution);
1621 WriteDOC(newval, virtadr, AliasResolution); // restore it
1623 newval = ~newval;
1624 if (oldval == newval) {
1625 printk(KERN_DEBUG "Found alias of DOC at 0x%lx to 0x%lx\n", doc->physadr, physadr);
1626 goto notfound;
1630 printk(KERN_NOTICE "DiskOnChip found at 0x%lx\n", physadr);
1632 len = sizeof(struct mtd_info) +
1633 sizeof(struct nand_chip) +
1634 sizeof(struct doc_priv) +
1635 (2 * sizeof(struct nand_bbt_descr));
1636 mtd = kmalloc(len, GFP_KERNEL);
1637 if (!mtd) {
1638 printk(KERN_ERR "DiskOnChip kmalloc (%d bytes) failed!\n", len);
1639 ret = -ENOMEM;
1640 goto fail;
1642 memset(mtd, 0, len);
1644 nand = (struct nand_chip *) (mtd + 1);
1645 doc = (struct doc_priv *) (nand + 1);
1646 nand->bbt_td = (struct nand_bbt_descr *) (doc + 1);
1647 nand->bbt_md = nand->bbt_td + 1;
1649 mtd->priv = nand;
1650 mtd->owner = THIS_MODULE;
1652 nand->priv = doc;
1653 nand->select_chip = doc200x_select_chip;
1654 nand->hwcontrol = doc200x_hwcontrol;
1655 nand->dev_ready = doc200x_dev_ready;
1656 nand->waitfunc = doc200x_wait;
1657 nand->block_bad = doc200x_block_bad;
1658 nand->enable_hwecc = doc200x_enable_hwecc;
1659 nand->calculate_ecc = doc200x_calculate_ecc;
1660 nand->correct_data = doc200x_correct_data;
1662 nand->autooob = &doc200x_oobinfo;
1663 nand->eccmode = NAND_ECC_HW6_512;
1664 nand->options = NAND_USE_FLASH_BBT | NAND_HWECC_SYNDROME;
1666 doc->physadr = physadr;
1667 doc->virtadr = virtadr;
1668 doc->ChipID = ChipID;
1669 doc->curfloor = -1;
1670 doc->curchip = -1;
1671 doc->mh0_page = -1;
1672 doc->mh1_page = -1;
1673 doc->nextdoc = doclist;
1675 if (ChipID == DOC_ChipID_Doc2k)
1676 numchips = doc2000_init(mtd);
1677 else if (ChipID == DOC_ChipID_DocMilPlus16)
1678 numchips = doc2001plus_init(mtd);
1679 else
1680 numchips = doc2001_init(mtd);
1682 if ((ret = nand_scan(mtd, numchips))) {
1683 /* DBB note: i believe nand_release is necessary here, as
1684 buffers may have been allocated in nand_base. Check with
1685 Thomas. FIX ME! */
1686 /* nand_release will call del_mtd_device, but we haven't yet
1687 added it. This is handled without incident by
1688 del_mtd_device, as far as I can tell. */
1689 nand_release(mtd);
1690 kfree(mtd);
1691 goto fail;
1694 /* Success! */
1695 doclist = mtd;
1696 return 0;
1698 notfound:
1699 /* Put back the contents of the DOCControl register, in case it's not
1700 actually a DiskOnChip. */
1701 WriteDOC(save_control, virtadr, DOCControl);
1702 fail:
1703 iounmap(virtadr);
1704 return ret;
1707 static void release_nanddoc(void)
1709 struct mtd_info *mtd, *nextmtd;
1710 struct nand_chip *nand;
1711 struct doc_priv *doc;
1713 for (mtd = doclist; mtd; mtd = nextmtd) {
1714 nand = mtd->priv;
1715 doc = nand->priv;
1717 nextmtd = doc->nextdoc;
1718 nand_release(mtd);
1719 iounmap(doc->virtadr);
1720 kfree(mtd);
1724 static int __init init_nanddoc(void)
1726 int i, ret = 0;
1728 /* We could create the decoder on demand, if memory is a concern.
1729 * This way we have it handy, if an error happens
1731 * Symbolsize is 10 (bits)
1732 * Primitve polynomial is x^10+x^3+1
1733 * first consecutive root is 510
1734 * primitve element to generate roots = 1
1735 * generator polinomial degree = 4
1737 rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS);
1738 if (!rs_decoder) {
1739 printk (KERN_ERR "DiskOnChip: Could not create a RS decoder\n");
1740 return -ENOMEM;
1743 if (doc_config_location) {
1744 printk(KERN_INFO "Using configured DiskOnChip probe address 0x%lx\n", doc_config_location);
1745 ret = doc_probe(doc_config_location);
1746 if (ret < 0)
1747 goto outerr;
1748 } else {
1749 for (i=0; (doc_locations[i] != 0xffffffff); i++) {
1750 doc_probe(doc_locations[i]);
1753 /* No banner message any more. Print a message if no DiskOnChip
1754 found, so the user knows we at least tried. */
1755 if (!doclist) {
1756 printk(KERN_INFO "No valid DiskOnChip devices found\n");
1757 ret = -ENODEV;
1758 goto outerr;
1760 return 0;
1761 outerr:
1762 free_rs(rs_decoder);
1763 return ret;
1766 static void __exit cleanup_nanddoc(void)
1768 /* Cleanup the nand/DoC resources */
1769 release_nanddoc();
1771 /* Free the reed solomon resources */
1772 if (rs_decoder) {
1773 free_rs(rs_decoder);
1777 module_init(init_nanddoc);
1778 module_exit(cleanup_nanddoc);
1780 MODULE_LICENSE("GPL");
1781 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1782 MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver\n");