Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ericvh...
[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/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->ecctype = MTD_ECC_RS_DiskOnChip;
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");