Merge branch 'devel' of master.kernel.org:/home/rmk/linux-2.6-serial
[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_read_oob(struct mtd_info *mtd, loff_t ofs,
63                         struct mtd_oob_ops *ops);
64 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
65                          struct mtd_oob_ops *ops);
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_oob = doc_read_oob;
588         mtd->write_oob = doc_write_oob;
589         mtd->sync = NULL;
590
591         this->totlen = 0;
592         this->numchips = 0;
593
594         this->curfloor = -1;
595         this->curchip = -1;
596         mutex_init(&this->lock);
597
598         /* Ident all the chips present. */
599         DoC_ScanChips(this, maxchips);
600
601         if (!this->totlen) {
602                 kfree(mtd);
603                 iounmap(this->virtadr);
604         } else {
605                 this->nextdoc = doc2klist;
606                 doc2klist = mtd;
607                 mtd->size = this->totlen;
608                 mtd->erasesize = this->erasesize;
609                 add_mtd_device(mtd);
610                 return;
611         }
612 }
613 EXPORT_SYMBOL_GPL(DoC2k_init);
614
615 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
616                     size_t * retlen, u_char * buf)
617 {
618         /* Just a special case of doc_read_ecc */
619         return doc_read_ecc(mtd, from, len, retlen, buf, NULL, NULL);
620 }
621
622 static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
623                         size_t * retlen, u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel)
624 {
625         struct DiskOnChip *this = mtd->priv;
626         void __iomem *docptr = this->virtadr;
627         struct Nand *mychip;
628         unsigned char syndrome[6];
629         volatile char dummy;
630         int i, len256 = 0, ret=0;
631         size_t left = len;
632
633         /* Don't allow read past end of device */
634         if (from >= this->totlen)
635                 return -EINVAL;
636
637         mutex_lock(&this->lock);
638
639         *retlen = 0;
640         while (left) {
641                 len = left;
642
643                 /* Don't allow a single read to cross a 512-byte block boundary */
644                 if (from + len > ((from | 0x1ff) + 1))
645                         len = ((from | 0x1ff) + 1) - from;
646
647                 /* The ECC will not be calculated correctly if less than 512 is read */
648                 if (len != 0x200 && eccbuf)
649                         printk(KERN_WARNING
650                                "ECC needs a full sector read (adr: %lx size %lx)\n",
651                                (long) from, (long) len);
652
653                 /* printk("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len); */
654
655
656                 /* Find the chip which is to be used and select it */
657                 mychip = &this->chips[from >> (this->chipshift)];
658
659                 if (this->curfloor != mychip->floor) {
660                         DoC_SelectFloor(this, mychip->floor);
661                         DoC_SelectChip(this, mychip->chip);
662                 } else if (this->curchip != mychip->chip) {
663                         DoC_SelectChip(this, mychip->chip);
664                 }
665
666                 this->curfloor = mychip->floor;
667                 this->curchip = mychip->chip;
668
669                 DoC_Command(this,
670                             (!this->page256
671                              && (from & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
672                             CDSN_CTRL_WP);
673                 DoC_Address(this, ADDR_COLUMN_PAGE, from, CDSN_CTRL_WP,
674                             CDSN_CTRL_ECC_IO);
675
676                 if (eccbuf) {
677                         /* Prime the ECC engine */
678                         WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
679                         WriteDOC(DOC_ECC_EN, docptr, ECCConf);
680                 } else {
681                         /* disable the ECC engine */
682                         WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
683                         WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
684                 }
685
686                 /* treat crossing 256-byte sector for 2M x 8bits devices */
687                 if (this->page256 && from + len > (from | 0xff) + 1) {
688                         len256 = (from | 0xff) + 1 - from;
689                         DoC_ReadBuf(this, buf, len256);
690
691                         DoC_Command(this, NAND_CMD_READ0, CDSN_CTRL_WP);
692                         DoC_Address(this, ADDR_COLUMN_PAGE, from + len256,
693                                     CDSN_CTRL_WP, CDSN_CTRL_ECC_IO);
694                 }
695
696                 DoC_ReadBuf(this, &buf[len256], len - len256);
697
698                 /* Let the caller know we completed it */
699                 *retlen += len;
700
701                 if (eccbuf) {
702                         /* Read the ECC data through the DiskOnChip ECC logic */
703                         /* Note: this will work even with 2M x 8bit devices as   */
704                         /*       they have 8 bytes of OOB per 256 page. mf.      */
705                         DoC_ReadBuf(this, eccbuf, 6);
706
707                         /* Flush the pipeline */
708                         if (DoC_is_Millennium(this)) {
709                                 dummy = ReadDOC(docptr, ECCConf);
710                                 dummy = ReadDOC(docptr, ECCConf);
711                                 i = ReadDOC(docptr, ECCConf);
712                         } else {
713                                 dummy = ReadDOC(docptr, 2k_ECCStatus);
714                                 dummy = ReadDOC(docptr, 2k_ECCStatus);
715                                 i = ReadDOC(docptr, 2k_ECCStatus);
716                         }
717
718                         /* Check the ECC Status */
719                         if (i & 0x80) {
720                                 int nb_errors;
721                                 /* There was an ECC error */
722 #ifdef ECC_DEBUG
723                                 printk(KERN_ERR "DiskOnChip ECC Error: Read at %lx\n", (long)from);
724 #endif
725                                 /* Read the ECC syndrom through the DiskOnChip ECC logic.
726                                    These syndrome will be all ZERO when there is no error */
727                                 for (i = 0; i < 6; i++) {
728                                         syndrome[i] =
729                                             ReadDOC(docptr, ECCSyndrome0 + i);
730                                 }
731                                 nb_errors = doc_decode_ecc(buf, syndrome);
732
733 #ifdef ECC_DEBUG
734                                 printk(KERN_ERR "Errors corrected: %x\n", nb_errors);
735 #endif
736                                 if (nb_errors < 0) {
737                                         /* We return error, but have actually done the read. Not that
738                                            this can be told to user-space, via sys_read(), but at least
739                                            MTD-aware stuff can know about it by checking *retlen */
740                                         ret = -EIO;
741                                 }
742                         }
743
744 #ifdef PSYCHO_DEBUG
745                         printk(KERN_DEBUG "ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
746                                      (long)from, eccbuf[0], eccbuf[1], eccbuf[2],
747                                      eccbuf[3], eccbuf[4], eccbuf[5]);
748 #endif
749
750                         /* disable the ECC engine */
751                         WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
752                 }
753
754                 /* according to 11.4.1, we need to wait for the busy line
755                  * drop if we read to the end of the page.  */
756                 if(0 == ((from + len) & 0x1ff))
757                 {
758                     DoC_WaitReady(this);
759                 }
760
761                 from += len;
762                 left -= len;
763                 buf += len;
764         }
765
766         mutex_unlock(&this->lock);
767
768         return ret;
769 }
770
771 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
772                      size_t * retlen, const u_char * buf)
773 {
774         char eccbuf[6];
775         return doc_write_ecc(mtd, to, len, retlen, buf, eccbuf, NULL);
776 }
777
778 static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
779                          size_t * retlen, const u_char * buf,
780                          u_char * eccbuf, struct nand_oobinfo *oobsel)
781 {
782         struct DiskOnChip *this = mtd->priv;
783         int di; /* Yes, DI is a hangover from when I was disassembling the binary driver */
784         void __iomem *docptr = this->virtadr;
785         volatile char dummy;
786         int len256 = 0;
787         struct Nand *mychip;
788         size_t left = len;
789         int status;
790
791         /* Don't allow write past end of device */
792         if (to >= this->totlen)
793                 return -EINVAL;
794
795         mutex_lock(&this->lock);
796
797         *retlen = 0;
798         while (left) {
799                 len = left;
800
801                 /* Don't allow a single write to cross a 512-byte block boundary */
802                 if (to + len > ((to | 0x1ff) + 1))
803                         len = ((to | 0x1ff) + 1) - to;
804
805                 /* The ECC will not be calculated correctly if less than 512 is written */
806 /* DBB-
807                 if (len != 0x200 && eccbuf)
808                         printk(KERN_WARNING
809                                "ECC needs a full sector write (adr: %lx size %lx)\n",
810                                (long) to, (long) len);
811    -DBB */
812
813                 /* printk("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */
814
815                 /* Find the chip which is to be used and select it */
816                 mychip = &this->chips[to >> (this->chipshift)];
817
818                 if (this->curfloor != mychip->floor) {
819                         DoC_SelectFloor(this, mychip->floor);
820                         DoC_SelectChip(this, mychip->chip);
821                 } else if (this->curchip != mychip->chip) {
822                         DoC_SelectChip(this, mychip->chip);
823                 }
824
825                 this->curfloor = mychip->floor;
826                 this->curchip = mychip->chip;
827
828                 /* Set device to main plane of flash */
829                 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
830                 DoC_Command(this,
831                             (!this->page256
832                              && (to & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
833                             CDSN_CTRL_WP);
834
835                 DoC_Command(this, NAND_CMD_SEQIN, 0);
836                 DoC_Address(this, ADDR_COLUMN_PAGE, to, 0, CDSN_CTRL_ECC_IO);
837
838                 if (eccbuf) {
839                         /* Prime the ECC engine */
840                         WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
841                         WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
842                 } else {
843                         /* disable the ECC engine */
844                         WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
845                         WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
846                 }
847
848                 /* treat crossing 256-byte sector for 2M x 8bits devices */
849                 if (this->page256 && to + len > (to | 0xff) + 1) {
850                         len256 = (to | 0xff) + 1 - to;
851                         DoC_WriteBuf(this, buf, len256);
852
853                         DoC_Command(this, NAND_CMD_PAGEPROG, 0);
854
855                         DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
856                         /* There's an implicit DoC_WaitReady() in DoC_Command */
857
858                         dummy = ReadDOC(docptr, CDSNSlowIO);
859                         DoC_Delay(this, 2);
860
861                         if (ReadDOC_(docptr, this->ioreg) & 1) {
862                                 printk(KERN_ERR "Error programming flash\n");
863                                 /* Error in programming */
864                                 *retlen = 0;
865                                 mutex_unlock(&this->lock);
866                                 return -EIO;
867                         }
868
869                         DoC_Command(this, NAND_CMD_SEQIN, 0);
870                         DoC_Address(this, ADDR_COLUMN_PAGE, to + len256, 0,
871                                     CDSN_CTRL_ECC_IO);
872                 }
873
874                 DoC_WriteBuf(this, &buf[len256], len - len256);
875
876                 if (eccbuf) {
877                         WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_CE, docptr,
878                                  CDSNControl);
879
880                         if (DoC_is_Millennium(this)) {
881                                 WriteDOC(0, docptr, NOP);
882                                 WriteDOC(0, docptr, NOP);
883                                 WriteDOC(0, docptr, NOP);
884                         } else {
885                                 WriteDOC_(0, docptr, this->ioreg);
886                                 WriteDOC_(0, docptr, this->ioreg);
887                                 WriteDOC_(0, docptr, this->ioreg);
888                         }
889
890                         WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_FLASH_IO | CDSN_CTRL_CE, docptr,
891                                  CDSNControl);
892
893                         /* Read the ECC data through the DiskOnChip ECC logic */
894                         for (di = 0; di < 6; di++) {
895                                 eccbuf[di] = ReadDOC(docptr, ECCSyndrome0 + di);
896                         }
897
898                         /* Reset the ECC engine */
899                         WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
900
901 #ifdef PSYCHO_DEBUG
902                         printk
903                             ("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
904                              (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
905                              eccbuf[4], eccbuf[5]);
906 #endif
907                 }
908
909                 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
910
911                 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
912                 /* There's an implicit DoC_WaitReady() in DoC_Command */
913
914                 if (DoC_is_Millennium(this)) {
915                         ReadDOC(docptr, ReadPipeInit);
916                         status = ReadDOC(docptr, LastDataRead);
917                 } else {
918                         dummy = ReadDOC(docptr, CDSNSlowIO);
919                         DoC_Delay(this, 2);
920                         status = ReadDOC_(docptr, this->ioreg);
921                 }
922
923                 if (status & 1) {
924                         printk(KERN_ERR "Error programming flash\n");
925                         /* Error in programming */
926                         *retlen = 0;
927                         mutex_unlock(&this->lock);
928                         return -EIO;
929                 }
930
931                 /* Let the caller know we completed it */
932                 *retlen += len;
933
934                 if (eccbuf) {
935                         unsigned char x[8];
936                         size_t dummy;
937                         int ret;
938
939                         /* Write the ECC data to flash */
940                         for (di=0; di<6; di++)
941                                 x[di] = eccbuf[di];
942
943                         x[6]=0x55;
944                         x[7]=0x55;
945
946                         ret = doc_write_oob_nolock(mtd, to, 8, &dummy, x);
947                         if (ret) {
948                                 mutex_unlock(&this->lock);
949                                 return ret;
950                         }
951                 }
952
953                 to += len;
954                 left -= len;
955                 buf += len;
956         }
957
958         mutex_unlock(&this->lock);
959         return 0;
960 }
961
962 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
963                         struct mtd_oob_ops *ops)
964 {
965         struct DiskOnChip *this = mtd->priv;
966         int len256 = 0, ret;
967         struct Nand *mychip;
968         uint8_t *buf = ops->oobbuf;
969         size_t len = ops->len;
970
971         BUG_ON(ops->mode != MTD_OOB_PLACE);
972
973         ofs += ops->ooboffs;
974
975         mutex_lock(&this->lock);
976
977         mychip = &this->chips[ofs >> this->chipshift];
978
979         if (this->curfloor != mychip->floor) {
980                 DoC_SelectFloor(this, mychip->floor);
981                 DoC_SelectChip(this, mychip->chip);
982         } else if (this->curchip != mychip->chip) {
983                 DoC_SelectChip(this, mychip->chip);
984         }
985         this->curfloor = mychip->floor;
986         this->curchip = mychip->chip;
987
988         /* update address for 2M x 8bit devices. OOB starts on the second */
989         /* page to maintain compatibility with doc_read_ecc. */
990         if (this->page256) {
991                 if (!(ofs & 0x8))
992                         ofs += 0x100;
993                 else
994                         ofs -= 0x8;
995         }
996
997         DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
998         DoC_Address(this, ADDR_COLUMN_PAGE, ofs, CDSN_CTRL_WP, 0);
999
1000         /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1001         /* Note: datasheet says it should automaticaly wrap to the */
1002         /*       next OOB block, but it didn't work here. mf.      */
1003         if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1004                 len256 = (ofs | 0x7) + 1 - ofs;
1005                 DoC_ReadBuf(this, buf, len256);
1006
1007                 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1008                 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff),
1009                             CDSN_CTRL_WP, 0);
1010         }
1011
1012         DoC_ReadBuf(this, &buf[len256], len - len256);
1013
1014         ops->retlen = len;
1015         /* Reading the full OOB data drops us off of the end of the page,
1016          * causing the flash device to go into busy mode, so we need
1017          * to wait until ready 11.4.1 and Toshiba TC58256FT docs */
1018
1019         ret = DoC_WaitReady(this);
1020
1021         mutex_unlock(&this->lock);
1022         return ret;
1023
1024 }
1025
1026 static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
1027                                 size_t * retlen, const u_char * buf)
1028 {
1029         struct DiskOnChip *this = mtd->priv;
1030         int len256 = 0;
1031         void __iomem *docptr = this->virtadr;
1032         struct Nand *mychip = &this->chips[ofs >> this->chipshift];
1033         volatile int dummy;
1034         int status;
1035
1036         //      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,
1037         //   buf[0], buf[1], buf[2], buf[3], buf[8], buf[9], buf[14],buf[15]);
1038
1039         /* Find the chip which is to be used and select it */
1040         if (this->curfloor != mychip->floor) {
1041                 DoC_SelectFloor(this, mychip->floor);
1042                 DoC_SelectChip(this, mychip->chip);
1043         } else if (this->curchip != mychip->chip) {
1044                 DoC_SelectChip(this, mychip->chip);
1045         }
1046         this->curfloor = mychip->floor;
1047         this->curchip = mychip->chip;
1048
1049         /* disable the ECC engine */
1050         WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
1051         WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
1052
1053         /* Reset the chip, see Software Requirement 11.4 item 1. */
1054         DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
1055
1056         /* issue the Read2 command to set the pointer to the Spare Data Area. */
1057         DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1058
1059         /* update address for 2M x 8bit devices. OOB starts on the second */
1060         /* page to maintain compatibility with doc_read_ecc. */
1061         if (this->page256) {
1062                 if (!(ofs & 0x8))
1063                         ofs += 0x100;
1064                 else
1065                         ofs -= 0x8;
1066         }
1067
1068         /* issue the Serial Data In command to initial the Page Program process */
1069         DoC_Command(this, NAND_CMD_SEQIN, 0);
1070         DoC_Address(this, ADDR_COLUMN_PAGE, ofs, 0, 0);
1071
1072         /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1073         /* Note: datasheet says it should automaticaly wrap to the */
1074         /*       next OOB block, but it didn't work here. mf.      */
1075         if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1076                 len256 = (ofs | 0x7) + 1 - ofs;
1077                 DoC_WriteBuf(this, buf, len256);
1078
1079                 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1080                 DoC_Command(this, NAND_CMD_STATUS, 0);
1081                 /* DoC_WaitReady() is implicit in DoC_Command */
1082
1083                 if (DoC_is_Millennium(this)) {
1084                         ReadDOC(docptr, ReadPipeInit);
1085                         status = ReadDOC(docptr, LastDataRead);
1086                 } else {
1087                         dummy = ReadDOC(docptr, CDSNSlowIO);
1088                         DoC_Delay(this, 2);
1089                         status = ReadDOC_(docptr, this->ioreg);
1090                 }
1091
1092                 if (status & 1) {
1093                         printk(KERN_ERR "Error programming oob data\n");
1094                         /* There was an error */
1095                         *retlen = 0;
1096                         return -EIO;
1097                 }
1098                 DoC_Command(this, NAND_CMD_SEQIN, 0);
1099                 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), 0, 0);
1100         }
1101
1102         DoC_WriteBuf(this, &buf[len256], len - len256);
1103
1104         DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1105         DoC_Command(this, NAND_CMD_STATUS, 0);
1106         /* DoC_WaitReady() is implicit in DoC_Command */
1107
1108         if (DoC_is_Millennium(this)) {
1109                 ReadDOC(docptr, ReadPipeInit);
1110                 status = ReadDOC(docptr, LastDataRead);
1111         } else {
1112                 dummy = ReadDOC(docptr, CDSNSlowIO);
1113                 DoC_Delay(this, 2);
1114                 status = ReadDOC_(docptr, this->ioreg);
1115         }
1116
1117         if (status & 1) {
1118                 printk(KERN_ERR "Error programming oob data\n");
1119                 /* There was an error */
1120                 *retlen = 0;
1121                 return -EIO;
1122         }
1123
1124         *retlen = len;
1125         return 0;
1126
1127 }
1128
1129 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
1130                          struct mtd_oob_ops *ops)
1131 {
1132         struct DiskOnChip *this = mtd->priv;
1133         int ret;
1134
1135         BUG_ON(ops->mode != MTD_OOB_PLACE);
1136
1137         mutex_lock(&this->lock);
1138         ret = doc_write_oob_nolock(mtd, ofs + ops->ooboffs, ops->len,
1139                                    &ops->retlen, ops->oobbuf);
1140
1141         mutex_unlock(&this->lock);
1142         return ret;
1143 }
1144
1145 static int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
1146 {
1147         struct DiskOnChip *this = mtd->priv;
1148         __u32 ofs = instr->addr;
1149         __u32 len = instr->len;
1150         volatile int dummy;
1151         void __iomem *docptr = this->virtadr;
1152         struct Nand *mychip;
1153         int status;
1154
1155         mutex_lock(&this->lock);
1156
1157         if (ofs & (mtd->erasesize-1) || len & (mtd->erasesize-1)) {
1158                 mutex_unlock(&this->lock);
1159                 return -EINVAL;
1160         }
1161
1162         instr->state = MTD_ERASING;
1163
1164         /* FIXME: Do this in the background. Use timers or schedule_task() */
1165         while(len) {
1166                 mychip = &this->chips[ofs >> this->chipshift];
1167
1168                 if (this->curfloor != mychip->floor) {
1169                         DoC_SelectFloor(this, mychip->floor);
1170                         DoC_SelectChip(this, mychip->chip);
1171                 } else if (this->curchip != mychip->chip) {
1172                         DoC_SelectChip(this, mychip->chip);
1173                 }
1174                 this->curfloor = mychip->floor;
1175                 this->curchip = mychip->chip;
1176
1177                 DoC_Command(this, NAND_CMD_ERASE1, 0);
1178                 DoC_Address(this, ADDR_PAGE, ofs, 0, 0);
1179                 DoC_Command(this, NAND_CMD_ERASE2, 0);
1180
1181                 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
1182
1183                 if (DoC_is_Millennium(this)) {
1184                         ReadDOC(docptr, ReadPipeInit);
1185                         status = ReadDOC(docptr, LastDataRead);
1186                 } else {
1187                         dummy = ReadDOC(docptr, CDSNSlowIO);
1188                         DoC_Delay(this, 2);
1189                         status = ReadDOC_(docptr, this->ioreg);
1190                 }
1191
1192                 if (status & 1) {
1193                         printk(KERN_ERR "Error erasing at 0x%x\n", ofs);
1194                         /* There was an error */
1195                         instr->state = MTD_ERASE_FAILED;
1196                         goto callback;
1197                 }
1198                 ofs += mtd->erasesize;
1199                 len -= mtd->erasesize;
1200         }
1201         instr->state = MTD_ERASE_DONE;
1202
1203  callback:
1204         mtd_erase_callback(instr);
1205
1206         mutex_unlock(&this->lock);
1207         return 0;
1208 }
1209
1210
1211 /****************************************************************************
1212  *
1213  * Module stuff
1214  *
1215  ****************************************************************************/
1216
1217 static void __exit cleanup_doc2000(void)
1218 {
1219         struct mtd_info *mtd;
1220         struct DiskOnChip *this;
1221
1222         while ((mtd = doc2klist)) {
1223                 this = mtd->priv;
1224                 doc2klist = this->nextdoc;
1225
1226                 del_mtd_device(mtd);
1227
1228                 iounmap(this->virtadr);
1229                 kfree(this->chips);
1230                 kfree(mtd);
1231         }
1232 }
1233
1234 module_exit(cleanup_doc2000);
1235
1236 MODULE_LICENSE("GPL");
1237 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
1238 MODULE_DESCRIPTION("MTD driver for DiskOnChip 2000 and Millennium");
1239