mac80211: fix virtual interfaces vs. injection
[linux/fpc-iii.git] / drivers / mtd / devices / doc2000.c
blob50de839c77a9edf410747ba963ae4c3c93d49156
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/miscdevice.h>
14 #include <linux/delay.h>
15 #include <linux/slab.h>
16 #include <linux/sched.h>
17 #include <linux/init.h>
18 #include <linux/types.h>
19 #include <linux/bitops.h>
20 #include <linux/mutex.h>
22 #include <linux/mtd/mtd.h>
23 #include <linux/mtd/nand.h>
24 #include <linux/mtd/doc2000.h>
26 #define DOC_SUPPORT_2000
27 #define DOC_SUPPORT_2000TSOP
28 #define DOC_SUPPORT_MILLENNIUM
30 #ifdef DOC_SUPPORT_2000
31 #define DoC_is_2000(doc) (doc->ChipID == DOC_ChipID_Doc2k)
32 #else
33 #define DoC_is_2000(doc) (0)
34 #endif
36 #if defined(DOC_SUPPORT_2000TSOP) || defined(DOC_SUPPORT_MILLENNIUM)
37 #define DoC_is_Millennium(doc) (doc->ChipID == DOC_ChipID_DocMil)
38 #else
39 #define DoC_is_Millennium(doc) (0)
40 #endif
42 /* #define ECC_DEBUG */
44 /* I have no idea why some DoC chips can not use memcpy_from|to_io().
45 * This may be due to the different revisions of the ASIC controller built-in or
46 * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
47 * this:
48 #undef USE_MEMCPY
51 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
52 size_t *retlen, u_char *buf);
53 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
54 size_t *retlen, const u_char *buf);
55 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
56 struct mtd_oob_ops *ops);
57 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
58 struct mtd_oob_ops *ops);
59 static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
60 size_t *retlen, const u_char *buf);
61 static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
63 static struct mtd_info *doc2klist = NULL;
65 /* Perform the required delay cycles by reading from the appropriate register */
66 static void DoC_Delay(struct DiskOnChip *doc, unsigned short cycles)
68 volatile char dummy;
69 int i;
71 for (i = 0; i < cycles; i++) {
72 if (DoC_is_Millennium(doc))
73 dummy = ReadDOC(doc->virtadr, NOP);
74 else
75 dummy = ReadDOC(doc->virtadr, DOCStatus);
80 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
81 static int _DoC_WaitReady(struct DiskOnChip *doc)
83 void __iomem *docptr = doc->virtadr;
84 unsigned long timeo = jiffies + (HZ * 10);
86 DEBUG(MTD_DEBUG_LEVEL3,
87 "_DoC_WaitReady called for out-of-line wait\n");
89 /* Out-of-line routine to wait for chip response */
90 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
91 /* issue 2 read from NOP register after reading from CDSNControl register
92 see Software Requirement 11.4 item 2. */
93 DoC_Delay(doc, 2);
95 if (time_after(jiffies, timeo)) {
96 DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
97 return -EIO;
99 udelay(1);
100 cond_resched();
103 return 0;
106 static inline int DoC_WaitReady(struct DiskOnChip *doc)
108 void __iomem *docptr = doc->virtadr;
110 /* This is inline, to optimise the common case, where it's ready instantly */
111 int ret = 0;
113 /* 4 read form NOP register should be issued in prior to the read from CDSNControl
114 see Software Requirement 11.4 item 2. */
115 DoC_Delay(doc, 4);
117 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
118 /* Call the out-of-line routine to wait */
119 ret = _DoC_WaitReady(doc);
121 /* issue 2 read from NOP register after reading from CDSNControl register
122 see Software Requirement 11.4 item 2. */
123 DoC_Delay(doc, 2);
125 return ret;
128 /* DoC_Command: Send a flash command to the flash chip through the CDSN Slow IO register to
129 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
130 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
132 static int DoC_Command(struct DiskOnChip *doc, unsigned char command,
133 unsigned char xtraflags)
135 void __iomem *docptr = doc->virtadr;
137 if (DoC_is_2000(doc))
138 xtraflags |= CDSN_CTRL_FLASH_IO;
140 /* Assert the CLE (Command Latch Enable) line to the flash chip */
141 WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
142 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
144 if (DoC_is_Millennium(doc))
145 WriteDOC(command, docptr, CDSNSlowIO);
147 /* Send the command */
148 WriteDOC_(command, docptr, doc->ioreg);
149 if (DoC_is_Millennium(doc))
150 WriteDOC(command, docptr, WritePipeTerm);
152 /* Lower the CLE line */
153 WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
154 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
156 /* Wait for the chip to respond - Software requirement 11.4.1 (extended for any command) */
157 return DoC_WaitReady(doc);
160 /* DoC_Address: Set the current address for the flash chip through the CDSN Slow IO register to
161 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
162 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
164 static int DoC_Address(struct DiskOnChip *doc, int numbytes, unsigned long ofs,
165 unsigned char xtraflags1, unsigned char xtraflags2)
167 int i;
168 void __iomem *docptr = doc->virtadr;
170 if (DoC_is_2000(doc))
171 xtraflags1 |= CDSN_CTRL_FLASH_IO;
173 /* Assert the ALE (Address Latch Enable) line to the flash chip */
174 WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
176 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
178 /* Send the address */
179 /* Devices with 256-byte page are addressed as:
180 Column (bits 0-7), Page (bits 8-15, 16-23, 24-31)
181 * there is no device on the market with page256
182 and more than 24 bits.
183 Devices with 512-byte page are addressed as:
184 Column (bits 0-7), Page (bits 9-16, 17-24, 25-31)
185 * 25-31 is sent only if the chip support it.
186 * bit 8 changes the read command to be sent
187 (NAND_CMD_READ0 or NAND_CMD_READ1).
190 if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE) {
191 if (DoC_is_Millennium(doc))
192 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
193 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
196 if (doc->page256) {
197 ofs = ofs >> 8;
198 } else {
199 ofs = ofs >> 9;
202 if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE) {
203 for (i = 0; i < doc->pageadrlen; i++, ofs = ofs >> 8) {
204 if (DoC_is_Millennium(doc))
205 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
206 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
210 if (DoC_is_Millennium(doc))
211 WriteDOC(ofs & 0xff, docptr, WritePipeTerm);
213 DoC_Delay(doc, 2); /* Needed for some slow flash chips. mf. */
215 /* FIXME: The SlowIO's for millennium could be replaced by
216 a single WritePipeTerm here. mf. */
218 /* Lower the ALE line */
219 WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr,
220 CDSNControl);
222 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
224 /* Wait for the chip to respond - Software requirement 11.4.1 */
225 return DoC_WaitReady(doc);
228 /* Read a buffer from DoC, taking care of Millennium odditys */
229 static void DoC_ReadBuf(struct DiskOnChip *doc, u_char * buf, int len)
231 volatile int dummy;
232 int modulus = 0xffff;
233 void __iomem *docptr = doc->virtadr;
234 int i;
236 if (len <= 0)
237 return;
239 if (DoC_is_Millennium(doc)) {
240 /* Read the data via the internal pipeline through CDSN IO register,
241 see Pipelined Read Operations 11.3 */
242 dummy = ReadDOC(docptr, ReadPipeInit);
244 /* Millennium should use the LastDataRead register - Pipeline Reads */
245 len--;
247 /* This is needed for correctly ECC calculation */
248 modulus = 0xff;
251 for (i = 0; i < len; i++)
252 buf[i] = ReadDOC_(docptr, doc->ioreg + (i & modulus));
254 if (DoC_is_Millennium(doc)) {
255 buf[i] = ReadDOC(docptr, LastDataRead);
259 /* Write a buffer to DoC, taking care of Millennium odditys */
260 static void DoC_WriteBuf(struct DiskOnChip *doc, const u_char * buf, int len)
262 void __iomem *docptr = doc->virtadr;
263 int i;
265 if (len <= 0)
266 return;
268 for (i = 0; i < len; i++)
269 WriteDOC_(buf[i], docptr, doc->ioreg + i);
271 if (DoC_is_Millennium(doc)) {
272 WriteDOC(0x00, docptr, WritePipeTerm);
277 /* DoC_SelectChip: Select a given flash chip within the current floor */
279 static inline int DoC_SelectChip(struct DiskOnChip *doc, int chip)
281 void __iomem *docptr = doc->virtadr;
283 /* Software requirement 11.4.4 before writing DeviceSelect */
284 /* Deassert the CE line to eliminate glitches on the FCE# outputs */
285 WriteDOC(CDSN_CTRL_WP, docptr, CDSNControl);
286 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
288 /* Select the individual flash chip requested */
289 WriteDOC(chip, docptr, CDSNDeviceSelect);
290 DoC_Delay(doc, 4);
292 /* Reassert the CE line */
293 WriteDOC(CDSN_CTRL_CE | CDSN_CTRL_FLASH_IO | CDSN_CTRL_WP, docptr,
294 CDSNControl);
295 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
297 /* Wait for it to be ready */
298 return DoC_WaitReady(doc);
301 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
303 static inline int DoC_SelectFloor(struct DiskOnChip *doc, int floor)
305 void __iomem *docptr = doc->virtadr;
307 /* Select the floor (bank) of chips required */
308 WriteDOC(floor, docptr, FloorSelect);
310 /* Wait for the chip to be ready */
311 return DoC_WaitReady(doc);
314 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
316 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
318 int mfr, id, i, j;
319 volatile char dummy;
321 /* Page in the required floor/chip */
322 DoC_SelectFloor(doc, floor);
323 DoC_SelectChip(doc, chip);
325 /* Reset the chip */
326 if (DoC_Command(doc, NAND_CMD_RESET, CDSN_CTRL_WP)) {
327 DEBUG(MTD_DEBUG_LEVEL2,
328 "DoC_Command (reset) for %d,%d returned true\n",
329 floor, chip);
330 return 0;
334 /* Read the NAND chip ID: 1. Send ReadID command */
335 if (DoC_Command(doc, NAND_CMD_READID, CDSN_CTRL_WP)) {
336 DEBUG(MTD_DEBUG_LEVEL2,
337 "DoC_Command (ReadID) for %d,%d returned true\n",
338 floor, chip);
339 return 0;
342 /* Read the NAND chip ID: 2. Send address byte zero */
343 DoC_Address(doc, ADDR_COLUMN, 0, CDSN_CTRL_WP, 0);
345 /* Read the manufacturer and device id codes from the device */
347 if (DoC_is_Millennium(doc)) {
348 DoC_Delay(doc, 2);
349 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
350 mfr = ReadDOC(doc->virtadr, LastDataRead);
352 DoC_Delay(doc, 2);
353 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
354 id = ReadDOC(doc->virtadr, LastDataRead);
355 } else {
356 /* CDSN Slow IO register see Software Req 11.4 item 5. */
357 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
358 DoC_Delay(doc, 2);
359 mfr = ReadDOC_(doc->virtadr, doc->ioreg);
361 /* CDSN Slow IO register see Software Req 11.4 item 5. */
362 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
363 DoC_Delay(doc, 2);
364 id = ReadDOC_(doc->virtadr, doc->ioreg);
367 /* No response - return failure */
368 if (mfr == 0xff || mfr == 0)
369 return 0;
371 /* Check it's the same as the first chip we identified.
372 * M-Systems say that any given DiskOnChip device should only
373 * contain _one_ type of flash part, although that's not a
374 * hardware restriction. */
375 if (doc->mfr) {
376 if (doc->mfr == mfr && doc->id == id)
377 return 1; /* This is the same as the first */
378 else
379 printk(KERN_WARNING
380 "Flash chip at floor %d, chip %d is different:\n",
381 floor, chip);
384 /* Print and store the manufacturer and ID codes. */
385 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
386 if (id == nand_flash_ids[i].id) {
387 /* Try to identify manufacturer */
388 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
389 if (nand_manuf_ids[j].id == mfr)
390 break;
392 printk(KERN_INFO
393 "Flash chip found: Manufacturer ID: %2.2X, "
394 "Chip ID: %2.2X (%s:%s)\n", mfr, id,
395 nand_manuf_ids[j].name, nand_flash_ids[i].name);
396 if (!doc->mfr) {
397 doc->mfr = mfr;
398 doc->id = id;
399 doc->chipshift =
400 ffs((nand_flash_ids[i].chipsize << 20)) - 1;
401 doc->page256 = (nand_flash_ids[i].pagesize == 256) ? 1 : 0;
402 doc->pageadrlen = doc->chipshift > 25 ? 3 : 2;
403 doc->erasesize =
404 nand_flash_ids[i].erasesize;
405 return 1;
407 return 0;
412 /* We haven't fully identified the chip. Print as much as we know. */
413 printk(KERN_WARNING "Unknown flash chip found: %2.2X %2.2X\n",
414 id, mfr);
416 printk(KERN_WARNING "Please report to dwmw2@infradead.org\n");
417 return 0;
420 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
422 static void DoC_ScanChips(struct DiskOnChip *this, int maxchips)
424 int floor, chip;
425 int numchips[MAX_FLOORS];
426 int ret = 1;
428 this->numchips = 0;
429 this->mfr = 0;
430 this->id = 0;
432 /* For each floor, find the number of valid chips it contains */
433 for (floor = 0; floor < MAX_FLOORS; floor++) {
434 ret = 1;
435 numchips[floor] = 0;
436 for (chip = 0; chip < maxchips && ret != 0; chip++) {
438 ret = DoC_IdentChip(this, floor, chip);
439 if (ret) {
440 numchips[floor]++;
441 this->numchips++;
446 /* If there are none at all that we recognise, bail */
447 if (!this->numchips) {
448 printk(KERN_NOTICE "No flash chips recognised.\n");
449 return;
452 /* Allocate an array to hold the information for each chip */
453 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
454 if (!this->chips) {
455 printk(KERN_NOTICE "No memory for allocating chip info structures\n");
456 return;
459 ret = 0;
461 /* Fill out the chip array with {floor, chipno} for each
462 * detected chip in the device. */
463 for (floor = 0; floor < MAX_FLOORS; floor++) {
464 for (chip = 0; chip < numchips[floor]; chip++) {
465 this->chips[ret].floor = floor;
466 this->chips[ret].chip = chip;
467 this->chips[ret].curadr = 0;
468 this->chips[ret].curmode = 0x50;
469 ret++;
473 /* Calculate and print the total size of the device */
474 this->totlen = this->numchips * (1 << this->chipshift);
476 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
477 this->numchips, this->totlen >> 20);
480 static int DoC2k_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
482 int tmp1, tmp2, retval;
483 if (doc1->physadr == doc2->physadr)
484 return 1;
486 /* Use the alias resolution register which was set aside for this
487 * purpose. If it's value is the same on both chips, they might
488 * be the same chip, and we write to one and check for a change in
489 * the other. It's unclear if this register is usuable in the
490 * DoC 2000 (it's in the Millennium docs), but it seems to work. */
491 tmp1 = ReadDOC(doc1->virtadr, AliasResolution);
492 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
493 if (tmp1 != tmp2)
494 return 0;
496 WriteDOC((tmp1 + 1) % 0xff, doc1->virtadr, AliasResolution);
497 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
498 if (tmp2 == (tmp1 + 1) % 0xff)
499 retval = 1;
500 else
501 retval = 0;
503 /* Restore register contents. May not be necessary, but do it just to
504 * be safe. */
505 WriteDOC(tmp1, doc1->virtadr, AliasResolution);
507 return retval;
510 /* This routine is found from the docprobe code by symbol_get(),
511 * which will bump the use count of this module. */
512 void DoC2k_init(struct mtd_info *mtd)
514 struct DiskOnChip *this = mtd->priv;
515 struct DiskOnChip *old = NULL;
516 int maxchips;
518 /* We must avoid being called twice for the same device. */
520 if (doc2klist)
521 old = doc2klist->priv;
523 while (old) {
524 if (DoC2k_is_alias(old, this)) {
525 printk(KERN_NOTICE
526 "Ignoring DiskOnChip 2000 at 0x%lX - already configured\n",
527 this->physadr);
528 iounmap(this->virtadr);
529 kfree(mtd);
530 return;
532 if (old->nextdoc)
533 old = old->nextdoc->priv;
534 else
535 old = NULL;
539 switch (this->ChipID) {
540 case DOC_ChipID_Doc2kTSOP:
541 mtd->name = "DiskOnChip 2000 TSOP";
542 this->ioreg = DoC_Mil_CDSN_IO;
543 /* Pretend it's a Millennium */
544 this->ChipID = DOC_ChipID_DocMil;
545 maxchips = MAX_CHIPS;
546 break;
547 case DOC_ChipID_Doc2k:
548 mtd->name = "DiskOnChip 2000";
549 this->ioreg = DoC_2k_CDSN_IO;
550 maxchips = MAX_CHIPS;
551 break;
552 case DOC_ChipID_DocMil:
553 mtd->name = "DiskOnChip Millennium";
554 this->ioreg = DoC_Mil_CDSN_IO;
555 maxchips = MAX_CHIPS_MIL;
556 break;
557 default:
558 printk("Unknown ChipID 0x%02x\n", this->ChipID);
559 kfree(mtd);
560 iounmap(this->virtadr);
561 return;
564 printk(KERN_NOTICE "%s found at address 0x%lX\n", mtd->name,
565 this->physadr);
567 mtd->type = MTD_NANDFLASH;
568 mtd->flags = MTD_CAP_NANDFLASH;
569 mtd->size = 0;
570 mtd->erasesize = 0;
571 mtd->writesize = 512;
572 mtd->oobsize = 16;
573 mtd->owner = THIS_MODULE;
574 mtd->erase = doc_erase;
575 mtd->point = NULL;
576 mtd->unpoint = NULL;
577 mtd->read = doc_read;
578 mtd->write = doc_write;
579 mtd->read_oob = doc_read_oob;
580 mtd->write_oob = doc_write_oob;
581 mtd->sync = NULL;
583 this->totlen = 0;
584 this->numchips = 0;
586 this->curfloor = -1;
587 this->curchip = -1;
588 mutex_init(&this->lock);
590 /* Ident all the chips present. */
591 DoC_ScanChips(this, maxchips);
593 if (!this->totlen) {
594 kfree(mtd);
595 iounmap(this->virtadr);
596 } else {
597 this->nextdoc = doc2klist;
598 doc2klist = mtd;
599 mtd->size = this->totlen;
600 mtd->erasesize = this->erasesize;
601 add_mtd_device(mtd);
602 return;
605 EXPORT_SYMBOL_GPL(DoC2k_init);
607 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
608 size_t * retlen, u_char * buf)
610 struct DiskOnChip *this = mtd->priv;
611 void __iomem *docptr = this->virtadr;
612 struct Nand *mychip;
613 unsigned char syndrome[6], eccbuf[6];
614 volatile char dummy;
615 int i, len256 = 0, ret=0;
616 size_t left = len;
618 /* Don't allow read past end of device */
619 if (from >= this->totlen)
620 return -EINVAL;
622 mutex_lock(&this->lock);
624 *retlen = 0;
625 while (left) {
626 len = left;
628 /* Don't allow a single read to cross a 512-byte block boundary */
629 if (from + len > ((from | 0x1ff) + 1))
630 len = ((from | 0x1ff) + 1) - from;
632 /* The ECC will not be calculated correctly if less than 512 is read */
633 if (len != 0x200)
634 printk(KERN_WARNING
635 "ECC needs a full sector read (adr: %lx size %lx)\n",
636 (long) from, (long) len);
638 /* printk("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len); */
641 /* Find the chip which is to be used and select it */
642 mychip = &this->chips[from >> (this->chipshift)];
644 if (this->curfloor != mychip->floor) {
645 DoC_SelectFloor(this, mychip->floor);
646 DoC_SelectChip(this, mychip->chip);
647 } else if (this->curchip != mychip->chip) {
648 DoC_SelectChip(this, mychip->chip);
651 this->curfloor = mychip->floor;
652 this->curchip = mychip->chip;
654 DoC_Command(this,
655 (!this->page256
656 && (from & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
657 CDSN_CTRL_WP);
658 DoC_Address(this, ADDR_COLUMN_PAGE, from, CDSN_CTRL_WP,
659 CDSN_CTRL_ECC_IO);
661 /* Prime the ECC engine */
662 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
663 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
665 /* treat crossing 256-byte sector for 2M x 8bits devices */
666 if (this->page256 && from + len > (from | 0xff) + 1) {
667 len256 = (from | 0xff) + 1 - from;
668 DoC_ReadBuf(this, buf, len256);
670 DoC_Command(this, NAND_CMD_READ0, CDSN_CTRL_WP);
671 DoC_Address(this, ADDR_COLUMN_PAGE, from + len256,
672 CDSN_CTRL_WP, CDSN_CTRL_ECC_IO);
675 DoC_ReadBuf(this, &buf[len256], len - len256);
677 /* Let the caller know we completed it */
678 *retlen += len;
680 /* Read the ECC data through the DiskOnChip ECC logic */
681 /* Note: this will work even with 2M x 8bit devices as */
682 /* they have 8 bytes of OOB per 256 page. mf. */
683 DoC_ReadBuf(this, eccbuf, 6);
685 /* Flush the pipeline */
686 if (DoC_is_Millennium(this)) {
687 dummy = ReadDOC(docptr, ECCConf);
688 dummy = ReadDOC(docptr, ECCConf);
689 i = ReadDOC(docptr, ECCConf);
690 } else {
691 dummy = ReadDOC(docptr, 2k_ECCStatus);
692 dummy = ReadDOC(docptr, 2k_ECCStatus);
693 i = ReadDOC(docptr, 2k_ECCStatus);
696 /* Check the ECC Status */
697 if (i & 0x80) {
698 int nb_errors;
699 /* There was an ECC error */
700 #ifdef ECC_DEBUG
701 printk(KERN_ERR "DiskOnChip ECC Error: Read at %lx\n", (long)from);
702 #endif
703 /* Read the ECC syndrom through the DiskOnChip ECC
704 logic. These syndrome will be all ZERO when there
705 is no error */
706 for (i = 0; i < 6; i++) {
707 syndrome[i] =
708 ReadDOC(docptr, ECCSyndrome0 + i);
710 nb_errors = doc_decode_ecc(buf, syndrome);
712 #ifdef ECC_DEBUG
713 printk(KERN_ERR "Errors corrected: %x\n", nb_errors);
714 #endif
715 if (nb_errors < 0) {
716 /* We return error, but have actually done the
717 read. Not that this can be told to
718 user-space, via sys_read(), but at least
719 MTD-aware stuff can know about it by
720 checking *retlen */
721 ret = -EIO;
725 #ifdef PSYCHO_DEBUG
726 printk(KERN_DEBUG "ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
727 (long)from, eccbuf[0], eccbuf[1], eccbuf[2],
728 eccbuf[3], eccbuf[4], eccbuf[5]);
729 #endif
731 /* disable the ECC engine */
732 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
734 /* according to 11.4.1, we need to wait for the busy line
735 * drop if we read to the end of the page. */
736 if(0 == ((from + len) & 0x1ff))
738 DoC_WaitReady(this);
741 from += len;
742 left -= len;
743 buf += len;
746 mutex_unlock(&this->lock);
748 return ret;
751 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
752 size_t * retlen, const u_char * buf)
754 struct DiskOnChip *this = mtd->priv;
755 int di; /* Yes, DI is a hangover from when I was disassembling the binary driver */
756 void __iomem *docptr = this->virtadr;
757 unsigned char eccbuf[6];
758 volatile char dummy;
759 int len256 = 0;
760 struct Nand *mychip;
761 size_t left = len;
762 int status;
764 /* Don't allow write past end of device */
765 if (to >= this->totlen)
766 return -EINVAL;
768 mutex_lock(&this->lock);
770 *retlen = 0;
771 while (left) {
772 len = left;
774 /* Don't allow a single write to cross a 512-byte block boundary */
775 if (to + len > ((to | 0x1ff) + 1))
776 len = ((to | 0x1ff) + 1) - to;
778 /* The ECC will not be calculated correctly if less than 512 is written */
779 /* DBB-
780 if (len != 0x200 && eccbuf)
781 printk(KERN_WARNING
782 "ECC needs a full sector write (adr: %lx size %lx)\n",
783 (long) to, (long) len);
784 -DBB */
786 /* printk("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */
788 /* Find the chip which is to be used and select it */
789 mychip = &this->chips[to >> (this->chipshift)];
791 if (this->curfloor != mychip->floor) {
792 DoC_SelectFloor(this, mychip->floor);
793 DoC_SelectChip(this, mychip->chip);
794 } else if (this->curchip != mychip->chip) {
795 DoC_SelectChip(this, mychip->chip);
798 this->curfloor = mychip->floor;
799 this->curchip = mychip->chip;
801 /* Set device to main plane of flash */
802 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
803 DoC_Command(this,
804 (!this->page256
805 && (to & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
806 CDSN_CTRL_WP);
808 DoC_Command(this, NAND_CMD_SEQIN, 0);
809 DoC_Address(this, ADDR_COLUMN_PAGE, to, 0, CDSN_CTRL_ECC_IO);
811 /* Prime the ECC engine */
812 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
813 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
815 /* treat crossing 256-byte sector for 2M x 8bits devices */
816 if (this->page256 && to + len > (to | 0xff) + 1) {
817 len256 = (to | 0xff) + 1 - to;
818 DoC_WriteBuf(this, buf, len256);
820 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
822 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
823 /* There's an implicit DoC_WaitReady() in DoC_Command */
825 dummy = ReadDOC(docptr, CDSNSlowIO);
826 DoC_Delay(this, 2);
828 if (ReadDOC_(docptr, this->ioreg) & 1) {
829 printk(KERN_ERR "Error programming flash\n");
830 /* Error in programming */
831 *retlen = 0;
832 mutex_unlock(&this->lock);
833 return -EIO;
836 DoC_Command(this, NAND_CMD_SEQIN, 0);
837 DoC_Address(this, ADDR_COLUMN_PAGE, to + len256, 0,
838 CDSN_CTRL_ECC_IO);
841 DoC_WriteBuf(this, &buf[len256], len - len256);
843 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_CE, docptr, CDSNControl);
845 if (DoC_is_Millennium(this)) {
846 WriteDOC(0, docptr, NOP);
847 WriteDOC(0, docptr, NOP);
848 WriteDOC(0, docptr, NOP);
849 } else {
850 WriteDOC_(0, docptr, this->ioreg);
851 WriteDOC_(0, docptr, this->ioreg);
852 WriteDOC_(0, docptr, this->ioreg);
855 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_FLASH_IO | CDSN_CTRL_CE, docptr,
856 CDSNControl);
858 /* Read the ECC data through the DiskOnChip ECC logic */
859 for (di = 0; di < 6; di++) {
860 eccbuf[di] = ReadDOC(docptr, ECCSyndrome0 + di);
863 /* Reset the ECC engine */
864 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
866 #ifdef PSYCHO_DEBUG
867 printk
868 ("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
869 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
870 eccbuf[4], eccbuf[5]);
871 #endif
872 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
874 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
875 /* There's an implicit DoC_WaitReady() in DoC_Command */
877 if (DoC_is_Millennium(this)) {
878 ReadDOC(docptr, ReadPipeInit);
879 status = ReadDOC(docptr, LastDataRead);
880 } else {
881 dummy = ReadDOC(docptr, CDSNSlowIO);
882 DoC_Delay(this, 2);
883 status = ReadDOC_(docptr, this->ioreg);
886 if (status & 1) {
887 printk(KERN_ERR "Error programming flash\n");
888 /* Error in programming */
889 *retlen = 0;
890 mutex_unlock(&this->lock);
891 return -EIO;
894 /* Let the caller know we completed it */
895 *retlen += len;
898 unsigned char x[8];
899 size_t dummy;
900 int ret;
902 /* Write the ECC data to flash */
903 for (di=0; di<6; di++)
904 x[di] = eccbuf[di];
906 x[6]=0x55;
907 x[7]=0x55;
909 ret = doc_write_oob_nolock(mtd, to, 8, &dummy, x);
910 if (ret) {
911 mutex_unlock(&this->lock);
912 return ret;
916 to += len;
917 left -= len;
918 buf += len;
921 mutex_unlock(&this->lock);
922 return 0;
925 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
926 struct mtd_oob_ops *ops)
928 struct DiskOnChip *this = mtd->priv;
929 int len256 = 0, ret;
930 struct Nand *mychip;
931 uint8_t *buf = ops->oobbuf;
932 size_t len = ops->len;
934 BUG_ON(ops->mode != MTD_OOB_PLACE);
936 ofs += ops->ooboffs;
938 mutex_lock(&this->lock);
940 mychip = &this->chips[ofs >> this->chipshift];
942 if (this->curfloor != mychip->floor) {
943 DoC_SelectFloor(this, mychip->floor);
944 DoC_SelectChip(this, mychip->chip);
945 } else if (this->curchip != mychip->chip) {
946 DoC_SelectChip(this, mychip->chip);
948 this->curfloor = mychip->floor;
949 this->curchip = mychip->chip;
951 /* update address for 2M x 8bit devices. OOB starts on the second */
952 /* page to maintain compatibility with doc_read_ecc. */
953 if (this->page256) {
954 if (!(ofs & 0x8))
955 ofs += 0x100;
956 else
957 ofs -= 0x8;
960 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
961 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, CDSN_CTRL_WP, 0);
963 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
964 /* Note: datasheet says it should automaticaly wrap to the */
965 /* next OOB block, but it didn't work here. mf. */
966 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
967 len256 = (ofs | 0x7) + 1 - ofs;
968 DoC_ReadBuf(this, buf, len256);
970 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
971 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff),
972 CDSN_CTRL_WP, 0);
975 DoC_ReadBuf(this, &buf[len256], len - len256);
977 ops->retlen = len;
978 /* Reading the full OOB data drops us off of the end of the page,
979 * causing the flash device to go into busy mode, so we need
980 * to wait until ready 11.4.1 and Toshiba TC58256FT docs */
982 ret = DoC_WaitReady(this);
984 mutex_unlock(&this->lock);
985 return ret;
989 static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
990 size_t * retlen, const u_char * buf)
992 struct DiskOnChip *this = mtd->priv;
993 int len256 = 0;
994 void __iomem *docptr = this->virtadr;
995 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
996 volatile int dummy;
997 int status;
999 // 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,
1000 // buf[0], buf[1], buf[2], buf[3], buf[8], buf[9], buf[14],buf[15]);
1002 /* Find the chip which is to be used and select it */
1003 if (this->curfloor != mychip->floor) {
1004 DoC_SelectFloor(this, mychip->floor);
1005 DoC_SelectChip(this, mychip->chip);
1006 } else if (this->curchip != mychip->chip) {
1007 DoC_SelectChip(this, mychip->chip);
1009 this->curfloor = mychip->floor;
1010 this->curchip = mychip->chip;
1012 /* disable the ECC engine */
1013 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
1014 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
1016 /* Reset the chip, see Software Requirement 11.4 item 1. */
1017 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
1019 /* issue the Read2 command to set the pointer to the Spare Data Area. */
1020 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1022 /* update address for 2M x 8bit devices. OOB starts on the second */
1023 /* page to maintain compatibility with doc_read_ecc. */
1024 if (this->page256) {
1025 if (!(ofs & 0x8))
1026 ofs += 0x100;
1027 else
1028 ofs -= 0x8;
1031 /* issue the Serial Data In command to initial the Page Program process */
1032 DoC_Command(this, NAND_CMD_SEQIN, 0);
1033 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, 0, 0);
1035 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1036 /* Note: datasheet says it should automaticaly wrap to the */
1037 /* next OOB block, but it didn't work here. mf. */
1038 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1039 len256 = (ofs | 0x7) + 1 - ofs;
1040 DoC_WriteBuf(this, buf, len256);
1042 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1043 DoC_Command(this, NAND_CMD_STATUS, 0);
1044 /* DoC_WaitReady() is implicit in DoC_Command */
1046 if (DoC_is_Millennium(this)) {
1047 ReadDOC(docptr, ReadPipeInit);
1048 status = ReadDOC(docptr, LastDataRead);
1049 } else {
1050 dummy = ReadDOC(docptr, CDSNSlowIO);
1051 DoC_Delay(this, 2);
1052 status = ReadDOC_(docptr, this->ioreg);
1055 if (status & 1) {
1056 printk(KERN_ERR "Error programming oob data\n");
1057 /* There was an error */
1058 *retlen = 0;
1059 return -EIO;
1061 DoC_Command(this, NAND_CMD_SEQIN, 0);
1062 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), 0, 0);
1065 DoC_WriteBuf(this, &buf[len256], len - len256);
1067 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1068 DoC_Command(this, NAND_CMD_STATUS, 0);
1069 /* DoC_WaitReady() is implicit in DoC_Command */
1071 if (DoC_is_Millennium(this)) {
1072 ReadDOC(docptr, ReadPipeInit);
1073 status = ReadDOC(docptr, LastDataRead);
1074 } else {
1075 dummy = ReadDOC(docptr, CDSNSlowIO);
1076 DoC_Delay(this, 2);
1077 status = ReadDOC_(docptr, this->ioreg);
1080 if (status & 1) {
1081 printk(KERN_ERR "Error programming oob data\n");
1082 /* There was an error */
1083 *retlen = 0;
1084 return -EIO;
1087 *retlen = len;
1088 return 0;
1092 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
1093 struct mtd_oob_ops *ops)
1095 struct DiskOnChip *this = mtd->priv;
1096 int ret;
1098 BUG_ON(ops->mode != MTD_OOB_PLACE);
1100 mutex_lock(&this->lock);
1101 ret = doc_write_oob_nolock(mtd, ofs + ops->ooboffs, ops->len,
1102 &ops->retlen, ops->oobbuf);
1104 mutex_unlock(&this->lock);
1105 return ret;
1108 static int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
1110 struct DiskOnChip *this = mtd->priv;
1111 __u32 ofs = instr->addr;
1112 __u32 len = instr->len;
1113 volatile int dummy;
1114 void __iomem *docptr = this->virtadr;
1115 struct Nand *mychip;
1116 int status;
1118 mutex_lock(&this->lock);
1120 if (ofs & (mtd->erasesize-1) || len & (mtd->erasesize-1)) {
1121 mutex_unlock(&this->lock);
1122 return -EINVAL;
1125 instr->state = MTD_ERASING;
1127 /* FIXME: Do this in the background. Use timers or schedule_task() */
1128 while(len) {
1129 mychip = &this->chips[ofs >> this->chipshift];
1131 if (this->curfloor != mychip->floor) {
1132 DoC_SelectFloor(this, mychip->floor);
1133 DoC_SelectChip(this, mychip->chip);
1134 } else if (this->curchip != mychip->chip) {
1135 DoC_SelectChip(this, mychip->chip);
1137 this->curfloor = mychip->floor;
1138 this->curchip = mychip->chip;
1140 DoC_Command(this, NAND_CMD_ERASE1, 0);
1141 DoC_Address(this, ADDR_PAGE, ofs, 0, 0);
1142 DoC_Command(this, NAND_CMD_ERASE2, 0);
1144 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
1146 if (DoC_is_Millennium(this)) {
1147 ReadDOC(docptr, ReadPipeInit);
1148 status = ReadDOC(docptr, LastDataRead);
1149 } else {
1150 dummy = ReadDOC(docptr, CDSNSlowIO);
1151 DoC_Delay(this, 2);
1152 status = ReadDOC_(docptr, this->ioreg);
1155 if (status & 1) {
1156 printk(KERN_ERR "Error erasing at 0x%x\n", ofs);
1157 /* There was an error */
1158 instr->state = MTD_ERASE_FAILED;
1159 goto callback;
1161 ofs += mtd->erasesize;
1162 len -= mtd->erasesize;
1164 instr->state = MTD_ERASE_DONE;
1166 callback:
1167 mtd_erase_callback(instr);
1169 mutex_unlock(&this->lock);
1170 return 0;
1174 /****************************************************************************
1176 * Module stuff
1178 ****************************************************************************/
1180 static void __exit cleanup_doc2000(void)
1182 struct mtd_info *mtd;
1183 struct DiskOnChip *this;
1185 while ((mtd = doc2klist)) {
1186 this = mtd->priv;
1187 doc2klist = this->nextdoc;
1189 del_mtd_device(mtd);
1191 iounmap(this->virtadr);
1192 kfree(this->chips);
1193 kfree(mtd);
1197 module_exit(cleanup_doc2000);
1199 MODULE_LICENSE("GPL");
1200 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
1201 MODULE_DESCRIPTION("MTD driver for DiskOnChip 2000 and Millennium");