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