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