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