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