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
20 #include <linux/kernel.h>
21 #include <linux/init.h>
22 #include <linux/sched.h>
23 #include <linux/delay.h>
24 #include <linux/rslib.h>
25 #include <linux/moduleparam.h>
26 #include <linux/slab.h>
29 #include <linux/mtd/mtd.h>
30 #include <linux/mtd/nand.h>
31 #include <linux/mtd/doc2000.h>
32 #include <linux/mtd/partitions.h>
33 #include <linux/mtd/inftl.h>
34 #include <linux/module.h>
36 /* Where to look for the devices? */
37 #ifndef CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS
38 #define CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS 0
41 static unsigned long doc_locations
[] __initdata
= {
42 #if defined (__alpha__) || defined(__i386__) || defined(__x86_64__)
43 #ifdef CONFIG_MTD_NAND_DISKONCHIP_PROBE_HIGH
44 0xfffc8000, 0xfffca000, 0xfffcc000, 0xfffce000,
45 0xfffd0000, 0xfffd2000, 0xfffd4000, 0xfffd6000,
46 0xfffd8000, 0xfffda000, 0xfffdc000, 0xfffde000,
47 0xfffe0000, 0xfffe2000, 0xfffe4000, 0xfffe6000,
48 0xfffe8000, 0xfffea000, 0xfffec000, 0xfffee000,
50 0xc8000, 0xca000, 0xcc000, 0xce000,
51 0xd0000, 0xd2000, 0xd4000, 0xd6000,
52 0xd8000, 0xda000, 0xdc000, 0xde000,
53 0xe0000, 0xe2000, 0xe4000, 0xe6000,
54 0xe8000, 0xea000, 0xec000, 0xee000,
59 static struct mtd_info
*doclist
= NULL
;
62 void __iomem
*virtadr
;
63 unsigned long physadr
;
66 int chips_per_floor
; /* The number of chips detected on each floor */
71 struct mtd_info
*nextdoc
;
73 /* Handle the last stage of initialization (BBT scan, partitioning) */
74 int (*late_init
)(struct mtd_info
*mtd
);
77 /* This is the syndrome computed by the HW ecc generator upon reading an empty
78 page, one with all 0xff for data and stored ecc code. */
79 static u_char empty_read_syndrome
[6] = { 0x26, 0xff, 0x6d, 0x47, 0x73, 0x7a };
81 /* This is the ecc value computed by the HW ecc generator upon writing an empty
82 page, one with all 0xff for data. */
83 static u_char empty_write_ecc
[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
85 #define INFTL_BBT_RESERVED_BLOCKS 4
87 #define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
88 #define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
89 #define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
91 static void doc200x_hwcontrol(struct mtd_info
*mtd
, int cmd
,
92 unsigned int bitmask
);
93 static void doc200x_select_chip(struct mtd_info
*mtd
, int chip
);
96 module_param(debug
, int, 0);
98 static int try_dword
= 1;
99 module_param(try_dword
, int, 0);
101 static int no_ecc_failures
= 0;
102 module_param(no_ecc_failures
, int, 0);
104 static int no_autopart
= 0;
105 module_param(no_autopart
, int, 0);
107 static int show_firmware_partition
= 0;
108 module_param(show_firmware_partition
, int, 0);
110 #ifdef CONFIG_MTD_NAND_DISKONCHIP_BBTWRITE
111 static int inftl_bbt_write
= 1;
113 static int inftl_bbt_write
= 0;
115 module_param(inftl_bbt_write
, int, 0);
117 static unsigned long doc_config_location
= CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS
;
118 module_param(doc_config_location
, ulong
, 0);
119 MODULE_PARM_DESC(doc_config_location
, "Physical memory address at which to probe for DiskOnChip");
121 /* Sector size for HW ECC */
122 #define SECTOR_SIZE 512
123 /* The sector bytes are packed into NB_DATA 10 bit words */
124 #define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / 10)
125 /* Number of roots */
127 /* First consective root */
129 /* Number of symbols */
132 /* the Reed Solomon control structure */
133 static struct rs_control
*rs_decoder
;
136 * The HW decoder in the DoC ASIC's provides us a error syndrome,
137 * which we must convert to a standard syndrome usable by the generic
138 * Reed-Solomon library code.
140 * Fabrice Bellard figured this out in the old docecc code. I added
141 * some comments, improved a minor bit and converted it to make use
142 * of the generic Reed-Solomon library. tglx
144 static int doc_ecc_decode(struct rs_control
*rs
, uint8_t *data
, uint8_t *ecc
)
146 int i
, j
, nerr
, errpos
[8];
148 uint16_t ds
[4], s
[5], tmp
, errval
[8], syn
[4];
150 memset(syn
, 0, sizeof(syn
));
151 /* Convert the ecc bytes into words */
152 ds
[0] = ((ecc
[4] & 0xff) >> 0) | ((ecc
[5] & 0x03) << 8);
153 ds
[1] = ((ecc
[5] & 0xfc) >> 2) | ((ecc
[2] & 0x0f) << 6);
154 ds
[2] = ((ecc
[2] & 0xf0) >> 4) | ((ecc
[3] & 0x3f) << 4);
155 ds
[3] = ((ecc
[3] & 0xc0) >> 6) | ((ecc
[0] & 0xff) << 2);
158 /* Initialize the syndrome buffer */
159 for (i
= 0; i
< NROOTS
; i
++)
163 * s[i] = ds[3]x^3 + ds[2]x^2 + ds[1]x^1 + ds[0]
164 * where x = alpha^(FCR + i)
166 for (j
= 1; j
< NROOTS
; j
++) {
169 tmp
= rs
->index_of
[ds
[j
]];
170 for (i
= 0; i
< NROOTS
; i
++)
171 s
[i
] ^= rs
->alpha_to
[rs_modnn(rs
, tmp
+ (FCR
+ i
) * j
)];
174 /* Calc syn[i] = s[i] / alpha^(v + i) */
175 for (i
= 0; i
< NROOTS
; i
++) {
177 syn
[i
] = rs_modnn(rs
, rs
->index_of
[s
[i
]] + (NN
- FCR
- i
));
179 /* Call the decoder library */
180 nerr
= decode_rs16(rs
, NULL
, NULL
, 1019, syn
, 0, errpos
, 0, errval
);
182 /* Incorrectable errors ? */
187 * Correct the errors. The bitpositions are a bit of magic,
188 * but they are given by the design of the de/encoder circuit
191 for (i
= 0; i
< nerr
; i
++) {
192 int index
, bitpos
, pos
= 1015 - errpos
[i
];
194 if (pos
>= NB_DATA
&& pos
< 1019)
197 /* extract bit position (MSB first) */
198 pos
= 10 * (NB_DATA
- 1 - pos
) - 6;
199 /* now correct the following 10 bits. At most two bytes
200 can be modified since pos is even */
201 index
= (pos
>> 3) ^ 1;
203 if ((index
>= 0 && index
< SECTOR_SIZE
) || index
== (SECTOR_SIZE
+ 1)) {
204 val
= (uint8_t) (errval
[i
] >> (2 + bitpos
));
206 if (index
< SECTOR_SIZE
)
209 index
= ((pos
>> 3) + 1) ^ 1;
210 bitpos
= (bitpos
+ 10) & 7;
213 if ((index
>= 0 && index
< SECTOR_SIZE
) || index
== (SECTOR_SIZE
+ 1)) {
214 val
= (uint8_t) (errval
[i
] << (8 - bitpos
));
216 if (index
< SECTOR_SIZE
)
221 /* If the parity is wrong, no rescue possible */
222 return parity
? -EBADMSG
: nerr
;
225 static void DoC_Delay(struct doc_priv
*doc
, unsigned short cycles
)
230 for (i
= 0; i
< cycles
; i
++) {
231 if (DoC_is_Millennium(doc
))
232 dummy
= ReadDOC(doc
->virtadr
, NOP
);
233 else if (DoC_is_MillenniumPlus(doc
))
234 dummy
= ReadDOC(doc
->virtadr
, Mplus_NOP
);
236 dummy
= ReadDOC(doc
->virtadr
, DOCStatus
);
241 #define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
243 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
244 static int _DoC_WaitReady(struct doc_priv
*doc
)
246 void __iomem
*docptr
= doc
->virtadr
;
247 unsigned long timeo
= jiffies
+ (HZ
* 10);
250 printk("_DoC_WaitReady...\n");
251 /* Out-of-line routine to wait for chip response */
252 if (DoC_is_MillenniumPlus(doc
)) {
253 while ((ReadDOC(docptr
, Mplus_FlashControl
) & CDSN_CTRL_FR_B_MASK
) != CDSN_CTRL_FR_B_MASK
) {
254 if (time_after(jiffies
, timeo
)) {
255 printk("_DoC_WaitReady timed out.\n");
262 while (!(ReadDOC(docptr
, CDSNControl
) & CDSN_CTRL_FR_B
)) {
263 if (time_after(jiffies
, timeo
)) {
264 printk("_DoC_WaitReady timed out.\n");
275 static inline int DoC_WaitReady(struct doc_priv
*doc
)
277 void __iomem
*docptr
= doc
->virtadr
;
280 if (DoC_is_MillenniumPlus(doc
)) {
283 if ((ReadDOC(docptr
, Mplus_FlashControl
) & CDSN_CTRL_FR_B_MASK
) != CDSN_CTRL_FR_B_MASK
)
284 /* Call the out-of-line routine to wait */
285 ret
= _DoC_WaitReady(doc
);
289 if (!(ReadDOC(docptr
, CDSNControl
) & CDSN_CTRL_FR_B
))
290 /* Call the out-of-line routine to wait */
291 ret
= _DoC_WaitReady(doc
);
296 printk("DoC_WaitReady OK\n");
300 static void doc2000_write_byte(struct mtd_info
*mtd
, u_char datum
)
302 struct nand_chip
*this = mtd
->priv
;
303 struct doc_priv
*doc
= this->priv
;
304 void __iomem
*docptr
= doc
->virtadr
;
307 printk("write_byte %02x\n", datum
);
308 WriteDOC(datum
, docptr
, CDSNSlowIO
);
309 WriteDOC(datum
, docptr
, 2k_CDSN_IO
);
312 static u_char
doc2000_read_byte(struct mtd_info
*mtd
)
314 struct nand_chip
*this = mtd
->priv
;
315 struct doc_priv
*doc
= this->priv
;
316 void __iomem
*docptr
= doc
->virtadr
;
319 ReadDOC(docptr
, CDSNSlowIO
);
321 ret
= ReadDOC(docptr
, 2k_CDSN_IO
);
323 printk("read_byte returns %02x\n", ret
);
327 static void doc2000_writebuf(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
329 struct nand_chip
*this = mtd
->priv
;
330 struct doc_priv
*doc
= this->priv
;
331 void __iomem
*docptr
= doc
->virtadr
;
334 printk("writebuf of %d bytes: ", len
);
335 for (i
= 0; i
< len
; i
++) {
336 WriteDOC_(buf
[i
], docptr
, DoC_2k_CDSN_IO
+ i
);
338 printk("%02x ", buf
[i
]);
344 static void doc2000_readbuf(struct mtd_info
*mtd
, u_char
*buf
, int len
)
346 struct nand_chip
*this = mtd
->priv
;
347 struct doc_priv
*doc
= this->priv
;
348 void __iomem
*docptr
= doc
->virtadr
;
352 printk("readbuf of %d bytes: ", len
);
354 for (i
= 0; i
< len
; i
++) {
355 buf
[i
] = ReadDOC(docptr
, 2k_CDSN_IO
+ i
);
359 static void doc2000_readbuf_dword(struct mtd_info
*mtd
, u_char
*buf
, int len
)
361 struct nand_chip
*this = mtd
->priv
;
362 struct doc_priv
*doc
= this->priv
;
363 void __iomem
*docptr
= doc
->virtadr
;
367 printk("readbuf_dword of %d bytes: ", len
);
369 if (unlikely((((unsigned long)buf
) | len
) & 3)) {
370 for (i
= 0; i
< len
; i
++) {
371 *(uint8_t *) (&buf
[i
]) = ReadDOC(docptr
, 2k_CDSN_IO
+ i
);
374 for (i
= 0; i
< len
; i
+= 4) {
375 *(uint32_t *) (&buf
[i
]) = readl(docptr
+ DoC_2k_CDSN_IO
+ i
);
380 static uint16_t __init
doc200x_ident_chip(struct mtd_info
*mtd
, int nr
)
382 struct nand_chip
*this = mtd
->priv
;
383 struct doc_priv
*doc
= this->priv
;
386 doc200x_select_chip(mtd
, nr
);
387 doc200x_hwcontrol(mtd
, NAND_CMD_READID
,
388 NAND_CTRL_CLE
| NAND_CTRL_CHANGE
);
389 doc200x_hwcontrol(mtd
, 0, NAND_CTRL_ALE
| NAND_CTRL_CHANGE
);
390 doc200x_hwcontrol(mtd
, NAND_CMD_NONE
, NAND_NCE
| NAND_CTRL_CHANGE
);
392 /* We can't use dev_ready here, but at least we wait for the
393 * command to complete
397 ret
= this->read_byte(mtd
) << 8;
398 ret
|= this->read_byte(mtd
);
400 if (doc
->ChipID
== DOC_ChipID_Doc2k
&& try_dword
&& !nr
) {
401 /* First chip probe. See if we get same results by 32-bit access */
406 void __iomem
*docptr
= doc
->virtadr
;
408 doc200x_hwcontrol(mtd
, NAND_CMD_READID
,
409 NAND_CTRL_CLE
| NAND_CTRL_CHANGE
);
410 doc200x_hwcontrol(mtd
, 0, NAND_CTRL_ALE
| NAND_CTRL_CHANGE
);
411 doc200x_hwcontrol(mtd
, NAND_CMD_NONE
,
412 NAND_NCE
| NAND_CTRL_CHANGE
);
416 ident
.dword
= readl(docptr
+ DoC_2k_CDSN_IO
);
417 if (((ident
.byte
[0] << 8) | ident
.byte
[1]) == ret
) {
418 printk(KERN_INFO
"DiskOnChip 2000 responds to DWORD access\n");
419 this->read_buf
= &doc2000_readbuf_dword
;
426 static void __init
doc2000_count_chips(struct mtd_info
*mtd
)
428 struct nand_chip
*this = mtd
->priv
;
429 struct doc_priv
*doc
= this->priv
;
433 /* Max 4 chips per floor on DiskOnChip 2000 */
434 doc
->chips_per_floor
= 4;
436 /* Find out what the first chip is */
437 mfrid
= doc200x_ident_chip(mtd
, 0);
439 /* Find how many chips in each floor. */
440 for (i
= 1; i
< 4; i
++) {
441 if (doc200x_ident_chip(mtd
, i
) != mfrid
)
444 doc
->chips_per_floor
= i
;
445 printk(KERN_DEBUG
"Detected %d chips per floor.\n", i
);
448 static int doc200x_wait(struct mtd_info
*mtd
, struct nand_chip
*this)
450 struct doc_priv
*doc
= this->priv
;
455 this->cmdfunc(mtd
, NAND_CMD_STATUS
, -1, -1);
457 status
= (int)this->read_byte(mtd
);
462 static void doc2001_write_byte(struct mtd_info
*mtd
, u_char datum
)
464 struct nand_chip
*this = mtd
->priv
;
465 struct doc_priv
*doc
= this->priv
;
466 void __iomem
*docptr
= doc
->virtadr
;
468 WriteDOC(datum
, docptr
, CDSNSlowIO
);
469 WriteDOC(datum
, docptr
, Mil_CDSN_IO
);
470 WriteDOC(datum
, docptr
, WritePipeTerm
);
473 static u_char
doc2001_read_byte(struct mtd_info
*mtd
)
475 struct nand_chip
*this = mtd
->priv
;
476 struct doc_priv
*doc
= this->priv
;
477 void __iomem
*docptr
= doc
->virtadr
;
479 //ReadDOC(docptr, CDSNSlowIO);
480 /* 11.4.5 -- delay twice to allow extended length cycle */
482 ReadDOC(docptr
, ReadPipeInit
);
483 //return ReadDOC(docptr, Mil_CDSN_IO);
484 return ReadDOC(docptr
, LastDataRead
);
487 static void doc2001_writebuf(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
489 struct nand_chip
*this = mtd
->priv
;
490 struct doc_priv
*doc
= this->priv
;
491 void __iomem
*docptr
= doc
->virtadr
;
494 for (i
= 0; i
< len
; i
++)
495 WriteDOC_(buf
[i
], docptr
, DoC_Mil_CDSN_IO
+ i
);
496 /* Terminate write pipeline */
497 WriteDOC(0x00, docptr
, WritePipeTerm
);
500 static void doc2001_readbuf(struct mtd_info
*mtd
, u_char
*buf
, int len
)
502 struct nand_chip
*this = mtd
->priv
;
503 struct doc_priv
*doc
= this->priv
;
504 void __iomem
*docptr
= doc
->virtadr
;
507 /* Start read pipeline */
508 ReadDOC(docptr
, ReadPipeInit
);
510 for (i
= 0; i
< len
- 1; i
++)
511 buf
[i
] = ReadDOC(docptr
, Mil_CDSN_IO
+ (i
& 0xff));
513 /* Terminate read pipeline */
514 buf
[i
] = ReadDOC(docptr
, LastDataRead
);
517 static u_char
doc2001plus_read_byte(struct mtd_info
*mtd
)
519 struct nand_chip
*this = mtd
->priv
;
520 struct doc_priv
*doc
= this->priv
;
521 void __iomem
*docptr
= doc
->virtadr
;
524 ReadDOC(docptr
, Mplus_ReadPipeInit
);
525 ReadDOC(docptr
, Mplus_ReadPipeInit
);
526 ret
= ReadDOC(docptr
, Mplus_LastDataRead
);
528 printk("read_byte returns %02x\n", ret
);
532 static void doc2001plus_writebuf(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
534 struct nand_chip
*this = mtd
->priv
;
535 struct doc_priv
*doc
= this->priv
;
536 void __iomem
*docptr
= doc
->virtadr
;
540 printk("writebuf of %d bytes: ", len
);
541 for (i
= 0; i
< len
; i
++) {
542 WriteDOC_(buf
[i
], docptr
, DoC_Mil_CDSN_IO
+ i
);
544 printk("%02x ", buf
[i
]);
550 static void doc2001plus_readbuf(struct mtd_info
*mtd
, u_char
*buf
, int len
)
552 struct nand_chip
*this = mtd
->priv
;
553 struct doc_priv
*doc
= this->priv
;
554 void __iomem
*docptr
= doc
->virtadr
;
558 printk("readbuf of %d bytes: ", len
);
560 /* Start read pipeline */
561 ReadDOC(docptr
, Mplus_ReadPipeInit
);
562 ReadDOC(docptr
, Mplus_ReadPipeInit
);
564 for (i
= 0; i
< len
- 2; i
++) {
565 buf
[i
] = ReadDOC(docptr
, Mil_CDSN_IO
);
567 printk("%02x ", buf
[i
]);
570 /* Terminate read pipeline */
571 buf
[len
- 2] = ReadDOC(docptr
, Mplus_LastDataRead
);
573 printk("%02x ", buf
[len
- 2]);
574 buf
[len
- 1] = ReadDOC(docptr
, Mplus_LastDataRead
);
576 printk("%02x ", buf
[len
- 1]);
581 static void doc2001plus_select_chip(struct mtd_info
*mtd
, int chip
)
583 struct nand_chip
*this = mtd
->priv
;
584 struct doc_priv
*doc
= this->priv
;
585 void __iomem
*docptr
= doc
->virtadr
;
589 printk("select chip (%d)\n", chip
);
592 /* Disable flash internally */
593 WriteDOC(0, docptr
, Mplus_FlashSelect
);
597 floor
= chip
/ doc
->chips_per_floor
;
598 chip
-= (floor
* doc
->chips_per_floor
);
600 /* Assert ChipEnable and deassert WriteProtect */
601 WriteDOC((DOC_FLASH_CE
), docptr
, Mplus_FlashSelect
);
602 this->cmdfunc(mtd
, NAND_CMD_RESET
, -1, -1);
605 doc
->curfloor
= floor
;
608 static void doc200x_select_chip(struct mtd_info
*mtd
, int chip
)
610 struct nand_chip
*this = mtd
->priv
;
611 struct doc_priv
*doc
= this->priv
;
612 void __iomem
*docptr
= doc
->virtadr
;
616 printk("select chip (%d)\n", chip
);
621 floor
= chip
/ doc
->chips_per_floor
;
622 chip
-= (floor
* doc
->chips_per_floor
);
624 /* 11.4.4 -- deassert CE before changing chip */
625 doc200x_hwcontrol(mtd
, NAND_CMD_NONE
, 0 | NAND_CTRL_CHANGE
);
627 WriteDOC(floor
, docptr
, FloorSelect
);
628 WriteDOC(chip
, docptr
, CDSNDeviceSelect
);
630 doc200x_hwcontrol(mtd
, NAND_CMD_NONE
, NAND_NCE
| NAND_CTRL_CHANGE
);
633 doc
->curfloor
= floor
;
636 #define CDSN_CTRL_MSK (CDSN_CTRL_CE | CDSN_CTRL_CLE | CDSN_CTRL_ALE)
638 static void doc200x_hwcontrol(struct mtd_info
*mtd
, int cmd
,
641 struct nand_chip
*this = mtd
->priv
;
642 struct doc_priv
*doc
= this->priv
;
643 void __iomem
*docptr
= doc
->virtadr
;
645 if (ctrl
& NAND_CTRL_CHANGE
) {
646 doc
->CDSNControl
&= ~CDSN_CTRL_MSK
;
647 doc
->CDSNControl
|= ctrl
& CDSN_CTRL_MSK
;
649 printk("hwcontrol(%d): %02x\n", cmd
, doc
->CDSNControl
);
650 WriteDOC(doc
->CDSNControl
, docptr
, CDSNControl
);
651 /* 11.4.3 -- 4 NOPs after CSDNControl write */
654 if (cmd
!= NAND_CMD_NONE
) {
655 if (DoC_is_2000(doc
))
656 doc2000_write_byte(mtd
, cmd
);
658 doc2001_write_byte(mtd
, cmd
);
662 static void doc2001plus_command(struct mtd_info
*mtd
, unsigned command
, int column
, int page_addr
)
664 struct nand_chip
*this = mtd
->priv
;
665 struct doc_priv
*doc
= this->priv
;
666 void __iomem
*docptr
= doc
->virtadr
;
669 * Must terminate write pipeline before sending any commands
672 if (command
== NAND_CMD_PAGEPROG
) {
673 WriteDOC(0x00, docptr
, Mplus_WritePipeTerm
);
674 WriteDOC(0x00, docptr
, Mplus_WritePipeTerm
);
678 * Write out the command to the device.
680 if (command
== NAND_CMD_SEQIN
) {
683 if (column
>= mtd
->writesize
) {
685 column
-= mtd
->writesize
;
686 readcmd
= NAND_CMD_READOOB
;
687 } else if (column
< 256) {
688 /* First 256 bytes --> READ0 */
689 readcmd
= NAND_CMD_READ0
;
692 readcmd
= NAND_CMD_READ1
;
694 WriteDOC(readcmd
, docptr
, Mplus_FlashCmd
);
696 WriteDOC(command
, docptr
, Mplus_FlashCmd
);
697 WriteDOC(0, docptr
, Mplus_WritePipeTerm
);
698 WriteDOC(0, docptr
, Mplus_WritePipeTerm
);
700 if (column
!= -1 || page_addr
!= -1) {
701 /* Serially input address */
703 /* Adjust columns for 16 bit buswidth */
704 if (this->options
& NAND_BUSWIDTH_16
&&
705 !nand_opcode_8bits(command
))
707 WriteDOC(column
, docptr
, Mplus_FlashAddress
);
709 if (page_addr
!= -1) {
710 WriteDOC((unsigned char)(page_addr
& 0xff), docptr
, Mplus_FlashAddress
);
711 WriteDOC((unsigned char)((page_addr
>> 8) & 0xff), docptr
, Mplus_FlashAddress
);
712 /* One more address cycle for higher density devices */
713 if (this->chipsize
& 0x0c000000) {
714 WriteDOC((unsigned char)((page_addr
>> 16) & 0x0f), docptr
, Mplus_FlashAddress
);
715 printk("high density\n");
718 WriteDOC(0, docptr
, Mplus_WritePipeTerm
);
719 WriteDOC(0, docptr
, Mplus_WritePipeTerm
);
721 if (command
== NAND_CMD_READ0
|| command
== NAND_CMD_READ1
||
722 command
== NAND_CMD_READOOB
|| command
== NAND_CMD_READID
)
723 WriteDOC(0, docptr
, Mplus_FlashControl
);
727 * program and erase have their own busy handlers
728 * status and sequential in needs no delay
732 case NAND_CMD_PAGEPROG
:
733 case NAND_CMD_ERASE1
:
734 case NAND_CMD_ERASE2
:
736 case NAND_CMD_STATUS
:
742 udelay(this->chip_delay
);
743 WriteDOC(NAND_CMD_STATUS
, docptr
, Mplus_FlashCmd
);
744 WriteDOC(0, docptr
, Mplus_WritePipeTerm
);
745 WriteDOC(0, docptr
, Mplus_WritePipeTerm
);
746 while (!(this->read_byte(mtd
) & 0x40)) ;
749 /* This applies to read commands */
752 * If we don't have access to the busy pin, we apply the given
755 if (!this->dev_ready
) {
756 udelay(this->chip_delay
);
761 /* Apply this short delay always to ensure that we do wait tWB in
762 * any case on any machine. */
764 /* wait until command is processed */
765 while (!this->dev_ready(mtd
)) ;
768 static int doc200x_dev_ready(struct mtd_info
*mtd
)
770 struct nand_chip
*this = mtd
->priv
;
771 struct doc_priv
*doc
= this->priv
;
772 void __iomem
*docptr
= doc
->virtadr
;
774 if (DoC_is_MillenniumPlus(doc
)) {
775 /* 11.4.2 -- must NOP four times before checking FR/B# */
777 if ((ReadDOC(docptr
, Mplus_FlashControl
) & CDSN_CTRL_FR_B_MASK
) != CDSN_CTRL_FR_B_MASK
) {
779 printk("not ready\n");
783 printk("was ready\n");
786 /* 11.4.2 -- must NOP four times before checking FR/B# */
788 if (!(ReadDOC(docptr
, CDSNControl
) & CDSN_CTRL_FR_B
)) {
790 printk("not ready\n");
793 /* 11.4.2 -- Must NOP twice if it's ready */
796 printk("was ready\n");
801 static int doc200x_block_bad(struct mtd_info
*mtd
, loff_t ofs
, int getchip
)
803 /* This is our last resort if we couldn't find or create a BBT. Just
804 pretend all blocks are good. */
808 static void doc200x_enable_hwecc(struct mtd_info
*mtd
, int mode
)
810 struct nand_chip
*this = mtd
->priv
;
811 struct doc_priv
*doc
= this->priv
;
812 void __iomem
*docptr
= doc
->virtadr
;
814 /* Prime the ECC engine */
817 WriteDOC(DOC_ECC_RESET
, docptr
, ECCConf
);
818 WriteDOC(DOC_ECC_EN
, docptr
, ECCConf
);
821 WriteDOC(DOC_ECC_RESET
, docptr
, ECCConf
);
822 WriteDOC(DOC_ECC_EN
| DOC_ECC_RW
, docptr
, ECCConf
);
827 static void doc2001plus_enable_hwecc(struct mtd_info
*mtd
, int mode
)
829 struct nand_chip
*this = mtd
->priv
;
830 struct doc_priv
*doc
= this->priv
;
831 void __iomem
*docptr
= doc
->virtadr
;
833 /* Prime the ECC engine */
836 WriteDOC(DOC_ECC_RESET
, docptr
, Mplus_ECCConf
);
837 WriteDOC(DOC_ECC_EN
, docptr
, Mplus_ECCConf
);
840 WriteDOC(DOC_ECC_RESET
, docptr
, Mplus_ECCConf
);
841 WriteDOC(DOC_ECC_EN
| DOC_ECC_RW
, docptr
, Mplus_ECCConf
);
846 /* This code is only called on write */
847 static int doc200x_calculate_ecc(struct mtd_info
*mtd
, const u_char
*dat
, unsigned char *ecc_code
)
849 struct nand_chip
*this = mtd
->priv
;
850 struct doc_priv
*doc
= this->priv
;
851 void __iomem
*docptr
= doc
->virtadr
;
855 /* flush the pipeline */
856 if (DoC_is_2000(doc
)) {
857 WriteDOC(doc
->CDSNControl
& ~CDSN_CTRL_FLASH_IO
, docptr
, CDSNControl
);
858 WriteDOC(0, docptr
, 2k_CDSN_IO
);
859 WriteDOC(0, docptr
, 2k_CDSN_IO
);
860 WriteDOC(0, docptr
, 2k_CDSN_IO
);
861 WriteDOC(doc
->CDSNControl
, docptr
, CDSNControl
);
862 } else if (DoC_is_MillenniumPlus(doc
)) {
863 WriteDOC(0, docptr
, Mplus_NOP
);
864 WriteDOC(0, docptr
, Mplus_NOP
);
865 WriteDOC(0, docptr
, Mplus_NOP
);
867 WriteDOC(0, docptr
, NOP
);
868 WriteDOC(0, docptr
, NOP
);
869 WriteDOC(0, docptr
, NOP
);
872 for (i
= 0; i
< 6; i
++) {
873 if (DoC_is_MillenniumPlus(doc
))
874 ecc_code
[i
] = ReadDOC_(docptr
, DoC_Mplus_ECCSyndrome0
+ i
);
876 ecc_code
[i
] = ReadDOC_(docptr
, DoC_ECCSyndrome0
+ i
);
877 if (ecc_code
[i
] != empty_write_ecc
[i
])
880 if (DoC_is_MillenniumPlus(doc
))
881 WriteDOC(DOC_ECC_DIS
, docptr
, Mplus_ECCConf
);
883 WriteDOC(DOC_ECC_DIS
, docptr
, ECCConf
);
885 /* If emptymatch=1, we might have an all-0xff data buffer. Check. */
887 /* Note: this somewhat expensive test should not be triggered
888 often. It could be optimized away by examining the data in
889 the writebuf routine, and remembering the result. */
890 for (i
= 0; i
< 512; i
++) {
897 /* If emptymatch still =1, we do have an all-0xff data buffer.
898 Return all-0xff ecc value instead of the computed one, so
899 it'll look just like a freshly-erased page. */
901 memset(ecc_code
, 0xff, 6);
906 static int doc200x_correct_data(struct mtd_info
*mtd
, u_char
*dat
,
907 u_char
*read_ecc
, u_char
*isnull
)
910 struct nand_chip
*this = mtd
->priv
;
911 struct doc_priv
*doc
= this->priv
;
912 void __iomem
*docptr
= doc
->virtadr
;
914 volatile u_char dummy
;
917 /* flush the pipeline */
918 if (DoC_is_2000(doc
)) {
919 dummy
= ReadDOC(docptr
, 2k_ECCStatus
);
920 dummy
= ReadDOC(docptr
, 2k_ECCStatus
);
921 dummy
= ReadDOC(docptr
, 2k_ECCStatus
);
922 } else if (DoC_is_MillenniumPlus(doc
)) {
923 dummy
= ReadDOC(docptr
, Mplus_ECCConf
);
924 dummy
= ReadDOC(docptr
, Mplus_ECCConf
);
925 dummy
= ReadDOC(docptr
, Mplus_ECCConf
);
927 dummy
= ReadDOC(docptr
, ECCConf
);
928 dummy
= ReadDOC(docptr
, ECCConf
);
929 dummy
= ReadDOC(docptr
, ECCConf
);
932 /* Error occurred ? */
934 for (i
= 0; i
< 6; i
++) {
935 if (DoC_is_MillenniumPlus(doc
))
936 calc_ecc
[i
] = ReadDOC_(docptr
, DoC_Mplus_ECCSyndrome0
+ i
);
938 calc_ecc
[i
] = ReadDOC_(docptr
, DoC_ECCSyndrome0
+ i
);
939 if (calc_ecc
[i
] != empty_read_syndrome
[i
])
942 /* If emptymatch=1, the read syndrome is consistent with an
943 all-0xff data and stored ecc block. Check the stored ecc. */
945 for (i
= 0; i
< 6; i
++) {
946 if (read_ecc
[i
] == 0xff)
952 /* If emptymatch still =1, check the data block. */
954 /* Note: this somewhat expensive test should not be triggered
955 often. It could be optimized away by examining the data in
956 the readbuf routine, and remembering the result. */
957 for (i
= 0; i
< 512; i
++) {
964 /* If emptymatch still =1, this is almost certainly a freshly-
965 erased block, in which case the ECC will not come out right.
966 We'll suppress the error and tell the caller everything's
967 OK. Because it is. */
969 ret
= doc_ecc_decode(rs_decoder
, dat
, calc_ecc
);
971 printk(KERN_ERR
"doc200x_correct_data corrected %d errors\n", ret
);
973 if (DoC_is_MillenniumPlus(doc
))
974 WriteDOC(DOC_ECC_DIS
, docptr
, Mplus_ECCConf
);
976 WriteDOC(DOC_ECC_DIS
, docptr
, ECCConf
);
977 if (no_ecc_failures
&& mtd_is_eccerr(ret
)) {
978 printk(KERN_ERR
"suppressing ECC failure\n");
984 //u_char mydatabuf[528];
986 /* The strange out-of-order .oobfree list below is a (possibly unneeded)
987 * attempt to retain compatibility. It used to read:
988 * .oobfree = { {8, 8} }
989 * Since that leaves two bytes unusable, it was changed. But the following
990 * scheme might affect existing jffs2 installs by moving the cleanmarker:
991 * .oobfree = { {6, 10} }
992 * jffs2 seems to handle the above gracefully, but the current scheme seems
993 * safer. The only problem with it is that any code that parses oobfree must
994 * be able to handle out-of-order segments.
996 static struct nand_ecclayout doc200x_oobinfo
= {
998 .eccpos
= {0, 1, 2, 3, 4, 5},
999 .oobfree
= {{8, 8}, {6, 2}}
1002 /* Find the (I)NFTL Media Header, and optionally also the mirror media header.
1003 On successful return, buf will contain a copy of the media header for
1004 further processing. id is the string to scan for, and will presumably be
1005 either "ANAND" or "BNAND". If findmirror=1, also look for the mirror media
1006 header. The page #s of the found media headers are placed in mh0_page and
1007 mh1_page in the DOC private structure. */
1008 static int __init
find_media_headers(struct mtd_info
*mtd
, u_char
*buf
, const char *id
, int findmirror
)
1010 struct nand_chip
*this = mtd
->priv
;
1011 struct doc_priv
*doc
= this->priv
;
1016 for (offs
= 0; offs
< mtd
->size
; offs
+= mtd
->erasesize
) {
1017 ret
= mtd_read(mtd
, offs
, mtd
->writesize
, &retlen
, buf
);
1018 if (retlen
!= mtd
->writesize
)
1021 printk(KERN_WARNING
"ECC error scanning DOC at 0x%x\n", offs
);
1023 if (memcmp(buf
, id
, 6))
1025 printk(KERN_INFO
"Found DiskOnChip %s Media Header at 0x%x\n", id
, offs
);
1026 if (doc
->mh0_page
== -1) {
1027 doc
->mh0_page
= offs
>> this->page_shift
;
1032 doc
->mh1_page
= offs
>> this->page_shift
;
1035 if (doc
->mh0_page
== -1) {
1036 printk(KERN_WARNING
"DiskOnChip %s Media Header not found.\n", id
);
1039 /* Only one mediaheader was found. We want buf to contain a
1040 mediaheader on return, so we'll have to re-read the one we found. */
1041 offs
= doc
->mh0_page
<< this->page_shift
;
1042 ret
= mtd_read(mtd
, offs
, mtd
->writesize
, &retlen
, buf
);
1043 if (retlen
!= mtd
->writesize
) {
1044 /* Insanity. Give up. */
1045 printk(KERN_ERR
"Read DiskOnChip Media Header once, but can't reread it???\n");
1051 static inline int __init
nftl_partscan(struct mtd_info
*mtd
, struct mtd_partition
*parts
)
1053 struct nand_chip
*this = mtd
->priv
;
1054 struct doc_priv
*doc
= this->priv
;
1057 struct NFTLMediaHeader
*mh
;
1058 const unsigned psize
= 1 << this->page_shift
;
1060 unsigned blocks
, maxblocks
;
1061 int offs
, numheaders
;
1063 buf
= kmalloc(mtd
->writesize
, GFP_KERNEL
);
1067 if (!(numheaders
= find_media_headers(mtd
, buf
, "ANAND", 1)))
1069 mh
= (struct NFTLMediaHeader
*)buf
;
1071 le16_to_cpus(&mh
->NumEraseUnits
);
1072 le16_to_cpus(&mh
->FirstPhysicalEUN
);
1073 le32_to_cpus(&mh
->FormattedSize
);
1075 printk(KERN_INFO
" DataOrgID = %s\n"
1076 " NumEraseUnits = %d\n"
1077 " FirstPhysicalEUN = %d\n"
1078 " FormattedSize = %d\n"
1079 " UnitSizeFactor = %d\n",
1080 mh
->DataOrgID
, mh
->NumEraseUnits
,
1081 mh
->FirstPhysicalEUN
, mh
->FormattedSize
,
1082 mh
->UnitSizeFactor
);
1084 blocks
= mtd
->size
>> this->phys_erase_shift
;
1085 maxblocks
= min(32768U, mtd
->erasesize
- psize
);
1087 if (mh
->UnitSizeFactor
== 0x00) {
1088 /* Auto-determine UnitSizeFactor. The constraints are:
1089 - There can be at most 32768 virtual blocks.
1090 - There can be at most (virtual block size - page size)
1091 virtual blocks (because MediaHeader+BBT must fit in 1).
1093 mh
->UnitSizeFactor
= 0xff;
1094 while (blocks
> maxblocks
) {
1096 maxblocks
= min(32768U, (maxblocks
<< 1) + psize
);
1097 mh
->UnitSizeFactor
--;
1099 printk(KERN_WARNING
"UnitSizeFactor=0x00 detected. Correct value is assumed to be 0x%02x.\n", mh
->UnitSizeFactor
);
1102 /* NOTE: The lines below modify internal variables of the NAND and MTD
1103 layers; variables with have already been configured by nand_scan.
1104 Unfortunately, we didn't know before this point what these values
1105 should be. Thus, this code is somewhat dependent on the exact
1106 implementation of the NAND layer. */
1107 if (mh
->UnitSizeFactor
!= 0xff) {
1108 this->bbt_erase_shift
+= (0xff - mh
->UnitSizeFactor
);
1109 mtd
->erasesize
<<= (0xff - mh
->UnitSizeFactor
);
1110 printk(KERN_INFO
"Setting virtual erase size to %d\n", mtd
->erasesize
);
1111 blocks
= mtd
->size
>> this->bbt_erase_shift
;
1112 maxblocks
= min(32768U, mtd
->erasesize
- psize
);
1115 if (blocks
> maxblocks
) {
1116 printk(KERN_ERR
"UnitSizeFactor of 0x%02x is inconsistent with device size. Aborting.\n", mh
->UnitSizeFactor
);
1120 /* Skip past the media headers. */
1121 offs
= max(doc
->mh0_page
, doc
->mh1_page
);
1122 offs
<<= this->page_shift
;
1123 offs
+= mtd
->erasesize
;
1125 if (show_firmware_partition
== 1) {
1126 parts
[0].name
= " DiskOnChip Firmware / Media Header partition";
1127 parts
[0].offset
= 0;
1128 parts
[0].size
= offs
;
1132 parts
[numparts
].name
= " DiskOnChip BDTL partition";
1133 parts
[numparts
].offset
= offs
;
1134 parts
[numparts
].size
= (mh
->NumEraseUnits
- numheaders
) << this->bbt_erase_shift
;
1136 offs
+= parts
[numparts
].size
;
1139 if (offs
< mtd
->size
) {
1140 parts
[numparts
].name
= " DiskOnChip Remainder partition";
1141 parts
[numparts
].offset
= offs
;
1142 parts
[numparts
].size
= mtd
->size
- offs
;
1152 /* This is a stripped-down copy of the code in inftlmount.c */
1153 static inline int __init
inftl_partscan(struct mtd_info
*mtd
, struct mtd_partition
*parts
)
1155 struct nand_chip
*this = mtd
->priv
;
1156 struct doc_priv
*doc
= this->priv
;
1159 struct INFTLMediaHeader
*mh
;
1160 struct INFTLPartition
*ip
;
1163 int vshift
, lastvunit
= 0;
1165 int end
= mtd
->size
;
1167 if (inftl_bbt_write
)
1168 end
-= (INFTL_BBT_RESERVED_BLOCKS
<< this->phys_erase_shift
);
1170 buf
= kmalloc(mtd
->writesize
, GFP_KERNEL
);
1175 if (!find_media_headers(mtd
, buf
, "BNAND", 0))
1177 doc
->mh1_page
= doc
->mh0_page
+ (4096 >> this->page_shift
);
1178 mh
= (struct INFTLMediaHeader
*)buf
;
1180 le32_to_cpus(&mh
->NoOfBootImageBlocks
);
1181 le32_to_cpus(&mh
->NoOfBinaryPartitions
);
1182 le32_to_cpus(&mh
->NoOfBDTLPartitions
);
1183 le32_to_cpus(&mh
->BlockMultiplierBits
);
1184 le32_to_cpus(&mh
->FormatFlags
);
1185 le32_to_cpus(&mh
->PercentUsed
);
1187 printk(KERN_INFO
" bootRecordID = %s\n"
1188 " NoOfBootImageBlocks = %d\n"
1189 " NoOfBinaryPartitions = %d\n"
1190 " NoOfBDTLPartitions = %d\n"
1191 " BlockMultiplerBits = %d\n"
1192 " FormatFlgs = %d\n"
1193 " OsakVersion = %d.%d.%d.%d\n"
1194 " PercentUsed = %d\n",
1195 mh
->bootRecordID
, mh
->NoOfBootImageBlocks
,
1196 mh
->NoOfBinaryPartitions
,
1197 mh
->NoOfBDTLPartitions
,
1198 mh
->BlockMultiplierBits
, mh
->FormatFlags
,
1199 ((unsigned char *) &mh
->OsakVersion
)[0] & 0xf,
1200 ((unsigned char *) &mh
->OsakVersion
)[1] & 0xf,
1201 ((unsigned char *) &mh
->OsakVersion
)[2] & 0xf,
1202 ((unsigned char *) &mh
->OsakVersion
)[3] & 0xf,
1205 vshift
= this->phys_erase_shift
+ mh
->BlockMultiplierBits
;
1207 blocks
= mtd
->size
>> vshift
;
1208 if (blocks
> 32768) {
1209 printk(KERN_ERR
"BlockMultiplierBits=%d is inconsistent with device size. Aborting.\n", mh
->BlockMultiplierBits
);
1213 blocks
= doc
->chips_per_floor
<< (this->chip_shift
- this->phys_erase_shift
);
1214 if (inftl_bbt_write
&& (blocks
> mtd
->erasesize
)) {
1215 printk(KERN_ERR
"Writeable BBTs spanning more than one erase block are not yet supported. FIX ME!\n");
1219 /* Scan the partitions */
1220 for (i
= 0; (i
< 4); i
++) {
1221 ip
= &(mh
->Partitions
[i
]);
1222 le32_to_cpus(&ip
->virtualUnits
);
1223 le32_to_cpus(&ip
->firstUnit
);
1224 le32_to_cpus(&ip
->lastUnit
);
1225 le32_to_cpus(&ip
->flags
);
1226 le32_to_cpus(&ip
->spareUnits
);
1227 le32_to_cpus(&ip
->Reserved0
);
1229 printk(KERN_INFO
" PARTITION[%d] ->\n"
1230 " virtualUnits = %d\n"
1234 " spareUnits = %d\n",
1235 i
, ip
->virtualUnits
, ip
->firstUnit
,
1236 ip
->lastUnit
, ip
->flags
,
1239 if ((show_firmware_partition
== 1) &&
1240 (i
== 0) && (ip
->firstUnit
> 0)) {
1241 parts
[0].name
= " DiskOnChip IPL / Media Header partition";
1242 parts
[0].offset
= 0;
1243 parts
[0].size
= mtd
->erasesize
* ip
->firstUnit
;
1247 if (ip
->flags
& INFTL_BINARY
)
1248 parts
[numparts
].name
= " DiskOnChip BDK partition";
1250 parts
[numparts
].name
= " DiskOnChip BDTL partition";
1251 parts
[numparts
].offset
= ip
->firstUnit
<< vshift
;
1252 parts
[numparts
].size
= (1 + ip
->lastUnit
- ip
->firstUnit
) << vshift
;
1254 if (ip
->lastUnit
> lastvunit
)
1255 lastvunit
= ip
->lastUnit
;
1256 if (ip
->flags
& INFTL_LAST
)
1260 if ((lastvunit
<< vshift
) < end
) {
1261 parts
[numparts
].name
= " DiskOnChip Remainder partition";
1262 parts
[numparts
].offset
= lastvunit
<< vshift
;
1263 parts
[numparts
].size
= end
- parts
[numparts
].offset
;
1272 static int __init
nftl_scan_bbt(struct mtd_info
*mtd
)
1275 struct nand_chip
*this = mtd
->priv
;
1276 struct doc_priv
*doc
= this->priv
;
1277 struct mtd_partition parts
[2];
1279 memset((char *)parts
, 0, sizeof(parts
));
1280 /* On NFTL, we have to find the media headers before we can read the
1281 BBTs, since they're stored in the media header eraseblocks. */
1282 numparts
= nftl_partscan(mtd
, parts
);
1285 this->bbt_td
->options
= NAND_BBT_ABSPAGE
| NAND_BBT_8BIT
|
1286 NAND_BBT_SAVECONTENT
| NAND_BBT_WRITE
|
1288 this->bbt_td
->veroffs
= 7;
1289 this->bbt_td
->pages
[0] = doc
->mh0_page
+ 1;
1290 if (doc
->mh1_page
!= -1) {
1291 this->bbt_md
->options
= NAND_BBT_ABSPAGE
| NAND_BBT_8BIT
|
1292 NAND_BBT_SAVECONTENT
| NAND_BBT_WRITE
|
1294 this->bbt_md
->veroffs
= 7;
1295 this->bbt_md
->pages
[0] = doc
->mh1_page
+ 1;
1297 this->bbt_md
= NULL
;
1300 ret
= this->scan_bbt(mtd
);
1304 return mtd_device_register(mtd
, parts
, no_autopart
? 0 : numparts
);
1307 static int __init
inftl_scan_bbt(struct mtd_info
*mtd
)
1310 struct nand_chip
*this = mtd
->priv
;
1311 struct doc_priv
*doc
= this->priv
;
1312 struct mtd_partition parts
[5];
1314 if (this->numchips
> doc
->chips_per_floor
) {
1315 printk(KERN_ERR
"Multi-floor INFTL devices not yet supported.\n");
1319 if (DoC_is_MillenniumPlus(doc
)) {
1320 this->bbt_td
->options
= NAND_BBT_2BIT
| NAND_BBT_ABSPAGE
;
1321 if (inftl_bbt_write
)
1322 this->bbt_td
->options
|= NAND_BBT_WRITE
;
1323 this->bbt_td
->pages
[0] = 2;
1324 this->bbt_md
= NULL
;
1326 this->bbt_td
->options
= NAND_BBT_LASTBLOCK
| NAND_BBT_8BIT
| NAND_BBT_VERSION
;
1327 if (inftl_bbt_write
)
1328 this->bbt_td
->options
|= NAND_BBT_WRITE
;
1329 this->bbt_td
->offs
= 8;
1330 this->bbt_td
->len
= 8;
1331 this->bbt_td
->veroffs
= 7;
1332 this->bbt_td
->maxblocks
= INFTL_BBT_RESERVED_BLOCKS
;
1333 this->bbt_td
->reserved_block_code
= 0x01;
1334 this->bbt_td
->pattern
= "MSYS_BBT";
1336 this->bbt_md
->options
= NAND_BBT_LASTBLOCK
| NAND_BBT_8BIT
| NAND_BBT_VERSION
;
1337 if (inftl_bbt_write
)
1338 this->bbt_md
->options
|= NAND_BBT_WRITE
;
1339 this->bbt_md
->offs
= 8;
1340 this->bbt_md
->len
= 8;
1341 this->bbt_md
->veroffs
= 7;
1342 this->bbt_md
->maxblocks
= INFTL_BBT_RESERVED_BLOCKS
;
1343 this->bbt_md
->reserved_block_code
= 0x01;
1344 this->bbt_md
->pattern
= "TBB_SYSM";
1347 ret
= this->scan_bbt(mtd
);
1351 memset((char *)parts
, 0, sizeof(parts
));
1352 numparts
= inftl_partscan(mtd
, parts
);
1353 /* At least for now, require the INFTL Media Header. We could probably
1354 do without it for non-INFTL use, since all it gives us is
1355 autopartitioning, but I want to give it more thought. */
1358 return mtd_device_register(mtd
, parts
, no_autopart
? 0 : numparts
);
1361 static inline int __init
doc2000_init(struct mtd_info
*mtd
)
1363 struct nand_chip
*this = mtd
->priv
;
1364 struct doc_priv
*doc
= this->priv
;
1366 this->read_byte
= doc2000_read_byte
;
1367 this->write_buf
= doc2000_writebuf
;
1368 this->read_buf
= doc2000_readbuf
;
1369 doc
->late_init
= nftl_scan_bbt
;
1371 doc
->CDSNControl
= CDSN_CTRL_FLASH_IO
| CDSN_CTRL_ECC_IO
;
1372 doc2000_count_chips(mtd
);
1373 mtd
->name
= "DiskOnChip 2000 (NFTL Model)";
1374 return (4 * doc
->chips_per_floor
);
1377 static inline int __init
doc2001_init(struct mtd_info
*mtd
)
1379 struct nand_chip
*this = mtd
->priv
;
1380 struct doc_priv
*doc
= this->priv
;
1382 this->read_byte
= doc2001_read_byte
;
1383 this->write_buf
= doc2001_writebuf
;
1384 this->read_buf
= doc2001_readbuf
;
1386 ReadDOC(doc
->virtadr
, ChipID
);
1387 ReadDOC(doc
->virtadr
, ChipID
);
1388 ReadDOC(doc
->virtadr
, ChipID
);
1389 if (ReadDOC(doc
->virtadr
, ChipID
) != DOC_ChipID_DocMil
) {
1390 /* It's not a Millennium; it's one of the newer
1391 DiskOnChip 2000 units with a similar ASIC.
1392 Treat it like a Millennium, except that it
1393 can have multiple chips. */
1394 doc2000_count_chips(mtd
);
1395 mtd
->name
= "DiskOnChip 2000 (INFTL Model)";
1396 doc
->late_init
= inftl_scan_bbt
;
1397 return (4 * doc
->chips_per_floor
);
1399 /* Bog-standard Millennium */
1400 doc
->chips_per_floor
= 1;
1401 mtd
->name
= "DiskOnChip Millennium";
1402 doc
->late_init
= nftl_scan_bbt
;
1407 static inline int __init
doc2001plus_init(struct mtd_info
*mtd
)
1409 struct nand_chip
*this = mtd
->priv
;
1410 struct doc_priv
*doc
= this->priv
;
1412 this->read_byte
= doc2001plus_read_byte
;
1413 this->write_buf
= doc2001plus_writebuf
;
1414 this->read_buf
= doc2001plus_readbuf
;
1415 doc
->late_init
= inftl_scan_bbt
;
1416 this->cmd_ctrl
= NULL
;
1417 this->select_chip
= doc2001plus_select_chip
;
1418 this->cmdfunc
= doc2001plus_command
;
1419 this->ecc
.hwctl
= doc2001plus_enable_hwecc
;
1421 doc
->chips_per_floor
= 1;
1422 mtd
->name
= "DiskOnChip Millennium Plus";
1427 static int __init
doc_probe(unsigned long physadr
)
1429 unsigned char ChipID
;
1430 struct mtd_info
*mtd
;
1431 struct nand_chip
*nand
;
1432 struct doc_priv
*doc
;
1433 void __iomem
*virtadr
;
1434 unsigned char save_control
;
1435 unsigned char tmp
, tmpb
, tmpc
;
1436 int reg
, len
, numchips
;
1439 if (!request_mem_region(physadr
, DOC_IOREMAP_LEN
, "DiskOnChip"))
1441 virtadr
= ioremap(physadr
, DOC_IOREMAP_LEN
);
1443 printk(KERN_ERR
"Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n", DOC_IOREMAP_LEN
, physadr
);
1448 /* It's not possible to cleanly detect the DiskOnChip - the
1449 * bootup procedure will put the device into reset mode, and
1450 * it's not possible to talk to it without actually writing
1451 * to the DOCControl register. So we store the current contents
1452 * of the DOCControl register's location, in case we later decide
1453 * that it's not a DiskOnChip, and want to put it back how we
1456 save_control
= ReadDOC(virtadr
, DOCControl
);
1458 /* Reset the DiskOnChip ASIC */
1459 WriteDOC(DOC_MODE_CLR_ERR
| DOC_MODE_MDWREN
| DOC_MODE_RESET
, virtadr
, DOCControl
);
1460 WriteDOC(DOC_MODE_CLR_ERR
| DOC_MODE_MDWREN
| DOC_MODE_RESET
, virtadr
, DOCControl
);
1462 /* Enable the DiskOnChip ASIC */
1463 WriteDOC(DOC_MODE_CLR_ERR
| DOC_MODE_MDWREN
| DOC_MODE_NORMAL
, virtadr
, DOCControl
);
1464 WriteDOC(DOC_MODE_CLR_ERR
| DOC_MODE_MDWREN
| DOC_MODE_NORMAL
, virtadr
, DOCControl
);
1466 ChipID
= ReadDOC(virtadr
, ChipID
);
1469 case DOC_ChipID_Doc2k
:
1470 reg
= DoC_2k_ECCStatus
;
1472 case DOC_ChipID_DocMil
:
1475 case DOC_ChipID_DocMilPlus16
:
1476 case DOC_ChipID_DocMilPlus32
:
1478 /* Possible Millennium Plus, need to do more checks */
1479 /* Possibly release from power down mode */
1480 for (tmp
= 0; (tmp
< 4); tmp
++)
1481 ReadDOC(virtadr
, Mplus_Power
);
1483 /* Reset the Millennium Plus ASIC */
1484 tmp
= DOC_MODE_RESET
| DOC_MODE_MDWREN
| DOC_MODE_RST_LAT
| DOC_MODE_BDECT
;
1485 WriteDOC(tmp
, virtadr
, Mplus_DOCControl
);
1486 WriteDOC(~tmp
, virtadr
, Mplus_CtrlConfirm
);
1489 /* Enable the Millennium Plus ASIC */
1490 tmp
= DOC_MODE_NORMAL
| DOC_MODE_MDWREN
| DOC_MODE_RST_LAT
| DOC_MODE_BDECT
;
1491 WriteDOC(tmp
, virtadr
, Mplus_DOCControl
);
1492 WriteDOC(~tmp
, virtadr
, Mplus_CtrlConfirm
);
1495 ChipID
= ReadDOC(virtadr
, ChipID
);
1498 case DOC_ChipID_DocMilPlus16
:
1499 reg
= DoC_Mplus_Toggle
;
1501 case DOC_ChipID_DocMilPlus32
:
1502 printk(KERN_ERR
"DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
1513 /* Check the TOGGLE bit in the ECC register */
1514 tmp
= ReadDOC_(virtadr
, reg
) & DOC_TOGGLE_BIT
;
1515 tmpb
= ReadDOC_(virtadr
, reg
) & DOC_TOGGLE_BIT
;
1516 tmpc
= ReadDOC_(virtadr
, reg
) & DOC_TOGGLE_BIT
;
1517 if ((tmp
== tmpb
) || (tmp
!= tmpc
)) {
1518 printk(KERN_WARNING
"Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr
);
1523 for (mtd
= doclist
; mtd
; mtd
= doc
->nextdoc
) {
1524 unsigned char oldval
;
1525 unsigned char newval
;
1528 /* Use the alias resolution register to determine if this is
1529 in fact the same DOC aliased to a new address. If writes
1530 to one chip's alias resolution register change the value on
1531 the other chip, they're the same chip. */
1532 if (ChipID
== DOC_ChipID_DocMilPlus16
) {
1533 oldval
= ReadDOC(doc
->virtadr
, Mplus_AliasResolution
);
1534 newval
= ReadDOC(virtadr
, Mplus_AliasResolution
);
1536 oldval
= ReadDOC(doc
->virtadr
, AliasResolution
);
1537 newval
= ReadDOC(virtadr
, AliasResolution
);
1539 if (oldval
!= newval
)
1541 if (ChipID
== DOC_ChipID_DocMilPlus16
) {
1542 WriteDOC(~newval
, virtadr
, Mplus_AliasResolution
);
1543 oldval
= ReadDOC(doc
->virtadr
, Mplus_AliasResolution
);
1544 WriteDOC(newval
, virtadr
, Mplus_AliasResolution
); // restore it
1546 WriteDOC(~newval
, virtadr
, AliasResolution
);
1547 oldval
= ReadDOC(doc
->virtadr
, AliasResolution
);
1548 WriteDOC(newval
, virtadr
, AliasResolution
); // restore it
1551 if (oldval
== newval
) {
1552 printk(KERN_DEBUG
"Found alias of DOC at 0x%lx to 0x%lx\n", doc
->physadr
, physadr
);
1557 printk(KERN_NOTICE
"DiskOnChip found at 0x%lx\n", physadr
);
1559 len
= sizeof(struct mtd_info
) +
1560 sizeof(struct nand_chip
) + sizeof(struct doc_priv
) + (2 * sizeof(struct nand_bbt_descr
));
1561 mtd
= kzalloc(len
, GFP_KERNEL
);
1567 nand
= (struct nand_chip
*) (mtd
+ 1);
1568 doc
= (struct doc_priv
*) (nand
+ 1);
1569 nand
->bbt_td
= (struct nand_bbt_descr
*) (doc
+ 1);
1570 nand
->bbt_md
= nand
->bbt_td
+ 1;
1573 mtd
->owner
= THIS_MODULE
;
1576 nand
->select_chip
= doc200x_select_chip
;
1577 nand
->cmd_ctrl
= doc200x_hwcontrol
;
1578 nand
->dev_ready
= doc200x_dev_ready
;
1579 nand
->waitfunc
= doc200x_wait
;
1580 nand
->block_bad
= doc200x_block_bad
;
1581 nand
->ecc
.hwctl
= doc200x_enable_hwecc
;
1582 nand
->ecc
.calculate
= doc200x_calculate_ecc
;
1583 nand
->ecc
.correct
= doc200x_correct_data
;
1585 nand
->ecc
.layout
= &doc200x_oobinfo
;
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
->bbt_options
= NAND_BBT_USE_FLASH
;
1591 /* Skip the automatic BBT scan so we can run it manually */
1592 nand
->options
|= NAND_SKIP_BBTSCAN
;
1594 doc
->physadr
= physadr
;
1595 doc
->virtadr
= virtadr
;
1596 doc
->ChipID
= ChipID
;
1601 doc
->nextdoc
= doclist
;
1603 if (ChipID
== DOC_ChipID_Doc2k
)
1604 numchips
= doc2000_init(mtd
);
1605 else if (ChipID
== DOC_ChipID_DocMilPlus16
)
1606 numchips
= doc2001plus_init(mtd
);
1608 numchips
= doc2001_init(mtd
);
1610 if ((ret
= nand_scan(mtd
, numchips
)) || (ret
= doc
->late_init(mtd
))) {
1611 /* DBB note: i believe nand_release is necessary here, as
1612 buffers may have been allocated in nand_base. Check with
1614 /* nand_release will call mtd_device_unregister, but we
1615 haven't yet added it. This is handled without incident by
1616 mtd_device_unregister, as far as I can tell. */
1627 /* Put back the contents of the DOCControl register, in case it's not
1628 actually a DiskOnChip. */
1629 WriteDOC(save_control
, virtadr
, DOCControl
);
1634 release_mem_region(physadr
, DOC_IOREMAP_LEN
);
1639 static void release_nanddoc(void)
1641 struct mtd_info
*mtd
, *nextmtd
;
1642 struct nand_chip
*nand
;
1643 struct doc_priv
*doc
;
1645 for (mtd
= doclist
; mtd
; mtd
= nextmtd
) {
1649 nextmtd
= doc
->nextdoc
;
1651 iounmap(doc
->virtadr
);
1652 release_mem_region(doc
->physadr
, DOC_IOREMAP_LEN
);
1657 static int __init
init_nanddoc(void)
1661 /* We could create the decoder on demand, if memory is a concern.
1662 * This way we have it handy, if an error happens
1664 * Symbolsize is 10 (bits)
1665 * Primitve polynomial is x^10+x^3+1
1666 * first consecutive root is 510
1667 * primitve element to generate roots = 1
1668 * generator polinomial degree = 4
1670 rs_decoder
= init_rs(10, 0x409, FCR
, 1, NROOTS
);
1672 printk(KERN_ERR
"DiskOnChip: Could not create a RS decoder\n");
1676 if (doc_config_location
) {
1677 printk(KERN_INFO
"Using configured DiskOnChip probe address 0x%lx\n", doc_config_location
);
1678 ret
= doc_probe(doc_config_location
);
1682 for (i
= 0; (doc_locations
[i
] != 0xffffffff); i
++) {
1683 doc_probe(doc_locations
[i
]);
1686 /* No banner message any more. Print a message if no DiskOnChip
1687 found, so the user knows we at least tried. */
1689 printk(KERN_INFO
"No valid DiskOnChip devices found\n");
1695 free_rs(rs_decoder
);
1699 static void __exit
cleanup_nanddoc(void)
1701 /* Cleanup the nand/DoC resources */
1704 /* Free the reed solomon resources */
1706 free_rs(rs_decoder
);
1710 module_init(init_nanddoc
);
1711 module_exit(cleanup_nanddoc
);
1713 MODULE_LICENSE("GPL");
1714 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1715 MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver");