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