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_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs,
63 unsigned long count, loff_t to, size_t *retlen,
64 u_char *eccbuf, struct nand_oobinfo *oobsel);
65 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
66 size_t *retlen, u_char *buf);
67 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
68 size_t *retlen, const u_char *buf);
69 static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
70 size_t *retlen, const u_char *buf);
71 static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
73 static struct mtd_info *doc2klist = NULL;
75 /* Perform the required delay cycles by reading from the appropriate register */
76 static void DoC_Delay(struct DiskOnChip *doc, unsigned short cycles)
81 for (i = 0; i < cycles; i++) {
82 if (DoC_is_Millennium(doc))
83 dummy = ReadDOC(doc->virtadr, NOP);
85 dummy = ReadDOC(doc->virtadr, DOCStatus);
90 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
91 static int _DoC_WaitReady(struct DiskOnChip *doc)
93 void __iomem *docptr = doc->virtadr;
94 unsigned long timeo = jiffies + (HZ * 10);
96 DEBUG(MTD_DEBUG_LEVEL3,
97 "_DoC_WaitReady called for out-of-line wait\n");
99 /* Out-of-line routine to wait for chip response */
100 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
101 /* issue 2 read from NOP register after reading from CDSNControl register
102 see Software Requirement 11.4 item 2. */
105 if (time_after(jiffies, timeo)) {
106 DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
116 static inline int DoC_WaitReady(struct DiskOnChip *doc)
118 void __iomem *docptr = doc->virtadr;
120 /* This is inline, to optimise the common case, where it's ready instantly */
123 /* 4 read form NOP register should be issued in prior to the read from CDSNControl
124 see Software Requirement 11.4 item 2. */
127 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
128 /* Call the out-of-line routine to wait */
129 ret = _DoC_WaitReady(doc);
131 /* issue 2 read from NOP register after reading from CDSNControl register
132 see Software Requirement 11.4 item 2. */
138 /* DoC_Command: Send a flash command to the flash chip through the CDSN Slow IO register to
139 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
140 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
142 static int DoC_Command(struct DiskOnChip *doc, unsigned char command,
143 unsigned char xtraflags)
145 void __iomem *docptr = doc->virtadr;
147 if (DoC_is_2000(doc))
148 xtraflags |= CDSN_CTRL_FLASH_IO;
150 /* Assert the CLE (Command Latch Enable) line to the flash chip */
151 WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
152 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
154 if (DoC_is_Millennium(doc))
155 WriteDOC(command, docptr, CDSNSlowIO);
157 /* Send the command */
158 WriteDOC_(command, docptr, doc->ioreg);
159 if (DoC_is_Millennium(doc))
160 WriteDOC(command, docptr, WritePipeTerm);
162 /* Lower the CLE line */
163 WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
164 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
166 /* Wait for the chip to respond - Software requirement 11.4.1 (extended for any command) */
167 return DoC_WaitReady(doc);
170 /* DoC_Address: Set the current address for the flash chip through the CDSN Slow IO register to
171 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
172 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
174 static int DoC_Address(struct DiskOnChip *doc, int numbytes, unsigned long ofs,
175 unsigned char xtraflags1, unsigned char xtraflags2)
178 void __iomem *docptr = doc->virtadr;
180 if (DoC_is_2000(doc))
181 xtraflags1 |= CDSN_CTRL_FLASH_IO;
183 /* Assert the ALE (Address Latch Enable) line to the flash chip */
184 WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
186 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
188 /* Send the address */
189 /* Devices with 256-byte page are addressed as:
190 Column (bits 0-7), Page (bits 8-15, 16-23, 24-31)
191 * there is no device on the market with page256
192 and more than 24 bits.
193 Devices with 512-byte page are addressed as:
194 Column (bits 0-7), Page (bits 9-16, 17-24, 25-31)
195 * 25-31 is sent only if the chip support it.
196 * bit 8 changes the read command to be sent
197 (NAND_CMD_READ0 or NAND_CMD_READ1).
200 if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE) {
201 if (DoC_is_Millennium(doc))
202 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
203 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
212 if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE) {
213 for (i = 0; i < doc->pageadrlen; i++, ofs = ofs >> 8) {
214 if (DoC_is_Millennium(doc))
215 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
216 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
220 if (DoC_is_Millennium(doc))
221 WriteDOC(ofs & 0xff, docptr, WritePipeTerm);
223 DoC_Delay(doc, 2); /* Needed for some slow flash chips. mf. */
225 /* FIXME: The SlowIO's for millennium could be replaced by
226 a single WritePipeTerm here. mf. */
228 /* Lower the ALE line */
229 WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr,
232 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
234 /* Wait for the chip to respond - Software requirement 11.4.1 */
235 return DoC_WaitReady(doc);
238 /* Read a buffer from DoC, taking care of Millennium odditys */
239 static void DoC_ReadBuf(struct DiskOnChip *doc, u_char * buf, int len)
242 int modulus = 0xffff;
243 void __iomem *docptr = doc->virtadr;
249 if (DoC_is_Millennium(doc)) {
250 /* Read the data via the internal pipeline through CDSN IO register,
251 see Pipelined Read Operations 11.3 */
252 dummy = ReadDOC(docptr, ReadPipeInit);
254 /* Millennium should use the LastDataRead register - Pipeline Reads */
257 /* This is needed for correctly ECC calculation */
261 for (i = 0; i < len; i++)
262 buf[i] = ReadDOC_(docptr, doc->ioreg + (i & modulus));
264 if (DoC_is_Millennium(doc)) {
265 buf[i] = ReadDOC(docptr, LastDataRead);
269 /* Write a buffer to DoC, taking care of Millennium odditys */
270 static void DoC_WriteBuf(struct DiskOnChip *doc, const u_char * buf, int len)
272 void __iomem *docptr = doc->virtadr;
278 for (i = 0; i < len; i++)
279 WriteDOC_(buf[i], docptr, doc->ioreg + i);
281 if (DoC_is_Millennium(doc)) {
282 WriteDOC(0x00, docptr, WritePipeTerm);
287 /* DoC_SelectChip: Select a given flash chip within the current floor */
289 static inline int DoC_SelectChip(struct DiskOnChip *doc, int chip)
291 void __iomem *docptr = doc->virtadr;
293 /* Software requirement 11.4.4 before writing DeviceSelect */
294 /* Deassert the CE line to eliminate glitches on the FCE# outputs */
295 WriteDOC(CDSN_CTRL_WP, docptr, CDSNControl);
296 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
298 /* Select the individual flash chip requested */
299 WriteDOC(chip, docptr, CDSNDeviceSelect);
302 /* Reassert the CE line */
303 WriteDOC(CDSN_CTRL_CE | CDSN_CTRL_FLASH_IO | CDSN_CTRL_WP, docptr,
305 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
307 /* Wait for it to be ready */
308 return DoC_WaitReady(doc);
311 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
313 static inline int DoC_SelectFloor(struct DiskOnChip *doc, int floor)
315 void __iomem *docptr = doc->virtadr;
317 /* Select the floor (bank) of chips required */
318 WriteDOC(floor, docptr, FloorSelect);
320 /* Wait for the chip to be ready */
321 return DoC_WaitReady(doc);
324 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
326 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
331 /* Page in the required floor/chip */
332 DoC_SelectFloor(doc, floor);
333 DoC_SelectChip(doc, chip);
336 if (DoC_Command(doc, NAND_CMD_RESET, CDSN_CTRL_WP)) {
337 DEBUG(MTD_DEBUG_LEVEL2,
338 "DoC_Command (reset) for %d,%d returned true\n",
344 /* Read the NAND chip ID: 1. Send ReadID command */
345 if (DoC_Command(doc, NAND_CMD_READID, CDSN_CTRL_WP)) {
346 DEBUG(MTD_DEBUG_LEVEL2,
347 "DoC_Command (ReadID) for %d,%d returned true\n",
352 /* Read the NAND chip ID: 2. Send address byte zero */
353 DoC_Address(doc, ADDR_COLUMN, 0, CDSN_CTRL_WP, 0);
355 /* Read the manufacturer and device id codes from the device */
357 if (DoC_is_Millennium(doc)) {
359 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
360 mfr = ReadDOC(doc->virtadr, LastDataRead);
363 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
364 id = ReadDOC(doc->virtadr, LastDataRead);
366 /* CDSN Slow IO register see Software Req 11.4 item 5. */
367 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
369 mfr = ReadDOC_(doc->virtadr, doc->ioreg);
371 /* CDSN Slow IO register see Software Req 11.4 item 5. */
372 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
374 id = ReadDOC_(doc->virtadr, doc->ioreg);
377 /* No response - return failure */
378 if (mfr == 0xff || mfr == 0)
381 /* Check it's the same as the first chip we identified.
382 * M-Systems say that any given DiskOnChip device should only
383 * contain _one_ type of flash part, although that's not a
384 * hardware restriction. */
386 if (doc->mfr == mfr && doc->id == id)
387 return 1; /* This is another the same the first */
390 "Flash chip at floor %d, chip %d is different:\n",
394 /* Print and store the manufacturer and ID codes. */
395 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
396 if (id == nand_flash_ids[i].id) {
397 /* Try to identify manufacturer */
398 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
399 if (nand_manuf_ids[j].id == mfr)
403 "Flash chip found: Manufacturer ID: %2.2X, "
404 "Chip ID: %2.2X (%s:%s)\n", mfr, id,
405 nand_manuf_ids[j].name, nand_flash_ids[i].name);
410 ffs((nand_flash_ids[i].chipsize << 20)) - 1;
411 doc->page256 = (nand_flash_ids[i].pagesize == 256) ? 1 : 0;
412 doc->pageadrlen = doc->chipshift > 25 ? 3 : 2;
414 nand_flash_ids[i].erasesize;
422 /* We haven't fully identified the chip. Print as much as we know. */
423 printk(KERN_WARNING "Unknown flash chip found: %2.2X %2.2X\n",
426 printk(KERN_WARNING "Please report to dwmw2@infradead.org\n");
430 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
432 static void DoC_ScanChips(struct DiskOnChip *this, int maxchips)
435 int numchips[MAX_FLOORS];
442 /* For each floor, find the number of valid chips it contains */
443 for (floor = 0; floor < MAX_FLOORS; floor++) {
446 for (chip = 0; chip < maxchips && ret != 0; chip++) {
448 ret = DoC_IdentChip(this, floor, chip);
456 /* If there are none at all that we recognise, bail */
457 if (!this->numchips) {
458 printk(KERN_NOTICE "No flash chips recognised.\n");
462 /* Allocate an array to hold the information for each chip */
463 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
465 printk(KERN_NOTICE "No memory for allocating chip info structures\n");
471 /* Fill out the chip array with {floor, chipno} for each
472 * detected chip in the device. */
473 for (floor = 0; floor < MAX_FLOORS; floor++) {
474 for (chip = 0; chip < numchips[floor]; chip++) {
475 this->chips[ret].floor = floor;
476 this->chips[ret].chip = chip;
477 this->chips[ret].curadr = 0;
478 this->chips[ret].curmode = 0x50;
483 /* Calculate and print the total size of the device */
484 this->totlen = this->numchips * (1 << this->chipshift);
486 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
487 this->numchips, this->totlen >> 20);
490 static int DoC2k_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
492 int tmp1, tmp2, retval;
493 if (doc1->physadr == doc2->physadr)
496 /* Use the alias resolution register which was set aside for this
497 * purpose. If it's value is the same on both chips, they might
498 * be the same chip, and we write to one and check for a change in
499 * the other. It's unclear if this register is usuable in the
500 * DoC 2000 (it's in the Millennium docs), but it seems to work. */
501 tmp1 = ReadDOC(doc1->virtadr, AliasResolution);
502 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
506 WriteDOC((tmp1 + 1) % 0xff, doc1->virtadr, AliasResolution);
507 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
508 if (tmp2 == (tmp1 + 1) % 0xff)
513 /* Restore register contents. May not be necessary, but do it just to
515 WriteDOC(tmp1, doc1->virtadr, AliasResolution);
520 /* This routine is found from the docprobe code by symbol_get(),
521 * which will bump the use count of this module. */
522 void DoC2k_init(struct mtd_info *mtd)
524 struct DiskOnChip *this = mtd->priv;
525 struct DiskOnChip *old = NULL;
528 /* We must avoid being called twice for the same device. */
531 old = doc2klist->priv;
534 if (DoC2k_is_alias(old, this)) {
536 "Ignoring DiskOnChip 2000 at 0x%lX - already configured\n",
538 iounmap(this->virtadr);
543 old = old->nextdoc->priv;
549 switch (this->ChipID) {
550 case DOC_ChipID_Doc2kTSOP:
551 mtd->name = "DiskOnChip 2000 TSOP";
552 this->ioreg = DoC_Mil_CDSN_IO;
553 /* Pretend it's a Millennium */
554 this->ChipID = DOC_ChipID_DocMil;
555 maxchips = MAX_CHIPS;
557 case DOC_ChipID_Doc2k:
558 mtd->name = "DiskOnChip 2000";
559 this->ioreg = DoC_2k_CDSN_IO;
560 maxchips = MAX_CHIPS;
562 case DOC_ChipID_DocMil:
563 mtd->name = "DiskOnChip Millennium";
564 this->ioreg = DoC_Mil_CDSN_IO;
565 maxchips = MAX_CHIPS_MIL;
568 printk("Unknown ChipID 0x%02x\n", this->ChipID);
570 iounmap(this->virtadr);
574 printk(KERN_NOTICE "%s found at address 0x%lX\n", mtd->name,
577 mtd->type = MTD_NANDFLASH;
578 mtd->flags = MTD_CAP_NANDFLASH;
579 mtd->ecctype = MTD_ECC_RS_DiskOnChip;
582 mtd->writesize = 512;
584 mtd->owner = THIS_MODULE;
585 mtd->erase = doc_erase;
588 mtd->read = doc_read;
589 mtd->write = doc_write;
590 mtd->read_ecc = doc_read_ecc;
591 mtd->write_ecc = doc_write_ecc;
592 mtd->writev_ecc = doc_writev_ecc;
593 mtd->read_oob = doc_read_oob;
594 mtd->write_oob = doc_write_oob;
602 mutex_init(&this->lock);
604 /* Ident all the chips present. */
605 DoC_ScanChips(this, maxchips);
609 iounmap(this->virtadr);
611 this->nextdoc = doc2klist;
613 mtd->size = this->totlen;
614 mtd->erasesize = this->erasesize;
619 EXPORT_SYMBOL_GPL(DoC2k_init);
621 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
622 size_t * retlen, u_char * buf)
624 /* Just a special case of doc_read_ecc */
625 return doc_read_ecc(mtd, from, len, retlen, buf, NULL, NULL);
628 static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
629 size_t * retlen, u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel)
631 struct DiskOnChip *this = mtd->priv;
632 void __iomem *docptr = this->virtadr;
634 unsigned char syndrome[6];
636 int i, len256 = 0, ret=0;
639 /* Don't allow read past end of device */
640 if (from >= this->totlen)
643 mutex_lock(&this->lock);
649 /* Don't allow a single read to cross a 512-byte block boundary */
650 if (from + len > ((from | 0x1ff) + 1))
651 len = ((from | 0x1ff) + 1) - from;
653 /* The ECC will not be calculated correctly if less than 512 is read */
654 if (len != 0x200 && eccbuf)
656 "ECC needs a full sector read (adr: %lx size %lx)\n",
657 (long) from, (long) len);
659 /* printk("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len); */
662 /* Find the chip which is to be used and select it */
663 mychip = &this->chips[from >> (this->chipshift)];
665 if (this->curfloor != mychip->floor) {
666 DoC_SelectFloor(this, mychip->floor);
667 DoC_SelectChip(this, mychip->chip);
668 } else if (this->curchip != mychip->chip) {
669 DoC_SelectChip(this, mychip->chip);
672 this->curfloor = mychip->floor;
673 this->curchip = mychip->chip;
677 && (from & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
679 DoC_Address(this, ADDR_COLUMN_PAGE, from, CDSN_CTRL_WP,
683 /* Prime the ECC engine */
684 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
685 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
687 /* disable the ECC engine */
688 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
689 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
692 /* treat crossing 256-byte sector for 2M x 8bits devices */
693 if (this->page256 && from + len > (from | 0xff) + 1) {
694 len256 = (from | 0xff) + 1 - from;
695 DoC_ReadBuf(this, buf, len256);
697 DoC_Command(this, NAND_CMD_READ0, CDSN_CTRL_WP);
698 DoC_Address(this, ADDR_COLUMN_PAGE, from + len256,
699 CDSN_CTRL_WP, CDSN_CTRL_ECC_IO);
702 DoC_ReadBuf(this, &buf[len256], len - len256);
704 /* Let the caller know we completed it */
708 /* Read the ECC data through the DiskOnChip ECC logic */
709 /* Note: this will work even with 2M x 8bit devices as */
710 /* they have 8 bytes of OOB per 256 page. mf. */
711 DoC_ReadBuf(this, eccbuf, 6);
713 /* Flush the pipeline */
714 if (DoC_is_Millennium(this)) {
715 dummy = ReadDOC(docptr, ECCConf);
716 dummy = ReadDOC(docptr, ECCConf);
717 i = ReadDOC(docptr, ECCConf);
719 dummy = ReadDOC(docptr, 2k_ECCStatus);
720 dummy = ReadDOC(docptr, 2k_ECCStatus);
721 i = ReadDOC(docptr, 2k_ECCStatus);
724 /* Check the ECC Status */
727 /* There was an ECC error */
729 printk(KERN_ERR "DiskOnChip ECC Error: Read at %lx\n", (long)from);
731 /* Read the ECC syndrom through the DiskOnChip ECC logic.
732 These syndrome will be all ZERO when there is no error */
733 for (i = 0; i < 6; i++) {
735 ReadDOC(docptr, ECCSyndrome0 + i);
737 nb_errors = doc_decode_ecc(buf, syndrome);
740 printk(KERN_ERR "Errors corrected: %x\n", nb_errors);
743 /* We return error, but have actually done the read. Not that
744 this can be told to user-space, via sys_read(), but at least
745 MTD-aware stuff can know about it by checking *retlen */
751 printk(KERN_DEBUG "ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
752 (long)from, eccbuf[0], eccbuf[1], eccbuf[2],
753 eccbuf[3], eccbuf[4], eccbuf[5]);
756 /* disable the ECC engine */
757 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
760 /* according to 11.4.1, we need to wait for the busy line
761 * drop if we read to the end of the page. */
762 if(0 == ((from + len) & 0x1ff))
772 mutex_unlock(&this->lock);
777 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
778 size_t * retlen, const u_char * buf)
781 return doc_write_ecc(mtd, to, len, retlen, buf, eccbuf, NULL);
784 static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
785 size_t * retlen, const u_char * buf,
786 u_char * eccbuf, struct nand_oobinfo *oobsel)
788 struct DiskOnChip *this = mtd->priv;
789 int di; /* Yes, DI is a hangover from when I was disassembling the binary driver */
790 void __iomem *docptr = this->virtadr;
797 /* Don't allow write past end of device */
798 if (to >= this->totlen)
801 mutex_lock(&this->lock);
807 /* Don't allow a single write to cross a 512-byte block boundary */
808 if (to + len > ((to | 0x1ff) + 1))
809 len = ((to | 0x1ff) + 1) - to;
811 /* The ECC will not be calculated correctly if less than 512 is written */
813 if (len != 0x200 && eccbuf)
815 "ECC needs a full sector write (adr: %lx size %lx)\n",
816 (long) to, (long) len);
819 /* printk("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */
821 /* Find the chip which is to be used and select it */
822 mychip = &this->chips[to >> (this->chipshift)];
824 if (this->curfloor != mychip->floor) {
825 DoC_SelectFloor(this, mychip->floor);
826 DoC_SelectChip(this, mychip->chip);
827 } else if (this->curchip != mychip->chip) {
828 DoC_SelectChip(this, mychip->chip);
831 this->curfloor = mychip->floor;
832 this->curchip = mychip->chip;
834 /* Set device to main plane of flash */
835 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
838 && (to & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
841 DoC_Command(this, NAND_CMD_SEQIN, 0);
842 DoC_Address(this, ADDR_COLUMN_PAGE, to, 0, CDSN_CTRL_ECC_IO);
845 /* Prime the ECC engine */
846 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
847 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
849 /* disable the ECC engine */
850 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
851 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
854 /* treat crossing 256-byte sector for 2M x 8bits devices */
855 if (this->page256 && to + len > (to | 0xff) + 1) {
856 len256 = (to | 0xff) + 1 - to;
857 DoC_WriteBuf(this, buf, len256);
859 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
861 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
862 /* There's an implicit DoC_WaitReady() in DoC_Command */
864 dummy = ReadDOC(docptr, CDSNSlowIO);
867 if (ReadDOC_(docptr, this->ioreg) & 1) {
868 printk(KERN_ERR "Error programming flash\n");
869 /* Error in programming */
871 mutex_unlock(&this->lock);
875 DoC_Command(this, NAND_CMD_SEQIN, 0);
876 DoC_Address(this, ADDR_COLUMN_PAGE, to + len256, 0,
880 DoC_WriteBuf(this, &buf[len256], len - len256);
883 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_CE, docptr,
886 if (DoC_is_Millennium(this)) {
887 WriteDOC(0, docptr, NOP);
888 WriteDOC(0, docptr, NOP);
889 WriteDOC(0, docptr, NOP);
891 WriteDOC_(0, docptr, this->ioreg);
892 WriteDOC_(0, docptr, this->ioreg);
893 WriteDOC_(0, docptr, this->ioreg);
896 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_FLASH_IO | CDSN_CTRL_CE, docptr,
899 /* Read the ECC data through the DiskOnChip ECC logic */
900 for (di = 0; di < 6; di++) {
901 eccbuf[di] = ReadDOC(docptr, ECCSyndrome0 + di);
904 /* Reset the ECC engine */
905 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
909 ("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
910 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
911 eccbuf[4], eccbuf[5]);
915 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
917 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
918 /* There's an implicit DoC_WaitReady() in DoC_Command */
920 if (DoC_is_Millennium(this)) {
921 ReadDOC(docptr, ReadPipeInit);
922 status = ReadDOC(docptr, LastDataRead);
924 dummy = ReadDOC(docptr, CDSNSlowIO);
926 status = ReadDOC_(docptr, this->ioreg);
930 printk(KERN_ERR "Error programming flash\n");
931 /* Error in programming */
933 mutex_unlock(&this->lock);
937 /* Let the caller know we completed it */
945 /* Write the ECC data to flash */
946 for (di=0; di<6; di++)
952 ret = doc_write_oob_nolock(mtd, to, 8, &dummy, x);
954 mutex_unlock(&this->lock);
964 mutex_unlock(&this->lock);
968 static int doc_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs,
969 unsigned long count, loff_t to, size_t *retlen,
970 u_char *eccbuf, struct nand_oobinfo *oobsel)
972 static char static_buf[512];
973 static DEFINE_MUTEX(writev_buf_mutex);
975 size_t totretlen = 0;
976 size_t thisvecofs = 0;
979 mutex_lock(&writev_buf_mutex);
982 size_t thislen, thisretlen;
985 buf = vecs->iov_base + thisvecofs;
986 thislen = vecs->iov_len - thisvecofs;
989 if (thislen >= 512) {
990 thislen = thislen & ~(512-1);
991 thisvecofs += thislen;
993 /* Not enough to fill a page. Copy into buf */
994 memcpy(static_buf, buf, thislen);
995 buf = &static_buf[thislen];
997 while(count && thislen < 512) {
1000 thisvecofs = min((512-thislen), vecs->iov_len);
1001 memcpy(buf, vecs->iov_base, thisvecofs);
1002 thislen += thisvecofs;
1007 if (count && thisvecofs == vecs->iov_len) {
1012 ret = doc_write_ecc(mtd, to, thislen, &thisretlen, buf, eccbuf, oobsel);
1014 totretlen += thisretlen;
1016 if (ret || thisretlen != thislen)
1022 mutex_unlock(&writev_buf_mutex);
1023 *retlen = totretlen;
1028 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
1029 size_t * retlen, u_char * buf)
1031 struct DiskOnChip *this = mtd->priv;
1032 int len256 = 0, ret;
1033 struct Nand *mychip;
1035 mutex_lock(&this->lock);
1037 mychip = &this->chips[ofs >> this->chipshift];
1039 if (this->curfloor != mychip->floor) {
1040 DoC_SelectFloor(this, mychip->floor);
1041 DoC_SelectChip(this, mychip->chip);
1042 } else if (this->curchip != mychip->chip) {
1043 DoC_SelectChip(this, mychip->chip);
1045 this->curfloor = mychip->floor;
1046 this->curchip = mychip->chip;
1048 /* update address for 2M x 8bit devices. OOB starts on the second */
1049 /* page to maintain compatibility with doc_read_ecc. */
1050 if (this->page256) {
1057 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1058 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, CDSN_CTRL_WP, 0);
1060 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1061 /* Note: datasheet says it should automaticaly wrap to the */
1062 /* next OOB block, but it didn't work here. mf. */
1063 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1064 len256 = (ofs | 0x7) + 1 - ofs;
1065 DoC_ReadBuf(this, buf, len256);
1067 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1068 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff),
1072 DoC_ReadBuf(this, &buf[len256], len - len256);
1075 /* Reading the full OOB data drops us off of the end of the page,
1076 * causing the flash device to go into busy mode, so we need
1077 * to wait until ready 11.4.1 and Toshiba TC58256FT docs */
1079 ret = DoC_WaitReady(this);
1081 mutex_unlock(&this->lock);
1086 static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
1087 size_t * retlen, const u_char * buf)
1089 struct DiskOnChip *this = mtd->priv;
1091 void __iomem *docptr = this->virtadr;
1092 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
1096 // 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,
1097 // buf[0], buf[1], buf[2], buf[3], buf[8], buf[9], buf[14],buf[15]);
1099 /* Find the chip which is to be used and select it */
1100 if (this->curfloor != mychip->floor) {
1101 DoC_SelectFloor(this, mychip->floor);
1102 DoC_SelectChip(this, mychip->chip);
1103 } else if (this->curchip != mychip->chip) {
1104 DoC_SelectChip(this, mychip->chip);
1106 this->curfloor = mychip->floor;
1107 this->curchip = mychip->chip;
1109 /* disable the ECC engine */
1110 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
1111 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
1113 /* Reset the chip, see Software Requirement 11.4 item 1. */
1114 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
1116 /* issue the Read2 command to set the pointer to the Spare Data Area. */
1117 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1119 /* update address for 2M x 8bit devices. OOB starts on the second */
1120 /* page to maintain compatibility with doc_read_ecc. */
1121 if (this->page256) {
1128 /* issue the Serial Data In command to initial the Page Program process */
1129 DoC_Command(this, NAND_CMD_SEQIN, 0);
1130 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, 0, 0);
1132 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1133 /* Note: datasheet says it should automaticaly wrap to the */
1134 /* next OOB block, but it didn't work here. mf. */
1135 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1136 len256 = (ofs | 0x7) + 1 - ofs;
1137 DoC_WriteBuf(this, buf, len256);
1139 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1140 DoC_Command(this, NAND_CMD_STATUS, 0);
1141 /* DoC_WaitReady() is implicit in DoC_Command */
1143 if (DoC_is_Millennium(this)) {
1144 ReadDOC(docptr, ReadPipeInit);
1145 status = ReadDOC(docptr, LastDataRead);
1147 dummy = ReadDOC(docptr, CDSNSlowIO);
1149 status = ReadDOC_(docptr, this->ioreg);
1153 printk(KERN_ERR "Error programming oob data\n");
1154 /* There was an error */
1158 DoC_Command(this, NAND_CMD_SEQIN, 0);
1159 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), 0, 0);
1162 DoC_WriteBuf(this, &buf[len256], len - len256);
1164 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1165 DoC_Command(this, NAND_CMD_STATUS, 0);
1166 /* DoC_WaitReady() is implicit in DoC_Command */
1168 if (DoC_is_Millennium(this)) {
1169 ReadDOC(docptr, ReadPipeInit);
1170 status = ReadDOC(docptr, LastDataRead);
1172 dummy = ReadDOC(docptr, CDSNSlowIO);
1174 status = ReadDOC_(docptr, this->ioreg);
1178 printk(KERN_ERR "Error programming oob data\n");
1179 /* There was an error */
1189 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
1190 size_t * retlen, const u_char * buf)
1192 struct DiskOnChip *this = mtd->priv;
1195 mutex_lock(&this->lock);
1196 ret = doc_write_oob_nolock(mtd, ofs, len, retlen, buf);
1198 mutex_unlock(&this->lock);
1202 static int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
1204 struct DiskOnChip *this = mtd->priv;
1205 __u32 ofs = instr->addr;
1206 __u32 len = instr->len;
1208 void __iomem *docptr = this->virtadr;
1209 struct Nand *mychip;
1212 mutex_lock(&this->lock);
1214 if (ofs & (mtd->erasesize-1) || len & (mtd->erasesize-1)) {
1215 mutex_unlock(&this->lock);
1219 instr->state = MTD_ERASING;
1221 /* FIXME: Do this in the background. Use timers or schedule_task() */
1223 mychip = &this->chips[ofs >> this->chipshift];
1225 if (this->curfloor != mychip->floor) {
1226 DoC_SelectFloor(this, mychip->floor);
1227 DoC_SelectChip(this, mychip->chip);
1228 } else if (this->curchip != mychip->chip) {
1229 DoC_SelectChip(this, mychip->chip);
1231 this->curfloor = mychip->floor;
1232 this->curchip = mychip->chip;
1234 DoC_Command(this, NAND_CMD_ERASE1, 0);
1235 DoC_Address(this, ADDR_PAGE, ofs, 0, 0);
1236 DoC_Command(this, NAND_CMD_ERASE2, 0);
1238 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
1240 if (DoC_is_Millennium(this)) {
1241 ReadDOC(docptr, ReadPipeInit);
1242 status = ReadDOC(docptr, LastDataRead);
1244 dummy = ReadDOC(docptr, CDSNSlowIO);
1246 status = ReadDOC_(docptr, this->ioreg);
1250 printk(KERN_ERR "Error erasing at 0x%x\n", ofs);
1251 /* There was an error */
1252 instr->state = MTD_ERASE_FAILED;
1255 ofs += mtd->erasesize;
1256 len -= mtd->erasesize;
1258 instr->state = MTD_ERASE_DONE;
1261 mtd_erase_callback(instr);
1263 mutex_unlock(&this->lock);
1268 /****************************************************************************
1272 ****************************************************************************/
1274 static void __exit cleanup_doc2000(void)
1276 struct mtd_info *mtd;
1277 struct DiskOnChip *this;
1279 while ((mtd = doc2klist)) {
1281 doc2klist = this->nextdoc;
1283 del_mtd_device(mtd);
1285 iounmap(this->virtadr);
1291 module_exit(cleanup_doc2000);
1293 MODULE_LICENSE("GPL");
1294 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
1295 MODULE_DESCRIPTION("MTD driver for DiskOnChip 2000 and Millennium");