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