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