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