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