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