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