1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <common.h>
21
22#if !defined(CONFIG_NAND_LEGACY)
23
24#include <linux/kernel.h>
25#include <linux/init.h>
26#include <linux/sched.h>
27#include <linux/delay.h>
28#include <linux/rslib.h>
29#include <linux/moduleparam.h>
30#include <asm/io.h>
31
32#include <linux/mtd/mtd.h>
33#include <linux/mtd/nand.h>
34#include <linux/mtd/doc2000.h>
35#include <linux/mtd/compatmac.h>
36#include <linux/mtd/partitions.h>
37#include <linux/mtd/inftl.h>
38
39
40#ifndef CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS
41#define CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS 0
42#endif
43
44static unsigned long __initdata doc_locations[] = {
45#if defined (__alpha__) || defined(__i386__) || defined(__x86_64__)
46#ifdef CONFIG_MTD_NAND_DISKONCHIP_PROBE_HIGH
47 0xfffc8000, 0xfffca000, 0xfffcc000, 0xfffce000,
48 0xfffd0000, 0xfffd2000, 0xfffd4000, 0xfffd6000,
49 0xfffd8000, 0xfffda000, 0xfffdc000, 0xfffde000,
50 0xfffe0000, 0xfffe2000, 0xfffe4000, 0xfffe6000,
51 0xfffe8000, 0xfffea000, 0xfffec000, 0xfffee000,
52#else
53 0xc8000, 0xca000, 0xcc000, 0xce000,
54 0xd0000, 0xd2000, 0xd4000, 0xd6000,
55 0xd8000, 0xda000, 0xdc000, 0xde000,
56 0xe0000, 0xe2000, 0xe4000, 0xe6000,
57 0xe8000, 0xea000, 0xec000, 0xee000,
58#endif
59#else
60#warning Unknown architecture for DiskOnChip. No default probe locations defined
61#endif
62 0xffffffff };
63
64static struct mtd_info *doclist = NULL;
65
66struct doc_priv {
67 void __iomem *virtadr;
68 unsigned long physadr;
69 u_char ChipID;
70 u_char CDSNControl;
71 int chips_per_floor;
72 int curfloor;
73 int curchip;
74 int mh0_page;
75 int mh1_page;
76 struct mtd_info *nextdoc;
77};
78
79
80
81static u_char empty_read_syndrome[6] = { 0x26, 0xff, 0x6d, 0x47, 0x73, 0x7a };
82
83
84
85static u_char empty_write_ecc[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
86
87#define INFTL_BBT_RESERVED_BLOCKS 4
88
89#define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
90#define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
91#define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
92
93static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
94 unsigned int bitmask);
95static void doc200x_select_chip(struct mtd_info *mtd, int chip);
96
97static int debug = 0;
98module_param(debug, int, 0);
99
100static int try_dword = 1;
101module_param(try_dword, int, 0);
102
103static int no_ecc_failures = 0;
104module_param(no_ecc_failures, int, 0);
105
106static int no_autopart = 0;
107module_param(no_autopart, int, 0);
108
109static int show_firmware_partition = 0;
110module_param(show_firmware_partition, int, 0);
111
112#ifdef CONFIG_MTD_NAND_DISKONCHIP_BBTWRITE
113static int inftl_bbt_write = 1;
114#else
115static int inftl_bbt_write = 0;
116#endif
117module_param(inftl_bbt_write, int, 0);
118
119static unsigned long doc_config_location = CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS;
120module_param(doc_config_location, ulong, 0);
121MODULE_PARM_DESC(doc_config_location, "Physical memory address at which to probe for DiskOnChip");
122
123
124#define SECTOR_SIZE 512
125
126#define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / 10)
127
128#define NROOTS 4
129
130#define FCR 510
131
132#define NN 1023
133
134
135static struct rs_control *rs_decoder;
136
137
138
139
140
141
142
143
144
145
146static int doc_ecc_decode(struct rs_control *rs, uint8_t *data, uint8_t *ecc)
147{
148 int i, j, nerr, errpos[8];
149 uint8_t parity;
150 uint16_t ds[4], s[5], tmp, errval[8], syn[4];
151
152
153 ds[0] = ((ecc[4] & 0xff) >> 0) | ((ecc[5] & 0x03) << 8);
154 ds[1] = ((ecc[5] & 0xfc) >> 2) | ((ecc[2] & 0x0f) << 6);
155 ds[2] = ((ecc[2] & 0xf0) >> 4) | ((ecc[3] & 0x3f) << 4);
156 ds[3] = ((ecc[3] & 0xc0) >> 6) | ((ecc[0] & 0xff) << 2);
157 parity = ecc[1];
158
159
160 for (i = 0; i < NROOTS; i++)
161 s[i] = ds[0];
162
163
164
165
166
167 for (j = 1; j < NROOTS; j++) {
168 if (ds[j] == 0)
169 continue;
170 tmp = rs->index_of[ds[j]];
171 for (i = 0; i < NROOTS; i++)
172 s[i] ^= rs->alpha_to[rs_modnn(rs, tmp + (FCR + i) * j)];
173 }
174
175
176 for (i = 0; i < NROOTS; i++) {
177 if (syn[i])
178 syn[i] = rs_modnn(rs, rs->index_of[s[i]] + (NN - FCR - i));
179 }
180
181 nerr = decode_rs16(rs, NULL, NULL, 1019, syn, 0, errpos, 0, errval);
182
183
184 if (nerr < 0)
185 return nerr;
186
187
188
189
190
191
192 for (i = 0; i < nerr; i++) {
193 int index, bitpos, pos = 1015 - errpos[i];
194 uint8_t val;
195 if (pos >= NB_DATA && pos < 1019)
196 continue;
197 if (pos < NB_DATA) {
198
199 pos = 10 * (NB_DATA - 1 - pos) - 6;
200
201
202 index = (pos >> 3) ^ 1;
203 bitpos = pos & 7;
204 if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
205 val = (uint8_t) (errval[i] >> (2 + bitpos));
206 parity ^= val;
207 if (index < SECTOR_SIZE)
208 data[index] ^= val;
209 }
210 index = ((pos >> 3) + 1) ^ 1;
211 bitpos = (bitpos + 10) & 7;
212 if (bitpos == 0)
213 bitpos = 8;
214 if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
215 val = (uint8_t) (errval[i] << (8 - bitpos));
216 parity ^= val;
217 if (index < SECTOR_SIZE)
218 data[index] ^= val;
219 }
220 }
221 }
222
223 return parity ? -EBADMSG : nerr;
224}
225
226static void DoC_Delay(struct doc_priv *doc, unsigned short cycles)
227{
228 volatile char dummy;
229 int i;
230
231 for (i = 0; i < cycles; i++) {
232 if (DoC_is_Millennium(doc))
233 dummy = ReadDOC(doc->virtadr, NOP);
234 else if (DoC_is_MillenniumPlus(doc))
235 dummy = ReadDOC(doc->virtadr, Mplus_NOP);
236 else
237 dummy = ReadDOC(doc->virtadr, DOCStatus);
238 }
239
240}
241
242#define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
243
244
245static int _DoC_WaitReady(struct doc_priv *doc)
246{
247 void __iomem *docptr = doc->virtadr;
248 unsigned long timeo = jiffies + (HZ * 10);
249
250 if (debug)
251 printk("_DoC_WaitReady...\n");
252
253 if (DoC_is_MillenniumPlus(doc)) {
254 while ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
255 if (time_after(jiffies, timeo)) {
256 printk("_DoC_WaitReady timed out.\n");
257 return -EIO;
258 }
259 udelay(1);
260 cond_resched();
261 }
262 } else {
263 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
264 if (time_after(jiffies, timeo)) {
265 printk("_DoC_WaitReady timed out.\n");
266 return -EIO;
267 }
268 udelay(1);
269 cond_resched();
270 }
271 }
272
273 return 0;
274}
275
276static inline int DoC_WaitReady(struct doc_priv *doc)
277{
278 void __iomem *docptr = doc->virtadr;
279 int ret = 0;
280
281 if (DoC_is_MillenniumPlus(doc)) {
282 DoC_Delay(doc, 4);
283
284 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
285
286 ret = _DoC_WaitReady(doc);
287 } else {
288 DoC_Delay(doc, 4);
289
290 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
291
292 ret = _DoC_WaitReady(doc);
293 DoC_Delay(doc, 2);
294 }
295
296 if (debug)
297 printk("DoC_WaitReady OK\n");
298 return ret;
299}
300
301static void doc2000_write_byte(struct mtd_info *mtd, u_char datum)
302{
303 struct nand_chip *this = mtd->priv;
304 struct doc_priv *doc = this->priv;
305 void __iomem *docptr = doc->virtadr;
306
307 if (debug)
308 printk("write_byte %02x\n", datum);
309 WriteDOC(datum, docptr, CDSNSlowIO);
310 WriteDOC(datum, docptr, 2k_CDSN_IO);
311}
312
313static u_char doc2000_read_byte(struct mtd_info *mtd)
314{
315 struct nand_chip *this = mtd->priv;
316 struct doc_priv *doc = this->priv;
317 void __iomem *docptr = doc->virtadr;
318 u_char ret;
319
320 ReadDOC(docptr, CDSNSlowIO);
321 DoC_Delay(doc, 2);
322 ret = ReadDOC(docptr, 2k_CDSN_IO);
323 if (debug)
324 printk("read_byte returns %02x\n", ret);
325 return ret;
326}
327
328static void doc2000_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
329{
330 struct nand_chip *this = mtd->priv;
331 struct doc_priv *doc = this->priv;
332 void __iomem *docptr = doc->virtadr;
333 int i;
334 if (debug)
335 printk("writebuf of %d bytes: ", len);
336 for (i = 0; i < len; i++) {
337 WriteDOC_(buf[i], docptr, DoC_2k_CDSN_IO + i);
338 if (debug && i < 16)
339 printk("%02x ", buf[i]);
340 }
341 if (debug)
342 printk("\n");
343}
344
345static void doc2000_readbuf(struct mtd_info *mtd, u_char *buf, int len)
346{
347 struct nand_chip *this = mtd->priv;
348 struct doc_priv *doc = this->priv;
349 void __iomem *docptr = doc->virtadr;
350 int i;
351
352 if (debug)
353 printk("readbuf of %d bytes: ", len);
354
355 for (i = 0; i < len; i++) {
356 buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
357 }
358}
359
360static void doc2000_readbuf_dword(struct mtd_info *mtd,
361 u_char *buf, int len)
362{
363 struct nand_chip *this = mtd->priv;
364 struct doc_priv *doc = this->priv;
365 void __iomem *docptr = doc->virtadr;
366 int i;
367
368 if (debug)
369 printk("readbuf_dword of %d bytes: ", len);
370
371 if (unlikely((((unsigned long)buf) | len) & 3)) {
372 for (i = 0; i < len; i++) {
373 *(uint8_t *) (&buf[i]) = ReadDOC(docptr, 2k_CDSN_IO + i);
374 }
375 } else {
376 for (i = 0; i < len; i += 4) {
377 *(uint32_t*) (&buf[i]) = readl(docptr + DoC_2k_CDSN_IO + i);
378 }
379 }
380}
381
382static int doc2000_verifybuf(struct mtd_info *mtd, const u_char *buf, int len)
383{
384 struct nand_chip *this = mtd->priv;
385 struct doc_priv *doc = this->priv;
386 void __iomem *docptr = doc->virtadr;
387 int i;
388
389 for (i = 0; i < len; i++)
390 if (buf[i] != ReadDOC(docptr, 2k_CDSN_IO))
391 return -EFAULT;
392 return 0;
393}
394
395static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr)
396{
397 struct nand_chip *this = mtd->priv;
398 struct doc_priv *doc = this->priv;
399 uint16_t ret;
400
401 doc200x_select_chip(mtd, nr);
402 doc200x_hwcontrol(mtd, NAND_CMD_READID,
403 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
404 doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
405 doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
406
407
408
409
410 udelay(50);
411
412 ret = this->read_byte(mtd) << 8;
413 ret |= this->read_byte(mtd);
414
415 if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) {
416
417 union {
418 uint32_t dword;
419 uint8_t byte[4];
420 } ident;
421 void __iomem *docptr = doc->virtadr;
422
423 doc200x_hwcontrol(mtd, NAND_CMD_READID,
424 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
425 doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
426 doc200x_hwcontrol(mtd, NAND_CMD_NONE,
427 NAND_NCE | NAND_CTRL_CHANGE);
428
429 udelay(50);
430
431 ident.dword = readl(docptr + DoC_2k_CDSN_IO);
432 if (((ident.byte[0] << 8) | ident.byte[1]) == ret) {
433 printk(KERN_INFO "DiskOnChip 2000 responds to DWORD access\n");
434 this->read_buf = &doc2000_readbuf_dword;
435 }
436 }
437
438 return ret;
439}
440
441static void __init doc2000_count_chips(struct mtd_info *mtd)
442{
443 struct nand_chip *this = mtd->priv;
444 struct doc_priv *doc = this->priv;
445 uint16_t mfrid;
446 int i;
447
448
449 doc->chips_per_floor = 4;
450
451
452 mfrid = doc200x_ident_chip(mtd, 0);
453
454
455 for (i = 1; i < 4; i++) {
456 if (doc200x_ident_chip(mtd, i) != mfrid)
457 break;
458 }
459 doc->chips_per_floor = i;
460 printk(KERN_DEBUG "Detected %d chips per floor.\n", i);
461}
462
463static int doc200x_wait(struct mtd_info *mtd, struct nand_chip *this)
464{
465 struct doc_priv *doc = this->priv;
466
467 int status;
468
469 DoC_WaitReady(doc);
470 this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
471 DoC_WaitReady(doc);
472 status = (int)this->read_byte(mtd);
473
474 return status;
475}
476
477static void doc2001_write_byte(struct mtd_info *mtd, u_char datum)
478{
479 struct nand_chip *this = mtd->priv;
480 struct doc_priv *doc = this->priv;
481 void __iomem *docptr = doc->virtadr;
482
483 WriteDOC(datum, docptr, CDSNSlowIO);
484 WriteDOC(datum, docptr, Mil_CDSN_IO);
485 WriteDOC(datum, docptr, WritePipeTerm);
486}
487
488static u_char doc2001_read_byte(struct mtd_info *mtd)
489{
490 struct nand_chip *this = mtd->priv;
491 struct doc_priv *doc = this->priv;
492 void __iomem *docptr = doc->virtadr;
493
494
495
496 DoC_Delay(doc, 2);
497 ReadDOC(docptr, ReadPipeInit);
498
499 return ReadDOC(docptr, LastDataRead);
500}
501
502static void doc2001_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
503{
504 struct nand_chip *this = mtd->priv;
505 struct doc_priv *doc = this->priv;
506 void __iomem *docptr = doc->virtadr;
507 int i;
508
509 for (i = 0; i < len; i++)
510 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
511
512 WriteDOC(0x00, docptr, WritePipeTerm);
513}
514
515static void doc2001_readbuf(struct mtd_info *mtd, u_char *buf, int len)
516{
517 struct nand_chip *this = mtd->priv;
518 struct doc_priv *doc = this->priv;
519 void __iomem *docptr = doc->virtadr;
520 int i;
521
522
523 ReadDOC(docptr, ReadPipeInit);
524
525 for (i = 0; i < len - 1; i++)
526 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
527
528
529 buf[i] = ReadDOC(docptr, LastDataRead);
530}
531
532static int doc2001_verifybuf(struct mtd_info *mtd, const u_char *buf, int len)
533{
534 struct nand_chip *this = mtd->priv;
535 struct doc_priv *doc = this->priv;
536 void __iomem *docptr = doc->virtadr;
537 int i;
538
539
540 ReadDOC(docptr, ReadPipeInit);
541
542 for (i = 0; i < len - 1; i++)
543 if (buf[i] != ReadDOC(docptr, Mil_CDSN_IO)) {
544 ReadDOC(docptr, LastDataRead);
545 return i;
546 }
547 if (buf[i] != ReadDOC(docptr, LastDataRead))
548 return i;
549 return 0;
550}
551
552static u_char doc2001plus_read_byte(struct mtd_info *mtd)
553{
554 struct nand_chip *this = mtd->priv;
555 struct doc_priv *doc = this->priv;
556 void __iomem *docptr = doc->virtadr;
557 u_char ret;
558
559 ReadDOC(docptr, Mplus_ReadPipeInit);
560 ReadDOC(docptr, Mplus_ReadPipeInit);
561 ret = ReadDOC(docptr, Mplus_LastDataRead);
562 if (debug)
563 printk("read_byte returns %02x\n", ret);
564 return ret;
565}
566
567static void doc2001plus_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
568{
569 struct nand_chip *this = mtd->priv;
570 struct doc_priv *doc = this->priv;
571 void __iomem *docptr = doc->virtadr;
572 int i;
573
574 if (debug)
575 printk("writebuf of %d bytes: ", len);
576 for (i = 0; i < len; i++) {
577 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
578 if (debug && i < 16)
579 printk("%02x ", buf[i]);
580 }
581 if (debug)
582 printk("\n");
583}
584
585static void doc2001plus_readbuf(struct mtd_info *mtd, u_char *buf, int len)
586{
587 struct nand_chip *this = mtd->priv;
588 struct doc_priv *doc = this->priv;
589 void __iomem *docptr = doc->virtadr;
590 int i;
591
592 if (debug)
593 printk("readbuf of %d bytes: ", len);
594
595
596 ReadDOC(docptr, Mplus_ReadPipeInit);
597 ReadDOC(docptr, Mplus_ReadPipeInit);
598
599 for (i = 0; i < len - 2; i++) {
600 buf[i] = ReadDOC(docptr, Mil_CDSN_IO);
601 if (debug && i < 16)
602 printk("%02x ", buf[i]);
603 }
604
605
606 buf[len - 2] = ReadDOC(docptr, Mplus_LastDataRead);
607 if (debug && i < 16)
608 printk("%02x ", buf[len - 2]);
609 buf[len - 1] = ReadDOC(docptr, Mplus_LastDataRead);
610 if (debug && i < 16)
611 printk("%02x ", buf[len - 1]);
612 if (debug)
613 printk("\n");
614}
615
616static int doc2001plus_verifybuf(struct mtd_info *mtd, const u_char *buf, int len)
617{
618 struct nand_chip *this = mtd->priv;
619 struct doc_priv *doc = this->priv;
620 void __iomem *docptr = doc->virtadr;
621 int i;
622
623 if (debug)
624 printk("verifybuf of %d bytes: ", len);
625
626
627 ReadDOC(docptr, Mplus_ReadPipeInit);
628 ReadDOC(docptr, Mplus_ReadPipeInit);
629
630 for (i = 0; i < len - 2; i++)
631 if (buf[i] != ReadDOC(docptr, Mil_CDSN_IO)) {
632 ReadDOC(docptr, Mplus_LastDataRead);
633 ReadDOC(docptr, Mplus_LastDataRead);
634 return i;
635 }
636 if (buf[len - 2] != ReadDOC(docptr, Mplus_LastDataRead))
637 return len - 2;
638 if (buf[len - 1] != ReadDOC(docptr, Mplus_LastDataRead))
639 return len - 1;
640 return 0;
641}
642
643static void doc2001plus_select_chip(struct mtd_info *mtd, int chip)
644{
645 struct nand_chip *this = mtd->priv;
646 struct doc_priv *doc = this->priv;
647 void __iomem *docptr = doc->virtadr;
648 int floor = 0;
649
650 if (debug)
651 printk("select chip (%d)\n", chip);
652
653 if (chip == -1) {
654
655 WriteDOC(0, docptr, Mplus_FlashSelect);
656 return;
657 }
658
659 floor = chip / doc->chips_per_floor;
660 chip -= (floor * doc->chips_per_floor);
661
662
663 WriteDOC((DOC_FLASH_CE), docptr, Mplus_FlashSelect);
664 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
665
666 doc->curchip = chip;
667 doc->curfloor = floor;
668}
669
670static void doc200x_select_chip(struct mtd_info *mtd, int chip)
671{
672 struct nand_chip *this = mtd->priv;
673 struct doc_priv *doc = this->priv;
674 void __iomem *docptr = doc->virtadr;
675 int floor = 0;
676
677 if (debug)
678 printk("select chip (%d)\n", chip);
679
680 if (chip == -1)
681 return;
682
683 floor = chip / doc->chips_per_floor;
684 chip -= (floor * doc->chips_per_floor);
685
686
687 doc200x_hwcontrol(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
688
689 WriteDOC(floor, docptr, FloorSelect);
690 WriteDOC(chip, docptr, CDSNDeviceSelect);
691
692 doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
693
694 doc->curchip = chip;
695 doc->curfloor = floor;
696}
697
698#define CDSN_CTRL_MSK (CDSN_CTRL_CE | CDSN_CTRL_CLE | CDSN_CTRL_ALE)
699
700static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
701 unsigned int ctrl)
702{
703 struct nand_chip *this = mtd->priv;
704 struct doc_priv *doc = this->priv;
705 void __iomem *docptr = doc->virtadr;
706
707 if (ctrl & NAND_CTRL_CHANGE) {
708 doc->CDSNControl &= ~CDSN_CTRL_MSK;
709 doc->CDSNControl |= ctrl & CDSN_CTRL_MSK;
710 if (debug)
711 printk("hwcontrol(%d): %02x\n", cmd, doc->CDSNControl);
712 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
713
714 DoC_Delay(doc, 4);
715 }
716 if (cmd != NAND_CMD_NONE) {
717 if (DoC_is_2000(doc))
718 doc2000_write_byte(mtd, cmd);
719 else
720 doc2001_write_byte(mtd, cmd);
721 }
722}
723
724static void doc2001plus_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
725{
726 struct nand_chip *this = mtd->priv;
727 struct doc_priv *doc = this->priv;
728 void __iomem *docptr = doc->virtadr;
729
730
731
732
733
734 if (command == NAND_CMD_PAGEPROG) {
735 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
736 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
737 }
738
739
740
741
742 if (command == NAND_CMD_SEQIN) {
743 int readcmd;
744
745 if (column >= mtd->writesize) {
746
747 column -= mtd->writesize;
748 readcmd = NAND_CMD_READOOB;
749 } else if (column < 256) {
750
751 readcmd = NAND_CMD_READ0;
752 } else {
753 column -= 256;
754 readcmd = NAND_CMD_READ1;
755 }
756 WriteDOC(readcmd, docptr, Mplus_FlashCmd);
757 }
758 WriteDOC(command, docptr, Mplus_FlashCmd);
759 WriteDOC(0, docptr, Mplus_WritePipeTerm);
760 WriteDOC(0, docptr, Mplus_WritePipeTerm);
761
762 if (column != -1 || page_addr != -1) {
763
764 if (column != -1) {
765
766 if (this->options & NAND_BUSWIDTH_16)
767 column >>= 1;
768 WriteDOC(column, docptr, Mplus_FlashAddress);
769 }
770 if (page_addr != -1) {
771 WriteDOC((unsigned char)(page_addr & 0xff), docptr, Mplus_FlashAddress);
772 WriteDOC((unsigned char)((page_addr >> 8) & 0xff), docptr, Mplus_FlashAddress);
773
774 if (this->chipsize & 0x0c000000) {
775 WriteDOC((unsigned char)((page_addr >> 16) & 0x0f), docptr, Mplus_FlashAddress);
776 printk("high density\n");
777 }
778 }
779 WriteDOC(0, docptr, Mplus_WritePipeTerm);
780 WriteDOC(0, docptr, Mplus_WritePipeTerm);
781
782 if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
783 command == NAND_CMD_READOOB || command == NAND_CMD_READID)
784 WriteDOC(0, docptr, Mplus_FlashControl);
785 }
786
787
788
789
790
791 switch (command) {
792
793 case NAND_CMD_PAGEPROG:
794 case NAND_CMD_ERASE1:
795 case NAND_CMD_ERASE2:
796 case NAND_CMD_SEQIN:
797 case NAND_CMD_STATUS:
798 return;
799
800 case NAND_CMD_RESET:
801 if (this->dev_ready)
802 break;
803 udelay(this->chip_delay);
804 WriteDOC(NAND_CMD_STATUS, docptr, Mplus_FlashCmd);
805 WriteDOC(0, docptr, Mplus_WritePipeTerm);
806 WriteDOC(0, docptr, Mplus_WritePipeTerm);
807 while (!(this->read_byte(mtd) & 0x40)) ;
808 return;
809
810
811 default:
812
813
814
815
816 if (!this->dev_ready) {
817 udelay(this->chip_delay);
818 return;
819 }
820 }
821
822
823
824 ndelay(100);
825
826 while (!this->dev_ready(mtd)) ;
827}
828
829static int doc200x_dev_ready(struct mtd_info *mtd)
830{
831 struct nand_chip *this = mtd->priv;
832 struct doc_priv *doc = this->priv;
833 void __iomem *docptr = doc->virtadr;
834
835 if (DoC_is_MillenniumPlus(doc)) {
836
837 DoC_Delay(doc, 4);
838 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
839 if (debug)
840 printk("not ready\n");
841 return 0;
842 }
843 if (debug)
844 printk("was ready\n");
845 return 1;
846 } else {
847
848 DoC_Delay(doc, 4);
849 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
850 if (debug)
851 printk("not ready\n");
852 return 0;
853 }
854
855 DoC_Delay(doc, 2);
856 if (debug)
857 printk("was ready\n");
858 return 1;
859 }
860}
861
862static int doc200x_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
863{
864
865
866 return 0;
867}
868
869static void doc200x_enable_hwecc(struct mtd_info *mtd, int mode)
870{
871 struct nand_chip *this = mtd->priv;
872 struct doc_priv *doc = this->priv;
873 void __iomem *docptr = doc->virtadr;
874
875
876 switch (mode) {
877 case NAND_ECC_READ:
878 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
879 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
880 break;
881 case NAND_ECC_WRITE:
882 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
883 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
884 break;
885 }
886}
887
888static void doc2001plus_enable_hwecc(struct mtd_info *mtd, int mode)
889{
890 struct nand_chip *this = mtd->priv;
891 struct doc_priv *doc = this->priv;
892 void __iomem *docptr = doc->virtadr;
893
894
895 switch (mode) {
896 case NAND_ECC_READ:
897 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
898 WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
899 break;
900 case NAND_ECC_WRITE:
901 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
902 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
903 break;
904 }
905}
906
907
908static int doc200x_calculate_ecc(struct mtd_info *mtd, const u_char *dat, unsigned char *ecc_code)
909{
910 struct nand_chip *this = mtd->priv;
911 struct doc_priv *doc = this->priv;
912 void __iomem *docptr = doc->virtadr;
913 int i;
914 int emptymatch = 1;
915
916
917 if (DoC_is_2000(doc)) {
918 WriteDOC(doc->CDSNControl & ~CDSN_CTRL_FLASH_IO, docptr, CDSNControl);
919 WriteDOC(0, docptr, 2k_CDSN_IO);
920 WriteDOC(0, docptr, 2k_CDSN_IO);
921 WriteDOC(0, docptr, 2k_CDSN_IO);
922 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
923 } else if (DoC_is_MillenniumPlus(doc)) {
924 WriteDOC(0, docptr, Mplus_NOP);
925 WriteDOC(0, docptr, Mplus_NOP);
926 WriteDOC(0, docptr, Mplus_NOP);
927 } else {
928 WriteDOC(0, docptr, NOP);
929 WriteDOC(0, docptr, NOP);
930 WriteDOC(0, docptr, NOP);
931 }
932
933 for (i = 0; i < 6; i++) {
934 if (DoC_is_MillenniumPlus(doc))
935 ecc_code[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
936 else
937 ecc_code[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
938 if (ecc_code[i] != empty_write_ecc[i])
939 emptymatch = 0;
940 }
941 if (DoC_is_MillenniumPlus(doc))
942 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
943 else
944 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
945#if 0
946
947 if (emptymatch) {
948
949
950
951 for (i = 0; i < 512; i++) {
952 if (dat[i] == 0xff)
953 continue;
954 emptymatch = 0;
955 break;
956 }
957 }
958
959
960
961 if (emptymatch)
962 memset(ecc_code, 0xff, 6);
963#endif
964 return 0;
965}
966
967static int doc200x_correct_data(struct mtd_info *mtd, u_char *dat,
968 u_char *read_ecc, u_char *isnull)
969{
970 int i, ret = 0;
971 struct nand_chip *this = mtd->priv;
972 struct doc_priv *doc = this->priv;
973 void __iomem *docptr = doc->virtadr;
974 uint8_t calc_ecc[6];
975 volatile u_char dummy;
976 int emptymatch = 1;
977
978
979 if (DoC_is_2000(doc)) {
980 dummy = ReadDOC(docptr, 2k_ECCStatus);
981 dummy = ReadDOC(docptr, 2k_ECCStatus);
982 dummy = ReadDOC(docptr, 2k_ECCStatus);
983 } else if (DoC_is_MillenniumPlus(doc)) {
984 dummy = ReadDOC(docptr, Mplus_ECCConf);
985 dummy = ReadDOC(docptr, Mplus_ECCConf);
986 dummy = ReadDOC(docptr, Mplus_ECCConf);
987 } else {
988 dummy = ReadDOC(docptr, ECCConf);
989 dummy = ReadDOC(docptr, ECCConf);
990 dummy = ReadDOC(docptr, ECCConf);
991 }
992
993
994 if (dummy & 0x80) {
995 for (i = 0; i < 6; i++) {
996 if (DoC_is_MillenniumPlus(doc))
997 calc_ecc[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
998 else
999 calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
1000 if (calc_ecc[i] != empty_read_syndrome[i])
1001 emptymatch = 0;
1002 }
1003
1004
1005 if (emptymatch) {
1006 for (i = 0; i < 6; i++) {
1007 if (read_ecc[i] == 0xff)
1008 continue;
1009 emptymatch = 0;
1010 break;
1011 }
1012 }
1013
1014 if (emptymatch) {
1015
1016
1017
1018 for (i = 0; i < 512; i++) {
1019 if (dat[i] == 0xff)
1020 continue;
1021 emptymatch = 0;
1022 break;
1023 }
1024 }
1025
1026
1027
1028
1029 if (!emptymatch)
1030 ret = doc_ecc_decode(rs_decoder, dat, calc_ecc);
1031 if (ret > 0)
1032 printk(KERN_ERR "doc200x_correct_data corrected %d errors\n", ret);
1033 }
1034 if (DoC_is_MillenniumPlus(doc))
1035 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
1036 else
1037 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
1038 if (no_ecc_failures && (ret == -EBADMSG)) {
1039 printk(KERN_ERR "suppressing ECC failure\n");
1040 ret = 0;
1041 }
1042 return ret;
1043}
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057static struct nand_ecclayout doc200x_oobinfo = {
1058 .eccbytes = 6,
1059 .eccpos = {0, 1, 2, 3, 4, 5},
1060 .oobfree = {{8, 8}, {6, 2}}
1061};
1062
1063
1064
1065
1066
1067
1068
1069static int __init find_media_headers(struct mtd_info *mtd, u_char *buf, const char *id, int findmirror)
1070{
1071 struct nand_chip *this = mtd->priv;
1072 struct doc_priv *doc = this->priv;
1073 unsigned offs;
1074 int ret;
1075 size_t retlen;
1076
1077 for (offs = 0; offs < mtd->size; offs += mtd->erasesize) {
1078 ret = mtd->read(mtd, offs, mtd->writesize, &retlen, buf);
1079 if (retlen != mtd->writesize)
1080 continue;
1081 if (ret) {
1082 printk(KERN_WARNING "ECC error scanning DOC at 0x%x\n", offs);
1083 }
1084 if (memcmp(buf, id, 6))
1085 continue;
1086 printk(KERN_INFO "Found DiskOnChip %s Media Header at 0x%x\n", id, offs);
1087 if (doc->mh0_page == -1) {
1088 doc->mh0_page = offs >> this->page_shift;
1089 if (!findmirror)
1090 return 1;
1091 continue;
1092 }
1093 doc->mh1_page = offs >> this->page_shift;
1094 return 2;
1095 }
1096 if (doc->mh0_page == -1) {
1097 printk(KERN_WARNING "DiskOnChip %s Media Header not found.\n", id);
1098 return 0;
1099 }
1100
1101
1102 offs = doc->mh0_page << this->page_shift;
1103 ret = mtd->read(mtd, offs, mtd->writesize, &retlen, buf);
1104 if (retlen != mtd->writesize) {
1105
1106 printk(KERN_ERR "Read DiskOnChip Media Header once, but can't reread it???\n");
1107 return 0;
1108 }
1109 return 1;
1110}
1111
1112static inline int __init nftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
1113{
1114 struct nand_chip *this = mtd->priv;
1115 struct doc_priv *doc = this->priv;
1116 int ret = 0;
1117 u_char *buf;
1118 struct NFTLMediaHeader *mh;
1119 const unsigned psize = 1 << this->page_shift;
1120 int numparts = 0;
1121 unsigned blocks, maxblocks;
1122 int offs, numheaders;
1123
1124 buf = kmalloc(mtd->writesize, GFP_KERNEL);
1125 if (!buf) {
1126 printk(KERN_ERR "DiskOnChip mediaheader kmalloc failed!\n");
1127 return 0;
1128 }
1129 if (!(numheaders = find_media_headers(mtd, buf, "ANAND", 1)))
1130 goto out;
1131 mh = (struct NFTLMediaHeader *)buf;
1132
1133 le16_to_cpus(&mh->NumEraseUnits);
1134 le16_to_cpus(&mh->FirstPhysicalEUN);
1135 le32_to_cpus(&mh->FormattedSize);
1136
1137 printk(KERN_INFO " DataOrgID = %s\n"
1138 " NumEraseUnits = %d\n"
1139 " FirstPhysicalEUN = %d\n"
1140 " FormattedSize = %d\n"
1141 " UnitSizeFactor = %d\n",
1142 mh->DataOrgID, mh->NumEraseUnits,
1143 mh->FirstPhysicalEUN, mh->FormattedSize,
1144 mh->UnitSizeFactor);
1145
1146 blocks = mtd->size >> this->phys_erase_shift;
1147 maxblocks = min(32768U, mtd->erasesize - psize);
1148
1149 if (mh->UnitSizeFactor == 0x00) {
1150
1151
1152
1153
1154
1155 mh->UnitSizeFactor = 0xff;
1156 while (blocks > maxblocks) {
1157 blocks >>= 1;
1158 maxblocks = min(32768U, (maxblocks << 1) + psize);
1159 mh->UnitSizeFactor--;
1160 }
1161 printk(KERN_WARNING "UnitSizeFactor=0x00 detected. Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor);
1162 }
1163
1164
1165
1166
1167
1168
1169 if (mh->UnitSizeFactor != 0xff) {
1170 this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
1171 mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
1172 printk(KERN_INFO "Setting virtual erase size to %d\n", mtd->erasesize);
1173 blocks = mtd->size >> this->bbt_erase_shift;
1174 maxblocks = min(32768U, mtd->erasesize - psize);
1175 }
1176
1177 if (blocks > maxblocks) {
1178 printk(KERN_ERR "UnitSizeFactor of 0x%02x is inconsistent with device size. Aborting.\n", mh->UnitSizeFactor);
1179 goto out;
1180 }
1181
1182
1183 offs = max(doc->mh0_page, doc->mh1_page);
1184 offs <<= this->page_shift;
1185 offs += mtd->erasesize;
1186
1187 if (show_firmware_partition == 1) {
1188 parts[0].name = " DiskOnChip Firmware / Media Header partition";
1189 parts[0].offset = 0;
1190 parts[0].size = offs;
1191 numparts = 1;
1192 }
1193
1194 parts[numparts].name = " DiskOnChip BDTL partition";
1195 parts[numparts].offset = offs;
1196 parts[numparts].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift;
1197
1198 offs += parts[numparts].size;
1199 numparts++;
1200
1201 if (offs < mtd->size) {
1202 parts[numparts].name = " DiskOnChip Remainder partition";
1203 parts[numparts].offset = offs;
1204 parts[numparts].size = mtd->size - offs;
1205 numparts++;
1206 }
1207
1208 ret = numparts;
1209 out:
1210 kfree(buf);
1211 return ret;
1212}
1213
1214
1215static inline int __init inftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
1216{
1217 struct nand_chip *this = mtd->priv;
1218 struct doc_priv *doc = this->priv;
1219 int ret = 0;
1220 u_char *buf;
1221 struct INFTLMediaHeader *mh;
1222 struct INFTLPartition *ip;
1223 int numparts = 0;
1224 int blocks;
1225 int vshift, lastvunit = 0;
1226 int i;
1227 int end = mtd->size;
1228
1229 if (inftl_bbt_write)
1230 end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift);
1231
1232 buf = kmalloc(mtd->writesize, GFP_KERNEL);
1233 if (!buf) {
1234 printk(KERN_ERR "DiskOnChip mediaheader kmalloc failed!\n");
1235 return 0;
1236 }
1237
1238 if (!find_media_headers(mtd, buf, "BNAND", 0))
1239 goto out;
1240 doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift);
1241 mh = (struct INFTLMediaHeader *)buf;
1242
1243 le32_to_cpus(&mh->NoOfBootImageBlocks);
1244 le32_to_cpus(&mh->NoOfBinaryPartitions);
1245 le32_to_cpus(&mh->NoOfBDTLPartitions);
1246 le32_to_cpus(&mh->BlockMultiplierBits);
1247 le32_to_cpus(&mh->FormatFlags);
1248 le32_to_cpus(&mh->PercentUsed);
1249
1250 printk(KERN_INFO " bootRecordID = %s\n"
1251 " NoOfBootImageBlocks = %d\n"
1252 " NoOfBinaryPartitions = %d\n"
1253 " NoOfBDTLPartitions = %d\n"
1254 " BlockMultiplerBits = %d\n"
1255 " FormatFlgs = %d\n"
1256 " OsakVersion = %d.%d.%d.%d\n"
1257 " PercentUsed = %d\n",
1258 mh->bootRecordID, mh->NoOfBootImageBlocks,
1259 mh->NoOfBinaryPartitions,
1260 mh->NoOfBDTLPartitions,
1261 mh->BlockMultiplierBits, mh->FormatFlags,
1262 ((unsigned char *) &mh->OsakVersion)[0] & 0xf,
1263 ((unsigned char *) &mh->OsakVersion)[1] & 0xf,
1264 ((unsigned char *) &mh->OsakVersion)[2] & 0xf,
1265 ((unsigned char *) &mh->OsakVersion)[3] & 0xf,
1266 mh->PercentUsed);
1267
1268 vshift = this->phys_erase_shift + mh->BlockMultiplierBits;
1269
1270 blocks = mtd->size >> vshift;
1271 if (blocks > 32768) {
1272 printk(KERN_ERR "BlockMultiplierBits=%d is inconsistent with device size. Aborting.\n", mh->BlockMultiplierBits);
1273 goto out;
1274 }
1275
1276 blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift);
1277 if (inftl_bbt_write && (blocks > mtd->erasesize)) {
1278 printk(KERN_ERR "Writeable BBTs spanning more than one erase block are not yet supported. FIX ME!\n");
1279 goto out;
1280 }
1281
1282
1283 for (i = 0; (i < 4); i++) {
1284 ip = &(mh->Partitions[i]);
1285 le32_to_cpus(&ip->virtualUnits);
1286 le32_to_cpus(&ip->firstUnit);
1287 le32_to_cpus(&ip->lastUnit);
1288 le32_to_cpus(&ip->flags);
1289 le32_to_cpus(&ip->spareUnits);
1290 le32_to_cpus(&ip->Reserved0);
1291
1292 printk(KERN_INFO " PARTITION[%d] ->\n"
1293 " virtualUnits = %d\n"
1294 " firstUnit = %d\n"
1295 " lastUnit = %d\n"
1296 " flags = 0x%x\n"
1297 " spareUnits = %d\n",
1298 i, ip->virtualUnits, ip->firstUnit,
1299 ip->lastUnit, ip->flags,
1300 ip->spareUnits);
1301
1302 if ((show_firmware_partition == 1) &&
1303 (i == 0) && (ip->firstUnit > 0)) {
1304 parts[0].name = " DiskOnChip IPL / Media Header partition";
1305 parts[0].offset = 0;
1306 parts[0].size = mtd->erasesize * ip->firstUnit;
1307 numparts = 1;
1308 }
1309
1310 if (ip->flags & INFTL_BINARY)
1311 parts[numparts].name = " DiskOnChip BDK partition";
1312 else
1313 parts[numparts].name = " DiskOnChip BDTL partition";
1314 parts[numparts].offset = ip->firstUnit << vshift;
1315 parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift;
1316 numparts++;
1317 if (ip->lastUnit > lastvunit)
1318 lastvunit = ip->lastUnit;
1319 if (ip->flags & INFTL_LAST)
1320 break;
1321 }
1322 lastvunit++;
1323 if ((lastvunit << vshift) < end) {
1324 parts[numparts].name = " DiskOnChip Remainder partition";
1325 parts[numparts].offset = lastvunit << vshift;
1326 parts[numparts].size = end - parts[numparts].offset;
1327 numparts++;
1328 }
1329 ret = numparts;
1330 out:
1331 kfree(buf);
1332 return ret;
1333}
1334
1335static int __init nftl_scan_bbt(struct mtd_info *mtd)
1336{
1337 int ret, numparts;
1338 struct nand_chip *this = mtd->priv;
1339 struct doc_priv *doc = this->priv;
1340 struct mtd_partition parts[2];
1341
1342 memset((char *)parts, 0, sizeof(parts));
1343
1344
1345 numparts = nftl_partscan(mtd, parts);
1346 if (!numparts)
1347 return -EIO;
1348 this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1349 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1350 NAND_BBT_VERSION;
1351 this->bbt_td->veroffs = 7;
1352 this->bbt_td->pages[0] = doc->mh0_page + 1;
1353 if (doc->mh1_page != -1) {
1354 this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1355 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1356 NAND_BBT_VERSION;
1357 this->bbt_md->veroffs = 7;
1358 this->bbt_md->pages[0] = doc->mh1_page + 1;
1359 } else {
1360 this->bbt_md = NULL;
1361 }
1362
1363
1364
1365 if ((ret = nand_scan_bbt(mtd, NULL)))
1366 return ret;
1367 add_mtd_device(mtd);
1368#ifdef CONFIG_MTD_PARTITIONS
1369 if (!no_autopart)
1370 add_mtd_partitions(mtd, parts, numparts);
1371#endif
1372 return 0;
1373}
1374
1375static int __init inftl_scan_bbt(struct mtd_info *mtd)
1376{
1377 int ret, numparts;
1378 struct nand_chip *this = mtd->priv;
1379 struct doc_priv *doc = this->priv;
1380 struct mtd_partition parts[5];
1381
1382 if (this->numchips > doc->chips_per_floor) {
1383 printk(KERN_ERR "Multi-floor INFTL devices not yet supported.\n");
1384 return -EIO;
1385 }
1386
1387 if (DoC_is_MillenniumPlus(doc)) {
1388 this->bbt_td->options = NAND_BBT_2BIT | NAND_BBT_ABSPAGE;
1389 if (inftl_bbt_write)
1390 this->bbt_td->options |= NAND_BBT_WRITE;
1391 this->bbt_td->pages[0] = 2;
1392 this->bbt_md = NULL;
1393 } else {
1394 this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
1395 if (inftl_bbt_write)
1396 this->bbt_td->options |= NAND_BBT_WRITE;
1397 this->bbt_td->offs = 8;
1398 this->bbt_td->len = 8;
1399 this->bbt_td->veroffs = 7;
1400 this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1401 this->bbt_td->reserved_block_code = 0x01;
1402 this->bbt_td->pattern = "MSYS_BBT";
1403
1404 this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
1405 if (inftl_bbt_write)
1406 this->bbt_md->options |= NAND_BBT_WRITE;
1407 this->bbt_md->offs = 8;
1408 this->bbt_md->len = 8;
1409 this->bbt_md->veroffs = 7;
1410 this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1411 this->bbt_md->reserved_block_code = 0x01;
1412 this->bbt_md->pattern = "TBB_SYSM";
1413 }
1414
1415
1416
1417 if ((ret = nand_scan_bbt(mtd, NULL)))
1418 return ret;
1419 memset((char *)parts, 0, sizeof(parts));
1420 numparts = inftl_partscan(mtd, parts);
1421
1422
1423
1424 if (!numparts)
1425 return -EIO;
1426 add_mtd_device(mtd);
1427#ifdef CONFIG_MTD_PARTITIONS
1428 if (!no_autopart)
1429 add_mtd_partitions(mtd, parts, numparts);
1430#endif
1431 return 0;
1432}
1433
1434static inline int __init doc2000_init(struct mtd_info *mtd)
1435{
1436 struct nand_chip *this = mtd->priv;
1437 struct doc_priv *doc = this->priv;
1438
1439 this->read_byte = doc2000_read_byte;
1440 this->write_buf = doc2000_writebuf;
1441 this->read_buf = doc2000_readbuf;
1442 this->verify_buf = doc2000_verifybuf;
1443 this->scan_bbt = nftl_scan_bbt;
1444
1445 doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
1446 doc2000_count_chips(mtd);
1447 mtd->name = "DiskOnChip 2000 (NFTL Model)";
1448 return (4 * doc->chips_per_floor);
1449}
1450
1451static inline int __init doc2001_init(struct mtd_info *mtd)
1452{
1453 struct nand_chip *this = mtd->priv;
1454 struct doc_priv *doc = this->priv;
1455
1456 this->read_byte = doc2001_read_byte;
1457 this->write_buf = doc2001_writebuf;
1458 this->read_buf = doc2001_readbuf;
1459 this->verify_buf = doc2001_verifybuf;
1460
1461 ReadDOC(doc->virtadr, ChipID);
1462 ReadDOC(doc->virtadr, ChipID);
1463 ReadDOC(doc->virtadr, ChipID);
1464 if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) {
1465
1466
1467
1468
1469 doc2000_count_chips(mtd);
1470 mtd->name = "DiskOnChip 2000 (INFTL Model)";
1471 this->scan_bbt = inftl_scan_bbt;
1472 return (4 * doc->chips_per_floor);
1473 } else {
1474
1475 doc->chips_per_floor = 1;
1476 mtd->name = "DiskOnChip Millennium";
1477 this->scan_bbt = nftl_scan_bbt;
1478 return 1;
1479 }
1480}
1481
1482static inline int __init doc2001plus_init(struct mtd_info *mtd)
1483{
1484 struct nand_chip *this = mtd->priv;
1485 struct doc_priv *doc = this->priv;
1486
1487 this->read_byte = doc2001plus_read_byte;
1488 this->write_buf = doc2001plus_writebuf;
1489 this->read_buf = doc2001plus_readbuf;
1490 this->verify_buf = doc2001plus_verifybuf;
1491 this->scan_bbt = inftl_scan_bbt;
1492 this->cmd_ctrl = NULL;
1493 this->select_chip = doc2001plus_select_chip;
1494 this->cmdfunc = doc2001plus_command;
1495 this->ecc.hwctl = doc2001plus_enable_hwecc;
1496
1497 doc->chips_per_floor = 1;
1498 mtd->name = "DiskOnChip Millennium Plus";
1499
1500 return 1;
1501}
1502
1503static int __init doc_probe(unsigned long physadr)
1504{
1505 unsigned char ChipID;
1506 struct mtd_info *mtd;
1507 struct nand_chip *nand;
1508 struct doc_priv *doc;
1509 void __iomem *virtadr;
1510 unsigned char save_control;
1511 unsigned char tmp, tmpb, tmpc;
1512 int reg, len, numchips;
1513 int ret = 0;
1514
1515 virtadr = ioremap(physadr, DOC_IOREMAP_LEN);
1516 if (!virtadr) {
1517 printk(KERN_ERR "Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n", DOC_IOREMAP_LEN, physadr);
1518 return -EIO;
1519 }
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529 save_control = ReadDOC(virtadr, DOCControl);
1530
1531
1532 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
1533 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
1534
1535
1536 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
1537 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
1538
1539 ChipID = ReadDOC(virtadr, ChipID);
1540
1541 switch (ChipID) {
1542 case DOC_ChipID_Doc2k:
1543 reg = DoC_2k_ECCStatus;
1544 break;
1545 case DOC_ChipID_DocMil:
1546 reg = DoC_ECCConf;
1547 break;
1548 case DOC_ChipID_DocMilPlus16:
1549 case DOC_ChipID_DocMilPlus32:
1550 case 0:
1551
1552
1553 for (tmp = 0; (tmp < 4); tmp++)
1554 ReadDOC(virtadr, Mplus_Power);
1555
1556
1557 tmp = DOC_MODE_RESET | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
1558 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1559 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1560
1561 mdelay(1);
1562
1563 tmp = DOC_MODE_NORMAL | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
1564 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1565 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1566 mdelay(1);
1567
1568 ChipID = ReadDOC(virtadr, ChipID);
1569
1570 switch (ChipID) {
1571 case DOC_ChipID_DocMilPlus16:
1572 reg = DoC_Mplus_Toggle;
1573 break;
1574 case DOC_ChipID_DocMilPlus32:
1575 printk(KERN_ERR "DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
1576 default:
1577 ret = -ENODEV;
1578 goto notfound;
1579 }
1580 break;
1581
1582 default:
1583 ret = -ENODEV;
1584 goto notfound;
1585 }
1586
1587 tmp = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1588 tmpb = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1589 tmpc = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1590 if ((tmp == tmpb) || (tmp != tmpc)) {
1591 printk(KERN_WARNING "Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr);
1592 ret = -ENODEV;
1593 goto notfound;
1594 }
1595
1596 for (mtd = doclist; mtd; mtd = doc->nextdoc) {
1597 unsigned char oldval;
1598 unsigned char newval;
1599 nand = mtd->priv;
1600 doc = nand->priv;
1601
1602
1603
1604
1605 if (ChipID == DOC_ChipID_DocMilPlus16) {
1606 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1607 newval = ReadDOC(virtadr, Mplus_AliasResolution);
1608 } else {
1609 oldval = ReadDOC(doc->virtadr, AliasResolution);
1610 newval = ReadDOC(virtadr, AliasResolution);
1611 }
1612 if (oldval != newval)
1613 continue;
1614 if (ChipID == DOC_ChipID_DocMilPlus16) {
1615 WriteDOC(~newval, virtadr, Mplus_AliasResolution);
1616 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1617 WriteDOC(newval, virtadr, Mplus_AliasResolution);
1618 } else {
1619 WriteDOC(~newval, virtadr, AliasResolution);
1620 oldval = ReadDOC(doc->virtadr, AliasResolution);
1621 WriteDOC(newval, virtadr, AliasResolution);
1622 }
1623 newval = ~newval;
1624 if (oldval == newval) {
1625 printk(KERN_DEBUG "Found alias of DOC at 0x%lx to 0x%lx\n", doc->physadr, physadr);
1626 goto notfound;
1627 }
1628 }
1629
1630 printk(KERN_NOTICE "DiskOnChip found at 0x%lx\n", physadr);
1631
1632 len = sizeof(struct mtd_info) +
1633 sizeof(struct nand_chip) + sizeof(struct doc_priv) + (2 * sizeof(struct nand_bbt_descr));
1634 mtd = kzalloc(len, GFP_KERNEL);
1635 if (!mtd) {
1636 printk(KERN_ERR "DiskOnChip kmalloc (%d bytes) failed!\n", len);
1637 ret = -ENOMEM;
1638 goto fail;
1639 }
1640
1641 nand = (struct nand_chip *) (mtd + 1);
1642 doc = (struct doc_priv *) (nand + 1);
1643 nand->bbt_td = (struct nand_bbt_descr *) (doc + 1);
1644 nand->bbt_md = nand->bbt_td + 1;
1645
1646 mtd->priv = nand;
1647 mtd->owner = THIS_MODULE;
1648
1649 nand->priv = doc;
1650 nand->select_chip = doc200x_select_chip;
1651 nand->cmd_ctrl = doc200x_hwcontrol;
1652 nand->dev_ready = doc200x_dev_ready;
1653 nand->waitfunc = doc200x_wait;
1654 nand->block_bad = doc200x_block_bad;
1655 nand->ecc.hwctl = doc200x_enable_hwecc;
1656 nand->ecc.calculate = doc200x_calculate_ecc;
1657 nand->ecc.correct = doc200x_correct_data;
1658
1659 nand->ecc.layout = &doc200x_oobinfo;
1660 nand->ecc.mode = NAND_ECC_HW_SYNDROME;
1661 nand->ecc.size = 512;
1662 nand->ecc.bytes = 6;
1663 nand->options = NAND_USE_FLASH_BBT;
1664
1665 doc->physadr = physadr;
1666 doc->virtadr = virtadr;
1667 doc->ChipID = ChipID;
1668 doc->curfloor = -1;
1669 doc->curchip = -1;
1670 doc->mh0_page = -1;
1671 doc->mh1_page = -1;
1672 doc->nextdoc = doclist;
1673
1674 if (ChipID == DOC_ChipID_Doc2k)
1675 numchips = doc2000_init(mtd);
1676 else if (ChipID == DOC_ChipID_DocMilPlus16)
1677 numchips = doc2001plus_init(mtd);
1678 else
1679 numchips = doc2001_init(mtd);
1680
1681 if ((ret = nand_scan(mtd, numchips))) {
1682
1683
1684
1685
1686
1687
1688 nand_release(mtd);
1689 kfree(mtd);
1690 goto fail;
1691 }
1692
1693
1694 doclist = mtd;
1695 return 0;
1696
1697 notfound:
1698
1699
1700 WriteDOC(save_control, virtadr, DOCControl);
1701 fail:
1702 iounmap(virtadr);
1703 return ret;
1704}
1705
1706static void release_nanddoc(void)
1707{
1708 struct mtd_info *mtd, *nextmtd;
1709 struct nand_chip *nand;
1710 struct doc_priv *doc;
1711
1712 for (mtd = doclist; mtd; mtd = nextmtd) {
1713 nand = mtd->priv;
1714 doc = nand->priv;
1715
1716 nextmtd = doc->nextdoc;
1717 nand_release(mtd);
1718 iounmap(doc->virtadr);
1719 kfree(mtd);
1720 }
1721}
1722
1723static int __init init_nanddoc(void)
1724{
1725 int i, ret = 0;
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736 rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS);
1737 if (!rs_decoder) {
1738 printk(KERN_ERR "DiskOnChip: Could not create a RS decoder\n");
1739 return -ENOMEM;
1740 }
1741
1742 if (doc_config_location) {
1743 printk(KERN_INFO "Using configured DiskOnChip probe address 0x%lx\n", doc_config_location);
1744 ret = doc_probe(doc_config_location);
1745 if (ret < 0)
1746 goto outerr;
1747 } else {
1748 for (i = 0; (doc_locations[i] != 0xffffffff); i++) {
1749 doc_probe(doc_locations[i]);
1750 }
1751 }
1752
1753
1754 if (!doclist) {
1755 printk(KERN_INFO "No valid DiskOnChip devices found\n");
1756 ret = -ENODEV;
1757 goto outerr;
1758 }
1759 return 0;
1760 outerr:
1761 free_rs(rs_decoder);
1762 return ret;
1763}
1764
1765static void __exit cleanup_nanddoc(void)
1766{
1767
1768 release_nanddoc();
1769
1770
1771 if (rs_decoder) {
1772 free_rs(rs_decoder);
1773 }
1774}
1775
1776module_init(init_nanddoc);
1777module_exit(cleanup_nanddoc);
1778
1779MODULE_LICENSE("GPL");
1780MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1781MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver\n");
1782#endif
1783