This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / drivers / mtd / devices / doc2001.c
1
2 /*
3  * Linux driver for Disk-On-Chip Millennium
4  * (c) 1999 Machine Vision Holdings, Inc.
5  * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
6  *
7  * $Id: doc2001.c,v 1.42 2004/04/04 12:36:45 gleixner Exp $
8  */
9
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <asm/errno.h>
13 #include <asm/io.h>
14 #include <asm/uaccess.h>
15 #include <linux/miscdevice.h>
16 #include <linux/pci.h>
17 #include <linux/delay.h>
18 #include <linux/slab.h>
19 #include <linux/sched.h>
20 #include <linux/init.h>
21 #include <linux/types.h>
22 #include <linux/bitops.h>
23
24 #include <linux/mtd/mtd.h>
25 #include <linux/mtd/nand.h>
26 #include <linux/mtd/doc2000.h>
27
28 /* #define ECC_DEBUG */
29
30 /* I have no idea why some DoC chips can not use memcop_form|to_io().
31  * This may be due to the different revisions of the ASIC controller built-in or
32  * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
33  * this:*/
34 #undef USE_MEMCPY
35
36 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
37                     size_t *retlen, u_char *buf);
38 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
39                      size_t *retlen, const u_char *buf);
40 static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
41                         size_t *retlen, u_char *buf, u_char *eccbuf, int oobsel);
42 static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
43                          size_t *retlen, const u_char *buf, u_char *eccbuf, int oobsel);
44 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
45                         size_t *retlen, u_char *buf);
46 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
47                          size_t *retlen, const u_char *buf);
48 static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
49
50 static struct mtd_info *docmillist = NULL;
51
52 /* Perform the required delay cycles by reading from the NOP register */
53 static void DoC_Delay(unsigned long docptr, unsigned short cycles)
54 {
55         volatile char dummy;
56         int i;
57
58         for (i = 0; i < cycles; i++)
59                 dummy = ReadDOC(docptr, NOP);
60 }
61
62 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
63 static int _DoC_WaitReady(unsigned long docptr)
64 {
65         unsigned short c = 0xffff;
66
67         DEBUG(MTD_DEBUG_LEVEL3,
68               "_DoC_WaitReady called for out-of-line wait\n");
69
70         /* Out-of-line routine to wait for chip response */
71         while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B) && --c)
72                 ;
73
74         if (c == 0)
75                 DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
76
77         return (c == 0);
78 }
79
80 static inline int DoC_WaitReady(unsigned long docptr)
81 {
82         /* This is inline, to optimise the common case, where it's ready instantly */
83         int ret = 0;
84
85         /* 4 read form NOP register should be issued in prior to the read from CDSNControl
86            see Software Requirement 11.4 item 2. */
87         DoC_Delay(docptr, 4);
88
89         if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
90                 /* Call the out-of-line routine to wait */
91                 ret = _DoC_WaitReady(docptr);
92
93         /* issue 2 read from NOP register after reading from CDSNControl register
94            see Software Requirement 11.4 item 2. */
95         DoC_Delay(docptr, 2);
96
97         return ret;
98 }
99
100 /* DoC_Command: Send a flash command to the flash chip through the CDSN IO register
101    with the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
102    required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
103
104 static inline void DoC_Command(unsigned long docptr, unsigned char command,
105                                unsigned char xtraflags)
106 {
107         /* Assert the CLE (Command Latch Enable) line to the flash chip */
108         WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
109         DoC_Delay(docptr, 4);
110
111         /* Send the command */
112         WriteDOC(command, docptr, Mil_CDSN_IO);
113         WriteDOC(0x00, docptr, WritePipeTerm);
114
115         /* Lower the CLE line */
116         WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
117         DoC_Delay(docptr, 4);
118 }
119
120 /* DoC_Address: Set the current address for the flash chip through the CDSN IO register
121    with the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
122    required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
123
124 static inline void DoC_Address(unsigned long docptr, int numbytes, unsigned long ofs,
125                                unsigned char xtraflags1, unsigned char xtraflags2)
126 {
127         /* Assert the ALE (Address Latch Enable) line to the flash chip */
128         WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
129         DoC_Delay(docptr, 4);
130
131         /* Send the address */
132         switch (numbytes)
133             {
134             case 1:
135                     /* Send single byte, bits 0-7. */
136                     WriteDOC(ofs & 0xff, docptr, Mil_CDSN_IO);
137                     WriteDOC(0x00, docptr, WritePipeTerm);
138                     break;
139             case 2:
140                     /* Send bits 9-16 followed by 17-23 */
141                     WriteDOC((ofs >> 9)  & 0xff, docptr, Mil_CDSN_IO);
142                     WriteDOC((ofs >> 17) & 0xff, docptr, Mil_CDSN_IO);
143                     WriteDOC(0x00, docptr, WritePipeTerm);
144                 break;
145             case 3:
146                     /* Send 0-7, 9-16, then 17-23 */
147                     WriteDOC(ofs & 0xff, docptr, Mil_CDSN_IO);
148                     WriteDOC((ofs >> 9)  & 0xff, docptr, Mil_CDSN_IO);
149                     WriteDOC((ofs >> 17) & 0xff, docptr, Mil_CDSN_IO);
150                     WriteDOC(0x00, docptr, WritePipeTerm);
151                 break;
152             default:
153                 return;
154             }
155
156         /* Lower the ALE line */
157         WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr, CDSNControl);
158         DoC_Delay(docptr, 4);
159 }
160
161 /* DoC_SelectChip: Select a given flash chip within the current floor */
162 static int DoC_SelectChip(unsigned long docptr, int chip)
163 {
164         /* Select the individual flash chip requested */
165         WriteDOC(chip, docptr, CDSNDeviceSelect);
166         DoC_Delay(docptr, 4);
167
168         /* Wait for it to be ready */
169         return DoC_WaitReady(docptr);
170 }
171
172 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
173 static int DoC_SelectFloor(unsigned long docptr, int floor)
174 {
175         /* Select the floor (bank) of chips required */
176         WriteDOC(floor, docptr, FloorSelect);
177
178         /* Wait for the chip to be ready */
179         return DoC_WaitReady(docptr);
180 }
181
182 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
183 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
184 {
185         int mfr, id, i, j;
186         volatile char dummy;
187
188         /* Page in the required floor/chip
189            FIXME: is this supported by Millennium ?? */
190         DoC_SelectFloor(doc->virtadr, floor);
191         DoC_SelectChip(doc->virtadr, chip);
192
193         /* Reset the chip, see Software Requirement 11.4 item 1. */
194         DoC_Command(doc->virtadr, NAND_CMD_RESET, CDSN_CTRL_WP);
195         DoC_WaitReady(doc->virtadr);
196
197         /* Read the NAND chip ID: 1. Send ReadID command */ 
198         DoC_Command(doc->virtadr, NAND_CMD_READID, CDSN_CTRL_WP);
199
200         /* Read the NAND chip ID: 2. Send address byte zero */ 
201         DoC_Address(doc->virtadr, 1, 0x00, CDSN_CTRL_WP, 0x00);
202
203         /* Read the manufacturer and device id codes of the flash device through
204            CDSN IO register see Software Requirement 11.4 item 5.*/
205         dummy = ReadDOC(doc->virtadr, ReadPipeInit);
206         DoC_Delay(doc->virtadr, 2);
207         mfr = ReadDOC(doc->virtadr, Mil_CDSN_IO);
208
209         DoC_Delay(doc->virtadr, 2);
210         id  = ReadDOC(doc->virtadr, Mil_CDSN_IO);
211         dummy = ReadDOC(doc->virtadr, LastDataRead);
212
213         /* No response - return failure */
214         if (mfr == 0xff || mfr == 0)
215                 return 0;
216
217         /* FIXME: to deal with multi-flash on multi-Millennium case more carefully */
218         for (i = 0; nand_flash_ids[i].name != NULL; i++) {
219                 if ( id == nand_flash_ids[i].id) {
220                         /* Try to identify manufacturer */
221                         for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
222                                 if (nand_manuf_ids[j].id == mfr)
223                                         break;
224                         }       
225                         printk(KERN_INFO "Flash chip found: Manufacturer ID: %2.2X, "
226                                "Chip ID: %2.2X (%s:%s)\n",
227                                mfr, id, nand_manuf_ids[j].name, nand_flash_ids[i].name);
228                         doc->mfr = mfr;
229                         doc->id = id;
230                         doc->chipshift = ffs((nand_flash_ids[i].chipsize << 20)) - 1;
231                         break;
232                 }
233         }
234
235         if (nand_flash_ids[i].name == NULL)
236                 return 0;
237         else
238                 return 1;
239 }
240
241 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
242 static void DoC_ScanChips(struct DiskOnChip *this)
243 {
244         int floor, chip;
245         int numchips[MAX_FLOORS_MIL];
246         int ret;
247
248         this->numchips = 0;
249         this->mfr = 0;
250         this->id = 0;
251
252         /* For each floor, find the number of valid chips it contains */
253         for (floor = 0,ret = 1; floor < MAX_FLOORS_MIL; floor++) {
254                 numchips[floor] = 0;
255                 for (chip = 0; chip < MAX_CHIPS_MIL && ret != 0; chip++) {
256                         ret = DoC_IdentChip(this, floor, chip);
257                         if (ret) {
258                                 numchips[floor]++;
259                                 this->numchips++;
260                         }
261                 }
262         }
263         /* If there are none at all that we recognise, bail */
264         if (!this->numchips) {
265                 printk("No flash chips recognised.\n");
266                 return;
267         }
268
269         /* Allocate an array to hold the information for each chip */
270         this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
271         if (!this->chips){
272                 printk("No memory for allocating chip info structures\n");
273                 return;
274         }
275
276         /* Fill out the chip array with {floor, chipno} for each 
277          * detected chip in the device. */
278         for (floor = 0, ret = 0; floor < MAX_FLOORS_MIL; floor++) {
279                 for (chip = 0 ; chip < numchips[floor] ; chip++) {
280                         this->chips[ret].floor = floor;
281                         this->chips[ret].chip = chip;
282                         this->chips[ret].curadr = 0;
283                         this->chips[ret].curmode = 0x50;
284                         ret++;
285                 }
286         }
287
288         /* Calculate and print the total size of the device */
289         this->totlen = this->numchips * (1 << this->chipshift);
290         printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
291                this->numchips ,this->totlen >> 20);
292 }
293
294 static int DoCMil_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
295 {
296         int tmp1, tmp2, retval;
297
298         if (doc1->physadr == doc2->physadr)
299                 return 1;
300
301         /* Use the alias resolution register which was set aside for this
302          * purpose. If it's value is the same on both chips, they might
303          * be the same chip, and we write to one and check for a change in
304          * the other. It's unclear if this register is usuable in the
305          * DoC 2000 (it's in the Millenium docs), but it seems to work. */
306         tmp1 = ReadDOC(doc1->virtadr, AliasResolution);
307         tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
308         if (tmp1 != tmp2)
309                 return 0;
310         
311         WriteDOC((tmp1+1) % 0xff, doc1->virtadr, AliasResolution);
312         tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
313         if (tmp2 == (tmp1+1) % 0xff)
314                 retval = 1;
315         else
316                 retval = 0;
317
318         /* Restore register contents.  May not be necessary, but do it just to
319          * be safe. */
320         WriteDOC(tmp1, doc1->virtadr, AliasResolution);
321
322         return retval;
323 }
324
325 static const char im_name[] = "DoCMil_init";
326
327 /* This routine is made available to other mtd code via
328  * inter_module_register.  It must only be accessed through
329  * inter_module_get which will bump the use count of this module.  The
330  * addresses passed back in mtd are valid as long as the use count of
331  * this module is non-zero, i.e. between inter_module_get and
332  * inter_module_put.  Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
333  */
334 static void DoCMil_init(struct mtd_info *mtd)
335 {
336         struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
337         struct DiskOnChip *old = NULL;
338
339         /* We must avoid being called twice for the same device. */
340         if (docmillist)
341                 old = (struct DiskOnChip *)docmillist->priv;
342
343         while (old) {
344                 if (DoCMil_is_alias(this, old)) {
345                         printk(KERN_NOTICE "Ignoring DiskOnChip Millennium at "
346                                "0x%lX - already configured\n", this->physadr);
347                         iounmap((void *)this->virtadr);
348                         kfree(mtd);
349                         return;
350                 }
351                 if (old->nextdoc)
352                         old = (struct DiskOnChip *)old->nextdoc->priv;
353                 else
354                         old = NULL;
355         }
356
357         mtd->name = "DiskOnChip Millennium";
358         printk(KERN_NOTICE "DiskOnChip Millennium found at address 0x%lX\n",
359                this->physadr);
360
361         mtd->type = MTD_NANDFLASH;
362         mtd->flags = MTD_CAP_NANDFLASH;
363         mtd->ecctype = MTD_ECC_RS_DiskOnChip;
364         mtd->size = 0;
365
366         /* FIXME: erase size is not always 8KiB */
367         mtd->erasesize = 0x2000;
368
369         mtd->oobblock = 512;
370         mtd->oobsize = 16;
371         mtd->owner = THIS_MODULE;
372         mtd->erase = doc_erase;
373         mtd->point = NULL;
374         mtd->unpoint = NULL;
375         mtd->read = doc_read;
376         mtd->write = doc_write;
377         mtd->read_ecc = doc_read_ecc;
378         mtd->write_ecc = doc_write_ecc;
379         mtd->read_oob = doc_read_oob;
380         mtd->write_oob = doc_write_oob;
381         mtd->sync = NULL;
382
383         this->totlen = 0;
384         this->numchips = 0;
385         this->curfloor = -1;
386         this->curchip = -1;
387
388         /* Ident all the chips present. */
389         DoC_ScanChips(this);
390
391         if (!this->totlen) {
392                 kfree(mtd);
393                 iounmap((void *)this->virtadr);
394         } else {
395                 this->nextdoc = docmillist;
396                 docmillist = mtd;
397                 mtd->size  = this->totlen;
398                 add_mtd_device(mtd);
399                 return;
400         }
401 }
402
403 static int doc_read (struct mtd_info *mtd, loff_t from, size_t len,
404                      size_t *retlen, u_char *buf)
405 {
406         /* Just a special case of doc_read_ecc */
407         return doc_read_ecc(mtd, from, len, retlen, buf, NULL, 0);
408 }
409
410 static int doc_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
411                          size_t *retlen, u_char *buf, u_char *eccbuf, int oobsel)
412 {
413         int i, ret;
414         volatile char dummy;
415         unsigned char syndrome[6];
416         struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
417         unsigned long docptr = this->virtadr;
418         struct Nand *mychip = &this->chips[from >> (this->chipshift)];
419
420         /* Don't allow read past end of device */
421         if (from >= this->totlen)
422                 return -EINVAL;
423
424         /* Don't allow a single read to cross a 512-byte block boundary */
425         if (from + len > ((from | 0x1ff) + 1)) 
426                 len = ((from | 0x1ff) + 1) - from;
427
428         /* Find the chip which is to be used and select it */
429         if (this->curfloor != mychip->floor) {
430                 DoC_SelectFloor(docptr, mychip->floor);
431                 DoC_SelectChip(docptr, mychip->chip);
432         } else if (this->curchip != mychip->chip) {
433                 DoC_SelectChip(docptr, mychip->chip);
434         }
435         this->curfloor = mychip->floor;
436         this->curchip = mychip->chip;
437
438         /* issue the Read0 or Read1 command depend on which half of the page
439            we are accessing. Polling the Flash Ready bit after issue 3 bytes
440            address in Sequence Read Mode, see Software Requirement 11.4 item 1.*/
441         DoC_Command(docptr, (from >> 8) & 1, CDSN_CTRL_WP);
442         DoC_Address(docptr, 3, from, CDSN_CTRL_WP, 0x00);
443         DoC_WaitReady(docptr);
444
445         if (eccbuf) {
446                 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
447                 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
448                 WriteDOC (DOC_ECC_EN, docptr, ECCConf);
449         } else {
450                 /* disable the ECC engine */
451                 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
452                 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
453         }
454
455         /* Read the data via the internal pipeline through CDSN IO register,
456            see Pipelined Read Operations 11.3 */
457         dummy = ReadDOC(docptr, ReadPipeInit);
458 #ifndef USE_MEMCPY
459         for (i = 0; i < len-1; i++) {
460                 /* N.B. you have to increase the source address in this way or the
461                    ECC logic will not work properly */
462                 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
463         }
464 #else
465         memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len - 1);
466 #endif
467         buf[len - 1] = ReadDOC(docptr, LastDataRead);
468
469         /* Let the caller know we completed it */
470         *retlen = len;
471         ret = 0;
472
473         if (eccbuf) {
474                 /* Read the ECC data from Spare Data Area,
475                    see Reed-Solomon EDC/ECC 11.1 */
476                 dummy = ReadDOC(docptr, ReadPipeInit);
477 #ifndef USE_MEMCPY
478                 for (i = 0; i < 5; i++) {
479                         /* N.B. you have to increase the source address in this way or the
480                            ECC logic will not work properly */
481                         eccbuf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
482                 }
483 #else
484                 memcpy_fromio(eccbuf, docptr + DoC_Mil_CDSN_IO, 5);
485 #endif
486                 eccbuf[5] = ReadDOC(docptr, LastDataRead);
487
488                 /* Flush the pipeline */
489                 dummy = ReadDOC(docptr, ECCConf);
490                 dummy = ReadDOC(docptr, ECCConf);
491
492                 /* Check the ECC Status */
493                 if (ReadDOC(docptr, ECCConf) & 0x80) {
494                         int nb_errors;
495                         /* There was an ECC error */
496 #ifdef ECC_DEBUG
497                         printk("DiskOnChip ECC Error: Read at %lx\n", (long)from);
498 #endif
499                         /* Read the ECC syndrom through the DiskOnChip ECC logic.
500                            These syndrome will be all ZERO when there is no error */
501                         for (i = 0; i < 6; i++) {
502                                 syndrome[i] = ReadDOC(docptr, ECCSyndrome0 + i);
503                         }
504                         nb_errors = doc_decode_ecc(buf, syndrome);
505 #ifdef ECC_DEBUG
506                         printk("ECC Errors corrected: %x\n", nb_errors);
507 #endif
508                         if (nb_errors < 0) {
509                                 /* We return error, but have actually done the read. Not that
510                                    this can be told to user-space, via sys_read(), but at least
511                                    MTD-aware stuff can know about it by checking *retlen */
512                                 ret = -EIO;
513                         }
514                 }
515
516 #ifdef PSYCHO_DEBUG
517                 printk("ECC DATA at %lx: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
518                        (long)from, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
519                        eccbuf[4], eccbuf[5]);
520 #endif
521
522                 /* disable the ECC engine */
523                 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
524         }
525
526         return ret;
527 }
528
529 static int doc_write (struct mtd_info *mtd, loff_t to, size_t len,
530                       size_t *retlen, const u_char *buf)
531 {
532         char eccbuf[6];
533         return doc_write_ecc(mtd, to, len, retlen, buf, eccbuf, 0);
534 }
535
536 static int doc_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
537                           size_t *retlen, const u_char *buf, u_char *eccbuf, int oobsel)
538 {
539         int i,ret = 0;
540         volatile char dummy;
541         struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
542         unsigned long docptr = this->virtadr;
543         struct Nand *mychip = &this->chips[to >> (this->chipshift)];
544
545         /* Don't allow write past end of device */
546         if (to >= this->totlen)
547                 return -EINVAL;
548
549 #if 0
550         /* Don't allow a single write to cross a 512-byte block boundary */
551         if (to + len > ( (to | 0x1ff) + 1)) 
552                 len = ((to | 0x1ff) + 1) - to;
553 #else
554         /* Don't allow writes which aren't exactly one block */
555         if (to & 0x1ff || len != 0x200)
556                 return -EINVAL;
557 #endif
558
559         /* Find the chip which is to be used and select it */
560         if (this->curfloor != mychip->floor) {
561                 DoC_SelectFloor(docptr, mychip->floor);
562                 DoC_SelectChip(docptr, mychip->chip);
563         } else if (this->curchip != mychip->chip) {
564                 DoC_SelectChip(docptr, mychip->chip);
565         }
566         this->curfloor = mychip->floor;
567         this->curchip = mychip->chip;
568
569         /* Reset the chip, see Software Requirement 11.4 item 1. */
570         DoC_Command(docptr, NAND_CMD_RESET, 0x00);
571         DoC_WaitReady(docptr);
572         /* Set device to main plane of flash */
573         DoC_Command(docptr, NAND_CMD_READ0, 0x00);
574
575         /* issue the Serial Data In command to initial the Page Program process */
576         DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
577         DoC_Address(docptr, 3, to, 0x00, 0x00);
578         DoC_WaitReady(docptr);
579
580         if (eccbuf) {
581                 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
582                 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
583                 WriteDOC (DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
584         } else {
585                 /* disable the ECC engine */
586                 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
587                 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
588         }
589
590         /* Write the data via the internal pipeline through CDSN IO register,
591            see Pipelined Write Operations 11.2 */
592 #ifndef USE_MEMCPY
593         for (i = 0; i < len; i++) {
594                 /* N.B. you have to increase the source address in this way or the
595                    ECC logic will not work properly */
596                 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
597         }
598 #else
599         memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
600 #endif
601         WriteDOC(0x00, docptr, WritePipeTerm);
602
603         if (eccbuf) {
604                 /* Write ECC data to flash, the ECC info is generated by the DiskOnChip ECC logic
605                    see Reed-Solomon EDC/ECC 11.1 */
606                 WriteDOC(0, docptr, NOP);
607                 WriteDOC(0, docptr, NOP);
608                 WriteDOC(0, docptr, NOP);
609
610                 /* Read the ECC data through the DiskOnChip ECC logic */
611                 for (i = 0; i < 6; i++) {
612                         eccbuf[i] = ReadDOC(docptr, ECCSyndrome0 + i);
613                 }
614
615                 /* ignore the ECC engine */
616                 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
617
618 #ifndef USE_MEMCPY
619                 /* Write the ECC data to flash */
620                 for (i = 0; i < 6; i++) {
621                         /* N.B. you have to increase the source address in this way or the
622                            ECC logic will not work properly */
623                         WriteDOC(eccbuf[i], docptr, Mil_CDSN_IO + i);
624                 }
625 #else
626                 memcpy_toio(docptr + DoC_Mil_CDSN_IO, eccbuf, 6);
627 #endif
628
629                 /* write the block status BLOCK_USED (0x5555) at the end of ECC data
630                    FIXME: this is only a hack for programming the IPL area for LinuxBIOS
631                    and should be replace with proper codes in user space utilities */ 
632                 WriteDOC(0x55, docptr, Mil_CDSN_IO);
633                 WriteDOC(0x55, docptr, Mil_CDSN_IO + 1);
634
635                 WriteDOC(0x00, docptr, WritePipeTerm);
636
637 #ifdef PSYCHO_DEBUG
638                 printk("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
639                        (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
640                        eccbuf[4], eccbuf[5]);
641 #endif
642         }
643
644         /* Commit the Page Program command and wait for ready
645            see Software Requirement 11.4 item 1.*/
646         DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
647         DoC_WaitReady(docptr);
648
649         /* Read the status of the flash device through CDSN IO register
650            see Software Requirement 11.4 item 5.*/
651         DoC_Command(docptr, NAND_CMD_STATUS, CDSN_CTRL_WP);
652         dummy = ReadDOC(docptr, ReadPipeInit);
653         DoC_Delay(docptr, 2);
654         if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
655                 printk("Error programming flash\n");
656                 /* Error in programming
657                    FIXME: implement Bad Block Replacement (in nftl.c ??) */
658                 *retlen = 0;
659                 ret = -EIO;
660         }
661         dummy = ReadDOC(docptr, LastDataRead);
662
663         /* Let the caller know we completed it */
664         *retlen = len;
665
666         return ret;
667 }
668
669 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
670                         size_t *retlen, u_char *buf)
671 {
672 #ifndef USE_MEMCPY
673         int i;
674 #endif
675         volatile char dummy;
676         struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
677         unsigned long docptr = this->virtadr;
678         struct Nand *mychip = &this->chips[ofs >> this->chipshift];
679
680         /* Find the chip which is to be used and select it */
681         if (this->curfloor != mychip->floor) {
682                 DoC_SelectFloor(docptr, mychip->floor);
683                 DoC_SelectChip(docptr, mychip->chip);
684         } else if (this->curchip != mychip->chip) {
685                 DoC_SelectChip(docptr, mychip->chip);
686         }
687         this->curfloor = mychip->floor;
688         this->curchip = mychip->chip;
689
690         /* disable the ECC engine */
691         WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
692         WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
693
694         /* issue the Read2 command to set the pointer to the Spare Data Area.
695            Polling the Flash Ready bit after issue 3 bytes address in
696            Sequence Read Mode, see Software Requirement 11.4 item 1.*/
697         DoC_Command(docptr, NAND_CMD_READOOB, CDSN_CTRL_WP);
698         DoC_Address(docptr, 3, ofs, CDSN_CTRL_WP, 0x00);
699         DoC_WaitReady(docptr);
700
701         /* Read the data out via the internal pipeline through CDSN IO register,
702            see Pipelined Read Operations 11.3 */
703         dummy = ReadDOC(docptr, ReadPipeInit);
704 #ifndef USE_MEMCPY
705         for (i = 0; i < len-1; i++) {
706                 /* N.B. you have to increase the source address in this way or the
707                    ECC logic will not work properly */
708                 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
709         }
710 #else
711         memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len - 1);
712 #endif
713         buf[len - 1] = ReadDOC(docptr, LastDataRead);
714
715         *retlen = len;
716
717         return 0;
718 }
719
720 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
721                          size_t *retlen, const u_char *buf)
722 {
723 #ifndef USE_MEMCPY
724         int i;
725 #endif
726         volatile char dummy;
727         int ret = 0;
728         struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
729         unsigned long docptr = this->virtadr;
730         struct Nand *mychip = &this->chips[ofs >> this->chipshift];
731
732         /* Find the chip which is to be used and select it */
733         if (this->curfloor != mychip->floor) {
734                 DoC_SelectFloor(docptr, mychip->floor);
735                 DoC_SelectChip(docptr, mychip->chip);
736         } else if (this->curchip != mychip->chip) {
737                 DoC_SelectChip(docptr, mychip->chip);
738         }
739         this->curfloor = mychip->floor;
740         this->curchip = mychip->chip;
741
742         /* disable the ECC engine */
743         WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
744         WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
745
746         /* Reset the chip, see Software Requirement 11.4 item 1. */
747         DoC_Command(docptr, NAND_CMD_RESET, CDSN_CTRL_WP);
748         DoC_WaitReady(docptr);
749         /* issue the Read2 command to set the pointer to the Spare Data Area. */
750         DoC_Command(docptr, NAND_CMD_READOOB, CDSN_CTRL_WP);
751
752         /* issue the Serial Data In command to initial the Page Program process */
753         DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
754         DoC_Address(docptr, 3, ofs, 0x00, 0x00);
755
756         /* Write the data via the internal pipeline through CDSN IO register,
757            see Pipelined Write Operations 11.2 */
758 #ifndef USE_MEMCPY
759         for (i = 0; i < len; i++) {
760                 /* N.B. you have to increase the source address in this way or the
761                    ECC logic will not work properly */
762                 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
763         }
764 #else
765         memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
766 #endif
767         WriteDOC(0x00, docptr, WritePipeTerm);
768
769         /* Commit the Page Program command and wait for ready
770            see Software Requirement 11.4 item 1.*/
771         DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
772         DoC_WaitReady(docptr);
773
774         /* Read the status of the flash device through CDSN IO register
775            see Software Requirement 11.4 item 5.*/
776         DoC_Command(docptr, NAND_CMD_STATUS, 0x00);
777         dummy = ReadDOC(docptr, ReadPipeInit);
778         DoC_Delay(docptr, 2);
779         if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
780                 printk("Error programming oob data\n");
781                 /* FIXME: implement Bad Block Replacement (in nftl.c ??) */
782                 *retlen = 0;
783                 ret = -EIO;
784         }
785         dummy = ReadDOC(docptr, LastDataRead);
786
787         *retlen = len;
788
789         return ret;
790 }
791
792 int doc_erase (struct mtd_info *mtd, struct erase_info *instr)
793 {
794         volatile char dummy;
795         struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
796         __u32 ofs = instr->addr;
797         __u32 len = instr->len;
798         unsigned long docptr = this->virtadr;
799         struct Nand *mychip = &this->chips[ofs >> this->chipshift];
800
801         if (len != mtd->erasesize) 
802                 printk(KERN_WARNING "Erase not right size (%x != %x)n",
803                        len, mtd->erasesize);
804
805         /* Find the chip which is to be used and select it */
806         if (this->curfloor != mychip->floor) {
807                 DoC_SelectFloor(docptr, mychip->floor);
808                 DoC_SelectChip(docptr, mychip->chip);
809         } else if (this->curchip != mychip->chip) {
810                 DoC_SelectChip(docptr, mychip->chip);
811         }
812         this->curfloor = mychip->floor;
813         this->curchip = mychip->chip;
814
815         instr->state = MTD_ERASE_PENDING;
816
817         /* issue the Erase Setup command */
818         DoC_Command(docptr, NAND_CMD_ERASE1, 0x00);
819         DoC_Address(docptr, 2, ofs, 0x00, 0x00);
820
821         /* Commit the Erase Start command and wait for ready
822            see Software Requirement 11.4 item 1.*/
823         DoC_Command(docptr, NAND_CMD_ERASE2, 0x00);
824         DoC_WaitReady(docptr);
825
826         instr->state = MTD_ERASING;
827
828         /* Read the status of the flash device through CDSN IO register
829            see Software Requirement 11.4 item 5.
830            FIXME: it seems that we are not wait long enough, some blocks are not
831            erased fully */
832         DoC_Command(docptr, NAND_CMD_STATUS, CDSN_CTRL_WP);
833         dummy = ReadDOC(docptr, ReadPipeInit);
834         DoC_Delay(docptr, 2);
835         if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
836                 printk("Error Erasing at 0x%x\n", ofs);
837                 /* There was an error
838                    FIXME: implement Bad Block Replacement (in nftl.c ??) */
839                 instr->state = MTD_ERASE_FAILED;
840         } else
841                 instr->state = MTD_ERASE_DONE;
842         dummy = ReadDOC(docptr, LastDataRead);
843
844         if (instr->callback) 
845                 instr->callback(instr);
846
847         return 0;
848 }
849
850 /****************************************************************************
851  *
852  * Module stuff
853  *
854  ****************************************************************************/
855
856 int __init init_doc2001(void)
857 {
858         inter_module_register(im_name, THIS_MODULE, &DoCMil_init);
859         return 0;
860 }
861
862 static void __exit cleanup_doc2001(void)
863 {
864         struct mtd_info *mtd;
865         struct DiskOnChip *this;
866
867         while ((mtd=docmillist)) {
868                 this = (struct DiskOnChip *)mtd->priv;
869                 docmillist = this->nextdoc;
870                         
871                 del_mtd_device(mtd);
872                         
873                 iounmap((void *)this->virtadr);
874                 kfree(this->chips);
875                 kfree(mtd);
876         }
877         inter_module_unregister(im_name);
878 }
879
880 module_exit(cleanup_doc2001);
881 module_init(init_doc2001);
882
883 MODULE_LICENSE("GPL");
884 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
885 MODULE_DESCRIPTION("Alternative driver for DiskOnChip Millennium");