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