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