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.53 2003/06/11 09:45:19 dwmw2 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>
23 #include <linux/mtd/mtd.h>
24 #include <linux/mtd/nand.h>
25 #include <linux/mtd/doc2000.h>
27 #define DOC_SUPPORT_2000
28 #define DOC_SUPPORT_MILLENNIUM
30 #ifdef DOC_SUPPORT_2000
31 #define DoC_is_2000(doc) (doc->ChipID == DOC_ChipID_Doc2k)
33 #define DoC_is_2000(doc) (0)
36 #ifdef DOC_SUPPORT_MILLENNIUM
37 #define DoC_is_Millennium(doc) (doc->ChipID == DOC_ChipID_DocMil)
39 #define DoC_is_Millennium(doc) (0)
42 /* #define ECC_DEBUG */
44 /* I have no idea why some DoC chips can not use memcpy_from|to_io().
45 * This may be due to the different revisions of the ASIC controller built-in or
46 * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
51 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
52 size_t *retlen, u_char *buf);
53 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
54 size_t *retlen, const u_char *buf);
55 static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
56 size_t *retlen, u_char *buf, u_char *eccbuf, int oobsel);
57 static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
58 size_t *retlen, const u_char *buf, u_char *eccbuf, int oobsel);
59 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
60 size_t *retlen, u_char *buf);
61 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
62 size_t *retlen, const u_char *buf);
63 static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
64 size_t *retlen, const u_char *buf);
65 static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
67 static struct mtd_info *doc2klist = NULL;
69 /* Perform the required delay cycles by reading from the appropriate register */
70 static void DoC_Delay(struct DiskOnChip *doc, unsigned short cycles)
75 for (i = 0; i < cycles; i++) {
76 if (DoC_is_Millennium(doc))
77 dummy = ReadDOC(doc->virtadr, NOP);
79 dummy = ReadDOC(doc->virtadr, DOCStatus);
84 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
85 static int _DoC_WaitReady(struct DiskOnChip *doc)
87 unsigned long docptr = doc->virtadr;
88 unsigned long timeo = jiffies + (HZ * 10);
90 DEBUG(MTD_DEBUG_LEVEL3,
91 "_DoC_WaitReady called for out-of-line wait\n");
93 /* Out-of-line routine to wait for chip response */
94 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
95 if (time_after(jiffies, timeo)) {
96 DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
106 static inline int DoC_WaitReady(struct DiskOnChip *doc)
108 unsigned long docptr = doc->virtadr;
109 /* This is inline, to optimise the common case, where it's ready instantly */
112 /* 4 read form NOP register should be issued in prior to the read from CDSNControl
113 see Software Requirement 11.4 item 2. */
116 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
117 /* Call the out-of-line routine to wait */
118 ret = _DoC_WaitReady(doc);
120 /* issue 2 read from NOP register after reading from CDSNControl register
121 see Software Requirement 11.4 item 2. */
127 /* DoC_Command: Send a flash command to the flash chip through the CDSN Slow IO register to
128 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
129 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
131 static inline int DoC_Command(struct DiskOnChip *doc, unsigned char command,
132 unsigned char xtraflags)
134 unsigned long docptr = doc->virtadr;
136 if (DoC_is_2000(doc))
137 xtraflags |= CDSN_CTRL_FLASH_IO;
139 /* Assert the CLE (Command Latch Enable) line to the flash chip */
140 WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
141 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
143 if (DoC_is_Millennium(doc))
144 WriteDOC(command, docptr, CDSNSlowIO);
146 /* Send the command */
147 WriteDOC_(command, docptr, doc->ioreg);
149 /* Lower the CLE line */
150 WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
151 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
153 /* Wait for the chip to respond - Software requirement 11.4.1 (extended for any command) */
154 return DoC_WaitReady(doc);
157 /* DoC_Address: Set the current address for the flash chip through the CDSN Slow IO register to
158 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
159 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
161 static int DoC_Address(struct DiskOnChip *doc, int numbytes, unsigned long ofs,
162 unsigned char xtraflags1, unsigned char xtraflags2)
164 unsigned long docptr;
167 docptr = doc->virtadr;
169 if (DoC_is_2000(doc))
170 xtraflags1 |= CDSN_CTRL_FLASH_IO;
172 /* Assert the ALE (Address Latch Enable) line to the flash chip */
173 WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
175 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
177 /* Send the address */
178 /* Devices with 256-byte page are addressed as:
179 Column (bits 0-7), Page (bits 8-15, 16-23, 24-31)
180 * there is no device on the market with page256
181 and more than 24 bits.
182 Devices with 512-byte page are addressed as:
183 Column (bits 0-7), Page (bits 9-16, 17-24, 25-31)
184 * 25-31 is sent only if the chip support it.
185 * bit 8 changes the read command to be sent
186 (NAND_CMD_READ0 or NAND_CMD_READ1).
189 if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE) {
190 if (DoC_is_Millennium(doc))
191 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
192 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
201 if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE) {
202 for (i = 0; i < doc->pageadrlen; i++, ofs = ofs >> 8) {
203 if (DoC_is_Millennium(doc))
204 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
205 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
209 DoC_Delay(doc, 2); /* Needed for some slow flash chips. mf. */
211 /* FIXME: The SlowIO's for millennium could be replaced by
212 a single WritePipeTerm here. mf. */
214 /* Lower the ALE line */
215 WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr,
218 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
220 /* Wait for the chip to respond - Software requirement 11.4.1 */
221 return DoC_WaitReady(doc);
224 /* Read a buffer from DoC, taking care of Millennium odditys */
225 static void DoC_ReadBuf(struct DiskOnChip *doc, u_char * buf, int len)
228 int modulus = 0xffff;
229 unsigned long docptr;
232 docptr = doc->virtadr;
237 if (DoC_is_Millennium(doc)) {
238 /* Read the data via the internal pipeline through CDSN IO register,
239 see Pipelined Read Operations 11.3 */
240 dummy = ReadDOC(docptr, ReadPipeInit);
242 /* Millennium should use the LastDataRead register - Pipeline Reads */
245 /* This is needed for correctly ECC calculation */
249 for (i = 0; i < len; i++)
250 buf[i] = ReadDOC_(docptr, doc->ioreg + (i & modulus));
252 if (DoC_is_Millennium(doc)) {
253 buf[i] = ReadDOC(docptr, LastDataRead);
257 /* Write a buffer to DoC, taking care of Millennium odditys */
258 static void DoC_WriteBuf(struct DiskOnChip *doc, const u_char * buf, int len)
260 unsigned long docptr;
263 docptr = doc->virtadr;
268 for (i = 0; i < len; i++)
269 WriteDOC_(buf[i], docptr, doc->ioreg + i);
271 if (DoC_is_Millennium(doc)) {
272 WriteDOC(0x00, docptr, WritePipeTerm);
277 /* DoC_SelectChip: Select a given flash chip within the current floor */
279 static inline int DoC_SelectChip(struct DiskOnChip *doc, int chip)
281 unsigned long docptr = doc->virtadr;
283 /* Software requirement 11.4.4 before writing DeviceSelect */
284 /* Deassert the CE line to eliminate glitches on the FCE# outputs */
285 WriteDOC(CDSN_CTRL_WP, docptr, CDSNControl);
286 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
288 /* Select the individual flash chip requested */
289 WriteDOC(chip, docptr, CDSNDeviceSelect);
292 /* Reassert the CE line */
293 WriteDOC(CDSN_CTRL_CE | CDSN_CTRL_FLASH_IO | CDSN_CTRL_WP, docptr,
295 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
297 /* Wait for it to be ready */
298 return DoC_WaitReady(doc);
301 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
303 static inline int DoC_SelectFloor(struct DiskOnChip *doc, int floor)
305 unsigned long docptr = doc->virtadr;
307 /* Select the floor (bank) of chips required */
308 WriteDOC(floor, docptr, FloorSelect);
310 /* Wait for the chip to be ready */
311 return DoC_WaitReady(doc);
314 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
316 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
321 /* Page in the required floor/chip */
322 DoC_SelectFloor(doc, floor);
323 DoC_SelectChip(doc, chip);
326 if (DoC_Command(doc, NAND_CMD_RESET, CDSN_CTRL_WP)) {
327 DEBUG(MTD_DEBUG_LEVEL2,
328 "DoC_Command (reset) for %d,%d returned true\n",
334 /* Read the NAND chip ID: 1. Send ReadID command */
335 if (DoC_Command(doc, NAND_CMD_READID, CDSN_CTRL_WP)) {
336 DEBUG(MTD_DEBUG_LEVEL2,
337 "DoC_Command (ReadID) for %d,%d returned true\n",
342 /* Read the NAND chip ID: 2. Send address byte zero */
343 DoC_Address(doc, ADDR_COLUMN, 0, CDSN_CTRL_WP, 0);
345 /* Read the manufacturer and device id codes from the device */
347 /* CDSN Slow IO register see Software Requirement 11.4 item 5. */
348 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
350 mfr = ReadDOC_(doc->virtadr, doc->ioreg);
352 /* CDSN Slow IO register see Software Requirement 11.4 item 5. */
353 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
355 id = ReadDOC_(doc->virtadr, doc->ioreg);
357 /* No response - return failure */
358 if (mfr == 0xff || mfr == 0)
361 /* Check it's the same as the first chip we identified.
362 * M-Systems say that any given DiskOnChip device should only
363 * contain _one_ type of flash part, although that's not a
364 * hardware restriction. */
366 if (doc->mfr == mfr && doc->id == id)
367 return 1; /* This is another the same the first */
370 "Flash chip at floor %d, chip %d is different:\n",
374 /* Print and store the manufacturer and ID codes. */
375 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
376 if (id == nand_flash_ids[i].id) {
377 /* Try to identify manufacturer */
378 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
379 if (nand_manuf_ids[j].id == mfr)
383 "Flash chip found: Manufacturer ID: %2.2X, "
384 "Chip ID: %2.2X (%s:%s)\n", mfr, id,
385 nand_manuf_ids[j].name, nand_flash_ids[i].name);
390 nand_flash_ids[i].chipshift;
391 doc->page256 = nand_flash_ids[i].page256;
393 nand_flash_ids[i].chipshift > 25 ? 3 : 2;
395 nand_flash_ids[i].erasesize;
403 /* We haven't fully identified the chip. Print as much as we know. */
404 printk(KERN_WARNING "Unknown flash chip found: %2.2X %2.2X\n",
407 printk(KERN_WARNING "Please report to dwmw2@infradead.org\n");
411 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
413 static void DoC_ScanChips(struct DiskOnChip *this)
416 int numchips[MAX_FLOORS];
417 int maxchips = MAX_CHIPS;
424 if (DoC_is_Millennium(this))
425 maxchips = MAX_CHIPS_MIL;
427 /* For each floor, find the number of valid chips it contains */
428 for (floor = 0; floor < MAX_FLOORS; floor++) {
431 for (chip = 0; chip < maxchips && ret != 0; chip++) {
433 ret = DoC_IdentChip(this, floor, chip);
441 /* If there are none at all that we recognise, bail */
442 if (!this->numchips) {
443 printk(KERN_NOTICE "No flash chips recognised.\n");
447 /* Allocate an array to hold the information for each chip */
448 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
450 printk(KERN_NOTICE "No memory for allocating chip info structures\n");
456 /* Fill out the chip array with {floor, chipno} for each
457 * detected chip in the device. */
458 for (floor = 0; floor < MAX_FLOORS; floor++) {
459 for (chip = 0; chip < numchips[floor]; chip++) {
460 this->chips[ret].floor = floor;
461 this->chips[ret].chip = chip;
462 this->chips[ret].curadr = 0;
463 this->chips[ret].curmode = 0x50;
468 /* Calculate and print the total size of the device */
469 this->totlen = this->numchips * (1 << this->chipshift);
471 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
472 this->numchips, this->totlen >> 20);
475 static int DoC2k_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
477 int tmp1, tmp2, retval;
478 if (doc1->physadr == doc2->physadr)
481 /* Use the alias resolution register which was set aside for this
482 * purpose. If it's value is the same on both chips, they might
483 * be the same chip, and we write to one and check for a change in
484 * the other. It's unclear if this register is usuable in the
485 * DoC 2000 (it's in the Millennium docs), but it seems to work. */
486 tmp1 = ReadDOC(doc1->virtadr, AliasResolution);
487 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
491 WriteDOC((tmp1 + 1) % 0xff, doc1->virtadr, AliasResolution);
492 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
493 if (tmp2 == (tmp1 + 1) % 0xff)
498 /* Restore register contents. May not be necessary, but do it just to
500 WriteDOC(tmp1, doc1->virtadr, AliasResolution);
505 static const char im_name[] = "DoC2k_init";
507 /* This routine is made available to other mtd code via
508 * inter_module_register. It must only be accessed through
509 * inter_module_get which will bump the use count of this module. The
510 * addresses passed back in mtd are valid as long as the use count of
511 * this module is non-zero, i.e. between inter_module_get and
512 * inter_module_put. Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
514 static void DoC2k_init(struct mtd_info *mtd)
516 struct DiskOnChip *this = (struct DiskOnChip *) mtd->priv;
517 struct DiskOnChip *old = NULL;
519 /* We must avoid being called twice for the same device. */
522 old = (struct DiskOnChip *) doc2klist->priv;
525 if (DoC2k_is_alias(old, this)) {
527 "Ignoring DiskOnChip 2000 at 0x%lX - already configured\n",
529 iounmap((void *) this->virtadr);
534 old = (struct DiskOnChip *) old->nextdoc->priv;
540 switch (this->ChipID) {
541 case DOC_ChipID_Doc2k:
542 mtd->name = "DiskOnChip 2000";
543 this->ioreg = DoC_2k_CDSN_IO;
545 case DOC_ChipID_DocMil:
546 mtd->name = "DiskOnChip Millennium";
547 this->ioreg = DoC_Mil_CDSN_IO;
551 printk(KERN_NOTICE "%s found at address 0x%lX\n", mtd->name,
554 mtd->type = MTD_NANDFLASH;
555 mtd->flags = MTD_CAP_NANDFLASH;
556 mtd->ecctype = MTD_ECC_RS_DiskOnChip;
561 mtd->owner = THIS_MODULE;
562 mtd->erase = doc_erase;
565 mtd->read = doc_read;
566 mtd->write = doc_write;
567 mtd->read_ecc = doc_read_ecc;
568 mtd->write_ecc = doc_write_ecc;
569 mtd->read_oob = doc_read_oob;
570 mtd->write_oob = doc_write_oob;
578 init_MUTEX(&this->lock);
580 /* Ident all the chips present. */
585 iounmap((void *) this->virtadr);
587 this->nextdoc = doc2klist;
589 mtd->size = this->totlen;
590 mtd->erasesize = this->erasesize;
596 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
597 size_t * retlen, u_char * buf)
599 /* Just a special case of doc_read_ecc */
600 return doc_read_ecc(mtd, from, len, retlen, buf, NULL, 0);
603 static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
604 size_t * retlen, u_char * buf, u_char * eccbuf, int oobsel)
606 struct DiskOnChip *this = (struct DiskOnChip *) mtd->priv;
607 unsigned long docptr;
609 unsigned char syndrome[6];
611 int i, len256 = 0, ret=0;
613 docptr = this->virtadr;
615 /* Don't allow read past end of device */
616 if (from >= this->totlen)
621 /* Don't allow a single read to cross a 512-byte block boundary */
622 if (from + len > ((from | 0x1ff) + 1))
623 len = ((from | 0x1ff) + 1) - from;
625 /* The ECC will not be calculated correctly if less than 512 is read */
626 if (len != 0x200 && eccbuf)
628 "ECC needs a full sector read (adr: %lx size %lx)\n",
629 (long) from, (long) len);
631 /* printk("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len); */
634 /* Find the chip which is to be used and select it */
635 mychip = &this->chips[from >> (this->chipshift)];
637 if (this->curfloor != mychip->floor) {
638 DoC_SelectFloor(this, mychip->floor);
639 DoC_SelectChip(this, mychip->chip);
640 } else if (this->curchip != mychip->chip) {
641 DoC_SelectChip(this, mychip->chip);
644 this->curfloor = mychip->floor;
645 this->curchip = mychip->chip;
649 && (from & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
651 DoC_Address(this, ADDR_COLUMN_PAGE, from, CDSN_CTRL_WP,
655 /* Prime the ECC engine */
656 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
657 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
659 /* disable the ECC engine */
660 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
661 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
664 /* treat crossing 256-byte sector for 2M x 8bits devices */
665 if (this->page256 && from + len > (from | 0xff) + 1) {
666 len256 = (from | 0xff) + 1 - from;
667 DoC_ReadBuf(this, buf, len256);
669 DoC_Command(this, NAND_CMD_READ0, CDSN_CTRL_WP);
670 DoC_Address(this, ADDR_COLUMN_PAGE, from + len256,
671 CDSN_CTRL_WP, CDSN_CTRL_ECC_IO);
674 DoC_ReadBuf(this, &buf[len256], len - len256);
676 /* Let the caller know we completed it */
680 /* Read the ECC data through the DiskOnChip ECC logic */
681 /* Note: this will work even with 2M x 8bit devices as */
682 /* they have 8 bytes of OOB per 256 page. mf. */
683 DoC_ReadBuf(this, eccbuf, 6);
685 /* Flush the pipeline */
686 if (DoC_is_Millennium(this)) {
687 dummy = ReadDOC(docptr, ECCConf);
688 dummy = ReadDOC(docptr, ECCConf);
689 i = ReadDOC(docptr, ECCConf);
691 dummy = ReadDOC(docptr, 2k_ECCStatus);
692 dummy = ReadDOC(docptr, 2k_ECCStatus);
693 i = ReadDOC(docptr, 2k_ECCStatus);
696 /* Check the ECC Status */
699 /* There was an ECC error */
701 printk(KERN_ERR "DiskOnChip ECC Error: Read at %lx\n", (long)from);
703 /* Read the ECC syndrom through the DiskOnChip ECC logic.
704 These syndrome will be all ZERO when there is no error */
705 for (i = 0; i < 6; i++) {
707 ReadDOC(docptr, ECCSyndrome0 + i);
709 nb_errors = doc_decode_ecc(buf, syndrome);
712 printk(KERN_ERR "Errors corrected: %x\n", nb_errors);
715 /* We return error, but have actually done the read. Not that
716 this can be told to user-space, via sys_read(), but at least
717 MTD-aware stuff can know about it by checking *retlen */
723 printk(KERN_DEBUG "ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
724 (long)from, eccbuf[0], eccbuf[1], eccbuf[2],
725 eccbuf[3], eccbuf[4], eccbuf[5]);
728 /* disable the ECC engine */
729 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
732 /* according to 11.4.1, we need to wait for the busy line
733 * drop if we read to the end of the page. */
734 if(0 == ((from + *retlen) & 0x1ff))
744 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
745 size_t * retlen, const u_char * buf)
748 return doc_write_ecc(mtd, to, len, retlen, buf, eccbuf, 0);
751 static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
752 size_t * retlen, const u_char * buf,
753 u_char * eccbuf, int oobsel)
755 struct DiskOnChip *this = (struct DiskOnChip *) mtd->priv;
756 int di; /* Yes, DI is a hangover from when I was disassembling the binary driver */
757 unsigned long docptr;
762 docptr = this->virtadr;
764 /* Don't allow write past end of device */
765 if (to >= this->totlen)
770 /* Don't allow a single write to cross a 512-byte block boundary */
771 if (to + len > ((to | 0x1ff) + 1))
772 len = ((to | 0x1ff) + 1) - to;
774 /* The ECC will not be calculated correctly if less than 512 is written */
775 if (len != 0x200 && eccbuf)
777 "ECC needs a full sector write (adr: %lx size %lx)\n",
778 (long) to, (long) len);
780 /* printk("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */
782 /* Find the chip which is to be used and select it */
783 mychip = &this->chips[to >> (this->chipshift)];
785 if (this->curfloor != mychip->floor) {
786 DoC_SelectFloor(this, mychip->floor);
787 DoC_SelectChip(this, mychip->chip);
788 } else if (this->curchip != mychip->chip) {
789 DoC_SelectChip(this, mychip->chip);
792 this->curfloor = mychip->floor;
793 this->curchip = mychip->chip;
795 /* Set device to main plane of flash */
796 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
799 && (to & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
802 DoC_Command(this, NAND_CMD_SEQIN, 0);
803 DoC_Address(this, ADDR_COLUMN_PAGE, to, 0, CDSN_CTRL_ECC_IO);
806 /* Prime the ECC engine */
807 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
808 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
810 /* disable the ECC engine */
811 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
812 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
815 /* treat crossing 256-byte sector for 2M x 8bits devices */
816 if (this->page256 && to + len > (to | 0xff) + 1) {
817 len256 = (to | 0xff) + 1 - to;
818 DoC_WriteBuf(this, buf, len256);
820 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
822 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
823 /* There's an implicit DoC_WaitReady() in DoC_Command */
825 dummy = ReadDOC(docptr, CDSNSlowIO);
828 if (ReadDOC_(docptr, this->ioreg) & 1) {
829 printk(KERN_ERR "Error programming flash\n");
830 /* Error in programming */
836 DoC_Command(this, NAND_CMD_SEQIN, 0);
837 DoC_Address(this, ADDR_COLUMN_PAGE, to + len256, 0,
841 DoC_WriteBuf(this, &buf[len256], len - len256);
844 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_CE, docptr,
847 if (DoC_is_Millennium(this)) {
848 WriteDOC(0, docptr, NOP);
849 WriteDOC(0, docptr, NOP);
850 WriteDOC(0, docptr, NOP);
852 WriteDOC_(0, docptr, this->ioreg);
853 WriteDOC_(0, docptr, this->ioreg);
854 WriteDOC_(0, docptr, this->ioreg);
857 /* Read the ECC data through the DiskOnChip ECC logic */
858 for (di = 0; di < 6; di++) {
859 eccbuf[di] = ReadDOC(docptr, ECCSyndrome0 + di);
862 /* Reset the ECC engine */
863 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
867 ("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
868 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
869 eccbuf[4], eccbuf[5]);
873 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
875 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
876 /* There's an implicit DoC_WaitReady() in DoC_Command */
878 dummy = ReadDOC(docptr, CDSNSlowIO);
881 if (ReadDOC_(docptr, this->ioreg) & 1) {
882 printk(KERN_ERR "Error programming flash\n");
883 /* Error in programming */
889 /* Let the caller know we completed it */
897 /* Write the ECC data to flash */
898 for (di=0; di<6; di++)
904 ret = doc_write_oob_nolock(mtd, to, 8, &dummy, x);
912 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
913 size_t * retlen, u_char * buf)
915 struct DiskOnChip *this = (struct DiskOnChip *) mtd->priv;
917 unsigned long docptr;
922 docptr = this->virtadr;
924 mychip = &this->chips[ofs >> this->chipshift];
926 if (this->curfloor != mychip->floor) {
927 DoC_SelectFloor(this, mychip->floor);
928 DoC_SelectChip(this, mychip->chip);
929 } else if (this->curchip != mychip->chip) {
930 DoC_SelectChip(this, mychip->chip);
932 this->curfloor = mychip->floor;
933 this->curchip = mychip->chip;
935 /* update address for 2M x 8bit devices. OOB starts on the second */
936 /* page to maintain compatibility with doc_read_ecc. */
944 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
945 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, CDSN_CTRL_WP, 0);
947 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
948 /* Note: datasheet says it should automaticaly wrap to the */
949 /* next OOB block, but it didn't work here. mf. */
950 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
951 len256 = (ofs | 0x7) + 1 - ofs;
952 DoC_ReadBuf(this, buf, len256);
954 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
955 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff),
959 DoC_ReadBuf(this, &buf[len256], len - len256);
962 /* Reading the full OOB data drops us off of the end of the page,
963 * causing the flash device to go into busy mode, so we need
964 * to wait until ready 11.4.1 and Toshiba TC58256FT docs */
966 ret = DoC_WaitReady(this);
973 static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
974 size_t * retlen, const u_char * buf)
976 struct DiskOnChip *this = (struct DiskOnChip *) mtd->priv;
978 unsigned long docptr = this->virtadr;
979 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
982 // 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,
983 // buf[0], buf[1], buf[2], buf[3], buf[8], buf[9], buf[14],buf[15]);
985 /* Find the chip which is to be used and select it */
986 if (this->curfloor != mychip->floor) {
987 DoC_SelectFloor(this, mychip->floor);
988 DoC_SelectChip(this, mychip->chip);
989 } else if (this->curchip != mychip->chip) {
990 DoC_SelectChip(this, mychip->chip);
992 this->curfloor = mychip->floor;
993 this->curchip = mychip->chip;
995 /* disable the ECC engine */
996 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
997 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
999 /* Reset the chip, see Software Requirement 11.4 item 1. */
1000 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
1002 /* issue the Read2 command to set the pointer to the Spare Data Area. */
1003 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1005 /* update address for 2M x 8bit devices. OOB starts on the second */
1006 /* page to maintain compatibility with doc_read_ecc. */
1007 if (this->page256) {
1014 /* issue the Serial Data In command to initial the Page Program process */
1015 DoC_Command(this, NAND_CMD_SEQIN, 0);
1016 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, 0, 0);
1018 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1019 /* Note: datasheet says it should automaticaly wrap to the */
1020 /* next OOB block, but it didn't work here. mf. */
1021 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1022 len256 = (ofs | 0x7) + 1 - ofs;
1023 DoC_WriteBuf(this, buf, len256);
1025 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1026 DoC_Command(this, NAND_CMD_STATUS, 0);
1027 /* DoC_WaitReady() is implicit in DoC_Command */
1029 dummy = ReadDOC(docptr, CDSNSlowIO);
1032 if (ReadDOC_(docptr, this->ioreg) & 1) {
1033 printk(KERN_ERR "Error programming oob data\n");
1034 /* There was an error */
1038 DoC_Command(this, NAND_CMD_SEQIN, 0);
1039 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), 0, 0);
1042 DoC_WriteBuf(this, &buf[len256], len - len256);
1044 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1045 DoC_Command(this, NAND_CMD_STATUS, 0);
1046 /* DoC_WaitReady() is implicit in DoC_Command */
1048 dummy = ReadDOC(docptr, CDSNSlowIO);
1051 if (ReadDOC_(docptr, this->ioreg) & 1) {
1052 printk(KERN_ERR "Error programming oob data\n");
1053 /* There was an error */
1063 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
1064 size_t * retlen, const u_char * buf)
1066 struct DiskOnChip *this = (struct DiskOnChip *) mtd->priv;
1070 ret = doc_write_oob_nolock(mtd, ofs, len, retlen, buf);
1076 static int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
1078 struct DiskOnChip *this = (struct DiskOnChip *) mtd->priv;
1079 __u32 ofs = instr->addr;
1080 __u32 len = instr->len;
1082 unsigned long docptr;
1083 struct Nand *mychip;
1087 if (ofs & (mtd->erasesize-1) || len & (mtd->erasesize-1)) {
1092 instr->state = MTD_ERASING;
1094 docptr = this->virtadr;
1096 /* FIXME: Do this in the background. Use timers or schedule_task() */
1098 mychip = &this->chips[ofs >> this->chipshift];
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 DoC_Command(this, NAND_CMD_ERASE1, 0);
1110 DoC_Address(this, ADDR_PAGE, ofs, 0, 0);
1111 DoC_Command(this, NAND_CMD_ERASE2, 0);
1113 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
1115 dummy = ReadDOC(docptr, CDSNSlowIO);
1118 if (ReadDOC_(docptr, this->ioreg) & 1) {
1119 printk(KERN_ERR "Error erasing at 0x%x\n", ofs);
1120 /* There was an error */
1121 instr->state = MTD_ERASE_FAILED;
1124 ofs += mtd->erasesize;
1125 len -= mtd->erasesize;
1127 instr->state = MTD_ERASE_DONE;
1130 if (instr->callback)
1131 instr->callback(instr);
1138 /****************************************************************************
1142 ****************************************************************************/
1144 int __init init_doc2000(void)
1146 inter_module_register(im_name, THIS_MODULE, &DoC2k_init);
1150 static void __exit cleanup_doc2000(void)
1152 struct mtd_info *mtd;
1153 struct DiskOnChip *this;
1155 while ((mtd = doc2klist)) {
1156 this = (struct DiskOnChip *) mtd->priv;
1157 doc2klist = this->nextdoc;
1159 del_mtd_device(mtd);
1161 iounmap((void *) this->virtadr);
1165 inter_module_unregister(im_name);
1168 module_exit(cleanup_doc2000);
1169 module_init(init_doc2000);
1171 MODULE_LICENSE("GPL");
1172 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
1173 MODULE_DESCRIPTION("MTD driver for DiskOnChip 2000 and Millennium");