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