[MTD] [NAND] S3C2412 fix hw ecc
[powerpc.git] / drivers / mtd / devices / doc2001plus.c
1 /*
2  * Linux driver for Disk-On-Chip Millennium Plus
3  *
4  * (c) 2002-2003 Greg Ungerer <gerg@snapgear.com>
5  * (c) 2002-2003 SnapGear Inc
6  * (c) 1999 Machine Vision Holdings, Inc.
7  * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
8  *
9  * $Id: doc2001plus.c,v 1.14 2005/11/07 11:14:24 gleixner Exp $
10  *
11  * Released under GPL
12  */
13
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <asm/errno.h>
17 #include <asm/io.h>
18 #include <asm/uaccess.h>
19 #include <linux/miscdevice.h>
20 #include <linux/pci.h>
21 #include <linux/delay.h>
22 #include <linux/slab.h>
23 #include <linux/sched.h>
24 #include <linux/init.h>
25 #include <linux/types.h>
26 #include <linux/bitops.h>
27
28 #include <linux/mtd/mtd.h>
29 #include <linux/mtd/nand.h>
30 #include <linux/mtd/doc2000.h>
31
32 /* #define ECC_DEBUG */
33
34 /* I have no idea why some DoC chips can not use memcop_form|to_io().
35  * This may be due to the different revisions of the ASIC controller built-in or
36  * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
37  * this:*/
38 #undef USE_MEMCPY
39
40 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
41                 size_t *retlen, u_char *buf);
42 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
43                 size_t *retlen, const u_char *buf);
44 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
45                         struct mtd_oob_ops *ops);
46 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
47                          struct mtd_oob_ops *ops);
48 static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
49
50 static struct mtd_info *docmilpluslist = NULL;
51
52
53 /* Perform the required delay cycles by writing to the NOP register */
54 static void DoC_Delay(void __iomem * docptr, int cycles)
55 {
56         int i;
57
58         for (i = 0; (i < cycles); i++)
59                 WriteDOC(0, docptr, Mplus_NOP);
60 }
61
62 #define CDSN_CTRL_FR_B_MASK     (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
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 int 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, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) && --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         /* read form NOP register should be issued prior to the read from CDSNControl
88            see Software Requirement 11.4 item 2. */
89         DoC_Delay(docptr, 4);
90
91         if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
92                 /* Call the out-of-line routine to wait */
93                 ret = _DoC_WaitReady(docptr);
94
95         return ret;
96 }
97
98 /* For some reason the Millennium Plus seems to occassionally put itself
99  * into reset mode. For me this happens randomly, with no pattern that I
100  * can detect. M-systems suggest always check this on any block level
101  * operation and setting to normal mode if in reset mode.
102  */
103 static inline void DoC_CheckASIC(void __iomem * docptr)
104 {
105         /* Make sure the DoC is in normal mode */
106         if ((ReadDOC(docptr, Mplus_DOCControl) & DOC_MODE_NORMAL) == 0) {
107                 WriteDOC((DOC_MODE_NORMAL | DOC_MODE_MDWREN), docptr, Mplus_DOCControl);
108                 WriteDOC(~(DOC_MODE_NORMAL | DOC_MODE_MDWREN), docptr, Mplus_CtrlConfirm);
109         }
110 }
111
112 /* DoC_Command: Send a flash command to the flash chip through the Flash
113  * command register. Need 2 Write Pipeline Terminates to complete send.
114  */
115 static void DoC_Command(void __iomem * docptr, unsigned char command,
116                                unsigned char xtraflags)
117 {
118         WriteDOC(command, docptr, Mplus_FlashCmd);
119         WriteDOC(command, docptr, Mplus_WritePipeTerm);
120         WriteDOC(command, docptr, Mplus_WritePipeTerm);
121 }
122
123 /* DoC_Address: Set the current address for the flash chip through the Flash
124  * Address register. Need 2 Write Pipeline Terminates to complete send.
125  */
126 static inline void DoC_Address(struct DiskOnChip *doc, int numbytes,
127                                unsigned long ofs, unsigned char xtraflags1,
128                                unsigned char xtraflags2)
129 {
130         void __iomem * docptr = doc->virtadr;
131
132         /* Allow for possible Mill Plus internal flash interleaving */
133         ofs >>= doc->interleave;
134
135         switch (numbytes) {
136         case 1:
137                 /* Send single byte, bits 0-7. */
138                 WriteDOC(ofs & 0xff, docptr, Mplus_FlashAddress);
139                 break;
140         case 2:
141                 /* Send bits 9-16 followed by 17-23 */
142                 WriteDOC((ofs >> 9)  & 0xff, docptr, Mplus_FlashAddress);
143                 WriteDOC((ofs >> 17) & 0xff, docptr, Mplus_FlashAddress);
144                 break;
145         case 3:
146                 /* Send 0-7, 9-16, then 17-23 */
147                 WriteDOC(ofs & 0xff, docptr, Mplus_FlashAddress);
148                 WriteDOC((ofs >> 9)  & 0xff, docptr, Mplus_FlashAddress);
149                 WriteDOC((ofs >> 17) & 0xff, docptr, Mplus_FlashAddress);
150                 break;
151         default:
152                 return;
153         }
154
155         WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
156         WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
157 }
158
159 /* DoC_SelectChip: Select a given flash chip within the current floor */
160 static int DoC_SelectChip(void __iomem * docptr, int chip)
161 {
162         /* No choice for flash chip on Millennium Plus */
163         return 0;
164 }
165
166 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
167 static int DoC_SelectFloor(void __iomem * docptr, int floor)
168 {
169         WriteDOC((floor & 0x3), docptr, Mplus_DeviceSelect);
170         return 0;
171 }
172
173 /*
174  * Translate the given offset into the appropriate command and offset.
175  * This does the mapping using the 16bit interleave layout defined by
176  * M-Systems, and looks like this for a sector pair:
177  *  +-----------+-------+-------+-------+--------------+---------+-----------+
178  *  | 0 --- 511 |512-517|518-519|520-521| 522 --- 1033 |1034-1039|1040 - 1055|
179  *  +-----------+-------+-------+-------+--------------+---------+-----------+
180  *  | Data 0    | ECC 0 |Flags0 |Flags1 | Data 1       |ECC 1    | OOB 1 + 2 |
181  *  +-----------+-------+-------+-------+--------------+---------+-----------+
182  */
183 /* FIXME: This lives in INFTL not here. Other users of flash devices
184    may not want it */
185 static unsigned int DoC_GetDataOffset(struct mtd_info *mtd, loff_t *from)
186 {
187         struct DiskOnChip *this = mtd->priv;
188
189         if (this->interleave) {
190                 unsigned int ofs = *from & 0x3ff;
191                 unsigned int cmd;
192
193                 if (ofs < 512) {
194                         cmd = NAND_CMD_READ0;
195                         ofs &= 0x1ff;
196                 } else if (ofs < 1014) {
197                         cmd = NAND_CMD_READ1;
198                         ofs = (ofs & 0x1ff) + 10;
199                 } else {
200                         cmd = NAND_CMD_READOOB;
201                         ofs = ofs - 1014;
202                 }
203
204                 *from = (*from & ~0x3ff) | ofs;
205                 return cmd;
206         } else {
207                 /* No interleave */
208                 if ((*from) & 0x100)
209                         return NAND_CMD_READ1;
210                 return NAND_CMD_READ0;
211         }
212 }
213
214 static unsigned int DoC_GetECCOffset(struct mtd_info *mtd, loff_t *from)
215 {
216         unsigned int ofs, cmd;
217
218         if (*from & 0x200) {
219                 cmd = NAND_CMD_READOOB;
220                 ofs = 10 + (*from & 0xf);
221         } else {
222                 cmd = NAND_CMD_READ1;
223                 ofs = (*from & 0xf);
224         }
225
226         *from = (*from & ~0x3ff) | ofs;
227         return cmd;
228 }
229
230 static unsigned int DoC_GetFlagsOffset(struct mtd_info *mtd, loff_t *from)
231 {
232         unsigned int ofs, cmd;
233
234         cmd = NAND_CMD_READ1;
235         ofs = (*from & 0x200) ? 8 : 6;
236         *from = (*from & ~0x3ff) | ofs;
237         return cmd;
238 }
239
240 static unsigned int DoC_GetHdrOffset(struct mtd_info *mtd, loff_t *from)
241 {
242         unsigned int ofs, cmd;
243
244         cmd = NAND_CMD_READOOB;
245         ofs = (*from & 0x200) ? 24 : 16;
246         *from = (*from & ~0x3ff) | ofs;
247         return cmd;
248 }
249
250 static inline void MemReadDOC(void __iomem * docptr, unsigned char *buf, int len)
251 {
252 #ifndef USE_MEMCPY
253         int i;
254         for (i = 0; i < len; i++)
255                 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
256 #else
257         memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len);
258 #endif
259 }
260
261 static inline void MemWriteDOC(void __iomem * docptr, unsigned char *buf, int len)
262 {
263 #ifndef USE_MEMCPY
264         int i;
265         for (i = 0; i < len; i++)
266                 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
267 #else
268         memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
269 #endif
270 }
271
272 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
273 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
274 {
275         int mfr, id, i, j;
276         volatile char dummy;
277         void __iomem * docptr = doc->virtadr;
278
279         /* Page in the required floor/chip */
280         DoC_SelectFloor(docptr, floor);
281         DoC_SelectChip(docptr, chip);
282
283         /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
284         WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
285
286         /* Reset the chip, see Software Requirement 11.4 item 1. */
287         DoC_Command(docptr, NAND_CMD_RESET, 0);
288         DoC_WaitReady(docptr);
289
290         /* Read the NAND chip ID: 1. Send ReadID command */
291         DoC_Command(docptr, NAND_CMD_READID, 0);
292
293         /* Read the NAND chip ID: 2. Send address byte zero */
294         DoC_Address(doc, 1, 0x00, 0, 0x00);
295
296         WriteDOC(0, docptr, Mplus_FlashControl);
297         DoC_WaitReady(docptr);
298
299         /* Read the manufacturer and device id codes of the flash device through
300            CDSN IO register see Software Requirement 11.4 item 5.*/
301         dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
302         dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
303
304         mfr = ReadDOC(docptr, Mil_CDSN_IO);
305         if (doc->interleave)
306                 dummy = ReadDOC(docptr, Mil_CDSN_IO); /* 2 way interleave */
307
308         id  = ReadDOC(docptr, Mil_CDSN_IO);
309         if (doc->interleave)
310                 dummy = ReadDOC(docptr, Mil_CDSN_IO); /* 2 way interleave */
311
312         dummy = ReadDOC(docptr, Mplus_LastDataRead);
313         dummy = ReadDOC(docptr, Mplus_LastDataRead);
314
315         /* Disable flash internally */
316         WriteDOC(0, docptr, Mplus_FlashSelect);
317
318         /* No response - return failure */
319         if (mfr == 0xff || mfr == 0)
320                 return 0;
321
322         for (i = 0; nand_flash_ids[i].name != NULL; i++) {
323                 if (id == nand_flash_ids[i].id) {
324                         /* Try to identify manufacturer */
325                         for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
326                                 if (nand_manuf_ids[j].id == mfr)
327                                         break;
328                         }
329                         printk(KERN_INFO "Flash chip found: Manufacturer ID: %2.2X, "
330                                "Chip ID: %2.2X (%s:%s)\n", mfr, id,
331                                nand_manuf_ids[j].name, nand_flash_ids[i].name);
332                         doc->mfr = mfr;
333                         doc->id = id;
334                         doc->chipshift = ffs((nand_flash_ids[i].chipsize << 20)) - 1;
335                         doc->erasesize = nand_flash_ids[i].erasesize << doc->interleave;
336                         break;
337                 }
338         }
339
340         if (nand_flash_ids[i].name == NULL)
341                 return 0;
342         return 1;
343 }
344
345 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
346 static void DoC_ScanChips(struct DiskOnChip *this)
347 {
348         int floor, chip;
349         int numchips[MAX_FLOORS_MPLUS];
350         int ret;
351
352         this->numchips = 0;
353         this->mfr = 0;
354         this->id = 0;
355
356         /* Work out the intended interleave setting */
357         this->interleave = 0;
358         if (this->ChipID == DOC_ChipID_DocMilPlus32)
359                 this->interleave = 1;
360
361         /* Check the ASIC agrees */
362         if ( (this->interleave << 2) !=
363              (ReadDOC(this->virtadr, Mplus_Configuration) & 4)) {
364                 u_char conf = ReadDOC(this->virtadr, Mplus_Configuration);
365                 printk(KERN_NOTICE "Setting DiskOnChip Millennium Plus interleave to %s\n",
366                        this->interleave?"on (16-bit)":"off (8-bit)");
367                 conf ^= 4;
368                 WriteDOC(conf, this->virtadr, Mplus_Configuration);
369         }
370
371         /* For each floor, find the number of valid chips it contains */
372         for (floor = 0,ret = 1; floor < MAX_FLOORS_MPLUS; floor++) {
373                 numchips[floor] = 0;
374                 for (chip = 0; chip < MAX_CHIPS_MPLUS && ret != 0; chip++) {
375                         ret = DoC_IdentChip(this, floor, chip);
376                         if (ret) {
377                                 numchips[floor]++;
378                                 this->numchips++;
379                         }
380                 }
381         }
382         /* If there are none at all that we recognise, bail */
383         if (!this->numchips) {
384                 printk("No flash chips recognised.\n");
385                 return;
386         }
387
388         /* Allocate an array to hold the information for each chip */
389         this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
390         if (!this->chips){
391                 printk("MTD: No memory for allocating chip info structures\n");
392                 return;
393         }
394
395         /* Fill out the chip array with {floor, chipno} for each
396          * detected chip in the device. */
397         for (floor = 0, ret = 0; floor < MAX_FLOORS_MPLUS; floor++) {
398                 for (chip = 0 ; chip < numchips[floor] ; chip++) {
399                         this->chips[ret].floor = floor;
400                         this->chips[ret].chip = chip;
401                         this->chips[ret].curadr = 0;
402                         this->chips[ret].curmode = 0x50;
403                         ret++;
404                 }
405         }
406
407         /* Calculate and print the total size of the device */
408         this->totlen = this->numchips * (1 << this->chipshift);
409         printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
410                this->numchips ,this->totlen >> 20);
411 }
412
413 static int DoCMilPlus_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
414 {
415         int tmp1, tmp2, retval;
416
417         if (doc1->physadr == doc2->physadr)
418                 return 1;
419
420         /* Use the alias resolution register which was set aside for this
421          * purpose. If it's value is the same on both chips, they might
422          * be the same chip, and we write to one and check for a change in
423          * the other. It's unclear if this register is usuable in the
424          * DoC 2000 (it's in the Millennium docs), but it seems to work. */
425         tmp1 = ReadDOC(doc1->virtadr, Mplus_AliasResolution);
426         tmp2 = ReadDOC(doc2->virtadr, Mplus_AliasResolution);
427         if (tmp1 != tmp2)
428                 return 0;
429
430         WriteDOC((tmp1+1) % 0xff, doc1->virtadr, Mplus_AliasResolution);
431         tmp2 = ReadDOC(doc2->virtadr, Mplus_AliasResolution);
432         if (tmp2 == (tmp1+1) % 0xff)
433                 retval = 1;
434         else
435                 retval = 0;
436
437         /* Restore register contents.  May not be necessary, but do it just to
438          * be safe. */
439         WriteDOC(tmp1, doc1->virtadr, Mplus_AliasResolution);
440
441         return retval;
442 }
443
444 /* This routine is found from the docprobe code by symbol_get(),
445  * which will bump the use count of this module. */
446 void DoCMilPlus_init(struct mtd_info *mtd)
447 {
448         struct DiskOnChip *this = mtd->priv;
449         struct DiskOnChip *old = NULL;
450
451         /* We must avoid being called twice for the same device. */
452         if (docmilpluslist)
453                 old = docmilpluslist->priv;
454
455         while (old) {
456                 if (DoCMilPlus_is_alias(this, old)) {
457                         printk(KERN_NOTICE "Ignoring DiskOnChip Millennium "
458                                 "Plus at 0x%lX - already configured\n",
459                                 this->physadr);
460                         iounmap(this->virtadr);
461                         kfree(mtd);
462                         return;
463                 }
464                 if (old->nextdoc)
465                         old = old->nextdoc->priv;
466                 else
467                         old = NULL;
468         }
469
470         mtd->name = "DiskOnChip Millennium Plus";
471         printk(KERN_NOTICE "DiskOnChip Millennium Plus found at "
472                 "address 0x%lX\n", this->physadr);
473
474         mtd->type = MTD_NANDFLASH;
475         mtd->flags = MTD_CAP_NANDFLASH;
476         mtd->size = 0;
477
478         mtd->erasesize = 0;
479         mtd->writesize = 512;
480         mtd->oobsize = 16;
481         mtd->owner = THIS_MODULE;
482         mtd->erase = doc_erase;
483         mtd->point = NULL;
484         mtd->unpoint = NULL;
485         mtd->read = doc_read;
486         mtd->write = doc_write;
487         mtd->read_oob = doc_read_oob;
488         mtd->write_oob = doc_write_oob;
489         mtd->sync = NULL;
490
491         this->totlen = 0;
492         this->numchips = 0;
493         this->curfloor = -1;
494         this->curchip = -1;
495
496         /* Ident all the chips present. */
497         DoC_ScanChips(this);
498
499         if (!this->totlen) {
500                 kfree(mtd);
501                 iounmap(this->virtadr);
502         } else {
503                 this->nextdoc = docmilpluslist;
504                 docmilpluslist = mtd;
505                 mtd->size  = this->totlen;
506                 mtd->erasesize = this->erasesize;
507                 add_mtd_device(mtd);
508                 return;
509         }
510 }
511 EXPORT_SYMBOL_GPL(DoCMilPlus_init);
512
513 #if 0
514 static int doc_dumpblk(struct mtd_info *mtd, loff_t from)
515 {
516         int i;
517         loff_t fofs;
518         struct DiskOnChip *this = mtd->priv;
519         void __iomem * docptr = this->virtadr;
520         struct Nand *mychip = &this->chips[from >> (this->chipshift)];
521         unsigned char *bp, buf[1056];
522         char c[32];
523
524         from &= ~0x3ff;
525
526         /* Don't allow read past end of device */
527         if (from >= this->totlen)
528                 return -EINVAL;
529
530         DoC_CheckASIC(docptr);
531
532         /* Find the chip which is to be used and select it */
533         if (this->curfloor != mychip->floor) {
534                 DoC_SelectFloor(docptr, mychip->floor);
535                 DoC_SelectChip(docptr, mychip->chip);
536         } else if (this->curchip != mychip->chip) {
537                 DoC_SelectChip(docptr, mychip->chip);
538         }
539         this->curfloor = mychip->floor;
540         this->curchip = mychip->chip;
541
542         /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
543         WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
544
545         /* Reset the chip, see Software Requirement 11.4 item 1. */
546         DoC_Command(docptr, NAND_CMD_RESET, 0);
547         DoC_WaitReady(docptr);
548
549         fofs = from;
550         DoC_Command(docptr, DoC_GetDataOffset(mtd, &fofs), 0);
551         DoC_Address(this, 3, fofs, 0, 0x00);
552         WriteDOC(0, docptr, Mplus_FlashControl);
553         DoC_WaitReady(docptr);
554
555         /* disable the ECC engine */
556         WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
557
558         ReadDOC(docptr, Mplus_ReadPipeInit);
559         ReadDOC(docptr, Mplus_ReadPipeInit);
560
561         /* Read the data via the internal pipeline through CDSN IO
562            register, see Pipelined Read Operations 11.3 */
563         MemReadDOC(docptr, buf, 1054);
564         buf[1054] = ReadDOC(docptr, Mplus_LastDataRead);
565         buf[1055] = ReadDOC(docptr, Mplus_LastDataRead);
566
567         memset(&c[0], 0, sizeof(c));
568         printk("DUMP OFFSET=%x:\n", (int)from);
569
570         for (i = 0, bp = &buf[0]; (i < 1056); i++) {
571                 if ((i % 16) == 0)
572                         printk("%08x: ", i);
573                 printk(" %02x", *bp);
574                 c[(i & 0xf)] = ((*bp >= 0x20) && (*bp <= 0x7f)) ? *bp : '.';
575                 bp++;
576                 if (((i + 1) % 16) == 0)
577                         printk("    %s\n", c);
578         }
579         printk("\n");
580
581         /* Disable flash internally */
582         WriteDOC(0, docptr, Mplus_FlashSelect);
583
584         return 0;
585 }
586 #endif
587
588 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
589                     size_t *retlen, u_char *buf)
590 {
591         int ret, i;
592         volatile char dummy;
593         loff_t fofs;
594         unsigned char syndrome[6], eccbuf[6];
595         struct DiskOnChip *this = mtd->priv;
596         void __iomem * docptr = this->virtadr;
597         struct Nand *mychip = &this->chips[from >> (this->chipshift)];
598
599         /* Don't allow read past end of device */
600         if (from >= this->totlen)
601                 return -EINVAL;
602
603         /* Don't allow a single read to cross a 512-byte block boundary */
604         if (from + len > ((from | 0x1ff) + 1))
605                 len = ((from | 0x1ff) + 1) - from;
606
607         DoC_CheckASIC(docptr);
608
609         /* Find the chip which is to be used and select it */
610         if (this->curfloor != mychip->floor) {
611                 DoC_SelectFloor(docptr, mychip->floor);
612                 DoC_SelectChip(docptr, mychip->chip);
613         } else if (this->curchip != mychip->chip) {
614                 DoC_SelectChip(docptr, mychip->chip);
615         }
616         this->curfloor = mychip->floor;
617         this->curchip = mychip->chip;
618
619         /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
620         WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
621
622         /* Reset the chip, see Software Requirement 11.4 item 1. */
623         DoC_Command(docptr, NAND_CMD_RESET, 0);
624         DoC_WaitReady(docptr);
625
626         fofs = from;
627         DoC_Command(docptr, DoC_GetDataOffset(mtd, &fofs), 0);
628         DoC_Address(this, 3, fofs, 0, 0x00);
629         WriteDOC(0, docptr, Mplus_FlashControl);
630         DoC_WaitReady(docptr);
631
632         /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
633         WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
634         WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
635
636         /* Let the caller know we completed it */
637         *retlen = len;
638         ret = 0;
639
640         ReadDOC(docptr, Mplus_ReadPipeInit);
641         ReadDOC(docptr, Mplus_ReadPipeInit);
642
643         /* Read the data via the internal pipeline through CDSN IO
644            register, see Pipelined Read Operations 11.3 */
645         MemReadDOC(docptr, buf, len);
646
647         /* Read the ECC data following raw data */
648         MemReadDOC(docptr, eccbuf, 4);
649         eccbuf[4] = ReadDOC(docptr, Mplus_LastDataRead);
650         eccbuf[5] = ReadDOC(docptr, Mplus_LastDataRead);
651
652         /* Flush the pipeline */
653         dummy = ReadDOC(docptr, Mplus_ECCConf);
654         dummy = ReadDOC(docptr, Mplus_ECCConf);
655
656         /* Check the ECC Status */
657         if (ReadDOC(docptr, Mplus_ECCConf) & 0x80) {
658                 int nb_errors;
659                 /* There was an ECC error */
660 #ifdef ECC_DEBUG
661                 printk("DiskOnChip ECC Error: Read at %lx\n", (long)from);
662 #endif
663                 /* Read the ECC syndrom through the DiskOnChip ECC logic.
664                    These syndrome will be all ZERO when there is no error */
665                 for (i = 0; i < 6; i++)
666                         syndrome[i] = ReadDOC(docptr, Mplus_ECCSyndrome0 + i);
667
668                 nb_errors = doc_decode_ecc(buf, syndrome);
669 #ifdef ECC_DEBUG
670                 printk("ECC Errors corrected: %x\n", nb_errors);
671 #endif
672                 if (nb_errors < 0) {
673                         /* We return error, but have actually done the
674                            read. Not that this can be told to user-space, via
675                            sys_read(), but at least MTD-aware stuff can know
676                            about it by checking *retlen */
677 #ifdef ECC_DEBUG
678                         printk("%s(%d): Millennium Plus ECC error (from=0x%x:\n",
679                                 __FILE__, __LINE__, (int)from);
680                         printk("        syndrome= %02x:%02x:%02x:%02x:%02x:"
681                                 "%02x\n",
682                                 syndrome[0], syndrome[1], syndrome[2],
683                                 syndrome[3], syndrome[4], syndrome[5]);
684                         printk("          eccbuf= %02x:%02x:%02x:%02x:%02x:"
685                                 "%02x\n",
686                                 eccbuf[0], eccbuf[1], eccbuf[2],
687                                 eccbuf[3], eccbuf[4], eccbuf[5]);
688 #endif
689                                 ret = -EIO;
690                 }
691         }
692
693 #ifdef PSYCHO_DEBUG
694         printk("ECC DATA at %lx: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
695                (long)from, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
696                eccbuf[4], eccbuf[5]);
697 #endif
698         /* disable the ECC engine */
699         WriteDOC(DOC_ECC_DIS, docptr , Mplus_ECCConf);
700
701         /* Disable flash internally */
702         WriteDOC(0, docptr, Mplus_FlashSelect);
703
704         return ret;
705 }
706
707 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
708                      size_t *retlen, const u_char *buf)
709 {
710         int i, before, ret = 0;
711         loff_t fto;
712         volatile char dummy;
713         char eccbuf[6];
714         struct DiskOnChip *this = mtd->priv;
715         void __iomem * docptr = this->virtadr;
716         struct Nand *mychip = &this->chips[to >> (this->chipshift)];
717
718         /* Don't allow write past end of device */
719         if (to >= this->totlen)
720                 return -EINVAL;
721
722         /* Don't allow writes which aren't exactly one block (512 bytes) */
723         if ((to & 0x1ff) || (len != 0x200))
724                 return -EINVAL;
725
726         /* Determine position of OOB flags, before or after data */
727         before = (this->interleave && (to & 0x200));
728
729         DoC_CheckASIC(docptr);
730
731         /* Find the chip which is to be used and select it */
732         if (this->curfloor != mychip->floor) {
733                 DoC_SelectFloor(docptr, mychip->floor);
734                 DoC_SelectChip(docptr, mychip->chip);
735         } else if (this->curchip != mychip->chip) {
736                 DoC_SelectChip(docptr, mychip->chip);
737         }
738         this->curfloor = mychip->floor;
739         this->curchip = mychip->chip;
740
741         /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
742         WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
743
744         /* Reset the chip, see Software Requirement 11.4 item 1. */
745         DoC_Command(docptr, NAND_CMD_RESET, 0);
746         DoC_WaitReady(docptr);
747
748         /* Set device to appropriate plane of flash */
749         fto = to;
750         WriteDOC(DoC_GetDataOffset(mtd, &fto), docptr, Mplus_FlashCmd);
751
752         /* On interleaved devices the flags for 2nd half 512 are before data */
753         if (eccbuf && before)
754                 fto -= 2;
755
756         /* issue the Serial Data In command to initial the Page Program process */
757         DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
758         DoC_Address(this, 3, fto, 0x00, 0x00);
759
760         /* Disable the ECC engine */
761         WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
762
763         if (before) {
764                 /* Write the block status BLOCK_USED (0x5555) */
765                 WriteDOC(0x55, docptr, Mil_CDSN_IO);
766                 WriteDOC(0x55, docptr, Mil_CDSN_IO);
767         }
768
769         /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
770         WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
771
772         MemWriteDOC(docptr, (unsigned char *) buf, len);
773
774         /* Write ECC data to flash, the ECC info is generated by
775            the DiskOnChip ECC logic see Reed-Solomon EDC/ECC 11.1 */
776         DoC_Delay(docptr, 3);
777
778         /* Read the ECC data through the DiskOnChip ECC logic */
779         for (i = 0; i < 6; i++)
780                 eccbuf[i] = ReadDOC(docptr, Mplus_ECCSyndrome0 + i);
781
782         /* disable the ECC engine */
783         WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
784
785         /* Write the ECC data to flash */
786         MemWriteDOC(docptr, eccbuf, 6);
787
788         if (!before) {
789                 /* Write the block status BLOCK_USED (0x5555) */
790                 WriteDOC(0x55, docptr, Mil_CDSN_IO+6);
791                 WriteDOC(0x55, docptr, Mil_CDSN_IO+7);
792         }
793
794 #ifdef PSYCHO_DEBUG
795         printk("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
796                (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
797                eccbuf[4], eccbuf[5]);
798 #endif
799
800         WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
801         WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
802
803         /* Commit the Page Program command and wait for ready
804            see Software Requirement 11.4 item 1.*/
805         DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
806         DoC_WaitReady(docptr);
807
808         /* Read the status of the flash device through CDSN IO register
809            see Software Requirement 11.4 item 5.*/
810         DoC_Command(docptr, NAND_CMD_STATUS, 0);
811         dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
812         dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
813         DoC_Delay(docptr, 2);
814         if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
815                 printk("MTD: Error 0x%x programming at 0x%x\n", dummy, (int)to);
816                 /* Error in programming
817                    FIXME: implement Bad Block Replacement (in nftl.c ??) */
818                 *retlen = 0;
819                 ret = -EIO;
820         }
821         dummy = ReadDOC(docptr, Mplus_LastDataRead);
822
823         /* Disable flash internally */
824         WriteDOC(0, docptr, Mplus_FlashSelect);
825
826         /* Let the caller know we completed it */
827         *retlen = len;
828
829         return ret;
830 }
831
832 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
833                         struct mtd_oob_ops *ops)
834 {
835         loff_t fofs, base;
836         struct DiskOnChip *this = mtd->priv;
837         void __iomem * docptr = this->virtadr;
838         struct Nand *mychip = &this->chips[ofs >> this->chipshift];
839         size_t i, size, got, want;
840         uint8_t *buf = ops->oobbuf;
841         size_t len = ops->len;
842
843         BUG_ON(ops->mode != MTD_OOB_PLACE);
844
845         ofs += ops->ooboffs;
846
847         DoC_CheckASIC(docptr);
848
849         /* Find the chip which is to be used and select it */
850         if (this->curfloor != mychip->floor) {
851                 DoC_SelectFloor(docptr, mychip->floor);
852                 DoC_SelectChip(docptr, mychip->chip);
853         } else if (this->curchip != mychip->chip) {
854                 DoC_SelectChip(docptr, mychip->chip);
855         }
856         this->curfloor = mychip->floor;
857         this->curchip = mychip->chip;
858
859         /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
860         WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
861
862         /* disable the ECC engine */
863         WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
864         DoC_WaitReady(docptr);
865
866         /* Maximum of 16 bytes in the OOB region, so limit read to that */
867         if (len > 16)
868                 len = 16;
869         got = 0;
870         want = len;
871
872         for (i = 0; ((i < 3) && (want > 0)); i++) {
873                 /* Figure out which region we are accessing... */
874                 fofs = ofs;
875                 base = ofs & 0xf;
876                 if (!this->interleave) {
877                         DoC_Command(docptr, NAND_CMD_READOOB, 0);
878                         size = 16 - base;
879                 } else if (base < 6) {
880                         DoC_Command(docptr, DoC_GetECCOffset(mtd, &fofs), 0);
881                         size = 6 - base;
882                 } else if (base < 8) {
883                         DoC_Command(docptr, DoC_GetFlagsOffset(mtd, &fofs), 0);
884                         size = 8 - base;
885                 } else {
886                         DoC_Command(docptr, DoC_GetHdrOffset(mtd, &fofs), 0);
887                         size = 16 - base;
888                 }
889                 if (size > want)
890                         size = want;
891
892                 /* Issue read command */
893                 DoC_Address(this, 3, fofs, 0, 0x00);
894                 WriteDOC(0, docptr, Mplus_FlashControl);
895                 DoC_WaitReady(docptr);
896
897                 ReadDOC(docptr, Mplus_ReadPipeInit);
898                 ReadDOC(docptr, Mplus_ReadPipeInit);
899                 MemReadDOC(docptr, &buf[got], size - 2);
900                 buf[got + size - 2] = ReadDOC(docptr, Mplus_LastDataRead);
901                 buf[got + size - 1] = ReadDOC(docptr, Mplus_LastDataRead);
902
903                 ofs += size;
904                 got += size;
905                 want -= size;
906         }
907
908         /* Disable flash internally */
909         WriteDOC(0, docptr, Mplus_FlashSelect);
910
911         ops->retlen = len;
912         return 0;
913 }
914
915 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
916                          struct mtd_oob_ops *ops)
917 {
918         volatile char dummy;
919         loff_t fofs, base;
920         struct DiskOnChip *this = mtd->priv;
921         void __iomem * docptr = this->virtadr;
922         struct Nand *mychip = &this->chips[ofs >> this->chipshift];
923         size_t i, size, got, want;
924         int ret = 0;
925         uint8_t *buf = ops->oobbuf;
926         size_t len = ops->len;
927
928         BUG_ON(ops->mode != MTD_OOB_PLACE);
929
930         ofs += ops->ooboffs;
931
932         DoC_CheckASIC(docptr);
933
934         /* Find the chip which is to be used and select it */
935         if (this->curfloor != mychip->floor) {
936                 DoC_SelectFloor(docptr, mychip->floor);
937                 DoC_SelectChip(docptr, mychip->chip);
938         } else if (this->curchip != mychip->chip) {
939                 DoC_SelectChip(docptr, mychip->chip);
940         }
941         this->curfloor = mychip->floor;
942         this->curchip = mychip->chip;
943
944         /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
945         WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
946
947
948         /* Maximum of 16 bytes in the OOB region, so limit write to that */
949         if (len > 16)
950                 len = 16;
951         got = 0;
952         want = len;
953
954         for (i = 0; ((i < 3) && (want > 0)); i++) {
955                 /* Reset the chip, see Software Requirement 11.4 item 1. */
956                 DoC_Command(docptr, NAND_CMD_RESET, 0);
957                 DoC_WaitReady(docptr);
958
959                 /* Figure out which region we are accessing... */
960                 fofs = ofs;
961                 base = ofs & 0x0f;
962                 if (!this->interleave) {
963                         WriteDOC(NAND_CMD_READOOB, docptr, Mplus_FlashCmd);
964                         size = 16 - base;
965                 } else if (base < 6) {
966                         WriteDOC(DoC_GetECCOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
967                         size = 6 - base;
968                 } else if (base < 8) {
969                         WriteDOC(DoC_GetFlagsOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
970                         size = 8 - base;
971                 } else {
972                         WriteDOC(DoC_GetHdrOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
973                         size = 16 - base;
974                 }
975                 if (size > want)
976                         size = want;
977
978                 /* Issue the Serial Data In command to initial the Page Program process */
979                 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
980                 DoC_Address(this, 3, fofs, 0, 0x00);
981
982                 /* Disable the ECC engine */
983                 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
984
985                 /* Write the data via the internal pipeline through CDSN IO
986                    register, see Pipelined Write Operations 11.2 */
987                 MemWriteDOC(docptr, (unsigned char *) &buf[got], size);
988                 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
989                 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
990
991                 /* Commit the Page Program command and wait for ready
992                    see Software Requirement 11.4 item 1.*/
993                 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
994                 DoC_WaitReady(docptr);
995
996                 /* Read the status of the flash device through CDSN IO register
997                    see Software Requirement 11.4 item 5.*/
998                 DoC_Command(docptr, NAND_CMD_STATUS, 0x00);
999                 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1000                 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1001                 DoC_Delay(docptr, 2);
1002                 if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
1003                         printk("MTD: Error 0x%x programming oob at 0x%x\n",
1004                                 dummy, (int)ofs);
1005                         /* FIXME: implement Bad Block Replacement */
1006                         ops->retlen = 0;
1007                         ret = -EIO;
1008                 }
1009                 dummy = ReadDOC(docptr, Mplus_LastDataRead);
1010
1011                 ofs += size;
1012                 got += size;
1013                 want -= size;
1014         }
1015
1016         /* Disable flash internally */
1017         WriteDOC(0, docptr, Mplus_FlashSelect);
1018
1019         ops->retlen = len;
1020         return ret;
1021 }
1022
1023 int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
1024 {
1025         volatile char dummy;
1026         struct DiskOnChip *this = mtd->priv;
1027         __u32 ofs = instr->addr;
1028         __u32 len = instr->len;
1029         void __iomem * docptr = this->virtadr;
1030         struct Nand *mychip = &this->chips[ofs >> this->chipshift];
1031
1032         DoC_CheckASIC(docptr);
1033
1034         if (len != mtd->erasesize)
1035                 printk(KERN_WARNING "MTD: Erase not right size (%x != %x)n",
1036                        len, mtd->erasesize);
1037
1038         /* Find the chip which is to be used and select it */
1039         if (this->curfloor != mychip->floor) {
1040                 DoC_SelectFloor(docptr, mychip->floor);
1041                 DoC_SelectChip(docptr, mychip->chip);
1042         } else if (this->curchip != mychip->chip) {
1043                 DoC_SelectChip(docptr, mychip->chip);
1044         }
1045         this->curfloor = mychip->floor;
1046         this->curchip = mychip->chip;
1047
1048         instr->state = MTD_ERASE_PENDING;
1049
1050         /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
1051         WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
1052
1053         DoC_Command(docptr, NAND_CMD_RESET, 0x00);
1054         DoC_WaitReady(docptr);
1055
1056         DoC_Command(docptr, NAND_CMD_ERASE1, 0);
1057         DoC_Address(this, 2, ofs, 0, 0x00);
1058         DoC_Command(docptr, NAND_CMD_ERASE2, 0);
1059         DoC_WaitReady(docptr);
1060         instr->state = MTD_ERASING;
1061
1062         /* Read the status of the flash device through CDSN IO register
1063            see Software Requirement 11.4 item 5. */
1064         DoC_Command(docptr, NAND_CMD_STATUS, 0);
1065         dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1066         dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1067         if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
1068                 printk("MTD: Error 0x%x erasing at 0x%x\n", dummy, ofs);
1069                 /* FIXME: implement Bad Block Replacement (in nftl.c ??) */
1070                 instr->state = MTD_ERASE_FAILED;
1071         } else {
1072                 instr->state = MTD_ERASE_DONE;
1073         }
1074         dummy = ReadDOC(docptr, Mplus_LastDataRead);
1075
1076         /* Disable flash internally */
1077         WriteDOC(0, docptr, Mplus_FlashSelect);
1078
1079         mtd_erase_callback(instr);
1080
1081         return 0;
1082 }
1083
1084 /****************************************************************************
1085  *
1086  * Module stuff
1087  *
1088  ****************************************************************************/
1089
1090 static void __exit cleanup_doc2001plus(void)
1091 {
1092         struct mtd_info *mtd;
1093         struct DiskOnChip *this;
1094
1095         while ((mtd=docmilpluslist)) {
1096                 this = mtd->priv;
1097                 docmilpluslist = this->nextdoc;
1098
1099                 del_mtd_device(mtd);
1100
1101                 iounmap(this->virtadr);
1102                 kfree(this->chips);
1103                 kfree(mtd);
1104         }
1105 }
1106
1107 module_exit(cleanup_doc2001plus);
1108
1109 MODULE_LICENSE("GPL");
1110 MODULE_AUTHOR("Greg Ungerer <gerg@snapgear.com> et al.");
1111 MODULE_DESCRIPTION("Driver for DiskOnChip Millennium Plus");