1 // SPDX-License-Identifier: GPL-2.0-only
3 * (C) 2003 Red Hat, Inc.
4 * (C) 2004 Dan Brown <dan_brown@ieee.org>
5 * (C) 2004 Kalev Lember <kalev@smartlink.ee>
7 * Author: David Woodhouse <dwmw2@infradead.org>
8 * Additional Diskonchip 2000 and Millennium support by Dan Brown <dan_brown@ieee.org>
9 * Diskonchip Millennium Plus support by Kalev Lember <kalev@smartlink.ee>
11 * Error correction code lifted from the old docecc code
12 * Author: Fabrice Bellard (fabrice.bellard@netgem.com)
13 * Copyright (C) 2000 Netgem S.A.
14 * converted to the generic Reed-Solomon library by Thomas Gleixner <tglx@linutronix.de>
16 * Interface to generic NAND code for M-Systems DiskOnChip devices
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/sched.h>
22 #include <linux/delay.h>
23 #include <linux/rslib.h>
24 #include <linux/moduleparam.h>
25 #include <linux/slab.h>
28 #include <linux/mtd/mtd.h>
29 #include <linux/mtd/rawnand.h>
30 #include <linux/mtd/doc2000.h>
31 #include <linux/mtd/partitions.h>
32 #include <linux/mtd/inftl.h>
33 #include <linux/module.h>
35 /* Where to look for the devices? */
36 #ifndef CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS
37 #define CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS 0
40 static unsigned long doc_locations
[] __initdata
= {
41 #if defined (__alpha__) || defined(__i386__) || defined(__x86_64__)
42 #ifdef CONFIG_MTD_NAND_DISKONCHIP_PROBE_HIGH
43 0xfffc8000, 0xfffca000, 0xfffcc000, 0xfffce000,
44 0xfffd0000, 0xfffd2000, 0xfffd4000, 0xfffd6000,
45 0xfffd8000, 0xfffda000, 0xfffdc000, 0xfffde000,
46 0xfffe0000, 0xfffe2000, 0xfffe4000, 0xfffe6000,
47 0xfffe8000, 0xfffea000, 0xfffec000, 0xfffee000,
49 0xc8000, 0xca000, 0xcc000, 0xce000,
50 0xd0000, 0xd2000, 0xd4000, 0xd6000,
51 0xd8000, 0xda000, 0xdc000, 0xde000,
52 0xe0000, 0xe2000, 0xe4000, 0xe6000,
53 0xe8000, 0xea000, 0xec000, 0xee000,
58 static struct mtd_info
*doclist
= NULL
;
61 void __iomem
*virtadr
;
62 unsigned long physadr
;
65 int chips_per_floor
; /* The number of chips detected on each floor */
70 struct rs_control
*rs_decoder
;
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 ecc value computed by the HW ecc generator upon writing an empty
78 page, one with all 0xff for data. */
79 static u_char empty_write_ecc
[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
81 #define INFTL_BBT_RESERVED_BLOCKS 4
83 #define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
84 #define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
85 #define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
87 static void doc200x_hwcontrol(struct nand_chip
*this, int cmd
,
88 unsigned int bitmask
);
89 static void doc200x_select_chip(struct nand_chip
*this, int chip
);
92 module_param(debug
, int, 0);
94 static int try_dword
= 1;
95 module_param(try_dword
, int, 0);
97 static int no_ecc_failures
= 0;
98 module_param(no_ecc_failures
, int, 0);
100 static int no_autopart
= 0;
101 module_param(no_autopart
, int, 0);
103 static int show_firmware_partition
= 0;
104 module_param(show_firmware_partition
, int, 0);
106 #ifdef CONFIG_MTD_NAND_DISKONCHIP_BBTWRITE
107 static int inftl_bbt_write
= 1;
109 static int inftl_bbt_write
= 0;
111 module_param(inftl_bbt_write
, int, 0);
113 static unsigned long doc_config_location
= CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS
;
114 module_param(doc_config_location
, ulong
, 0);
115 MODULE_PARM_DESC(doc_config_location
, "Physical memory address at which to probe for DiskOnChip");
117 /* Sector size for HW ECC */
118 #define SECTOR_SIZE 512
119 /* The sector bytes are packed into NB_DATA 10 bit words */
120 #define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / 10)
121 /* Number of roots */
123 /* First consective root */
125 /* Number of symbols */
129 * The HW decoder in the DoC ASIC's provides us a error syndrome,
130 * which we must convert to a standard syndrome usable by the generic
131 * Reed-Solomon library code.
133 * Fabrice Bellard figured this out in the old docecc code. I added
134 * some comments, improved a minor bit and converted it to make use
135 * of the generic Reed-Solomon library. tglx
137 static int doc_ecc_decode(struct rs_control
*rs
, uint8_t *data
, uint8_t *ecc
)
139 int i
, j
, nerr
, errpos
[8];
141 uint16_t ds
[4], s
[5], tmp
, errval
[8], syn
[4];
142 struct rs_codec
*cd
= rs
->codec
;
144 memset(syn
, 0, sizeof(syn
));
145 /* Convert the ecc bytes into words */
146 ds
[0] = ((ecc
[4] & 0xff) >> 0) | ((ecc
[5] & 0x03) << 8);
147 ds
[1] = ((ecc
[5] & 0xfc) >> 2) | ((ecc
[2] & 0x0f) << 6);
148 ds
[2] = ((ecc
[2] & 0xf0) >> 4) | ((ecc
[3] & 0x3f) << 4);
149 ds
[3] = ((ecc
[3] & 0xc0) >> 6) | ((ecc
[0] & 0xff) << 2);
152 /* Initialize the syndrome buffer */
153 for (i
= 0; i
< NROOTS
; i
++)
157 * s[i] = ds[3]x^3 + ds[2]x^2 + ds[1]x^1 + ds[0]
158 * where x = alpha^(FCR + i)
160 for (j
= 1; j
< NROOTS
; j
++) {
163 tmp
= cd
->index_of
[ds
[j
]];
164 for (i
= 0; i
< NROOTS
; i
++)
165 s
[i
] ^= cd
->alpha_to
[rs_modnn(cd
, tmp
+ (FCR
+ i
) * j
)];
168 /* Calc syn[i] = s[i] / alpha^(v + i) */
169 for (i
= 0; i
< NROOTS
; i
++) {
171 syn
[i
] = rs_modnn(cd
, cd
->index_of
[s
[i
]] + (NN
- FCR
- i
));
173 /* Call the decoder library */
174 nerr
= decode_rs16(rs
, NULL
, NULL
, 1019, syn
, 0, errpos
, 0, errval
);
176 /* Incorrectable errors ? */
181 * Correct the errors. The bitpositions are a bit of magic,
182 * but they are given by the design of the de/encoder circuit
185 for (i
= 0; i
< nerr
; i
++) {
186 int index
, bitpos
, pos
= 1015 - errpos
[i
];
188 if (pos
>= NB_DATA
&& pos
< 1019)
191 /* extract bit position (MSB first) */
192 pos
= 10 * (NB_DATA
- 1 - pos
) - 6;
193 /* now correct the following 10 bits. At most two bytes
194 can be modified since pos is even */
195 index
= (pos
>> 3) ^ 1;
197 if ((index
>= 0 && index
< SECTOR_SIZE
) || index
== (SECTOR_SIZE
+ 1)) {
198 val
= (uint8_t) (errval
[i
] >> (2 + bitpos
));
200 if (index
< SECTOR_SIZE
)
203 index
= ((pos
>> 3) + 1) ^ 1;
204 bitpos
= (bitpos
+ 10) & 7;
207 if ((index
>= 0 && index
< SECTOR_SIZE
) || index
== (SECTOR_SIZE
+ 1)) {
208 val
= (uint8_t) (errval
[i
] << (8 - bitpos
));
210 if (index
< SECTOR_SIZE
)
215 /* If the parity is wrong, no rescue possible */
216 return parity
? -EBADMSG
: nerr
;
219 static void DoC_Delay(struct doc_priv
*doc
, unsigned short cycles
)
224 for (i
= 0; i
< cycles
; i
++) {
225 if (DoC_is_Millennium(doc
))
226 dummy
= ReadDOC(doc
->virtadr
, NOP
);
227 else if (DoC_is_MillenniumPlus(doc
))
228 dummy
= ReadDOC(doc
->virtadr
, Mplus_NOP
);
230 dummy
= ReadDOC(doc
->virtadr
, DOCStatus
);
235 #define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
237 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
238 static int _DoC_WaitReady(struct doc_priv
*doc
)
240 void __iomem
*docptr
= doc
->virtadr
;
241 unsigned long timeo
= jiffies
+ (HZ
* 10);
244 printk("_DoC_WaitReady...\n");
245 /* Out-of-line routine to wait for chip response */
246 if (DoC_is_MillenniumPlus(doc
)) {
247 while ((ReadDOC(docptr
, Mplus_FlashControl
) & CDSN_CTRL_FR_B_MASK
) != CDSN_CTRL_FR_B_MASK
) {
248 if (time_after(jiffies
, timeo
)) {
249 printk("_DoC_WaitReady timed out.\n");
256 while (!(ReadDOC(docptr
, CDSNControl
) & CDSN_CTRL_FR_B
)) {
257 if (time_after(jiffies
, timeo
)) {
258 printk("_DoC_WaitReady timed out.\n");
269 static inline int DoC_WaitReady(struct doc_priv
*doc
)
271 void __iomem
*docptr
= doc
->virtadr
;
274 if (DoC_is_MillenniumPlus(doc
)) {
277 if ((ReadDOC(docptr
, Mplus_FlashControl
) & CDSN_CTRL_FR_B_MASK
) != CDSN_CTRL_FR_B_MASK
)
278 /* Call the out-of-line routine to wait */
279 ret
= _DoC_WaitReady(doc
);
283 if (!(ReadDOC(docptr
, CDSNControl
) & CDSN_CTRL_FR_B
))
284 /* Call the out-of-line routine to wait */
285 ret
= _DoC_WaitReady(doc
);
290 printk("DoC_WaitReady OK\n");
294 static void doc2000_write_byte(struct nand_chip
*this, u_char datum
)
296 struct doc_priv
*doc
= nand_get_controller_data(this);
297 void __iomem
*docptr
= doc
->virtadr
;
300 printk("write_byte %02x\n", datum
);
301 WriteDOC(datum
, docptr
, CDSNSlowIO
);
302 WriteDOC(datum
, docptr
, 2k_CDSN_IO
);
305 static u_char
doc2000_read_byte(struct nand_chip
*this)
307 struct doc_priv
*doc
= nand_get_controller_data(this);
308 void __iomem
*docptr
= doc
->virtadr
;
311 ReadDOC(docptr
, CDSNSlowIO
);
313 ret
= ReadDOC(docptr
, 2k_CDSN_IO
);
315 printk("read_byte returns %02x\n", ret
);
319 static void doc2000_writebuf(struct nand_chip
*this, const u_char
*buf
,
322 struct doc_priv
*doc
= nand_get_controller_data(this);
323 void __iomem
*docptr
= doc
->virtadr
;
326 printk("writebuf of %d bytes: ", len
);
327 for (i
= 0; i
< len
; i
++) {
328 WriteDOC_(buf
[i
], docptr
, DoC_2k_CDSN_IO
+ i
);
330 printk("%02x ", buf
[i
]);
336 static void doc2000_readbuf(struct nand_chip
*this, u_char
*buf
, int len
)
338 struct doc_priv
*doc
= nand_get_controller_data(this);
339 void __iomem
*docptr
= doc
->virtadr
;
343 printk("readbuf of %d bytes: ", len
);
345 for (i
= 0; i
< len
; i
++)
346 buf
[i
] = ReadDOC(docptr
, 2k_CDSN_IO
+ i
);
349 static void doc2000_readbuf_dword(struct nand_chip
*this, u_char
*buf
, int len
)
351 struct doc_priv
*doc
= nand_get_controller_data(this);
352 void __iomem
*docptr
= doc
->virtadr
;
356 printk("readbuf_dword of %d bytes: ", len
);
358 if (unlikely((((unsigned long)buf
) | len
) & 3)) {
359 for (i
= 0; i
< len
; i
++) {
360 *(uint8_t *) (&buf
[i
]) = ReadDOC(docptr
, 2k_CDSN_IO
+ i
);
363 for (i
= 0; i
< len
; i
+= 4) {
364 *(uint32_t *) (&buf
[i
]) = readl(docptr
+ DoC_2k_CDSN_IO
+ i
);
369 static uint16_t __init
doc200x_ident_chip(struct mtd_info
*mtd
, int nr
)
371 struct nand_chip
*this = mtd_to_nand(mtd
);
372 struct doc_priv
*doc
= nand_get_controller_data(this);
375 doc200x_select_chip(this, nr
);
376 doc200x_hwcontrol(this, NAND_CMD_READID
,
377 NAND_CTRL_CLE
| NAND_CTRL_CHANGE
);
378 doc200x_hwcontrol(this, 0, NAND_CTRL_ALE
| NAND_CTRL_CHANGE
);
379 doc200x_hwcontrol(this, NAND_CMD_NONE
, NAND_NCE
| NAND_CTRL_CHANGE
);
381 /* We can't use dev_ready here, but at least we wait for the
382 * command to complete
386 ret
= this->legacy
.read_byte(this) << 8;
387 ret
|= this->legacy
.read_byte(this);
389 if (doc
->ChipID
== DOC_ChipID_Doc2k
&& try_dword
&& !nr
) {
390 /* First chip probe. See if we get same results by 32-bit access */
395 void __iomem
*docptr
= doc
->virtadr
;
397 doc200x_hwcontrol(this, NAND_CMD_READID
,
398 NAND_CTRL_CLE
| NAND_CTRL_CHANGE
);
399 doc200x_hwcontrol(this, 0, NAND_CTRL_ALE
| NAND_CTRL_CHANGE
);
400 doc200x_hwcontrol(this, NAND_CMD_NONE
,
401 NAND_NCE
| NAND_CTRL_CHANGE
);
405 ident
.dword
= readl(docptr
+ DoC_2k_CDSN_IO
);
406 if (((ident
.byte
[0] << 8) | ident
.byte
[1]) == ret
) {
407 pr_info("DiskOnChip 2000 responds to DWORD access\n");
408 this->legacy
.read_buf
= &doc2000_readbuf_dword
;
415 static void __init
doc2000_count_chips(struct mtd_info
*mtd
)
417 struct nand_chip
*this = mtd_to_nand(mtd
);
418 struct doc_priv
*doc
= nand_get_controller_data(this);
422 /* Max 4 chips per floor on DiskOnChip 2000 */
423 doc
->chips_per_floor
= 4;
425 /* Find out what the first chip is */
426 mfrid
= doc200x_ident_chip(mtd
, 0);
428 /* Find how many chips in each floor. */
429 for (i
= 1; i
< 4; i
++) {
430 if (doc200x_ident_chip(mtd
, i
) != mfrid
)
433 doc
->chips_per_floor
= i
;
434 pr_debug("Detected %d chips per floor.\n", i
);
437 static int doc200x_wait(struct nand_chip
*this)
439 struct doc_priv
*doc
= nand_get_controller_data(this);
444 nand_status_op(this, NULL
);
446 status
= (int)this->legacy
.read_byte(this);
451 static void doc2001_write_byte(struct nand_chip
*this, u_char datum
)
453 struct doc_priv
*doc
= nand_get_controller_data(this);
454 void __iomem
*docptr
= doc
->virtadr
;
456 WriteDOC(datum
, docptr
, CDSNSlowIO
);
457 WriteDOC(datum
, docptr
, Mil_CDSN_IO
);
458 WriteDOC(datum
, docptr
, WritePipeTerm
);
461 static u_char
doc2001_read_byte(struct nand_chip
*this)
463 struct doc_priv
*doc
= nand_get_controller_data(this);
464 void __iomem
*docptr
= doc
->virtadr
;
466 //ReadDOC(docptr, CDSNSlowIO);
467 /* 11.4.5 -- delay twice to allow extended length cycle */
469 ReadDOC(docptr
, ReadPipeInit
);
470 //return ReadDOC(docptr, Mil_CDSN_IO);
471 return ReadDOC(docptr
, LastDataRead
);
474 static void doc2001_writebuf(struct nand_chip
*this, const u_char
*buf
, int len
)
476 struct doc_priv
*doc
= nand_get_controller_data(this);
477 void __iomem
*docptr
= doc
->virtadr
;
480 for (i
= 0; i
< len
; i
++)
481 WriteDOC_(buf
[i
], docptr
, DoC_Mil_CDSN_IO
+ i
);
482 /* Terminate write pipeline */
483 WriteDOC(0x00, docptr
, WritePipeTerm
);
486 static void doc2001_readbuf(struct nand_chip
*this, u_char
*buf
, int len
)
488 struct doc_priv
*doc
= nand_get_controller_data(this);
489 void __iomem
*docptr
= doc
->virtadr
;
492 /* Start read pipeline */
493 ReadDOC(docptr
, ReadPipeInit
);
495 for (i
= 0; i
< len
- 1; i
++)
496 buf
[i
] = ReadDOC(docptr
, Mil_CDSN_IO
+ (i
& 0xff));
498 /* Terminate read pipeline */
499 buf
[i
] = ReadDOC(docptr
, LastDataRead
);
502 static u_char
doc2001plus_read_byte(struct nand_chip
*this)
504 struct doc_priv
*doc
= nand_get_controller_data(this);
505 void __iomem
*docptr
= doc
->virtadr
;
508 ReadDOC(docptr
, Mplus_ReadPipeInit
);
509 ReadDOC(docptr
, Mplus_ReadPipeInit
);
510 ret
= ReadDOC(docptr
, Mplus_LastDataRead
);
512 printk("read_byte returns %02x\n", ret
);
516 static void doc2001plus_writebuf(struct nand_chip
*this, const u_char
*buf
, int len
)
518 struct doc_priv
*doc
= nand_get_controller_data(this);
519 void __iomem
*docptr
= doc
->virtadr
;
523 printk("writebuf of %d bytes: ", len
);
524 for (i
= 0; i
< len
; i
++) {
525 WriteDOC_(buf
[i
], docptr
, DoC_Mil_CDSN_IO
+ i
);
527 printk("%02x ", buf
[i
]);
533 static void doc2001plus_readbuf(struct nand_chip
*this, u_char
*buf
, int len
)
535 struct doc_priv
*doc
= nand_get_controller_data(this);
536 void __iomem
*docptr
= doc
->virtadr
;
540 printk("readbuf of %d bytes: ", len
);
542 /* Start read pipeline */
543 ReadDOC(docptr
, Mplus_ReadPipeInit
);
544 ReadDOC(docptr
, Mplus_ReadPipeInit
);
546 for (i
= 0; i
< len
- 2; i
++) {
547 buf
[i
] = ReadDOC(docptr
, Mil_CDSN_IO
);
549 printk("%02x ", buf
[i
]);
552 /* Terminate read pipeline */
553 buf
[len
- 2] = ReadDOC(docptr
, Mplus_LastDataRead
);
555 printk("%02x ", buf
[len
- 2]);
556 buf
[len
- 1] = ReadDOC(docptr
, Mplus_LastDataRead
);
558 printk("%02x ", buf
[len
- 1]);
563 static void doc2001plus_select_chip(struct nand_chip
*this, int chip
)
565 struct doc_priv
*doc
= nand_get_controller_data(this);
566 void __iomem
*docptr
= doc
->virtadr
;
570 printk("select chip (%d)\n", chip
);
573 /* Disable flash internally */
574 WriteDOC(0, docptr
, Mplus_FlashSelect
);
578 floor
= chip
/ doc
->chips_per_floor
;
579 chip
-= (floor
* doc
->chips_per_floor
);
581 /* Assert ChipEnable and deassert WriteProtect */
582 WriteDOC((DOC_FLASH_CE
), docptr
, Mplus_FlashSelect
);
586 doc
->curfloor
= floor
;
589 static void doc200x_select_chip(struct nand_chip
*this, int chip
)
591 struct doc_priv
*doc
= nand_get_controller_data(this);
592 void __iomem
*docptr
= doc
->virtadr
;
596 printk("select chip (%d)\n", chip
);
601 floor
= chip
/ doc
->chips_per_floor
;
602 chip
-= (floor
* doc
->chips_per_floor
);
604 /* 11.4.4 -- deassert CE before changing chip */
605 doc200x_hwcontrol(this, NAND_CMD_NONE
, 0 | NAND_CTRL_CHANGE
);
607 WriteDOC(floor
, docptr
, FloorSelect
);
608 WriteDOC(chip
, docptr
, CDSNDeviceSelect
);
610 doc200x_hwcontrol(this, NAND_CMD_NONE
, NAND_NCE
| NAND_CTRL_CHANGE
);
613 doc
->curfloor
= floor
;
616 #define CDSN_CTRL_MSK (CDSN_CTRL_CE | CDSN_CTRL_CLE | CDSN_CTRL_ALE)
618 static void doc200x_hwcontrol(struct nand_chip
*this, int cmd
,
621 struct doc_priv
*doc
= nand_get_controller_data(this);
622 void __iomem
*docptr
= doc
->virtadr
;
624 if (ctrl
& NAND_CTRL_CHANGE
) {
625 doc
->CDSNControl
&= ~CDSN_CTRL_MSK
;
626 doc
->CDSNControl
|= ctrl
& CDSN_CTRL_MSK
;
628 printk("hwcontrol(%d): %02x\n", cmd
, doc
->CDSNControl
);
629 WriteDOC(doc
->CDSNControl
, docptr
, CDSNControl
);
630 /* 11.4.3 -- 4 NOPs after CSDNControl write */
633 if (cmd
!= NAND_CMD_NONE
) {
634 if (DoC_is_2000(doc
))
635 doc2000_write_byte(this, cmd
);
637 doc2001_write_byte(this, cmd
);
641 static void doc2001plus_command(struct nand_chip
*this, unsigned command
,
642 int column
, int page_addr
)
644 struct mtd_info
*mtd
= nand_to_mtd(this);
645 struct doc_priv
*doc
= nand_get_controller_data(this);
646 void __iomem
*docptr
= doc
->virtadr
;
649 * Must terminate write pipeline before sending any commands
652 if (command
== NAND_CMD_PAGEPROG
) {
653 WriteDOC(0x00, docptr
, Mplus_WritePipeTerm
);
654 WriteDOC(0x00, docptr
, Mplus_WritePipeTerm
);
658 * Write out the command to the device.
660 if (command
== NAND_CMD_SEQIN
) {
663 if (column
>= mtd
->writesize
) {
665 column
-= mtd
->writesize
;
666 readcmd
= NAND_CMD_READOOB
;
667 } else if (column
< 256) {
668 /* First 256 bytes --> READ0 */
669 readcmd
= NAND_CMD_READ0
;
672 readcmd
= NAND_CMD_READ1
;
674 WriteDOC(readcmd
, docptr
, Mplus_FlashCmd
);
676 WriteDOC(command
, docptr
, Mplus_FlashCmd
);
677 WriteDOC(0, docptr
, Mplus_WritePipeTerm
);
678 WriteDOC(0, docptr
, Mplus_WritePipeTerm
);
680 if (column
!= -1 || page_addr
!= -1) {
681 /* Serially input address */
683 /* Adjust columns for 16 bit buswidth */
684 if (this->options
& NAND_BUSWIDTH_16
&&
685 !nand_opcode_8bits(command
))
687 WriteDOC(column
, docptr
, Mplus_FlashAddress
);
689 if (page_addr
!= -1) {
690 WriteDOC((unsigned char)(page_addr
& 0xff), docptr
, Mplus_FlashAddress
);
691 WriteDOC((unsigned char)((page_addr
>> 8) & 0xff), docptr
, Mplus_FlashAddress
);
692 if (this->options
& NAND_ROW_ADDR_3
) {
693 WriteDOC((unsigned char)((page_addr
>> 16) & 0x0f), docptr
, Mplus_FlashAddress
);
694 printk("high density\n");
697 WriteDOC(0, docptr
, Mplus_WritePipeTerm
);
698 WriteDOC(0, docptr
, Mplus_WritePipeTerm
);
700 if (command
== NAND_CMD_READ0
|| command
== NAND_CMD_READ1
||
701 command
== NAND_CMD_READOOB
|| command
== NAND_CMD_READID
)
702 WriteDOC(0, docptr
, Mplus_FlashControl
);
706 * program and erase have their own busy handlers
707 * status and sequential in needs no delay
711 case NAND_CMD_PAGEPROG
:
712 case NAND_CMD_ERASE1
:
713 case NAND_CMD_ERASE2
:
715 case NAND_CMD_STATUS
:
719 if (this->legacy
.dev_ready
)
721 udelay(this->legacy
.chip_delay
);
722 WriteDOC(NAND_CMD_STATUS
, docptr
, Mplus_FlashCmd
);
723 WriteDOC(0, docptr
, Mplus_WritePipeTerm
);
724 WriteDOC(0, docptr
, Mplus_WritePipeTerm
);
725 while (!(this->legacy
.read_byte(this) & 0x40)) ;
728 /* This applies to read commands */
731 * If we don't have access to the busy pin, we apply the given
734 if (!this->legacy
.dev_ready
) {
735 udelay(this->legacy
.chip_delay
);
740 /* Apply this short delay always to ensure that we do wait tWB in
741 * any case on any machine. */
743 /* wait until command is processed */
744 while (!this->legacy
.dev_ready(this)) ;
747 static int doc200x_dev_ready(struct nand_chip
*this)
749 struct doc_priv
*doc
= nand_get_controller_data(this);
750 void __iomem
*docptr
= doc
->virtadr
;
752 if (DoC_is_MillenniumPlus(doc
)) {
753 /* 11.4.2 -- must NOP four times before checking FR/B# */
755 if ((ReadDOC(docptr
, Mplus_FlashControl
) & CDSN_CTRL_FR_B_MASK
) != CDSN_CTRL_FR_B_MASK
) {
757 printk("not ready\n");
761 printk("was ready\n");
764 /* 11.4.2 -- must NOP four times before checking FR/B# */
766 if (!(ReadDOC(docptr
, CDSNControl
) & CDSN_CTRL_FR_B
)) {
768 printk("not ready\n");
771 /* 11.4.2 -- Must NOP twice if it's ready */
774 printk("was ready\n");
779 static int doc200x_block_bad(struct nand_chip
*this, loff_t ofs
)
781 /* This is our last resort if we couldn't find or create a BBT. Just
782 pretend all blocks are good. */
786 static void doc200x_enable_hwecc(struct nand_chip
*this, int mode
)
788 struct doc_priv
*doc
= nand_get_controller_data(this);
789 void __iomem
*docptr
= doc
->virtadr
;
791 /* Prime the ECC engine */
794 WriteDOC(DOC_ECC_RESET
, docptr
, ECCConf
);
795 WriteDOC(DOC_ECC_EN
, docptr
, ECCConf
);
798 WriteDOC(DOC_ECC_RESET
, docptr
, ECCConf
);
799 WriteDOC(DOC_ECC_EN
| DOC_ECC_RW
, docptr
, ECCConf
);
804 static void doc2001plus_enable_hwecc(struct nand_chip
*this, int mode
)
806 struct doc_priv
*doc
= nand_get_controller_data(this);
807 void __iomem
*docptr
= doc
->virtadr
;
809 /* Prime the ECC engine */
812 WriteDOC(DOC_ECC_RESET
, docptr
, Mplus_ECCConf
);
813 WriteDOC(DOC_ECC_EN
, docptr
, Mplus_ECCConf
);
816 WriteDOC(DOC_ECC_RESET
, docptr
, Mplus_ECCConf
);
817 WriteDOC(DOC_ECC_EN
| DOC_ECC_RW
, docptr
, Mplus_ECCConf
);
822 /* This code is only called on write */
823 static int doc200x_calculate_ecc(struct nand_chip
*this, const u_char
*dat
,
824 unsigned char *ecc_code
)
826 struct doc_priv
*doc
= nand_get_controller_data(this);
827 void __iomem
*docptr
= doc
->virtadr
;
831 /* flush the pipeline */
832 if (DoC_is_2000(doc
)) {
833 WriteDOC(doc
->CDSNControl
& ~CDSN_CTRL_FLASH_IO
, docptr
, CDSNControl
);
834 WriteDOC(0, docptr
, 2k_CDSN_IO
);
835 WriteDOC(0, docptr
, 2k_CDSN_IO
);
836 WriteDOC(0, docptr
, 2k_CDSN_IO
);
837 WriteDOC(doc
->CDSNControl
, docptr
, CDSNControl
);
838 } else if (DoC_is_MillenniumPlus(doc
)) {
839 WriteDOC(0, docptr
, Mplus_NOP
);
840 WriteDOC(0, docptr
, Mplus_NOP
);
841 WriteDOC(0, docptr
, Mplus_NOP
);
843 WriteDOC(0, docptr
, NOP
);
844 WriteDOC(0, docptr
, NOP
);
845 WriteDOC(0, docptr
, NOP
);
848 for (i
= 0; i
< 6; i
++) {
849 if (DoC_is_MillenniumPlus(doc
))
850 ecc_code
[i
] = ReadDOC_(docptr
, DoC_Mplus_ECCSyndrome0
+ i
);
852 ecc_code
[i
] = ReadDOC_(docptr
, DoC_ECCSyndrome0
+ i
);
853 if (ecc_code
[i
] != empty_write_ecc
[i
])
856 if (DoC_is_MillenniumPlus(doc
))
857 WriteDOC(DOC_ECC_DIS
, docptr
, Mplus_ECCConf
);
859 WriteDOC(DOC_ECC_DIS
, docptr
, ECCConf
);
861 /* If emptymatch=1, we might have an all-0xff data buffer. Check. */
863 /* Note: this somewhat expensive test should not be triggered
864 often. It could be optimized away by examining the data in
865 the writebuf routine, and remembering the result. */
866 for (i
= 0; i
< 512; i
++) {
873 /* If emptymatch still =1, we do have an all-0xff data buffer.
874 Return all-0xff ecc value instead of the computed one, so
875 it'll look just like a freshly-erased page. */
877 memset(ecc_code
, 0xff, 6);
882 static int doc200x_correct_data(struct nand_chip
*this, u_char
*dat
,
883 u_char
*read_ecc
, u_char
*isnull
)
886 struct doc_priv
*doc
= nand_get_controller_data(this);
887 void __iomem
*docptr
= doc
->virtadr
;
889 volatile u_char dummy
;
891 /* flush the pipeline */
892 if (DoC_is_2000(doc
)) {
893 dummy
= ReadDOC(docptr
, 2k_ECCStatus
);
894 dummy
= ReadDOC(docptr
, 2k_ECCStatus
);
895 dummy
= ReadDOC(docptr
, 2k_ECCStatus
);
896 } else if (DoC_is_MillenniumPlus(doc
)) {
897 dummy
= ReadDOC(docptr
, Mplus_ECCConf
);
898 dummy
= ReadDOC(docptr
, Mplus_ECCConf
);
899 dummy
= ReadDOC(docptr
, Mplus_ECCConf
);
901 dummy
= ReadDOC(docptr
, ECCConf
);
902 dummy
= ReadDOC(docptr
, ECCConf
);
903 dummy
= ReadDOC(docptr
, ECCConf
);
906 /* Error occurred ? */
908 for (i
= 0; i
< 6; i
++) {
909 if (DoC_is_MillenniumPlus(doc
))
910 calc_ecc
[i
] = ReadDOC_(docptr
, DoC_Mplus_ECCSyndrome0
+ i
);
912 calc_ecc
[i
] = ReadDOC_(docptr
, DoC_ECCSyndrome0
+ i
);
915 ret
= doc_ecc_decode(doc
->rs_decoder
, dat
, calc_ecc
);
917 pr_err("doc200x_correct_data corrected %d errors\n",
920 if (DoC_is_MillenniumPlus(doc
))
921 WriteDOC(DOC_ECC_DIS
, docptr
, Mplus_ECCConf
);
923 WriteDOC(DOC_ECC_DIS
, docptr
, ECCConf
);
924 if (no_ecc_failures
&& mtd_is_eccerr(ret
)) {
925 pr_err("suppressing ECC failure\n");
931 //u_char mydatabuf[528];
933 static int doc200x_ooblayout_ecc(struct mtd_info
*mtd
, int section
,
934 struct mtd_oob_region
*oobregion
)
939 oobregion
->offset
= 0;
940 oobregion
->length
= 6;
945 static int doc200x_ooblayout_free(struct mtd_info
*mtd
, int section
,
946 struct mtd_oob_region
*oobregion
)
952 * The strange out-of-order free bytes definition is a (possibly
953 * unneeded) attempt to retain compatibility. It used to read:
954 * .oobfree = { {8, 8} }
955 * Since that leaves two bytes unusable, it was changed. But the
956 * following scheme might affect existing jffs2 installs by moving the
958 * .oobfree = { {6, 10} }
959 * jffs2 seems to handle the above gracefully, but the current scheme
960 * seems safer. The only problem with it is that any code retrieving
961 * free bytes position must be able to handle out-of-order segments.
964 oobregion
->offset
= 8;
965 oobregion
->length
= 8;
967 oobregion
->offset
= 6;
968 oobregion
->length
= 2;
974 static const struct mtd_ooblayout_ops doc200x_ooblayout_ops
= {
975 .ecc
= doc200x_ooblayout_ecc
,
976 .free
= doc200x_ooblayout_free
,
979 /* Find the (I)NFTL Media Header, and optionally also the mirror media header.
980 On successful return, buf will contain a copy of the media header for
981 further processing. id is the string to scan for, and will presumably be
982 either "ANAND" or "BNAND". If findmirror=1, also look for the mirror media
983 header. The page #s of the found media headers are placed in mh0_page and
984 mh1_page in the DOC private structure. */
985 static int __init
find_media_headers(struct mtd_info
*mtd
, u_char
*buf
, const char *id
, int findmirror
)
987 struct nand_chip
*this = mtd_to_nand(mtd
);
988 struct doc_priv
*doc
= nand_get_controller_data(this);
993 for (offs
= 0; offs
< mtd
->size
; offs
+= mtd
->erasesize
) {
994 ret
= mtd_read(mtd
, offs
, mtd
->writesize
, &retlen
, buf
);
995 if (retlen
!= mtd
->writesize
)
998 pr_warn("ECC error scanning DOC at 0x%x\n", offs
);
1000 if (memcmp(buf
, id
, 6))
1002 pr_info("Found DiskOnChip %s Media Header at 0x%x\n", id
, offs
);
1003 if (doc
->mh0_page
== -1) {
1004 doc
->mh0_page
= offs
>> this->page_shift
;
1009 doc
->mh1_page
= offs
>> this->page_shift
;
1012 if (doc
->mh0_page
== -1) {
1013 pr_warn("DiskOnChip %s Media Header not found.\n", id
);
1016 /* Only one mediaheader was found. We want buf to contain a
1017 mediaheader on return, so we'll have to re-read the one we found. */
1018 offs
= doc
->mh0_page
<< this->page_shift
;
1019 ret
= mtd_read(mtd
, offs
, mtd
->writesize
, &retlen
, buf
);
1020 if (retlen
!= mtd
->writesize
) {
1021 /* Insanity. Give up. */
1022 pr_err("Read DiskOnChip Media Header once, but can't reread it???\n");
1028 static inline int __init
nftl_partscan(struct mtd_info
*mtd
, struct mtd_partition
*parts
)
1030 struct nand_chip
*this = mtd_to_nand(mtd
);
1031 struct doc_priv
*doc
= nand_get_controller_data(this);
1032 struct nand_memory_organization
*memorg
;
1035 struct NFTLMediaHeader
*mh
;
1036 const unsigned psize
= 1 << this->page_shift
;
1038 unsigned blocks
, maxblocks
;
1039 int offs
, numheaders
;
1041 memorg
= nanddev_get_memorg(&this->base
);
1043 buf
= kmalloc(mtd
->writesize
, GFP_KERNEL
);
1047 if (!(numheaders
= find_media_headers(mtd
, buf
, "ANAND", 1)))
1049 mh
= (struct NFTLMediaHeader
*)buf
;
1051 le16_to_cpus(&mh
->NumEraseUnits
);
1052 le16_to_cpus(&mh
->FirstPhysicalEUN
);
1053 le32_to_cpus(&mh
->FormattedSize
);
1055 pr_info(" DataOrgID = %s\n"
1056 " NumEraseUnits = %d\n"
1057 " FirstPhysicalEUN = %d\n"
1058 " FormattedSize = %d\n"
1059 " UnitSizeFactor = %d\n",
1060 mh
->DataOrgID
, mh
->NumEraseUnits
,
1061 mh
->FirstPhysicalEUN
, mh
->FormattedSize
,
1062 mh
->UnitSizeFactor
);
1064 blocks
= mtd
->size
>> this->phys_erase_shift
;
1065 maxblocks
= min(32768U, mtd
->erasesize
- psize
);
1067 if (mh
->UnitSizeFactor
== 0x00) {
1068 /* Auto-determine UnitSizeFactor. The constraints are:
1069 - There can be at most 32768 virtual blocks.
1070 - There can be at most (virtual block size - page size)
1071 virtual blocks (because MediaHeader+BBT must fit in 1).
1073 mh
->UnitSizeFactor
= 0xff;
1074 while (blocks
> maxblocks
) {
1076 maxblocks
= min(32768U, (maxblocks
<< 1) + psize
);
1077 mh
->UnitSizeFactor
--;
1079 pr_warn("UnitSizeFactor=0x00 detected. Correct value is assumed to be 0x%02x.\n", mh
->UnitSizeFactor
);
1082 /* NOTE: The lines below modify internal variables of the NAND and MTD
1083 layers; variables with have already been configured by nand_scan.
1084 Unfortunately, we didn't know before this point what these values
1085 should be. Thus, this code is somewhat dependent on the exact
1086 implementation of the NAND layer. */
1087 if (mh
->UnitSizeFactor
!= 0xff) {
1088 this->bbt_erase_shift
+= (0xff - mh
->UnitSizeFactor
);
1089 memorg
->pages_per_eraseblock
<<= (0xff - mh
->UnitSizeFactor
);
1090 mtd
->erasesize
<<= (0xff - mh
->UnitSizeFactor
);
1091 pr_info("Setting virtual erase size to %d\n", mtd
->erasesize
);
1092 blocks
= mtd
->size
>> this->bbt_erase_shift
;
1093 maxblocks
= min(32768U, mtd
->erasesize
- psize
);
1096 if (blocks
> maxblocks
) {
1097 pr_err("UnitSizeFactor of 0x%02x is inconsistent with device size. Aborting.\n", mh
->UnitSizeFactor
);
1101 /* Skip past the media headers. */
1102 offs
= max(doc
->mh0_page
, doc
->mh1_page
);
1103 offs
<<= this->page_shift
;
1104 offs
+= mtd
->erasesize
;
1106 if (show_firmware_partition
== 1) {
1107 parts
[0].name
= " DiskOnChip Firmware / Media Header partition";
1108 parts
[0].offset
= 0;
1109 parts
[0].size
= offs
;
1113 parts
[numparts
].name
= " DiskOnChip BDTL partition";
1114 parts
[numparts
].offset
= offs
;
1115 parts
[numparts
].size
= (mh
->NumEraseUnits
- numheaders
) << this->bbt_erase_shift
;
1117 offs
+= parts
[numparts
].size
;
1120 if (offs
< mtd
->size
) {
1121 parts
[numparts
].name
= " DiskOnChip Remainder partition";
1122 parts
[numparts
].offset
= offs
;
1123 parts
[numparts
].size
= mtd
->size
- offs
;
1133 /* This is a stripped-down copy of the code in inftlmount.c */
1134 static inline int __init
inftl_partscan(struct mtd_info
*mtd
, struct mtd_partition
*parts
)
1136 struct nand_chip
*this = mtd_to_nand(mtd
);
1137 struct doc_priv
*doc
= nand_get_controller_data(this);
1140 struct INFTLMediaHeader
*mh
;
1141 struct INFTLPartition
*ip
;
1144 int vshift
, lastvunit
= 0;
1146 int end
= mtd
->size
;
1148 if (inftl_bbt_write
)
1149 end
-= (INFTL_BBT_RESERVED_BLOCKS
<< this->phys_erase_shift
);
1151 buf
= kmalloc(mtd
->writesize
, GFP_KERNEL
);
1156 if (!find_media_headers(mtd
, buf
, "BNAND", 0))
1158 doc
->mh1_page
= doc
->mh0_page
+ (4096 >> this->page_shift
);
1159 mh
= (struct INFTLMediaHeader
*)buf
;
1161 le32_to_cpus(&mh
->NoOfBootImageBlocks
);
1162 le32_to_cpus(&mh
->NoOfBinaryPartitions
);
1163 le32_to_cpus(&mh
->NoOfBDTLPartitions
);
1164 le32_to_cpus(&mh
->BlockMultiplierBits
);
1165 le32_to_cpus(&mh
->FormatFlags
);
1166 le32_to_cpus(&mh
->PercentUsed
);
1168 pr_info(" bootRecordID = %s\n"
1169 " NoOfBootImageBlocks = %d\n"
1170 " NoOfBinaryPartitions = %d\n"
1171 " NoOfBDTLPartitions = %d\n"
1172 " BlockMultiplerBits = %d\n"
1173 " FormatFlgs = %d\n"
1174 " OsakVersion = %d.%d.%d.%d\n"
1175 " PercentUsed = %d\n",
1176 mh
->bootRecordID
, mh
->NoOfBootImageBlocks
,
1177 mh
->NoOfBinaryPartitions
,
1178 mh
->NoOfBDTLPartitions
,
1179 mh
->BlockMultiplierBits
, mh
->FormatFlags
,
1180 ((unsigned char *) &mh
->OsakVersion
)[0] & 0xf,
1181 ((unsigned char *) &mh
->OsakVersion
)[1] & 0xf,
1182 ((unsigned char *) &mh
->OsakVersion
)[2] & 0xf,
1183 ((unsigned char *) &mh
->OsakVersion
)[3] & 0xf,
1186 vshift
= this->phys_erase_shift
+ mh
->BlockMultiplierBits
;
1188 blocks
= mtd
->size
>> vshift
;
1189 if (blocks
> 32768) {
1190 pr_err("BlockMultiplierBits=%d is inconsistent with device size. Aborting.\n", mh
->BlockMultiplierBits
);
1194 blocks
= doc
->chips_per_floor
<< (this->chip_shift
- this->phys_erase_shift
);
1195 if (inftl_bbt_write
&& (blocks
> mtd
->erasesize
)) {
1196 pr_err("Writeable BBTs spanning more than one erase block are not yet supported. FIX ME!\n");
1200 /* Scan the partitions */
1201 for (i
= 0; (i
< 4); i
++) {
1202 ip
= &(mh
->Partitions
[i
]);
1203 le32_to_cpus(&ip
->virtualUnits
);
1204 le32_to_cpus(&ip
->firstUnit
);
1205 le32_to_cpus(&ip
->lastUnit
);
1206 le32_to_cpus(&ip
->flags
);
1207 le32_to_cpus(&ip
->spareUnits
);
1208 le32_to_cpus(&ip
->Reserved0
);
1210 pr_info(" PARTITION[%d] ->\n"
1211 " virtualUnits = %d\n"
1215 " spareUnits = %d\n",
1216 i
, ip
->virtualUnits
, ip
->firstUnit
,
1217 ip
->lastUnit
, ip
->flags
,
1220 if ((show_firmware_partition
== 1) &&
1221 (i
== 0) && (ip
->firstUnit
> 0)) {
1222 parts
[0].name
= " DiskOnChip IPL / Media Header partition";
1223 parts
[0].offset
= 0;
1224 parts
[0].size
= mtd
->erasesize
* ip
->firstUnit
;
1228 if (ip
->flags
& INFTL_BINARY
)
1229 parts
[numparts
].name
= " DiskOnChip BDK partition";
1231 parts
[numparts
].name
= " DiskOnChip BDTL partition";
1232 parts
[numparts
].offset
= ip
->firstUnit
<< vshift
;
1233 parts
[numparts
].size
= (1 + ip
->lastUnit
- ip
->firstUnit
) << vshift
;
1235 if (ip
->lastUnit
> lastvunit
)
1236 lastvunit
= ip
->lastUnit
;
1237 if (ip
->flags
& INFTL_LAST
)
1241 if ((lastvunit
<< vshift
) < end
) {
1242 parts
[numparts
].name
= " DiskOnChip Remainder partition";
1243 parts
[numparts
].offset
= lastvunit
<< vshift
;
1244 parts
[numparts
].size
= end
- parts
[numparts
].offset
;
1253 static int __init
nftl_scan_bbt(struct mtd_info
*mtd
)
1256 struct nand_chip
*this = mtd_to_nand(mtd
);
1257 struct doc_priv
*doc
= nand_get_controller_data(this);
1258 struct mtd_partition parts
[2];
1260 memset((char *)parts
, 0, sizeof(parts
));
1261 /* On NFTL, we have to find the media headers before we can read the
1262 BBTs, since they're stored in the media header eraseblocks. */
1263 numparts
= nftl_partscan(mtd
, parts
);
1266 this->bbt_td
->options
= NAND_BBT_ABSPAGE
| NAND_BBT_8BIT
|
1267 NAND_BBT_SAVECONTENT
| NAND_BBT_WRITE
|
1269 this->bbt_td
->veroffs
= 7;
1270 this->bbt_td
->pages
[0] = doc
->mh0_page
+ 1;
1271 if (doc
->mh1_page
!= -1) {
1272 this->bbt_md
->options
= NAND_BBT_ABSPAGE
| NAND_BBT_8BIT
|
1273 NAND_BBT_SAVECONTENT
| NAND_BBT_WRITE
|
1275 this->bbt_md
->veroffs
= 7;
1276 this->bbt_md
->pages
[0] = doc
->mh1_page
+ 1;
1278 this->bbt_md
= NULL
;
1281 ret
= nand_create_bbt(this);
1285 return mtd_device_register(mtd
, parts
, no_autopart
? 0 : numparts
);
1288 static int __init
inftl_scan_bbt(struct mtd_info
*mtd
)
1291 struct nand_chip
*this = mtd_to_nand(mtd
);
1292 struct doc_priv
*doc
= nand_get_controller_data(this);
1293 struct mtd_partition parts
[5];
1295 if (nanddev_ntargets(&this->base
) > doc
->chips_per_floor
) {
1296 pr_err("Multi-floor INFTL devices not yet supported.\n");
1300 if (DoC_is_MillenniumPlus(doc
)) {
1301 this->bbt_td
->options
= NAND_BBT_2BIT
| NAND_BBT_ABSPAGE
;
1302 if (inftl_bbt_write
)
1303 this->bbt_td
->options
|= NAND_BBT_WRITE
;
1304 this->bbt_td
->pages
[0] = 2;
1305 this->bbt_md
= NULL
;
1307 this->bbt_td
->options
= NAND_BBT_LASTBLOCK
| NAND_BBT_8BIT
| NAND_BBT_VERSION
;
1308 if (inftl_bbt_write
)
1309 this->bbt_td
->options
|= NAND_BBT_WRITE
;
1310 this->bbt_td
->offs
= 8;
1311 this->bbt_td
->len
= 8;
1312 this->bbt_td
->veroffs
= 7;
1313 this->bbt_td
->maxblocks
= INFTL_BBT_RESERVED_BLOCKS
;
1314 this->bbt_td
->reserved_block_code
= 0x01;
1315 this->bbt_td
->pattern
= "MSYS_BBT";
1317 this->bbt_md
->options
= NAND_BBT_LASTBLOCK
| NAND_BBT_8BIT
| NAND_BBT_VERSION
;
1318 if (inftl_bbt_write
)
1319 this->bbt_md
->options
|= NAND_BBT_WRITE
;
1320 this->bbt_md
->offs
= 8;
1321 this->bbt_md
->len
= 8;
1322 this->bbt_md
->veroffs
= 7;
1323 this->bbt_md
->maxblocks
= INFTL_BBT_RESERVED_BLOCKS
;
1324 this->bbt_md
->reserved_block_code
= 0x01;
1325 this->bbt_md
->pattern
= "TBB_SYSM";
1328 ret
= nand_create_bbt(this);
1332 memset((char *)parts
, 0, sizeof(parts
));
1333 numparts
= inftl_partscan(mtd
, parts
);
1334 /* At least for now, require the INFTL Media Header. We could probably
1335 do without it for non-INFTL use, since all it gives us is
1336 autopartitioning, but I want to give it more thought. */
1339 return mtd_device_register(mtd
, parts
, no_autopart
? 0 : numparts
);
1342 static inline int __init
doc2000_init(struct mtd_info
*mtd
)
1344 struct nand_chip
*this = mtd_to_nand(mtd
);
1345 struct doc_priv
*doc
= nand_get_controller_data(this);
1347 this->legacy
.read_byte
= doc2000_read_byte
;
1348 this->legacy
.write_buf
= doc2000_writebuf
;
1349 this->legacy
.read_buf
= doc2000_readbuf
;
1350 doc
->late_init
= nftl_scan_bbt
;
1352 doc
->CDSNControl
= CDSN_CTRL_FLASH_IO
| CDSN_CTRL_ECC_IO
;
1353 doc2000_count_chips(mtd
);
1354 mtd
->name
= "DiskOnChip 2000 (NFTL Model)";
1355 return (4 * doc
->chips_per_floor
);
1358 static inline int __init
doc2001_init(struct mtd_info
*mtd
)
1360 struct nand_chip
*this = mtd_to_nand(mtd
);
1361 struct doc_priv
*doc
= nand_get_controller_data(this);
1363 this->legacy
.read_byte
= doc2001_read_byte
;
1364 this->legacy
.write_buf
= doc2001_writebuf
;
1365 this->legacy
.read_buf
= doc2001_readbuf
;
1367 ReadDOC(doc
->virtadr
, ChipID
);
1368 ReadDOC(doc
->virtadr
, ChipID
);
1369 ReadDOC(doc
->virtadr
, ChipID
);
1370 if (ReadDOC(doc
->virtadr
, ChipID
) != DOC_ChipID_DocMil
) {
1371 /* It's not a Millennium; it's one of the newer
1372 DiskOnChip 2000 units with a similar ASIC.
1373 Treat it like a Millennium, except that it
1374 can have multiple chips. */
1375 doc2000_count_chips(mtd
);
1376 mtd
->name
= "DiskOnChip 2000 (INFTL Model)";
1377 doc
->late_init
= inftl_scan_bbt
;
1378 return (4 * doc
->chips_per_floor
);
1380 /* Bog-standard Millennium */
1381 doc
->chips_per_floor
= 1;
1382 mtd
->name
= "DiskOnChip Millennium";
1383 doc
->late_init
= nftl_scan_bbt
;
1388 static inline int __init
doc2001plus_init(struct mtd_info
*mtd
)
1390 struct nand_chip
*this = mtd_to_nand(mtd
);
1391 struct doc_priv
*doc
= nand_get_controller_data(this);
1393 this->legacy
.read_byte
= doc2001plus_read_byte
;
1394 this->legacy
.write_buf
= doc2001plus_writebuf
;
1395 this->legacy
.read_buf
= doc2001plus_readbuf
;
1396 doc
->late_init
= inftl_scan_bbt
;
1397 this->legacy
.cmd_ctrl
= NULL
;
1398 this->legacy
.select_chip
= doc2001plus_select_chip
;
1399 this->legacy
.cmdfunc
= doc2001plus_command
;
1400 this->ecc
.hwctl
= doc2001plus_enable_hwecc
;
1402 doc
->chips_per_floor
= 1;
1403 mtd
->name
= "DiskOnChip Millennium Plus";
1408 static int __init
doc_probe(unsigned long physadr
)
1410 struct nand_chip
*nand
= NULL
;
1411 struct doc_priv
*doc
= NULL
;
1412 unsigned char ChipID
;
1413 struct mtd_info
*mtd
;
1414 void __iomem
*virtadr
;
1415 unsigned char save_control
;
1416 unsigned char tmp
, tmpb
, tmpc
;
1417 int reg
, len
, numchips
;
1420 if (!request_mem_region(physadr
, DOC_IOREMAP_LEN
, "DiskOnChip"))
1422 virtadr
= ioremap(physadr
, DOC_IOREMAP_LEN
);
1424 pr_err("Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n",
1425 DOC_IOREMAP_LEN
, physadr
);
1430 /* It's not possible to cleanly detect the DiskOnChip - the
1431 * bootup procedure will put the device into reset mode, and
1432 * it's not possible to talk to it without actually writing
1433 * to the DOCControl register. So we store the current contents
1434 * of the DOCControl register's location, in case we later decide
1435 * that it's not a DiskOnChip, and want to put it back how we
1438 save_control
= ReadDOC(virtadr
, DOCControl
);
1440 /* Reset the DiskOnChip ASIC */
1441 WriteDOC(DOC_MODE_CLR_ERR
| DOC_MODE_MDWREN
| DOC_MODE_RESET
, virtadr
, DOCControl
);
1442 WriteDOC(DOC_MODE_CLR_ERR
| DOC_MODE_MDWREN
| DOC_MODE_RESET
, virtadr
, DOCControl
);
1444 /* Enable the DiskOnChip ASIC */
1445 WriteDOC(DOC_MODE_CLR_ERR
| DOC_MODE_MDWREN
| DOC_MODE_NORMAL
, virtadr
, DOCControl
);
1446 WriteDOC(DOC_MODE_CLR_ERR
| DOC_MODE_MDWREN
| DOC_MODE_NORMAL
, virtadr
, DOCControl
);
1448 ChipID
= ReadDOC(virtadr
, ChipID
);
1451 case DOC_ChipID_Doc2k
:
1452 reg
= DoC_2k_ECCStatus
;
1454 case DOC_ChipID_DocMil
:
1457 case DOC_ChipID_DocMilPlus16
:
1458 case DOC_ChipID_DocMilPlus32
:
1460 /* Possible Millennium Plus, need to do more checks */
1461 /* Possibly release from power down mode */
1462 for (tmp
= 0; (tmp
< 4); tmp
++)
1463 ReadDOC(virtadr
, Mplus_Power
);
1465 /* Reset the Millennium Plus ASIC */
1466 tmp
= DOC_MODE_RESET
| DOC_MODE_MDWREN
| DOC_MODE_RST_LAT
| DOC_MODE_BDECT
;
1467 WriteDOC(tmp
, virtadr
, Mplus_DOCControl
);
1468 WriteDOC(~tmp
, virtadr
, Mplus_CtrlConfirm
);
1470 usleep_range(1000, 2000);
1471 /* Enable the Millennium Plus ASIC */
1472 tmp
= DOC_MODE_NORMAL
| DOC_MODE_MDWREN
| DOC_MODE_RST_LAT
| DOC_MODE_BDECT
;
1473 WriteDOC(tmp
, virtadr
, Mplus_DOCControl
);
1474 WriteDOC(~tmp
, virtadr
, Mplus_CtrlConfirm
);
1475 usleep_range(1000, 2000);
1477 ChipID
= ReadDOC(virtadr
, ChipID
);
1480 case DOC_ChipID_DocMilPlus16
:
1481 reg
= DoC_Mplus_Toggle
;
1483 case DOC_ChipID_DocMilPlus32
:
1484 pr_err("DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
1496 /* Check the TOGGLE bit in the ECC register */
1497 tmp
= ReadDOC_(virtadr
, reg
) & DOC_TOGGLE_BIT
;
1498 tmpb
= ReadDOC_(virtadr
, reg
) & DOC_TOGGLE_BIT
;
1499 tmpc
= ReadDOC_(virtadr
, reg
) & DOC_TOGGLE_BIT
;
1500 if ((tmp
== tmpb
) || (tmp
!= tmpc
)) {
1501 pr_warn("Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr
);
1506 for (mtd
= doclist
; mtd
; mtd
= doc
->nextdoc
) {
1507 unsigned char oldval
;
1508 unsigned char newval
;
1509 nand
= mtd_to_nand(mtd
);
1510 doc
= nand_get_controller_data(nand
);
1511 /* Use the alias resolution register to determine if this is
1512 in fact the same DOC aliased to a new address. If writes
1513 to one chip's alias resolution register change the value on
1514 the other chip, they're the same chip. */
1515 if (ChipID
== DOC_ChipID_DocMilPlus16
) {
1516 oldval
= ReadDOC(doc
->virtadr
, Mplus_AliasResolution
);
1517 newval
= ReadDOC(virtadr
, Mplus_AliasResolution
);
1519 oldval
= ReadDOC(doc
->virtadr
, AliasResolution
);
1520 newval
= ReadDOC(virtadr
, AliasResolution
);
1522 if (oldval
!= newval
)
1524 if (ChipID
== DOC_ChipID_DocMilPlus16
) {
1525 WriteDOC(~newval
, virtadr
, Mplus_AliasResolution
);
1526 oldval
= ReadDOC(doc
->virtadr
, Mplus_AliasResolution
);
1527 WriteDOC(newval
, virtadr
, Mplus_AliasResolution
); // restore it
1529 WriteDOC(~newval
, virtadr
, AliasResolution
);
1530 oldval
= ReadDOC(doc
->virtadr
, AliasResolution
);
1531 WriteDOC(newval
, virtadr
, AliasResolution
); // restore it
1534 if (oldval
== newval
) {
1535 pr_debug("Found alias of DOC at 0x%lx to 0x%lx\n",
1536 doc
->physadr
, physadr
);
1541 pr_notice("DiskOnChip found at 0x%lx\n", physadr
);
1543 len
= sizeof(struct nand_chip
) + sizeof(struct doc_priv
) +
1544 (2 * sizeof(struct nand_bbt_descr
));
1545 nand
= kzalloc(len
, GFP_KERNEL
);
1553 * Allocate a RS codec instance
1555 * Symbolsize is 10 (bits)
1556 * Primitve polynomial is x^10+x^3+1
1557 * First consecutive root is 510
1558 * Primitve element to generate roots = 1
1559 * Generator polinomial degree = 4
1561 doc
= (struct doc_priv
*) (nand
+ 1);
1562 doc
->rs_decoder
= init_rs(10, 0x409, FCR
, 1, NROOTS
);
1563 if (!doc
->rs_decoder
) {
1564 pr_err("DiskOnChip: Could not create a RS codec\n");
1569 mtd
= nand_to_mtd(nand
);
1570 nand
->bbt_td
= (struct nand_bbt_descr
*) (doc
+ 1);
1571 nand
->bbt_md
= nand
->bbt_td
+ 1;
1573 mtd
->owner
= THIS_MODULE
;
1574 mtd_set_ooblayout(mtd
, &doc200x_ooblayout_ops
);
1576 nand_set_controller_data(nand
, doc
);
1577 nand
->legacy
.select_chip
= doc200x_select_chip
;
1578 nand
->legacy
.cmd_ctrl
= doc200x_hwcontrol
;
1579 nand
->legacy
.dev_ready
= doc200x_dev_ready
;
1580 nand
->legacy
.waitfunc
= doc200x_wait
;
1581 nand
->legacy
.block_bad
= doc200x_block_bad
;
1582 nand
->ecc
.hwctl
= doc200x_enable_hwecc
;
1583 nand
->ecc
.calculate
= doc200x_calculate_ecc
;
1584 nand
->ecc
.correct
= doc200x_correct_data
;
1586 nand
->ecc
.mode
= NAND_ECC_HW_SYNDROME
;
1587 nand
->ecc
.size
= 512;
1588 nand
->ecc
.bytes
= 6;
1589 nand
->ecc
.strength
= 2;
1590 nand
->ecc
.options
= NAND_ECC_GENERIC_ERASED_CHECK
;
1591 nand
->bbt_options
= NAND_BBT_USE_FLASH
;
1592 /* Skip the automatic BBT scan so we can run it manually */
1593 nand
->options
|= NAND_SKIP_BBTSCAN
;
1595 doc
->physadr
= physadr
;
1596 doc
->virtadr
= virtadr
;
1597 doc
->ChipID
= ChipID
;
1602 doc
->nextdoc
= doclist
;
1604 if (ChipID
== DOC_ChipID_Doc2k
)
1605 numchips
= doc2000_init(mtd
);
1606 else if (ChipID
== DOC_ChipID_DocMilPlus16
)
1607 numchips
= doc2001plus_init(mtd
);
1609 numchips
= doc2001_init(mtd
);
1611 if ((ret
= nand_scan(nand
, numchips
)) || (ret
= doc
->late_init(mtd
))) {
1612 /* DBB note: i believe nand_release is necessary here, as
1613 buffers may have been allocated in nand_base. Check with
1615 /* nand_release will call mtd_device_unregister, but we
1616 haven't yet added it. This is handled without incident by
1617 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
);
1632 free_rs(doc
->rs_decoder
);
1637 release_mem_region(physadr
, DOC_IOREMAP_LEN
);
1642 static void release_nanddoc(void)
1644 struct mtd_info
*mtd
, *nextmtd
;
1645 struct nand_chip
*nand
;
1646 struct doc_priv
*doc
;
1648 for (mtd
= doclist
; mtd
; mtd
= nextmtd
) {
1649 nand
= mtd_to_nand(mtd
);
1650 doc
= nand_get_controller_data(nand
);
1652 nextmtd
= doc
->nextdoc
;
1654 iounmap(doc
->virtadr
);
1655 release_mem_region(doc
->physadr
, DOC_IOREMAP_LEN
);
1656 free_rs(doc
->rs_decoder
);
1661 static int __init
init_nanddoc(void)
1665 if (doc_config_location
) {
1666 pr_info("Using configured DiskOnChip probe address 0x%lx\n",
1667 doc_config_location
);
1668 ret
= doc_probe(doc_config_location
);
1672 for (i
= 0; (doc_locations
[i
] != 0xffffffff); i
++) {
1673 doc_probe(doc_locations
[i
]);
1676 /* No banner message any more. Print a message if no DiskOnChip
1677 found, so the user knows we at least tried. */
1679 pr_info("No valid DiskOnChip devices found\n");
1685 static void __exit
cleanup_nanddoc(void)
1687 /* Cleanup the nand/DoC resources */
1691 module_init(init_nanddoc
);
1692 module_exit(cleanup_nanddoc
);
1694 MODULE_LICENSE("GPL");
1695 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1696 MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver");