Import changeset
[linux-flexiantxendom0-3.2.10.git] / drivers / mtd / 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.24 2000/12/01 13:11:02 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/malloc.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/nand_ids.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
37 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
38                     size_t *retlen, u_char *buf);
39 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
40                      size_t *retlen, const u_char *buf);
41 static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
42                         size_t *retlen, u_char *buf, u_char *eccbuf);
43 static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
44                          size_t *retlen, const u_char *buf, u_char *eccbuf);
45 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
46                         size_t *retlen, u_char *buf);
47 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
48                          size_t *retlen, const u_char *buf);
49 static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
50
51 static struct mtd_info *docmillist = NULL;
52
53 /* Perform the required delay cycles by reading from the NOP register */
54 static void DoC_Delay(unsigned long docptr, unsigned short cycles)
55 {
56         volatile char dummy;
57         int i;
58
59         for (i = 0; i < cycles; i++)
60                 dummy = ReadDOC(docptr, NOP);
61 }
62
63 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
64 static int _DoC_WaitReady(unsigned long docptr)
65 {
66         unsigned short c = 0xffff;
67
68         DEBUG(MTD_DEBUG_LEVEL3,
69               "_DoC_WaitReady called for out-of-line wait\n");
70
71         /* Out-of-line routine to wait for chip response */
72         while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B) && --c)
73                 ;
74
75         if (c == 0)
76                 DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
77
78         return (c == 0);
79 }
80
81 static inline int DoC_WaitReady(unsigned long docptr)
82 {
83         /* This is inline, to optimise the common case, where it's ready instantly */
84         int ret = 0;
85
86         /* 4 read form NOP register should be issued in prior to the read from CDSNControl
87            see Software Requirement 11.4 item 2. */
88         DoC_Delay(docptr, 4);
89
90         if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
91                 /* Call the out-of-line routine to wait */
92                 ret = _DoC_WaitReady(docptr);
93
94         /* issue 2 read from NOP register after reading from CDSNControl register
95            see Software Requirement 11.4 item 2. */
96         DoC_Delay(docptr, 2);
97
98         return ret;
99 }
100
101 /* DoC_Command: Send a flash command to the flash chip through the CDSN Slow IO register to
102    bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
103    required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
104
105 static inline void DoC_Command(unsigned long docptr, unsigned char command,
106                                unsigned char xtraflags)
107 {
108         /* Assert the CLE (Command Latch Enable) line to the flash chip */
109         WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
110         DoC_Delay(docptr, 4);
111
112         /* Send the command */
113         WriteDOC(command, docptr, CDSNSlowIO);
114         WriteDOC(command, docptr, Mil_CDSN_IO);
115
116         /* Lower the CLE line */
117         WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
118         DoC_Delay(docptr, 4);
119 }
120
121 /* DoC_Address: Set the current address for the flash chip through the CDSN Slow IO register to
122    bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
123    required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
124
125 static inline void DoC_Address(unsigned long docptr, int numbytes, unsigned long ofs,
126                                unsigned char xtraflags1, unsigned char xtraflags2)
127 {
128         /* Assert the ALE (Address Latch Enable) line to the flash chip */
129         WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
130         DoC_Delay(docptr, 4);
131
132         /* Send the address */
133         switch (numbytes)
134             {
135             case 1:
136                 /* Send single byte, bits 0-7. */
137                 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
138                 WriteDOC(ofs & 0xff, docptr, Mil_CDSN_IO);
139                 break;
140             case 2:
141                 /* Send bits 9-16 followed by 17-23 */
142                 WriteDOC((ofs >> 9)  & 0xff, docptr, CDSNSlowIO);
143                 WriteDOC((ofs >> 9)  & 0xff, docptr, Mil_CDSN_IO);
144                 WriteDOC((ofs >> 17) & 0xff, docptr, CDSNSlowIO);
145                 WriteDOC((ofs >> 17) & 0xff, docptr, Mil_CDSN_IO);
146                 break;
147             case 3:
148                 /* Send 0-7, 9-16, then 17-23 */
149                 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
150                 WriteDOC(ofs & 0xff, docptr, Mil_CDSN_IO);
151                 WriteDOC((ofs >> 9)  & 0xff, docptr, CDSNSlowIO);
152                 WriteDOC((ofs >> 9)  & 0xff, docptr, Mil_CDSN_IO);
153                 WriteDOC((ofs >> 17) & 0xff, docptr, CDSNSlowIO);
154                 WriteDOC((ofs >> 17) & 0xff, docptr, Mil_CDSN_IO);
155                 break;
156             default:
157                 return;
158             }
159
160         /* Lower the ALE line */
161         WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr, CDSNControl);
162         DoC_Delay(docptr, 4);
163 }
164
165 /* DoC_SelectChip: Select a given flash chip within the current floor */
166 static int DoC_SelectChip(unsigned long docptr, int chip)
167 {
168         /* Select the individual flash chip requested */
169         WriteDOC(chip, docptr, CDSNDeviceSelect);
170         DoC_Delay(docptr, 4);
171
172         /* Wait for it to be ready */
173         return DoC_WaitReady(docptr);
174 }
175
176 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
177 static int DoC_SelectFloor(unsigned long docptr, int floor)
178 {
179         /* Select the floor (bank) of chips required */
180         WriteDOC(floor, docptr, FloorSelect);
181
182         /* Wait for the chip to be ready */
183         return DoC_WaitReady(docptr);
184 }
185
186 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
187 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
188 {
189         int mfr, id, i;
190         volatile char dummy;
191
192         /* Page in the required floor/chip
193            FIXME: is this supported by Millennium ?? */
194         DoC_SelectFloor(doc->virtadr, floor);
195         DoC_SelectChip(doc->virtadr, chip);
196
197         /* Reset the chip, see Software Requirement 11.4 item 1. */
198         DoC_Command(doc->virtadr, NAND_CMD_RESET, CDSN_CTRL_WP);
199         DoC_WaitReady(doc->virtadr);
200
201         /* Read the NAND chip ID: 1. Send ReadID command */ 
202         DoC_Command(doc->virtadr, NAND_CMD_READID, CDSN_CTRL_WP);
203
204         /* Read the NAND chip ID: 2. Send address byte zero */ 
205         DoC_Address(doc->virtadr, 1, 0x00, CDSN_CTRL_WP, 0x00);
206
207         /* Read the manufacturer and device id codes of the flash device through
208            CDSN Slow IO register see Software Requirement 11.4 item 5.*/
209         dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
210         DoC_Delay(doc->virtadr, 2);
211         mfr = ReadDOC(doc->virtadr, Mil_CDSN_IO);
212
213         dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
214         DoC_Delay(doc->virtadr, 2);
215         id  = ReadDOC(doc->virtadr, Mil_CDSN_IO);
216
217         /* No response - return failure */
218         if (mfr == 0xff || mfr == 0)
219                 return 0;
220
221         /* FIXME: to deal with multi-flash on multi-Millennium case more carefully */
222         for (i = 0; nand_flash_ids[i].name != NULL; i++) {
223                 if (mfr == nand_flash_ids[i].manufacture_id &&
224                     id == nand_flash_ids[i].model_id) {
225                         printk(KERN_INFO "Flash chip found: Manufacturer ID: %2.2X, "
226                                "Chip ID: %2.2X (%s)\n",
227                                mfr, id, nand_flash_ids[i].name);
228                         doc->mfr = mfr;
229                         doc->id = id;
230                         doc->chipshift = nand_flash_ids[i].chipshift;
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_NOTICE "%d flash chips found. Total DiskOnChip size: %ld Mbytes\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->size = 0;
364
365         /* FIXME: erase size is not always 8kB */
366         mtd->erasesize = 0x2000;
367
368         mtd->oobblock = 512;
369         mtd->oobsize = 16;
370         mtd->module = THIS_MODULE;
371         mtd->erase = doc_erase;
372         mtd->point = NULL;
373         mtd->unpoint = NULL;
374         mtd->read = doc_read;
375         mtd->write = doc_write;
376         mtd->read_ecc = doc_read_ecc;
377         mtd->write_ecc = doc_write_ecc;
378         mtd->read_oob = doc_read_oob;
379         mtd->write_oob = doc_write_oob;
380         mtd->sync = NULL;
381
382         this->totlen = 0;
383         this->numchips = 0;
384         this->curfloor = -1;
385         this->curchip = -1;
386
387         /* Ident all the chips present. */
388         DoC_ScanChips(this);
389
390         if (!this->totlen) {
391                 kfree(mtd);
392                 iounmap((void *)this->virtadr);
393         } else {
394                 this->nextdoc = docmillist;
395                 docmillist = mtd;
396                 mtd->size  = this->totlen;
397                 add_mtd_device(mtd);
398                 return;
399         }
400 }
401
402 static int doc_read (struct mtd_info *mtd, loff_t from, size_t len,
403                      size_t *retlen, u_char *buf)
404 {
405         /* Just a special case of doc_read_ecc */
406         return doc_read_ecc(mtd, from, len, retlen, buf, NULL);
407 }
408
409 static int doc_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
410                          size_t *retlen, u_char *buf, u_char *eccbuf)
411 {
412         int i, ret;
413         volatile char dummy;
414         unsigned char syndrome[6];
415         struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
416         unsigned long docptr = this->virtadr;
417         struct Nand *mychip = &this->chips[from >> (this->chipshift)];
418
419         /* Don't allow read past end of device */
420         if (from >= this->totlen)
421                 return -EINVAL;
422
423         /* Don't allow a single read to cross a 512-byte block boundary */
424         if (from + len > ((from | 0x1ff) + 1)) 
425                 len = ((from | 0x1ff) + 1) - from;
426
427         /* Find the chip which is to be used and select it */
428         if (this->curfloor != mychip->floor) {
429                 DoC_SelectFloor(docptr, mychip->floor);
430                 DoC_SelectChip(docptr, mychip->chip);
431         } else if (this->curchip != mychip->chip) {
432                 DoC_SelectChip(docptr, mychip->chip);
433         }
434         this->curfloor = mychip->floor;
435         this->curchip = mychip->chip;
436
437         /* issue the Read0 or Read1 command depend on which half of the page
438            we are accessing. Polling the Flash Ready bit after issue 3 bytes
439            address in Sequence Read Mode, see Software Requirement 11.4 item 1.*/
440         DoC_Command(docptr, (from >> 8) & 1, CDSN_CTRL_WP);
441         DoC_Address(docptr, 3, from, CDSN_CTRL_WP, 0x00);
442         DoC_WaitReady(docptr);
443
444         if (eccbuf) {
445                 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
446                 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
447                 WriteDOC (DOC_ECC_EN, docptr, ECCConf);
448         } else {
449                 /* disable the ECC engine */
450                 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
451                 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
452         }
453
454         /* Read the data via the internal pipeline through CDSN IO register,
455            see Pipelined Read Operations 11.3 */
456         dummy = ReadDOC(docptr, ReadPipeInit);
457 #ifndef USE_MEMCPY
458         for (i = 0; i < len-1; i++) {
459                 /* N.B. you have to increase the source address in this way or the
460                    ECC logic will not work properly */
461                 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
462         }
463 #else
464         memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len - 1);
465 #endif
466         buf[len - 1] = ReadDOC(docptr, LastDataRead);
467
468         /* Let the caller know we completed it */
469         *retlen = len;
470         ret = 0;
471
472         if (eccbuf) {
473                 /* Read the ECC data from Spare Data Area,
474                    see Reed-Solomon EDC/ECC 11.1 */
475                 dummy = ReadDOC(docptr, ReadPipeInit);
476 #ifndef USE_MEMCPY
477                 for (i = 0; i < 5; i++) {
478                         /* N.B. you have to increase the source address in this way or the
479                            ECC logic will not work properly */
480                         eccbuf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
481                 }
482 #else
483                 memcpy_fromio(eccbuf, docptr + DoC_Mil_CDSN_IO, 5);
484 #endif
485                 eccbuf[5] = ReadDOC(docptr, LastDataRead);
486
487                 /* Flush the pipeline */
488                 dummy = ReadDOC(docptr, ECCConf);
489                 dummy = ReadDOC(docptr, ECCConf);
490
491                 /* Check the ECC Status */
492                 if (ReadDOC(docptr, ECCConf) & 0x80) {
493                         int nb_errors;
494                         /* There was an ECC error */
495 #ifdef ECC_DEBUG
496                         printk("DiskOnChip ECC Error: Read at %lx\n", (long)from);
497 #endif
498                         /* Read the ECC syndrom through the DiskOnChip ECC logic.
499                            These syndrome will be all ZERO when there is no error */
500                         for (i = 0; i < 6; i++) {
501                                 syndrome[i] = ReadDOC(docptr, ECCSyndrome0 + i);
502                         }
503                         nb_errors = doc_decode_ecc(buf, syndrome);
504 #ifdef ECC_DEBUG
505                         printk("Errors corrected: %x\n", nb_errors);
506 #endif
507                         if (nb_errors < 0) {
508                                 /* We return error, but have actually done the read. Not that
509                                    this can be told to user-space, via sys_read(), but at least
510                                    MTD-aware stuff can know about it by checking *retlen */
511                                 ret = -EIO;
512                         }
513                 }
514
515 #ifdef PSYCHO_DEBUG
516                 printk("ECC DATA at %lx: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
517                        (long)from, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
518                        eccbuf[4], eccbuf[5]);
519 #endif
520                 /* disable the ECC engine */
521                 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
522         }
523
524         return ret;
525 }
526
527 static int doc_write (struct mtd_info *mtd, loff_t to, size_t len,
528                       size_t *retlen, const u_char *buf)
529 {
530         char eccbuf[6];
531         return doc_write_ecc(mtd, to, len, retlen, buf, eccbuf);
532 }
533
534 static int doc_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
535                           size_t *retlen, const u_char *buf, u_char *eccbuf)
536 {
537         int i;
538         volatile char dummy;
539         struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
540         unsigned long docptr = this->virtadr;
541         struct Nand *mychip = &this->chips[to >> (this->chipshift)];
542
543         /* Don't allow write past end of device */
544         if (to >= this->totlen)
545                 return -EINVAL;
546
547 #if 0
548         /* Don't allow a single write to cross a 512-byte block boundary */
549         if (to + len > ( (to | 0x1ff) + 1)) 
550                 len = ((to | 0x1ff) + 1) - to;
551 #else
552         /* Don't allow writes which aren't exactly one block */
553         if (to & 0x1ff || len != 0x200)
554                 return -EINVAL;
555 #endif
556
557         /* Find the chip which is to be used and select it */
558         if (this->curfloor != mychip->floor) {
559                 DoC_SelectFloor(docptr, mychip->floor);
560                 DoC_SelectChip(docptr, mychip->chip);
561         } else if (this->curchip != mychip->chip) {
562                 DoC_SelectChip(docptr, mychip->chip);
563         }
564         this->curfloor = mychip->floor;
565         this->curchip = mychip->chip;
566
567         /* Reset the chip, see Software Requirement 11.4 item 1. */
568         DoC_Command(docptr, NAND_CMD_RESET, 0x00);
569         DoC_WaitReady(docptr);
570         /* Set device to main plane of flash */
571         DoC_Command(docptr, NAND_CMD_READ0, 0x00);
572
573         /* issue the Serial Data In command to initial the Page Program process */
574         DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
575         DoC_Address(docptr, 3, to, 0x00, 0x00);
576         DoC_WaitReady(docptr);
577
578         if (eccbuf) {
579                 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
580                 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
581                 WriteDOC (DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
582         } else {
583                 /* disable the ECC engine */
584                 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
585                 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
586         }
587
588         /* Write the data via the internal pipeline through CDSN IO register,
589            see Pipelined Write Operations 11.2 */
590 #ifndef USE_MEMCPY
591         for (i = 0; i < len; i++) {
592                 /* N.B. you have to increase the source address in this way or the
593                    ECC logic will not work properly */
594                 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
595         }
596 #else
597         memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
598 #endif
599         WriteDOC(0x00, docptr, WritePipeTerm);
600
601         if (eccbuf) {
602                 /* Write ECC data to flash, the ECC info is generated by the DiskOnChip ECC logic
603                    see Reed-Solomon EDC/ECC 11.1 */
604                 WriteDOC(0, docptr, NOP);
605                 WriteDOC(0, docptr, NOP);
606                 WriteDOC(0, docptr, NOP);
607
608                 /* Read the ECC data through the DiskOnChip ECC logic */
609                 for (i = 0; i < 6; i++) {
610                         eccbuf[i] = ReadDOC(docptr, ECCSyndrome0 + i);
611                 }
612
613                 /* ignore the ECC engine */
614                 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
615
616 #ifndef USE_MEMCPY
617                 /* Write the ECC data to flash */
618                 for (i = 0; i < 6; i++) {
619                         /* N.B. you have to increase the source address in this way or the
620                            ECC logic will not work properly */
621                         WriteDOC(eccbuf[i], docptr, Mil_CDSN_IO + i);
622                 }
623 #else
624                 memcpy_toio(docptr + DoC_Mil_CDSN_IO, eccbuf, 6);
625 #endif
626
627                 /* write the block status BLOCK_USED (0x5555) at the end of ECC data
628                    FIXME: this is only a hack for programming the IPL area for LinuxBIOS
629                    and should be replace with proper codes in user space utilities */ 
630                 WriteDOC(0x55, docptr, Mil_CDSN_IO);
631                 WriteDOC(0x55, docptr, Mil_CDSN_IO + 1);
632
633                 WriteDOC(0x00, docptr, WritePipeTerm);
634
635 #ifdef PSYCHO_DEBUG
636                 printk("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
637                        (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
638                        eccbuf[4], eccbuf[5]);
639 #endif
640         }
641
642         /* Commit the Page Program command and wait for ready
643            see Software Requirement 11.4 item 1.*/
644         DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
645         DoC_WaitReady(docptr);
646
647         /* Read the status of the flash device through CDSN Slow IO register
648            see Software Requirement 11.4 item 5.*/
649         DoC_Command(docptr, NAND_CMD_STATUS, CDSN_CTRL_WP);
650         dummy = ReadDOC(docptr, CDSNSlowIO);
651         DoC_Delay(docptr, 2);
652         if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
653                 printk("Error programming flash\n");
654                 /* Error in programming
655                    FIXME: implement Bad Block Replacement (in nftl.c ??) */
656                 *retlen = 0;
657                 return -EIO;
658         }
659
660         /* Let the caller know we completed it */
661         *retlen = len;
662
663         return 0;
664 }
665
666 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
667                         size_t *retlen, u_char *buf)
668 {
669 #ifndef USE_MEMCPY
670         int i;
671 #endif
672         volatile char dummy;
673         struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
674         unsigned long docptr = this->virtadr;
675         struct Nand *mychip = &this->chips[ofs >> this->chipshift];
676
677         /* Find the chip which is to be used and select it */
678         if (this->curfloor != mychip->floor) {
679                 DoC_SelectFloor(docptr, mychip->floor);
680                 DoC_SelectChip(docptr, mychip->chip);
681         } else if (this->curchip != mychip->chip) {
682                 DoC_SelectChip(docptr, mychip->chip);
683         }
684         this->curfloor = mychip->floor;
685         this->curchip = mychip->chip;
686
687         /* disable the ECC engine */
688         WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
689         WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
690
691         /* issue the Read2 command to set the pointer to the Spare Data Area.
692            Polling the Flash Ready bit after issue 3 bytes address in
693            Sequence Read Mode, see Software Requirement 11.4 item 1.*/
694         DoC_Command(docptr, NAND_CMD_READOOB, CDSN_CTRL_WP);
695         DoC_Address(docptr, 3, ofs, CDSN_CTRL_WP, 0x00);
696         DoC_WaitReady(docptr);
697
698         /* Read the data out via the internal pipeline through CDSN IO register,
699            see Pipelined Read Operations 11.3 */
700         dummy = ReadDOC(docptr, ReadPipeInit);
701 #ifndef USE_MEMCPY
702         for (i = 0; i < len-1; i++) {
703                 /* N.B. you have to increase the source address in this way or the
704                    ECC logic will not work properly */
705                 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
706         }
707         buf[i] = ReadDOC(docptr, LastDataRead);
708 #else
709         memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len - 1);
710 #endif
711         buf[len - 1] = ReadDOC(docptr, LastDataRead);
712
713         *retlen = len;
714
715         return 0;
716 }
717
718 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
719                          size_t *retlen, const u_char *buf)
720 {
721 #ifndef USE_MEMCPY
722         int i;
723 #endif
724         volatile char dummy;
725         struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
726         unsigned long docptr = this->virtadr;
727         struct Nand *mychip = &this->chips[ofs >> this->chipshift];
728
729         /* Find the chip which is to be used and select it */
730         if (this->curfloor != mychip->floor) {
731                 DoC_SelectFloor(docptr, mychip->floor);
732                 DoC_SelectChip(docptr, mychip->chip);
733         } else if (this->curchip != mychip->chip) {
734                 DoC_SelectChip(docptr, mychip->chip);
735         }
736         this->curfloor = mychip->floor;
737         this->curchip = mychip->chip;
738
739         /* disable the ECC engine */
740         WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
741         WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
742
743         /* Reset the chip, see Software Requirement 11.4 item 1. */
744         DoC_Command(docptr, NAND_CMD_RESET, CDSN_CTRL_WP);
745         DoC_WaitReady(docptr);
746         /* issue the Read2 command to set the pointer to the Spare Data Area. */
747         DoC_Command(docptr, NAND_CMD_READOOB, CDSN_CTRL_WP);
748
749         /* issue the Serial Data In command to initial the Page Program process */
750         DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
751         DoC_Address(docptr, 3, ofs, 0x00, 0x00);
752
753         /* Write the data via the internal pipeline through CDSN IO register,
754            see Pipelined Write Operations 11.2 */
755 #ifndef USE_MEMCPY
756         for (i = 0; i < len; i++) {
757                 /* N.B. you have to increase the source address in this way or the
758                    ECC logic will not work properly */
759                 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
760         }
761 #else
762         memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
763 #endif
764         WriteDOC(0x00, docptr, WritePipeTerm);
765
766         /* Commit the Page Program command and wait for ready
767            see Software Requirement 11.4 item 1.*/
768         DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
769         DoC_WaitReady(docptr);
770
771         /* Read the status of the flash device through CDSN Slow IO register
772            see Software Requirement 11.4 item 5.*/
773         DoC_Command(docptr, NAND_CMD_STATUS, 0x00);
774         dummy = ReadDOC(docptr, CDSNSlowIO);
775         DoC_Delay(docptr, 2);
776         if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
777                 printk("Error programming oob data\n");
778                 /* FIXME: implement Bad Block Replacement (in nftl.c ??) */
779                 *retlen = 0;
780                 return -EIO;
781         }
782
783         *retlen = len;
784
785         return 0;
786 }
787
788 int doc_erase (struct mtd_info *mtd, struct erase_info *instr)
789 {
790         volatile char dummy;
791         struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
792         unsigned long ofs = instr->addr;
793         unsigned long len = instr->len;
794         unsigned long docptr = this->virtadr;
795         struct Nand *mychip = &this->chips[ofs >> this->chipshift];
796
797         if (len != mtd->erasesize) 
798                 printk(KERN_WARNING "Erase not right size (%lx != %lx)n",
799                        len, mtd->erasesize);
800
801         /* Find the chip which is to be used and select it */
802         if (this->curfloor != mychip->floor) {
803                 DoC_SelectFloor(docptr, mychip->floor);
804                 DoC_SelectChip(docptr, mychip->chip);
805         } else if (this->curchip != mychip->chip) {
806                 DoC_SelectChip(docptr, mychip->chip);
807         }
808         this->curfloor = mychip->floor;
809         this->curchip = mychip->chip;
810
811         instr->state = MTD_ERASE_PENDING;
812
813         /* issue the Erase Setup command */
814         DoC_Command(docptr, NAND_CMD_ERASE1, 0x00);
815         DoC_Address(docptr, 2, ofs, 0x00, 0x00);
816
817         /* Commit the Erase Start command and wait for ready
818            see Software Requirement 11.4 item 1.*/
819         DoC_Command(docptr, NAND_CMD_ERASE2, 0x00);
820         DoC_WaitReady(docptr);
821
822         instr->state = MTD_ERASING;
823
824         /* Read the status of the flash device through CDSN Slow IO register
825            see Software Requirement 11.4 item 5.
826            FIXME: it seems that we are not wait long enough, some blocks are not
827            erased fully */
828         DoC_Command(docptr, NAND_CMD_STATUS, CDSN_CTRL_WP);
829         dummy = ReadDOC(docptr, CDSNSlowIO);
830         DoC_Delay(docptr, 2);
831         if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
832                 printk("Error Erasing at 0x%lx\n", ofs);
833                 /* There was an error
834                    FIXME: implement Bad Block Replacement (in nftl.c ??) */
835                 instr->state = MTD_ERASE_FAILED;
836         } else
837                 instr->state = MTD_ERASE_DONE;
838
839         if (instr->callback) 
840                 instr->callback(instr);
841
842         return 0;
843 }
844
845 /****************************************************************************
846  *
847  * Module stuff
848  *
849  ****************************************************************************/
850
851 #if LINUX_VERSION_CODE < 0x20212 && defined(MODULE)
852 #define cleanup_doc2001 cleanup_module
853 #define init_doc2001 init_module
854 #endif
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