GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / mtd / nand / diskonchip.c
blob21728e3d030a1fee6a58dd8fee06ac8269272d48
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
20 #include <linux/kernel.h>
21 #include <linux/init.h>
22 #include <linux/sched.h>
23 #include <linux/delay.h>
24 #include <linux/rslib.h>
25 #include <linux/moduleparam.h>
26 #include <linux/slab.h>
27 #include <asm/io.h>
29 #include <linux/mtd/mtd.h>
30 #include <linux/mtd/nand.h>
31 #include <linux/mtd/doc2000.h>
32 #include <linux/mtd/partitions.h>
33 #include <linux/mtd/inftl.h>
35 /* Where to look for the devices? */
36 #ifndef CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS
37 #define CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS 0
38 #endif
40 static unsigned long __initdata doc_locations[] = {
41 #if defined(__alpha__) || defined(__i386__) || defined(__x86_64__)
42 #ifdef CONFIG_MTD_NAND_DISKONCHIP_PROBE_HIGH
43 0xfffc8000, 0xfffca000, 0xfffcc000, 0xfffce000,
44 0xfffd0000, 0xfffd2000, 0xfffd4000, 0xfffd6000,
45 0xfffd8000, 0xfffda000, 0xfffdc000, 0xfffde000,
46 0xfffe0000, 0xfffe2000, 0xfffe4000, 0xfffe6000,
47 0xfffe8000, 0xfffea000, 0xfffec000, 0xfffee000,
48 #else /* CONFIG_MTD_DOCPROBE_HIGH */
49 0xc8000, 0xca000, 0xcc000, 0xce000,
50 0xd0000, 0xd2000, 0xd4000, 0xd6000,
51 0xd8000, 0xda000, 0xdc000, 0xde000,
52 0xe0000, 0xe2000, 0xe4000, 0xe6000,
53 0xe8000, 0xea000, 0xec000, 0xee000,
54 #endif /* CONFIG_MTD_DOCPROBE_HIGH */
55 #else
56 #warning Unknown architecture for DiskOnChip. No default probe locations defined
57 #endif
58 0xffffffff };
60 static struct mtd_info *doclist = NULL;
62 struct doc_priv {
63 void __iomem *virtadr;
64 unsigned long physadr;
65 u_char ChipID;
66 u_char CDSNControl;
67 int chips_per_floor; /* The number of chips detected on each floor */
68 int curfloor;
69 int curchip;
70 int mh0_page;
71 int mh1_page;
72 struct mtd_info *nextdoc;
75 /* This is the syndrome computed by the HW ecc generator upon reading an empty
76 page, one with all 0xff for data and stored ecc code. */
77 static u_char empty_read_syndrome[6] = { 0x26, 0xff, 0x6d, 0x47, 0x73, 0x7a };
79 /* This is the ecc value computed by the HW ecc generator upon writing an empty
80 page, one with all 0xff for data. */
81 static u_char empty_write_ecc[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
83 #define INFTL_BBT_RESERVED_BLOCKS 4
85 #define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
86 #define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
87 #define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
89 static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
90 unsigned int bitmask);
91 static void doc200x_select_chip(struct mtd_info *mtd, int chip);
93 static int debug = 0;
94 module_param(debug, int, 0);
96 static int try_dword = 1;
97 module_param(try_dword, int, 0);
99 static int no_ecc_failures = 0;
100 module_param(no_ecc_failures, int, 0);
102 static int no_autopart = 0;
103 module_param(no_autopart, int, 0);
105 static int show_firmware_partition = 0;
106 module_param(show_firmware_partition, int, 0);
108 #ifdef CONFIG_MTD_NAND_DISKONCHIP_BBTWRITE
109 static int inftl_bbt_write = 1;
110 #else
111 static int inftl_bbt_write = 0;
112 #endif
113 module_param(inftl_bbt_write, int, 0);
115 static unsigned long doc_config_location = CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS;
116 module_param(doc_config_location, ulong, 0);
117 MODULE_PARM_DESC(doc_config_location, "Physical memory address at which to probe for DiskOnChip");
119 /* Sector size for HW ECC */
120 #define SECTOR_SIZE 512
121 /* The sector bytes are packed into NB_DATA 10 bit words */
122 #define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / 10)
123 /* Number of roots */
124 #define NROOTS 4
125 /* First consective root */
126 #define FCR 510
127 /* Number of symbols */
128 #define NN 1023
130 /* the Reed Solomon control structure */
131 static struct rs_control *rs_decoder;
134 * The HW decoder in the DoC ASIC's provides us a error syndrome,
135 * which we must convert to a standard syndrom usable by the generic
136 * Reed-Solomon library code.
138 * Fabrice Bellard figured this out in the old docecc code. I added
139 * some comments, improved a minor bit and converted it to make use
140 * of the generic Reed-Solomon libary. tglx
142 static int doc_ecc_decode(struct rs_control *rs, uint8_t *data, uint8_t *ecc)
144 int i, j, nerr, errpos[8];
145 uint8_t parity;
146 uint16_t ds[4], s[5], tmp, errval[8], syn[4];
148 memset(syn, 0, sizeof(syn));
149 /* Convert the ecc bytes into words */
150 ds[0] = ((ecc[4] & 0xff) >> 0) | ((ecc[5] & 0x03) << 8);
151 ds[1] = ((ecc[5] & 0xfc) >> 2) | ((ecc[2] & 0x0f) << 6);
152 ds[2] = ((ecc[2] & 0xf0) >> 4) | ((ecc[3] & 0x3f) << 4);
153 ds[3] = ((ecc[3] & 0xc0) >> 6) | ((ecc[0] & 0xff) << 2);
154 parity = ecc[1];
156 /* Initialize the syndrom buffer */
157 for (i = 0; i < NROOTS; i++)
158 s[i] = ds[0];
160 * Evaluate
161 * s[i] = ds[3]x^3 + ds[2]x^2 + ds[1]x^1 + ds[0]
162 * where x = alpha^(FCR + i)
164 for (j = 1; j < NROOTS; j++) {
165 if (ds[j] == 0)
166 continue;
167 tmp = rs->index_of[ds[j]];
168 for (i = 0; i < NROOTS; i++)
169 s[i] ^= rs->alpha_to[rs_modnn(rs, tmp + (FCR + i) * j)];
172 /* Calc syn[i] = s[i] / alpha^(v + i) */
173 for (i = 0; i < NROOTS; i++) {
174 if (s[i])
175 syn[i] = rs_modnn(rs, rs->index_of[s[i]] + (NN - FCR - i));
177 /* Call the decoder library */
178 nerr = decode_rs16(rs, NULL, NULL, 1019, syn, 0, errpos, 0, errval);
180 /* Incorrectable errors ? */
181 if (nerr < 0)
182 return nerr;
185 * Correct the errors. The bitpositions are a bit of magic,
186 * but they are given by the design of the de/encoder circuit
187 * in the DoC ASIC's.
189 for (i = 0; i < nerr; i++) {
190 int index, bitpos, pos = 1015 - errpos[i];
191 uint8_t val;
192 if (pos >= NB_DATA && pos < 1019)
193 continue;
194 if (pos < NB_DATA) {
195 /* extract bit position (MSB first) */
196 pos = 10 * (NB_DATA - 1 - pos) - 6;
197 /* now correct the following 10 bits. At most two bytes
198 can be modified since pos is even */
199 index = (pos >> 3) ^ 1;
200 bitpos = pos & 7;
201 if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
202 val = (uint8_t) (errval[i] >> (2 + bitpos));
203 parity ^= val;
204 if (index < SECTOR_SIZE)
205 data[index] ^= val;
207 index = ((pos >> 3) + 1) ^ 1;
208 bitpos = (bitpos + 10) & 7;
209 if (bitpos == 0)
210 bitpos = 8;
211 if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
212 val = (uint8_t) (errval[i] << (8 - bitpos));
213 parity ^= val;
214 if (index < SECTOR_SIZE)
215 data[index] ^= val;
219 /* If the parity is wrong, no rescue possible */
220 return parity ? -EBADMSG : nerr;
223 static void DoC_Delay(struct doc_priv *doc, unsigned short cycles)
225 volatile char dummy;
226 int i;
228 for (i = 0; i < cycles; i++) {
229 if (DoC_is_Millennium(doc))
230 dummy = ReadDOC(doc->virtadr, NOP);
231 else if (DoC_is_MillenniumPlus(doc))
232 dummy = ReadDOC(doc->virtadr, Mplus_NOP);
233 else
234 dummy = ReadDOC(doc->virtadr, DOCStatus);
239 #define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
241 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
242 static int _DoC_WaitReady(struct doc_priv *doc)
244 void __iomem *docptr = doc->virtadr;
245 unsigned long timeo = jiffies + (HZ * 10);
247 if (debug)
248 printk("_DoC_WaitReady...\n");
249 /* Out-of-line routine to wait for chip response */
250 if (DoC_is_MillenniumPlus(doc)) {
251 while ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
252 if (time_after(jiffies, timeo)) {
253 printk("_DoC_WaitReady timed out.\n");
254 return -EIO;
256 udelay(1);
257 cond_resched();
259 } else {
260 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
261 if (time_after(jiffies, timeo)) {
262 printk("_DoC_WaitReady timed out.\n");
263 return -EIO;
265 udelay(1);
266 cond_resched();
270 return 0;
273 static inline int DoC_WaitReady(struct doc_priv *doc)
275 void __iomem *docptr = doc->virtadr;
276 int ret = 0;
278 if (DoC_is_MillenniumPlus(doc)) {
279 DoC_Delay(doc, 4);
281 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
282 /* Call the out-of-line routine to wait */
283 ret = _DoC_WaitReady(doc);
284 } else {
285 DoC_Delay(doc, 4);
287 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
288 /* Call the out-of-line routine to wait */
289 ret = _DoC_WaitReady(doc);
290 DoC_Delay(doc, 2);
293 if (debug)
294 printk("DoC_WaitReady OK\n");
295 return ret;
298 static void doc2000_write_byte(struct mtd_info *mtd, u_char datum)
300 struct nand_chip *this = mtd->priv;
301 struct doc_priv *doc = this->priv;
302 void __iomem *docptr = doc->virtadr;
304 if (debug)
305 printk("write_byte %02x\n", datum);
306 WriteDOC(datum, docptr, CDSNSlowIO);
307 WriteDOC(datum, docptr, 2k_CDSN_IO);
310 static u_char doc2000_read_byte(struct mtd_info *mtd)
312 struct nand_chip *this = mtd->priv;
313 struct doc_priv *doc = this->priv;
314 void __iomem *docptr = doc->virtadr;
315 u_char ret;
317 ReadDOC(docptr, CDSNSlowIO);
318 DoC_Delay(doc, 2);
319 ret = ReadDOC(docptr, 2k_CDSN_IO);
320 if (debug)
321 printk("read_byte returns %02x\n", ret);
322 return ret;
325 static void doc2000_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
327 struct nand_chip *this = mtd->priv;
328 struct doc_priv *doc = this->priv;
329 void __iomem *docptr = doc->virtadr;
330 int i;
331 if (debug)
332 printk("writebuf of %d bytes: ", len);
333 for (i = 0; i < len; i++) {
334 WriteDOC_(buf[i], docptr, DoC_2k_CDSN_IO + i);
335 if (debug && i < 16)
336 printk("%02x ", buf[i]);
338 if (debug)
339 printk("\n");
342 static void doc2000_readbuf(struct mtd_info *mtd, u_char *buf, int len)
344 struct nand_chip *this = mtd->priv;
345 struct doc_priv *doc = this->priv;
346 void __iomem *docptr = doc->virtadr;
347 int i;
349 if (debug)
350 printk("readbuf of %d bytes: ", len);
352 for (i = 0; i < len; i++) {
353 buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
357 static void doc2000_readbuf_dword(struct mtd_info *mtd, u_char *buf, int len)
359 struct nand_chip *this = mtd->priv;
360 struct doc_priv *doc = this->priv;
361 void __iomem *docptr = doc->virtadr;
362 int i;
364 if (debug)
365 printk("readbuf_dword of %d bytes: ", len);
367 if (unlikely((((unsigned long)buf) | len) & 3)) {
368 for (i = 0; i < len; i++) {
369 *(uint8_t *) (&buf[i]) = ReadDOC(docptr, 2k_CDSN_IO + i);
371 } else {
372 for (i = 0; i < len; i += 4) {
373 *(uint32_t *) (&buf[i]) = readl(docptr + DoC_2k_CDSN_IO + i);
378 static int doc2000_verifybuf(struct mtd_info *mtd, const u_char *buf, int len)
380 struct nand_chip *this = mtd->priv;
381 struct doc_priv *doc = this->priv;
382 void __iomem *docptr = doc->virtadr;
383 int i;
385 for (i = 0; i < len; i++)
386 if (buf[i] != ReadDOC(docptr, 2k_CDSN_IO))
387 return -EFAULT;
388 return 0;
391 static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr)
393 struct nand_chip *this = mtd->priv;
394 struct doc_priv *doc = this->priv;
395 uint16_t ret;
397 doc200x_select_chip(mtd, nr);
398 doc200x_hwcontrol(mtd, NAND_CMD_READID,
399 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
400 doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
401 doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
403 /* We cant' use dev_ready here, but at least we wait for the
404 * command to complete
406 udelay(50);
408 ret = this->read_byte(mtd) << 8;
409 ret |= this->read_byte(mtd);
411 if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) {
412 /* First chip probe. See if we get same results by 32-bit access */
413 union {
414 uint32_t dword;
415 uint8_t byte[4];
416 } ident;
417 void __iomem *docptr = doc->virtadr;
419 doc200x_hwcontrol(mtd, NAND_CMD_READID,
420 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
421 doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
422 doc200x_hwcontrol(mtd, NAND_CMD_NONE,
423 NAND_NCE | NAND_CTRL_CHANGE);
425 udelay(50);
427 ident.dword = readl(docptr + DoC_2k_CDSN_IO);
428 if (((ident.byte[0] << 8) | ident.byte[1]) == ret) {
429 printk(KERN_INFO "DiskOnChip 2000 responds to DWORD access\n");
430 this->read_buf = &doc2000_readbuf_dword;
434 return ret;
437 static void __init doc2000_count_chips(struct mtd_info *mtd)
439 struct nand_chip *this = mtd->priv;
440 struct doc_priv *doc = this->priv;
441 uint16_t mfrid;
442 int i;
444 /* Max 4 chips per floor on DiskOnChip 2000 */
445 doc->chips_per_floor = 4;
447 /* Find out what the first chip is */
448 mfrid = doc200x_ident_chip(mtd, 0);
450 /* Find how many chips in each floor. */
451 for (i = 1; i < 4; i++) {
452 if (doc200x_ident_chip(mtd, i) != mfrid)
453 break;
455 doc->chips_per_floor = i;
456 printk(KERN_DEBUG "Detected %d chips per floor.\n", i);
459 static int doc200x_wait(struct mtd_info *mtd, struct nand_chip *this)
461 struct doc_priv *doc = this->priv;
463 int status;
465 DoC_WaitReady(doc);
466 this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
467 DoC_WaitReady(doc);
468 status = (int)this->read_byte(mtd);
470 return status;
473 static void doc2001_write_byte(struct mtd_info *mtd, u_char datum)
475 struct nand_chip *this = mtd->priv;
476 struct doc_priv *doc = this->priv;
477 void __iomem *docptr = doc->virtadr;
479 WriteDOC(datum, docptr, CDSNSlowIO);
480 WriteDOC(datum, docptr, Mil_CDSN_IO);
481 WriteDOC(datum, docptr, WritePipeTerm);
484 static u_char doc2001_read_byte(struct mtd_info *mtd)
486 struct nand_chip *this = mtd->priv;
487 struct doc_priv *doc = this->priv;
488 void __iomem *docptr = doc->virtadr;
490 //ReadDOC(docptr, CDSNSlowIO);
491 /* 11.4.5 -- delay twice to allow extended length cycle */
492 DoC_Delay(doc, 2);
493 ReadDOC(docptr, ReadPipeInit);
494 //return ReadDOC(docptr, Mil_CDSN_IO);
495 return ReadDOC(docptr, LastDataRead);
498 static void doc2001_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
500 struct nand_chip *this = mtd->priv;
501 struct doc_priv *doc = this->priv;
502 void __iomem *docptr = doc->virtadr;
503 int i;
505 for (i = 0; i < len; i++)
506 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
507 /* Terminate write pipeline */
508 WriteDOC(0x00, docptr, WritePipeTerm);
511 static void doc2001_readbuf(struct mtd_info *mtd, u_char *buf, int len)
513 struct nand_chip *this = mtd->priv;
514 struct doc_priv *doc = this->priv;
515 void __iomem *docptr = doc->virtadr;
516 int i;
518 /* Start read pipeline */
519 ReadDOC(docptr, ReadPipeInit);
521 for (i = 0; i < len - 1; i++)
522 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
524 /* Terminate read pipeline */
525 buf[i] = ReadDOC(docptr, LastDataRead);
528 static int doc2001_verifybuf(struct mtd_info *mtd, const u_char *buf, int len)
530 struct nand_chip *this = mtd->priv;
531 struct doc_priv *doc = this->priv;
532 void __iomem *docptr = doc->virtadr;
533 int i;
535 /* Start read pipeline */
536 ReadDOC(docptr, ReadPipeInit);
538 for (i = 0; i < len - 1; i++)
539 if (buf[i] != ReadDOC(docptr, Mil_CDSN_IO)) {
540 ReadDOC(docptr, LastDataRead);
541 return i;
543 if (buf[i] != ReadDOC(docptr, LastDataRead))
544 return i;
545 return 0;
548 static u_char doc2001plus_read_byte(struct mtd_info *mtd)
550 struct nand_chip *this = mtd->priv;
551 struct doc_priv *doc = this->priv;
552 void __iomem *docptr = doc->virtadr;
553 u_char ret;
555 ReadDOC(docptr, Mplus_ReadPipeInit);
556 ReadDOC(docptr, Mplus_ReadPipeInit);
557 ret = ReadDOC(docptr, Mplus_LastDataRead);
558 if (debug)
559 printk("read_byte returns %02x\n", ret);
560 return ret;
563 static void doc2001plus_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
565 struct nand_chip *this = mtd->priv;
566 struct doc_priv *doc = this->priv;
567 void __iomem *docptr = doc->virtadr;
568 int i;
570 if (debug)
571 printk("writebuf of %d bytes: ", len);
572 for (i = 0; i < len; i++) {
573 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
574 if (debug && i < 16)
575 printk("%02x ", buf[i]);
577 if (debug)
578 printk("\n");
581 static void doc2001plus_readbuf(struct mtd_info *mtd, u_char *buf, int len)
583 struct nand_chip *this = mtd->priv;
584 struct doc_priv *doc = this->priv;
585 void __iomem *docptr = doc->virtadr;
586 int i;
588 if (debug)
589 printk("readbuf of %d bytes: ", len);
591 /* Start read pipeline */
592 ReadDOC(docptr, Mplus_ReadPipeInit);
593 ReadDOC(docptr, Mplus_ReadPipeInit);
595 for (i = 0; i < len - 2; i++) {
596 buf[i] = ReadDOC(docptr, Mil_CDSN_IO);
597 if (debug && i < 16)
598 printk("%02x ", buf[i]);
601 /* Terminate read pipeline */
602 buf[len - 2] = ReadDOC(docptr, Mplus_LastDataRead);
603 if (debug && i < 16)
604 printk("%02x ", buf[len - 2]);
605 buf[len - 1] = ReadDOC(docptr, Mplus_LastDataRead);
606 if (debug && i < 16)
607 printk("%02x ", buf[len - 1]);
608 if (debug)
609 printk("\n");
612 static int doc2001plus_verifybuf(struct mtd_info *mtd, const u_char *buf, int len)
614 struct nand_chip *this = mtd->priv;
615 struct doc_priv *doc = this->priv;
616 void __iomem *docptr = doc->virtadr;
617 int i;
619 if (debug)
620 printk("verifybuf of %d bytes: ", len);
622 /* Start read pipeline */
623 ReadDOC(docptr, Mplus_ReadPipeInit);
624 ReadDOC(docptr, Mplus_ReadPipeInit);
626 for (i = 0; i < len - 2; i++)
627 if (buf[i] != ReadDOC(docptr, Mil_CDSN_IO)) {
628 ReadDOC(docptr, Mplus_LastDataRead);
629 ReadDOC(docptr, Mplus_LastDataRead);
630 return i;
632 if (buf[len - 2] != ReadDOC(docptr, Mplus_LastDataRead))
633 return len - 2;
634 if (buf[len - 1] != ReadDOC(docptr, Mplus_LastDataRead))
635 return len - 1;
636 return 0;
639 static void doc2001plus_select_chip(struct mtd_info *mtd, int chip)
641 struct nand_chip *this = mtd->priv;
642 struct doc_priv *doc = this->priv;
643 void __iomem *docptr = doc->virtadr;
644 int floor = 0;
646 if (debug)
647 printk("select chip (%d)\n", chip);
649 if (chip == -1) {
650 /* Disable flash internally */
651 WriteDOC(0, docptr, Mplus_FlashSelect);
652 return;
655 floor = chip / doc->chips_per_floor;
656 chip -= (floor * doc->chips_per_floor);
658 /* Assert ChipEnable and deassert WriteProtect */
659 WriteDOC((DOC_FLASH_CE), docptr, Mplus_FlashSelect);
660 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
662 doc->curchip = chip;
663 doc->curfloor = floor;
666 static void doc200x_select_chip(struct mtd_info *mtd, int chip)
668 struct nand_chip *this = mtd->priv;
669 struct doc_priv *doc = this->priv;
670 void __iomem *docptr = doc->virtadr;
671 int floor = 0;
673 if (debug)
674 printk("select chip (%d)\n", chip);
676 if (chip == -1)
677 return;
679 floor = chip / doc->chips_per_floor;
680 chip -= (floor * doc->chips_per_floor);
682 /* 11.4.4 -- deassert CE before changing chip */
683 doc200x_hwcontrol(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
685 WriteDOC(floor, docptr, FloorSelect);
686 WriteDOC(chip, docptr, CDSNDeviceSelect);
688 doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
690 doc->curchip = chip;
691 doc->curfloor = floor;
694 #define CDSN_CTRL_MSK (CDSN_CTRL_CE | CDSN_CTRL_CLE | CDSN_CTRL_ALE)
696 static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
697 unsigned int ctrl)
699 struct nand_chip *this = mtd->priv;
700 struct doc_priv *doc = this->priv;
701 void __iomem *docptr = doc->virtadr;
703 if (ctrl & NAND_CTRL_CHANGE) {
704 doc->CDSNControl &= ~CDSN_CTRL_MSK;
705 doc->CDSNControl |= ctrl & CDSN_CTRL_MSK;
706 if (debug)
707 printk("hwcontrol(%d): %02x\n", cmd, doc->CDSNControl);
708 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
709 /* 11.4.3 -- 4 NOPs after CSDNControl write */
710 DoC_Delay(doc, 4);
712 if (cmd != NAND_CMD_NONE) {
713 if (DoC_is_2000(doc))
714 doc2000_write_byte(mtd, cmd);
715 else
716 doc2001_write_byte(mtd, cmd);
720 static void doc2001plus_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
722 struct nand_chip *this = mtd->priv;
723 struct doc_priv *doc = this->priv;
724 void __iomem *docptr = doc->virtadr;
727 * Must terminate write pipeline before sending any commands
728 * to the device.
730 if (command == NAND_CMD_PAGEPROG) {
731 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
732 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
736 * Write out the command to the device.
738 if (command == NAND_CMD_SEQIN) {
739 int readcmd;
741 if (column >= mtd->writesize) {
742 /* OOB area */
743 column -= mtd->writesize;
744 readcmd = NAND_CMD_READOOB;
745 } else if (column < 256) {
746 /* First 256 bytes --> READ0 */
747 readcmd = NAND_CMD_READ0;
748 } else {
749 column -= 256;
750 readcmd = NAND_CMD_READ1;
752 WriteDOC(readcmd, docptr, Mplus_FlashCmd);
754 WriteDOC(command, docptr, Mplus_FlashCmd);
755 WriteDOC(0, docptr, Mplus_WritePipeTerm);
756 WriteDOC(0, docptr, Mplus_WritePipeTerm);
758 if (column != -1 || page_addr != -1) {
759 /* Serially input address */
760 if (column != -1) {
761 /* Adjust columns for 16 bit buswidth */
762 if (this->options & NAND_BUSWIDTH_16)
763 column >>= 1;
764 WriteDOC(column, docptr, Mplus_FlashAddress);
766 if (page_addr != -1) {
767 WriteDOC((unsigned char)(page_addr & 0xff), docptr, Mplus_FlashAddress);
768 WriteDOC((unsigned char)((page_addr >> 8) & 0xff), docptr, Mplus_FlashAddress);
769 /* One more address cycle for higher density devices */
770 if (this->chipsize & 0x0c000000) {
771 WriteDOC((unsigned char)((page_addr >> 16) & 0x0f), docptr, Mplus_FlashAddress);
772 printk("high density\n");
775 WriteDOC(0, docptr, Mplus_WritePipeTerm);
776 WriteDOC(0, docptr, Mplus_WritePipeTerm);
777 /* deassert ALE */
778 if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
779 command == NAND_CMD_READOOB || command == NAND_CMD_READID)
780 WriteDOC(0, docptr, Mplus_FlashControl);
784 * program and erase have their own busy handlers
785 * status and sequential in needs no delay
787 switch (command) {
789 case NAND_CMD_PAGEPROG:
790 case NAND_CMD_ERASE1:
791 case NAND_CMD_ERASE2:
792 case NAND_CMD_SEQIN:
793 case NAND_CMD_STATUS:
794 return;
796 case NAND_CMD_RESET:
797 if (this->dev_ready)
798 break;
799 udelay(this->chip_delay);
800 WriteDOC(NAND_CMD_STATUS, docptr, Mplus_FlashCmd);
801 WriteDOC(0, docptr, Mplus_WritePipeTerm);
802 WriteDOC(0, docptr, Mplus_WritePipeTerm);
803 while (!(this->read_byte(mtd) & 0x40)) ;
804 return;
806 /* This applies to read commands */
807 default:
809 * If we don't have access to the busy pin, we apply the given
810 * command delay
812 if (!this->dev_ready) {
813 udelay(this->chip_delay);
814 return;
818 /* Apply this short delay always to ensure that we do wait tWB in
819 * any case on any machine. */
820 ndelay(100);
821 /* wait until command is processed */
822 while (!this->dev_ready(mtd)) ;
825 static int doc200x_dev_ready(struct mtd_info *mtd)
827 struct nand_chip *this = mtd->priv;
828 struct doc_priv *doc = this->priv;
829 void __iomem *docptr = doc->virtadr;
831 if (DoC_is_MillenniumPlus(doc)) {
832 /* 11.4.2 -- must NOP four times before checking FR/B# */
833 DoC_Delay(doc, 4);
834 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
835 if (debug)
836 printk("not ready\n");
837 return 0;
839 if (debug)
840 printk("was ready\n");
841 return 1;
842 } else {
843 /* 11.4.2 -- must NOP four times before checking FR/B# */
844 DoC_Delay(doc, 4);
845 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
846 if (debug)
847 printk("not ready\n");
848 return 0;
850 /* 11.4.2 -- Must NOP twice if it's ready */
851 DoC_Delay(doc, 2);
852 if (debug)
853 printk("was ready\n");
854 return 1;
858 static int doc200x_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
860 /* This is our last resort if we couldn't find or create a BBT. Just
861 pretend all blocks are good. */
862 return 0;
865 static void doc200x_enable_hwecc(struct mtd_info *mtd, int mode)
867 struct nand_chip *this = mtd->priv;
868 struct doc_priv *doc = this->priv;
869 void __iomem *docptr = doc->virtadr;
871 /* Prime the ECC engine */
872 switch (mode) {
873 case NAND_ECC_READ:
874 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
875 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
876 break;
877 case NAND_ECC_WRITE:
878 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
879 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
880 break;
884 static void doc2001plus_enable_hwecc(struct mtd_info *mtd, int mode)
886 struct nand_chip *this = mtd->priv;
887 struct doc_priv *doc = this->priv;
888 void __iomem *docptr = doc->virtadr;
890 /* Prime the ECC engine */
891 switch (mode) {
892 case NAND_ECC_READ:
893 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
894 WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
895 break;
896 case NAND_ECC_WRITE:
897 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
898 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
899 break;
903 /* This code is only called on write */
904 static int doc200x_calculate_ecc(struct mtd_info *mtd, const u_char *dat, unsigned char *ecc_code)
906 struct nand_chip *this = mtd->priv;
907 struct doc_priv *doc = this->priv;
908 void __iomem *docptr = doc->virtadr;
909 int i;
910 int emptymatch = 1;
912 /* flush the pipeline */
913 if (DoC_is_2000(doc)) {
914 WriteDOC(doc->CDSNControl & ~CDSN_CTRL_FLASH_IO, docptr, CDSNControl);
915 WriteDOC(0, docptr, 2k_CDSN_IO);
916 WriteDOC(0, docptr, 2k_CDSN_IO);
917 WriteDOC(0, docptr, 2k_CDSN_IO);
918 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
919 } else if (DoC_is_MillenniumPlus(doc)) {
920 WriteDOC(0, docptr, Mplus_NOP);
921 WriteDOC(0, docptr, Mplus_NOP);
922 WriteDOC(0, docptr, Mplus_NOP);
923 } else {
924 WriteDOC(0, docptr, NOP);
925 WriteDOC(0, docptr, NOP);
926 WriteDOC(0, docptr, NOP);
929 for (i = 0; i < 6; i++) {
930 if (DoC_is_MillenniumPlus(doc))
931 ecc_code[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
932 else
933 ecc_code[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
934 if (ecc_code[i] != empty_write_ecc[i])
935 emptymatch = 0;
937 if (DoC_is_MillenniumPlus(doc))
938 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
939 else
940 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
941 return 0;
944 static int doc200x_correct_data(struct mtd_info *mtd, u_char *dat,
945 u_char *read_ecc, u_char *isnull)
947 int i, ret = 0;
948 struct nand_chip *this = mtd->priv;
949 struct doc_priv *doc = this->priv;
950 void __iomem *docptr = doc->virtadr;
951 uint8_t calc_ecc[6];
952 volatile u_char dummy;
953 int emptymatch = 1;
955 /* flush the pipeline */
956 if (DoC_is_2000(doc)) {
957 dummy = ReadDOC(docptr, 2k_ECCStatus);
958 dummy = ReadDOC(docptr, 2k_ECCStatus);
959 dummy = ReadDOC(docptr, 2k_ECCStatus);
960 } else if (DoC_is_MillenniumPlus(doc)) {
961 dummy = ReadDOC(docptr, Mplus_ECCConf);
962 dummy = ReadDOC(docptr, Mplus_ECCConf);
963 dummy = ReadDOC(docptr, Mplus_ECCConf);
964 } else {
965 dummy = ReadDOC(docptr, ECCConf);
966 dummy = ReadDOC(docptr, ECCConf);
967 dummy = ReadDOC(docptr, ECCConf);
970 /* Error occured ? */
971 if (dummy & 0x80) {
972 for (i = 0; i < 6; i++) {
973 if (DoC_is_MillenniumPlus(doc))
974 calc_ecc[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
975 else
976 calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
977 if (calc_ecc[i] != empty_read_syndrome[i])
978 emptymatch = 0;
980 /* If emptymatch=1, the read syndrome is consistent with an
981 all-0xff data and stored ecc block. Check the stored ecc. */
982 if (emptymatch) {
983 for (i = 0; i < 6; i++) {
984 if (read_ecc[i] == 0xff)
985 continue;
986 emptymatch = 0;
987 break;
990 /* If emptymatch still =1, check the data block. */
991 if (emptymatch) {
992 /* Note: this somewhat expensive test should not be triggered
993 often. It could be optimized away by examining the data in
994 the readbuf routine, and remembering the result. */
995 for (i = 0; i < 512; i++) {
996 if (dat[i] == 0xff)
997 continue;
998 emptymatch = 0;
999 break;
1002 /* If emptymatch still =1, this is almost certainly a freshly-
1003 erased block, in which case the ECC will not come out right.
1004 We'll suppress the error and tell the caller everything's
1005 OK. Because it is. */
1006 if (!emptymatch)
1007 ret = doc_ecc_decode(rs_decoder, dat, calc_ecc);
1008 if (ret > 0)
1009 printk(KERN_ERR "doc200x_correct_data corrected %d errors\n", ret);
1011 if (DoC_is_MillenniumPlus(doc))
1012 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
1013 else
1014 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
1015 if (no_ecc_failures && (ret == -EBADMSG)) {
1016 printk(KERN_ERR "suppressing ECC failure\n");
1017 ret = 0;
1019 return ret;
1022 //u_char mydatabuf[528];
1024 /* The strange out-of-order .oobfree list below is a (possibly unneeded)
1025 * attempt to retain compatibility. It used to read:
1026 * .oobfree = { {8, 8} }
1027 * Since that leaves two bytes unusable, it was changed. But the following
1028 * scheme might affect existing jffs2 installs by moving the cleanmarker:
1029 * .oobfree = { {6, 10} }
1030 * jffs2 seems to handle the above gracefully, but the current scheme seems
1031 * safer. The only problem with it is that any code that parses oobfree must
1032 * be able to handle out-of-order segments.
1034 static struct nand_ecclayout doc200x_oobinfo = {
1035 .eccbytes = 6,
1036 .eccpos = {0, 1, 2, 3, 4, 5},
1037 .oobfree = {{8, 8}, {6, 2}}
1040 /* Find the (I)NFTL Media Header, and optionally also the mirror media header.
1041 On successful return, buf will contain a copy of the media header for
1042 further processing. id is the string to scan for, and will presumably be
1043 either "ANAND" or "BNAND". If findmirror=1, also look for the mirror media
1044 header. The page #s of the found media headers are placed in mh0_page and
1045 mh1_page in the DOC private structure. */
1046 static int __init find_media_headers(struct mtd_info *mtd, u_char *buf, const char *id, int findmirror)
1048 struct nand_chip *this = mtd->priv;
1049 struct doc_priv *doc = this->priv;
1050 unsigned offs;
1051 int ret;
1052 size_t retlen;
1054 for (offs = 0; offs < mtd->size; offs += mtd->erasesize) {
1055 ret = mtd->read(mtd, offs, mtd->writesize, &retlen, buf);
1056 if (retlen != mtd->writesize)
1057 continue;
1058 if (ret) {
1059 printk(KERN_WARNING "ECC error scanning DOC at 0x%x\n", offs);
1061 if (memcmp(buf, id, 6))
1062 continue;
1063 printk(KERN_INFO "Found DiskOnChip %s Media Header at 0x%x\n", id, offs);
1064 if (doc->mh0_page == -1) {
1065 doc->mh0_page = offs >> this->page_shift;
1066 if (!findmirror)
1067 return 1;
1068 continue;
1070 doc->mh1_page = offs >> this->page_shift;
1071 return 2;
1073 if (doc->mh0_page == -1) {
1074 printk(KERN_WARNING "DiskOnChip %s Media Header not found.\n", id);
1075 return 0;
1077 /* Only one mediaheader was found. We want buf to contain a
1078 mediaheader on return, so we'll have to re-read the one we found. */
1079 offs = doc->mh0_page << this->page_shift;
1080 ret = mtd->read(mtd, offs, mtd->writesize, &retlen, buf);
1081 if (retlen != mtd->writesize) {
1082 /* Insanity. Give up. */
1083 printk(KERN_ERR "Read DiskOnChip Media Header once, but can't reread it???\n");
1084 return 0;
1086 return 1;
1089 static inline int __init nftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
1091 struct nand_chip *this = mtd->priv;
1092 struct doc_priv *doc = this->priv;
1093 int ret = 0;
1094 u_char *buf;
1095 struct NFTLMediaHeader *mh;
1096 const unsigned psize = 1 << this->page_shift;
1097 int numparts = 0;
1098 unsigned blocks, maxblocks;
1099 int offs, numheaders;
1101 buf = kmalloc(mtd->writesize, GFP_KERNEL);
1102 if (!buf) {
1103 printk(KERN_ERR "DiskOnChip mediaheader kmalloc failed!\n");
1104 return 0;
1106 if (!(numheaders = find_media_headers(mtd, buf, "ANAND", 1)))
1107 goto out;
1108 mh = (struct NFTLMediaHeader *)buf;
1110 le16_to_cpus(&mh->NumEraseUnits);
1111 le16_to_cpus(&mh->FirstPhysicalEUN);
1112 le32_to_cpus(&mh->FormattedSize);
1114 printk(KERN_INFO " DataOrgID = %s\n"
1115 " NumEraseUnits = %d\n"
1116 " FirstPhysicalEUN = %d\n"
1117 " FormattedSize = %d\n"
1118 " UnitSizeFactor = %d\n",
1119 mh->DataOrgID, mh->NumEraseUnits,
1120 mh->FirstPhysicalEUN, mh->FormattedSize,
1121 mh->UnitSizeFactor);
1123 blocks = mtd->size >> this->phys_erase_shift;
1124 maxblocks = min(32768U, mtd->erasesize - psize);
1126 if (mh->UnitSizeFactor == 0x00) {
1127 /* Auto-determine UnitSizeFactor. The constraints are:
1128 - There can be at most 32768 virtual blocks.
1129 - There can be at most (virtual block size - page size)
1130 virtual blocks (because MediaHeader+BBT must fit in 1).
1132 mh->UnitSizeFactor = 0xff;
1133 while (blocks > maxblocks) {
1134 blocks >>= 1;
1135 maxblocks = min(32768U, (maxblocks << 1) + psize);
1136 mh->UnitSizeFactor--;
1138 printk(KERN_WARNING "UnitSizeFactor=0x00 detected. Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor);
1141 /* NOTE: The lines below modify internal variables of the NAND and MTD
1142 layers; variables with have already been configured by nand_scan.
1143 Unfortunately, we didn't know before this point what these values
1144 should be. Thus, this code is somewhat dependant on the exact
1145 implementation of the NAND layer. */
1146 if (mh->UnitSizeFactor != 0xff) {
1147 this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
1148 mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
1149 printk(KERN_INFO "Setting virtual erase size to %d\n", mtd->erasesize);
1150 blocks = mtd->size >> this->bbt_erase_shift;
1151 maxblocks = min(32768U, mtd->erasesize - psize);
1154 if (blocks > maxblocks) {
1155 printk(KERN_ERR "UnitSizeFactor of 0x%02x is inconsistent with device size. Aborting.\n", mh->UnitSizeFactor);
1156 goto out;
1159 /* Skip past the media headers. */
1160 offs = max(doc->mh0_page, doc->mh1_page);
1161 offs <<= this->page_shift;
1162 offs += mtd->erasesize;
1164 if (show_firmware_partition == 1) {
1165 parts[0].name = " DiskOnChip Firmware / Media Header partition";
1166 parts[0].offset = 0;
1167 parts[0].size = offs;
1168 numparts = 1;
1171 parts[numparts].name = " DiskOnChip BDTL partition";
1172 parts[numparts].offset = offs;
1173 parts[numparts].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift;
1175 offs += parts[numparts].size;
1176 numparts++;
1178 if (offs < mtd->size) {
1179 parts[numparts].name = " DiskOnChip Remainder partition";
1180 parts[numparts].offset = offs;
1181 parts[numparts].size = mtd->size - offs;
1182 numparts++;
1185 ret = numparts;
1186 out:
1187 kfree(buf);
1188 return ret;
1191 /* This is a stripped-down copy of the code in inftlmount.c */
1192 static inline int __init inftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
1194 struct nand_chip *this = mtd->priv;
1195 struct doc_priv *doc = this->priv;
1196 int ret = 0;
1197 u_char *buf;
1198 struct INFTLMediaHeader *mh;
1199 struct INFTLPartition *ip;
1200 int numparts = 0;
1201 int blocks;
1202 int vshift, lastvunit = 0;
1203 int i;
1204 int end = mtd->size;
1206 if (inftl_bbt_write)
1207 end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift);
1209 buf = kmalloc(mtd->writesize, GFP_KERNEL);
1210 if (!buf) {
1211 printk(KERN_ERR "DiskOnChip mediaheader kmalloc failed!\n");
1212 return 0;
1215 if (!find_media_headers(mtd, buf, "BNAND", 0))
1216 goto out;
1217 doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift);
1218 mh = (struct INFTLMediaHeader *)buf;
1220 le32_to_cpus(&mh->NoOfBootImageBlocks);
1221 le32_to_cpus(&mh->NoOfBinaryPartitions);
1222 le32_to_cpus(&mh->NoOfBDTLPartitions);
1223 le32_to_cpus(&mh->BlockMultiplierBits);
1224 le32_to_cpus(&mh->FormatFlags);
1225 le32_to_cpus(&mh->PercentUsed);
1227 printk(KERN_INFO " bootRecordID = %s\n"
1228 " NoOfBootImageBlocks = %d\n"
1229 " NoOfBinaryPartitions = %d\n"
1230 " NoOfBDTLPartitions = %d\n"
1231 " BlockMultiplerBits = %d\n"
1232 " FormatFlgs = %d\n"
1233 " OsakVersion = %d.%d.%d.%d\n"
1234 " PercentUsed = %d\n",
1235 mh->bootRecordID, mh->NoOfBootImageBlocks,
1236 mh->NoOfBinaryPartitions,
1237 mh->NoOfBDTLPartitions,
1238 mh->BlockMultiplierBits, mh->FormatFlags,
1239 ((unsigned char *) &mh->OsakVersion)[0] & 0xf,
1240 ((unsigned char *) &mh->OsakVersion)[1] & 0xf,
1241 ((unsigned char *) &mh->OsakVersion)[2] & 0xf,
1242 ((unsigned char *) &mh->OsakVersion)[3] & 0xf,
1243 mh->PercentUsed);
1245 vshift = this->phys_erase_shift + mh->BlockMultiplierBits;
1247 blocks = mtd->size >> vshift;
1248 if (blocks > 32768) {
1249 printk(KERN_ERR "BlockMultiplierBits=%d is inconsistent with device size. Aborting.\n", mh->BlockMultiplierBits);
1250 goto out;
1253 blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift);
1254 if (inftl_bbt_write && (blocks > mtd->erasesize)) {
1255 printk(KERN_ERR "Writeable BBTs spanning more than one erase block are not yet supported. FIX ME!\n");
1256 goto out;
1259 /* Scan the partitions */
1260 for (i = 0; (i < 4); i++) {
1261 ip = &(mh->Partitions[i]);
1262 le32_to_cpus(&ip->virtualUnits);
1263 le32_to_cpus(&ip->firstUnit);
1264 le32_to_cpus(&ip->lastUnit);
1265 le32_to_cpus(&ip->flags);
1266 le32_to_cpus(&ip->spareUnits);
1267 le32_to_cpus(&ip->Reserved0);
1269 printk(KERN_INFO " PARTITION[%d] ->\n"
1270 " virtualUnits = %d\n"
1271 " firstUnit = %d\n"
1272 " lastUnit = %d\n"
1273 " flags = 0x%x\n"
1274 " spareUnits = %d\n",
1275 i, ip->virtualUnits, ip->firstUnit,
1276 ip->lastUnit, ip->flags,
1277 ip->spareUnits);
1279 if ((show_firmware_partition == 1) &&
1280 (i == 0) && (ip->firstUnit > 0)) {
1281 parts[0].name = " DiskOnChip IPL / Media Header partition";
1282 parts[0].offset = 0;
1283 parts[0].size = mtd->erasesize * ip->firstUnit;
1284 numparts = 1;
1287 if (ip->flags & INFTL_BINARY)
1288 parts[numparts].name = " DiskOnChip BDK partition";
1289 else
1290 parts[numparts].name = " DiskOnChip BDTL partition";
1291 parts[numparts].offset = ip->firstUnit << vshift;
1292 parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift;
1293 numparts++;
1294 if (ip->lastUnit > lastvunit)
1295 lastvunit = ip->lastUnit;
1296 if (ip->flags & INFTL_LAST)
1297 break;
1299 lastvunit++;
1300 if ((lastvunit << vshift) < end) {
1301 parts[numparts].name = " DiskOnChip Remainder partition";
1302 parts[numparts].offset = lastvunit << vshift;
1303 parts[numparts].size = end - parts[numparts].offset;
1304 numparts++;
1306 ret = numparts;
1307 out:
1308 kfree(buf);
1309 return ret;
1312 static int __init nftl_scan_bbt(struct mtd_info *mtd)
1314 int ret, numparts;
1315 struct nand_chip *this = mtd->priv;
1316 struct doc_priv *doc = this->priv;
1317 struct mtd_partition parts[2];
1319 memset((char *)parts, 0, sizeof(parts));
1320 /* On NFTL, we have to find the media headers before we can read the
1321 BBTs, since they're stored in the media header eraseblocks. */
1322 numparts = nftl_partscan(mtd, parts);
1323 if (!numparts)
1324 return -EIO;
1325 this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1326 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1327 NAND_BBT_VERSION;
1328 this->bbt_td->veroffs = 7;
1329 this->bbt_td->pages[0] = doc->mh0_page + 1;
1330 if (doc->mh1_page != -1) {
1331 this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1332 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1333 NAND_BBT_VERSION;
1334 this->bbt_md->veroffs = 7;
1335 this->bbt_md->pages[0] = doc->mh1_page + 1;
1336 } else {
1337 this->bbt_md = NULL;
1340 /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
1341 At least as nand_bbt.c is currently written. */
1342 if ((ret = nand_scan_bbt(mtd, NULL)))
1343 return ret;
1344 add_mtd_device(mtd);
1345 #ifdef CONFIG_MTD_PARTITIONS
1346 if (!no_autopart)
1347 add_mtd_partitions(mtd, parts, numparts);
1348 #endif
1349 return 0;
1352 static int __init inftl_scan_bbt(struct mtd_info *mtd)
1354 int ret, numparts;
1355 struct nand_chip *this = mtd->priv;
1356 struct doc_priv *doc = this->priv;
1357 struct mtd_partition parts[5];
1359 if (this->numchips > doc->chips_per_floor) {
1360 printk(KERN_ERR "Multi-floor INFTL devices not yet supported.\n");
1361 return -EIO;
1364 if (DoC_is_MillenniumPlus(doc)) {
1365 this->bbt_td->options = NAND_BBT_2BIT | NAND_BBT_ABSPAGE;
1366 if (inftl_bbt_write)
1367 this->bbt_td->options |= NAND_BBT_WRITE;
1368 this->bbt_td->pages[0] = 2;
1369 this->bbt_md = NULL;
1370 } else {
1371 this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
1372 if (inftl_bbt_write)
1373 this->bbt_td->options |= NAND_BBT_WRITE;
1374 this->bbt_td->offs = 8;
1375 this->bbt_td->len = 8;
1376 this->bbt_td->veroffs = 7;
1377 this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1378 this->bbt_td->reserved_block_code = 0x01;
1379 this->bbt_td->pattern = "MSYS_BBT";
1381 this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
1382 if (inftl_bbt_write)
1383 this->bbt_md->options |= NAND_BBT_WRITE;
1384 this->bbt_md->offs = 8;
1385 this->bbt_md->len = 8;
1386 this->bbt_md->veroffs = 7;
1387 this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1388 this->bbt_md->reserved_block_code = 0x01;
1389 this->bbt_md->pattern = "TBB_SYSM";
1392 /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
1393 At least as nand_bbt.c is currently written. */
1394 if ((ret = nand_scan_bbt(mtd, NULL)))
1395 return ret;
1396 memset((char *)parts, 0, sizeof(parts));
1397 numparts = inftl_partscan(mtd, parts);
1398 /* At least for now, require the INFTL Media Header. We could probably
1399 do without it for non-INFTL use, since all it gives us is
1400 autopartitioning, but I want to give it more thought. */
1401 if (!numparts)
1402 return -EIO;
1403 add_mtd_device(mtd);
1404 #ifdef CONFIG_MTD_PARTITIONS
1405 if (!no_autopart)
1406 add_mtd_partitions(mtd, parts, numparts);
1407 #endif
1408 return 0;
1411 static inline int __init doc2000_init(struct mtd_info *mtd)
1413 struct nand_chip *this = mtd->priv;
1414 struct doc_priv *doc = this->priv;
1416 this->read_byte = doc2000_read_byte;
1417 this->write_buf = doc2000_writebuf;
1418 this->read_buf = doc2000_readbuf;
1419 this->verify_buf = doc2000_verifybuf;
1420 this->scan_bbt = nftl_scan_bbt;
1422 doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
1423 doc2000_count_chips(mtd);
1424 mtd->name = "DiskOnChip 2000 (NFTL Model)";
1425 return (4 * doc->chips_per_floor);
1428 static inline int __init doc2001_init(struct mtd_info *mtd)
1430 struct nand_chip *this = mtd->priv;
1431 struct doc_priv *doc = this->priv;
1433 this->read_byte = doc2001_read_byte;
1434 this->write_buf = doc2001_writebuf;
1435 this->read_buf = doc2001_readbuf;
1436 this->verify_buf = doc2001_verifybuf;
1438 ReadDOC(doc->virtadr, ChipID);
1439 ReadDOC(doc->virtadr, ChipID);
1440 ReadDOC(doc->virtadr, ChipID);
1441 if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) {
1442 /* It's not a Millennium; it's one of the newer
1443 DiskOnChip 2000 units with a similar ASIC.
1444 Treat it like a Millennium, except that it
1445 can have multiple chips. */
1446 doc2000_count_chips(mtd);
1447 mtd->name = "DiskOnChip 2000 (INFTL Model)";
1448 this->scan_bbt = inftl_scan_bbt;
1449 return (4 * doc->chips_per_floor);
1450 } else {
1451 /* Bog-standard Millennium */
1452 doc->chips_per_floor = 1;
1453 mtd->name = "DiskOnChip Millennium";
1454 this->scan_bbt = nftl_scan_bbt;
1455 return 1;
1459 static inline int __init doc2001plus_init(struct mtd_info *mtd)
1461 struct nand_chip *this = mtd->priv;
1462 struct doc_priv *doc = this->priv;
1464 this->read_byte = doc2001plus_read_byte;
1465 this->write_buf = doc2001plus_writebuf;
1466 this->read_buf = doc2001plus_readbuf;
1467 this->verify_buf = doc2001plus_verifybuf;
1468 this->scan_bbt = inftl_scan_bbt;
1469 this->cmd_ctrl = NULL;
1470 this->select_chip = doc2001plus_select_chip;
1471 this->cmdfunc = doc2001plus_command;
1472 this->ecc.hwctl = doc2001plus_enable_hwecc;
1474 doc->chips_per_floor = 1;
1475 mtd->name = "DiskOnChip Millennium Plus";
1477 return 1;
1480 static int __init doc_probe(unsigned long physadr)
1482 unsigned char ChipID;
1483 struct mtd_info *mtd;
1484 struct nand_chip *nand;
1485 struct doc_priv *doc;
1486 void __iomem *virtadr;
1487 unsigned char save_control;
1488 unsigned char tmp, tmpb, tmpc;
1489 int reg, len, numchips;
1490 int ret = 0;
1492 virtadr = ioremap(physadr, DOC_IOREMAP_LEN);
1493 if (!virtadr) {
1494 printk(KERN_ERR "Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n", DOC_IOREMAP_LEN, physadr);
1495 return -EIO;
1498 /* It's not possible to cleanly detect the DiskOnChip - the
1499 * bootup procedure will put the device into reset mode, and
1500 * it's not possible to talk to it without actually writing
1501 * to the DOCControl register. So we store the current contents
1502 * of the DOCControl register's location, in case we later decide
1503 * that it's not a DiskOnChip, and want to put it back how we
1504 * found it.
1506 save_control = ReadDOC(virtadr, DOCControl);
1508 /* Reset the DiskOnChip ASIC */
1509 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
1510 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
1512 /* Enable the DiskOnChip ASIC */
1513 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
1514 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
1516 ChipID = ReadDOC(virtadr, ChipID);
1518 switch (ChipID) {
1519 case DOC_ChipID_Doc2k:
1520 reg = DoC_2k_ECCStatus;
1521 break;
1522 case DOC_ChipID_DocMil:
1523 reg = DoC_ECCConf;
1524 break;
1525 case DOC_ChipID_DocMilPlus16:
1526 case DOC_ChipID_DocMilPlus32:
1527 case 0:
1528 /* Possible Millennium Plus, need to do more checks */
1529 /* Possibly release from power down mode */
1530 for (tmp = 0; (tmp < 4); tmp++)
1531 ReadDOC(virtadr, Mplus_Power);
1533 /* Reset the Millennium Plus ASIC */
1534 tmp = DOC_MODE_RESET | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
1535 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1536 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1538 mdelay(1);
1539 /* Enable the Millennium Plus ASIC */
1540 tmp = DOC_MODE_NORMAL | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
1541 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1542 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1543 mdelay(1);
1545 ChipID = ReadDOC(virtadr, ChipID);
1547 switch (ChipID) {
1548 case DOC_ChipID_DocMilPlus16:
1549 reg = DoC_Mplus_Toggle;
1550 break;
1551 case DOC_ChipID_DocMilPlus32:
1552 printk(KERN_ERR "DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
1553 default:
1554 ret = -ENODEV;
1555 goto notfound;
1557 break;
1559 default:
1560 ret = -ENODEV;
1561 goto notfound;
1563 /* Check the TOGGLE bit in the ECC register */
1564 tmp = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1565 tmpb = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1566 tmpc = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1567 if ((tmp == tmpb) || (tmp != tmpc)) {
1568 printk(KERN_WARNING "Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr);
1569 ret = -ENODEV;
1570 goto notfound;
1573 for (mtd = doclist; mtd; mtd = doc->nextdoc) {
1574 unsigned char oldval;
1575 unsigned char newval;
1576 nand = mtd->priv;
1577 doc = nand->priv;
1578 /* Use the alias resolution register to determine if this is
1579 in fact the same DOC aliased to a new address. If writes
1580 to one chip's alias resolution register change the value on
1581 the other chip, they're the same chip. */
1582 if (ChipID == DOC_ChipID_DocMilPlus16) {
1583 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1584 newval = ReadDOC(virtadr, Mplus_AliasResolution);
1585 } else {
1586 oldval = ReadDOC(doc->virtadr, AliasResolution);
1587 newval = ReadDOC(virtadr, AliasResolution);
1589 if (oldval != newval)
1590 continue;
1591 if (ChipID == DOC_ChipID_DocMilPlus16) {
1592 WriteDOC(~newval, virtadr, Mplus_AliasResolution);
1593 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1594 WriteDOC(newval, virtadr, Mplus_AliasResolution); // restore it
1595 } else {
1596 WriteDOC(~newval, virtadr, AliasResolution);
1597 oldval = ReadDOC(doc->virtadr, AliasResolution);
1598 WriteDOC(newval, virtadr, AliasResolution); // restore it
1600 newval = ~newval;
1601 if (oldval == newval) {
1602 printk(KERN_DEBUG "Found alias of DOC at 0x%lx to 0x%lx\n", doc->physadr, physadr);
1603 goto notfound;
1607 printk(KERN_NOTICE "DiskOnChip found at 0x%lx\n", physadr);
1609 len = sizeof(struct mtd_info) +
1610 sizeof(struct nand_chip) + sizeof(struct doc_priv) + (2 * sizeof(struct nand_bbt_descr));
1611 mtd = kzalloc(len, GFP_KERNEL);
1612 if (!mtd) {
1613 printk(KERN_ERR "DiskOnChip kmalloc (%d bytes) failed!\n", len);
1614 ret = -ENOMEM;
1615 goto fail;
1618 nand = (struct nand_chip *) (mtd + 1);
1619 doc = (struct doc_priv *) (nand + 1);
1620 nand->bbt_td = (struct nand_bbt_descr *) (doc + 1);
1621 nand->bbt_md = nand->bbt_td + 1;
1623 mtd->priv = nand;
1624 mtd->owner = THIS_MODULE;
1626 nand->priv = doc;
1627 nand->select_chip = doc200x_select_chip;
1628 nand->cmd_ctrl = doc200x_hwcontrol;
1629 nand->dev_ready = doc200x_dev_ready;
1630 nand->waitfunc = doc200x_wait;
1631 nand->block_bad = doc200x_block_bad;
1632 nand->ecc.hwctl = doc200x_enable_hwecc;
1633 nand->ecc.calculate = doc200x_calculate_ecc;
1634 nand->ecc.correct = doc200x_correct_data;
1636 nand->ecc.layout = &doc200x_oobinfo;
1637 nand->ecc.mode = NAND_ECC_HW_SYNDROME;
1638 nand->ecc.size = 512;
1639 nand->ecc.bytes = 6;
1640 nand->options = NAND_USE_FLASH_BBT;
1642 doc->physadr = physadr;
1643 doc->virtadr = virtadr;
1644 doc->ChipID = ChipID;
1645 doc->curfloor = -1;
1646 doc->curchip = -1;
1647 doc->mh0_page = -1;
1648 doc->mh1_page = -1;
1649 doc->nextdoc = doclist;
1651 if (ChipID == DOC_ChipID_Doc2k)
1652 numchips = doc2000_init(mtd);
1653 else if (ChipID == DOC_ChipID_DocMilPlus16)
1654 numchips = doc2001plus_init(mtd);
1655 else
1656 numchips = doc2001_init(mtd);
1658 if ((ret = nand_scan(mtd, numchips))) {
1659 /* DBB note: i believe nand_release is necessary here, as
1660 buffers may have been allocated in nand_base. Check with
1661 Thomas. FIX ME! */
1662 /* nand_release will call del_mtd_device, but we haven't yet
1663 added it. This is handled without incident by
1664 del_mtd_device, as far as I can tell. */
1665 nand_release(mtd);
1666 kfree(mtd);
1667 goto fail;
1670 /* Success! */
1671 doclist = mtd;
1672 return 0;
1674 notfound:
1675 /* Put back the contents of the DOCControl register, in case it's not
1676 actually a DiskOnChip. */
1677 WriteDOC(save_control, virtadr, DOCControl);
1678 fail:
1679 iounmap(virtadr);
1680 return ret;
1683 static void release_nanddoc(void)
1685 struct mtd_info *mtd, *nextmtd;
1686 struct nand_chip *nand;
1687 struct doc_priv *doc;
1689 for (mtd = doclist; mtd; mtd = nextmtd) {
1690 nand = mtd->priv;
1691 doc = nand->priv;
1693 nextmtd = doc->nextdoc;
1694 nand_release(mtd);
1695 iounmap(doc->virtadr);
1696 kfree(mtd);
1700 static int __init init_nanddoc(void)
1702 int i, ret = 0;
1704 /* We could create the decoder on demand, if memory is a concern.
1705 * This way we have it handy, if an error happens
1707 * Symbolsize is 10 (bits)
1708 * Primitve polynomial is x^10+x^3+1
1709 * first consecutive root is 510
1710 * primitve element to generate roots = 1
1711 * generator polinomial degree = 4
1713 rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS);
1714 if (!rs_decoder) {
1715 printk(KERN_ERR "DiskOnChip: Could not create a RS decoder\n");
1716 return -ENOMEM;
1719 if (doc_config_location) {
1720 printk(KERN_INFO "Using configured DiskOnChip probe address 0x%lx\n", doc_config_location);
1721 ret = doc_probe(doc_config_location);
1722 if (ret < 0)
1723 goto outerr;
1724 } else {
1725 for (i = 0; (doc_locations[i] != 0xffffffff); i++) {
1726 doc_probe(doc_locations[i]);
1729 /* No banner message any more. Print a message if no DiskOnChip
1730 found, so the user knows we at least tried. */
1731 if (!doclist) {
1732 printk(KERN_INFO "No valid DiskOnChip devices found\n");
1733 ret = -ENODEV;
1734 goto outerr;
1736 return 0;
1737 outerr:
1738 free_rs(rs_decoder);
1739 return ret;
1742 static void __exit cleanup_nanddoc(void)
1744 /* Cleanup the nand/DoC resources */
1745 release_nanddoc();
1747 /* Free the reed solomon resources */
1748 if (rs_decoder) {
1749 free_rs(rs_decoder);
1753 module_init(init_nanddoc);
1754 module_exit(cleanup_nanddoc);
1756 MODULE_LICENSE("GPL");
1757 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1758 MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver");