Merge branch 'master' of /home/tglx/work/kernel/git/mtd-2.6/
[pandora-kernel.git] / drivers / mtd / nand / diskonchip.c
1 /*
2  * drivers/mtd/nand/diskonchip.c
3  *
4  * (C) 2003 Red Hat, Inc.
5  * (C) 2004 Dan Brown <dan_brown@ieee.org>
6  * (C) 2004 Kalev Lember <kalev@smartlink.ee>
7  *
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>
11  *
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>
16  *
17  * Interface to generic NAND code for M-Systems DiskOnChip devices
18  *
19  * $Id: diskonchip.c,v 1.55 2005/11/07 11:14:30 gleixner Exp $
20  */
21
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>
29
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>
36
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
41
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 };
68
69 static struct mtd_info *doclist = NULL;
70
71 struct doc_priv {
72         void __iomem *virtadr;
73         unsigned long physadr;
74         u_char ChipID;
75         u_char CDSNControl;
76         int chips_per_floor;    /* The number of chips detected on each floor */
77         int curfloor;
78         int curchip;
79         int mh0_page;
80         int mh1_page;
81         struct mtd_info *nextdoc;
82 };
83
84 /* This is the syndrome computed by the HW ecc generator upon reading an empty
85    page, one with all 0xff for data and stored ecc code. */
86 static u_char empty_read_syndrome[6] = { 0x26, 0xff, 0x6d, 0x47, 0x73, 0x7a };
87
88 /* This is the ecc value computed by the HW ecc generator upon writing an empty
89    page, one with all 0xff for data. */
90 static u_char empty_write_ecc[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
91
92 #define INFTL_BBT_RESERVED_BLOCKS 4
93
94 #define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
95 #define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
96 #define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
97
98 static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd);
99 static void doc200x_select_chip(struct mtd_info *mtd, int chip);
100
101 static int debug = 0;
102 module_param(debug, int, 0);
103
104 static int try_dword = 1;
105 module_param(try_dword, int, 0);
106
107 static int no_ecc_failures = 0;
108 module_param(no_ecc_failures, int, 0);
109
110 static int no_autopart = 0;
111 module_param(no_autopart, int, 0);
112
113 static int show_firmware_partition = 0;
114 module_param(show_firmware_partition, int, 0);
115
116 #ifdef MTD_NAND_DISKONCHIP_BBTWRITE
117 static int inftl_bbt_write = 1;
118 #else
119 static int inftl_bbt_write = 0;
120 #endif
121 module_param(inftl_bbt_write, int, 0);
122
123 static unsigned long doc_config_location = CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS;
124 module_param(doc_config_location, ulong, 0);
125 MODULE_PARM_DESC(doc_config_location, "Physical memory address at which to probe for DiskOnChip");
126
127 /* Sector size for HW ECC */
128 #define SECTOR_SIZE 512
129 /* The sector bytes are packed into NB_DATA 10 bit words */
130 #define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / 10)
131 /* Number of roots */
132 #define NROOTS 4
133 /* First consective root */
134 #define FCR 510
135 /* Number of symbols */
136 #define NN 1023
137
138 /* the Reed Solomon control structure */
139 static struct rs_control *rs_decoder;
140
141 /*
142  * The HW decoder in the DoC ASIC's provides us a error syndrome,
143  * which we must convert to a standard syndrom usable by the generic
144  * Reed-Solomon library code.
145  *
146  * Fabrice Bellard figured this out in the old docecc code. I added
147  * some comments, improved a minor bit and converted it to make use
148  * of the generic Reed-Solomon libary. tglx
149  */
150 static int doc_ecc_decode(struct rs_control *rs, uint8_t *data, uint8_t *ecc)
151 {
152         int i, j, nerr, errpos[8];
153         uint8_t parity;
154         uint16_t ds[4], s[5], tmp, errval[8], syn[4];
155
156         /* Convert the ecc bytes into words */
157         ds[0] = ((ecc[4] & 0xff) >> 0) | ((ecc[5] & 0x03) << 8);
158         ds[1] = ((ecc[5] & 0xfc) >> 2) | ((ecc[2] & 0x0f) << 6);
159         ds[2] = ((ecc[2] & 0xf0) >> 4) | ((ecc[3] & 0x3f) << 4);
160         ds[3] = ((ecc[3] & 0xc0) >> 6) | ((ecc[0] & 0xff) << 2);
161         parity = ecc[1];
162
163         /* Initialize the syndrom buffer */
164         for (i = 0; i < NROOTS; i++)
165                 s[i] = ds[0];
166         /*
167          *  Evaluate
168          *  s[i] = ds[3]x^3 + ds[2]x^2 + ds[1]x^1 + ds[0]
169          *  where x = alpha^(FCR + i)
170          */
171         for (j = 1; j < NROOTS; j++) {
172                 if (ds[j] == 0)
173                         continue;
174                 tmp = rs->index_of[ds[j]];
175                 for (i = 0; i < NROOTS; i++)
176                         s[i] ^= rs->alpha_to[rs_modnn(rs, tmp + (FCR + i) * j)];
177         }
178
179         /* Calc s[i] = s[i] / alpha^(v + i) */
180         for (i = 0; i < NROOTS; i++) {
181                 if (syn[i])
182                         syn[i] = rs_modnn(rs, rs->index_of[s[i]] + (NN - FCR - i));
183         }
184         /* Call the decoder library */
185         nerr = decode_rs16(rs, NULL, NULL, 1019, syn, 0, errpos, 0, errval);
186
187         /* Incorrectable errors ? */
188         if (nerr < 0)
189                 return nerr;
190
191         /*
192          * Correct the errors. The bitpositions are a bit of magic,
193          * but they are given by the design of the de/encoder circuit
194          * in the DoC ASIC's.
195          */
196         for (i = 0; i < nerr; i++) {
197                 int index, bitpos, pos = 1015 - errpos[i];
198                 uint8_t val;
199                 if (pos >= NB_DATA && pos < 1019)
200                         continue;
201                 if (pos < NB_DATA) {
202                         /* extract bit position (MSB first) */
203                         pos = 10 * (NB_DATA - 1 - pos) - 6;
204                         /* now correct the following 10 bits. At most two bytes
205                            can be modified since pos is even */
206                         index = (pos >> 3) ^ 1;
207                         bitpos = pos & 7;
208                         if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
209                                 val = (uint8_t) (errval[i] >> (2 + bitpos));
210                                 parity ^= val;
211                                 if (index < SECTOR_SIZE)
212                                         data[index] ^= val;
213                         }
214                         index = ((pos >> 3) + 1) ^ 1;
215                         bitpos = (bitpos + 10) & 7;
216                         if (bitpos == 0)
217                                 bitpos = 8;
218                         if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
219                                 val = (uint8_t) (errval[i] << (8 - bitpos));
220                                 parity ^= val;
221                                 if (index < SECTOR_SIZE)
222                                         data[index] ^= val;
223                         }
224                 }
225         }
226         /* If the parity is wrong, no rescue possible */
227         return parity ? -1 : nerr;
228 }
229
230 static void DoC_Delay(struct doc_priv *doc, unsigned short cycles)
231 {
232         volatile char dummy;
233         int i;
234
235         for (i = 0; i < cycles; i++) {
236                 if (DoC_is_Millennium(doc))
237                         dummy = ReadDOC(doc->virtadr, NOP);
238                 else if (DoC_is_MillenniumPlus(doc))
239                         dummy = ReadDOC(doc->virtadr, Mplus_NOP);
240                 else
241                         dummy = ReadDOC(doc->virtadr, DOCStatus);
242         }
243
244 }
245
246 #define CDSN_CTRL_FR_B_MASK     (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
247
248 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
249 static int _DoC_WaitReady(struct doc_priv *doc)
250 {
251         void __iomem *docptr = doc->virtadr;
252         unsigned long timeo = jiffies + (HZ * 10);
253
254         if (debug)
255                 printk("_DoC_WaitReady...\n");
256         /* Out-of-line routine to wait for chip response */
257         if (DoC_is_MillenniumPlus(doc)) {
258                 while ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
259                         if (time_after(jiffies, timeo)) {
260                                 printk("_DoC_WaitReady timed out.\n");
261                                 return -EIO;
262                         }
263                         udelay(1);
264                         cond_resched();
265                 }
266         } else {
267                 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
268                         if (time_after(jiffies, timeo)) {
269                                 printk("_DoC_WaitReady timed out.\n");
270                                 return -EIO;
271                         }
272                         udelay(1);
273                         cond_resched();
274                 }
275         }
276
277         return 0;
278 }
279
280 static inline int DoC_WaitReady(struct doc_priv *doc)
281 {
282         void __iomem *docptr = doc->virtadr;
283         int ret = 0;
284
285         if (DoC_is_MillenniumPlus(doc)) {
286                 DoC_Delay(doc, 4);
287
288                 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
289                         /* Call the out-of-line routine to wait */
290                         ret = _DoC_WaitReady(doc);
291         } else {
292                 DoC_Delay(doc, 4);
293
294                 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
295                         /* Call the out-of-line routine to wait */
296                         ret = _DoC_WaitReady(doc);
297                 DoC_Delay(doc, 2);
298         }
299
300         if (debug)
301                 printk("DoC_WaitReady OK\n");
302         return ret;
303 }
304
305 static void doc2000_write_byte(struct mtd_info *mtd, u_char datum)
306 {
307         struct nand_chip *this = mtd->priv;
308         struct doc_priv *doc = this->priv;
309         void __iomem *docptr = doc->virtadr;
310
311         if (debug)
312                 printk("write_byte %02x\n", datum);
313         WriteDOC(datum, docptr, CDSNSlowIO);
314         WriteDOC(datum, docptr, 2k_CDSN_IO);
315 }
316
317 static u_char doc2000_read_byte(struct mtd_info *mtd)
318 {
319         struct nand_chip *this = mtd->priv;
320         struct doc_priv *doc = this->priv;
321         void __iomem *docptr = doc->virtadr;
322         u_char ret;
323
324         ReadDOC(docptr, CDSNSlowIO);
325         DoC_Delay(doc, 2);
326         ret = ReadDOC(docptr, 2k_CDSN_IO);
327         if (debug)
328                 printk("read_byte returns %02x\n", ret);
329         return ret;
330 }
331
332 static void doc2000_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
333 {
334         struct nand_chip *this = mtd->priv;
335         struct doc_priv *doc = this->priv;
336         void __iomem *docptr = doc->virtadr;
337         int i;
338         if (debug)
339                 printk("writebuf of %d bytes: ", len);
340         for (i = 0; i < len; i++) {
341                 WriteDOC_(buf[i], docptr, DoC_2k_CDSN_IO + i);
342                 if (debug && i < 16)
343                         printk("%02x ", buf[i]);
344         }
345         if (debug)
346                 printk("\n");
347 }
348
349 static void doc2000_readbuf(struct mtd_info *mtd, u_char *buf, int len)
350 {
351         struct nand_chip *this = mtd->priv;
352         struct doc_priv *doc = this->priv;
353         void __iomem *docptr = doc->virtadr;
354         int i;
355
356         if (debug)
357                 printk("readbuf of %d bytes: ", len);
358
359         for (i = 0; i < len; i++) {
360                 buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
361         }
362 }
363
364 static void doc2000_readbuf_dword(struct mtd_info *mtd, u_char *buf, int len)
365 {
366         struct nand_chip *this = mtd->priv;
367         struct doc_priv *doc = this->priv;
368         void __iomem *docptr = doc->virtadr;
369         int i;
370
371         if (debug)
372                 printk("readbuf_dword of %d bytes: ", len);
373
374         if (unlikely((((unsigned long)buf) | len) & 3)) {
375                 for (i = 0; i < len; i++) {
376                         *(uint8_t *) (&buf[i]) = ReadDOC(docptr, 2k_CDSN_IO + i);
377                 }
378         } else {
379                 for (i = 0; i < len; i += 4) {
380                         *(uint32_t *) (&buf[i]) = readl(docptr + DoC_2k_CDSN_IO + i);
381                 }
382         }
383 }
384
385 static int doc2000_verifybuf(struct mtd_info *mtd, const u_char *buf, int len)
386 {
387         struct nand_chip *this = mtd->priv;
388         struct doc_priv *doc = this->priv;
389         void __iomem *docptr = doc->virtadr;
390         int i;
391
392         for (i = 0; i < len; i++)
393                 if (buf[i] != ReadDOC(docptr, 2k_CDSN_IO))
394                         return -EFAULT;
395         return 0;
396 }
397
398 static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr)
399 {
400         struct nand_chip *this = mtd->priv;
401         struct doc_priv *doc = this->priv;
402         uint16_t ret;
403
404         doc200x_select_chip(mtd, nr);
405         doc200x_hwcontrol(mtd, NAND_CTL_SETCLE);
406         this->write_byte(mtd, NAND_CMD_READID);
407         doc200x_hwcontrol(mtd, NAND_CTL_CLRCLE);
408         doc200x_hwcontrol(mtd, NAND_CTL_SETALE);
409         this->write_byte(mtd, 0);
410         doc200x_hwcontrol(mtd, NAND_CTL_CLRALE);
411
412         /* We cant' use dev_ready here, but at least we wait for the
413          * command to complete
414          */
415         udelay(50);
416
417         ret = this->read_byte(mtd) << 8;
418         ret |= this->read_byte(mtd);
419
420         if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) {
421                 /* First chip probe. See if we get same results by 32-bit access */
422                 union {
423                         uint32_t dword;
424                         uint8_t byte[4];
425                 } ident;
426                 void __iomem *docptr = doc->virtadr;
427
428                 doc200x_hwcontrol(mtd, NAND_CTL_SETCLE);
429                 doc2000_write_byte(mtd, NAND_CMD_READID);
430                 doc200x_hwcontrol(mtd, NAND_CTL_CLRCLE);
431                 doc200x_hwcontrol(mtd, NAND_CTL_SETALE);
432                 doc2000_write_byte(mtd, 0);
433                 doc200x_hwcontrol(mtd, NAND_CTL_CLRALE);
434
435                 udelay(50);
436
437                 ident.dword = readl(docptr + DoC_2k_CDSN_IO);
438                 if (((ident.byte[0] << 8) | ident.byte[1]) == ret) {
439                         printk(KERN_INFO "DiskOnChip 2000 responds to DWORD access\n");
440                         this->read_buf = &doc2000_readbuf_dword;
441                 }
442         }
443
444         return ret;
445 }
446
447 static void __init doc2000_count_chips(struct mtd_info *mtd)
448 {
449         struct nand_chip *this = mtd->priv;
450         struct doc_priv *doc = this->priv;
451         uint16_t mfrid;
452         int i;
453
454         /* Max 4 chips per floor on DiskOnChip 2000 */
455         doc->chips_per_floor = 4;
456
457         /* Find out what the first chip is */
458         mfrid = doc200x_ident_chip(mtd, 0);
459
460         /* Find how many chips in each floor. */
461         for (i = 1; i < 4; i++) {
462                 if (doc200x_ident_chip(mtd, i) != mfrid)
463                         break;
464         }
465         doc->chips_per_floor = i;
466         printk(KERN_DEBUG "Detected %d chips per floor.\n", i);
467 }
468
469 static int doc200x_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
470 {
471         struct doc_priv *doc = this->priv;
472
473         int status;
474
475         DoC_WaitReady(doc);
476         this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
477         DoC_WaitReady(doc);
478         status = (int)this->read_byte(mtd);
479
480         return status;
481 }
482
483 static void doc2001_write_byte(struct mtd_info *mtd, u_char datum)
484 {
485         struct nand_chip *this = mtd->priv;
486         struct doc_priv *doc = this->priv;
487         void __iomem *docptr = doc->virtadr;
488
489         WriteDOC(datum, docptr, CDSNSlowIO);
490         WriteDOC(datum, docptr, Mil_CDSN_IO);
491         WriteDOC(datum, docptr, WritePipeTerm);
492 }
493
494 static u_char doc2001_read_byte(struct mtd_info *mtd)
495 {
496         struct nand_chip *this = mtd->priv;
497         struct doc_priv *doc = this->priv;
498         void __iomem *docptr = doc->virtadr;
499
500         //ReadDOC(docptr, CDSNSlowIO);
501         /* 11.4.5 -- delay twice to allow extended length cycle */
502         DoC_Delay(doc, 2);
503         ReadDOC(docptr, ReadPipeInit);
504         //return ReadDOC(docptr, Mil_CDSN_IO);
505         return ReadDOC(docptr, LastDataRead);
506 }
507
508 static void doc2001_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
509 {
510         struct nand_chip *this = mtd->priv;
511         struct doc_priv *doc = this->priv;
512         void __iomem *docptr = doc->virtadr;
513         int i;
514
515         for (i = 0; i < len; i++)
516                 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
517         /* Terminate write pipeline */
518         WriteDOC(0x00, docptr, WritePipeTerm);
519 }
520
521 static void doc2001_readbuf(struct mtd_info *mtd, u_char *buf, int len)
522 {
523         struct nand_chip *this = mtd->priv;
524         struct doc_priv *doc = this->priv;
525         void __iomem *docptr = doc->virtadr;
526         int i;
527
528         /* Start read pipeline */
529         ReadDOC(docptr, ReadPipeInit);
530
531         for (i = 0; i < len - 1; i++)
532                 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
533
534         /* Terminate read pipeline */
535         buf[i] = ReadDOC(docptr, LastDataRead);
536 }
537
538 static int doc2001_verifybuf(struct mtd_info *mtd, const u_char *buf, int len)
539 {
540         struct nand_chip *this = mtd->priv;
541         struct doc_priv *doc = this->priv;
542         void __iomem *docptr = doc->virtadr;
543         int i;
544
545         /* Start read pipeline */
546         ReadDOC(docptr, ReadPipeInit);
547
548         for (i = 0; i < len - 1; i++)
549                 if (buf[i] != ReadDOC(docptr, Mil_CDSN_IO)) {
550                         ReadDOC(docptr, LastDataRead);
551                         return i;
552                 }
553         if (buf[i] != ReadDOC(docptr, LastDataRead))
554                 return i;
555         return 0;
556 }
557
558 static u_char doc2001plus_read_byte(struct mtd_info *mtd)
559 {
560         struct nand_chip *this = mtd->priv;
561         struct doc_priv *doc = this->priv;
562         void __iomem *docptr = doc->virtadr;
563         u_char ret;
564
565         ReadDOC(docptr, Mplus_ReadPipeInit);
566         ReadDOC(docptr, Mplus_ReadPipeInit);
567         ret = ReadDOC(docptr, Mplus_LastDataRead);
568         if (debug)
569                 printk("read_byte returns %02x\n", ret);
570         return ret;
571 }
572
573 static void doc2001plus_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
574 {
575         struct nand_chip *this = mtd->priv;
576         struct doc_priv *doc = this->priv;
577         void __iomem *docptr = doc->virtadr;
578         int i;
579
580         if (debug)
581                 printk("writebuf of %d bytes: ", len);
582         for (i = 0; i < len; i++) {
583                 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
584                 if (debug && i < 16)
585                         printk("%02x ", buf[i]);
586         }
587         if (debug)
588                 printk("\n");
589 }
590
591 static void doc2001plus_readbuf(struct mtd_info *mtd, u_char *buf, int len)
592 {
593         struct nand_chip *this = mtd->priv;
594         struct doc_priv *doc = this->priv;
595         void __iomem *docptr = doc->virtadr;
596         int i;
597
598         if (debug)
599                 printk("readbuf of %d bytes: ", len);
600
601         /* Start read pipeline */
602         ReadDOC(docptr, Mplus_ReadPipeInit);
603         ReadDOC(docptr, Mplus_ReadPipeInit);
604
605         for (i = 0; i < len - 2; i++) {
606                 buf[i] = ReadDOC(docptr, Mil_CDSN_IO);
607                 if (debug && i < 16)
608                         printk("%02x ", buf[i]);
609         }
610
611         /* Terminate read pipeline */
612         buf[len - 2] = ReadDOC(docptr, Mplus_LastDataRead);
613         if (debug && i < 16)
614                 printk("%02x ", buf[len - 2]);
615         buf[len - 1] = ReadDOC(docptr, Mplus_LastDataRead);
616         if (debug && i < 16)
617                 printk("%02x ", buf[len - 1]);
618         if (debug)
619                 printk("\n");
620 }
621
622 static int doc2001plus_verifybuf(struct mtd_info *mtd, const u_char *buf, int len)
623 {
624         struct nand_chip *this = mtd->priv;
625         struct doc_priv *doc = this->priv;
626         void __iomem *docptr = doc->virtadr;
627         int i;
628
629         if (debug)
630                 printk("verifybuf of %d bytes: ", len);
631
632         /* Start read pipeline */
633         ReadDOC(docptr, Mplus_ReadPipeInit);
634         ReadDOC(docptr, Mplus_ReadPipeInit);
635
636         for (i = 0; i < len - 2; i++)
637                 if (buf[i] != ReadDOC(docptr, Mil_CDSN_IO)) {
638                         ReadDOC(docptr, Mplus_LastDataRead);
639                         ReadDOC(docptr, Mplus_LastDataRead);
640                         return i;
641                 }
642         if (buf[len - 2] != ReadDOC(docptr, Mplus_LastDataRead))
643                 return len - 2;
644         if (buf[len - 1] != ReadDOC(docptr, Mplus_LastDataRead))
645                 return len - 1;
646         return 0;
647 }
648
649 static void doc2001plus_select_chip(struct mtd_info *mtd, int chip)
650 {
651         struct nand_chip *this = mtd->priv;
652         struct doc_priv *doc = this->priv;
653         void __iomem *docptr = doc->virtadr;
654         int floor = 0;
655
656         if (debug)
657                 printk("select chip (%d)\n", chip);
658
659         if (chip == -1) {
660                 /* Disable flash internally */
661                 WriteDOC(0, docptr, Mplus_FlashSelect);
662                 return;
663         }
664
665         floor = chip / doc->chips_per_floor;
666         chip -= (floor * doc->chips_per_floor);
667
668         /* Assert ChipEnable and deassert WriteProtect */
669         WriteDOC((DOC_FLASH_CE), docptr, Mplus_FlashSelect);
670         this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
671
672         doc->curchip = chip;
673         doc->curfloor = floor;
674 }
675
676 static void doc200x_select_chip(struct mtd_info *mtd, int chip)
677 {
678         struct nand_chip *this = mtd->priv;
679         struct doc_priv *doc = this->priv;
680         void __iomem *docptr = doc->virtadr;
681         int floor = 0;
682
683         if (debug)
684                 printk("select chip (%d)\n", chip);
685
686         if (chip == -1)
687                 return;
688
689         floor = chip / doc->chips_per_floor;
690         chip -= (floor * doc->chips_per_floor);
691
692         /* 11.4.4 -- deassert CE before changing chip */
693         doc200x_hwcontrol(mtd, NAND_CTL_CLRNCE);
694
695         WriteDOC(floor, docptr, FloorSelect);
696         WriteDOC(chip, docptr, CDSNDeviceSelect);
697
698         doc200x_hwcontrol(mtd, NAND_CTL_SETNCE);
699
700         doc->curchip = chip;
701         doc->curfloor = floor;
702 }
703
704 static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd)
705 {
706         struct nand_chip *this = mtd->priv;
707         struct doc_priv *doc = this->priv;
708         void __iomem *docptr = doc->virtadr;
709
710         switch (cmd) {
711         case NAND_CTL_SETNCE:
712                 doc->CDSNControl |= CDSN_CTRL_CE;
713                 break;
714         case NAND_CTL_CLRNCE:
715                 doc->CDSNControl &= ~CDSN_CTRL_CE;
716                 break;
717         case NAND_CTL_SETCLE:
718                 doc->CDSNControl |= CDSN_CTRL_CLE;
719                 break;
720         case NAND_CTL_CLRCLE:
721                 doc->CDSNControl &= ~CDSN_CTRL_CLE;
722                 break;
723         case NAND_CTL_SETALE:
724                 doc->CDSNControl |= CDSN_CTRL_ALE;
725                 break;
726         case NAND_CTL_CLRALE:
727                 doc->CDSNControl &= ~CDSN_CTRL_ALE;
728                 break;
729         case NAND_CTL_SETWP:
730                 doc->CDSNControl |= CDSN_CTRL_WP;
731                 break;
732         case NAND_CTL_CLRWP:
733                 doc->CDSNControl &= ~CDSN_CTRL_WP;
734                 break;
735         }
736         if (debug)
737                 printk("hwcontrol(%d): %02x\n", cmd, doc->CDSNControl);
738         WriteDOC(doc->CDSNControl, docptr, CDSNControl);
739         /* 11.4.3 -- 4 NOPs after CSDNControl write */
740         DoC_Delay(doc, 4);
741 }
742
743 static void doc2001plus_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
744 {
745         struct nand_chip *this = mtd->priv;
746         struct doc_priv *doc = this->priv;
747         void __iomem *docptr = doc->virtadr;
748
749         /*
750          * Must terminate write pipeline before sending any commands
751          * to the device.
752          */
753         if (command == NAND_CMD_PAGEPROG) {
754                 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
755                 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
756         }
757
758         /*
759          * Write out the command to the device.
760          */
761         if (command == NAND_CMD_SEQIN) {
762                 int readcmd;
763
764                 if (column >= mtd->writesize) {
765                         /* OOB area */
766                         column -= mtd->writesize;
767                         readcmd = NAND_CMD_READOOB;
768                 } else if (column < 256) {
769                         /* First 256 bytes --> READ0 */
770                         readcmd = NAND_CMD_READ0;
771                 } else {
772                         column -= 256;
773                         readcmd = NAND_CMD_READ1;
774                 }
775                 WriteDOC(readcmd, docptr, Mplus_FlashCmd);
776         }
777         WriteDOC(command, docptr, Mplus_FlashCmd);
778         WriteDOC(0, docptr, Mplus_WritePipeTerm);
779         WriteDOC(0, docptr, Mplus_WritePipeTerm);
780
781         if (column != -1 || page_addr != -1) {
782                 /* Serially input address */
783                 if (column != -1) {
784                         /* Adjust columns for 16 bit buswidth */
785                         if (this->options & NAND_BUSWIDTH_16)
786                                 column >>= 1;
787                         WriteDOC(column, docptr, Mplus_FlashAddress);
788                 }
789                 if (page_addr != -1) {
790                         WriteDOC((unsigned char)(page_addr & 0xff), docptr, Mplus_FlashAddress);
791                         WriteDOC((unsigned char)((page_addr >> 8) & 0xff), docptr, Mplus_FlashAddress);
792                         /* One more address cycle for higher density devices */
793                         if (this->chipsize & 0x0c000000) {
794                                 WriteDOC((unsigned char)((page_addr >> 16) & 0x0f), docptr, Mplus_FlashAddress);
795                                 printk("high density\n");
796                         }
797                 }
798                 WriteDOC(0, docptr, Mplus_WritePipeTerm);
799                 WriteDOC(0, docptr, Mplus_WritePipeTerm);
800                 /* deassert ALE */
801                 if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
802                     command == NAND_CMD_READOOB || command == NAND_CMD_READID)
803                         WriteDOC(0, docptr, Mplus_FlashControl);
804         }
805
806         /*
807          * program and erase have their own busy handlers
808          * status and sequential in needs no delay
809          */
810         switch (command) {
811
812         case NAND_CMD_PAGEPROG:
813         case NAND_CMD_ERASE1:
814         case NAND_CMD_ERASE2:
815         case NAND_CMD_SEQIN:
816         case NAND_CMD_STATUS:
817                 return;
818
819         case NAND_CMD_RESET:
820                 if (this->dev_ready)
821                         break;
822                 udelay(this->chip_delay);
823                 WriteDOC(NAND_CMD_STATUS, docptr, Mplus_FlashCmd);
824                 WriteDOC(0, docptr, Mplus_WritePipeTerm);
825                 WriteDOC(0, docptr, Mplus_WritePipeTerm);
826                 while (!(this->read_byte(mtd) & 0x40)) ;
827                 return;
828
829                 /* This applies to read commands */
830         default:
831                 /*
832                  * If we don't have access to the busy pin, we apply the given
833                  * command delay
834                  */
835                 if (!this->dev_ready) {
836                         udelay(this->chip_delay);
837                         return;
838                 }
839         }
840
841         /* Apply this short delay always to ensure that we do wait tWB in
842          * any case on any machine. */
843         ndelay(100);
844         /* wait until command is processed */
845         while (!this->dev_ready(mtd)) ;
846 }
847
848 static int doc200x_dev_ready(struct mtd_info *mtd)
849 {
850         struct nand_chip *this = mtd->priv;
851         struct doc_priv *doc = this->priv;
852         void __iomem *docptr = doc->virtadr;
853
854         if (DoC_is_MillenniumPlus(doc)) {
855                 /* 11.4.2 -- must NOP four times before checking FR/B# */
856                 DoC_Delay(doc, 4);
857                 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
858                         if (debug)
859                                 printk("not ready\n");
860                         return 0;
861                 }
862                 if (debug)
863                         printk("was ready\n");
864                 return 1;
865         } else {
866                 /* 11.4.2 -- must NOP four times before checking FR/B# */
867                 DoC_Delay(doc, 4);
868                 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
869                         if (debug)
870                                 printk("not ready\n");
871                         return 0;
872                 }
873                 /* 11.4.2 -- Must NOP twice if it's ready */
874                 DoC_Delay(doc, 2);
875                 if (debug)
876                         printk("was ready\n");
877                 return 1;
878         }
879 }
880
881 static int doc200x_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
882 {
883         /* This is our last resort if we couldn't find or create a BBT.  Just
884            pretend all blocks are good. */
885         return 0;
886 }
887
888 static void doc200x_enable_hwecc(struct mtd_info *mtd, int mode)
889 {
890         struct nand_chip *this = mtd->priv;
891         struct doc_priv *doc = this->priv;
892         void __iomem *docptr = doc->virtadr;
893
894         /* Prime the ECC engine */
895         switch (mode) {
896         case NAND_ECC_READ:
897                 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
898                 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
899                 break;
900         case NAND_ECC_WRITE:
901                 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
902                 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
903                 break;
904         }
905 }
906
907 static void doc2001plus_enable_hwecc(struct mtd_info *mtd, int mode)
908 {
909         struct nand_chip *this = mtd->priv;
910         struct doc_priv *doc = this->priv;
911         void __iomem *docptr = doc->virtadr;
912
913         /* Prime the ECC engine */
914         switch (mode) {
915         case NAND_ECC_READ:
916                 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
917                 WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
918                 break;
919         case NAND_ECC_WRITE:
920                 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
921                 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
922                 break;
923         }
924 }
925
926 /* This code is only called on write */
927 static int doc200x_calculate_ecc(struct mtd_info *mtd, const u_char *dat, unsigned char *ecc_code)
928 {
929         struct nand_chip *this = mtd->priv;
930         struct doc_priv *doc = this->priv;
931         void __iomem *docptr = doc->virtadr;
932         int i;
933         int emptymatch = 1;
934
935         /* flush the pipeline */
936         if (DoC_is_2000(doc)) {
937                 WriteDOC(doc->CDSNControl & ~CDSN_CTRL_FLASH_IO, docptr, CDSNControl);
938                 WriteDOC(0, docptr, 2k_CDSN_IO);
939                 WriteDOC(0, docptr, 2k_CDSN_IO);
940                 WriteDOC(0, docptr, 2k_CDSN_IO);
941                 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
942         } else if (DoC_is_MillenniumPlus(doc)) {
943                 WriteDOC(0, docptr, Mplus_NOP);
944                 WriteDOC(0, docptr, Mplus_NOP);
945                 WriteDOC(0, docptr, Mplus_NOP);
946         } else {
947                 WriteDOC(0, docptr, NOP);
948                 WriteDOC(0, docptr, NOP);
949                 WriteDOC(0, docptr, NOP);
950         }
951
952         for (i = 0; i < 6; i++) {
953                 if (DoC_is_MillenniumPlus(doc))
954                         ecc_code[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
955                 else
956                         ecc_code[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
957                 if (ecc_code[i] != empty_write_ecc[i])
958                         emptymatch = 0;
959         }
960         if (DoC_is_MillenniumPlus(doc))
961                 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
962         else
963                 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
964 #if 0
965         /* If emptymatch=1, we might have an all-0xff data buffer.  Check. */
966         if (emptymatch) {
967                 /* Note: this somewhat expensive test should not be triggered
968                    often.  It could be optimized away by examining the data in
969                    the writebuf routine, and remembering the result. */
970                 for (i = 0; i < 512; i++) {
971                         if (dat[i] == 0xff)
972                                 continue;
973                         emptymatch = 0;
974                         break;
975                 }
976         }
977         /* If emptymatch still =1, we do have an all-0xff data buffer.
978            Return all-0xff ecc value instead of the computed one, so
979            it'll look just like a freshly-erased page. */
980         if (emptymatch)
981                 memset(ecc_code, 0xff, 6);
982 #endif
983         return 0;
984 }
985
986 static int doc200x_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc)
987 {
988         int i, ret = 0;
989         struct nand_chip *this = mtd->priv;
990         struct doc_priv *doc = this->priv;
991         void __iomem *docptr = doc->virtadr;
992         volatile u_char dummy;
993         int emptymatch = 1;
994
995         /* flush the pipeline */
996         if (DoC_is_2000(doc)) {
997                 dummy = ReadDOC(docptr, 2k_ECCStatus);
998                 dummy = ReadDOC(docptr, 2k_ECCStatus);
999                 dummy = ReadDOC(docptr, 2k_ECCStatus);
1000         } else if (DoC_is_MillenniumPlus(doc)) {
1001                 dummy = ReadDOC(docptr, Mplus_ECCConf);
1002                 dummy = ReadDOC(docptr, Mplus_ECCConf);
1003                 dummy = ReadDOC(docptr, Mplus_ECCConf);
1004         } else {
1005                 dummy = ReadDOC(docptr, ECCConf);
1006                 dummy = ReadDOC(docptr, ECCConf);
1007                 dummy = ReadDOC(docptr, ECCConf);
1008         }
1009
1010         /* Error occured ? */
1011         if (dummy & 0x80) {
1012                 for (i = 0; i < 6; i++) {
1013                         if (DoC_is_MillenniumPlus(doc))
1014                                 calc_ecc[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
1015                         else
1016                                 calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
1017                         if (calc_ecc[i] != empty_read_syndrome[i])
1018                                 emptymatch = 0;
1019                 }
1020                 /* If emptymatch=1, the read syndrome is consistent with an
1021                    all-0xff data and stored ecc block.  Check the stored ecc. */
1022                 if (emptymatch) {
1023                         for (i = 0; i < 6; i++) {
1024                                 if (read_ecc[i] == 0xff)
1025                                         continue;
1026                                 emptymatch = 0;
1027                                 break;
1028                         }
1029                 }
1030                 /* If emptymatch still =1, check the data block. */
1031                 if (emptymatch) {
1032                         /* Note: this somewhat expensive test should not be triggered
1033                            often.  It could be optimized away by examining the data in
1034                            the readbuf routine, and remembering the result. */
1035                         for (i = 0; i < 512; i++) {
1036                                 if (dat[i] == 0xff)
1037                                         continue;
1038                                 emptymatch = 0;
1039                                 break;
1040                         }
1041                 }
1042                 /* If emptymatch still =1, this is almost certainly a freshly-
1043                    erased block, in which case the ECC will not come out right.
1044                    We'll suppress the error and tell the caller everything's
1045                    OK.  Because it is. */
1046                 if (!emptymatch)
1047                         ret = doc_ecc_decode(rs_decoder, dat, calc_ecc);
1048                 if (ret > 0)
1049                         printk(KERN_ERR "doc200x_correct_data corrected %d errors\n", ret);
1050         }
1051         if (DoC_is_MillenniumPlus(doc))
1052                 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
1053         else
1054                 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
1055         if (no_ecc_failures && (ret == -1)) {
1056                 printk(KERN_ERR "suppressing ECC failure\n");
1057                 ret = 0;
1058         }
1059         return ret;
1060 }
1061
1062 //u_char mydatabuf[528];
1063
1064 /* The strange out-of-order .oobfree list below is a (possibly unneeded)
1065  * attempt to retain compatibility.  It used to read:
1066  *      .oobfree = { {8, 8} }
1067  * Since that leaves two bytes unusable, it was changed.  But the following
1068  * scheme might affect existing jffs2 installs by moving the cleanmarker:
1069  *      .oobfree = { {6, 10} }
1070  * jffs2 seems to handle the above gracefully, but the current scheme seems
1071  * safer.  The only problem with it is that any code that parses oobfree must
1072  * be able to handle out-of-order segments.
1073  */
1074 static struct nand_oobinfo doc200x_oobinfo = {
1075         .useecc = MTD_NANDECC_AUTOPLACE,
1076         .eccbytes = 6,
1077         .eccpos = {0, 1, 2, 3, 4, 5},
1078         .oobfree = {{8, 8}, {6, 2}}
1079 };
1080
1081 /* Find the (I)NFTL Media Header, and optionally also the mirror media header.
1082    On sucessful return, buf will contain a copy of the media header for
1083    further processing.  id is the string to scan for, and will presumably be
1084    either "ANAND" or "BNAND".  If findmirror=1, also look for the mirror media
1085    header.  The page #s of the found media headers are placed in mh0_page and
1086    mh1_page in the DOC private structure. */
1087 static int __init find_media_headers(struct mtd_info *mtd, u_char *buf, const char *id, int findmirror)
1088 {
1089         struct nand_chip *this = mtd->priv;
1090         struct doc_priv *doc = this->priv;
1091         unsigned offs;
1092         int ret;
1093         size_t retlen;
1094
1095         for (offs = 0; offs < mtd->size; offs += mtd->erasesize) {
1096                 ret = mtd->read(mtd, offs, mtd->writesize, &retlen, buf);
1097                 if (retlen != mtd->writesize)
1098                         continue;
1099                 if (ret) {
1100                         printk(KERN_WARNING "ECC error scanning DOC at 0x%x\n", offs);
1101                 }
1102                 if (memcmp(buf, id, 6))
1103                         continue;
1104                 printk(KERN_INFO "Found DiskOnChip %s Media Header at 0x%x\n", id, offs);
1105                 if (doc->mh0_page == -1) {
1106                         doc->mh0_page = offs >> this->page_shift;
1107                         if (!findmirror)
1108                                 return 1;
1109                         continue;
1110                 }
1111                 doc->mh1_page = offs >> this->page_shift;
1112                 return 2;
1113         }
1114         if (doc->mh0_page == -1) {
1115                 printk(KERN_WARNING "DiskOnChip %s Media Header not found.\n", id);
1116                 return 0;
1117         }
1118         /* Only one mediaheader was found.  We want buf to contain a
1119            mediaheader on return, so we'll have to re-read the one we found. */
1120         offs = doc->mh0_page << this->page_shift;
1121         ret = mtd->read(mtd, offs, mtd->writesize, &retlen, buf);
1122         if (retlen != mtd->writesize) {
1123                 /* Insanity.  Give up. */
1124                 printk(KERN_ERR "Read DiskOnChip Media Header once, but can't reread it???\n");
1125                 return 0;
1126         }
1127         return 1;
1128 }
1129
1130 static inline int __init nftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
1131 {
1132         struct nand_chip *this = mtd->priv;
1133         struct doc_priv *doc = this->priv;
1134         int ret = 0;
1135         u_char *buf;
1136         struct NFTLMediaHeader *mh;
1137         const unsigned psize = 1 << this->page_shift;
1138         int numparts = 0;
1139         unsigned blocks, maxblocks;
1140         int offs, numheaders;
1141
1142         buf = kmalloc(mtd->writesize, GFP_KERNEL);
1143         if (!buf) {
1144                 printk(KERN_ERR "DiskOnChip mediaheader kmalloc failed!\n");
1145                 return 0;
1146         }
1147         if (!(numheaders = find_media_headers(mtd, buf, "ANAND", 1)))
1148                 goto out;
1149         mh = (struct NFTLMediaHeader *)buf;
1150
1151         mh->NumEraseUnits = le16_to_cpu(mh->NumEraseUnits);
1152         mh->FirstPhysicalEUN = le16_to_cpu(mh->FirstPhysicalEUN);
1153         mh->FormattedSize = le32_to_cpu(mh->FormattedSize);
1154
1155         printk(KERN_INFO "    DataOrgID        = %s\n"
1156                          "    NumEraseUnits    = %d\n"
1157                          "    FirstPhysicalEUN = %d\n"
1158                          "    FormattedSize    = %d\n"
1159                          "    UnitSizeFactor   = %d\n",
1160                 mh->DataOrgID, mh->NumEraseUnits,
1161                 mh->FirstPhysicalEUN, mh->FormattedSize,
1162                 mh->UnitSizeFactor);
1163
1164         blocks = mtd->size >> this->phys_erase_shift;
1165         maxblocks = min(32768U, mtd->erasesize - psize);
1166
1167         if (mh->UnitSizeFactor == 0x00) {
1168                 /* Auto-determine UnitSizeFactor.  The constraints are:
1169                    - There can be at most 32768 virtual blocks.
1170                    - There can be at most (virtual block size - page size)
1171                    virtual blocks (because MediaHeader+BBT must fit in 1).
1172                  */
1173                 mh->UnitSizeFactor = 0xff;
1174                 while (blocks > maxblocks) {
1175                         blocks >>= 1;
1176                         maxblocks = min(32768U, (maxblocks << 1) + psize);
1177                         mh->UnitSizeFactor--;
1178                 }
1179                 printk(KERN_WARNING "UnitSizeFactor=0x00 detected.  Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor);
1180         }
1181
1182         /* NOTE: The lines below modify internal variables of the NAND and MTD
1183            layers; variables with have already been configured by nand_scan.
1184            Unfortunately, we didn't know before this point what these values
1185            should be.  Thus, this code is somewhat dependant on the exact
1186            implementation of the NAND layer.  */
1187         if (mh->UnitSizeFactor != 0xff) {
1188                 this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
1189                 mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
1190                 printk(KERN_INFO "Setting virtual erase size to %d\n", mtd->erasesize);
1191                 blocks = mtd->size >> this->bbt_erase_shift;
1192                 maxblocks = min(32768U, mtd->erasesize - psize);
1193         }
1194
1195         if (blocks > maxblocks) {
1196                 printk(KERN_ERR "UnitSizeFactor of 0x%02x is inconsistent with device size.  Aborting.\n", mh->UnitSizeFactor);
1197                 goto out;
1198         }
1199
1200         /* Skip past the media headers. */
1201         offs = max(doc->mh0_page, doc->mh1_page);
1202         offs <<= this->page_shift;
1203         offs += mtd->erasesize;
1204
1205         if (show_firmware_partition == 1) {
1206                 parts[0].name = " DiskOnChip Firmware / Media Header partition";
1207                 parts[0].offset = 0;
1208                 parts[0].size = offs;
1209                 numparts = 1;
1210         }
1211
1212         parts[numparts].name = " DiskOnChip BDTL partition";
1213         parts[numparts].offset = offs;
1214         parts[numparts].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift;
1215
1216         offs += parts[numparts].size;
1217         numparts++;
1218
1219         if (offs < mtd->size) {
1220                 parts[numparts].name = " DiskOnChip Remainder partition";
1221                 parts[numparts].offset = offs;
1222                 parts[numparts].size = mtd->size - offs;
1223                 numparts++;
1224         }
1225
1226         ret = numparts;
1227  out:
1228         kfree(buf);
1229         return ret;
1230 }
1231
1232 /* This is a stripped-down copy of the code in inftlmount.c */
1233 static inline int __init inftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
1234 {
1235         struct nand_chip *this = mtd->priv;
1236         struct doc_priv *doc = this->priv;
1237         int ret = 0;
1238         u_char *buf;
1239         struct INFTLMediaHeader *mh;
1240         struct INFTLPartition *ip;
1241         int numparts = 0;
1242         int blocks;
1243         int vshift, lastvunit = 0;
1244         int i;
1245         int end = mtd->size;
1246
1247         if (inftl_bbt_write)
1248                 end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift);
1249
1250         buf = kmalloc(mtd->writesize, GFP_KERNEL);
1251         if (!buf) {
1252                 printk(KERN_ERR "DiskOnChip mediaheader kmalloc failed!\n");
1253                 return 0;
1254         }
1255
1256         if (!find_media_headers(mtd, buf, "BNAND", 0))
1257                 goto out;
1258         doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift);
1259         mh = (struct INFTLMediaHeader *)buf;
1260
1261         mh->NoOfBootImageBlocks = le32_to_cpu(mh->NoOfBootImageBlocks);
1262         mh->NoOfBinaryPartitions = le32_to_cpu(mh->NoOfBinaryPartitions);
1263         mh->NoOfBDTLPartitions = le32_to_cpu(mh->NoOfBDTLPartitions);
1264         mh->BlockMultiplierBits = le32_to_cpu(mh->BlockMultiplierBits);
1265         mh->FormatFlags = le32_to_cpu(mh->FormatFlags);
1266         mh->PercentUsed = le32_to_cpu(mh->PercentUsed);
1267
1268         printk(KERN_INFO "    bootRecordID          = %s\n"
1269                          "    NoOfBootImageBlocks   = %d\n"
1270                          "    NoOfBinaryPartitions  = %d\n"
1271                          "    NoOfBDTLPartitions    = %d\n"
1272                          "    BlockMultiplerBits    = %d\n"
1273                          "    FormatFlgs            = %d\n"
1274                          "    OsakVersion           = %d.%d.%d.%d\n"
1275                          "    PercentUsed           = %d\n",
1276                 mh->bootRecordID, mh->NoOfBootImageBlocks,
1277                 mh->NoOfBinaryPartitions,
1278                 mh->NoOfBDTLPartitions,
1279                 mh->BlockMultiplierBits, mh->FormatFlags,
1280                 ((unsigned char *) &mh->OsakVersion)[0] & 0xf,
1281                 ((unsigned char *) &mh->OsakVersion)[1] & 0xf,
1282                 ((unsigned char *) &mh->OsakVersion)[2] & 0xf,
1283                 ((unsigned char *) &mh->OsakVersion)[3] & 0xf,
1284                 mh->PercentUsed);
1285
1286         vshift = this->phys_erase_shift + mh->BlockMultiplierBits;
1287
1288         blocks = mtd->size >> vshift;
1289         if (blocks > 32768) {
1290                 printk(KERN_ERR "BlockMultiplierBits=%d is inconsistent with device size.  Aborting.\n", mh->BlockMultiplierBits);
1291                 goto out;
1292         }
1293
1294         blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift);
1295         if (inftl_bbt_write && (blocks > mtd->erasesize)) {
1296                 printk(KERN_ERR "Writeable BBTs spanning more than one erase block are not yet supported.  FIX ME!\n");
1297                 goto out;
1298         }
1299
1300         /* Scan the partitions */
1301         for (i = 0; (i < 4); i++) {
1302                 ip = &(mh->Partitions[i]);
1303                 ip->virtualUnits = le32_to_cpu(ip->virtualUnits);
1304                 ip->firstUnit = le32_to_cpu(ip->firstUnit);
1305                 ip->lastUnit = le32_to_cpu(ip->lastUnit);
1306                 ip->flags = le32_to_cpu(ip->flags);
1307                 ip->spareUnits = le32_to_cpu(ip->spareUnits);
1308                 ip->Reserved0 = le32_to_cpu(ip->Reserved0);
1309
1310                 printk(KERN_INFO        "    PARTITION[%d] ->\n"
1311                         "        virtualUnits    = %d\n"
1312                         "        firstUnit       = %d\n"
1313                         "        lastUnit        = %d\n"
1314                         "        flags           = 0x%x\n"
1315                         "        spareUnits      = %d\n",
1316                         i, ip->virtualUnits, ip->firstUnit,
1317                         ip->lastUnit, ip->flags,
1318                         ip->spareUnits);
1319
1320                 if ((show_firmware_partition == 1) &&
1321                     (i == 0) && (ip->firstUnit > 0)) {
1322                         parts[0].name = " DiskOnChip IPL / Media Header partition";
1323                         parts[0].offset = 0;
1324                         parts[0].size = mtd->erasesize * ip->firstUnit;
1325                         numparts = 1;
1326                 }
1327
1328                 if (ip->flags & INFTL_BINARY)
1329                         parts[numparts].name = " DiskOnChip BDK partition";
1330                 else
1331                         parts[numparts].name = " DiskOnChip BDTL partition";
1332                 parts[numparts].offset = ip->firstUnit << vshift;
1333                 parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift;
1334                 numparts++;
1335                 if (ip->lastUnit > lastvunit)
1336                         lastvunit = ip->lastUnit;
1337                 if (ip->flags & INFTL_LAST)
1338                         break;
1339         }
1340         lastvunit++;
1341         if ((lastvunit << vshift) < end) {
1342                 parts[numparts].name = " DiskOnChip Remainder partition";
1343                 parts[numparts].offset = lastvunit << vshift;
1344                 parts[numparts].size = end - parts[numparts].offset;
1345                 numparts++;
1346         }
1347         ret = numparts;
1348  out:
1349         kfree(buf);
1350         return ret;
1351 }
1352
1353 static int __init nftl_scan_bbt(struct mtd_info *mtd)
1354 {
1355         int ret, numparts;
1356         struct nand_chip *this = mtd->priv;
1357         struct doc_priv *doc = this->priv;
1358         struct mtd_partition parts[2];
1359
1360         memset((char *)parts, 0, sizeof(parts));
1361         /* On NFTL, we have to find the media headers before we can read the
1362            BBTs, since they're stored in the media header eraseblocks. */
1363         numparts = nftl_partscan(mtd, parts);
1364         if (!numparts)
1365                 return -EIO;
1366         this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1367                                 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1368                                 NAND_BBT_VERSION;
1369         this->bbt_td->veroffs = 7;
1370         this->bbt_td->pages[0] = doc->mh0_page + 1;
1371         if (doc->mh1_page != -1) {
1372                 this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1373                                         NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1374                                         NAND_BBT_VERSION;
1375                 this->bbt_md->veroffs = 7;
1376                 this->bbt_md->pages[0] = doc->mh1_page + 1;
1377         } else {
1378                 this->bbt_md = NULL;
1379         }
1380
1381         /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
1382            At least as nand_bbt.c is currently written. */
1383         if ((ret = nand_scan_bbt(mtd, NULL)))
1384                 return ret;
1385         add_mtd_device(mtd);
1386 #ifdef CONFIG_MTD_PARTITIONS
1387         if (!no_autopart)
1388                 add_mtd_partitions(mtd, parts, numparts);
1389 #endif
1390         return 0;
1391 }
1392
1393 static int __init inftl_scan_bbt(struct mtd_info *mtd)
1394 {
1395         int ret, numparts;
1396         struct nand_chip *this = mtd->priv;
1397         struct doc_priv *doc = this->priv;
1398         struct mtd_partition parts[5];
1399
1400         if (this->numchips > doc->chips_per_floor) {
1401                 printk(KERN_ERR "Multi-floor INFTL devices not yet supported.\n");
1402                 return -EIO;
1403         }
1404
1405         if (DoC_is_MillenniumPlus(doc)) {
1406                 this->bbt_td->options = NAND_BBT_2BIT | NAND_BBT_ABSPAGE;
1407                 if (inftl_bbt_write)
1408                         this->bbt_td->options |= NAND_BBT_WRITE;
1409                 this->bbt_td->pages[0] = 2;
1410                 this->bbt_md = NULL;
1411         } else {
1412                 this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
1413                 if (inftl_bbt_write)
1414                         this->bbt_td->options |= NAND_BBT_WRITE;
1415                 this->bbt_td->offs = 8;
1416                 this->bbt_td->len = 8;
1417                 this->bbt_td->veroffs = 7;
1418                 this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1419                 this->bbt_td->reserved_block_code = 0x01;
1420                 this->bbt_td->pattern = "MSYS_BBT";
1421
1422                 this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
1423                 if (inftl_bbt_write)
1424                         this->bbt_md->options |= NAND_BBT_WRITE;
1425                 this->bbt_md->offs = 8;
1426                 this->bbt_md->len = 8;
1427                 this->bbt_md->veroffs = 7;
1428                 this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1429                 this->bbt_md->reserved_block_code = 0x01;
1430                 this->bbt_md->pattern = "TBB_SYSM";
1431         }
1432
1433         /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
1434            At least as nand_bbt.c is currently written. */
1435         if ((ret = nand_scan_bbt(mtd, NULL)))
1436                 return ret;
1437         memset((char *)parts, 0, sizeof(parts));
1438         numparts = inftl_partscan(mtd, parts);
1439         /* At least for now, require the INFTL Media Header.  We could probably
1440            do without it for non-INFTL use, since all it gives us is
1441            autopartitioning, but I want to give it more thought. */
1442         if (!numparts)
1443                 return -EIO;
1444         add_mtd_device(mtd);
1445 #ifdef CONFIG_MTD_PARTITIONS
1446         if (!no_autopart)
1447                 add_mtd_partitions(mtd, parts, numparts);
1448 #endif
1449         return 0;
1450 }
1451
1452 static inline int __init doc2000_init(struct mtd_info *mtd)
1453 {
1454         struct nand_chip *this = mtd->priv;
1455         struct doc_priv *doc = this->priv;
1456
1457         this->write_byte = doc2000_write_byte;
1458         this->read_byte = doc2000_read_byte;
1459         this->write_buf = doc2000_writebuf;
1460         this->read_buf = doc2000_readbuf;
1461         this->verify_buf = doc2000_verifybuf;
1462         this->scan_bbt = nftl_scan_bbt;
1463
1464         doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
1465         doc2000_count_chips(mtd);
1466         mtd->name = "DiskOnChip 2000 (NFTL Model)";
1467         return (4 * doc->chips_per_floor);
1468 }
1469
1470 static inline int __init doc2001_init(struct mtd_info *mtd)
1471 {
1472         struct nand_chip *this = mtd->priv;
1473         struct doc_priv *doc = this->priv;
1474
1475         this->write_byte = doc2001_write_byte;
1476         this->read_byte = doc2001_read_byte;
1477         this->write_buf = doc2001_writebuf;
1478         this->read_buf = doc2001_readbuf;
1479         this->verify_buf = doc2001_verifybuf;
1480
1481         ReadDOC(doc->virtadr, ChipID);
1482         ReadDOC(doc->virtadr, ChipID);
1483         ReadDOC(doc->virtadr, ChipID);
1484         if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) {
1485                 /* It's not a Millennium; it's one of the newer
1486                    DiskOnChip 2000 units with a similar ASIC.
1487                    Treat it like a Millennium, except that it
1488                    can have multiple chips. */
1489                 doc2000_count_chips(mtd);
1490                 mtd->name = "DiskOnChip 2000 (INFTL Model)";
1491                 this->scan_bbt = inftl_scan_bbt;
1492                 return (4 * doc->chips_per_floor);
1493         } else {
1494                 /* Bog-standard Millennium */
1495                 doc->chips_per_floor = 1;
1496                 mtd->name = "DiskOnChip Millennium";
1497                 this->scan_bbt = nftl_scan_bbt;
1498                 return 1;
1499         }
1500 }
1501
1502 static inline int __init doc2001plus_init(struct mtd_info *mtd)
1503 {
1504         struct nand_chip *this = mtd->priv;
1505         struct doc_priv *doc = this->priv;
1506
1507         this->write_byte = NULL;
1508         this->read_byte = doc2001plus_read_byte;
1509         this->write_buf = doc2001plus_writebuf;
1510         this->read_buf = doc2001plus_readbuf;
1511         this->verify_buf = doc2001plus_verifybuf;
1512         this->scan_bbt = inftl_scan_bbt;
1513         this->hwcontrol = NULL;
1514         this->select_chip = doc2001plus_select_chip;
1515         this->cmdfunc = doc2001plus_command;
1516         this->enable_hwecc = doc2001plus_enable_hwecc;
1517
1518         doc->chips_per_floor = 1;
1519         mtd->name = "DiskOnChip Millennium Plus";
1520
1521         return 1;
1522 }
1523
1524 static int __init doc_probe(unsigned long physadr)
1525 {
1526         unsigned char ChipID;
1527         struct mtd_info *mtd;
1528         struct nand_chip *nand;
1529         struct doc_priv *doc;
1530         void __iomem *virtadr;
1531         unsigned char save_control;
1532         unsigned char tmp, tmpb, tmpc;
1533         int reg, len, numchips;
1534         int ret = 0;
1535
1536         virtadr = ioremap(physadr, DOC_IOREMAP_LEN);
1537         if (!virtadr) {
1538                 printk(KERN_ERR "Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n", DOC_IOREMAP_LEN, physadr);
1539                 return -EIO;
1540         }
1541
1542         /* It's not possible to cleanly detect the DiskOnChip - the
1543          * bootup procedure will put the device into reset mode, and
1544          * it's not possible to talk to it without actually writing
1545          * to the DOCControl register. So we store the current contents
1546          * of the DOCControl register's location, in case we later decide
1547          * that it's not a DiskOnChip, and want to put it back how we
1548          * found it.
1549          */
1550         save_control = ReadDOC(virtadr, DOCControl);
1551
1552         /* Reset the DiskOnChip ASIC */
1553         WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
1554         WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
1555
1556         /* Enable the DiskOnChip ASIC */
1557         WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
1558         WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
1559
1560         ChipID = ReadDOC(virtadr, ChipID);
1561
1562         switch (ChipID) {
1563         case DOC_ChipID_Doc2k:
1564                 reg = DoC_2k_ECCStatus;
1565                 break;
1566         case DOC_ChipID_DocMil:
1567                 reg = DoC_ECCConf;
1568                 break;
1569         case DOC_ChipID_DocMilPlus16:
1570         case DOC_ChipID_DocMilPlus32:
1571         case 0:
1572                 /* Possible Millennium Plus, need to do more checks */
1573                 /* Possibly release from power down mode */
1574                 for (tmp = 0; (tmp < 4); tmp++)
1575                         ReadDOC(virtadr, Mplus_Power);
1576
1577                 /* Reset the Millennium Plus ASIC */
1578                 tmp = DOC_MODE_RESET | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
1579                 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1580                 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1581
1582                 mdelay(1);
1583                 /* Enable the Millennium Plus ASIC */
1584                 tmp = DOC_MODE_NORMAL | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
1585                 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1586                 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1587                 mdelay(1);
1588
1589                 ChipID = ReadDOC(virtadr, ChipID);
1590
1591                 switch (ChipID) {
1592                 case DOC_ChipID_DocMilPlus16:
1593                         reg = DoC_Mplus_Toggle;
1594                         break;
1595                 case DOC_ChipID_DocMilPlus32:
1596                         printk(KERN_ERR "DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
1597                 default:
1598                         ret = -ENODEV;
1599                         goto notfound;
1600                 }
1601                 break;
1602
1603         default:
1604                 ret = -ENODEV;
1605                 goto notfound;
1606         }
1607         /* Check the TOGGLE bit in the ECC register */
1608         tmp = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1609         tmpb = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1610         tmpc = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1611         if ((tmp == tmpb) || (tmp != tmpc)) {
1612                 printk(KERN_WARNING "Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr);
1613                 ret = -ENODEV;
1614                 goto notfound;
1615         }
1616
1617         for (mtd = doclist; mtd; mtd = doc->nextdoc) {
1618                 unsigned char oldval;
1619                 unsigned char newval;
1620                 nand = mtd->priv;
1621                 doc = nand->priv;
1622                 /* Use the alias resolution register to determine if this is
1623                    in fact the same DOC aliased to a new address.  If writes
1624                    to one chip's alias resolution register change the value on
1625                    the other chip, they're the same chip. */
1626                 if (ChipID == DOC_ChipID_DocMilPlus16) {
1627                         oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1628                         newval = ReadDOC(virtadr, Mplus_AliasResolution);
1629                 } else {
1630                         oldval = ReadDOC(doc->virtadr, AliasResolution);
1631                         newval = ReadDOC(virtadr, AliasResolution);
1632                 }
1633                 if (oldval != newval)
1634                         continue;
1635                 if (ChipID == DOC_ChipID_DocMilPlus16) {
1636                         WriteDOC(~newval, virtadr, Mplus_AliasResolution);
1637                         oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1638                         WriteDOC(newval, virtadr, Mplus_AliasResolution);       // restore it
1639                 } else {
1640                         WriteDOC(~newval, virtadr, AliasResolution);
1641                         oldval = ReadDOC(doc->virtadr, AliasResolution);
1642                         WriteDOC(newval, virtadr, AliasResolution);     // restore it
1643                 }
1644                 newval = ~newval;
1645                 if (oldval == newval) {
1646                         printk(KERN_DEBUG "Found alias of DOC at 0x%lx to 0x%lx\n", doc->physadr, physadr);
1647                         goto notfound;
1648                 }
1649         }
1650
1651         printk(KERN_NOTICE "DiskOnChip found at 0x%lx\n", physadr);
1652
1653         len = sizeof(struct mtd_info) +
1654             sizeof(struct nand_chip) + sizeof(struct doc_priv) + (2 * sizeof(struct nand_bbt_descr));
1655         mtd = kmalloc(len, GFP_KERNEL);
1656         if (!mtd) {
1657                 printk(KERN_ERR "DiskOnChip kmalloc (%d bytes) failed!\n", len);
1658                 ret = -ENOMEM;
1659                 goto fail;
1660         }
1661         memset(mtd, 0, len);
1662
1663         nand                    = (struct nand_chip *) (mtd + 1);
1664         doc                     = (struct doc_priv *) (nand + 1);
1665         nand->bbt_td            = (struct nand_bbt_descr *) (doc + 1);
1666         nand->bbt_md            = nand->bbt_td + 1;
1667
1668         mtd->priv               = nand;
1669         mtd->owner              = THIS_MODULE;
1670
1671         nand->priv              = doc;
1672         nand->select_chip       = doc200x_select_chip;
1673         nand->hwcontrol         = doc200x_hwcontrol;
1674         nand->dev_ready         = doc200x_dev_ready;
1675         nand->waitfunc          = doc200x_wait;
1676         nand->block_bad         = doc200x_block_bad;
1677         nand->ecc.hwctl         = doc200x_enable_hwecc;
1678         nand->ecc.calculate     = doc200x_calculate_ecc;
1679         nand->ecc.correct       = doc200x_correct_data;
1680
1681         nand->autooob           = &doc200x_oobinfo;
1682         nand->ecc.mode          = NAND_ECC_HW_SYNDROME;
1683         nand->ecc.size          = 512;
1684         nand->ecc.bytes         = 6;
1685         nand->options           = NAND_USE_FLASH_BBT | NAND_HWECC_SYNDROME;
1686
1687         doc->physadr            = physadr;
1688         doc->virtadr            = virtadr;
1689         doc->ChipID             = ChipID;
1690         doc->curfloor           = -1;
1691         doc->curchip            = -1;
1692         doc->mh0_page           = -1;
1693         doc->mh1_page           = -1;
1694         doc->nextdoc            = doclist;
1695
1696         if (ChipID == DOC_ChipID_Doc2k)
1697                 numchips = doc2000_init(mtd);
1698         else if (ChipID == DOC_ChipID_DocMilPlus16)
1699                 numchips = doc2001plus_init(mtd);
1700         else
1701                 numchips = doc2001_init(mtd);
1702
1703         if ((ret = nand_scan(mtd, numchips))) {
1704                 /* DBB note: i believe nand_release is necessary here, as
1705                    buffers may have been allocated in nand_base.  Check with
1706                    Thomas. FIX ME! */
1707                 /* nand_release will call del_mtd_device, but we haven't yet
1708                    added it.  This is handled without incident by
1709                    del_mtd_device, as far as I can tell. */
1710                 nand_release(mtd);
1711                 kfree(mtd);
1712                 goto fail;
1713         }
1714
1715         /* Success! */
1716         doclist = mtd;
1717         return 0;
1718
1719  notfound:
1720         /* Put back the contents of the DOCControl register, in case it's not
1721            actually a DiskOnChip.  */
1722         WriteDOC(save_control, virtadr, DOCControl);
1723  fail:
1724         iounmap(virtadr);
1725         return ret;
1726 }
1727
1728 static void release_nanddoc(void)
1729 {
1730         struct mtd_info *mtd, *nextmtd;
1731         struct nand_chip *nand;
1732         struct doc_priv *doc;
1733
1734         for (mtd = doclist; mtd; mtd = nextmtd) {
1735                 nand = mtd->priv;
1736                 doc = nand->priv;
1737
1738                 nextmtd = doc->nextdoc;
1739                 nand_release(mtd);
1740                 iounmap(doc->virtadr);
1741                 kfree(mtd);
1742         }
1743 }
1744
1745 static int __init init_nanddoc(void)
1746 {
1747         int i, ret = 0;
1748
1749         /* We could create the decoder on demand, if memory is a concern.
1750          * This way we have it handy, if an error happens
1751          *
1752          * Symbolsize is 10 (bits)
1753          * Primitve polynomial is x^10+x^3+1
1754          * first consecutive root is 510
1755          * primitve element to generate roots = 1
1756          * generator polinomial degree = 4
1757          */
1758         rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS);
1759         if (!rs_decoder) {
1760                 printk(KERN_ERR "DiskOnChip: Could not create a RS decoder\n");
1761                 return -ENOMEM;
1762         }
1763
1764         if (doc_config_location) {
1765                 printk(KERN_INFO "Using configured DiskOnChip probe address 0x%lx\n", doc_config_location);
1766                 ret = doc_probe(doc_config_location);
1767                 if (ret < 0)
1768                         goto outerr;
1769         } else {
1770                 for (i = 0; (doc_locations[i] != 0xffffffff); i++) {
1771                         doc_probe(doc_locations[i]);
1772                 }
1773         }
1774         /* No banner message any more. Print a message if no DiskOnChip
1775            found, so the user knows we at least tried. */
1776         if (!doclist) {
1777                 printk(KERN_INFO "No valid DiskOnChip devices found\n");
1778                 ret = -ENODEV;
1779                 goto outerr;
1780         }
1781         return 0;
1782  outerr:
1783         free_rs(rs_decoder);
1784         return ret;
1785 }
1786
1787 static void __exit cleanup_nanddoc(void)
1788 {
1789         /* Cleanup the nand/DoC resources */
1790         release_nanddoc();
1791
1792         /* Free the reed solomon resources */
1793         if (rs_decoder) {
1794                 free_rs(rs_decoder);
1795         }
1796 }
1797
1798 module_init(init_nanddoc);
1799 module_exit(cleanup_nanddoc);
1800
1801 MODULE_LICENSE("GPL");
1802 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1803 MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver\n");