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