Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / drivers / mtd / devices / doc2000.c
1
2 /*
3  * Linux driver for Disk-On-Chip 2000 and Millennium
4  * (c) 1999 Machine Vision Holdings, Inc.
5  * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
6  *
7  * $Id: doc2000.c,v 1.67 2005/11/07 11:14:24 gleixner Exp $
8  */
9
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <asm/errno.h>
13 #include <asm/io.h>
14 #include <asm/uaccess.h>
15 #include <linux/miscdevice.h>
16 #include <linux/pci.h>
17 #include <linux/delay.h>
18 #include <linux/slab.h>
19 #include <linux/sched.h>
20 #include <linux/init.h>
21 #include <linux/types.h>
22 #include <linux/bitops.h>
23 #include <linux/mutex.h>
24
25 #include <linux/mtd/mtd.h>
26 #include <linux/mtd/nand.h>
27 #include <linux/mtd/doc2000.h>
28
29 #define DOC_SUPPORT_2000
30 #define DOC_SUPPORT_2000TSOP
31 #define DOC_SUPPORT_MILLENNIUM
32
33 #ifdef DOC_SUPPORT_2000
34 #define DoC_is_2000(doc) (doc->ChipID == DOC_ChipID_Doc2k)
35 #else
36 #define DoC_is_2000(doc) (0)
37 #endif
38
39 #if defined(DOC_SUPPORT_2000TSOP) || defined(DOC_SUPPORT_MILLENNIUM)
40 #define DoC_is_Millennium(doc) (doc->ChipID == DOC_ChipID_DocMil)
41 #else
42 #define DoC_is_Millennium(doc) (0)
43 #endif
44
45 /* #define ECC_DEBUG */
46
47 /* I have no idea why some DoC chips can not use memcpy_from|to_io().
48  * This may be due to the different revisions of the ASIC controller built-in or
49  * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
50  * this:
51  #undef USE_MEMCPY
52 */
53
54 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
55                     size_t *retlen, u_char *buf);
56 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
57                      size_t *retlen, const u_char *buf);
58 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
59                         struct mtd_oob_ops *ops);
60 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
61                          struct mtd_oob_ops *ops);
62 static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
63                          size_t *retlen, const u_char *buf);
64 static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
65
66 static struct mtd_info *doc2klist = NULL;
67
68 /* Perform the required delay cycles by reading from the appropriate register */
69 static void DoC_Delay(struct DiskOnChip *doc, unsigned short cycles)
70 {
71         volatile char dummy;
72         int i;
73
74         for (i = 0; i < cycles; i++) {
75                 if (DoC_is_Millennium(doc))
76                         dummy = ReadDOC(doc->virtadr, NOP);
77                 else
78                         dummy = ReadDOC(doc->virtadr, DOCStatus);
79         }
80
81 }
82
83 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
84 static int _DoC_WaitReady(struct DiskOnChip *doc)
85 {
86         void __iomem *docptr = doc->virtadr;
87         unsigned long timeo = jiffies + (HZ * 10);
88
89         DEBUG(MTD_DEBUG_LEVEL3,
90               "_DoC_WaitReady called for out-of-line wait\n");
91
92         /* Out-of-line routine to wait for chip response */
93         while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
94                 /* issue 2 read from NOP register after reading from CDSNControl register
95                 see Software Requirement 11.4 item 2. */
96                 DoC_Delay(doc, 2);
97
98                 if (time_after(jiffies, timeo)) {
99                         DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
100                         return -EIO;
101                 }
102                 udelay(1);
103                 cond_resched();
104         }
105
106         return 0;
107 }
108
109 static inline int DoC_WaitReady(struct DiskOnChip *doc)
110 {
111         void __iomem *docptr = doc->virtadr;
112
113         /* This is inline, to optimise the common case, where it's ready instantly */
114         int ret = 0;
115
116         /* 4 read form NOP register should be issued in prior to the read from CDSNControl
117            see Software Requirement 11.4 item 2. */
118         DoC_Delay(doc, 4);
119
120         if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
121                 /* Call the out-of-line routine to wait */
122                 ret = _DoC_WaitReady(doc);
123
124         /* issue 2 read from NOP register after reading from CDSNControl register
125            see Software Requirement 11.4 item 2. */
126         DoC_Delay(doc, 2);
127
128         return ret;
129 }
130
131 /* DoC_Command: Send a flash command to the flash chip through the CDSN Slow IO register to
132    bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
133    required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
134
135 static int DoC_Command(struct DiskOnChip *doc, unsigned char command,
136                               unsigned char xtraflags)
137 {
138         void __iomem *docptr = doc->virtadr;
139
140         if (DoC_is_2000(doc))
141                 xtraflags |= CDSN_CTRL_FLASH_IO;
142
143         /* Assert the CLE (Command Latch Enable) line to the flash chip */
144         WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
145         DoC_Delay(doc, 4);      /* Software requirement 11.4.3 for Millennium */
146
147         if (DoC_is_Millennium(doc))
148                 WriteDOC(command, docptr, CDSNSlowIO);
149
150         /* Send the command */
151         WriteDOC_(command, docptr, doc->ioreg);
152         if (DoC_is_Millennium(doc))
153                 WriteDOC(command, docptr, WritePipeTerm);
154
155         /* Lower the CLE line */
156         WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
157         DoC_Delay(doc, 4);      /* Software requirement 11.4.3 for Millennium */
158
159         /* Wait for the chip to respond - Software requirement 11.4.1 (extended for any command) */
160         return DoC_WaitReady(doc);
161 }
162
163 /* DoC_Address: Set the current address for the flash chip through the CDSN Slow IO register to
164    bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
165    required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
166
167 static int DoC_Address(struct DiskOnChip *doc, int numbytes, unsigned long ofs,
168                        unsigned char xtraflags1, unsigned char xtraflags2)
169 {
170         int i;
171         void __iomem *docptr = doc->virtadr;
172
173         if (DoC_is_2000(doc))
174                 xtraflags1 |= CDSN_CTRL_FLASH_IO;
175
176         /* Assert the ALE (Address Latch Enable) line to the flash chip */
177         WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
178
179         DoC_Delay(doc, 4);      /* Software requirement 11.4.3 for Millennium */
180
181         /* Send the address */
182         /* Devices with 256-byte page are addressed as:
183            Column (bits 0-7), Page (bits 8-15, 16-23, 24-31)
184            * there is no device on the market with page256
185            and more than 24 bits.
186            Devices with 512-byte page are addressed as:
187            Column (bits 0-7), Page (bits 9-16, 17-24, 25-31)
188            * 25-31 is sent only if the chip support it.
189            * bit 8 changes the read command to be sent
190            (NAND_CMD_READ0 or NAND_CMD_READ1).
191          */
192
193         if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE) {
194                 if (DoC_is_Millennium(doc))
195                         WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
196                 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
197         }
198
199         if (doc->page256) {
200                 ofs = ofs >> 8;
201         } else {
202                 ofs = ofs >> 9;
203         }
204
205         if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE) {
206                 for (i = 0; i < doc->pageadrlen; i++, ofs = ofs >> 8) {
207                         if (DoC_is_Millennium(doc))
208                                 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
209                         WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
210                 }
211         }
212
213         if (DoC_is_Millennium(doc))
214                 WriteDOC(ofs & 0xff, docptr, WritePipeTerm);
215
216         DoC_Delay(doc, 2);      /* Needed for some slow flash chips. mf. */
217
218         /* FIXME: The SlowIO's for millennium could be replaced by
219            a single WritePipeTerm here. mf. */
220
221         /* Lower the ALE line */
222         WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr,
223                  CDSNControl);
224
225         DoC_Delay(doc, 4);      /* Software requirement 11.4.3 for Millennium */
226
227         /* Wait for the chip to respond - Software requirement 11.4.1 */
228         return DoC_WaitReady(doc);
229 }
230
231 /* Read a buffer from DoC, taking care of Millennium odditys */
232 static void DoC_ReadBuf(struct DiskOnChip *doc, u_char * buf, int len)
233 {
234         volatile int dummy;
235         int modulus = 0xffff;
236         void __iomem *docptr = doc->virtadr;
237         int i;
238
239         if (len <= 0)
240                 return;
241
242         if (DoC_is_Millennium(doc)) {
243                 /* Read the data via the internal pipeline through CDSN IO register,
244                    see Pipelined Read Operations 11.3 */
245                 dummy = ReadDOC(docptr, ReadPipeInit);
246
247                 /* Millennium should use the LastDataRead register - Pipeline Reads */
248                 len--;
249
250                 /* This is needed for correctly ECC calculation */
251                 modulus = 0xff;
252         }
253
254         for (i = 0; i < len; i++)
255                 buf[i] = ReadDOC_(docptr, doc->ioreg + (i & modulus));
256
257         if (DoC_is_Millennium(doc)) {
258                 buf[i] = ReadDOC(docptr, LastDataRead);
259         }
260 }
261
262 /* Write a buffer to DoC, taking care of Millennium odditys */
263 static void DoC_WriteBuf(struct DiskOnChip *doc, const u_char * buf, int len)
264 {
265         void __iomem *docptr = doc->virtadr;
266         int i;
267
268         if (len <= 0)
269                 return;
270
271         for (i = 0; i < len; i++)
272                 WriteDOC_(buf[i], docptr, doc->ioreg + i);
273
274         if (DoC_is_Millennium(doc)) {
275                 WriteDOC(0x00, docptr, WritePipeTerm);
276         }
277 }
278
279
280 /* DoC_SelectChip: Select a given flash chip within the current floor */
281
282 static inline int DoC_SelectChip(struct DiskOnChip *doc, int chip)
283 {
284         void __iomem *docptr = doc->virtadr;
285
286         /* Software requirement 11.4.4 before writing DeviceSelect */
287         /* Deassert the CE line to eliminate glitches on the FCE# outputs */
288         WriteDOC(CDSN_CTRL_WP, docptr, CDSNControl);
289         DoC_Delay(doc, 4);      /* Software requirement 11.4.3 for Millennium */
290
291         /* Select the individual flash chip requested */
292         WriteDOC(chip, docptr, CDSNDeviceSelect);
293         DoC_Delay(doc, 4);
294
295         /* Reassert the CE line */
296         WriteDOC(CDSN_CTRL_CE | CDSN_CTRL_FLASH_IO | CDSN_CTRL_WP, docptr,
297                  CDSNControl);
298         DoC_Delay(doc, 4);      /* Software requirement 11.4.3 for Millennium */
299
300         /* Wait for it to be ready */
301         return DoC_WaitReady(doc);
302 }
303
304 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
305
306 static inline int DoC_SelectFloor(struct DiskOnChip *doc, int floor)
307 {
308         void __iomem *docptr = doc->virtadr;
309
310         /* Select the floor (bank) of chips required */
311         WriteDOC(floor, docptr, FloorSelect);
312
313         /* Wait for the chip to be ready */
314         return DoC_WaitReady(doc);
315 }
316
317 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
318
319 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
320 {
321         int mfr, id, i, j;
322         volatile char dummy;
323
324         /* Page in the required floor/chip */
325         DoC_SelectFloor(doc, floor);
326         DoC_SelectChip(doc, chip);
327
328         /* Reset the chip */
329         if (DoC_Command(doc, NAND_CMD_RESET, CDSN_CTRL_WP)) {
330                 DEBUG(MTD_DEBUG_LEVEL2,
331                       "DoC_Command (reset) for %d,%d returned true\n",
332                       floor, chip);
333                 return 0;
334         }
335
336
337         /* Read the NAND chip ID: 1. Send ReadID command */
338         if (DoC_Command(doc, NAND_CMD_READID, CDSN_CTRL_WP)) {
339                 DEBUG(MTD_DEBUG_LEVEL2,
340                       "DoC_Command (ReadID) for %d,%d returned true\n",
341                       floor, chip);
342                 return 0;
343         }
344
345         /* Read the NAND chip ID: 2. Send address byte zero */
346         DoC_Address(doc, ADDR_COLUMN, 0, CDSN_CTRL_WP, 0);
347
348         /* Read the manufacturer and device id codes from the device */
349
350         if (DoC_is_Millennium(doc)) {
351                 DoC_Delay(doc, 2);
352                 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
353                 mfr = ReadDOC(doc->virtadr, LastDataRead);
354
355                 DoC_Delay(doc, 2);
356                 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
357                 id = ReadDOC(doc->virtadr, LastDataRead);
358         } else {
359                 /* CDSN Slow IO register see Software Req 11.4 item 5. */
360                 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
361                 DoC_Delay(doc, 2);
362                 mfr = ReadDOC_(doc->virtadr, doc->ioreg);
363
364                 /* CDSN Slow IO register see Software Req 11.4 item 5. */
365                 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
366                 DoC_Delay(doc, 2);
367                 id = ReadDOC_(doc->virtadr, doc->ioreg);
368         }
369
370         /* No response - return failure */
371         if (mfr == 0xff || mfr == 0)
372                 return 0;
373
374         /* Check it's the same as the first chip we identified.
375          * M-Systems say that any given DiskOnChip device should only
376          * contain _one_ type of flash part, although that's not a
377          * hardware restriction. */
378         if (doc->mfr) {
379                 if (doc->mfr == mfr && doc->id == id)
380                         return 1;       /* This is another the same the first */
381                 else
382                         printk(KERN_WARNING
383                                "Flash chip at floor %d, chip %d is different:\n",
384                                floor, chip);
385         }
386
387         /* Print and store the manufacturer and ID codes. */
388         for (i = 0; nand_flash_ids[i].name != NULL; i++) {
389                 if (id == nand_flash_ids[i].id) {
390                         /* Try to identify manufacturer */
391                         for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
392                                 if (nand_manuf_ids[j].id == mfr)
393                                         break;
394                         }
395                         printk(KERN_INFO
396                                "Flash chip found: Manufacturer ID: %2.2X, "
397                                "Chip ID: %2.2X (%s:%s)\n", mfr, id,
398                                nand_manuf_ids[j].name, nand_flash_ids[i].name);
399                         if (!doc->mfr) {
400                                 doc->mfr = mfr;
401                                 doc->id = id;
402                                 doc->chipshift =
403                                         ffs((nand_flash_ids[i].chipsize << 20)) - 1;
404                                 doc->page256 = (nand_flash_ids[i].pagesize == 256) ? 1 : 0;
405                                 doc->pageadrlen = doc->chipshift > 25 ? 3 : 2;
406                                 doc->erasesize =
407                                     nand_flash_ids[i].erasesize;
408                                 return 1;
409                         }
410                         return 0;
411                 }
412         }
413
414
415         /* We haven't fully identified the chip. Print as much as we know. */
416         printk(KERN_WARNING "Unknown flash chip found: %2.2X %2.2X\n",
417                id, mfr);
418
419         printk(KERN_WARNING "Please report to dwmw2@infradead.org\n");
420         return 0;
421 }
422
423 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
424
425 static void DoC_ScanChips(struct DiskOnChip *this, int maxchips)
426 {
427         int floor, chip;
428         int numchips[MAX_FLOORS];
429         int ret = 1;
430
431         this->numchips = 0;
432         this->mfr = 0;
433         this->id = 0;
434
435         /* For each floor, find the number of valid chips it contains */
436         for (floor = 0; floor < MAX_FLOORS; floor++) {
437                 ret = 1;
438                 numchips[floor] = 0;
439                 for (chip = 0; chip < maxchips && ret != 0; chip++) {
440
441                         ret = DoC_IdentChip(this, floor, chip);
442                         if (ret) {
443                                 numchips[floor]++;
444                                 this->numchips++;
445                         }
446                 }
447         }
448
449         /* If there are none at all that we recognise, bail */
450         if (!this->numchips) {
451                 printk(KERN_NOTICE "No flash chips recognised.\n");
452                 return;
453         }
454
455         /* Allocate an array to hold the information for each chip */
456         this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
457         if (!this->chips) {
458                 printk(KERN_NOTICE "No memory for allocating chip info structures\n");
459                 return;
460         }
461
462         ret = 0;
463
464         /* Fill out the chip array with {floor, chipno} for each
465          * detected chip in the device. */
466         for (floor = 0; floor < MAX_FLOORS; floor++) {
467                 for (chip = 0; chip < numchips[floor]; chip++) {
468                         this->chips[ret].floor = floor;
469                         this->chips[ret].chip = chip;
470                         this->chips[ret].curadr = 0;
471                         this->chips[ret].curmode = 0x50;
472                         ret++;
473                 }
474         }
475
476         /* Calculate and print the total size of the device */
477         this->totlen = this->numchips * (1 << this->chipshift);
478
479         printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
480                this->numchips, this->totlen >> 20);
481 }
482
483 static int DoC2k_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
484 {
485         int tmp1, tmp2, retval;
486         if (doc1->physadr == doc2->physadr)
487                 return 1;
488
489         /* Use the alias resolution register which was set aside for this
490          * purpose. If it's value is the same on both chips, they might
491          * be the same chip, and we write to one and check for a change in
492          * the other. It's unclear if this register is usuable in the
493          * DoC 2000 (it's in the Millennium docs), but it seems to work. */
494         tmp1 = ReadDOC(doc1->virtadr, AliasResolution);
495         tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
496         if (tmp1 != tmp2)
497                 return 0;
498
499         WriteDOC((tmp1 + 1) % 0xff, doc1->virtadr, AliasResolution);
500         tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
501         if (tmp2 == (tmp1 + 1) % 0xff)
502                 retval = 1;
503         else
504                 retval = 0;
505
506         /* Restore register contents.  May not be necessary, but do it just to
507          * be safe. */
508         WriteDOC(tmp1, doc1->virtadr, AliasResolution);
509
510         return retval;
511 }
512
513 /* This routine is found from the docprobe code by symbol_get(),
514  * which will bump the use count of this module. */
515 void DoC2k_init(struct mtd_info *mtd)
516 {
517         struct DiskOnChip *this = mtd->priv;
518         struct DiskOnChip *old = NULL;
519         int maxchips;
520
521         /* We must avoid being called twice for the same device. */
522
523         if (doc2klist)
524                 old = doc2klist->priv;
525
526         while (old) {
527                 if (DoC2k_is_alias(old, this)) {
528                         printk(KERN_NOTICE
529                                "Ignoring DiskOnChip 2000 at 0x%lX - already configured\n",
530                                this->physadr);
531                         iounmap(this->virtadr);
532                         kfree(mtd);
533                         return;
534                 }
535                 if (old->nextdoc)
536                         old = old->nextdoc->priv;
537                 else
538                         old = NULL;
539         }
540
541
542         switch (this->ChipID) {
543         case DOC_ChipID_Doc2kTSOP:
544                 mtd->name = "DiskOnChip 2000 TSOP";
545                 this->ioreg = DoC_Mil_CDSN_IO;
546                 /* Pretend it's a Millennium */
547                 this->ChipID = DOC_ChipID_DocMil;
548                 maxchips = MAX_CHIPS;
549                 break;
550         case DOC_ChipID_Doc2k:
551                 mtd->name = "DiskOnChip 2000";
552                 this->ioreg = DoC_2k_CDSN_IO;
553                 maxchips = MAX_CHIPS;
554                 break;
555         case DOC_ChipID_DocMil:
556                 mtd->name = "DiskOnChip Millennium";
557                 this->ioreg = DoC_Mil_CDSN_IO;
558                 maxchips = MAX_CHIPS_MIL;
559                 break;
560         default:
561                 printk("Unknown ChipID 0x%02x\n", this->ChipID);
562                 kfree(mtd);
563                 iounmap(this->virtadr);
564                 return;
565         }
566
567         printk(KERN_NOTICE "%s found at address 0x%lX\n", mtd->name,
568                this->physadr);
569
570         mtd->type = MTD_NANDFLASH;
571         mtd->flags = MTD_CAP_NANDFLASH;
572         mtd->ecctype = MTD_ECC_RS_DiskOnChip;
573         mtd->size = 0;
574         mtd->erasesize = 0;
575         mtd->writesize = 512;
576         mtd->oobsize = 16;
577         mtd->owner = THIS_MODULE;
578         mtd->erase = doc_erase;
579         mtd->point = NULL;
580         mtd->unpoint = NULL;
581         mtd->read = doc_read;
582         mtd->write = doc_write;
583         mtd->read_oob = doc_read_oob;
584         mtd->write_oob = doc_write_oob;
585         mtd->sync = NULL;
586
587         this->totlen = 0;
588         this->numchips = 0;
589
590         this->curfloor = -1;
591         this->curchip = -1;
592         mutex_init(&this->lock);
593
594         /* Ident all the chips present. */
595         DoC_ScanChips(this, maxchips);
596
597         if (!this->totlen) {
598                 kfree(mtd);
599                 iounmap(this->virtadr);
600         } else {
601                 this->nextdoc = doc2klist;
602                 doc2klist = mtd;
603                 mtd->size = this->totlen;
604                 mtd->erasesize = this->erasesize;
605                 add_mtd_device(mtd);
606                 return;
607         }
608 }
609 EXPORT_SYMBOL_GPL(DoC2k_init);
610
611 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
612                     size_t * retlen, u_char * buf)
613 {
614         struct DiskOnChip *this = mtd->priv;
615         void __iomem *docptr = this->virtadr;
616         struct Nand *mychip;
617         unsigned char syndrome[6], eccbuf[6];
618         volatile char dummy;
619         int i, len256 = 0, ret=0;
620         size_t left = len;
621
622         /* Don't allow read past end of device */
623         if (from >= this->totlen)
624                 return -EINVAL;
625
626         mutex_lock(&this->lock);
627
628         *retlen = 0;
629         while (left) {
630                 len = left;
631
632                 /* Don't allow a single read to cross a 512-byte block boundary */
633                 if (from + len > ((from | 0x1ff) + 1))
634                         len = ((from | 0x1ff) + 1) - from;
635
636                 /* The ECC will not be calculated correctly if less than 512 is read */
637                 if (len != 0x200 && eccbuf)
638                         printk(KERN_WARNING
639                                "ECC needs a full sector read (adr: %lx size %lx)\n",
640                                (long) from, (long) len);
641
642                 /* printk("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len); */
643
644
645                 /* Find the chip which is to be used and select it */
646                 mychip = &this->chips[from >> (this->chipshift)];
647
648                 if (this->curfloor != mychip->floor) {
649                         DoC_SelectFloor(this, mychip->floor);
650                         DoC_SelectChip(this, mychip->chip);
651                 } else if (this->curchip != mychip->chip) {
652                         DoC_SelectChip(this, mychip->chip);
653                 }
654
655                 this->curfloor = mychip->floor;
656                 this->curchip = mychip->chip;
657
658                 DoC_Command(this,
659                             (!this->page256
660                              && (from & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
661                             CDSN_CTRL_WP);
662                 DoC_Address(this, ADDR_COLUMN_PAGE, from, CDSN_CTRL_WP,
663                             CDSN_CTRL_ECC_IO);
664
665                 /* Prime the ECC engine */
666                 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
667                 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
668
669                 /* treat crossing 256-byte sector for 2M x 8bits devices */
670                 if (this->page256 && from + len > (from | 0xff) + 1) {
671                         len256 = (from | 0xff) + 1 - from;
672                         DoC_ReadBuf(this, buf, len256);
673
674                         DoC_Command(this, NAND_CMD_READ0, CDSN_CTRL_WP);
675                         DoC_Address(this, ADDR_COLUMN_PAGE, from + len256,
676                                     CDSN_CTRL_WP, CDSN_CTRL_ECC_IO);
677                 }
678
679                 DoC_ReadBuf(this, &buf[len256], len - len256);
680
681                 /* Let the caller know we completed it */
682                 *retlen += len;
683
684                 /* Read the ECC data through the DiskOnChip ECC logic */
685                 /* Note: this will work even with 2M x 8bit devices as   */
686                 /*       they have 8 bytes of OOB per 256 page. mf.      */
687                 DoC_ReadBuf(this, eccbuf, 6);
688
689                 /* Flush the pipeline */
690                 if (DoC_is_Millennium(this)) {
691                         dummy = ReadDOC(docptr, ECCConf);
692                         dummy = ReadDOC(docptr, ECCConf);
693                         i = ReadDOC(docptr, ECCConf);
694                 } else {
695                         dummy = ReadDOC(docptr, 2k_ECCStatus);
696                         dummy = ReadDOC(docptr, 2k_ECCStatus);
697                         i = ReadDOC(docptr, 2k_ECCStatus);
698                 }
699
700                 /* Check the ECC Status */
701                 if (i & 0x80) {
702                         int nb_errors;
703                         /* There was an ECC error */
704 #ifdef ECC_DEBUG
705                         printk(KERN_ERR "DiskOnChip ECC Error: Read at %lx\n", (long)from);
706 #endif
707                         /* Read the ECC syndrom through the DiskOnChip ECC
708                            logic.  These syndrome will be all ZERO when there
709                            is no error */
710                         for (i = 0; i < 6; i++) {
711                                 syndrome[i] =
712                                         ReadDOC(docptr, ECCSyndrome0 + i);
713                         }
714                         nb_errors = doc_decode_ecc(buf, syndrome);
715
716 #ifdef ECC_DEBUG
717                         printk(KERN_ERR "Errors corrected: %x\n", nb_errors);
718 #endif
719                         if (nb_errors < 0) {
720                                 /* We return error, but have actually done the
721                                    read. Not that this can be told to
722                                    user-space, via sys_read(), but at least
723                                    MTD-aware stuff can know about it by
724                                    checking *retlen */
725                                 ret = -EIO;
726                         }
727                 }
728
729 #ifdef PSYCHO_DEBUG
730                 printk(KERN_DEBUG "ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
731                        (long)from, eccbuf[0], eccbuf[1], eccbuf[2],
732                        eccbuf[3], eccbuf[4], eccbuf[5]);
733 #endif
734
735                 /* disable the ECC engine */
736                 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
737
738                 /* according to 11.4.1, we need to wait for the busy line
739                  * drop if we read to the end of the page.  */
740                 if(0 == ((from + len) & 0x1ff))
741                 {
742                     DoC_WaitReady(this);
743                 }
744
745                 from += len;
746                 left -= len;
747                 buf += len;
748         }
749
750         mutex_unlock(&this->lock);
751
752         return ret;
753 }
754
755 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
756                      size_t * retlen, const u_char * buf)
757 {
758         struct DiskOnChip *this = mtd->priv;
759         int di; /* Yes, DI is a hangover from when I was disassembling the binary driver */
760         void __iomem *docptr = this->virtadr;
761         unsigned char eccbuf[6];
762         volatile char dummy;
763         int len256 = 0;
764         struct Nand *mychip;
765         size_t left = len;
766         int status;
767
768         /* Don't allow write past end of device */
769         if (to >= this->totlen)
770                 return -EINVAL;
771
772         mutex_lock(&this->lock);
773
774         *retlen = 0;
775         while (left) {
776                 len = left;
777
778                 /* Don't allow a single write to cross a 512-byte block boundary */
779                 if (to + len > ((to | 0x1ff) + 1))
780                         len = ((to | 0x1ff) + 1) - to;
781
782                 /* The ECC will not be calculated correctly if less than 512 is written */
783 /* DBB-
784                 if (len != 0x200 && eccbuf)
785                         printk(KERN_WARNING
786                                "ECC needs a full sector write (adr: %lx size %lx)\n",
787                                (long) to, (long) len);
788    -DBB */
789
790                 /* printk("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */
791
792                 /* Find the chip which is to be used and select it */
793                 mychip = &this->chips[to >> (this->chipshift)];
794
795                 if (this->curfloor != mychip->floor) {
796                         DoC_SelectFloor(this, mychip->floor);
797                         DoC_SelectChip(this, mychip->chip);
798                 } else if (this->curchip != mychip->chip) {
799                         DoC_SelectChip(this, mychip->chip);
800                 }
801
802                 this->curfloor = mychip->floor;
803                 this->curchip = mychip->chip;
804
805                 /* Set device to main plane of flash */
806                 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
807                 DoC_Command(this,
808                             (!this->page256
809                              && (to & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
810                             CDSN_CTRL_WP);
811
812                 DoC_Command(this, NAND_CMD_SEQIN, 0);
813                 DoC_Address(this, ADDR_COLUMN_PAGE, to, 0, CDSN_CTRL_ECC_IO);
814
815                 /* Prime the ECC engine */
816                 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
817                 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
818
819                 /* treat crossing 256-byte sector for 2M x 8bits devices */
820                 if (this->page256 && to + len > (to | 0xff) + 1) {
821                         len256 = (to | 0xff) + 1 - to;
822                         DoC_WriteBuf(this, buf, len256);
823
824                         DoC_Command(this, NAND_CMD_PAGEPROG, 0);
825
826                         DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
827                         /* There's an implicit DoC_WaitReady() in DoC_Command */
828
829                         dummy = ReadDOC(docptr, CDSNSlowIO);
830                         DoC_Delay(this, 2);
831
832                         if (ReadDOC_(docptr, this->ioreg) & 1) {
833                                 printk(KERN_ERR "Error programming flash\n");
834                                 /* Error in programming */
835                                 *retlen = 0;
836                                 mutex_unlock(&this->lock);
837                                 return -EIO;
838                         }
839
840                         DoC_Command(this, NAND_CMD_SEQIN, 0);
841                         DoC_Address(this, ADDR_COLUMN_PAGE, to + len256, 0,
842                                     CDSN_CTRL_ECC_IO);
843                 }
844
845                 DoC_WriteBuf(this, &buf[len256], len - len256);
846
847                 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_CE, docptr, CDSNControl);
848
849                 if (DoC_is_Millennium(this)) {
850                         WriteDOC(0, docptr, NOP);
851                         WriteDOC(0, docptr, NOP);
852                         WriteDOC(0, docptr, NOP);
853                 } else {
854                         WriteDOC_(0, docptr, this->ioreg);
855                         WriteDOC_(0, docptr, this->ioreg);
856                         WriteDOC_(0, docptr, this->ioreg);
857                 }
858
859                 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_FLASH_IO | CDSN_CTRL_CE, docptr,
860                          CDSNControl);
861
862                 /* Read the ECC data through the DiskOnChip ECC logic */
863                 for (di = 0; di < 6; di++) {
864                         eccbuf[di] = ReadDOC(docptr, ECCSyndrome0 + di);
865                 }
866
867                 /* Reset the ECC engine */
868                 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
869
870 #ifdef PSYCHO_DEBUG
871                 printk
872                         ("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
873                          (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
874                          eccbuf[4], eccbuf[5]);
875 #endif
876                 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
877
878                 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
879                 /* There's an implicit DoC_WaitReady() in DoC_Command */
880
881                 if (DoC_is_Millennium(this)) {
882                         ReadDOC(docptr, ReadPipeInit);
883                         status = ReadDOC(docptr, LastDataRead);
884                 } else {
885                         dummy = ReadDOC(docptr, CDSNSlowIO);
886                         DoC_Delay(this, 2);
887                         status = ReadDOC_(docptr, this->ioreg);
888                 }
889
890                 if (status & 1) {
891                         printk(KERN_ERR "Error programming flash\n");
892                         /* Error in programming */
893                         *retlen = 0;
894                         mutex_unlock(&this->lock);
895                         return -EIO;
896                 }
897
898                 /* Let the caller know we completed it */
899                 *retlen += len;
900
901                 if (eccbuf) {
902                         unsigned char x[8];
903                         size_t dummy;
904                         int ret;
905
906                         /* Write the ECC data to flash */
907                         for (di=0; di<6; di++)
908                                 x[di] = eccbuf[di];
909
910                         x[6]=0x55;
911                         x[7]=0x55;
912
913                         ret = doc_write_oob_nolock(mtd, to, 8, &dummy, x);
914                         if (ret) {
915                                 mutex_unlock(&this->lock);
916                                 return ret;
917                         }
918                 }
919
920                 to += len;
921                 left -= len;
922                 buf += len;
923         }
924
925         mutex_unlock(&this->lock);
926         return 0;
927 }
928
929 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
930                         struct mtd_oob_ops *ops)
931 {
932         struct DiskOnChip *this = mtd->priv;
933         int len256 = 0, ret;
934         struct Nand *mychip;
935         uint8_t *buf = ops->oobbuf;
936         size_t len = ops->len;
937
938         BUG_ON(ops->mode != MTD_OOB_PLACE);
939
940         ofs += ops->ooboffs;
941
942         mutex_lock(&this->lock);
943
944         mychip = &this->chips[ofs >> this->chipshift];
945
946         if (this->curfloor != mychip->floor) {
947                 DoC_SelectFloor(this, mychip->floor);
948                 DoC_SelectChip(this, mychip->chip);
949         } else if (this->curchip != mychip->chip) {
950                 DoC_SelectChip(this, mychip->chip);
951         }
952         this->curfloor = mychip->floor;
953         this->curchip = mychip->chip;
954
955         /* update address for 2M x 8bit devices. OOB starts on the second */
956         /* page to maintain compatibility with doc_read_ecc. */
957         if (this->page256) {
958                 if (!(ofs & 0x8))
959                         ofs += 0x100;
960                 else
961                         ofs -= 0x8;
962         }
963
964         DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
965         DoC_Address(this, ADDR_COLUMN_PAGE, ofs, CDSN_CTRL_WP, 0);
966
967         /* treat crossing 8-byte OOB data for 2M x 8bit devices */
968         /* Note: datasheet says it should automaticaly wrap to the */
969         /*       next OOB block, but it didn't work here. mf.      */
970         if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
971                 len256 = (ofs | 0x7) + 1 - ofs;
972                 DoC_ReadBuf(this, buf, len256);
973
974                 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
975                 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff),
976                             CDSN_CTRL_WP, 0);
977         }
978
979         DoC_ReadBuf(this, &buf[len256], len - len256);
980
981         ops->retlen = len;
982         /* Reading the full OOB data drops us off of the end of the page,
983          * causing the flash device to go into busy mode, so we need
984          * to wait until ready 11.4.1 and Toshiba TC58256FT docs */
985
986         ret = DoC_WaitReady(this);
987
988         mutex_unlock(&this->lock);
989         return ret;
990
991 }
992
993 static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
994                                 size_t * retlen, const u_char * buf)
995 {
996         struct DiskOnChip *this = mtd->priv;
997         int len256 = 0;
998         void __iomem *docptr = this->virtadr;
999         struct Nand *mychip = &this->chips[ofs >> this->chipshift];
1000         volatile int dummy;
1001         int status;
1002
1003         //      printk("doc_write_oob(%lx, %d): %2.2X %2.2X %2.2X %2.2X ... %2.2X %2.2X .. %2.2X %2.2X\n",(long)ofs, len,
1004         //   buf[0], buf[1], buf[2], buf[3], buf[8], buf[9], buf[14],buf[15]);
1005
1006         /* Find the chip which is to be used and select it */
1007         if (this->curfloor != mychip->floor) {
1008                 DoC_SelectFloor(this, mychip->floor);
1009                 DoC_SelectChip(this, mychip->chip);
1010         } else if (this->curchip != mychip->chip) {
1011                 DoC_SelectChip(this, mychip->chip);
1012         }
1013         this->curfloor = mychip->floor;
1014         this->curchip = mychip->chip;
1015
1016         /* disable the ECC engine */
1017         WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
1018         WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
1019
1020         /* Reset the chip, see Software Requirement 11.4 item 1. */
1021         DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
1022
1023         /* issue the Read2 command to set the pointer to the Spare Data Area. */
1024         DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1025
1026         /* update address for 2M x 8bit devices. OOB starts on the second */
1027         /* page to maintain compatibility with doc_read_ecc. */
1028         if (this->page256) {
1029                 if (!(ofs & 0x8))
1030                         ofs += 0x100;
1031                 else
1032                         ofs -= 0x8;
1033         }
1034
1035         /* issue the Serial Data In command to initial the Page Program process */
1036         DoC_Command(this, NAND_CMD_SEQIN, 0);
1037         DoC_Address(this, ADDR_COLUMN_PAGE, ofs, 0, 0);
1038
1039         /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1040         /* Note: datasheet says it should automaticaly wrap to the */
1041         /*       next OOB block, but it didn't work here. mf.      */
1042         if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1043                 len256 = (ofs | 0x7) + 1 - ofs;
1044                 DoC_WriteBuf(this, buf, len256);
1045
1046                 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1047                 DoC_Command(this, NAND_CMD_STATUS, 0);
1048                 /* DoC_WaitReady() is implicit in DoC_Command */
1049
1050                 if (DoC_is_Millennium(this)) {
1051                         ReadDOC(docptr, ReadPipeInit);
1052                         status = ReadDOC(docptr, LastDataRead);
1053                 } else {
1054                         dummy = ReadDOC(docptr, CDSNSlowIO);
1055                         DoC_Delay(this, 2);
1056                         status = ReadDOC_(docptr, this->ioreg);
1057                 }
1058
1059                 if (status & 1) {
1060                         printk(KERN_ERR "Error programming oob data\n");
1061                         /* There was an error */
1062                         *retlen = 0;
1063                         return -EIO;
1064                 }
1065                 DoC_Command(this, NAND_CMD_SEQIN, 0);
1066                 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), 0, 0);
1067         }
1068
1069         DoC_WriteBuf(this, &buf[len256], len - len256);
1070
1071         DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1072         DoC_Command(this, NAND_CMD_STATUS, 0);
1073         /* DoC_WaitReady() is implicit in DoC_Command */
1074
1075         if (DoC_is_Millennium(this)) {
1076                 ReadDOC(docptr, ReadPipeInit);
1077                 status = ReadDOC(docptr, LastDataRead);
1078         } else {
1079                 dummy = ReadDOC(docptr, CDSNSlowIO);
1080                 DoC_Delay(this, 2);
1081                 status = ReadDOC_(docptr, this->ioreg);
1082         }
1083
1084         if (status & 1) {
1085                 printk(KERN_ERR "Error programming oob data\n");
1086                 /* There was an error */
1087                 *retlen = 0;
1088                 return -EIO;
1089         }
1090
1091         *retlen = len;
1092         return 0;
1093
1094 }
1095
1096 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
1097                          struct mtd_oob_ops *ops)
1098 {
1099         struct DiskOnChip *this = mtd->priv;
1100         int ret;
1101
1102         BUG_ON(ops->mode != MTD_OOB_PLACE);
1103
1104         mutex_lock(&this->lock);
1105         ret = doc_write_oob_nolock(mtd, ofs + ops->ooboffs, ops->len,
1106                                    &ops->retlen, ops->oobbuf);
1107
1108         mutex_unlock(&this->lock);
1109         return ret;
1110 }
1111
1112 static int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
1113 {
1114         struct DiskOnChip *this = mtd->priv;
1115         __u32 ofs = instr->addr;
1116         __u32 len = instr->len;
1117         volatile int dummy;
1118         void __iomem *docptr = this->virtadr;
1119         struct Nand *mychip;
1120         int status;
1121
1122         mutex_lock(&this->lock);
1123
1124         if (ofs & (mtd->erasesize-1) || len & (mtd->erasesize-1)) {
1125                 mutex_unlock(&this->lock);
1126                 return -EINVAL;
1127         }
1128
1129         instr->state = MTD_ERASING;
1130
1131         /* FIXME: Do this in the background. Use timers or schedule_task() */
1132         while(len) {
1133                 mychip = &this->chips[ofs >> this->chipshift];
1134
1135                 if (this->curfloor != mychip->floor) {
1136                         DoC_SelectFloor(this, mychip->floor);
1137                         DoC_SelectChip(this, mychip->chip);
1138                 } else if (this->curchip != mychip->chip) {
1139                         DoC_SelectChip(this, mychip->chip);
1140                 }
1141                 this->curfloor = mychip->floor;
1142                 this->curchip = mychip->chip;
1143
1144                 DoC_Command(this, NAND_CMD_ERASE1, 0);
1145                 DoC_Address(this, ADDR_PAGE, ofs, 0, 0);
1146                 DoC_Command(this, NAND_CMD_ERASE2, 0);
1147
1148                 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
1149
1150                 if (DoC_is_Millennium(this)) {
1151                         ReadDOC(docptr, ReadPipeInit);
1152                         status = ReadDOC(docptr, LastDataRead);
1153                 } else {
1154                         dummy = ReadDOC(docptr, CDSNSlowIO);
1155                         DoC_Delay(this, 2);
1156                         status = ReadDOC_(docptr, this->ioreg);
1157                 }
1158
1159                 if (status & 1) {
1160                         printk(KERN_ERR "Error erasing at 0x%x\n", ofs);
1161                         /* There was an error */
1162                         instr->state = MTD_ERASE_FAILED;
1163                         goto callback;
1164                 }
1165                 ofs += mtd->erasesize;
1166                 len -= mtd->erasesize;
1167         }
1168         instr->state = MTD_ERASE_DONE;
1169
1170  callback:
1171         mtd_erase_callback(instr);
1172
1173         mutex_unlock(&this->lock);
1174         return 0;
1175 }
1176
1177
1178 /****************************************************************************
1179  *
1180  * Module stuff
1181  *
1182  ****************************************************************************/
1183
1184 static void __exit cleanup_doc2000(void)
1185 {
1186         struct mtd_info *mtd;
1187         struct DiskOnChip *this;
1188
1189         while ((mtd = doc2klist)) {
1190                 this = mtd->priv;
1191                 doc2klist = this->nextdoc;
1192
1193                 del_mtd_device(mtd);
1194
1195                 iounmap(this->virtadr);
1196                 kfree(this->chips);
1197                 kfree(mtd);
1198         }
1199 }
1200
1201 module_exit(cleanup_doc2000);
1202
1203 MODULE_LICENSE("GPL");
1204 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
1205 MODULE_DESCRIPTION("MTD driver for DiskOnChip 2000 and Millennium");
1206