On Tue, Nov 06, 2007 at 02:33:53AM -0800, akpm@linux-foundation.org wrote:
[mmotm.git] / drivers / mtd / devices / doc2000.c
blob5bf5f460e1327828c203710ffee7041044fb14b2
2 /*
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>
6 */
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <asm/errno.h>
11 #include <asm/io.h>
12 #include <asm/uaccess.h>
13 #include <linux/delay.h>
14 #include <linux/slab.h>
15 #include <linux/sched.h>
16 #include <linux/init.h>
17 #include <linux/types.h>
18 #include <linux/bitops.h>
19 #include <linux/mutex.h>
21 #include <linux/mtd/mtd.h>
22 #include <linux/mtd/nand.h>
23 #include <linux/mtd/doc2000.h>
25 #define DOC_SUPPORT_2000
26 #define DOC_SUPPORT_2000TSOP
27 #define DOC_SUPPORT_MILLENNIUM
29 #ifdef DOC_SUPPORT_2000
30 #define DoC_is_2000(doc) (doc->ChipID == DOC_ChipID_Doc2k)
31 #else
32 #define DoC_is_2000(doc) (0)
33 #endif
35 #if defined(DOC_SUPPORT_2000TSOP) || defined(DOC_SUPPORT_MILLENNIUM)
36 #define DoC_is_Millennium(doc) (doc->ChipID == DOC_ChipID_DocMil)
37 #else
38 #define DoC_is_Millennium(doc) (0)
39 #endif
41 /* #define ECC_DEBUG */
43 /* I have no idea why some DoC chips can not use memcpy_from|to_io().
44 * This may be due to the different revisions of the ASIC controller built-in or
45 * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
46 * this:
47 #undef USE_MEMCPY
50 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
51 size_t *retlen, u_char *buf);
52 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
53 size_t *retlen, const u_char *buf);
54 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
55 struct mtd_oob_ops *ops);
56 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
57 struct mtd_oob_ops *ops);
58 static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
59 size_t *retlen, const u_char *buf);
60 static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
62 static struct mtd_info *doc2klist = NULL;
64 /* Perform the required delay cycles by reading from the appropriate register */
65 static void DoC_Delay(struct DiskOnChip *doc, unsigned short cycles)
67 volatile char dummy;
68 int i;
70 for (i = 0; i < cycles; i++) {
71 if (DoC_is_Millennium(doc))
72 dummy = ReadDOC(doc->virtadr, NOP);
73 else
74 dummy = ReadDOC(doc->virtadr, DOCStatus);
79 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
80 static int _DoC_WaitReady(struct DiskOnChip *doc)
82 void __iomem *docptr = doc->virtadr;
83 unsigned long timeo = jiffies + (HZ * 10);
85 DEBUG(MTD_DEBUG_LEVEL3,
86 "_DoC_WaitReady called for out-of-line wait\n");
88 /* Out-of-line routine to wait for chip response */
89 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
90 /* issue 2 read from NOP register after reading from CDSNControl register
91 see Software Requirement 11.4 item 2. */
92 DoC_Delay(doc, 2);
94 if (time_after(jiffies, timeo)) {
95 DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
96 return -EIO;
98 udelay(1);
99 cond_resched();
102 return 0;
105 static inline int DoC_WaitReady(struct DiskOnChip *doc)
107 void __iomem *docptr = doc->virtadr;
109 /* This is inline, to optimise the common case, where it's ready instantly */
110 int ret = 0;
112 /* 4 read form NOP register should be issued in prior to the read from CDSNControl
113 see Software Requirement 11.4 item 2. */
114 DoC_Delay(doc, 4);
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. */
122 DoC_Delay(doc, 2);
124 return ret;
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 int DoC_Command(struct DiskOnChip *doc, unsigned char command,
132 unsigned char xtraflags)
134 void __iomem *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);
148 if (DoC_is_Millennium(doc))
149 WriteDOC(command, docptr, WritePipeTerm);
151 /* Lower the CLE line */
152 WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
153 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
155 /* Wait for the chip to respond - Software requirement 11.4.1 (extended for any command) */
156 return DoC_WaitReady(doc);
159 /* DoC_Address: Set the current address for the flash chip through the CDSN Slow IO register to
160 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
161 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
163 static int DoC_Address(struct DiskOnChip *doc, int numbytes, unsigned long ofs,
164 unsigned char xtraflags1, unsigned char xtraflags2)
166 int i;
167 void __iomem *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);
195 if (doc->page256) {
196 ofs = ofs >> 8;
197 } else {
198 ofs = ofs >> 9;
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 if (DoC_is_Millennium(doc))
210 WriteDOC(ofs & 0xff, docptr, WritePipeTerm);
212 DoC_Delay(doc, 2); /* Needed for some slow flash chips. mf. */
214 /* FIXME: The SlowIO's for millennium could be replaced by
215 a single WritePipeTerm here. mf. */
217 /* Lower the ALE line */
218 WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr,
219 CDSNControl);
221 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
223 /* Wait for the chip to respond - Software requirement 11.4.1 */
224 return DoC_WaitReady(doc);
227 /* Read a buffer from DoC, taking care of Millennium odditys */
228 static void DoC_ReadBuf(struct DiskOnChip *doc, u_char * buf, int len)
230 volatile int dummy;
231 int modulus = 0xffff;
232 void __iomem *docptr = doc->virtadr;
233 int i;
235 if (len <= 0)
236 return;
238 if (DoC_is_Millennium(doc)) {
239 /* Read the data via the internal pipeline through CDSN IO register,
240 see Pipelined Read Operations 11.3 */
241 dummy = ReadDOC(docptr, ReadPipeInit);
243 /* Millennium should use the LastDataRead register - Pipeline Reads */
244 len--;
246 /* This is needed for correctly ECC calculation */
247 modulus = 0xff;
250 for (i = 0; i < len; i++)
251 buf[i] = ReadDOC_(docptr, doc->ioreg + (i & modulus));
253 if (DoC_is_Millennium(doc)) {
254 buf[i] = ReadDOC(docptr, LastDataRead);
258 /* Write a buffer to DoC, taking care of Millennium odditys */
259 static void DoC_WriteBuf(struct DiskOnChip *doc, const u_char * buf, int len)
261 void __iomem *docptr = doc->virtadr;
262 int i;
264 if (len <= 0)
265 return;
267 for (i = 0; i < len; i++)
268 WriteDOC_(buf[i], docptr, doc->ioreg + i);
270 if (DoC_is_Millennium(doc)) {
271 WriteDOC(0x00, docptr, WritePipeTerm);
276 /* DoC_SelectChip: Select a given flash chip within the current floor */
278 static inline int DoC_SelectChip(struct DiskOnChip *doc, int chip)
280 void __iomem *docptr = doc->virtadr;
282 /* Software requirement 11.4.4 before writing DeviceSelect */
283 /* Deassert the CE line to eliminate glitches on the FCE# outputs */
284 WriteDOC(CDSN_CTRL_WP, docptr, CDSNControl);
285 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
287 /* Select the individual flash chip requested */
288 WriteDOC(chip, docptr, CDSNDeviceSelect);
289 DoC_Delay(doc, 4);
291 /* Reassert the CE line */
292 WriteDOC(CDSN_CTRL_CE | CDSN_CTRL_FLASH_IO | CDSN_CTRL_WP, docptr,
293 CDSNControl);
294 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
296 /* Wait for it to be ready */
297 return DoC_WaitReady(doc);
300 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
302 static inline int DoC_SelectFloor(struct DiskOnChip *doc, int floor)
304 void __iomem *docptr = doc->virtadr;
306 /* Select the floor (bank) of chips required */
307 WriteDOC(floor, docptr, FloorSelect);
309 /* Wait for the chip to be ready */
310 return DoC_WaitReady(doc);
313 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
315 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
317 int mfr, id, i, j;
318 volatile char dummy;
320 /* Page in the required floor/chip */
321 DoC_SelectFloor(doc, floor);
322 DoC_SelectChip(doc, chip);
324 /* Reset the chip */
325 if (DoC_Command(doc, NAND_CMD_RESET, CDSN_CTRL_WP)) {
326 DEBUG(MTD_DEBUG_LEVEL2,
327 "DoC_Command (reset) for %d,%d returned true\n",
328 floor, chip);
329 return 0;
333 /* Read the NAND chip ID: 1. Send ReadID command */
334 if (DoC_Command(doc, NAND_CMD_READID, CDSN_CTRL_WP)) {
335 DEBUG(MTD_DEBUG_LEVEL2,
336 "DoC_Command (ReadID) for %d,%d returned true\n",
337 floor, chip);
338 return 0;
341 /* Read the NAND chip ID: 2. Send address byte zero */
342 DoC_Address(doc, ADDR_COLUMN, 0, CDSN_CTRL_WP, 0);
344 /* Read the manufacturer and device id codes from the device */
346 if (DoC_is_Millennium(doc)) {
347 DoC_Delay(doc, 2);
348 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
349 mfr = ReadDOC(doc->virtadr, LastDataRead);
351 DoC_Delay(doc, 2);
352 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
353 id = ReadDOC(doc->virtadr, LastDataRead);
354 } else {
355 /* CDSN Slow IO register see Software Req 11.4 item 5. */
356 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
357 DoC_Delay(doc, 2);
358 mfr = ReadDOC_(doc->virtadr, doc->ioreg);
360 /* CDSN Slow IO register see Software Req 11.4 item 5. */
361 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
362 DoC_Delay(doc, 2);
363 id = ReadDOC_(doc->virtadr, doc->ioreg);
366 /* No response - return failure */
367 if (mfr == 0xff || mfr == 0)
368 return 0;
370 /* Check it's the same as the first chip we identified.
371 * M-Systems say that any given DiskOnChip device should only
372 * contain _one_ type of flash part, although that's not a
373 * hardware restriction. */
374 if (doc->mfr) {
375 if (doc->mfr == mfr && doc->id == id)
376 return 1; /* This is the same as the first */
377 else
378 printk(KERN_WARNING
379 "Flash chip at floor %d, chip %d is different:\n",
380 floor, chip);
383 /* Print and store the manufacturer and ID codes. */
384 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
385 if (id == nand_flash_ids[i].id) {
386 /* Try to identify manufacturer */
387 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
388 if (nand_manuf_ids[j].id == mfr)
389 break;
391 printk(KERN_INFO
392 "Flash chip found: Manufacturer ID: %2.2X, "
393 "Chip ID: %2.2X (%s:%s)\n", mfr, id,
394 nand_manuf_ids[j].name, nand_flash_ids[i].name);
395 if (!doc->mfr) {
396 doc->mfr = mfr;
397 doc->id = id;
398 doc->chipshift =
399 ffs((nand_flash_ids[i].chipsize << 20)) - 1;
400 doc->page256 = (nand_flash_ids[i].pagesize == 256) ? 1 : 0;
401 doc->pageadrlen = doc->chipshift > 25 ? 3 : 2;
402 doc->erasesize =
403 nand_flash_ids[i].erasesize;
404 return 1;
406 return 0;
411 /* We haven't fully identified the chip. Print as much as we know. */
412 printk(KERN_WARNING "Unknown flash chip found: %2.2X %2.2X\n",
413 id, mfr);
415 printk(KERN_WARNING "Please report to dwmw2@infradead.org\n");
416 return 0;
419 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
421 static void DoC_ScanChips(struct DiskOnChip *this, int maxchips)
423 int floor, chip;
424 int numchips[MAX_FLOORS];
425 int ret = 1;
427 this->numchips = 0;
428 this->mfr = 0;
429 this->id = 0;
431 /* For each floor, find the number of valid chips it contains */
432 for (floor = 0; floor < MAX_FLOORS; floor++) {
433 ret = 1;
434 numchips[floor] = 0;
435 for (chip = 0; chip < maxchips && ret != 0; chip++) {
437 ret = DoC_IdentChip(this, floor, chip);
438 if (ret) {
439 numchips[floor]++;
440 this->numchips++;
445 /* If there are none at all that we recognise, bail */
446 if (!this->numchips) {
447 printk(KERN_NOTICE "No flash chips recognised.\n");
448 return;
451 /* Allocate an array to hold the information for each chip */
452 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
453 if (!this->chips) {
454 printk(KERN_NOTICE "No memory for allocating chip info structures\n");
455 return;
458 ret = 0;
460 /* Fill out the chip array with {floor, chipno} for each
461 * detected chip in the device. */
462 for (floor = 0; floor < MAX_FLOORS; floor++) {
463 for (chip = 0; chip < numchips[floor]; chip++) {
464 this->chips[ret].floor = floor;
465 this->chips[ret].chip = chip;
466 this->chips[ret].curadr = 0;
467 this->chips[ret].curmode = 0x50;
468 ret++;
472 /* Calculate and print the total size of the device */
473 this->totlen = this->numchips * (1 << this->chipshift);
475 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
476 this->numchips, this->totlen >> 20);
479 static int DoC2k_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
481 int tmp1, tmp2, retval;
482 if (doc1->physadr == doc2->physadr)
483 return 1;
485 /* Use the alias resolution register which was set aside for this
486 * purpose. If it's value is the same on both chips, they might
487 * be the same chip, and we write to one and check for a change in
488 * the other. It's unclear if this register is usuable in the
489 * DoC 2000 (it's in the Millennium docs), but it seems to work. */
490 tmp1 = ReadDOC(doc1->virtadr, AliasResolution);
491 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
492 if (tmp1 != tmp2)
493 return 0;
495 WriteDOC((tmp1 + 1) % 0xff, doc1->virtadr, AliasResolution);
496 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
497 if (tmp2 == (tmp1 + 1) % 0xff)
498 retval = 1;
499 else
500 retval = 0;
502 /* Restore register contents. May not be necessary, but do it just to
503 * be safe. */
504 WriteDOC(tmp1, doc1->virtadr, AliasResolution);
506 return retval;
509 /* This routine is found from the docprobe code by symbol_get(),
510 * which will bump the use count of this module. */
511 void DoC2k_init(struct mtd_info *mtd)
513 struct DiskOnChip *this = mtd->priv;
514 struct DiskOnChip *old = NULL;
515 int maxchips;
517 /* We must avoid being called twice for the same device. */
519 if (doc2klist)
520 old = doc2klist->priv;
522 while (old) {
523 if (DoC2k_is_alias(old, this)) {
524 printk(KERN_NOTICE
525 "Ignoring DiskOnChip 2000 at 0x%lX - already configured\n",
526 this->physadr);
527 iounmap(this->virtadr);
528 kfree(mtd);
529 return;
531 if (old->nextdoc)
532 old = old->nextdoc->priv;
533 else
534 old = NULL;
538 switch (this->ChipID) {
539 case DOC_ChipID_Doc2kTSOP:
540 mtd->name = "DiskOnChip 2000 TSOP";
541 this->ioreg = DoC_Mil_CDSN_IO;
542 /* Pretend it's a Millennium */
543 this->ChipID = DOC_ChipID_DocMil;
544 maxchips = MAX_CHIPS;
545 break;
546 case DOC_ChipID_Doc2k:
547 mtd->name = "DiskOnChip 2000";
548 this->ioreg = DoC_2k_CDSN_IO;
549 maxchips = MAX_CHIPS;
550 break;
551 case DOC_ChipID_DocMil:
552 mtd->name = "DiskOnChip Millennium";
553 this->ioreg = DoC_Mil_CDSN_IO;
554 maxchips = MAX_CHIPS_MIL;
555 break;
556 default:
557 printk("Unknown ChipID 0x%02x\n", this->ChipID);
558 kfree(mtd);
559 iounmap(this->virtadr);
560 return;
563 printk(KERN_NOTICE "%s found at address 0x%lX\n", mtd->name,
564 this->physadr);
566 mtd->type = MTD_NANDFLASH;
567 mtd->flags = MTD_CAP_NANDFLASH;
568 mtd->size = 0;
569 mtd->erasesize = 0;
570 mtd->writesize = 512;
571 mtd->oobsize = 16;
572 mtd->owner = THIS_MODULE;
573 mtd->erase = doc_erase;
574 mtd->point = NULL;
575 mtd->unpoint = NULL;
576 mtd->read = doc_read;
577 mtd->write = doc_write;
578 mtd->read_oob = doc_read_oob;
579 mtd->write_oob = doc_write_oob;
580 mtd->sync = NULL;
582 this->totlen = 0;
583 this->numchips = 0;
585 this->curfloor = -1;
586 this->curchip = -1;
587 mutex_init(&this->lock);
589 /* Ident all the chips present. */
590 DoC_ScanChips(this, maxchips);
592 if (!this->totlen) {
593 kfree(mtd);
594 iounmap(this->virtadr);
595 } else {
596 this->nextdoc = doc2klist;
597 doc2klist = mtd;
598 mtd->size = this->totlen;
599 mtd->erasesize = this->erasesize;
600 add_mtd_device(mtd);
601 return;
604 EXPORT_SYMBOL_GPL(DoC2k_init);
606 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
607 size_t * retlen, u_char * buf)
609 struct DiskOnChip *this = mtd->priv;
610 void __iomem *docptr = this->virtadr;
611 struct Nand *mychip;
612 unsigned char syndrome[6], eccbuf[6];
613 volatile char dummy;
614 int i, len256 = 0, ret=0;
615 size_t left = len;
617 /* Don't allow read past end of device */
618 if (from >= this->totlen)
619 return -EINVAL;
621 mutex_lock(&this->lock);
623 *retlen = 0;
624 while (left) {
625 len = left;
627 /* Don't allow a single read to cross a 512-byte block boundary */
628 if (from + len > ((from | 0x1ff) + 1))
629 len = ((from | 0x1ff) + 1) - from;
631 /* The ECC will not be calculated correctly if less than 512 is read */
632 if (len != 0x200)
633 printk(KERN_WARNING
634 "ECC needs a full sector read (adr: %lx size %lx)\n",
635 (long) from, (long) len);
637 /* printk("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len); */
640 /* Find the chip which is to be used and select it */
641 mychip = &this->chips[from >> (this->chipshift)];
643 if (this->curfloor != mychip->floor) {
644 DoC_SelectFloor(this, mychip->floor);
645 DoC_SelectChip(this, mychip->chip);
646 } else if (this->curchip != mychip->chip) {
647 DoC_SelectChip(this, mychip->chip);
650 this->curfloor = mychip->floor;
651 this->curchip = mychip->chip;
653 DoC_Command(this,
654 (!this->page256
655 && (from & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
656 CDSN_CTRL_WP);
657 DoC_Address(this, ADDR_COLUMN_PAGE, from, CDSN_CTRL_WP,
658 CDSN_CTRL_ECC_IO);
660 /* Prime the ECC engine */
661 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
662 WriteDOC(DOC_ECC_EN, 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 */
677 *retlen += len;
679 /* Read the ECC data through the DiskOnChip ECC logic */
680 /* Note: this will work even with 2M x 8bit devices as */
681 /* they have 8 bytes of OOB per 256 page. mf. */
682 DoC_ReadBuf(this, eccbuf, 6);
684 /* Flush the pipeline */
685 if (DoC_is_Millennium(this)) {
686 dummy = ReadDOC(docptr, ECCConf);
687 dummy = ReadDOC(docptr, ECCConf);
688 i = ReadDOC(docptr, ECCConf);
689 } else {
690 dummy = ReadDOC(docptr, 2k_ECCStatus);
691 dummy = ReadDOC(docptr, 2k_ECCStatus);
692 i = ReadDOC(docptr, 2k_ECCStatus);
695 /* Check the ECC Status */
696 if (i & 0x80) {
697 int nb_errors;
698 /* There was an ECC error */
699 #ifdef ECC_DEBUG
700 printk(KERN_ERR "DiskOnChip ECC Error: Read at %lx\n", (long)from);
701 #endif
702 /* Read the ECC syndrom through the DiskOnChip ECC
703 logic. These syndrome will be all ZERO when there
704 is no error */
705 for (i = 0; i < 6; i++) {
706 syndrome[i] =
707 ReadDOC(docptr, ECCSyndrome0 + i);
709 nb_errors = doc_decode_ecc(buf, syndrome);
711 #ifdef ECC_DEBUG
712 printk(KERN_ERR "Errors corrected: %x\n", nb_errors);
713 #endif
714 if (nb_errors < 0) {
715 /* We return error, but have actually done the
716 read. Not that this can be told to
717 user-space, via sys_read(), but at least
718 MTD-aware stuff can know about it by
719 checking *retlen */
720 ret = -EIO;
724 #ifdef PSYCHO_DEBUG
725 printk(KERN_DEBUG "ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
726 (long)from, eccbuf[0], eccbuf[1], eccbuf[2],
727 eccbuf[3], eccbuf[4], eccbuf[5]);
728 #endif
730 /* disable the ECC engine */
731 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
733 /* according to 11.4.1, we need to wait for the busy line
734 * drop if we read to the end of the page. */
735 if(0 == ((from + len) & 0x1ff))
737 DoC_WaitReady(this);
740 from += len;
741 left -= len;
742 buf += len;
745 mutex_unlock(&this->lock);
747 return ret;
750 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
751 size_t * retlen, const u_char * buf)
753 struct DiskOnChip *this = mtd->priv;
754 int di; /* Yes, DI is a hangover from when I was disassembling the binary driver */
755 void __iomem *docptr = this->virtadr;
756 unsigned char eccbuf[6];
757 volatile char dummy;
758 int len256 = 0;
759 struct Nand *mychip;
760 size_t left = len;
761 int status;
763 /* Don't allow write past end of device */
764 if (to >= this->totlen)
765 return -EINVAL;
767 mutex_lock(&this->lock);
769 *retlen = 0;
770 while (left) {
771 len = left;
773 /* Don't allow a single write to cross a 512-byte block boundary */
774 if (to + len > ((to | 0x1ff) + 1))
775 len = ((to | 0x1ff) + 1) - to;
777 /* The ECC will not be calculated correctly if less than 512 is written */
778 /* DBB-
779 if (len != 0x200 && eccbuf)
780 printk(KERN_WARNING
781 "ECC needs a full sector write (adr: %lx size %lx)\n",
782 (long) to, (long) len);
783 -DBB */
785 /* printk("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */
787 /* Find the chip which is to be used and select it */
788 mychip = &this->chips[to >> (this->chipshift)];
790 if (this->curfloor != mychip->floor) {
791 DoC_SelectFloor(this, mychip->floor);
792 DoC_SelectChip(this, mychip->chip);
793 } else if (this->curchip != mychip->chip) {
794 DoC_SelectChip(this, mychip->chip);
797 this->curfloor = mychip->floor;
798 this->curchip = mychip->chip;
800 /* Set device to main plane of flash */
801 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
802 DoC_Command(this,
803 (!this->page256
804 && (to & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
805 CDSN_CTRL_WP);
807 DoC_Command(this, NAND_CMD_SEQIN, 0);
808 DoC_Address(this, ADDR_COLUMN_PAGE, to, 0, CDSN_CTRL_ECC_IO);
810 /* Prime the ECC engine */
811 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
812 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
814 /* treat crossing 256-byte sector for 2M x 8bits devices */
815 if (this->page256 && to + len > (to | 0xff) + 1) {
816 len256 = (to | 0xff) + 1 - to;
817 DoC_WriteBuf(this, buf, len256);
819 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
821 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
822 /* There's an implicit DoC_WaitReady() in DoC_Command */
824 dummy = ReadDOC(docptr, CDSNSlowIO);
825 DoC_Delay(this, 2);
827 if (ReadDOC_(docptr, this->ioreg) & 1) {
828 printk(KERN_ERR "Error programming flash\n");
829 /* Error in programming */
830 *retlen = 0;
831 mutex_unlock(&this->lock);
832 return -EIO;
835 DoC_Command(this, NAND_CMD_SEQIN, 0);
836 DoC_Address(this, ADDR_COLUMN_PAGE, to + len256, 0,
837 CDSN_CTRL_ECC_IO);
840 DoC_WriteBuf(this, &buf[len256], len - len256);
842 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_CE, docptr, CDSNControl);
844 if (DoC_is_Millennium(this)) {
845 WriteDOC(0, docptr, NOP);
846 WriteDOC(0, docptr, NOP);
847 WriteDOC(0, docptr, NOP);
848 } else {
849 WriteDOC_(0, docptr, this->ioreg);
850 WriteDOC_(0, docptr, this->ioreg);
851 WriteDOC_(0, docptr, this->ioreg);
854 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_FLASH_IO | CDSN_CTRL_CE, docptr,
855 CDSNControl);
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);
865 #ifdef PSYCHO_DEBUG
866 printk
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]);
870 #endif
871 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
873 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
874 /* There's an implicit DoC_WaitReady() in DoC_Command */
876 if (DoC_is_Millennium(this)) {
877 ReadDOC(docptr, ReadPipeInit);
878 status = ReadDOC(docptr, LastDataRead);
879 } else {
880 dummy = ReadDOC(docptr, CDSNSlowIO);
881 DoC_Delay(this, 2);
882 status = ReadDOC_(docptr, this->ioreg);
885 if (status & 1) {
886 printk(KERN_ERR "Error programming flash\n");
887 /* Error in programming */
888 *retlen = 0;
889 mutex_unlock(&this->lock);
890 return -EIO;
893 /* Let the caller know we completed it */
894 *retlen += len;
897 unsigned char x[8];
898 size_t dummy;
899 int ret;
901 /* Write the ECC data to flash */
902 for (di=0; di<6; di++)
903 x[di] = eccbuf[di];
905 x[6]=0x55;
906 x[7]=0x55;
908 ret = doc_write_oob_nolock(mtd, to, 8, &dummy, x);
909 if (ret) {
910 mutex_unlock(&this->lock);
911 return ret;
915 to += len;
916 left -= len;
917 buf += len;
920 mutex_unlock(&this->lock);
921 return 0;
924 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
925 struct mtd_oob_ops *ops)
927 struct DiskOnChip *this = mtd->priv;
928 int len256 = 0, ret;
929 struct Nand *mychip;
930 uint8_t *buf = ops->oobbuf;
931 size_t len = ops->len;
933 BUG_ON(ops->mode != MTD_OOB_PLACE);
935 ofs += ops->ooboffs;
937 mutex_lock(&this->lock);
939 mychip = &this->chips[ofs >> this->chipshift];
941 if (this->curfloor != mychip->floor) {
942 DoC_SelectFloor(this, mychip->floor);
943 DoC_SelectChip(this, mychip->chip);
944 } else if (this->curchip != mychip->chip) {
945 DoC_SelectChip(this, mychip->chip);
947 this->curfloor = mychip->floor;
948 this->curchip = mychip->chip;
950 /* update address for 2M x 8bit devices. OOB starts on the second */
951 /* page to maintain compatibility with doc_read_ecc. */
952 if (this->page256) {
953 if (!(ofs & 0x8))
954 ofs += 0x100;
955 else
956 ofs -= 0x8;
959 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
960 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, CDSN_CTRL_WP, 0);
962 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
963 /* Note: datasheet says it should automaticaly wrap to the */
964 /* next OOB block, but it didn't work here. mf. */
965 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
966 len256 = (ofs | 0x7) + 1 - ofs;
967 DoC_ReadBuf(this, buf, len256);
969 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
970 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff),
971 CDSN_CTRL_WP, 0);
974 DoC_ReadBuf(this, &buf[len256], len - len256);
976 ops->retlen = len;
977 /* Reading the full OOB data drops us off of the end of the page,
978 * causing the flash device to go into busy mode, so we need
979 * to wait until ready 11.4.1 and Toshiba TC58256FT docs */
981 ret = DoC_WaitReady(this);
983 mutex_unlock(&this->lock);
984 return ret;
988 static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
989 size_t * retlen, const u_char * buf)
991 struct DiskOnChip *this = mtd->priv;
992 int len256 = 0;
993 void __iomem *docptr = this->virtadr;
994 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
995 volatile int dummy;
996 int status;
998 // 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,
999 // buf[0], buf[1], buf[2], buf[3], buf[8], buf[9], buf[14],buf[15]);
1001 /* Find the chip which is to be used and select it */
1002 if (this->curfloor != mychip->floor) {
1003 DoC_SelectFloor(this, mychip->floor);
1004 DoC_SelectChip(this, mychip->chip);
1005 } else if (this->curchip != mychip->chip) {
1006 DoC_SelectChip(this, mychip->chip);
1008 this->curfloor = mychip->floor;
1009 this->curchip = mychip->chip;
1011 /* disable the ECC engine */
1012 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
1013 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
1015 /* Reset the chip, see Software Requirement 11.4 item 1. */
1016 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
1018 /* issue the Read2 command to set the pointer to the Spare Data Area. */
1019 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1021 /* update address for 2M x 8bit devices. OOB starts on the second */
1022 /* page to maintain compatibility with doc_read_ecc. */
1023 if (this->page256) {
1024 if (!(ofs & 0x8))
1025 ofs += 0x100;
1026 else
1027 ofs -= 0x8;
1030 /* issue the Serial Data In command to initial the Page Program process */
1031 DoC_Command(this, NAND_CMD_SEQIN, 0);
1032 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, 0, 0);
1034 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1035 /* Note: datasheet says it should automaticaly wrap to the */
1036 /* next OOB block, but it didn't work here. mf. */
1037 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1038 len256 = (ofs | 0x7) + 1 - ofs;
1039 DoC_WriteBuf(this, buf, len256);
1041 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1042 DoC_Command(this, NAND_CMD_STATUS, 0);
1043 /* DoC_WaitReady() is implicit in DoC_Command */
1045 if (DoC_is_Millennium(this)) {
1046 ReadDOC(docptr, ReadPipeInit);
1047 status = ReadDOC(docptr, LastDataRead);
1048 } else {
1049 dummy = ReadDOC(docptr, CDSNSlowIO);
1050 DoC_Delay(this, 2);
1051 status = ReadDOC_(docptr, this->ioreg);
1054 if (status & 1) {
1055 printk(KERN_ERR "Error programming oob data\n");
1056 /* There was an error */
1057 *retlen = 0;
1058 return -EIO;
1060 DoC_Command(this, NAND_CMD_SEQIN, 0);
1061 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), 0, 0);
1064 DoC_WriteBuf(this, &buf[len256], len - len256);
1066 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1067 DoC_Command(this, NAND_CMD_STATUS, 0);
1068 /* DoC_WaitReady() is implicit in DoC_Command */
1070 if (DoC_is_Millennium(this)) {
1071 ReadDOC(docptr, ReadPipeInit);
1072 status = ReadDOC(docptr, LastDataRead);
1073 } else {
1074 dummy = ReadDOC(docptr, CDSNSlowIO);
1075 DoC_Delay(this, 2);
1076 status = ReadDOC_(docptr, this->ioreg);
1079 if (status & 1) {
1080 printk(KERN_ERR "Error programming oob data\n");
1081 /* There was an error */
1082 *retlen = 0;
1083 return -EIO;
1086 *retlen = len;
1087 return 0;
1091 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
1092 struct mtd_oob_ops *ops)
1094 struct DiskOnChip *this = mtd->priv;
1095 int ret;
1097 BUG_ON(ops->mode != MTD_OOB_PLACE);
1099 mutex_lock(&this->lock);
1100 ret = doc_write_oob_nolock(mtd, ofs + ops->ooboffs, ops->len,
1101 &ops->retlen, ops->oobbuf);
1103 mutex_unlock(&this->lock);
1104 return ret;
1107 static int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
1109 struct DiskOnChip *this = mtd->priv;
1110 __u32 ofs = instr->addr;
1111 __u32 len = instr->len;
1112 volatile int dummy;
1113 void __iomem *docptr = this->virtadr;
1114 struct Nand *mychip;
1115 int status;
1117 mutex_lock(&this->lock);
1119 if (ofs & (mtd->erasesize-1) || len & (mtd->erasesize-1)) {
1120 mutex_unlock(&this->lock);
1121 return -EINVAL;
1124 instr->state = MTD_ERASING;
1126 /* FIXME: Do this in the background. Use timers or schedule_task() */
1127 while(len) {
1128 mychip = &this->chips[ofs >> this->chipshift];
1130 if (this->curfloor != mychip->floor) {
1131 DoC_SelectFloor(this, mychip->floor);
1132 DoC_SelectChip(this, mychip->chip);
1133 } else if (this->curchip != mychip->chip) {
1134 DoC_SelectChip(this, mychip->chip);
1136 this->curfloor = mychip->floor;
1137 this->curchip = mychip->chip;
1139 DoC_Command(this, NAND_CMD_ERASE1, 0);
1140 DoC_Address(this, ADDR_PAGE, ofs, 0, 0);
1141 DoC_Command(this, NAND_CMD_ERASE2, 0);
1143 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
1145 if (DoC_is_Millennium(this)) {
1146 ReadDOC(docptr, ReadPipeInit);
1147 status = ReadDOC(docptr, LastDataRead);
1148 } else {
1149 dummy = ReadDOC(docptr, CDSNSlowIO);
1150 DoC_Delay(this, 2);
1151 status = ReadDOC_(docptr, this->ioreg);
1154 if (status & 1) {
1155 printk(KERN_ERR "Error erasing at 0x%x\n", ofs);
1156 /* There was an error */
1157 instr->state = MTD_ERASE_FAILED;
1158 goto callback;
1160 ofs += mtd->erasesize;
1161 len -= mtd->erasesize;
1163 instr->state = MTD_ERASE_DONE;
1165 callback:
1166 mtd_erase_callback(instr);
1168 mutex_unlock(&this->lock);
1169 return 0;
1173 /****************************************************************************
1175 * Module stuff
1177 ****************************************************************************/
1179 static void __exit cleanup_doc2000(void)
1181 struct mtd_info *mtd;
1182 struct DiskOnChip *this;
1184 while ((mtd = doc2klist)) {
1185 this = mtd->priv;
1186 doc2klist = this->nextdoc;
1188 del_mtd_device(mtd);
1190 iounmap(this->virtadr);
1191 kfree(this->chips);
1192 kfree(mtd);
1196 module_exit(cleanup_doc2000);
1198 MODULE_LICENSE("GPL");
1199 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
1200 MODULE_DESCRIPTION("MTD driver for DiskOnChip 2000 and Millennium");