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_oob(struct mtd_info *mtd, loff_t ofs,
59 struct mtd_oob_ops *ops);
60 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
61 struct mtd_oob_ops *ops);
62 static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
63 size_t *retlen, const u_char *buf);
64 static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
66 static struct mtd_info *doc2klist = NULL;
68 /* Perform the required delay cycles by reading from the appropriate register */
69 static void DoC_Delay(struct DiskOnChip *doc, unsigned short cycles)
74 for (i = 0; i < cycles; i++) {
75 if (DoC_is_Millennium(doc))
76 dummy = ReadDOC(doc->virtadr, NOP);
78 dummy = ReadDOC(doc->virtadr, DOCStatus);
83 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
84 static int _DoC_WaitReady(struct DiskOnChip *doc)
86 void __iomem *docptr = doc->virtadr;
87 unsigned long timeo = jiffies + (HZ * 10);
89 DEBUG(MTD_DEBUG_LEVEL3,
90 "_DoC_WaitReady called for out-of-line wait\n");
92 /* Out-of-line routine to wait for chip response */
93 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
94 /* issue 2 read from NOP register after reading from CDSNControl register
95 see Software Requirement 11.4 item 2. */
98 if (time_after(jiffies, timeo)) {
99 DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
109 static inline int DoC_WaitReady(struct DiskOnChip *doc)
111 void __iomem *docptr = doc->virtadr;
113 /* This is inline, to optimise the common case, where it's ready instantly */
116 /* 4 read form NOP register should be issued in prior to the read from CDSNControl
117 see Software Requirement 11.4 item 2. */
120 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
121 /* Call the out-of-line routine to wait */
122 ret = _DoC_WaitReady(doc);
124 /* issue 2 read from NOP register after reading from CDSNControl register
125 see Software Requirement 11.4 item 2. */
131 /* DoC_Command: Send a flash command to the flash chip through the CDSN Slow IO register to
132 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
133 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
135 static int DoC_Command(struct DiskOnChip *doc, unsigned char command,
136 unsigned char xtraflags)
138 void __iomem *docptr = doc->virtadr;
140 if (DoC_is_2000(doc))
141 xtraflags |= CDSN_CTRL_FLASH_IO;
143 /* Assert the CLE (Command Latch Enable) line to the flash chip */
144 WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
145 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
147 if (DoC_is_Millennium(doc))
148 WriteDOC(command, docptr, CDSNSlowIO);
150 /* Send the command */
151 WriteDOC_(command, docptr, doc->ioreg);
152 if (DoC_is_Millennium(doc))
153 WriteDOC(command, docptr, WritePipeTerm);
155 /* Lower the CLE line */
156 WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
157 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
159 /* Wait for the chip to respond - Software requirement 11.4.1 (extended for any command) */
160 return DoC_WaitReady(doc);
163 /* DoC_Address: Set the current address for the flash chip through the CDSN Slow IO register to
164 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
165 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
167 static int DoC_Address(struct DiskOnChip *doc, int numbytes, unsigned long ofs,
168 unsigned char xtraflags1, unsigned char xtraflags2)
171 void __iomem *docptr = doc->virtadr;
173 if (DoC_is_2000(doc))
174 xtraflags1 |= CDSN_CTRL_FLASH_IO;
176 /* Assert the ALE (Address Latch Enable) line to the flash chip */
177 WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
179 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
181 /* Send the address */
182 /* Devices with 256-byte page are addressed as:
183 Column (bits 0-7), Page (bits 8-15, 16-23, 24-31)
184 * there is no device on the market with page256
185 and more than 24 bits.
186 Devices with 512-byte page are addressed as:
187 Column (bits 0-7), Page (bits 9-16, 17-24, 25-31)
188 * 25-31 is sent only if the chip support it.
189 * bit 8 changes the read command to be sent
190 (NAND_CMD_READ0 or NAND_CMD_READ1).
193 if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE) {
194 if (DoC_is_Millennium(doc))
195 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
196 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
205 if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE) {
206 for (i = 0; i < doc->pageadrlen; i++, ofs = ofs >> 8) {
207 if (DoC_is_Millennium(doc))
208 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
209 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
213 if (DoC_is_Millennium(doc))
214 WriteDOC(ofs & 0xff, docptr, WritePipeTerm);
216 DoC_Delay(doc, 2); /* Needed for some slow flash chips. mf. */
218 /* FIXME: The SlowIO's for millennium could be replaced by
219 a single WritePipeTerm here. mf. */
221 /* Lower the ALE line */
222 WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr,
225 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
227 /* Wait for the chip to respond - Software requirement 11.4.1 */
228 return DoC_WaitReady(doc);
231 /* Read a buffer from DoC, taking care of Millennium odditys */
232 static void DoC_ReadBuf(struct DiskOnChip *doc, u_char * buf, int len)
235 int modulus = 0xffff;
236 void __iomem *docptr = doc->virtadr;
242 if (DoC_is_Millennium(doc)) {
243 /* Read the data via the internal pipeline through CDSN IO register,
244 see Pipelined Read Operations 11.3 */
245 dummy = ReadDOC(docptr, ReadPipeInit);
247 /* Millennium should use the LastDataRead register - Pipeline Reads */
250 /* This is needed for correctly ECC calculation */
254 for (i = 0; i < len; i++)
255 buf[i] = ReadDOC_(docptr, doc->ioreg + (i & modulus));
257 if (DoC_is_Millennium(doc)) {
258 buf[i] = ReadDOC(docptr, LastDataRead);
262 /* Write a buffer to DoC, taking care of Millennium odditys */
263 static void DoC_WriteBuf(struct DiskOnChip *doc, const u_char * buf, int len)
265 void __iomem *docptr = doc->virtadr;
271 for (i = 0; i < len; i++)
272 WriteDOC_(buf[i], docptr, doc->ioreg + i);
274 if (DoC_is_Millennium(doc)) {
275 WriteDOC(0x00, docptr, WritePipeTerm);
280 /* DoC_SelectChip: Select a given flash chip within the current floor */
282 static inline int DoC_SelectChip(struct DiskOnChip *doc, int chip)
284 void __iomem *docptr = doc->virtadr;
286 /* Software requirement 11.4.4 before writing DeviceSelect */
287 /* Deassert the CE line to eliminate glitches on the FCE# outputs */
288 WriteDOC(CDSN_CTRL_WP, docptr, CDSNControl);
289 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
291 /* Select the individual flash chip requested */
292 WriteDOC(chip, docptr, CDSNDeviceSelect);
295 /* Reassert the CE line */
296 WriteDOC(CDSN_CTRL_CE | CDSN_CTRL_FLASH_IO | CDSN_CTRL_WP, docptr,
298 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
300 /* Wait for it to be ready */
301 return DoC_WaitReady(doc);
304 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
306 static inline int DoC_SelectFloor(struct DiskOnChip *doc, int floor)
308 void __iomem *docptr = doc->virtadr;
310 /* Select the floor (bank) of chips required */
311 WriteDOC(floor, docptr, FloorSelect);
313 /* Wait for the chip to be ready */
314 return DoC_WaitReady(doc);
317 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
319 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
324 /* Page in the required floor/chip */
325 DoC_SelectFloor(doc, floor);
326 DoC_SelectChip(doc, chip);
329 if (DoC_Command(doc, NAND_CMD_RESET, CDSN_CTRL_WP)) {
330 DEBUG(MTD_DEBUG_LEVEL2,
331 "DoC_Command (reset) for %d,%d returned true\n",
337 /* Read the NAND chip ID: 1. Send ReadID command */
338 if (DoC_Command(doc, NAND_CMD_READID, CDSN_CTRL_WP)) {
339 DEBUG(MTD_DEBUG_LEVEL2,
340 "DoC_Command (ReadID) for %d,%d returned true\n",
345 /* Read the NAND chip ID: 2. Send address byte zero */
346 DoC_Address(doc, ADDR_COLUMN, 0, CDSN_CTRL_WP, 0);
348 /* Read the manufacturer and device id codes from the device */
350 if (DoC_is_Millennium(doc)) {
352 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
353 mfr = ReadDOC(doc->virtadr, LastDataRead);
356 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
357 id = ReadDOC(doc->virtadr, LastDataRead);
359 /* CDSN Slow IO register see Software Req 11.4 item 5. */
360 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
362 mfr = ReadDOC_(doc->virtadr, doc->ioreg);
364 /* CDSN Slow IO register see Software Req 11.4 item 5. */
365 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
367 id = ReadDOC_(doc->virtadr, doc->ioreg);
370 /* No response - return failure */
371 if (mfr == 0xff || mfr == 0)
374 /* Check it's the same as the first chip we identified.
375 * M-Systems say that any given DiskOnChip device should only
376 * contain _one_ type of flash part, although that's not a
377 * hardware restriction. */
379 if (doc->mfr == mfr && doc->id == id)
380 return 1; /* This is another the same the first */
383 "Flash chip at floor %d, chip %d is different:\n",
387 /* Print and store the manufacturer and ID codes. */
388 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
389 if (id == nand_flash_ids[i].id) {
390 /* Try to identify manufacturer */
391 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
392 if (nand_manuf_ids[j].id == mfr)
396 "Flash chip found: Manufacturer ID: %2.2X, "
397 "Chip ID: %2.2X (%s:%s)\n", mfr, id,
398 nand_manuf_ids[j].name, nand_flash_ids[i].name);
403 ffs((nand_flash_ids[i].chipsize << 20)) - 1;
404 doc->page256 = (nand_flash_ids[i].pagesize == 256) ? 1 : 0;
405 doc->pageadrlen = doc->chipshift > 25 ? 3 : 2;
407 nand_flash_ids[i].erasesize;
415 /* We haven't fully identified the chip. Print as much as we know. */
416 printk(KERN_WARNING "Unknown flash chip found: %2.2X %2.2X\n",
419 printk(KERN_WARNING "Please report to dwmw2@infradead.org\n");
423 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
425 static void DoC_ScanChips(struct DiskOnChip *this, int maxchips)
428 int numchips[MAX_FLOORS];
435 /* For each floor, find the number of valid chips it contains */
436 for (floor = 0; floor < MAX_FLOORS; floor++) {
439 for (chip = 0; chip < maxchips && ret != 0; chip++) {
441 ret = DoC_IdentChip(this, floor, chip);
449 /* If there are none at all that we recognise, bail */
450 if (!this->numchips) {
451 printk(KERN_NOTICE "No flash chips recognised.\n");
455 /* Allocate an array to hold the information for each chip */
456 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
458 printk(KERN_NOTICE "No memory for allocating chip info structures\n");
464 /* Fill out the chip array with {floor, chipno} for each
465 * detected chip in the device. */
466 for (floor = 0; floor < MAX_FLOORS; floor++) {
467 for (chip = 0; chip < numchips[floor]; chip++) {
468 this->chips[ret].floor = floor;
469 this->chips[ret].chip = chip;
470 this->chips[ret].curadr = 0;
471 this->chips[ret].curmode = 0x50;
476 /* Calculate and print the total size of the device */
477 this->totlen = this->numchips * (1 << this->chipshift);
479 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
480 this->numchips, this->totlen >> 20);
483 static int DoC2k_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
485 int tmp1, tmp2, retval;
486 if (doc1->physadr == doc2->physadr)
489 /* Use the alias resolution register which was set aside for this
490 * purpose. If it's value is the same on both chips, they might
491 * be the same chip, and we write to one and check for a change in
492 * the other. It's unclear if this register is usuable in the
493 * DoC 2000 (it's in the Millennium docs), but it seems to work. */
494 tmp1 = ReadDOC(doc1->virtadr, AliasResolution);
495 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
499 WriteDOC((tmp1 + 1) % 0xff, doc1->virtadr, AliasResolution);
500 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
501 if (tmp2 == (tmp1 + 1) % 0xff)
506 /* Restore register contents. May not be necessary, but do it just to
508 WriteDOC(tmp1, doc1->virtadr, AliasResolution);
513 /* This routine is found from the docprobe code by symbol_get(),
514 * which will bump the use count of this module. */
515 void DoC2k_init(struct mtd_info *mtd)
517 struct DiskOnChip *this = mtd->priv;
518 struct DiskOnChip *old = NULL;
521 /* We must avoid being called twice for the same device. */
524 old = doc2klist->priv;
527 if (DoC2k_is_alias(old, this)) {
529 "Ignoring DiskOnChip 2000 at 0x%lX - already configured\n",
531 iounmap(this->virtadr);
536 old = old->nextdoc->priv;
542 switch (this->ChipID) {
543 case DOC_ChipID_Doc2kTSOP:
544 mtd->name = "DiskOnChip 2000 TSOP";
545 this->ioreg = DoC_Mil_CDSN_IO;
546 /* Pretend it's a Millennium */
547 this->ChipID = DOC_ChipID_DocMil;
548 maxchips = MAX_CHIPS;
550 case DOC_ChipID_Doc2k:
551 mtd->name = "DiskOnChip 2000";
552 this->ioreg = DoC_2k_CDSN_IO;
553 maxchips = MAX_CHIPS;
555 case DOC_ChipID_DocMil:
556 mtd->name = "DiskOnChip Millennium";
557 this->ioreg = DoC_Mil_CDSN_IO;
558 maxchips = MAX_CHIPS_MIL;
561 printk("Unknown ChipID 0x%02x\n", this->ChipID);
563 iounmap(this->virtadr);
567 printk(KERN_NOTICE "%s found at address 0x%lX\n", mtd->name,
570 mtd->type = MTD_NANDFLASH;
571 mtd->flags = MTD_CAP_NANDFLASH;
574 mtd->writesize = 512;
576 mtd->owner = THIS_MODULE;
577 mtd->erase = doc_erase;
580 mtd->read = doc_read;
581 mtd->write = doc_write;
582 mtd->read_oob = doc_read_oob;
583 mtd->write_oob = doc_write_oob;
591 mutex_init(&this->lock);
593 /* Ident all the chips present. */
594 DoC_ScanChips(this, maxchips);
598 iounmap(this->virtadr);
600 this->nextdoc = doc2klist;
602 mtd->size = this->totlen;
603 mtd->erasesize = this->erasesize;
608 EXPORT_SYMBOL_GPL(DoC2k_init);
610 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
611 size_t * retlen, u_char * buf)
613 struct DiskOnChip *this = mtd->priv;
614 void __iomem *docptr = this->virtadr;
616 unsigned char syndrome[6], eccbuf[6];
618 int i, len256 = 0, ret=0;
621 /* Don't allow read past end of device */
622 if (from >= this->totlen)
625 mutex_lock(&this->lock);
631 /* Don't allow a single read to cross a 512-byte block boundary */
632 if (from + len > ((from | 0x1ff) + 1))
633 len = ((from | 0x1ff) + 1) - from;
635 /* The ECC will not be calculated correctly if less than 512 is read */
636 if (len != 0x200 && eccbuf)
638 "ECC needs a full sector read (adr: %lx size %lx)\n",
639 (long) from, (long) len);
641 /* printk("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len); */
644 /* Find the chip which is to be used and select it */
645 mychip = &this->chips[from >> (this->chipshift)];
647 if (this->curfloor != mychip->floor) {
648 DoC_SelectFloor(this, mychip->floor);
649 DoC_SelectChip(this, mychip->chip);
650 } else if (this->curchip != mychip->chip) {
651 DoC_SelectChip(this, mychip->chip);
654 this->curfloor = mychip->floor;
655 this->curchip = mychip->chip;
659 && (from & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
661 DoC_Address(this, ADDR_COLUMN_PAGE, from, CDSN_CTRL_WP,
664 /* Prime the ECC engine */
665 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
666 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
668 /* treat crossing 256-byte sector for 2M x 8bits devices */
669 if (this->page256 && from + len > (from | 0xff) + 1) {
670 len256 = (from | 0xff) + 1 - from;
671 DoC_ReadBuf(this, buf, len256);
673 DoC_Command(this, NAND_CMD_READ0, CDSN_CTRL_WP);
674 DoC_Address(this, ADDR_COLUMN_PAGE, from + len256,
675 CDSN_CTRL_WP, CDSN_CTRL_ECC_IO);
678 DoC_ReadBuf(this, &buf[len256], len - len256);
680 /* Let the caller know we completed it */
683 /* Read the ECC data through the DiskOnChip ECC logic */
684 /* Note: this will work even with 2M x 8bit devices as */
685 /* they have 8 bytes of OOB per 256 page. mf. */
686 DoC_ReadBuf(this, eccbuf, 6);
688 /* Flush the pipeline */
689 if (DoC_is_Millennium(this)) {
690 dummy = ReadDOC(docptr, ECCConf);
691 dummy = ReadDOC(docptr, ECCConf);
692 i = ReadDOC(docptr, ECCConf);
694 dummy = ReadDOC(docptr, 2k_ECCStatus);
695 dummy = ReadDOC(docptr, 2k_ECCStatus);
696 i = ReadDOC(docptr, 2k_ECCStatus);
699 /* Check the ECC Status */
702 /* There was an ECC error */
704 printk(KERN_ERR "DiskOnChip ECC Error: Read at %lx\n", (long)from);
706 /* Read the ECC syndrom through the DiskOnChip ECC
707 logic. These syndrome will be all ZERO when there
709 for (i = 0; i < 6; i++) {
711 ReadDOC(docptr, ECCSyndrome0 + i);
713 nb_errors = doc_decode_ecc(buf, syndrome);
716 printk(KERN_ERR "Errors corrected: %x\n", nb_errors);
719 /* We return error, but have actually done the
720 read. Not that this can be told to
721 user-space, via sys_read(), but at least
722 MTD-aware stuff can know about it by
729 printk(KERN_DEBUG "ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
730 (long)from, eccbuf[0], eccbuf[1], eccbuf[2],
731 eccbuf[3], eccbuf[4], eccbuf[5]);
734 /* disable the ECC engine */
735 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
737 /* according to 11.4.1, we need to wait for the busy line
738 * drop if we read to the end of the page. */
739 if(0 == ((from + len) & 0x1ff))
749 mutex_unlock(&this->lock);
754 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
755 size_t * retlen, const u_char * buf)
757 struct DiskOnChip *this = mtd->priv;
758 int di; /* Yes, DI is a hangover from when I was disassembling the binary driver */
759 void __iomem *docptr = this->virtadr;
760 unsigned char eccbuf[6];
767 /* Don't allow write past end of device */
768 if (to >= this->totlen)
771 mutex_lock(&this->lock);
777 /* Don't allow a single write to cross a 512-byte block boundary */
778 if (to + len > ((to | 0x1ff) + 1))
779 len = ((to | 0x1ff) + 1) - to;
781 /* The ECC will not be calculated correctly if less than 512 is written */
783 if (len != 0x200 && eccbuf)
785 "ECC needs a full sector write (adr: %lx size %lx)\n",
786 (long) to, (long) len);
789 /* printk("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */
791 /* Find the chip which is to be used and select it */
792 mychip = &this->chips[to >> (this->chipshift)];
794 if (this->curfloor != mychip->floor) {
795 DoC_SelectFloor(this, mychip->floor);
796 DoC_SelectChip(this, mychip->chip);
797 } else if (this->curchip != mychip->chip) {
798 DoC_SelectChip(this, mychip->chip);
801 this->curfloor = mychip->floor;
802 this->curchip = mychip->chip;
804 /* Set device to main plane of flash */
805 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
808 && (to & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
811 DoC_Command(this, NAND_CMD_SEQIN, 0);
812 DoC_Address(this, ADDR_COLUMN_PAGE, to, 0, CDSN_CTRL_ECC_IO);
814 /* Prime the ECC engine */
815 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
816 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
818 /* treat crossing 256-byte sector for 2M x 8bits devices */
819 if (this->page256 && to + len > (to | 0xff) + 1) {
820 len256 = (to | 0xff) + 1 - to;
821 DoC_WriteBuf(this, buf, len256);
823 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
825 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
826 /* There's an implicit DoC_WaitReady() in DoC_Command */
828 dummy = ReadDOC(docptr, CDSNSlowIO);
831 if (ReadDOC_(docptr, this->ioreg) & 1) {
832 printk(KERN_ERR "Error programming flash\n");
833 /* Error in programming */
835 mutex_unlock(&this->lock);
839 DoC_Command(this, NAND_CMD_SEQIN, 0);
840 DoC_Address(this, ADDR_COLUMN_PAGE, to + len256, 0,
844 DoC_WriteBuf(this, &buf[len256], len - len256);
846 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_CE, docptr, CDSNControl);
848 if (DoC_is_Millennium(this)) {
849 WriteDOC(0, docptr, NOP);
850 WriteDOC(0, docptr, NOP);
851 WriteDOC(0, docptr, NOP);
853 WriteDOC_(0, docptr, this->ioreg);
854 WriteDOC_(0, docptr, this->ioreg);
855 WriteDOC_(0, docptr, this->ioreg);
858 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_FLASH_IO | CDSN_CTRL_CE, docptr,
861 /* Read the ECC data through the DiskOnChip ECC logic */
862 for (di = 0; di < 6; di++) {
863 eccbuf[di] = ReadDOC(docptr, ECCSyndrome0 + di);
866 /* Reset the ECC engine */
867 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
871 ("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
872 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
873 eccbuf[4], eccbuf[5]);
875 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
877 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
878 /* There's an implicit DoC_WaitReady() in DoC_Command */
880 if (DoC_is_Millennium(this)) {
881 ReadDOC(docptr, ReadPipeInit);
882 status = ReadDOC(docptr, LastDataRead);
884 dummy = ReadDOC(docptr, CDSNSlowIO);
886 status = ReadDOC_(docptr, this->ioreg);
890 printk(KERN_ERR "Error programming flash\n");
891 /* Error in programming */
893 mutex_unlock(&this->lock);
897 /* Let the caller know we completed it */
905 /* Write the ECC data to flash */
906 for (di=0; di<6; di++)
912 ret = doc_write_oob_nolock(mtd, to, 8, &dummy, x);
914 mutex_unlock(&this->lock);
924 mutex_unlock(&this->lock);
928 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
929 struct mtd_oob_ops *ops)
931 struct DiskOnChip *this = mtd->priv;
934 uint8_t *buf = ops->oobbuf;
935 size_t len = ops->len;
937 BUG_ON(ops->mode != MTD_OOB_PLACE);
941 mutex_lock(&this->lock);
943 mychip = &this->chips[ofs >> this->chipshift];
945 if (this->curfloor != mychip->floor) {
946 DoC_SelectFloor(this, mychip->floor);
947 DoC_SelectChip(this, mychip->chip);
948 } else if (this->curchip != mychip->chip) {
949 DoC_SelectChip(this, mychip->chip);
951 this->curfloor = mychip->floor;
952 this->curchip = mychip->chip;
954 /* update address for 2M x 8bit devices. OOB starts on the second */
955 /* page to maintain compatibility with doc_read_ecc. */
963 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
964 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, CDSN_CTRL_WP, 0);
966 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
967 /* Note: datasheet says it should automaticaly wrap to the */
968 /* next OOB block, but it didn't work here. mf. */
969 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
970 len256 = (ofs | 0x7) + 1 - ofs;
971 DoC_ReadBuf(this, buf, len256);
973 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
974 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff),
978 DoC_ReadBuf(this, &buf[len256], len - len256);
981 /* Reading the full OOB data drops us off of the end of the page,
982 * causing the flash device to go into busy mode, so we need
983 * to wait until ready 11.4.1 and Toshiba TC58256FT docs */
985 ret = DoC_WaitReady(this);
987 mutex_unlock(&this->lock);
992 static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
993 size_t * retlen, const u_char * buf)
995 struct DiskOnChip *this = mtd->priv;
997 void __iomem *docptr = this->virtadr;
998 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
1002 // 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,
1003 // buf[0], buf[1], buf[2], buf[3], buf[8], buf[9], buf[14],buf[15]);
1005 /* Find the chip which is to be used and select it */
1006 if (this->curfloor != mychip->floor) {
1007 DoC_SelectFloor(this, mychip->floor);
1008 DoC_SelectChip(this, mychip->chip);
1009 } else if (this->curchip != mychip->chip) {
1010 DoC_SelectChip(this, mychip->chip);
1012 this->curfloor = mychip->floor;
1013 this->curchip = mychip->chip;
1015 /* disable the ECC engine */
1016 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
1017 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
1019 /* Reset the chip, see Software Requirement 11.4 item 1. */
1020 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
1022 /* issue the Read2 command to set the pointer to the Spare Data Area. */
1023 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1025 /* update address for 2M x 8bit devices. OOB starts on the second */
1026 /* page to maintain compatibility with doc_read_ecc. */
1027 if (this->page256) {
1034 /* issue the Serial Data In command to initial the Page Program process */
1035 DoC_Command(this, NAND_CMD_SEQIN, 0);
1036 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, 0, 0);
1038 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1039 /* Note: datasheet says it should automaticaly wrap to the */
1040 /* next OOB block, but it didn't work here. mf. */
1041 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1042 len256 = (ofs | 0x7) + 1 - ofs;
1043 DoC_WriteBuf(this, buf, len256);
1045 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1046 DoC_Command(this, NAND_CMD_STATUS, 0);
1047 /* DoC_WaitReady() is implicit in DoC_Command */
1049 if (DoC_is_Millennium(this)) {
1050 ReadDOC(docptr, ReadPipeInit);
1051 status = ReadDOC(docptr, LastDataRead);
1053 dummy = ReadDOC(docptr, CDSNSlowIO);
1055 status = ReadDOC_(docptr, this->ioreg);
1059 printk(KERN_ERR "Error programming oob data\n");
1060 /* There was an error */
1064 DoC_Command(this, NAND_CMD_SEQIN, 0);
1065 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), 0, 0);
1068 DoC_WriteBuf(this, &buf[len256], len - len256);
1070 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1071 DoC_Command(this, NAND_CMD_STATUS, 0);
1072 /* DoC_WaitReady() is implicit in DoC_Command */
1074 if (DoC_is_Millennium(this)) {
1075 ReadDOC(docptr, ReadPipeInit);
1076 status = ReadDOC(docptr, LastDataRead);
1078 dummy = ReadDOC(docptr, CDSNSlowIO);
1080 status = ReadDOC_(docptr, this->ioreg);
1084 printk(KERN_ERR "Error programming oob data\n");
1085 /* There was an error */
1095 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
1096 struct mtd_oob_ops *ops)
1098 struct DiskOnChip *this = mtd->priv;
1101 BUG_ON(ops->mode != MTD_OOB_PLACE);
1103 mutex_lock(&this->lock);
1104 ret = doc_write_oob_nolock(mtd, ofs + ops->ooboffs, ops->len,
1105 &ops->retlen, ops->oobbuf);
1107 mutex_unlock(&this->lock);
1111 static int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
1113 struct DiskOnChip *this = mtd->priv;
1114 __u32 ofs = instr->addr;
1115 __u32 len = instr->len;
1117 void __iomem *docptr = this->virtadr;
1118 struct Nand *mychip;
1121 mutex_lock(&this->lock);
1123 if (ofs & (mtd->erasesize-1) || len & (mtd->erasesize-1)) {
1124 mutex_unlock(&this->lock);
1128 instr->state = MTD_ERASING;
1130 /* FIXME: Do this in the background. Use timers or schedule_task() */
1132 mychip = &this->chips[ofs >> this->chipshift];
1134 if (this->curfloor != mychip->floor) {
1135 DoC_SelectFloor(this, mychip->floor);
1136 DoC_SelectChip(this, mychip->chip);
1137 } else if (this->curchip != mychip->chip) {
1138 DoC_SelectChip(this, mychip->chip);
1140 this->curfloor = mychip->floor;
1141 this->curchip = mychip->chip;
1143 DoC_Command(this, NAND_CMD_ERASE1, 0);
1144 DoC_Address(this, ADDR_PAGE, ofs, 0, 0);
1145 DoC_Command(this, NAND_CMD_ERASE2, 0);
1147 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
1149 if (DoC_is_Millennium(this)) {
1150 ReadDOC(docptr, ReadPipeInit);
1151 status = ReadDOC(docptr, LastDataRead);
1153 dummy = ReadDOC(docptr, CDSNSlowIO);
1155 status = ReadDOC_(docptr, this->ioreg);
1159 printk(KERN_ERR "Error erasing at 0x%x\n", ofs);
1160 /* There was an error */
1161 instr->state = MTD_ERASE_FAILED;
1164 ofs += mtd->erasesize;
1165 len -= mtd->erasesize;
1167 instr->state = MTD_ERASE_DONE;
1170 mtd_erase_callback(instr);
1172 mutex_unlock(&this->lock);
1177 /****************************************************************************
1181 ****************************************************************************/
1183 static void __exit cleanup_doc2000(void)
1185 struct mtd_info *mtd;
1186 struct DiskOnChip *this;
1188 while ((mtd = doc2klist)) {
1190 doc2klist = this->nextdoc;
1192 del_mtd_device(mtd);
1194 iounmap(this->virtadr);
1200 module_exit(cleanup_doc2000);
1202 MODULE_LICENSE("GPL");
1203 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
1204 MODULE_DESCRIPTION("MTD driver for DiskOnChip 2000 and Millennium");