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