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