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