Use dentry_path() to create full path to inode object
[pohmelfs.git] / drivers / mtd / devices / doc2000.c
blobb1cdf647901960fd88ef2bce4a787c21ace8a03d
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 pr_debug("_DoC_WaitReady called for out-of-line wait\n");
87 /* Out-of-line routine to wait for chip response */
88 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
89 /* issue 2 read from NOP register after reading from CDSNControl register
90 see Software Requirement 11.4 item 2. */
91 DoC_Delay(doc, 2);
93 if (time_after(jiffies, timeo)) {
94 pr_debug("_DoC_WaitReady timed out.\n");
95 return -EIO;
97 udelay(1);
98 cond_resched();
101 return 0;
104 static inline int DoC_WaitReady(struct DiskOnChip *doc)
106 void __iomem *docptr = doc->virtadr;
108 /* This is inline, to optimise the common case, where it's ready instantly */
109 int ret = 0;
111 /* 4 read form NOP register should be issued in prior to the read from CDSNControl
112 see Software Requirement 11.4 item 2. */
113 DoC_Delay(doc, 4);
115 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
116 /* Call the out-of-line routine to wait */
117 ret = _DoC_WaitReady(doc);
119 /* issue 2 read from NOP register after reading from CDSNControl register
120 see Software Requirement 11.4 item 2. */
121 DoC_Delay(doc, 2);
123 return ret;
126 /* DoC_Command: Send a flash command to the flash chip through the CDSN Slow IO register to
127 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
128 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
130 static int DoC_Command(struct DiskOnChip *doc, unsigned char command,
131 unsigned char xtraflags)
133 void __iomem *docptr = doc->virtadr;
135 if (DoC_is_2000(doc))
136 xtraflags |= CDSN_CTRL_FLASH_IO;
138 /* Assert the CLE (Command Latch Enable) line to the flash chip */
139 WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
140 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
142 if (DoC_is_Millennium(doc))
143 WriteDOC(command, docptr, CDSNSlowIO);
145 /* Send the command */
146 WriteDOC_(command, docptr, doc->ioreg);
147 if (DoC_is_Millennium(doc))
148 WriteDOC(command, docptr, WritePipeTerm);
150 /* Lower the CLE line */
151 WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
152 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
154 /* Wait for the chip to respond - Software requirement 11.4.1 (extended for any command) */
155 return DoC_WaitReady(doc);
158 /* DoC_Address: Set the current address for the flash chip through the CDSN Slow IO register to
159 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
160 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
162 static int DoC_Address(struct DiskOnChip *doc, int numbytes, unsigned long ofs,
163 unsigned char xtraflags1, unsigned char xtraflags2)
165 int i;
166 void __iomem *docptr = doc->virtadr;
168 if (DoC_is_2000(doc))
169 xtraflags1 |= CDSN_CTRL_FLASH_IO;
171 /* Assert the ALE (Address Latch Enable) line to the flash chip */
172 WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
174 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
176 /* Send the address */
177 /* Devices with 256-byte page are addressed as:
178 Column (bits 0-7), Page (bits 8-15, 16-23, 24-31)
179 * there is no device on the market with page256
180 and more than 24 bits.
181 Devices with 512-byte page are addressed as:
182 Column (bits 0-7), Page (bits 9-16, 17-24, 25-31)
183 * 25-31 is sent only if the chip support it.
184 * bit 8 changes the read command to be sent
185 (NAND_CMD_READ0 or NAND_CMD_READ1).
188 if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE) {
189 if (DoC_is_Millennium(doc))
190 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
191 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
194 if (doc->page256) {
195 ofs = ofs >> 8;
196 } else {
197 ofs = ofs >> 9;
200 if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE) {
201 for (i = 0; i < doc->pageadrlen; i++, ofs = ofs >> 8) {
202 if (DoC_is_Millennium(doc))
203 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
204 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
208 if (DoC_is_Millennium(doc))
209 WriteDOC(ofs & 0xff, docptr, WritePipeTerm);
211 DoC_Delay(doc, 2); /* Needed for some slow flash chips. mf. */
213 /* FIXME: The SlowIO's for millennium could be replaced by
214 a single WritePipeTerm here. mf. */
216 /* Lower the ALE line */
217 WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr,
218 CDSNControl);
220 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
222 /* Wait for the chip to respond - Software requirement 11.4.1 */
223 return DoC_WaitReady(doc);
226 /* Read a buffer from DoC, taking care of Millennium odditys */
227 static void DoC_ReadBuf(struct DiskOnChip *doc, u_char * buf, int len)
229 volatile int dummy;
230 int modulus = 0xffff;
231 void __iomem *docptr = doc->virtadr;
232 int i;
234 if (len <= 0)
235 return;
237 if (DoC_is_Millennium(doc)) {
238 /* Read the data via the internal pipeline through CDSN IO register,
239 see Pipelined Read Operations 11.3 */
240 dummy = ReadDOC(docptr, ReadPipeInit);
242 /* Millennium should use the LastDataRead register - Pipeline Reads */
243 len--;
245 /* This is needed for correctly ECC calculation */
246 modulus = 0xff;
249 for (i = 0; i < len; i++)
250 buf[i] = ReadDOC_(docptr, doc->ioreg + (i & modulus));
252 if (DoC_is_Millennium(doc)) {
253 buf[i] = ReadDOC(docptr, LastDataRead);
257 /* Write a buffer to DoC, taking care of Millennium odditys */
258 static void DoC_WriteBuf(struct DiskOnChip *doc, const u_char * buf, int len)
260 void __iomem *docptr = doc->virtadr;
261 int i;
263 if (len <= 0)
264 return;
266 for (i = 0; i < len; i++)
267 WriteDOC_(buf[i], docptr, doc->ioreg + i);
269 if (DoC_is_Millennium(doc)) {
270 WriteDOC(0x00, docptr, WritePipeTerm);
275 /* DoC_SelectChip: Select a given flash chip within the current floor */
277 static inline int DoC_SelectChip(struct DiskOnChip *doc, int chip)
279 void __iomem *docptr = doc->virtadr;
281 /* Software requirement 11.4.4 before writing DeviceSelect */
282 /* Deassert the CE line to eliminate glitches on the FCE# outputs */
283 WriteDOC(CDSN_CTRL_WP, docptr, CDSNControl);
284 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
286 /* Select the individual flash chip requested */
287 WriteDOC(chip, docptr, CDSNDeviceSelect);
288 DoC_Delay(doc, 4);
290 /* Reassert the CE line */
291 WriteDOC(CDSN_CTRL_CE | CDSN_CTRL_FLASH_IO | CDSN_CTRL_WP, docptr,
292 CDSNControl);
293 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
295 /* Wait for it to be ready */
296 return DoC_WaitReady(doc);
299 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
301 static inline int DoC_SelectFloor(struct DiskOnChip *doc, int floor)
303 void __iomem *docptr = doc->virtadr;
305 /* Select the floor (bank) of chips required */
306 WriteDOC(floor, docptr, FloorSelect);
308 /* Wait for the chip to be ready */
309 return DoC_WaitReady(doc);
312 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
314 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
316 int mfr, id, i, j;
317 volatile char dummy;
319 /* Page in the required floor/chip */
320 DoC_SelectFloor(doc, floor);
321 DoC_SelectChip(doc, chip);
323 /* Reset the chip */
324 if (DoC_Command(doc, NAND_CMD_RESET, CDSN_CTRL_WP)) {
325 pr_debug("DoC_Command (reset) for %d,%d returned true\n",
326 floor, chip);
327 return 0;
331 /* Read the NAND chip ID: 1. Send ReadID command */
332 if (DoC_Command(doc, NAND_CMD_READID, CDSN_CTRL_WP)) {
333 pr_debug("DoC_Command (ReadID) for %d,%d returned true\n",
334 floor, chip);
335 return 0;
338 /* Read the NAND chip ID: 2. Send address byte zero */
339 DoC_Address(doc, ADDR_COLUMN, 0, CDSN_CTRL_WP, 0);
341 /* Read the manufacturer and device id codes from the device */
343 if (DoC_is_Millennium(doc)) {
344 DoC_Delay(doc, 2);
345 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
346 mfr = ReadDOC(doc->virtadr, LastDataRead);
348 DoC_Delay(doc, 2);
349 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
350 id = ReadDOC(doc->virtadr, LastDataRead);
351 } else {
352 /* CDSN Slow IO register see Software Req 11.4 item 5. */
353 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
354 DoC_Delay(doc, 2);
355 mfr = ReadDOC_(doc->virtadr, doc->ioreg);
357 /* CDSN Slow IO register see Software Req 11.4 item 5. */
358 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
359 DoC_Delay(doc, 2);
360 id = ReadDOC_(doc->virtadr, doc->ioreg);
363 /* No response - return failure */
364 if (mfr == 0xff || mfr == 0)
365 return 0;
367 /* Check it's the same as the first chip we identified.
368 * M-Systems say that any given DiskOnChip device should only
369 * contain _one_ type of flash part, although that's not a
370 * hardware restriction. */
371 if (doc->mfr) {
372 if (doc->mfr == mfr && doc->id == id)
373 return 1; /* This is the same as the first */
374 else
375 printk(KERN_WARNING
376 "Flash chip at floor %d, chip %d is different:\n",
377 floor, chip);
380 /* Print and store the manufacturer and ID codes. */
381 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
382 if (id == nand_flash_ids[i].id) {
383 /* Try to identify manufacturer */
384 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
385 if (nand_manuf_ids[j].id == mfr)
386 break;
388 printk(KERN_INFO
389 "Flash chip found: Manufacturer ID: %2.2X, "
390 "Chip ID: %2.2X (%s:%s)\n", mfr, id,
391 nand_manuf_ids[j].name, nand_flash_ids[i].name);
392 if (!doc->mfr) {
393 doc->mfr = mfr;
394 doc->id = id;
395 doc->chipshift =
396 ffs((nand_flash_ids[i].chipsize << 20)) - 1;
397 doc->page256 = (nand_flash_ids[i].pagesize == 256) ? 1 : 0;
398 doc->pageadrlen = doc->chipshift > 25 ? 3 : 2;
399 doc->erasesize =
400 nand_flash_ids[i].erasesize;
401 return 1;
403 return 0;
408 /* We haven't fully identified the chip. Print as much as we know. */
409 printk(KERN_WARNING "Unknown flash chip found: %2.2X %2.2X\n",
410 id, mfr);
412 printk(KERN_WARNING "Please report to dwmw2@infradead.org\n");
413 return 0;
416 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
418 static void DoC_ScanChips(struct DiskOnChip *this, int maxchips)
420 int floor, chip;
421 int numchips[MAX_FLOORS];
422 int ret = 1;
424 this->numchips = 0;
425 this->mfr = 0;
426 this->id = 0;
428 /* For each floor, find the number of valid chips it contains */
429 for (floor = 0; floor < MAX_FLOORS; floor++) {
430 ret = 1;
431 numchips[floor] = 0;
432 for (chip = 0; chip < maxchips && ret != 0; chip++) {
434 ret = DoC_IdentChip(this, floor, chip);
435 if (ret) {
436 numchips[floor]++;
437 this->numchips++;
442 /* If there are none at all that we recognise, bail */
443 if (!this->numchips) {
444 printk(KERN_NOTICE "No flash chips recognised.\n");
445 return;
448 /* Allocate an array to hold the information for each chip */
449 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
450 if (!this->chips) {
451 printk(KERN_NOTICE "No memory for allocating chip info structures\n");
452 return;
455 ret = 0;
457 /* Fill out the chip array with {floor, chipno} for each
458 * detected chip in the device. */
459 for (floor = 0; floor < MAX_FLOORS; floor++) {
460 for (chip = 0; chip < numchips[floor]; chip++) {
461 this->chips[ret].floor = floor;
462 this->chips[ret].chip = chip;
463 this->chips[ret].curadr = 0;
464 this->chips[ret].curmode = 0x50;
465 ret++;
469 /* Calculate and print the total size of the device */
470 this->totlen = this->numchips * (1 << this->chipshift);
472 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
473 this->numchips, this->totlen >> 20);
476 static int DoC2k_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
478 int tmp1, tmp2, retval;
479 if (doc1->physadr == doc2->physadr)
480 return 1;
482 /* Use the alias resolution register which was set aside for this
483 * purpose. If it's value is the same on both chips, they might
484 * be the same chip, and we write to one and check for a change in
485 * the other. It's unclear if this register is usuable in the
486 * DoC 2000 (it's in the Millennium docs), but it seems to work. */
487 tmp1 = ReadDOC(doc1->virtadr, AliasResolution);
488 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
489 if (tmp1 != tmp2)
490 return 0;
492 WriteDOC((tmp1 + 1) % 0xff, doc1->virtadr, AliasResolution);
493 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
494 if (tmp2 == (tmp1 + 1) % 0xff)
495 retval = 1;
496 else
497 retval = 0;
499 /* Restore register contents. May not be necessary, but do it just to
500 * be safe. */
501 WriteDOC(tmp1, doc1->virtadr, AliasResolution);
503 return retval;
506 /* This routine is found from the docprobe code by symbol_get(),
507 * which will bump the use count of this module. */
508 void DoC2k_init(struct mtd_info *mtd)
510 struct DiskOnChip *this = mtd->priv;
511 struct DiskOnChip *old = NULL;
512 int maxchips;
514 /* We must avoid being called twice for the same device. */
516 if (doc2klist)
517 old = doc2klist->priv;
519 while (old) {
520 if (DoC2k_is_alias(old, this)) {
521 printk(KERN_NOTICE
522 "Ignoring DiskOnChip 2000 at 0x%lX - already configured\n",
523 this->physadr);
524 iounmap(this->virtadr);
525 kfree(mtd);
526 return;
528 if (old->nextdoc)
529 old = old->nextdoc->priv;
530 else
531 old = NULL;
535 switch (this->ChipID) {
536 case DOC_ChipID_Doc2kTSOP:
537 mtd->name = "DiskOnChip 2000 TSOP";
538 this->ioreg = DoC_Mil_CDSN_IO;
539 /* Pretend it's a Millennium */
540 this->ChipID = DOC_ChipID_DocMil;
541 maxchips = MAX_CHIPS;
542 break;
543 case DOC_ChipID_Doc2k:
544 mtd->name = "DiskOnChip 2000";
545 this->ioreg = DoC_2k_CDSN_IO;
546 maxchips = MAX_CHIPS;
547 break;
548 case DOC_ChipID_DocMil:
549 mtd->name = "DiskOnChip Millennium";
550 this->ioreg = DoC_Mil_CDSN_IO;
551 maxchips = MAX_CHIPS_MIL;
552 break;
553 default:
554 printk("Unknown ChipID 0x%02x\n", this->ChipID);
555 kfree(mtd);
556 iounmap(this->virtadr);
557 return;
560 printk(KERN_NOTICE "%s found at address 0x%lX\n", mtd->name,
561 this->physadr);
563 mtd->type = MTD_NANDFLASH;
564 mtd->flags = MTD_CAP_NANDFLASH;
565 mtd->writesize = 512;
566 mtd->oobsize = 16;
567 mtd->owner = THIS_MODULE;
568 mtd->erase = doc_erase;
569 mtd->read = doc_read;
570 mtd->write = doc_write;
571 mtd->read_oob = doc_read_oob;
572 mtd->write_oob = doc_write_oob;
573 this->curfloor = -1;
574 this->curchip = -1;
575 mutex_init(&this->lock);
577 /* Ident all the chips present. */
578 DoC_ScanChips(this, maxchips);
580 if (!this->totlen) {
581 kfree(mtd);
582 iounmap(this->virtadr);
583 } else {
584 this->nextdoc = doc2klist;
585 doc2klist = mtd;
586 mtd->size = this->totlen;
587 mtd->erasesize = this->erasesize;
588 mtd_device_register(mtd, NULL, 0);
589 return;
592 EXPORT_SYMBOL_GPL(DoC2k_init);
594 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
595 size_t * retlen, u_char * buf)
597 struct DiskOnChip *this = mtd->priv;
598 void __iomem *docptr = this->virtadr;
599 struct Nand *mychip;
600 unsigned char syndrome[6], eccbuf[6];
601 volatile char dummy;
602 int i, len256 = 0, ret=0;
603 size_t left = len;
605 /* Don't allow read past end of device */
606 if (from >= this->totlen)
607 return -EINVAL;
609 mutex_lock(&this->lock);
611 *retlen = 0;
612 while (left) {
613 len = left;
615 /* Don't allow a single read to cross a 512-byte block boundary */
616 if (from + len > ((from | 0x1ff) + 1))
617 len = ((from | 0x1ff) + 1) - from;
619 /* The ECC will not be calculated correctly if less than 512 is read */
620 if (len != 0x200)
621 printk(KERN_WARNING
622 "ECC needs a full sector read (adr: %lx size %lx)\n",
623 (long) from, (long) len);
625 /* printk("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len); */
628 /* Find the chip which is to be used and select it */
629 mychip = &this->chips[from >> (this->chipshift)];
631 if (this->curfloor != mychip->floor) {
632 DoC_SelectFloor(this, mychip->floor);
633 DoC_SelectChip(this, mychip->chip);
634 } else if (this->curchip != mychip->chip) {
635 DoC_SelectChip(this, mychip->chip);
638 this->curfloor = mychip->floor;
639 this->curchip = mychip->chip;
641 DoC_Command(this,
642 (!this->page256
643 && (from & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
644 CDSN_CTRL_WP);
645 DoC_Address(this, ADDR_COLUMN_PAGE, from, CDSN_CTRL_WP,
646 CDSN_CTRL_ECC_IO);
648 /* Prime the ECC engine */
649 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
650 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
652 /* treat crossing 256-byte sector for 2M x 8bits devices */
653 if (this->page256 && from + len > (from | 0xff) + 1) {
654 len256 = (from | 0xff) + 1 - from;
655 DoC_ReadBuf(this, buf, len256);
657 DoC_Command(this, NAND_CMD_READ0, CDSN_CTRL_WP);
658 DoC_Address(this, ADDR_COLUMN_PAGE, from + len256,
659 CDSN_CTRL_WP, CDSN_CTRL_ECC_IO);
662 DoC_ReadBuf(this, &buf[len256], len - len256);
664 /* Let the caller know we completed it */
665 *retlen += len;
667 /* Read the ECC data through the DiskOnChip ECC logic */
668 /* Note: this will work even with 2M x 8bit devices as */
669 /* they have 8 bytes of OOB per 256 page. mf. */
670 DoC_ReadBuf(this, eccbuf, 6);
672 /* Flush the pipeline */
673 if (DoC_is_Millennium(this)) {
674 dummy = ReadDOC(docptr, ECCConf);
675 dummy = ReadDOC(docptr, ECCConf);
676 i = ReadDOC(docptr, ECCConf);
677 } else {
678 dummy = ReadDOC(docptr, 2k_ECCStatus);
679 dummy = ReadDOC(docptr, 2k_ECCStatus);
680 i = ReadDOC(docptr, 2k_ECCStatus);
683 /* Check the ECC Status */
684 if (i & 0x80) {
685 int nb_errors;
686 /* There was an ECC error */
687 #ifdef ECC_DEBUG
688 printk(KERN_ERR "DiskOnChip ECC Error: Read at %lx\n", (long)from);
689 #endif
690 /* Read the ECC syndrome through the DiskOnChip ECC
691 logic. These syndrome will be all ZERO when there
692 is no error */
693 for (i = 0; i < 6; i++) {
694 syndrome[i] =
695 ReadDOC(docptr, ECCSyndrome0 + i);
697 nb_errors = doc_decode_ecc(buf, syndrome);
699 #ifdef ECC_DEBUG
700 printk(KERN_ERR "Errors corrected: %x\n", nb_errors);
701 #endif
702 if (nb_errors < 0) {
703 /* We return error, but have actually done the
704 read. Not that this can be told to
705 user-space, via sys_read(), but at least
706 MTD-aware stuff can know about it by
707 checking *retlen */
708 ret = -EIO;
712 #ifdef PSYCHO_DEBUG
713 printk(KERN_DEBUG "ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
714 (long)from, eccbuf[0], eccbuf[1], eccbuf[2],
715 eccbuf[3], eccbuf[4], eccbuf[5]);
716 #endif
718 /* disable the ECC engine */
719 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
721 /* according to 11.4.1, we need to wait for the busy line
722 * drop if we read to the end of the page. */
723 if(0 == ((from + len) & 0x1ff))
725 DoC_WaitReady(this);
728 from += len;
729 left -= len;
730 buf += len;
733 mutex_unlock(&this->lock);
735 return ret;
738 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
739 size_t * retlen, const u_char * buf)
741 struct DiskOnChip *this = mtd->priv;
742 int di; /* Yes, DI is a hangover from when I was disassembling the binary driver */
743 void __iomem *docptr = this->virtadr;
744 unsigned char eccbuf[6];
745 volatile char dummy;
746 int len256 = 0;
747 struct Nand *mychip;
748 size_t left = len;
749 int status;
751 /* Don't allow write past end of device */
752 if (to >= this->totlen)
753 return -EINVAL;
755 mutex_lock(&this->lock);
757 *retlen = 0;
758 while (left) {
759 len = left;
761 /* Don't allow a single write to cross a 512-byte block boundary */
762 if (to + len > ((to | 0x1ff) + 1))
763 len = ((to | 0x1ff) + 1) - to;
765 /* The ECC will not be calculated correctly if less than 512 is written */
766 /* DBB-
767 if (len != 0x200 && eccbuf)
768 printk(KERN_WARNING
769 "ECC needs a full sector write (adr: %lx size %lx)\n",
770 (long) to, (long) len);
771 -DBB */
773 /* printk("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */
775 /* Find the chip which is to be used and select it */
776 mychip = &this->chips[to >> (this->chipshift)];
778 if (this->curfloor != mychip->floor) {
779 DoC_SelectFloor(this, mychip->floor);
780 DoC_SelectChip(this, mychip->chip);
781 } else if (this->curchip != mychip->chip) {
782 DoC_SelectChip(this, mychip->chip);
785 this->curfloor = mychip->floor;
786 this->curchip = mychip->chip;
788 /* Set device to main plane of flash */
789 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
790 DoC_Command(this,
791 (!this->page256
792 && (to & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
793 CDSN_CTRL_WP);
795 DoC_Command(this, NAND_CMD_SEQIN, 0);
796 DoC_Address(this, ADDR_COLUMN_PAGE, to, 0, CDSN_CTRL_ECC_IO);
798 /* Prime the ECC engine */
799 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
800 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
802 /* treat crossing 256-byte sector for 2M x 8bits devices */
803 if (this->page256 && to + len > (to | 0xff) + 1) {
804 len256 = (to | 0xff) + 1 - to;
805 DoC_WriteBuf(this, buf, len256);
807 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
809 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
810 /* There's an implicit DoC_WaitReady() in DoC_Command */
812 dummy = ReadDOC(docptr, CDSNSlowIO);
813 DoC_Delay(this, 2);
815 if (ReadDOC_(docptr, this->ioreg) & 1) {
816 printk(KERN_ERR "Error programming flash\n");
817 /* Error in programming */
818 *retlen = 0;
819 mutex_unlock(&this->lock);
820 return -EIO;
823 DoC_Command(this, NAND_CMD_SEQIN, 0);
824 DoC_Address(this, ADDR_COLUMN_PAGE, to + len256, 0,
825 CDSN_CTRL_ECC_IO);
828 DoC_WriteBuf(this, &buf[len256], len - len256);
830 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_CE, docptr, CDSNControl);
832 if (DoC_is_Millennium(this)) {
833 WriteDOC(0, docptr, NOP);
834 WriteDOC(0, docptr, NOP);
835 WriteDOC(0, docptr, NOP);
836 } else {
837 WriteDOC_(0, docptr, this->ioreg);
838 WriteDOC_(0, docptr, this->ioreg);
839 WriteDOC_(0, docptr, this->ioreg);
842 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_FLASH_IO | CDSN_CTRL_CE, docptr,
843 CDSNControl);
845 /* Read the ECC data through the DiskOnChip ECC logic */
846 for (di = 0; di < 6; di++) {
847 eccbuf[di] = ReadDOC(docptr, ECCSyndrome0 + di);
850 /* Reset the ECC engine */
851 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
853 #ifdef PSYCHO_DEBUG
854 printk
855 ("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
856 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
857 eccbuf[4], eccbuf[5]);
858 #endif
859 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
861 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
862 /* There's an implicit DoC_WaitReady() in DoC_Command */
864 if (DoC_is_Millennium(this)) {
865 ReadDOC(docptr, ReadPipeInit);
866 status = ReadDOC(docptr, LastDataRead);
867 } else {
868 dummy = ReadDOC(docptr, CDSNSlowIO);
869 DoC_Delay(this, 2);
870 status = ReadDOC_(docptr, this->ioreg);
873 if (status & 1) {
874 printk(KERN_ERR "Error programming flash\n");
875 /* Error in programming */
876 *retlen = 0;
877 mutex_unlock(&this->lock);
878 return -EIO;
881 /* Let the caller know we completed it */
882 *retlen += len;
885 unsigned char x[8];
886 size_t dummy;
887 int ret;
889 /* Write the ECC data to flash */
890 for (di=0; di<6; di++)
891 x[di] = eccbuf[di];
893 x[6]=0x55;
894 x[7]=0x55;
896 ret = doc_write_oob_nolock(mtd, to, 8, &dummy, x);
897 if (ret) {
898 mutex_unlock(&this->lock);
899 return ret;
903 to += len;
904 left -= len;
905 buf += len;
908 mutex_unlock(&this->lock);
909 return 0;
912 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
913 struct mtd_oob_ops *ops)
915 struct DiskOnChip *this = mtd->priv;
916 int len256 = 0, ret;
917 struct Nand *mychip;
918 uint8_t *buf = ops->oobbuf;
919 size_t len = ops->len;
921 BUG_ON(ops->mode != MTD_OPS_PLACE_OOB);
923 ofs += ops->ooboffs;
925 mutex_lock(&this->lock);
927 mychip = &this->chips[ofs >> this->chipshift];
929 if (this->curfloor != mychip->floor) {
930 DoC_SelectFloor(this, mychip->floor);
931 DoC_SelectChip(this, mychip->chip);
932 } else if (this->curchip != mychip->chip) {
933 DoC_SelectChip(this, mychip->chip);
935 this->curfloor = mychip->floor;
936 this->curchip = mychip->chip;
938 /* update address for 2M x 8bit devices. OOB starts on the second */
939 /* page to maintain compatibility with doc_read_ecc. */
940 if (this->page256) {
941 if (!(ofs & 0x8))
942 ofs += 0x100;
943 else
944 ofs -= 0x8;
947 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
948 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, CDSN_CTRL_WP, 0);
950 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
951 /* Note: datasheet says it should automaticaly wrap to the */
952 /* next OOB block, but it didn't work here. mf. */
953 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
954 len256 = (ofs | 0x7) + 1 - ofs;
955 DoC_ReadBuf(this, buf, len256);
957 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
958 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff),
959 CDSN_CTRL_WP, 0);
962 DoC_ReadBuf(this, &buf[len256], len - len256);
964 ops->retlen = len;
965 /* Reading the full OOB data drops us off of the end of the page,
966 * causing the flash device to go into busy mode, so we need
967 * to wait until ready 11.4.1 and Toshiba TC58256FT docs */
969 ret = DoC_WaitReady(this);
971 mutex_unlock(&this->lock);
972 return ret;
976 static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
977 size_t * retlen, const u_char * buf)
979 struct DiskOnChip *this = mtd->priv;
980 int len256 = 0;
981 void __iomem *docptr = this->virtadr;
982 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
983 volatile int dummy;
984 int status;
986 // 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,
987 // buf[0], buf[1], buf[2], buf[3], buf[8], buf[9], buf[14],buf[15]);
989 /* Find the chip which is to be used and select it */
990 if (this->curfloor != mychip->floor) {
991 DoC_SelectFloor(this, mychip->floor);
992 DoC_SelectChip(this, mychip->chip);
993 } else if (this->curchip != mychip->chip) {
994 DoC_SelectChip(this, mychip->chip);
996 this->curfloor = mychip->floor;
997 this->curchip = mychip->chip;
999 /* disable the ECC engine */
1000 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
1001 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
1003 /* Reset the chip, see Software Requirement 11.4 item 1. */
1004 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
1006 /* issue the Read2 command to set the pointer to the Spare Data Area. */
1007 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1009 /* update address for 2M x 8bit devices. OOB starts on the second */
1010 /* page to maintain compatibility with doc_read_ecc. */
1011 if (this->page256) {
1012 if (!(ofs & 0x8))
1013 ofs += 0x100;
1014 else
1015 ofs -= 0x8;
1018 /* issue the Serial Data In command to initial the Page Program process */
1019 DoC_Command(this, NAND_CMD_SEQIN, 0);
1020 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, 0, 0);
1022 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1023 /* Note: datasheet says it should automaticaly wrap to the */
1024 /* next OOB block, but it didn't work here. mf. */
1025 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1026 len256 = (ofs | 0x7) + 1 - ofs;
1027 DoC_WriteBuf(this, buf, len256);
1029 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1030 DoC_Command(this, NAND_CMD_STATUS, 0);
1031 /* DoC_WaitReady() is implicit in DoC_Command */
1033 if (DoC_is_Millennium(this)) {
1034 ReadDOC(docptr, ReadPipeInit);
1035 status = ReadDOC(docptr, LastDataRead);
1036 } else {
1037 dummy = ReadDOC(docptr, CDSNSlowIO);
1038 DoC_Delay(this, 2);
1039 status = ReadDOC_(docptr, this->ioreg);
1042 if (status & 1) {
1043 printk(KERN_ERR "Error programming oob data\n");
1044 /* There was an error */
1045 *retlen = 0;
1046 return -EIO;
1048 DoC_Command(this, NAND_CMD_SEQIN, 0);
1049 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), 0, 0);
1052 DoC_WriteBuf(this, &buf[len256], len - len256);
1054 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1055 DoC_Command(this, NAND_CMD_STATUS, 0);
1056 /* DoC_WaitReady() is implicit in DoC_Command */
1058 if (DoC_is_Millennium(this)) {
1059 ReadDOC(docptr, ReadPipeInit);
1060 status = ReadDOC(docptr, LastDataRead);
1061 } else {
1062 dummy = ReadDOC(docptr, CDSNSlowIO);
1063 DoC_Delay(this, 2);
1064 status = ReadDOC_(docptr, this->ioreg);
1067 if (status & 1) {
1068 printk(KERN_ERR "Error programming oob data\n");
1069 /* There was an error */
1070 *retlen = 0;
1071 return -EIO;
1074 *retlen = len;
1075 return 0;
1079 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
1080 struct mtd_oob_ops *ops)
1082 struct DiskOnChip *this = mtd->priv;
1083 int ret;
1085 BUG_ON(ops->mode != MTD_OPS_PLACE_OOB);
1087 mutex_lock(&this->lock);
1088 ret = doc_write_oob_nolock(mtd, ofs + ops->ooboffs, ops->len,
1089 &ops->retlen, ops->oobbuf);
1091 mutex_unlock(&this->lock);
1092 return ret;
1095 static int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
1097 struct DiskOnChip *this = mtd->priv;
1098 __u32 ofs = instr->addr;
1099 __u32 len = instr->len;
1100 volatile int dummy;
1101 void __iomem *docptr = this->virtadr;
1102 struct Nand *mychip;
1103 int status;
1105 mutex_lock(&this->lock);
1107 if (ofs & (mtd->erasesize-1) || len & (mtd->erasesize-1)) {
1108 mutex_unlock(&this->lock);
1109 return -EINVAL;
1112 instr->state = MTD_ERASING;
1114 /* FIXME: Do this in the background. Use timers or schedule_task() */
1115 while(len) {
1116 mychip = &this->chips[ofs >> this->chipshift];
1118 if (this->curfloor != mychip->floor) {
1119 DoC_SelectFloor(this, mychip->floor);
1120 DoC_SelectChip(this, mychip->chip);
1121 } else if (this->curchip != mychip->chip) {
1122 DoC_SelectChip(this, mychip->chip);
1124 this->curfloor = mychip->floor;
1125 this->curchip = mychip->chip;
1127 DoC_Command(this, NAND_CMD_ERASE1, 0);
1128 DoC_Address(this, ADDR_PAGE, ofs, 0, 0);
1129 DoC_Command(this, NAND_CMD_ERASE2, 0);
1131 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
1133 if (DoC_is_Millennium(this)) {
1134 ReadDOC(docptr, ReadPipeInit);
1135 status = ReadDOC(docptr, LastDataRead);
1136 } else {
1137 dummy = ReadDOC(docptr, CDSNSlowIO);
1138 DoC_Delay(this, 2);
1139 status = ReadDOC_(docptr, this->ioreg);
1142 if (status & 1) {
1143 printk(KERN_ERR "Error erasing at 0x%x\n", ofs);
1144 /* There was an error */
1145 instr->state = MTD_ERASE_FAILED;
1146 goto callback;
1148 ofs += mtd->erasesize;
1149 len -= mtd->erasesize;
1151 instr->state = MTD_ERASE_DONE;
1153 callback:
1154 mtd_erase_callback(instr);
1156 mutex_unlock(&this->lock);
1157 return 0;
1161 /****************************************************************************
1163 * Module stuff
1165 ****************************************************************************/
1167 static void __exit cleanup_doc2000(void)
1169 struct mtd_info *mtd;
1170 struct DiskOnChip *this;
1172 while ((mtd = doc2klist)) {
1173 this = mtd->priv;
1174 doc2klist = this->nextdoc;
1176 mtd_device_unregister(mtd);
1178 iounmap(this->virtadr);
1179 kfree(this->chips);
1180 kfree(mtd);
1184 module_exit(cleanup_doc2000);
1186 MODULE_LICENSE("GPL");
1187 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
1188 MODULE_DESCRIPTION("MTD driver for DiskOnChip 2000 and Millennium");