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