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