2 * Driver for NAND support, Rick Bronson
3 * borrowed heavily from:
4 * (c) 1999 Machine Vision Holdings, Inc.
5 * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
14 #ifdef CONFIG_SHOW_BOOT_PROGRESS
15 # include <status_led.h>
16 # define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
18 # define SHOW_BOOT_PROGRESS(arg)
21 #if (CONFIG_COMMANDS & CFG_CMD_NAND)
23 #include <linux/mtd/nftl.h>
24 #include <linux/mtd/nand.h>
25 #include <linux/mtd/nand_ids.h>
28 * Definition of the out of band configuration structure
30 struct nand_oob_config {
31 int ecc_pos[6]; /* position of ECC bytes inside oob */
32 int badblock_pos; /* position of bad block flag inside oob -1 = inactive */
33 int eccvalid_pos; /* position of ECC valid flag inside oob -1 = inactive */
34 } oob_config = { {0}, 0, 0};
41 #define CONFIG_MTD_NAND_ECC /* enable ECC */
42 /* #define CONFIG_MTD_NAND_ECC_JFFS2 */
47 static void nand_print(struct nand_chip *nand);
48 static int nand_rw (struct nand_chip* nand, int cmd,
49 size_t start, size_t len,
50 size_t * retlen, u_char * buf);
51 static int nand_erase(struct nand_chip* nand, size_t ofs, size_t len);
52 static int nand_read_ecc(struct nand_chip *nand, size_t start, size_t len,
53 size_t * retlen, u_char *buf, u_char *ecc_code);
54 static int nand_write_ecc (struct nand_chip* nand, size_t to, size_t len,
55 size_t * retlen, const u_char * buf, u_char * ecc_code);
56 #ifdef CONFIG_MTD_NAND_ECC
57 static int nand_correct_data (u_char *dat, u_char *read_ecc, u_char *calc_ecc);
58 static void nand_calculate_ecc (const u_char *dat, u_char *ecc_code);
61 static struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE] = {{0}};
63 /* Current NAND Device */
64 static int curr_device = -1;
66 /* ------------------------------------------------------------------------- */
68 int do_nand (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
75 printf ("Usage:\n%s\n", cmdtp->usage);
78 if (strcmp(argv[1],"info") == 0) {
83 for (i=0; i<CFG_MAX_NAND_DEVICE; ++i) {
84 if(nand_dev_desc[i].ChipID == NAND_ChipID_UNKNOWN)
85 continue; /* list only known devices */
86 printf ("Device %d: ", i);
87 nand_print(&nand_dev_desc[i]);
91 } else if (strcmp(argv[1],"device") == 0) {
92 if ((curr_device < 0) || (curr_device >= CFG_MAX_NAND_DEVICE)) {
93 puts ("\nno devices available\n");
96 printf ("\nDevice %d: ", curr_device);
97 nand_print(&nand_dev_desc[curr_device]);
100 printf ("Usage:\n%s\n", cmdtp->usage);
103 if (strcmp(argv[1],"device") == 0) {
104 int dev = (int)simple_strtoul(argv[2], NULL, 10);
106 printf ("\nDevice %d: ", dev);
107 if (dev >= CFG_MAX_NAND_DEVICE) {
108 puts ("unknown device\n");
111 nand_print(&nand_dev_desc[dev]);
112 /*nand_print (dev);*/
114 if (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN) {
120 puts ("... is now current device\n");
125 printf ("Usage:\n%s\n", cmdtp->usage);
128 /* at least 4 args */
130 if (strcmp(argv[1],"read") == 0 || strcmp(argv[1],"write") == 0) {
131 ulong addr = simple_strtoul(argv[2], NULL, 16);
132 ulong off = simple_strtoul(argv[3], NULL, 16);
133 ulong size = simple_strtoul(argv[4], NULL, 16);
134 int cmd = (strcmp(argv[1],"read") == 0);
137 printf ("\nNAND %s: device %d offset %ld, size %ld ... ",
138 cmd ? "read" : "write", curr_device, off, size);
140 ret = nand_rw(nand_dev_desc + curr_device, cmd, off, size,
141 &total, (u_char*)addr);
143 printf ("%d bytes %s: %s\n", total, cmd ? "read" : "write",
144 ret ? "ERROR" : "OK");
147 } else if (strcmp(argv[1],"erase") == 0) {
148 ulong off = simple_strtoul(argv[2], NULL, 16);
149 ulong size = simple_strtoul(argv[3], NULL, 16);
152 printf ("\nNAND erase: device %d offset %ld, size %ld ... ",
153 curr_device, off, size);
155 ret = nand_erase (nand_dev_desc + curr_device, off, size);
157 printf("%s\n", ret ? "ERROR" : "OK");
161 printf ("Usage:\n%s\n", cmdtp->usage);
169 int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
171 char *boot_device = NULL;
181 addr = CFG_LOAD_ADDR;
182 boot_device = getenv ("bootdevice");
185 addr = simple_strtoul(argv[1], NULL, 16);
186 boot_device = getenv ("bootdevice");
189 addr = simple_strtoul(argv[1], NULL, 16);
190 boot_device = argv[2];
193 addr = simple_strtoul(argv[1], NULL, 16);
194 boot_device = argv[2];
195 offset = simple_strtoul(argv[3], NULL, 16);
198 printf ("Usage:\n%s\n", cmdtp->usage);
199 SHOW_BOOT_PROGRESS (-1);
204 puts ("\n** No boot device **\n");
205 SHOW_BOOT_PROGRESS (-1);
209 dev = simple_strtoul(boot_device, &ep, 16);
211 if ((dev >= CFG_MAX_NAND_DEVICE) ||
212 (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN)) {
213 printf ("\n** Device %d not available\n", dev);
214 SHOW_BOOT_PROGRESS (-1);
218 printf ("\nLoading from device %d: %s at 0x%lX (offset 0x%lX)\n",
219 dev, nand_dev_desc[dev].name, nand_dev_desc[dev].IO_ADDR,
222 if (nand_rw (nand_dev_desc + dev, 1, offset,
223 SECTORSIZE, NULL, (u_char *)addr)) {
224 printf ("** Read error on %d\n", dev);
225 SHOW_BOOT_PROGRESS (-1);
229 hdr = (image_header_t *)addr;
231 if (ntohl(hdr->ih_magic) == IH_MAGIC) {
233 print_image_hdr (hdr);
235 cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
238 printf ("\n** Bad Magic Number 0x%x **\n", hdr->ih_magic);
239 SHOW_BOOT_PROGRESS (-1);
243 if (nand_rw (nand_dev_desc + dev, 1, offset + SECTORSIZE, cnt,
244 NULL, (u_char *)(addr+SECTORSIZE))) {
245 printf ("** Read error on %d\n", dev);
246 SHOW_BOOT_PROGRESS (-1);
250 /* Loading ok, update default load address */
254 /* Check if we should attempt an auto-start */
255 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
257 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
259 local_args[0] = argv[0];
260 local_args[1] = NULL;
262 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
264 do_bootm (cmdtp, 0, 1, local_args);
270 static int nand_rw (struct nand_chip* nand, int cmd,
271 size_t start, size_t len,
272 size_t * retlen, u_char * buf)
274 int noecc, ret = 0, n, total = 0;
278 /* The ECC will not be calculated correctly if
279 less than 512 is written or read */
280 noecc = (start != (start | 0x1ff) + 1) || (len < 0x200);
282 ret = nand_read_ecc(nand, start, len,
284 noecc ? NULL : eccbuf);
286 ret = nand_write_ecc(nand, start, len,
288 noecc ? NULL : eccbuf);
304 static void nand_print(struct nand_chip *nand)
306 printf("%s at 0x%lX,\n"
307 "\t %d chip%s %s, size %d MB, \n"
308 "\t total size %ld MB, sector size %ld kB\n",
309 nand->name, nand->IO_ADDR, nand->numchips,
310 nand->numchips>1 ? "s" : "", nand->chips_name,
311 1 << (nand->chipshift - 20),
312 nand->totlen >> 20, nand->erasesize >> 10);
314 if (nand->nftl_found) {
315 struct NFTLrecord *nftl = &nand->nftl;
316 unsigned long bin_size, flash_size;
318 bin_size = nftl->nb_boot_blocks * nand->erasesize;
319 flash_size = (nftl->nb_blocks - nftl->nb_boot_blocks) * nand->erasesize;
321 printf("\t NFTL boot record:\n"
322 "\t Binary partition: size %ld%s\n"
323 "\t Flash disk partition: size %ld%s, offset 0x%lx\n",
324 bin_size > (1 << 20) ? bin_size >> 20 : bin_size >> 10,
325 bin_size > (1 << 20) ? "MB" : "kB",
326 flash_size > (1 << 20) ? flash_size >> 20 : flash_size >> 10,
327 flash_size > (1 << 20) ? "MB" : "kB", bin_size);
329 puts ("\t No NFTL boot record found.\n");
333 /* ------------------------------------------------------------------------- */
335 /* This function is needed to avoid calls of the __ashrdi3 function. */
336 static int shr(int val, int shift)
341 static int NanD_WaitReady(struct nand_chip *nand)
343 /* This is inline, to optimise the common case, where it's ready instantly */
345 NAND_WAIT_READY(nand);
350 /* NanD_Command: Send a flash command to the flash chip */
352 static inline int NanD_Command(struct nand_chip *nand, unsigned char command)
354 unsigned long nandptr = nand->IO_ADDR;
356 /* Assert the CLE (Command Latch Enable) line to the flash chip */
357 NAND_CTL_SETCLE(nandptr);
359 /* Send the command */
360 WRITE_NAND_COMMAND(command, nandptr);
362 /* Lower the CLE line */
363 NAND_CTL_CLRCLE(nandptr);
365 return NanD_WaitReady(nand);
368 /* NanD_Address: Set the current address for the flash chip */
370 static int NanD_Address(struct nand_chip *nand, int numbytes, unsigned long ofs)
372 unsigned long nandptr;
375 nandptr = nand->IO_ADDR;
377 /* Assert the ALE (Address Latch Enable) line to the flash chip */
378 NAND_CTL_SETALE(nandptr);
380 /* Send the address */
381 /* Devices with 256-byte page are addressed as:
382 Column (bits 0-7), Page (bits 8-15, 16-23, 24-31)
383 * there is no device on the market with page256
384 and more than 24 bits.
385 Devices with 512-byte page are addressed as:
386 Column (bits 0-7), Page (bits 9-16, 17-24, 25-31)
387 * 25-31 is sent only if the chip support it.
388 * bit 8 changes the read command to be sent
389 (NAND_CMD_READ0 or NAND_CMD_READ1).
392 if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE)
393 WRITE_NAND_ADDRESS(ofs, nandptr);
395 ofs = ofs >> nand->page_shift;
397 if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE)
398 for (i = 0; i < nand->pageadrlen; i++, ofs = ofs >> 8)
399 WRITE_NAND_ADDRESS(ofs, nandptr);
401 /* Lower the ALE line */
402 NAND_CTL_CLRALE(nandptr);
404 /* Wait for the chip to respond */
405 return NanD_WaitReady(nand);
408 /* NanD_SelectChip: Select a given flash chip within the current floor */
410 static inline int NanD_SelectChip(struct nand_chip *nand, int chip)
412 /* Wait for it to be ready */
413 return NanD_WaitReady(nand);
416 /* NanD_IdentChip: Identify a given NAND chip given {floor,chip} */
418 static int NanD_IdentChip(struct nand_chip *nand, int floor, int chip)
422 NAND_ENABLE_CE(nand); /* set pin low */
424 if (NanD_Command(nand, NAND_CMD_RESET)) {
426 printf("NanD_Command (reset) for %d,%d returned true\n",
429 NAND_DISABLE_CE(nand); /* set pin high */
433 /* Read the NAND chip ID: 1. Send ReadID command */
434 if (NanD_Command(nand, NAND_CMD_READID)) {
436 printf("NanD_Command (ReadID) for %d,%d returned true\n",
439 NAND_DISABLE_CE(nand); /* set pin high */
443 /* Read the NAND chip ID: 2. Send address byte zero */
444 NanD_Address(nand, ADDR_COLUMN, 0);
446 /* Read the manufacturer and device id codes from the device */
448 mfr = READ_NAND(nand->IO_ADDR);
450 id = READ_NAND(nand->IO_ADDR);
452 NAND_DISABLE_CE(nand); /* set pin high */
453 /* No response - return failure */
454 if (mfr == 0xff || mfr == 0)
456 printf("NanD_Command (ReadID) got %d %d\n", mfr, id);
460 /* Check it's the same as the first chip we identified.
461 * M-Systems say that any given nand_chip device should only
462 * contain _one_ type of flash part, although that's not a
463 * hardware restriction. */
465 if (nand->mfr == mfr && nand->id == id)
466 return 1; /* This is another the same the first */
468 printf("Flash chip at floor %d, chip %d is different:\n",
472 /* Print and store the manufacturer and ID codes. */
473 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
474 if (mfr == nand_flash_ids[i].manufacture_id &&
475 id == nand_flash_ids[i].model_id) {
477 printf("Flash chip found:\n\t Manufacturer ID: 0x%2.2X, "
478 "Chip ID: 0x%2.2X (%s)\n", mfr, id,
479 nand_flash_ids[i].name);
485 nand_flash_ids[i].chipshift;
486 nand->page256 = nand_flash_ids[i].page256;
488 nand->oobblock = 256;
490 nand->page_shift = 8;
492 nand->oobblock = 512;
494 nand->page_shift = 9;
497 nand_flash_ids[i].pageadrlen;
499 nand_flash_ids[i].erasesize;
501 nand_flash_ids[i].name;
510 /* We haven't fully identified the chip. Print as much as we know. */
511 printf("Unknown flash chip found: %2.2X %2.2X\n",
518 /* NanD_ScanChips: Find all NAND chips present in a nand_chip, and identify them */
520 static void NanD_ScanChips(struct nand_chip *nand)
523 int numchips[NAND_MAX_FLOORS];
524 int maxchips = NAND_MAX_CHIPS;
532 /* For each floor, find the number of valid chips it contains */
533 for (floor = 0; floor < NAND_MAX_FLOORS; floor++) {
536 for (chip = 0; chip < maxchips && ret != 0; chip++) {
538 ret = NanD_IdentChip(nand, floor, chip);
546 /* If there are none at all that we recognise, bail */
547 if (!nand->numchips) {
548 puts ("No flash chips recognised.\n");
552 /* Allocate an array to hold the information for each chip */
553 nand->chips = malloc(sizeof(struct Nand) * nand->numchips);
555 puts ("No memory for allocating chip info structures\n");
561 /* Fill out the chip array with {floor, chipno} for each
562 * detected chip in the device. */
563 for (floor = 0; floor < NAND_MAX_FLOORS; floor++) {
564 for (chip = 0; chip < numchips[floor]; chip++) {
565 nand->chips[ret].floor = floor;
566 nand->chips[ret].chip = chip;
567 nand->chips[ret].curadr = 0;
568 nand->chips[ret].curmode = 0x50;
573 /* Calculate and print the total size of the device */
574 nand->totlen = nand->numchips * (1 << nand->chipshift);
577 printf("%d flash chips found. Total nand_chip size: %ld MB\n",
578 nand->numchips, nand->totlen >> 20);
581 #ifdef CONFIG_MTD_NAND_ECC
582 /* we need to be fast here, 1 us per read translates to 1 second per meg */
583 static void nand_fast_copy (unsigned char *source, unsigned char *dest, long cntr)
612 /* we need to be fast here, 1 us per read translates to 1 second per meg */
613 static void nand_fast_read(unsigned char *data_buf, int cntr, unsigned long nandptr)
617 *data_buf++ = READ_NAND(nandptr);
618 *data_buf++ = READ_NAND(nandptr);
619 *data_buf++ = READ_NAND(nandptr);
620 *data_buf++ = READ_NAND(nandptr);
621 *data_buf++ = READ_NAND(nandptr);
622 *data_buf++ = READ_NAND(nandptr);
623 *data_buf++ = READ_NAND(nandptr);
624 *data_buf++ = READ_NAND(nandptr);
625 *data_buf++ = READ_NAND(nandptr);
626 *data_buf++ = READ_NAND(nandptr);
627 *data_buf++ = READ_NAND(nandptr);
628 *data_buf++ = READ_NAND(nandptr);
629 *data_buf++ = READ_NAND(nandptr);
630 *data_buf++ = READ_NAND(nandptr);
631 *data_buf++ = READ_NAND(nandptr);
632 *data_buf++ = READ_NAND(nandptr);
637 *data_buf++ = READ_NAND(nandptr);
642 /* This routine is made available to other mtd code via
643 * inter_module_register. It must only be accessed through
644 * inter_module_get which will bump the use count of this module. The
645 * addresses passed back in mtd are valid as long as the use count of
646 * this module is non-zero, i.e. between inter_module_get and
647 * inter_module_put. Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
653 static int nand_read_ecc(struct nand_chip *nand, size_t start, size_t len,
654 size_t * retlen, u_char *buf, u_char *ecc_code)
658 #ifdef CONFIG_MTD_NAND_ECC
664 unsigned long nandptr = nand->IO_ADDR;
666 /* Do not allow reads past end of device */
667 if ((start + len) > nand->totlen) {
668 printf ("nand_read_ecc: Attempt read beyond end of device %x %x %x\n", (uint) start, (uint) len, (uint) nand->totlen);
673 /* First we calculate the starting page */
674 page = shr(start, nand->page_shift);
676 /* Get raw starting column */
677 col = start & (nand->oobblock - 1);
679 /* Initialize return value */
682 /* Select the NAND device */
683 NAND_ENABLE_CE(nand); /* set pin low */
685 /* Loop until all data read */
686 while (*retlen < len) {
689 #ifdef CONFIG_MTD_NAND_ECC
691 /* Do we have this page in cache ? */
692 if (nand->cache_page == page)
694 /* Send the read command */
695 NanD_Command(nand, NAND_CMD_READ0);
696 NanD_Address(nand, ADDR_COLUMN_PAGE, (page << nand->page_shift) + col);
697 /* Read in a page + oob data */
698 nand_fast_read(nand->data_buf, nand->oobblock + nand->oobsize, nandptr);
700 /* copy data into cache, for read out of cache and if ecc fails */
701 if (nand->data_cache)
702 memcpy (nand->data_cache, nand->data_buf, nand->oobblock + nand->oobsize);
704 /* Pick the ECC bytes out of the oob data */
705 for (j = 0; j < 6; j++)
706 ecc_code[j] = nand->data_buf[(nand->oobblock + oob_config.ecc_pos[j])];
708 /* Calculate the ECC and verify it */
709 /* If block was not written with ECC, skip ECC */
710 if (oob_config.eccvalid_pos != -1 &&
711 (nand->data_buf[nand->oobblock + oob_config.eccvalid_pos] & 0x0f) != 0x0f) {
713 nand_calculate_ecc (&nand->data_buf[0], &ecc_calc[0]);
714 switch (nand_correct_data (&nand->data_buf[0], &ecc_code[0], &ecc_calc[0])) {
716 printf ("nand_read_ecc: " "Failed ECC read, page 0x%08x\n", page);
720 case 2: /* transfer ECC corrected data to cache */
721 memcpy (nand->data_cache, nand->data_buf, 256);
726 if (oob_config.eccvalid_pos != -1 &&
727 nand->oobblock == 512 && (nand->data_buf[nand->oobblock + oob_config.eccvalid_pos] & 0xf0) != 0xf0) {
729 nand_calculate_ecc (&nand->data_buf[256], &ecc_calc[3]);
730 switch (nand_correct_data (&nand->data_buf[256], &ecc_code[3], &ecc_calc[3])) {
732 printf ("nand_read_ecc: " "Failed ECC read, page 0x%08x\n", page);
736 case 2: /* transfer ECC corrected data to cache */
737 if (nand->data_cache)
738 memcpy (&nand->data_cache[256], &nand->data_buf[256], 256);
743 /* Read the data from ECC data buffer into return buffer */
744 data_poi = (nand->data_cache) ? nand->data_cache : nand->data_buf;
746 if ((*retlen + (nand->oobblock - col)) >= len) {
747 nand_fast_copy (data_poi, buf + *retlen, len - *retlen);
750 nand_fast_copy (data_poi, buf + *retlen, nand->oobblock - col);
751 *retlen += nand->oobblock - col;
753 /* Set cache page address, invalidate, if ecc_failed */
754 nand->cache_page = (nand->data_cache && !ecc_failed) ? page : -1;
756 ecc_status += ecc_failed;
760 /* Send the read command */
761 NanD_Command(nand, NAND_CMD_READ0);
762 NanD_Address(nand, ADDR_COLUMN_PAGE, (page << nand->page_shift) + col);
763 /* Read the data directly into the return buffer */
764 if ((*retlen + (nand->oobblock - col)) >= len) {
765 nand_fast_read(buf + *retlen, len - *retlen, nandptr);
770 nand_fast_read(buf + *retlen, nand->oobblock - col, nandptr);
771 *retlen += nand->oobblock - col;
774 /* For subsequent reads align to page boundary. */
776 /* Increment page address */
780 /* De-select the NAND device */
781 NAND_DISABLE_CE(nand); /* set pin high */
784 * Return success, if no ECC failures, else -EIO
785 * fs driver will take care of that, because
786 * retlen == desired len and result == -EIO
788 return ecc_status ? -1 : 0;
793 * Nand_page_program function is used for write and writev !
795 static int nand_write_page (struct nand_chip *nand,
796 int page, int col, int last, u_char * ecc_code)
800 #ifdef CONFIG_MTD_NAND_ECC
801 unsigned long nandptr = nand->IO_ADDR;
802 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
803 int ecc_bytes = (nand->oobblock == 512) ? 6 : 3;
807 for (i = nand->oobblock; i < nand->oobblock + nand->oobsize; i++)
808 nand->data_buf[i] = 0xff;
810 #ifdef CONFIG_MTD_NAND_ECC
811 /* Zero out the ECC array */
812 for (i = 0; i < 6; i++)
815 /* Read back previous written data, if col > 0 */
817 NanD_Command(nand, NAND_CMD_READ0);
818 NanD_Address(nand, ADDR_COLUMN_PAGE, (page << nand->page_shift) + col);
819 for (i = 0; i < col; i++)
820 nand->data_buf[i] = READ_NAND (nandptr);
823 /* Calculate and write the ECC if we have enough data */
824 if ((col < nand->eccsize) && (last >= nand->eccsize)) {
825 nand_calculate_ecc (&nand->data_buf[0], &(ecc_code[0]));
826 for (i = 0; i < 3; i++)
827 nand->data_buf[(nand->oobblock + oob_config.ecc_pos[i])] = ecc_code[i];
828 if (oob_config.eccvalid_pos != -1)
829 nand->data_buf[nand->oobblock + oob_config.eccvalid_pos] = 0xf0;
832 /* Calculate and write the second ECC if we have enough data */
833 if ((nand->oobblock == 512) && (last == nand->oobblock)) {
834 nand_calculate_ecc (&nand->data_buf[256], &(ecc_code[3]));
835 for (i = 3; i < 6; i++)
836 nand->data_buf[(nand->oobblock + oob_config.ecc_pos[i])] = ecc_code[i];
837 if (oob_config.eccvalid_pos != -1)
838 nand->data_buf[nand->oobblock + oob_config.eccvalid_pos] &= 0x0f;
841 /* Prepad for partial page programming !!! */
842 for (i = 0; i < col; i++)
843 nand->data_buf[i] = 0xff;
845 /* Postpad for partial page programming !!! oob is already padded */
846 for (i = last; i < nand->oobblock; i++)
847 nand->data_buf[i] = 0xff;
849 /* Send command to begin auto page programming */
850 NanD_Command(nand, NAND_CMD_SEQIN);
851 NanD_Address(nand, ADDR_COLUMN_PAGE, (page << nand->page_shift) + col);
853 /* Write out complete page of data */
854 for (i = 0; i < (nand->oobblock + nand->oobsize); i++)
855 WRITE_NAND(nand->data_buf[i], nand->IO_ADDR);
857 /* Send command to actually program the data */
858 NanD_Command(nand, NAND_CMD_PAGEPROG);
859 NanD_Command(nand, NAND_CMD_STATUS);
861 /* See if device thinks it succeeded */
862 if (READ_NAND(nand->IO_ADDR) & 0x01) {
863 printf ("nand_write_ecc: " "Failed write, page 0x%08x, ", page);
866 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
868 * The NAND device assumes that it is always writing to
869 * a cleanly erased page. Hence, it performs its internal
870 * write verification only on bits that transitioned from
871 * 1 to 0. The device does NOT verify the whole page on a
872 * byte by byte basis. It is possible that the page was
873 * not completely erased or the page is becoming unusable
874 * due to wear. The read with ECC would catch the error
875 * later when the ECC page check fails, but we would rather
876 * catch it early in the page write stage. Better to write
877 * no data than invalid data.
880 /* Send command to read back the page */
881 if (col < nand->eccsize)
882 NanD_Command(nand, NAND_CMD_READ0);
884 NanD_Command(nand, NAND_CMD_READ1);
885 NanD_Address(nand, ADDR_COLUMN_PAGE, (page << nand->page_shift) + col);
887 /* Loop through and verify the data */
888 for (i = col; i < last; i++) {
889 if (nand->data_buf[i] != readb (nand->IO_ADDR)) {
890 printf ("nand_write_ecc: " "Failed write verify, page 0x%08x ", page);
895 #ifdef CONFIG_MTD_NAND_ECC
897 * We also want to check that the ECC bytes wrote
898 * correctly for the same reasons stated above.
900 NanD_Command(nand, NAND_CMD_READOOB);
901 NanD_Address(nand, ADDR_COLUMN_PAGE, (page << nand->page_shift) + col);
902 for (i = 0; i < nand->oobsize; i++)
903 nand->data_buf[i] = readb (nand->IO_ADDR);
904 for (i = 0; i < ecc_bytes; i++) {
905 if ((nand->data_buf[(oob_config.ecc_pos[i])] != ecc_code[i]) && ecc_code[i]) {
906 printf ("nand_write_ecc: Failed ECC write "
907 "verify, page 0x%08x, " "%6i bytes were succesful\n", page, i);
915 static int nand_write_ecc (struct nand_chip* nand, size_t to, size_t len,
916 size_t * retlen, const u_char * buf, u_char * ecc_code)
918 int i, page, col, cnt, ret = 0;
920 /* Do not allow write past end of device */
921 if ((to + len) > nand->totlen) {
922 printf ("nand_write_oob: Attempt to write past end of page\n");
926 /* Shift to get page */
927 page = ((int) to) >> nand->page_shift;
929 /* Get the starting column */
930 col = to & (nand->oobblock - 1);
932 /* Initialize return length value */
935 /* Select the NAND device */
936 NAND_ENABLE_CE(nand); /* set pin low */
938 /* Check the WP bit */
939 NanD_Command(nand, NAND_CMD_STATUS);
940 if (!(READ_NAND(nand->IO_ADDR) & 0x80)) {
941 printf ("nand_write_ecc: Device is write protected!!!\n");
946 /* Loop until all data is written */
947 while (*retlen < len) {
948 /* Invalidate cache, if we write to this page */
949 if (nand->cache_page == page)
950 nand->cache_page = -1;
952 /* Write data into buffer */
953 if ((col + len) >= nand->oobblock)
954 for (i = col, cnt = 0; i < nand->oobblock; i++, cnt++)
955 nand->data_buf[i] = buf[(*retlen + cnt)];
957 for (i = col, cnt = 0; cnt < (len - *retlen); i++, cnt++)
958 nand->data_buf[i] = buf[(*retlen + cnt)];
959 /* We use the same function for write and writev !) */
960 ret = nand_write_page (nand, page, col, i, ecc_code);
964 /* Next data start at page boundary */
967 /* Update written bytes count */
970 /* Increment page address */
978 /* De-select the NAND device */
979 NAND_DISABLE_CE(nand); /* set pin high */
985 /* Read a buffer from NanD */
986 static void NanD_ReadBuf(struct nand_chip *nand, u_char * buf, int len)
988 unsigned long nandptr;
990 nandptr = nand->IO_ADDR;
992 for (; len > 0; len--)
993 *buf++ = READ_NAND(nandptr);
996 /* Write a buffer to NanD */
997 static void NanD_WriteBuf(struct nand_chip *nand, const u_char * buf, int len)
999 unsigned long nandptr;
1002 nandptr = nand->IO_ADDR;
1007 for (i = 0; i < len; i++)
1008 WRITE_NAND(buf[i], nandptr);
1012 /* find_boot_record: Find the NFTL Media Header and its Spare copy which contains the
1013 * various device information of the NFTL partition and Bad Unit Table. Update
1014 * the ReplUnitTable[] table accroding to the Bad Unit Table. ReplUnitTable[]
1015 * is used for management of Erase Unit in other routines in nftl.c and nftlmount.c
1017 static int find_boot_record(struct NFTLrecord *nftl)
1019 struct nftl_uci1 h1;
1020 struct nftl_oob oob;
1021 unsigned int block, boot_record_count = 0;
1024 struct NFTLMediaHeader *mh = &nftl->MediaHdr;
1027 nftl->MediaUnit = BLOCK_NIL;
1028 nftl->SpareMediaUnit = BLOCK_NIL;
1030 /* search for a valid boot record */
1031 for (block = 0; block < nftl->nb_blocks; block++) {
1034 /* Check for ANAND header first. Then can whinge if it's found but later
1036 if ((ret = nand_read_ecc(nftl->mtd, block * nftl->EraseSize, SECTORSIZE,
1037 &retlen, buf, NULL))) {
1038 static int warncount = 5;
1041 printf("Block read at 0x%x failed\n", block * nftl->EraseSize);
1043 puts ("Further failures for this block will not be printed\n");
1048 if (retlen < 6 || memcmp(buf, "ANAND", 6)) {
1049 /* ANAND\0 not found. Continue */
1051 printf("ANAND header not found at 0x%x\n", block * nftl->EraseSize);
1057 printf("ANAND header found at 0x%x\n", block * nftl->EraseSize);
1060 /* To be safer with BIOS, also use erase mark as discriminant */
1061 if ((ret = nand_read_oob(nftl->mtd, block * nftl->EraseSize + SECTORSIZE + 8,
1062 8, &retlen, (char *)&h1) < 0)) {
1064 printf("ANAND header found at 0x%x, but OOB data read failed\n",
1065 block * nftl->EraseSize);
1070 /* OK, we like it. */
1072 if (boot_record_count) {
1073 /* We've already processed one. So we just check if
1074 this one is the same as the first one we found */
1075 if (memcmp(mh, buf, sizeof(struct NFTLMediaHeader))) {
1077 printf("NFTL Media Headers at 0x%x and 0x%x disagree.\n",
1078 nftl->MediaUnit * nftl->EraseSize, block * nftl->EraseSize);
1080 /* if (debug) Print both side by side */
1083 if (boot_record_count == 1)
1084 nftl->SpareMediaUnit = block;
1086 boot_record_count++;
1090 /* This is the first we've seen. Copy the media header structure into place */
1091 memcpy(mh, buf, sizeof(struct NFTLMediaHeader));
1093 /* Do some sanity checks on it */
1094 if (mh->UnitSizeFactor != 0xff) {
1095 puts ("Sorry, we don't support UnitSizeFactor "
1100 nftl->nb_boot_blocks = le16_to_cpu(mh->FirstPhysicalEUN);
1101 if ((nftl->nb_boot_blocks + 2) >= nftl->nb_blocks) {
1102 printf ("NFTL Media Header sanity check failed:\n"
1103 "nb_boot_blocks (%d) + 2 > nb_blocks (%d)\n",
1104 nftl->nb_boot_blocks, nftl->nb_blocks);
1108 nftl->numvunits = le32_to_cpu(mh->FormattedSize) / nftl->EraseSize;
1109 if (nftl->numvunits > (nftl->nb_blocks - nftl->nb_boot_blocks - 2)) {
1110 printf ("NFTL Media Header sanity check failed:\n"
1111 "numvunits (%d) > nb_blocks (%d) - nb_boot_blocks(%d) - 2\n",
1114 nftl->nb_boot_blocks);
1118 nftl->nr_sects = nftl->numvunits * (nftl->EraseSize / SECTORSIZE);
1120 /* If we're not using the last sectors in the device for some reason,
1121 reduce nb_blocks accordingly so we forget they're there */
1122 nftl->nb_blocks = le16_to_cpu(mh->NumEraseUnits) + le16_to_cpu(mh->FirstPhysicalEUN);
1124 /* read the Bad Erase Unit Table and modify ReplUnitTable[] accordingly */
1125 for (i = 0; i < nftl->nb_blocks; i++) {
1126 if ((i & (SECTORSIZE - 1)) == 0) {
1127 /* read one sector for every SECTORSIZE of blocks */
1128 if ((ret = nand_read_ecc(nftl->mtd, block * nftl->EraseSize +
1129 i + SECTORSIZE, SECTORSIZE,
1130 &retlen, buf, (char *)&oob)) < 0) {
1131 puts ("Read of bad sector table failed\n");
1135 /* mark the Bad Erase Unit as RESERVED in ReplUnitTable */
1136 if (buf[i & (SECTORSIZE - 1)] != 0xff)
1137 nftl->ReplUnitTable[i] = BLOCK_RESERVED;
1140 nftl->MediaUnit = block;
1141 boot_record_count++;
1143 } /* foreach (block) */
1145 return boot_record_count?0:-1;
1147 static int nand_read_oob(struct nand_chip* nand, size_t ofs, size_t len,
1148 size_t * retlen, u_char * buf)
1150 int len256 = 0, ret;
1151 unsigned long nandptr;
1152 struct Nand *mychip;
1154 nandptr = nand->IO_ADDR;
1156 mychip = &nand->chips[shr(ofs, nand->chipshift)];
1158 /* update address for 2M x 8bit devices. OOB starts on the second */
1159 /* page to maintain compatibility with nand_read_ecc. */
1160 if (nand->page256) {
1167 NanD_Command(nand, NAND_CMD_READOOB);
1168 NanD_Address(nand, ADDR_COLUMN_PAGE, ofs);
1170 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1171 /* Note: datasheet says it should automaticaly wrap to the */
1172 /* next OOB block, but it didn't work here. mf. */
1173 if (nand->page256 && ofs + len > (ofs | 0x7) + 1) {
1174 len256 = (ofs | 0x7) + 1 - ofs;
1175 NanD_ReadBuf(nand, buf, len256);
1177 NanD_Command(nand, NAND_CMD_READOOB);
1178 NanD_Address(nand, ADDR_COLUMN_PAGE, ofs & (~0x1ff));
1181 NanD_ReadBuf(nand, &buf[len256], len - len256);
1184 /* Reading the full OOB data drops us off of the end of the page,
1185 * causing the flash device to go into busy mode, so we need
1186 * to wait until ready 11.4.1 and Toshiba TC58256FT nands */
1188 ret = NanD_WaitReady(nand);
1193 static int nand_write_oob(struct nand_chip* nand, size_t ofs, size_t len,
1194 size_t * retlen, const u_char * buf)
1197 unsigned long nandptr = nand->IO_ADDR;
1200 printf("nand_write_oob(%lx, %d): %2.2X %2.2X %2.2X %2.2X ... %2.2X %2.2X .. %2.2X %2.2X\n",
1201 (long)ofs, len, buf[0], buf[1], buf[2], buf[3],
1202 buf[8], buf[9], buf[14],buf[15]);
1205 /* Reset the chip */
1206 NanD_Command(nand, NAND_CMD_RESET);
1208 /* issue the Read2 command to set the pointer to the Spare Data Area. */
1209 NanD_Command(nand, NAND_CMD_READOOB);
1210 NanD_Address(nand, ADDR_COLUMN_PAGE, ofs);
1212 /* update address for 2M x 8bit devices. OOB starts on the second */
1213 /* page to maintain compatibility with nand_read_ecc. */
1214 if (nand->page256) {
1221 /* issue the Serial Data In command to initial the Page Program process */
1222 NanD_Command(nand, NAND_CMD_SEQIN);
1223 NanD_Address(nand, ADDR_COLUMN_PAGE, ofs);
1225 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1226 /* Note: datasheet says it should automaticaly wrap to the */
1227 /* next OOB block, but it didn't work here. mf. */
1228 if (nand->page256 && ofs + len > (ofs | 0x7) + 1) {
1229 len256 = (ofs | 0x7) + 1 - ofs;
1230 NanD_WriteBuf(nand, buf, len256);
1232 NanD_Command(nand, NAND_CMD_PAGEPROG);
1233 NanD_Command(nand, NAND_CMD_STATUS);
1234 /* NanD_WaitReady() is implicit in NanD_Command */
1236 if (READ_NAND(nandptr) & 1) {
1237 puts ("Error programming oob data\n");
1238 /* There was an error */
1242 NanD_Command(nand, NAND_CMD_SEQIN);
1243 NanD_Address(nand, ADDR_COLUMN_PAGE, ofs & (~0x1ff));
1246 NanD_WriteBuf(nand, &buf[len256], len - len256);
1248 NanD_Command(nand, NAND_CMD_PAGEPROG);
1249 NanD_Command(nand, NAND_CMD_STATUS);
1250 /* NanD_WaitReady() is implicit in NanD_Command */
1252 if (READ_NAND(nandptr) & 1) {
1253 puts ("Error programming oob data\n");
1254 /* There was an error */
1265 static int nand_erase(struct nand_chip* nand, size_t ofs, size_t len)
1267 unsigned long nandptr;
1268 struct Nand *mychip;
1270 if (ofs & (nand->erasesize-1) || len & (nand->erasesize-1)) {
1271 printf ("Offset and size must be sector aligned, erasesize = %d\n",
1272 (int) nand->erasesize);
1276 nandptr = nand->IO_ADDR;
1278 /* FIXME: Do nand in the background. Use timers or schedule_task() */
1280 mychip = &nand->chips[shr(ofs, nand->chipshift)];
1282 NanD_Command(nand, NAND_CMD_ERASE1);
1283 NanD_Address(nand, ADDR_PAGE, ofs);
1284 NanD_Command(nand, NAND_CMD_ERASE2);
1286 NanD_Command(nand, NAND_CMD_STATUS);
1288 if (READ_NAND(nandptr) & 1) {
1289 printf("Error erasing at 0x%lx\n", (long)ofs);
1290 /* There was an error */
1293 ofs += nand->erasesize;
1294 len -= nand->erasesize;
1301 static inline int nandcheck(unsigned long potential, unsigned long physadr)
1308 void nand_probe(unsigned long physadr)
1310 struct nand_chip *nand = NULL;
1311 int i = 0, ChipID = 1;
1313 #ifdef CONFIG_MTD_NAND_ECC_JFFS2
1314 oob_config.ecc_pos[0] = NAND_JFFS2_OOB_ECCPOS0;
1315 oob_config.ecc_pos[1] = NAND_JFFS2_OOB_ECCPOS1;
1316 oob_config.ecc_pos[2] = NAND_JFFS2_OOB_ECCPOS2;
1317 oob_config.ecc_pos[3] = NAND_JFFS2_OOB_ECCPOS3;
1318 oob_config.ecc_pos[4] = NAND_JFFS2_OOB_ECCPOS4;
1319 oob_config.ecc_pos[5] = NAND_JFFS2_OOB_ECCPOS5;
1320 oob_config.badblock_pos = 5;
1321 oob_config.eccvalid_pos = 4;
1323 oob_config.ecc_pos[0] = NAND_NOOB_ECCPOS0;
1324 oob_config.ecc_pos[1] = NAND_NOOB_ECCPOS1;
1325 oob_config.ecc_pos[2] = NAND_NOOB_ECCPOS2;
1326 oob_config.ecc_pos[3] = NAND_NOOB_ECCPOS3;
1327 oob_config.ecc_pos[4] = NAND_NOOB_ECCPOS4;
1328 oob_config.ecc_pos[5] = NAND_NOOB_ECCPOS5;
1329 oob_config.badblock_pos = NAND_NOOB_BADBPOS;
1330 oob_config.eccvalid_pos = NAND_NOOB_ECCVPOS;
1333 for (i=0; i<CFG_MAX_NAND_DEVICE; i++) {
1334 if (nand_dev_desc[i].ChipID == NAND_ChipID_UNKNOWN) {
1335 nand = nand_dev_desc + i;
1340 if (curr_device == -1)
1343 memset((char *)nand, 0, sizeof(struct nand_chip));
1345 nand->cache_page = -1; /* init the cache page */
1346 nand->IO_ADDR = physadr;
1347 nand->ChipID = ChipID;
1348 NanD_ScanChips(nand);
1349 nand->data_buf = malloc (nand->oobblock + nand->oobsize);
1350 if (!nand->data_buf) {
1351 puts ("Cannot allocate memory for data structures.\n");
1356 #ifdef CONFIG_MTD_NAND_ECC
1358 * Pre-calculated 256-way 1 byte column parity
1360 static const u_char nand_ecc_precalc_table[] = {
1361 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00,
1362 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
1363 0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
1364 0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
1365 0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
1366 0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
1367 0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
1368 0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
1369 0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
1370 0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
1371 0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
1372 0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
1373 0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
1374 0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
1375 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
1376 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00
1381 * Creates non-inverted ECC code from line parity
1383 static void nand_trans_result(u_char reg2, u_char reg3,
1386 u_char a, b, i, tmp1, tmp2;
1388 /* Initialize variables */
1392 /* Calculate first ECC byte */
1393 for (i = 0; i < 4; i++) {
1394 if (reg3 & a) /* LP15,13,11,9 --> ecc_code[0] */
1397 if (reg2 & a) /* LP14,12,10,8 --> ecc_code[0] */
1403 /* Calculate second ECC byte */
1405 for (i = 0; i < 4; i++) {
1406 if (reg3 & a) /* LP7,5,3,1 --> ecc_code[1] */
1409 if (reg2 & a) /* LP6,4,2,0 --> ecc_code[1] */
1415 /* Store two of the ECC bytes */
1421 * Calculate 3 byte ECC code for 256 byte block
1423 static void nand_calculate_ecc (const u_char *dat, u_char *ecc_code)
1425 u_char idx, reg1, reg2, reg3;
1428 /* Initialize variables */
1429 reg1 = reg2 = reg3 = 0;
1430 ecc_code[0] = ecc_code[1] = ecc_code[2] = 0;
1432 /* Build up column parity */
1433 for(j = 0; j < 256; j++) {
1435 /* Get CP0 - CP5 from table */
1436 idx = nand_ecc_precalc_table[dat[j]];
1437 reg1 ^= (idx & 0x3f);
1439 /* All bit XOR = 1 ? */
1442 reg2 ^= ~((u_char) j);
1446 /* Create non-inverted ECC code from line parity */
1447 nand_trans_result(reg2, reg3, ecc_code);
1449 /* Calculate final ECC code */
1450 ecc_code[0] = ~ecc_code[0];
1451 ecc_code[1] = ~ecc_code[1];
1452 ecc_code[2] = ((~reg1) << 2) | 0x03;
1456 * Detect and correct a 1 bit error for 256 byte block
1458 static int nand_correct_data (u_char *dat, u_char *read_ecc, u_char *calc_ecc)
1460 u_char a, b, c, d1, d2, d3, add, bit, i;
1462 /* Do error detection */
1463 d1 = calc_ecc[0] ^ read_ecc[0];
1464 d2 = calc_ecc[1] ^ read_ecc[1];
1465 d3 = calc_ecc[2] ^ read_ecc[2];
1467 if ((d1 | d2 | d3) == 0) {
1472 a = (d1 ^ (d1 >> 1)) & 0x55;
1473 b = (d2 ^ (d2 >> 1)) & 0x55;
1474 c = (d3 ^ (d3 >> 1)) & 0x54;
1476 /* Found and will correct single bit error in the data */
1477 if ((a == 0x55) && (b == 0x55) && (c == 0x54)) {
1481 for (i=0; i<4; i++) {
1488 for (i=0; i<4; i++) {
1497 for (i=0; i<3; i++) {
1527 /* ECC Code Error Correction */
1528 read_ecc[0] = calc_ecc[0];
1529 read_ecc[1] = calc_ecc[1];
1530 read_ecc[2] = calc_ecc[2];
1534 /* Uncorrectable Error */
1540 /* Should never happen */
1544 #endif /* (CONFIG_COMMANDS & CFG_CMD_NAND) */