2 * Driver for Disk-On-Chip 2000 and Millennium
3 * (c) 1999 Machine Vision Holdings, Inc.
4 * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
6 * $Id: doc2000.c,v 1.46 2001/10/02 15:05:13 dwmw2 Exp $
15 #ifdef CONFIG_SHOW_BOOT_PROGRESS
16 # include <status_led.h>
17 # define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
19 # define SHOW_BOOT_PROGRESS(arg)
22 #if (CONFIG_COMMANDS & CFG_CMD_DOC)
24 #include <linux/mtd/nand.h>
25 #include <linux/mtd/nand_ids.h>
26 #include <linux/mtd/doc2000.h>
27 #include <linux/mtd/nftl.h>
29 #ifdef CFG_DOC_SUPPORT_2000
30 #define DoC_is_2000(doc) (doc->ChipID == DOC_ChipID_Doc2k)
32 #define DoC_is_2000(doc) (0)
35 #ifdef CFG_DOC_SUPPORT_MILLENNIUM
36 #define DoC_is_Millennium(doc) (doc->ChipID == DOC_ChipID_DocMil)
38 #define DoC_is_Millennium(doc) (0)
41 /* CFG_DOC_PASSIVE_PROBE:
42 In order to ensure that the BIOS checksum is correct at boot time, and
43 hence that the onboard BIOS extension gets executed, the DiskOnChip
44 goes into reset mode when it is read sequentially: all registers
45 return 0xff until the chip is woken up again by writing to the
48 Unfortunately, this means that the probe for the DiskOnChip is unsafe,
49 because one of the first things it does is write to where it thinks
50 the DOCControl register should be - which may well be shared memory
51 for another device. I've had machines which lock up when this is
52 attempted. Hence the possibility to do a passive probe, which will fail
53 to detect a chip in reset mode, but is at least guaranteed not to lock
56 If you have this problem, uncomment the following line:
57 #define CFG_DOC_PASSIVE_PROBE
65 static struct DiskOnChip doc_dev_desc[CFG_MAX_DOC_DEVICE];
67 /* Current DOC Device */
68 static int curr_device = -1;
70 /* ------------------------------------------------------------------------- */
72 int do_doc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
79 printf ("Usage:\n%s\n", cmdtp->usage);
82 if (strcmp(argv[1],"info") == 0) {
87 for (i=0; i<CFG_MAX_DOC_DEVICE; ++i) {
88 if(doc_dev_desc[i].ChipID == DOC_ChipID_UNKNOWN)
89 continue; /* list only known devices */
90 printf ("Device %d: ", i);
91 doc_print(&doc_dev_desc[i]);
95 } else if (strcmp(argv[1],"device") == 0) {
96 if ((curr_device < 0) || (curr_device >= CFG_MAX_DOC_DEVICE)) {
97 puts ("\nno devices available\n");
100 printf ("\nDevice %d: ", curr_device);
101 doc_print(&doc_dev_desc[curr_device]);
104 printf ("Usage:\n%s\n", cmdtp->usage);
107 if (strcmp(argv[1],"device") == 0) {
108 int dev = (int)simple_strtoul(argv[2], NULL, 10);
110 printf ("\nDevice %d: ", dev);
111 if (dev >= CFG_MAX_DOC_DEVICE) {
112 puts ("unknown device\n");
115 doc_print(&doc_dev_desc[dev]);
118 if (doc_dev_desc[dev].ChipID == DOC_ChipID_UNKNOWN) {
124 puts ("... is now current device\n");
129 printf ("Usage:\n%s\n", cmdtp->usage);
132 /* at least 4 args */
134 if (strcmp(argv[1],"read") == 0 || strcmp(argv[1],"write") == 0) {
135 ulong addr = simple_strtoul(argv[2], NULL, 16);
136 ulong off = simple_strtoul(argv[3], NULL, 16);
137 ulong size = simple_strtoul(argv[4], NULL, 16);
138 int cmd = (strcmp(argv[1],"read") == 0);
141 printf ("\nDOC %s: device %d offset %ld, size %ld ... ",
142 cmd ? "read" : "write", curr_device, off, size);
144 ret = doc_rw(doc_dev_desc + curr_device, cmd, off, size,
145 &total, (u_char*)addr);
147 printf ("%d bytes %s: %s\n", total, cmd ? "read" : "write",
148 ret ? "ERROR" : "OK");
151 } else if (strcmp(argv[1],"erase") == 0) {
152 ulong off = simple_strtoul(argv[2], NULL, 16);
153 ulong size = simple_strtoul(argv[3], NULL, 16);
156 printf ("\nDOC erase: device %d offset %ld, size %ld ... ",
157 curr_device, off, size);
159 ret = doc_erase (doc_dev_desc + curr_device, off, size);
161 printf("%s\n", ret ? "ERROR" : "OK");
165 printf ("Usage:\n%s\n", cmdtp->usage);
173 int do_docboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
175 char *boot_device = NULL;
186 addr = CFG_LOAD_ADDR;
187 boot_device = getenv ("bootdevice");
190 addr = simple_strtoul(argv[1], NULL, 16);
191 boot_device = getenv ("bootdevice");
194 addr = simple_strtoul(argv[1], NULL, 16);
195 boot_device = argv[2];
198 addr = simple_strtoul(argv[1], NULL, 16);
199 boot_device = argv[2];
200 offset = simple_strtoul(argv[3], NULL, 16);
203 printf ("Usage:\n%s\n", cmdtp->usage);
204 SHOW_BOOT_PROGRESS (-1);
209 puts ("\n** No boot device **\n");
210 SHOW_BOOT_PROGRESS (-1);
214 dev = simple_strtoul(boot_device, &ep, 16);
216 if ((dev >= CFG_MAX_DOC_DEVICE) ||
217 (doc_dev_desc[dev].ChipID == DOC_ChipID_UNKNOWN)) {
218 printf ("\n** Device %d not available\n", dev);
219 SHOW_BOOT_PROGRESS (-1);
223 printf ("\nLoading from device %d: %s at 0x%lX (offset 0x%lX)\n",
224 dev, doc_dev_desc[dev].name, doc_dev_desc[dev].physadr,
227 if (doc_rw (doc_dev_desc + dev, 1, offset,
228 SECTORSIZE, NULL, (u_char *)addr)) {
229 printf ("** Read error on %d\n", dev);
230 SHOW_BOOT_PROGRESS (-1);
234 hdr = (image_header_t *)addr;
236 if (hdr->ih_magic == IH_MAGIC) {
238 print_image_hdr (hdr);
240 cnt = (hdr->ih_size + sizeof(image_header_t));
243 puts ("\n** Bad Magic Number **\n");
244 SHOW_BOOT_PROGRESS (-1);
248 if (doc_rw (doc_dev_desc + dev, 1, offset + SECTORSIZE, cnt,
249 NULL, (u_char *)(addr+SECTORSIZE))) {
250 printf ("** Read error on %d\n", dev);
251 SHOW_BOOT_PROGRESS (-1);
255 /* Loading ok, update default load address */
259 /* Check if we should attempt an auto-start */
260 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
262 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
264 local_args[0] = argv[0];
265 local_args[1] = NULL;
267 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
269 do_bootm (cmdtp, 0, 1, local_args);
275 int doc_rw (struct DiskOnChip* this, int cmd,
276 loff_t from, size_t len,
277 size_t * retlen, u_char * buf)
279 int noecc, ret = 0, n, total = 0;
283 /* The ECC will not be calculated correctly if
284 less than 512 is written or read */
285 noecc = (from != (from | 0x1ff) + 1) || (len < 0x200);
288 ret = doc_read_ecc(this, from, len,
290 noecc ? NULL : eccbuf);
292 ret = doc_write_ecc(this, from, len,
294 noecc ? NULL : eccbuf);
311 void doc_print(struct DiskOnChip *this) {
312 printf("%s at 0x%lX,\n"
313 "\t %d chip%s %s, size %d MB, \n"
314 "\t total size %ld MB, sector size %ld kB\n",
315 this->name, this->physadr, this->numchips,
316 this->numchips>1 ? "s" : "", this->chips_name,
317 1 << (this->chipshift - 20),
318 this->totlen >> 20, this->erasesize >> 10);
320 if (this->nftl_found) {
321 struct NFTLrecord *nftl = &this->nftl;
322 unsigned long bin_size, flash_size;
324 bin_size = nftl->nb_boot_blocks * this->erasesize;
325 flash_size = (nftl->nb_blocks - nftl->nb_boot_blocks) * this->erasesize;
327 printf("\t NFTL boot record:\n"
328 "\t Binary partition: size %ld%s\n"
329 "\t Flash disk partition: size %ld%s, offset 0x%lx\n",
330 bin_size > (1 << 20) ? bin_size >> 20 : bin_size >> 10,
331 bin_size > (1 << 20) ? "MB" : "kB",
332 flash_size > (1 << 20) ? flash_size >> 20 : flash_size >> 10,
333 flash_size > (1 << 20) ? "MB" : "kB", bin_size);
335 puts ("\t No NFTL boot record found.\n");
339 /* ------------------------------------------------------------------------- */
341 /* This function is needed to avoid calls of the __ashrdi3 function. */
342 static int shr(int val, int shift) {
346 /* Perform the required delay cycles by reading from the appropriate register */
347 static void DoC_Delay(struct DiskOnChip *doc, unsigned short cycles)
352 for (i = 0; i < cycles; i++) {
353 if (DoC_is_Millennium(doc))
354 dummy = ReadDOC(doc->virtadr, NOP);
356 dummy = ReadDOC(doc->virtadr, DOCStatus);
361 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
362 static int _DoC_WaitReady(struct DiskOnChip *doc)
364 unsigned long docptr = doc->virtadr;
365 unsigned long start = get_timer(0);
368 puts ("_DoC_WaitReady called for out-of-line wait\n");
371 /* Out-of-line routine to wait for chip response */
372 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
373 #ifdef CFG_DOC_SHORT_TIMEOUT
374 /* it seems that after a certain time the DoC deasserts
375 * the CDSN_CTRL_FR_B although it is not ready...
376 * using a short timout solve this (timer increments every ms) */
377 if (get_timer(start) > 10) {
381 if (get_timer(start) > 10 * 1000) {
382 puts ("_DoC_WaitReady timed out.\n");
392 static int DoC_WaitReady(struct DiskOnChip *doc)
394 unsigned long docptr = doc->virtadr;
395 /* This is inline, to optimise the common case, where it's ready instantly */
398 /* 4 read form NOP register should be issued in prior to the read from CDSNControl
399 see Software Requirement 11.4 item 2. */
402 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
403 /* Call the out-of-line routine to wait */
404 ret = _DoC_WaitReady(doc);
406 /* issue 2 read from NOP register after reading from CDSNControl register
407 see Software Requirement 11.4 item 2. */
413 /* DoC_Command: Send a flash command to the flash chip through the CDSN Slow IO register to
414 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
415 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
417 static inline int DoC_Command(struct DiskOnChip *doc, unsigned char command,
418 unsigned char xtraflags)
420 unsigned long docptr = doc->virtadr;
422 if (DoC_is_2000(doc))
423 xtraflags |= CDSN_CTRL_FLASH_IO;
425 /* Assert the CLE (Command Latch Enable) line to the flash chip */
426 WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
427 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
429 if (DoC_is_Millennium(doc))
430 WriteDOC(command, docptr, CDSNSlowIO);
432 /* Send the command */
433 WriteDOC_(command, docptr, doc->ioreg);
435 /* Lower the CLE line */
436 WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
437 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
439 /* Wait for the chip to respond - Software requirement 11.4.1 (extended for any command) */
440 return DoC_WaitReady(doc);
443 /* DoC_Address: Set the current address for the flash chip through the CDSN Slow IO register to
444 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
445 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
447 static int DoC_Address(struct DiskOnChip *doc, int numbytes, unsigned long ofs,
448 unsigned char xtraflags1, unsigned char xtraflags2)
450 unsigned long docptr;
453 docptr = doc->virtadr;
455 if (DoC_is_2000(doc))
456 xtraflags1 |= CDSN_CTRL_FLASH_IO;
458 /* Assert the ALE (Address Latch Enable) line to the flash chip */
459 WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
461 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
463 /* Send the address */
464 /* Devices with 256-byte page are addressed as:
465 Column (bits 0-7), Page (bits 8-15, 16-23, 24-31)
466 * there is no device on the market with page256
467 and more than 24 bits.
468 Devices with 512-byte page are addressed as:
469 Column (bits 0-7), Page (bits 9-16, 17-24, 25-31)
470 * 25-31 is sent only if the chip support it.
471 * bit 8 changes the read command to be sent
472 (NAND_CMD_READ0 or NAND_CMD_READ1).
475 if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE) {
476 if (DoC_is_Millennium(doc))
477 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
478 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
487 if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE) {
488 for (i = 0; i < doc->pageadrlen; i++, ofs = ofs >> 8) {
489 if (DoC_is_Millennium(doc))
490 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
491 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
495 DoC_Delay(doc, 2); /* Needed for some slow flash chips. mf. */
497 /* FIXME: The SlowIO's for millennium could be replaced by
498 a single WritePipeTerm here. mf. */
500 /* Lower the ALE line */
501 WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr,
504 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
506 /* Wait for the chip to respond - Software requirement 11.4.1 */
507 return DoC_WaitReady(doc);
510 /* Read a buffer from DoC, taking care of Millennium odditys */
511 static void DoC_ReadBuf(struct DiskOnChip *doc, u_char * buf, int len)
514 int modulus = 0xffff;
515 unsigned long docptr;
518 docptr = doc->virtadr;
523 if (DoC_is_Millennium(doc)) {
524 /* Read the data via the internal pipeline through CDSN IO register,
525 see Pipelined Read Operations 11.3 */
526 dummy = ReadDOC(docptr, ReadPipeInit);
528 /* Millennium should use the LastDataRead register - Pipeline Reads */
531 /* This is needed for correctly ECC calculation */
535 for (i = 0; i < len; i++)
536 buf[i] = ReadDOC_(docptr, doc->ioreg + (i & modulus));
538 if (DoC_is_Millennium(doc)) {
539 buf[i] = ReadDOC(docptr, LastDataRead);
543 /* Write a buffer to DoC, taking care of Millennium odditys */
544 static void DoC_WriteBuf(struct DiskOnChip *doc, const u_char * buf, int len)
546 unsigned long docptr;
549 docptr = doc->virtadr;
554 for (i = 0; i < len; i++)
555 WriteDOC_(buf[i], docptr, doc->ioreg + i);
557 if (DoC_is_Millennium(doc)) {
558 WriteDOC(0x00, docptr, WritePipeTerm);
563 /* DoC_SelectChip: Select a given flash chip within the current floor */
565 static inline int DoC_SelectChip(struct DiskOnChip *doc, int chip)
567 unsigned long docptr = doc->virtadr;
569 /* Software requirement 11.4.4 before writing DeviceSelect */
570 /* Deassert the CE line to eliminate glitches on the FCE# outputs */
571 WriteDOC(CDSN_CTRL_WP, docptr, CDSNControl);
572 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
574 /* Select the individual flash chip requested */
575 WriteDOC(chip, docptr, CDSNDeviceSelect);
578 /* Reassert the CE line */
579 WriteDOC(CDSN_CTRL_CE | CDSN_CTRL_FLASH_IO | CDSN_CTRL_WP, docptr,
581 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
583 /* Wait for it to be ready */
584 return DoC_WaitReady(doc);
587 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
589 static inline int DoC_SelectFloor(struct DiskOnChip *doc, int floor)
591 unsigned long docptr = doc->virtadr;
593 /* Select the floor (bank) of chips required */
594 WriteDOC(floor, docptr, FloorSelect);
596 /* Wait for the chip to be ready */
597 return DoC_WaitReady(doc);
600 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
602 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
607 /* Page in the required floor/chip */
608 DoC_SelectFloor(doc, floor);
609 DoC_SelectChip(doc, chip);
612 if (DoC_Command(doc, NAND_CMD_RESET, CDSN_CTRL_WP)) {
614 printf("DoC_Command (reset) for %d,%d returned true\n",
621 /* Read the NAND chip ID: 1. Send ReadID command */
622 if (DoC_Command(doc, NAND_CMD_READID, CDSN_CTRL_WP)) {
624 printf("DoC_Command (ReadID) for %d,%d returned true\n",
630 /* Read the NAND chip ID: 2. Send address byte zero */
631 DoC_Address(doc, ADDR_COLUMN, 0, CDSN_CTRL_WP, 0);
633 /* Read the manufacturer and device id codes from the device */
635 /* CDSN Slow IO register see Software Requirement 11.4 item 5. */
636 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
638 mfr = ReadDOC_(doc->virtadr, doc->ioreg);
640 /* CDSN Slow IO register see Software Requirement 11.4 item 5. */
641 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
643 id = ReadDOC_(doc->virtadr, doc->ioreg);
645 /* No response - return failure */
646 if (mfr == 0xff || mfr == 0)
649 /* Check it's the same as the first chip we identified.
650 * M-Systems say that any given DiskOnChip device should only
651 * contain _one_ type of flash part, although that's not a
652 * hardware restriction. */
654 if (doc->mfr == mfr && doc->id == id)
655 return 1; /* This is another the same the first */
657 printf("Flash chip at floor %d, chip %d is different:\n",
661 /* Print and store the manufacturer and ID codes. */
662 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
663 if (mfr == nand_flash_ids[i].manufacture_id &&
664 id == nand_flash_ids[i].model_id) {
666 printf("Flash chip found: Manufacturer ID: %2.2X, "
667 "Chip ID: %2.2X (%s)\n", mfr, id,
668 nand_flash_ids[i].name);
674 nand_flash_ids[i].chipshift;
675 doc->page256 = nand_flash_ids[i].page256;
677 nand_flash_ids[i].pageadrlen;
679 nand_flash_ids[i].erasesize;
681 nand_flash_ids[i].name;
690 /* We haven't fully identified the chip. Print as much as we know. */
691 printf("Unknown flash chip found: %2.2X %2.2X\n",
698 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
700 static void DoC_ScanChips(struct DiskOnChip *this)
703 int numchips[MAX_FLOORS];
704 int maxchips = MAX_CHIPS;
711 if (DoC_is_Millennium(this))
712 maxchips = MAX_CHIPS_MIL;
714 /* For each floor, find the number of valid chips it contains */
715 for (floor = 0; floor < MAX_FLOORS; floor++) {
718 for (chip = 0; chip < maxchips && ret != 0; chip++) {
720 ret = DoC_IdentChip(this, floor, chip);
728 /* If there are none at all that we recognise, bail */
729 if (!this->numchips) {
730 puts ("No flash chips recognised.\n");
734 /* Allocate an array to hold the information for each chip */
735 this->chips = malloc(sizeof(struct Nand) * this->numchips);
737 puts ("No memory for allocating chip info structures\n");
743 /* Fill out the chip array with {floor, chipno} for each
744 * detected chip in the device. */
745 for (floor = 0; floor < MAX_FLOORS; floor++) {
746 for (chip = 0; chip < numchips[floor]; chip++) {
747 this->chips[ret].floor = floor;
748 this->chips[ret].chip = chip;
749 this->chips[ret].curadr = 0;
750 this->chips[ret].curmode = 0x50;
755 /* Calculate and print the total size of the device */
756 this->totlen = this->numchips * (1 << this->chipshift);
759 printf("%d flash chips found. Total DiskOnChip size: %ld MB\n",
760 this->numchips, this->totlen >> 20);
764 /* find_boot_record: Find the NFTL Media Header and its Spare copy which contains the
765 * various device information of the NFTL partition and Bad Unit Table. Update
766 * the ReplUnitTable[] table accroding to the Bad Unit Table. ReplUnitTable[]
767 * is used for management of Erase Unit in other routines in nftl.c and nftlmount.c
769 static int find_boot_record(struct NFTLrecord *nftl)
773 unsigned int block, boot_record_count = 0;
776 struct NFTLMediaHeader *mh = &nftl->MediaHdr;
779 nftl->MediaUnit = BLOCK_NIL;
780 nftl->SpareMediaUnit = BLOCK_NIL;
782 /* search for a valid boot record */
783 for (block = 0; block < nftl->nb_blocks; block++) {
786 /* Check for ANAND header first. Then can whinge if it's found but later
788 if ((ret = doc_read_ecc(nftl->mtd, block * nftl->EraseSize, SECTORSIZE,
789 &retlen, buf, NULL))) {
790 static int warncount = 5;
793 printf("Block read at 0x%x failed\n", block * nftl->EraseSize);
795 puts ("Further failures for this block will not be printed\n");
800 if (retlen < 6 || memcmp(buf, "ANAND", 6)) {
801 /* ANAND\0 not found. Continue */
803 printf("ANAND header not found at 0x%x\n", block * nftl->EraseSize);
809 printf("ANAND header found at 0x%x\n", block * nftl->EraseSize);
812 /* To be safer with BIOS, also use erase mark as discriminant */
813 if ((ret = doc_read_oob(nftl->mtd, block * nftl->EraseSize + SECTORSIZE + 8,
814 8, &retlen, (char *)&h1) < 0)) {
816 printf("ANAND header found at 0x%x, but OOB data read failed\n",
817 block * nftl->EraseSize);
822 /* OK, we like it. */
824 if (boot_record_count) {
825 /* We've already processed one. So we just check if
826 this one is the same as the first one we found */
827 if (memcmp(mh, buf, sizeof(struct NFTLMediaHeader))) {
829 printf("NFTL Media Headers at 0x%x and 0x%x disagree.\n",
830 nftl->MediaUnit * nftl->EraseSize, block * nftl->EraseSize);
832 /* if (debug) Print both side by side */
835 if (boot_record_count == 1)
836 nftl->SpareMediaUnit = block;
842 /* This is the first we've seen. Copy the media header structure into place */
843 memcpy(mh, buf, sizeof(struct NFTLMediaHeader));
845 /* Do some sanity checks on it */
846 if (mh->UnitSizeFactor != 0xff) {
847 puts ("Sorry, we don't support UnitSizeFactor "
852 nftl->nb_boot_blocks = le16_to_cpu(mh->FirstPhysicalEUN);
853 if ((nftl->nb_boot_blocks + 2) >= nftl->nb_blocks) {
854 printf ("NFTL Media Header sanity check failed:\n"
855 "nb_boot_blocks (%d) + 2 > nb_blocks (%d)\n",
856 nftl->nb_boot_blocks, nftl->nb_blocks);
860 nftl->numvunits = le32_to_cpu(mh->FormattedSize) / nftl->EraseSize;
861 if (nftl->numvunits > (nftl->nb_blocks - nftl->nb_boot_blocks - 2)) {
862 printf ("NFTL Media Header sanity check failed:\n"
863 "numvunits (%d) > nb_blocks (%d) - nb_boot_blocks(%d) - 2\n",
866 nftl->nb_boot_blocks);
870 nftl->nr_sects = nftl->numvunits * (nftl->EraseSize / SECTORSIZE);
872 /* If we're not using the last sectors in the device for some reason,
873 reduce nb_blocks accordingly so we forget they're there */
874 nftl->nb_blocks = le16_to_cpu(mh->NumEraseUnits) + le16_to_cpu(mh->FirstPhysicalEUN);
876 /* read the Bad Erase Unit Table and modify ReplUnitTable[] accordingly */
877 for (i = 0; i < nftl->nb_blocks; i++) {
878 if ((i & (SECTORSIZE - 1)) == 0) {
879 /* read one sector for every SECTORSIZE of blocks */
880 if ((ret = doc_read_ecc(nftl->mtd, block * nftl->EraseSize +
881 i + SECTORSIZE, SECTORSIZE,
882 &retlen, buf, (char *)&oob)) < 0) {
883 puts ("Read of bad sector table failed\n");
887 /* mark the Bad Erase Unit as RESERVED in ReplUnitTable */
888 if (buf[i & (SECTORSIZE - 1)] != 0xff)
889 nftl->ReplUnitTable[i] = BLOCK_RESERVED;
892 nftl->MediaUnit = block;
895 } /* foreach (block) */
897 return boot_record_count?0:-1;
900 /* This routine is made available to other mtd code via
901 * inter_module_register. It must only be accessed through
902 * inter_module_get which will bump the use count of this module. The
903 * addresses passed back in mtd are valid as long as the use count of
904 * this module is non-zero, i.e. between inter_module_get and
905 * inter_module_put. Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
907 static void DoC2k_init(struct DiskOnChip* this)
909 struct NFTLrecord *nftl;
911 switch (this->ChipID) {
912 case DOC_ChipID_Doc2k:
913 this->name = "DiskOnChip 2000";
914 this->ioreg = DoC_2k_CDSN_IO;
916 case DOC_ChipID_DocMil:
917 this->name = "DiskOnChip Millennium";
918 this->ioreg = DoC_Mil_CDSN_IO;
923 printf("%s found at address 0x%lX\n", this->name,
933 /* Ident all the chips present. */
938 /* Get physical parameters */
939 nftl->EraseSize = this->erasesize;
940 nftl->nb_blocks = this->totlen / this->erasesize;
943 if (find_boot_record(nftl) != 0)
944 this->nftl_found = 0;
946 this->nftl_found = 1;
948 printf("%s @ 0x%lX, %ld MB\n", this->name, this->physadr, this->totlen >> 20);
951 int doc_read_ecc(struct DiskOnChip* this, loff_t from, size_t len,
952 size_t * retlen, u_char * buf, u_char * eccbuf)
954 unsigned long docptr;
956 unsigned char syndrome[6];
958 int i, len256 = 0, ret=0;
960 docptr = this->virtadr;
962 /* Don't allow read past end of device */
963 if (from >= this->totlen) {
964 puts ("Out of flash\n");
968 /* Don't allow a single read to cross a 512-byte block boundary */
969 if (from + len > ((from | 0x1ff) + 1))
970 len = ((from | 0x1ff) + 1) - from;
972 /* The ECC will not be calculated correctly if less than 512 is read */
973 if (len != 0x200 && eccbuf)
974 printf("ECC needs a full sector read (adr: %lx size %lx)\n",
975 (long) from, (long) len);
978 printf("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len);
981 /* Find the chip which is to be used and select it */
982 mychip = &this->chips[shr(from, this->chipshift)];
984 if (this->curfloor != mychip->floor) {
985 DoC_SelectFloor(this, mychip->floor);
986 DoC_SelectChip(this, mychip->chip);
987 } else if (this->curchip != mychip->chip) {
988 DoC_SelectChip(this, mychip->chip);
991 this->curfloor = mychip->floor;
992 this->curchip = mychip->chip;
996 && (from & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
998 DoC_Address(this, ADDR_COLUMN_PAGE, from, CDSN_CTRL_WP,
1002 /* Prime the ECC engine */
1003 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
1004 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
1006 /* disable the ECC engine */
1007 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
1008 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
1011 /* treat crossing 256-byte sector for 2M x 8bits devices */
1012 if (this->page256 && from + len > (from | 0xff) + 1) {
1013 len256 = (from | 0xff) + 1 - from;
1014 DoC_ReadBuf(this, buf, len256);
1016 DoC_Command(this, NAND_CMD_READ0, CDSN_CTRL_WP);
1017 DoC_Address(this, ADDR_COLUMN_PAGE, from + len256,
1018 CDSN_CTRL_WP, CDSN_CTRL_ECC_IO);
1021 DoC_ReadBuf(this, &buf[len256], len - len256);
1023 /* Let the caller know we completed it */
1027 /* Read the ECC data through the DiskOnChip ECC logic */
1028 /* Note: this will work even with 2M x 8bit devices as */
1029 /* they have 8 bytes of OOB per 256 page. mf. */
1030 DoC_ReadBuf(this, eccbuf, 6);
1032 /* Flush the pipeline */
1033 if (DoC_is_Millennium(this)) {
1034 dummy = ReadDOC(docptr, ECCConf);
1035 dummy = ReadDOC(docptr, ECCConf);
1036 i = ReadDOC(docptr, ECCConf);
1038 dummy = ReadDOC(docptr, 2k_ECCStatus);
1039 dummy = ReadDOC(docptr, 2k_ECCStatus);
1040 i = ReadDOC(docptr, 2k_ECCStatus);
1043 /* Check the ECC Status */
1046 /* There was an ECC error */
1048 printf("DiskOnChip ECC Error: Read at %lx\n", (long)from);
1050 /* Read the ECC syndrom through the DiskOnChip ECC logic.
1051 These syndrome will be all ZERO when there is no error */
1052 for (i = 0; i < 6; i++) {
1054 ReadDOC(docptr, ECCSyndrome0 + i);
1056 nb_errors = doc_decode_ecc(buf, syndrome);
1059 printf("Errors corrected: %x\n", nb_errors);
1061 if (nb_errors < 0) {
1062 /* We return error, but have actually done the read. Not that
1063 this can be told to user-space, via sys_read(), but at least
1064 MTD-aware stuff can know about it by checking *retlen */
1065 printf("ECC Errors at %lx\n", (long)from);
1071 printf("ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
1072 (long)from, eccbuf[0], eccbuf[1], eccbuf[2],
1073 eccbuf[3], eccbuf[4], eccbuf[5]);
1076 /* disable the ECC engine */
1077 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
1080 /* according to 11.4.1, we need to wait for the busy line
1081 * drop if we read to the end of the page. */
1082 if(0 == ((from + *retlen) & 0x1ff))
1084 DoC_WaitReady(this);
1090 int doc_write_ecc(struct DiskOnChip* this, loff_t to, size_t len,
1091 size_t * retlen, const u_char * buf,
1094 int di; /* Yes, DI is a hangover from when I was disassembling the binary driver */
1095 unsigned long docptr;
1096 volatile char dummy;
1098 struct Nand *mychip;
1100 docptr = this->virtadr;
1102 /* Don't allow write past end of device */
1103 if (to >= this->totlen) {
1104 puts ("Out of flash\n");
1108 /* Don't allow a single write to cross a 512-byte block boundary */
1109 if (to + len > ((to | 0x1ff) + 1))
1110 len = ((to | 0x1ff) + 1) - to;
1112 /* The ECC will not be calculated correctly if less than 512 is written */
1113 if (len != 0x200 && eccbuf)
1114 printf("ECC needs a full sector write (adr: %lx size %lx)\n",
1115 (long) to, (long) len);
1117 /* printf("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */
1119 /* Find the chip which is to be used and select it */
1120 mychip = &this->chips[shr(to, this->chipshift)];
1122 if (this->curfloor != mychip->floor) {
1123 DoC_SelectFloor(this, mychip->floor);
1124 DoC_SelectChip(this, mychip->chip);
1125 } else if (this->curchip != mychip->chip) {
1126 DoC_SelectChip(this, mychip->chip);
1129 this->curfloor = mychip->floor;
1130 this->curchip = mychip->chip;
1132 /* Set device to main plane of flash */
1133 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
1136 && (to & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
1139 DoC_Command(this, NAND_CMD_SEQIN, 0);
1140 DoC_Address(this, ADDR_COLUMN_PAGE, to, 0, CDSN_CTRL_ECC_IO);
1143 /* Prime the ECC engine */
1144 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
1145 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
1147 /* disable the ECC engine */
1148 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
1149 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
1152 /* treat crossing 256-byte sector for 2M x 8bits devices */
1153 if (this->page256 && to + len > (to | 0xff) + 1) {
1154 len256 = (to | 0xff) + 1 - to;
1155 DoC_WriteBuf(this, buf, len256);
1157 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1159 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
1160 /* There's an implicit DoC_WaitReady() in DoC_Command */
1162 dummy = ReadDOC(docptr, CDSNSlowIO);
1165 if (ReadDOC_(docptr, this->ioreg) & 1) {
1166 puts ("Error programming flash\n");
1167 /* Error in programming */
1172 DoC_Command(this, NAND_CMD_SEQIN, 0);
1173 DoC_Address(this, ADDR_COLUMN_PAGE, to + len256, 0,
1177 DoC_WriteBuf(this, &buf[len256], len - len256);
1180 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_CE, docptr,
1183 if (DoC_is_Millennium(this)) {
1184 WriteDOC(0, docptr, NOP);
1185 WriteDOC(0, docptr, NOP);
1186 WriteDOC(0, docptr, NOP);
1188 WriteDOC_(0, docptr, this->ioreg);
1189 WriteDOC_(0, docptr, this->ioreg);
1190 WriteDOC_(0, docptr, this->ioreg);
1193 /* Read the ECC data through the DiskOnChip ECC logic */
1194 for (di = 0; di < 6; di++) {
1195 eccbuf[di] = ReadDOC(docptr, ECCSyndrome0 + di);
1198 /* Reset the ECC engine */
1199 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
1203 ("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
1204 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
1205 eccbuf[4], eccbuf[5]);
1209 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1211 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
1212 /* There's an implicit DoC_WaitReady() in DoC_Command */
1214 dummy = ReadDOC(docptr, CDSNSlowIO);
1217 if (ReadDOC_(docptr, this->ioreg) & 1) {
1218 puts ("Error programming flash\n");
1219 /* Error in programming */
1224 /* Let the caller know we completed it */
1232 /* Write the ECC data to flash */
1233 for (di=0; di<6; di++)
1239 ret = doc_write_oob(this, to, 8, &dummy, x);
1245 int doc_read_oob(struct DiskOnChip* this, loff_t ofs, size_t len,
1246 size_t * retlen, u_char * buf)
1248 int len256 = 0, ret;
1249 unsigned long docptr;
1250 struct Nand *mychip;
1252 docptr = this->virtadr;
1254 mychip = &this->chips[shr(ofs, this->chipshift)];
1256 if (this->curfloor != mychip->floor) {
1257 DoC_SelectFloor(this, mychip->floor);
1258 DoC_SelectChip(this, mychip->chip);
1259 } else if (this->curchip != mychip->chip) {
1260 DoC_SelectChip(this, mychip->chip);
1262 this->curfloor = mychip->floor;
1263 this->curchip = mychip->chip;
1265 /* update address for 2M x 8bit devices. OOB starts on the second */
1266 /* page to maintain compatibility with doc_read_ecc. */
1267 if (this->page256) {
1274 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1275 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, CDSN_CTRL_WP, 0);
1277 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1278 /* Note: datasheet says it should automaticaly wrap to the */
1279 /* next OOB block, but it didn't work here. mf. */
1280 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1281 len256 = (ofs | 0x7) + 1 - ofs;
1282 DoC_ReadBuf(this, buf, len256);
1284 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1285 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff),
1289 DoC_ReadBuf(this, &buf[len256], len - len256);
1292 /* Reading the full OOB data drops us off of the end of the page,
1293 * causing the flash device to go into busy mode, so we need
1294 * to wait until ready 11.4.1 and Toshiba TC58256FT docs */
1296 ret = DoC_WaitReady(this);
1302 int doc_write_oob(struct DiskOnChip* this, loff_t ofs, size_t len,
1303 size_t * retlen, const u_char * buf)
1306 unsigned long docptr = this->virtadr;
1307 struct Nand *mychip = &this->chips[shr(ofs, this->chipshift)];
1311 printf("doc_write_oob(%lx, %d): %2.2X %2.2X %2.2X %2.2X ... %2.2X %2.2X .. %2.2X %2.2X\n",
1312 (long)ofs, len, buf[0], buf[1], buf[2], buf[3],
1313 buf[8], buf[9], buf[14],buf[15]);
1316 /* Find the chip which is to be used and select it */
1317 if (this->curfloor != mychip->floor) {
1318 DoC_SelectFloor(this, mychip->floor);
1319 DoC_SelectChip(this, mychip->chip);
1320 } else if (this->curchip != mychip->chip) {
1321 DoC_SelectChip(this, mychip->chip);
1323 this->curfloor = mychip->floor;
1324 this->curchip = mychip->chip;
1326 /* disable the ECC engine */
1327 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
1328 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
1330 /* Reset the chip, see Software Requirement 11.4 item 1. */
1331 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
1333 /* issue the Read2 command to set the pointer to the Spare Data Area. */
1334 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1336 /* update address for 2M x 8bit devices. OOB starts on the second */
1337 /* page to maintain compatibility with doc_read_ecc. */
1338 if (this->page256) {
1345 /* issue the Serial Data In command to initial the Page Program process */
1346 DoC_Command(this, NAND_CMD_SEQIN, 0);
1347 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, 0, 0);
1349 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1350 /* Note: datasheet says it should automaticaly wrap to the */
1351 /* next OOB block, but it didn't work here. mf. */
1352 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1353 len256 = (ofs | 0x7) + 1 - ofs;
1354 DoC_WriteBuf(this, buf, len256);
1356 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1357 DoC_Command(this, NAND_CMD_STATUS, 0);
1358 /* DoC_WaitReady() is implicit in DoC_Command */
1360 dummy = ReadDOC(docptr, CDSNSlowIO);
1363 if (ReadDOC_(docptr, this->ioreg) & 1) {
1364 puts ("Error programming oob data\n");
1365 /* There was an error */
1369 DoC_Command(this, NAND_CMD_SEQIN, 0);
1370 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), 0, 0);
1373 DoC_WriteBuf(this, &buf[len256], len - len256);
1375 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1376 DoC_Command(this, NAND_CMD_STATUS, 0);
1377 /* DoC_WaitReady() is implicit in DoC_Command */
1379 dummy = ReadDOC(docptr, CDSNSlowIO);
1382 if (ReadDOC_(docptr, this->ioreg) & 1) {
1383 puts ("Error programming oob data\n");
1384 /* There was an error */
1394 int doc_erase(struct DiskOnChip* this, loff_t ofs, size_t len)
1397 unsigned long docptr;
1398 struct Nand *mychip;
1400 if (ofs & (this->erasesize-1) || len & (this->erasesize-1)) {
1401 puts ("Offset and size must be sector aligned\n");
1405 docptr = this->virtadr;
1407 /* FIXME: Do this in the background. Use timers or schedule_task() */
1409 mychip = &this->chips[shr(ofs, this->chipshift)];
1411 if (this->curfloor != mychip->floor) {
1412 DoC_SelectFloor(this, mychip->floor);
1413 DoC_SelectChip(this, mychip->chip);
1414 } else if (this->curchip != mychip->chip) {
1415 DoC_SelectChip(this, mychip->chip);
1417 this->curfloor = mychip->floor;
1418 this->curchip = mychip->chip;
1420 DoC_Command(this, NAND_CMD_ERASE1, 0);
1421 DoC_Address(this, ADDR_PAGE, ofs, 0, 0);
1422 DoC_Command(this, NAND_CMD_ERASE2, 0);
1424 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
1426 dummy = ReadDOC(docptr, CDSNSlowIO);
1429 if (ReadDOC_(docptr, this->ioreg) & 1) {
1430 printf("Error erasing at 0x%lx\n", (long)ofs);
1431 /* There was an error */
1434 ofs += this->erasesize;
1435 len -= this->erasesize;
1442 static inline int doccheck(unsigned long potential, unsigned long physadr)
1444 unsigned long window=potential;
1445 unsigned char tmp, ChipID;
1446 #ifndef DOC_PASSIVE_PROBE
1450 /* Routine copied from the Linux DOC driver */
1452 #ifdef CFG_DOCPROBE_55AA
1453 /* Check for 0x55 0xAA signature at beginning of window,
1454 this is no longer true once we remove the IPL (for Millennium */
1455 if (ReadDOC(window, Sig1) != 0x55 || ReadDOC(window, Sig2) != 0xaa)
1457 #endif /* CFG_DOCPROBE_55AA */
1459 #ifndef DOC_PASSIVE_PROBE
1460 /* It's not possible to cleanly detect the DiskOnChip - the
1461 * bootup procedure will put the device into reset mode, and
1462 * it's not possible to talk to it without actually writing
1463 * to the DOCControl register. So we store the current contents
1464 * of the DOCControl register's location, in case we later decide
1465 * that it's not a DiskOnChip, and want to put it back how we
1468 tmp2 = ReadDOC(window, DOCControl);
1470 /* Reset the DiskOnChip ASIC */
1471 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET,
1472 window, DOCControl);
1473 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET,
1474 window, DOCControl);
1476 /* Enable the DiskOnChip ASIC */
1477 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL,
1478 window, DOCControl);
1479 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL,
1480 window, DOCControl);
1481 #endif /* !DOC_PASSIVE_PROBE */
1483 ChipID = ReadDOC(window, ChipID);
1486 case DOC_ChipID_Doc2k:
1487 /* Check the TOGGLE bit in the ECC register */
1488 tmp = ReadDOC(window, 2k_ECCStatus) & DOC_TOGGLE_BIT;
1489 if ((ReadDOC(window, 2k_ECCStatus) & DOC_TOGGLE_BIT) != tmp)
1493 case DOC_ChipID_DocMil:
1494 /* Check the TOGGLE bit in the ECC register */
1495 tmp = ReadDOC(window, ECCConf) & DOC_TOGGLE_BIT;
1496 if ((ReadDOC(window, ECCConf) & DOC_TOGGLE_BIT) != tmp)
1501 #ifndef CFG_DOCPROBE_55AA
1503 * if the ID isn't the DoC2000 or DoCMillenium ID, so we can assume
1504 * the DOC is missing
1507 printf("Possible DiskOnChip with unknown ChipID %2.2X found at 0x%lx\n",
1511 #ifndef DOC_PASSIVE_PROBE
1512 /* Put back the contents of the DOCControl register, in case it's not
1513 * actually a DiskOnChip.
1515 WriteDOC(tmp2, window, DOCControl);
1520 puts ("DiskOnChip failed TOGGLE test, dropping.\n");
1522 #ifndef DOC_PASSIVE_PROBE
1523 /* Put back the contents of the DOCControl register: it's not a DiskOnChip */
1524 WriteDOC(tmp2, window, DOCControl);
1529 void doc_probe(unsigned long physadr)
1531 struct DiskOnChip *this = NULL;
1534 if ((ChipID = doccheck(physadr, physadr))) {
1536 for (i=0; i<CFG_MAX_DOC_DEVICE; i++) {
1537 if (doc_dev_desc[i].ChipID == DOC_ChipID_UNKNOWN) {
1538 this = doc_dev_desc + i;
1544 puts ("Cannot allocate memory for data structures.\n");
1548 if (curr_device == -1)
1551 memset((char *)this, 0, sizeof(struct DiskOnChip));
1553 this->virtadr = physadr;
1554 this->physadr = physadr;
1555 this->ChipID = ChipID;
1559 puts ("No DiskOnChip found\n");
1563 #endif /* (CONFIG_COMMANDS & CFG_CMD_DOC) */