Merge branch 'for-linus' of master.kernel.org:/pub/scm/linux/kernel/git/dtor/input
[pandora-kernel.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->size = 0;
573         mtd->erasesize = 0;
574         mtd->writesize = 512;
575         mtd->oobsize = 16;
576         mtd->owner = THIS_MODULE;
577         mtd->erase = doc_erase;
578         mtd->point = NULL;
579         mtd->unpoint = NULL;
580         mtd->read = doc_read;
581         mtd->write = doc_write;
582         mtd->read_oob = doc_read_oob;
583         mtd->write_oob = doc_write_oob;
584         mtd->sync = NULL;
585
586         this->totlen = 0;
587         this->numchips = 0;
588
589         this->curfloor = -1;
590         this->curchip = -1;
591         mutex_init(&this->lock);
592
593         /* Ident all the chips present. */
594         DoC_ScanChips(this, maxchips);
595
596         if (!this->totlen) {
597                 kfree(mtd);
598                 iounmap(this->virtadr);
599         } else {
600                 this->nextdoc = doc2klist;
601                 doc2klist = mtd;
602                 mtd->size = this->totlen;
603                 mtd->erasesize = this->erasesize;
604                 add_mtd_device(mtd);
605                 return;
606         }
607 }
608 EXPORT_SYMBOL_GPL(DoC2k_init);
609
610 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
611                     size_t * retlen, u_char * buf)
612 {
613         struct DiskOnChip *this = mtd->priv;
614         void __iomem *docptr = this->virtadr;
615         struct Nand *mychip;
616         unsigned char syndrome[6], eccbuf[6];
617         volatile char dummy;
618         int i, len256 = 0, ret=0;
619         size_t left = len;
620
621         /* Don't allow read past end of device */
622         if (from >= this->totlen)
623                 return -EINVAL;
624
625         mutex_lock(&this->lock);
626
627         *retlen = 0;
628         while (left) {
629                 len = left;
630
631                 /* Don't allow a single read to cross a 512-byte block boundary */
632                 if (from + len > ((from | 0x1ff) + 1))
633                         len = ((from | 0x1ff) + 1) - from;
634
635                 /* The ECC will not be calculated correctly if less than 512 is read */
636                 if (len != 0x200 && eccbuf)
637                         printk(KERN_WARNING
638                                "ECC needs a full sector read (adr: %lx size %lx)\n",
639                                (long) from, (long) len);
640
641                 /* printk("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len); */
642
643
644                 /* Find the chip which is to be used and select it */
645                 mychip = &this->chips[from >> (this->chipshift)];
646
647                 if (this->curfloor != mychip->floor) {
648                         DoC_SelectFloor(this, mychip->floor);
649                         DoC_SelectChip(this, mychip->chip);
650                 } else if (this->curchip != mychip->chip) {
651                         DoC_SelectChip(this, mychip->chip);
652                 }
653
654                 this->curfloor = mychip->floor;
655                 this->curchip = mychip->chip;
656
657                 DoC_Command(this,
658                             (!this->page256
659                              && (from & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
660                             CDSN_CTRL_WP);
661                 DoC_Address(this, ADDR_COLUMN_PAGE, from, CDSN_CTRL_WP,
662                             CDSN_CTRL_ECC_IO);
663
664                 /* Prime the ECC engine */
665                 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
666                 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
667
668                 /* treat crossing 256-byte sector for 2M x 8bits devices */
669                 if (this->page256 && from + len > (from | 0xff) + 1) {
670                         len256 = (from | 0xff) + 1 - from;
671                         DoC_ReadBuf(this, buf, len256);
672
673                         DoC_Command(this, NAND_CMD_READ0, CDSN_CTRL_WP);
674                         DoC_Address(this, ADDR_COLUMN_PAGE, from + len256,
675                                     CDSN_CTRL_WP, CDSN_CTRL_ECC_IO);
676                 }
677
678                 DoC_ReadBuf(this, &buf[len256], len - len256);
679
680                 /* Let the caller know we completed it */
681                 *retlen += len;
682
683                 /* Read the ECC data through the DiskOnChip ECC logic */
684                 /* Note: this will work even with 2M x 8bit devices as   */
685                 /*       they have 8 bytes of OOB per 256 page. mf.      */
686                 DoC_ReadBuf(this, eccbuf, 6);
687
688                 /* Flush the pipeline */
689                 if (DoC_is_Millennium(this)) {
690                         dummy = ReadDOC(docptr, ECCConf);
691                         dummy = ReadDOC(docptr, ECCConf);
692                         i = ReadDOC(docptr, ECCConf);
693                 } else {
694                         dummy = ReadDOC(docptr, 2k_ECCStatus);
695                         dummy = ReadDOC(docptr, 2k_ECCStatus);
696                         i = ReadDOC(docptr, 2k_ECCStatus);
697                 }
698
699                 /* Check the ECC Status */
700                 if (i & 0x80) {
701                         int nb_errors;
702                         /* There was an ECC error */
703 #ifdef ECC_DEBUG
704                         printk(KERN_ERR "DiskOnChip ECC Error: Read at %lx\n", (long)from);
705 #endif
706                         /* Read the ECC syndrom through the DiskOnChip ECC
707                            logic.  These syndrome will be all ZERO when there
708                            is no error */
709                         for (i = 0; i < 6; i++) {
710                                 syndrome[i] =
711                                         ReadDOC(docptr, ECCSyndrome0 + i);
712                         }
713                         nb_errors = doc_decode_ecc(buf, syndrome);
714
715 #ifdef ECC_DEBUG
716                         printk(KERN_ERR "Errors corrected: %x\n", nb_errors);
717 #endif
718                         if (nb_errors < 0) {
719                                 /* We return error, but have actually done the
720                                    read. Not that this can be told to
721                                    user-space, via sys_read(), but at least
722                                    MTD-aware stuff can know about it by
723                                    checking *retlen */
724                                 ret = -EIO;
725                         }
726                 }
727
728 #ifdef PSYCHO_DEBUG
729                 printk(KERN_DEBUG "ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
730                        (long)from, eccbuf[0], eccbuf[1], eccbuf[2],
731                        eccbuf[3], eccbuf[4], eccbuf[5]);
732 #endif
733
734                 /* disable the ECC engine */
735                 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
736
737                 /* according to 11.4.1, we need to wait for the busy line
738                  * drop if we read to the end of the page.  */
739                 if(0 == ((from + len) & 0x1ff))
740                 {
741                     DoC_WaitReady(this);
742                 }
743
744                 from += len;
745                 left -= len;
746                 buf += len;
747         }
748
749         mutex_unlock(&this->lock);
750
751         return ret;
752 }
753
754 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
755                      size_t * retlen, const u_char * buf)
756 {
757         struct DiskOnChip *this = mtd->priv;
758         int di; /* Yes, DI is a hangover from when I was disassembling the binary driver */
759         void __iomem *docptr = this->virtadr;
760         unsigned char eccbuf[6];
761         volatile char dummy;
762         int len256 = 0;
763         struct Nand *mychip;
764         size_t left = len;
765         int status;
766
767         /* Don't allow write past end of device */
768         if (to >= this->totlen)
769                 return -EINVAL;
770
771         mutex_lock(&this->lock);
772
773         *retlen = 0;
774         while (left) {
775                 len = left;
776
777                 /* Don't allow a single write to cross a 512-byte block boundary */
778                 if (to + len > ((to | 0x1ff) + 1))
779                         len = ((to | 0x1ff) + 1) - to;
780
781                 /* The ECC will not be calculated correctly if less than 512 is written */
782 /* DBB-
783                 if (len != 0x200 && eccbuf)
784                         printk(KERN_WARNING
785                                "ECC needs a full sector write (adr: %lx size %lx)\n",
786                                (long) to, (long) len);
787    -DBB */
788
789                 /* printk("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */
790
791                 /* Find the chip which is to be used and select it */
792                 mychip = &this->chips[to >> (this->chipshift)];
793
794                 if (this->curfloor != mychip->floor) {
795                         DoC_SelectFloor(this, mychip->floor);
796                         DoC_SelectChip(this, mychip->chip);
797                 } else if (this->curchip != mychip->chip) {
798                         DoC_SelectChip(this, mychip->chip);
799                 }
800
801                 this->curfloor = mychip->floor;
802                 this->curchip = mychip->chip;
803
804                 /* Set device to main plane of flash */
805                 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
806                 DoC_Command(this,
807                             (!this->page256
808                              && (to & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
809                             CDSN_CTRL_WP);
810
811                 DoC_Command(this, NAND_CMD_SEQIN, 0);
812                 DoC_Address(this, ADDR_COLUMN_PAGE, to, 0, CDSN_CTRL_ECC_IO);
813
814                 /* Prime the ECC engine */
815                 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
816                 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
817
818                 /* treat crossing 256-byte sector for 2M x 8bits devices */
819                 if (this->page256 && to + len > (to | 0xff) + 1) {
820                         len256 = (to | 0xff) + 1 - to;
821                         DoC_WriteBuf(this, buf, len256);
822
823                         DoC_Command(this, NAND_CMD_PAGEPROG, 0);
824
825                         DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
826                         /* There's an implicit DoC_WaitReady() in DoC_Command */
827
828                         dummy = ReadDOC(docptr, CDSNSlowIO);
829                         DoC_Delay(this, 2);
830
831                         if (ReadDOC_(docptr, this->ioreg) & 1) {
832                                 printk(KERN_ERR "Error programming flash\n");
833                                 /* Error in programming */
834                                 *retlen = 0;
835                                 mutex_unlock(&this->lock);
836                                 return -EIO;
837                         }
838
839                         DoC_Command(this, NAND_CMD_SEQIN, 0);
840                         DoC_Address(this, ADDR_COLUMN_PAGE, to + len256, 0,
841                                     CDSN_CTRL_ECC_IO);
842                 }
843
844                 DoC_WriteBuf(this, &buf[len256], len - len256);
845
846                 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_CE, docptr, CDSNControl);
847
848                 if (DoC_is_Millennium(this)) {
849                         WriteDOC(0, docptr, NOP);
850                         WriteDOC(0, docptr, NOP);
851                         WriteDOC(0, docptr, NOP);
852                 } else {
853                         WriteDOC_(0, docptr, this->ioreg);
854                         WriteDOC_(0, docptr, this->ioreg);
855                         WriteDOC_(0, docptr, this->ioreg);
856                 }
857
858                 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_FLASH_IO | CDSN_CTRL_CE, docptr,
859                          CDSNControl);
860
861                 /* Read the ECC data through the DiskOnChip ECC logic */
862                 for (di = 0; di < 6; di++) {
863                         eccbuf[di] = ReadDOC(docptr, ECCSyndrome0 + di);
864                 }
865
866                 /* Reset the ECC engine */
867                 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
868
869 #ifdef PSYCHO_DEBUG
870                 printk
871                         ("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
872                          (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
873                          eccbuf[4], eccbuf[5]);
874 #endif
875                 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
876
877                 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
878                 /* There's an implicit DoC_WaitReady() in DoC_Command */
879
880                 if (DoC_is_Millennium(this)) {
881                         ReadDOC(docptr, ReadPipeInit);
882                         status = ReadDOC(docptr, LastDataRead);
883                 } else {
884                         dummy = ReadDOC(docptr, CDSNSlowIO);
885                         DoC_Delay(this, 2);
886                         status = ReadDOC_(docptr, this->ioreg);
887                 }
888
889                 if (status & 1) {
890                         printk(KERN_ERR "Error programming flash\n");
891                         /* Error in programming */
892                         *retlen = 0;
893                         mutex_unlock(&this->lock);
894                         return -EIO;
895                 }
896
897                 /* Let the caller know we completed it */
898                 *retlen += len;
899
900                 if (eccbuf) {
901                         unsigned char x[8];
902                         size_t dummy;
903                         int ret;
904
905                         /* Write the ECC data to flash */
906                         for (di=0; di<6; di++)
907                                 x[di] = eccbuf[di];
908
909                         x[6]=0x55;
910                         x[7]=0x55;
911
912                         ret = doc_write_oob_nolock(mtd, to, 8, &dummy, x);
913                         if (ret) {
914                                 mutex_unlock(&this->lock);
915                                 return ret;
916                         }
917                 }
918
919                 to += len;
920                 left -= len;
921                 buf += len;
922         }
923
924         mutex_unlock(&this->lock);
925         return 0;
926 }
927
928 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
929                         struct mtd_oob_ops *ops)
930 {
931         struct DiskOnChip *this = mtd->priv;
932         int len256 = 0, ret;
933         struct Nand *mychip;
934         uint8_t *buf = ops->oobbuf;
935         size_t len = ops->len;
936
937         BUG_ON(ops->mode != MTD_OOB_PLACE);
938
939         ofs += ops->ooboffs;
940
941         mutex_lock(&this->lock);
942
943         mychip = &this->chips[ofs >> this->chipshift];
944
945         if (this->curfloor != mychip->floor) {
946                 DoC_SelectFloor(this, mychip->floor);
947                 DoC_SelectChip(this, mychip->chip);
948         } else if (this->curchip != mychip->chip) {
949                 DoC_SelectChip(this, mychip->chip);
950         }
951         this->curfloor = mychip->floor;
952         this->curchip = mychip->chip;
953
954         /* update address for 2M x 8bit devices. OOB starts on the second */
955         /* page to maintain compatibility with doc_read_ecc. */
956         if (this->page256) {
957                 if (!(ofs & 0x8))
958                         ofs += 0x100;
959                 else
960                         ofs -= 0x8;
961         }
962
963         DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
964         DoC_Address(this, ADDR_COLUMN_PAGE, ofs, CDSN_CTRL_WP, 0);
965
966         /* treat crossing 8-byte OOB data for 2M x 8bit devices */
967         /* Note: datasheet says it should automaticaly wrap to the */
968         /*       next OOB block, but it didn't work here. mf.      */
969         if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
970                 len256 = (ofs | 0x7) + 1 - ofs;
971                 DoC_ReadBuf(this, buf, len256);
972
973                 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
974                 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff),
975                             CDSN_CTRL_WP, 0);
976         }
977
978         DoC_ReadBuf(this, &buf[len256], len - len256);
979
980         ops->retlen = len;
981         /* Reading the full OOB data drops us off of the end of the page,
982          * causing the flash device to go into busy mode, so we need
983          * to wait until ready 11.4.1 and Toshiba TC58256FT docs */
984
985         ret = DoC_WaitReady(this);
986
987         mutex_unlock(&this->lock);
988         return ret;
989
990 }
991
992 static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
993                                 size_t * retlen, const u_char * buf)
994 {
995         struct DiskOnChip *this = mtd->priv;
996         int len256 = 0;
997         void __iomem *docptr = this->virtadr;
998         struct Nand *mychip = &this->chips[ofs >> this->chipshift];
999         volatile int dummy;
1000         int status;
1001
1002         //      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,
1003         //   buf[0], buf[1], buf[2], buf[3], buf[8], buf[9], buf[14],buf[15]);
1004
1005         /* Find the chip which is to be used and select it */
1006         if (this->curfloor != mychip->floor) {
1007                 DoC_SelectFloor(this, mychip->floor);
1008                 DoC_SelectChip(this, mychip->chip);
1009         } else if (this->curchip != mychip->chip) {
1010                 DoC_SelectChip(this, mychip->chip);
1011         }
1012         this->curfloor = mychip->floor;
1013         this->curchip = mychip->chip;
1014
1015         /* disable the ECC engine */
1016         WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
1017         WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
1018
1019         /* Reset the chip, see Software Requirement 11.4 item 1. */
1020         DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
1021
1022         /* issue the Read2 command to set the pointer to the Spare Data Area. */
1023         DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1024
1025         /* update address for 2M x 8bit devices. OOB starts on the second */
1026         /* page to maintain compatibility with doc_read_ecc. */
1027         if (this->page256) {
1028                 if (!(ofs & 0x8))
1029                         ofs += 0x100;
1030                 else
1031                         ofs -= 0x8;
1032         }
1033
1034         /* issue the Serial Data In command to initial the Page Program process */
1035         DoC_Command(this, NAND_CMD_SEQIN, 0);
1036         DoC_Address(this, ADDR_COLUMN_PAGE, ofs, 0, 0);
1037
1038         /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1039         /* Note: datasheet says it should automaticaly wrap to the */
1040         /*       next OOB block, but it didn't work here. mf.      */
1041         if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1042                 len256 = (ofs | 0x7) + 1 - ofs;
1043                 DoC_WriteBuf(this, buf, len256);
1044
1045                 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1046                 DoC_Command(this, NAND_CMD_STATUS, 0);
1047                 /* DoC_WaitReady() is implicit in DoC_Command */
1048
1049                 if (DoC_is_Millennium(this)) {
1050                         ReadDOC(docptr, ReadPipeInit);
1051                         status = ReadDOC(docptr, LastDataRead);
1052                 } else {
1053                         dummy = ReadDOC(docptr, CDSNSlowIO);
1054                         DoC_Delay(this, 2);
1055                         status = ReadDOC_(docptr, this->ioreg);
1056                 }
1057
1058                 if (status & 1) {
1059                         printk(KERN_ERR "Error programming oob data\n");
1060                         /* There was an error */
1061                         *retlen = 0;
1062                         return -EIO;
1063                 }
1064                 DoC_Command(this, NAND_CMD_SEQIN, 0);
1065                 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), 0, 0);
1066         }
1067
1068         DoC_WriteBuf(this, &buf[len256], len - len256);
1069
1070         DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1071         DoC_Command(this, NAND_CMD_STATUS, 0);
1072         /* DoC_WaitReady() is implicit in DoC_Command */
1073
1074         if (DoC_is_Millennium(this)) {
1075                 ReadDOC(docptr, ReadPipeInit);
1076                 status = ReadDOC(docptr, LastDataRead);
1077         } else {
1078                 dummy = ReadDOC(docptr, CDSNSlowIO);
1079                 DoC_Delay(this, 2);
1080                 status = ReadDOC_(docptr, this->ioreg);
1081         }
1082
1083         if (status & 1) {
1084                 printk(KERN_ERR "Error programming oob data\n");
1085                 /* There was an error */
1086                 *retlen = 0;
1087                 return -EIO;
1088         }
1089
1090         *retlen = len;
1091         return 0;
1092
1093 }
1094
1095 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
1096                          struct mtd_oob_ops *ops)
1097 {
1098         struct DiskOnChip *this = mtd->priv;
1099         int ret;
1100
1101         BUG_ON(ops->mode != MTD_OOB_PLACE);
1102
1103         mutex_lock(&this->lock);
1104         ret = doc_write_oob_nolock(mtd, ofs + ops->ooboffs, ops->len,
1105                                    &ops->retlen, ops->oobbuf);
1106
1107         mutex_unlock(&this->lock);
1108         return ret;
1109 }
1110
1111 static int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
1112 {
1113         struct DiskOnChip *this = mtd->priv;
1114         __u32 ofs = instr->addr;
1115         __u32 len = instr->len;
1116         volatile int dummy;
1117         void __iomem *docptr = this->virtadr;
1118         struct Nand *mychip;
1119         int status;
1120
1121         mutex_lock(&this->lock);
1122
1123         if (ofs & (mtd->erasesize-1) || len & (mtd->erasesize-1)) {
1124                 mutex_unlock(&this->lock);
1125                 return -EINVAL;
1126         }
1127
1128         instr->state = MTD_ERASING;
1129
1130         /* FIXME: Do this in the background. Use timers or schedule_task() */
1131         while(len) {
1132                 mychip = &this->chips[ofs >> this->chipshift];
1133
1134                 if (this->curfloor != mychip->floor) {
1135                         DoC_SelectFloor(this, mychip->floor);
1136                         DoC_SelectChip(this, mychip->chip);
1137                 } else if (this->curchip != mychip->chip) {
1138                         DoC_SelectChip(this, mychip->chip);
1139                 }
1140                 this->curfloor = mychip->floor;
1141                 this->curchip = mychip->chip;
1142
1143                 DoC_Command(this, NAND_CMD_ERASE1, 0);
1144                 DoC_Address(this, ADDR_PAGE, ofs, 0, 0);
1145                 DoC_Command(this, NAND_CMD_ERASE2, 0);
1146
1147                 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
1148
1149                 if (DoC_is_Millennium(this)) {
1150                         ReadDOC(docptr, ReadPipeInit);
1151                         status = ReadDOC(docptr, LastDataRead);
1152                 } else {
1153                         dummy = ReadDOC(docptr, CDSNSlowIO);
1154                         DoC_Delay(this, 2);
1155                         status = ReadDOC_(docptr, this->ioreg);
1156                 }
1157
1158                 if (status & 1) {
1159                         printk(KERN_ERR "Error erasing at 0x%x\n", ofs);
1160                         /* There was an error */
1161                         instr->state = MTD_ERASE_FAILED;
1162                         goto callback;
1163                 }
1164                 ofs += mtd->erasesize;
1165                 len -= mtd->erasesize;
1166         }
1167         instr->state = MTD_ERASE_DONE;
1168
1169  callback:
1170         mtd_erase_callback(instr);
1171
1172         mutex_unlock(&this->lock);
1173         return 0;
1174 }
1175
1176
1177 /****************************************************************************
1178  *
1179  * Module stuff
1180  *
1181  ****************************************************************************/
1182
1183 static void __exit cleanup_doc2000(void)
1184 {
1185         struct mtd_info *mtd;
1186         struct DiskOnChip *this;
1187
1188         while ((mtd = doc2klist)) {
1189                 this = mtd->priv;
1190                 doc2klist = this->nextdoc;
1191
1192                 del_mtd_device(mtd);
1193
1194                 iounmap(this->virtadr);
1195                 kfree(this->chips);
1196                 kfree(mtd);
1197         }
1198 }
1199
1200 module_exit(cleanup_doc2000);
1201
1202 MODULE_LICENSE("GPL");
1203 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
1204 MODULE_DESCRIPTION("MTD driver for DiskOnChip 2000 and Millennium");
1205