initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / mtd / devices / doc2000.c
blobfefbf5eaf80514f92421e149518413281d158a22
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.62 2004/08/09 14:04:02 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 unsigned long 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 unsigned long docptr = doc->virtadr;
118 /* This is inline, to optimise the common case, where it's ready instantly */
119 int ret = 0;
121 /* 4 read form NOP register should be issued in prior to the read from CDSNControl
122 see Software Requirement 11.4 item 2. */
123 DoC_Delay(doc, 4);
125 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
126 /* Call the out-of-line routine to wait */
127 ret = _DoC_WaitReady(doc);
129 /* issue 2 read from NOP register after reading from CDSNControl register
130 see Software Requirement 11.4 item 2. */
131 DoC_Delay(doc, 2);
133 return ret;
136 /* DoC_Command: Send a flash command to the flash chip through the CDSN Slow IO register to
137 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
138 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
140 static inline int DoC_Command(struct DiskOnChip *doc, unsigned char command,
141 unsigned char xtraflags)
143 unsigned long docptr = doc->virtadr;
145 if (DoC_is_2000(doc))
146 xtraflags |= CDSN_CTRL_FLASH_IO;
148 /* Assert the CLE (Command Latch Enable) line to the flash chip */
149 WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
150 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
152 if (DoC_is_Millennium(doc))
153 WriteDOC(command, docptr, CDSNSlowIO);
155 /* Send the command */
156 WriteDOC_(command, docptr, doc->ioreg);
157 if (DoC_is_Millennium(doc))
158 WriteDOC(command, docptr, WritePipeTerm);
160 /* Lower the CLE line */
161 WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
162 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
164 /* Wait for the chip to respond - Software requirement 11.4.1 (extended for any command) */
165 return DoC_WaitReady(doc);
168 /* DoC_Address: Set the current address for the flash chip through the CDSN Slow IO register to
169 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
170 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
172 static int DoC_Address(struct DiskOnChip *doc, int numbytes, unsigned long ofs,
173 unsigned char xtraflags1, unsigned char xtraflags2)
175 unsigned long docptr;
176 int i;
178 docptr = doc->virtadr;
180 if (DoC_is_2000(doc))
181 xtraflags1 |= CDSN_CTRL_FLASH_IO;
183 /* Assert the ALE (Address Latch Enable) line to the flash chip */
184 WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
186 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
188 /* Send the address */
189 /* Devices with 256-byte page are addressed as:
190 Column (bits 0-7), Page (bits 8-15, 16-23, 24-31)
191 * there is no device on the market with page256
192 and more than 24 bits.
193 Devices with 512-byte page are addressed as:
194 Column (bits 0-7), Page (bits 9-16, 17-24, 25-31)
195 * 25-31 is sent only if the chip support it.
196 * bit 8 changes the read command to be sent
197 (NAND_CMD_READ0 or NAND_CMD_READ1).
200 if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE) {
201 if (DoC_is_Millennium(doc))
202 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
203 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
206 if (doc->page256) {
207 ofs = ofs >> 8;
208 } else {
209 ofs = ofs >> 9;
212 if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE) {
213 for (i = 0; i < doc->pageadrlen; i++, ofs = ofs >> 8) {
214 if (DoC_is_Millennium(doc))
215 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
216 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
220 if (DoC_is_Millennium(doc))
221 WriteDOC(ofs & 0xff, docptr, WritePipeTerm);
223 DoC_Delay(doc, 2); /* Needed for some slow flash chips. mf. */
225 /* FIXME: The SlowIO's for millennium could be replaced by
226 a single WritePipeTerm here. mf. */
228 /* Lower the ALE line */
229 WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr,
230 CDSNControl);
232 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
234 /* Wait for the chip to respond - Software requirement 11.4.1 */
235 return DoC_WaitReady(doc);
238 /* Read a buffer from DoC, taking care of Millennium odditys */
239 static void DoC_ReadBuf(struct DiskOnChip *doc, u_char * buf, int len)
241 volatile int dummy;
242 int modulus = 0xffff;
243 unsigned long docptr;
244 int i;
246 docptr = doc->virtadr;
248 if (len <= 0)
249 return;
251 if (DoC_is_Millennium(doc)) {
252 /* Read the data via the internal pipeline through CDSN IO register,
253 see Pipelined Read Operations 11.3 */
254 dummy = ReadDOC(docptr, ReadPipeInit);
256 /* Millennium should use the LastDataRead register - Pipeline Reads */
257 len--;
259 /* This is needed for correctly ECC calculation */
260 modulus = 0xff;
263 for (i = 0; i < len; i++)
264 buf[i] = ReadDOC_(docptr, doc->ioreg + (i & modulus));
266 if (DoC_is_Millennium(doc)) {
267 buf[i] = ReadDOC(docptr, LastDataRead);
271 /* Write a buffer to DoC, taking care of Millennium odditys */
272 static void DoC_WriteBuf(struct DiskOnChip *doc, const u_char * buf, int len)
274 unsigned long docptr;
275 int i;
277 docptr = doc->virtadr;
279 if (len <= 0)
280 return;
282 for (i = 0; i < len; i++)
283 WriteDOC_(buf[i], docptr, doc->ioreg + i);
285 if (DoC_is_Millennium(doc)) {
286 WriteDOC(0x00, docptr, WritePipeTerm);
291 /* DoC_SelectChip: Select a given flash chip within the current floor */
293 static inline int DoC_SelectChip(struct DiskOnChip *doc, int chip)
295 unsigned long docptr = doc->virtadr;
297 /* Software requirement 11.4.4 before writing DeviceSelect */
298 /* Deassert the CE line to eliminate glitches on the FCE# outputs */
299 WriteDOC(CDSN_CTRL_WP, docptr, CDSNControl);
300 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
302 /* Select the individual flash chip requested */
303 WriteDOC(chip, docptr, CDSNDeviceSelect);
304 DoC_Delay(doc, 4);
306 /* Reassert the CE line */
307 WriteDOC(CDSN_CTRL_CE | CDSN_CTRL_FLASH_IO | CDSN_CTRL_WP, docptr,
308 CDSNControl);
309 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
311 /* Wait for it to be ready */
312 return DoC_WaitReady(doc);
315 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
317 static inline int DoC_SelectFloor(struct DiskOnChip *doc, int floor)
319 unsigned long docptr = doc->virtadr;
321 /* Select the floor (bank) of chips required */
322 WriteDOC(floor, docptr, FloorSelect);
324 /* Wait for the chip to be ready */
325 return DoC_WaitReady(doc);
328 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
330 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
332 int mfr, id, i, j;
333 volatile char dummy;
335 /* Page in the required floor/chip */
336 DoC_SelectFloor(doc, floor);
337 DoC_SelectChip(doc, chip);
339 /* Reset the chip */
340 if (DoC_Command(doc, NAND_CMD_RESET, CDSN_CTRL_WP)) {
341 DEBUG(MTD_DEBUG_LEVEL2,
342 "DoC_Command (reset) for %d,%d returned true\n",
343 floor, chip);
344 return 0;
348 /* Read the NAND chip ID: 1. Send ReadID command */
349 if (DoC_Command(doc, NAND_CMD_READID, CDSN_CTRL_WP)) {
350 DEBUG(MTD_DEBUG_LEVEL2,
351 "DoC_Command (ReadID) for %d,%d returned true\n",
352 floor, chip);
353 return 0;
356 /* Read the NAND chip ID: 2. Send address byte zero */
357 DoC_Address(doc, ADDR_COLUMN, 0, CDSN_CTRL_WP, 0);
359 /* Read the manufacturer and device id codes from the device */
361 if (DoC_is_Millennium(doc)) {
362 DoC_Delay(doc, 2);
363 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
364 mfr = ReadDOC(doc->virtadr, LastDataRead);
366 DoC_Delay(doc, 2);
367 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
368 id = ReadDOC(doc->virtadr, LastDataRead);
369 } else {
370 /* CDSN Slow IO register see Software Req 11.4 item 5. */
371 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
372 DoC_Delay(doc, 2);
373 mfr = ReadDOC_(doc->virtadr, doc->ioreg);
375 /* CDSN Slow IO register see Software Req 11.4 item 5. */
376 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
377 DoC_Delay(doc, 2);
378 id = ReadDOC_(doc->virtadr, doc->ioreg);
381 /* No response - return failure */
382 if (mfr == 0xff || mfr == 0)
383 return 0;
385 /* Check it's the same as the first chip we identified.
386 * M-Systems say that any given DiskOnChip device should only
387 * contain _one_ type of flash part, although that's not a
388 * hardware restriction. */
389 if (doc->mfr) {
390 if (doc->mfr == mfr && doc->id == id)
391 return 1; /* This is another the same the first */
392 else
393 printk(KERN_WARNING
394 "Flash chip at floor %d, chip %d is different:\n",
395 floor, chip);
398 /* Print and store the manufacturer and ID codes. */
399 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
400 if (id == nand_flash_ids[i].id) {
401 /* Try to identify manufacturer */
402 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
403 if (nand_manuf_ids[j].id == mfr)
404 break;
406 printk(KERN_INFO
407 "Flash chip found: Manufacturer ID: %2.2X, "
408 "Chip ID: %2.2X (%s:%s)\n", mfr, id,
409 nand_manuf_ids[j].name, nand_flash_ids[i].name);
410 if (!doc->mfr) {
411 doc->mfr = mfr;
412 doc->id = id;
413 doc->chipshift =
414 ffs((nand_flash_ids[i].chipsize << 20)) - 1;
415 doc->page256 = (nand_flash_ids[i].pagesize == 256) ? 1 : 0;
416 doc->pageadrlen = doc->chipshift > 25 ? 3 : 2;
417 doc->erasesize =
418 nand_flash_ids[i].erasesize;
419 return 1;
421 return 0;
426 /* We haven't fully identified the chip. Print as much as we know. */
427 printk(KERN_WARNING "Unknown flash chip found: %2.2X %2.2X\n",
428 id, mfr);
430 printk(KERN_WARNING "Please report to dwmw2@infradead.org\n");
431 return 0;
434 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
436 static void DoC_ScanChips(struct DiskOnChip *this, int maxchips)
438 int floor, chip;
439 int numchips[MAX_FLOORS];
440 int ret = 1;
442 this->numchips = 0;
443 this->mfr = 0;
444 this->id = 0;
446 /* For each floor, find the number of valid chips it contains */
447 for (floor = 0; floor < MAX_FLOORS; floor++) {
448 ret = 1;
449 numchips[floor] = 0;
450 for (chip = 0; chip < maxchips && ret != 0; chip++) {
452 ret = DoC_IdentChip(this, floor, chip);
453 if (ret) {
454 numchips[floor]++;
455 this->numchips++;
460 /* If there are none at all that we recognise, bail */
461 if (!this->numchips) {
462 printk(KERN_NOTICE "No flash chips recognised.\n");
463 return;
466 /* Allocate an array to hold the information for each chip */
467 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
468 if (!this->chips) {
469 printk(KERN_NOTICE "No memory for allocating chip info structures\n");
470 return;
473 ret = 0;
475 /* Fill out the chip array with {floor, chipno} for each
476 * detected chip in the device. */
477 for (floor = 0; floor < MAX_FLOORS; floor++) {
478 for (chip = 0; chip < numchips[floor]; chip++) {
479 this->chips[ret].floor = floor;
480 this->chips[ret].chip = chip;
481 this->chips[ret].curadr = 0;
482 this->chips[ret].curmode = 0x50;
483 ret++;
487 /* Calculate and print the total size of the device */
488 this->totlen = this->numchips * (1 << this->chipshift);
490 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
491 this->numchips, this->totlen >> 20);
494 static int DoC2k_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
496 int tmp1, tmp2, retval;
497 if (doc1->physadr == doc2->physadr)
498 return 1;
500 /* Use the alias resolution register which was set aside for this
501 * purpose. If it's value is the same on both chips, they might
502 * be the same chip, and we write to one and check for a change in
503 * the other. It's unclear if this register is usuable in the
504 * DoC 2000 (it's in the Millennium docs), but it seems to work. */
505 tmp1 = ReadDOC(doc1->virtadr, AliasResolution);
506 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
507 if (tmp1 != tmp2)
508 return 0;
510 WriteDOC((tmp1 + 1) % 0xff, doc1->virtadr, AliasResolution);
511 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
512 if (tmp2 == (tmp1 + 1) % 0xff)
513 retval = 1;
514 else
515 retval = 0;
517 /* Restore register contents. May not be necessary, but do it just to
518 * be safe. */
519 WriteDOC(tmp1, doc1->virtadr, AliasResolution);
521 return retval;
524 static const char im_name[] = "DoC2k_init";
526 /* This routine is made available to other mtd code via
527 * inter_module_register. It must only be accessed through
528 * inter_module_get which will bump the use count of this module. The
529 * addresses passed back in mtd are valid as long as the use count of
530 * this module is non-zero, i.e. between inter_module_get and
531 * inter_module_put. Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
533 static void DoC2k_init(struct mtd_info *mtd)
535 struct DiskOnChip *this = (struct DiskOnChip *) mtd->priv;
536 struct DiskOnChip *old = NULL;
537 int maxchips;
539 /* We must avoid being called twice for the same device. */
541 if (doc2klist)
542 old = (struct DiskOnChip *) doc2klist->priv;
544 while (old) {
545 if (DoC2k_is_alias(old, this)) {
546 printk(KERN_NOTICE
547 "Ignoring DiskOnChip 2000 at 0x%lX - already configured\n",
548 this->physadr);
549 iounmap((void *) this->virtadr);
550 kfree(mtd);
551 return;
553 if (old->nextdoc)
554 old = (struct DiskOnChip *) old->nextdoc->priv;
555 else
556 old = NULL;
560 switch (this->ChipID) {
561 case DOC_ChipID_Doc2kTSOP:
562 mtd->name = "DiskOnChip 2000 TSOP";
563 this->ioreg = DoC_Mil_CDSN_IO;
564 /* Pretend it's a Millennium */
565 this->ChipID = DOC_ChipID_DocMil;
566 maxchips = MAX_CHIPS;
567 break;
568 case DOC_ChipID_Doc2k:
569 mtd->name = "DiskOnChip 2000";
570 this->ioreg = DoC_2k_CDSN_IO;
571 maxchips = MAX_CHIPS;
572 break;
573 case DOC_ChipID_DocMil:
574 mtd->name = "DiskOnChip Millennium";
575 this->ioreg = DoC_Mil_CDSN_IO;
576 maxchips = MAX_CHIPS_MIL;
577 break;
578 default:
579 printk("Unknown ChipID 0x%02x\n", this->ChipID);
580 kfree(mtd);
581 iounmap((void *) this->virtadr);
582 return;
585 printk(KERN_NOTICE "%s found at address 0x%lX\n", mtd->name,
586 this->physadr);
588 mtd->type = MTD_NANDFLASH;
589 mtd->flags = MTD_CAP_NANDFLASH;
590 mtd->ecctype = MTD_ECC_RS_DiskOnChip;
591 mtd->size = 0;
592 mtd->erasesize = 0;
593 mtd->oobblock = 512;
594 mtd->oobsize = 16;
595 mtd->owner = THIS_MODULE;
596 mtd->erase = doc_erase;
597 mtd->point = NULL;
598 mtd->unpoint = NULL;
599 mtd->read = doc_read;
600 mtd->write = doc_write;
601 mtd->read_ecc = doc_read_ecc;
602 mtd->write_ecc = doc_write_ecc;
603 mtd->writev_ecc = doc_writev_ecc;
604 mtd->read_oob = doc_read_oob;
605 mtd->write_oob = doc_write_oob;
606 mtd->sync = NULL;
608 this->totlen = 0;
609 this->numchips = 0;
611 this->curfloor = -1;
612 this->curchip = -1;
613 init_MUTEX(&this->lock);
615 /* Ident all the chips present. */
616 DoC_ScanChips(this, maxchips);
618 if (!this->totlen) {
619 kfree(mtd);
620 iounmap((void *) this->virtadr);
621 } else {
622 this->nextdoc = doc2klist;
623 doc2klist = mtd;
624 mtd->size = this->totlen;
625 mtd->erasesize = this->erasesize;
626 add_mtd_device(mtd);
627 return;
631 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
632 size_t * retlen, u_char * buf)
634 /* Just a special case of doc_read_ecc */
635 return doc_read_ecc(mtd, from, len, retlen, buf, NULL, NULL);
638 static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
639 size_t * retlen, u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel)
641 struct DiskOnChip *this = (struct DiskOnChip *) mtd->priv;
642 unsigned long docptr;
643 struct Nand *mychip;
644 unsigned char syndrome[6];
645 volatile char dummy;
646 int i, len256 = 0, ret=0;
647 size_t left = len;
649 docptr = this->virtadr;
651 /* Don't allow read past end of device */
652 if (from >= this->totlen)
653 return -EINVAL;
655 down(&this->lock);
657 *retlen = 0;
658 while (left) {
659 len = left;
661 /* Don't allow a single read to cross a 512-byte block boundary */
662 if (from + len > ((from | 0x1ff) + 1))
663 len = ((from | 0x1ff) + 1) - from;
665 /* The ECC will not be calculated correctly if less than 512 is read */
666 if (len != 0x200 && eccbuf)
667 printk(KERN_WARNING
668 "ECC needs a full sector read (adr: %lx size %lx)\n",
669 (long) from, (long) len);
671 /* printk("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len); */
674 /* Find the chip which is to be used and select it */
675 mychip = &this->chips[from >> (this->chipshift)];
677 if (this->curfloor != mychip->floor) {
678 DoC_SelectFloor(this, mychip->floor);
679 DoC_SelectChip(this, mychip->chip);
680 } else if (this->curchip != mychip->chip) {
681 DoC_SelectChip(this, mychip->chip);
684 this->curfloor = mychip->floor;
685 this->curchip = mychip->chip;
687 DoC_Command(this,
688 (!this->page256
689 && (from & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
690 CDSN_CTRL_WP);
691 DoC_Address(this, ADDR_COLUMN_PAGE, from, CDSN_CTRL_WP,
692 CDSN_CTRL_ECC_IO);
694 if (eccbuf) {
695 /* Prime the ECC engine */
696 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
697 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
698 } else {
699 /* disable the ECC engine */
700 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
701 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
704 /* treat crossing 256-byte sector for 2M x 8bits devices */
705 if (this->page256 && from + len > (from | 0xff) + 1) {
706 len256 = (from | 0xff) + 1 - from;
707 DoC_ReadBuf(this, buf, len256);
709 DoC_Command(this, NAND_CMD_READ0, CDSN_CTRL_WP);
710 DoC_Address(this, ADDR_COLUMN_PAGE, from + len256,
711 CDSN_CTRL_WP, CDSN_CTRL_ECC_IO);
714 DoC_ReadBuf(this, &buf[len256], len - len256);
716 /* Let the caller know we completed it */
717 *retlen += len;
719 if (eccbuf) {
720 /* Read the ECC data through the DiskOnChip ECC logic */
721 /* Note: this will work even with 2M x 8bit devices as */
722 /* they have 8 bytes of OOB per 256 page. mf. */
723 DoC_ReadBuf(this, eccbuf, 6);
725 /* Flush the pipeline */
726 if (DoC_is_Millennium(this)) {
727 dummy = ReadDOC(docptr, ECCConf);
728 dummy = ReadDOC(docptr, ECCConf);
729 i = ReadDOC(docptr, ECCConf);
730 } else {
731 dummy = ReadDOC(docptr, 2k_ECCStatus);
732 dummy = ReadDOC(docptr, 2k_ECCStatus);
733 i = ReadDOC(docptr, 2k_ECCStatus);
736 /* Check the ECC Status */
737 if (i & 0x80) {
738 int nb_errors;
739 /* There was an ECC error */
740 #ifdef ECC_DEBUG
741 printk(KERN_ERR "DiskOnChip ECC Error: Read at %lx\n", (long)from);
742 #endif
743 /* Read the ECC syndrom through the DiskOnChip ECC logic.
744 These syndrome will be all ZERO when there is no error */
745 for (i = 0; i < 6; i++) {
746 syndrome[i] =
747 ReadDOC(docptr, ECCSyndrome0 + i);
749 nb_errors = doc_decode_ecc(buf, syndrome);
751 #ifdef ECC_DEBUG
752 printk(KERN_ERR "Errors corrected: %x\n", nb_errors);
753 #endif
754 if (nb_errors < 0) {
755 /* We return error, but have actually done the read. Not that
756 this can be told to user-space, via sys_read(), but at least
757 MTD-aware stuff can know about it by checking *retlen */
758 ret = -EIO;
762 #ifdef PSYCHO_DEBUG
763 printk(KERN_DEBUG "ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
764 (long)from, eccbuf[0], eccbuf[1], eccbuf[2],
765 eccbuf[3], eccbuf[4], eccbuf[5]);
766 #endif
768 /* disable the ECC engine */
769 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
772 /* according to 11.4.1, we need to wait for the busy line
773 * drop if we read to the end of the page. */
774 if(0 == ((from + len) & 0x1ff))
776 DoC_WaitReady(this);
779 from += len;
780 left -= len;
781 buf += len;
784 up(&this->lock);
786 return ret;
789 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
790 size_t * retlen, const u_char * buf)
792 char eccbuf[6];
793 return doc_write_ecc(mtd, to, len, retlen, buf, eccbuf, NULL);
796 static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
797 size_t * retlen, const u_char * buf,
798 u_char * eccbuf, struct nand_oobinfo *oobsel)
800 struct DiskOnChip *this = (struct DiskOnChip *) mtd->priv;
801 int di; /* Yes, DI is a hangover from when I was disassembling the binary driver */
802 unsigned long docptr;
803 volatile char dummy;
804 int len256 = 0;
805 struct Nand *mychip;
806 size_t left = len;
807 int status;
809 docptr = this->virtadr;
811 /* Don't allow write past end of device */
812 if (to >= this->totlen)
813 return -EINVAL;
815 down(&this->lock);
817 *retlen = 0;
818 while (left) {
819 len = left;
821 /* Don't allow a single write to cross a 512-byte block boundary */
822 if (to + len > ((to | 0x1ff) + 1))
823 len = ((to | 0x1ff) + 1) - to;
825 /* The ECC will not be calculated correctly if less than 512 is written */
826 /* DBB-
827 if (len != 0x200 && eccbuf)
828 printk(KERN_WARNING
829 "ECC needs a full sector write (adr: %lx size %lx)\n",
830 (long) to, (long) len);
831 -DBB */
833 /* printk("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */
835 /* Find the chip which is to be used and select it */
836 mychip = &this->chips[to >> (this->chipshift)];
838 if (this->curfloor != mychip->floor) {
839 DoC_SelectFloor(this, mychip->floor);
840 DoC_SelectChip(this, mychip->chip);
841 } else if (this->curchip != mychip->chip) {
842 DoC_SelectChip(this, mychip->chip);
845 this->curfloor = mychip->floor;
846 this->curchip = mychip->chip;
848 /* Set device to main plane of flash */
849 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
850 DoC_Command(this,
851 (!this->page256
852 && (to & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
853 CDSN_CTRL_WP);
855 DoC_Command(this, NAND_CMD_SEQIN, 0);
856 DoC_Address(this, ADDR_COLUMN_PAGE, to, 0, CDSN_CTRL_ECC_IO);
858 if (eccbuf) {
859 /* Prime the ECC engine */
860 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
861 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
862 } else {
863 /* disable the ECC engine */
864 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
865 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
868 /* treat crossing 256-byte sector for 2M x 8bits devices */
869 if (this->page256 && to + len > (to | 0xff) + 1) {
870 len256 = (to | 0xff) + 1 - to;
871 DoC_WriteBuf(this, buf, len256);
873 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
875 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
876 /* There's an implicit DoC_WaitReady() in DoC_Command */
878 dummy = ReadDOC(docptr, CDSNSlowIO);
879 DoC_Delay(this, 2);
881 if (ReadDOC_(docptr, this->ioreg) & 1) {
882 printk(KERN_ERR "Error programming flash\n");
883 /* Error in programming */
884 *retlen = 0;
885 up(&this->lock);
886 return -EIO;
889 DoC_Command(this, NAND_CMD_SEQIN, 0);
890 DoC_Address(this, ADDR_COLUMN_PAGE, to + len256, 0,
891 CDSN_CTRL_ECC_IO);
894 DoC_WriteBuf(this, &buf[len256], len - len256);
896 if (eccbuf) {
897 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_CE, docptr,
898 CDSNControl);
900 if (DoC_is_Millennium(this)) {
901 WriteDOC(0, docptr, NOP);
902 WriteDOC(0, docptr, NOP);
903 WriteDOC(0, docptr, NOP);
904 } else {
905 WriteDOC_(0, docptr, this->ioreg);
906 WriteDOC_(0, docptr, this->ioreg);
907 WriteDOC_(0, docptr, this->ioreg);
910 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_FLASH_IO | CDSN_CTRL_CE, docptr,
911 CDSNControl);
913 /* Read the ECC data through the DiskOnChip ECC logic */
914 for (di = 0; di < 6; di++) {
915 eccbuf[di] = ReadDOC(docptr, ECCSyndrome0 + di);
918 /* Reset the ECC engine */
919 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
921 #ifdef PSYCHO_DEBUG
922 printk
923 ("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
924 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
925 eccbuf[4], eccbuf[5]);
926 #endif
929 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
931 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
932 /* There's an implicit DoC_WaitReady() in DoC_Command */
934 if (DoC_is_Millennium(this)) {
935 ReadDOC(docptr, ReadPipeInit);
936 status = ReadDOC(docptr, LastDataRead);
937 } else {
938 dummy = ReadDOC(docptr, CDSNSlowIO);
939 DoC_Delay(this, 2);
940 status = ReadDOC_(docptr, this->ioreg);
943 if (status & 1) {
944 printk(KERN_ERR "Error programming flash\n");
945 /* Error in programming */
946 *retlen = 0;
947 up(&this->lock);
948 return -EIO;
951 /* Let the caller know we completed it */
952 *retlen += len;
954 if (eccbuf) {
955 unsigned char x[8];
956 size_t dummy;
957 int ret;
959 /* Write the ECC data to flash */
960 for (di=0; di<6; di++)
961 x[di] = eccbuf[di];
963 x[6]=0x55;
964 x[7]=0x55;
966 ret = doc_write_oob_nolock(mtd, to, 8, &dummy, x);
967 if (ret) {
968 up(&this->lock);
969 return ret;
973 to += len;
974 left -= len;
975 buf += len;
978 up(&this->lock);
979 return 0;
982 static int doc_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs,
983 unsigned long count, loff_t to, size_t *retlen,
984 u_char *eccbuf, struct nand_oobinfo *oobsel)
986 static char static_buf[512];
987 static DECLARE_MUTEX(writev_buf_sem);
989 size_t totretlen = 0;
990 size_t thisvecofs = 0;
991 int ret= 0;
993 down(&writev_buf_sem);
995 while(count) {
996 size_t thislen, thisretlen;
997 unsigned char *buf;
999 buf = vecs->iov_base + thisvecofs;
1000 thislen = vecs->iov_len - thisvecofs;
1003 if (thislen >= 512) {
1004 thislen = thislen & ~(512-1);
1005 thisvecofs += thislen;
1006 } else {
1007 /* Not enough to fill a page. Copy into buf */
1008 memcpy(static_buf, buf, thislen);
1009 buf = &static_buf[thislen];
1011 while(count && thislen < 512) {
1012 vecs++;
1013 count--;
1014 thisvecofs = min((512-thislen), vecs->iov_len);
1015 memcpy(buf, vecs->iov_base, thisvecofs);
1016 thislen += thisvecofs;
1017 buf += thisvecofs;
1019 buf = static_buf;
1021 if (count && thisvecofs == vecs->iov_len) {
1022 thisvecofs = 0;
1023 vecs++;
1024 count--;
1026 ret = doc_write_ecc(mtd, to, thislen, &thisretlen, buf, eccbuf, oobsel);
1028 totretlen += thisretlen;
1030 if (ret || thisretlen != thislen)
1031 break;
1033 to += thislen;
1036 up(&writev_buf_sem);
1037 *retlen = totretlen;
1038 return ret;
1042 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
1043 size_t * retlen, u_char * buf)
1045 struct DiskOnChip *this = (struct DiskOnChip *) mtd->priv;
1046 int len256 = 0, ret;
1047 unsigned long docptr;
1048 struct Nand *mychip;
1050 down(&this->lock);
1052 docptr = this->virtadr;
1054 mychip = &this->chips[ofs >> this->chipshift];
1056 if (this->curfloor != mychip->floor) {
1057 DoC_SelectFloor(this, mychip->floor);
1058 DoC_SelectChip(this, mychip->chip);
1059 } else if (this->curchip != mychip->chip) {
1060 DoC_SelectChip(this, mychip->chip);
1062 this->curfloor = mychip->floor;
1063 this->curchip = mychip->chip;
1065 /* update address for 2M x 8bit devices. OOB starts on the second */
1066 /* page to maintain compatibility with doc_read_ecc. */
1067 if (this->page256) {
1068 if (!(ofs & 0x8))
1069 ofs += 0x100;
1070 else
1071 ofs -= 0x8;
1074 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1075 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, CDSN_CTRL_WP, 0);
1077 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1078 /* Note: datasheet says it should automaticaly wrap to the */
1079 /* next OOB block, but it didn't work here. mf. */
1080 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1081 len256 = (ofs | 0x7) + 1 - ofs;
1082 DoC_ReadBuf(this, buf, len256);
1084 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1085 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff),
1086 CDSN_CTRL_WP, 0);
1089 DoC_ReadBuf(this, &buf[len256], len - len256);
1091 *retlen = len;
1092 /* Reading the full OOB data drops us off of the end of the page,
1093 * causing the flash device to go into busy mode, so we need
1094 * to wait until ready 11.4.1 and Toshiba TC58256FT docs */
1096 ret = DoC_WaitReady(this);
1098 up(&this->lock);
1099 return ret;
1103 static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
1104 size_t * retlen, const u_char * buf)
1106 struct DiskOnChip *this = (struct DiskOnChip *) mtd->priv;
1107 int len256 = 0;
1108 unsigned long docptr = this->virtadr;
1109 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
1110 volatile int dummy;
1111 int status;
1113 // 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,
1114 // buf[0], buf[1], buf[2], buf[3], buf[8], buf[9], buf[14],buf[15]);
1116 /* Find the chip which is to be used and select it */
1117 if (this->curfloor != mychip->floor) {
1118 DoC_SelectFloor(this, mychip->floor);
1119 DoC_SelectChip(this, mychip->chip);
1120 } else if (this->curchip != mychip->chip) {
1121 DoC_SelectChip(this, mychip->chip);
1123 this->curfloor = mychip->floor;
1124 this->curchip = mychip->chip;
1126 /* disable the ECC engine */
1127 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
1128 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
1130 /* Reset the chip, see Software Requirement 11.4 item 1. */
1131 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
1133 /* issue the Read2 command to set the pointer to the Spare Data Area. */
1134 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1136 /* update address for 2M x 8bit devices. OOB starts on the second */
1137 /* page to maintain compatibility with doc_read_ecc. */
1138 if (this->page256) {
1139 if (!(ofs & 0x8))
1140 ofs += 0x100;
1141 else
1142 ofs -= 0x8;
1145 /* issue the Serial Data In command to initial the Page Program process */
1146 DoC_Command(this, NAND_CMD_SEQIN, 0);
1147 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, 0, 0);
1149 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1150 /* Note: datasheet says it should automaticaly wrap to the */
1151 /* next OOB block, but it didn't work here. mf. */
1152 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1153 len256 = (ofs | 0x7) + 1 - ofs;
1154 DoC_WriteBuf(this, buf, len256);
1156 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1157 DoC_Command(this, NAND_CMD_STATUS, 0);
1158 /* DoC_WaitReady() is implicit in DoC_Command */
1160 if (DoC_is_Millennium(this)) {
1161 ReadDOC(docptr, ReadPipeInit);
1162 status = ReadDOC(docptr, LastDataRead);
1163 } else {
1164 dummy = ReadDOC(docptr, CDSNSlowIO);
1165 DoC_Delay(this, 2);
1166 status = ReadDOC_(docptr, this->ioreg);
1169 if (status & 1) {
1170 printk(KERN_ERR "Error programming oob data\n");
1171 /* There was an error */
1172 *retlen = 0;
1173 return -EIO;
1175 DoC_Command(this, NAND_CMD_SEQIN, 0);
1176 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), 0, 0);
1179 DoC_WriteBuf(this, &buf[len256], len - len256);
1181 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1182 DoC_Command(this, NAND_CMD_STATUS, 0);
1183 /* DoC_WaitReady() is implicit in DoC_Command */
1185 if (DoC_is_Millennium(this)) {
1186 ReadDOC(docptr, ReadPipeInit);
1187 status = ReadDOC(docptr, LastDataRead);
1188 } else {
1189 dummy = ReadDOC(docptr, CDSNSlowIO);
1190 DoC_Delay(this, 2);
1191 status = ReadDOC_(docptr, this->ioreg);
1194 if (status & 1) {
1195 printk(KERN_ERR "Error programming oob data\n");
1196 /* There was an error */
1197 *retlen = 0;
1198 return -EIO;
1201 *retlen = len;
1202 return 0;
1206 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
1207 size_t * retlen, const u_char * buf)
1209 struct DiskOnChip *this = (struct DiskOnChip *) mtd->priv;
1210 int ret;
1212 down(&this->lock);
1213 ret = doc_write_oob_nolock(mtd, ofs, len, retlen, buf);
1215 up(&this->lock);
1216 return ret;
1219 static int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
1221 struct DiskOnChip *this = (struct DiskOnChip *) mtd->priv;
1222 __u32 ofs = instr->addr;
1223 __u32 len = instr->len;
1224 volatile int dummy;
1225 unsigned long docptr;
1226 struct Nand *mychip;
1227 int status;
1229 down(&this->lock);
1231 if (ofs & (mtd->erasesize-1) || len & (mtd->erasesize-1)) {
1232 up(&this->lock);
1233 return -EINVAL;
1236 instr->state = MTD_ERASING;
1238 docptr = this->virtadr;
1240 /* FIXME: Do this in the background. Use timers or schedule_task() */
1241 while(len) {
1242 mychip = &this->chips[ofs >> this->chipshift];
1244 if (this->curfloor != mychip->floor) {
1245 DoC_SelectFloor(this, mychip->floor);
1246 DoC_SelectChip(this, mychip->chip);
1247 } else if (this->curchip != mychip->chip) {
1248 DoC_SelectChip(this, mychip->chip);
1250 this->curfloor = mychip->floor;
1251 this->curchip = mychip->chip;
1253 DoC_Command(this, NAND_CMD_ERASE1, 0);
1254 DoC_Address(this, ADDR_PAGE, ofs, 0, 0);
1255 DoC_Command(this, NAND_CMD_ERASE2, 0);
1257 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
1259 if (DoC_is_Millennium(this)) {
1260 ReadDOC(docptr, ReadPipeInit);
1261 status = ReadDOC(docptr, LastDataRead);
1262 } else {
1263 dummy = ReadDOC(docptr, CDSNSlowIO);
1264 DoC_Delay(this, 2);
1265 status = ReadDOC_(docptr, this->ioreg);
1268 if (status & 1) {
1269 printk(KERN_ERR "Error erasing at 0x%x\n", ofs);
1270 /* There was an error */
1271 instr->state = MTD_ERASE_FAILED;
1272 goto callback;
1274 ofs += mtd->erasesize;
1275 len -= mtd->erasesize;
1277 instr->state = MTD_ERASE_DONE;
1279 callback:
1280 mtd_erase_callback(instr);
1282 up(&this->lock);
1283 return 0;
1287 /****************************************************************************
1289 * Module stuff
1291 ****************************************************************************/
1293 int __init init_doc2000(void)
1295 inter_module_register(im_name, THIS_MODULE, &DoC2k_init);
1296 return 0;
1299 static void __exit cleanup_doc2000(void)
1301 struct mtd_info *mtd;
1302 struct DiskOnChip *this;
1304 while ((mtd = doc2klist)) {
1305 this = (struct DiskOnChip *) mtd->priv;
1306 doc2klist = this->nextdoc;
1308 del_mtd_device(mtd);
1310 iounmap((void *) this->virtadr);
1311 kfree(this->chips);
1312 kfree(mtd);
1314 inter_module_unregister(im_name);
1317 module_exit(cleanup_doc2000);
1318 module_init(init_doc2000);
1320 MODULE_LICENSE("GPL");
1321 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
1322 MODULE_DESCRIPTION("MTD driver for DiskOnChip 2000 and Millennium");