Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[sfrench/cifs-2.6.git] / drivers / mtd / devices / doc2000.c
1
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  *
7  * $Id: doc2000.c,v 1.67 2005/11/07 11:14:24 gleixner Exp $
8  */
9
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <asm/errno.h>
13 #include <asm/io.h>
14 #include <asm/uaccess.h>
15 #include <linux/miscdevice.h>
16 #include <linux/pci.h>
17 #include <linux/delay.h>
18 #include <linux/slab.h>
19 #include <linux/sched.h>
20 #include <linux/init.h>
21 #include <linux/types.h>
22 #include <linux/bitops.h>
23 #include <linux/mutex.h>
24
25 #include <linux/mtd/mtd.h>
26 #include <linux/mtd/nand.h>
27 #include <linux/mtd/doc2000.h>
28
29 #define DOC_SUPPORT_2000
30 #define DOC_SUPPORT_2000TSOP
31 #define DOC_SUPPORT_MILLENNIUM
32
33 #ifdef DOC_SUPPORT_2000
34 #define DoC_is_2000(doc) (doc->ChipID == DOC_ChipID_Doc2k)
35 #else
36 #define DoC_is_2000(doc) (0)
37 #endif
38
39 #if defined(DOC_SUPPORT_2000TSOP) || defined(DOC_SUPPORT_MILLENNIUM)
40 #define DoC_is_Millennium(doc) (doc->ChipID == DOC_ChipID_DocMil)
41 #else
42 #define DoC_is_Millennium(doc) (0)
43 #endif
44
45 /* #define ECC_DEBUG */
46
47 /* I have no idea why some DoC chips can not use memcpy_from|to_io().
48  * This may be due to the different revisions of the ASIC controller built-in or
49  * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
50  * this:
51  #undef USE_MEMCPY
52 */
53
54 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
55                     size_t *retlen, u_char *buf);
56 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
57                      size_t *retlen, const u_char *buf);
58 static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
59                         size_t *retlen, u_char *buf, u_char *eccbuf, struct nand_oobinfo *oobsel);
60 static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
61                          size_t *retlen, const u_char *buf, u_char *eccbuf, struct nand_oobinfo *oobsel);
62 static int doc_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs,
63                           unsigned long count, loff_t to, size_t *retlen,
64                           u_char *eccbuf, struct nand_oobinfo *oobsel);
65 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
66                         size_t *retlen, u_char *buf);
67 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
68                          size_t *retlen, const u_char *buf);
69 static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
70                          size_t *retlen, const u_char *buf);
71 static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
72
73 static struct mtd_info *doc2klist = NULL;
74
75 /* Perform the required delay cycles by reading from the appropriate register */
76 static void DoC_Delay(struct DiskOnChip *doc, unsigned short cycles)
77 {
78         volatile char dummy;
79         int i;
80
81         for (i = 0; i < cycles; i++) {
82                 if (DoC_is_Millennium(doc))
83                         dummy = ReadDOC(doc->virtadr, NOP);
84                 else
85                         dummy = ReadDOC(doc->virtadr, DOCStatus);
86         }
87
88 }
89
90 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
91 static int _DoC_WaitReady(struct DiskOnChip *doc)
92 {
93         void __iomem *docptr = doc->virtadr;
94         unsigned long timeo = jiffies + (HZ * 10);
95
96         DEBUG(MTD_DEBUG_LEVEL3,
97               "_DoC_WaitReady called for out-of-line wait\n");
98
99         /* Out-of-line routine to wait for chip response */
100         while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
101                 /* issue 2 read from NOP register after reading from CDSNControl register
102                 see Software Requirement 11.4 item 2. */
103                 DoC_Delay(doc, 2);
104
105                 if (time_after(jiffies, timeo)) {
106                         DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
107                         return -EIO;
108                 }
109                 udelay(1);
110                 cond_resched();
111         }
112
113         return 0;
114 }
115
116 static inline int DoC_WaitReady(struct DiskOnChip *doc)
117 {
118         void __iomem *docptr = doc->virtadr;
119
120         /* This is inline, to optimise the common case, where it's ready instantly */
121         int ret = 0;
122
123         /* 4 read form NOP register should be issued in prior to the read from CDSNControl
124            see Software Requirement 11.4 item 2. */
125         DoC_Delay(doc, 4);
126
127         if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
128                 /* Call the out-of-line routine to wait */
129                 ret = _DoC_WaitReady(doc);
130
131         /* issue 2 read from NOP register after reading from CDSNControl register
132            see Software Requirement 11.4 item 2. */
133         DoC_Delay(doc, 2);
134
135         return ret;
136 }
137
138 /* DoC_Command: Send a flash command to the flash chip through the CDSN Slow IO register to
139    bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
140    required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
141
142 static int DoC_Command(struct DiskOnChip *doc, unsigned char command,
143                               unsigned char xtraflags)
144 {
145         void __iomem *docptr = doc->virtadr;
146
147         if (DoC_is_2000(doc))
148                 xtraflags |= CDSN_CTRL_FLASH_IO;
149
150         /* Assert the CLE (Command Latch Enable) line to the flash chip */
151         WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
152         DoC_Delay(doc, 4);      /* Software requirement 11.4.3 for Millennium */
153
154         if (DoC_is_Millennium(doc))
155                 WriteDOC(command, docptr, CDSNSlowIO);
156
157         /* Send the command */
158         WriteDOC_(command, docptr, doc->ioreg);
159         if (DoC_is_Millennium(doc))
160                 WriteDOC(command, docptr, WritePipeTerm);
161
162         /* Lower the CLE line */
163         WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
164         DoC_Delay(doc, 4);      /* Software requirement 11.4.3 for Millennium */
165
166         /* Wait for the chip to respond - Software requirement 11.4.1 (extended for any command) */
167         return DoC_WaitReady(doc);
168 }
169
170 /* DoC_Address: Set the current address for the flash chip through the CDSN Slow IO register to
171    bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
172    required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
173
174 static int DoC_Address(struct DiskOnChip *doc, int numbytes, unsigned long ofs,
175                        unsigned char xtraflags1, unsigned char xtraflags2)
176 {
177         int i;
178         void __iomem *docptr = doc->virtadr;
179
180         if (DoC_is_2000(doc))
181                 xtraflags1 |= CDSN_CTRL_FLASH_IO;
182
183         /* Assert the ALE (Address Latch Enable) line to the flash chip */
184         WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
185
186         DoC_Delay(doc, 4);      /* Software requirement 11.4.3 for Millennium */
187
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).
198          */
199
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);
204         }
205
206         if (doc->page256) {
207                 ofs = ofs >> 8;
208         } else {
209                 ofs = ofs >> 9;
210         }
211
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);
217                 }
218         }
219
220         if (DoC_is_Millennium(doc))
221                 WriteDOC(ofs & 0xff, docptr, WritePipeTerm);
222
223         DoC_Delay(doc, 2);      /* Needed for some slow flash chips. mf. */
224
225         /* FIXME: The SlowIO's for millennium could be replaced by
226            a single WritePipeTerm here. mf. */
227
228         /* Lower the ALE line */
229         WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr,
230                  CDSNControl);
231
232         DoC_Delay(doc, 4);      /* Software requirement 11.4.3 for Millennium */
233
234         /* Wait for the chip to respond - Software requirement 11.4.1 */
235         return DoC_WaitReady(doc);
236 }
237
238 /* Read a buffer from DoC, taking care of Millennium odditys */
239 static void DoC_ReadBuf(struct DiskOnChip *doc, u_char * buf, int len)
240 {
241         volatile int dummy;
242         int modulus = 0xffff;
243         void __iomem *docptr = doc->virtadr;
244         int i;
245
246         if (len <= 0)
247                 return;
248
249         if (DoC_is_Millennium(doc)) {
250                 /* Read the data via the internal pipeline through CDSN IO register,
251                    see Pipelined Read Operations 11.3 */
252                 dummy = ReadDOC(docptr, ReadPipeInit);
253
254                 /* Millennium should use the LastDataRead register - Pipeline Reads */
255                 len--;
256
257                 /* This is needed for correctly ECC calculation */
258                 modulus = 0xff;
259         }
260
261         for (i = 0; i < len; i++)
262                 buf[i] = ReadDOC_(docptr, doc->ioreg + (i & modulus));
263
264         if (DoC_is_Millennium(doc)) {
265                 buf[i] = ReadDOC(docptr, LastDataRead);
266         }
267 }
268
269 /* Write a buffer to DoC, taking care of Millennium odditys */
270 static void DoC_WriteBuf(struct DiskOnChip *doc, const u_char * buf, int len)
271 {
272         void __iomem *docptr = doc->virtadr;
273         int i;
274
275         if (len <= 0)
276                 return;
277
278         for (i = 0; i < len; i++)
279                 WriteDOC_(buf[i], docptr, doc->ioreg + i);
280
281         if (DoC_is_Millennium(doc)) {
282                 WriteDOC(0x00, docptr, WritePipeTerm);
283         }
284 }
285
286
287 /* DoC_SelectChip: Select a given flash chip within the current floor */
288
289 static inline int DoC_SelectChip(struct DiskOnChip *doc, int chip)
290 {
291         void __iomem *docptr = doc->virtadr;
292
293         /* Software requirement 11.4.4 before writing DeviceSelect */
294         /* Deassert the CE line to eliminate glitches on the FCE# outputs */
295         WriteDOC(CDSN_CTRL_WP, docptr, CDSNControl);
296         DoC_Delay(doc, 4);      /* Software requirement 11.4.3 for Millennium */
297
298         /* Select the individual flash chip requested */
299         WriteDOC(chip, docptr, CDSNDeviceSelect);
300         DoC_Delay(doc, 4);
301
302         /* Reassert the CE line */
303         WriteDOC(CDSN_CTRL_CE | CDSN_CTRL_FLASH_IO | CDSN_CTRL_WP, docptr,
304                  CDSNControl);
305         DoC_Delay(doc, 4);      /* Software requirement 11.4.3 for Millennium */
306
307         /* Wait for it to be ready */
308         return DoC_WaitReady(doc);
309 }
310
311 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
312
313 static inline int DoC_SelectFloor(struct DiskOnChip *doc, int floor)
314 {
315         void __iomem *docptr = doc->virtadr;
316
317         /* Select the floor (bank) of chips required */
318         WriteDOC(floor, docptr, FloorSelect);
319
320         /* Wait for the chip to be ready */
321         return DoC_WaitReady(doc);
322 }
323
324 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
325
326 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
327 {
328         int mfr, id, i, j;
329         volatile char dummy;
330
331         /* Page in the required floor/chip */
332         DoC_SelectFloor(doc, floor);
333         DoC_SelectChip(doc, chip);
334
335         /* Reset the chip */
336         if (DoC_Command(doc, NAND_CMD_RESET, CDSN_CTRL_WP)) {
337                 DEBUG(MTD_DEBUG_LEVEL2,
338                       "DoC_Command (reset) for %d,%d returned true\n",
339                       floor, chip);
340                 return 0;
341         }
342
343
344         /* Read the NAND chip ID: 1. Send ReadID command */
345         if (DoC_Command(doc, NAND_CMD_READID, CDSN_CTRL_WP)) {
346                 DEBUG(MTD_DEBUG_LEVEL2,
347                       "DoC_Command (ReadID) for %d,%d returned true\n",
348                       floor, chip);
349                 return 0;
350         }
351
352         /* Read the NAND chip ID: 2. Send address byte zero */
353         DoC_Address(doc, ADDR_COLUMN, 0, CDSN_CTRL_WP, 0);
354
355         /* Read the manufacturer and device id codes from the device */
356
357         if (DoC_is_Millennium(doc)) {
358                 DoC_Delay(doc, 2);
359                 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
360                 mfr = ReadDOC(doc->virtadr, LastDataRead);
361
362                 DoC_Delay(doc, 2);
363                 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
364                 id = ReadDOC(doc->virtadr, LastDataRead);
365         } else {
366                 /* CDSN Slow IO register see Software Req 11.4 item 5. */
367                 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
368                 DoC_Delay(doc, 2);
369                 mfr = ReadDOC_(doc->virtadr, doc->ioreg);
370
371                 /* CDSN Slow IO register see Software Req 11.4 item 5. */
372                 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
373                 DoC_Delay(doc, 2);
374                 id = ReadDOC_(doc->virtadr, doc->ioreg);
375         }
376
377         /* No response - return failure */
378         if (mfr == 0xff || mfr == 0)
379                 return 0;
380
381         /* Check it's the same as the first chip we identified.
382          * M-Systems say that any given DiskOnChip device should only
383          * contain _one_ type of flash part, although that's not a
384          * hardware restriction. */
385         if (doc->mfr) {
386                 if (doc->mfr == mfr && doc->id == id)
387                         return 1;       /* This is another the same the first */
388                 else
389                         printk(KERN_WARNING
390                                "Flash chip at floor %d, chip %d is different:\n",
391                                floor, chip);
392         }
393
394         /* Print and store the manufacturer and ID codes. */
395         for (i = 0; nand_flash_ids[i].name != NULL; i++) {
396                 if (id == nand_flash_ids[i].id) {
397                         /* Try to identify manufacturer */
398                         for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
399                                 if (nand_manuf_ids[j].id == mfr)
400                                         break;
401                         }
402                         printk(KERN_INFO
403                                "Flash chip found: Manufacturer ID: %2.2X, "
404                                "Chip ID: %2.2X (%s:%s)\n", mfr, id,
405                                nand_manuf_ids[j].name, nand_flash_ids[i].name);
406                         if (!doc->mfr) {
407                                 doc->mfr = mfr;
408                                 doc->id = id;
409                                 doc->chipshift =
410                                         ffs((nand_flash_ids[i].chipsize << 20)) - 1;
411                                 doc->page256 = (nand_flash_ids[i].pagesize == 256) ? 1 : 0;
412                                 doc->pageadrlen = doc->chipshift > 25 ? 3 : 2;
413                                 doc->erasesize =
414                                     nand_flash_ids[i].erasesize;
415                                 return 1;
416                         }
417                         return 0;
418                 }
419         }
420
421
422         /* We haven't fully identified the chip. Print as much as we know. */
423         printk(KERN_WARNING "Unknown flash chip found: %2.2X %2.2X\n",
424                id, mfr);
425
426         printk(KERN_WARNING "Please report to dwmw2@infradead.org\n");
427         return 0;
428 }
429
430 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
431
432 static void DoC_ScanChips(struct DiskOnChip *this, int maxchips)
433 {
434         int floor, chip;
435         int numchips[MAX_FLOORS];
436         int ret = 1;
437
438         this->numchips = 0;
439         this->mfr = 0;
440         this->id = 0;
441
442         /* For each floor, find the number of valid chips it contains */
443         for (floor = 0; floor < MAX_FLOORS; floor++) {
444                 ret = 1;
445                 numchips[floor] = 0;
446                 for (chip = 0; chip < maxchips && ret != 0; chip++) {
447
448                         ret = DoC_IdentChip(this, floor, chip);
449                         if (ret) {
450                                 numchips[floor]++;
451                                 this->numchips++;
452                         }
453                 }
454         }
455
456         /* If there are none at all that we recognise, bail */
457         if (!this->numchips) {
458                 printk(KERN_NOTICE "No flash chips recognised.\n");
459                 return;
460         }
461
462         /* Allocate an array to hold the information for each chip */
463         this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
464         if (!this->chips) {
465                 printk(KERN_NOTICE "No memory for allocating chip info structures\n");
466                 return;
467         }
468
469         ret = 0;
470
471         /* Fill out the chip array with {floor, chipno} for each
472          * detected chip in the device. */
473         for (floor = 0; floor < MAX_FLOORS; floor++) {
474                 for (chip = 0; chip < numchips[floor]; chip++) {
475                         this->chips[ret].floor = floor;
476                         this->chips[ret].chip = chip;
477                         this->chips[ret].curadr = 0;
478                         this->chips[ret].curmode = 0x50;
479                         ret++;
480                 }
481         }
482
483         /* Calculate and print the total size of the device */
484         this->totlen = this->numchips * (1 << this->chipshift);
485
486         printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
487                this->numchips, this->totlen >> 20);
488 }
489
490 static int DoC2k_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
491 {
492         int tmp1, tmp2, retval;
493         if (doc1->physadr == doc2->physadr)
494                 return 1;
495
496         /* Use the alias resolution register which was set aside for this
497          * purpose. If it's value is the same on both chips, they might
498          * be the same chip, and we write to one and check for a change in
499          * the other. It's unclear if this register is usuable in the
500          * DoC 2000 (it's in the Millennium docs), but it seems to work. */
501         tmp1 = ReadDOC(doc1->virtadr, AliasResolution);
502         tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
503         if (tmp1 != tmp2)
504                 return 0;
505
506         WriteDOC((tmp1 + 1) % 0xff, doc1->virtadr, AliasResolution);
507         tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
508         if (tmp2 == (tmp1 + 1) % 0xff)
509                 retval = 1;
510         else
511                 retval = 0;
512
513         /* Restore register contents.  May not be necessary, but do it just to
514          * be safe. */
515         WriteDOC(tmp1, doc1->virtadr, AliasResolution);
516
517         return retval;
518 }
519
520 /* This routine is found from the docprobe code by symbol_get(),
521  * which will bump the use count of this module. */
522 void DoC2k_init(struct mtd_info *mtd)
523 {
524         struct DiskOnChip *this = mtd->priv;
525         struct DiskOnChip *old = NULL;
526         int maxchips;
527
528         /* We must avoid being called twice for the same device. */
529
530         if (doc2klist)
531                 old = doc2klist->priv;
532
533         while (old) {
534                 if (DoC2k_is_alias(old, this)) {
535                         printk(KERN_NOTICE
536                                "Ignoring DiskOnChip 2000 at 0x%lX - already configured\n",
537                                this->physadr);
538                         iounmap(this->virtadr);
539                         kfree(mtd);
540                         return;
541                 }
542                 if (old->nextdoc)
543                         old = old->nextdoc->priv;
544                 else
545                         old = NULL;
546         }
547
548
549         switch (this->ChipID) {
550         case DOC_ChipID_Doc2kTSOP:
551                 mtd->name = "DiskOnChip 2000 TSOP";
552                 this->ioreg = DoC_Mil_CDSN_IO;
553                 /* Pretend it's a Millennium */
554                 this->ChipID = DOC_ChipID_DocMil;
555                 maxchips = MAX_CHIPS;
556                 break;
557         case DOC_ChipID_Doc2k:
558                 mtd->name = "DiskOnChip 2000";
559                 this->ioreg = DoC_2k_CDSN_IO;
560                 maxchips = MAX_CHIPS;
561                 break;
562         case DOC_ChipID_DocMil:
563                 mtd->name = "DiskOnChip Millennium";
564                 this->ioreg = DoC_Mil_CDSN_IO;
565                 maxchips = MAX_CHIPS_MIL;
566                 break;
567         default:
568                 printk("Unknown ChipID 0x%02x\n", this->ChipID);
569                 kfree(mtd);
570                 iounmap(this->virtadr);
571                 return;
572         }
573
574         printk(KERN_NOTICE "%s found at address 0x%lX\n", mtd->name,
575                this->physadr);
576
577         mtd->type = MTD_NANDFLASH;
578         mtd->flags = MTD_CAP_NANDFLASH;
579         mtd->ecctype = MTD_ECC_RS_DiskOnChip;
580         mtd->size = 0;
581         mtd->erasesize = 0;
582         mtd->oobblock = 512;
583         mtd->oobsize = 16;
584         mtd->owner = THIS_MODULE;
585         mtd->erase = doc_erase;
586         mtd->point = NULL;
587         mtd->unpoint = NULL;
588         mtd->read = doc_read;
589         mtd->write = doc_write;
590         mtd->read_ecc = doc_read_ecc;
591         mtd->write_ecc = doc_write_ecc;
592         mtd->writev_ecc = doc_writev_ecc;
593         mtd->read_oob = doc_read_oob;
594         mtd->write_oob = doc_write_oob;
595         mtd->sync = NULL;
596
597         this->totlen = 0;
598         this->numchips = 0;
599
600         this->curfloor = -1;
601         this->curchip = -1;
602         mutex_init(&this->lock);
603
604         /* Ident all the chips present. */
605         DoC_ScanChips(this, maxchips);
606
607         if (!this->totlen) {
608                 kfree(mtd);
609                 iounmap(this->virtadr);
610         } else {
611                 this->nextdoc = doc2klist;
612                 doc2klist = mtd;
613                 mtd->size = this->totlen;
614                 mtd->erasesize = this->erasesize;
615                 add_mtd_device(mtd);
616                 return;
617         }
618 }
619 EXPORT_SYMBOL_GPL(DoC2k_init);
620
621 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
622                     size_t * retlen, u_char * buf)
623 {
624         /* Just a special case of doc_read_ecc */
625         return doc_read_ecc(mtd, from, len, retlen, buf, NULL, NULL);
626 }
627
628 static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
629                         size_t * retlen, u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel)
630 {
631         struct DiskOnChip *this = mtd->priv;
632         void __iomem *docptr = this->virtadr;
633         struct Nand *mychip;
634         unsigned char syndrome[6];
635         volatile char dummy;
636         int i, len256 = 0, ret=0;
637         size_t left = len;
638
639         /* Don't allow read past end of device */
640         if (from >= this->totlen)
641                 return -EINVAL;
642
643         mutex_lock(&this->lock);
644
645         *retlen = 0;
646         while (left) {
647                 len = left;
648
649                 /* Don't allow a single read to cross a 512-byte block boundary */
650                 if (from + len > ((from | 0x1ff) + 1))
651                         len = ((from | 0x1ff) + 1) - from;
652
653                 /* The ECC will not be calculated correctly if less than 512 is read */
654                 if (len != 0x200 && eccbuf)
655                         printk(KERN_WARNING
656                                "ECC needs a full sector read (adr: %lx size %lx)\n",
657                                (long) from, (long) len);
658
659                 /* printk("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len); */
660
661
662                 /* Find the chip which is to be used and select it */
663                 mychip = &this->chips[from >> (this->chipshift)];
664
665                 if (this->curfloor != mychip->floor) {
666                         DoC_SelectFloor(this, mychip->floor);
667                         DoC_SelectChip(this, mychip->chip);
668                 } else if (this->curchip != mychip->chip) {
669                         DoC_SelectChip(this, mychip->chip);
670                 }
671
672                 this->curfloor = mychip->floor;
673                 this->curchip = mychip->chip;
674
675                 DoC_Command(this,
676                             (!this->page256
677                              && (from & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
678                             CDSN_CTRL_WP);
679                 DoC_Address(this, ADDR_COLUMN_PAGE, from, CDSN_CTRL_WP,
680                             CDSN_CTRL_ECC_IO);
681
682                 if (eccbuf) {
683                         /* Prime the ECC engine */
684                         WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
685                         WriteDOC(DOC_ECC_EN, docptr, ECCConf);
686                 } else {
687                         /* disable the ECC engine */
688                         WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
689                         WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
690                 }
691
692                 /* treat crossing 256-byte sector for 2M x 8bits devices */
693                 if (this->page256 && from + len > (from | 0xff) + 1) {
694                         len256 = (from | 0xff) + 1 - from;
695                         DoC_ReadBuf(this, buf, len256);
696
697                         DoC_Command(this, NAND_CMD_READ0, CDSN_CTRL_WP);
698                         DoC_Address(this, ADDR_COLUMN_PAGE, from + len256,
699                                     CDSN_CTRL_WP, CDSN_CTRL_ECC_IO);
700                 }
701
702                 DoC_ReadBuf(this, &buf[len256], len - len256);
703
704                 /* Let the caller know we completed it */
705                 *retlen += len;
706
707                 if (eccbuf) {
708                         /* Read the ECC data through the DiskOnChip ECC logic */
709                         /* Note: this will work even with 2M x 8bit devices as   */
710                         /*       they have 8 bytes of OOB per 256 page. mf.      */
711                         DoC_ReadBuf(this, eccbuf, 6);
712
713                         /* Flush the pipeline */
714                         if (DoC_is_Millennium(this)) {
715                                 dummy = ReadDOC(docptr, ECCConf);
716                                 dummy = ReadDOC(docptr, ECCConf);
717                                 i = ReadDOC(docptr, ECCConf);
718                         } else {
719                                 dummy = ReadDOC(docptr, 2k_ECCStatus);
720                                 dummy = ReadDOC(docptr, 2k_ECCStatus);
721                                 i = ReadDOC(docptr, 2k_ECCStatus);
722                         }
723
724                         /* Check the ECC Status */
725                         if (i & 0x80) {
726                                 int nb_errors;
727                                 /* There was an ECC error */
728 #ifdef ECC_DEBUG
729                                 printk(KERN_ERR "DiskOnChip ECC Error: Read at %lx\n", (long)from);
730 #endif
731                                 /* Read the ECC syndrom through the DiskOnChip ECC logic.
732                                    These syndrome will be all ZERO when there is no error */
733                                 for (i = 0; i < 6; i++) {
734                                         syndrome[i] =
735                                             ReadDOC(docptr, ECCSyndrome0 + i);
736                                 }
737                                 nb_errors = doc_decode_ecc(buf, syndrome);
738
739 #ifdef ECC_DEBUG
740                                 printk(KERN_ERR "Errors corrected: %x\n", nb_errors);
741 #endif
742                                 if (nb_errors < 0) {
743                                         /* We return error, but have actually done the read. Not that
744                                            this can be told to user-space, via sys_read(), but at least
745                                            MTD-aware stuff can know about it by checking *retlen */
746                                         ret = -EIO;
747                                 }
748                         }
749
750 #ifdef PSYCHO_DEBUG
751                         printk(KERN_DEBUG "ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
752                                      (long)from, eccbuf[0], eccbuf[1], eccbuf[2],
753                                      eccbuf[3], eccbuf[4], eccbuf[5]);
754 #endif
755
756                         /* disable the ECC engine */
757                         WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
758                 }
759
760                 /* according to 11.4.1, we need to wait for the busy line
761                  * drop if we read to the end of the page.  */
762                 if(0 == ((from + len) & 0x1ff))
763                 {
764                     DoC_WaitReady(this);
765                 }
766
767                 from += len;
768                 left -= len;
769                 buf += len;
770         }
771
772         mutex_unlock(&this->lock);
773
774         return ret;
775 }
776
777 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
778                      size_t * retlen, const u_char * buf)
779 {
780         char eccbuf[6];
781         return doc_write_ecc(mtd, to, len, retlen, buf, eccbuf, NULL);
782 }
783
784 static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
785                          size_t * retlen, const u_char * buf,
786                          u_char * eccbuf, struct nand_oobinfo *oobsel)
787 {
788         struct DiskOnChip *this = mtd->priv;
789         int di; /* Yes, DI is a hangover from when I was disassembling the binary driver */
790         void __iomem *docptr = this->virtadr;
791         volatile char dummy;
792         int len256 = 0;
793         struct Nand *mychip;
794         size_t left = len;
795         int status;
796
797         /* Don't allow write past end of device */
798         if (to >= this->totlen)
799                 return -EINVAL;
800
801         mutex_lock(&this->lock);
802
803         *retlen = 0;
804         while (left) {
805                 len = left;
806
807                 /* Don't allow a single write to cross a 512-byte block boundary */
808                 if (to + len > ((to | 0x1ff) + 1))
809                         len = ((to | 0x1ff) + 1) - to;
810
811                 /* The ECC will not be calculated correctly if less than 512 is written */
812 /* DBB-
813                 if (len != 0x200 && eccbuf)
814                         printk(KERN_WARNING
815                                "ECC needs a full sector write (adr: %lx size %lx)\n",
816                                (long) to, (long) len);
817    -DBB */
818
819                 /* printk("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */
820
821                 /* Find the chip which is to be used and select it */
822                 mychip = &this->chips[to >> (this->chipshift)];
823
824                 if (this->curfloor != mychip->floor) {
825                         DoC_SelectFloor(this, mychip->floor);
826                         DoC_SelectChip(this, mychip->chip);
827                 } else if (this->curchip != mychip->chip) {
828                         DoC_SelectChip(this, mychip->chip);
829                 }
830
831                 this->curfloor = mychip->floor;
832                 this->curchip = mychip->chip;
833
834                 /* Set device to main plane of flash */
835                 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
836                 DoC_Command(this,
837                             (!this->page256
838                              && (to & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
839                             CDSN_CTRL_WP);
840
841                 DoC_Command(this, NAND_CMD_SEQIN, 0);
842                 DoC_Address(this, ADDR_COLUMN_PAGE, to, 0, CDSN_CTRL_ECC_IO);
843
844                 if (eccbuf) {
845                         /* Prime the ECC engine */
846                         WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
847                         WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
848                 } else {
849                         /* disable the ECC engine */
850                         WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
851                         WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
852                 }
853
854                 /* treat crossing 256-byte sector for 2M x 8bits devices */
855                 if (this->page256 && to + len > (to | 0xff) + 1) {
856                         len256 = (to | 0xff) + 1 - to;
857                         DoC_WriteBuf(this, buf, len256);
858
859                         DoC_Command(this, NAND_CMD_PAGEPROG, 0);
860
861                         DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
862                         /* There's an implicit DoC_WaitReady() in DoC_Command */
863
864                         dummy = ReadDOC(docptr, CDSNSlowIO);
865                         DoC_Delay(this, 2);
866
867                         if (ReadDOC_(docptr, this->ioreg) & 1) {
868                                 printk(KERN_ERR "Error programming flash\n");
869                                 /* Error in programming */
870                                 *retlen = 0;
871                                 mutex_unlock(&this->lock);
872                                 return -EIO;
873                         }
874
875                         DoC_Command(this, NAND_CMD_SEQIN, 0);
876                         DoC_Address(this, ADDR_COLUMN_PAGE, to + len256, 0,
877                                     CDSN_CTRL_ECC_IO);
878                 }
879
880                 DoC_WriteBuf(this, &buf[len256], len - len256);
881
882                 if (eccbuf) {
883                         WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_CE, docptr,
884                                  CDSNControl);
885
886                         if (DoC_is_Millennium(this)) {
887                                 WriteDOC(0, docptr, NOP);
888                                 WriteDOC(0, docptr, NOP);
889                                 WriteDOC(0, docptr, NOP);
890                         } else {
891                                 WriteDOC_(0, docptr, this->ioreg);
892                                 WriteDOC_(0, docptr, this->ioreg);
893                                 WriteDOC_(0, docptr, this->ioreg);
894                         }
895
896                         WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_FLASH_IO | CDSN_CTRL_CE, docptr,
897                                  CDSNControl);
898
899                         /* Read the ECC data through the DiskOnChip ECC logic */
900                         for (di = 0; di < 6; di++) {
901                                 eccbuf[di] = ReadDOC(docptr, ECCSyndrome0 + di);
902                         }
903
904                         /* Reset the ECC engine */
905                         WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
906
907 #ifdef PSYCHO_DEBUG
908                         printk
909                             ("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
910                              (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
911                              eccbuf[4], eccbuf[5]);
912 #endif
913                 }
914
915                 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
916
917                 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
918                 /* There's an implicit DoC_WaitReady() in DoC_Command */
919
920                 if (DoC_is_Millennium(this)) {
921                         ReadDOC(docptr, ReadPipeInit);
922                         status = ReadDOC(docptr, LastDataRead);
923                 } else {
924                         dummy = ReadDOC(docptr, CDSNSlowIO);
925                         DoC_Delay(this, 2);
926                         status = ReadDOC_(docptr, this->ioreg);
927                 }
928
929                 if (status & 1) {
930                         printk(KERN_ERR "Error programming flash\n");
931                         /* Error in programming */
932                         *retlen = 0;
933                         mutex_unlock(&this->lock);
934                         return -EIO;
935                 }
936
937                 /* Let the caller know we completed it */
938                 *retlen += len;
939
940                 if (eccbuf) {
941                         unsigned char x[8];
942                         size_t dummy;
943                         int ret;
944
945                         /* Write the ECC data to flash */
946                         for (di=0; di<6; di++)
947                                 x[di] = eccbuf[di];
948
949                         x[6]=0x55;
950                         x[7]=0x55;
951
952                         ret = doc_write_oob_nolock(mtd, to, 8, &dummy, x);
953                         if (ret) {
954                                 mutex_unlock(&this->lock);
955                                 return ret;
956                         }
957                 }
958
959                 to += len;
960                 left -= len;
961                 buf += len;
962         }
963
964         mutex_unlock(&this->lock);
965         return 0;
966 }
967
968 static int doc_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs,
969                           unsigned long count, loff_t to, size_t *retlen,
970                           u_char *eccbuf, struct nand_oobinfo *oobsel)
971 {
972         static char static_buf[512];
973         static DEFINE_MUTEX(writev_buf_mutex);
974
975         size_t totretlen = 0;
976         size_t thisvecofs = 0;
977         int ret= 0;
978
979         mutex_lock(&writev_buf_mutex);
980
981         while(count) {
982                 size_t thislen, thisretlen;
983                 unsigned char *buf;
984
985                 buf = vecs->iov_base + thisvecofs;
986                 thislen = vecs->iov_len - thisvecofs;
987
988
989                 if (thislen >= 512) {
990                         thislen = thislen & ~(512-1);
991                         thisvecofs += thislen;
992                 } else {
993                         /* Not enough to fill a page. Copy into buf */
994                         memcpy(static_buf, buf, thislen);
995                         buf = &static_buf[thislen];
996
997                         while(count && thislen < 512) {
998                                 vecs++;
999                                 count--;
1000                                 thisvecofs = min((512-thislen), vecs->iov_len);
1001                                 memcpy(buf, vecs->iov_base, thisvecofs);
1002                                 thislen += thisvecofs;
1003                                 buf += thisvecofs;
1004                         }
1005                         buf = static_buf;
1006                 }
1007                 if (count && thisvecofs == vecs->iov_len) {
1008                         thisvecofs = 0;
1009                         vecs++;
1010                         count--;
1011                 }
1012                 ret = doc_write_ecc(mtd, to, thislen, &thisretlen, buf, eccbuf, oobsel);
1013
1014                 totretlen += thisretlen;
1015
1016                 if (ret || thisretlen != thislen)
1017                         break;
1018
1019                 to += thislen;
1020         }
1021
1022         mutex_unlock(&writev_buf_mutex);
1023         *retlen = totretlen;
1024         return ret;
1025 }
1026
1027
1028 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
1029                         size_t * retlen, u_char * buf)
1030 {
1031         struct DiskOnChip *this = mtd->priv;
1032         int len256 = 0, ret;
1033         struct Nand *mychip;
1034
1035         mutex_lock(&this->lock);
1036
1037         mychip = &this->chips[ofs >> this->chipshift];
1038
1039         if (this->curfloor != mychip->floor) {
1040                 DoC_SelectFloor(this, mychip->floor);
1041                 DoC_SelectChip(this, mychip->chip);
1042         } else if (this->curchip != mychip->chip) {
1043                 DoC_SelectChip(this, mychip->chip);
1044         }
1045         this->curfloor = mychip->floor;
1046         this->curchip = mychip->chip;
1047
1048         /* update address for 2M x 8bit devices. OOB starts on the second */
1049         /* page to maintain compatibility with doc_read_ecc. */
1050         if (this->page256) {
1051                 if (!(ofs & 0x8))
1052                         ofs += 0x100;
1053                 else
1054                         ofs -= 0x8;
1055         }
1056
1057         DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1058         DoC_Address(this, ADDR_COLUMN_PAGE, ofs, CDSN_CTRL_WP, 0);
1059
1060         /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1061         /* Note: datasheet says it should automaticaly wrap to the */
1062         /*       next OOB block, but it didn't work here. mf.      */
1063         if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1064                 len256 = (ofs | 0x7) + 1 - ofs;
1065                 DoC_ReadBuf(this, buf, len256);
1066
1067                 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1068                 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff),
1069                             CDSN_CTRL_WP, 0);
1070         }
1071
1072         DoC_ReadBuf(this, &buf[len256], len - len256);
1073
1074         *retlen = len;
1075         /* Reading the full OOB data drops us off of the end of the page,
1076          * causing the flash device to go into busy mode, so we need
1077          * to wait until ready 11.4.1 and Toshiba TC58256FT docs */
1078
1079         ret = DoC_WaitReady(this);
1080
1081         mutex_unlock(&this->lock);
1082         return ret;
1083
1084 }
1085
1086 static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
1087                                 size_t * retlen, const u_char * buf)
1088 {
1089         struct DiskOnChip *this = mtd->priv;
1090         int len256 = 0;
1091         void __iomem *docptr = this->virtadr;
1092         struct Nand *mychip = &this->chips[ofs >> this->chipshift];
1093         volatile int dummy;
1094         int status;
1095
1096         //      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,
1097         //   buf[0], buf[1], buf[2], buf[3], buf[8], buf[9], buf[14],buf[15]);
1098
1099         /* Find the chip which is to be used and select it */
1100         if (this->curfloor != mychip->floor) {
1101                 DoC_SelectFloor(this, mychip->floor);
1102                 DoC_SelectChip(this, mychip->chip);
1103         } else if (this->curchip != mychip->chip) {
1104                 DoC_SelectChip(this, mychip->chip);
1105         }
1106         this->curfloor = mychip->floor;
1107         this->curchip = mychip->chip;
1108
1109         /* disable the ECC engine */
1110         WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
1111         WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
1112
1113         /* Reset the chip, see Software Requirement 11.4 item 1. */
1114         DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
1115
1116         /* issue the Read2 command to set the pointer to the Spare Data Area. */
1117         DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1118
1119         /* update address for 2M x 8bit devices. OOB starts on the second */
1120         /* page to maintain compatibility with doc_read_ecc. */
1121         if (this->page256) {
1122                 if (!(ofs & 0x8))
1123                         ofs += 0x100;
1124                 else
1125                         ofs -= 0x8;
1126         }
1127
1128         /* issue the Serial Data In command to initial the Page Program process */
1129         DoC_Command(this, NAND_CMD_SEQIN, 0);
1130         DoC_Address(this, ADDR_COLUMN_PAGE, ofs, 0, 0);
1131
1132         /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1133         /* Note: datasheet says it should automaticaly wrap to the */
1134         /*       next OOB block, but it didn't work here. mf.      */
1135         if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1136                 len256 = (ofs | 0x7) + 1 - ofs;
1137                 DoC_WriteBuf(this, buf, len256);
1138
1139                 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1140                 DoC_Command(this, NAND_CMD_STATUS, 0);
1141                 /* DoC_WaitReady() is implicit in DoC_Command */
1142
1143                 if (DoC_is_Millennium(this)) {
1144                         ReadDOC(docptr, ReadPipeInit);
1145                         status = ReadDOC(docptr, LastDataRead);
1146                 } else {
1147                         dummy = ReadDOC(docptr, CDSNSlowIO);
1148                         DoC_Delay(this, 2);
1149                         status = ReadDOC_(docptr, this->ioreg);
1150                 }
1151
1152                 if (status & 1) {
1153                         printk(KERN_ERR "Error programming oob data\n");
1154                         /* There was an error */
1155                         *retlen = 0;
1156                         return -EIO;
1157                 }
1158                 DoC_Command(this, NAND_CMD_SEQIN, 0);
1159                 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), 0, 0);
1160         }
1161
1162         DoC_WriteBuf(this, &buf[len256], len - len256);
1163
1164         DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1165         DoC_Command(this, NAND_CMD_STATUS, 0);
1166         /* DoC_WaitReady() is implicit in DoC_Command */
1167
1168         if (DoC_is_Millennium(this)) {
1169                 ReadDOC(docptr, ReadPipeInit);
1170                 status = ReadDOC(docptr, LastDataRead);
1171         } else {
1172                 dummy = ReadDOC(docptr, CDSNSlowIO);
1173                 DoC_Delay(this, 2);
1174                 status = ReadDOC_(docptr, this->ioreg);
1175         }
1176
1177         if (status & 1) {
1178                 printk(KERN_ERR "Error programming oob data\n");
1179                 /* There was an error */
1180                 *retlen = 0;
1181                 return -EIO;
1182         }
1183
1184         *retlen = len;
1185         return 0;
1186
1187 }
1188
1189 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
1190                          size_t * retlen, const u_char * buf)
1191 {
1192         struct DiskOnChip *this = mtd->priv;
1193         int ret;
1194
1195         mutex_lock(&this->lock);
1196         ret = doc_write_oob_nolock(mtd, ofs, len, retlen, buf);
1197
1198         mutex_unlock(&this->lock);
1199         return ret;
1200 }
1201
1202 static int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
1203 {
1204         struct DiskOnChip *this = mtd->priv;
1205         __u32 ofs = instr->addr;
1206         __u32 len = instr->len;
1207         volatile int dummy;
1208         void __iomem *docptr = this->virtadr;
1209         struct Nand *mychip;
1210         int status;
1211
1212         mutex_lock(&this->lock);
1213
1214         if (ofs & (mtd->erasesize-1) || len & (mtd->erasesize-1)) {
1215                 mutex_unlock(&this->lock);
1216                 return -EINVAL;
1217         }
1218
1219         instr->state = MTD_ERASING;
1220
1221         /* FIXME: Do this in the background. Use timers or schedule_task() */
1222         while(len) {
1223                 mychip = &this->chips[ofs >> this->chipshift];
1224
1225                 if (this->curfloor != mychip->floor) {
1226                         DoC_SelectFloor(this, mychip->floor);
1227                         DoC_SelectChip(this, mychip->chip);
1228                 } else if (this->curchip != mychip->chip) {
1229                         DoC_SelectChip(this, mychip->chip);
1230                 }
1231                 this->curfloor = mychip->floor;
1232                 this->curchip = mychip->chip;
1233
1234                 DoC_Command(this, NAND_CMD_ERASE1, 0);
1235                 DoC_Address(this, ADDR_PAGE, ofs, 0, 0);
1236                 DoC_Command(this, NAND_CMD_ERASE2, 0);
1237
1238                 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
1239
1240                 if (DoC_is_Millennium(this)) {
1241                         ReadDOC(docptr, ReadPipeInit);
1242                         status = ReadDOC(docptr, LastDataRead);
1243                 } else {
1244                         dummy = ReadDOC(docptr, CDSNSlowIO);
1245                         DoC_Delay(this, 2);
1246                         status = ReadDOC_(docptr, this->ioreg);
1247                 }
1248
1249                 if (status & 1) {
1250                         printk(KERN_ERR "Error erasing at 0x%x\n", ofs);
1251                         /* There was an error */
1252                         instr->state = MTD_ERASE_FAILED;
1253                         goto callback;
1254                 }
1255                 ofs += mtd->erasesize;
1256                 len -= mtd->erasesize;
1257         }
1258         instr->state = MTD_ERASE_DONE;
1259
1260  callback:
1261         mtd_erase_callback(instr);
1262
1263         mutex_unlock(&this->lock);
1264         return 0;
1265 }
1266
1267
1268 /****************************************************************************
1269  *
1270  * Module stuff
1271  *
1272  ****************************************************************************/
1273
1274 static void __exit cleanup_doc2000(void)
1275 {
1276         struct mtd_info *mtd;
1277         struct DiskOnChip *this;
1278
1279         while ((mtd = doc2klist)) {
1280                 this = mtd->priv;
1281                 doc2klist = this->nextdoc;
1282
1283                 del_mtd_device(mtd);
1284
1285                 iounmap(this->virtadr);
1286                 kfree(this->chips);
1287                 kfree(mtd);
1288         }
1289 }
1290
1291 module_exit(cleanup_doc2000);
1292
1293 MODULE_LICENSE("GPL");
1294 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
1295 MODULE_DESCRIPTION("MTD driver for DiskOnChip 2000 and Millennium");
1296