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