2 * drivers/mtd/nand/diskonchip.c
4 * (C) 2003 Red Hat, Inc.
6 * Author: David Woodhouse <dwmw2@infradead.org>
8 * Interface to generic NAND code for M-Systems DiskOnChip devices
10 * $Id: diskonchip.c,v 1.23 2004/07/13 00:14:35 dbrown Exp $
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/sched.h>
16 #include <linux/delay.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/nand.h>
21 #include <linux/mtd/doc2000.h>
22 #include <linux/mtd/compatmac.h>
23 #include <linux/mtd/partitions.h>
24 #include <linux/mtd/inftl.h>
26 /* Where to look for the devices? */
27 #ifndef CONFIG_MTD_DOCPROBE_ADDRESS
28 #define CONFIG_MTD_DOCPROBE_ADDRESS 0
31 static unsigned long __initdata doc_locations[] = {
32 #if defined (__alpha__) || defined(__i386__) || defined(__x86_64__)
33 #ifdef CONFIG_MTD_DOCPROBE_HIGH
34 0xfffc8000, 0xfffca000, 0xfffcc000, 0xfffce000,
35 0xfffd0000, 0xfffd2000, 0xfffd4000, 0xfffd6000,
36 0xfffd8000, 0xfffda000, 0xfffdc000, 0xfffde000,
37 0xfffe0000, 0xfffe2000, 0xfffe4000, 0xfffe6000,
38 0xfffe8000, 0xfffea000, 0xfffec000, 0xfffee000,
39 #else /* CONFIG_MTD_DOCPROBE_HIGH */
40 0xc8000, 0xca000, 0xcc000, 0xce000,
41 0xd0000, 0xd2000, 0xd4000, 0xd6000,
42 0xd8000, 0xda000, 0xdc000, 0xde000,
43 0xe0000, 0xe2000, 0xe4000, 0xe6000,
44 0xe8000, 0xea000, 0xec000, 0xee000,
45 #endif /* CONFIG_MTD_DOCPROBE_HIGH */
46 #elif defined(__PPC__)
48 #elif defined(CONFIG_MOMENCO_OCELOT)
51 #elif defined(CONFIG_MOMENCO_OCELOT_G) || defined (CONFIG_MOMENCO_OCELOT_C)
54 #warning Unknown architecture for DiskOnChip. No default probe locations defined
58 static struct mtd_info *doclist = NULL;
61 unsigned long virtadr;
62 unsigned long physadr;
65 int chips_per_floor; /* The number of chips detected on each floor */
70 struct mtd_info *nextdoc;
73 /* Max number of eraseblocks to scan (from start of device) for the (I)NFTL
74 MediaHeader. The spec says to just keep going, I think, but that's just
76 #define MAX_MEDIAHEADER_SCAN 8
78 /* This is the syndrome computed by the HW ecc generator upon reading an empty
79 page, one with all 0xff for data and stored ecc code. */
80 static u_char empty_read_syndrome[6] = { 0x26, 0xff, 0x6d, 0x47, 0x73, 0x7a };
81 /* This is the ecc value computed by the HW ecc generator upon writing an empty
82 page, one with all 0xff for data. */
83 static u_char empty_write_ecc[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
85 #define INFTL_BBT_RESERVED_BLOCKS 4
87 #define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
88 #define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
90 static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd);
91 static void doc200x_select_chip(struct mtd_info *mtd, int chip);
94 MODULE_PARM(debug, "i");
96 static int try_dword=1;
97 MODULE_PARM(try_dword, "i");
99 static int no_ecc_failures=0;
100 MODULE_PARM(no_ecc_failures, "i");
102 static int no_autopart=0;
103 MODULE_PARM(no_autopart, "i");
105 #ifdef MTD_NAND_DISKONCHIP_BBTWRITE
106 static int inftl_bbt_write=1;
108 static int inftl_bbt_write=0;
110 MODULE_PARM(inftl_bbt_write, "i");
112 static unsigned long doc_config_location = CONFIG_MTD_DOCPROBE_ADDRESS;
113 MODULE_PARM(doc_config_location, "l");
114 MODULE_PARM_DESC(doc_config_location, "Physical memory address at which to probe for DiskOnChip");
116 static void DoC_Delay(struct doc_priv *doc, unsigned short cycles)
121 for (i = 0; i < cycles; i++) {
122 if (DoC_is_Millennium(doc))
123 dummy = ReadDOC(doc->virtadr, NOP);
125 dummy = ReadDOC(doc->virtadr, DOCStatus);
129 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
130 static int _DoC_WaitReady(struct doc_priv *doc)
132 unsigned long docptr = doc->virtadr;
133 unsigned long timeo = jiffies + (HZ * 10);
135 if(debug) printk("_DoC_WaitReady...\n");
136 /* Out-of-line routine to wait for chip response */
137 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
138 if (time_after(jiffies, timeo)) {
139 printk("_DoC_WaitReady timed out.\n");
149 static inline int DoC_WaitReady(struct doc_priv *doc)
151 unsigned long docptr = doc->virtadr;
156 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
157 /* Call the out-of-line routine to wait */
158 ret = _DoC_WaitReady(doc);
161 if(debug) printk("DoC_WaitReady OK\n");
165 static void doc2000_write_byte(struct mtd_info *mtd, u_char datum)
167 struct nand_chip *this = mtd->priv;
168 struct doc_priv *doc = (void *)this->priv;
169 unsigned long docptr = doc->virtadr;
171 if(debug)printk("write_byte %02x\n", datum);
172 WriteDOC(datum, docptr, CDSNSlowIO);
173 WriteDOC(datum, docptr, 2k_CDSN_IO);
176 static u_char doc2000_read_byte(struct mtd_info *mtd)
178 struct nand_chip *this = mtd->priv;
179 struct doc_priv *doc = (void *)this->priv;
180 unsigned long docptr = doc->virtadr;
183 ReadDOC(docptr, CDSNSlowIO);
185 ret = ReadDOC(docptr, 2k_CDSN_IO);
186 if (debug) printk("read_byte returns %02x\n", ret);
190 static void doc2000_writebuf(struct mtd_info *mtd,
191 const u_char *buf, int len)
193 struct nand_chip *this = mtd->priv;
194 struct doc_priv *doc = (void *)this->priv;
195 unsigned long docptr = doc->virtadr;
197 if (debug)printk("writebuf of %d bytes: ", len);
198 for (i=0; i < len; i++) {
199 WriteDOC_(buf[i], docptr, DoC_2k_CDSN_IO + i);
201 printk("%02x ", buf[i]);
203 if (debug) printk("\n");
206 static void doc2000_readbuf(struct mtd_info *mtd,
207 u_char *buf, int len)
209 struct nand_chip *this = mtd->priv;
210 struct doc_priv *doc = (void *)this->priv;
211 unsigned long docptr = doc->virtadr;
214 if (debug)printk("readbuf of %d bytes: ", len);
216 for (i=0; i < len; i++) {
217 buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
221 static void doc2000_readbuf_dword(struct mtd_info *mtd,
222 u_char *buf, int len)
224 struct nand_chip *this = mtd->priv;
225 struct doc_priv *doc = (void *)this->priv;
226 unsigned long docptr = doc->virtadr;
229 if (debug) printk("readbuf_dword of %d bytes: ", len);
231 if (unlikely((((unsigned long)buf)|len) & 3)) {
232 for (i=0; i < len; i++) {
233 *(uint8_t *)(&buf[i]) = ReadDOC(docptr, 2k_CDSN_IO + i);
236 for (i=0; i < len; i+=4) {
237 *(uint32_t*)(&buf[i]) = readl(docptr + DoC_2k_CDSN_IO + i);
242 static int doc2000_verifybuf(struct mtd_info *mtd,
243 const u_char *buf, int len)
245 struct nand_chip *this = mtd->priv;
246 struct doc_priv *doc = (void *)this->priv;
247 unsigned long docptr = doc->virtadr;
250 for (i=0; i < len; i++)
251 if (buf[i] != ReadDOC(docptr, 2k_CDSN_IO))
256 static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr)
258 struct nand_chip *this = mtd->priv;
259 struct doc_priv *doc = (void *)this->priv;
262 doc200x_select_chip(mtd, nr);
263 doc200x_hwcontrol(mtd, NAND_CTL_SETCLE);
264 this->write_byte(mtd, NAND_CMD_READID);
265 doc200x_hwcontrol(mtd, NAND_CTL_CLRCLE);
266 doc200x_hwcontrol(mtd, NAND_CTL_SETALE);
267 this->write_byte(mtd, 0);
268 doc200x_hwcontrol(mtd, NAND_CTL_CLRALE);
270 ret = this->read_byte(mtd) << 8;
271 ret |= this->read_byte(mtd);
273 if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) {
274 /* First chip probe. See if we get same results by 32-bit access */
279 unsigned long docptr = doc->virtadr;
281 doc200x_hwcontrol(mtd, NAND_CTL_SETCLE);
282 doc2000_write_byte(mtd, NAND_CMD_READID);
283 doc200x_hwcontrol(mtd, NAND_CTL_CLRCLE);
284 doc200x_hwcontrol(mtd, NAND_CTL_SETALE);
285 doc2000_write_byte(mtd, 0);
286 doc200x_hwcontrol(mtd, NAND_CTL_CLRALE);
288 ident.dword = readl(docptr + DoC_2k_CDSN_IO);
289 if (((ident.byte[0] << 8) | ident.byte[1]) == ret) {
290 printk(KERN_INFO "DiskOnChip 2000 responds to DWORD access\n");
291 this->read_buf = &doc2000_readbuf_dword;
298 static void __init doc2000_count_chips(struct mtd_info *mtd)
300 struct nand_chip *this = mtd->priv;
301 struct doc_priv *doc = (void *)this->priv;
305 /* Max 4 chips per floor on DiskOnChip 2000 */
306 doc->chips_per_floor = 4;
308 /* Find out what the first chip is */
309 mfrid = doc200x_ident_chip(mtd, 0);
311 /* Find how many chips in each floor. */
312 for (i = 1; i < 4; i++) {
313 if (doc200x_ident_chip(mtd, i) != mfrid)
316 doc->chips_per_floor = i;
317 printk(KERN_DEBUG "Detected %d chips per floor.\n", i);
320 static int doc200x_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
322 struct doc_priv *doc = (void *)this->priv;
327 this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
329 status = (int)this->read_byte(mtd);
334 static void doc2001_write_byte(struct mtd_info *mtd, u_char datum)
336 struct nand_chip *this = mtd->priv;
337 struct doc_priv *doc = (void *)this->priv;
338 unsigned long docptr = doc->virtadr;
340 WriteDOC(datum, docptr, CDSNSlowIO);
341 WriteDOC(datum, docptr, Mil_CDSN_IO);
342 WriteDOC(datum, docptr, WritePipeTerm);
345 static u_char doc2001_read_byte(struct mtd_info *mtd)
347 struct nand_chip *this = mtd->priv;
348 struct doc_priv *doc = (void *)this->priv;
349 unsigned long docptr = doc->virtadr;
351 //ReadDOC(docptr, CDSNSlowIO);
352 /* 11.4.5 -- delay twice to allow extended length cycle */
354 ReadDOC(docptr, ReadPipeInit);
355 //return ReadDOC(docptr, Mil_CDSN_IO);
356 return ReadDOC(docptr, LastDataRead);
359 static void doc2001_writebuf(struct mtd_info *mtd,
360 const u_char *buf, int len)
362 struct nand_chip *this = mtd->priv;
363 struct doc_priv *doc = (void *)this->priv;
364 unsigned long docptr = doc->virtadr;
367 for (i=0; i < len; i++)
368 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
369 /* Terminate write pipeline */
370 WriteDOC(0x00, docptr, WritePipeTerm);
373 static void doc2001_readbuf(struct mtd_info *mtd,
374 u_char *buf, int len)
376 struct nand_chip *this = mtd->priv;
377 struct doc_priv *doc = (void *)this->priv;
378 unsigned long docptr = doc->virtadr;
381 /* Start read pipeline */
382 ReadDOC(docptr, ReadPipeInit);
384 for (i=0; i < len-1; i++)
385 buf[i] = ReadDOC(docptr, Mil_CDSN_IO);
387 /* Terminate read pipeline */
388 buf[i] = ReadDOC(docptr, LastDataRead);
391 static int doc2001_verifybuf(struct mtd_info *mtd,
392 const u_char *buf, int len)
394 struct nand_chip *this = mtd->priv;
395 struct doc_priv *doc = (void *)this->priv;
396 unsigned long docptr = doc->virtadr;
399 /* Start read pipeline */
400 ReadDOC(docptr, ReadPipeInit);
402 for (i=0; i < len-1; i++)
403 if (buf[i] != ReadDOC(docptr, Mil_CDSN_IO)) {
404 ReadDOC(docptr, LastDataRead);
407 if (buf[i] != ReadDOC(docptr, LastDataRead))
412 static void doc200x_select_chip(struct mtd_info *mtd, int chip)
414 struct nand_chip *this = mtd->priv;
415 struct doc_priv *doc = (void *)this->priv;
416 unsigned long docptr = doc->virtadr;
419 /* 11.4.4 -- deassert CE before changing chip */
420 doc200x_hwcontrol(mtd, NAND_CTL_CLRNCE);
422 if(debug)printk("select chip (%d)\n", chip);
427 floor = chip / doc->chips_per_floor;
428 chip -= (floor * doc->chips_per_floor);
430 WriteDOC(floor, docptr, FloorSelect);
431 WriteDOC(chip, docptr, CDSNDeviceSelect);
433 doc200x_hwcontrol(mtd, NAND_CTL_SETNCE);
436 doc->curfloor = floor;
439 static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd)
441 struct nand_chip *this = mtd->priv;
442 struct doc_priv *doc = (void *)this->priv;
443 unsigned long docptr = doc->virtadr;
446 case NAND_CTL_SETNCE:
447 doc->CDSNControl |= CDSN_CTRL_CE;
449 case NAND_CTL_CLRNCE:
450 doc->CDSNControl &= ~CDSN_CTRL_CE;
452 case NAND_CTL_SETCLE:
453 doc->CDSNControl |= CDSN_CTRL_CLE;
455 case NAND_CTL_CLRCLE:
456 doc->CDSNControl &= ~CDSN_CTRL_CLE;
458 case NAND_CTL_SETALE:
459 doc->CDSNControl |= CDSN_CTRL_ALE;
461 case NAND_CTL_CLRALE:
462 doc->CDSNControl &= ~CDSN_CTRL_ALE;
465 doc->CDSNControl |= CDSN_CTRL_WP;
468 doc->CDSNControl &= ~CDSN_CTRL_WP;
471 if (debug)printk("hwcontrol(%d): %02x\n", cmd, doc->CDSNControl);
472 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
473 /* 11.4.3 -- 4 NOPs after CSDNControl write */
477 static int doc200x_dev_ready(struct mtd_info *mtd)
479 struct nand_chip *this = mtd->priv;
480 struct doc_priv *doc = (void *)this->priv;
481 unsigned long docptr = doc->virtadr;
483 /* 11.4.2 -- must NOP four times before checking FR/B# */
485 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
487 printk("not ready\n");
490 /* 11.4.2 -- Must NOP twice if it's ready */
492 if (debug)printk("was ready\n");
496 static int doc200x_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
498 /* This is our last resort if we couldn't find or create a BBT. Just
499 pretend all blocks are good. */
503 static void doc200x_enable_hwecc(struct mtd_info *mtd, int mode)
505 struct nand_chip *this = mtd->priv;
506 struct doc_priv *doc = (void *)this->priv;
507 unsigned long docptr = doc->virtadr;
509 /* Prime the ECC engine */
512 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
513 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
516 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
517 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
522 /* This code is only called on write */
523 static int doc200x_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
524 unsigned char *ecc_code)
526 struct nand_chip *this = mtd->priv;
527 struct doc_priv *doc = (void *)this->priv;
528 unsigned long docptr = doc->virtadr;
532 /* flush the pipeline */
533 if (DoC_is_2000(doc)) {
534 WriteDOC(doc->CDSNControl & ~CDSN_CTRL_FLASH_IO, docptr, CDSNControl);
535 WriteDOC(0, docptr, 2k_CDSN_IO);
536 WriteDOC(0, docptr, 2k_CDSN_IO);
537 WriteDOC(0, docptr, 2k_CDSN_IO);
538 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
540 WriteDOC(0, docptr, NOP);
541 WriteDOC(0, docptr, NOP);
542 WriteDOC(0, docptr, NOP);
545 for (i = 0; i < 6; i++) {
546 ecc_code[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
547 if (ecc_code[i] != empty_write_ecc[i])
550 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
552 /* If emptymatch=1, we might have an all-0xff data buffer. Check. */
554 /* Note: this somewhat expensive test should not be triggered
555 often. It could be optimized away by examining the data in
556 the writebuf routine, and remembering the result. */
557 for (i = 0; i < 512; i++) {
558 if (dat[i] == 0xff) continue;
563 /* If emptymatch still =1, we do have an all-0xff data buffer.
564 Return all-0xff ecc value instead of the computed one, so
565 it'll look just like a freshly-erased page. */
566 if (emptymatch) memset(ecc_code, 0xff, 6);
571 static int doc200x_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc)
574 struct nand_chip *this = mtd->priv;
575 struct doc_priv *doc = (void *)this->priv;
576 unsigned long docptr = doc->virtadr;
577 volatile u_char dummy;
580 /* flush the pipeline */
581 if (DoC_is_2000(doc)) {
582 dummy = ReadDOC(docptr, 2k_ECCStatus);
583 dummy = ReadDOC(docptr, 2k_ECCStatus);
584 dummy = ReadDOC(docptr, 2k_ECCStatus);
586 dummy = ReadDOC(docptr, ECCConf);
587 dummy = ReadDOC(docptr, ECCConf);
588 dummy = ReadDOC(docptr, ECCConf);
591 /* Error occured ? */
593 for (i = 0; i < 6; i++) {
594 calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
595 if (calc_ecc[i] != empty_read_syndrome[i])
598 /* If emptymatch=1, the read syndrome is consistent with an
599 all-0xff data and stored ecc block. Check the stored ecc. */
601 for (i = 0; i < 6; i++) {
602 if (read_ecc[i] == 0xff) continue;
607 /* If emptymatch still =1, check the data block. */
609 /* Note: this somewhat expensive test should not be triggered
610 often. It could be optimized away by examining the data in
611 the readbuf routine, and remembering the result. */
612 for (i = 0; i < 512; i++) {
613 if (dat[i] == 0xff) continue;
618 /* If emptymatch still =1, this is almost certainly a freshly-
619 erased block, in which case the ECC will not come out right.
620 We'll suppress the error and tell the caller everything's
621 OK. Because it is. */
622 if (!emptymatch) ret = doc_decode_ecc (dat, calc_ecc);
624 printk(KERN_ERR "doc200x_correct_data corrected %d errors\n", ret);
626 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
627 if (no_ecc_failures && (ret == -1)) {
628 printk(KERN_ERR "suppressing ECC failure\n");
634 //u_char mydatabuf[528];
636 static struct nand_oobinfo doc200x_oobinfo = {
637 .useecc = MTD_NANDECC_AUTOPLACE,
639 .eccpos = {0, 1, 2, 3, 4, 5},
640 .oobfree = { {8, 8} }
643 /* Find the (I)NFTL Media Header, and optionally also the mirror media header.
644 On sucessful return, buf will contain a copy of the media header for
645 further processing. id is the string to scan for, and will presumably be
646 either "ANAND" or "BNAND". If findmirror=1, also look for the mirror media
647 header. The page #s of the found media headers are placed in mh0_page and
648 mh1_page in the DOC private structure. */
649 static int __init find_media_headers(struct mtd_info *mtd, u_char *buf,
650 const char *id, int findmirror)
652 struct nand_chip *this = mtd->priv;
653 struct doc_priv *doc = (void *)this->priv;
654 int offs, end = (MAX_MEDIAHEADER_SCAN << this->phys_erase_shift);
657 end = min(end, mtd->size); // paranoia
658 for (offs = 0; offs < end; offs += mtd->erasesize) {
659 ret = mtd->read(mtd, offs, mtd->oobblock, &retlen, buf);
660 if (retlen != mtd->oobblock) continue;
662 printk(KERN_WARNING "ECC error scanning DOC at 0x%x\n",
665 if (memcmp(buf, id, 6)) continue;
666 printk(KERN_INFO "Found DiskOnChip %s Media Header at 0x%x\n", id, offs);
667 if (doc->mh0_page == -1) {
668 doc->mh0_page = offs >> this->page_shift;
669 if (!findmirror) return 1;
672 doc->mh1_page = offs >> this->page_shift;
675 if (doc->mh0_page == -1) {
676 printk(KERN_WARNING "DiskOnChip %s Media Header not found.\n", id);
679 /* Only one mediaheader was found. We want buf to contain a
680 mediaheader on return, so we'll have to re-read the one we found. */
681 offs = doc->mh0_page << this->page_shift;
682 ret = mtd->read(mtd, offs, mtd->oobblock, &retlen, buf);
683 if (retlen != mtd->oobblock) {
684 /* Insanity. Give up. */
685 printk(KERN_ERR "Read DiskOnChip Media Header once, but can't reread it???\n");
691 static inline int __init nftl_partscan(struct mtd_info *mtd,
692 struct mtd_partition *parts)
694 struct nand_chip *this = mtd->priv;
695 struct doc_priv *doc = (void *)this->priv;
696 u_char *buf = this->data_buf;
697 struct NFTLMediaHeader *mh = (struct NFTLMediaHeader *) buf;
698 const int psize = 1 << this->page_shift;
699 int blocks, maxblocks;
700 int offs, numheaders;
702 if (!(numheaders=find_media_headers(mtd, buf, "ANAND", 1))) return 0;
704 //#ifdef CONFIG_MTD_DEBUG_VERBOSE
705 // if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
706 printk(KERN_INFO " DataOrgID = %s\n"
707 " NumEraseUnits = %d\n"
708 " FirstPhysicalEUN = %d\n"
709 " FormattedSize = %d\n"
710 " UnitSizeFactor = %d\n",
711 mh->DataOrgID, mh->NumEraseUnits,
712 mh->FirstPhysicalEUN, mh->FormattedSize,
716 blocks = mtd->size >> this->phys_erase_shift;
717 maxblocks = min(32768, mtd->erasesize - psize);
719 if (mh->UnitSizeFactor == 0x00) {
720 /* Auto-determine UnitSizeFactor. The constraints are:
721 - There can be at most 32768 virtual blocks.
722 - There can be at most (virtual block size - page size)
723 virtual blocks (because MediaHeader+BBT must fit in 1).
725 mh->UnitSizeFactor = 0xff;
726 while (blocks > maxblocks) {
728 maxblocks = min(32768, (maxblocks << 1) + psize);
729 mh->UnitSizeFactor--;
731 printk(KERN_WARNING "UnitSizeFactor=0x00 detected. Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor);
734 /* NOTE: The lines below modify internal variables of the NAND and MTD
735 layers; variables with have already been configured by nand_scan.
736 Unfortunately, we didn't know before this point what these values
737 should be. Thus, this code is somewhat dependant on the exact
738 implementation of the NAND layer. */
739 if (mh->UnitSizeFactor != 0xff) {
740 this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
741 mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
742 printk(KERN_INFO "Setting virtual erase size to %d\n", mtd->erasesize);
743 blocks = mtd->size >> this->bbt_erase_shift;
744 maxblocks = min(32768, mtd->erasesize - psize);
747 if (blocks > maxblocks) {
748 printk(KERN_ERR "UnitSizeFactor of 0x%02x is inconsistent with device size. Aborting.\n", mh->UnitSizeFactor);
752 /* Skip past the media headers. */
753 offs = max(doc->mh0_page, doc->mh1_page);
754 offs <<= this->page_shift;
755 offs += mtd->erasesize;
757 //parts[0].name = " DiskOnChip Boot / Media Header partition";
758 //parts[0].offset = 0;
759 //parts[0].size = offs;
761 parts[0].name = " DiskOnChip BDTL partition";
762 parts[0].offset = offs;
763 parts[0].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift;
765 offs += parts[0].size;
766 if (offs < mtd->size) {
767 parts[1].name = " DiskOnChip Remainder partition";
768 parts[1].offset = offs;
769 parts[1].size = mtd->size - offs;
775 /* This is a stripped-down copy of the code in inftlmount.c */
776 static inline int __init inftl_partscan(struct mtd_info *mtd,
777 struct mtd_partition *parts)
779 struct nand_chip *this = mtd->priv;
780 struct doc_priv *doc = (void *)this->priv;
781 u_char *buf = this->data_buf;
782 struct INFTLMediaHeader *mh = (struct INFTLMediaHeader *) buf;
783 struct INFTLPartition *ip;
786 int vshift, lastvunit = 0;
791 end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift);
793 if (!find_media_headers(mtd, buf, "BNAND", 0)) return 0;
794 doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift);
796 mh->NoOfBootImageBlocks = le32_to_cpu(mh->NoOfBootImageBlocks);
797 mh->NoOfBinaryPartitions = le32_to_cpu(mh->NoOfBinaryPartitions);
798 mh->NoOfBDTLPartitions = le32_to_cpu(mh->NoOfBDTLPartitions);
799 mh->BlockMultiplierBits = le32_to_cpu(mh->BlockMultiplierBits);
800 mh->FormatFlags = le32_to_cpu(mh->FormatFlags);
801 mh->PercentUsed = le32_to_cpu(mh->PercentUsed);
803 //#ifdef CONFIG_MTD_DEBUG_VERBOSE
804 // if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
805 printk(KERN_INFO " bootRecordID = %s\n"
806 " NoOfBootImageBlocks = %d\n"
807 " NoOfBinaryPartitions = %d\n"
808 " NoOfBDTLPartitions = %d\n"
809 " BlockMultiplerBits = %d\n"
811 " OsakVersion = 0x%x\n"
812 " PercentUsed = %d\n",
813 mh->bootRecordID, mh->NoOfBootImageBlocks,
814 mh->NoOfBinaryPartitions,
815 mh->NoOfBDTLPartitions,
816 mh->BlockMultiplierBits, mh->FormatFlags,
817 mh->OsakVersion, mh->PercentUsed);
820 vshift = this->phys_erase_shift + mh->BlockMultiplierBits;
822 blocks = mtd->size >> vshift;
823 if (blocks > 32768) {
824 printk(KERN_ERR "BlockMultiplierBits=%d is inconsistent with device size. Aborting.\n", mh->BlockMultiplierBits);
828 blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift);
829 if (inftl_bbt_write && (blocks > mtd->erasesize)) {
830 printk(KERN_ERR "Writeable BBTs spanning more than one erase block are not yet supported. FIX ME!\n");
834 /* Scan the partitions */
835 for (i = 0; (i < 4); i++) {
836 ip = &(mh->Partitions[i]);
837 ip->virtualUnits = le32_to_cpu(ip->virtualUnits);
838 ip->firstUnit = le32_to_cpu(ip->firstUnit);
839 ip->lastUnit = le32_to_cpu(ip->lastUnit);
840 ip->flags = le32_to_cpu(ip->flags);
841 ip->spareUnits = le32_to_cpu(ip->spareUnits);
842 ip->Reserved0 = le32_to_cpu(ip->Reserved0);
844 //#ifdef CONFIG_MTD_DEBUG_VERBOSE
845 // if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
846 printk(KERN_INFO " PARTITION[%d] ->\n"
847 " virtualUnits = %d\n"
851 " spareUnits = %d\n",
852 i, ip->virtualUnits, ip->firstUnit,
853 ip->lastUnit, ip->flags,
858 if ((i == 0) && (ip->firstUnit > 0)) {
859 parts[0].name = " DiskOnChip IPL / Media Header partition";
861 parts[0].size = mtd->erasesize * ip->firstUnit;
866 if (ip->flags & INFTL_BINARY)
867 parts[numparts].name = " DiskOnChip BDK partition";
869 parts[numparts].name = " DiskOnChip BDTL partition";
870 parts[numparts].offset = ip->firstUnit << vshift;
871 parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift;
873 if (ip->lastUnit > lastvunit) lastvunit = ip->lastUnit;
874 if (ip->flags & INFTL_LAST) break;
877 if ((lastvunit << vshift) < end) {
878 parts[numparts].name = " DiskOnChip Remainder partition";
879 parts[numparts].offset = lastvunit << vshift;
880 parts[numparts].size = end - parts[numparts].offset;
886 static int __init nftl_scan_bbt(struct mtd_info *mtd)
889 struct nand_chip *this = mtd->priv;
890 struct doc_priv *doc = (void *)this->priv;
891 struct mtd_partition parts[2];
893 memset((char *) parts, 0, sizeof(parts));
894 /* On NFTL, we have to find the media headers before we can read the
895 BBTs, since they're stored in the media header eraseblocks. */
896 numparts = nftl_partscan(mtd, parts);
897 if (!numparts) return -EIO;
898 this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
899 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
901 this->bbt_td->veroffs = 7;
902 this->bbt_td->pages[0] = doc->mh0_page + 1;
903 if (doc->mh1_page != -1) {
904 this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
905 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
907 this->bbt_md->veroffs = 7;
908 this->bbt_md->pages[0] = doc->mh1_page + 1;
913 /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
914 At least as nand_bbt.c is currently written. */
915 if ((ret = nand_scan_bbt(mtd, NULL)))
918 #if defined(CONFIG_MTD_PARTITIONS) || defined(CONFIG_MTD_PARTITIONS_MODULE)
919 if (!no_autopart) add_mtd_partitions(mtd, parts, numparts);
924 static int __init inftl_scan_bbt(struct mtd_info *mtd)
927 struct nand_chip *this = mtd->priv;
928 struct doc_priv *doc = (void *)this->priv;
929 struct mtd_partition parts[5];
931 if (this->numchips > doc->chips_per_floor) {
932 printk(KERN_ERR "Multi-floor INFTL devices not yet supported.\n");
936 if (mtd->size == (8<<20)) {
938 /* This doesn't seem to work for me. I get ECC errors on every page. */
939 /* The Millennium 8MiB is actually an NFTL device! */
940 mtd->name = "DiskOnChip Millennium 8MiB (NFTL)";
941 return nftl_scan_bbt(mtd);
943 printk(KERN_ERR "DiskOnChip Millennium 8MiB is not supported.\n");
947 this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT |
950 this->bbt_td->options |= NAND_BBT_WRITE;
951 this->bbt_td->offs = 8;
952 this->bbt_td->len = 8;
953 this->bbt_td->veroffs = 7;
954 this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
955 this->bbt_td->reserved_block_code = 0x01;
956 this->bbt_td->pattern = "MSYS_BBT";
958 this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT |
961 this->bbt_md->options |= NAND_BBT_WRITE;
962 this->bbt_md->offs = 8;
963 this->bbt_md->len = 8;
964 this->bbt_md->veroffs = 7;
965 this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
966 this->bbt_md->reserved_block_code = 0x01;
967 this->bbt_md->pattern = "TBB_SYSM";
969 /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
970 At least as nand_bbt.c is currently written. */
971 if ((ret = nand_scan_bbt(mtd, NULL)))
973 memset((char *) parts, 0, sizeof(parts));
974 numparts = inftl_partscan(mtd, parts);
975 /* At least for now, require the INFTL Media Header. We could probably
976 do without it for non-INFTL use, since all it gives us is
977 autopartitioning, but I want to give it more thought. */
978 if (!numparts) return -EIO;
980 #if defined(CONFIG_MTD_PARTITIONS) || defined(CONFIG_MTD_PARTITIONS_MODULE)
981 if (!no_autopart) add_mtd_partitions(mtd, parts, numparts);
986 static inline int __init doc2000_init(struct mtd_info *mtd)
988 struct nand_chip *this = mtd->priv;
989 struct doc_priv *doc = (void *)this->priv;
991 this->write_byte = doc2000_write_byte;
992 this->read_byte = doc2000_read_byte;
993 this->write_buf = doc2000_writebuf;
994 this->read_buf = doc2000_readbuf;
995 this->verify_buf = doc2000_verifybuf;
996 this->scan_bbt = nftl_scan_bbt;
998 doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
999 doc2000_count_chips(mtd);
1000 mtd->name = "DiskOnChip 2000 (NFTL Model)";
1001 return (4 * doc->chips_per_floor);
1004 static inline int __init doc2001_init(struct mtd_info *mtd)
1006 struct nand_chip *this = mtd->priv;
1007 struct doc_priv *doc = (void *)this->priv;
1009 this->write_byte = doc2001_write_byte;
1010 this->read_byte = doc2001_read_byte;
1011 this->write_buf = doc2001_writebuf;
1012 this->read_buf = doc2001_readbuf;
1013 this->verify_buf = doc2001_verifybuf;
1014 this->scan_bbt = inftl_scan_bbt;
1016 ReadDOC(doc->virtadr, ChipID);
1017 ReadDOC(doc->virtadr, ChipID);
1018 ReadDOC(doc->virtadr, ChipID);
1019 if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) {
1020 /* It's not a Millennium; it's one of the newer
1021 DiskOnChip 2000 units with a similar ASIC.
1022 Treat it like a Millennium, except that it
1023 can have multiple chips. */
1024 doc2000_count_chips(mtd);
1025 mtd->name = "DiskOnChip 2000 (INFTL Model)";
1026 return (4 * doc->chips_per_floor);
1028 /* Bog-standard Millennium */
1029 doc->chips_per_floor = 1;
1030 mtd->name = "DiskOnChip Millennium";
1035 static inline int __init doc_probe(unsigned long physadr)
1037 unsigned char ChipID;
1038 struct mtd_info *mtd;
1039 struct nand_chip *nand;
1040 struct doc_priv *doc;
1041 unsigned long virtadr;
1042 unsigned char save_control;
1043 unsigned char tmp, tmpb, tmpc;
1044 int reg, len, numchips;
1047 virtadr = (unsigned long)ioremap(physadr, DOC_IOREMAP_LEN);
1049 printk(KERN_ERR "Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n", DOC_IOREMAP_LEN, physadr);
1053 /* It's not possible to cleanly detect the DiskOnChip - the
1054 * bootup procedure will put the device into reset mode, and
1055 * it's not possible to talk to it without actually writing
1056 * to the DOCControl register. So we store the current contents
1057 * of the DOCControl register's location, in case we later decide
1058 * that it's not a DiskOnChip, and want to put it back how we
1061 save_control = ReadDOC(virtadr, DOCControl);
1063 /* Reset the DiskOnChip ASIC */
1064 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET,
1065 virtadr, DOCControl);
1066 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET,
1067 virtadr, DOCControl);
1069 /* Enable the DiskOnChip ASIC */
1070 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL,
1071 virtadr, DOCControl);
1072 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL,
1073 virtadr, DOCControl);
1075 ChipID = ReadDOC(virtadr, ChipID);
1078 case DOC_ChipID_Doc2k:
1079 reg = DoC_2k_ECCStatus;
1081 case DOC_ChipID_DocMil:
1088 /* Check the TOGGLE bit in the ECC register */
1089 tmp = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1090 tmpb = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1091 tmpc = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1092 if ((tmp == tmpb) || (tmp != tmpc)) {
1093 printk(KERN_WARNING "Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr);
1098 for (mtd = doclist; mtd; mtd = doc->nextdoc) {
1100 doc = (void *)nand->priv;
1101 /* Use the alias resolution register to determine if this is
1102 in fact the same DOC aliased to a new address. If writes
1103 to one chip's alias resolution register change the value on
1104 the other chip, they're the same chip. */
1105 unsigned char oldval = ReadDOC(doc->virtadr, AliasResolution);
1106 unsigned char newval = ReadDOC(virtadr, AliasResolution);
1107 if (oldval != newval)
1109 WriteDOC(~newval, virtadr, AliasResolution);
1110 oldval = ReadDOC(doc->virtadr, AliasResolution);
1111 WriteDOC(newval, virtadr, AliasResolution); // restore it
1113 if (oldval == newval) {
1114 //printk(KERN_DEBUG "Found alias of DOC at 0x%lx to 0x%lx\n", doc->physadr, physadr);
1119 printk(KERN_NOTICE "DiskOnChip found at 0x%lx\n", physadr);
1121 len = sizeof(struct mtd_info) +
1122 sizeof(struct nand_chip) +
1123 sizeof(struct doc_priv) +
1124 (2 * sizeof(struct nand_bbt_descr));
1125 mtd = kmalloc(len, GFP_KERNEL);
1127 printk(KERN_ERR "DiskOnChip kmalloc (%d bytes) failed!\n", len);
1131 memset(mtd, 0, len);
1133 nand = (struct nand_chip *) (mtd + 1);
1134 doc = (struct doc_priv *) (nand + 1);
1135 nand->bbt_td = (struct nand_bbt_descr *) (doc + 1);
1136 nand->bbt_md = nand->bbt_td + 1;
1138 mtd->priv = (void *) nand;
1139 mtd->owner = THIS_MODULE;
1141 nand->priv = (void *) doc;
1142 nand->select_chip = doc200x_select_chip;
1143 nand->hwcontrol = doc200x_hwcontrol;
1144 nand->dev_ready = doc200x_dev_ready;
1145 nand->waitfunc = doc200x_wait;
1146 nand->block_bad = doc200x_block_bad;
1147 nand->enable_hwecc = doc200x_enable_hwecc;
1148 nand->calculate_ecc = doc200x_calculate_ecc;
1149 nand->correct_data = doc200x_correct_data;
1151 nand->autooob = &doc200x_oobinfo;
1152 nand->eccmode = NAND_ECC_HW6_512;
1153 nand->options = NAND_USE_FLASH_BBT | NAND_HWECC_SYNDROME;
1155 doc->physadr = physadr;
1156 doc->virtadr = virtadr;
1157 doc->ChipID = ChipID;
1162 doc->nextdoc = doclist;
1164 if (ChipID == DOC_ChipID_Doc2k)
1165 numchips = doc2000_init(mtd);
1167 numchips = doc2001_init(mtd);
1169 if ((ret = nand_scan(mtd, numchips))) {
1170 /* DBB note: i believe nand_release is necessary here, as
1171 buffers may have been allocated in nand_base. Check with
1173 /* nand_release will call del_mtd_device, but we haven't yet
1174 added it. This is handled without incident by
1175 del_mtd_device, as far as I can tell. */
1186 /* Put back the contents of the DOCControl register, in case it's not
1187 actually a DiskOnChip. */
1188 WriteDOC(save_control, virtadr, DOCControl);
1190 iounmap((void *)virtadr);
1194 int __init init_nanddoc(void)
1198 if (doc_config_location) {
1199 printk(KERN_INFO "Using configured DiskOnChip probe address 0x%lx\n", doc_config_location);
1200 return doc_probe(doc_config_location);
1202 for (i=0; (doc_locations[i] != 0xffffffff); i++) {
1203 doc_probe(doc_locations[i]);
1206 /* No banner message any more. Print a message if no DiskOnChip
1207 found, so the user knows we at least tried. */
1209 printk(KERN_INFO "No valid DiskOnChip devices found\n");
1215 void __exit cleanup_nanddoc(void)
1217 struct mtd_info *mtd, *nextmtd;
1218 struct nand_chip *nand;
1219 struct doc_priv *doc;
1221 for (mtd = doclist; mtd; mtd = nextmtd) {
1223 doc = (void *)nand->priv;
1225 nextmtd = doc->nextdoc;
1227 iounmap((void *)doc->virtadr);
1232 module_init(init_nanddoc);
1233 module_exit(cleanup_nanddoc);
1235 MODULE_LICENSE("GPL");
1236 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1237 MODULE_DESCRIPTION("M-Systems DiskOnChip 2000 and Millennium device driver\n");