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