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/nftl.h>
25 #include <linux/mtd/nand.h>
26 #include <linux/mtd/nand_ids.h>
27 #include <linux/mtd/doc2000.h>
28 #include <linux/mtd/nftl.h>
30 #ifdef CFG_DOC_SUPPORT_2000
31 #define DoC_is_2000(doc) (doc->ChipID == DOC_ChipID_Doc2k)
33 #define DoC_is_2000(doc) (0)
36 #ifdef CFG_DOC_SUPPORT_MILLENNIUM
37 #define DoC_is_Millennium(doc) (doc->ChipID == DOC_ChipID_DocMil)
39 #define DoC_is_Millennium(doc) (0)
42 /* CFG_DOC_PASSIVE_PROBE:
43 In order to ensure that the BIOS checksum is correct at boot time, and
44 hence that the onboard BIOS extension gets executed, the DiskOnChip
45 goes into reset mode when it is read sequentially: all registers
46 return 0xff until the chip is woken up again by writing to the
49 Unfortunately, this means that the probe for the DiskOnChip is unsafe,
50 because one of the first things it does is write to where it thinks
51 the DOCControl register should be - which may well be shared memory
52 for another device. I've had machines which lock up when this is
53 attempted. Hence the possibility to do a passive probe, which will fail
54 to detect a chip in reset mode, but is at least guaranteed not to lock
57 If you have this problem, uncomment the following line:
58 #define CFG_DOC_PASSIVE_PROBE
66 static struct DiskOnChip doc_dev_desc[CFG_MAX_DOC_DEVICE];
68 /* Current DOC Device */
69 static int curr_device = -1;
71 /* ------------------------------------------------------------------------- */
73 int do_doc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
80 printf ("Usage:\n%s\n", cmdtp->usage);
83 if (strcmp(argv[1],"info") == 0) {
88 for (i=0; i<CFG_MAX_DOC_DEVICE; ++i) {
89 if(doc_dev_desc[i].ChipID == DOC_ChipID_UNKNOWN)
90 continue; /* list only known devices */
91 printf ("Device %d: ", i);
92 doc_print(&doc_dev_desc[i]);
96 } else if (strcmp(argv[1],"device") == 0) {
97 if ((curr_device < 0) || (curr_device >= CFG_MAX_DOC_DEVICE)) {
98 puts ("\nno devices available\n");
101 printf ("\nDevice %d: ", curr_device);
102 doc_print(&doc_dev_desc[curr_device]);
105 printf ("Usage:\n%s\n", cmdtp->usage);
108 if (strcmp(argv[1],"device") == 0) {
109 int dev = (int)simple_strtoul(argv[2], NULL, 10);
111 printf ("\nDevice %d: ", dev);
112 if (dev >= CFG_MAX_DOC_DEVICE) {
113 puts ("unknown device\n");
116 doc_print(&doc_dev_desc[dev]);
119 if (doc_dev_desc[dev].ChipID == DOC_ChipID_UNKNOWN) {
125 puts ("... is now current device\n");
130 printf ("Usage:\n%s\n", cmdtp->usage);
133 /* at least 4 args */
135 if (strcmp(argv[1],"read") == 0 || strcmp(argv[1],"write") == 0) {
136 ulong addr = simple_strtoul(argv[2], NULL, 16);
137 ulong off = simple_strtoul(argv[3], NULL, 16);
138 ulong size = simple_strtoul(argv[4], NULL, 16);
139 int cmd = (strcmp(argv[1],"read") == 0);
142 printf ("\nDOC %s: device %d offset %ld, size %ld ... ",
143 cmd ? "read" : "write", curr_device, off, size);
145 ret = doc_rw(doc_dev_desc + curr_device, cmd, off, size,
146 &total, (u_char*)addr);
148 printf ("%d bytes %s: %s\n", total, cmd ? "read" : "write",
149 ret ? "ERROR" : "OK");
152 } else if (strcmp(argv[1],"erase") == 0) {
153 ulong off = simple_strtoul(argv[2], NULL, 16);
154 ulong size = simple_strtoul(argv[3], NULL, 16);
157 printf ("\nDOC erase: device %d offset %ld, size %ld ... ",
158 curr_device, off, size);
160 ret = doc_erase (doc_dev_desc + curr_device, off, size);
162 printf("%s\n", ret ? "ERROR" : "OK");
166 printf ("Usage:\n%s\n", cmdtp->usage);
175 "doc - Disk-On-Chip sub-system\n",
176 "info - show available DOC devices\n"
177 "doc device [dev] - show or set current device\n"
178 "doc read addr off size\n"
179 "doc write addr off size - read/write `size'"
180 " bytes starting at offset `off'\n"
181 " to/from memory address `addr'\n"
182 "doc erase off size - erase `size' bytes of DOC from offset `off'\n"
185 int do_docboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
187 char *boot_device = NULL;
198 addr = CFG_LOAD_ADDR;
199 boot_device = getenv ("bootdevice");
202 addr = simple_strtoul(argv[1], NULL, 16);
203 boot_device = getenv ("bootdevice");
206 addr = simple_strtoul(argv[1], NULL, 16);
207 boot_device = argv[2];
210 addr = simple_strtoul(argv[1], NULL, 16);
211 boot_device = argv[2];
212 offset = simple_strtoul(argv[3], NULL, 16);
215 printf ("Usage:\n%s\n", cmdtp->usage);
216 SHOW_BOOT_PROGRESS (-1);
221 puts ("\n** No boot device **\n");
222 SHOW_BOOT_PROGRESS (-1);
226 dev = simple_strtoul(boot_device, &ep, 16);
228 if ((dev >= CFG_MAX_DOC_DEVICE) ||
229 (doc_dev_desc[dev].ChipID == DOC_ChipID_UNKNOWN)) {
230 printf ("\n** Device %d not available\n", dev);
231 SHOW_BOOT_PROGRESS (-1);
235 printf ("\nLoading from device %d: %s at 0x%lX (offset 0x%lX)\n",
236 dev, doc_dev_desc[dev].name, doc_dev_desc[dev].physadr,
239 if (doc_rw (doc_dev_desc + dev, 1, offset,
240 SECTORSIZE, NULL, (u_char *)addr)) {
241 printf ("** Read error on %d\n", dev);
242 SHOW_BOOT_PROGRESS (-1);
246 hdr = (image_header_t *)addr;
248 if (hdr->ih_magic == IH_MAGIC) {
250 print_image_hdr (hdr);
252 cnt = (hdr->ih_size + sizeof(image_header_t));
255 puts ("\n** Bad Magic Number **\n");
256 SHOW_BOOT_PROGRESS (-1);
260 if (doc_rw (doc_dev_desc + dev, 1, offset + SECTORSIZE, cnt,
261 NULL, (u_char *)(addr+SECTORSIZE))) {
262 printf ("** Read error on %d\n", dev);
263 SHOW_BOOT_PROGRESS (-1);
267 /* Loading ok, update default load address */
271 /* Check if we should attempt an auto-start */
272 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
274 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
276 local_args[0] = argv[0];
277 local_args[1] = NULL;
279 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
281 do_bootm (cmdtp, 0, 1, local_args);
288 docboot, 4, 1, do_docboot,
289 "docboot - boot from DOC device\n",
293 int doc_rw (struct DiskOnChip* this, int cmd,
294 loff_t from, size_t len,
295 size_t * retlen, u_char * buf)
297 int noecc, ret = 0, n, total = 0;
301 /* The ECC will not be calculated correctly if
302 less than 512 is written or read */
303 noecc = (from != (from | 0x1ff) + 1) || (len < 0x200);
306 ret = doc_read_ecc(this, from, len,
308 noecc ? NULL : eccbuf);
310 ret = doc_write_ecc(this, from, len,
312 noecc ? NULL : eccbuf);
329 void doc_print(struct DiskOnChip *this) {
330 printf("%s at 0x%lX,\n"
331 "\t %d chip%s %s, size %d MB, \n"
332 "\t total size %ld MB, sector size %ld kB\n",
333 this->name, this->physadr, this->numchips,
334 this->numchips>1 ? "s" : "", this->chips_name,
335 1 << (this->chipshift - 20),
336 this->totlen >> 20, this->erasesize >> 10);
338 if (this->nftl_found) {
339 struct NFTLrecord *nftl = &this->nftl;
340 unsigned long bin_size, flash_size;
342 bin_size = nftl->nb_boot_blocks * this->erasesize;
343 flash_size = (nftl->nb_blocks - nftl->nb_boot_blocks) * this->erasesize;
345 printf("\t NFTL boot record:\n"
346 "\t Binary partition: size %ld%s\n"
347 "\t Flash disk partition: size %ld%s, offset 0x%lx\n",
348 bin_size > (1 << 20) ? bin_size >> 20 : bin_size >> 10,
349 bin_size > (1 << 20) ? "MB" : "kB",
350 flash_size > (1 << 20) ? flash_size >> 20 : flash_size >> 10,
351 flash_size > (1 << 20) ? "MB" : "kB", bin_size);
353 puts ("\t No NFTL boot record found.\n");
357 /* ------------------------------------------------------------------------- */
359 /* This function is needed to avoid calls of the __ashrdi3 function. */
360 static int shr(int val, int shift) {
364 /* Perform the required delay cycles by reading from the appropriate register */
365 static void DoC_Delay(struct DiskOnChip *doc, unsigned short cycles)
370 for (i = 0; i < cycles; i++) {
371 if (DoC_is_Millennium(doc))
372 dummy = ReadDOC(doc->virtadr, NOP);
374 dummy = ReadDOC(doc->virtadr, DOCStatus);
379 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
380 static int _DoC_WaitReady(struct DiskOnChip *doc)
382 unsigned long docptr = doc->virtadr;
383 unsigned long start = get_timer(0);
386 puts ("_DoC_WaitReady called for out-of-line wait\n");
389 /* Out-of-line routine to wait for chip response */
390 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
391 #ifdef CFG_DOC_SHORT_TIMEOUT
392 /* it seems that after a certain time the DoC deasserts
393 * the CDSN_CTRL_FR_B although it is not ready...
394 * using a short timout solve this (timer increments every ms) */
395 if (get_timer(start) > 10) {
399 if (get_timer(start) > 10 * 1000) {
400 puts ("_DoC_WaitReady timed out.\n");
410 static int DoC_WaitReady(struct DiskOnChip *doc)
412 unsigned long docptr = doc->virtadr;
413 /* This is inline, to optimise the common case, where it's ready instantly */
416 /* 4 read form NOP register should be issued in prior to the read from CDSNControl
417 see Software Requirement 11.4 item 2. */
420 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
421 /* Call the out-of-line routine to wait */
422 ret = _DoC_WaitReady(doc);
424 /* issue 2 read from NOP register after reading from CDSNControl register
425 see Software Requirement 11.4 item 2. */
431 /* DoC_Command: Send a flash command to the flash chip through the CDSN Slow IO register to
432 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
433 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
435 static inline int DoC_Command(struct DiskOnChip *doc, unsigned char command,
436 unsigned char xtraflags)
438 unsigned long docptr = doc->virtadr;
440 if (DoC_is_2000(doc))
441 xtraflags |= CDSN_CTRL_FLASH_IO;
443 /* Assert the CLE (Command Latch Enable) line to the flash chip */
444 WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
445 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
447 if (DoC_is_Millennium(doc))
448 WriteDOC(command, docptr, CDSNSlowIO);
450 /* Send the command */
451 WriteDOC_(command, docptr, doc->ioreg);
453 /* Lower the CLE line */
454 WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
455 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
457 /* Wait for the chip to respond - Software requirement 11.4.1 (extended for any command) */
458 return DoC_WaitReady(doc);
461 /* DoC_Address: Set the current address for the flash chip through the CDSN Slow IO register to
462 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
463 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
465 static int DoC_Address(struct DiskOnChip *doc, int numbytes, unsigned long ofs,
466 unsigned char xtraflags1, unsigned char xtraflags2)
468 unsigned long docptr;
471 docptr = doc->virtadr;
473 if (DoC_is_2000(doc))
474 xtraflags1 |= CDSN_CTRL_FLASH_IO;
476 /* Assert the ALE (Address Latch Enable) line to the flash chip */
477 WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
479 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
481 /* Send the address */
482 /* Devices with 256-byte page are addressed as:
483 Column (bits 0-7), Page (bits 8-15, 16-23, 24-31)
484 * there is no device on the market with page256
485 and more than 24 bits.
486 Devices with 512-byte page are addressed as:
487 Column (bits 0-7), Page (bits 9-16, 17-24, 25-31)
488 * 25-31 is sent only if the chip support it.
489 * bit 8 changes the read command to be sent
490 (NAND_CMD_READ0 or NAND_CMD_READ1).
493 if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE) {
494 if (DoC_is_Millennium(doc))
495 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
496 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
505 if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE) {
506 for (i = 0; i < doc->pageadrlen; i++, ofs = ofs >> 8) {
507 if (DoC_is_Millennium(doc))
508 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
509 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
513 DoC_Delay(doc, 2); /* Needed for some slow flash chips. mf. */
515 /* FIXME: The SlowIO's for millennium could be replaced by
516 a single WritePipeTerm here. mf. */
518 /* Lower the ALE line */
519 WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr,
522 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
524 /* Wait for the chip to respond - Software requirement 11.4.1 */
525 return DoC_WaitReady(doc);
528 /* Read a buffer from DoC, taking care of Millennium odditys */
529 static void DoC_ReadBuf(struct DiskOnChip *doc, u_char * buf, int len)
532 int modulus = 0xffff;
533 unsigned long docptr;
536 docptr = doc->virtadr;
541 if (DoC_is_Millennium(doc)) {
542 /* Read the data via the internal pipeline through CDSN IO register,
543 see Pipelined Read Operations 11.3 */
544 dummy = ReadDOC(docptr, ReadPipeInit);
546 /* Millennium should use the LastDataRead register - Pipeline Reads */
549 /* This is needed for correctly ECC calculation */
553 for (i = 0; i < len; i++)
554 buf[i] = ReadDOC_(docptr, doc->ioreg + (i & modulus));
556 if (DoC_is_Millennium(doc)) {
557 buf[i] = ReadDOC(docptr, LastDataRead);
561 /* Write a buffer to DoC, taking care of Millennium odditys */
562 static void DoC_WriteBuf(struct DiskOnChip *doc, const u_char * buf, int len)
564 unsigned long docptr;
567 docptr = doc->virtadr;
572 for (i = 0; i < len; i++)
573 WriteDOC_(buf[i], docptr, doc->ioreg + i);
575 if (DoC_is_Millennium(doc)) {
576 WriteDOC(0x00, docptr, WritePipeTerm);
581 /* DoC_SelectChip: Select a given flash chip within the current floor */
583 static inline int DoC_SelectChip(struct DiskOnChip *doc, int chip)
585 unsigned long docptr = doc->virtadr;
587 /* Software requirement 11.4.4 before writing DeviceSelect */
588 /* Deassert the CE line to eliminate glitches on the FCE# outputs */
589 WriteDOC(CDSN_CTRL_WP, docptr, CDSNControl);
590 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
592 /* Select the individual flash chip requested */
593 WriteDOC(chip, docptr, CDSNDeviceSelect);
596 /* Reassert the CE line */
597 WriteDOC(CDSN_CTRL_CE | CDSN_CTRL_FLASH_IO | CDSN_CTRL_WP, docptr,
599 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
601 /* Wait for it to be ready */
602 return DoC_WaitReady(doc);
605 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
607 static inline int DoC_SelectFloor(struct DiskOnChip *doc, int floor)
609 unsigned long docptr = doc->virtadr;
611 /* Select the floor (bank) of chips required */
612 WriteDOC(floor, docptr, FloorSelect);
614 /* Wait for the chip to be ready */
615 return DoC_WaitReady(doc);
618 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
620 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
625 /* Page in the required floor/chip */
626 DoC_SelectFloor(doc, floor);
627 DoC_SelectChip(doc, chip);
630 if (DoC_Command(doc, NAND_CMD_RESET, CDSN_CTRL_WP)) {
632 printf("DoC_Command (reset) for %d,%d returned true\n",
639 /* Read the NAND chip ID: 1. Send ReadID command */
640 if (DoC_Command(doc, NAND_CMD_READID, CDSN_CTRL_WP)) {
642 printf("DoC_Command (ReadID) for %d,%d returned true\n",
648 /* Read the NAND chip ID: 2. Send address byte zero */
649 DoC_Address(doc, ADDR_COLUMN, 0, CDSN_CTRL_WP, 0);
651 /* Read the manufacturer and device id codes from the device */
653 /* CDSN Slow IO register see Software Requirement 11.4 item 5. */
654 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
656 mfr = ReadDOC_(doc->virtadr, doc->ioreg);
658 /* CDSN Slow IO register see Software Requirement 11.4 item 5. */
659 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
661 id = ReadDOC_(doc->virtadr, doc->ioreg);
663 /* No response - return failure */
664 if (mfr == 0xff || mfr == 0)
667 /* Check it's the same as the first chip we identified.
668 * M-Systems say that any given DiskOnChip device should only
669 * contain _one_ type of flash part, although that's not a
670 * hardware restriction. */
672 if (doc->mfr == mfr && doc->id == id)
673 return 1; /* This is another the same the first */
675 printf("Flash chip at floor %d, chip %d is different:\n",
679 /* Print and store the manufacturer and ID codes. */
680 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
681 if (mfr == nand_flash_ids[i].manufacture_id &&
682 id == nand_flash_ids[i].model_id) {
684 printf("Flash chip found: Manufacturer ID: %2.2X, "
685 "Chip ID: %2.2X (%s)\n", mfr, id,
686 nand_flash_ids[i].name);
692 nand_flash_ids[i].chipshift;
693 doc->page256 = nand_flash_ids[i].page256;
695 nand_flash_ids[i].pageadrlen;
697 nand_flash_ids[i].erasesize;
699 nand_flash_ids[i].name;
708 /* We haven't fully identified the chip. Print as much as we know. */
709 printf("Unknown flash chip found: %2.2X %2.2X\n",
716 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
718 static void DoC_ScanChips(struct DiskOnChip *this)
721 int numchips[MAX_FLOORS];
722 int maxchips = MAX_CHIPS;
729 if (DoC_is_Millennium(this))
730 maxchips = MAX_CHIPS_MIL;
732 /* For each floor, find the number of valid chips it contains */
733 for (floor = 0; floor < MAX_FLOORS; floor++) {
736 for (chip = 0; chip < maxchips && ret != 0; chip++) {
738 ret = DoC_IdentChip(this, floor, chip);
746 /* If there are none at all that we recognise, bail */
747 if (!this->numchips) {
748 puts ("No flash chips recognised.\n");
752 /* Allocate an array to hold the information for each chip */
753 this->chips = malloc(sizeof(struct Nand) * this->numchips);
755 puts ("No memory for allocating chip info structures\n");
761 /* Fill out the chip array with {floor, chipno} for each
762 * detected chip in the device. */
763 for (floor = 0; floor < MAX_FLOORS; floor++) {
764 for (chip = 0; chip < numchips[floor]; chip++) {
765 this->chips[ret].floor = floor;
766 this->chips[ret].chip = chip;
767 this->chips[ret].curadr = 0;
768 this->chips[ret].curmode = 0x50;
773 /* Calculate and print the total size of the device */
774 this->totlen = this->numchips * (1 << this->chipshift);
777 printf("%d flash chips found. Total DiskOnChip size: %ld MB\n",
778 this->numchips, this->totlen >> 20);
782 /* find_boot_record: Find the NFTL Media Header and its Spare copy which contains the
783 * various device information of the NFTL partition and Bad Unit Table. Update
784 * the ReplUnitTable[] table accroding to the Bad Unit Table. ReplUnitTable[]
785 * is used for management of Erase Unit in other routines in nftl.c and nftlmount.c
787 static int find_boot_record(struct NFTLrecord *nftl)
791 unsigned int block, boot_record_count = 0;
794 struct NFTLMediaHeader *mh = &nftl->MediaHdr;
797 nftl->MediaUnit = BLOCK_NIL;
798 nftl->SpareMediaUnit = BLOCK_NIL;
800 /* search for a valid boot record */
801 for (block = 0; block < nftl->nb_blocks; block++) {
804 /* Check for ANAND header first. Then can whinge if it's found but later
806 if ((ret = doc_read_ecc(nftl->mtd, block * nftl->EraseSize, SECTORSIZE,
807 &retlen, buf, NULL))) {
808 static int warncount = 5;
811 printf("Block read at 0x%x failed\n", block * nftl->EraseSize);
813 puts ("Further failures for this block will not be printed\n");
818 if (retlen < 6 || memcmp(buf, "ANAND", 6)) {
819 /* ANAND\0 not found. Continue */
821 printf("ANAND header not found at 0x%x\n", block * nftl->EraseSize);
827 printf("ANAND header found at 0x%x\n", block * nftl->EraseSize);
830 /* To be safer with BIOS, also use erase mark as discriminant */
831 if ((ret = doc_read_oob(nftl->mtd, block * nftl->EraseSize + SECTORSIZE + 8,
832 8, &retlen, (char *)&h1) < 0)) {
834 printf("ANAND header found at 0x%x, but OOB data read failed\n",
835 block * nftl->EraseSize);
840 /* OK, we like it. */
842 if (boot_record_count) {
843 /* We've already processed one. So we just check if
844 this one is the same as the first one we found */
845 if (memcmp(mh, buf, sizeof(struct NFTLMediaHeader))) {
847 printf("NFTL Media Headers at 0x%x and 0x%x disagree.\n",
848 nftl->MediaUnit * nftl->EraseSize, block * nftl->EraseSize);
850 /* if (debug) Print both side by side */
853 if (boot_record_count == 1)
854 nftl->SpareMediaUnit = block;
860 /* This is the first we've seen. Copy the media header structure into place */
861 memcpy(mh, buf, sizeof(struct NFTLMediaHeader));
863 /* Do some sanity checks on it */
864 if (mh->UnitSizeFactor != 0xff) {
865 puts ("Sorry, we don't support UnitSizeFactor "
870 nftl->nb_boot_blocks = le16_to_cpu(mh->FirstPhysicalEUN);
871 if ((nftl->nb_boot_blocks + 2) >= nftl->nb_blocks) {
872 printf ("NFTL Media Header sanity check failed:\n"
873 "nb_boot_blocks (%d) + 2 > nb_blocks (%d)\n",
874 nftl->nb_boot_blocks, nftl->nb_blocks);
878 nftl->numvunits = le32_to_cpu(mh->FormattedSize) / nftl->EraseSize;
879 if (nftl->numvunits > (nftl->nb_blocks - nftl->nb_boot_blocks - 2)) {
880 printf ("NFTL Media Header sanity check failed:\n"
881 "numvunits (%d) > nb_blocks (%d) - nb_boot_blocks(%d) - 2\n",
884 nftl->nb_boot_blocks);
888 nftl->nr_sects = nftl->numvunits * (nftl->EraseSize / SECTORSIZE);
890 /* If we're not using the last sectors in the device for some reason,
891 reduce nb_blocks accordingly so we forget they're there */
892 nftl->nb_blocks = le16_to_cpu(mh->NumEraseUnits) + le16_to_cpu(mh->FirstPhysicalEUN);
894 /* read the Bad Erase Unit Table and modify ReplUnitTable[] accordingly */
895 for (i = 0; i < nftl->nb_blocks; i++) {
896 if ((i & (SECTORSIZE - 1)) == 0) {
897 /* read one sector for every SECTORSIZE of blocks */
898 if ((ret = doc_read_ecc(nftl->mtd, block * nftl->EraseSize +
899 i + SECTORSIZE, SECTORSIZE,
900 &retlen, buf, (char *)&oob)) < 0) {
901 puts ("Read of bad sector table failed\n");
905 /* mark the Bad Erase Unit as RESERVED in ReplUnitTable */
906 if (buf[i & (SECTORSIZE - 1)] != 0xff)
907 nftl->ReplUnitTable[i] = BLOCK_RESERVED;
910 nftl->MediaUnit = block;
913 } /* foreach (block) */
915 return boot_record_count?0:-1;
918 /* This routine is made available to other mtd code via
919 * inter_module_register. It must only be accessed through
920 * inter_module_get which will bump the use count of this module. The
921 * addresses passed back in mtd are valid as long as the use count of
922 * this module is non-zero, i.e. between inter_module_get and
923 * inter_module_put. Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
925 static void DoC2k_init(struct DiskOnChip* this)
927 struct NFTLrecord *nftl;
929 switch (this->ChipID) {
930 case DOC_ChipID_Doc2k:
931 this->name = "DiskOnChip 2000";
932 this->ioreg = DoC_2k_CDSN_IO;
934 case DOC_ChipID_DocMil:
935 this->name = "DiskOnChip Millennium";
936 this->ioreg = DoC_Mil_CDSN_IO;
941 printf("%s found at address 0x%lX\n", this->name,
951 /* Ident all the chips present. */
956 /* Get physical parameters */
957 nftl->EraseSize = this->erasesize;
958 nftl->nb_blocks = this->totlen / this->erasesize;
961 if (find_boot_record(nftl) != 0)
962 this->nftl_found = 0;
964 this->nftl_found = 1;
966 printf("%s @ 0x%lX, %ld MB\n", this->name, this->physadr, this->totlen >> 20);
969 int doc_read_ecc(struct DiskOnChip* this, loff_t from, size_t len,
970 size_t * retlen, u_char * buf, u_char * eccbuf)
972 unsigned long docptr;
974 unsigned char syndrome[6];
976 int i, len256 = 0, ret=0;
978 docptr = this->virtadr;
980 /* Don't allow read past end of device */
981 if (from >= this->totlen) {
982 puts ("Out of flash\n");
986 /* Don't allow a single read to cross a 512-byte block boundary */
987 if (from + len > ((from | 0x1ff) + 1))
988 len = ((from | 0x1ff) + 1) - from;
990 /* The ECC will not be calculated correctly if less than 512 is read */
991 if (len != 0x200 && eccbuf)
992 printf("ECC needs a full sector read (adr: %lx size %lx)\n",
993 (long) from, (long) len);
996 printf("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len);
999 /* Find the chip which is to be used and select it */
1000 mychip = &this->chips[shr(from, this->chipshift)];
1002 if (this->curfloor != mychip->floor) {
1003 DoC_SelectFloor(this, mychip->floor);
1004 DoC_SelectChip(this, mychip->chip);
1005 } else if (this->curchip != mychip->chip) {
1006 DoC_SelectChip(this, mychip->chip);
1009 this->curfloor = mychip->floor;
1010 this->curchip = mychip->chip;
1014 && (from & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
1016 DoC_Address(this, ADDR_COLUMN_PAGE, from, CDSN_CTRL_WP,
1020 /* Prime the ECC engine */
1021 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
1022 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
1024 /* disable the ECC engine */
1025 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
1026 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
1029 /* treat crossing 256-byte sector for 2M x 8bits devices */
1030 if (this->page256 && from + len > (from | 0xff) + 1) {
1031 len256 = (from | 0xff) + 1 - from;
1032 DoC_ReadBuf(this, buf, len256);
1034 DoC_Command(this, NAND_CMD_READ0, CDSN_CTRL_WP);
1035 DoC_Address(this, ADDR_COLUMN_PAGE, from + len256,
1036 CDSN_CTRL_WP, CDSN_CTRL_ECC_IO);
1039 DoC_ReadBuf(this, &buf[len256], len - len256);
1041 /* Let the caller know we completed it */
1045 /* Read the ECC data through the DiskOnChip ECC logic */
1046 /* Note: this will work even with 2M x 8bit devices as */
1047 /* they have 8 bytes of OOB per 256 page. mf. */
1048 DoC_ReadBuf(this, eccbuf, 6);
1050 /* Flush the pipeline */
1051 if (DoC_is_Millennium(this)) {
1052 dummy = ReadDOC(docptr, ECCConf);
1053 dummy = ReadDOC(docptr, ECCConf);
1054 i = ReadDOC(docptr, ECCConf);
1056 dummy = ReadDOC(docptr, 2k_ECCStatus);
1057 dummy = ReadDOC(docptr, 2k_ECCStatus);
1058 i = ReadDOC(docptr, 2k_ECCStatus);
1061 /* Check the ECC Status */
1064 /* There was an ECC error */
1066 printf("DiskOnChip ECC Error: Read at %lx\n", (long)from);
1068 /* Read the ECC syndrom through the DiskOnChip ECC logic.
1069 These syndrome will be all ZERO when there is no error */
1070 for (i = 0; i < 6; i++) {
1072 ReadDOC(docptr, ECCSyndrome0 + i);
1074 nb_errors = doc_decode_ecc(buf, syndrome);
1077 printf("Errors corrected: %x\n", nb_errors);
1079 if (nb_errors < 0) {
1080 /* We return error, but have actually done the read. Not that
1081 this can be told to user-space, via sys_read(), but at least
1082 MTD-aware stuff can know about it by checking *retlen */
1083 printf("ECC Errors at %lx\n", (long)from);
1089 printf("ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
1090 (long)from, eccbuf[0], eccbuf[1], eccbuf[2],
1091 eccbuf[3], eccbuf[4], eccbuf[5]);
1094 /* disable the ECC engine */
1095 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
1098 /* according to 11.4.1, we need to wait for the busy line
1099 * drop if we read to the end of the page. */
1100 if(0 == ((from + *retlen) & 0x1ff))
1102 DoC_WaitReady(this);
1108 int doc_write_ecc(struct DiskOnChip* this, loff_t to, size_t len,
1109 size_t * retlen, const u_char * buf,
1112 int di; /* Yes, DI is a hangover from when I was disassembling the binary driver */
1113 unsigned long docptr;
1114 volatile char dummy;
1116 struct Nand *mychip;
1118 docptr = this->virtadr;
1120 /* Don't allow write past end of device */
1121 if (to >= this->totlen) {
1122 puts ("Out of flash\n");
1126 /* Don't allow a single write to cross a 512-byte block boundary */
1127 if (to + len > ((to | 0x1ff) + 1))
1128 len = ((to | 0x1ff) + 1) - to;
1130 /* The ECC will not be calculated correctly if less than 512 is written */
1131 if (len != 0x200 && eccbuf)
1132 printf("ECC needs a full sector write (adr: %lx size %lx)\n",
1133 (long) to, (long) len);
1135 /* printf("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */
1137 /* Find the chip which is to be used and select it */
1138 mychip = &this->chips[shr(to, this->chipshift)];
1140 if (this->curfloor != mychip->floor) {
1141 DoC_SelectFloor(this, mychip->floor);
1142 DoC_SelectChip(this, mychip->chip);
1143 } else if (this->curchip != mychip->chip) {
1144 DoC_SelectChip(this, mychip->chip);
1147 this->curfloor = mychip->floor;
1148 this->curchip = mychip->chip;
1150 /* Set device to main plane of flash */
1151 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
1154 && (to & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
1157 DoC_Command(this, NAND_CMD_SEQIN, 0);
1158 DoC_Address(this, ADDR_COLUMN_PAGE, to, 0, CDSN_CTRL_ECC_IO);
1161 /* Prime the ECC engine */
1162 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
1163 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
1165 /* disable the ECC engine */
1166 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
1167 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
1170 /* treat crossing 256-byte sector for 2M x 8bits devices */
1171 if (this->page256 && to + len > (to | 0xff) + 1) {
1172 len256 = (to | 0xff) + 1 - to;
1173 DoC_WriteBuf(this, buf, len256);
1175 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1177 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
1178 /* There's an implicit DoC_WaitReady() in DoC_Command */
1180 dummy = ReadDOC(docptr, CDSNSlowIO);
1183 if (ReadDOC_(docptr, this->ioreg) & 1) {
1184 puts ("Error programming flash\n");
1185 /* Error in programming */
1190 DoC_Command(this, NAND_CMD_SEQIN, 0);
1191 DoC_Address(this, ADDR_COLUMN_PAGE, to + len256, 0,
1195 DoC_WriteBuf(this, &buf[len256], len - len256);
1198 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_CE, docptr,
1201 if (DoC_is_Millennium(this)) {
1202 WriteDOC(0, docptr, NOP);
1203 WriteDOC(0, docptr, NOP);
1204 WriteDOC(0, docptr, NOP);
1206 WriteDOC_(0, docptr, this->ioreg);
1207 WriteDOC_(0, docptr, this->ioreg);
1208 WriteDOC_(0, docptr, this->ioreg);
1211 /* Read the ECC data through the DiskOnChip ECC logic */
1212 for (di = 0; di < 6; di++) {
1213 eccbuf[di] = ReadDOC(docptr, ECCSyndrome0 + di);
1216 /* Reset the ECC engine */
1217 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
1221 ("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
1222 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
1223 eccbuf[4], eccbuf[5]);
1227 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1229 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
1230 /* There's an implicit DoC_WaitReady() in DoC_Command */
1232 dummy = ReadDOC(docptr, CDSNSlowIO);
1235 if (ReadDOC_(docptr, this->ioreg) & 1) {
1236 puts ("Error programming flash\n");
1237 /* Error in programming */
1242 /* Let the caller know we completed it */
1250 /* Write the ECC data to flash */
1251 for (di=0; di<6; di++)
1257 ret = doc_write_oob(this, to, 8, &dummy, x);
1263 int doc_read_oob(struct DiskOnChip* this, loff_t ofs, size_t len,
1264 size_t * retlen, u_char * buf)
1266 int len256 = 0, ret;
1267 unsigned long docptr;
1268 struct Nand *mychip;
1270 docptr = this->virtadr;
1272 mychip = &this->chips[shr(ofs, this->chipshift)];
1274 if (this->curfloor != mychip->floor) {
1275 DoC_SelectFloor(this, mychip->floor);
1276 DoC_SelectChip(this, mychip->chip);
1277 } else if (this->curchip != mychip->chip) {
1278 DoC_SelectChip(this, mychip->chip);
1280 this->curfloor = mychip->floor;
1281 this->curchip = mychip->chip;
1283 /* update address for 2M x 8bit devices. OOB starts on the second */
1284 /* page to maintain compatibility with doc_read_ecc. */
1285 if (this->page256) {
1292 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1293 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, CDSN_CTRL_WP, 0);
1295 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1296 /* Note: datasheet says it should automaticaly wrap to the */
1297 /* next OOB block, but it didn't work here. mf. */
1298 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1299 len256 = (ofs | 0x7) + 1 - ofs;
1300 DoC_ReadBuf(this, buf, len256);
1302 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1303 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff),
1307 DoC_ReadBuf(this, &buf[len256], len - len256);
1310 /* Reading the full OOB data drops us off of the end of the page,
1311 * causing the flash device to go into busy mode, so we need
1312 * to wait until ready 11.4.1 and Toshiba TC58256FT docs */
1314 ret = DoC_WaitReady(this);
1320 int doc_write_oob(struct DiskOnChip* this, loff_t ofs, size_t len,
1321 size_t * retlen, const u_char * buf)
1324 unsigned long docptr = this->virtadr;
1325 struct Nand *mychip = &this->chips[shr(ofs, this->chipshift)];
1329 printf("doc_write_oob(%lx, %d): %2.2X %2.2X %2.2X %2.2X ... %2.2X %2.2X .. %2.2X %2.2X\n",
1330 (long)ofs, len, buf[0], buf[1], buf[2], buf[3],
1331 buf[8], buf[9], buf[14],buf[15]);
1334 /* Find the chip which is to be used and select it */
1335 if (this->curfloor != mychip->floor) {
1336 DoC_SelectFloor(this, mychip->floor);
1337 DoC_SelectChip(this, mychip->chip);
1338 } else if (this->curchip != mychip->chip) {
1339 DoC_SelectChip(this, mychip->chip);
1341 this->curfloor = mychip->floor;
1342 this->curchip = mychip->chip;
1344 /* disable the ECC engine */
1345 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
1346 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
1348 /* Reset the chip, see Software Requirement 11.4 item 1. */
1349 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
1351 /* issue the Read2 command to set the pointer to the Spare Data Area. */
1352 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1354 /* update address for 2M x 8bit devices. OOB starts on the second */
1355 /* page to maintain compatibility with doc_read_ecc. */
1356 if (this->page256) {
1363 /* issue the Serial Data In command to initial the Page Program process */
1364 DoC_Command(this, NAND_CMD_SEQIN, 0);
1365 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, 0, 0);
1367 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1368 /* Note: datasheet says it should automaticaly wrap to the */
1369 /* next OOB block, but it didn't work here. mf. */
1370 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1371 len256 = (ofs | 0x7) + 1 - ofs;
1372 DoC_WriteBuf(this, buf, len256);
1374 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1375 DoC_Command(this, NAND_CMD_STATUS, 0);
1376 /* DoC_WaitReady() is implicit in DoC_Command */
1378 dummy = ReadDOC(docptr, CDSNSlowIO);
1381 if (ReadDOC_(docptr, this->ioreg) & 1) {
1382 puts ("Error programming oob data\n");
1383 /* There was an error */
1387 DoC_Command(this, NAND_CMD_SEQIN, 0);
1388 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), 0, 0);
1391 DoC_WriteBuf(this, &buf[len256], len - len256);
1393 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1394 DoC_Command(this, NAND_CMD_STATUS, 0);
1395 /* DoC_WaitReady() is implicit in DoC_Command */
1397 dummy = ReadDOC(docptr, CDSNSlowIO);
1400 if (ReadDOC_(docptr, this->ioreg) & 1) {
1401 puts ("Error programming oob data\n");
1402 /* There was an error */
1412 int doc_erase(struct DiskOnChip* this, loff_t ofs, size_t len)
1415 unsigned long docptr;
1416 struct Nand *mychip;
1418 if (ofs & (this->erasesize-1) || len & (this->erasesize-1)) {
1419 puts ("Offset and size must be sector aligned\n");
1423 docptr = this->virtadr;
1425 /* FIXME: Do this in the background. Use timers or schedule_task() */
1427 mychip = &this->chips[shr(ofs, this->chipshift)];
1429 if (this->curfloor != mychip->floor) {
1430 DoC_SelectFloor(this, mychip->floor);
1431 DoC_SelectChip(this, mychip->chip);
1432 } else if (this->curchip != mychip->chip) {
1433 DoC_SelectChip(this, mychip->chip);
1435 this->curfloor = mychip->floor;
1436 this->curchip = mychip->chip;
1438 DoC_Command(this, NAND_CMD_ERASE1, 0);
1439 DoC_Address(this, ADDR_PAGE, ofs, 0, 0);
1440 DoC_Command(this, NAND_CMD_ERASE2, 0);
1442 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
1444 dummy = ReadDOC(docptr, CDSNSlowIO);
1447 if (ReadDOC_(docptr, this->ioreg) & 1) {
1448 printf("Error erasing at 0x%lx\n", (long)ofs);
1449 /* There was an error */
1452 ofs += this->erasesize;
1453 len -= this->erasesize;
1460 static inline int doccheck(unsigned long potential, unsigned long physadr)
1462 unsigned long window=potential;
1463 unsigned char tmp, ChipID;
1464 #ifndef DOC_PASSIVE_PROBE
1468 /* Routine copied from the Linux DOC driver */
1470 #ifdef CFG_DOCPROBE_55AA
1471 /* Check for 0x55 0xAA signature at beginning of window,
1472 this is no longer true once we remove the IPL (for Millennium */
1473 if (ReadDOC(window, Sig1) != 0x55 || ReadDOC(window, Sig2) != 0xaa)
1475 #endif /* CFG_DOCPROBE_55AA */
1477 #ifndef DOC_PASSIVE_PROBE
1478 /* It's not possible to cleanly detect the DiskOnChip - the
1479 * bootup procedure will put the device into reset mode, and
1480 * it's not possible to talk to it without actually writing
1481 * to the DOCControl register. So we store the current contents
1482 * of the DOCControl register's location, in case we later decide
1483 * that it's not a DiskOnChip, and want to put it back how we
1486 tmp2 = ReadDOC(window, DOCControl);
1488 /* Reset the DiskOnChip ASIC */
1489 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET,
1490 window, DOCControl);
1491 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET,
1492 window, DOCControl);
1494 /* Enable the DiskOnChip ASIC */
1495 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL,
1496 window, DOCControl);
1497 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL,
1498 window, DOCControl);
1499 #endif /* !DOC_PASSIVE_PROBE */
1501 ChipID = ReadDOC(window, ChipID);
1504 case DOC_ChipID_Doc2k:
1505 /* Check the TOGGLE bit in the ECC register */
1506 tmp = ReadDOC(window, 2k_ECCStatus) & DOC_TOGGLE_BIT;
1507 if ((ReadDOC(window, 2k_ECCStatus) & DOC_TOGGLE_BIT) != tmp)
1511 case DOC_ChipID_DocMil:
1512 /* Check the TOGGLE bit in the ECC register */
1513 tmp = ReadDOC(window, ECCConf) & DOC_TOGGLE_BIT;
1514 if ((ReadDOC(window, ECCConf) & DOC_TOGGLE_BIT) != tmp)
1519 #ifndef CFG_DOCPROBE_55AA
1521 * if the ID isn't the DoC2000 or DoCMillenium ID, so we can assume
1522 * the DOC is missing
1525 printf("Possible DiskOnChip with unknown ChipID %2.2X found at 0x%lx\n",
1529 #ifndef DOC_PASSIVE_PROBE
1530 /* Put back the contents of the DOCControl register, in case it's not
1531 * actually a DiskOnChip.
1533 WriteDOC(tmp2, window, DOCControl);
1538 puts ("DiskOnChip failed TOGGLE test, dropping.\n");
1540 #ifndef DOC_PASSIVE_PROBE
1541 /* Put back the contents of the DOCControl register: it's not a DiskOnChip */
1542 WriteDOC(tmp2, window, DOCControl);
1547 void doc_probe(unsigned long physadr)
1549 struct DiskOnChip *this = NULL;
1552 if ((ChipID = doccheck(physadr, physadr))) {
1554 for (i=0; i<CFG_MAX_DOC_DEVICE; i++) {
1555 if (doc_dev_desc[i].ChipID == DOC_ChipID_UNKNOWN) {
1556 this = doc_dev_desc + i;
1562 puts ("Cannot allocate memory for data structures.\n");
1566 if (curr_device == -1)
1569 memset((char *)this, 0, sizeof(struct DiskOnChip));
1571 this->virtadr = physadr;
1572 this->physadr = physadr;
1573 this->ChipID = ChipID;
1577 puts ("No DiskOnChip found\n");
1581 #endif /* (CONFIG_COMMANDS & CFG_CMD_DOC) */