]> err.no Git - linux-2.6/blob - drivers/mtd/nand/diskonchip.c
[MTD] DiskOnChip code cleanup
[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.47 2005/01/31 22:21:15 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         printk(KERN_INFO "    DataOrgID        = %s\n"
1126                          "    NumEraseUnits    = %d\n"
1127                          "    FirstPhysicalEUN = %d\n"
1128                          "    FormattedSize    = %d\n"
1129                          "    UnitSizeFactor   = %d\n",
1130                 mh->DataOrgID, mh->NumEraseUnits,
1131                 mh->FirstPhysicalEUN, mh->FormattedSize,
1132                 mh->UnitSizeFactor);
1133
1134         blocks = mtd->size >> this->phys_erase_shift;
1135         maxblocks = min(32768U, mtd->erasesize - psize);
1136
1137         if (mh->UnitSizeFactor == 0x00) {
1138                 /* Auto-determine UnitSizeFactor.  The constraints are:
1139                    - There can be at most 32768 virtual blocks.
1140                    - There can be at most (virtual block size - page size)
1141                      virtual blocks (because MediaHeader+BBT must fit in 1).
1142                 */
1143                 mh->UnitSizeFactor = 0xff;
1144                 while (blocks > maxblocks) {
1145                         blocks >>= 1;
1146                         maxblocks = min(32768U, (maxblocks << 1) + psize);
1147                         mh->UnitSizeFactor--;
1148                 }
1149                 printk(KERN_WARNING "UnitSizeFactor=0x00 detected.  Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor);
1150         }
1151
1152         /* NOTE: The lines below modify internal variables of the NAND and MTD
1153            layers; variables with have already been configured by nand_scan.
1154            Unfortunately, we didn't know before this point what these values
1155            should be.  Thus, this code is somewhat dependant on the exact
1156            implementation of the NAND layer.  */
1157         if (mh->UnitSizeFactor != 0xff) {
1158                 this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
1159                 mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
1160                 printk(KERN_INFO "Setting virtual erase size to %d\n", mtd->erasesize);
1161                 blocks = mtd->size >> this->bbt_erase_shift;
1162                 maxblocks = min(32768U, mtd->erasesize - psize);
1163         }
1164
1165         if (blocks > maxblocks) {
1166                 printk(KERN_ERR "UnitSizeFactor of 0x%02x is inconsistent with device size.  Aborting.\n", mh->UnitSizeFactor);
1167                 goto out;
1168         }
1169
1170         /* Skip past the media headers. */
1171         offs = max(doc->mh0_page, doc->mh1_page);
1172         offs <<= this->page_shift;
1173         offs += mtd->erasesize;
1174
1175         parts[0].name = " DiskOnChip BDTL partition";
1176         parts[0].offset = offs;
1177         parts[0].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift;
1178
1179         offs += parts[0].size;
1180         if (offs < mtd->size) {
1181                 parts[1].name = " DiskOnChip Remainder partition";
1182                 parts[1].offset = offs;
1183                 parts[1].size = mtd->size - offs;
1184                 ret = 2;
1185                 goto out;
1186         }
1187         ret = 1;
1188 out:
1189         kfree(buf);
1190         return ret;
1191 }
1192
1193 /* This is a stripped-down copy of the code in inftlmount.c */
1194 static inline int __init inftl_partscan(struct mtd_info *mtd,
1195                                  struct mtd_partition *parts)
1196 {
1197         struct nand_chip *this = mtd->priv;
1198         struct doc_priv *doc = this->priv;
1199         int ret = 0;
1200         u_char *buf;
1201         struct INFTLMediaHeader *mh;
1202         struct INFTLPartition *ip;
1203         int numparts = 0;
1204         int blocks;
1205         int vshift, lastvunit = 0;
1206         int i;
1207         int end = mtd->size;
1208
1209         if (inftl_bbt_write)
1210                 end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift);
1211
1212         buf = kmalloc(mtd->oobblock, GFP_KERNEL);
1213         if (!buf) {
1214                 printk(KERN_ERR "DiskOnChip mediaheader kmalloc failed!\n");
1215                 return 0;
1216         }
1217
1218         if (!find_media_headers(mtd, buf, "BNAND", 0)) goto out;
1219         doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift);
1220         mh = (struct INFTLMediaHeader *) buf;
1221
1222         mh->NoOfBootImageBlocks = le32_to_cpu(mh->NoOfBootImageBlocks);
1223         mh->NoOfBinaryPartitions = le32_to_cpu(mh->NoOfBinaryPartitions);
1224         mh->NoOfBDTLPartitions = le32_to_cpu(mh->NoOfBDTLPartitions);
1225         mh->BlockMultiplierBits = le32_to_cpu(mh->BlockMultiplierBits);
1226         mh->FormatFlags = le32_to_cpu(mh->FormatFlags);
1227         mh->PercentUsed = le32_to_cpu(mh->PercentUsed);
1228  
1229         printk(KERN_INFO "    bootRecordID          = %s\n"
1230                          "    NoOfBootImageBlocks   = %d\n"
1231                          "    NoOfBinaryPartitions  = %d\n"
1232                          "    NoOfBDTLPartitions    = %d\n"
1233                          "    BlockMultiplerBits    = %d\n"
1234                          "    FormatFlgs            = %d\n"
1235                          "    OsakVersion           = %d.%d.%d.%d\n"
1236                          "    PercentUsed           = %d\n",
1237                 mh->bootRecordID, mh->NoOfBootImageBlocks,
1238                 mh->NoOfBinaryPartitions,
1239                 mh->NoOfBDTLPartitions,
1240                 mh->BlockMultiplierBits, mh->FormatFlags,
1241                 ((unsigned char *) &mh->OsakVersion)[0] & 0xf,
1242                 ((unsigned char *) &mh->OsakVersion)[1] & 0xf,
1243                 ((unsigned char *) &mh->OsakVersion)[2] & 0xf,
1244                 ((unsigned char *) &mh->OsakVersion)[3] & 0xf,
1245                 mh->PercentUsed);
1246
1247         vshift = this->phys_erase_shift + mh->BlockMultiplierBits;
1248
1249         blocks = mtd->size >> vshift;
1250         if (blocks > 32768) {
1251                 printk(KERN_ERR "BlockMultiplierBits=%d is inconsistent with device size.  Aborting.\n", mh->BlockMultiplierBits);
1252                 goto out;
1253         }
1254
1255         blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift);
1256         if (inftl_bbt_write && (blocks > mtd->erasesize)) {
1257                 printk(KERN_ERR "Writeable BBTs spanning more than one erase block are not yet supported.  FIX ME!\n");
1258                 goto out;
1259         }
1260
1261         /* Scan the partitions */
1262         for (i = 0; (i < 4); i++) {
1263                 ip = &(mh->Partitions[i]);
1264                 ip->virtualUnits = le32_to_cpu(ip->virtualUnits);
1265                 ip->firstUnit = le32_to_cpu(ip->firstUnit);
1266                 ip->lastUnit = le32_to_cpu(ip->lastUnit);
1267                 ip->flags = le32_to_cpu(ip->flags);
1268                 ip->spareUnits = le32_to_cpu(ip->spareUnits);
1269                 ip->Reserved0 = le32_to_cpu(ip->Reserved0);
1270
1271                 printk(KERN_INFO        "    PARTITION[%d] ->\n"
1272                         "        virtualUnits    = %d\n"
1273                         "        firstUnit       = %d\n"
1274                         "        lastUnit        = %d\n"
1275                         "        flags           = 0x%x\n"
1276                         "        spareUnits      = %d\n",
1277                         i, ip->virtualUnits, ip->firstUnit,
1278                         ip->lastUnit, ip->flags,
1279                         ip->spareUnits);
1280
1281 #if 0
1282                 if ((i == 0) && (ip->firstUnit > 0)) {
1283                         parts[0].name = " DiskOnChip IPL / Media Header partition";
1284                         parts[0].offset = 0;
1285                         parts[0].size = mtd->erasesize * ip->firstUnit;
1286                         numparts = 1;
1287                 }
1288 #endif
1289
1290                 if (ip->flags & INFTL_BINARY)
1291                         parts[numparts].name = " DiskOnChip BDK partition";
1292                 else
1293                         parts[numparts].name = " DiskOnChip BDTL partition";
1294                 parts[numparts].offset = ip->firstUnit << vshift;
1295                 parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift;
1296                 numparts++;
1297                 if (ip->lastUnit > lastvunit) lastvunit = ip->lastUnit;
1298                 if (ip->flags & INFTL_LAST) break;
1299         }
1300         lastvunit++;
1301         if ((lastvunit << vshift) < end) {
1302                 parts[numparts].name = " DiskOnChip Remainder partition";
1303                 parts[numparts].offset = lastvunit << vshift;
1304                 parts[numparts].size = end - parts[numparts].offset;
1305                 numparts++;
1306         }
1307         ret = numparts;
1308 out:
1309         kfree(buf);
1310         return ret;
1311 }
1312
1313 static int __init nftl_scan_bbt(struct mtd_info *mtd)
1314 {
1315         int ret, numparts;
1316         struct nand_chip *this = mtd->priv;
1317         struct doc_priv *doc = this->priv;
1318         struct mtd_partition parts[2];
1319
1320         memset((char *) parts, 0, sizeof(parts));
1321         /* On NFTL, we have to find the media headers before we can read the
1322            BBTs, since they're stored in the media header eraseblocks. */
1323         numparts = nftl_partscan(mtd, parts);
1324         if (!numparts) return -EIO;
1325         this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1326                                 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1327                                 NAND_BBT_VERSION;
1328         this->bbt_td->veroffs = 7;
1329         this->bbt_td->pages[0] = doc->mh0_page + 1;
1330         if (doc->mh1_page != -1) {
1331                 this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1332                                         NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1333                                         NAND_BBT_VERSION;
1334                 this->bbt_md->veroffs = 7;
1335                 this->bbt_md->pages[0] = doc->mh1_page + 1;
1336         } else {
1337                 this->bbt_md = NULL;
1338         }
1339
1340         /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
1341            At least as nand_bbt.c is currently written. */
1342         if ((ret = nand_scan_bbt(mtd, NULL)))
1343                 return ret;
1344         add_mtd_device(mtd);
1345 #ifdef CONFIG_MTD_PARTITIONS
1346         if (!no_autopart)
1347                 add_mtd_partitions(mtd, parts, numparts);
1348 #endif
1349         return 0;
1350 }
1351
1352 static int __init inftl_scan_bbt(struct mtd_info *mtd)
1353 {
1354         int ret, numparts;
1355         struct nand_chip *this = mtd->priv;
1356         struct doc_priv *doc = this->priv;
1357         struct mtd_partition parts[5];
1358
1359         if (this->numchips > doc->chips_per_floor) {
1360                 printk(KERN_ERR "Multi-floor INFTL devices not yet supported.\n");
1361                 return -EIO;
1362         }
1363
1364         if (DoC_is_MillenniumPlus(doc)) {
1365                 this->bbt_td->options = NAND_BBT_2BIT | NAND_BBT_ABSPAGE;
1366                 if (inftl_bbt_write)
1367                         this->bbt_td->options |= NAND_BBT_WRITE;
1368                 this->bbt_td->pages[0] = 2;
1369                 this->bbt_md = NULL;
1370         } else {
1371                 this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT |
1372                                         NAND_BBT_VERSION;
1373                 if (inftl_bbt_write)
1374                         this->bbt_td->options |= NAND_BBT_WRITE;
1375                 this->bbt_td->offs = 8;
1376                 this->bbt_td->len = 8;
1377                 this->bbt_td->veroffs = 7;
1378                 this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1379                 this->bbt_td->reserved_block_code = 0x01;
1380                 this->bbt_td->pattern = "MSYS_BBT";
1381
1382                 this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT |
1383                                         NAND_BBT_VERSION;
1384                 if (inftl_bbt_write)
1385                         this->bbt_md->options |= NAND_BBT_WRITE;
1386                 this->bbt_md->offs = 8;
1387                 this->bbt_md->len = 8;
1388                 this->bbt_md->veroffs = 7;
1389                 this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1390                 this->bbt_md->reserved_block_code = 0x01;
1391                 this->bbt_md->pattern = "TBB_SYSM";
1392         }
1393
1394         /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
1395            At least as nand_bbt.c is currently written. */
1396         if ((ret = nand_scan_bbt(mtd, NULL)))
1397                 return ret;
1398         memset((char *) parts, 0, sizeof(parts));
1399         numparts = inftl_partscan(mtd, parts);
1400         /* At least for now, require the INFTL Media Header.  We could probably
1401            do without it for non-INFTL use, since all it gives us is
1402            autopartitioning, but I want to give it more thought. */
1403         if (!numparts) return -EIO;
1404         add_mtd_device(mtd);
1405 #ifdef CONFIG_MTD_PARTITIONS
1406         if (!no_autopart)
1407                 add_mtd_partitions(mtd, parts, numparts);
1408 #endif
1409         return 0;
1410 }
1411
1412 static inline int __init doc2000_init(struct mtd_info *mtd)
1413 {
1414         struct nand_chip *this = mtd->priv;
1415         struct doc_priv *doc = this->priv;
1416
1417         this->write_byte = doc2000_write_byte;
1418         this->read_byte = doc2000_read_byte;
1419         this->write_buf = doc2000_writebuf;
1420         this->read_buf = doc2000_readbuf;
1421         this->verify_buf = doc2000_verifybuf;
1422         this->scan_bbt = nftl_scan_bbt;
1423
1424         doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
1425         doc2000_count_chips(mtd);
1426         mtd->name = "DiskOnChip 2000 (NFTL Model)";
1427         return (4 * doc->chips_per_floor);
1428 }
1429
1430 static inline int __init doc2001_init(struct mtd_info *mtd)
1431 {
1432         struct nand_chip *this = mtd->priv;
1433         struct doc_priv *doc = this->priv;
1434
1435         this->write_byte = doc2001_write_byte;
1436         this->read_byte = doc2001_read_byte;
1437         this->write_buf = doc2001_writebuf;
1438         this->read_buf = doc2001_readbuf;
1439         this->verify_buf = doc2001_verifybuf;
1440
1441         ReadDOC(doc->virtadr, ChipID);
1442         ReadDOC(doc->virtadr, ChipID);
1443         ReadDOC(doc->virtadr, ChipID);
1444         if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) {
1445                 /* It's not a Millennium; it's one of the newer
1446                    DiskOnChip 2000 units with a similar ASIC. 
1447                    Treat it like a Millennium, except that it
1448                    can have multiple chips. */
1449                 doc2000_count_chips(mtd);
1450                 mtd->name = "DiskOnChip 2000 (INFTL Model)";
1451                 this->scan_bbt = inftl_scan_bbt;
1452                 return (4 * doc->chips_per_floor);
1453         } else {
1454                 /* Bog-standard Millennium */
1455                 doc->chips_per_floor = 1;
1456                 mtd->name = "DiskOnChip Millennium";
1457                 this->scan_bbt = nftl_scan_bbt;
1458                 return 1;
1459         }
1460 }
1461
1462 static inline int __init doc2001plus_init(struct mtd_info *mtd)
1463 {
1464         struct nand_chip *this = mtd->priv;
1465         struct doc_priv *doc = this->priv;
1466
1467         this->write_byte = NULL;
1468         this->read_byte = doc2001plus_read_byte;
1469         this->write_buf = doc2001plus_writebuf;
1470         this->read_buf = doc2001plus_readbuf;
1471         this->verify_buf = doc2001plus_verifybuf;
1472         this->scan_bbt = inftl_scan_bbt;
1473         this->hwcontrol = NULL;
1474         this->select_chip = doc2001plus_select_chip;
1475         this->cmdfunc = doc2001plus_command;
1476         this->enable_hwecc = doc2001plus_enable_hwecc;
1477
1478         doc->chips_per_floor = 1;
1479         mtd->name = "DiskOnChip Millennium Plus";
1480
1481         return 1;
1482 }
1483
1484 static inline int __init doc_probe(unsigned long physadr)
1485 {
1486         unsigned char ChipID;
1487         struct mtd_info *mtd;
1488         struct nand_chip *nand;
1489         struct doc_priv *doc;
1490         void __iomem *virtadr;
1491         unsigned char save_control;
1492         unsigned char tmp, tmpb, tmpc;
1493         int reg, len, numchips;
1494         int ret = 0;
1495
1496         virtadr = ioremap(physadr, DOC_IOREMAP_LEN);
1497         if (!virtadr) {
1498                 printk(KERN_ERR "Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n", DOC_IOREMAP_LEN, physadr);
1499                 return -EIO;
1500         }
1501
1502         /* It's not possible to cleanly detect the DiskOnChip - the
1503          * bootup procedure will put the device into reset mode, and
1504          * it's not possible to talk to it without actually writing
1505          * to the DOCControl register. So we store the current contents
1506          * of the DOCControl register's location, in case we later decide
1507          * that it's not a DiskOnChip, and want to put it back how we
1508          * found it. 
1509          */
1510         save_control = ReadDOC(virtadr, DOCControl);
1511
1512         /* Reset the DiskOnChip ASIC */
1513         WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, 
1514                  virtadr, DOCControl);
1515         WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, 
1516                  virtadr, DOCControl);
1517
1518         /* Enable the DiskOnChip ASIC */
1519         WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, 
1520                  virtadr, DOCControl);
1521         WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, 
1522                  virtadr, DOCControl);
1523
1524         ChipID = ReadDOC(virtadr, ChipID);
1525
1526         switch(ChipID) {
1527         case DOC_ChipID_Doc2k:
1528                 reg = DoC_2k_ECCStatus;
1529                 break;
1530         case DOC_ChipID_DocMil:
1531                 reg = DoC_ECCConf;
1532                 break;
1533         case DOC_ChipID_DocMilPlus16:
1534         case DOC_ChipID_DocMilPlus32:
1535         case 0:
1536                 /* Possible Millennium Plus, need to do more checks */
1537                 /* Possibly release from power down mode */
1538                 for (tmp = 0; (tmp < 4); tmp++)
1539                         ReadDOC(virtadr, Mplus_Power);
1540
1541                 /* Reset the Millennium Plus ASIC */
1542                 tmp = DOC_MODE_RESET | DOC_MODE_MDWREN | DOC_MODE_RST_LAT |
1543                         DOC_MODE_BDECT;
1544                 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1545                 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1546
1547                 mdelay(1);
1548                 /* Enable the Millennium Plus ASIC */
1549                 tmp = DOC_MODE_NORMAL | DOC_MODE_MDWREN | DOC_MODE_RST_LAT |
1550                         DOC_MODE_BDECT;
1551                 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1552                 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1553                 mdelay(1);
1554
1555                 ChipID = ReadDOC(virtadr, ChipID);
1556
1557                 switch (ChipID) {
1558                 case DOC_ChipID_DocMilPlus16:
1559                         reg = DoC_Mplus_Toggle;
1560                         break;
1561                 case DOC_ChipID_DocMilPlus32:
1562                         printk(KERN_ERR "DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
1563                 default:
1564                         ret = -ENODEV;
1565                         goto notfound;
1566                 }
1567                 break;
1568
1569         default:
1570                 ret = -ENODEV;
1571                 goto notfound;
1572         }
1573         /* Check the TOGGLE bit in the ECC register */
1574         tmp  = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1575         tmpb = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1576         tmpc = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1577         if ((tmp == tmpb) || (tmp != tmpc)) {
1578                 printk(KERN_WARNING "Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr);
1579                 ret = -ENODEV;
1580                 goto notfound;
1581         }
1582
1583         for (mtd = doclist; mtd; mtd = doc->nextdoc) {
1584                 unsigned char oldval;
1585                 unsigned char newval;
1586                 nand = mtd->priv;
1587                 doc = nand->priv;
1588                 /* Use the alias resolution register to determine if this is
1589                    in fact the same DOC aliased to a new address.  If writes
1590                    to one chip's alias resolution register change the value on
1591                    the other chip, they're the same chip. */
1592                 if (ChipID == DOC_ChipID_DocMilPlus16) {
1593                         oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1594                         newval = ReadDOC(virtadr, Mplus_AliasResolution);
1595                 } else {
1596                         oldval = ReadDOC(doc->virtadr, AliasResolution);
1597                         newval = ReadDOC(virtadr, AliasResolution);
1598                 }
1599                 if (oldval != newval)
1600                         continue;
1601                 if (ChipID == DOC_ChipID_DocMilPlus16) {
1602                         WriteDOC(~newval, virtadr, Mplus_AliasResolution);
1603                         oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1604                         WriteDOC(newval, virtadr, Mplus_AliasResolution); // restore it
1605                 } else {
1606                         WriteDOC(~newval, virtadr, AliasResolution);
1607                         oldval = ReadDOC(doc->virtadr, AliasResolution);
1608                         WriteDOC(newval, virtadr, AliasResolution); // restore it
1609                 }
1610                 newval = ~newval;
1611                 if (oldval == newval) {
1612                         printk(KERN_DEBUG "Found alias of DOC at 0x%lx to 0x%lx\n", doc->physadr, physadr);
1613                         goto notfound;
1614                 }
1615         }
1616
1617         printk(KERN_NOTICE "DiskOnChip found at 0x%lx\n", physadr);
1618
1619         len = sizeof(struct mtd_info) +
1620               sizeof(struct nand_chip) +
1621               sizeof(struct doc_priv) +
1622               (2 * sizeof(struct nand_bbt_descr));
1623         mtd =  kmalloc(len, GFP_KERNEL);
1624         if (!mtd) {
1625                 printk(KERN_ERR "DiskOnChip kmalloc (%d bytes) failed!\n", len);
1626                 ret = -ENOMEM;
1627                 goto fail;
1628         }
1629         memset(mtd, 0, len);
1630
1631         nand                    = (struct nand_chip *) (mtd + 1);
1632         doc                     = (struct doc_priv *) (nand + 1);
1633         nand->bbt_td            = (struct nand_bbt_descr *) (doc + 1);
1634         nand->bbt_md            = nand->bbt_td + 1;
1635
1636         mtd->priv               = nand;
1637         mtd->owner              = THIS_MODULE;
1638
1639         nand->priv              = doc;
1640         nand->select_chip       = doc200x_select_chip;
1641         nand->hwcontrol         = doc200x_hwcontrol;
1642         nand->dev_ready         = doc200x_dev_ready;
1643         nand->waitfunc          = doc200x_wait;
1644         nand->block_bad         = doc200x_block_bad;
1645         nand->enable_hwecc      = doc200x_enable_hwecc;
1646         nand->calculate_ecc     = doc200x_calculate_ecc;
1647         nand->correct_data      = doc200x_correct_data;
1648
1649         nand->autooob           = &doc200x_oobinfo;
1650         nand->eccmode           = NAND_ECC_HW6_512;
1651         nand->options           = NAND_USE_FLASH_BBT | NAND_HWECC_SYNDROME;
1652
1653         doc->physadr            = physadr;
1654         doc->virtadr            = virtadr;
1655         doc->ChipID             = ChipID;
1656         doc->curfloor           = -1;
1657         doc->curchip            = -1;
1658         doc->mh0_page           = -1;
1659         doc->mh1_page           = -1;
1660         doc->nextdoc            = doclist;
1661
1662         if (ChipID == DOC_ChipID_Doc2k)
1663                 numchips = doc2000_init(mtd);
1664         else if (ChipID == DOC_ChipID_DocMilPlus16)
1665                 numchips = doc2001plus_init(mtd);
1666         else
1667                 numchips = doc2001_init(mtd);
1668
1669         if ((ret = nand_scan(mtd, numchips))) {
1670                 /* DBB note: i believe nand_release is necessary here, as
1671                    buffers may have been allocated in nand_base.  Check with
1672                    Thomas. FIX ME! */
1673                 /* nand_release will call del_mtd_device, but we haven't yet
1674                    added it.  This is handled without incident by
1675                    del_mtd_device, as far as I can tell. */
1676                 nand_release(mtd);
1677                 kfree(mtd);
1678                 goto fail;
1679         }
1680
1681         /* Success! */
1682         doclist = mtd;
1683         return 0;
1684
1685 notfound:
1686         /* Put back the contents of the DOCControl register, in case it's not
1687            actually a DiskOnChip.  */
1688         WriteDOC(save_control, virtadr, DOCControl);
1689 fail:
1690         iounmap(virtadr);
1691         return ret;
1692 }
1693
1694 static void release_nanddoc(void)
1695 {
1696         struct mtd_info *mtd, *nextmtd;
1697         struct nand_chip *nand;
1698         struct doc_priv *doc;
1699
1700         for (mtd = doclist; mtd; mtd = nextmtd) {
1701                 nand = mtd->priv;
1702                 doc = nand->priv;
1703
1704                 nextmtd = doc->nextdoc;
1705                 nand_release(mtd);
1706                 iounmap(doc->virtadr);
1707                 kfree(mtd);
1708         }
1709 }
1710
1711 static int __init init_nanddoc(void)
1712 {
1713         int i, ret = 0;
1714
1715         /* We could create the decoder on demand, if memory is a concern.
1716          * This way we have it handy, if an error happens 
1717          *
1718          * Symbolsize is 10 (bits)
1719          * Primitve polynomial is x^10+x^3+1
1720          * first consecutive root is 510
1721          * primitve element to generate roots = 1
1722          * generator polinomial degree = 4
1723          */
1724         rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS);
1725         if (!rs_decoder) {
1726                 printk (KERN_ERR "DiskOnChip: Could not create a RS decoder\n");
1727                 return -ENOMEM;
1728         }
1729
1730         if (doc_config_location) {
1731                 printk(KERN_INFO "Using configured DiskOnChip probe address 0x%lx\n", doc_config_location);
1732                 ret = doc_probe(doc_config_location);
1733                 if (ret < 0)
1734                         goto outerr;
1735         } else {
1736                 for (i=0; (doc_locations[i] != 0xffffffff); i++) {
1737                         doc_probe(doc_locations[i]);
1738                 }
1739         }
1740         /* No banner message any more. Print a message if no DiskOnChip
1741            found, so the user knows we at least tried. */
1742         if (!doclist) {
1743                 printk(KERN_INFO "No valid DiskOnChip devices found\n");
1744                 ret = -ENODEV;
1745                 goto outerr;
1746         }
1747         return 0;
1748 outerr:
1749         free_rs(rs_decoder);
1750         return ret;
1751 }
1752
1753 static void __exit cleanup_nanddoc(void)
1754 {
1755         /* Cleanup the nand/DoC resources */
1756         release_nanddoc();
1757
1758         /* Free the reed solomon resources */
1759         if (rs_decoder) {
1760                 free_rs(rs_decoder);
1761         }
1762 }
1763
1764 module_init(init_nanddoc);
1765 module_exit(cleanup_nanddoc);
1766
1767 MODULE_LICENSE("GPL");
1768 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1769 MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver\n");