20 #include <linux/kernel.h>
22 #include <linux/sched.h>
26 #include <linux/slab.h>
34 #include <linux/module.h>
37 #ifndef CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS
38 #define CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS 0
41 static unsigned long __initdata doc_locations[] = {
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,
57 #warning Unknown architecture for DiskOnChip. No default probe locations defined
78 static u_char empty_read_syndrome[6] = { 0x26, 0xff, 0x6d, 0x47, 0x73, 0x7a };
82 static u_char empty_write_ecc[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
84 #define INFTL_BBT_RESERVED_BLOCKS 4
86 #define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
87 #define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
88 #define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
90 static void doc200x_hwcontrol(
struct mtd_info *mtd,
int cmd,
92 static void doc200x_select_chip(
struct mtd_info *mtd,
int chip);
97 static int try_dword = 1;
100 static int no_ecc_failures = 0;
103 static int no_autopart = 0;
106 static int show_firmware_partition = 0;
109 #ifdef CONFIG_MTD_NAND_DISKONCHIP_BBTWRITE
110 static int inftl_bbt_write = 1;
112 static int inftl_bbt_write = 0;
118 MODULE_PARM_DESC(doc_config_location,
"Physical memory address at which to probe for DiskOnChip");
121 #define SECTOR_SIZE 512
123 #define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / 10)
145 int i,
j, nerr, errpos[8];
149 memset(syn, 0,
sizeof(syn));
151 ds[0] = ((ecc[4] & 0xff) >> 0) | ((ecc[5] & 0x03) << 8);
152 ds[1] = ((ecc[5] & 0xfc) >> 2) | ((ecc[2] & 0x0f) << 6);
153 ds[2] = ((ecc[2] & 0xf0) >> 4) | ((ecc[3] & 0x3f) << 4);
154 ds[3] = ((ecc[3] & 0xc0) >> 6) | ((ecc[0] & 0xff) << 2);
158 for (i = 0; i <
NROOTS; i++)
165 for (j = 1; j <
NROOTS; j++) {
169 for (i = 0; i <
NROOTS; i++)
170 s[i] ^= rs->
alpha_to[rs_modnn(rs, tmp + (
FCR + i) *
j)];
174 for (i = 0; i <
NROOTS; i++) {
179 nerr = decode_rs16(rs,
NULL,
NULL, 1019, syn, 0, errpos, 0, errval);
190 for (i = 0; i < nerr; i++) {
191 int index, bitpos,
pos = 1015 - errpos[
i];
193 if (pos >=
NB_DATA && pos < 1019)
200 index = (pos >> 3) ^ 1;
203 val = (
uint8_t) (errval[i] >> (2 + bitpos));
208 index = ((pos >> 3) + 1) ^ 1;
209 bitpos = (bitpos + 10) & 7;
213 val = (
uint8_t) (errval[i] << (8 - bitpos));
221 return parity ? -
EBADMSG : nerr;
224 static void DoC_Delay(
struct doc_priv *doc,
unsigned short cycles)
229 for (i = 0; i < cycles; i++) {
240 #define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
243 static int _DoC_WaitReady(
struct doc_priv *doc)
246 unsigned long timeo =
jiffies + (
HZ * 10);
249 printk(
"_DoC_WaitReady...\n");
254 printk(
"_DoC_WaitReady timed out.\n");
263 printk(
"_DoC_WaitReady timed out.\n");
274 static inline int DoC_WaitReady(
struct doc_priv *doc)
284 ret = _DoC_WaitReady(doc);
290 ret = _DoC_WaitReady(doc);
295 printk(
"DoC_WaitReady OK\n");
299 static void doc2000_write_byte(
struct mtd_info *mtd,
u_char datum)
306 printk(
"write_byte %02x\n", datum);
307 WriteDOC(datum, docptr, CDSNSlowIO);
308 WriteDOC(datum, docptr, 2k_CDSN_IO);
320 ret =
ReadDOC(docptr, 2k_CDSN_IO);
322 printk(
"read_byte returns %02x\n", ret);
333 printk(
"writebuf of %d bytes: ", len);
334 for (i = 0; i < len; i++) {
343 static void doc2000_readbuf(
struct mtd_info *mtd,
u_char *buf,
int len)
351 printk(
"readbuf of %d bytes: ", len);
353 for (i = 0; i < len; i++) {
354 buf[
i] =
ReadDOC(docptr, 2k_CDSN_IO + i);
358 static void doc2000_readbuf_dword(
struct mtd_info *mtd,
u_char *buf,
int len)
366 printk(
"readbuf_dword of %d bytes: ", len);
368 if (
unlikely((((
unsigned long)buf) | len) & 3)) {
369 for (i = 0; i < len; i++) {
373 for (i = 0; i < len; i += 4) {
385 doc200x_select_chip(mtd, nr);
396 ret = this->read_byte(mtd) << 8;
397 ret |= this->read_byte(mtd);
416 if (((
ident.byte[0] << 8) |
ident.byte[1]) == ret) {
418 this->read_buf = &doc2000_readbuf_dword;
436 mfrid = doc200x_ident_chip(mtd, 0);
439 for (i = 1; i < 4; i++) {
440 if (doc200x_ident_chip(mtd, i) != mfrid)
456 status = (
int)this->read_byte(mtd);
461 static void doc2001_write_byte(
struct mtd_info *mtd,
u_char datum)
467 WriteDOC(datum, docptr, CDSNSlowIO);
468 WriteDOC(datum, docptr, Mil_CDSN_IO);
469 WriteDOC(datum, docptr, WritePipeTerm);
483 return ReadDOC(docptr, LastDataRead);
486 static void doc2001_writebuf(
struct mtd_info *mtd,
const u_char *buf,
int len)
493 for (i = 0; i < len; i++)
496 WriteDOC(0x00, docptr, WritePipeTerm);
499 static void doc2001_readbuf(
struct mtd_info *mtd,
u_char *buf,
int len)
509 for (i = 0; i < len - 1; i++)
510 buf[i] =
ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
513 buf[
i] =
ReadDOC(docptr, LastDataRead);
523 ReadDOC(docptr, Mplus_ReadPipeInit);
524 ReadDOC(docptr, Mplus_ReadPipeInit);
525 ret =
ReadDOC(docptr, Mplus_LastDataRead);
527 printk(
"read_byte returns %02x\n", ret);
531 static void doc2001plus_writebuf(
struct mtd_info *mtd,
const u_char *buf,
int len)
539 printk(
"writebuf of %d bytes: ", len);
540 for (i = 0; i < len; i++) {
549 static void doc2001plus_readbuf(
struct mtd_info *mtd,
u_char *buf,
int len)
557 printk(
"readbuf of %d bytes: ", len);
560 ReadDOC(docptr, Mplus_ReadPipeInit);
561 ReadDOC(docptr, Mplus_ReadPipeInit);
563 for (i = 0; i < len - 2; i++) {
564 buf[
i] =
ReadDOC(docptr, Mil_CDSN_IO);
570 buf[len - 2] =
ReadDOC(docptr, Mplus_LastDataRead);
572 printk(
"%02x ", buf[len - 2]);
573 buf[len - 1] =
ReadDOC(docptr, Mplus_LastDataRead);
575 printk(
"%02x ", buf[len - 1]);
580 static void doc2001plus_select_chip(
struct mtd_info *mtd,
int chip)
588 printk(
"select chip (%d)\n", chip);
592 WriteDOC(0, docptr, Mplus_FlashSelect);
607 static void doc200x_select_chip(
struct mtd_info *mtd,
int chip)
615 printk(
"select chip (%d)\n", chip);
626 WriteDOC(floor, docptr, FloorSelect);
627 WriteDOC(chip, docptr, CDSNDeviceSelect);
635 #define CDSN_CTRL_MSK (CDSN_CTRL_CE | CDSN_CTRL_CLE | CDSN_CTRL_ALE)
637 static void doc200x_hwcontrol(
struct mtd_info *mtd,
int cmd,
655 doc2000_write_byte(mtd, cmd);
657 doc2001_write_byte(mtd, cmd);
661 static void doc2001plus_command(
struct mtd_info *mtd,
unsigned command,
int column,
int page_addr)
672 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
673 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
686 }
else if (column < 256) {
693 WriteDOC(readcmd, docptr, Mplus_FlashCmd);
695 WriteDOC(command, docptr, Mplus_FlashCmd);
696 WriteDOC(0, docptr, Mplus_WritePipeTerm);
697 WriteDOC(0, docptr, Mplus_WritePipeTerm);
699 if (column != -1 || page_addr != -1) {
705 WriteDOC(column, docptr, Mplus_FlashAddress);
707 if (page_addr != -1) {
708 WriteDOC((
unsigned char)(page_addr & 0xff), docptr, Mplus_FlashAddress);
709 WriteDOC((
unsigned char)((page_addr >> 8) & 0xff), docptr, Mplus_FlashAddress);
711 if (this->chipsize & 0x0c000000) {
712 WriteDOC((
unsigned char)((page_addr >> 16) & 0x0f), docptr, Mplus_FlashAddress);
716 WriteDOC(0, docptr, Mplus_WritePipeTerm);
717 WriteDOC(0, docptr, Mplus_WritePipeTerm);
721 WriteDOC(0, docptr, Mplus_FlashControl);
742 WriteDOC(0, docptr, Mplus_WritePipeTerm);
743 WriteDOC(0, docptr, Mplus_WritePipeTerm);
744 while (!(this->read_byte(mtd) & 0x40)) ;
753 if (!this->dev_ready) {
763 while (!this->dev_ready(mtd)) ;
766 static int doc200x_dev_ready(
struct mtd_info *mtd)
799 static int doc200x_block_bad(
struct mtd_info *mtd, loff_t ofs,
int getchip)
806 static void doc200x_enable_hwecc(
struct mtd_info *mtd,
int mode)
825 static void doc2001plus_enable_hwecc(
struct mtd_info *mtd,
int mode)
845 static int doc200x_calculate_ecc(
struct mtd_info *mtd,
const u_char *
dat,
unsigned char *ecc_code)
870 for (i = 0; i < 6; i++) {
875 if (ecc_code[i] != empty_write_ecc[i])
888 for (i = 0; i < 512; i++) {
899 memset(ecc_code, 0xff, 6);
904 static int doc200x_correct_data(
struct mtd_info *mtd,
u_char *dat,
917 dummy =
ReadDOC(docptr, 2k_ECCStatus);
918 dummy =
ReadDOC(docptr, 2k_ECCStatus);
919 dummy =
ReadDOC(docptr, 2k_ECCStatus);
921 dummy =
ReadDOC(docptr, Mplus_ECCConf);
922 dummy =
ReadDOC(docptr, Mplus_ECCConf);
923 dummy =
ReadDOC(docptr, Mplus_ECCConf);
925 dummy =
ReadDOC(docptr, ECCConf);
926 dummy =
ReadDOC(docptr, ECCConf);
927 dummy =
ReadDOC(docptr, ECCConf);
932 for (i = 0; i < 6; i++) {
937 if (calc_ecc[i] != empty_read_syndrome[i])
943 for (i = 0; i < 6; i++) {
944 if (read_ecc[i] == 0xff)
955 for (i = 0; i < 512; i++) {
967 ret = doc_ecc_decode(rs_decoder, dat, calc_ecc);
969 printk(
KERN_ERR "doc200x_correct_data corrected %d errors\n", ret);
975 if (no_ecc_failures && mtd_is_eccerr(ret)) {
996 .eccpos = {0, 1, 2, 3, 4, 5},
997 .oobfree = {{8, 8}, {6, 2}}
1006 static int __init find_media_headers(
struct mtd_info *mtd,
u_char *buf,
const char *
id,
int findmirror)
1023 printk(
KERN_INFO "Found DiskOnChip %s Media Header at 0x%x\n",
id, offs);
1025 doc->
mh0_page = offs >> this->page_shift;
1030 doc->
mh1_page = offs >> this->page_shift;
1039 offs = doc->
mh0_page << this->page_shift;
1043 printk(
KERN_ERR "Read DiskOnChip Media Header once, but can't reread it???\n");
1056 const unsigned psize = 1 << this->page_shift;
1058 unsigned blocks, maxblocks;
1059 int offs, numheaders;
1066 if (!(numheaders = find_media_headers(mtd, buf,
"ANAND", 1)))
1075 " NumEraseUnits = %d\n"
1076 " FirstPhysicalEUN = %d\n"
1077 " FormattedSize = %d\n"
1078 " UnitSizeFactor = %d\n",
1083 blocks = mtd->
size >> this->phys_erase_shift;
1093 while (blocks > maxblocks) {
1095 maxblocks =
min(32768
U, (maxblocks << 1) + psize);
1110 blocks = mtd->
size >> this->bbt_erase_shift;
1114 if (blocks > maxblocks) {
1121 offs <<= this->page_shift;
1124 if (show_firmware_partition == 1) {
1125 parts[0].
name =
" DiskOnChip Firmware / Media Header partition";
1131 parts[numparts].
name =
" DiskOnChip BDTL partition";
1133 parts[numparts].
size = (mh->
NumEraseUnits - numheaders) << this->bbt_erase_shift;
1135 offs += parts[numparts].
size;
1138 if (offs < mtd->
size) {
1139 parts[numparts].
name =
" DiskOnChip Remainder partition";
1162 int vshift, lastvunit = 0;
1166 if (inftl_bbt_write)
1175 if (!find_media_headers(mtd, buf,
"BNAND", 0))
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",
1207 blocks = mtd->
size >> vshift;
1208 if (blocks > 32768) {
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");
1220 for (i = 0; (i < 4); i++) {
1230 " virtualUnits = %d\n"
1234 " spareUnits = %d\n",
1239 if ((show_firmware_partition == 1) &&
1241 parts[0].
name =
" DiskOnChip IPL / Media Header partition";
1248 parts[numparts].
name =
" DiskOnChip BDK partition";
1250 parts[numparts].
name =
" DiskOnChip BDTL partition";
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;
1279 memset((
char *)parts, 0,
sizeof(parts));
1282 numparts = nftl_partscan(mtd, parts);
1288 this->bbt_td->veroffs = 7;
1289 this->bbt_td->pages[0] = doc->
mh0_page + 1;
1294 this->bbt_md->veroffs = 7;
1295 this->bbt_md->pages[0] = doc->
mh1_page + 1;
1297 this->bbt_md =
NULL;
1318 printk(
KERN_ERR "Multi-floor INFTL devices not yet supported.\n");
1324 if (inftl_bbt_write)
1326 this->bbt_td->pages[0] = 2;
1327 this->bbt_md =
NULL;
1330 if (inftl_bbt_write)
1332 this->bbt_td->offs = 8;
1333 this->bbt_td->len = 8;
1334 this->bbt_td->veroffs = 7;
1336 this->bbt_td->reserved_block_code = 0x01;
1337 this->bbt_td->pattern =
"MSYS_BBT";
1340 if (inftl_bbt_write)
1342 this->bbt_md->offs = 8;
1343 this->bbt_md->len = 8;
1344 this->bbt_md->veroffs = 7;
1346 this->bbt_md->reserved_block_code = 0x01;
1347 this->bbt_md->pattern =
"TBB_SYSM";
1354 memset((
char *)parts, 0,
sizeof(parts));
1355 numparts = inftl_partscan(mtd, parts);
1372 this->read_byte = doc2000_read_byte;
1374 this->read_buf = doc2000_readbuf;
1375 this->scan_bbt = nftl_scan_bbt;
1378 doc2000_count_chips(mtd);
1379 mtd->
name =
"DiskOnChip 2000 (NFTL Model)";
1388 this->read_byte = doc2001_read_byte;
1390 this->read_buf = doc2001_readbuf;
1400 doc2000_count_chips(mtd);
1401 mtd->
name =
"DiskOnChip 2000 (INFTL Model)";
1402 this->scan_bbt = inftl_scan_bbt;
1407 mtd->
name =
"DiskOnChip Millennium";
1408 this->scan_bbt = nftl_scan_bbt;
1418 this->read_byte = doc2001plus_read_byte;
1420 this->read_buf = doc2001plus_readbuf;
1421 this->scan_bbt = inftl_scan_bbt;
1422 this->cmd_ctrl =
NULL;
1423 this->select_chip = doc2001plus_select_chip;
1424 this->cmdfunc = doc2001plus_command;
1425 this->
ecc.hwctl = doc2001plus_enable_hwecc;
1428 mtd->
name =
"DiskOnChip Millennium Plus";
1440 unsigned char save_control;
1441 unsigned char tmp, tmpb, tmpc;
1442 int reg, len, numchips;
1459 save_control =
ReadDOC(virtadr, DOCControl);
1469 ChipID =
ReadDOC(virtadr, ChipID);
1483 for (tmp = 0; (tmp < 4); tmp++)
1484 ReadDOC(virtadr, Mplus_Power);
1488 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1489 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1494 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1495 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1498 ChipID =
ReadDOC(virtadr, ChipID);
1505 printk(
KERN_ERR "DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
1520 if ((tmp == tmpb) || (tmp != tmpc)) {
1521 printk(
KERN_WARNING "Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr);
1526 for (mtd = doclist; mtd; mtd = doc->
nextdoc) {
1527 unsigned char oldval;
1528 unsigned char newval;
1537 newval =
ReadDOC(virtadr, Mplus_AliasResolution);
1540 newval =
ReadDOC(virtadr, AliasResolution);
1542 if (oldval != newval)
1545 WriteDOC(~newval, virtadr, Mplus_AliasResolution);
1547 WriteDOC(newval, virtadr, Mplus_AliasResolution);
1549 WriteDOC(~newval, virtadr, AliasResolution);
1551 WriteDOC(newval, virtadr, AliasResolution);
1554 if (oldval == newval) {
1572 doc = (
struct doc_priv *) (nand + 1);
1581 nand->
cmd_ctrl = doc200x_hwcontrol;
1585 nand->
ecc.hwctl = doc200x_enable_hwecc;
1586 nand->
ecc.calculate = doc200x_calculate_ecc;
1587 nand->
ecc.correct = doc200x_correct_data;
1589 nand->
ecc.layout = &doc200x_oobinfo;
1591 nand->
ecc.size = 512;
1592 nand->
ecc.bytes = 6;
1593 nand->
ecc.strength = 2;
1606 numchips = doc2000_init(mtd);
1608 numchips = doc2001plus_init(mtd);
1610 numchips = doc2001_init(mtd);
1631 WriteDOC(save_control, virtadr, DOCControl);
1637 static void release_nanddoc(
void)
1643 for (mtd = doclist; mtd; mtd = nextmtd) {
1654 static int __init init_nanddoc(
void)
1673 if (doc_config_location) {
1674 printk(
KERN_INFO "Using configured DiskOnChip probe address 0x%lx\n", doc_config_location);
1675 ret = doc_probe(doc_config_location);
1679 for (i = 0; (doc_locations[
i] != 0xffffffff); i++) {
1680 doc_probe(doc_locations[i]);
1696 static void __exit cleanup_nanddoc(
void)
1712 MODULE_DESCRIPTION(
"M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver");