3 * Linux driver for Disk-On-Chip 2000 and Millennium
4 * (c) 1999 Machine Vision Holdings, Inc.
5 * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
7 * $Id: doc2000.c,v 1.67 2005/11/07 11:14:24 gleixner Exp $
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <asm/errno.h>
14 #include <asm/uaccess.h>
15 #include <linux/miscdevice.h>
16 #include <linux/pci.h>
17 #include <linux/delay.h>
18 #include <linux/slab.h>
19 #include <linux/sched.h>
20 #include <linux/init.h>
21 #include <linux/types.h>
22 #include <linux/bitops.h>
23 #include <linux/mutex.h>
25 #include <linux/mtd/mtd.h>
26 #include <linux/mtd/nand.h>
27 #include <linux/mtd/doc2000.h>
29 #define DOC_SUPPORT_2000
30 #define DOC_SUPPORT_2000TSOP
31 #define DOC_SUPPORT_MILLENNIUM
33 #ifdef DOC_SUPPORT_2000
34 #define DoC_is_2000(doc) (doc->ChipID == DOC_ChipID_Doc2k)
36 #define DoC_is_2000(doc) (0)
39 #if defined(DOC_SUPPORT_2000TSOP) || defined(DOC_SUPPORT_MILLENNIUM)
40 #define DoC_is_Millennium(doc) (doc->ChipID == DOC_ChipID_DocMil)
42 #define DoC_is_Millennium(doc) (0)
45 /* #define ECC_DEBUG */
47 /* I have no idea why some DoC chips can not use memcpy_from|to_io().
48 * This may be due to the different revisions of the ASIC controller built-in or
49 * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
54 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
55 size_t *retlen, u_char *buf);
56 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
57 size_t *retlen, const u_char *buf);
58 static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
59 size_t *retlen, u_char *buf, u_char *eccbuf, struct nand_oobinfo *oobsel);
60 static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
61 size_t *retlen, const u_char *buf, u_char *eccbuf, struct nand_oobinfo *oobsel);
62 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
63 struct mtd_oob_ops *ops);
64 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
65 struct mtd_oob_ops *ops);
66 static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
67 size_t *retlen, const u_char *buf);
68 static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
70 static struct mtd_info *doc2klist = NULL;
72 /* Perform the required delay cycles by reading from the appropriate register */
73 static void DoC_Delay(struct DiskOnChip *doc, unsigned short cycles)
78 for (i = 0; i < cycles; i++) {
79 if (DoC_is_Millennium(doc))
80 dummy = ReadDOC(doc->virtadr, NOP);
82 dummy = ReadDOC(doc->virtadr, DOCStatus);
87 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
88 static int _DoC_WaitReady(struct DiskOnChip *doc)
90 void __iomem *docptr = doc->virtadr;
91 unsigned long timeo = jiffies + (HZ * 10);
93 DEBUG(MTD_DEBUG_LEVEL3,
94 "_DoC_WaitReady called for out-of-line wait\n");
96 /* Out-of-line routine to wait for chip response */
97 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
98 /* issue 2 read from NOP register after reading from CDSNControl register
99 see Software Requirement 11.4 item 2. */
102 if (time_after(jiffies, timeo)) {
103 DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
113 static inline int DoC_WaitReady(struct DiskOnChip *doc)
115 void __iomem *docptr = doc->virtadr;
117 /* This is inline, to optimise the common case, where it's ready instantly */
120 /* 4 read form NOP register should be issued in prior to the read from CDSNControl
121 see Software Requirement 11.4 item 2. */
124 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
125 /* Call the out-of-line routine to wait */
126 ret = _DoC_WaitReady(doc);
128 /* issue 2 read from NOP register after reading from CDSNControl register
129 see Software Requirement 11.4 item 2. */
135 /* DoC_Command: Send a flash command to the flash chip through the CDSN Slow IO register to
136 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
137 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
139 static int DoC_Command(struct DiskOnChip *doc, unsigned char command,
140 unsigned char xtraflags)
142 void __iomem *docptr = doc->virtadr;
144 if (DoC_is_2000(doc))
145 xtraflags |= CDSN_CTRL_FLASH_IO;
147 /* Assert the CLE (Command Latch Enable) line to the flash chip */
148 WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
149 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
151 if (DoC_is_Millennium(doc))
152 WriteDOC(command, docptr, CDSNSlowIO);
154 /* Send the command */
155 WriteDOC_(command, docptr, doc->ioreg);
156 if (DoC_is_Millennium(doc))
157 WriteDOC(command, docptr, WritePipeTerm);
159 /* Lower the CLE line */
160 WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
161 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
163 /* Wait for the chip to respond - Software requirement 11.4.1 (extended for any command) */
164 return DoC_WaitReady(doc);
167 /* DoC_Address: Set the current address for the flash chip through the CDSN Slow IO register to
168 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
169 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
171 static int DoC_Address(struct DiskOnChip *doc, int numbytes, unsigned long ofs,
172 unsigned char xtraflags1, unsigned char xtraflags2)
175 void __iomem *docptr = doc->virtadr;
177 if (DoC_is_2000(doc))
178 xtraflags1 |= CDSN_CTRL_FLASH_IO;
180 /* Assert the ALE (Address Latch Enable) line to the flash chip */
181 WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
183 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
185 /* Send the address */
186 /* Devices with 256-byte page are addressed as:
187 Column (bits 0-7), Page (bits 8-15, 16-23, 24-31)
188 * there is no device on the market with page256
189 and more than 24 bits.
190 Devices with 512-byte page are addressed as:
191 Column (bits 0-7), Page (bits 9-16, 17-24, 25-31)
192 * 25-31 is sent only if the chip support it.
193 * bit 8 changes the read command to be sent
194 (NAND_CMD_READ0 or NAND_CMD_READ1).
197 if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE) {
198 if (DoC_is_Millennium(doc))
199 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
200 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
209 if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE) {
210 for (i = 0; i < doc->pageadrlen; i++, ofs = ofs >> 8) {
211 if (DoC_is_Millennium(doc))
212 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
213 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
217 if (DoC_is_Millennium(doc))
218 WriteDOC(ofs & 0xff, docptr, WritePipeTerm);
220 DoC_Delay(doc, 2); /* Needed for some slow flash chips. mf. */
222 /* FIXME: The SlowIO's for millennium could be replaced by
223 a single WritePipeTerm here. mf. */
225 /* Lower the ALE line */
226 WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr,
229 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
231 /* Wait for the chip to respond - Software requirement 11.4.1 */
232 return DoC_WaitReady(doc);
235 /* Read a buffer from DoC, taking care of Millennium odditys */
236 static void DoC_ReadBuf(struct DiskOnChip *doc, u_char * buf, int len)
239 int modulus = 0xffff;
240 void __iomem *docptr = doc->virtadr;
246 if (DoC_is_Millennium(doc)) {
247 /* Read the data via the internal pipeline through CDSN IO register,
248 see Pipelined Read Operations 11.3 */
249 dummy = ReadDOC(docptr, ReadPipeInit);
251 /* Millennium should use the LastDataRead register - Pipeline Reads */
254 /* This is needed for correctly ECC calculation */
258 for (i = 0; i < len; i++)
259 buf[i] = ReadDOC_(docptr, doc->ioreg + (i & modulus));
261 if (DoC_is_Millennium(doc)) {
262 buf[i] = ReadDOC(docptr, LastDataRead);
266 /* Write a buffer to DoC, taking care of Millennium odditys */
267 static void DoC_WriteBuf(struct DiskOnChip *doc, const u_char * buf, int len)
269 void __iomem *docptr = doc->virtadr;
275 for (i = 0; i < len; i++)
276 WriteDOC_(buf[i], docptr, doc->ioreg + i);
278 if (DoC_is_Millennium(doc)) {
279 WriteDOC(0x00, docptr, WritePipeTerm);
284 /* DoC_SelectChip: Select a given flash chip within the current floor */
286 static inline int DoC_SelectChip(struct DiskOnChip *doc, int chip)
288 void __iomem *docptr = doc->virtadr;
290 /* Software requirement 11.4.4 before writing DeviceSelect */
291 /* Deassert the CE line to eliminate glitches on the FCE# outputs */
292 WriteDOC(CDSN_CTRL_WP, docptr, CDSNControl);
293 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
295 /* Select the individual flash chip requested */
296 WriteDOC(chip, docptr, CDSNDeviceSelect);
299 /* Reassert the CE line */
300 WriteDOC(CDSN_CTRL_CE | CDSN_CTRL_FLASH_IO | CDSN_CTRL_WP, docptr,
302 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
304 /* Wait for it to be ready */
305 return DoC_WaitReady(doc);
308 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
310 static inline int DoC_SelectFloor(struct DiskOnChip *doc, int floor)
312 void __iomem *docptr = doc->virtadr;
314 /* Select the floor (bank) of chips required */
315 WriteDOC(floor, docptr, FloorSelect);
317 /* Wait for the chip to be ready */
318 return DoC_WaitReady(doc);
321 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
323 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
328 /* Page in the required floor/chip */
329 DoC_SelectFloor(doc, floor);
330 DoC_SelectChip(doc, chip);
333 if (DoC_Command(doc, NAND_CMD_RESET, CDSN_CTRL_WP)) {
334 DEBUG(MTD_DEBUG_LEVEL2,
335 "DoC_Command (reset) for %d,%d returned true\n",
341 /* Read the NAND chip ID: 1. Send ReadID command */
342 if (DoC_Command(doc, NAND_CMD_READID, CDSN_CTRL_WP)) {
343 DEBUG(MTD_DEBUG_LEVEL2,
344 "DoC_Command (ReadID) for %d,%d returned true\n",
349 /* Read the NAND chip ID: 2. Send address byte zero */
350 DoC_Address(doc, ADDR_COLUMN, 0, CDSN_CTRL_WP, 0);
352 /* Read the manufacturer and device id codes from the device */
354 if (DoC_is_Millennium(doc)) {
356 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
357 mfr = ReadDOC(doc->virtadr, LastDataRead);
360 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
361 id = ReadDOC(doc->virtadr, LastDataRead);
363 /* CDSN Slow IO register see Software Req 11.4 item 5. */
364 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
366 mfr = ReadDOC_(doc->virtadr, doc->ioreg);
368 /* CDSN Slow IO register see Software Req 11.4 item 5. */
369 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
371 id = ReadDOC_(doc->virtadr, doc->ioreg);
374 /* No response - return failure */
375 if (mfr == 0xff || mfr == 0)
378 /* Check it's the same as the first chip we identified.
379 * M-Systems say that any given DiskOnChip device should only
380 * contain _one_ type of flash part, although that's not a
381 * hardware restriction. */
383 if (doc->mfr == mfr && doc->id == id)
384 return 1; /* This is another the same the first */
387 "Flash chip at floor %d, chip %d is different:\n",
391 /* Print and store the manufacturer and ID codes. */
392 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
393 if (id == nand_flash_ids[i].id) {
394 /* Try to identify manufacturer */
395 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
396 if (nand_manuf_ids[j].id == mfr)
400 "Flash chip found: Manufacturer ID: %2.2X, "
401 "Chip ID: %2.2X (%s:%s)\n", mfr, id,
402 nand_manuf_ids[j].name, nand_flash_ids[i].name);
407 ffs((nand_flash_ids[i].chipsize << 20)) - 1;
408 doc->page256 = (nand_flash_ids[i].pagesize == 256) ? 1 : 0;
409 doc->pageadrlen = doc->chipshift > 25 ? 3 : 2;
411 nand_flash_ids[i].erasesize;
419 /* We haven't fully identified the chip. Print as much as we know. */
420 printk(KERN_WARNING "Unknown flash chip found: %2.2X %2.2X\n",
423 printk(KERN_WARNING "Please report to dwmw2@infradead.org\n");
427 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
429 static void DoC_ScanChips(struct DiskOnChip *this, int maxchips)
432 int numchips[MAX_FLOORS];
439 /* For each floor, find the number of valid chips it contains */
440 for (floor = 0; floor < MAX_FLOORS; floor++) {
443 for (chip = 0; chip < maxchips && ret != 0; chip++) {
445 ret = DoC_IdentChip(this, floor, chip);
453 /* If there are none at all that we recognise, bail */
454 if (!this->numchips) {
455 printk(KERN_NOTICE "No flash chips recognised.\n");
459 /* Allocate an array to hold the information for each chip */
460 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
462 printk(KERN_NOTICE "No memory for allocating chip info structures\n");
468 /* Fill out the chip array with {floor, chipno} for each
469 * detected chip in the device. */
470 for (floor = 0; floor < MAX_FLOORS; floor++) {
471 for (chip = 0; chip < numchips[floor]; chip++) {
472 this->chips[ret].floor = floor;
473 this->chips[ret].chip = chip;
474 this->chips[ret].curadr = 0;
475 this->chips[ret].curmode = 0x50;
480 /* Calculate and print the total size of the device */
481 this->totlen = this->numchips * (1 << this->chipshift);
483 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
484 this->numchips, this->totlen >> 20);
487 static int DoC2k_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
489 int tmp1, tmp2, retval;
490 if (doc1->physadr == doc2->physadr)
493 /* Use the alias resolution register which was set aside for this
494 * purpose. If it's value is the same on both chips, they might
495 * be the same chip, and we write to one and check for a change in
496 * the other. It's unclear if this register is usuable in the
497 * DoC 2000 (it's in the Millennium docs), but it seems to work. */
498 tmp1 = ReadDOC(doc1->virtadr, AliasResolution);
499 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
503 WriteDOC((tmp1 + 1) % 0xff, doc1->virtadr, AliasResolution);
504 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
505 if (tmp2 == (tmp1 + 1) % 0xff)
510 /* Restore register contents. May not be necessary, but do it just to
512 WriteDOC(tmp1, doc1->virtadr, AliasResolution);
517 /* This routine is found from the docprobe code by symbol_get(),
518 * which will bump the use count of this module. */
519 void DoC2k_init(struct mtd_info *mtd)
521 struct DiskOnChip *this = mtd->priv;
522 struct DiskOnChip *old = NULL;
525 /* We must avoid being called twice for the same device. */
528 old = doc2klist->priv;
531 if (DoC2k_is_alias(old, this)) {
533 "Ignoring DiskOnChip 2000 at 0x%lX - already configured\n",
535 iounmap(this->virtadr);
540 old = old->nextdoc->priv;
546 switch (this->ChipID) {
547 case DOC_ChipID_Doc2kTSOP:
548 mtd->name = "DiskOnChip 2000 TSOP";
549 this->ioreg = DoC_Mil_CDSN_IO;
550 /* Pretend it's a Millennium */
551 this->ChipID = DOC_ChipID_DocMil;
552 maxchips = MAX_CHIPS;
554 case DOC_ChipID_Doc2k:
555 mtd->name = "DiskOnChip 2000";
556 this->ioreg = DoC_2k_CDSN_IO;
557 maxchips = MAX_CHIPS;
559 case DOC_ChipID_DocMil:
560 mtd->name = "DiskOnChip Millennium";
561 this->ioreg = DoC_Mil_CDSN_IO;
562 maxchips = MAX_CHIPS_MIL;
565 printk("Unknown ChipID 0x%02x\n", this->ChipID);
567 iounmap(this->virtadr);
571 printk(KERN_NOTICE "%s found at address 0x%lX\n", mtd->name,
574 mtd->type = MTD_NANDFLASH;
575 mtd->flags = MTD_CAP_NANDFLASH;
576 mtd->ecctype = MTD_ECC_RS_DiskOnChip;
579 mtd->writesize = 512;
581 mtd->owner = THIS_MODULE;
582 mtd->erase = doc_erase;
585 mtd->read = doc_read;
586 mtd->write = doc_write;
587 mtd->read_oob = doc_read_oob;
588 mtd->write_oob = doc_write_oob;
596 mutex_init(&this->lock);
598 /* Ident all the chips present. */
599 DoC_ScanChips(this, maxchips);
603 iounmap(this->virtadr);
605 this->nextdoc = doc2klist;
607 mtd->size = this->totlen;
608 mtd->erasesize = this->erasesize;
613 EXPORT_SYMBOL_GPL(DoC2k_init);
615 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
616 size_t * retlen, u_char * buf)
618 /* Just a special case of doc_read_ecc */
619 return doc_read_ecc(mtd, from, len, retlen, buf, NULL, NULL);
622 static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
623 size_t * retlen, u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel)
625 struct DiskOnChip *this = mtd->priv;
626 void __iomem *docptr = this->virtadr;
628 unsigned char syndrome[6];
630 int i, len256 = 0, ret=0;
633 /* Don't allow read past end of device */
634 if (from >= this->totlen)
637 mutex_lock(&this->lock);
643 /* Don't allow a single read to cross a 512-byte block boundary */
644 if (from + len > ((from | 0x1ff) + 1))
645 len = ((from | 0x1ff) + 1) - from;
647 /* The ECC will not be calculated correctly if less than 512 is read */
648 if (len != 0x200 && eccbuf)
650 "ECC needs a full sector read (adr: %lx size %lx)\n",
651 (long) from, (long) len);
653 /* printk("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len); */
656 /* Find the chip which is to be used and select it */
657 mychip = &this->chips[from >> (this->chipshift)];
659 if (this->curfloor != mychip->floor) {
660 DoC_SelectFloor(this, mychip->floor);
661 DoC_SelectChip(this, mychip->chip);
662 } else if (this->curchip != mychip->chip) {
663 DoC_SelectChip(this, mychip->chip);
666 this->curfloor = mychip->floor;
667 this->curchip = mychip->chip;
671 && (from & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
673 DoC_Address(this, ADDR_COLUMN_PAGE, from, CDSN_CTRL_WP,
677 /* Prime the ECC engine */
678 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
679 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
681 /* disable the ECC engine */
682 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
683 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
686 /* treat crossing 256-byte sector for 2M x 8bits devices */
687 if (this->page256 && from + len > (from | 0xff) + 1) {
688 len256 = (from | 0xff) + 1 - from;
689 DoC_ReadBuf(this, buf, len256);
691 DoC_Command(this, NAND_CMD_READ0, CDSN_CTRL_WP);
692 DoC_Address(this, ADDR_COLUMN_PAGE, from + len256,
693 CDSN_CTRL_WP, CDSN_CTRL_ECC_IO);
696 DoC_ReadBuf(this, &buf[len256], len - len256);
698 /* Let the caller know we completed it */
702 /* Read the ECC data through the DiskOnChip ECC logic */
703 /* Note: this will work even with 2M x 8bit devices as */
704 /* they have 8 bytes of OOB per 256 page. mf. */
705 DoC_ReadBuf(this, eccbuf, 6);
707 /* Flush the pipeline */
708 if (DoC_is_Millennium(this)) {
709 dummy = ReadDOC(docptr, ECCConf);
710 dummy = ReadDOC(docptr, ECCConf);
711 i = ReadDOC(docptr, ECCConf);
713 dummy = ReadDOC(docptr, 2k_ECCStatus);
714 dummy = ReadDOC(docptr, 2k_ECCStatus);
715 i = ReadDOC(docptr, 2k_ECCStatus);
718 /* Check the ECC Status */
721 /* There was an ECC error */
723 printk(KERN_ERR "DiskOnChip ECC Error: Read at %lx\n", (long)from);
725 /* Read the ECC syndrom through the DiskOnChip ECC logic.
726 These syndrome will be all ZERO when there is no error */
727 for (i = 0; i < 6; i++) {
729 ReadDOC(docptr, ECCSyndrome0 + i);
731 nb_errors = doc_decode_ecc(buf, syndrome);
734 printk(KERN_ERR "Errors corrected: %x\n", nb_errors);
737 /* We return error, but have actually done the read. Not that
738 this can be told to user-space, via sys_read(), but at least
739 MTD-aware stuff can know about it by checking *retlen */
745 printk(KERN_DEBUG "ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
746 (long)from, eccbuf[0], eccbuf[1], eccbuf[2],
747 eccbuf[3], eccbuf[4], eccbuf[5]);
750 /* disable the ECC engine */
751 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
754 /* according to 11.4.1, we need to wait for the busy line
755 * drop if we read to the end of the page. */
756 if(0 == ((from + len) & 0x1ff))
766 mutex_unlock(&this->lock);
771 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
772 size_t * retlen, const u_char * buf)
775 return doc_write_ecc(mtd, to, len, retlen, buf, eccbuf, NULL);
778 static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
779 size_t * retlen, const u_char * buf,
780 u_char * eccbuf, struct nand_oobinfo *oobsel)
782 struct DiskOnChip *this = mtd->priv;
783 int di; /* Yes, DI is a hangover from when I was disassembling the binary driver */
784 void __iomem *docptr = this->virtadr;
791 /* Don't allow write past end of device */
792 if (to >= this->totlen)
795 mutex_lock(&this->lock);
801 /* Don't allow a single write to cross a 512-byte block boundary */
802 if (to + len > ((to | 0x1ff) + 1))
803 len = ((to | 0x1ff) + 1) - to;
805 /* The ECC will not be calculated correctly if less than 512 is written */
807 if (len != 0x200 && eccbuf)
809 "ECC needs a full sector write (adr: %lx size %lx)\n",
810 (long) to, (long) len);
813 /* printk("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */
815 /* Find the chip which is to be used and select it */
816 mychip = &this->chips[to >> (this->chipshift)];
818 if (this->curfloor != mychip->floor) {
819 DoC_SelectFloor(this, mychip->floor);
820 DoC_SelectChip(this, mychip->chip);
821 } else if (this->curchip != mychip->chip) {
822 DoC_SelectChip(this, mychip->chip);
825 this->curfloor = mychip->floor;
826 this->curchip = mychip->chip;
828 /* Set device to main plane of flash */
829 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
832 && (to & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
835 DoC_Command(this, NAND_CMD_SEQIN, 0);
836 DoC_Address(this, ADDR_COLUMN_PAGE, to, 0, CDSN_CTRL_ECC_IO);
839 /* Prime the ECC engine */
840 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
841 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
843 /* disable the ECC engine */
844 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
845 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
848 /* treat crossing 256-byte sector for 2M x 8bits devices */
849 if (this->page256 && to + len > (to | 0xff) + 1) {
850 len256 = (to | 0xff) + 1 - to;
851 DoC_WriteBuf(this, buf, len256);
853 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
855 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
856 /* There's an implicit DoC_WaitReady() in DoC_Command */
858 dummy = ReadDOC(docptr, CDSNSlowIO);
861 if (ReadDOC_(docptr, this->ioreg) & 1) {
862 printk(KERN_ERR "Error programming flash\n");
863 /* Error in programming */
865 mutex_unlock(&this->lock);
869 DoC_Command(this, NAND_CMD_SEQIN, 0);
870 DoC_Address(this, ADDR_COLUMN_PAGE, to + len256, 0,
874 DoC_WriteBuf(this, &buf[len256], len - len256);
877 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_CE, docptr,
880 if (DoC_is_Millennium(this)) {
881 WriteDOC(0, docptr, NOP);
882 WriteDOC(0, docptr, NOP);
883 WriteDOC(0, docptr, NOP);
885 WriteDOC_(0, docptr, this->ioreg);
886 WriteDOC_(0, docptr, this->ioreg);
887 WriteDOC_(0, docptr, this->ioreg);
890 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_FLASH_IO | CDSN_CTRL_CE, docptr,
893 /* Read the ECC data through the DiskOnChip ECC logic */
894 for (di = 0; di < 6; di++) {
895 eccbuf[di] = ReadDOC(docptr, ECCSyndrome0 + di);
898 /* Reset the ECC engine */
899 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
903 ("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
904 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
905 eccbuf[4], eccbuf[5]);
909 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
911 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
912 /* There's an implicit DoC_WaitReady() in DoC_Command */
914 if (DoC_is_Millennium(this)) {
915 ReadDOC(docptr, ReadPipeInit);
916 status = ReadDOC(docptr, LastDataRead);
918 dummy = ReadDOC(docptr, CDSNSlowIO);
920 status = ReadDOC_(docptr, this->ioreg);
924 printk(KERN_ERR "Error programming flash\n");
925 /* Error in programming */
927 mutex_unlock(&this->lock);
931 /* Let the caller know we completed it */
939 /* Write the ECC data to flash */
940 for (di=0; di<6; di++)
946 ret = doc_write_oob_nolock(mtd, to, 8, &dummy, x);
948 mutex_unlock(&this->lock);
958 mutex_unlock(&this->lock);
962 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
963 struct mtd_oob_ops *ops)
965 struct DiskOnChip *this = mtd->priv;
968 uint8_t *buf = ops->oobbuf;
969 size_t len = ops->len;
971 BUG_ON(ops->mode != MTD_OOB_PLACE);
975 mutex_lock(&this->lock);
977 mychip = &this->chips[ofs >> this->chipshift];
979 if (this->curfloor != mychip->floor) {
980 DoC_SelectFloor(this, mychip->floor);
981 DoC_SelectChip(this, mychip->chip);
982 } else if (this->curchip != mychip->chip) {
983 DoC_SelectChip(this, mychip->chip);
985 this->curfloor = mychip->floor;
986 this->curchip = mychip->chip;
988 /* update address for 2M x 8bit devices. OOB starts on the second */
989 /* page to maintain compatibility with doc_read_ecc. */
997 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
998 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, CDSN_CTRL_WP, 0);
1000 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1001 /* Note: datasheet says it should automaticaly wrap to the */
1002 /* next OOB block, but it didn't work here. mf. */
1003 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1004 len256 = (ofs | 0x7) + 1 - ofs;
1005 DoC_ReadBuf(this, buf, len256);
1007 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1008 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff),
1012 DoC_ReadBuf(this, &buf[len256], len - len256);
1015 /* Reading the full OOB data drops us off of the end of the page,
1016 * causing the flash device to go into busy mode, so we need
1017 * to wait until ready 11.4.1 and Toshiba TC58256FT docs */
1019 ret = DoC_WaitReady(this);
1021 mutex_unlock(&this->lock);
1026 static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
1027 size_t * retlen, const u_char * buf)
1029 struct DiskOnChip *this = mtd->priv;
1031 void __iomem *docptr = this->virtadr;
1032 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
1036 // printk("doc_write_oob(%lx, %d): %2.2X %2.2X %2.2X %2.2X ... %2.2X %2.2X .. %2.2X %2.2X\n",(long)ofs, len,
1037 // buf[0], buf[1], buf[2], buf[3], buf[8], buf[9], buf[14],buf[15]);
1039 /* Find the chip which is to be used and select it */
1040 if (this->curfloor != mychip->floor) {
1041 DoC_SelectFloor(this, mychip->floor);
1042 DoC_SelectChip(this, mychip->chip);
1043 } else if (this->curchip != mychip->chip) {
1044 DoC_SelectChip(this, mychip->chip);
1046 this->curfloor = mychip->floor;
1047 this->curchip = mychip->chip;
1049 /* disable the ECC engine */
1050 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
1051 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
1053 /* Reset the chip, see Software Requirement 11.4 item 1. */
1054 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
1056 /* issue the Read2 command to set the pointer to the Spare Data Area. */
1057 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1059 /* update address for 2M x 8bit devices. OOB starts on the second */
1060 /* page to maintain compatibility with doc_read_ecc. */
1061 if (this->page256) {
1068 /* issue the Serial Data In command to initial the Page Program process */
1069 DoC_Command(this, NAND_CMD_SEQIN, 0);
1070 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, 0, 0);
1072 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1073 /* Note: datasheet says it should automaticaly wrap to the */
1074 /* next OOB block, but it didn't work here. mf. */
1075 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1076 len256 = (ofs | 0x7) + 1 - ofs;
1077 DoC_WriteBuf(this, buf, len256);
1079 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1080 DoC_Command(this, NAND_CMD_STATUS, 0);
1081 /* DoC_WaitReady() is implicit in DoC_Command */
1083 if (DoC_is_Millennium(this)) {
1084 ReadDOC(docptr, ReadPipeInit);
1085 status = ReadDOC(docptr, LastDataRead);
1087 dummy = ReadDOC(docptr, CDSNSlowIO);
1089 status = ReadDOC_(docptr, this->ioreg);
1093 printk(KERN_ERR "Error programming oob data\n");
1094 /* There was an error */
1098 DoC_Command(this, NAND_CMD_SEQIN, 0);
1099 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), 0, 0);
1102 DoC_WriteBuf(this, &buf[len256], len - len256);
1104 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1105 DoC_Command(this, NAND_CMD_STATUS, 0);
1106 /* DoC_WaitReady() is implicit in DoC_Command */
1108 if (DoC_is_Millennium(this)) {
1109 ReadDOC(docptr, ReadPipeInit);
1110 status = ReadDOC(docptr, LastDataRead);
1112 dummy = ReadDOC(docptr, CDSNSlowIO);
1114 status = ReadDOC_(docptr, this->ioreg);
1118 printk(KERN_ERR "Error programming oob data\n");
1119 /* There was an error */
1129 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
1130 struct mtd_oob_ops *ops)
1132 struct DiskOnChip *this = mtd->priv;
1135 BUG_ON(ops->mode != MTD_OOB_PLACE);
1137 mutex_lock(&this->lock);
1138 ret = doc_write_oob_nolock(mtd, ofs + ops->ooboffs, ops->len,
1139 &ops->retlen, ops->oobbuf);
1141 mutex_unlock(&this->lock);
1145 static int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
1147 struct DiskOnChip *this = mtd->priv;
1148 __u32 ofs = instr->addr;
1149 __u32 len = instr->len;
1151 void __iomem *docptr = this->virtadr;
1152 struct Nand *mychip;
1155 mutex_lock(&this->lock);
1157 if (ofs & (mtd->erasesize-1) || len & (mtd->erasesize-1)) {
1158 mutex_unlock(&this->lock);
1162 instr->state = MTD_ERASING;
1164 /* FIXME: Do this in the background. Use timers or schedule_task() */
1166 mychip = &this->chips[ofs >> this->chipshift];
1168 if (this->curfloor != mychip->floor) {
1169 DoC_SelectFloor(this, mychip->floor);
1170 DoC_SelectChip(this, mychip->chip);
1171 } else if (this->curchip != mychip->chip) {
1172 DoC_SelectChip(this, mychip->chip);
1174 this->curfloor = mychip->floor;
1175 this->curchip = mychip->chip;
1177 DoC_Command(this, NAND_CMD_ERASE1, 0);
1178 DoC_Address(this, ADDR_PAGE, ofs, 0, 0);
1179 DoC_Command(this, NAND_CMD_ERASE2, 0);
1181 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
1183 if (DoC_is_Millennium(this)) {
1184 ReadDOC(docptr, ReadPipeInit);
1185 status = ReadDOC(docptr, LastDataRead);
1187 dummy = ReadDOC(docptr, CDSNSlowIO);
1189 status = ReadDOC_(docptr, this->ioreg);
1193 printk(KERN_ERR "Error erasing at 0x%x\n", ofs);
1194 /* There was an error */
1195 instr->state = MTD_ERASE_FAILED;
1198 ofs += mtd->erasesize;
1199 len -= mtd->erasesize;
1201 instr->state = MTD_ERASE_DONE;
1204 mtd_erase_callback(instr);
1206 mutex_unlock(&this->lock);
1211 /****************************************************************************
1215 ****************************************************************************/
1217 static void __exit cleanup_doc2000(void)
1219 struct mtd_info *mtd;
1220 struct DiskOnChip *this;
1222 while ((mtd = doc2klist)) {
1224 doc2klist = this->nextdoc;
1226 del_mtd_device(mtd);
1228 iounmap(this->virtadr);
1234 module_exit(cleanup_doc2000);
1236 MODULE_LICENSE("GPL");
1237 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
1238 MODULE_DESCRIPTION("MTD driver for DiskOnChip 2000 and Millennium");