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 static const char im_name[] = "DoC2k_init";
522 /* This routine is made available to other mtd code via
523 * inter_module_register. It must only be accessed through
524 * inter_module_get which will bump the use count of this module. The
525 * addresses passed back in mtd are valid as long as the use count of
526 * this module is non-zero, i.e. between inter_module_get and
527 * inter_module_put. Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
529 static void DoC2k_init(struct mtd_info *mtd)
531 struct DiskOnChip *this = mtd->priv;
532 struct DiskOnChip *old = NULL;
535 /* We must avoid being called twice for the same device. */
538 old = doc2klist->priv;
541 if (DoC2k_is_alias(old, this)) {
543 "Ignoring DiskOnChip 2000 at 0x%lX - already configured\n",
545 iounmap(this->virtadr);
550 old = old->nextdoc->priv;
556 switch (this->ChipID) {
557 case DOC_ChipID_Doc2kTSOP:
558 mtd->name = "DiskOnChip 2000 TSOP";
559 this->ioreg = DoC_Mil_CDSN_IO;
560 /* Pretend it's a Millennium */
561 this->ChipID = DOC_ChipID_DocMil;
562 maxchips = MAX_CHIPS;
564 case DOC_ChipID_Doc2k:
565 mtd->name = "DiskOnChip 2000";
566 this->ioreg = DoC_2k_CDSN_IO;
567 maxchips = MAX_CHIPS;
569 case DOC_ChipID_DocMil:
570 mtd->name = "DiskOnChip Millennium";
571 this->ioreg = DoC_Mil_CDSN_IO;
572 maxchips = MAX_CHIPS_MIL;
575 printk("Unknown ChipID 0x%02x\n", this->ChipID);
577 iounmap(this->virtadr);
581 printk(KERN_NOTICE "%s found at address 0x%lX\n", mtd->name,
584 mtd->type = MTD_NANDFLASH;
585 mtd->flags = MTD_CAP_NANDFLASH;
586 mtd->ecctype = MTD_ECC_RS_DiskOnChip;
591 mtd->owner = THIS_MODULE;
592 mtd->erase = doc_erase;
595 mtd->read = doc_read;
596 mtd->write = doc_write;
597 mtd->read_ecc = doc_read_ecc;
598 mtd->write_ecc = doc_write_ecc;
599 mtd->writev_ecc = doc_writev_ecc;
600 mtd->read_oob = doc_read_oob;
601 mtd->write_oob = doc_write_oob;
609 mutex_init(&this->lock);
611 /* Ident all the chips present. */
612 DoC_ScanChips(this, maxchips);
616 iounmap(this->virtadr);
618 this->nextdoc = doc2klist;
620 mtd->size = this->totlen;
621 mtd->erasesize = this->erasesize;
627 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
628 size_t * retlen, u_char * buf)
630 /* Just a special case of doc_read_ecc */
631 return doc_read_ecc(mtd, from, len, retlen, buf, NULL, NULL);
634 static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
635 size_t * retlen, u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel)
637 struct DiskOnChip *this = mtd->priv;
638 void __iomem *docptr = this->virtadr;
640 unsigned char syndrome[6];
642 int i, len256 = 0, ret=0;
645 /* Don't allow read past end of device */
646 if (from >= this->totlen)
649 mutex_lock(&this->lock);
655 /* Don't allow a single read to cross a 512-byte block boundary */
656 if (from + len > ((from | 0x1ff) + 1))
657 len = ((from | 0x1ff) + 1) - from;
659 /* The ECC will not be calculated correctly if less than 512 is read */
660 if (len != 0x200 && eccbuf)
662 "ECC needs a full sector read (adr: %lx size %lx)\n",
663 (long) from, (long) len);
665 /* printk("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len); */
668 /* Find the chip which is to be used and select it */
669 mychip = &this->chips[from >> (this->chipshift)];
671 if (this->curfloor != mychip->floor) {
672 DoC_SelectFloor(this, mychip->floor);
673 DoC_SelectChip(this, mychip->chip);
674 } else if (this->curchip != mychip->chip) {
675 DoC_SelectChip(this, mychip->chip);
678 this->curfloor = mychip->floor;
679 this->curchip = mychip->chip;
683 && (from & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
685 DoC_Address(this, ADDR_COLUMN_PAGE, from, CDSN_CTRL_WP,
689 /* Prime the ECC engine */
690 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
691 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
693 /* disable the ECC engine */
694 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
695 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
698 /* treat crossing 256-byte sector for 2M x 8bits devices */
699 if (this->page256 && from + len > (from | 0xff) + 1) {
700 len256 = (from | 0xff) + 1 - from;
701 DoC_ReadBuf(this, buf, len256);
703 DoC_Command(this, NAND_CMD_READ0, CDSN_CTRL_WP);
704 DoC_Address(this, ADDR_COLUMN_PAGE, from + len256,
705 CDSN_CTRL_WP, CDSN_CTRL_ECC_IO);
708 DoC_ReadBuf(this, &buf[len256], len - len256);
710 /* Let the caller know we completed it */
714 /* Read the ECC data through the DiskOnChip ECC logic */
715 /* Note: this will work even with 2M x 8bit devices as */
716 /* they have 8 bytes of OOB per 256 page. mf. */
717 DoC_ReadBuf(this, eccbuf, 6);
719 /* Flush the pipeline */
720 if (DoC_is_Millennium(this)) {
721 dummy = ReadDOC(docptr, ECCConf);
722 dummy = ReadDOC(docptr, ECCConf);
723 i = ReadDOC(docptr, ECCConf);
725 dummy = ReadDOC(docptr, 2k_ECCStatus);
726 dummy = ReadDOC(docptr, 2k_ECCStatus);
727 i = ReadDOC(docptr, 2k_ECCStatus);
730 /* Check the ECC Status */
733 /* There was an ECC error */
735 printk(KERN_ERR "DiskOnChip ECC Error: Read at %lx\n", (long)from);
737 /* Read the ECC syndrom through the DiskOnChip ECC logic.
738 These syndrome will be all ZERO when there is no error */
739 for (i = 0; i < 6; i++) {
741 ReadDOC(docptr, ECCSyndrome0 + i);
743 nb_errors = doc_decode_ecc(buf, syndrome);
746 printk(KERN_ERR "Errors corrected: %x\n", nb_errors);
749 /* We return error, but have actually done the read. Not that
750 this can be told to user-space, via sys_read(), but at least
751 MTD-aware stuff can know about it by checking *retlen */
757 printk(KERN_DEBUG "ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
758 (long)from, eccbuf[0], eccbuf[1], eccbuf[2],
759 eccbuf[3], eccbuf[4], eccbuf[5]);
762 /* disable the ECC engine */
763 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
766 /* according to 11.4.1, we need to wait for the busy line
767 * drop if we read to the end of the page. */
768 if(0 == ((from + len) & 0x1ff))
778 mutex_unlock(&this->lock);
783 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
784 size_t * retlen, const u_char * buf)
787 return doc_write_ecc(mtd, to, len, retlen, buf, eccbuf, NULL);
790 static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
791 size_t * retlen, const u_char * buf,
792 u_char * eccbuf, struct nand_oobinfo *oobsel)
794 struct DiskOnChip *this = mtd->priv;
795 int di; /* Yes, DI is a hangover from when I was disassembling the binary driver */
796 void __iomem *docptr = this->virtadr;
803 /* Don't allow write past end of device */
804 if (to >= this->totlen)
807 mutex_lock(&this->lock);
813 /* Don't allow a single write to cross a 512-byte block boundary */
814 if (to + len > ((to | 0x1ff) + 1))
815 len = ((to | 0x1ff) + 1) - to;
817 /* The ECC will not be calculated correctly if less than 512 is written */
819 if (len != 0x200 && eccbuf)
821 "ECC needs a full sector write (adr: %lx size %lx)\n",
822 (long) to, (long) len);
825 /* printk("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */
827 /* Find the chip which is to be used and select it */
828 mychip = &this->chips[to >> (this->chipshift)];
830 if (this->curfloor != mychip->floor) {
831 DoC_SelectFloor(this, mychip->floor);
832 DoC_SelectChip(this, mychip->chip);
833 } else if (this->curchip != mychip->chip) {
834 DoC_SelectChip(this, mychip->chip);
837 this->curfloor = mychip->floor;
838 this->curchip = mychip->chip;
840 /* Set device to main plane of flash */
841 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
844 && (to & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
847 DoC_Command(this, NAND_CMD_SEQIN, 0);
848 DoC_Address(this, ADDR_COLUMN_PAGE, to, 0, CDSN_CTRL_ECC_IO);
851 /* Prime the ECC engine */
852 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
853 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
855 /* disable the ECC engine */
856 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
857 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
860 /* treat crossing 256-byte sector for 2M x 8bits devices */
861 if (this->page256 && to + len > (to | 0xff) + 1) {
862 len256 = (to | 0xff) + 1 - to;
863 DoC_WriteBuf(this, buf, len256);
865 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
867 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
868 /* There's an implicit DoC_WaitReady() in DoC_Command */
870 dummy = ReadDOC(docptr, CDSNSlowIO);
873 if (ReadDOC_(docptr, this->ioreg) & 1) {
874 printk(KERN_ERR "Error programming flash\n");
875 /* Error in programming */
877 mutex_unlock(&this->lock);
881 DoC_Command(this, NAND_CMD_SEQIN, 0);
882 DoC_Address(this, ADDR_COLUMN_PAGE, to + len256, 0,
886 DoC_WriteBuf(this, &buf[len256], len - len256);
889 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_CE, docptr,
892 if (DoC_is_Millennium(this)) {
893 WriteDOC(0, docptr, NOP);
894 WriteDOC(0, docptr, NOP);
895 WriteDOC(0, docptr, NOP);
897 WriteDOC_(0, docptr, this->ioreg);
898 WriteDOC_(0, docptr, this->ioreg);
899 WriteDOC_(0, docptr, this->ioreg);
902 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_FLASH_IO | CDSN_CTRL_CE, docptr,
905 /* Read the ECC data through the DiskOnChip ECC logic */
906 for (di = 0; di < 6; di++) {
907 eccbuf[di] = ReadDOC(docptr, ECCSyndrome0 + di);
910 /* Reset the ECC engine */
911 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
915 ("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
916 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
917 eccbuf[4], eccbuf[5]);
921 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
923 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
924 /* There's an implicit DoC_WaitReady() in DoC_Command */
926 if (DoC_is_Millennium(this)) {
927 ReadDOC(docptr, ReadPipeInit);
928 status = ReadDOC(docptr, LastDataRead);
930 dummy = ReadDOC(docptr, CDSNSlowIO);
932 status = ReadDOC_(docptr, this->ioreg);
936 printk(KERN_ERR "Error programming flash\n");
937 /* Error in programming */
939 mutex_unlock(&this->lock);
943 /* Let the caller know we completed it */
951 /* Write the ECC data to flash */
952 for (di=0; di<6; di++)
958 ret = doc_write_oob_nolock(mtd, to, 8, &dummy, x);
960 mutex_unlock(&this->lock);
970 mutex_unlock(&this->lock);
974 static int doc_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs,
975 unsigned long count, loff_t to, size_t *retlen,
976 u_char *eccbuf, struct nand_oobinfo *oobsel)
978 static char static_buf[512];
979 static DEFINE_MUTEX(writev_buf_mutex);
981 size_t totretlen = 0;
982 size_t thisvecofs = 0;
985 mutex_lock(&writev_buf_mutex);
988 size_t thislen, thisretlen;
991 buf = vecs->iov_base + thisvecofs;
992 thislen = vecs->iov_len - thisvecofs;
995 if (thislen >= 512) {
996 thislen = thislen & ~(512-1);
997 thisvecofs += thislen;
999 /* Not enough to fill a page. Copy into buf */
1000 memcpy(static_buf, buf, thislen);
1001 buf = &static_buf[thislen];
1003 while(count && thislen < 512) {
1006 thisvecofs = min((512-thislen), vecs->iov_len);
1007 memcpy(buf, vecs->iov_base, thisvecofs);
1008 thislen += thisvecofs;
1013 if (count && thisvecofs == vecs->iov_len) {
1018 ret = doc_write_ecc(mtd, to, thislen, &thisretlen, buf, eccbuf, oobsel);
1020 totretlen += thisretlen;
1022 if (ret || thisretlen != thislen)
1028 mutex_unlock(&writev_buf_mutex);
1029 *retlen = totretlen;
1034 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
1035 size_t * retlen, u_char * buf)
1037 struct DiskOnChip *this = mtd->priv;
1038 int len256 = 0, ret;
1039 struct Nand *mychip;
1041 mutex_lock(&this->lock);
1043 mychip = &this->chips[ofs >> this->chipshift];
1045 if (this->curfloor != mychip->floor) {
1046 DoC_SelectFloor(this, mychip->floor);
1047 DoC_SelectChip(this, mychip->chip);
1048 } else if (this->curchip != mychip->chip) {
1049 DoC_SelectChip(this, mychip->chip);
1051 this->curfloor = mychip->floor;
1052 this->curchip = mychip->chip;
1054 /* update address for 2M x 8bit devices. OOB starts on the second */
1055 /* page to maintain compatibility with doc_read_ecc. */
1056 if (this->page256) {
1063 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1064 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, CDSN_CTRL_WP, 0);
1066 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1067 /* Note: datasheet says it should automaticaly wrap to the */
1068 /* next OOB block, but it didn't work here. mf. */
1069 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1070 len256 = (ofs | 0x7) + 1 - ofs;
1071 DoC_ReadBuf(this, buf, len256);
1073 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1074 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff),
1078 DoC_ReadBuf(this, &buf[len256], len - len256);
1081 /* Reading the full OOB data drops us off of the end of the page,
1082 * causing the flash device to go into busy mode, so we need
1083 * to wait until ready 11.4.1 and Toshiba TC58256FT docs */
1085 ret = DoC_WaitReady(this);
1087 mutex_unlock(&this->lock);
1092 static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
1093 size_t * retlen, const u_char * buf)
1095 struct DiskOnChip *this = mtd->priv;
1097 void __iomem *docptr = this->virtadr;
1098 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
1102 // 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,
1103 // buf[0], buf[1], buf[2], buf[3], buf[8], buf[9], buf[14],buf[15]);
1105 /* Find the chip which is to be used and select it */
1106 if (this->curfloor != mychip->floor) {
1107 DoC_SelectFloor(this, mychip->floor);
1108 DoC_SelectChip(this, mychip->chip);
1109 } else if (this->curchip != mychip->chip) {
1110 DoC_SelectChip(this, mychip->chip);
1112 this->curfloor = mychip->floor;
1113 this->curchip = mychip->chip;
1115 /* disable the ECC engine */
1116 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
1117 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
1119 /* Reset the chip, see Software Requirement 11.4 item 1. */
1120 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
1122 /* issue the Read2 command to set the pointer to the Spare Data Area. */
1123 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1125 /* update address for 2M x 8bit devices. OOB starts on the second */
1126 /* page to maintain compatibility with doc_read_ecc. */
1127 if (this->page256) {
1134 /* issue the Serial Data In command to initial the Page Program process */
1135 DoC_Command(this, NAND_CMD_SEQIN, 0);
1136 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, 0, 0);
1138 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1139 /* Note: datasheet says it should automaticaly wrap to the */
1140 /* next OOB block, but it didn't work here. mf. */
1141 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1142 len256 = (ofs | 0x7) + 1 - ofs;
1143 DoC_WriteBuf(this, buf, len256);
1145 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1146 DoC_Command(this, NAND_CMD_STATUS, 0);
1147 /* DoC_WaitReady() is implicit in DoC_Command */
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 programming oob data\n");
1160 /* There was an error */
1164 DoC_Command(this, NAND_CMD_SEQIN, 0);
1165 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), 0, 0);
1168 DoC_WriteBuf(this, &buf[len256], len - len256);
1170 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1171 DoC_Command(this, NAND_CMD_STATUS, 0);
1172 /* DoC_WaitReady() is implicit in DoC_Command */
1174 if (DoC_is_Millennium(this)) {
1175 ReadDOC(docptr, ReadPipeInit);
1176 status = ReadDOC(docptr, LastDataRead);
1178 dummy = ReadDOC(docptr, CDSNSlowIO);
1180 status = ReadDOC_(docptr, this->ioreg);
1184 printk(KERN_ERR "Error programming oob data\n");
1185 /* There was an error */
1195 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
1196 size_t * retlen, const u_char * buf)
1198 struct DiskOnChip *this = mtd->priv;
1201 mutex_lock(&this->lock);
1202 ret = doc_write_oob_nolock(mtd, ofs, len, retlen, buf);
1204 mutex_unlock(&this->lock);
1208 static int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
1210 struct DiskOnChip *this = mtd->priv;
1211 __u32 ofs = instr->addr;
1212 __u32 len = instr->len;
1214 void __iomem *docptr = this->virtadr;
1215 struct Nand *mychip;
1218 mutex_lock(&this->lock);
1220 if (ofs & (mtd->erasesize-1) || len & (mtd->erasesize-1)) {
1221 mutex_unlock(&this->lock);
1225 instr->state = MTD_ERASING;
1227 /* FIXME: Do this in the background. Use timers or schedule_task() */
1229 mychip = &this->chips[ofs >> this->chipshift];
1231 if (this->curfloor != mychip->floor) {
1232 DoC_SelectFloor(this, mychip->floor);
1233 DoC_SelectChip(this, mychip->chip);
1234 } else if (this->curchip != mychip->chip) {
1235 DoC_SelectChip(this, mychip->chip);
1237 this->curfloor = mychip->floor;
1238 this->curchip = mychip->chip;
1240 DoC_Command(this, NAND_CMD_ERASE1, 0);
1241 DoC_Address(this, ADDR_PAGE, ofs, 0, 0);
1242 DoC_Command(this, NAND_CMD_ERASE2, 0);
1244 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
1246 if (DoC_is_Millennium(this)) {
1247 ReadDOC(docptr, ReadPipeInit);
1248 status = ReadDOC(docptr, LastDataRead);
1250 dummy = ReadDOC(docptr, CDSNSlowIO);
1252 status = ReadDOC_(docptr, this->ioreg);
1256 printk(KERN_ERR "Error erasing at 0x%x\n", ofs);
1257 /* There was an error */
1258 instr->state = MTD_ERASE_FAILED;
1261 ofs += mtd->erasesize;
1262 len -= mtd->erasesize;
1264 instr->state = MTD_ERASE_DONE;
1267 mtd_erase_callback(instr);
1269 mutex_unlock(&this->lock);
1274 /****************************************************************************
1278 ****************************************************************************/
1280 static int __init init_doc2000(void)
1282 inter_module_register(im_name, THIS_MODULE, &DoC2k_init);
1286 static void __exit cleanup_doc2000(void)
1288 struct mtd_info *mtd;
1289 struct DiskOnChip *this;
1291 while ((mtd = doc2klist)) {
1293 doc2klist = this->nextdoc;
1295 del_mtd_device(mtd);
1297 iounmap(this->virtadr);
1301 inter_module_unregister(im_name);
1304 module_exit(cleanup_doc2000);
1305 module_init(init_doc2000);
1307 MODULE_LICENSE("GPL");
1308 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
1309 MODULE_DESCRIPTION("MTD driver for DiskOnChip 2000 and Millennium");