1 2/* 3 * Linux driver for Disk-On-Chip 2000 and Millennium 4 * (c) 1999 Machine Vision Holdings, Inc. 5 * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org> 6 */ 7 8#include <linux/kernel.h> 9#include <linux/module.h> 10#include <asm/errno.h> 11#include <asm/io.h> 12#include <asm/uaccess.h> 13#include <linux/delay.h> 14#include <linux/slab.h> 15#include <linux/sched.h> 16#include <linux/init.h> 17#include <linux/types.h> 18#include <linux/bitops.h> 19#include <linux/mutex.h> 20 21#include <linux/mtd/mtd.h> 22#include <linux/mtd/nand.h> 23#include <linux/mtd/doc2000.h> 24 25#define DOC_SUPPORT_2000 26#define DOC_SUPPORT_2000TSOP 27#define DOC_SUPPORT_MILLENNIUM 28 29#ifdef DOC_SUPPORT_2000 30#define DoC_is_2000(doc) (doc->ChipID == DOC_ChipID_Doc2k) 31#else 32#define DoC_is_2000(doc) (0) 33#endif 34 35#if defined(DOC_SUPPORT_2000TSOP) || defined(DOC_SUPPORT_MILLENNIUM) 36#define DoC_is_Millennium(doc) (doc->ChipID == DOC_ChipID_DocMil) 37#else 38#define DoC_is_Millennium(doc) (0) 39#endif 40 41/* #define ECC_DEBUG */ 42 43/* I have no idea why some DoC chips can not use memcpy_from|to_io(). 44 * This may be due to the different revisions of the ASIC controller built-in or 45 * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment 46 * this: 47 #undef USE_MEMCPY 48*/ 49 50static int doc_read(struct mtd_info *mtd, loff_t from, size_t len, 51 size_t *retlen, u_char *buf); 52static int doc_write(struct mtd_info *mtd, loff_t to, size_t len, 53 size_t *retlen, const u_char *buf); 54static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, 55 struct mtd_oob_ops *ops); 56static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, 57 struct mtd_oob_ops *ops); 58static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len, 59 size_t *retlen, const u_char *buf); 60static int doc_erase (struct mtd_info *mtd, struct erase_info *instr); 61 62static struct mtd_info *doc2klist = NULL; 63 64/* Perform the required delay cycles by reading from the appropriate register */ 65static void DoC_Delay(struct DiskOnChip *doc, unsigned short cycles) 66{ 67 volatile char dummy; 68 int i; 69 70 for (i = 0; i < cycles; i++) { 71 if (DoC_is_Millennium(doc)) 72 dummy = ReadDOC(doc->virtadr, NOP); 73 else 74 dummy = ReadDOC(doc->virtadr, DOCStatus); 75 } 76 77} 78 79/* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */ 80static int _DoC_WaitReady(struct DiskOnChip *doc) 81{ 82 void __iomem *docptr = doc->virtadr; 83 unsigned long timeo = jiffies + (HZ * 10); 84 85 DEBUG(MTD_DEBUG_LEVEL3, 86 "_DoC_WaitReady called for out-of-line wait\n"); 87 88 /* Out-of-line routine to wait for chip response */ 89 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) { 90 /* issue 2 read from NOP register after reading from CDSNControl register 91 see Software Requirement 11.4 item 2. */ 92 DoC_Delay(doc, 2); 93 94 if (time_after(jiffies, timeo)) { 95 DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n"); 96 return -EIO; 97 } 98 udelay(1); 99 cond_resched(); 100 } 101 102 return 0; 103} 104 105static inline int DoC_WaitReady(struct DiskOnChip *doc) 106{ 107 void __iomem *docptr = doc->virtadr; 108 109 /* This is inline, to optimise the common case, where it's ready instantly */ 110 int ret = 0; 111 112 /* 4 read form NOP register should be issued in prior to the read from CDSNControl 113 see Software Requirement 11.4 item 2. */ 114 DoC_Delay(doc, 4); 115 116 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) 117 /* Call the out-of-line routine to wait */ 118 ret = _DoC_WaitReady(doc); 119 120 /* issue 2 read from NOP register after reading from CDSNControl register 121 see Software Requirement 11.4 item 2. */ 122 DoC_Delay(doc, 2); 123 124 return ret; 125} 126 127/* DoC_Command: Send a flash command to the flash chip through the CDSN Slow IO register to 128 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is 129 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */ 130 131static int DoC_Command(struct DiskOnChip *doc, unsigned char command, 132 unsigned char xtraflags) 133{ 134 void __iomem *docptr = doc->virtadr; 135 136 if (DoC_is_2000(doc)) 137 xtraflags |= CDSN_CTRL_FLASH_IO; 138 139 /* Assert the CLE (Command Latch Enable) line to the flash chip */ 140 WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl); 141 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */ 142 143 if (DoC_is_Millennium(doc)) 144 WriteDOC(command, docptr, CDSNSlowIO); 145 146 /* Send the command */ 147 WriteDOC_(command, docptr, doc->ioreg); 148 if (DoC_is_Millennium(doc)) 149 WriteDOC(command, docptr, WritePipeTerm); 150 151 /* Lower the CLE line */ 152 WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl); 153 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */ 154 155 /* Wait for the chip to respond - Software requirement 11.4.1 (extended for any command) */ 156 return DoC_WaitReady(doc); 157} 158 159/* DoC_Address: Set the current address for the flash chip through the CDSN Slow IO register to 160 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is 161 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */ 162 163static int DoC_Address(struct DiskOnChip *doc, int numbytes, unsigned long ofs, 164 unsigned char xtraflags1, unsigned char xtraflags2) 165{ 166 int i; 167 void __iomem *docptr = doc->virtadr; 168 169 if (DoC_is_2000(doc)) 170 xtraflags1 |= CDSN_CTRL_FLASH_IO; 171 172 /* Assert the ALE (Address Latch Enable) line to the flash chip */ 173 WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl); 174 175 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */ 176 177 /* Send the address */ 178 /* Devices with 256-byte page are addressed as: 179 Column (bits 0-7), Page (bits 8-15, 16-23, 24-31) 180 * there is no device on the market with page256 181 and more than 24 bits. 182 Devices with 512-byte page are addressed as: 183 Column (bits 0-7), Page (bits 9-16, 17-24, 25-31) 184 * 25-31 is sent only if the chip support it. 185 * bit 8 changes the read command to be sent 186 (NAND_CMD_READ0 or NAND_CMD_READ1). 187 */ 188 189 if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE) { 190 if (DoC_is_Millennium(doc)) 191 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO); 192 WriteDOC_(ofs & 0xff, docptr, doc->ioreg); 193 } 194 195 if (doc->page256) { 196 ofs = ofs >> 8; 197 } else { 198 ofs = ofs >> 9; 199 } 200 201 if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE) { 202 for (i = 0; i < doc->pageadrlen; i++, ofs = ofs >> 8) { 203 if (DoC_is_Millennium(doc)) 204 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO); 205 WriteDOC_(ofs & 0xff, docptr, doc->ioreg); 206 } 207 } 208 209 if (DoC_is_Millennium(doc)) 210 WriteDOC(ofs & 0xff, docptr, WritePipeTerm); 211 212 DoC_Delay(doc, 2); /* Needed for some slow flash chips. mf. */ 213 214 215 /* Lower the ALE line */ 216 WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr, 217 CDSNControl); 218 219 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */ 220 221 /* Wait for the chip to respond - Software requirement 11.4.1 */ 222 return DoC_WaitReady(doc); 223} 224 225/* Read a buffer from DoC, taking care of Millennium odditys */ 226static void DoC_ReadBuf(struct DiskOnChip *doc, u_char * buf, int len) 227{ 228 volatile int dummy; 229 int modulus = 0xffff; 230 void __iomem *docptr = doc->virtadr; 231 int i; 232 233 if (len <= 0) 234 return; 235 236 if (DoC_is_Millennium(doc)) { 237 /* Read the data via the internal pipeline through CDSN IO register, 238 see Pipelined Read Operations 11.3 */ 239 dummy = ReadDOC(docptr, ReadPipeInit); 240 241 /* Millennium should use the LastDataRead register - Pipeline Reads */ 242 len--; 243 244 /* This is needed for correctly ECC calculation */ 245 modulus = 0xff; 246 } 247 248 for (i = 0; i < len; i++) 249 buf[i] = ReadDOC_(docptr, doc->ioreg + (i & modulus)); 250 251 if (DoC_is_Millennium(doc)) { 252 buf[i] = ReadDOC(docptr, LastDataRead); 253 } 254} 255 256/* Write a buffer to DoC, taking care of Millennium odditys */ 257static void DoC_WriteBuf(struct DiskOnChip *doc, const u_char * buf, int len) 258{ 259 void __iomem *docptr = doc->virtadr; 260 int i; 261 262 if (len <= 0) 263 return; 264 265 for (i = 0; i < len; i++) 266 WriteDOC_(buf[i], docptr, doc->ioreg + i); 267 268 if (DoC_is_Millennium(doc)) { 269 WriteDOC(0x00, docptr, WritePipeTerm); 270 } 271} 272 273 274/* DoC_SelectChip: Select a given flash chip within the current floor */ 275 276static inline int DoC_SelectChip(struct DiskOnChip *doc, int chip) 277{ 278 void __iomem *docptr = doc->virtadr; 279 280 /* Software requirement 11.4.4 before writing DeviceSelect */ 281 /* Deassert the CE line to eliminate glitches on the FCE# outputs */ 282 WriteDOC(CDSN_CTRL_WP, docptr, CDSNControl); 283 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */ 284 285 /* Select the individual flash chip requested */ 286 WriteDOC(chip, docptr, CDSNDeviceSelect); 287 DoC_Delay(doc, 4); 288 289 /* Reassert the CE line */ 290 WriteDOC(CDSN_CTRL_CE | CDSN_CTRL_FLASH_IO | CDSN_CTRL_WP, docptr, 291 CDSNControl); 292 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */ 293 294 /* Wait for it to be ready */ 295 return DoC_WaitReady(doc); 296} 297 298/* DoC_SelectFloor: Select a given floor (bank of flash chips) */ 299 300static inline int DoC_SelectFloor(struct DiskOnChip *doc, int floor) 301{ 302 void __iomem *docptr = doc->virtadr; 303 304 /* Select the floor (bank) of chips required */ 305 WriteDOC(floor, docptr, FloorSelect); 306 307 /* Wait for the chip to be ready */ 308 return DoC_WaitReady(doc); 309} 310 311/* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */ 312 313static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip) 314{ 315 int mfr, id, i, j; 316 volatile char dummy; 317 318 /* Page in the required floor/chip */ 319 DoC_SelectFloor(doc, floor); 320 DoC_SelectChip(doc, chip); 321 322 /* Reset the chip */ 323 if (DoC_Command(doc, NAND_CMD_RESET, CDSN_CTRL_WP)) { 324 DEBUG(MTD_DEBUG_LEVEL2, 325 "DoC_Command (reset) for %d,%d returned true\n", 326 floor, chip); 327 return 0; 328 } 329 330 331 /* Read the NAND chip ID: 1. Send ReadID command */ 332 if (DoC_Command(doc, NAND_CMD_READID, CDSN_CTRL_WP)) { 333 DEBUG(MTD_DEBUG_LEVEL2, 334 "DoC_Command (ReadID) for %d,%d returned true\n", 335 floor, chip); 336 return 0; 337 } 338 339 /* Read the NAND chip ID: 2. Send address byte zero */ 340 DoC_Address(doc, ADDR_COLUMN, 0, CDSN_CTRL_WP, 0); 341 342 /* Read the manufacturer and device id codes from the device */ 343 344 if (DoC_is_Millennium(doc)) { 345 DoC_Delay(doc, 2); 346 dummy = ReadDOC(doc->virtadr, ReadPipeInit); 347 mfr = ReadDOC(doc->virtadr, LastDataRead); 348 349 DoC_Delay(doc, 2); 350 dummy = ReadDOC(doc->virtadr, ReadPipeInit); 351 id = ReadDOC(doc->virtadr, LastDataRead); 352 } else { 353 /* CDSN Slow IO register see Software Req 11.4 item 5. */ 354 dummy = ReadDOC(doc->virtadr, CDSNSlowIO); 355 DoC_Delay(doc, 2); 356 mfr = ReadDOC_(doc->virtadr, doc->ioreg); 357 358 /* CDSN Slow IO register see Software Req 11.4 item 5. */ 359 dummy = ReadDOC(doc->virtadr, CDSNSlowIO); 360 DoC_Delay(doc, 2); 361 id = ReadDOC_(doc->virtadr, doc->ioreg); 362 } 363 364 /* No response - return failure */ 365 if (mfr == 0xff || mfr == 0) 366 return 0; 367 368 /* Check it's the same as the first chip we identified. 369 * M-Systems say that any given DiskOnChip device should only 370 * contain _one_ type of flash part, although that's not a 371 * hardware restriction. */ 372 if (doc->mfr) { 373 if (doc->mfr == mfr && doc->id == id) 374 return 1; /* This is the same as the first */ 375 else 376 printk(KERN_WARNING 377 "Flash chip at floor %d, chip %d is different:\n", 378 floor, chip); 379 } 380 381 /* Print and store the manufacturer and ID codes. */ 382 for (i = 0; nand_flash_ids[i].name != NULL; i++) { 383 if (id == nand_flash_ids[i].id) { 384 /* Try to identify manufacturer */ 385 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) { 386 if (nand_manuf_ids[j].id == mfr) 387 break; 388 } 389 printk(KERN_INFO 390 "Flash chip found: Manufacturer ID: %2.2X, " 391 "Chip ID: %2.2X (%s:%s)\n", mfr, id, 392 nand_manuf_ids[j].name, nand_flash_ids[i].name); 393 if (!doc->mfr) { 394 doc->mfr = mfr; 395 doc->id = id; 396 doc->chipshift = 397 ffs((nand_flash_ids[i].chipsize << 20)) - 1; 398 doc->page256 = (nand_flash_ids[i].pagesize == 256) ? 1 : 0; 399 doc->pageadrlen = doc->chipshift > 25 ? 3 : 2; 400 doc->erasesize = 401 nand_flash_ids[i].erasesize; 402 return 1; 403 } 404 return 0; 405 } 406 } 407 408 409 /* We haven't fully identified the chip. Print as much as we know. */ 410 printk(KERN_WARNING "Unknown flash chip found: %2.2X %2.2X\n", 411 id, mfr); 412 413 printk(KERN_WARNING "Please report to dwmw2@infradead.org\n"); 414 return 0; 415} 416 417/* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */ 418 419static void DoC_ScanChips(struct DiskOnChip *this, int maxchips) 420{ 421 int floor, chip; 422 int numchips[MAX_FLOORS]; 423 int ret = 1; 424 425 this->numchips = 0; 426 this->mfr = 0; 427 this->id = 0; 428 429 /* For each floor, find the number of valid chips it contains */ 430 for (floor = 0; floor < MAX_FLOORS; floor++) { 431 ret = 1; 432 numchips[floor] = 0; 433 for (chip = 0; chip < maxchips && ret != 0; chip++) { 434 435 ret = DoC_IdentChip(this, floor, chip); 436 if (ret) { 437 numchips[floor]++; 438 this->numchips++; 439 } 440 } 441 } 442 443 /* If there are none at all that we recognise, bail */ 444 if (!this->numchips) { 445 printk(KERN_NOTICE "No flash chips recognised.\n"); 446 return; 447 } 448 449 /* Allocate an array to hold the information for each chip */ 450 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL); 451 if (!this->chips) { 452 printk(KERN_NOTICE "No memory for allocating chip info structures\n"); 453 return; 454 } 455 456 ret = 0; 457 458 /* Fill out the chip array with {floor, chipno} for each 459 * detected chip in the device. */ 460 for (floor = 0; floor < MAX_FLOORS; floor++) { 461 for (chip = 0; chip < numchips[floor]; chip++) { 462 this->chips[ret].floor = floor; 463 this->chips[ret].chip = chip; 464 this->chips[ret].curadr = 0; 465 this->chips[ret].curmode = 0x50; 466 ret++; 467 } 468 } 469 470 /* Calculate and print the total size of the device */ 471 this->totlen = this->numchips * (1 << this->chipshift); 472 473 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n", 474 this->numchips, this->totlen >> 20); 475} 476 477static int DoC2k_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2) 478{ 479 int tmp1, tmp2, retval; 480 if (doc1->physadr == doc2->physadr) 481 return 1; 482 483 /* Use the alias resolution register which was set aside for this 484 * purpose. If it's value is the same on both chips, they might 485 * be the same chip, and we write to one and check for a change in 486 * the other. It's unclear if this register is usuable in the 487 * DoC 2000 (it's in the Millennium docs), but it seems to work. */ 488 tmp1 = ReadDOC(doc1->virtadr, AliasResolution); 489 tmp2 = ReadDOC(doc2->virtadr, AliasResolution); 490 if (tmp1 != tmp2) 491 return 0; 492 493 WriteDOC((tmp1 + 1) % 0xff, doc1->virtadr, AliasResolution); 494 tmp2 = ReadDOC(doc2->virtadr, AliasResolution); 495 if (tmp2 == (tmp1 + 1) % 0xff) 496 retval = 1; 497 else 498 retval = 0; 499 500 /* Restore register contents. May not be necessary, but do it just to 501 * be safe. */ 502 WriteDOC(tmp1, doc1->virtadr, AliasResolution); 503 504 return retval; 505} 506 507/* This routine is found from the docprobe code by symbol_get(), 508 * which will bump the use count of this module. */ 509void DoC2k_init(struct mtd_info *mtd) 510{ 511 struct DiskOnChip *this = mtd->priv; 512 struct DiskOnChip *old = NULL; 513 int maxchips; 514 515 /* We must avoid being called twice for the same device. */ 516 517 if (doc2klist) 518 old = doc2klist->priv; 519 520 while (old) { 521 if (DoC2k_is_alias(old, this)) { 522 printk(KERN_NOTICE 523 "Ignoring DiskOnChip 2000 at 0x%lX - already configured\n", 524 this->physadr); 525 iounmap(this->virtadr); 526 kfree(mtd); 527 return; 528 } 529 if (old->nextdoc) 530 old = old->nextdoc->priv; 531 else 532 old = NULL; 533 } 534 535 536 switch (this->ChipID) { 537 case DOC_ChipID_Doc2kTSOP: 538 mtd->name = "DiskOnChip 2000 TSOP"; 539 this->ioreg = DoC_Mil_CDSN_IO; 540 /* Pretend it's a Millennium */ 541 this->ChipID = DOC_ChipID_DocMil; 542 maxchips = MAX_CHIPS; 543 break; 544 case DOC_ChipID_Doc2k: 545 mtd->name = "DiskOnChip 2000"; 546 this->ioreg = DoC_2k_CDSN_IO; 547 maxchips = MAX_CHIPS; 548 break; 549 case DOC_ChipID_DocMil: 550 mtd->name = "DiskOnChip Millennium"; 551 this->ioreg = DoC_Mil_CDSN_IO; 552 maxchips = MAX_CHIPS_MIL; 553 break; 554 default: 555 printk("Unknown ChipID 0x%02x\n", this->ChipID); 556 kfree(mtd); 557 iounmap(this->virtadr); 558 return; 559 } 560 561 printk(KERN_NOTICE "%s found at address 0x%lX\n", mtd->name, 562 this->physadr); 563 564 mtd->type = MTD_NANDFLASH; 565 mtd->flags = MTD_CAP_NANDFLASH; 566 mtd->size = 0; 567 mtd->erasesize = 0; 568 mtd->writesize = 512; 569 mtd->oobsize = 16; 570 mtd->owner = THIS_MODULE; 571 mtd->erase = doc_erase; 572 mtd->point = NULL; 573 mtd->unpoint = NULL; 574 mtd->read = doc_read; 575 mtd->write = doc_write; 576 mtd->read_oob = doc_read_oob; 577 mtd->write_oob = doc_write_oob; 578 mtd->sync = NULL; 579 580 this->totlen = 0; 581 this->numchips = 0; 582 583 this->curfloor = -1; 584 this->curchip = -1; 585 mutex_init(&this->lock); 586 587 /* Ident all the chips present. */ 588 DoC_ScanChips(this, maxchips); 589 590 if (!this->totlen) { 591 kfree(mtd); 592 iounmap(this->virtadr); 593 } else { 594 this->nextdoc = doc2klist; 595 doc2klist = mtd; 596 mtd->size = this->totlen; 597 mtd->erasesize = this->erasesize; 598 add_mtd_device(mtd); 599 return; 600 } 601} 602EXPORT_SYMBOL_GPL(DoC2k_init); 603 604static int doc_read(struct mtd_info *mtd, loff_t from, size_t len, 605 size_t * retlen, u_char * buf) 606{ 607 struct DiskOnChip *this = mtd->priv; 608 void __iomem *docptr = this->virtadr; 609 struct Nand *mychip; 610 unsigned char syndrome[6], eccbuf[6]; 611 volatile char dummy; 612 int i, len256 = 0, ret=0; 613 size_t left = len; 614 615 /* Don't allow read past end of device */ 616 if (from >= this->totlen) 617 return -EINVAL; 618 619 mutex_lock(&this->lock); 620 621 *retlen = 0; 622 while (left) { 623 len = left; 624 625 /* Don't allow a single read to cross a 512-byte block boundary */ 626 if (from + len > ((from | 0x1ff) + 1)) 627 len = ((from | 0x1ff) + 1) - from; 628 629 /* The ECC will not be calculated correctly if less than 512 is read */ 630 if (len != 0x200) 631 printk(KERN_WARNING 632 "ECC needs a full sector read (adr: %lx size %lx)\n", 633 (long) from, (long) len); 634 635 /* printk("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len); */ 636 637 638 /* Find the chip which is to be used and select it */ 639 mychip = &this->chips[from >> (this->chipshift)]; 640 641 if (this->curfloor != mychip->floor) { 642 DoC_SelectFloor(this, mychip->floor); 643 DoC_SelectChip(this, mychip->chip); 644 } else if (this->curchip != mychip->chip) { 645 DoC_SelectChip(this, mychip->chip); 646 } 647 648 this->curfloor = mychip->floor; 649 this->curchip = mychip->chip; 650 651 DoC_Command(this, 652 (!this->page256 653 && (from & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0, 654 CDSN_CTRL_WP); 655 DoC_Address(this, ADDR_COLUMN_PAGE, from, CDSN_CTRL_WP, 656 CDSN_CTRL_ECC_IO); 657 658 /* Prime the ECC engine */ 659 WriteDOC(DOC_ECC_RESET, docptr, ECCConf); 660 WriteDOC(DOC_ECC_EN, docptr, ECCConf); 661 662 /* treat crossing 256-byte sector for 2M x 8bits devices */ 663 if (this->page256 && from + len > (from | 0xff) + 1) { 664 len256 = (from | 0xff) + 1 - from; 665 DoC_ReadBuf(this, buf, len256); 666 667 DoC_Command(this, NAND_CMD_READ0, CDSN_CTRL_WP); 668 DoC_Address(this, ADDR_COLUMN_PAGE, from + len256, 669 CDSN_CTRL_WP, CDSN_CTRL_ECC_IO); 670 } 671 672 DoC_ReadBuf(this, &buf[len256], len - len256); 673 674 /* Let the caller know we completed it */ 675 *retlen += len; 676 677 /* Read the ECC data through the DiskOnChip ECC logic */ 678 /* Note: this will work even with 2M x 8bit devices as */ 679 /* they have 8 bytes of OOB per 256 page. mf. */ 680 DoC_ReadBuf(this, eccbuf, 6); 681 682 /* Flush the pipeline */ 683 if (DoC_is_Millennium(this)) { 684 dummy = ReadDOC(docptr, ECCConf); 685 dummy = ReadDOC(docptr, ECCConf); 686 i = ReadDOC(docptr, ECCConf); 687 } else { 688 dummy = ReadDOC(docptr, 2k_ECCStatus); 689 dummy = ReadDOC(docptr, 2k_ECCStatus); 690 i = ReadDOC(docptr, 2k_ECCStatus); 691 } 692 693 /* Check the ECC Status */ 694 if (i & 0x80) { 695 int nb_errors; 696 /* There was an ECC error */ 697#ifdef ECC_DEBUG 698 printk(KERN_ERR "DiskOnChip ECC Error: Read at %lx\n", (long)from); 699#endif 700 /* Read the ECC syndrom through the DiskOnChip ECC 701 logic. These syndrome will be all ZERO when there 702 is no error */ 703 for (i = 0; i < 6; i++) { 704 syndrome[i] = 705 ReadDOC(docptr, ECCSyndrome0 + i); 706 } 707 nb_errors = doc_decode_ecc(buf, syndrome); 708 709#ifdef ECC_DEBUG 710 printk(KERN_ERR "Errors corrected: %x\n", nb_errors); 711#endif 712 if (nb_errors < 0) { 713 /* We return error, but have actually done the 714 read. Not that this can be told to 715 user-space, via sys_read(), but at least 716 MTD-aware stuff can know about it by 717 checking *retlen */ 718 ret = -EIO; 719 } 720 } 721 722#ifdef PSYCHO_DEBUG 723 printk(KERN_DEBUG "ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n", 724 (long)from, eccbuf[0], eccbuf[1], eccbuf[2], 725 eccbuf[3], eccbuf[4], eccbuf[5]); 726#endif 727 728 /* disable the ECC engine */ 729 WriteDOC(DOC_ECC_DIS, docptr , ECCConf); 730 731 /* according to 11.4.1, we need to wait for the busy line 732 * drop if we read to the end of the page. */ 733 if(0 == ((from + len) & 0x1ff)) 734 { 735 DoC_WaitReady(this); 736 } 737 738 from += len; 739 left -= len; 740 buf += len; 741 } 742 743 mutex_unlock(&this->lock); 744 745 return ret; 746} 747 748static int doc_write(struct mtd_info *mtd, loff_t to, size_t len, 749 size_t * retlen, const u_char * buf) 750{ 751 struct DiskOnChip *this = mtd->priv; 752 int di; /* Yes, DI is a hangover from when I was disassembling the binary driver */ 753 void __iomem *docptr = this->virtadr; 754 unsigned char eccbuf[6]; 755 volatile char dummy; 756 int len256 = 0; 757 struct Nand *mychip; 758 size_t left = len; 759 int status; 760 761 /* Don't allow write past end of device */ 762 if (to >= this->totlen) 763 return -EINVAL; 764 765 mutex_lock(&this->lock); 766 767 *retlen = 0; 768 while (left) { 769 len = left; 770 771 /* Don't allow a single write to cross a 512-byte block boundary */ 772 if (to + len > ((to | 0x1ff) + 1)) 773 len = ((to | 0x1ff) + 1) - to; 774 775 /* The ECC will not be calculated correctly if less than 512 is written */ 776/* DBB- 777 if (len != 0x200 && eccbuf) 778 printk(KERN_WARNING 779 "ECC needs a full sector write (adr: %lx size %lx)\n", 780 (long) to, (long) len); 781 -DBB */ 782 783 /* printk("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */ 784 785 /* Find the chip which is to be used and select it */ 786 mychip = &this->chips[to >> (this->chipshift)]; 787 788 if (this->curfloor != mychip->floor) { 789 DoC_SelectFloor(this, mychip->floor); 790 DoC_SelectChip(this, mychip->chip); 791 } else if (this->curchip != mychip->chip) { 792 DoC_SelectChip(this, mychip->chip); 793 } 794 795 this->curfloor = mychip->floor; 796 this->curchip = mychip->chip; 797 798 /* Set device to main plane of flash */ 799 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP); 800 DoC_Command(this, 801 (!this->page256 802 && (to & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0, 803 CDSN_CTRL_WP); 804 805 DoC_Command(this, NAND_CMD_SEQIN, 0); 806 DoC_Address(this, ADDR_COLUMN_PAGE, to, 0, CDSN_CTRL_ECC_IO); 807 808 /* Prime the ECC engine */ 809 WriteDOC(DOC_ECC_RESET, docptr, ECCConf); 810 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf); 811 812 /* treat crossing 256-byte sector for 2M x 8bits devices */ 813 if (this->page256 && to + len > (to | 0xff) + 1) { 814 len256 = (to | 0xff) + 1 - to; 815 DoC_WriteBuf(this, buf, len256); 816 817 DoC_Command(this, NAND_CMD_PAGEPROG, 0); 818 819 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP); 820 /* There's an implicit DoC_WaitReady() in DoC_Command */ 821 822 dummy = ReadDOC(docptr, CDSNSlowIO); 823 DoC_Delay(this, 2); 824 825 if (ReadDOC_(docptr, this->ioreg) & 1) { 826 printk(KERN_ERR "Error programming flash\n"); 827 /* Error in programming */ 828 *retlen = 0; 829 mutex_unlock(&this->lock); 830 return -EIO; 831 } 832 833 DoC_Command(this, NAND_CMD_SEQIN, 0); 834 DoC_Address(this, ADDR_COLUMN_PAGE, to + len256, 0, 835 CDSN_CTRL_ECC_IO); 836 } 837 838 DoC_WriteBuf(this, &buf[len256], len - len256); 839 840 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_CE, docptr, CDSNControl); 841 842 if (DoC_is_Millennium(this)) { 843 WriteDOC(0, docptr, NOP); 844 WriteDOC(0, docptr, NOP); 845 WriteDOC(0, docptr, NOP); 846 } else { 847 WriteDOC_(0, docptr, this->ioreg); 848 WriteDOC_(0, docptr, this->ioreg); 849 WriteDOC_(0, docptr, this->ioreg); 850 } 851 852 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_FLASH_IO | CDSN_CTRL_CE, docptr, 853 CDSNControl); 854 855 /* Read the ECC data through the DiskOnChip ECC logic */ 856 for (di = 0; di < 6; di++) { 857 eccbuf[di] = ReadDOC(docptr, ECCSyndrome0 + di); 858 } 859 860 /* Reset the ECC engine */ 861 WriteDOC(DOC_ECC_DIS, docptr, ECCConf); 862 863#ifdef PSYCHO_DEBUG 864 printk 865 ("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n", 866 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3], 867 eccbuf[4], eccbuf[5]); 868#endif 869 DoC_Command(this, NAND_CMD_PAGEPROG, 0); 870 871 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP); 872 /* There's an implicit DoC_WaitReady() in DoC_Command */ 873 874 if (DoC_is_Millennium(this)) { 875 ReadDOC(docptr, ReadPipeInit); 876 status = ReadDOC(docptr, LastDataRead); 877 } else { 878 dummy = ReadDOC(docptr, CDSNSlowIO); 879 DoC_Delay(this, 2); 880 status = ReadDOC_(docptr, this->ioreg); 881 } 882 883 if (status & 1) { 884 printk(KERN_ERR "Error programming flash\n"); 885 /* Error in programming */ 886 *retlen = 0; 887 mutex_unlock(&this->lock); 888 return -EIO; 889 } 890 891 /* Let the caller know we completed it */ 892 *retlen += len; 893 894 { 895 unsigned char x[8]; 896 size_t dummy; 897 int ret; 898 899 /* Write the ECC data to flash */ 900 for (di=0; di<6; di++) 901 x[di] = eccbuf[di]; 902 903 x[6]=0x55; 904 x[7]=0x55; 905 906 ret = doc_write_oob_nolock(mtd, to, 8, &dummy, x); 907 if (ret) { 908 mutex_unlock(&this->lock); 909 return ret; 910 } 911 } 912 913 to += len; 914 left -= len; 915 buf += len; 916 } 917 918 mutex_unlock(&this->lock); 919 return 0; 920} 921 922static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, 923 struct mtd_oob_ops *ops) 924{ 925 struct DiskOnChip *this = mtd->priv; 926 int len256 = 0, ret; 927 struct Nand *mychip; 928 uint8_t *buf = ops->oobbuf; 929 size_t len = ops->len; 930 931 BUG_ON(ops->mode != MTD_OOB_PLACE); 932 933 ofs += ops->ooboffs; 934 935 mutex_lock(&this->lock); 936 937 mychip = &this->chips[ofs >> this->chipshift]; 938 939 if (this->curfloor != mychip->floor) { 940 DoC_SelectFloor(this, mychip->floor); 941 DoC_SelectChip(this, mychip->chip); 942 } else if (this->curchip != mychip->chip) { 943 DoC_SelectChip(this, mychip->chip); 944 } 945 this->curfloor = mychip->floor; 946 this->curchip = mychip->chip; 947 948 /* update address for 2M x 8bit devices. OOB starts on the second */ 949 /* page to maintain compatibility with doc_read_ecc. */ 950 if (this->page256) { 951 if (!(ofs & 0x8)) 952 ofs += 0x100; 953 else 954 ofs -= 0x8; 955 } 956 957 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP); 958 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, CDSN_CTRL_WP, 0); 959 960 /* treat crossing 8-byte OOB data for 2M x 8bit devices */ 961 /* Note: datasheet says it should automaticaly wrap to the */ 962 /* next OOB block, but it didn't work here. mf. */ 963 if (this->page256 && ofs + len > (ofs | 0x7) + 1) { 964 len256 = (ofs | 0x7) + 1 - ofs; 965 DoC_ReadBuf(this, buf, len256); 966 967 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP); 968 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), 969 CDSN_CTRL_WP, 0); 970 } 971 972 DoC_ReadBuf(this, &buf[len256], len - len256); 973 974 ops->retlen = len; 975 /* Reading the full OOB data drops us off of the end of the page, 976 * causing the flash device to go into busy mode, so we need 977 * to wait until ready 11.4.1 and Toshiba TC58256FT docs */ 978 979 ret = DoC_WaitReady(this); 980 981 mutex_unlock(&this->lock); 982 return ret; 983 984} 985 986static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len, 987 size_t * retlen, const u_char * buf) 988{ 989 struct DiskOnChip *this = mtd->priv; 990 int len256 = 0; 991 void __iomem *docptr = this->virtadr; 992 struct Nand *mychip = &this->chips[ofs >> this->chipshift]; 993 volatile int dummy; 994 int status; 995 996 // printk("doc_write_oob(%lx, %d): %2.2X %2.2X %2.2X %2.2X ... %2.2X %2.2X .. %2.2X %2.2X\n",(long)ofs, len, 997 // buf[0], buf[1], buf[2], buf[3], buf[8], buf[9], buf[14],buf[15]); 998 999 /* Find the chip which is to be used and select it */ 1000 if (this->curfloor != mychip->floor) { 1001 DoC_SelectFloor(this, mychip->floor); 1002 DoC_SelectChip(this, mychip->chip); 1003 } else if (this->curchip != mychip->chip) { 1004 DoC_SelectChip(this, mychip->chip); 1005 } 1006 this->curfloor = mychip->floor; 1007 this->curchip = mychip->chip; 1008 1009 /* disable the ECC engine */ 1010 WriteDOC (DOC_ECC_RESET, docptr, ECCConf); 1011 WriteDOC (DOC_ECC_DIS, docptr, ECCConf); 1012 1013 /* Reset the chip, see Software Requirement 11.4 item 1. */ 1014 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP); 1015 1016 /* issue the Read2 command to set the pointer to the Spare Data Area. */ 1017 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP); 1018 1019 /* update address for 2M x 8bit devices. OOB starts on the second */ 1020 /* page to maintain compatibility with doc_read_ecc. */ 1021 if (this->page256) { 1022 if (!(ofs & 0x8)) 1023 ofs += 0x100; 1024 else 1025 ofs -= 0x8; 1026 } 1027 1028 /* issue the Serial Data In command to initial the Page Program process */ 1029 DoC_Command(this, NAND_CMD_SEQIN, 0); 1030 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, 0, 0); 1031 1032 /* treat crossing 8-byte OOB data for 2M x 8bit devices */ 1033 /* Note: datasheet says it should automaticaly wrap to the */ 1034 /* next OOB block, but it didn't work here. mf. */ 1035 if (this->page256 && ofs + len > (ofs | 0x7) + 1) { 1036 len256 = (ofs | 0x7) + 1 - ofs; 1037 DoC_WriteBuf(this, buf, len256); 1038 1039 DoC_Command(this, NAND_CMD_PAGEPROG, 0); 1040 DoC_Command(this, NAND_CMD_STATUS, 0); 1041 /* DoC_WaitReady() is implicit in DoC_Command */ 1042 1043 if (DoC_is_Millennium(this)) { 1044 ReadDOC(docptr, ReadPipeInit); 1045 status = ReadDOC(docptr, LastDataRead); 1046 } else { 1047 dummy = ReadDOC(docptr, CDSNSlowIO); 1048 DoC_Delay(this, 2); 1049 status = ReadDOC_(docptr, this->ioreg); 1050 } 1051 1052 if (status & 1) { 1053 printk(KERN_ERR "Error programming oob data\n"); 1054 /* There was an error */ 1055 *retlen = 0; 1056 return -EIO; 1057 } 1058 DoC_Command(this, NAND_CMD_SEQIN, 0); 1059 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), 0, 0); 1060 } 1061 1062 DoC_WriteBuf(this, &buf[len256], len - len256); 1063 1064 DoC_Command(this, NAND_CMD_PAGEPROG, 0); 1065 DoC_Command(this, NAND_CMD_STATUS, 0); 1066 /* DoC_WaitReady() is implicit in DoC_Command */ 1067 1068 if (DoC_is_Millennium(this)) { 1069 ReadDOC(docptr, ReadPipeInit); 1070 status = ReadDOC(docptr, LastDataRead); 1071 } else { 1072 dummy = ReadDOC(docptr, CDSNSlowIO); 1073 DoC_Delay(this, 2); 1074 status = ReadDOC_(docptr, this->ioreg); 1075 } 1076 1077 if (status & 1) { 1078 printk(KERN_ERR "Error programming oob data\n"); 1079 /* There was an error */ 1080 *retlen = 0; 1081 return -EIO; 1082 } 1083 1084 *retlen = len; 1085 return 0; 1086 1087} 1088 1089static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, 1090 struct mtd_oob_ops *ops) 1091{ 1092 struct DiskOnChip *this = mtd->priv; 1093 int ret; 1094 1095 BUG_ON(ops->mode != MTD_OOB_PLACE); 1096 1097 mutex_lock(&this->lock); 1098 ret = doc_write_oob_nolock(mtd, ofs + ops->ooboffs, ops->len, 1099 &ops->retlen, ops->oobbuf); 1100 1101 mutex_unlock(&this->lock); 1102 return ret; 1103} 1104 1105static int doc_erase(struct mtd_info *mtd, struct erase_info *instr) 1106{ 1107 struct DiskOnChip *this = mtd->priv; 1108 __u32 ofs = instr->addr; 1109 __u32 len = instr->len; 1110 volatile int dummy; 1111 void __iomem *docptr = this->virtadr; 1112 struct Nand *mychip; 1113 int status; 1114 1115 mutex_lock(&this->lock); 1116 1117 if (ofs & (mtd->erasesize-1) || len & (mtd->erasesize-1)) { 1118 mutex_unlock(&this->lock); 1119 return -EINVAL; 1120 } 1121 1122 instr->state = MTD_ERASING; 1123 1124 while(len) { 1125 mychip = &this->chips[ofs >> this->chipshift]; 1126 1127 if (this->curfloor != mychip->floor) { 1128 DoC_SelectFloor(this, mychip->floor); 1129 DoC_SelectChip(this, mychip->chip); 1130 } else if (this->curchip != mychip->chip) { 1131 DoC_SelectChip(this, mychip->chip); 1132 } 1133 this->curfloor = mychip->floor; 1134 this->curchip = mychip->chip; 1135 1136 DoC_Command(this, NAND_CMD_ERASE1, 0); 1137 DoC_Address(this, ADDR_PAGE, ofs, 0, 0); 1138 DoC_Command(this, NAND_CMD_ERASE2, 0); 1139 1140 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP); 1141 1142 if (DoC_is_Millennium(this)) { 1143 ReadDOC(docptr, ReadPipeInit); 1144 status = ReadDOC(docptr, LastDataRead); 1145 } else { 1146 dummy = ReadDOC(docptr, CDSNSlowIO); 1147 DoC_Delay(this, 2); 1148 status = ReadDOC_(docptr, this->ioreg); 1149 } 1150 1151 if (status & 1) { 1152 printk(KERN_ERR "Error erasing at 0x%x\n", ofs); 1153 /* There was an error */ 1154 instr->state = MTD_ERASE_FAILED; 1155 goto callback; 1156 } 1157 ofs += mtd->erasesize; 1158 len -= mtd->erasesize; 1159 } 1160 instr->state = MTD_ERASE_DONE; 1161 1162 callback: 1163 mtd_erase_callback(instr); 1164 1165 mutex_unlock(&this->lock); 1166 return 0; 1167} 1168 1169 1170/**************************************************************************** 1171 * 1172 * Module stuff 1173 * 1174 ****************************************************************************/ 1175 1176static void __exit cleanup_doc2000(void) 1177{ 1178 struct mtd_info *mtd; 1179 struct DiskOnChip *this; 1180 1181 while ((mtd = doc2klist)) { 1182 this = mtd->priv; 1183 doc2klist = this->nextdoc; 1184 1185 del_mtd_device(mtd); 1186 1187 iounmap(this->virtadr); 1188 kfree(this->chips); 1189 kfree(mtd); 1190 } 1191} 1192 1193module_exit(cleanup_doc2000); 1194 1195MODULE_LICENSE("GPL"); 1196MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al."); 1197MODULE_DESCRIPTION("MTD driver for DiskOnChip 2000 and Millennium"); 1198