2 * drivers/mtd/nand/diskonchip.c
4 * (C) 2003 Red Hat, Inc.
5 * (C) 2004 Dan Brown <dan_brown@ieee.org>
6 * (C) 2004 Kalev Lember <kalev@smartlink.ee>
8 * Author: David Woodhouse <dwmw2@infradead.org>
9 * Additional Diskonchip 2000 and Millennium support by Dan Brown <dan_brown@ieee.org>
10 * Diskonchip Millennium Plus support by Kalev Lember <kalev@smartlink.ee>
12 * Error correction code lifted from the old docecc code
13 * Author: Fabrice Bellard (fabrice.bellard@netgem.com)
14 * Copyright (C) 2000 Netgem S.A.
15 * converted to the generic Reed-Solomon library by Thomas Gleixner <tglx@linutronix.de>
17 * Interface to generic NAND code for M-Systems DiskOnChip devices
19 * $Id: diskonchip.c,v 1.55 2005/11/07 11:14:30 gleixner Exp $
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/sched.h>
25 #include <linux/delay.h>
26 #include <linux/rslib.h>
27 #include <linux/moduleparam.h>
30 #include <linux/mtd/mtd.h>
31 #include <linux/mtd/nand.h>
32 #include <linux/mtd/doc2000.h>
33 #include <linux/mtd/compatmac.h>
34 #include <linux/mtd/partitions.h>
35 #include <linux/mtd/inftl.h>
37 /* Where to look for the devices? */
38 #ifndef CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS
39 #define CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS 0
42 static unsigned long __initdata doc_locations
[] = {
43 #if defined (__alpha__) || defined(__i386__) || defined(__x86_64__)
44 #ifdef CONFIG_MTD_NAND_DISKONCHIP_PROBE_HIGH
45 0xfffc8000, 0xfffca000, 0xfffcc000, 0xfffce000,
46 0xfffd0000, 0xfffd2000, 0xfffd4000, 0xfffd6000,
47 0xfffd8000, 0xfffda000, 0xfffdc000, 0xfffde000,
48 0xfffe0000, 0xfffe2000, 0xfffe4000, 0xfffe6000,
49 0xfffe8000, 0xfffea000, 0xfffec000, 0xfffee000,
50 #else /* CONFIG_MTD_DOCPROBE_HIGH */
51 0xc8000, 0xca000, 0xcc000, 0xce000,
52 0xd0000, 0xd2000, 0xd4000, 0xd6000,
53 0xd8000, 0xda000, 0xdc000, 0xde000,
54 0xe0000, 0xe2000, 0xe4000, 0xe6000,
55 0xe8000, 0xea000, 0xec000, 0xee000,
56 #endif /* CONFIG_MTD_DOCPROBE_HIGH */
57 #elif defined(__PPC__)
59 #elif defined(CONFIG_MOMENCO_OCELOT)
62 #elif defined(CONFIG_MOMENCO_OCELOT_G) || defined (CONFIG_MOMENCO_OCELOT_C)
65 #warning Unknown architecture for DiskOnChip. No default probe locations defined
69 static struct mtd_info
*doclist
= NULL
;
72 void __iomem
*virtadr
;
73 unsigned long physadr
;
76 int chips_per_floor
; /* The number of chips detected on each floor */
81 struct mtd_info
*nextdoc
;
84 /* This is the syndrome computed by the HW ecc generator upon reading an empty
85 page, one with all 0xff for data and stored ecc code. */
86 static u_char empty_read_syndrome
[6] = { 0x26, 0xff, 0x6d, 0x47, 0x73, 0x7a };
88 /* This is the ecc value computed by the HW ecc generator upon writing an empty
89 page, one with all 0xff for data. */
90 static u_char empty_write_ecc
[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
92 #define INFTL_BBT_RESERVED_BLOCKS 4
94 #define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
95 #define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
96 #define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
98 static void doc200x_hwcontrol(struct mtd_info
*mtd
, int cmd
);
99 static void doc200x_select_chip(struct mtd_info
*mtd
, int chip
);
101 static int debug
= 0;
102 module_param(debug
, int, 0);
104 static int try_dword
= 1;
105 module_param(try_dword
, int, 0);
107 static int no_ecc_failures
= 0;
108 module_param(no_ecc_failures
, int, 0);
110 static int no_autopart
= 0;
111 module_param(no_autopart
, int, 0);
113 static int show_firmware_partition
= 0;
114 module_param(show_firmware_partition
, int, 0);
116 #ifdef MTD_NAND_DISKONCHIP_BBTWRITE
117 static int inftl_bbt_write
= 1;
119 static int inftl_bbt_write
= 0;
121 module_param(inftl_bbt_write
, int, 0);
123 static unsigned long doc_config_location
= CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS
;
124 module_param(doc_config_location
, ulong
, 0);
125 MODULE_PARM_DESC(doc_config_location
, "Physical memory address at which to probe for DiskOnChip");
127 /* Sector size for HW ECC */
128 #define SECTOR_SIZE 512
129 /* The sector bytes are packed into NB_DATA 10 bit words */
130 #define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / 10)
131 /* Number of roots */
133 /* First consective root */
135 /* Number of symbols */
138 /* the Reed Solomon control structure */
139 static struct rs_control
*rs_decoder
;
142 * The HW decoder in the DoC ASIC's provides us a error syndrome,
143 * which we must convert to a standard syndrom usable by the generic
144 * Reed-Solomon library code.
146 * Fabrice Bellard figured this out in the old docecc code. I added
147 * some comments, improved a minor bit and converted it to make use
148 * of the generic Reed-Solomon libary. tglx
150 static int doc_ecc_decode(struct rs_control
*rs
, uint8_t *data
, uint8_t *ecc
)
152 int i
, j
, nerr
, errpos
[8];
154 uint16_t ds
[4], s
[5], tmp
, errval
[8], syn
[4];
156 /* Convert the ecc bytes into words */
157 ds
[0] = ((ecc
[4] & 0xff) >> 0) | ((ecc
[5] & 0x03) << 8);
158 ds
[1] = ((ecc
[5] & 0xfc) >> 2) | ((ecc
[2] & 0x0f) << 6);
159 ds
[2] = ((ecc
[2] & 0xf0) >> 4) | ((ecc
[3] & 0x3f) << 4);
160 ds
[3] = ((ecc
[3] & 0xc0) >> 6) | ((ecc
[0] & 0xff) << 2);
163 /* Initialize the syndrom buffer */
164 for (i
= 0; i
< NROOTS
; i
++)
168 * s[i] = ds[3]x^3 + ds[2]x^2 + ds[1]x^1 + ds[0]
169 * where x = alpha^(FCR + i)
171 for (j
= 1; j
< NROOTS
; j
++) {
174 tmp
= rs
->index_of
[ds
[j
]];
175 for (i
= 0; i
< NROOTS
; i
++)
176 s
[i
] ^= rs
->alpha_to
[rs_modnn(rs
, tmp
+ (FCR
+ i
) * j
)];
179 /* Calc s[i] = s[i] / alpha^(v + i) */
180 for (i
= 0; i
< NROOTS
; i
++) {
182 syn
[i
] = rs_modnn(rs
, rs
->index_of
[s
[i
]] + (NN
- FCR
- i
));
184 /* Call the decoder library */
185 nerr
= decode_rs16(rs
, NULL
, NULL
, 1019, syn
, 0, errpos
, 0, errval
);
187 /* Incorrectable errors ? */
192 * Correct the errors. The bitpositions are a bit of magic,
193 * but they are given by the design of the de/encoder circuit
196 for (i
= 0; i
< nerr
; i
++) {
197 int index
, bitpos
, pos
= 1015 - errpos
[i
];
199 if (pos
>= NB_DATA
&& pos
< 1019)
202 /* extract bit position (MSB first) */
203 pos
= 10 * (NB_DATA
- 1 - pos
) - 6;
204 /* now correct the following 10 bits. At most two bytes
205 can be modified since pos is even */
206 index
= (pos
>> 3) ^ 1;
208 if ((index
>= 0 && index
< SECTOR_SIZE
) || index
== (SECTOR_SIZE
+ 1)) {
209 val
= (uint8_t) (errval
[i
] >> (2 + bitpos
));
211 if (index
< SECTOR_SIZE
)
214 index
= ((pos
>> 3) + 1) ^ 1;
215 bitpos
= (bitpos
+ 10) & 7;
218 if ((index
>= 0 && index
< SECTOR_SIZE
) || index
== (SECTOR_SIZE
+ 1)) {
219 val
= (uint8_t) (errval
[i
] << (8 - bitpos
));
221 if (index
< SECTOR_SIZE
)
226 /* If the parity is wrong, no rescue possible */
227 return parity
? -1 : nerr
;
230 static void DoC_Delay(struct doc_priv
*doc
, unsigned short cycles
)
235 for (i
= 0; i
< cycles
; i
++) {
236 if (DoC_is_Millennium(doc
))
237 dummy
= ReadDOC(doc
->virtadr
, NOP
);
238 else if (DoC_is_MillenniumPlus(doc
))
239 dummy
= ReadDOC(doc
->virtadr
, Mplus_NOP
);
241 dummy
= ReadDOC(doc
->virtadr
, DOCStatus
);
246 #define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
248 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
249 static int _DoC_WaitReady(struct doc_priv
*doc
)
251 void __iomem
*docptr
= doc
->virtadr
;
252 unsigned long timeo
= jiffies
+ (HZ
* 10);
255 printk("_DoC_WaitReady...\n");
256 /* Out-of-line routine to wait for chip response */
257 if (DoC_is_MillenniumPlus(doc
)) {
258 while ((ReadDOC(docptr
, Mplus_FlashControl
) & CDSN_CTRL_FR_B_MASK
) != CDSN_CTRL_FR_B_MASK
) {
259 if (time_after(jiffies
, timeo
)) {
260 printk("_DoC_WaitReady timed out.\n");
267 while (!(ReadDOC(docptr
, CDSNControl
) & CDSN_CTRL_FR_B
)) {
268 if (time_after(jiffies
, timeo
)) {
269 printk("_DoC_WaitReady timed out.\n");
280 static inline int DoC_WaitReady(struct doc_priv
*doc
)
282 void __iomem
*docptr
= doc
->virtadr
;
285 if (DoC_is_MillenniumPlus(doc
)) {
288 if ((ReadDOC(docptr
, Mplus_FlashControl
) & CDSN_CTRL_FR_B_MASK
) != CDSN_CTRL_FR_B_MASK
)
289 /* Call the out-of-line routine to wait */
290 ret
= _DoC_WaitReady(doc
);
294 if (!(ReadDOC(docptr
, CDSNControl
) & CDSN_CTRL_FR_B
))
295 /* Call the out-of-line routine to wait */
296 ret
= _DoC_WaitReady(doc
);
301 printk("DoC_WaitReady OK\n");
305 static void doc2000_write_byte(struct mtd_info
*mtd
, u_char datum
)
307 struct nand_chip
*this = mtd
->priv
;
308 struct doc_priv
*doc
= this->priv
;
309 void __iomem
*docptr
= doc
->virtadr
;
312 printk("write_byte %02x\n", datum
);
313 WriteDOC(datum
, docptr
, CDSNSlowIO
);
314 WriteDOC(datum
, docptr
, 2k_CDSN_IO
);
317 static u_char
doc2000_read_byte(struct mtd_info
*mtd
)
319 struct nand_chip
*this = mtd
->priv
;
320 struct doc_priv
*doc
= this->priv
;
321 void __iomem
*docptr
= doc
->virtadr
;
324 ReadDOC(docptr
, CDSNSlowIO
);
326 ret
= ReadDOC(docptr
, 2k_CDSN_IO
);
328 printk("read_byte returns %02x\n", ret
);
332 static void doc2000_writebuf(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
334 struct nand_chip
*this = mtd
->priv
;
335 struct doc_priv
*doc
= this->priv
;
336 void __iomem
*docptr
= doc
->virtadr
;
339 printk("writebuf of %d bytes: ", len
);
340 for (i
= 0; i
< len
; i
++) {
341 WriteDOC_(buf
[i
], docptr
, DoC_2k_CDSN_IO
+ i
);
343 printk("%02x ", buf
[i
]);
349 static void doc2000_readbuf(struct mtd_info
*mtd
, u_char
*buf
, int len
)
351 struct nand_chip
*this = mtd
->priv
;
352 struct doc_priv
*doc
= this->priv
;
353 void __iomem
*docptr
= doc
->virtadr
;
357 printk("readbuf of %d bytes: ", len
);
359 for (i
= 0; i
< len
; i
++) {
360 buf
[i
] = ReadDOC(docptr
, 2k_CDSN_IO
+ i
);
364 static void doc2000_readbuf_dword(struct mtd_info
*mtd
, u_char
*buf
, int len
)
366 struct nand_chip
*this = mtd
->priv
;
367 struct doc_priv
*doc
= this->priv
;
368 void __iomem
*docptr
= doc
->virtadr
;
372 printk("readbuf_dword of %d bytes: ", len
);
374 if (unlikely((((unsigned long)buf
) | len
) & 3)) {
375 for (i
= 0; i
< len
; i
++) {
376 *(uint8_t *) (&buf
[i
]) = ReadDOC(docptr
, 2k_CDSN_IO
+ i
);
379 for (i
= 0; i
< len
; i
+= 4) {
380 *(uint32_t *) (&buf
[i
]) = readl(docptr
+ DoC_2k_CDSN_IO
+ i
);
385 static int doc2000_verifybuf(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
387 struct nand_chip
*this = mtd
->priv
;
388 struct doc_priv
*doc
= this->priv
;
389 void __iomem
*docptr
= doc
->virtadr
;
392 for (i
= 0; i
< len
; i
++)
393 if (buf
[i
] != ReadDOC(docptr
, 2k_CDSN_IO
))
398 static uint16_t __init
doc200x_ident_chip(struct mtd_info
*mtd
, int nr
)
400 struct nand_chip
*this = mtd
->priv
;
401 struct doc_priv
*doc
= this->priv
;
404 doc200x_select_chip(mtd
, nr
);
405 doc200x_hwcontrol(mtd
, NAND_CTL_SETCLE
);
406 this->write_byte(mtd
, NAND_CMD_READID
);
407 doc200x_hwcontrol(mtd
, NAND_CTL_CLRCLE
);
408 doc200x_hwcontrol(mtd
, NAND_CTL_SETALE
);
409 this->write_byte(mtd
, 0);
410 doc200x_hwcontrol(mtd
, NAND_CTL_CLRALE
);
412 /* We cant' use dev_ready here, but at least we wait for the
413 * command to complete
417 ret
= this->read_byte(mtd
) << 8;
418 ret
|= this->read_byte(mtd
);
420 if (doc
->ChipID
== DOC_ChipID_Doc2k
&& try_dword
&& !nr
) {
421 /* First chip probe. See if we get same results by 32-bit access */
426 void __iomem
*docptr
= doc
->virtadr
;
428 doc200x_hwcontrol(mtd
, NAND_CTL_SETCLE
);
429 doc2000_write_byte(mtd
, NAND_CMD_READID
);
430 doc200x_hwcontrol(mtd
, NAND_CTL_CLRCLE
);
431 doc200x_hwcontrol(mtd
, NAND_CTL_SETALE
);
432 doc2000_write_byte(mtd
, 0);
433 doc200x_hwcontrol(mtd
, NAND_CTL_CLRALE
);
437 ident
.dword
= readl(docptr
+ DoC_2k_CDSN_IO
);
438 if (((ident
.byte
[0] << 8) | ident
.byte
[1]) == ret
) {
439 printk(KERN_INFO
"DiskOnChip 2000 responds to DWORD access\n");
440 this->read_buf
= &doc2000_readbuf_dword
;
447 static void __init
doc2000_count_chips(struct mtd_info
*mtd
)
449 struct nand_chip
*this = mtd
->priv
;
450 struct doc_priv
*doc
= this->priv
;
454 /* Max 4 chips per floor on DiskOnChip 2000 */
455 doc
->chips_per_floor
= 4;
457 /* Find out what the first chip is */
458 mfrid
= doc200x_ident_chip(mtd
, 0);
460 /* Find how many chips in each floor. */
461 for (i
= 1; i
< 4; i
++) {
462 if (doc200x_ident_chip(mtd
, i
) != mfrid
)
465 doc
->chips_per_floor
= i
;
466 printk(KERN_DEBUG
"Detected %d chips per floor.\n", i
);
469 static int doc200x_wait(struct mtd_info
*mtd
, struct nand_chip
*this, int state
)
471 struct doc_priv
*doc
= this->priv
;
476 this->cmdfunc(mtd
, NAND_CMD_STATUS
, -1, -1);
478 status
= (int)this->read_byte(mtd
);
483 static void doc2001_write_byte(struct mtd_info
*mtd
, u_char datum
)
485 struct nand_chip
*this = mtd
->priv
;
486 struct doc_priv
*doc
= this->priv
;
487 void __iomem
*docptr
= doc
->virtadr
;
489 WriteDOC(datum
, docptr
, CDSNSlowIO
);
490 WriteDOC(datum
, docptr
, Mil_CDSN_IO
);
491 WriteDOC(datum
, docptr
, WritePipeTerm
);
494 static u_char
doc2001_read_byte(struct mtd_info
*mtd
)
496 struct nand_chip
*this = mtd
->priv
;
497 struct doc_priv
*doc
= this->priv
;
498 void __iomem
*docptr
= doc
->virtadr
;
500 //ReadDOC(docptr, CDSNSlowIO);
501 /* 11.4.5 -- delay twice to allow extended length cycle */
503 ReadDOC(docptr
, ReadPipeInit
);
504 //return ReadDOC(docptr, Mil_CDSN_IO);
505 return ReadDOC(docptr
, LastDataRead
);
508 static void doc2001_writebuf(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
510 struct nand_chip
*this = mtd
->priv
;
511 struct doc_priv
*doc
= this->priv
;
512 void __iomem
*docptr
= doc
->virtadr
;
515 for (i
= 0; i
< len
; i
++)
516 WriteDOC_(buf
[i
], docptr
, DoC_Mil_CDSN_IO
+ i
);
517 /* Terminate write pipeline */
518 WriteDOC(0x00, docptr
, WritePipeTerm
);
521 static void doc2001_readbuf(struct mtd_info
*mtd
, u_char
*buf
, int len
)
523 struct nand_chip
*this = mtd
->priv
;
524 struct doc_priv
*doc
= this->priv
;
525 void __iomem
*docptr
= doc
->virtadr
;
528 /* Start read pipeline */
529 ReadDOC(docptr
, ReadPipeInit
);
531 for (i
= 0; i
< len
- 1; i
++)
532 buf
[i
] = ReadDOC(docptr
, Mil_CDSN_IO
+ (i
& 0xff));
534 /* Terminate read pipeline */
535 buf
[i
] = ReadDOC(docptr
, LastDataRead
);
538 static int doc2001_verifybuf(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
540 struct nand_chip
*this = mtd
->priv
;
541 struct doc_priv
*doc
= this->priv
;
542 void __iomem
*docptr
= doc
->virtadr
;
545 /* Start read pipeline */
546 ReadDOC(docptr
, ReadPipeInit
);
548 for (i
= 0; i
< len
- 1; i
++)
549 if (buf
[i
] != ReadDOC(docptr
, Mil_CDSN_IO
)) {
550 ReadDOC(docptr
, LastDataRead
);
553 if (buf
[i
] != ReadDOC(docptr
, LastDataRead
))
558 static u_char
doc2001plus_read_byte(struct mtd_info
*mtd
)
560 struct nand_chip
*this = mtd
->priv
;
561 struct doc_priv
*doc
= this->priv
;
562 void __iomem
*docptr
= doc
->virtadr
;
565 ReadDOC(docptr
, Mplus_ReadPipeInit
);
566 ReadDOC(docptr
, Mplus_ReadPipeInit
);
567 ret
= ReadDOC(docptr
, Mplus_LastDataRead
);
569 printk("read_byte returns %02x\n", ret
);
573 static void doc2001plus_writebuf(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
575 struct nand_chip
*this = mtd
->priv
;
576 struct doc_priv
*doc
= this->priv
;
577 void __iomem
*docptr
= doc
->virtadr
;
581 printk("writebuf of %d bytes: ", len
);
582 for (i
= 0; i
< len
; i
++) {
583 WriteDOC_(buf
[i
], docptr
, DoC_Mil_CDSN_IO
+ i
);
585 printk("%02x ", buf
[i
]);
591 static void doc2001plus_readbuf(struct mtd_info
*mtd
, u_char
*buf
, int len
)
593 struct nand_chip
*this = mtd
->priv
;
594 struct doc_priv
*doc
= this->priv
;
595 void __iomem
*docptr
= doc
->virtadr
;
599 printk("readbuf of %d bytes: ", len
);
601 /* Start read pipeline */
602 ReadDOC(docptr
, Mplus_ReadPipeInit
);
603 ReadDOC(docptr
, Mplus_ReadPipeInit
);
605 for (i
= 0; i
< len
- 2; i
++) {
606 buf
[i
] = ReadDOC(docptr
, Mil_CDSN_IO
);
608 printk("%02x ", buf
[i
]);
611 /* Terminate read pipeline */
612 buf
[len
- 2] = ReadDOC(docptr
, Mplus_LastDataRead
);
614 printk("%02x ", buf
[len
- 2]);
615 buf
[len
- 1] = ReadDOC(docptr
, Mplus_LastDataRead
);
617 printk("%02x ", buf
[len
- 1]);
622 static int doc2001plus_verifybuf(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
624 struct nand_chip
*this = mtd
->priv
;
625 struct doc_priv
*doc
= this->priv
;
626 void __iomem
*docptr
= doc
->virtadr
;
630 printk("verifybuf of %d bytes: ", len
);
632 /* Start read pipeline */
633 ReadDOC(docptr
, Mplus_ReadPipeInit
);
634 ReadDOC(docptr
, Mplus_ReadPipeInit
);
636 for (i
= 0; i
< len
- 2; i
++)
637 if (buf
[i
] != ReadDOC(docptr
, Mil_CDSN_IO
)) {
638 ReadDOC(docptr
, Mplus_LastDataRead
);
639 ReadDOC(docptr
, Mplus_LastDataRead
);
642 if (buf
[len
- 2] != ReadDOC(docptr
, Mplus_LastDataRead
))
644 if (buf
[len
- 1] != ReadDOC(docptr
, Mplus_LastDataRead
))
649 static void doc2001plus_select_chip(struct mtd_info
*mtd
, int chip
)
651 struct nand_chip
*this = mtd
->priv
;
652 struct doc_priv
*doc
= this->priv
;
653 void __iomem
*docptr
= doc
->virtadr
;
657 printk("select chip (%d)\n", chip
);
660 /* Disable flash internally */
661 WriteDOC(0, docptr
, Mplus_FlashSelect
);
665 floor
= chip
/ doc
->chips_per_floor
;
666 chip
-= (floor
* doc
->chips_per_floor
);
668 /* Assert ChipEnable and deassert WriteProtect */
669 WriteDOC((DOC_FLASH_CE
), docptr
, Mplus_FlashSelect
);
670 this->cmdfunc(mtd
, NAND_CMD_RESET
, -1, -1);
673 doc
->curfloor
= floor
;
676 static void doc200x_select_chip(struct mtd_info
*mtd
, int chip
)
678 struct nand_chip
*this = mtd
->priv
;
679 struct doc_priv
*doc
= this->priv
;
680 void __iomem
*docptr
= doc
->virtadr
;
684 printk("select chip (%d)\n", chip
);
689 floor
= chip
/ doc
->chips_per_floor
;
690 chip
-= (floor
* doc
->chips_per_floor
);
692 /* 11.4.4 -- deassert CE before changing chip */
693 doc200x_hwcontrol(mtd
, NAND_CTL_CLRNCE
);
695 WriteDOC(floor
, docptr
, FloorSelect
);
696 WriteDOC(chip
, docptr
, CDSNDeviceSelect
);
698 doc200x_hwcontrol(mtd
, NAND_CTL_SETNCE
);
701 doc
->curfloor
= floor
;
704 static void doc200x_hwcontrol(struct mtd_info
*mtd
, int cmd
)
706 struct nand_chip
*this = mtd
->priv
;
707 struct doc_priv
*doc
= this->priv
;
708 void __iomem
*docptr
= doc
->virtadr
;
711 case NAND_CTL_SETNCE
:
712 doc
->CDSNControl
|= CDSN_CTRL_CE
;
714 case NAND_CTL_CLRNCE
:
715 doc
->CDSNControl
&= ~CDSN_CTRL_CE
;
717 case NAND_CTL_SETCLE
:
718 doc
->CDSNControl
|= CDSN_CTRL_CLE
;
720 case NAND_CTL_CLRCLE
:
721 doc
->CDSNControl
&= ~CDSN_CTRL_CLE
;
723 case NAND_CTL_SETALE
:
724 doc
->CDSNControl
|= CDSN_CTRL_ALE
;
726 case NAND_CTL_CLRALE
:
727 doc
->CDSNControl
&= ~CDSN_CTRL_ALE
;
730 doc
->CDSNControl
|= CDSN_CTRL_WP
;
733 doc
->CDSNControl
&= ~CDSN_CTRL_WP
;
737 printk("hwcontrol(%d): %02x\n", cmd
, doc
->CDSNControl
);
738 WriteDOC(doc
->CDSNControl
, docptr
, CDSNControl
);
739 /* 11.4.3 -- 4 NOPs after CSDNControl write */
743 static void doc2001plus_command(struct mtd_info
*mtd
, unsigned command
, int column
, int page_addr
)
745 struct nand_chip
*this = mtd
->priv
;
746 struct doc_priv
*doc
= this->priv
;
747 void __iomem
*docptr
= doc
->virtadr
;
750 * Must terminate write pipeline before sending any commands
753 if (command
== NAND_CMD_PAGEPROG
) {
754 WriteDOC(0x00, docptr
, Mplus_WritePipeTerm
);
755 WriteDOC(0x00, docptr
, Mplus_WritePipeTerm
);
759 * Write out the command to the device.
761 if (command
== NAND_CMD_SEQIN
) {
764 if (column
>= mtd
->oobblock
) {
766 column
-= mtd
->oobblock
;
767 readcmd
= NAND_CMD_READOOB
;
768 } else if (column
< 256) {
769 /* First 256 bytes --> READ0 */
770 readcmd
= NAND_CMD_READ0
;
773 readcmd
= NAND_CMD_READ1
;
775 WriteDOC(readcmd
, docptr
, Mplus_FlashCmd
);
777 WriteDOC(command
, docptr
, Mplus_FlashCmd
);
778 WriteDOC(0, docptr
, Mplus_WritePipeTerm
);
779 WriteDOC(0, docptr
, Mplus_WritePipeTerm
);
781 if (column
!= -1 || page_addr
!= -1) {
782 /* Serially input address */
784 /* Adjust columns for 16 bit buswidth */
785 if (this->options
& NAND_BUSWIDTH_16
)
787 WriteDOC(column
, docptr
, Mplus_FlashAddress
);
789 if (page_addr
!= -1) {
790 WriteDOC((unsigned char)(page_addr
& 0xff), docptr
, Mplus_FlashAddress
);
791 WriteDOC((unsigned char)((page_addr
>> 8) & 0xff), docptr
, Mplus_FlashAddress
);
792 /* One more address cycle for higher density devices */
793 if (this->chipsize
& 0x0c000000) {
794 WriteDOC((unsigned char)((page_addr
>> 16) & 0x0f), docptr
, Mplus_FlashAddress
);
795 printk("high density\n");
798 WriteDOC(0, docptr
, Mplus_WritePipeTerm
);
799 WriteDOC(0, docptr
, Mplus_WritePipeTerm
);
801 if (command
== NAND_CMD_READ0
|| command
== NAND_CMD_READ1
||
802 command
== NAND_CMD_READOOB
|| command
== NAND_CMD_READID
)
803 WriteDOC(0, docptr
, Mplus_FlashControl
);
807 * program and erase have their own busy handlers
808 * status and sequential in needs no delay
812 case NAND_CMD_PAGEPROG
:
813 case NAND_CMD_ERASE1
:
814 case NAND_CMD_ERASE2
:
816 case NAND_CMD_STATUS
:
822 udelay(this->chip_delay
);
823 WriteDOC(NAND_CMD_STATUS
, docptr
, Mplus_FlashCmd
);
824 WriteDOC(0, docptr
, Mplus_WritePipeTerm
);
825 WriteDOC(0, docptr
, Mplus_WritePipeTerm
);
826 while (!(this->read_byte(mtd
) & 0x40)) ;
829 /* This applies to read commands */
832 * If we don't have access to the busy pin, we apply the given
835 if (!this->dev_ready
) {
836 udelay(this->chip_delay
);
841 /* Apply this short delay always to ensure that we do wait tWB in
842 * any case on any machine. */
844 /* wait until command is processed */
845 while (!this->dev_ready(mtd
)) ;
848 static int doc200x_dev_ready(struct mtd_info
*mtd
)
850 struct nand_chip
*this = mtd
->priv
;
851 struct doc_priv
*doc
= this->priv
;
852 void __iomem
*docptr
= doc
->virtadr
;
854 if (DoC_is_MillenniumPlus(doc
)) {
855 /* 11.4.2 -- must NOP four times before checking FR/B# */
857 if ((ReadDOC(docptr
, Mplus_FlashControl
) & CDSN_CTRL_FR_B_MASK
) != CDSN_CTRL_FR_B_MASK
) {
859 printk("not ready\n");
863 printk("was ready\n");
866 /* 11.4.2 -- must NOP four times before checking FR/B# */
868 if (!(ReadDOC(docptr
, CDSNControl
) & CDSN_CTRL_FR_B
)) {
870 printk("not ready\n");
873 /* 11.4.2 -- Must NOP twice if it's ready */
876 printk("was ready\n");
881 static int doc200x_block_bad(struct mtd_info
*mtd
, loff_t ofs
, int getchip
)
883 /* This is our last resort if we couldn't find or create a BBT. Just
884 pretend all blocks are good. */
888 static void doc200x_enable_hwecc(struct mtd_info
*mtd
, int mode
)
890 struct nand_chip
*this = mtd
->priv
;
891 struct doc_priv
*doc
= this->priv
;
892 void __iomem
*docptr
= doc
->virtadr
;
894 /* Prime the ECC engine */
897 WriteDOC(DOC_ECC_RESET
, docptr
, ECCConf
);
898 WriteDOC(DOC_ECC_EN
, docptr
, ECCConf
);
901 WriteDOC(DOC_ECC_RESET
, docptr
, ECCConf
);
902 WriteDOC(DOC_ECC_EN
| DOC_ECC_RW
, docptr
, ECCConf
);
907 static void doc2001plus_enable_hwecc(struct mtd_info
*mtd
, int mode
)
909 struct nand_chip
*this = mtd
->priv
;
910 struct doc_priv
*doc
= this->priv
;
911 void __iomem
*docptr
= doc
->virtadr
;
913 /* Prime the ECC engine */
916 WriteDOC(DOC_ECC_RESET
, docptr
, Mplus_ECCConf
);
917 WriteDOC(DOC_ECC_EN
, docptr
, Mplus_ECCConf
);
920 WriteDOC(DOC_ECC_RESET
, docptr
, Mplus_ECCConf
);
921 WriteDOC(DOC_ECC_EN
| DOC_ECC_RW
, docptr
, Mplus_ECCConf
);
926 /* This code is only called on write */
927 static int doc200x_calculate_ecc(struct mtd_info
*mtd
, const u_char
*dat
, unsigned char *ecc_code
)
929 struct nand_chip
*this = mtd
->priv
;
930 struct doc_priv
*doc
= this->priv
;
931 void __iomem
*docptr
= doc
->virtadr
;
935 /* flush the pipeline */
936 if (DoC_is_2000(doc
)) {
937 WriteDOC(doc
->CDSNControl
& ~CDSN_CTRL_FLASH_IO
, docptr
, CDSNControl
);
938 WriteDOC(0, docptr
, 2k_CDSN_IO
);
939 WriteDOC(0, docptr
, 2k_CDSN_IO
);
940 WriteDOC(0, docptr
, 2k_CDSN_IO
);
941 WriteDOC(doc
->CDSNControl
, docptr
, CDSNControl
);
942 } else if (DoC_is_MillenniumPlus(doc
)) {
943 WriteDOC(0, docptr
, Mplus_NOP
);
944 WriteDOC(0, docptr
, Mplus_NOP
);
945 WriteDOC(0, docptr
, Mplus_NOP
);
947 WriteDOC(0, docptr
, NOP
);
948 WriteDOC(0, docptr
, NOP
);
949 WriteDOC(0, docptr
, NOP
);
952 for (i
= 0; i
< 6; i
++) {
953 if (DoC_is_MillenniumPlus(doc
))
954 ecc_code
[i
] = ReadDOC_(docptr
, DoC_Mplus_ECCSyndrome0
+ i
);
956 ecc_code
[i
] = ReadDOC_(docptr
, DoC_ECCSyndrome0
+ i
);
957 if (ecc_code
[i
] != empty_write_ecc
[i
])
960 if (DoC_is_MillenniumPlus(doc
))
961 WriteDOC(DOC_ECC_DIS
, docptr
, Mplus_ECCConf
);
963 WriteDOC(DOC_ECC_DIS
, docptr
, ECCConf
);
965 /* If emptymatch=1, we might have an all-0xff data buffer. Check. */
967 /* Note: this somewhat expensive test should not be triggered
968 often. It could be optimized away by examining the data in
969 the writebuf routine, and remembering the result. */
970 for (i
= 0; i
< 512; i
++) {
977 /* If emptymatch still =1, we do have an all-0xff data buffer.
978 Return all-0xff ecc value instead of the computed one, so
979 it'll look just like a freshly-erased page. */
981 memset(ecc_code
, 0xff, 6);
986 static int doc200x_correct_data(struct mtd_info
*mtd
, u_char
*dat
, u_char
*read_ecc
, u_char
*calc_ecc
)
989 struct nand_chip
*this = mtd
->priv
;
990 struct doc_priv
*doc
= this->priv
;
991 void __iomem
*docptr
= doc
->virtadr
;
992 volatile u_char dummy
;
995 /* flush the pipeline */
996 if (DoC_is_2000(doc
)) {
997 dummy
= ReadDOC(docptr
, 2k_ECCStatus
);
998 dummy
= ReadDOC(docptr
, 2k_ECCStatus
);
999 dummy
= ReadDOC(docptr
, 2k_ECCStatus
);
1000 } else if (DoC_is_MillenniumPlus(doc
)) {
1001 dummy
= ReadDOC(docptr
, Mplus_ECCConf
);
1002 dummy
= ReadDOC(docptr
, Mplus_ECCConf
);
1003 dummy
= ReadDOC(docptr
, Mplus_ECCConf
);
1005 dummy
= ReadDOC(docptr
, ECCConf
);
1006 dummy
= ReadDOC(docptr
, ECCConf
);
1007 dummy
= ReadDOC(docptr
, ECCConf
);
1010 /* Error occured ? */
1012 for (i
= 0; i
< 6; i
++) {
1013 if (DoC_is_MillenniumPlus(doc
))
1014 calc_ecc
[i
] = ReadDOC_(docptr
, DoC_Mplus_ECCSyndrome0
+ i
);
1016 calc_ecc
[i
] = ReadDOC_(docptr
, DoC_ECCSyndrome0
+ i
);
1017 if (calc_ecc
[i
] != empty_read_syndrome
[i
])
1020 /* If emptymatch=1, the read syndrome is consistent with an
1021 all-0xff data and stored ecc block. Check the stored ecc. */
1023 for (i
= 0; i
< 6; i
++) {
1024 if (read_ecc
[i
] == 0xff)
1030 /* If emptymatch still =1, check the data block. */
1032 /* Note: this somewhat expensive test should not be triggered
1033 often. It could be optimized away by examining the data in
1034 the readbuf routine, and remembering the result. */
1035 for (i
= 0; i
< 512; i
++) {
1042 /* If emptymatch still =1, this is almost certainly a freshly-
1043 erased block, in which case the ECC will not come out right.
1044 We'll suppress the error and tell the caller everything's
1045 OK. Because it is. */
1047 ret
= doc_ecc_decode(rs_decoder
, dat
, calc_ecc
);
1049 printk(KERN_ERR
"doc200x_correct_data corrected %d errors\n", ret
);
1051 if (DoC_is_MillenniumPlus(doc
))
1052 WriteDOC(DOC_ECC_DIS
, docptr
, Mplus_ECCConf
);
1054 WriteDOC(DOC_ECC_DIS
, docptr
, ECCConf
);
1055 if (no_ecc_failures
&& (ret
== -1)) {
1056 printk(KERN_ERR
"suppressing ECC failure\n");
1062 //u_char mydatabuf[528];
1064 /* The strange out-of-order .oobfree list below is a (possibly unneeded)
1065 * attempt to retain compatibility. It used to read:
1066 * .oobfree = { {8, 8} }
1067 * Since that leaves two bytes unusable, it was changed. But the following
1068 * scheme might affect existing jffs2 installs by moving the cleanmarker:
1069 * .oobfree = { {6, 10} }
1070 * jffs2 seems to handle the above gracefully, but the current scheme seems
1071 * safer. The only problem with it is that any code that parses oobfree must
1072 * be able to handle out-of-order segments.
1074 static struct nand_oobinfo doc200x_oobinfo
= {
1075 .useecc
= MTD_NANDECC_AUTOPLACE
,
1077 .eccpos
= {0, 1, 2, 3, 4, 5},
1078 .oobfree
= {{8, 8}, {6, 2}}
1081 /* Find the (I)NFTL Media Header, and optionally also the mirror media header.
1082 On sucessful return, buf will contain a copy of the media header for
1083 further processing. id is the string to scan for, and will presumably be
1084 either "ANAND" or "BNAND". If findmirror=1, also look for the mirror media
1085 header. The page #s of the found media headers are placed in mh0_page and
1086 mh1_page in the DOC private structure. */
1087 static int __init
find_media_headers(struct mtd_info
*mtd
, u_char
*buf
, const char *id
, int findmirror
)
1089 struct nand_chip
*this = mtd
->priv
;
1090 struct doc_priv
*doc
= this->priv
;
1095 for (offs
= 0; offs
< mtd
->size
; offs
+= mtd
->erasesize
) {
1096 ret
= mtd
->read(mtd
, offs
, mtd
->oobblock
, &retlen
, buf
);
1097 if (retlen
!= mtd
->oobblock
)
1100 printk(KERN_WARNING
"ECC error scanning DOC at 0x%x\n", offs
);
1102 if (memcmp(buf
, id
, 6))
1104 printk(KERN_INFO
"Found DiskOnChip %s Media Header at 0x%x\n", id
, offs
);
1105 if (doc
->mh0_page
== -1) {
1106 doc
->mh0_page
= offs
>> this->page_shift
;
1111 doc
->mh1_page
= offs
>> this->page_shift
;
1114 if (doc
->mh0_page
== -1) {
1115 printk(KERN_WARNING
"DiskOnChip %s Media Header not found.\n", id
);
1118 /* Only one mediaheader was found. We want buf to contain a
1119 mediaheader on return, so we'll have to re-read the one we found. */
1120 offs
= doc
->mh0_page
<< this->page_shift
;
1121 ret
= mtd
->read(mtd
, offs
, mtd
->oobblock
, &retlen
, buf
);
1122 if (retlen
!= mtd
->oobblock
) {
1123 /* Insanity. Give up. */
1124 printk(KERN_ERR
"Read DiskOnChip Media Header once, but can't reread it???\n");
1130 static inline int __init
nftl_partscan(struct mtd_info
*mtd
, struct mtd_partition
*parts
)
1132 struct nand_chip
*this = mtd
->priv
;
1133 struct doc_priv
*doc
= this->priv
;
1136 struct NFTLMediaHeader
*mh
;
1137 const unsigned psize
= 1 << this->page_shift
;
1139 unsigned blocks
, maxblocks
;
1140 int offs
, numheaders
;
1142 buf
= kmalloc(mtd
->oobblock
, GFP_KERNEL
);
1144 printk(KERN_ERR
"DiskOnChip mediaheader kmalloc failed!\n");
1147 if (!(numheaders
= find_media_headers(mtd
, buf
, "ANAND", 1)))
1149 mh
= (struct NFTLMediaHeader
*)buf
;
1151 mh
->NumEraseUnits
= le16_to_cpu(mh
->NumEraseUnits
);
1152 mh
->FirstPhysicalEUN
= le16_to_cpu(mh
->FirstPhysicalEUN
);
1153 mh
->FormattedSize
= le32_to_cpu(mh
->FormattedSize
);
1155 printk(KERN_INFO
" DataOrgID = %s\n"
1156 " NumEraseUnits = %d\n"
1157 " FirstPhysicalEUN = %d\n"
1158 " FormattedSize = %d\n"
1159 " UnitSizeFactor = %d\n",
1160 mh
->DataOrgID
, mh
->NumEraseUnits
,
1161 mh
->FirstPhysicalEUN
, mh
->FormattedSize
,
1162 mh
->UnitSizeFactor
);
1164 blocks
= mtd
->size
>> this->phys_erase_shift
;
1165 maxblocks
= min(32768U, mtd
->erasesize
- psize
);
1167 if (mh
->UnitSizeFactor
== 0x00) {
1168 /* Auto-determine UnitSizeFactor. The constraints are:
1169 - There can be at most 32768 virtual blocks.
1170 - There can be at most (virtual block size - page size)
1171 virtual blocks (because MediaHeader+BBT must fit in 1).
1173 mh
->UnitSizeFactor
= 0xff;
1174 while (blocks
> maxblocks
) {
1176 maxblocks
= min(32768U, (maxblocks
<< 1) + psize
);
1177 mh
->UnitSizeFactor
--;
1179 printk(KERN_WARNING
"UnitSizeFactor=0x00 detected. Correct value is assumed to be 0x%02x.\n", mh
->UnitSizeFactor
);
1182 /* NOTE: The lines below modify internal variables of the NAND and MTD
1183 layers; variables with have already been configured by nand_scan.
1184 Unfortunately, we didn't know before this point what these values
1185 should be. Thus, this code is somewhat dependant on the exact
1186 implementation of the NAND layer. */
1187 if (mh
->UnitSizeFactor
!= 0xff) {
1188 this->bbt_erase_shift
+= (0xff - mh
->UnitSizeFactor
);
1189 mtd
->erasesize
<<= (0xff - mh
->UnitSizeFactor
);
1190 printk(KERN_INFO
"Setting virtual erase size to %d\n", mtd
->erasesize
);
1191 blocks
= mtd
->size
>> this->bbt_erase_shift
;
1192 maxblocks
= min(32768U, mtd
->erasesize
- psize
);
1195 if (blocks
> maxblocks
) {
1196 printk(KERN_ERR
"UnitSizeFactor of 0x%02x is inconsistent with device size. Aborting.\n", mh
->UnitSizeFactor
);
1200 /* Skip past the media headers. */
1201 offs
= max(doc
->mh0_page
, doc
->mh1_page
);
1202 offs
<<= this->page_shift
;
1203 offs
+= mtd
->erasesize
;
1205 if (show_firmware_partition
== 1) {
1206 parts
[0].name
= " DiskOnChip Firmware / Media Header partition";
1207 parts
[0].offset
= 0;
1208 parts
[0].size
= offs
;
1212 parts
[numparts
].name
= " DiskOnChip BDTL partition";
1213 parts
[numparts
].offset
= offs
;
1214 parts
[numparts
].size
= (mh
->NumEraseUnits
- numheaders
) << this->bbt_erase_shift
;
1216 offs
+= parts
[numparts
].size
;
1219 if (offs
< mtd
->size
) {
1220 parts
[numparts
].name
= " DiskOnChip Remainder partition";
1221 parts
[numparts
].offset
= offs
;
1222 parts
[numparts
].size
= mtd
->size
- offs
;
1232 /* This is a stripped-down copy of the code in inftlmount.c */
1233 static inline int __init
inftl_partscan(struct mtd_info
*mtd
, struct mtd_partition
*parts
)
1235 struct nand_chip
*this = mtd
->priv
;
1236 struct doc_priv
*doc
= this->priv
;
1239 struct INFTLMediaHeader
*mh
;
1240 struct INFTLPartition
*ip
;
1243 int vshift
, lastvunit
= 0;
1245 int end
= mtd
->size
;
1247 if (inftl_bbt_write
)
1248 end
-= (INFTL_BBT_RESERVED_BLOCKS
<< this->phys_erase_shift
);
1250 buf
= kmalloc(mtd
->oobblock
, GFP_KERNEL
);
1252 printk(KERN_ERR
"DiskOnChip mediaheader kmalloc failed!\n");
1256 if (!find_media_headers(mtd
, buf
, "BNAND", 0))
1258 doc
->mh1_page
= doc
->mh0_page
+ (4096 >> this->page_shift
);
1259 mh
= (struct INFTLMediaHeader
*)buf
;
1261 mh
->NoOfBootImageBlocks
= le32_to_cpu(mh
->NoOfBootImageBlocks
);
1262 mh
->NoOfBinaryPartitions
= le32_to_cpu(mh
->NoOfBinaryPartitions
);
1263 mh
->NoOfBDTLPartitions
= le32_to_cpu(mh
->NoOfBDTLPartitions
);
1264 mh
->BlockMultiplierBits
= le32_to_cpu(mh
->BlockMultiplierBits
);
1265 mh
->FormatFlags
= le32_to_cpu(mh
->FormatFlags
);
1266 mh
->PercentUsed
= le32_to_cpu(mh
->PercentUsed
);
1268 printk(KERN_INFO
" bootRecordID = %s\n"
1269 " NoOfBootImageBlocks = %d\n"
1270 " NoOfBinaryPartitions = %d\n"
1271 " NoOfBDTLPartitions = %d\n"
1272 " BlockMultiplerBits = %d\n"
1273 " FormatFlgs = %d\n"
1274 " OsakVersion = %d.%d.%d.%d\n"
1275 " PercentUsed = %d\n",
1276 mh
->bootRecordID
, mh
->NoOfBootImageBlocks
,
1277 mh
->NoOfBinaryPartitions
,
1278 mh
->NoOfBDTLPartitions
,
1279 mh
->BlockMultiplierBits
, mh
->FormatFlags
,
1280 ((unsigned char *) &mh
->OsakVersion
)[0] & 0xf,
1281 ((unsigned char *) &mh
->OsakVersion
)[1] & 0xf,
1282 ((unsigned char *) &mh
->OsakVersion
)[2] & 0xf,
1283 ((unsigned char *) &mh
->OsakVersion
)[3] & 0xf,
1286 vshift
= this->phys_erase_shift
+ mh
->BlockMultiplierBits
;
1288 blocks
= mtd
->size
>> vshift
;
1289 if (blocks
> 32768) {
1290 printk(KERN_ERR
"BlockMultiplierBits=%d is inconsistent with device size. Aborting.\n", mh
->BlockMultiplierBits
);
1294 blocks
= doc
->chips_per_floor
<< (this->chip_shift
- this->phys_erase_shift
);
1295 if (inftl_bbt_write
&& (blocks
> mtd
->erasesize
)) {
1296 printk(KERN_ERR
"Writeable BBTs spanning more than one erase block are not yet supported. FIX ME!\n");
1300 /* Scan the partitions */
1301 for (i
= 0; (i
< 4); i
++) {
1302 ip
= &(mh
->Partitions
[i
]);
1303 ip
->virtualUnits
= le32_to_cpu(ip
->virtualUnits
);
1304 ip
->firstUnit
= le32_to_cpu(ip
->firstUnit
);
1305 ip
->lastUnit
= le32_to_cpu(ip
->lastUnit
);
1306 ip
->flags
= le32_to_cpu(ip
->flags
);
1307 ip
->spareUnits
= le32_to_cpu(ip
->spareUnits
);
1308 ip
->Reserved0
= le32_to_cpu(ip
->Reserved0
);
1310 printk(KERN_INFO
" PARTITION[%d] ->\n"
1311 " virtualUnits = %d\n"
1315 " spareUnits = %d\n",
1316 i
, ip
->virtualUnits
, ip
->firstUnit
,
1317 ip
->lastUnit
, ip
->flags
,
1320 if ((show_firmware_partition
== 1) &&
1321 (i
== 0) && (ip
->firstUnit
> 0)) {
1322 parts
[0].name
= " DiskOnChip IPL / Media Header partition";
1323 parts
[0].offset
= 0;
1324 parts
[0].size
= mtd
->erasesize
* ip
->firstUnit
;
1328 if (ip
->flags
& INFTL_BINARY
)
1329 parts
[numparts
].name
= " DiskOnChip BDK partition";
1331 parts
[numparts
].name
= " DiskOnChip BDTL partition";
1332 parts
[numparts
].offset
= ip
->firstUnit
<< vshift
;
1333 parts
[numparts
].size
= (1 + ip
->lastUnit
- ip
->firstUnit
) << vshift
;
1335 if (ip
->lastUnit
> lastvunit
)
1336 lastvunit
= ip
->lastUnit
;
1337 if (ip
->flags
& INFTL_LAST
)
1341 if ((lastvunit
<< vshift
) < end
) {
1342 parts
[numparts
].name
= " DiskOnChip Remainder partition";
1343 parts
[numparts
].offset
= lastvunit
<< vshift
;
1344 parts
[numparts
].size
= end
- parts
[numparts
].offset
;
1353 static int __init
nftl_scan_bbt(struct mtd_info
*mtd
)
1356 struct nand_chip
*this = mtd
->priv
;
1357 struct doc_priv
*doc
= this->priv
;
1358 struct mtd_partition parts
[2];
1360 memset((char *)parts
, 0, sizeof(parts
));
1361 /* On NFTL, we have to find the media headers before we can read the
1362 BBTs, since they're stored in the media header eraseblocks. */
1363 numparts
= nftl_partscan(mtd
, parts
);
1366 this->bbt_td
->options
= NAND_BBT_ABSPAGE
| NAND_BBT_8BIT
|
1367 NAND_BBT_SAVECONTENT
| NAND_BBT_WRITE
|
1369 this->bbt_td
->veroffs
= 7;
1370 this->bbt_td
->pages
[0] = doc
->mh0_page
+ 1;
1371 if (doc
->mh1_page
!= -1) {
1372 this->bbt_md
->options
= NAND_BBT_ABSPAGE
| NAND_BBT_8BIT
|
1373 NAND_BBT_SAVECONTENT
| NAND_BBT_WRITE
|
1375 this->bbt_md
->veroffs
= 7;
1376 this->bbt_md
->pages
[0] = doc
->mh1_page
+ 1;
1378 this->bbt_md
= NULL
;
1381 /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
1382 At least as nand_bbt.c is currently written. */
1383 if ((ret
= nand_scan_bbt(mtd
, NULL
)))
1385 add_mtd_device(mtd
);
1386 #ifdef CONFIG_MTD_PARTITIONS
1388 add_mtd_partitions(mtd
, parts
, numparts
);
1393 static int __init
inftl_scan_bbt(struct mtd_info
*mtd
)
1396 struct nand_chip
*this = mtd
->priv
;
1397 struct doc_priv
*doc
= this->priv
;
1398 struct mtd_partition parts
[5];
1400 if (this->numchips
> doc
->chips_per_floor
) {
1401 printk(KERN_ERR
"Multi-floor INFTL devices not yet supported.\n");
1405 if (DoC_is_MillenniumPlus(doc
)) {
1406 this->bbt_td
->options
= NAND_BBT_2BIT
| NAND_BBT_ABSPAGE
;
1407 if (inftl_bbt_write
)
1408 this->bbt_td
->options
|= NAND_BBT_WRITE
;
1409 this->bbt_td
->pages
[0] = 2;
1410 this->bbt_md
= NULL
;
1412 this->bbt_td
->options
= NAND_BBT_LASTBLOCK
| NAND_BBT_8BIT
| NAND_BBT_VERSION
;
1413 if (inftl_bbt_write
)
1414 this->bbt_td
->options
|= NAND_BBT_WRITE
;
1415 this->bbt_td
->offs
= 8;
1416 this->bbt_td
->len
= 8;
1417 this->bbt_td
->veroffs
= 7;
1418 this->bbt_td
->maxblocks
= INFTL_BBT_RESERVED_BLOCKS
;
1419 this->bbt_td
->reserved_block_code
= 0x01;
1420 this->bbt_td
->pattern
= "MSYS_BBT";
1422 this->bbt_md
->options
= NAND_BBT_LASTBLOCK
| NAND_BBT_8BIT
| NAND_BBT_VERSION
;
1423 if (inftl_bbt_write
)
1424 this->bbt_md
->options
|= NAND_BBT_WRITE
;
1425 this->bbt_md
->offs
= 8;
1426 this->bbt_md
->len
= 8;
1427 this->bbt_md
->veroffs
= 7;
1428 this->bbt_md
->maxblocks
= INFTL_BBT_RESERVED_BLOCKS
;
1429 this->bbt_md
->reserved_block_code
= 0x01;
1430 this->bbt_md
->pattern
= "TBB_SYSM";
1433 /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
1434 At least as nand_bbt.c is currently written. */
1435 if ((ret
= nand_scan_bbt(mtd
, NULL
)))
1437 memset((char *)parts
, 0, sizeof(parts
));
1438 numparts
= inftl_partscan(mtd
, parts
);
1439 /* At least for now, require the INFTL Media Header. We could probably
1440 do without it for non-INFTL use, since all it gives us is
1441 autopartitioning, but I want to give it more thought. */
1444 add_mtd_device(mtd
);
1445 #ifdef CONFIG_MTD_PARTITIONS
1447 add_mtd_partitions(mtd
, parts
, numparts
);
1452 static inline int __init
doc2000_init(struct mtd_info
*mtd
)
1454 struct nand_chip
*this = mtd
->priv
;
1455 struct doc_priv
*doc
= this->priv
;
1457 this->write_byte
= doc2000_write_byte
;
1458 this->read_byte
= doc2000_read_byte
;
1459 this->write_buf
= doc2000_writebuf
;
1460 this->read_buf
= doc2000_readbuf
;
1461 this->verify_buf
= doc2000_verifybuf
;
1462 this->scan_bbt
= nftl_scan_bbt
;
1464 doc
->CDSNControl
= CDSN_CTRL_FLASH_IO
| CDSN_CTRL_ECC_IO
;
1465 doc2000_count_chips(mtd
);
1466 mtd
->name
= "DiskOnChip 2000 (NFTL Model)";
1467 return (4 * doc
->chips_per_floor
);
1470 static inline int __init
doc2001_init(struct mtd_info
*mtd
)
1472 struct nand_chip
*this = mtd
->priv
;
1473 struct doc_priv
*doc
= this->priv
;
1475 this->write_byte
= doc2001_write_byte
;
1476 this->read_byte
= doc2001_read_byte
;
1477 this->write_buf
= doc2001_writebuf
;
1478 this->read_buf
= doc2001_readbuf
;
1479 this->verify_buf
= doc2001_verifybuf
;
1481 ReadDOC(doc
->virtadr
, ChipID
);
1482 ReadDOC(doc
->virtadr
, ChipID
);
1483 ReadDOC(doc
->virtadr
, ChipID
);
1484 if (ReadDOC(doc
->virtadr
, ChipID
) != DOC_ChipID_DocMil
) {
1485 /* It's not a Millennium; it's one of the newer
1486 DiskOnChip 2000 units with a similar ASIC.
1487 Treat it like a Millennium, except that it
1488 can have multiple chips. */
1489 doc2000_count_chips(mtd
);
1490 mtd
->name
= "DiskOnChip 2000 (INFTL Model)";
1491 this->scan_bbt
= inftl_scan_bbt
;
1492 return (4 * doc
->chips_per_floor
);
1494 /* Bog-standard Millennium */
1495 doc
->chips_per_floor
= 1;
1496 mtd
->name
= "DiskOnChip Millennium";
1497 this->scan_bbt
= nftl_scan_bbt
;
1502 static inline int __init
doc2001plus_init(struct mtd_info
*mtd
)
1504 struct nand_chip
*this = mtd
->priv
;
1505 struct doc_priv
*doc
= this->priv
;
1507 this->write_byte
= NULL
;
1508 this->read_byte
= doc2001plus_read_byte
;
1509 this->write_buf
= doc2001plus_writebuf
;
1510 this->read_buf
= doc2001plus_readbuf
;
1511 this->verify_buf
= doc2001plus_verifybuf
;
1512 this->scan_bbt
= inftl_scan_bbt
;
1513 this->hwcontrol
= NULL
;
1514 this->select_chip
= doc2001plus_select_chip
;
1515 this->cmdfunc
= doc2001plus_command
;
1516 this->enable_hwecc
= doc2001plus_enable_hwecc
;
1518 doc
->chips_per_floor
= 1;
1519 mtd
->name
= "DiskOnChip Millennium Plus";
1524 static int __init
doc_probe(unsigned long physadr
)
1526 unsigned char ChipID
;
1527 struct mtd_info
*mtd
;
1528 struct nand_chip
*nand
;
1529 struct doc_priv
*doc
;
1530 void __iomem
*virtadr
;
1531 unsigned char save_control
;
1532 unsigned char tmp
, tmpb
, tmpc
;
1533 int reg
, len
, numchips
;
1536 virtadr
= ioremap(physadr
, DOC_IOREMAP_LEN
);
1538 printk(KERN_ERR
"Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n", DOC_IOREMAP_LEN
, physadr
);
1542 /* It's not possible to cleanly detect the DiskOnChip - the
1543 * bootup procedure will put the device into reset mode, and
1544 * it's not possible to talk to it without actually writing
1545 * to the DOCControl register. So we store the current contents
1546 * of the DOCControl register's location, in case we later decide
1547 * that it's not a DiskOnChip, and want to put it back how we
1550 save_control
= ReadDOC(virtadr
, DOCControl
);
1552 /* Reset the DiskOnChip ASIC */
1553 WriteDOC(DOC_MODE_CLR_ERR
| DOC_MODE_MDWREN
| DOC_MODE_RESET
, virtadr
, DOCControl
);
1554 WriteDOC(DOC_MODE_CLR_ERR
| DOC_MODE_MDWREN
| DOC_MODE_RESET
, virtadr
, DOCControl
);
1556 /* Enable the DiskOnChip ASIC */
1557 WriteDOC(DOC_MODE_CLR_ERR
| DOC_MODE_MDWREN
| DOC_MODE_NORMAL
, virtadr
, DOCControl
);
1558 WriteDOC(DOC_MODE_CLR_ERR
| DOC_MODE_MDWREN
| DOC_MODE_NORMAL
, virtadr
, DOCControl
);
1560 ChipID
= ReadDOC(virtadr
, ChipID
);
1563 case DOC_ChipID_Doc2k
:
1564 reg
= DoC_2k_ECCStatus
;
1566 case DOC_ChipID_DocMil
:
1569 case DOC_ChipID_DocMilPlus16
:
1570 case DOC_ChipID_DocMilPlus32
:
1572 /* Possible Millennium Plus, need to do more checks */
1573 /* Possibly release from power down mode */
1574 for (tmp
= 0; (tmp
< 4); tmp
++)
1575 ReadDOC(virtadr
, Mplus_Power
);
1577 /* Reset the Millennium Plus ASIC */
1578 tmp
= DOC_MODE_RESET
| DOC_MODE_MDWREN
| DOC_MODE_RST_LAT
| DOC_MODE_BDECT
;
1579 WriteDOC(tmp
, virtadr
, Mplus_DOCControl
);
1580 WriteDOC(~tmp
, virtadr
, Mplus_CtrlConfirm
);
1583 /* Enable the Millennium Plus ASIC */
1584 tmp
= DOC_MODE_NORMAL
| DOC_MODE_MDWREN
| DOC_MODE_RST_LAT
| DOC_MODE_BDECT
;
1585 WriteDOC(tmp
, virtadr
, Mplus_DOCControl
);
1586 WriteDOC(~tmp
, virtadr
, Mplus_CtrlConfirm
);
1589 ChipID
= ReadDOC(virtadr
, ChipID
);
1592 case DOC_ChipID_DocMilPlus16
:
1593 reg
= DoC_Mplus_Toggle
;
1595 case DOC_ChipID_DocMilPlus32
:
1596 printk(KERN_ERR
"DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
1607 /* Check the TOGGLE bit in the ECC register */
1608 tmp
= ReadDOC_(virtadr
, reg
) & DOC_TOGGLE_BIT
;
1609 tmpb
= ReadDOC_(virtadr
, reg
) & DOC_TOGGLE_BIT
;
1610 tmpc
= ReadDOC_(virtadr
, reg
) & DOC_TOGGLE_BIT
;
1611 if ((tmp
== tmpb
) || (tmp
!= tmpc
)) {
1612 printk(KERN_WARNING
"Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr
);
1617 for (mtd
= doclist
; mtd
; mtd
= doc
->nextdoc
) {
1618 unsigned char oldval
;
1619 unsigned char newval
;
1622 /* Use the alias resolution register to determine if this is
1623 in fact the same DOC aliased to a new address. If writes
1624 to one chip's alias resolution register change the value on
1625 the other chip, they're the same chip. */
1626 if (ChipID
== DOC_ChipID_DocMilPlus16
) {
1627 oldval
= ReadDOC(doc
->virtadr
, Mplus_AliasResolution
);
1628 newval
= ReadDOC(virtadr
, Mplus_AliasResolution
);
1630 oldval
= ReadDOC(doc
->virtadr
, AliasResolution
);
1631 newval
= ReadDOC(virtadr
, AliasResolution
);
1633 if (oldval
!= newval
)
1635 if (ChipID
== DOC_ChipID_DocMilPlus16
) {
1636 WriteDOC(~newval
, virtadr
, Mplus_AliasResolution
);
1637 oldval
= ReadDOC(doc
->virtadr
, Mplus_AliasResolution
);
1638 WriteDOC(newval
, virtadr
, Mplus_AliasResolution
); // restore it
1640 WriteDOC(~newval
, virtadr
, AliasResolution
);
1641 oldval
= ReadDOC(doc
->virtadr
, AliasResolution
);
1642 WriteDOC(newval
, virtadr
, AliasResolution
); // restore it
1645 if (oldval
== newval
) {
1646 printk(KERN_DEBUG
"Found alias of DOC at 0x%lx to 0x%lx\n", doc
->physadr
, physadr
);
1651 printk(KERN_NOTICE
"DiskOnChip found at 0x%lx\n", physadr
);
1653 len
= sizeof(struct mtd_info
) +
1654 sizeof(struct nand_chip
) + sizeof(struct doc_priv
) + (2 * sizeof(struct nand_bbt_descr
));
1655 mtd
= kmalloc(len
, GFP_KERNEL
);
1657 printk(KERN_ERR
"DiskOnChip kmalloc (%d bytes) failed!\n", len
);
1661 memset(mtd
, 0, len
);
1663 nand
= (struct nand_chip
*) (mtd
+ 1);
1664 doc
= (struct doc_priv
*) (nand
+ 1);
1665 nand
->bbt_td
= (struct nand_bbt_descr
*) (doc
+ 1);
1666 nand
->bbt_md
= nand
->bbt_td
+ 1;
1669 mtd
->owner
= THIS_MODULE
;
1672 nand
->select_chip
= doc200x_select_chip
;
1673 nand
->hwcontrol
= doc200x_hwcontrol
;
1674 nand
->dev_ready
= doc200x_dev_ready
;
1675 nand
->waitfunc
= doc200x_wait
;
1676 nand
->block_bad
= doc200x_block_bad
;
1677 nand
->ecc
.hwctl
= doc200x_enable_hwecc
;
1678 nand
->ecc
.calculate
= doc200x_calculate_ecc
;
1679 nand
->ecc
.correct
= doc200x_correct_data
;
1681 nand
->autooob
= &doc200x_oobinfo
;
1682 nand
->ecc
.mode
= NAND_ECC_HW_SYNDROME
;
1683 nand
->ecc
.size
= 512;
1684 nand
->ecc
.bytes
= 6;
1685 nand
->options
= NAND_USE_FLASH_BBT
| NAND_HWECC_SYNDROME
;
1687 doc
->physadr
= physadr
;
1688 doc
->virtadr
= virtadr
;
1689 doc
->ChipID
= ChipID
;
1694 doc
->nextdoc
= doclist
;
1696 if (ChipID
== DOC_ChipID_Doc2k
)
1697 numchips
= doc2000_init(mtd
);
1698 else if (ChipID
== DOC_ChipID_DocMilPlus16
)
1699 numchips
= doc2001plus_init(mtd
);
1701 numchips
= doc2001_init(mtd
);
1703 if ((ret
= nand_scan(mtd
, numchips
))) {
1704 /* DBB note: i believe nand_release is necessary here, as
1705 buffers may have been allocated in nand_base. Check with
1707 /* nand_release will call del_mtd_device, but we haven't yet
1708 added it. This is handled without incident by
1709 del_mtd_device, as far as I can tell. */
1720 /* Put back the contents of the DOCControl register, in case it's not
1721 actually a DiskOnChip. */
1722 WriteDOC(save_control
, virtadr
, DOCControl
);
1728 static void release_nanddoc(void)
1730 struct mtd_info
*mtd
, *nextmtd
;
1731 struct nand_chip
*nand
;
1732 struct doc_priv
*doc
;
1734 for (mtd
= doclist
; mtd
; mtd
= nextmtd
) {
1738 nextmtd
= doc
->nextdoc
;
1740 iounmap(doc
->virtadr
);
1745 static int __init
init_nanddoc(void)
1749 /* We could create the decoder on demand, if memory is a concern.
1750 * This way we have it handy, if an error happens
1752 * Symbolsize is 10 (bits)
1753 * Primitve polynomial is x^10+x^3+1
1754 * first consecutive root is 510
1755 * primitve element to generate roots = 1
1756 * generator polinomial degree = 4
1758 rs_decoder
= init_rs(10, 0x409, FCR
, 1, NROOTS
);
1760 printk(KERN_ERR
"DiskOnChip: Could not create a RS decoder\n");
1764 if (doc_config_location
) {
1765 printk(KERN_INFO
"Using configured DiskOnChip probe address 0x%lx\n", doc_config_location
);
1766 ret
= doc_probe(doc_config_location
);
1770 for (i
= 0; (doc_locations
[i
] != 0xffffffff); i
++) {
1771 doc_probe(doc_locations
[i
]);
1774 /* No banner message any more. Print a message if no DiskOnChip
1775 found, so the user knows we at least tried. */
1777 printk(KERN_INFO
"No valid DiskOnChip devices found\n");
1783 free_rs(rs_decoder
);
1787 static void __exit
cleanup_nanddoc(void)
1789 /* Cleanup the nand/DoC resources */
1792 /* Free the reed solomon resources */
1794 free_rs(rs_decoder
);
1798 module_init(init_nanddoc
);
1799 module_exit(cleanup_nanddoc
);
1801 MODULE_LICENSE("GPL");
1802 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1803 MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver\n");