Linux 2.6.20.7
[linux/fpc-iii.git] / drivers / mtd / devices / doc2000.c
blob603a7951ac9b1e3e9b646811a840c4e8139745b8
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>
7 * $Id: doc2000.c,v 1.67 2005/11/07 11:14:24 gleixner Exp $
8 */
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <asm/errno.h>
13 #include <asm/io.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)
35 #else
36 #define DoC_is_2000(doc) (0)
37 #endif
39 #if defined(DOC_SUPPORT_2000TSOP) || defined(DOC_SUPPORT_MILLENNIUM)
40 #define DoC_is_Millennium(doc) (doc->ChipID == DOC_ChipID_DocMil)
41 #else
42 #define DoC_is_Millennium(doc) (0)
43 #endif
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
50 * this:
51 #undef USE_MEMCPY
54 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
55 size_t *retlen, u_char *buf);
56 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
57 size_t *retlen, const u_char *buf);
58 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
59 struct mtd_oob_ops *ops);
60 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
61 struct mtd_oob_ops *ops);
62 static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
63 size_t *retlen, const u_char *buf);
64 static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
66 static struct mtd_info *doc2klist = NULL;
68 /* Perform the required delay cycles by reading from the appropriate register */
69 static void DoC_Delay(struct DiskOnChip *doc, unsigned short cycles)
71 volatile char dummy;
72 int i;
74 for (i = 0; i < cycles; i++) {
75 if (DoC_is_Millennium(doc))
76 dummy = ReadDOC(doc->virtadr, NOP);
77 else
78 dummy = ReadDOC(doc->virtadr, DOCStatus);
83 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
84 static int _DoC_WaitReady(struct DiskOnChip *doc)
86 void __iomem *docptr = doc->virtadr;
87 unsigned long timeo = jiffies + (HZ * 10);
89 DEBUG(MTD_DEBUG_LEVEL3,
90 "_DoC_WaitReady called for out-of-line wait\n");
92 /* Out-of-line routine to wait for chip response */
93 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
94 /* issue 2 read from NOP register after reading from CDSNControl register
95 see Software Requirement 11.4 item 2. */
96 DoC_Delay(doc, 2);
98 if (time_after(jiffies, timeo)) {
99 DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
100 return -EIO;
102 udelay(1);
103 cond_resched();
106 return 0;
109 static inline int DoC_WaitReady(struct DiskOnChip *doc)
111 void __iomem *docptr = doc->virtadr;
113 /* This is inline, to optimise the common case, where it's ready instantly */
114 int ret = 0;
116 /* 4 read form NOP register should be issued in prior to the read from CDSNControl
117 see Software Requirement 11.4 item 2. */
118 DoC_Delay(doc, 4);
120 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
121 /* Call the out-of-line routine to wait */
122 ret = _DoC_WaitReady(doc);
124 /* issue 2 read from NOP register after reading from CDSNControl register
125 see Software Requirement 11.4 item 2. */
126 DoC_Delay(doc, 2);
128 return ret;
131 /* DoC_Command: Send a flash command to the flash chip through the CDSN Slow IO register to
132 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
133 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
135 static int DoC_Command(struct DiskOnChip *doc, unsigned char command,
136 unsigned char xtraflags)
138 void __iomem *docptr = doc->virtadr;
140 if (DoC_is_2000(doc))
141 xtraflags |= CDSN_CTRL_FLASH_IO;
143 /* Assert the CLE (Command Latch Enable) line to the flash chip */
144 WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
145 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
147 if (DoC_is_Millennium(doc))
148 WriteDOC(command, docptr, CDSNSlowIO);
150 /* Send the command */
151 WriteDOC_(command, docptr, doc->ioreg);
152 if (DoC_is_Millennium(doc))
153 WriteDOC(command, docptr, WritePipeTerm);
155 /* Lower the CLE line */
156 WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
157 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
159 /* Wait for the chip to respond - Software requirement 11.4.1 (extended for any command) */
160 return DoC_WaitReady(doc);
163 /* DoC_Address: Set the current address for the flash chip through the CDSN Slow IO register to
164 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
165 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
167 static int DoC_Address(struct DiskOnChip *doc, int numbytes, unsigned long ofs,
168 unsigned char xtraflags1, unsigned char xtraflags2)
170 int i;
171 void __iomem *docptr = doc->virtadr;
173 if (DoC_is_2000(doc))
174 xtraflags1 |= CDSN_CTRL_FLASH_IO;
176 /* Assert the ALE (Address Latch Enable) line to the flash chip */
177 WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
179 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
181 /* Send the address */
182 /* Devices with 256-byte page are addressed as:
183 Column (bits 0-7), Page (bits 8-15, 16-23, 24-31)
184 * there is no device on the market with page256
185 and more than 24 bits.
186 Devices with 512-byte page are addressed as:
187 Column (bits 0-7), Page (bits 9-16, 17-24, 25-31)
188 * 25-31 is sent only if the chip support it.
189 * bit 8 changes the read command to be sent
190 (NAND_CMD_READ0 or NAND_CMD_READ1).
193 if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE) {
194 if (DoC_is_Millennium(doc))
195 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
196 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
199 if (doc->page256) {
200 ofs = ofs >> 8;
201 } else {
202 ofs = ofs >> 9;
205 if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE) {
206 for (i = 0; i < doc->pageadrlen; i++, ofs = ofs >> 8) {
207 if (DoC_is_Millennium(doc))
208 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
209 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
213 if (DoC_is_Millennium(doc))
214 WriteDOC(ofs & 0xff, docptr, WritePipeTerm);
216 DoC_Delay(doc, 2); /* Needed for some slow flash chips. mf. */
218 /* FIXME: The SlowIO's for millennium could be replaced by
219 a single WritePipeTerm here. mf. */
221 /* Lower the ALE line */
222 WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr,
223 CDSNControl);
225 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
227 /* Wait for the chip to respond - Software requirement 11.4.1 */
228 return DoC_WaitReady(doc);
231 /* Read a buffer from DoC, taking care of Millennium odditys */
232 static void DoC_ReadBuf(struct DiskOnChip *doc, u_char * buf, int len)
234 volatile int dummy;
235 int modulus = 0xffff;
236 void __iomem *docptr = doc->virtadr;
237 int i;
239 if (len <= 0)
240 return;
242 if (DoC_is_Millennium(doc)) {
243 /* Read the data via the internal pipeline through CDSN IO register,
244 see Pipelined Read Operations 11.3 */
245 dummy = ReadDOC(docptr, ReadPipeInit);
247 /* Millennium should use the LastDataRead register - Pipeline Reads */
248 len--;
250 /* This is needed for correctly ECC calculation */
251 modulus = 0xff;
254 for (i = 0; i < len; i++)
255 buf[i] = ReadDOC_(docptr, doc->ioreg + (i & modulus));
257 if (DoC_is_Millennium(doc)) {
258 buf[i] = ReadDOC(docptr, LastDataRead);
262 /* Write a buffer to DoC, taking care of Millennium odditys */
263 static void DoC_WriteBuf(struct DiskOnChip *doc, const u_char * buf, int len)
265 void __iomem *docptr = doc->virtadr;
266 int i;
268 if (len <= 0)
269 return;
271 for (i = 0; i < len; i++)
272 WriteDOC_(buf[i], docptr, doc->ioreg + i);
274 if (DoC_is_Millennium(doc)) {
275 WriteDOC(0x00, docptr, WritePipeTerm);
280 /* DoC_SelectChip: Select a given flash chip within the current floor */
282 static inline int DoC_SelectChip(struct DiskOnChip *doc, int chip)
284 void __iomem *docptr = doc->virtadr;
286 /* Software requirement 11.4.4 before writing DeviceSelect */
287 /* Deassert the CE line to eliminate glitches on the FCE# outputs */
288 WriteDOC(CDSN_CTRL_WP, docptr, CDSNControl);
289 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
291 /* Select the individual flash chip requested */
292 WriteDOC(chip, docptr, CDSNDeviceSelect);
293 DoC_Delay(doc, 4);
295 /* Reassert the CE line */
296 WriteDOC(CDSN_CTRL_CE | CDSN_CTRL_FLASH_IO | CDSN_CTRL_WP, docptr,
297 CDSNControl);
298 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
300 /* Wait for it to be ready */
301 return DoC_WaitReady(doc);
304 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
306 static inline int DoC_SelectFloor(struct DiskOnChip *doc, int floor)
308 void __iomem *docptr = doc->virtadr;
310 /* Select the floor (bank) of chips required */
311 WriteDOC(floor, docptr, FloorSelect);
313 /* Wait for the chip to be ready */
314 return DoC_WaitReady(doc);
317 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
319 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
321 int mfr, id, i, j;
322 volatile char dummy;
324 /* Page in the required floor/chip */
325 DoC_SelectFloor(doc, floor);
326 DoC_SelectChip(doc, chip);
328 /* Reset the chip */
329 if (DoC_Command(doc, NAND_CMD_RESET, CDSN_CTRL_WP)) {
330 DEBUG(MTD_DEBUG_LEVEL2,
331 "DoC_Command (reset) for %d,%d returned true\n",
332 floor, chip);
333 return 0;
337 /* Read the NAND chip ID: 1. Send ReadID command */
338 if (DoC_Command(doc, NAND_CMD_READID, CDSN_CTRL_WP)) {
339 DEBUG(MTD_DEBUG_LEVEL2,
340 "DoC_Command (ReadID) for %d,%d returned true\n",
341 floor, chip);
342 return 0;
345 /* Read the NAND chip ID: 2. Send address byte zero */
346 DoC_Address(doc, ADDR_COLUMN, 0, CDSN_CTRL_WP, 0);
348 /* Read the manufacturer and device id codes from the device */
350 if (DoC_is_Millennium(doc)) {
351 DoC_Delay(doc, 2);
352 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
353 mfr = ReadDOC(doc->virtadr, LastDataRead);
355 DoC_Delay(doc, 2);
356 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
357 id = ReadDOC(doc->virtadr, LastDataRead);
358 } else {
359 /* CDSN Slow IO register see Software Req 11.4 item 5. */
360 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
361 DoC_Delay(doc, 2);
362 mfr = ReadDOC_(doc->virtadr, doc->ioreg);
364 /* CDSN Slow IO register see Software Req 11.4 item 5. */
365 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
366 DoC_Delay(doc, 2);
367 id = ReadDOC_(doc->virtadr, doc->ioreg);
370 /* No response - return failure */
371 if (mfr == 0xff || mfr == 0)
372 return 0;
374 /* Check it's the same as the first chip we identified.
375 * M-Systems say that any given DiskOnChip device should only
376 * contain _one_ type of flash part, although that's not a
377 * hardware restriction. */
378 if (doc->mfr) {
379 if (doc->mfr == mfr && doc->id == id)
380 return 1; /* This is another the same the first */
381 else
382 printk(KERN_WARNING
383 "Flash chip at floor %d, chip %d is different:\n",
384 floor, chip);
387 /* Print and store the manufacturer and ID codes. */
388 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
389 if (id == nand_flash_ids[i].id) {
390 /* Try to identify manufacturer */
391 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
392 if (nand_manuf_ids[j].id == mfr)
393 break;
395 printk(KERN_INFO
396 "Flash chip found: Manufacturer ID: %2.2X, "
397 "Chip ID: %2.2X (%s:%s)\n", mfr, id,
398 nand_manuf_ids[j].name, nand_flash_ids[i].name);
399 if (!doc->mfr) {
400 doc->mfr = mfr;
401 doc->id = id;
402 doc->chipshift =
403 ffs((nand_flash_ids[i].chipsize << 20)) - 1;
404 doc->page256 = (nand_flash_ids[i].pagesize == 256) ? 1 : 0;
405 doc->pageadrlen = doc->chipshift > 25 ? 3 : 2;
406 doc->erasesize =
407 nand_flash_ids[i].erasesize;
408 return 1;
410 return 0;
415 /* We haven't fully identified the chip. Print as much as we know. */
416 printk(KERN_WARNING "Unknown flash chip found: %2.2X %2.2X\n",
417 id, mfr);
419 printk(KERN_WARNING "Please report to dwmw2@infradead.org\n");
420 return 0;
423 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
425 static void DoC_ScanChips(struct DiskOnChip *this, int maxchips)
427 int floor, chip;
428 int numchips[MAX_FLOORS];
429 int ret = 1;
431 this->numchips = 0;
432 this->mfr = 0;
433 this->id = 0;
435 /* For each floor, find the number of valid chips it contains */
436 for (floor = 0; floor < MAX_FLOORS; floor++) {
437 ret = 1;
438 numchips[floor] = 0;
439 for (chip = 0; chip < maxchips && ret != 0; chip++) {
441 ret = DoC_IdentChip(this, floor, chip);
442 if (ret) {
443 numchips[floor]++;
444 this->numchips++;
449 /* If there are none at all that we recognise, bail */
450 if (!this->numchips) {
451 printk(KERN_NOTICE "No flash chips recognised.\n");
452 return;
455 /* Allocate an array to hold the information for each chip */
456 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
457 if (!this->chips) {
458 printk(KERN_NOTICE "No memory for allocating chip info structures\n");
459 return;
462 ret = 0;
464 /* Fill out the chip array with {floor, chipno} for each
465 * detected chip in the device. */
466 for (floor = 0; floor < MAX_FLOORS; floor++) {
467 for (chip = 0; chip < numchips[floor]; chip++) {
468 this->chips[ret].floor = floor;
469 this->chips[ret].chip = chip;
470 this->chips[ret].curadr = 0;
471 this->chips[ret].curmode = 0x50;
472 ret++;
476 /* Calculate and print the total size of the device */
477 this->totlen = this->numchips * (1 << this->chipshift);
479 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
480 this->numchips, this->totlen >> 20);
483 static int DoC2k_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
485 int tmp1, tmp2, retval;
486 if (doc1->physadr == doc2->physadr)
487 return 1;
489 /* Use the alias resolution register which was set aside for this
490 * purpose. If it's value is the same on both chips, they might
491 * be the same chip, and we write to one and check for a change in
492 * the other. It's unclear if this register is usuable in the
493 * DoC 2000 (it's in the Millennium docs), but it seems to work. */
494 tmp1 = ReadDOC(doc1->virtadr, AliasResolution);
495 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
496 if (tmp1 != tmp2)
497 return 0;
499 WriteDOC((tmp1 + 1) % 0xff, doc1->virtadr, AliasResolution);
500 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
501 if (tmp2 == (tmp1 + 1) % 0xff)
502 retval = 1;
503 else
504 retval = 0;
506 /* Restore register contents. May not be necessary, but do it just to
507 * be safe. */
508 WriteDOC(tmp1, doc1->virtadr, AliasResolution);
510 return retval;
513 /* This routine is found from the docprobe code by symbol_get(),
514 * which will bump the use count of this module. */
515 void DoC2k_init(struct mtd_info *mtd)
517 struct DiskOnChip *this = mtd->priv;
518 struct DiskOnChip *old = NULL;
519 int maxchips;
521 /* We must avoid being called twice for the same device. */
523 if (doc2klist)
524 old = doc2klist->priv;
526 while (old) {
527 if (DoC2k_is_alias(old, this)) {
528 printk(KERN_NOTICE
529 "Ignoring DiskOnChip 2000 at 0x%lX - already configured\n",
530 this->physadr);
531 iounmap(this->virtadr);
532 kfree(mtd);
533 return;
535 if (old->nextdoc)
536 old = old->nextdoc->priv;
537 else
538 old = NULL;
542 switch (this->ChipID) {
543 case DOC_ChipID_Doc2kTSOP:
544 mtd->name = "DiskOnChip 2000 TSOP";
545 this->ioreg = DoC_Mil_CDSN_IO;
546 /* Pretend it's a Millennium */
547 this->ChipID = DOC_ChipID_DocMil;
548 maxchips = MAX_CHIPS;
549 break;
550 case DOC_ChipID_Doc2k:
551 mtd->name = "DiskOnChip 2000";
552 this->ioreg = DoC_2k_CDSN_IO;
553 maxchips = MAX_CHIPS;
554 break;
555 case DOC_ChipID_DocMil:
556 mtd->name = "DiskOnChip Millennium";
557 this->ioreg = DoC_Mil_CDSN_IO;
558 maxchips = MAX_CHIPS_MIL;
559 break;
560 default:
561 printk("Unknown ChipID 0x%02x\n", this->ChipID);
562 kfree(mtd);
563 iounmap(this->virtadr);
564 return;
567 printk(KERN_NOTICE "%s found at address 0x%lX\n", mtd->name,
568 this->physadr);
570 mtd->type = MTD_NANDFLASH;
571 mtd->flags = MTD_CAP_NANDFLASH;
572 mtd->ecctype = MTD_ECC_RS_DiskOnChip;
573 mtd->size = 0;
574 mtd->erasesize = 0;
575 mtd->writesize = 512;
576 mtd->oobsize = 16;
577 mtd->owner = THIS_MODULE;
578 mtd->erase = doc_erase;
579 mtd->point = NULL;
580 mtd->unpoint = NULL;
581 mtd->read = doc_read;
582 mtd->write = doc_write;
583 mtd->read_oob = doc_read_oob;
584 mtd->write_oob = doc_write_oob;
585 mtd->sync = NULL;
587 this->totlen = 0;
588 this->numchips = 0;
590 this->curfloor = -1;
591 this->curchip = -1;
592 mutex_init(&this->lock);
594 /* Ident all the chips present. */
595 DoC_ScanChips(this, maxchips);
597 if (!this->totlen) {
598 kfree(mtd);
599 iounmap(this->virtadr);
600 } else {
601 this->nextdoc = doc2klist;
602 doc2klist = mtd;
603 mtd->size = this->totlen;
604 mtd->erasesize = this->erasesize;
605 add_mtd_device(mtd);
606 return;
609 EXPORT_SYMBOL_GPL(DoC2k_init);
611 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
612 size_t * retlen, u_char * buf)
614 struct DiskOnChip *this = mtd->priv;
615 void __iomem *docptr = this->virtadr;
616 struct Nand *mychip;
617 unsigned char syndrome[6], eccbuf[6];
618 volatile char dummy;
619 int i, len256 = 0, ret=0;
620 size_t left = len;
622 /* Don't allow read past end of device */
623 if (from >= this->totlen)
624 return -EINVAL;
626 mutex_lock(&this->lock);
628 *retlen = 0;
629 while (left) {
630 len = left;
632 /* Don't allow a single read to cross a 512-byte block boundary */
633 if (from + len > ((from | 0x1ff) + 1))
634 len = ((from | 0x1ff) + 1) - from;
636 /* The ECC will not be calculated correctly if less than 512 is read */
637 if (len != 0x200 && eccbuf)
638 printk(KERN_WARNING
639 "ECC needs a full sector read (adr: %lx size %lx)\n",
640 (long) from, (long) len);
642 /* printk("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len); */
645 /* Find the chip which is to be used and select it */
646 mychip = &this->chips[from >> (this->chipshift)];
648 if (this->curfloor != mychip->floor) {
649 DoC_SelectFloor(this, mychip->floor);
650 DoC_SelectChip(this, mychip->chip);
651 } else if (this->curchip != mychip->chip) {
652 DoC_SelectChip(this, mychip->chip);
655 this->curfloor = mychip->floor;
656 this->curchip = mychip->chip;
658 DoC_Command(this,
659 (!this->page256
660 && (from & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
661 CDSN_CTRL_WP);
662 DoC_Address(this, ADDR_COLUMN_PAGE, from, CDSN_CTRL_WP,
663 CDSN_CTRL_ECC_IO);
665 /* Prime the ECC engine */
666 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
667 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
669 /* treat crossing 256-byte sector for 2M x 8bits devices */
670 if (this->page256 && from + len > (from | 0xff) + 1) {
671 len256 = (from | 0xff) + 1 - from;
672 DoC_ReadBuf(this, buf, len256);
674 DoC_Command(this, NAND_CMD_READ0, CDSN_CTRL_WP);
675 DoC_Address(this, ADDR_COLUMN_PAGE, from + len256,
676 CDSN_CTRL_WP, CDSN_CTRL_ECC_IO);
679 DoC_ReadBuf(this, &buf[len256], len - len256);
681 /* Let the caller know we completed it */
682 *retlen += len;
684 /* Read the ECC data through the DiskOnChip ECC logic */
685 /* Note: this will work even with 2M x 8bit devices as */
686 /* they have 8 bytes of OOB per 256 page. mf. */
687 DoC_ReadBuf(this, eccbuf, 6);
689 /* Flush the pipeline */
690 if (DoC_is_Millennium(this)) {
691 dummy = ReadDOC(docptr, ECCConf);
692 dummy = ReadDOC(docptr, ECCConf);
693 i = ReadDOC(docptr, ECCConf);
694 } else {
695 dummy = ReadDOC(docptr, 2k_ECCStatus);
696 dummy = ReadDOC(docptr, 2k_ECCStatus);
697 i = ReadDOC(docptr, 2k_ECCStatus);
700 /* Check the ECC Status */
701 if (i & 0x80) {
702 int nb_errors;
703 /* There was an ECC error */
704 #ifdef ECC_DEBUG
705 printk(KERN_ERR "DiskOnChip ECC Error: Read at %lx\n", (long)from);
706 #endif
707 /* Read the ECC syndrom through the DiskOnChip ECC
708 logic. These syndrome will be all ZERO when there
709 is no error */
710 for (i = 0; i < 6; i++) {
711 syndrome[i] =
712 ReadDOC(docptr, ECCSyndrome0 + i);
714 nb_errors = doc_decode_ecc(buf, syndrome);
716 #ifdef ECC_DEBUG
717 printk(KERN_ERR "Errors corrected: %x\n", nb_errors);
718 #endif
719 if (nb_errors < 0) {
720 /* We return error, but have actually done the
721 read. Not that this can be told to
722 user-space, via sys_read(), but at least
723 MTD-aware stuff can know about it by
724 checking *retlen */
725 ret = -EIO;
729 #ifdef PSYCHO_DEBUG
730 printk(KERN_DEBUG "ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
731 (long)from, eccbuf[0], eccbuf[1], eccbuf[2],
732 eccbuf[3], eccbuf[4], eccbuf[5]);
733 #endif
735 /* disable the ECC engine */
736 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
738 /* according to 11.4.1, we need to wait for the busy line
739 * drop if we read to the end of the page. */
740 if(0 == ((from + len) & 0x1ff))
742 DoC_WaitReady(this);
745 from += len;
746 left -= len;
747 buf += len;
750 mutex_unlock(&this->lock);
752 return ret;
755 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
756 size_t * retlen, const u_char * buf)
758 struct DiskOnChip *this = mtd->priv;
759 int di; /* Yes, DI is a hangover from when I was disassembling the binary driver */
760 void __iomem *docptr = this->virtadr;
761 unsigned char eccbuf[6];
762 volatile char dummy;
763 int len256 = 0;
764 struct Nand *mychip;
765 size_t left = len;
766 int status;
768 /* Don't allow write past end of device */
769 if (to >= this->totlen)
770 return -EINVAL;
772 mutex_lock(&this->lock);
774 *retlen = 0;
775 while (left) {
776 len = left;
778 /* Don't allow a single write to cross a 512-byte block boundary */
779 if (to + len > ((to | 0x1ff) + 1))
780 len = ((to | 0x1ff) + 1) - to;
782 /* The ECC will not be calculated correctly if less than 512 is written */
783 /* DBB-
784 if (len != 0x200 && eccbuf)
785 printk(KERN_WARNING
786 "ECC needs a full sector write (adr: %lx size %lx)\n",
787 (long) to, (long) len);
788 -DBB */
790 /* printk("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */
792 /* Find the chip which is to be used and select it */
793 mychip = &this->chips[to >> (this->chipshift)];
795 if (this->curfloor != mychip->floor) {
796 DoC_SelectFloor(this, mychip->floor);
797 DoC_SelectChip(this, mychip->chip);
798 } else if (this->curchip != mychip->chip) {
799 DoC_SelectChip(this, mychip->chip);
802 this->curfloor = mychip->floor;
803 this->curchip = mychip->chip;
805 /* Set device to main plane of flash */
806 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
807 DoC_Command(this,
808 (!this->page256
809 && (to & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
810 CDSN_CTRL_WP);
812 DoC_Command(this, NAND_CMD_SEQIN, 0);
813 DoC_Address(this, ADDR_COLUMN_PAGE, to, 0, CDSN_CTRL_ECC_IO);
815 /* Prime the ECC engine */
816 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
817 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
819 /* treat crossing 256-byte sector for 2M x 8bits devices */
820 if (this->page256 && to + len > (to | 0xff) + 1) {
821 len256 = (to | 0xff) + 1 - to;
822 DoC_WriteBuf(this, buf, len256);
824 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
826 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
827 /* There's an implicit DoC_WaitReady() in DoC_Command */
829 dummy = ReadDOC(docptr, CDSNSlowIO);
830 DoC_Delay(this, 2);
832 if (ReadDOC_(docptr, this->ioreg) & 1) {
833 printk(KERN_ERR "Error programming flash\n");
834 /* Error in programming */
835 *retlen = 0;
836 mutex_unlock(&this->lock);
837 return -EIO;
840 DoC_Command(this, NAND_CMD_SEQIN, 0);
841 DoC_Address(this, ADDR_COLUMN_PAGE, to + len256, 0,
842 CDSN_CTRL_ECC_IO);
845 DoC_WriteBuf(this, &buf[len256], len - len256);
847 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_CE, docptr, CDSNControl);
849 if (DoC_is_Millennium(this)) {
850 WriteDOC(0, docptr, NOP);
851 WriteDOC(0, docptr, NOP);
852 WriteDOC(0, docptr, NOP);
853 } else {
854 WriteDOC_(0, docptr, this->ioreg);
855 WriteDOC_(0, docptr, this->ioreg);
856 WriteDOC_(0, docptr, this->ioreg);
859 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_FLASH_IO | CDSN_CTRL_CE, docptr,
860 CDSNControl);
862 /* Read the ECC data through the DiskOnChip ECC logic */
863 for (di = 0; di < 6; di++) {
864 eccbuf[di] = ReadDOC(docptr, ECCSyndrome0 + di);
867 /* Reset the ECC engine */
868 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
870 #ifdef PSYCHO_DEBUG
871 printk
872 ("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
873 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
874 eccbuf[4], eccbuf[5]);
875 #endif
876 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
878 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
879 /* There's an implicit DoC_WaitReady() in DoC_Command */
881 if (DoC_is_Millennium(this)) {
882 ReadDOC(docptr, ReadPipeInit);
883 status = ReadDOC(docptr, LastDataRead);
884 } else {
885 dummy = ReadDOC(docptr, CDSNSlowIO);
886 DoC_Delay(this, 2);
887 status = ReadDOC_(docptr, this->ioreg);
890 if (status & 1) {
891 printk(KERN_ERR "Error programming flash\n");
892 /* Error in programming */
893 *retlen = 0;
894 mutex_unlock(&this->lock);
895 return -EIO;
898 /* Let the caller know we completed it */
899 *retlen += len;
901 if (eccbuf) {
902 unsigned char x[8];
903 size_t dummy;
904 int ret;
906 /* Write the ECC data to flash */
907 for (di=0; di<6; di++)
908 x[di] = eccbuf[di];
910 x[6]=0x55;
911 x[7]=0x55;
913 ret = doc_write_oob_nolock(mtd, to, 8, &dummy, x);
914 if (ret) {
915 mutex_unlock(&this->lock);
916 return ret;
920 to += len;
921 left -= len;
922 buf += len;
925 mutex_unlock(&this->lock);
926 return 0;
929 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
930 struct mtd_oob_ops *ops)
932 struct DiskOnChip *this = mtd->priv;
933 int len256 = 0, ret;
934 struct Nand *mychip;
935 uint8_t *buf = ops->oobbuf;
936 size_t len = ops->len;
938 BUG_ON(ops->mode != MTD_OOB_PLACE);
940 ofs += ops->ooboffs;
942 mutex_lock(&this->lock);
944 mychip = &this->chips[ofs >> this->chipshift];
946 if (this->curfloor != mychip->floor) {
947 DoC_SelectFloor(this, mychip->floor);
948 DoC_SelectChip(this, mychip->chip);
949 } else if (this->curchip != mychip->chip) {
950 DoC_SelectChip(this, mychip->chip);
952 this->curfloor = mychip->floor;
953 this->curchip = mychip->chip;
955 /* update address for 2M x 8bit devices. OOB starts on the second */
956 /* page to maintain compatibility with doc_read_ecc. */
957 if (this->page256) {
958 if (!(ofs & 0x8))
959 ofs += 0x100;
960 else
961 ofs -= 0x8;
964 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
965 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, CDSN_CTRL_WP, 0);
967 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
968 /* Note: datasheet says it should automaticaly wrap to the */
969 /* next OOB block, but it didn't work here. mf. */
970 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
971 len256 = (ofs | 0x7) + 1 - ofs;
972 DoC_ReadBuf(this, buf, len256);
974 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
975 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff),
976 CDSN_CTRL_WP, 0);
979 DoC_ReadBuf(this, &buf[len256], len - len256);
981 ops->retlen = len;
982 /* Reading the full OOB data drops us off of the end of the page,
983 * causing the flash device to go into busy mode, so we need
984 * to wait until ready 11.4.1 and Toshiba TC58256FT docs */
986 ret = DoC_WaitReady(this);
988 mutex_unlock(&this->lock);
989 return ret;
993 static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
994 size_t * retlen, const u_char * buf)
996 struct DiskOnChip *this = mtd->priv;
997 int len256 = 0;
998 void __iomem *docptr = this->virtadr;
999 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
1000 volatile int dummy;
1001 int status;
1003 // 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,
1004 // buf[0], buf[1], buf[2], buf[3], buf[8], buf[9], buf[14],buf[15]);
1006 /* Find the chip which is to be used and select it */
1007 if (this->curfloor != mychip->floor) {
1008 DoC_SelectFloor(this, mychip->floor);
1009 DoC_SelectChip(this, mychip->chip);
1010 } else if (this->curchip != mychip->chip) {
1011 DoC_SelectChip(this, mychip->chip);
1013 this->curfloor = mychip->floor;
1014 this->curchip = mychip->chip;
1016 /* disable the ECC engine */
1017 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
1018 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
1020 /* Reset the chip, see Software Requirement 11.4 item 1. */
1021 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
1023 /* issue the Read2 command to set the pointer to the Spare Data Area. */
1024 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1026 /* update address for 2M x 8bit devices. OOB starts on the second */
1027 /* page to maintain compatibility with doc_read_ecc. */
1028 if (this->page256) {
1029 if (!(ofs & 0x8))
1030 ofs += 0x100;
1031 else
1032 ofs -= 0x8;
1035 /* issue the Serial Data In command to initial the Page Program process */
1036 DoC_Command(this, NAND_CMD_SEQIN, 0);
1037 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, 0, 0);
1039 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1040 /* Note: datasheet says it should automaticaly wrap to the */
1041 /* next OOB block, but it didn't work here. mf. */
1042 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1043 len256 = (ofs | 0x7) + 1 - ofs;
1044 DoC_WriteBuf(this, buf, len256);
1046 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1047 DoC_Command(this, NAND_CMD_STATUS, 0);
1048 /* DoC_WaitReady() is implicit in DoC_Command */
1050 if (DoC_is_Millennium(this)) {
1051 ReadDOC(docptr, ReadPipeInit);
1052 status = ReadDOC(docptr, LastDataRead);
1053 } else {
1054 dummy = ReadDOC(docptr, CDSNSlowIO);
1055 DoC_Delay(this, 2);
1056 status = ReadDOC_(docptr, this->ioreg);
1059 if (status & 1) {
1060 printk(KERN_ERR "Error programming oob data\n");
1061 /* There was an error */
1062 *retlen = 0;
1063 return -EIO;
1065 DoC_Command(this, NAND_CMD_SEQIN, 0);
1066 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), 0, 0);
1069 DoC_WriteBuf(this, &buf[len256], len - len256);
1071 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1072 DoC_Command(this, NAND_CMD_STATUS, 0);
1073 /* DoC_WaitReady() is implicit in DoC_Command */
1075 if (DoC_is_Millennium(this)) {
1076 ReadDOC(docptr, ReadPipeInit);
1077 status = ReadDOC(docptr, LastDataRead);
1078 } else {
1079 dummy = ReadDOC(docptr, CDSNSlowIO);
1080 DoC_Delay(this, 2);
1081 status = ReadDOC_(docptr, this->ioreg);
1084 if (status & 1) {
1085 printk(KERN_ERR "Error programming oob data\n");
1086 /* There was an error */
1087 *retlen = 0;
1088 return -EIO;
1091 *retlen = len;
1092 return 0;
1096 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
1097 struct mtd_oob_ops *ops)
1099 struct DiskOnChip *this = mtd->priv;
1100 int ret;
1102 BUG_ON(ops->mode != MTD_OOB_PLACE);
1104 mutex_lock(&this->lock);
1105 ret = doc_write_oob_nolock(mtd, ofs + ops->ooboffs, ops->len,
1106 &ops->retlen, ops->oobbuf);
1108 mutex_unlock(&this->lock);
1109 return ret;
1112 static int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
1114 struct DiskOnChip *this = mtd->priv;
1115 __u32 ofs = instr->addr;
1116 __u32 len = instr->len;
1117 volatile int dummy;
1118 void __iomem *docptr = this->virtadr;
1119 struct Nand *mychip;
1120 int status;
1122 mutex_lock(&this->lock);
1124 if (ofs & (mtd->erasesize-1) || len & (mtd->erasesize-1)) {
1125 mutex_unlock(&this->lock);
1126 return -EINVAL;
1129 instr->state = MTD_ERASING;
1131 /* FIXME: Do this in the background. Use timers or schedule_task() */
1132 while(len) {
1133 mychip = &this->chips[ofs >> this->chipshift];
1135 if (this->curfloor != mychip->floor) {
1136 DoC_SelectFloor(this, mychip->floor);
1137 DoC_SelectChip(this, mychip->chip);
1138 } else if (this->curchip != mychip->chip) {
1139 DoC_SelectChip(this, mychip->chip);
1141 this->curfloor = mychip->floor;
1142 this->curchip = mychip->chip;
1144 DoC_Command(this, NAND_CMD_ERASE1, 0);
1145 DoC_Address(this, ADDR_PAGE, ofs, 0, 0);
1146 DoC_Command(this, NAND_CMD_ERASE2, 0);
1148 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
1150 if (DoC_is_Millennium(this)) {
1151 ReadDOC(docptr, ReadPipeInit);
1152 status = ReadDOC(docptr, LastDataRead);
1153 } else {
1154 dummy = ReadDOC(docptr, CDSNSlowIO);
1155 DoC_Delay(this, 2);
1156 status = ReadDOC_(docptr, this->ioreg);
1159 if (status & 1) {
1160 printk(KERN_ERR "Error erasing at 0x%x\n", ofs);
1161 /* There was an error */
1162 instr->state = MTD_ERASE_FAILED;
1163 goto callback;
1165 ofs += mtd->erasesize;
1166 len -= mtd->erasesize;
1168 instr->state = MTD_ERASE_DONE;
1170 callback:
1171 mtd_erase_callback(instr);
1173 mutex_unlock(&this->lock);
1174 return 0;
1178 /****************************************************************************
1180 * Module stuff
1182 ****************************************************************************/
1184 static void __exit cleanup_doc2000(void)
1186 struct mtd_info *mtd;
1187 struct DiskOnChip *this;
1189 while ((mtd = doc2klist)) {
1190 this = mtd->priv;
1191 doc2klist = this->nextdoc;
1193 del_mtd_device(mtd);
1195 iounmap(this->virtadr);
1196 kfree(this->chips);
1197 kfree(mtd);
1201 module_exit(cleanup_doc2000);
1203 MODULE_LICENSE("GPL");
1204 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
1205 MODULE_DESCRIPTION("MTD driver for DiskOnChip 2000 and Millennium");