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