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