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