3 * Driver for NAND support, Rick Bronson
4 * borrowed heavily from:
5 * (c) 1999 Machine Vision Holdings, Inc.
6 * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
8 * Added 16-bit nand support
9 * (C) 2004 Texas Instruments
17 #include <linux/mtd/nand_legacy.h>
18 #include <linux/mtd/nand_ids.h>
19 #include <jffs2/jffs2.h>
21 #ifdef CONFIG_OMAP1510
22 void archflashwp(void *archdata, int wp);
25 #define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
30 /* ****************** WARNING *********************
31 * When ALLOW_ERASE_BAD_DEBUG is non-zero the erase command will
32 * erase (or at least attempt to erase) blocks that are marked
33 * bad. This can be very handy if you are _sure_ that the block
34 * is OK, say because you marked a good block bad to test bad
35 * block handling and you are done testing, or if you have
36 * accidentally marked blocks bad.
38 * Erasing factory marked bad blocks is a _bad_ idea. If the
39 * erase succeeds there is no reliable way to find them again,
40 * and attempting to program or erase bad blocks can affect
41 * the data in _other_ (good) blocks.
43 #define ALLOW_ERASE_BAD_DEBUG 0
45 #define CONFIG_MTD_NAND_ECC /* enable ECC */
46 #define CONFIG_MTD_NAND_ECC_JFFS2
48 /* bits for nand_legacy_rw() `cmd'; or together as needed */
49 #define NANDRW_READ 0x01
50 #define NANDRW_WRITE 0x00
51 #define NANDRW_JFFS2 0x02
52 #define NANDRW_JFFS2_SKIP 0x04
56 * Exported variables etc.
59 /* Definition of the out of band configuration structure */
60 struct nand_oob_config {
61 /* position of ECC bytes inside oob */
63 /* position of bad blk flag inside oob -1 = inactive */
65 /* position of ECC valid flag inside oob -1 = inactive */
67 } oob_config = { {0}, 0, 0};
69 struct nand_chip nand_dev_desc[CONFIG_SYS_MAX_NAND_DEVICE] = {{0}};
71 int curr_device = -1; /* Current NAND Device */
77 int nand_legacy_erase(struct nand_chip* nand, size_t ofs,
78 size_t len, int clean);
79 int nand_legacy_rw(struct nand_chip* nand, int cmd,
80 size_t start, size_t len,
81 size_t * retlen, u_char * buf);
82 void nand_print(struct nand_chip *nand);
83 void nand_print_bad(struct nand_chip *nand);
84 int nand_read_oob(struct nand_chip* nand, size_t ofs, size_t len,
85 size_t * retlen, u_char * buf);
86 int nand_write_oob(struct nand_chip* nand, size_t ofs, size_t len,
87 size_t * retlen, const u_char * buf);
92 static int NanD_WaitReady(struct nand_chip *nand, int ale_wait);
93 static int nand_read_ecc(struct nand_chip *nand, size_t start, size_t len,
94 size_t * retlen, u_char *buf, u_char *ecc_code);
95 static int nand_write_ecc (struct nand_chip* nand, size_t to, size_t len,
96 size_t * retlen, const u_char * buf,
98 #ifdef CONFIG_MTD_NAND_ECC
99 static int nand_correct_data (u_char *dat, u_char *read_ecc, u_char *calc_ecc);
100 static void nand_calculate_ecc (const u_char *dat, u_char *ecc_code);
106 * Function definitions
110 /* returns 0 if block containing pos is OK:
111 * valid erase block and
112 * not marked bad, or no bad mark position is specified
113 * returns 1 if marked bad or otherwise invalid
115 static int check_block (struct nand_chip *nand, unsigned long pos)
119 uint16_t oob_data16[6];
120 int page0 = pos & (-nand->erasesize);
121 int page1 = page0 + nand->oobblock;
122 int badpos = oob_config.badblock_pos;
124 if (pos >= nand->totlen)
128 return 0; /* no way to check, assume OK */
131 if (nand_read_oob(nand, (page0 + 0), 12, &retlen, (uint8_t *)oob_data16)
132 || (oob_data16[2] & 0xff00) != 0xff00)
134 if (nand_read_oob(nand, (page1 + 0), 12, &retlen, (uint8_t *)oob_data16)
135 || (oob_data16[2] & 0xff00) != 0xff00)
138 /* Note - bad block marker can be on first or second page */
139 if (nand_read_oob(nand, page0 + badpos, 1, &retlen, (unsigned char *)&oob_data)
141 || nand_read_oob (nand, page1 + badpos, 1, &retlen, (unsigned char *)&oob_data)
149 /* print bad blocks in NAND flash */
150 void nand_print_bad(struct nand_chip* nand)
154 for (pos = 0; pos < nand->totlen; pos += nand->erasesize) {
155 if (check_block(nand, pos))
156 printf(" 0x%8.8lx\n", pos);
161 /* cmd: 0: NANDRW_WRITE write, fail on bad block
162 * 1: NANDRW_READ read, fail on bad block
163 * 2: NANDRW_WRITE | NANDRW_JFFS2 write, skip bad blocks
164 * 3: NANDRW_READ | NANDRW_JFFS2 read, data all 0xff for bad blocks
165 * 7: NANDRW_READ | NANDRW_JFFS2 | NANDRW_JFFS2_SKIP read, skip bad blocks
167 int nand_legacy_rw (struct nand_chip* nand, int cmd,
168 size_t start, size_t len,
169 size_t * retlen, u_char * buf)
171 int ret = 0, n, total = 0;
173 /* eblk (once set) is the start of the erase block containing the
174 * data being processed.
176 unsigned long eblk = ~0; /* force mismatch on first pass */
177 unsigned long erasesize = nand->erasesize;
180 if ((start & (-erasesize)) != eblk) {
181 /* have crossed into new erase block, deal with
182 * it if it is sure marked bad.
184 eblk = start & (-erasesize); /* start of block */
185 if (check_block(nand, eblk)) {
186 if (cmd == (NANDRW_READ | NANDRW_JFFS2)) {
188 start - eblk < erasesize) {
195 } else if (cmd == (NANDRW_READ | NANDRW_JFFS2 | NANDRW_JFFS2_SKIP)) {
198 } else if (cmd == (NANDRW_WRITE | NANDRW_JFFS2)) {
208 /* The ECC will not be calculated correctly if
209 less than 512 is written or read */
210 /* Is request at least 512 bytes AND it starts on a proper boundry */
211 if((start != ROUND_DOWN(start, 0x200)) || (len < 0x200))
212 printf("Warning block writes should be at least 512 bytes and start on a 512 byte boundry\n");
214 if (cmd & NANDRW_READ) {
215 ret = nand_read_ecc(nand, start,
216 min(len, eblk + erasesize - start),
217 (size_t *)&n, (u_char*)buf, (u_char *)eccbuf);
219 ret = nand_write_ecc(nand, start,
220 min(len, eblk + erasesize - start),
221 (size_t *)&n, (u_char*)buf, (u_char *)eccbuf);
238 void nand_print(struct nand_chip *nand)
240 if (nand->numchips > 1) {
241 printf("%s at 0x%lx,\n"
242 "\t %d chips %s, size %d MB, \n"
243 "\t total size %ld MB, sector size %ld kB\n",
244 nand->name, nand->IO_ADDR, nand->numchips,
245 nand->chips_name, 1 << (nand->chipshift - 20),
246 nand->totlen >> 20, nand->erasesize >> 10);
249 printf("%s at 0x%lx (", nand->chips_name, nand->IO_ADDR);
250 print_size(nand->totlen, ", ");
251 print_size(nand->erasesize, " sector)\n");
255 /* ------------------------------------------------------------------------- */
257 static int NanD_WaitReady(struct nand_chip *nand, int ale_wait)
259 /* This is inline, to optimise the common case, where it's ready instantly */
262 #ifdef NAND_NO_RB /* in config file, shorter delays currently wrap accesses */
264 NAND_WAIT_READY(nand); /* do the worst case 25us wait */
267 #else /* has functional r/b signal */
268 NAND_WAIT_READY(nand);
273 /* NanD_Command: Send a flash command to the flash chip */
275 static inline int NanD_Command(struct nand_chip *nand, unsigned char command)
277 unsigned long nandptr = nand->IO_ADDR;
279 /* Assert the CLE (Command Latch Enable) line to the flash chip */
280 NAND_CTL_SETCLE(nandptr);
282 /* Send the command */
283 WRITE_NAND_COMMAND(command, nandptr);
285 /* Lower the CLE line */
286 NAND_CTL_CLRCLE(nandptr);
289 if(command == NAND_CMD_RESET){
291 NanD_Command(nand, NAND_CMD_STATUS);
293 ret_val = READ_NAND(nandptr);/* wait till ready */
294 } while((ret_val & 0x40) != 0x40);
297 return NanD_WaitReady(nand, 0);
300 /* NanD_Address: Set the current address for the flash chip */
302 static int NanD_Address(struct nand_chip *nand, int numbytes, unsigned long ofs)
304 unsigned long nandptr;
307 nandptr = nand->IO_ADDR;
309 /* Assert the ALE (Address Latch Enable) line to the flash chip */
310 NAND_CTL_SETALE(nandptr);
312 /* Send the address */
313 /* Devices with 256-byte page are addressed as:
314 * Column (bits 0-7), Page (bits 8-15, 16-23, 24-31)
315 * there is no device on the market with page256
316 * and more than 24 bits.
317 * Devices with 512-byte page are addressed as:
318 * Column (bits 0-7), Page (bits 9-16, 17-24, 25-31)
319 * 25-31 is sent only if the chip support it.
320 * bit 8 changes the read command to be sent
321 * (NAND_CMD_READ0 or NAND_CMD_READ1).
324 if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE)
325 WRITE_NAND_ADDRESS(ofs, nandptr);
327 ofs = ofs >> nand->page_shift;
329 if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE) {
330 for (i = 0; i < nand->pageadrlen; i++, ofs = ofs >> 8) {
331 WRITE_NAND_ADDRESS(ofs, nandptr);
335 /* Lower the ALE line */
336 NAND_CTL_CLRALE(nandptr);
338 /* Wait for the chip to respond */
339 return NanD_WaitReady(nand, 1);
342 /* NanD_SelectChip: Select a given flash chip within the current floor */
344 static inline int NanD_SelectChip(struct nand_chip *nand, int chip)
346 /* Wait for it to be ready */
347 return NanD_WaitReady(nand, 0);
350 /* NanD_IdentChip: Identify a given NAND chip given {floor,chip} */
352 static int NanD_IdentChip(struct nand_chip *nand, int floor, int chip)
356 NAND_ENABLE_CE(nand); /* set pin low */
358 if (NanD_Command(nand, NAND_CMD_RESET)) {
360 printf("NanD_Command (reset) for %d,%d returned true\n",
363 NAND_DISABLE_CE(nand); /* set pin high */
367 /* Read the NAND chip ID: 1. Send ReadID command */
368 if (NanD_Command(nand, NAND_CMD_READID)) {
370 printf("NanD_Command (ReadID) for %d,%d returned true\n",
373 NAND_DISABLE_CE(nand); /* set pin high */
377 /* Read the NAND chip ID: 2. Send address byte zero */
378 NanD_Address(nand, ADDR_COLUMN, 0);
380 /* Read the manufacturer and device id codes from the device */
382 mfr = READ_NAND(nand->IO_ADDR);
384 id = READ_NAND(nand->IO_ADDR);
386 NAND_DISABLE_CE(nand); /* set pin high */
389 printf("NanD_Command (ReadID) got %x %x\n", mfr, id);
391 if (mfr == 0xff || mfr == 0) {
392 /* No response - return failure */
396 /* Check it's the same as the first chip we identified.
397 * M-Systems say that any given nand_chip device should only
398 * contain _one_ type of flash part, although that's not a
399 * hardware restriction. */
401 if (nand->mfr == mfr && nand->id == id) {
402 return 1; /* This is another the same the first */
404 printf("Flash chip at floor %d, chip %d is different:\n",
409 /* Print and store the manufacturer and ID codes. */
410 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
411 if (mfr == nand_flash_ids[i].manufacture_id &&
412 id == nand_flash_ids[i].model_id) {
414 printf("Flash chip found:\n\t Manufacturer ID: 0x%2.2X, "
415 "Chip ID: 0x%2.2X (%s)\n", mfr, id,
416 nand_flash_ids[i].name);
422 nand_flash_ids[i].chipshift;
423 nand->page256 = nand_flash_ids[i].page256;
426 nand->oobblock = 256;
428 nand->page_shift = 8;
430 nand->oobblock = 512;
432 nand->page_shift = 9;
434 nand->pageadrlen = nand_flash_ids[i].pageadrlen;
435 nand->erasesize = nand_flash_ids[i].erasesize;
436 nand->chips_name = nand_flash_ids[i].name;
437 nand->bus16 = nand_flash_ids[i].bus16;
446 /* We haven't fully identified the chip. Print as much as we know. */
447 printf("Unknown flash chip found: %2.2X %2.2X\n",
454 /* NanD_ScanChips: Find all NAND chips present in a nand_chip, and identify them */
456 static void NanD_ScanChips(struct nand_chip *nand)
459 int numchips[NAND_MAX_FLOORS];
460 int maxchips = CONFIG_SYS_NAND_MAX_CHIPS;
468 /* For each floor, find the number of valid chips it contains */
469 for (floor = 0; floor < NAND_MAX_FLOORS; floor++) {
472 for (chip = 0; chip < maxchips && ret != 0; chip++) {
474 ret = NanD_IdentChip(nand, floor, chip);
482 /* If there are none at all that we recognise, bail */
483 if (!nand->numchips) {
485 puts ("No NAND flash chips recognised.\n");
490 /* Allocate an array to hold the information for each chip */
491 nand->chips = malloc(sizeof(struct Nand) * nand->numchips);
493 puts ("No memory for allocating chip info structures\n");
499 /* Fill out the chip array with {floor, chipno} for each
500 * detected chip in the device. */
501 for (floor = 0; floor < NAND_MAX_FLOORS; floor++) {
502 for (chip = 0; chip < numchips[floor]; chip++) {
503 nand->chips[ret].floor = floor;
504 nand->chips[ret].chip = chip;
505 nand->chips[ret].curadr = 0;
506 nand->chips[ret].curmode = 0x50;
511 /* Calculate and print the total size of the device */
512 nand->totlen = nand->numchips * (1 << nand->chipshift);
515 printf("%d flash chips found. Total nand_chip size: %ld MB\n",
516 nand->numchips, nand->totlen >> 20);
520 /* we need to be fast here, 1 us per read translates to 1 second per meg */
521 static void NanD_ReadBuf (struct nand_chip *nand, u_char * data_buf, int cntr)
523 unsigned long nandptr = nand->IO_ADDR;
525 NanD_Command (nand, NAND_CMD_READ0);
531 val = READ_NAND (nandptr);
532 *data_buf++ = val & 0xff;
533 *data_buf++ = val >> 8;
534 val = READ_NAND (nandptr);
535 *data_buf++ = val & 0xff;
536 *data_buf++ = val >> 8;
537 val = READ_NAND (nandptr);
538 *data_buf++ = val & 0xff;
539 *data_buf++ = val >> 8;
540 val = READ_NAND (nandptr);
541 *data_buf++ = val & 0xff;
542 *data_buf++ = val >> 8;
543 val = READ_NAND (nandptr);
544 *data_buf++ = val & 0xff;
545 *data_buf++ = val >> 8;
546 val = READ_NAND (nandptr);
547 *data_buf++ = val & 0xff;
548 *data_buf++ = val >> 8;
549 val = READ_NAND (nandptr);
550 *data_buf++ = val & 0xff;
551 *data_buf++ = val >> 8;
552 val = READ_NAND (nandptr);
553 *data_buf++ = val & 0xff;
554 *data_buf++ = val >> 8;
559 val = READ_NAND (nandptr);
560 *data_buf++ = val & 0xff;
561 *data_buf++ = val >> 8;
566 *data_buf++ = READ_NAND (nandptr);
567 *data_buf++ = READ_NAND (nandptr);
568 *data_buf++ = READ_NAND (nandptr);
569 *data_buf++ = READ_NAND (nandptr);
570 *data_buf++ = READ_NAND (nandptr);
571 *data_buf++ = READ_NAND (nandptr);
572 *data_buf++ = READ_NAND (nandptr);
573 *data_buf++ = READ_NAND (nandptr);
574 *data_buf++ = READ_NAND (nandptr);
575 *data_buf++ = READ_NAND (nandptr);
576 *data_buf++ = READ_NAND (nandptr);
577 *data_buf++ = READ_NAND (nandptr);
578 *data_buf++ = READ_NAND (nandptr);
579 *data_buf++ = READ_NAND (nandptr);
580 *data_buf++ = READ_NAND (nandptr);
581 *data_buf++ = READ_NAND (nandptr);
586 *data_buf++ = READ_NAND (nandptr);
595 static int nand_read_ecc(struct nand_chip *nand, size_t start, size_t len,
596 size_t * retlen, u_char *buf, u_char *ecc_code)
600 #ifdef CONFIG_MTD_NAND_ECC
607 /* Do not allow reads past end of device */
608 if ((start + len) > nand->totlen) {
609 printf ("%s: Attempt read beyond end of device %x %x %x\n",
610 __FUNCTION__, (uint) start, (uint) len, (uint) nand->totlen);
615 /* First we calculate the starting page */
616 /*page = shr(start, nand->page_shift);*/
617 page = start >> nand->page_shift;
619 /* Get raw starting column */
620 col = start & (nand->oobblock - 1);
622 /* Initialize return value */
625 /* Select the NAND device */
626 NAND_ENABLE_CE(nand); /* set pin low */
628 /* Loop until all data read */
629 while (*retlen < len) {
631 #ifdef CONFIG_MTD_NAND_ECC
632 /* Do we have this page in cache ? */
633 if (nand->cache_page == page)
635 /* Send the read command */
636 NanD_Command(nand, NAND_CMD_READ0);
638 NanD_Address(nand, ADDR_COLUMN_PAGE,
639 (page << nand->page_shift) + (col >> 1));
641 NanD_Address(nand, ADDR_COLUMN_PAGE,
642 (page << nand->page_shift) + col);
645 /* Read in a page + oob data */
646 NanD_ReadBuf(nand, nand->data_buf, nand->oobblock + nand->oobsize);
648 /* copy data into cache, for read out of cache and if ecc fails */
649 if (nand->data_cache) {
650 memcpy (nand->data_cache, nand->data_buf,
651 nand->oobblock + nand->oobsize);
654 /* Pick the ECC bytes out of the oob data */
655 for (j = 0; j < 6; j++) {
656 ecc_code[j] = nand->data_buf[(nand->oobblock + oob_config.ecc_pos[j])];
659 /* Calculate the ECC and verify it */
660 /* If block was not written with ECC, skip ECC */
661 if (oob_config.eccvalid_pos != -1 &&
662 (nand->data_buf[nand->oobblock + oob_config.eccvalid_pos] & 0x0f) != 0x0f) {
664 nand_calculate_ecc (&nand->data_buf[0], &ecc_calc[0]);
665 switch (nand_correct_data (&nand->data_buf[0], &ecc_code[0], &ecc_calc[0])) {
667 printf ("%s: Failed ECC read, page 0x%08x\n", __FUNCTION__, page);
671 case 2: /* transfer ECC corrected data to cache */
672 if (nand->data_cache)
673 memcpy (nand->data_cache, nand->data_buf, 256);
678 if (oob_config.eccvalid_pos != -1 &&
679 nand->oobblock == 512 && (nand->data_buf[nand->oobblock + oob_config.eccvalid_pos] & 0xf0) != 0xf0) {
681 nand_calculate_ecc (&nand->data_buf[256], &ecc_calc[3]);
682 switch (nand_correct_data (&nand->data_buf[256], &ecc_code[3], &ecc_calc[3])) {
684 printf ("%s: Failed ECC read, page 0x%08x\n", __FUNCTION__, page);
688 case 2: /* transfer ECC corrected data to cache */
689 if (nand->data_cache)
690 memcpy (&nand->data_cache[256], &nand->data_buf[256], 256);
695 /* Read the data from ECC data buffer into return buffer */
696 data_poi = (nand->data_cache) ? nand->data_cache : nand->data_buf;
698 if ((*retlen + (nand->oobblock - col)) >= len) {
699 memcpy (buf + *retlen, data_poi, len - *retlen);
702 memcpy (buf + *retlen, data_poi, nand->oobblock - col);
703 *retlen += nand->oobblock - col;
705 /* Set cache page address, invalidate, if ecc_failed */
706 nand->cache_page = (nand->data_cache && !ecc_failed) ? page : -1;
708 ecc_status += ecc_failed;
712 /* Send the read command */
713 NanD_Command(nand, NAND_CMD_READ0);
715 NanD_Address(nand, ADDR_COLUMN_PAGE,
716 (page << nand->page_shift) + (col >> 1));
718 NanD_Address(nand, ADDR_COLUMN_PAGE,
719 (page << nand->page_shift) + col);
722 /* Read the data directly into the return buffer */
723 if ((*retlen + (nand->oobblock - col)) >= len) {
724 NanD_ReadBuf(nand, buf + *retlen, len - *retlen);
729 NanD_ReadBuf(nand, buf + *retlen, nand->oobblock - col);
730 *retlen += nand->oobblock - col;
733 /* For subsequent reads align to page boundary. */
735 /* Increment page address */
739 /* De-select the NAND device */
740 NAND_DISABLE_CE(nand); /* set pin high */
743 * Return success, if no ECC failures, else -EIO
744 * fs driver will take care of that, because
745 * retlen == desired len and result == -EIO
747 return ecc_status ? -1 : 0;
751 * Nand_page_program function is used for write and writev !
753 static int nand_write_page (struct nand_chip *nand,
754 int page, int col, int last, u_char * ecc_code)
758 unsigned long nandptr = nand->IO_ADDR;
760 #ifdef CONFIG_MTD_NAND_ECC
761 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
762 int ecc_bytes = (nand->oobblock == 512) ? 6 : 3;
766 for (i = nand->oobblock; i < nand->oobblock + nand->oobsize; i++)
767 nand->data_buf[i] = 0xff;
769 #ifdef CONFIG_MTD_NAND_ECC
770 /* Zero out the ECC array */
771 for (i = 0; i < 6; i++)
774 /* Read back previous written data, if col > 0 */
776 NanD_Command (nand, NAND_CMD_READ0);
778 NanD_Address (nand, ADDR_COLUMN_PAGE,
779 (page << nand->page_shift) + (col >> 1));
781 NanD_Address (nand, ADDR_COLUMN_PAGE,
782 (page << nand->page_shift) + col);
788 for (i = 0; i < col; i += 2) {
789 val = READ_NAND (nandptr);
790 nand->data_buf[i] = val & 0xff;
791 nand->data_buf[i + 1] = val >> 8;
794 for (i = 0; i < col; i++)
795 nand->data_buf[i] = READ_NAND (nandptr);
799 /* Calculate and write the ECC if we have enough data */
800 if ((col < nand->eccsize) && (last >= nand->eccsize)) {
801 nand_calculate_ecc (&nand->data_buf[0], &(ecc_code[0]));
802 for (i = 0; i < 3; i++) {
803 nand->data_buf[(nand->oobblock +
804 oob_config.ecc_pos[i])] = ecc_code[i];
806 if (oob_config.eccvalid_pos != -1) {
807 nand->data_buf[nand->oobblock +
808 oob_config.eccvalid_pos] = 0xf0;
812 /* Calculate and write the second ECC if we have enough data */
813 if ((nand->oobblock == 512) && (last == nand->oobblock)) {
814 nand_calculate_ecc (&nand->data_buf[256], &(ecc_code[3]));
815 for (i = 3; i < 6; i++) {
816 nand->data_buf[(nand->oobblock +
817 oob_config.ecc_pos[i])] = ecc_code[i];
819 if (oob_config.eccvalid_pos != -1) {
820 nand->data_buf[nand->oobblock +
821 oob_config.eccvalid_pos] &= 0x0f;
825 /* Prepad for partial page programming !!! */
826 for (i = 0; i < col; i++)
827 nand->data_buf[i] = 0xff;
829 /* Postpad for partial page programming !!! oob is already padded */
830 for (i = last; i < nand->oobblock; i++)
831 nand->data_buf[i] = 0xff;
833 /* Send command to begin auto page programming */
834 NanD_Command (nand, NAND_CMD_READ0);
835 NanD_Command (nand, NAND_CMD_SEQIN);
837 NanD_Address (nand, ADDR_COLUMN_PAGE,
838 (page << nand->page_shift) + (col >> 1));
840 NanD_Address (nand, ADDR_COLUMN_PAGE,
841 (page << nand->page_shift) + col);
844 /* Write out complete page of data */
846 for (i = 0; i < (nand->oobblock + nand->oobsize); i += 2) {
847 WRITE_NAND (nand->data_buf[i] +
848 (nand->data_buf[i + 1] << 8),
852 for (i = 0; i < (nand->oobblock + nand->oobsize); i++)
853 WRITE_NAND (nand->data_buf[i], nand->IO_ADDR);
856 /* Send command to actually program the data */
857 NanD_Command (nand, NAND_CMD_PAGEPROG);
858 NanD_Command (nand, NAND_CMD_STATUS);
864 ret_val = READ_NAND (nandptr); /* wait till ready */
865 } while ((ret_val & 0x40) != 0x40);
868 /* See if device thinks it succeeded */
869 if (READ_NAND (nand->IO_ADDR) & 0x01) {
870 printf ("%s: Failed write, page 0x%08x, ", __FUNCTION__,
874 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
876 * The NAND device assumes that it is always writing to
877 * a cleanly erased page. Hence, it performs its internal
878 * write verification only on bits that transitioned from
879 * 1 to 0. The device does NOT verify the whole page on a
880 * byte by byte basis. It is possible that the page was
881 * not completely erased or the page is becoming unusable
882 * due to wear. The read with ECC would catch the error
883 * later when the ECC page check fails, but we would rather
884 * catch it early in the page write stage. Better to write
885 * no data than invalid data.
888 /* Send command to read back the page */
889 if (col < nand->eccsize)
890 NanD_Command (nand, NAND_CMD_READ0);
892 NanD_Command (nand, NAND_CMD_READ1);
894 NanD_Address (nand, ADDR_COLUMN_PAGE,
895 (page << nand->page_shift) + (col >> 1));
897 NanD_Address (nand, ADDR_COLUMN_PAGE,
898 (page << nand->page_shift) + col);
901 /* Loop through and verify the data */
903 for (i = col; i < last; i = +2) {
904 if ((nand->data_buf[i] +
905 (nand->data_buf[i + 1] << 8)) != READ_NAND (nand->IO_ADDR)) {
906 printf ("%s: Failed write verify, page 0x%08x ",
912 for (i = col; i < last; i++) {
913 if (nand->data_buf[i] != READ_NAND (nand->IO_ADDR)) {
914 printf ("%s: Failed write verify, page 0x%08x ",
921 #ifdef CONFIG_MTD_NAND_ECC
923 * We also want to check that the ECC bytes wrote
924 * correctly for the same reasons stated above.
926 NanD_Command (nand, NAND_CMD_READOOB);
928 NanD_Address (nand, ADDR_COLUMN_PAGE,
929 (page << nand->page_shift) + (col >> 1));
931 NanD_Address (nand, ADDR_COLUMN_PAGE,
932 (page << nand->page_shift) + col);
935 for (i = 0; i < nand->oobsize; i += 2) {
938 val = READ_NAND (nand->IO_ADDR);
939 nand->data_buf[i] = val & 0xff;
940 nand->data_buf[i + 1] = val >> 8;
943 for (i = 0; i < nand->oobsize; i++) {
944 nand->data_buf[i] = READ_NAND (nand->IO_ADDR);
947 for (i = 0; i < ecc_bytes; i++) {
948 if ((nand->data_buf[(oob_config.ecc_pos[i])] != ecc_code[i]) && ecc_code[i]) {
949 printf ("%s: Failed ECC write "
950 "verify, page 0x%08x, "
951 "%6i bytes were succesful\n",
952 __FUNCTION__, page, i);
956 #endif /* CONFIG_MTD_NAND_ECC */
957 #endif /* CONFIG_MTD_NAND_VERIFY_WRITE */
961 static int nand_write_ecc (struct nand_chip* nand, size_t to, size_t len,
962 size_t * retlen, const u_char * buf, u_char * ecc_code)
964 int i, page, col, cnt, ret = 0;
966 /* Do not allow write past end of device */
967 if ((to + len) > nand->totlen) {
968 printf ("%s: Attempt to write past end of page\n", __FUNCTION__);
972 /* Shift to get page */
973 page = ((int) to) >> nand->page_shift;
975 /* Get the starting column */
976 col = to & (nand->oobblock - 1);
978 /* Initialize return length value */
981 /* Select the NAND device */
982 #ifdef CONFIG_OMAP1510
985 #ifdef CONFIG_SYS_NAND_WP
989 NAND_ENABLE_CE(nand); /* set pin low */
991 /* Check the WP bit */
992 NanD_Command(nand, NAND_CMD_STATUS);
993 if (!(READ_NAND(nand->IO_ADDR) & 0x80)) {
994 printf ("%s: Device is write protected!!!\n", __FUNCTION__);
999 /* Loop until all data is written */
1000 while (*retlen < len) {
1001 /* Invalidate cache, if we write to this page */
1002 if (nand->cache_page == page)
1003 nand->cache_page = -1;
1005 /* Write data into buffer */
1006 if ((col + len) >= nand->oobblock) {
1007 for (i = col, cnt = 0; i < nand->oobblock; i++, cnt++) {
1008 nand->data_buf[i] = buf[(*retlen + cnt)];
1011 for (i = col, cnt = 0; cnt < (len - *retlen); i++, cnt++) {
1012 nand->data_buf[i] = buf[(*retlen + cnt)];
1015 /* We use the same function for write and writev !) */
1016 ret = nand_write_page (nand, page, col, i, ecc_code);
1020 /* Next data start at page boundary */
1023 /* Update written bytes count */
1026 /* Increment page address */
1034 /* De-select the NAND device */
1035 NAND_DISABLE_CE(nand); /* set pin high */
1036 #ifdef CONFIG_OMAP1510
1039 #ifdef CONFIG_SYS_NAND_WP
1046 /* read from the 16 bytes of oob data that correspond to a 512 byte
1047 * page or 2 256-byte pages.
1049 int nand_read_oob(struct nand_chip* nand, size_t ofs, size_t len,
1050 size_t * retlen, u_char * buf)
1053 struct Nand *mychip;
1056 mychip = &nand->chips[ofs >> nand->chipshift];
1058 /* update address for 2M x 8bit devices. OOB starts on the second */
1059 /* page to maintain compatibility with nand_read_ecc. */
1060 if (nand->page256) {
1067 NAND_ENABLE_CE(nand); /* set pin low */
1068 NanD_Command(nand, NAND_CMD_READOOB);
1070 NanD_Address(nand, ADDR_COLUMN_PAGE,
1071 ((ofs >> nand->page_shift) << nand->page_shift) +
1072 ((ofs & (nand->oobblock - 1)) >> 1));
1074 NanD_Address(nand, ADDR_COLUMN_PAGE, ofs);
1077 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1078 /* Note: datasheet says it should automaticaly wrap to the */
1079 /* next OOB block, but it didn't work here. mf. */
1080 if (nand->page256 && ofs + len > (ofs | 0x7) + 1) {
1081 len256 = (ofs | 0x7) + 1 - ofs;
1082 NanD_ReadBuf(nand, buf, len256);
1084 NanD_Command(nand, NAND_CMD_READOOB);
1085 NanD_Address(nand, ADDR_COLUMN_PAGE, ofs & (~0x1ff));
1088 NanD_ReadBuf(nand, &buf[len256], len - len256);
1091 /* Reading the full OOB data drops us off of the end of the page,
1092 * causing the flash device to go into busy mode, so we need
1093 * to wait until ready 11.4.1 and Toshiba TC58256FT nands */
1095 ret = NanD_WaitReady(nand, 1);
1096 NAND_DISABLE_CE(nand); /* set pin high */
1102 /* write to the 16 bytes of oob data that correspond to a 512 byte
1103 * page or 2 256-byte pages.
1105 int nand_write_oob(struct nand_chip* nand, size_t ofs, size_t len,
1106 size_t * retlen, const u_char * buf)
1110 unsigned long nandptr = nand->IO_ADDR;
1113 printf("nand_write_oob(%lx, %d): %2.2X %2.2X %2.2X %2.2X ... %2.2X %2.2X .. %2.2X %2.2X\n",
1114 (long)ofs, len, buf[0], buf[1], buf[2], buf[3],
1115 buf[8], buf[9], buf[14],buf[15]);
1118 NAND_ENABLE_CE(nand); /* set pin low to enable chip */
1120 /* Reset the chip */
1121 NanD_Command(nand, NAND_CMD_RESET);
1123 /* issue the Read2 command to set the pointer to the Spare Data Area. */
1124 NanD_Command(nand, NAND_CMD_READOOB);
1126 NanD_Address(nand, ADDR_COLUMN_PAGE,
1127 ((ofs >> nand->page_shift) << nand->page_shift) +
1128 ((ofs & (nand->oobblock - 1)) >> 1));
1130 NanD_Address(nand, ADDR_COLUMN_PAGE, ofs);
1133 /* update address for 2M x 8bit devices. OOB starts on the second */
1134 /* page to maintain compatibility with nand_read_ecc. */
1135 if (nand->page256) {
1142 /* issue the Serial Data In command to initial the Page Program process */
1143 NanD_Command(nand, NAND_CMD_SEQIN);
1145 NanD_Address(nand, ADDR_COLUMN_PAGE,
1146 ((ofs >> nand->page_shift) << nand->page_shift) +
1147 ((ofs & (nand->oobblock - 1)) >> 1));
1149 NanD_Address(nand, ADDR_COLUMN_PAGE, ofs);
1152 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1153 /* Note: datasheet says it should automaticaly wrap to the */
1154 /* next OOB block, but it didn't work here. mf. */
1155 if (nand->page256 && ofs + len > (ofs | 0x7) + 1) {
1156 len256 = (ofs | 0x7) + 1 - ofs;
1157 for (i = 0; i < len256; i++)
1158 WRITE_NAND(buf[i], nandptr);
1160 NanD_Command(nand, NAND_CMD_PAGEPROG);
1161 NanD_Command(nand, NAND_CMD_STATUS);
1165 ret_val = READ_NAND(nandptr); /* wait till ready */
1166 } while ((ret_val & 0x40) != 0x40);
1169 if (READ_NAND(nandptr) & 1) {
1170 puts ("Error programming oob data\n");
1171 /* There was an error */
1172 NAND_DISABLE_CE(nand); /* set pin high */
1176 NanD_Command(nand, NAND_CMD_SEQIN);
1177 NanD_Address(nand, ADDR_COLUMN_PAGE, ofs & (~0x1ff));
1181 for (i = len256; i < len; i += 2) {
1182 WRITE_NAND(buf[i] + (buf[i+1] << 8), nandptr);
1185 for (i = len256; i < len; i++)
1186 WRITE_NAND(buf[i], nandptr);
1189 NanD_Command(nand, NAND_CMD_PAGEPROG);
1190 NanD_Command(nand, NAND_CMD_STATUS);
1194 ret_val = READ_NAND(nandptr); /* wait till ready */
1195 } while ((ret_val & 0x40) != 0x40);
1198 if (READ_NAND(nandptr) & 1) {
1199 puts ("Error programming oob data\n");
1200 /* There was an error */
1201 NAND_DISABLE_CE(nand); /* set pin high */
1206 NAND_DISABLE_CE(nand); /* set pin high */
1212 int nand_legacy_erase(struct nand_chip* nand, size_t ofs, size_t len, int clean)
1214 /* This is defined as a structure so it will work on any system
1215 * using native endian jffs2 (the default).
1217 static struct jffs2_unknown_node clean_marker = {
1218 JFFS2_MAGIC_BITMASK,
1219 JFFS2_NODETYPE_CLEANMARKER,
1220 8 /* 8 bytes in this node */
1222 unsigned long nandptr;
1223 struct Nand *mychip;
1226 if (ofs & (nand->erasesize-1) || len & (nand->erasesize-1)) {
1227 printf ("Offset and size must be sector aligned, erasesize = %d\n",
1228 (int) nand->erasesize);
1232 nandptr = nand->IO_ADDR;
1234 /* Select the NAND device */
1235 #ifdef CONFIG_OMAP1510
1238 #ifdef CONFIG_SYS_NAND_WP
1241 NAND_ENABLE_CE(nand); /* set pin low */
1243 /* Check the WP bit */
1244 NanD_Command(nand, NAND_CMD_STATUS);
1245 if (!(READ_NAND(nand->IO_ADDR) & 0x80)) {
1246 printf ("nand_write_ecc: Device is write protected!!!\n");
1251 /* Check the WP bit */
1252 NanD_Command(nand, NAND_CMD_STATUS);
1253 if (!(READ_NAND(nand->IO_ADDR) & 0x80)) {
1254 printf ("%s: Device is write protected!!!\n", __FUNCTION__);
1259 /* FIXME: Do nand in the background. Use timers or schedule_task() */
1261 /*mychip = &nand->chips[shr(ofs, nand->chipshift)];*/
1262 mychip = &nand->chips[ofs >> nand->chipshift];
1264 /* always check for bad block first, genuine bad blocks
1265 * should _never_ be erased.
1267 if (ALLOW_ERASE_BAD_DEBUG || !check_block(nand, ofs)) {
1268 /* Select the NAND device */
1269 NAND_ENABLE_CE(nand); /* set pin low */
1271 NanD_Command(nand, NAND_CMD_ERASE1);
1272 NanD_Address(nand, ADDR_PAGE, ofs);
1273 NanD_Command(nand, NAND_CMD_ERASE2);
1275 NanD_Command(nand, NAND_CMD_STATUS);
1280 ret_val = READ_NAND(nandptr); /* wait till ready */
1281 } while ((ret_val & 0x40) != 0x40);
1284 if (READ_NAND(nandptr) & 1) {
1285 printf ("%s: Error erasing at 0x%lx\n",
1286 __FUNCTION__, (long)ofs);
1287 /* There was an error */
1292 int n; /* return value not used */
1295 /* clean marker position and size depend
1296 * on the page size, since 256 byte pages
1297 * only have 8 bytes of oob data
1299 if (nand->page256) {
1300 p = NAND_JFFS2_OOB8_FSDAPOS;
1301 l = NAND_JFFS2_OOB8_FSDALEN;
1303 p = NAND_JFFS2_OOB16_FSDAPOS;
1304 l = NAND_JFFS2_OOB16_FSDALEN;
1307 ret = nand_write_oob(nand, ofs + p, l, (size_t *)&n,
1308 (u_char *)&clean_marker);
1309 /* quit here if write failed */
1314 ofs += nand->erasesize;
1315 len -= nand->erasesize;
1319 /* De-select the NAND device */
1320 NAND_DISABLE_CE(nand); /* set pin high */
1321 #ifdef CONFIG_OMAP1510
1324 #ifdef CONFIG_SYS_NAND_WP
1332 static inline int nandcheck(unsigned long potential, unsigned long physadr)
1337 unsigned long nand_probe(unsigned long physadr)
1339 struct nand_chip *nand = NULL;
1340 int i = 0, ChipID = 1;
1342 #ifdef CONFIG_MTD_NAND_ECC_JFFS2
1343 oob_config.ecc_pos[0] = NAND_JFFS2_OOB_ECCPOS0;
1344 oob_config.ecc_pos[1] = NAND_JFFS2_OOB_ECCPOS1;
1345 oob_config.ecc_pos[2] = NAND_JFFS2_OOB_ECCPOS2;
1346 oob_config.ecc_pos[3] = NAND_JFFS2_OOB_ECCPOS3;
1347 oob_config.ecc_pos[4] = NAND_JFFS2_OOB_ECCPOS4;
1348 oob_config.ecc_pos[5] = NAND_JFFS2_OOB_ECCPOS5;
1349 oob_config.eccvalid_pos = 4;
1351 oob_config.ecc_pos[0] = NAND_NOOB_ECCPOS0;
1352 oob_config.ecc_pos[1] = NAND_NOOB_ECCPOS1;
1353 oob_config.ecc_pos[2] = NAND_NOOB_ECCPOS2;
1354 oob_config.ecc_pos[3] = NAND_NOOB_ECCPOS3;
1355 oob_config.ecc_pos[4] = NAND_NOOB_ECCPOS4;
1356 oob_config.ecc_pos[5] = NAND_NOOB_ECCPOS5;
1357 oob_config.eccvalid_pos = NAND_NOOB_ECCVPOS;
1359 oob_config.badblock_pos = 5;
1361 for (i=0; i<CONFIG_SYS_MAX_NAND_DEVICE; i++) {
1362 if (nand_dev_desc[i].ChipID == NAND_ChipID_UNKNOWN) {
1363 nand = &nand_dev_desc[i];
1370 memset((char *)nand, 0, sizeof(struct nand_chip));
1372 nand->IO_ADDR = physadr;
1373 nand->cache_page = -1; /* init the cache page */
1374 NanD_ScanChips(nand);
1376 if (nand->totlen == 0) {
1377 /* no chips found, clean up and quit */
1378 memset((char *)nand, 0, sizeof(struct nand_chip));
1379 nand->ChipID = NAND_ChipID_UNKNOWN;
1383 nand->ChipID = ChipID;
1384 if (curr_device == -1)
1387 nand->data_buf = malloc (nand->oobblock + nand->oobsize);
1388 if (!nand->data_buf) {
1389 puts ("Cannot allocate memory for data structures.\n");
1393 return (nand->totlen);
1396 #ifdef CONFIG_MTD_NAND_ECC
1398 * Pre-calculated 256-way 1 byte column parity
1400 static const u_char nand_ecc_precalc_table[] = {
1401 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a,
1402 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00,
1403 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f,
1404 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
1405 0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c,
1406 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
1407 0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59,
1408 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
1409 0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33,
1410 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
1411 0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56,
1412 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
1413 0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55,
1414 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
1415 0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30,
1416 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
1417 0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30,
1418 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
1419 0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55,
1420 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
1421 0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56,
1422 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
1423 0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33,
1424 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
1425 0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59,
1426 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
1427 0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c,
1428 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
1429 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f,
1430 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
1431 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a,
1432 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00
1437 * Creates non-inverted ECC code from line parity
1439 static void nand_trans_result(u_char reg2, u_char reg3,
1442 u_char a, b, i, tmp1, tmp2;
1444 /* Initialize variables */
1448 /* Calculate first ECC byte */
1449 for (i = 0; i < 4; i++) {
1450 if (reg3 & a) /* LP15,13,11,9 --> ecc_code[0] */
1453 if (reg2 & a) /* LP14,12,10,8 --> ecc_code[0] */
1459 /* Calculate second ECC byte */
1461 for (i = 0; i < 4; i++) {
1462 if (reg3 & a) /* LP7,5,3,1 --> ecc_code[1] */
1465 if (reg2 & a) /* LP6,4,2,0 --> ecc_code[1] */
1471 /* Store two of the ECC bytes */
1477 * Calculate 3 byte ECC code for 256 byte block
1479 static void nand_calculate_ecc (const u_char *dat, u_char *ecc_code)
1481 u_char idx, reg1, reg3;
1484 /* Initialize variables */
1486 ecc_code[0] = ecc_code[1] = ecc_code[2] = 0;
1488 /* Build up column parity */
1489 for(j = 0; j < 256; j++) {
1491 /* Get CP0 - CP5 from table */
1492 idx = nand_ecc_precalc_table[dat[j]];
1495 /* All bit XOR = 1 ? */
1501 /* Create non-inverted ECC code from line parity */
1502 nand_trans_result((reg1 & 0x40) ? ~reg3 : reg3, reg3, ecc_code);
1504 /* Calculate final ECC code */
1505 ecc_code[0] = ~ecc_code[0];
1506 ecc_code[1] = ~ecc_code[1];
1507 ecc_code[2] = ((~reg1) << 2) | 0x03;
1511 * Detect and correct a 1 bit error for 256 byte block
1513 static int nand_correct_data (u_char *dat, u_char *read_ecc, u_char *calc_ecc)
1515 u_char a, b, c, d1, d2, d3, add, bit, i;
1517 /* Do error detection */
1518 d1 = calc_ecc[0] ^ read_ecc[0];
1519 d2 = calc_ecc[1] ^ read_ecc[1];
1520 d3 = calc_ecc[2] ^ read_ecc[2];
1522 if ((d1 | d2 | d3) == 0) {
1526 a = (d1 ^ (d1 >> 1)) & 0x55;
1527 b = (d2 ^ (d2 >> 1)) & 0x55;
1528 c = (d3 ^ (d3 >> 1)) & 0x54;
1530 /* Found and will correct single bit error in the data */
1531 if ((a == 0x55) && (b == 0x55) && (c == 0x54)) {
1535 for (i=0; i<4; i++) {
1542 for (i=0; i<4; i++) {
1551 for (i=0; i<3; i++) {
1581 /* ECC Code Error Correction */
1582 read_ecc[0] = calc_ecc[0];
1583 read_ecc[1] = calc_ecc[1];
1584 read_ecc[2] = calc_ecc[2];
1588 /* Uncorrectable Error */
1594 /* Should never happen */
1600 #ifdef CONFIG_JFFS2_NAND
1601 int read_jffs2_nand(size_t start, size_t len,
1602 size_t * retlen, u_char * buf, int nanddev)
1604 return nand_legacy_rw(nand_dev_desc + nanddev, NANDRW_READ | NANDRW_JFFS2,
1605 start, len, retlen, buf);
1607 #endif /* CONFIG_JFFS2_NAND */