1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#include <common.h>
31#include <asm/arch/hardware.h>
32#include <asm/io.h>
33#include <asm/bitops.h>
34#include <asm/errno.h>
35#include <malloc.h>
36#include <nand.h>
37#include <linux/bch.h>
38#include <linux/bitrev.h>
39#include <linux/mtd/docg4.h>
40
41
42
43
44
45
46static inline void write_nop(void __iomem *docptr)
47{
48 writew(0, docptr + DOC_NOP);
49}
50
51
52static int poll_status(void __iomem *docptr)
53{
54
55
56
57
58
59
60 uint8_t flash_status;
61
62
63 flash_status = readb(docptr + DOC_FLASHCONTROL);
64
65 do {
66 flash_status = readb(docptr + DOC_FLASHCONTROL);
67 } while (!(flash_status & DOC_CTRL_FLASHREADY));
68
69 return 0;
70}
71
72static void write_addr(void __iomem *docptr, uint32_t docg4_addr)
73{
74
75
76 writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
77 docg4_addr >>= 8;
78 writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
79 docg4_addr >>= 8;
80 writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
81 docg4_addr >>= 8;
82 writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
83}
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102static const int ignore_badblocks;
103
104struct docg4_priv {
105 int status;
106 struct {
107 unsigned int command;
108 int column;
109 int page;
110 } last_command;
111 uint8_t oob_buf[16];
112 uint8_t ecc_buf[7];
113 int oob_page;
114 struct bch_control *bch;
115};
116
117
118
119
120
121static struct nand_ecclayout docg4_oobinfo = {
122 .eccbytes = 9,
123 .eccpos = {7, 8, 9, 10, 11, 12, 13, 14, 15},
124 .oobavail = 7,
125 .oobfree = { {0, 7} }
126};
127
128static void reset(void __iomem *docptr)
129{
130
131
132 writew(DOC_ASICMODE_RESET | DOC_ASICMODE_MDWREN, docptr + DOC_ASICMODE);
133 writew(~(DOC_ASICMODE_RESET | DOC_ASICMODE_MDWREN),
134 docptr + DOC_ASICMODECONFIRM);
135 write_nop(docptr);
136
137 writew(DOC_ASICMODE_NORMAL | DOC_ASICMODE_MDWREN,
138 docptr + DOC_ASICMODE);
139 writew(~(DOC_ASICMODE_NORMAL | DOC_ASICMODE_MDWREN),
140 docptr + DOC_ASICMODECONFIRM);
141
142 writew(DOC_ECCCONF1_ECC_ENABLE, docptr + DOC_ECCCONF1);
143
144 poll_status(docptr);
145}
146
147static void docg4_select_chip(struct mtd_info *mtd, int chip)
148{
149
150
151
152
153 void __iomem *docptr = CONFIG_SYS_NAND_BASE;
154
155 if (chip < 0)
156 return;
157
158 if (chip > 0)
159 printf("multiple floors currently unsupported\n");
160
161 writew(0, docptr + DOC_DEVICESELECT);
162}
163
164static void read_hw_ecc(void __iomem *docptr, uint8_t *ecc_buf)
165{
166
167
168 int i;
169 for (i = 0; i < 7; i++) {
170 ecc_buf[i] = readb(docptr + DOC_BCH_SYNDROM(i));
171 ecc_buf[i] = readb(docptr + DOC_BCH_SYNDROM(i));
172 }
173}
174
175static int correct_data(struct mtd_info *mtd, uint8_t *buf, int page)
176{
177
178
179
180
181
182 struct nand_chip *nand = mtd->priv;
183 struct docg4_priv *doc = nand->priv;
184 void __iomem *docptr = CONFIG_SYS_NAND_BASE;
185 int i, numerrs;
186 unsigned int errpos[4];
187 const uint8_t blank_read_hwecc[8] = {
188 0xcf, 0x72, 0xfc, 0x1b, 0xa9, 0xc7, 0xb9, 0 };
189
190 read_hw_ecc(docptr, doc->ecc_buf);
191
192
193 if (!memcmp(doc->ecc_buf, blank_read_hwecc, 7))
194 return 0;
195
196
197 if (!ignore_badblocks) {
198
199
200
201
202
203
204
205
206
207 if (doc->oob_buf[15]) {
208 int bit, numsetbits = 0;
209 unsigned long written_flag = doc->oob_buf[15];
210
211 for (bit = 0; bit < 8; bit++) {
212 if (written_flag & 0x01)
213 numsetbits++;
214 written_flag >>= 1;
215 }
216 if (numsetbits > 4) {
217 printf("errors in blank page at offset %08x\n",
218 page * DOCG4_PAGE_SIZE);
219 return 0;
220 }
221 }
222 }
223
224
225
226
227
228
229
230
231 for (i = 0; i < 7; i++)
232 doc->ecc_buf[i] = bitrev8(doc->ecc_buf[i]);
233
234 numerrs = decode_bch(doc->bch, NULL, DOCG4_USERDATA_LEN, NULL,
235 doc->ecc_buf, NULL, errpos);
236
237 if (numerrs == -EBADMSG) {
238 printf("uncorrectable errors at offset %08x\n",
239 page * DOCG4_PAGE_SIZE);
240 return -EBADMSG;
241 }
242
243 BUG_ON(numerrs < 0);
244
245
246 for (i = 0; i < numerrs; i++)
247 errpos[i] = (errpos[i] & ~7)|(7-(errpos[i] & 7));
248
249
250 for (i = 0; i < numerrs; i++) {
251
252 if (errpos[i] > DOCG4_USERDATA_LEN * 8)
253 continue;
254
255
256 if (errpos[i] > DOCG4_PAGE_SIZE * 8)
257 __change_bit(errpos[i] - DOCG4_PAGE_SIZE * 8,
258 (unsigned long *)doc->oob_buf);
259
260 else
261 __change_bit(errpos[i], (unsigned long *)buf);
262 }
263
264 printf("%d error(s) corrected at offset %08x\n",
265 numerrs, page * DOCG4_PAGE_SIZE);
266
267 return numerrs;
268}
269
270static int read_progstatus(struct docg4_priv *doc, void __iomem *docptr)
271{
272
273
274
275
276
277
278
279 uint16_t status1 = readw(docptr + DOC_IOSPACE_DATA);
280 uint16_t status2 = readw(docptr + DOC_IOSPACE_DATA);
281 uint16_t status3 = readw(docptr + DOCG4_MYSTERY_REG);
282
283 MTDDEBUG(MTD_DEBUG_LEVEL3, "docg4: %s: %02x %02x %02x\n",
284 __func__, status1, status2, status3);
285
286 if (status1 != DOCG4_PROGSTATUS_GOOD ||
287 status2 != DOCG4_PROGSTATUS_GOOD_2 ||
288 status3 != DOCG4_PROGSTATUS_GOOD_2) {
289 doc->status = NAND_STATUS_FAIL;
290 printf("read_progstatus failed: %02x, %02x, %02x\n",
291 status1, status2, status3);
292 return -EIO;
293 }
294 return 0;
295}
296
297static int pageprog(struct mtd_info *mtd)
298{
299
300
301
302
303
304 struct nand_chip *nand = mtd->priv;
305 struct docg4_priv *doc = nand->priv;
306 void __iomem *docptr = CONFIG_SYS_NAND_BASE;
307 int retval = 0;
308
309 MTDDEBUG(MTD_DEBUG_LEVEL3, "docg4: %s\n", __func__);
310
311 writew(DOCG4_SEQ_PAGEPROG, docptr + DOC_FLASHSEQUENCE);
312 writew(DOC_CMD_PROG_CYCLE2, docptr + DOC_FLASHCOMMAND);
313 write_nop(docptr);
314 write_nop(docptr);
315
316
317 poll_status(docptr);
318
319 writew(DOCG4_SEQ_FLUSH, docptr + DOC_FLASHSEQUENCE);
320 writew(DOCG4_CMD_FLUSH, docptr + DOC_FLASHCOMMAND);
321 writew(DOC_ECCCONF0_READ_MODE | 4, docptr + DOC_ECCCONF0);
322 write_nop(docptr);
323 write_nop(docptr);
324 write_nop(docptr);
325 write_nop(docptr);
326 write_nop(docptr);
327
328 retval = read_progstatus(doc, docptr);
329 writew(0, docptr + DOC_DATAEND);
330 write_nop(docptr);
331 poll_status(docptr);
332 write_nop(docptr);
333
334 return retval;
335}
336
337static void sequence_reset(void __iomem *docptr)
338{
339
340
341 writew(DOC_CTRL_UNKNOWN | DOC_CTRL_CE, docptr + DOC_FLASHCONTROL);
342 writew(DOC_SEQ_RESET, docptr + DOC_FLASHSEQUENCE);
343 writew(DOC_CMD_RESET, docptr + DOC_FLASHCOMMAND);
344 write_nop(docptr);
345 write_nop(docptr);
346 poll_status(docptr);
347 write_nop(docptr);
348}
349
350static void read_page_prologue(void __iomem *docptr, uint32_t docg4_addr)
351{
352
353
354 sequence_reset(docptr);
355
356 writew(DOCG4_SEQ_PAGE_READ, docptr + DOC_FLASHSEQUENCE);
357 writew(DOCG4_CMD_PAGE_READ, docptr + DOC_FLASHCOMMAND);
358 write_nop(docptr);
359
360 write_addr(docptr, docg4_addr);
361
362 write_nop(docptr);
363 writew(DOCG4_CMD_READ2, docptr + DOC_FLASHCOMMAND);
364 write_nop(docptr);
365 write_nop(docptr);
366
367 poll_status(docptr);
368}
369
370static void write_page_prologue(void __iomem *docptr, uint32_t docg4_addr)
371{
372
373
374 sequence_reset(docptr);
375 writew(DOCG4_SEQ_PAGEWRITE, docptr + DOC_FLASHSEQUENCE);
376 writew(DOCG4_CMD_PAGEWRITE, docptr + DOC_FLASHCOMMAND);
377 write_nop(docptr);
378 write_addr(docptr, docg4_addr);
379 write_nop(docptr);
380 write_nop(docptr);
381 poll_status(docptr);
382}
383
384static uint32_t mtd_to_docg4_address(int page, int column)
385{
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415 int g4_page = page / 4;
416 int g4_index = (page % 4) * 0x108 + column/2;
417 return (g4_page << 16) | g4_index;
418}
419
420static void docg4_command(struct mtd_info *mtd, unsigned command, int column,
421 int page_addr)
422{
423
424
425 struct nand_chip *nand = mtd->priv;
426 struct docg4_priv *doc = nand->priv;
427 uint32_t g4_addr = mtd_to_docg4_address(page_addr, column);
428
429 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s %x, page_addr=%x, column=%x\n",
430 __func__, command, page_addr, column);
431
432
433
434
435
436 doc->last_command.command = command;
437 doc->last_command.column = column;
438 doc->last_command.page = page_addr;
439
440 switch (command) {
441 case NAND_CMD_RESET:
442 reset(CONFIG_SYS_NAND_BASE);
443 break;
444
445 case NAND_CMD_READ0:
446 read_page_prologue(CONFIG_SYS_NAND_BASE, g4_addr);
447 break;
448
449 case NAND_CMD_STATUS:
450
451 break;
452
453 case NAND_CMD_SEQIN:
454 write_page_prologue(CONFIG_SYS_NAND_BASE, g4_addr);
455
456
457 if (doc->oob_page == page_addr)
458 memcpy(nand->oob_poi, doc->oob_buf, 16);
459 break;
460
461 case NAND_CMD_PAGEPROG:
462 pageprog(mtd);
463 break;
464
465
466 case NAND_CMD_READOOB:
467 case NAND_CMD_READID:
468 case NAND_CMD_ERASE1:
469 case NAND_CMD_ERASE2:
470 printf("docg4_command: unexpected nand command 0x%x\n",
471 command);
472 break;
473 }
474}
475
476static void docg4_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
477{
478 int i;
479 struct nand_chip *nand = mtd->priv;
480 uint16_t *p = (uint16_t *)buf;
481 len >>= 1;
482
483 for (i = 0; i < len; i++)
484 p[i] = readw(nand->IO_ADDR_R);
485}
486
487static int docg4_read_oob(struct mtd_info *mtd, struct nand_chip *nand,
488 int page)
489{
490 struct docg4_priv *doc = nand->priv;
491 void __iomem *docptr = CONFIG_SYS_NAND_BASE;
492 uint16_t status;
493
494 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: page %x\n", __func__, page);
495
496
497
498
499
500
501
502 if (doc->last_command.command == NAND_CMD_READ0 &&
503 doc->last_command.page == page) {
504 memcpy(nand->oob_poi, doc->oob_buf, 16);
505 return 0;
506 }
507
508
509
510
511 docg4_command(mtd, NAND_CMD_READ0, nand->ecc.size, page);
512
513 writew(DOC_ECCCONF0_READ_MODE | DOCG4_OOB_SIZE, docptr + DOC_ECCCONF0);
514 write_nop(docptr);
515 write_nop(docptr);
516 write_nop(docptr);
517 write_nop(docptr);
518 write_nop(docptr);
519
520
521 status = readw(docptr + DOC_IOSPACE_DATA);
522 if (status & DOCG4_READ_ERROR) {
523 printf("docg4_read_oob failed: status = 0x%02x\n", status);
524 return -EIO;
525 }
526
527 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: status = 0x%x\n", __func__, status);
528
529 docg4_read_buf(mtd, nand->oob_poi, 16);
530
531 write_nop(docptr);
532 write_nop(docptr);
533 write_nop(docptr);
534 writew(0, docptr + DOC_DATAEND);
535 write_nop(docptr);
536
537 return 0;
538}
539
540static int docg4_write_oob(struct mtd_info *mtd, struct nand_chip *nand,
541 int page)
542{
543
544
545
546
547
548
549
550
551
552
553
554
555 struct docg4_priv *doc = nand->priv;
556 doc->oob_page = page;
557 memcpy(doc->oob_buf, nand->oob_poi, 16);
558 return 0;
559}
560
561static int docg4_block_neverbad(struct mtd_info *mtd, loff_t ofs, int getchip)
562{
563
564 return 0;
565}
566
567static void docg4_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
568{
569 int i;
570 struct nand_chip *nand = mtd->priv;
571 uint16_t *p = (uint16_t *)buf;
572 len >>= 1;
573
574 for (i = 0; i < len; i++)
575 writew(p[i], nand->IO_ADDR_W);
576}
577
578static int write_page(struct mtd_info *mtd, struct nand_chip *nand,
579 const uint8_t *buf, int use_ecc)
580{
581 void __iomem *docptr = CONFIG_SYS_NAND_BASE;
582 uint8_t ecc_buf[8];
583
584 writew(DOC_ECCCONF0_ECC_ENABLE |
585 DOC_ECCCONF0_UNKNOWN |
586 DOCG4_BCH_SIZE,
587 docptr + DOC_ECCCONF0);
588 write_nop(docptr);
589
590
591 docg4_write_buf16(mtd, buf, DOCG4_PAGE_SIZE);
592
593
594 docg4_write_buf16(mtd, nand->oob_poi, 6);
595
596
597 writew(nand->oob_poi[6], docptr + DOCG4_OOB_6_7);
598
599 write_nop(docptr);
600 write_nop(docptr);
601
602
603 if (likely(use_ecc)) {
604
605 uint8_t hamming = readb(docptr + DOC_HAMMINGPARITY);
606 hamming = readb(docptr + DOC_HAMMINGPARITY);
607 writew(hamming, docptr + DOCG4_OOB_6_7);
608 write_nop(docptr);
609
610
611 read_hw_ecc(docptr, ecc_buf);
612 ecc_buf[7] = 0;
613 }
614
615
616 else {
617 writew(nand->oob_poi[7], docptr + DOCG4_OOB_6_7);
618 write_nop(docptr);
619 memcpy(ecc_buf, &nand->oob_poi[8], 8);
620 }
621
622 docg4_write_buf16(mtd, ecc_buf, 8);
623 write_nop(docptr);
624 write_nop(docptr);
625 writew(0, docptr + DOC_DATAEND);
626 write_nop(docptr);
627
628 return 0;
629}
630
631static int docg4_write_page_raw(struct mtd_info *mtd, struct nand_chip *nand,
632 const uint8_t *buf, int oob_required)
633{
634 return write_page(mtd, nand, buf, 0);
635}
636
637static int docg4_write_page(struct mtd_info *mtd, struct nand_chip *nand,
638 const uint8_t *buf, int oob_required)
639{
640 return write_page(mtd, nand, buf, 1);
641}
642
643static int read_page(struct mtd_info *mtd, struct nand_chip *nand,
644 uint8_t *buf, int page, int use_ecc)
645{
646 struct docg4_priv *doc = nand->priv;
647 void __iomem *docptr = CONFIG_SYS_NAND_BASE;
648 uint16_t status, edc_err, *buf16;
649
650 writew(DOC_ECCCONF0_READ_MODE |
651 DOC_ECCCONF0_ECC_ENABLE |
652 DOC_ECCCONF0_UNKNOWN |
653 DOCG4_BCH_SIZE,
654 docptr + DOC_ECCCONF0);
655 write_nop(docptr);
656 write_nop(docptr);
657 write_nop(docptr);
658 write_nop(docptr);
659 write_nop(docptr);
660
661
662 status = readw(docptr + DOC_IOSPACE_DATA);
663 if (status & DOCG4_READ_ERROR) {
664 printf("docg4_read_page: bad status: 0x%02x\n", status);
665 writew(0, docptr + DOC_DATAEND);
666 return -EIO;
667 }
668
669 docg4_read_buf(mtd, buf, DOCG4_PAGE_SIZE);
670
671
672 docg4_read_buf(mtd, nand->oob_poi, 14);
673
674
675 buf16 = (uint16_t *)(nand->oob_poi + 14);
676 *buf16 = readw(docptr + DOCG4_MYSTERY_REG);
677
678
679
680
681
682
683
684 memcpy(doc->oob_buf, nand->oob_poi, 16);
685
686 write_nop(docptr);
687
688 if (likely(use_ecc)) {
689
690 edc_err = readw(docptr + DOC_ECCCONF1);
691 edc_err = readw(docptr + DOC_ECCCONF1);
692
693
694 if (edc_err & DOC_ECCCONF1_BCH_SYNDROM_ERR) {
695 int bits_corrected = correct_data(mtd, buf, page);
696 if (bits_corrected == -EBADMSG)
697 mtd->ecc_stats.failed++;
698 else
699 mtd->ecc_stats.corrected += bits_corrected;
700 }
701 }
702
703 writew(0, docptr + DOC_DATAEND);
704 return 0;
705}
706
707
708static int docg4_read_page_raw(struct mtd_info *mtd, struct nand_chip *nand,
709 uint8_t *buf, int oob_required, int page)
710{
711 return read_page(mtd, nand, buf, page, 0);
712}
713
714static int docg4_read_page(struct mtd_info *mtd, struct nand_chip *nand,
715 uint8_t *buf, int oob_required, int page)
716{
717 return read_page(mtd, nand, buf, page, 1);
718}
719
720static void docg4_erase_block(struct mtd_info *mtd, int page)
721{
722 struct nand_chip *nand = mtd->priv;
723 struct docg4_priv *doc = nand->priv;
724 void __iomem *docptr = CONFIG_SYS_NAND_BASE;
725 uint16_t g4_page;
726
727 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: page %04x\n", __func__, page);
728
729 sequence_reset(docptr);
730
731 writew(DOCG4_SEQ_BLOCKERASE, docptr + DOC_FLASHSEQUENCE);
732 writew(DOC_CMD_PROG_BLOCK_ADDR, docptr + DOC_FLASHCOMMAND);
733 write_nop(docptr);
734
735
736 g4_page = (uint16_t)(page / 4);
737 writeb(g4_page & 0xff, docptr + DOC_FLASHADDRESS);
738 g4_page >>= 8;
739 writeb(g4_page & 0xff, docptr + DOC_FLASHADDRESS);
740 write_nop(docptr);
741
742
743 writew(DOC_CMD_ERASECYCLE2, docptr + DOC_FLASHCOMMAND);
744 write_nop(docptr);
745 write_nop(docptr);
746
747 poll_status(docptr);
748 writew(DOCG4_SEQ_FLUSH, docptr + DOC_FLASHSEQUENCE);
749 writew(DOCG4_CMD_FLUSH, docptr + DOC_FLASHCOMMAND);
750 writew(DOC_ECCCONF0_READ_MODE | 4, docptr + DOC_ECCCONF0);
751 write_nop(docptr);
752 write_nop(docptr);
753 write_nop(docptr);
754 write_nop(docptr);
755 write_nop(docptr);
756
757 read_progstatus(doc, docptr);
758
759 writew(0, docptr + DOC_DATAEND);
760 write_nop(docptr);
761 poll_status(docptr);
762 write_nop(docptr);
763}
764
765static int read_factory_bbt(struct mtd_info *mtd)
766{
767
768
769
770
771
772 struct nand_chip *nand = mtd->priv;
773 uint32_t g4_addr = mtd_to_docg4_address(DOCG4_FACTORY_BBT_PAGE, 0);
774 uint8_t *buf;
775 int i, block, status;
776
777 buf = kzalloc(DOCG4_PAGE_SIZE, GFP_KERNEL);
778 if (buf == NULL)
779 return -ENOMEM;
780
781 read_page_prologue(CONFIG_SYS_NAND_BASE, g4_addr);
782 status = docg4_read_page(mtd, nand, buf, 0, DOCG4_FACTORY_BBT_PAGE);
783 if (status)
784 goto exit;
785
786
787
788
789
790
791
792
793 if (nand->bbt == NULL)
794 goto exit;
795
796
797
798
799
800
801 for (i = block = 0; block < DOCG4_NUMBLOCKS; block += 8, i++) {
802 int bitnum;
803 uint8_t mask;
804 for (bitnum = 0, mask = 0x80;
805 bitnum < 8; bitnum++, mask >>= 1) {
806 if (!(buf[i] & mask)) {
807 int badblock = block + bitnum;
808 nand->bbt[badblock / 4] |=
809 0x03 << ((badblock % 4) * 2);
810 mtd->ecc_stats.badblocks++;
811 printf("factory-marked bad block: %d\n",
812 badblock);
813 }
814 }
815 }
816 exit:
817 kfree(buf);
818 return status;
819}
820
821static int docg4_block_markbad(struct mtd_info *mtd, loff_t ofs)
822{
823
824
825
826
827
828
829
830
831
832 int ret, i;
833 uint8_t *buf;
834 struct nand_chip *nand = mtd->priv;
835 struct nand_bbt_descr *bbtd = nand->badblock_pattern;
836 int block = (int)(ofs >> nand->bbt_erase_shift);
837 int page = (int)(ofs >> nand->page_shift);
838 uint32_t g4_addr = mtd_to_docg4_address(page, 0);
839
840 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: %08llx\n", __func__, ofs);
841
842 if (unlikely(ofs & (DOCG4_BLOCK_SIZE - 1)))
843 printf("%s: ofs %llx not start of block!\n",
844 __func__, ofs);
845
846
847 buf = kzalloc(DOCG4_PAGE_SIZE, GFP_KERNEL);
848 if (buf == NULL)
849 return -ENOMEM;
850
851
852 nand->bbt[block / 4] |= 0x01 << ((block & 0x03) * 2);
853
854
855 memset(nand->oob_poi, 0xff, mtd->oobsize);
856 for (i = 0; i < bbtd->len; i++)
857 nand->oob_poi[bbtd->offs + i] = ~bbtd->pattern[i];
858
859
860 write_page_prologue(CONFIG_SYS_NAND_BASE, g4_addr);
861 docg4_write_page(mtd, nand, buf, 1);
862 ret = pageprog(mtd);
863 if (!ret)
864 mtd->ecc_stats.badblocks++;
865
866 kfree(buf);
867
868 return ret;
869}
870
871static uint8_t docg4_read_byte(struct mtd_info *mtd)
872{
873 struct nand_chip *nand = mtd->priv;
874 struct docg4_priv *doc = nand->priv;
875
876 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s\n", __func__);
877
878 if (doc->last_command.command == NAND_CMD_STATUS) {
879 int status;
880
881
882
883
884
885
886 doc->last_command.command = 0;
887
888 if (doc->status) {
889 status = doc->status;
890 doc->status = 0;
891 }
892
893
894 else
895 status = NAND_STATUS_WP | NAND_STATUS_READY;
896
897 return status;
898 }
899
900 printf("unexpectd call to read_byte()\n");
901
902 return 0;
903}
904
905static int docg4_wait(struct mtd_info *mtd, struct nand_chip *nand)
906{
907 struct docg4_priv *doc = nand->priv;
908 int status = NAND_STATUS_WP;
909 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s...\n", __func__);
910
911
912 if (doc->status) {
913 status |= doc->status;
914 doc->status = 0;
915 return status;
916 }
917
918 status |= poll_status(CONFIG_SYS_NAND_BASE);
919 return status;
920}
921
922int docg4_nand_init(struct mtd_info *mtd, struct nand_chip *nand, int devnum)
923{
924 uint16_t id1, id2;
925 struct docg4_priv *docg4;
926 int retval;
927
928 docg4 = kzalloc(sizeof(*docg4), GFP_KERNEL);
929 if (!docg4)
930 return -1;
931
932 mtd->priv = nand;
933 nand->priv = docg4;
934
935
936
937
938
939 mtd->size = DOCG4_CHIP_SIZE;
940 mtd->name = "Msys_Diskonchip_G4";
941 mtd->writesize = DOCG4_PAGE_SIZE;
942 mtd->erasesize = DOCG4_BLOCK_SIZE;
943 mtd->oobsize = DOCG4_OOB_SIZE;
944
945 nand->IO_ADDR_R =
946 (void __iomem *)CONFIG_SYS_NAND_BASE + DOC_IOSPACE_DATA;
947 nand->IO_ADDR_W = nand->IO_ADDR_R;
948 nand->chipsize = DOCG4_CHIP_SIZE;
949 nand->chip_shift = DOCG4_CHIP_SHIFT;
950 nand->bbt_erase_shift = DOCG4_ERASE_SHIFT;
951 nand->phys_erase_shift = DOCG4_ERASE_SHIFT;
952 nand->chip_delay = 20;
953 nand->page_shift = DOCG4_PAGE_SHIFT;
954 nand->pagemask = 0x3ffff;
955 nand->badblockpos = NAND_LARGE_BADBLOCK_POS;
956 nand->badblockbits = 8;
957 nand->ecc.layout = &docg4_oobinfo;
958 nand->ecc.mode = NAND_ECC_HW_SYNDROME;
959 nand->ecc.size = DOCG4_PAGE_SIZE;
960 nand->ecc.prepad = 8;
961 nand->ecc.bytes = 8;
962 nand->ecc.strength = DOCG4_T;
963 nand->options = NAND_BUSWIDTH_16 | NAND_NO_SUBPAGE_WRITE;
964 nand->controller = &nand->hwcontrol;
965
966
967 nand->cmdfunc = docg4_command;
968 nand->waitfunc = docg4_wait;
969 nand->select_chip = docg4_select_chip;
970 nand->read_byte = docg4_read_byte;
971 nand->block_markbad = docg4_block_markbad;
972 nand->read_buf = docg4_read_buf;
973 nand->write_buf = docg4_write_buf16;
974 nand->scan_bbt = nand_default_bbt;
975 nand->erase_cmd = docg4_erase_block;
976 nand->ecc.read_page = docg4_read_page;
977 nand->ecc.write_page = docg4_write_page;
978 nand->ecc.read_page_raw = docg4_read_page_raw;
979 nand->ecc.write_page_raw = docg4_write_page_raw;
980 nand->ecc.read_oob = docg4_read_oob;
981 nand->ecc.write_oob = docg4_write_oob;
982
983
984
985
986
987
988
989 if (ignore_badblocks) {
990 nand->options |= NAND_SKIP_BBTSCAN;
991 nand->block_bad = docg4_block_neverbad;
992 }
993
994 reset(CONFIG_SYS_NAND_BASE);
995
996
997 id1 = readw(CONFIG_SYS_NAND_BASE + DOC_CHIPID);
998 id1 = readw(CONFIG_SYS_NAND_BASE + DOCG4_MYSTERY_REG);
999 id2 = readw(CONFIG_SYS_NAND_BASE + DOC_CHIPID_INV);
1000 id2 = readw(CONFIG_SYS_NAND_BASE + DOCG4_MYSTERY_REG);
1001 if (id1 != DOCG4_IDREG1_VALUE || id2 != DOCG4_IDREG2_VALUE)
1002 return -1;
1003
1004
1005 docg4->bch = init_bch(DOCG4_M, DOCG4_T, DOCG4_PRIMITIVE_POLY);
1006 if (docg4->bch == NULL)
1007 return -1;
1008
1009 retval = nand_scan_tail(mtd);
1010 if (retval)
1011 return -1;
1012
1013
1014
1015
1016
1017 nand->scan_bbt(mtd);
1018 nand->options |= NAND_BBT_SCANNED;
1019 retval = read_factory_bbt(mtd);
1020 if (retval)
1021 return -1;
1022
1023 retval = nand_register(devnum);
1024 if (retval)
1025 return -1;
1026
1027 return 0;
1028}
1029