Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / mtd / devices / doc2000.c
blob5fc532895a24eb37b7b859ea8e767e8f50812071
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.66 2005/01/05 18:05:12 dwmw2 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>
24 #include <linux/mtd/mtd.h>
25 #include <linux/mtd/nand.h>
26 #include <linux/mtd/doc2000.h>
28 #define DOC_SUPPORT_2000
29 #define DOC_SUPPORT_2000TSOP
30 #define DOC_SUPPORT_MILLENNIUM
32 #ifdef DOC_SUPPORT_2000
33 #define DoC_is_2000(doc) (doc->ChipID == DOC_ChipID_Doc2k)
34 #else
35 #define DoC_is_2000(doc) (0)
36 #endif
38 #if defined(DOC_SUPPORT_2000TSOP) || defined(DOC_SUPPORT_MILLENNIUM)
39 #define DoC_is_Millennium(doc) (doc->ChipID == DOC_ChipID_DocMil)
40 #else
41 #define DoC_is_Millennium(doc) (0)
42 #endif
44 /* #define ECC_DEBUG */
46 /* I have no idea why some DoC chips can not use memcpy_from|to_io().
47 * This may be due to the different revisions of the ASIC controller built-in or
48 * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
49 * this:
50 #undef USE_MEMCPY
53 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
54 size_t *retlen, u_char *buf);
55 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
56 size_t *retlen, const u_char *buf);
57 static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
58 size_t *retlen, u_char *buf, u_char *eccbuf, struct nand_oobinfo *oobsel);
59 static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
60 size_t *retlen, const u_char *buf, u_char *eccbuf, struct nand_oobinfo *oobsel);
61 static int doc_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs,
62 unsigned long count, loff_t to, size_t *retlen,
63 u_char *eccbuf, struct nand_oobinfo *oobsel);
64 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
65 size_t *retlen, u_char *buf);
66 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
67 size_t *retlen, const u_char *buf);
68 static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
69 size_t *retlen, const u_char *buf);
70 static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
72 static struct mtd_info *doc2klist = NULL;
74 /* Perform the required delay cycles by reading from the appropriate register */
75 static void DoC_Delay(struct DiskOnChip *doc, unsigned short cycles)
77 volatile char dummy;
78 int i;
80 for (i = 0; i < cycles; i++) {
81 if (DoC_is_Millennium(doc))
82 dummy = ReadDOC(doc->virtadr, NOP);
83 else
84 dummy = ReadDOC(doc->virtadr, DOCStatus);
89 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
90 static int _DoC_WaitReady(struct DiskOnChip *doc)
92 void __iomem *docptr = doc->virtadr;
93 unsigned long timeo = jiffies + (HZ * 10);
95 DEBUG(MTD_DEBUG_LEVEL3,
96 "_DoC_WaitReady called for out-of-line wait\n");
98 /* Out-of-line routine to wait for chip response */
99 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
100 /* issue 2 read from NOP register after reading from CDSNControl register
101 see Software Requirement 11.4 item 2. */
102 DoC_Delay(doc, 2);
104 if (time_after(jiffies, timeo)) {
105 DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
106 return -EIO;
108 udelay(1);
109 cond_resched();
112 return 0;
115 static inline int DoC_WaitReady(struct DiskOnChip *doc)
117 void __iomem *docptr = doc->virtadr;
119 /* This is inline, to optimise the common case, where it's ready instantly */
120 int ret = 0;
122 /* 4 read form NOP register should be issued in prior to the read from CDSNControl
123 see Software Requirement 11.4 item 2. */
124 DoC_Delay(doc, 4);
126 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
127 /* Call the out-of-line routine to wait */
128 ret = _DoC_WaitReady(doc);
130 /* issue 2 read from NOP register after reading from CDSNControl register
131 see Software Requirement 11.4 item 2. */
132 DoC_Delay(doc, 2);
134 return ret;
137 /* DoC_Command: Send a flash command to the flash chip through the CDSN Slow IO register to
138 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
139 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
141 static inline int DoC_Command(struct DiskOnChip *doc, unsigned char command,
142 unsigned char xtraflags)
144 void __iomem *docptr = doc->virtadr;
146 if (DoC_is_2000(doc))
147 xtraflags |= CDSN_CTRL_FLASH_IO;
149 /* Assert the CLE (Command Latch Enable) line to the flash chip */
150 WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
151 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
153 if (DoC_is_Millennium(doc))
154 WriteDOC(command, docptr, CDSNSlowIO);
156 /* Send the command */
157 WriteDOC_(command, docptr, doc->ioreg);
158 if (DoC_is_Millennium(doc))
159 WriteDOC(command, docptr, WritePipeTerm);
161 /* Lower the CLE line */
162 WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
163 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
165 /* Wait for the chip to respond - Software requirement 11.4.1 (extended for any command) */
166 return DoC_WaitReady(doc);
169 /* DoC_Address: Set the current address for the flash chip through the CDSN Slow IO register to
170 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
171 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
173 static int DoC_Address(struct DiskOnChip *doc, int numbytes, unsigned long ofs,
174 unsigned char xtraflags1, unsigned char xtraflags2)
176 int i;
177 void __iomem *docptr = doc->virtadr;
179 if (DoC_is_2000(doc))
180 xtraflags1 |= CDSN_CTRL_FLASH_IO;
182 /* Assert the ALE (Address Latch Enable) line to the flash chip */
183 WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
185 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
187 /* Send the address */
188 /* Devices with 256-byte page are addressed as:
189 Column (bits 0-7), Page (bits 8-15, 16-23, 24-31)
190 * there is no device on the market with page256
191 and more than 24 bits.
192 Devices with 512-byte page are addressed as:
193 Column (bits 0-7), Page (bits 9-16, 17-24, 25-31)
194 * 25-31 is sent only if the chip support it.
195 * bit 8 changes the read command to be sent
196 (NAND_CMD_READ0 or NAND_CMD_READ1).
199 if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE) {
200 if (DoC_is_Millennium(doc))
201 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
202 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
205 if (doc->page256) {
206 ofs = ofs >> 8;
207 } else {
208 ofs = ofs >> 9;
211 if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE) {
212 for (i = 0; i < doc->pageadrlen; i++, ofs = ofs >> 8) {
213 if (DoC_is_Millennium(doc))
214 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
215 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
219 if (DoC_is_Millennium(doc))
220 WriteDOC(ofs & 0xff, docptr, WritePipeTerm);
222 DoC_Delay(doc, 2); /* Needed for some slow flash chips. mf. */
224 /* FIXME: The SlowIO's for millennium could be replaced by
225 a single WritePipeTerm here. mf. */
227 /* Lower the ALE line */
228 WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr,
229 CDSNControl);
231 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
233 /* Wait for the chip to respond - Software requirement 11.4.1 */
234 return DoC_WaitReady(doc);
237 /* Read a buffer from DoC, taking care of Millennium odditys */
238 static void DoC_ReadBuf(struct DiskOnChip *doc, u_char * buf, int len)
240 volatile int dummy;
241 int modulus = 0xffff;
242 void __iomem *docptr = doc->virtadr;
243 int i;
245 if (len <= 0)
246 return;
248 if (DoC_is_Millennium(doc)) {
249 /* Read the data via the internal pipeline through CDSN IO register,
250 see Pipelined Read Operations 11.3 */
251 dummy = ReadDOC(docptr, ReadPipeInit);
253 /* Millennium should use the LastDataRead register - Pipeline Reads */
254 len--;
256 /* This is needed for correctly ECC calculation */
257 modulus = 0xff;
260 for (i = 0; i < len; i++)
261 buf[i] = ReadDOC_(docptr, doc->ioreg + (i & modulus));
263 if (DoC_is_Millennium(doc)) {
264 buf[i] = ReadDOC(docptr, LastDataRead);
268 /* Write a buffer to DoC, taking care of Millennium odditys */
269 static void DoC_WriteBuf(struct DiskOnChip *doc, const u_char * buf, int len)
271 void __iomem *docptr = doc->virtadr;
272 int i;
274 if (len <= 0)
275 return;
277 for (i = 0; i < len; i++)
278 WriteDOC_(buf[i], docptr, doc->ioreg + i);
280 if (DoC_is_Millennium(doc)) {
281 WriteDOC(0x00, docptr, WritePipeTerm);
286 /* DoC_SelectChip: Select a given flash chip within the current floor */
288 static inline int DoC_SelectChip(struct DiskOnChip *doc, int chip)
290 void __iomem *docptr = doc->virtadr;
292 /* Software requirement 11.4.4 before writing DeviceSelect */
293 /* Deassert the CE line to eliminate glitches on the FCE# outputs */
294 WriteDOC(CDSN_CTRL_WP, docptr, CDSNControl);
295 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
297 /* Select the individual flash chip requested */
298 WriteDOC(chip, docptr, CDSNDeviceSelect);
299 DoC_Delay(doc, 4);
301 /* Reassert the CE line */
302 WriteDOC(CDSN_CTRL_CE | CDSN_CTRL_FLASH_IO | CDSN_CTRL_WP, docptr,
303 CDSNControl);
304 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
306 /* Wait for it to be ready */
307 return DoC_WaitReady(doc);
310 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
312 static inline int DoC_SelectFloor(struct DiskOnChip *doc, int floor)
314 void __iomem *docptr = doc->virtadr;
316 /* Select the floor (bank) of chips required */
317 WriteDOC(floor, docptr, FloorSelect);
319 /* Wait for the chip to be ready */
320 return DoC_WaitReady(doc);
323 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
325 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
327 int mfr, id, i, j;
328 volatile char dummy;
330 /* Page in the required floor/chip */
331 DoC_SelectFloor(doc, floor);
332 DoC_SelectChip(doc, chip);
334 /* Reset the chip */
335 if (DoC_Command(doc, NAND_CMD_RESET, CDSN_CTRL_WP)) {
336 DEBUG(MTD_DEBUG_LEVEL2,
337 "DoC_Command (reset) for %d,%d returned true\n",
338 floor, chip);
339 return 0;
343 /* Read the NAND chip ID: 1. Send ReadID command */
344 if (DoC_Command(doc, NAND_CMD_READID, CDSN_CTRL_WP)) {
345 DEBUG(MTD_DEBUG_LEVEL2,
346 "DoC_Command (ReadID) for %d,%d returned true\n",
347 floor, chip);
348 return 0;
351 /* Read the NAND chip ID: 2. Send address byte zero */
352 DoC_Address(doc, ADDR_COLUMN, 0, CDSN_CTRL_WP, 0);
354 /* Read the manufacturer and device id codes from the device */
356 if (DoC_is_Millennium(doc)) {
357 DoC_Delay(doc, 2);
358 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
359 mfr = ReadDOC(doc->virtadr, LastDataRead);
361 DoC_Delay(doc, 2);
362 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
363 id = ReadDOC(doc->virtadr, LastDataRead);
364 } else {
365 /* CDSN Slow IO register see Software Req 11.4 item 5. */
366 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
367 DoC_Delay(doc, 2);
368 mfr = ReadDOC_(doc->virtadr, doc->ioreg);
370 /* CDSN Slow IO register see Software Req 11.4 item 5. */
371 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
372 DoC_Delay(doc, 2);
373 id = ReadDOC_(doc->virtadr, doc->ioreg);
376 /* No response - return failure */
377 if (mfr == 0xff || mfr == 0)
378 return 0;
380 /* Check it's the same as the first chip we identified.
381 * M-Systems say that any given DiskOnChip device should only
382 * contain _one_ type of flash part, although that's not a
383 * hardware restriction. */
384 if (doc->mfr) {
385 if (doc->mfr == mfr && doc->id == id)
386 return 1; /* This is another the same the first */
387 else
388 printk(KERN_WARNING
389 "Flash chip at floor %d, chip %d is different:\n",
390 floor, chip);
393 /* Print and store the manufacturer and ID codes. */
394 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
395 if (id == nand_flash_ids[i].id) {
396 /* Try to identify manufacturer */
397 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
398 if (nand_manuf_ids[j].id == mfr)
399 break;
401 printk(KERN_INFO
402 "Flash chip found: Manufacturer ID: %2.2X, "
403 "Chip ID: %2.2X (%s:%s)\n", mfr, id,
404 nand_manuf_ids[j].name, nand_flash_ids[i].name);
405 if (!doc->mfr) {
406 doc->mfr = mfr;
407 doc->id = id;
408 doc->chipshift =
409 ffs((nand_flash_ids[i].chipsize << 20)) - 1;
410 doc->page256 = (nand_flash_ids[i].pagesize == 256) ? 1 : 0;
411 doc->pageadrlen = doc->chipshift > 25 ? 3 : 2;
412 doc->erasesize =
413 nand_flash_ids[i].erasesize;
414 return 1;
416 return 0;
421 /* We haven't fully identified the chip. Print as much as we know. */
422 printk(KERN_WARNING "Unknown flash chip found: %2.2X %2.2X\n",
423 id, mfr);
425 printk(KERN_WARNING "Please report to dwmw2@infradead.org\n");
426 return 0;
429 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
431 static void DoC_ScanChips(struct DiskOnChip *this, int maxchips)
433 int floor, chip;
434 int numchips[MAX_FLOORS];
435 int ret = 1;
437 this->numchips = 0;
438 this->mfr = 0;
439 this->id = 0;
441 /* For each floor, find the number of valid chips it contains */
442 for (floor = 0; floor < MAX_FLOORS; floor++) {
443 ret = 1;
444 numchips[floor] = 0;
445 for (chip = 0; chip < maxchips && ret != 0; chip++) {
447 ret = DoC_IdentChip(this, floor, chip);
448 if (ret) {
449 numchips[floor]++;
450 this->numchips++;
455 /* If there are none at all that we recognise, bail */
456 if (!this->numchips) {
457 printk(KERN_NOTICE "No flash chips recognised.\n");
458 return;
461 /* Allocate an array to hold the information for each chip */
462 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
463 if (!this->chips) {
464 printk(KERN_NOTICE "No memory for allocating chip info structures\n");
465 return;
468 ret = 0;
470 /* Fill out the chip array with {floor, chipno} for each
471 * detected chip in the device. */
472 for (floor = 0; floor < MAX_FLOORS; floor++) {
473 for (chip = 0; chip < numchips[floor]; chip++) {
474 this->chips[ret].floor = floor;
475 this->chips[ret].chip = chip;
476 this->chips[ret].curadr = 0;
477 this->chips[ret].curmode = 0x50;
478 ret++;
482 /* Calculate and print the total size of the device */
483 this->totlen = this->numchips * (1 << this->chipshift);
485 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
486 this->numchips, this->totlen >> 20);
489 static int DoC2k_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
491 int tmp1, tmp2, retval;
492 if (doc1->physadr == doc2->physadr)
493 return 1;
495 /* Use the alias resolution register which was set aside for this
496 * purpose. If it's value is the same on both chips, they might
497 * be the same chip, and we write to one and check for a change in
498 * the other. It's unclear if this register is usuable in the
499 * DoC 2000 (it's in the Millennium docs), but it seems to work. */
500 tmp1 = ReadDOC(doc1->virtadr, AliasResolution);
501 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
502 if (tmp1 != tmp2)
503 return 0;
505 WriteDOC((tmp1 + 1) % 0xff, doc1->virtadr, AliasResolution);
506 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
507 if (tmp2 == (tmp1 + 1) % 0xff)
508 retval = 1;
509 else
510 retval = 0;
512 /* Restore register contents. May not be necessary, but do it just to
513 * be safe. */
514 WriteDOC(tmp1, doc1->virtadr, AliasResolution);
516 return retval;
519 static const char im_name[] = "DoC2k_init";
521 /* This routine is made available to other mtd code via
522 * inter_module_register. It must only be accessed through
523 * inter_module_get which will bump the use count of this module. The
524 * addresses passed back in mtd are valid as long as the use count of
525 * this module is non-zero, i.e. between inter_module_get and
526 * inter_module_put. Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
528 static void DoC2k_init(struct mtd_info *mtd)
530 struct DiskOnChip *this = mtd->priv;
531 struct DiskOnChip *old = NULL;
532 int maxchips;
534 /* We must avoid being called twice for the same device. */
536 if (doc2klist)
537 old = doc2klist->priv;
539 while (old) {
540 if (DoC2k_is_alias(old, this)) {
541 printk(KERN_NOTICE
542 "Ignoring DiskOnChip 2000 at 0x%lX - already configured\n",
543 this->physadr);
544 iounmap(this->virtadr);
545 kfree(mtd);
546 return;
548 if (old->nextdoc)
549 old = old->nextdoc->priv;
550 else
551 old = NULL;
555 switch (this->ChipID) {
556 case DOC_ChipID_Doc2kTSOP:
557 mtd->name = "DiskOnChip 2000 TSOP";
558 this->ioreg = DoC_Mil_CDSN_IO;
559 /* Pretend it's a Millennium */
560 this->ChipID = DOC_ChipID_DocMil;
561 maxchips = MAX_CHIPS;
562 break;
563 case DOC_ChipID_Doc2k:
564 mtd->name = "DiskOnChip 2000";
565 this->ioreg = DoC_2k_CDSN_IO;
566 maxchips = MAX_CHIPS;
567 break;
568 case DOC_ChipID_DocMil:
569 mtd->name = "DiskOnChip Millennium";
570 this->ioreg = DoC_Mil_CDSN_IO;
571 maxchips = MAX_CHIPS_MIL;
572 break;
573 default:
574 printk("Unknown ChipID 0x%02x\n", this->ChipID);
575 kfree(mtd);
576 iounmap(this->virtadr);
577 return;
580 printk(KERN_NOTICE "%s found at address 0x%lX\n", mtd->name,
581 this->physadr);
583 mtd->type = MTD_NANDFLASH;
584 mtd->flags = MTD_CAP_NANDFLASH;
585 mtd->ecctype = MTD_ECC_RS_DiskOnChip;
586 mtd->size = 0;
587 mtd->erasesize = 0;
588 mtd->oobblock = 512;
589 mtd->oobsize = 16;
590 mtd->owner = THIS_MODULE;
591 mtd->erase = doc_erase;
592 mtd->point = NULL;
593 mtd->unpoint = NULL;
594 mtd->read = doc_read;
595 mtd->write = doc_write;
596 mtd->read_ecc = doc_read_ecc;
597 mtd->write_ecc = doc_write_ecc;
598 mtd->writev_ecc = doc_writev_ecc;
599 mtd->read_oob = doc_read_oob;
600 mtd->write_oob = doc_write_oob;
601 mtd->sync = NULL;
603 this->totlen = 0;
604 this->numchips = 0;
606 this->curfloor = -1;
607 this->curchip = -1;
608 init_MUTEX(&this->lock);
610 /* Ident all the chips present. */
611 DoC_ScanChips(this, maxchips);
613 if (!this->totlen) {
614 kfree(mtd);
615 iounmap(this->virtadr);
616 } else {
617 this->nextdoc = doc2klist;
618 doc2klist = mtd;
619 mtd->size = this->totlen;
620 mtd->erasesize = this->erasesize;
621 add_mtd_device(mtd);
622 return;
626 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
627 size_t * retlen, u_char * buf)
629 /* Just a special case of doc_read_ecc */
630 return doc_read_ecc(mtd, from, len, retlen, buf, NULL, NULL);
633 static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
634 size_t * retlen, u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel)
636 struct DiskOnChip *this = mtd->priv;
637 void __iomem *docptr = this->virtadr;
638 struct Nand *mychip;
639 unsigned char syndrome[6];
640 volatile char dummy;
641 int i, len256 = 0, ret=0;
642 size_t left = len;
644 /* Don't allow read past end of device */
645 if (from >= this->totlen)
646 return -EINVAL;
648 down(&this->lock);
650 *retlen = 0;
651 while (left) {
652 len = left;
654 /* Don't allow a single read to cross a 512-byte block boundary */
655 if (from + len > ((from | 0x1ff) + 1))
656 len = ((from | 0x1ff) + 1) - from;
658 /* The ECC will not be calculated correctly if less than 512 is read */
659 if (len != 0x200 && eccbuf)
660 printk(KERN_WARNING
661 "ECC needs a full sector read (adr: %lx size %lx)\n",
662 (long) from, (long) len);
664 /* printk("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len); */
667 /* Find the chip which is to be used and select it */
668 mychip = &this->chips[from >> (this->chipshift)];
670 if (this->curfloor != mychip->floor) {
671 DoC_SelectFloor(this, mychip->floor);
672 DoC_SelectChip(this, mychip->chip);
673 } else if (this->curchip != mychip->chip) {
674 DoC_SelectChip(this, mychip->chip);
677 this->curfloor = mychip->floor;
678 this->curchip = mychip->chip;
680 DoC_Command(this,
681 (!this->page256
682 && (from & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
683 CDSN_CTRL_WP);
684 DoC_Address(this, ADDR_COLUMN_PAGE, from, CDSN_CTRL_WP,
685 CDSN_CTRL_ECC_IO);
687 if (eccbuf) {
688 /* Prime the ECC engine */
689 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
690 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
691 } else {
692 /* disable the ECC engine */
693 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
694 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
697 /* treat crossing 256-byte sector for 2M x 8bits devices */
698 if (this->page256 && from + len > (from | 0xff) + 1) {
699 len256 = (from | 0xff) + 1 - from;
700 DoC_ReadBuf(this, buf, len256);
702 DoC_Command(this, NAND_CMD_READ0, CDSN_CTRL_WP);
703 DoC_Address(this, ADDR_COLUMN_PAGE, from + len256,
704 CDSN_CTRL_WP, CDSN_CTRL_ECC_IO);
707 DoC_ReadBuf(this, &buf[len256], len - len256);
709 /* Let the caller know we completed it */
710 *retlen += len;
712 if (eccbuf) {
713 /* Read the ECC data through the DiskOnChip ECC logic */
714 /* Note: this will work even with 2M x 8bit devices as */
715 /* they have 8 bytes of OOB per 256 page. mf. */
716 DoC_ReadBuf(this, eccbuf, 6);
718 /* Flush the pipeline */
719 if (DoC_is_Millennium(this)) {
720 dummy = ReadDOC(docptr, ECCConf);
721 dummy = ReadDOC(docptr, ECCConf);
722 i = ReadDOC(docptr, ECCConf);
723 } else {
724 dummy = ReadDOC(docptr, 2k_ECCStatus);
725 dummy = ReadDOC(docptr, 2k_ECCStatus);
726 i = ReadDOC(docptr, 2k_ECCStatus);
729 /* Check the ECC Status */
730 if (i & 0x80) {
731 int nb_errors;
732 /* There was an ECC error */
733 #ifdef ECC_DEBUG
734 printk(KERN_ERR "DiskOnChip ECC Error: Read at %lx\n", (long)from);
735 #endif
736 /* Read the ECC syndrom through the DiskOnChip ECC logic.
737 These syndrome will be all ZERO when there is no error */
738 for (i = 0; i < 6; i++) {
739 syndrome[i] =
740 ReadDOC(docptr, ECCSyndrome0 + i);
742 nb_errors = doc_decode_ecc(buf, syndrome);
744 #ifdef ECC_DEBUG
745 printk(KERN_ERR "Errors corrected: %x\n", nb_errors);
746 #endif
747 if (nb_errors < 0) {
748 /* We return error, but have actually done the read. Not that
749 this can be told to user-space, via sys_read(), but at least
750 MTD-aware stuff can know about it by checking *retlen */
751 ret = -EIO;
755 #ifdef PSYCHO_DEBUG
756 printk(KERN_DEBUG "ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
757 (long)from, eccbuf[0], eccbuf[1], eccbuf[2],
758 eccbuf[3], eccbuf[4], eccbuf[5]);
759 #endif
761 /* disable the ECC engine */
762 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
765 /* according to 11.4.1, we need to wait for the busy line
766 * drop if we read to the end of the page. */
767 if(0 == ((from + len) & 0x1ff))
769 DoC_WaitReady(this);
772 from += len;
773 left -= len;
774 buf += len;
777 up(&this->lock);
779 return ret;
782 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
783 size_t * retlen, const u_char * buf)
785 char eccbuf[6];
786 return doc_write_ecc(mtd, to, len, retlen, buf, eccbuf, NULL);
789 static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
790 size_t * retlen, const u_char * buf,
791 u_char * eccbuf, struct nand_oobinfo *oobsel)
793 struct DiskOnChip *this = mtd->priv;
794 int di; /* Yes, DI is a hangover from when I was disassembling the binary driver */
795 void __iomem *docptr = this->virtadr;
796 volatile char dummy;
797 int len256 = 0;
798 struct Nand *mychip;
799 size_t left = len;
800 int status;
802 /* Don't allow write past end of device */
803 if (to >= this->totlen)
804 return -EINVAL;
806 down(&this->lock);
808 *retlen = 0;
809 while (left) {
810 len = left;
812 /* Don't allow a single write to cross a 512-byte block boundary */
813 if (to + len > ((to | 0x1ff) + 1))
814 len = ((to | 0x1ff) + 1) - to;
816 /* The ECC will not be calculated correctly if less than 512 is written */
817 /* DBB-
818 if (len != 0x200 && eccbuf)
819 printk(KERN_WARNING
820 "ECC needs a full sector write (adr: %lx size %lx)\n",
821 (long) to, (long) len);
822 -DBB */
824 /* printk("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */
826 /* Find the chip which is to be used and select it */
827 mychip = &this->chips[to >> (this->chipshift)];
829 if (this->curfloor != mychip->floor) {
830 DoC_SelectFloor(this, mychip->floor);
831 DoC_SelectChip(this, mychip->chip);
832 } else if (this->curchip != mychip->chip) {
833 DoC_SelectChip(this, mychip->chip);
836 this->curfloor = mychip->floor;
837 this->curchip = mychip->chip;
839 /* Set device to main plane of flash */
840 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
841 DoC_Command(this,
842 (!this->page256
843 && (to & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
844 CDSN_CTRL_WP);
846 DoC_Command(this, NAND_CMD_SEQIN, 0);
847 DoC_Address(this, ADDR_COLUMN_PAGE, to, 0, CDSN_CTRL_ECC_IO);
849 if (eccbuf) {
850 /* Prime the ECC engine */
851 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
852 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
853 } else {
854 /* disable the ECC engine */
855 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
856 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
859 /* treat crossing 256-byte sector for 2M x 8bits devices */
860 if (this->page256 && to + len > (to | 0xff) + 1) {
861 len256 = (to | 0xff) + 1 - to;
862 DoC_WriteBuf(this, buf, len256);
864 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
866 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
867 /* There's an implicit DoC_WaitReady() in DoC_Command */
869 dummy = ReadDOC(docptr, CDSNSlowIO);
870 DoC_Delay(this, 2);
872 if (ReadDOC_(docptr, this->ioreg) & 1) {
873 printk(KERN_ERR "Error programming flash\n");
874 /* Error in programming */
875 *retlen = 0;
876 up(&this->lock);
877 return -EIO;
880 DoC_Command(this, NAND_CMD_SEQIN, 0);
881 DoC_Address(this, ADDR_COLUMN_PAGE, to + len256, 0,
882 CDSN_CTRL_ECC_IO);
885 DoC_WriteBuf(this, &buf[len256], len - len256);
887 if (eccbuf) {
888 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_CE, docptr,
889 CDSNControl);
891 if (DoC_is_Millennium(this)) {
892 WriteDOC(0, docptr, NOP);
893 WriteDOC(0, docptr, NOP);
894 WriteDOC(0, docptr, NOP);
895 } else {
896 WriteDOC_(0, docptr, this->ioreg);
897 WriteDOC_(0, docptr, this->ioreg);
898 WriteDOC_(0, docptr, this->ioreg);
901 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_FLASH_IO | CDSN_CTRL_CE, docptr,
902 CDSNControl);
904 /* Read the ECC data through the DiskOnChip ECC logic */
905 for (di = 0; di < 6; di++) {
906 eccbuf[di] = ReadDOC(docptr, ECCSyndrome0 + di);
909 /* Reset the ECC engine */
910 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
912 #ifdef PSYCHO_DEBUG
913 printk
914 ("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
915 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
916 eccbuf[4], eccbuf[5]);
917 #endif
920 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
922 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
923 /* There's an implicit DoC_WaitReady() in DoC_Command */
925 if (DoC_is_Millennium(this)) {
926 ReadDOC(docptr, ReadPipeInit);
927 status = ReadDOC(docptr, LastDataRead);
928 } else {
929 dummy = ReadDOC(docptr, CDSNSlowIO);
930 DoC_Delay(this, 2);
931 status = ReadDOC_(docptr, this->ioreg);
934 if (status & 1) {
935 printk(KERN_ERR "Error programming flash\n");
936 /* Error in programming */
937 *retlen = 0;
938 up(&this->lock);
939 return -EIO;
942 /* Let the caller know we completed it */
943 *retlen += len;
945 if (eccbuf) {
946 unsigned char x[8];
947 size_t dummy;
948 int ret;
950 /* Write the ECC data to flash */
951 for (di=0; di<6; di++)
952 x[di] = eccbuf[di];
954 x[6]=0x55;
955 x[7]=0x55;
957 ret = doc_write_oob_nolock(mtd, to, 8, &dummy, x);
958 if (ret) {
959 up(&this->lock);
960 return ret;
964 to += len;
965 left -= len;
966 buf += len;
969 up(&this->lock);
970 return 0;
973 static int doc_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs,
974 unsigned long count, loff_t to, size_t *retlen,
975 u_char *eccbuf, struct nand_oobinfo *oobsel)
977 static char static_buf[512];
978 static DECLARE_MUTEX(writev_buf_sem);
980 size_t totretlen = 0;
981 size_t thisvecofs = 0;
982 int ret= 0;
984 down(&writev_buf_sem);
986 while(count) {
987 size_t thislen, thisretlen;
988 unsigned char *buf;
990 buf = vecs->iov_base + thisvecofs;
991 thislen = vecs->iov_len - thisvecofs;
994 if (thislen >= 512) {
995 thislen = thislen & ~(512-1);
996 thisvecofs += thislen;
997 } else {
998 /* Not enough to fill a page. Copy into buf */
999 memcpy(static_buf, buf, thislen);
1000 buf = &static_buf[thislen];
1002 while(count && thislen < 512) {
1003 vecs++;
1004 count--;
1005 thisvecofs = min((512-thislen), vecs->iov_len);
1006 memcpy(buf, vecs->iov_base, thisvecofs);
1007 thislen += thisvecofs;
1008 buf += thisvecofs;
1010 buf = static_buf;
1012 if (count && thisvecofs == vecs->iov_len) {
1013 thisvecofs = 0;
1014 vecs++;
1015 count--;
1017 ret = doc_write_ecc(mtd, to, thislen, &thisretlen, buf, eccbuf, oobsel);
1019 totretlen += thisretlen;
1021 if (ret || thisretlen != thislen)
1022 break;
1024 to += thislen;
1027 up(&writev_buf_sem);
1028 *retlen = totretlen;
1029 return ret;
1033 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
1034 size_t * retlen, u_char * buf)
1036 struct DiskOnChip *this = mtd->priv;
1037 int len256 = 0, ret;
1038 struct Nand *mychip;
1040 down(&this->lock);
1042 mychip = &this->chips[ofs >> this->chipshift];
1044 if (this->curfloor != mychip->floor) {
1045 DoC_SelectFloor(this, mychip->floor);
1046 DoC_SelectChip(this, mychip->chip);
1047 } else if (this->curchip != mychip->chip) {
1048 DoC_SelectChip(this, mychip->chip);
1050 this->curfloor = mychip->floor;
1051 this->curchip = mychip->chip;
1053 /* update address for 2M x 8bit devices. OOB starts on the second */
1054 /* page to maintain compatibility with doc_read_ecc. */
1055 if (this->page256) {
1056 if (!(ofs & 0x8))
1057 ofs += 0x100;
1058 else
1059 ofs -= 0x8;
1062 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1063 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, CDSN_CTRL_WP, 0);
1065 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1066 /* Note: datasheet says it should automaticaly wrap to the */
1067 /* next OOB block, but it didn't work here. mf. */
1068 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1069 len256 = (ofs | 0x7) + 1 - ofs;
1070 DoC_ReadBuf(this, buf, len256);
1072 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1073 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff),
1074 CDSN_CTRL_WP, 0);
1077 DoC_ReadBuf(this, &buf[len256], len - len256);
1079 *retlen = len;
1080 /* Reading the full OOB data drops us off of the end of the page,
1081 * causing the flash device to go into busy mode, so we need
1082 * to wait until ready 11.4.1 and Toshiba TC58256FT docs */
1084 ret = DoC_WaitReady(this);
1086 up(&this->lock);
1087 return ret;
1091 static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
1092 size_t * retlen, const u_char * buf)
1094 struct DiskOnChip *this = mtd->priv;
1095 int len256 = 0;
1096 void __iomem *docptr = this->virtadr;
1097 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
1098 volatile int dummy;
1099 int status;
1101 // 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,
1102 // buf[0], buf[1], buf[2], buf[3], buf[8], buf[9], buf[14],buf[15]);
1104 /* Find the chip which is to be used and select it */
1105 if (this->curfloor != mychip->floor) {
1106 DoC_SelectFloor(this, mychip->floor);
1107 DoC_SelectChip(this, mychip->chip);
1108 } else if (this->curchip != mychip->chip) {
1109 DoC_SelectChip(this, mychip->chip);
1111 this->curfloor = mychip->floor;
1112 this->curchip = mychip->chip;
1114 /* disable the ECC engine */
1115 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
1116 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
1118 /* Reset the chip, see Software Requirement 11.4 item 1. */
1119 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
1121 /* issue the Read2 command to set the pointer to the Spare Data Area. */
1122 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1124 /* update address for 2M x 8bit devices. OOB starts on the second */
1125 /* page to maintain compatibility with doc_read_ecc. */
1126 if (this->page256) {
1127 if (!(ofs & 0x8))
1128 ofs += 0x100;
1129 else
1130 ofs -= 0x8;
1133 /* issue the Serial Data In command to initial the Page Program process */
1134 DoC_Command(this, NAND_CMD_SEQIN, 0);
1135 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, 0, 0);
1137 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1138 /* Note: datasheet says it should automaticaly wrap to the */
1139 /* next OOB block, but it didn't work here. mf. */
1140 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1141 len256 = (ofs | 0x7) + 1 - ofs;
1142 DoC_WriteBuf(this, buf, len256);
1144 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1145 DoC_Command(this, NAND_CMD_STATUS, 0);
1146 /* DoC_WaitReady() is implicit in DoC_Command */
1148 if (DoC_is_Millennium(this)) {
1149 ReadDOC(docptr, ReadPipeInit);
1150 status = ReadDOC(docptr, LastDataRead);
1151 } else {
1152 dummy = ReadDOC(docptr, CDSNSlowIO);
1153 DoC_Delay(this, 2);
1154 status = ReadDOC_(docptr, this->ioreg);
1157 if (status & 1) {
1158 printk(KERN_ERR "Error programming oob data\n");
1159 /* There was an error */
1160 *retlen = 0;
1161 return -EIO;
1163 DoC_Command(this, NAND_CMD_SEQIN, 0);
1164 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), 0, 0);
1167 DoC_WriteBuf(this, &buf[len256], len - len256);
1169 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1170 DoC_Command(this, NAND_CMD_STATUS, 0);
1171 /* DoC_WaitReady() is implicit in DoC_Command */
1173 if (DoC_is_Millennium(this)) {
1174 ReadDOC(docptr, ReadPipeInit);
1175 status = ReadDOC(docptr, LastDataRead);
1176 } else {
1177 dummy = ReadDOC(docptr, CDSNSlowIO);
1178 DoC_Delay(this, 2);
1179 status = ReadDOC_(docptr, this->ioreg);
1182 if (status & 1) {
1183 printk(KERN_ERR "Error programming oob data\n");
1184 /* There was an error */
1185 *retlen = 0;
1186 return -EIO;
1189 *retlen = len;
1190 return 0;
1194 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
1195 size_t * retlen, const u_char * buf)
1197 struct DiskOnChip *this = mtd->priv;
1198 int ret;
1200 down(&this->lock);
1201 ret = doc_write_oob_nolock(mtd, ofs, len, retlen, buf);
1203 up(&this->lock);
1204 return ret;
1207 static int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
1209 struct DiskOnChip *this = mtd->priv;
1210 __u32 ofs = instr->addr;
1211 __u32 len = instr->len;
1212 volatile int dummy;
1213 void __iomem *docptr = this->virtadr;
1214 struct Nand *mychip;
1215 int status;
1217 down(&this->lock);
1219 if (ofs & (mtd->erasesize-1) || len & (mtd->erasesize-1)) {
1220 up(&this->lock);
1221 return -EINVAL;
1224 instr->state = MTD_ERASING;
1226 /* FIXME: Do this in the background. Use timers or schedule_task() */
1227 while(len) {
1228 mychip = &this->chips[ofs >> this->chipshift];
1230 if (this->curfloor != mychip->floor) {
1231 DoC_SelectFloor(this, mychip->floor);
1232 DoC_SelectChip(this, mychip->chip);
1233 } else if (this->curchip != mychip->chip) {
1234 DoC_SelectChip(this, mychip->chip);
1236 this->curfloor = mychip->floor;
1237 this->curchip = mychip->chip;
1239 DoC_Command(this, NAND_CMD_ERASE1, 0);
1240 DoC_Address(this, ADDR_PAGE, ofs, 0, 0);
1241 DoC_Command(this, NAND_CMD_ERASE2, 0);
1243 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
1245 if (DoC_is_Millennium(this)) {
1246 ReadDOC(docptr, ReadPipeInit);
1247 status = ReadDOC(docptr, LastDataRead);
1248 } else {
1249 dummy = ReadDOC(docptr, CDSNSlowIO);
1250 DoC_Delay(this, 2);
1251 status = ReadDOC_(docptr, this->ioreg);
1254 if (status & 1) {
1255 printk(KERN_ERR "Error erasing at 0x%x\n", ofs);
1256 /* There was an error */
1257 instr->state = MTD_ERASE_FAILED;
1258 goto callback;
1260 ofs += mtd->erasesize;
1261 len -= mtd->erasesize;
1263 instr->state = MTD_ERASE_DONE;
1265 callback:
1266 mtd_erase_callback(instr);
1268 up(&this->lock);
1269 return 0;
1273 /****************************************************************************
1275 * Module stuff
1277 ****************************************************************************/
1279 static int __init init_doc2000(void)
1281 inter_module_register(im_name, THIS_MODULE, &DoC2k_init);
1282 return 0;
1285 static void __exit cleanup_doc2000(void)
1287 struct mtd_info *mtd;
1288 struct DiskOnChip *this;
1290 while ((mtd = doc2klist)) {
1291 this = mtd->priv;
1292 doc2klist = this->nextdoc;
1294 del_mtd_device(mtd);
1296 iounmap(this->virtadr);
1297 kfree(this->chips);
1298 kfree(mtd);
1300 inter_module_unregister(im_name);
1303 module_exit(cleanup_doc2000);
1304 module_init(init_doc2000);
1306 MODULE_LICENSE("GPL");
1307 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
1308 MODULE_DESCRIPTION("MTD driver for DiskOnChip 2000 and Millennium");