This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / mtd / nand / diskonchip.c
1 /* 
2  * drivers/mtd/nand/diskonchip.c
3  *
4  * (C) 2003 Red Hat, Inc.
5  *
6  * Author: David Woodhouse <dwmw2@infradead.org>
7  *
8  * Interface to generic NAND code for M-Systems DiskOnChip devices
9  *
10  * $Id: diskonchip.c,v 1.23 2004/07/13 00:14:35 dbrown Exp $
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/sched.h>
16 #include <linux/delay.h>
17 #include <asm/io.h>
18
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/nand.h>
21 #include <linux/mtd/doc2000.h>
22 #include <linux/mtd/compatmac.h>
23 #include <linux/mtd/partitions.h>
24 #include <linux/mtd/inftl.h>
25
26 /* Where to look for the devices? */
27 #ifndef CONFIG_MTD_DOCPROBE_ADDRESS
28 #define CONFIG_MTD_DOCPROBE_ADDRESS 0
29 #endif
30
31 static unsigned long __initdata doc_locations[] = {
32 #if defined (__alpha__) || defined(__i386__) || defined(__x86_64__)
33 #ifdef CONFIG_MTD_DOCPROBE_HIGH
34         0xfffc8000, 0xfffca000, 0xfffcc000, 0xfffce000, 
35         0xfffd0000, 0xfffd2000, 0xfffd4000, 0xfffd6000,
36         0xfffd8000, 0xfffda000, 0xfffdc000, 0xfffde000, 
37         0xfffe0000, 0xfffe2000, 0xfffe4000, 0xfffe6000, 
38         0xfffe8000, 0xfffea000, 0xfffec000, 0xfffee000,
39 #else /*  CONFIG_MTD_DOCPROBE_HIGH */
40         0xc8000, 0xca000, 0xcc000, 0xce000, 
41         0xd0000, 0xd2000, 0xd4000, 0xd6000,
42         0xd8000, 0xda000, 0xdc000, 0xde000, 
43         0xe0000, 0xe2000, 0xe4000, 0xe6000, 
44         0xe8000, 0xea000, 0xec000, 0xee000,
45 #endif /*  CONFIG_MTD_DOCPROBE_HIGH */
46 #elif defined(__PPC__)
47         0xe4000000,
48 #elif defined(CONFIG_MOMENCO_OCELOT)
49         0x2f000000,
50         0xff000000,
51 #elif defined(CONFIG_MOMENCO_OCELOT_G) || defined (CONFIG_MOMENCO_OCELOT_C)
52         0xff000000,
53 ##else
54 #warning Unknown architecture for DiskOnChip. No default probe locations defined
55 #endif
56         0xffffffff };
57
58 static struct mtd_info *doclist = NULL;
59
60 struct doc_priv {
61         unsigned long virtadr;
62         unsigned long physadr;
63         u_char ChipID;
64         u_char CDSNControl;
65         int chips_per_floor; /* The number of chips detected on each floor */
66         int curfloor;
67         int curchip;
68         int mh0_page;
69         int mh1_page;
70         struct mtd_info *nextdoc;
71 };
72
73 /* Max number of eraseblocks to scan (from start of device) for the (I)NFTL
74    MediaHeader.  The spec says to just keep going, I think, but that's just
75    silly. */
76 #define MAX_MEDIAHEADER_SCAN 8
77
78 /* This is the syndrome computed by the HW ecc generator upon reading an empty
79    page, one with all 0xff for data and stored ecc code. */
80 static u_char empty_read_syndrome[6] = { 0x26, 0xff, 0x6d, 0x47, 0x73, 0x7a };
81 /* This is the ecc value computed by the HW ecc generator upon writing an empty
82    page, one with all 0xff for data. */
83 static u_char empty_write_ecc[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
84
85 #define INFTL_BBT_RESERVED_BLOCKS 4
86
87 #define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
88 #define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
89
90 static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd);
91 static void doc200x_select_chip(struct mtd_info *mtd, int chip);
92
93 static int debug=0;
94 MODULE_PARM(debug, "i");
95
96 static int try_dword=1;
97 MODULE_PARM(try_dword, "i");
98
99 static int no_ecc_failures=0;
100 MODULE_PARM(no_ecc_failures, "i");
101
102 static int no_autopart=0;
103 MODULE_PARM(no_autopart, "i");
104
105 #ifdef MTD_NAND_DISKONCHIP_BBTWRITE
106 static int inftl_bbt_write=1;
107 #else
108 static int inftl_bbt_write=0;
109 #endif
110 MODULE_PARM(inftl_bbt_write, "i");
111
112 static unsigned long doc_config_location = CONFIG_MTD_DOCPROBE_ADDRESS;
113 MODULE_PARM(doc_config_location, "l");
114 MODULE_PARM_DESC(doc_config_location, "Physical memory address at which to probe for DiskOnChip");
115
116 static void DoC_Delay(struct doc_priv *doc, unsigned short cycles)
117 {
118         volatile char dummy;
119         int i;
120         
121         for (i = 0; i < cycles; i++) {
122                 if (DoC_is_Millennium(doc))
123                         dummy = ReadDOC(doc->virtadr, NOP);
124                 else
125                         dummy = ReadDOC(doc->virtadr, DOCStatus);
126         }
127         
128 }
129 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
130 static int _DoC_WaitReady(struct doc_priv *doc)
131 {
132         unsigned long docptr = doc->virtadr;
133         unsigned long timeo = jiffies + (HZ * 10);
134
135         if(debug) printk("_DoC_WaitReady...\n");
136         /* Out-of-line routine to wait for chip response */
137         while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
138                 if (time_after(jiffies, timeo)) {
139                         printk("_DoC_WaitReady timed out.\n");
140                         return -EIO;
141                 }
142                 udelay(1);
143                 cond_resched();
144         }
145
146         return 0;
147 }
148
149 static inline int DoC_WaitReady(struct doc_priv *doc)
150 {
151         unsigned long docptr = doc->virtadr;
152         int ret = 0;
153
154         DoC_Delay(doc, 4);
155
156         if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
157                 /* Call the out-of-line routine to wait */
158                 ret = _DoC_WaitReady(doc);
159
160         DoC_Delay(doc, 2);
161         if(debug) printk("DoC_WaitReady OK\n");
162         return ret;
163 }
164
165 static void doc2000_write_byte(struct mtd_info *mtd, u_char datum)
166 {
167         struct nand_chip *this = mtd->priv;
168         struct doc_priv *doc = (void *)this->priv;
169         unsigned long docptr = doc->virtadr;
170
171         if(debug)printk("write_byte %02x\n", datum);
172         WriteDOC(datum, docptr, CDSNSlowIO);
173         WriteDOC(datum, docptr, 2k_CDSN_IO);
174 }
175
176 static u_char doc2000_read_byte(struct mtd_info *mtd)
177 {
178         struct nand_chip *this = mtd->priv;
179         struct doc_priv *doc = (void *)this->priv;
180         unsigned long docptr = doc->virtadr;
181         u_char ret;
182
183         ReadDOC(docptr, CDSNSlowIO);
184         DoC_Delay(doc, 2);
185         ret = ReadDOC(docptr, 2k_CDSN_IO);
186         if (debug) printk("read_byte returns %02x\n", ret);
187         return ret;
188 }
189
190 static void doc2000_writebuf(struct mtd_info *mtd, 
191                              const u_char *buf, int len)
192 {
193         struct nand_chip *this = mtd->priv;
194         struct doc_priv *doc = (void *)this->priv;
195         unsigned long docptr = doc->virtadr;
196         int i;
197         if (debug)printk("writebuf of %d bytes: ", len);
198         for (i=0; i < len; i++) {
199                 WriteDOC_(buf[i], docptr, DoC_2k_CDSN_IO + i);
200                 if (debug && i < 16)
201                         printk("%02x ", buf[i]);
202         }
203         if (debug) printk("\n");
204 }
205
206 static void doc2000_readbuf(struct mtd_info *mtd, 
207                             u_char *buf, int len)
208 {
209         struct nand_chip *this = mtd->priv;
210         struct doc_priv *doc = (void *)this->priv;
211         unsigned long docptr = doc->virtadr;
212         int i;
213
214         if (debug)printk("readbuf of %d bytes: ", len);
215
216         for (i=0; i < len; i++) {
217                 buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
218         }
219 }
220
221 static void doc2000_readbuf_dword(struct mtd_info *mtd, 
222                             u_char *buf, int len)
223 {
224         struct nand_chip *this = mtd->priv;
225         struct doc_priv *doc = (void *)this->priv;
226         unsigned long docptr = doc->virtadr;
227         int i;
228
229         if (debug) printk("readbuf_dword of %d bytes: ", len);
230
231         if (unlikely((((unsigned long)buf)|len) & 3)) {
232                 for (i=0; i < len; i++) {
233                         *(uint8_t *)(&buf[i]) = ReadDOC(docptr, 2k_CDSN_IO + i);
234                 }
235         } else {
236                 for (i=0; i < len; i+=4) {
237                         *(uint32_t*)(&buf[i]) = readl(docptr + DoC_2k_CDSN_IO + i);
238                 }
239         }
240 }
241
242 static int doc2000_verifybuf(struct mtd_info *mtd, 
243                               const u_char *buf, int len)
244 {
245         struct nand_chip *this = mtd->priv;
246         struct doc_priv *doc = (void *)this->priv;
247         unsigned long docptr = doc->virtadr;
248         int i;
249
250         for (i=0; i < len; i++)
251                 if (buf[i] != ReadDOC(docptr, 2k_CDSN_IO))
252                         return -EFAULT;
253         return 0;
254 }
255
256 static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr)
257 {
258         struct nand_chip *this = mtd->priv;
259         struct doc_priv *doc = (void *)this->priv;
260         uint16_t ret;
261
262         doc200x_select_chip(mtd, nr);
263         doc200x_hwcontrol(mtd, NAND_CTL_SETCLE);
264         this->write_byte(mtd, NAND_CMD_READID);
265         doc200x_hwcontrol(mtd, NAND_CTL_CLRCLE);
266         doc200x_hwcontrol(mtd, NAND_CTL_SETALE);
267         this->write_byte(mtd, 0);
268         doc200x_hwcontrol(mtd, NAND_CTL_CLRALE);
269
270         ret = this->read_byte(mtd) << 8;
271         ret |= this->read_byte(mtd);
272
273         if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) {
274                 /* First chip probe. See if we get same results by 32-bit access */
275                 union {
276                         uint32_t dword;
277                         uint8_t byte[4];
278                 } ident;
279                 unsigned long docptr = doc->virtadr;
280
281                 doc200x_hwcontrol(mtd, NAND_CTL_SETCLE);
282                 doc2000_write_byte(mtd, NAND_CMD_READID);
283                 doc200x_hwcontrol(mtd, NAND_CTL_CLRCLE);
284                 doc200x_hwcontrol(mtd, NAND_CTL_SETALE);
285                 doc2000_write_byte(mtd, 0);
286                 doc200x_hwcontrol(mtd, NAND_CTL_CLRALE);
287
288                 ident.dword = readl(docptr + DoC_2k_CDSN_IO);
289                 if (((ident.byte[0] << 8) | ident.byte[1]) == ret) {
290                         printk(KERN_INFO "DiskOnChip 2000 responds to DWORD access\n");
291                         this->read_buf = &doc2000_readbuf_dword;
292                 }
293         }
294                 
295         return ret;
296 }
297
298 static void __init doc2000_count_chips(struct mtd_info *mtd)
299 {
300         struct nand_chip *this = mtd->priv;
301         struct doc_priv *doc = (void *)this->priv;
302         uint16_t mfrid;
303         int i;
304
305         /* Max 4 chips per floor on DiskOnChip 2000 */
306         doc->chips_per_floor = 4;
307
308         /* Find out what the first chip is */
309         mfrid = doc200x_ident_chip(mtd, 0);
310
311         /* Find how many chips in each floor. */
312         for (i = 1; i < 4; i++) {
313                 if (doc200x_ident_chip(mtd, i) != mfrid)
314                         break;
315         }
316         doc->chips_per_floor = i;
317         printk(KERN_DEBUG "Detected %d chips per floor.\n", i);
318 }
319
320 static int doc200x_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
321 {
322         struct doc_priv *doc = (void *)this->priv;
323
324         int status;
325         
326         DoC_WaitReady(doc);
327         this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
328         DoC_WaitReady(doc);
329         status = (int)this->read_byte(mtd);
330
331         return status;
332 }
333
334 static void doc2001_write_byte(struct mtd_info *mtd, u_char datum)
335 {
336         struct nand_chip *this = mtd->priv;
337         struct doc_priv *doc = (void *)this->priv;
338         unsigned long docptr = doc->virtadr;
339
340         WriteDOC(datum, docptr, CDSNSlowIO);
341         WriteDOC(datum, docptr, Mil_CDSN_IO);
342         WriteDOC(datum, docptr, WritePipeTerm);
343 }
344
345 static u_char doc2001_read_byte(struct mtd_info *mtd)
346 {
347         struct nand_chip *this = mtd->priv;
348         struct doc_priv *doc = (void *)this->priv;
349         unsigned long docptr = doc->virtadr;
350
351         //ReadDOC(docptr, CDSNSlowIO);
352         /* 11.4.5 -- delay twice to allow extended length cycle */
353         DoC_Delay(doc, 2);
354         ReadDOC(docptr, ReadPipeInit);
355         //return ReadDOC(docptr, Mil_CDSN_IO);
356         return ReadDOC(docptr, LastDataRead);
357 }
358
359 static void doc2001_writebuf(struct mtd_info *mtd, 
360                              const u_char *buf, int len)
361 {
362         struct nand_chip *this = mtd->priv;
363         struct doc_priv *doc = (void *)this->priv;
364         unsigned long docptr = doc->virtadr;
365         int i;
366
367         for (i=0; i < len; i++)
368                 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
369         /* Terminate write pipeline */
370         WriteDOC(0x00, docptr, WritePipeTerm);
371 }
372
373 static void doc2001_readbuf(struct mtd_info *mtd, 
374                             u_char *buf, int len)
375 {
376         struct nand_chip *this = mtd->priv;
377         struct doc_priv *doc = (void *)this->priv;
378         unsigned long docptr = doc->virtadr;
379         int i;
380
381         /* Start read pipeline */
382         ReadDOC(docptr, ReadPipeInit);
383
384         for (i=0; i < len-1; i++)
385                 buf[i] = ReadDOC(docptr, Mil_CDSN_IO);
386
387         /* Terminate read pipeline */
388         buf[i] = ReadDOC(docptr, LastDataRead);
389 }
390
391 static int doc2001_verifybuf(struct mtd_info *mtd, 
392                              const u_char *buf, int len)
393 {
394         struct nand_chip *this = mtd->priv;
395         struct doc_priv *doc = (void *)this->priv;
396         unsigned long docptr = doc->virtadr;
397         int i;
398
399         /* Start read pipeline */
400         ReadDOC(docptr, ReadPipeInit);
401
402         for (i=0; i < len-1; i++)
403                 if (buf[i] != ReadDOC(docptr, Mil_CDSN_IO)) {
404                         ReadDOC(docptr, LastDataRead);
405                         return i;
406                 }
407         if (buf[i] != ReadDOC(docptr, LastDataRead))
408                 return i;
409         return 0;
410 }
411
412 static void doc200x_select_chip(struct mtd_info *mtd, int chip)
413 {
414         struct nand_chip *this = mtd->priv;
415         struct doc_priv *doc = (void *)this->priv;
416         unsigned long docptr = doc->virtadr;
417         int floor = 0;
418
419         /* 11.4.4 -- deassert CE before changing chip */
420         doc200x_hwcontrol(mtd, NAND_CTL_CLRNCE);
421
422         if(debug)printk("select chip (%d)\n", chip);
423
424         if (chip == -1)
425                 return;
426
427         floor = chip / doc->chips_per_floor;
428         chip -= (floor *  doc->chips_per_floor);
429
430         WriteDOC(floor, docptr, FloorSelect);
431         WriteDOC(chip, docptr, CDSNDeviceSelect);
432
433         doc200x_hwcontrol(mtd, NAND_CTL_SETNCE);
434
435         doc->curchip = chip;
436         doc->curfloor = floor;
437 }
438
439 static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd)
440 {
441         struct nand_chip *this = mtd->priv;
442         struct doc_priv *doc = (void *)this->priv;
443         unsigned long docptr = doc->virtadr;
444
445         switch(cmd) {
446         case NAND_CTL_SETNCE:
447                 doc->CDSNControl |= CDSN_CTRL_CE;
448                 break;
449         case NAND_CTL_CLRNCE:
450                 doc->CDSNControl &= ~CDSN_CTRL_CE;
451                 break;
452         case NAND_CTL_SETCLE:
453                 doc->CDSNControl |= CDSN_CTRL_CLE;
454                 break;
455         case NAND_CTL_CLRCLE:
456                 doc->CDSNControl &= ~CDSN_CTRL_CLE;
457                 break;
458         case NAND_CTL_SETALE:
459                 doc->CDSNControl |= CDSN_CTRL_ALE;
460                 break;
461         case NAND_CTL_CLRALE:
462                 doc->CDSNControl &= ~CDSN_CTRL_ALE;
463                 break;
464         case NAND_CTL_SETWP:
465                 doc->CDSNControl |= CDSN_CTRL_WP;
466                 break;
467         case NAND_CTL_CLRWP:
468                 doc->CDSNControl &= ~CDSN_CTRL_WP;
469                 break;
470         }
471         if (debug)printk("hwcontrol(%d): %02x\n", cmd, doc->CDSNControl);
472         WriteDOC(doc->CDSNControl, docptr, CDSNControl);
473         /* 11.4.3 -- 4 NOPs after CSDNControl write */
474         DoC_Delay(doc, 4);
475 }
476
477 static int doc200x_dev_ready(struct mtd_info *mtd)
478 {
479         struct nand_chip *this = mtd->priv;
480         struct doc_priv *doc = (void *)this->priv;
481         unsigned long docptr = doc->virtadr;
482
483         /* 11.4.2 -- must NOP four times before checking FR/B# */
484         DoC_Delay(doc, 4);
485         if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
486                 if(debug)
487                         printk("not ready\n");
488                 return 0;
489         }
490         /* 11.4.2 -- Must NOP twice if it's ready */
491         DoC_Delay(doc, 2);
492         if (debug)printk("was ready\n");
493         return 1; 
494 }       
495
496 static int doc200x_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
497 {
498         /* This is our last resort if we couldn't find or create a BBT.  Just
499            pretend all blocks are good. */
500         return 0;
501 }
502
503 static void doc200x_enable_hwecc(struct mtd_info *mtd, int mode)
504 {
505         struct nand_chip *this = mtd->priv;
506         struct doc_priv *doc = (void *)this->priv;
507         unsigned long docptr = doc->virtadr;
508
509         /* Prime the ECC engine */
510         switch(mode) {
511         case NAND_ECC_READ:
512                 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
513                 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
514                 break;
515         case NAND_ECC_WRITE:
516                 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
517                 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
518                 break;
519         }       
520 }
521
522 /* This code is only called on write */
523 static int doc200x_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
524                                  unsigned char *ecc_code)
525 {
526         struct nand_chip *this = mtd->priv;
527         struct doc_priv *doc = (void *)this->priv;
528         unsigned long docptr = doc->virtadr;
529         int i;
530         int emptymatch = 1;
531
532         /* flush the pipeline */
533         if (DoC_is_2000(doc)) {
534                 WriteDOC(doc->CDSNControl & ~CDSN_CTRL_FLASH_IO, docptr, CDSNControl);
535                 WriteDOC(0, docptr, 2k_CDSN_IO);
536                 WriteDOC(0, docptr, 2k_CDSN_IO);
537                 WriteDOC(0, docptr, 2k_CDSN_IO);
538                 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
539         } else {
540                 WriteDOC(0, docptr, NOP);
541                 WriteDOC(0, docptr, NOP);
542                 WriteDOC(0, docptr, NOP);
543         }
544
545         for (i = 0; i < 6; i++) {
546                 ecc_code[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
547                 if (ecc_code[i] != empty_write_ecc[i])
548                         emptymatch = 0;
549         }
550         WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
551 #if 0
552         /* If emptymatch=1, we might have an all-0xff data buffer.  Check. */
553         if (emptymatch) {
554                 /* Note: this somewhat expensive test should not be triggered
555                    often.  It could be optimized away by examining the data in
556                    the writebuf routine, and remembering the result. */
557                 for (i = 0; i < 512; i++) {
558                         if (dat[i] == 0xff) continue;
559                         emptymatch = 0;
560                         break;
561                 }
562         }
563         /* If emptymatch still =1, we do have an all-0xff data buffer.
564            Return all-0xff ecc value instead of the computed one, so
565            it'll look just like a freshly-erased page. */
566         if (emptymatch) memset(ecc_code, 0xff, 6);
567 #endif
568         return 0;
569 }
570
571 static int doc200x_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc)
572 {
573         int i, ret = 0;
574         struct nand_chip *this = mtd->priv;
575         struct doc_priv *doc = (void *)this->priv;
576         unsigned long docptr = doc->virtadr;
577         volatile u_char dummy;
578         int emptymatch = 1;
579         
580         /* flush the pipeline */
581         if (DoC_is_2000(doc)) {
582                 dummy = ReadDOC(docptr, 2k_ECCStatus);
583                 dummy = ReadDOC(docptr, 2k_ECCStatus);
584                 dummy = ReadDOC(docptr, 2k_ECCStatus);
585         } else {
586                 dummy = ReadDOC(docptr, ECCConf);
587                 dummy = ReadDOC(docptr, ECCConf);
588                 dummy = ReadDOC(docptr, ECCConf);
589         }
590         
591         /* Error occured ? */
592         if (dummy & 0x80) {
593                 for (i = 0; i < 6; i++) {
594                         calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
595                         if (calc_ecc[i] != empty_read_syndrome[i])
596                                 emptymatch = 0;
597                 }
598                 /* If emptymatch=1, the read syndrome is consistent with an
599                    all-0xff data and stored ecc block.  Check the stored ecc. */
600                 if (emptymatch) {
601                         for (i = 0; i < 6; i++) {
602                                 if (read_ecc[i] == 0xff) continue;
603                                 emptymatch = 0;
604                                 break;
605                         }
606                 }
607                 /* If emptymatch still =1, check the data block. */
608                 if (emptymatch) {
609                 /* Note: this somewhat expensive test should not be triggered
610                    often.  It could be optimized away by examining the data in
611                    the readbuf routine, and remembering the result. */
612                         for (i = 0; i < 512; i++) {
613                                 if (dat[i] == 0xff) continue;
614                                 emptymatch = 0;
615                                 break;
616                         }
617                 }
618                 /* If emptymatch still =1, this is almost certainly a freshly-
619                    erased block, in which case the ECC will not come out right.
620                    We'll suppress the error and tell the caller everything's
621                    OK.  Because it is. */
622                 if (!emptymatch) ret = doc_decode_ecc (dat, calc_ecc);
623                 if (ret > 0)
624                         printk(KERN_ERR "doc200x_correct_data corrected %d errors\n", ret);
625         }       
626         WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
627         if (no_ecc_failures && (ret == -1)) {
628                 printk(KERN_ERR "suppressing ECC failure\n");
629                 ret = 0;
630         }
631         return ret;
632 }
633                 
634 //u_char mydatabuf[528];
635
636 static struct nand_oobinfo doc200x_oobinfo = {
637         .useecc = MTD_NANDECC_AUTOPLACE,
638         .eccbytes = 6,
639         .eccpos = {0, 1, 2, 3, 4, 5},
640         .oobfree = { {8, 8} }
641 };
642  
643 /* Find the (I)NFTL Media Header, and optionally also the mirror media header.
644    On sucessful return, buf will contain a copy of the media header for
645    further processing.  id is the string to scan for, and will presumably be
646    either "ANAND" or "BNAND".  If findmirror=1, also look for the mirror media
647    header.  The page #s of the found media headers are placed in mh0_page and
648    mh1_page in the DOC private structure. */
649 static int __init find_media_headers(struct mtd_info *mtd, u_char *buf,
650                                      const char *id, int findmirror)
651 {
652         struct nand_chip *this = mtd->priv;
653         struct doc_priv *doc = (void *)this->priv;
654         int offs, end = (MAX_MEDIAHEADER_SCAN << this->phys_erase_shift);
655         int ret, retlen;
656
657         end = min(end, mtd->size); // paranoia
658         for (offs = 0; offs < end; offs += mtd->erasesize) {
659                 ret = mtd->read(mtd, offs, mtd->oobblock, &retlen, buf);
660                 if (retlen != mtd->oobblock) continue;
661                 if (ret) {
662                         printk(KERN_WARNING "ECC error scanning DOC at 0x%x\n",
663                                 offs);
664                 }
665                 if (memcmp(buf, id, 6)) continue;
666                 printk(KERN_INFO "Found DiskOnChip %s Media Header at 0x%x\n", id, offs);
667                 if (doc->mh0_page == -1) {
668                         doc->mh0_page = offs >> this->page_shift;
669                         if (!findmirror) return 1;
670                         continue;
671                 }
672                 doc->mh1_page = offs >> this->page_shift;
673                 return 2;
674         }
675         if (doc->mh0_page == -1) {
676                 printk(KERN_WARNING "DiskOnChip %s Media Header not found.\n", id);
677                 return 0;
678         }
679         /* Only one mediaheader was found.  We want buf to contain a
680            mediaheader on return, so we'll have to re-read the one we found. */
681         offs = doc->mh0_page << this->page_shift;
682         ret = mtd->read(mtd, offs, mtd->oobblock, &retlen, buf);
683         if (retlen != mtd->oobblock) {
684                 /* Insanity.  Give up. */
685                 printk(KERN_ERR "Read DiskOnChip Media Header once, but can't reread it???\n");
686                 return 0;
687         }
688         return 1;
689 }
690
691 static inline int __init nftl_partscan(struct mtd_info *mtd,
692                                 struct mtd_partition *parts)
693 {
694         struct nand_chip *this = mtd->priv;
695         struct doc_priv *doc = (void *)this->priv;
696         u_char *buf = this->data_buf;
697         struct NFTLMediaHeader *mh = (struct NFTLMediaHeader *) buf;
698         const int psize = 1 << this->page_shift;
699         int blocks, maxblocks;
700         int offs, numheaders;
701
702         if (!(numheaders=find_media_headers(mtd, buf, "ANAND", 1))) return 0;
703
704 //#ifdef CONFIG_MTD_DEBUG_VERBOSE
705 //      if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
706         printk(KERN_INFO "    DataOrgID        = %s\n"
707                          "    NumEraseUnits    = %d\n"
708                          "    FirstPhysicalEUN = %d\n"
709                          "    FormattedSize    = %d\n"
710                          "    UnitSizeFactor   = %d\n",
711                 mh->DataOrgID, mh->NumEraseUnits,
712                 mh->FirstPhysicalEUN, mh->FormattedSize,
713                 mh->UnitSizeFactor);
714 //#endif
715
716         blocks = mtd->size >> this->phys_erase_shift;
717         maxblocks = min(32768, mtd->erasesize - psize);
718
719         if (mh->UnitSizeFactor == 0x00) {
720                 /* Auto-determine UnitSizeFactor.  The constraints are:
721                    - There can be at most 32768 virtual blocks.
722                    - There can be at most (virtual block size - page size)
723                      virtual blocks (because MediaHeader+BBT must fit in 1).
724                 */
725                 mh->UnitSizeFactor = 0xff;
726                 while (blocks > maxblocks) {
727                         blocks >>= 1;
728                         maxblocks = min(32768, (maxblocks << 1) + psize);
729                         mh->UnitSizeFactor--;
730                 }
731                 printk(KERN_WARNING "UnitSizeFactor=0x00 detected.  Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor);
732         }
733
734         /* NOTE: The lines below modify internal variables of the NAND and MTD
735            layers; variables with have already been configured by nand_scan.
736            Unfortunately, we didn't know before this point what these values
737            should be.  Thus, this code is somewhat dependant on the exact
738            implementation of the NAND layer.  */
739         if (mh->UnitSizeFactor != 0xff) {
740                 this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
741                 mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
742                 printk(KERN_INFO "Setting virtual erase size to %d\n", mtd->erasesize);
743                 blocks = mtd->size >> this->bbt_erase_shift;
744                 maxblocks = min(32768, mtd->erasesize - psize);
745         }
746
747         if (blocks > maxblocks) {
748                 printk(KERN_ERR "UnitSizeFactor of 0x%02x is inconsistent with device size.  Aborting.\n", mh->UnitSizeFactor);
749                 return 0;
750         }
751
752         /* Skip past the media headers. */
753         offs = max(doc->mh0_page, doc->mh1_page);
754         offs <<= this->page_shift;
755         offs += mtd->erasesize;
756
757         //parts[0].name = " DiskOnChip Boot / Media Header partition";
758         //parts[0].offset = 0;
759         //parts[0].size = offs;
760
761         parts[0].name = " DiskOnChip BDTL partition";
762         parts[0].offset = offs;
763         parts[0].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift;
764
765         offs += parts[0].size;
766         if (offs < mtd->size) {
767                 parts[1].name = " DiskOnChip Remainder partition";
768                 parts[1].offset = offs;
769                 parts[1].size = mtd->size - offs;
770                 return 2;
771         }
772         return 1;
773 }
774
775 /* This is a stripped-down copy of the code in inftlmount.c */
776 static inline int __init inftl_partscan(struct mtd_info *mtd,
777                                  struct mtd_partition *parts)
778 {
779         struct nand_chip *this = mtd->priv;
780         struct doc_priv *doc = (void *)this->priv;
781         u_char *buf = this->data_buf;
782         struct INFTLMediaHeader *mh = (struct INFTLMediaHeader *) buf;
783         struct INFTLPartition *ip;
784         int numparts = 0;
785         int blocks;
786         int vshift, lastvunit = 0;
787         int i;
788         int end = mtd->size;
789
790         if (inftl_bbt_write)
791                 end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift);
792
793         if (!find_media_headers(mtd, buf, "BNAND", 0)) return 0;
794         doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift);
795
796         mh->NoOfBootImageBlocks = le32_to_cpu(mh->NoOfBootImageBlocks);
797         mh->NoOfBinaryPartitions = le32_to_cpu(mh->NoOfBinaryPartitions);
798         mh->NoOfBDTLPartitions = le32_to_cpu(mh->NoOfBDTLPartitions);
799         mh->BlockMultiplierBits = le32_to_cpu(mh->BlockMultiplierBits);
800         mh->FormatFlags = le32_to_cpu(mh->FormatFlags);
801         mh->PercentUsed = le32_to_cpu(mh->PercentUsed);
802  
803 //#ifdef CONFIG_MTD_DEBUG_VERBOSE
804 //      if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
805         printk(KERN_INFO "    bootRecordID          = %s\n"
806                          "    NoOfBootImageBlocks   = %d\n"
807                          "    NoOfBinaryPartitions  = %d\n"
808                          "    NoOfBDTLPartitions    = %d\n"
809                          "    BlockMultiplerBits    = %d\n"
810                          "    FormatFlgs            = %d\n"
811                          "    OsakVersion           = 0x%x\n"
812                          "    PercentUsed           = %d\n",
813                 mh->bootRecordID, mh->NoOfBootImageBlocks,
814                 mh->NoOfBinaryPartitions,
815                 mh->NoOfBDTLPartitions,
816                 mh->BlockMultiplierBits, mh->FormatFlags,
817                 mh->OsakVersion, mh->PercentUsed);
818 //#endif
819
820         vshift = this->phys_erase_shift + mh->BlockMultiplierBits;
821
822         blocks = mtd->size >> vshift;
823         if (blocks > 32768) {
824                 printk(KERN_ERR "BlockMultiplierBits=%d is inconsistent with device size.  Aborting.\n", mh->BlockMultiplierBits);
825                 return 0;
826         }
827
828         blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift);
829         if (inftl_bbt_write && (blocks > mtd->erasesize)) {
830                 printk(KERN_ERR "Writeable BBTs spanning more than one erase block are not yet supported.  FIX ME!\n");
831                 return 0;
832         }
833
834         /* Scan the partitions */
835         for (i = 0; (i < 4); i++) {
836                 ip = &(mh->Partitions[i]);
837                 ip->virtualUnits = le32_to_cpu(ip->virtualUnits);
838                 ip->firstUnit = le32_to_cpu(ip->firstUnit);
839                 ip->lastUnit = le32_to_cpu(ip->lastUnit);
840                 ip->flags = le32_to_cpu(ip->flags);
841                 ip->spareUnits = le32_to_cpu(ip->spareUnits);
842                 ip->Reserved0 = le32_to_cpu(ip->Reserved0);
843
844 //#ifdef CONFIG_MTD_DEBUG_VERBOSE
845 //              if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
846                 printk(KERN_INFO        "    PARTITION[%d] ->\n"
847                         "        virtualUnits    = %d\n"
848                         "        firstUnit       = %d\n"
849                         "        lastUnit        = %d\n"
850                         "        flags           = 0x%x\n"
851                         "        spareUnits      = %d\n",
852                         i, ip->virtualUnits, ip->firstUnit,
853                         ip->lastUnit, ip->flags,
854                         ip->spareUnits);
855 //#endif
856
857 /*
858                 if ((i == 0) && (ip->firstUnit > 0)) {
859                         parts[0].name = " DiskOnChip IPL / Media Header partition";
860                         parts[0].offset = 0;
861                         parts[0].size = mtd->erasesize * ip->firstUnit;
862                         numparts = 1;
863                 }
864 */
865
866                 if (ip->flags & INFTL_BINARY)
867                         parts[numparts].name = " DiskOnChip BDK partition";
868                 else
869                         parts[numparts].name = " DiskOnChip BDTL partition";
870                 parts[numparts].offset = ip->firstUnit << vshift;
871                 parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift;
872                 numparts++;
873                 if (ip->lastUnit > lastvunit) lastvunit = ip->lastUnit;
874                 if (ip->flags & INFTL_LAST) break;
875         }
876         lastvunit++;
877         if ((lastvunit << vshift) < end) {
878                 parts[numparts].name = " DiskOnChip Remainder partition";
879                 parts[numparts].offset = lastvunit << vshift;
880                 parts[numparts].size = end - parts[numparts].offset;
881                 numparts++;
882         }
883         return numparts;
884 }
885
886 static int __init nftl_scan_bbt(struct mtd_info *mtd)
887 {
888         int ret, numparts;
889         struct nand_chip *this = mtd->priv;
890         struct doc_priv *doc = (void *)this->priv;
891         struct mtd_partition parts[2];
892
893         memset((char *) parts, 0, sizeof(parts));
894         /* On NFTL, we have to find the media headers before we can read the
895            BBTs, since they're stored in the media header eraseblocks. */
896         numparts = nftl_partscan(mtd, parts);
897         if (!numparts) return -EIO;
898         this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
899                                 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
900                                 NAND_BBT_VERSION;
901         this->bbt_td->veroffs = 7;
902         this->bbt_td->pages[0] = doc->mh0_page + 1;
903         if (doc->mh1_page != -1) {
904                 this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
905                                         NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
906                                         NAND_BBT_VERSION;
907                 this->bbt_md->veroffs = 7;
908                 this->bbt_md->pages[0] = doc->mh1_page + 1;
909         } else {
910                 this->bbt_md = NULL;
911         }
912
913         /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
914            At least as nand_bbt.c is currently written. */
915         if ((ret = nand_scan_bbt(mtd, NULL)))
916                 return ret;
917         add_mtd_device(mtd);
918 #if defined(CONFIG_MTD_PARTITIONS) || defined(CONFIG_MTD_PARTITIONS_MODULE)
919         if (!no_autopart) add_mtd_partitions(mtd, parts, numparts);
920 #endif
921         return 0;
922 }
923
924 static int __init inftl_scan_bbt(struct mtd_info *mtd)
925 {
926         int ret, numparts;
927         struct nand_chip *this = mtd->priv;
928         struct doc_priv *doc = (void *)this->priv;
929         struct mtd_partition parts[5];
930
931         if (this->numchips > doc->chips_per_floor) {
932                 printk(KERN_ERR "Multi-floor INFTL devices not yet supported.\n");
933                 return -EIO;
934         }
935
936         if (mtd->size == (8<<20)) {
937 #if 0
938 /* This doesn't seem to work for me.  I get ECC errors on every page. */
939                 /* The Millennium 8MiB is actually an NFTL device! */
940                 mtd->name = "DiskOnChip Millennium 8MiB (NFTL)";
941                 return nftl_scan_bbt(mtd);
942 #endif
943                 printk(KERN_ERR "DiskOnChip Millennium 8MiB is not supported.\n");
944                 return -EIO;
945         }
946
947         this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT |
948                                 NAND_BBT_VERSION;
949         if (inftl_bbt_write)
950                 this->bbt_td->options |= NAND_BBT_WRITE;
951         this->bbt_td->offs = 8;
952         this->bbt_td->len = 8;
953         this->bbt_td->veroffs = 7;
954         this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
955         this->bbt_td->reserved_block_code = 0x01;
956         this->bbt_td->pattern = "MSYS_BBT";
957
958         this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT |
959                                 NAND_BBT_VERSION;
960         if (inftl_bbt_write)
961                 this->bbt_md->options |= NAND_BBT_WRITE;
962         this->bbt_md->offs = 8;
963         this->bbt_md->len = 8;
964         this->bbt_md->veroffs = 7;
965         this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
966         this->bbt_md->reserved_block_code = 0x01;
967         this->bbt_md->pattern = "TBB_SYSM";
968
969         /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
970            At least as nand_bbt.c is currently written. */
971         if ((ret = nand_scan_bbt(mtd, NULL)))
972                 return ret;
973         memset((char *) parts, 0, sizeof(parts));
974         numparts = inftl_partscan(mtd, parts);
975         /* At least for now, require the INFTL Media Header.  We could probably
976            do without it for non-INFTL use, since all it gives us is
977            autopartitioning, but I want to give it more thought. */
978         if (!numparts) return -EIO;
979         add_mtd_device(mtd);
980 #if defined(CONFIG_MTD_PARTITIONS) || defined(CONFIG_MTD_PARTITIONS_MODULE)
981         if (!no_autopart) add_mtd_partitions(mtd, parts, numparts);
982 #endif
983         return 0;
984 }
985
986 static inline int __init doc2000_init(struct mtd_info *mtd)
987 {
988         struct nand_chip *this = mtd->priv;
989         struct doc_priv *doc = (void *)this->priv;
990
991         this->write_byte = doc2000_write_byte;
992         this->read_byte = doc2000_read_byte;
993         this->write_buf = doc2000_writebuf;
994         this->read_buf = doc2000_readbuf;
995         this->verify_buf = doc2000_verifybuf;
996         this->scan_bbt = nftl_scan_bbt;
997
998         doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
999         doc2000_count_chips(mtd);
1000         mtd->name = "DiskOnChip 2000 (NFTL Model)";
1001         return (4 * doc->chips_per_floor);
1002 }
1003
1004 static inline int __init doc2001_init(struct mtd_info *mtd)
1005 {
1006         struct nand_chip *this = mtd->priv;
1007         struct doc_priv *doc = (void *)this->priv;
1008
1009         this->write_byte = doc2001_write_byte;
1010         this->read_byte = doc2001_read_byte;
1011         this->write_buf = doc2001_writebuf;
1012         this->read_buf = doc2001_readbuf;
1013         this->verify_buf = doc2001_verifybuf;
1014         this->scan_bbt = inftl_scan_bbt;
1015
1016         ReadDOC(doc->virtadr, ChipID);
1017         ReadDOC(doc->virtadr, ChipID);
1018         ReadDOC(doc->virtadr, ChipID);
1019         if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) {
1020                 /* It's not a Millennium; it's one of the newer
1021                    DiskOnChip 2000 units with a similar ASIC. 
1022                    Treat it like a Millennium, except that it
1023                    can have multiple chips. */
1024                 doc2000_count_chips(mtd);
1025                 mtd->name = "DiskOnChip 2000 (INFTL Model)";
1026                 return (4 * doc->chips_per_floor);
1027         } else {
1028                 /* Bog-standard Millennium */
1029                 doc->chips_per_floor = 1;
1030                 mtd->name = "DiskOnChip Millennium";
1031                 return 1;
1032         }
1033 }
1034
1035 static inline int __init doc_probe(unsigned long physadr)
1036 {
1037         unsigned char ChipID;
1038         struct mtd_info *mtd;
1039         struct nand_chip *nand;
1040         struct doc_priv *doc;
1041         unsigned long virtadr;
1042         unsigned char save_control;
1043         unsigned char tmp, tmpb, tmpc;
1044         int reg, len, numchips;
1045         int ret = 0;
1046
1047         virtadr = (unsigned long)ioremap(physadr, DOC_IOREMAP_LEN);
1048         if (!virtadr) {
1049                 printk(KERN_ERR "Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n", DOC_IOREMAP_LEN, physadr);
1050                 return -EIO;
1051         }
1052
1053         /* It's not possible to cleanly detect the DiskOnChip - the
1054          * bootup procedure will put the device into reset mode, and
1055          * it's not possible to talk to it without actually writing
1056          * to the DOCControl register. So we store the current contents
1057          * of the DOCControl register's location, in case we later decide
1058          * that it's not a DiskOnChip, and want to put it back how we
1059          * found it. 
1060          */
1061         save_control = ReadDOC(virtadr, DOCControl);
1062
1063         /* Reset the DiskOnChip ASIC */
1064         WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, 
1065                  virtadr, DOCControl);
1066         WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, 
1067                  virtadr, DOCControl);
1068
1069         /* Enable the DiskOnChip ASIC */
1070         WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, 
1071                  virtadr, DOCControl);
1072         WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, 
1073                  virtadr, DOCControl);
1074
1075         ChipID = ReadDOC(virtadr, ChipID);
1076
1077         switch(ChipID) {
1078         case DOC_ChipID_Doc2k:
1079                 reg = DoC_2k_ECCStatus;
1080                 break;
1081         case DOC_ChipID_DocMil:
1082                 reg = DoC_ECCConf;
1083                 break;
1084         default:
1085                 ret = -ENODEV;
1086                 goto notfound;
1087         }
1088         /* Check the TOGGLE bit in the ECC register */
1089         tmp  = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1090         tmpb = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1091         tmpc = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1092         if ((tmp == tmpb) || (tmp != tmpc)) {
1093                 printk(KERN_WARNING "Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr);
1094                 ret = -ENODEV;
1095                 goto notfound;
1096         }
1097
1098         for (mtd = doclist; mtd; mtd = doc->nextdoc) {
1099                 nand = mtd->priv;
1100                 doc = (void *)nand->priv;
1101                 /* Use the alias resolution register to determine if this is
1102                    in fact the same DOC aliased to a new address.  If writes
1103                    to one chip's alias resolution register change the value on
1104                    the other chip, they're the same chip. */
1105                 unsigned char oldval = ReadDOC(doc->virtadr, AliasResolution);
1106                 unsigned char newval = ReadDOC(virtadr, AliasResolution);
1107                 if (oldval != newval)
1108                         continue;
1109                 WriteDOC(~newval, virtadr, AliasResolution);
1110                 oldval = ReadDOC(doc->virtadr, AliasResolution);
1111                 WriteDOC(newval, virtadr, AliasResolution); // restore it
1112                 newval = ~newval;
1113                 if (oldval == newval) {
1114                         //printk(KERN_DEBUG "Found alias of DOC at 0x%lx to 0x%lx\n", doc->physadr, physadr);
1115                         goto notfound;
1116                 }
1117         }
1118
1119         printk(KERN_NOTICE "DiskOnChip found at 0x%lx\n", physadr);
1120
1121         len = sizeof(struct mtd_info) +
1122               sizeof(struct nand_chip) +
1123               sizeof(struct doc_priv) +
1124               (2 * sizeof(struct nand_bbt_descr));
1125         mtd = kmalloc(len, GFP_KERNEL);
1126         if (!mtd) {
1127                 printk(KERN_ERR "DiskOnChip kmalloc (%d bytes) failed!\n", len);
1128                 ret = -ENOMEM;
1129                 goto fail;
1130         }
1131         memset(mtd, 0, len);
1132
1133         nand                    = (struct nand_chip *) (mtd + 1);
1134         doc                     = (struct doc_priv *) (nand + 1);
1135         nand->bbt_td            = (struct nand_bbt_descr *) (doc + 1);
1136         nand->bbt_md            = nand->bbt_td + 1;
1137
1138         mtd->priv               = (void *) nand;
1139         mtd->owner              = THIS_MODULE;
1140
1141         nand->priv              = (void *) doc;
1142         nand->select_chip       = doc200x_select_chip;
1143         nand->hwcontrol         = doc200x_hwcontrol;
1144         nand->dev_ready         = doc200x_dev_ready;
1145         nand->waitfunc          = doc200x_wait;
1146         nand->block_bad         = doc200x_block_bad;
1147         nand->enable_hwecc      = doc200x_enable_hwecc;
1148         nand->calculate_ecc     = doc200x_calculate_ecc;
1149         nand->correct_data      = doc200x_correct_data;
1150         //nand->data_buf
1151         nand->autooob           = &doc200x_oobinfo;
1152         nand->eccmode           = NAND_ECC_HW6_512;
1153         nand->options           = NAND_USE_FLASH_BBT | NAND_HWECC_SYNDROME;
1154
1155         doc->physadr            = physadr;
1156         doc->virtadr            = virtadr;
1157         doc->ChipID             = ChipID;
1158         doc->curfloor           = -1;
1159         doc->curchip            = -1;
1160         doc->mh0_page           = -1;
1161         doc->mh1_page           = -1;
1162         doc->nextdoc            = doclist;
1163
1164         if (ChipID == DOC_ChipID_Doc2k)
1165                 numchips = doc2000_init(mtd);
1166         else
1167                 numchips = doc2001_init(mtd);
1168
1169         if ((ret = nand_scan(mtd, numchips))) {
1170                 /* DBB note: i believe nand_release is necessary here, as
1171                    buffers may have been allocated in nand_base.  Check with
1172                    Thomas. FIX ME! */
1173                 /* nand_release will call del_mtd_device, but we haven't yet
1174                    added it.  This is handled without incident by
1175                    del_mtd_device, as far as I can tell. */
1176                 nand_release(mtd);
1177                 kfree(mtd);
1178                 goto fail;
1179         }
1180
1181         /* Success! */
1182         doclist = mtd;
1183         return 0;
1184
1185 notfound:
1186         /* Put back the contents of the DOCControl register, in case it's not
1187            actually a DiskOnChip.  */
1188         WriteDOC(save_control, virtadr, DOCControl);
1189 fail:
1190         iounmap((void *)virtadr);
1191         return ret;
1192 }
1193
1194 int __init init_nanddoc(void)
1195 {
1196         int i;
1197
1198         if (doc_config_location) {
1199                 printk(KERN_INFO "Using configured DiskOnChip probe address 0x%lx\n", doc_config_location);
1200                 return doc_probe(doc_config_location);
1201         } else {
1202                 for (i=0; (doc_locations[i] != 0xffffffff); i++) {
1203                         doc_probe(doc_locations[i]);
1204                 }
1205         }
1206         /* No banner message any more. Print a message if no DiskOnChip
1207            found, so the user knows we at least tried. */
1208         if (!doclist) {
1209                 printk(KERN_INFO "No valid DiskOnChip devices found\n");
1210                 return -ENODEV;
1211         }
1212         return 0;
1213 }
1214
1215 void __exit cleanup_nanddoc(void)
1216 {
1217         struct mtd_info *mtd, *nextmtd;
1218         struct nand_chip *nand;
1219         struct doc_priv *doc;
1220
1221         for (mtd = doclist; mtd; mtd = nextmtd) {
1222                 nand = mtd->priv;
1223                 doc = (void *)nand->priv;
1224
1225                 nextmtd = doc->nextdoc;
1226                 nand_release(mtd);
1227                 iounmap((void *)doc->virtadr);
1228                 kfree(mtd);
1229         }
1230 }
1231         
1232 module_init(init_nanddoc);
1233 module_exit(cleanup_nanddoc);
1234
1235 MODULE_LICENSE("GPL");
1236 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1237 MODULE_DESCRIPTION("M-Systems DiskOnChip 2000 and Millennium device driver\n");