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