MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / drivers / mtd / nand / diskonchip.c
blob5d1e181375ac395bc960b50abffdd7e14adbe729
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.55 2005/11/07 11:14:30 gleixner 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_NAND_DISKONCHIP_PROBE_ADDRESS
39 #define CONFIG_MTD_NAND_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_NAND_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 int setcol;
82 int activecol;
83 int page_addr;
84 struct mtd_info *nextdoc;
87 /* This is the syndrome computed by the HW ecc generator upon reading an empty
88 page, one with all 0xff for data and stored ecc code. */
89 static u_char empty_read_syndrome[6] = { 0x26, 0xff, 0x6d, 0x47, 0x73, 0x7a };
91 /* This is the ecc value computed by the HW ecc generator upon writing an empty
92 page, one with all 0xff for data. */
93 static u_char empty_write_ecc[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
95 #define INFTL_BBT_RESERVED_BLOCKS 4
97 #define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
98 #define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
99 #define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
101 static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
102 unsigned int bitmask);
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 static int no_autopart = 0;
115 module_param(no_autopart, int, 0);
117 static int show_firmware_partition = 0;
118 module_param(show_firmware_partition, int, 0);
120 #ifdef MTD_NAND_DISKONCHIP_BBTWRITE
121 static int inftl_bbt_write = 1;
122 #else
123 static int inftl_bbt_write = 0;
124 #endif
125 module_param(inftl_bbt_write, int, 0);
127 static unsigned long doc_config_location = CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS;
128 module_param(doc_config_location, ulong, 0);
129 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) || index == (SECTOR_SIZE + 1)) {
213 val = (uint8_t) (errval[i] >> (2 + bitpos));
214 parity ^= val;
215 if (index < SECTOR_SIZE)
216 data[index] ^= val;
218 index = ((pos >> 3) + 1) ^ 1;
219 bitpos = (bitpos + 10) & 7;
220 if (bitpos == 0)
221 bitpos = 8;
222 if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
223 val = (uint8_t) (errval[i] << (8 - bitpos));
224 parity ^= val;
225 if (index < SECTOR_SIZE)
226 data[index] ^= val;
230 /* If the parity is wrong, no rescue possible */
231 return parity ? -1 : nerr;
234 static void DoC_Delay(struct doc_priv *doc, unsigned short cycles)
236 volatile char dummy;
237 int i;
239 for (i = 0; i < cycles; i++) {
240 if (DoC_is_Millennium(doc))
241 dummy = ReadDOC(doc->virtadr, NOP);
242 else if (DoC_is_MillenniumPlus(doc))
243 dummy = ReadDOC(doc->virtadr, Mplus_NOP);
244 else
245 dummy = ReadDOC(doc->virtadr, DOCStatus);
250 #define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
252 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
253 static int _DoC_WaitReady(struct doc_priv *doc)
255 void __iomem *docptr = doc->virtadr;
256 unsigned long timeo = jiffies + (HZ * 10);
258 if (debug)
259 printk("_DoC_WaitReady...\n");
260 /* Out-of-line routine to wait for chip response */
261 if (DoC_is_MillenniumPlus(doc)) {
262 while ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
263 if (time_after(jiffies, timeo)) {
264 printk("_DoC_WaitReady timed out.\n");
265 return -EIO;
267 udelay(1);
268 cond_resched();
270 } else {
271 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
272 if (time_after(jiffies, timeo)) {
273 printk("_DoC_WaitReady timed out.\n");
274 return -EIO;
276 udelay(1);
277 cond_resched();
281 return 0;
284 static inline int DoC_WaitReady(struct doc_priv *doc)
286 void __iomem *docptr = doc->virtadr;
287 int ret = 0;
289 if (DoC_is_MillenniumPlus(doc)) {
290 DoC_Delay(doc, 4);
292 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
293 /* Call the out-of-line routine to wait */
294 ret = _DoC_WaitReady(doc);
295 } else {
296 DoC_Delay(doc, 4);
298 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
299 /* Call the out-of-line routine to wait */
300 ret = _DoC_WaitReady(doc);
301 DoC_Delay(doc, 2);
304 if (debug)
305 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)
316 printk("write_byte %02x\n", datum);
317 WriteDOC(datum, docptr, CDSNSlowIO);
318 WriteDOC(datum, docptr, 2k_CDSN_IO);
321 static u_char doc2000_read_byte(struct mtd_info *mtd)
323 struct nand_chip *this = mtd->priv;
324 struct doc_priv *doc = this->priv;
325 void __iomem *docptr = doc->virtadr;
326 u_char ret;
328 ReadDOC(docptr, CDSNSlowIO);
329 DoC_Delay(doc, 2);
330 ret = ReadDOC(docptr, 2k_CDSN_IO);
331 if (debug)
332 printk("read_byte returns %02x\n", ret);
333 return ret;
336 static void doc2000_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
338 struct nand_chip *this = mtd->priv;
339 struct doc_priv *doc = this->priv;
340 void __iomem *docptr = doc->virtadr;
341 int i;
342 if (debug)
343 printk("writebuf of %d bytes: ", len);
344 for (i = 0; i < len; i++) {
345 WriteDOC_(buf[i], docptr, DoC_2k_CDSN_IO + i);
346 if (debug && i < 16)
347 printk("%02x ", buf[i]);
349 if (debug)
350 printk("\n");
353 static void doc2000_readbuf(struct mtd_info *mtd, u_char *buf, int len)
355 struct nand_chip *this = mtd->priv;
356 struct doc_priv *doc = this->priv;
357 void __iomem *docptr = doc->virtadr;
358 int i;
360 if (debug)
361 printk("readbuf of %d bytes: ", len);
363 for (i = 0; i < len; i++) {
364 buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
368 static void doc2000_readbuf_dword(struct mtd_info *mtd, u_char *buf, int len)
370 struct nand_chip *this = mtd->priv;
371 struct doc_priv *doc = this->priv;
372 void __iomem *docptr = doc->virtadr;
373 int i;
375 if (debug)
376 printk("readbuf_dword of %d bytes: ", len);
378 if (unlikely((((unsigned long)buf) | len) & 3)) {
379 for (i = 0; i < len; i++) {
380 *(uint8_t *) (&buf[i]) = ReadDOC(docptr, 2k_CDSN_IO + i);
382 } else {
383 for (i = 0; i < len; i += 4) {
384 *(uint32_t *) (&buf[i]) = readl(docptr + DoC_2k_CDSN_IO + i);
389 static int doc2000_verifybuf(struct mtd_info *mtd, const u_char *buf, int len)
391 struct nand_chip *this = mtd->priv;
392 struct doc_priv *doc = this->priv;
393 void __iomem *docptr = doc->virtadr;
394 int i;
396 for (i = 0; i < len; i++)
397 if (buf[i] != ReadDOC(docptr, 2k_CDSN_IO))
398 return -EFAULT;
399 return 0;
402 static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr)
404 struct nand_chip *this = mtd->priv;
405 struct doc_priv *doc = this->priv;
406 uint16_t ret;
408 doc200x_select_chip(mtd, nr);
409 doc200x_hwcontrol(mtd, NAND_CMD_READID,
410 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
411 doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
412 doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
414 /* We cant' use dev_ready here, but at least we wait for the
415 * command to complete
417 udelay(50);
419 ret = this->read_byte(mtd) << 8;
420 ret |= this->read_byte(mtd);
422 if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) {
423 /* First chip probe. See if we get same results by 32-bit access */
424 union {
425 uint32_t dword;
426 uint8_t byte[4];
427 } ident;
428 void __iomem *docptr = doc->virtadr;
430 doc200x_hwcontrol(mtd, NAND_CMD_READID,
431 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
432 doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
433 doc200x_hwcontrol(mtd, NAND_CMD_NONE,
434 NAND_NCE | NAND_CTRL_CHANGE);
436 udelay(50);
438 ident.dword = readl(docptr + DoC_2k_CDSN_IO);
439 if (((ident.byte[0] << 8) | ident.byte[1]) == ret) {
440 printk(KERN_INFO "DiskOnChip 2000 responds to DWORD access\n");
441 this->read_buf = &doc2000_readbuf_dword;
445 return ret;
448 static void __init doc2000_count_chips(struct mtd_info *mtd)
450 struct nand_chip *this = mtd->priv;
451 struct doc_priv *doc = this->priv;
452 uint16_t mfrid;
453 int i;
455 /* Max 4 chips per floor on DiskOnChip 2000 */
456 doc->chips_per_floor = 4;
458 /* Find out what the first chip is */
459 mfrid = doc200x_ident_chip(mtd, 0);
461 /* Find how many chips in each floor. */
462 for (i = 1; i < 4; i++) {
463 if (doc200x_ident_chip(mtd, i) != mfrid)
464 break;
466 doc->chips_per_floor = i;
467 printk(KERN_DEBUG "Detected %d chips per floor.\n", i);
470 static int doc200x_wait(struct mtd_info *mtd, struct nand_chip *this)
472 struct doc_priv *doc = this->priv;
474 int status;
476 DoC_WaitReady(doc);
477 this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
478 DoC_WaitReady(doc);
479 status = (int)this->read_byte(mtd);
481 return status;
484 static void doc2001_write_byte(struct mtd_info *mtd, u_char datum)
486 struct nand_chip *this = mtd->priv;
487 struct doc_priv *doc = this->priv;
488 void __iomem *docptr = doc->virtadr;
490 WriteDOC(datum, docptr, CDSNSlowIO);
491 WriteDOC(datum, docptr, Mil_CDSN_IO);
492 WriteDOC(datum, docptr, WritePipeTerm);
495 static u_char doc2001_read_byte(struct mtd_info *mtd)
497 struct nand_chip *this = mtd->priv;
498 struct doc_priv *doc = this->priv;
499 void __iomem *docptr = doc->virtadr;
501 //ReadDOC(docptr, CDSNSlowIO);
502 /* 11.4.5 -- delay twice to allow extended length cycle */
503 DoC_Delay(doc, 2);
504 ReadDOC(docptr, ReadPipeInit);
505 //return ReadDOC(docptr, Mil_CDSN_IO);
506 return ReadDOC(docptr, LastDataRead);
509 static void doc2001_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
511 struct nand_chip *this = mtd->priv;
512 struct doc_priv *doc = this->priv;
513 void __iomem *docptr = doc->virtadr;
514 int i;
516 for (i = 0; i < len; i++)
517 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
518 /* Terminate write pipeline */
519 WriteDOC(0x00, docptr, WritePipeTerm);
522 static void doc2001_readbuf(struct mtd_info *mtd, u_char *buf, int len)
524 struct nand_chip *this = mtd->priv;
525 struct doc_priv *doc = this->priv;
526 void __iomem *docptr = doc->virtadr;
527 int i;
529 /* Start read pipeline */
530 ReadDOC(docptr, ReadPipeInit);
532 for (i = 0; i < len - 1; i++)
533 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
535 /* Terminate read pipeline */
536 buf[i] = ReadDOC(docptr, LastDataRead);
539 static int doc2001_verifybuf(struct mtd_info *mtd, const u_char *buf, int len)
541 struct nand_chip *this = mtd->priv;
542 struct doc_priv *doc = this->priv;
543 void __iomem *docptr = doc->virtadr;
544 int i;
546 /* Start read pipeline */
547 ReadDOC(docptr, ReadPipeInit);
549 for (i = 0; i < len - 1; i++)
550 if (buf[i] != ReadDOC(docptr, Mil_CDSN_IO)) {
551 ReadDOC(docptr, LastDataRead);
552 return i;
554 if (buf[i] != ReadDOC(docptr, LastDataRead))
555 return i;
556 return 0;
559 static int doc2001plus_addroffset(struct mtd_info *mtd, int *columnp)
561 if (*columnp >= 512) {
562 /* OOB area */
563 *columnp -= 512;
564 return NAND_CMD_READOOB;
566 if (*columnp < 256) {
567 /* First 256 bytes --> READ0 */
568 return NAND_CMD_READ0;
570 *columnp -= 256;
571 return NAND_CMD_READ1;
575 * Translate the given offset into the appropriate command and offset.
576 * This does the mapping using the 16bit interleave layout defined by
577 * M-Systems, and looks like this for a sector pair:
578 * +-----------+-------+-------+-------+--------------+---------+-----------+
579 * | 0 --- 511 |512-517|518-519|520-521| 522 --- 1033 |1034-1039|1040 - 1055|
580 * +-----------+-------+-------+-------+--------------+---------+-----+-----+
581 * | Data 0 | ECC 0 |Flags0 |Flags1 | Data 1 |ECC 1 | OOB0| OOB1|
582 * +-----------+-------+-------+-------+--------------+---------+-----+-----+
583 * Thing to remember is that 2 physical pages are interleaved together, and
584 * then mapped in the above odd looking way.
586 static int doc2001plus_addroffset32(struct mtd_info *mtd, int command, int *columnp, int page_addr)
588 struct nand_chip *this = mtd->priv;
589 struct doc_priv *doc = this->priv;
590 int col = *columnp;
592 if (command == NAND_CMD_READ1)
593 col += 256;
594 if (command == NAND_CMD_READOOB)
595 col += 512;
596 doc->setcol = col;
597 doc->activecol = col;
598 doc->page_addr = page_addr;
600 /* Even numbered pages first. */
601 if ((page_addr & 0x1) == 0) {
602 if (col < 512) {
603 *columnp = (col >> 1);
604 return NAND_CMD_READ0;
606 if (col < 520) {
607 *columnp = (col - 512) >> 1;
608 return NAND_CMD_READ1;
610 *columnp = ((col - 520) + 16) >> 1;
611 return NAND_CMD_READOOB;
614 /* Odd number pages next */
615 if (col < 502) {
616 *columnp = (col + 10) >> 1;
617 return NAND_CMD_READ1;
619 if (col < 518) {
620 *columnp = (col - 502) >> 1;
621 return NAND_CMD_READOOB;
623 if (col < 520) {
624 *columnp = ((col - 518) + 8) >> 1;
625 return NAND_CMD_READ1;
627 *columnp = ((col - 520) + 24) >> 1;
628 return NAND_CMD_READOOB;
631 static void doc2001plus_command (struct mtd_info *mtd, unsigned command, int column, int page_addr)
633 struct nand_chip *this = mtd->priv;
634 struct doc_priv *doc = this->priv;
635 void __iomem *docptr = doc->virtadr;
636 int readcmd = NAND_CMD_READ0;
639 * Must terminate write pipeline before sending any commands
640 * to the device.
642 if (command == NAND_CMD_PAGEPROG) {
643 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
644 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
648 * Write out the command to the device. If using the interleaved
649 * device then we may need to map the command and address parts.
650 * Ick :-(
652 switch (command) {
653 case NAND_CMD_SEQIN:
654 if (doc->ChipID == DOC_ChipID_DocMilPlus32) {
655 readcmd = doc2001plus_addroffset32(mtd, command, &column, page_addr);
656 page_addr >>= 1;
657 } else {
658 readcmd = doc2001plus_addroffset(mtd, &column);
660 WriteDOC(readcmd, docptr, Mplus_FlashCmd);
661 break;
662 case NAND_CMD_READ0:
663 case NAND_CMD_READ1:
664 case NAND_CMD_READOOB:
665 if (doc->ChipID == DOC_ChipID_DocMilPlus32) {
666 command = doc2001plus_addroffset32(mtd, command, &column, page_addr);
667 page_addr >>= 1;
669 break;
670 default:
671 break;
674 WriteDOC(command, docptr, Mplus_FlashCmd);
675 WriteDOC(0, docptr, Mplus_WritePipeTerm);
676 WriteDOC(0, docptr, Mplus_WritePipeTerm);
678 if (column != -1 || page_addr != -1) {
679 /* Serially input address */
680 if (column != -1) {
681 /* Adjust columns for 16 bit buswidth */
682 if (this->options & NAND_BUSWIDTH_16)
683 column >>= 1;
684 WriteDOC(column, docptr, Mplus_FlashAddress);
686 if (page_addr != -1) {
687 WriteDOC((unsigned char) (page_addr & 0xff), docptr, Mplus_FlashAddress);
688 WriteDOC((unsigned char) ((page_addr >> 8) & 0xff), docptr, Mplus_FlashAddress);
689 /* One more address cycle for higher density devices */
690 if (this->chipsize & 0x0c000000) {
691 WriteDOC((unsigned char) ((page_addr >> 16) & 0x0f), docptr, Mplus_FlashAddress);
692 printk("high density\n");
695 WriteDOC(0, docptr, Mplus_WritePipeTerm);
696 WriteDOC(0, docptr, Mplus_WritePipeTerm);
697 /* deassert ALE */
698 if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 || command == NAND_CMD_READOOB || command == NAND_CMD_READID)
699 WriteDOC(0, docptr, Mplus_FlashControl);
703 * program and erase have their own busy handlers
704 * status and sequential in needs no delay
706 switch (command) {
708 case NAND_CMD_PAGEPROG:
709 case NAND_CMD_ERASE1:
710 case NAND_CMD_ERASE2:
711 case NAND_CMD_SEQIN:
712 case NAND_CMD_STATUS:
713 return;
715 case NAND_CMD_RESET:
716 if (this->dev_ready)
717 break;
718 udelay(this->chip_delay);
719 WriteDOC(NAND_CMD_STATUS, docptr, Mplus_FlashCmd);
720 WriteDOC(0, docptr, Mplus_WritePipeTerm);
721 WriteDOC(0, docptr, Mplus_WritePipeTerm);
722 while ( !(this->read_byte(mtd) & 0x40));
723 return;
725 /* This applies to read commands */
726 default:
728 * If we don't have access to the busy pin, we apply the given
729 * command delay
731 if (!this->dev_ready) {
732 udelay (this->chip_delay);
733 return;
737 /* Apply this short delay always to ensure that we do wait tWB in
738 * any case on any machine. */
739 ndelay (100);
740 /* wait until command is processed */
741 while (!this->dev_ready(mtd));
744 static u_char doc2001plus_read_byte(struct mtd_info *mtd)
746 struct nand_chip *this = mtd->priv;
747 struct doc_priv *doc = this->priv;
748 void __iomem *docptr = doc->virtadr;
749 u_char ret;
751 ReadDOC(docptr, Mplus_ReadPipeInit);
752 ReadDOC(docptr, Mplus_ReadPipeInit);
753 ret = ReadDOC(docptr, Mplus_LastDataRead);
754 if (debug)
755 printk("read_byte returns %02x\n", ret);
756 return ret;
759 static int doc2001plus_maxlinesiz(struct doc_priv *doc)
761 if (doc->ChipID == DOC_ChipID_DocMilPlus32) {
762 if (doc->activecol < (512+6))
763 return (512+6 - doc->activecol);
764 if (doc->activecol < (512+8))
765 return (512+8 - doc->activecol);
767 return (512+16 - doc->activecol);
770 static void doc2001plus_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
772 struct nand_chip *this = mtd->priv;
773 struct doc_priv *doc = this->priv;
774 void __iomem *docptr = doc->virtadr;
775 int i, want, siz, loop;
777 for (loop = 0, want = len; ((loop < 3) && (want > 0)); loop++) {
778 if (debug)printk("writebuf of %d bytes: ", len);
780 /* Figure out maximum strait line read size */
781 siz = doc2001plus_maxlinesiz(doc);
782 if (siz > want)
783 siz = want;
785 if (doc->setcol != doc->activecol) {
786 /* Finish existing write, and restart at new pos */
787 doc2001plus_command(mtd, NAND_CMD_PAGEPROG, -1, -1);
788 doc200x_wait(mtd, this);
789 /* FIXME: handle failure cases */
791 doc2001plus_command(mtd, NAND_CMD_RESET, -1, -1);
792 doc2001plus_command(mtd, NAND_CMD_SEQIN, doc->activecol, doc->page_addr);
795 for (i=0; i < len; i++) {
796 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
797 if (debug && i < 16)
798 printk("%02x ", buf[i]);
800 if (debug) printk("\n");
802 want -= siz;
803 doc->activecol += siz;
805 if (debug)
806 printk("\n");
809 static void doc2001plus_readbuf(struct mtd_info *mtd, u_char *buf, int len)
811 struct nand_chip *this = mtd->priv;
812 struct doc_priv *doc = this->priv;
813 void __iomem *docptr = doc->virtadr;
814 u_char *nbuf = buf;
815 int i, want, siz, loop;
817 for (loop = 0, want = len; ((loop < 3) && (want > 0)); loop++) {
818 if (debug)printk("readbuf of %d bytes: ", len);
820 /* Figure out maximum strait line read size */
821 siz = doc2001plus_maxlinesiz(doc);
822 if (siz > want)
823 siz = want;
825 if (doc->setcol != doc->activecol)
826 doc2001plus_command(mtd, NAND_CMD_READ0, doc->activecol, doc->page_addr);
828 /* Start read pipeline */
829 ReadDOC(docptr, Mplus_ReadPipeInit);
830 ReadDOC(docptr, Mplus_ReadPipeInit);
832 for (i=0; i < siz-2; i++, nbuf++) {
833 *nbuf = ReadDOC(docptr, Mil_CDSN_IO);
834 if (debug && i < 16)
835 printk("%02x ", *nbuf);
838 /* Terminate read pipeline */
839 *nbuf = ReadDOC(docptr, Mplus_LastDataRead);
840 if (debug && i < 16)
841 printk("%02x ", *nbuf);
842 nbuf++;
843 *nbuf = ReadDOC(docptr, Mplus_LastDataRead);
844 if (debug && i < 16)
845 printk("%02x ", *nbuf);
846 nbuf++;
847 if (debug) printk("\n");
849 want -= siz;
850 doc->activecol += siz;
854 static int doc2001plus_verifybuf(struct mtd_info *mtd, const u_char *buf, int len)
856 struct nand_chip *this = mtd->priv;
857 struct doc_priv *doc = this->priv;
858 void __iomem *docptr = doc->virtadr;
859 int i;
861 if (debug)
862 printk("verifybuf of %d bytes: ", len);
864 /* Start read pipeline */
865 ReadDOC(docptr, Mplus_ReadPipeInit);
866 ReadDOC(docptr, Mplus_ReadPipeInit);
868 for (i = 0; i < len - 2; i++)
869 if (buf[i] != ReadDOC(docptr, Mil_CDSN_IO)) {
870 ReadDOC(docptr, Mplus_LastDataRead);
871 ReadDOC(docptr, Mplus_LastDataRead);
872 return i;
874 if (buf[len - 2] != ReadDOC(docptr, Mplus_LastDataRead))
875 return len - 2;
876 if (buf[len - 1] != ReadDOC(docptr, Mplus_LastDataRead))
877 return len - 1;
878 return 0;
881 static void doc2001plus_select_chip(struct mtd_info *mtd, int chip)
883 struct nand_chip *this = mtd->priv;
884 struct doc_priv *doc = this->priv;
885 void __iomem *docptr = doc->virtadr;
886 int floor = 0;
888 if (debug)
889 printk("select chip (%d)\n", chip);
891 if (chip == -1) {
892 /* Disable flash internally */
893 WriteDOC(0, docptr, Mplus_FlashSelect);
894 return;
897 floor = chip / doc->chips_per_floor;
898 chip -= (floor * doc->chips_per_floor);
900 /* Assert ChipEnable and deassert WriteProtect */
901 WriteDOC((DOC_FLASH_CE), docptr, Mplus_FlashSelect);
902 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
904 doc->curchip = chip;
905 doc->curfloor = floor;
908 static void doc200x_select_chip(struct mtd_info *mtd, int chip)
910 struct nand_chip *this = mtd->priv;
911 struct doc_priv *doc = this->priv;
912 void __iomem *docptr = doc->virtadr;
913 int floor = 0;
915 if (debug)
916 printk("select chip (%d)\n", chip);
918 if (chip == -1)
919 return;
921 floor = chip / doc->chips_per_floor;
922 chip -= (floor * doc->chips_per_floor);
924 /* 11.4.4 -- deassert CE before changing chip */
925 doc200x_hwcontrol(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
927 WriteDOC(floor, docptr, FloorSelect);
928 WriteDOC(chip, docptr, CDSNDeviceSelect);
930 doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
932 doc->curchip = chip;
933 doc->curfloor = floor;
936 #define CDSN_CTRL_MSK (CDSN_CTRL_CE | CDSN_CTRL_CLE | CDSN_CTRL_ALE)
938 static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
939 unsigned int ctrl)
941 struct nand_chip *this = mtd->priv;
942 struct doc_priv *doc = this->priv;
943 void __iomem *docptr = doc->virtadr;
945 if (ctrl & NAND_CTRL_CHANGE) {
946 doc->CDSNControl &= ~CDSN_CTRL_MSK;
947 doc->CDSNControl |= ctrl & CDSN_CTRL_MSK;
948 if (debug)
949 printk("hwcontrol(%d): %02x\n", cmd, doc->CDSNControl);
950 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
951 /* 11.4.3 -- 4 NOPs after CSDNControl write */
952 DoC_Delay(doc, 4);
954 if (cmd != NAND_CMD_NONE) {
955 if (DoC_is_2000(doc))
956 doc2000_write_byte(mtd, cmd);
957 else
958 doc2001_write_byte(mtd, cmd);
962 static int doc200x_dev_ready(struct mtd_info *mtd)
964 struct nand_chip *this = mtd->priv;
965 struct doc_priv *doc = this->priv;
966 void __iomem *docptr = doc->virtadr;
968 if (DoC_is_MillenniumPlus(doc)) {
969 /* 11.4.2 -- must NOP four times before checking FR/B# */
970 DoC_Delay(doc, 4);
971 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
972 if (debug)
973 printk("not ready\n");
974 return 0;
976 if (debug)
977 printk("was ready\n");
978 return 1;
979 } else {
980 /* 11.4.2 -- must NOP four times before checking FR/B# */
981 DoC_Delay(doc, 4);
982 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
983 if (debug)
984 printk("not ready\n");
985 return 0;
987 /* 11.4.2 -- Must NOP twice if it's ready */
988 DoC_Delay(doc, 2);
989 if (debug)
990 printk("was ready\n");
991 return 1;
995 static int doc200x_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
997 /* This is our last resort if we couldn't find or create a BBT. Just
998 pretend all blocks are good. */
999 return 0;
1002 static void doc200x_enable_hwecc(struct mtd_info *mtd, int mode)
1004 struct nand_chip *this = mtd->priv;
1005 struct doc_priv *doc = this->priv;
1006 void __iomem *docptr = doc->virtadr;
1008 /* Prime the ECC engine */
1009 switch (mode) {
1010 case NAND_ECC_READ:
1011 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
1012 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
1013 break;
1014 case NAND_ECC_WRITE:
1015 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
1016 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
1017 break;
1021 static void doc2001plus_enable_hwecc(struct mtd_info *mtd, int mode)
1023 struct nand_chip *this = mtd->priv;
1024 struct doc_priv *doc = this->priv;
1025 void __iomem *docptr = doc->virtadr;
1027 /* Prime the ECC engine */
1028 switch (mode) {
1029 case NAND_ECC_READ:
1030 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
1031 WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
1032 break;
1033 case NAND_ECC_WRITE:
1034 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
1035 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
1036 break;
1040 /* This code is only called on write */
1041 static int doc200x_calculate_ecc(struct mtd_info *mtd, const u_char *dat, unsigned char *ecc_code)
1043 struct nand_chip *this = mtd->priv;
1044 struct doc_priv *doc = this->priv;
1045 void __iomem *docptr = doc->virtadr;
1046 int i;
1047 int emptymatch = 1;
1049 /* flush the pipeline */
1050 if (DoC_is_2000(doc)) {
1051 WriteDOC(doc->CDSNControl & ~CDSN_CTRL_FLASH_IO, docptr, CDSNControl);
1052 WriteDOC(0, docptr, 2k_CDSN_IO);
1053 WriteDOC(0, docptr, 2k_CDSN_IO);
1054 WriteDOC(0, docptr, 2k_CDSN_IO);
1055 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
1056 } else if (DoC_is_MillenniumPlus(doc)) {
1057 WriteDOC(0, docptr, Mplus_NOP);
1058 WriteDOC(0, docptr, Mplus_NOP);
1059 WriteDOC(0, docptr, Mplus_NOP);
1060 } else {
1061 WriteDOC(0, docptr, NOP);
1062 WriteDOC(0, docptr, NOP);
1063 WriteDOC(0, docptr, NOP);
1066 for (i = 0; i < 6; i++) {
1067 if (DoC_is_MillenniumPlus(doc))
1068 ecc_code[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
1069 else
1070 ecc_code[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
1071 if (ecc_code[i] != empty_write_ecc[i])
1072 emptymatch = 0;
1074 if (DoC_is_MillenniumPlus(doc))
1075 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
1076 else
1077 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
1078 #if 0
1079 /* If emptymatch=1, we might have an all-0xff data buffer. Check. */
1080 if (emptymatch) {
1081 /* Note: this somewhat expensive test should not be triggered
1082 often. It could be optimized away by examining the data in
1083 the writebuf routine, and remembering the result. */
1084 for (i = 0; i < 512; i++) {
1085 if (dat[i] == 0xff)
1086 continue;
1087 emptymatch = 0;
1088 break;
1091 /* If emptymatch still =1, we do have an all-0xff data buffer.
1092 Return all-0xff ecc value instead of the computed one, so
1093 it'll look just like a freshly-erased page. */
1094 if (emptymatch)
1095 memset(ecc_code, 0xff, 6);
1096 #endif
1097 return 0;
1100 static int doc200x_correct_data(struct mtd_info *mtd, u_char *dat,
1101 u_char *read_ecc, u_char *isnull)
1103 int i, ret = 0;
1104 struct nand_chip *this = mtd->priv;
1105 struct doc_priv *doc = this->priv;
1106 void __iomem *docptr = doc->virtadr;
1107 uint8_t calc_ecc[6];
1108 volatile u_char dummy;
1109 int emptymatch = 1;
1111 /* flush the pipeline */
1112 if (DoC_is_2000(doc)) {
1113 dummy = ReadDOC(docptr, 2k_ECCStatus);
1114 dummy = ReadDOC(docptr, 2k_ECCStatus);
1115 dummy = ReadDOC(docptr, 2k_ECCStatus);
1116 } else if (DoC_is_MillenniumPlus(doc)) {
1117 dummy = ReadDOC(docptr, Mplus_ECCConf);
1118 dummy = ReadDOC(docptr, Mplus_ECCConf);
1119 dummy = ReadDOC(docptr, Mplus_ECCConf);
1120 } else {
1121 dummy = ReadDOC(docptr, ECCConf);
1122 dummy = ReadDOC(docptr, ECCConf);
1123 dummy = ReadDOC(docptr, ECCConf);
1126 /* Error occured ? */
1127 if (dummy & 0x80) {
1128 for (i = 0; i < 6; i++) {
1129 if (DoC_is_MillenniumPlus(doc))
1130 calc_ecc[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
1131 else
1132 calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
1133 if (calc_ecc[i] != empty_read_syndrome[i])
1134 emptymatch = 0;
1136 /* If emptymatch=1, the read syndrome is consistent with an
1137 all-0xff data and stored ecc block. Check the stored ecc. */
1138 if (emptymatch) {
1139 for (i = 0; i < 6; i++) {
1140 if (read_ecc[i] == 0xff)
1141 continue;
1142 emptymatch = 0;
1143 break;
1146 /* If emptymatch still =1, check the data block. */
1147 if (emptymatch) {
1148 /* Note: this somewhat expensive test should not be triggered
1149 often. It could be optimized away by examining the data in
1150 the readbuf routine, and remembering the result. */
1151 for (i = 0; i < 512; i++) {
1152 if (dat[i] == 0xff)
1153 continue;
1154 emptymatch = 0;
1155 break;
1158 /* If emptymatch still =1, this is almost certainly a freshly-
1159 erased block, in which case the ECC will not come out right.
1160 We'll suppress the error and tell the caller everything's
1161 OK. Because it is. */
1162 if (!emptymatch)
1163 ret = doc_ecc_decode(rs_decoder, dat, calc_ecc);
1164 if (ret > 0)
1165 printk(KERN_ERR "doc200x_correct_data corrected %d errors\n", ret);
1167 if (DoC_is_MillenniumPlus(doc))
1168 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
1169 else
1170 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
1171 if (no_ecc_failures && (ret == -1)) {
1172 printk(KERN_ERR "suppressing ECC failure\n");
1173 ret = 0;
1175 return ret;
1178 //u_char mydatabuf[528];
1180 /* The strange out-of-order .oobfree list below is a (possibly unneeded)
1181 * attempt to retain compatibility. It used to read:
1182 * .oobfree = { {8, 8} }
1183 * Since that leaves two bytes unusable, it was changed. But the following
1184 * scheme might affect existing jffs2 installs by moving the cleanmarker:
1185 * .oobfree = { {6, 10} }
1186 * jffs2 seems to handle the above gracefully, but the current scheme seems
1187 * safer. The only problem with it is that any code that parses oobfree must
1188 * be able to handle out-of-order segments.
1190 static struct nand_ecclayout doc200x_oobinfo = {
1191 .eccbytes = 6,
1192 .eccpos = {0, 1, 2, 3, 4, 5},
1193 .oobfree = {{8, 8}, {6, 2}}
1196 /* Find the (I)NFTL Media Header, and optionally also the mirror media header.
1197 On sucessful return, buf will contain a copy of the media header for
1198 further processing. id is the string to scan for, and will presumably be
1199 either "ANAND" or "BNAND". If findmirror=1, also look for the mirror media
1200 header. The page #s of the found media headers are placed in mh0_page and
1201 mh1_page in the DOC private structure. */
1202 static int __init find_media_headers(struct mtd_info *mtd, u_char *buf, const char *id, int findmirror)
1204 struct nand_chip *this = mtd->priv;
1205 struct doc_priv *doc = this->priv;
1206 unsigned offs;
1207 int ret;
1208 size_t retlen;
1210 for (offs = 0; offs < mtd->size; offs += mtd->erasesize) {
1211 ret = mtd->read(mtd, offs, mtd->writesize, &retlen, buf);
1212 if (retlen != mtd->writesize)
1213 continue;
1214 if (ret) {
1215 printk(KERN_WARNING "ECC error scanning DOC at 0x%x\n", offs);
1217 if (memcmp(buf, id, 6))
1218 continue;
1219 printk(KERN_INFO "Found DiskOnChip %s Media Header at 0x%x\n", id, offs);
1220 if (doc->mh0_page == -1) {
1221 doc->mh0_page = offs >> this->page_shift;
1222 if (!findmirror)
1223 return 1;
1224 continue;
1226 doc->mh1_page = offs >> this->page_shift;
1227 return 2;
1229 if (doc->mh0_page == -1) {
1230 printk(KERN_WARNING "DiskOnChip %s Media Header not found.\n", id);
1231 return 0;
1233 /* Only one mediaheader was found. We want buf to contain a
1234 mediaheader on return, so we'll have to re-read the one we found. */
1235 offs = doc->mh0_page << this->page_shift;
1236 ret = mtd->read(mtd, offs, mtd->writesize, &retlen, buf);
1237 if (retlen != mtd->writesize) {
1238 /* Insanity. Give up. */
1239 printk(KERN_ERR "Read DiskOnChip Media Header once, but can't reread it???\n");
1240 return 0;
1242 return 1;
1245 static inline int __init nftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
1247 struct nand_chip *this = mtd->priv;
1248 struct doc_priv *doc = this->priv;
1249 int ret = 0;
1250 u_char *buf;
1251 struct NFTLMediaHeader *mh;
1252 const unsigned psize = 1 << this->page_shift;
1253 int numparts = 0;
1254 unsigned blocks, maxblocks;
1255 int offs, numheaders;
1257 buf = kmalloc(mtd->writesize, GFP_KERNEL);
1258 if (!buf) {
1259 printk(KERN_ERR "DiskOnChip mediaheader kmalloc failed!\n");
1260 return 0;
1262 if (!(numheaders = find_media_headers(mtd, buf, "ANAND", 1)))
1263 goto out;
1264 mh = (struct NFTLMediaHeader *)buf;
1266 mh->NumEraseUnits = le16_to_cpu(mh->NumEraseUnits);
1267 mh->FirstPhysicalEUN = le16_to_cpu(mh->FirstPhysicalEUN);
1268 mh->FormattedSize = le32_to_cpu(mh->FormattedSize);
1270 printk(KERN_INFO " DataOrgID = %s\n"
1271 " NumEraseUnits = %d\n"
1272 " FirstPhysicalEUN = %d\n"
1273 " FormattedSize = %d\n"
1274 " UnitSizeFactor = %d\n",
1275 mh->DataOrgID, mh->NumEraseUnits,
1276 mh->FirstPhysicalEUN, mh->FormattedSize,
1277 mh->UnitSizeFactor);
1279 blocks = mtd->size >> this->phys_erase_shift;
1280 maxblocks = min(32768U, mtd->erasesize - psize);
1282 if (mh->UnitSizeFactor == 0x00) {
1283 /* Auto-determine UnitSizeFactor. The constraints are:
1284 - There can be at most 32768 virtual blocks.
1285 - There can be at most (virtual block size - page size)
1286 virtual blocks (because MediaHeader+BBT must fit in 1).
1288 mh->UnitSizeFactor = 0xff;
1289 while (blocks > maxblocks) {
1290 blocks >>= 1;
1291 maxblocks = min(32768U, (maxblocks << 1) + psize);
1292 mh->UnitSizeFactor--;
1294 printk(KERN_WARNING "UnitSizeFactor=0x00 detected. Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor);
1297 /* NOTE: The lines below modify internal variables of the NAND and MTD
1298 layers; variables with have already been configured by nand_scan.
1299 Unfortunately, we didn't know before this point what these values
1300 should be. Thus, this code is somewhat dependant on the exact
1301 implementation of the NAND layer. */
1302 if (mh->UnitSizeFactor != 0xff) {
1303 this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
1304 mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
1305 printk(KERN_INFO "Setting virtual erase size to %d\n", mtd->erasesize);
1306 blocks = mtd->size >> this->bbt_erase_shift;
1307 maxblocks = min(32768U, mtd->erasesize - psize);
1310 if (blocks > maxblocks) {
1311 printk(KERN_ERR "UnitSizeFactor of 0x%02x is inconsistent with device size. Aborting.\n", mh->UnitSizeFactor);
1312 goto out;
1315 /* Skip past the media headers. */
1316 offs = max(doc->mh0_page, doc->mh1_page);
1317 offs <<= this->page_shift;
1318 offs += mtd->erasesize;
1320 if (show_firmware_partition == 1) {
1321 parts[0].name = " DiskOnChip Firmware / Media Header partition";
1322 parts[0].offset = 0;
1323 parts[0].size = offs;
1324 numparts = 1;
1327 parts[numparts].name = " DiskOnChip BDTL partition";
1328 parts[numparts].offset = offs;
1329 parts[numparts].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift;
1331 offs += parts[numparts].size;
1332 numparts++;
1334 if (offs < mtd->size) {
1335 parts[numparts].name = " DiskOnChip Remainder partition";
1336 parts[numparts].offset = offs;
1337 parts[numparts].size = mtd->size - offs;
1338 numparts++;
1341 ret = numparts;
1342 out:
1343 kfree(buf);
1344 return ret;
1347 /* This is a stripped-down copy of the code in inftlmount.c */
1348 static inline int __init inftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
1350 struct nand_chip *this = mtd->priv;
1351 struct doc_priv *doc = this->priv;
1352 int ret = 0;
1353 u_char *buf;
1354 struct INFTLMediaHeader *mh;
1355 struct INFTLPartition *ip;
1356 int numparts = 0;
1357 int blocks;
1358 int vshift, lastvunit = 0;
1359 int i;
1360 int end = mtd->size;
1362 if (inftl_bbt_write)
1363 end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift);
1365 buf = kmalloc(mtd->writesize, GFP_KERNEL);
1366 if (!buf) {
1367 printk(KERN_ERR "DiskOnChip mediaheader kmalloc failed!\n");
1368 return 0;
1371 if (!find_media_headers(mtd, buf, "BNAND", 0))
1372 goto out;
1373 doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift);
1374 mh = (struct INFTLMediaHeader *)buf;
1376 mh->NoOfBootImageBlocks = le32_to_cpu(mh->NoOfBootImageBlocks);
1377 mh->NoOfBinaryPartitions = le32_to_cpu(mh->NoOfBinaryPartitions);
1378 mh->NoOfBDTLPartitions = le32_to_cpu(mh->NoOfBDTLPartitions);
1379 mh->BlockMultiplierBits = le32_to_cpu(mh->BlockMultiplierBits);
1380 mh->FormatFlags = le32_to_cpu(mh->FormatFlags);
1381 mh->PercentUsed = le32_to_cpu(mh->PercentUsed);
1383 printk(KERN_INFO " bootRecordID = %s\n"
1384 " NoOfBootImageBlocks = %d\n"
1385 " NoOfBinaryPartitions = %d\n"
1386 " NoOfBDTLPartitions = %d\n"
1387 " BlockMultiplerBits = %d\n"
1388 " FormatFlgs = %d\n"
1389 " OsakVersion = %d.%d.%d.%d\n"
1390 " PercentUsed = %d\n",
1391 mh->bootRecordID, mh->NoOfBootImageBlocks,
1392 mh->NoOfBinaryPartitions,
1393 mh->NoOfBDTLPartitions,
1394 mh->BlockMultiplierBits, mh->FormatFlags,
1395 ((unsigned char *) &mh->OsakVersion)[0] & 0xf,
1396 ((unsigned char *) &mh->OsakVersion)[1] & 0xf,
1397 ((unsigned char *) &mh->OsakVersion)[2] & 0xf,
1398 ((unsigned char *) &mh->OsakVersion)[3] & 0xf,
1399 mh->PercentUsed);
1401 vshift = this->phys_erase_shift + mh->BlockMultiplierBits;
1403 blocks = mtd->size >> vshift;
1404 if (blocks > 32768) {
1405 printk(KERN_ERR "BlockMultiplierBits=%d is inconsistent with device size. Aborting.\n", mh->BlockMultiplierBits);
1406 goto out;
1409 blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift);
1410 if (inftl_bbt_write && (blocks > mtd->erasesize)) {
1411 printk(KERN_ERR "Writeable BBTs spanning more than one erase block are not yet supported. FIX ME!\n");
1412 goto out;
1415 /* Scan the partitions */
1416 for (i = 0; (i < 4); i++) {
1417 ip = &(mh->Partitions[i]);
1418 ip->virtualUnits = le32_to_cpu(ip->virtualUnits);
1419 ip->firstUnit = le32_to_cpu(ip->firstUnit);
1420 ip->lastUnit = le32_to_cpu(ip->lastUnit);
1421 ip->flags = le32_to_cpu(ip->flags);
1422 ip->spareUnits = le32_to_cpu(ip->spareUnits);
1423 ip->Reserved0 = le32_to_cpu(ip->Reserved0);
1425 printk(KERN_INFO " PARTITION[%d] ->\n"
1426 " virtualUnits = %d\n"
1427 " firstUnit = %d\n"
1428 " lastUnit = %d\n"
1429 " flags = 0x%x\n"
1430 " spareUnits = %d\n",
1431 i, ip->virtualUnits, ip->firstUnit,
1432 ip->lastUnit, ip->flags,
1433 ip->spareUnits);
1435 if ((show_firmware_partition == 1) &&
1436 (i == 0) && (ip->firstUnit > 0)) {
1437 parts[0].name = " DiskOnChip IPL / Media Header partition";
1438 parts[0].offset = 0;
1439 parts[0].size = mtd->erasesize * ip->firstUnit;
1440 numparts = 1;
1443 if (ip->flags & INFTL_BINARY)
1444 parts[numparts].name = " DiskOnChip BDK partition";
1445 else
1446 parts[numparts].name = " DiskOnChip BDTL partition";
1447 parts[numparts].offset = ip->firstUnit << vshift;
1448 parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift;
1449 numparts++;
1450 if (ip->lastUnit > lastvunit)
1451 lastvunit = ip->lastUnit;
1452 if (ip->flags & INFTL_LAST)
1453 break;
1455 lastvunit++;
1456 if ((lastvunit << vshift) < end) {
1457 parts[numparts].name = " DiskOnChip Remainder partition";
1458 parts[numparts].offset = lastvunit << vshift;
1459 parts[numparts].size = end - parts[numparts].offset;
1460 numparts++;
1462 ret = numparts;
1463 out:
1464 kfree(buf);
1465 return ret;
1468 static int __init nftl_scan_bbt(struct mtd_info *mtd)
1470 int ret, numparts;
1471 struct nand_chip *this = mtd->priv;
1472 struct doc_priv *doc = this->priv;
1473 struct mtd_partition parts[2];
1475 memset((char *)parts, 0, sizeof(parts));
1476 /* On NFTL, we have to find the media headers before we can read the
1477 BBTs, since they're stored in the media header eraseblocks. */
1478 numparts = nftl_partscan(mtd, parts);
1479 if (!numparts)
1480 return -EIO;
1481 this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1482 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1483 NAND_BBT_VERSION;
1484 this->bbt_td->veroffs = 7;
1485 this->bbt_td->pages[0] = doc->mh0_page + 1;
1486 if (doc->mh1_page != -1) {
1487 this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1488 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1489 NAND_BBT_VERSION;
1490 this->bbt_md->veroffs = 7;
1491 this->bbt_md->pages[0] = doc->mh1_page + 1;
1492 } else {
1493 this->bbt_md = NULL;
1496 /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
1497 At least as nand_bbt.c is currently written. */
1498 if ((ret = nand_scan_bbt(mtd, NULL)))
1499 return ret;
1500 add_mtd_device(mtd);
1501 #ifdef CONFIG_MTD_PARTITIONS
1502 if (!no_autopart)
1503 add_mtd_partitions(mtd, parts, numparts);
1504 #endif
1505 return 0;
1508 static int __init inftl_scan_bbt(struct mtd_info *mtd)
1510 int ret, numparts;
1511 struct nand_chip *this = mtd->priv;
1512 struct doc_priv *doc = this->priv;
1513 struct mtd_partition parts[5];
1515 if (this->numchips > doc->chips_per_floor) {
1516 printk(KERN_ERR "Multi-floor INFTL devices not yet supported.\n");
1517 return -EIO;
1520 if (DoC_is_MillenniumPlus(doc)) {
1521 this->bbt_td->options = NAND_BBT_2BIT | NAND_BBT_ABSPAGE;
1522 if (inftl_bbt_write)
1523 this->bbt_td->options |= NAND_BBT_WRITE;
1524 if (doc->ChipID == DOC_ChipID_DocMilPlus32) {
1525 mtd->erasesize <<= 1;
1526 this->bbt_td->pages[0] = 4;
1527 } else
1528 this->bbt_td->pages[0] = 2;
1529 this->bbt_md = NULL;
1530 } else {
1531 this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
1532 if (inftl_bbt_write)
1533 this->bbt_td->options |= NAND_BBT_WRITE;
1534 this->bbt_td->offs = 8;
1535 this->bbt_td->len = 8;
1536 this->bbt_td->veroffs = 7;
1537 this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1538 this->bbt_td->reserved_block_code = 0x01;
1539 this->bbt_td->pattern = "MSYS_BBT";
1541 this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
1542 if (inftl_bbt_write)
1543 this->bbt_md->options |= NAND_BBT_WRITE;
1544 this->bbt_md->offs = 8;
1545 this->bbt_md->len = 8;
1546 this->bbt_md->veroffs = 7;
1547 this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1548 this->bbt_md->reserved_block_code = 0x01;
1549 this->bbt_md->pattern = "TBB_SYSM";
1552 /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
1553 At least as nand_bbt.c is currently written. */
1554 if ((ret = nand_scan_bbt(mtd, NULL)))
1555 return ret;
1556 memset((char *)parts, 0, sizeof(parts));
1557 numparts = inftl_partscan(mtd, parts);
1558 /* At least for now, require the INFTL Media Header. We could probably
1559 do without it for non-INFTL use, since all it gives us is
1560 autopartitioning, but I want to give it more thought. */
1561 if (!numparts)
1562 numparts = nftl_partscan(mtd, parts);
1563 if (!numparts) return -EIO;
1564 add_mtd_device(mtd);
1565 #ifdef CONFIG_MTD_PARTITIONS
1566 if (!no_autopart)
1567 add_mtd_partitions(mtd, parts, numparts);
1568 #endif
1569 return 0;
1572 static inline int __init doc2000_init(struct mtd_info *mtd)
1574 struct nand_chip *this = mtd->priv;
1575 struct doc_priv *doc = this->priv;
1577 this->read_byte = doc2000_read_byte;
1578 this->write_buf = doc2000_writebuf;
1579 this->read_buf = doc2000_readbuf;
1580 this->verify_buf = doc2000_verifybuf;
1581 this->scan_bbt = nftl_scan_bbt;
1583 doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
1584 doc2000_count_chips(mtd);
1585 mtd->name = "DiskOnChip 2000 (NFTL Model)";
1586 return (4 * doc->chips_per_floor);
1589 static inline int __init doc2001_init(struct mtd_info *mtd)
1591 struct nand_chip *this = mtd->priv;
1592 struct doc_priv *doc = this->priv;
1594 this->read_byte = doc2001_read_byte;
1595 this->write_buf = doc2001_writebuf;
1596 this->read_buf = doc2001_readbuf;
1597 this->verify_buf = doc2001_verifybuf;
1599 ReadDOC(doc->virtadr, ChipID);
1600 ReadDOC(doc->virtadr, ChipID);
1601 ReadDOC(doc->virtadr, ChipID);
1602 if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) {
1603 /* It's not a Millennium; it's one of the newer
1604 DiskOnChip 2000 units with a similar ASIC.
1605 Treat it like a Millennium, except that it
1606 can have multiple chips. */
1607 doc2000_count_chips(mtd);
1608 mtd->name = "DiskOnChip 2000 (INFTL Model)";
1609 this->scan_bbt = inftl_scan_bbt;
1610 return (4 * doc->chips_per_floor);
1611 } else {
1612 /* Bog-standard Millennium */
1613 doc->chips_per_floor = 1;
1614 mtd->name = "DiskOnChip Millennium";
1615 this->scan_bbt = nftl_scan_bbt;
1616 return 1;
1620 static inline int __init doc2001plus_init(struct mtd_info *mtd)
1622 struct nand_chip *this = mtd->priv;
1623 struct doc_priv *doc = this->priv;
1625 this->read_byte = doc2001plus_read_byte;
1626 this->write_buf = doc2001plus_writebuf;
1627 this->read_buf = doc2001plus_readbuf;
1628 this->verify_buf = doc2001plus_verifybuf;
1629 this->scan_bbt = inftl_scan_bbt;
1630 this->cmd_ctrl = NULL;
1631 this->select_chip = doc2001plus_select_chip;
1632 this->cmdfunc = doc2001plus_command;
1633 this->ecc.hwctl = doc2001plus_enable_hwecc;
1635 doc->chips_per_floor = 1;
1636 mtd->name = "DiskOnChip Millennium Plus";
1638 return 1;
1641 static int __init doc_probe(unsigned long physadr)
1643 unsigned char ChipID;
1644 struct mtd_info *mtd;
1645 struct nand_chip *nand;
1646 struct doc_priv *doc;
1647 void __iomem *virtadr;
1648 unsigned char save_control;
1649 unsigned char tmp, tmpb, tmpc;
1650 int reg, len, numchips;
1651 int ret = 0;
1653 virtadr = ioremap(physadr, DOC_IOREMAP_LEN);
1654 if (!virtadr) {
1655 printk(KERN_ERR "Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n", DOC_IOREMAP_LEN, physadr);
1656 return -EIO;
1659 /* It's not possible to cleanly detect the DiskOnChip - the
1660 * bootup procedure will put the device into reset mode, and
1661 * it's not possible to talk to it without actually writing
1662 * to the DOCControl register. So we store the current contents
1663 * of the DOCControl register's location, in case we later decide
1664 * that it's not a DiskOnChip, and want to put it back how we
1665 * found it.
1667 save_control = ReadDOC(virtadr, DOCControl);
1669 /* Reset the DiskOnChip ASIC */
1670 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
1671 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
1673 /* Enable the DiskOnChip ASIC */
1674 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
1675 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
1677 ChipID = ReadDOC(virtadr, ChipID);
1679 switch (ChipID) {
1680 case DOC_ChipID_Doc2k:
1681 reg = DoC_2k_ECCStatus;
1682 break;
1683 case DOC_ChipID_DocMil:
1684 reg = DoC_ECCConf;
1685 break;
1686 case DOC_ChipID_DocMilPlus16:
1687 case DOC_ChipID_DocMilPlus32:
1688 case 0:
1689 /* Possible Millennium Plus, need to do more checks */
1690 /* Possibly release from power down mode */
1691 for (tmp = 0; (tmp < 4); tmp++)
1692 ReadDOC(virtadr, Mplus_Power);
1694 /* Reset the Millennium Plus ASIC */
1695 tmp = DOC_MODE_RESET | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
1696 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1697 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1699 mdelay(1);
1700 /* Enable the Millennium Plus ASIC */
1701 tmp = DOC_MODE_NORMAL | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
1702 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1703 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1704 mdelay(1);
1706 ChipID = ReadDOC(virtadr, ChipID);
1708 switch (ChipID) {
1709 case DOC_ChipID_DocMilPlus16:
1710 reg = DoC_Mplus_Toggle;
1711 break;
1712 case DOC_ChipID_DocMilPlus32:
1713 reg = DoC_Mplus_Toggle;
1714 break;
1715 default:
1716 ret = -ENODEV;
1717 goto notfound;
1719 break;
1721 default:
1722 ret = -ENODEV;
1723 goto notfound;
1725 /* Check the TOGGLE bit in the ECC register */
1726 tmp = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1727 tmpb = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1728 tmpc = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1729 if ((tmp == tmpb) || (tmp != tmpc)) {
1730 printk(KERN_WARNING "Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr);
1731 ret = -ENODEV;
1732 goto notfound;
1735 for (mtd = doclist; mtd; mtd = doc->nextdoc) {
1736 unsigned char oldval;
1737 unsigned char newval;
1738 nand = mtd->priv;
1739 doc = nand->priv;
1740 /* Use the alias resolution register to determine if this is
1741 in fact the same DOC aliased to a new address. If writes
1742 to one chip's alias resolution register change the value on
1743 the other chip, they're the same chip. */
1744 if ((ChipID == DOC_ChipID_DocMilPlus16) ||
1745 (ChipID == DOC_ChipID_DocMilPlus32)) {
1746 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1747 newval = ReadDOC(virtadr, Mplus_AliasResolution);
1748 } else {
1749 oldval = ReadDOC(doc->virtadr, AliasResolution);
1750 newval = ReadDOC(virtadr, AliasResolution);
1752 if (oldval != newval)
1753 continue;
1754 if ((ChipID == DOC_ChipID_DocMilPlus16) ||
1755 (ChipID == DOC_ChipID_DocMilPlus32)) {
1756 WriteDOC(~newval, virtadr, Mplus_AliasResolution);
1757 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1758 WriteDOC(newval, virtadr, Mplus_AliasResolution); // restore it
1759 } else {
1760 WriteDOC(~newval, virtadr, AliasResolution);
1761 oldval = ReadDOC(doc->virtadr, AliasResolution);
1762 WriteDOC(newval, virtadr, AliasResolution); // restore it
1764 newval = ~newval;
1765 if (oldval == newval) {
1766 printk(KERN_DEBUG "Found alias of DOC at 0x%lx to 0x%lx\n", doc->physadr, physadr);
1767 goto notfound;
1771 printk(KERN_NOTICE "DiskOnChip found at 0x%lx\n", physadr);
1773 len = sizeof(struct mtd_info) +
1774 sizeof(struct nand_chip) + sizeof(struct doc_priv) + (2 * sizeof(struct nand_bbt_descr));
1775 mtd = kmalloc(len, GFP_KERNEL);
1776 if (!mtd) {
1777 printk(KERN_ERR "DiskOnChip kmalloc (%d bytes) failed!\n", len);
1778 ret = -ENOMEM;
1779 goto fail;
1781 memset(mtd, 0, len);
1783 nand = (struct nand_chip *) (mtd + 1);
1784 doc = (struct doc_priv *) (nand + 1);
1785 nand->bbt_td = (struct nand_bbt_descr *) (doc + 1);
1786 nand->bbt_md = nand->bbt_td + 1;
1788 mtd->priv = nand;
1789 mtd->owner = THIS_MODULE;
1791 nand->priv = doc;
1792 nand->select_chip = doc200x_select_chip;
1793 nand->cmd_ctrl = doc200x_hwcontrol;
1794 nand->dev_ready = doc200x_dev_ready;
1795 nand->waitfunc = doc200x_wait;
1796 nand->block_bad = doc200x_block_bad;
1797 nand->ecc.hwctl = doc200x_enable_hwecc;
1798 nand->ecc.calculate = doc200x_calculate_ecc;
1799 nand->ecc.correct = doc200x_correct_data;
1801 nand->ecc.layout = &doc200x_oobinfo;
1802 nand->ecc.mode = NAND_ECC_HW_SYNDROME;
1803 nand->ecc.size = 512;
1804 nand->ecc.bytes = 6;
1805 nand->options = NAND_USE_FLASH_BBT;
1807 doc->physadr = physadr;
1808 doc->virtadr = virtadr;
1809 doc->ChipID = ChipID;
1810 doc->curfloor = -1;
1811 doc->curchip = -1;
1812 doc->mh0_page = -1;
1813 doc->mh1_page = -1;
1814 doc->nextdoc = doclist;
1816 if (ChipID == DOC_ChipID_Doc2k)
1817 numchips = doc2000_init(mtd);
1818 else if ((ChipID == DOC_ChipID_DocMilPlus16) ||
1819 (ChipID == DOC_ChipID_DocMilPlus32))
1820 numchips = doc2001plus_init(mtd);
1821 else
1822 numchips = doc2001_init(mtd);
1824 if ((ret = nand_scan(mtd, numchips))) {
1825 /* DBB note: i believe nand_release is necessary here, as
1826 buffers may have been allocated in nand_base. Check with
1827 Thomas. FIX ME! */
1828 /* nand_release will call del_mtd_device, but we haven't yet
1829 added it. This is handled without incident by
1830 del_mtd_device, as far as I can tell. */
1831 nand_release(mtd);
1832 kfree(mtd);
1833 goto fail;
1836 /* Success! */
1837 doclist = mtd;
1838 return 0;
1840 notfound:
1841 /* Put back the contents of the DOCControl register, in case it's not
1842 actually a DiskOnChip. */
1843 WriteDOC(save_control, virtadr, DOCControl);
1844 fail:
1845 iounmap(virtadr);
1846 return ret;
1849 static void release_nanddoc(void)
1851 struct mtd_info *mtd, *nextmtd;
1852 struct nand_chip *nand;
1853 struct doc_priv *doc;
1855 for (mtd = doclist; mtd; mtd = nextmtd) {
1856 nand = mtd->priv;
1857 doc = nand->priv;
1859 nextmtd = doc->nextdoc;
1860 nand_release(mtd);
1861 iounmap(doc->virtadr);
1862 kfree(mtd);
1866 static int __init init_nanddoc(void)
1868 int i, ret = 0;
1870 /* We could create the decoder on demand, if memory is a concern.
1871 * This way we have it handy, if an error happens
1873 * Symbolsize is 10 (bits)
1874 * Primitve polynomial is x^10+x^3+1
1875 * first consecutive root is 510
1876 * primitve element to generate roots = 1
1877 * generator polinomial degree = 4
1879 rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS);
1880 if (!rs_decoder) {
1881 printk(KERN_ERR "DiskOnChip: Could not create a RS decoder\n");
1882 return -ENOMEM;
1885 if (doc_config_location) {
1886 printk(KERN_INFO "Using configured DiskOnChip probe address 0x%lx\n", doc_config_location);
1887 ret = doc_probe(doc_config_location);
1888 if (ret < 0)
1889 goto outerr;
1890 } else {
1891 for (i = 0; (doc_locations[i] != 0xffffffff); i++) {
1892 doc_probe(doc_locations[i]);
1895 /* No banner message any more. Print a message if no DiskOnChip
1896 found, so the user knows we at least tried. */
1897 if (!doclist) {
1898 printk(KERN_INFO "No valid DiskOnChip devices found\n");
1899 ret = -ENODEV;
1900 goto outerr;
1902 return 0;
1903 outerr:
1904 free_rs(rs_decoder);
1905 return ret;
1908 static void __exit cleanup_nanddoc(void)
1910 /* Cleanup the nand/DoC resources */
1911 release_nanddoc();
1913 /* Free the reed solomon resources */
1914 if (rs_decoder) {
1915 free_rs(rs_decoder);
1919 module_init(init_nanddoc);
1920 module_exit(cleanup_nanddoc);
1922 MODULE_LICENSE("GPL");
1923 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1924 MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver\n");