2 * Copyright (c) 2011 The Chromium OS Authors.
3 * (C) Copyright 2011 NVIDIA Corporation <www.nvidia.com>
4 * (C) Copyright 2006 Detlev Zundel, dzu@denx.de
5 * (C) Copyright 2006 DENX Software Engineering
7 * See file CREDITS for list of people who contributed to this
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 #include <asm/arch/clock.h>
30 #include <asm/arch/funcmux.h>
31 #include <asm/arch-tegra/clk_rst.h>
32 #include <asm/errno.h>
35 #include "tegra_nand.h"
37 DECLARE_GLOBAL_DATA_PTR;
39 #define NAND_CMD_TIMEOUT_MS 10
41 #define SKIPPED_SPARE_BYTES 4
43 /* ECC bytes to be generated for tag data */
44 #define TAG_ECC_BYTES 4
46 /* 64 byte oob block info for large page (== 2KB) device
48 * OOB flash layout for Tegra with Reed-Solomon 4 symbol correct ECC:
54 * Yaffs2 will use 16 tag bytes.
56 static struct nand_ecclayout eccoob = {
59 4, 5, 6, 7, 8, 9, 10, 11, 12,
60 13, 14, 15, 16, 17, 18, 19, 20, 21,
61 22, 23, 24, 25, 26, 27, 28, 29, 30,
62 31, 32, 33, 34, 35, 36, 37, 38, 39,
75 ECC_TAG_ERROR = 1 << 0,
76 ECC_DATA_ERROR = 1 << 1
79 /* Timing parameters */
81 FDT_NAND_MAX_TRP_TREA,
83 FDT_NAND_MAX_TCR_TAR_TRR,
85 FDT_NAND_MAX_TCS_TCH_TALS_TALH,
94 /* Information about an attached NAND chip */
96 struct nand_ctlr *reg;
97 int enabled; /* 1 to enable, 0 to disable */
98 struct fdt_gpio_state wp_gpio; /* write-protect GPIO */
99 s32 width; /* bit width, normally 8 */
100 u32 timing[FDT_NAND_TIMING_COUNT];
104 struct nand_ctlr *reg;
107 * When running in PIO mode to get READ ID bytes from register
108 * RESP_0, we need this variable as an index to know which byte in
109 * register RESP_0 should be read.
110 * Because common code in nand_base.c invokes read_byte function two
111 * times for NAND_CMD_READID.
112 * And our controller returns 4 bytes at once in register RESP_0.
115 struct fdt_nand config;
118 static struct nand_drv nand_ctrl;
119 static struct mtd_info *our_mtd;
120 static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
122 #ifdef CONFIG_SYS_DCACHE_OFF
123 static inline void dma_prepare(void *start, unsigned long length,
129 * Prepare for a DMA transaction
131 * For a write we flush out our data. For a read we invalidate, since we
132 * need to do this before we read from the buffer after the DMA has
133 * completed, so may as well do it now.
135 * @param start Start address for DMA buffer (should be cache-aligned)
136 * @param length Length of DMA buffer in bytes
137 * @param is_writing 0 if reading, non-zero if writing
139 static void dma_prepare(void *start, unsigned long length, int is_writing)
141 unsigned long addr = (unsigned long)start;
143 length = ALIGN(length, ARCH_DMA_MINALIGN);
145 flush_dcache_range(addr, addr + length);
147 invalidate_dcache_range(addr, addr + length);
152 * Wait for command completion
154 * @param reg nand_ctlr structure
156 * 1 - Command completed
159 static int nand_waitfor_cmd_completion(struct nand_ctlr *reg)
165 for (i = 0; i < NAND_CMD_TIMEOUT_MS * 1000; i++) {
166 if ((readl(®->command) & CMD_GO) ||
167 !(readl(®->status) & STATUS_RBSY0) ||
168 !(readl(®->isr) & ISR_IS_CMD_DONE)) {
172 reg_val = readl(®->dma_mst_ctrl);
174 * If DMA_MST_CTRL_EN_A_ENABLE or DMA_MST_CTRL_EN_B_ENABLE
175 * is set, that means DMA engine is running.
177 * Then we have to wait until DMA_MST_CTRL_IS_DMA_DONE
178 * is cleared, indicating DMA transfer completion.
180 running = reg_val & (DMA_MST_CTRL_EN_A_ENABLE |
181 DMA_MST_CTRL_EN_B_ENABLE);
182 if (!running || (reg_val & DMA_MST_CTRL_IS_DMA_DONE))
190 * Read one byte from the chip
192 * @param mtd MTD device structure
195 * Read function for 8bit bus-width
197 static uint8_t read_byte(struct mtd_info *mtd)
199 struct nand_chip *chip = mtd->priv;
201 struct nand_drv *info;
203 info = (struct nand_drv *)chip->priv;
205 /* In PIO mode, only 4 bytes can be transferred with single CMD_GO. */
206 if (info->pio_byte_index > 3) {
207 info->pio_byte_index = 0;
208 writel(CMD_GO | CMD_PIO
210 &info->reg->command);
211 if (!nand_waitfor_cmd_completion(info->reg))
212 printf("Command timeout\n");
215 dword_read = readl(&info->reg->resp);
216 dword_read = dword_read >> (8 * info->pio_byte_index);
217 info->pio_byte_index++;
218 return (uint8_t)dword_read;
222 * Read len bytes from the chip into a buffer
224 * @param mtd MTD device structure
225 * @param buf buffer to store data to
226 * @param len number of bytes to read
228 * Read function for 8bit bus-width
230 static void read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
234 struct nand_chip *chip = mtd->priv;
235 struct nand_drv *info = (struct nand_drv *)chip->priv;
237 for (i = 0; i < len; i += 4) {
238 s = (len - i) > 4 ? 4 : len - i;
239 writel(CMD_PIO | CMD_RX | CMD_A_VALID | CMD_CE0 |
240 ((s - 1) << CMD_TRANS_SIZE_SHIFT) | CMD_GO,
241 &info->reg->command);
242 if (!nand_waitfor_cmd_completion(info->reg))
243 puts("Command timeout during read_buf\n");
244 reg = readl(&info->reg->resp);
245 memcpy(buf + i, ®, s);
250 * Check NAND status to see if it is ready or not
252 * @param mtd MTD device structure
257 static int nand_dev_ready(struct mtd_info *mtd)
259 struct nand_chip *chip = mtd->priv;
261 struct nand_drv *info;
263 info = (struct nand_drv *)chip->priv;
265 reg_val = readl(&info->reg->status);
266 if (reg_val & STATUS_RBSY0)
272 /* Dummy implementation: we don't support multiple chips */
273 static void nand_select_chip(struct mtd_info *mtd, int chipnr)
286 * Clear all interrupt status bits
288 * @param reg nand_ctlr structure
290 static void nand_clear_interrupt_status(struct nand_ctlr *reg)
294 /* Clear interrupt status */
295 reg_val = readl(®->isr);
296 writel(reg_val, ®->isr);
300 * Send command to NAND device
302 * @param mtd MTD device structure
303 * @param command the command to be sent
304 * @param column the column address for this command, -1 if none
305 * @param page_addr the page address for this command, -1 if none
307 static void nand_command(struct mtd_info *mtd, unsigned int command,
308 int column, int page_addr)
310 struct nand_chip *chip = mtd->priv;
311 struct nand_drv *info;
313 info = (struct nand_drv *)chip->priv;
316 * Write out the command to the device.
318 * Only command NAND_CMD_RESET or NAND_CMD_READID will come
319 * here before mtd->writesize is initialized.
322 /* Emulate NAND_CMD_READOOB */
323 if (command == NAND_CMD_READOOB) {
324 assert(mtd->writesize != 0);
325 column += mtd->writesize;
326 command = NAND_CMD_READ0;
329 /* Adjust columns for 16 bit bus-width */
330 if (column != -1 && (chip->options & NAND_BUSWIDTH_16))
333 nand_clear_interrupt_status(info->reg);
335 /* Stop DMA engine, clear DMA completion status */
336 writel(DMA_MST_CTRL_EN_A_DISABLE
337 | DMA_MST_CTRL_EN_B_DISABLE
338 | DMA_MST_CTRL_IS_DMA_DONE,
339 &info->reg->dma_mst_ctrl);
342 * Program and erase have their own busy handlers
343 * status and sequential in needs no delay
346 case NAND_CMD_READID:
347 writel(NAND_CMD_READID, &info->reg->cmd_reg1);
348 writel(column & 0xFF, &info->reg->addr_reg1);
349 writel(CMD_GO | CMD_CLE | CMD_ALE | CMD_PIO
351 ((4 - 1) << CMD_TRANS_SIZE_SHIFT)
353 &info->reg->command);
354 info->pio_byte_index = 0;
357 writel(NAND_CMD_PARAM, &info->reg->cmd_reg1);
358 writel(column & 0xFF, &info->reg->addr_reg1);
359 writel(CMD_GO | CMD_CLE | CMD_ALE | CMD_CE0,
360 &info->reg->command);
363 writel(NAND_CMD_READ0, &info->reg->cmd_reg1);
364 writel(NAND_CMD_READSTART, &info->reg->cmd_reg2);
365 writel((page_addr << 16) | (column & 0xFFFF),
366 &info->reg->addr_reg1);
367 writel(page_addr >> 16, &info->reg->addr_reg2);
370 writel(NAND_CMD_SEQIN, &info->reg->cmd_reg1);
371 writel(NAND_CMD_PAGEPROG, &info->reg->cmd_reg2);
372 writel((page_addr << 16) | (column & 0xFFFF),
373 &info->reg->addr_reg1);
374 writel(page_addr >> 16,
375 &info->reg->addr_reg2);
377 case NAND_CMD_PAGEPROG:
379 case NAND_CMD_ERASE1:
380 writel(NAND_CMD_ERASE1, &info->reg->cmd_reg1);
381 writel(NAND_CMD_ERASE2, &info->reg->cmd_reg2);
382 writel(page_addr, &info->reg->addr_reg1);
383 writel(CMD_GO | CMD_CLE | CMD_ALE |
384 CMD_SEC_CMD | CMD_CE0 | CMD_ALE_BYTES3,
385 &info->reg->command);
387 case NAND_CMD_ERASE2:
389 case NAND_CMD_STATUS:
390 writel(NAND_CMD_STATUS, &info->reg->cmd_reg1);
391 writel(CMD_GO | CMD_CLE | CMD_PIO | CMD_RX
392 | ((1 - 0) << CMD_TRANS_SIZE_SHIFT)
394 &info->reg->command);
395 info->pio_byte_index = 0;
398 writel(NAND_CMD_RESET, &info->reg->cmd_reg1);
399 writel(CMD_GO | CMD_CLE | CMD_CE0,
400 &info->reg->command);
402 case NAND_CMD_RNDOUT:
404 printf("%s: Unsupported command %d\n", __func__, command);
407 if (!nand_waitfor_cmd_completion(info->reg))
408 printf("Command 0x%02X timeout\n", command);
412 * Check whether the pointed buffer are all 0xff (blank).
414 * @param buf data buffer for blank check
415 * @param len length of the buffer in byte
420 static int blank_check(u8 *buf, int len)
424 for (i = 0; i < len; i++)
431 * After a DMA transfer for read, we call this function to see whether there
432 * is any uncorrectable error on the pointed data buffer or oob buffer.
434 * @param reg nand_ctlr structure
435 * @param databuf data buffer
436 * @param a_len data buffer length
437 * @param oobbuf oob buffer
438 * @param b_len oob buffer length
440 * ECC_OK - no ECC error or correctable ECC error
441 * ECC_TAG_ERROR - uncorrectable tag ECC error
442 * ECC_DATA_ERROR - uncorrectable data ECC error
443 * ECC_DATA_ERROR + ECC_TAG_ERROR - uncorrectable data+tag ECC error
445 static int check_ecc_error(struct nand_ctlr *reg, u8 *databuf,
446 int a_len, u8 *oobbuf, int b_len)
448 int return_val = ECC_OK;
451 if (!(readl(®->isr) & ISR_IS_ECC_ERR))
455 * Area A is used for the data block (databuf). Area B is used for
456 * the spare block (oobbuf)
458 reg_val = readl(®->dec_status);
459 if ((reg_val & DEC_STATUS_A_ECC_FAIL) && databuf) {
460 reg_val = readl(®->bch_dec_status_buf);
462 * If uncorrectable error occurs on data area, then see whether
463 * they are all FF. If all are FF, it's a blank page.
466 if ((reg_val & BCH_DEC_STATUS_FAIL_SEC_FLAG_MASK) &&
467 !blank_check(databuf, a_len))
468 return_val |= ECC_DATA_ERROR;
471 if ((reg_val & DEC_STATUS_B_ECC_FAIL) && oobbuf) {
472 reg_val = readl(®->bch_dec_status_buf);
474 * If uncorrectable error occurs on tag area, then see whether
475 * they are all FF. If all are FF, it's a blank page.
478 if ((reg_val & BCH_DEC_STATUS_FAIL_TAG_MASK) &&
479 !blank_check(oobbuf, b_len))
480 return_val |= ECC_TAG_ERROR;
487 * Set GO bit to send command to device
489 * @param reg nand_ctlr structure
491 static void start_command(struct nand_ctlr *reg)
495 reg_val = readl(®->command);
497 writel(reg_val, ®->command);
501 * Clear command GO bit, DMA GO bit, and DMA completion status
503 * @param reg nand_ctlr structure
505 static void stop_command(struct nand_ctlr *reg)
508 writel(0, ®->command);
510 /* Stop DMA engine and clear DMA completion status */
511 writel(DMA_MST_CTRL_GO_DISABLE
512 | DMA_MST_CTRL_IS_DMA_DONE,
517 * Set up NAND bus width and page size
519 * @param info nand_info structure
520 * @param *reg_val address of reg_val
521 * @return 0 if ok, -1 on error
523 static int set_bus_width_page_size(struct fdt_nand *config,
526 if (config->width == 8)
527 *reg_val = CFG_BUS_WIDTH_8BIT;
528 else if (config->width == 16)
529 *reg_val = CFG_BUS_WIDTH_16BIT;
531 debug("%s: Unsupported bus width %d\n", __func__,
536 if (our_mtd->writesize == 512)
537 *reg_val |= CFG_PAGE_SIZE_512;
538 else if (our_mtd->writesize == 2048)
539 *reg_val |= CFG_PAGE_SIZE_2048;
540 else if (our_mtd->writesize == 4096)
541 *reg_val |= CFG_PAGE_SIZE_4096;
543 debug("%s: Unsupported page size %d\n", __func__,
552 * Page read/write function
554 * @param mtd mtd info structure
555 * @param chip nand chip info structure
556 * @param buf data buffer
557 * @param page page number
558 * @param with_ecc 1 to enable ECC, 0 to disable ECC
559 * @param is_writing 0 for read, 1 for write
560 * @return 0 when successfully completed
561 * -EIO when command timeout
563 static int nand_rw_page(struct mtd_info *mtd, struct nand_chip *chip,
564 uint8_t *buf, int page, int with_ecc, int is_writing)
568 struct nand_oobfree *free = chip->ecc.layout->oobfree;
569 /* 4*128=512 (byte) is the value that our HW can support. */
570 ALLOC_CACHE_ALIGN_BUFFER(u32, tag_buf, 128);
572 struct nand_drv *info;
573 struct fdt_nand *config;
575 if ((uintptr_t)buf & 0x03) {
576 printf("buf %p has to be 4-byte aligned\n", buf);
580 info = (struct nand_drv *)chip->priv;
581 config = &info->config;
582 if (set_bus_width_page_size(config, ®_val))
585 /* Need to be 4-byte aligned */
586 tag_ptr = (char *)tag_buf;
588 stop_command(info->reg);
590 writel((1 << chip->page_shift) - 1, &info->reg->dma_cfg_a);
591 writel(virt_to_phys(buf), &info->reg->data_block_ptr);
594 writel(virt_to_phys(tag_ptr), &info->reg->tag_ptr);
596 memcpy(tag_ptr, chip->oob_poi + free->offset,
597 chip->ecc.layout->oobavail +
600 writel(virt_to_phys(chip->oob_poi), &info->reg->tag_ptr);
603 /* Set ECC selection, configure ECC settings */
605 tag_size = chip->ecc.layout->oobavail + TAG_ECC_BYTES;
606 reg_val |= (CFG_SKIP_SPARE_SEL_4
607 | CFG_SKIP_SPARE_ENABLE
608 | CFG_HW_ECC_CORRECTION_ENABLE
609 | CFG_ECC_EN_TAG_DISABLE
616 tag_size += SKIPPED_SPARE_BYTES;
617 dma_prepare(tag_ptr, tag_size, is_writing);
619 tag_size = mtd->oobsize;
620 reg_val |= (CFG_SKIP_SPARE_DISABLE
621 | CFG_HW_ECC_CORRECTION_DISABLE
622 | CFG_ECC_EN_TAG_DISABLE
625 dma_prepare(chip->oob_poi, tag_size, is_writing);
627 writel(reg_val, &info->reg->config);
629 dma_prepare(buf, 1 << chip->page_shift, is_writing);
631 writel(BCH_CONFIG_BCH_ECC_DISABLE, &info->reg->bch_config);
633 writel(tag_size - 1, &info->reg->dma_cfg_b);
635 nand_clear_interrupt_status(info->reg);
637 reg_val = CMD_CLE | CMD_ALE
639 | (CMD_ALE_BYTES5 << CMD_ALE_BYTE_SIZE_SHIFT)
642 | (CMD_TRANS_SIZE_PAGE << CMD_TRANS_SIZE_SHIFT)
645 reg_val |= (CMD_AFT_DAT_DISABLE | CMD_RX);
647 reg_val |= (CMD_AFT_DAT_ENABLE | CMD_TX);
648 writel(reg_val, &info->reg->command);
650 /* Setup DMA engine */
651 reg_val = DMA_MST_CTRL_GO_ENABLE
652 | DMA_MST_CTRL_BURST_8WORDS
653 | DMA_MST_CTRL_EN_A_ENABLE
654 | DMA_MST_CTRL_EN_B_ENABLE;
657 reg_val |= DMA_MST_CTRL_DIR_READ;
659 reg_val |= DMA_MST_CTRL_DIR_WRITE;
661 writel(reg_val, &info->reg->dma_mst_ctrl);
663 start_command(info->reg);
665 if (!nand_waitfor_cmd_completion(info->reg)) {
667 printf("Read Page 0x%X timeout ", page);
669 printf("Write Page 0x%X timeout ", page);
673 printf("without ECC");
678 if (with_ecc && !is_writing) {
679 memcpy(chip->oob_poi, tag_ptr,
680 SKIPPED_SPARE_BYTES);
681 memcpy(chip->oob_poi + free->offset,
682 tag_ptr + SKIPPED_SPARE_BYTES,
683 chip->ecc.layout->oobavail);
684 reg_val = (u32)check_ecc_error(info->reg, (u8 *)buf,
685 1 << chip->page_shift,
686 (u8 *)(tag_ptr + SKIPPED_SPARE_BYTES),
687 chip->ecc.layout->oobavail);
688 if (reg_val & ECC_TAG_ERROR)
689 printf("Read Page 0x%X tag ECC error\n", page);
690 if (reg_val & ECC_DATA_ERROR)
691 printf("Read Page 0x%X data ECC error\n",
693 if (reg_val & (ECC_DATA_ERROR | ECC_TAG_ERROR))
700 * Hardware ecc based page read function
702 * @param mtd mtd info structure
703 * @param chip nand chip info structure
704 * @param buf buffer to store read data
705 * @param page page number to read
706 * @return 0 when successfully completed
707 * -EIO when command timeout
709 static int nand_read_page_hwecc(struct mtd_info *mtd,
710 struct nand_chip *chip, uint8_t *buf, int page)
712 return nand_rw_page(mtd, chip, buf, page, 1, 0);
716 * Hardware ecc based page write function
718 * @param mtd mtd info structure
719 * @param chip nand chip info structure
720 * @param buf data buffer
722 static void nand_write_page_hwecc(struct mtd_info *mtd,
723 struct nand_chip *chip, const uint8_t *buf)
726 struct nand_drv *info;
728 info = (struct nand_drv *)chip->priv;
730 page = (readl(&info->reg->addr_reg1) >> 16) |
731 (readl(&info->reg->addr_reg2) << 16);
733 nand_rw_page(mtd, chip, (uint8_t *)buf, page, 1, 1);
738 * Read raw page data without ecc
740 * @param mtd mtd info structure
741 * @param chip nand chip info structure
742 * @param buf buffer to store read data
743 * @param page page number to read
744 * @return 0 when successfully completed
745 * -EINVAL when chip->oob_poi is not double-word aligned
746 * -EIO when command timeout
748 static int nand_read_page_raw(struct mtd_info *mtd,
749 struct nand_chip *chip, uint8_t *buf, int page)
751 return nand_rw_page(mtd, chip, buf, page, 0, 0);
755 * Raw page write function
757 * @param mtd mtd info structure
758 * @param chip nand chip info structure
759 * @param buf data buffer
761 static void nand_write_page_raw(struct mtd_info *mtd,
762 struct nand_chip *chip, const uint8_t *buf)
765 struct nand_drv *info;
767 info = (struct nand_drv *)chip->priv;
768 page = (readl(&info->reg->addr_reg1) >> 16) |
769 (readl(&info->reg->addr_reg2) << 16);
771 nand_rw_page(mtd, chip, (uint8_t *)buf, page, 0, 1);
775 * OOB data read/write function
777 * @param mtd mtd info structure
778 * @param chip nand chip info structure
779 * @param page page number to read
780 * @param with_ecc 1 to enable ECC, 0 to disable ECC
781 * @param is_writing 0 for read, 1 for write
782 * @return 0 when successfully completed
783 * -EINVAL when chip->oob_poi is not double-word aligned
784 * -EIO when command timeout
786 static int nand_rw_oob(struct mtd_info *mtd, struct nand_chip *chip,
787 int page, int with_ecc, int is_writing)
791 struct nand_oobfree *free = chip->ecc.layout->oobfree;
792 struct nand_drv *info;
794 if (((int)chip->oob_poi) & 0x03)
796 info = (struct nand_drv *)chip->priv;
797 if (set_bus_width_page_size(&info->config, ®_val))
800 stop_command(info->reg);
802 writel(virt_to_phys(chip->oob_poi), &info->reg->tag_ptr);
804 /* Set ECC selection */
805 tag_size = mtd->oobsize;
807 reg_val |= CFG_ECC_EN_TAG_ENABLE;
809 reg_val |= (CFG_ECC_EN_TAG_DISABLE);
811 reg_val |= ((tag_size - 1) |
812 CFG_SKIP_SPARE_DISABLE |
813 CFG_HW_ECC_CORRECTION_DISABLE |
815 writel(reg_val, &info->reg->config);
817 dma_prepare(chip->oob_poi, tag_size, is_writing);
819 writel(BCH_CONFIG_BCH_ECC_DISABLE, &info->reg->bch_config);
821 if (is_writing && with_ecc)
822 tag_size -= TAG_ECC_BYTES;
824 writel(tag_size - 1, &info->reg->dma_cfg_b);
826 nand_clear_interrupt_status(info->reg);
828 reg_val = CMD_CLE | CMD_ALE
830 | (CMD_ALE_BYTES5 << CMD_ALE_BYTE_SIZE_SHIFT)
834 reg_val |= (CMD_AFT_DAT_DISABLE | CMD_RX);
836 reg_val |= (CMD_AFT_DAT_ENABLE | CMD_TX);
837 writel(reg_val, &info->reg->command);
839 /* Setup DMA engine */
840 reg_val = DMA_MST_CTRL_GO_ENABLE
841 | DMA_MST_CTRL_BURST_8WORDS
842 | DMA_MST_CTRL_EN_B_ENABLE;
844 reg_val |= DMA_MST_CTRL_DIR_READ;
846 reg_val |= DMA_MST_CTRL_DIR_WRITE;
848 writel(reg_val, &info->reg->dma_mst_ctrl);
850 start_command(info->reg);
852 if (!nand_waitfor_cmd_completion(info->reg)) {
854 printf("Read OOB of Page 0x%X timeout\n", page);
856 printf("Write OOB of Page 0x%X timeout\n", page);
860 if (with_ecc && !is_writing) {
861 reg_val = (u32)check_ecc_error(info->reg, 0, 0,
862 (u8 *)(chip->oob_poi + free->offset),
863 chip->ecc.layout->oobavail);
864 if (reg_val & ECC_TAG_ERROR)
865 printf("Read OOB of Page 0x%X tag ECC error\n", page);
871 * OOB data read function
873 * @param mtd mtd info structure
874 * @param chip nand chip info structure
875 * @param page page number to read
876 * @param sndcmd flag whether to issue read command or not
877 * @return 1 - issue read command next time
880 static int nand_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
881 int page, int sndcmd)
884 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
887 nand_rw_oob(mtd, chip, page, 0, 0);
892 * OOB data write function
894 * @param mtd mtd info structure
895 * @param chip nand chip info structure
896 * @param page page number to write
897 * @return 0 when successfully completed
898 * -EINVAL when chip->oob_poi is not double-word aligned
899 * -EIO when command timeout
901 static int nand_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
904 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
906 return nand_rw_oob(mtd, chip, page, 0, 1);
910 * Set up NAND memory timings according to the provided parameters
912 * @param timing Timing parameters
913 * @param reg NAND controller register address
915 static void setup_timing(unsigned timing[FDT_NAND_TIMING_COUNT],
916 struct nand_ctlr *reg)
918 u32 reg_val, clk_rate, clk_period, time_val;
920 clk_rate = (u32)clock_get_periph_rate(PERIPH_ID_NDFLASH,
921 CLOCK_ID_PERIPH) / 1000000;
922 clk_period = 1000 / clk_rate;
923 reg_val = ((timing[FDT_NAND_MAX_TRP_TREA] / clk_period) <<
924 TIMING_TRP_RESP_CNT_SHIFT) & TIMING_TRP_RESP_CNT_MASK;
925 reg_val |= ((timing[FDT_NAND_TWB] / clk_period) <<
926 TIMING_TWB_CNT_SHIFT) & TIMING_TWB_CNT_MASK;
927 time_val = timing[FDT_NAND_MAX_TCR_TAR_TRR] / clk_period;
929 reg_val |= ((time_val - 2) << TIMING_TCR_TAR_TRR_CNT_SHIFT) &
930 TIMING_TCR_TAR_TRR_CNT_MASK;
931 reg_val |= ((timing[FDT_NAND_TWHR] / clk_period) <<
932 TIMING_TWHR_CNT_SHIFT) & TIMING_TWHR_CNT_MASK;
933 time_val = timing[FDT_NAND_MAX_TCS_TCH_TALS_TALH] / clk_period;
935 reg_val |= ((time_val - 1) << TIMING_TCS_CNT_SHIFT) &
937 reg_val |= ((timing[FDT_NAND_TWH] / clk_period) <<
938 TIMING_TWH_CNT_SHIFT) & TIMING_TWH_CNT_MASK;
939 reg_val |= ((timing[FDT_NAND_TWP] / clk_period) <<
940 TIMING_TWP_CNT_SHIFT) & TIMING_TWP_CNT_MASK;
941 reg_val |= ((timing[FDT_NAND_TRH] / clk_period) <<
942 TIMING_TRH_CNT_SHIFT) & TIMING_TRH_CNT_MASK;
943 reg_val |= ((timing[FDT_NAND_MAX_TRP_TREA] / clk_period) <<
944 TIMING_TRP_CNT_SHIFT) & TIMING_TRP_CNT_MASK;
945 writel(reg_val, ®->timing);
948 time_val = timing[FDT_NAND_TADL] / clk_period;
950 reg_val = (time_val - 2) & TIMING2_TADL_CNT_MASK;
951 writel(reg_val, ®->timing2);
955 * Decode NAND parameters from the device tree
957 * @param blob Device tree blob
958 * @param node Node containing "nand-flash" compatble node
959 * @return 0 if ok, -ve on error (FDT_ERR_...)
961 static int fdt_decode_nand(const void *blob, int node, struct fdt_nand *config)
965 config->reg = (struct nand_ctlr *)fdtdec_get_addr(blob, node, "reg");
966 config->enabled = fdtdec_get_is_enabled(blob, node);
967 config->width = fdtdec_get_int(blob, node, "nvidia,nand-width", 8);
968 err = fdtdec_decode_gpio(blob, node, "nvidia,wp-gpios",
972 err = fdtdec_get_int_array(blob, node, "nvidia,timing",
973 config->timing, FDT_NAND_TIMING_COUNT);
977 /* Now look up the controller and decode that */
978 node = fdt_next_node(blob, node, NULL);
986 * Board-specific NAND initialization
988 * @param nand nand chip info structure
989 * @return 0, after initialized, -1 on error
991 int tegra_nand_init(struct nand_chip *nand, int devnum)
993 struct nand_drv *info = &nand_ctrl;
994 struct fdt_nand *config = &info->config;
997 node = fdtdec_next_compatible(gd->fdt_blob, 0,
998 COMPAT_NVIDIA_TEGRA20_NAND);
1001 if (fdt_decode_nand(gd->fdt_blob, node, config)) {
1002 printf("Could not decode nand-flash in device tree\n");
1005 if (!config->enabled)
1007 info->reg = config->reg;
1008 nand->ecc.mode = NAND_ECC_HW;
1009 nand->ecc.layout = &eccoob;
1011 nand->options = LP_OPTIONS;
1012 nand->cmdfunc = nand_command;
1013 nand->read_byte = read_byte;
1014 nand->read_buf = read_buf;
1015 nand->ecc.read_page = nand_read_page_hwecc;
1016 nand->ecc.write_page = nand_write_page_hwecc;
1017 nand->ecc.read_page_raw = nand_read_page_raw;
1018 nand->ecc.write_page_raw = nand_write_page_raw;
1019 nand->ecc.read_oob = nand_read_oob;
1020 nand->ecc.write_oob = nand_write_oob;
1021 nand->select_chip = nand_select_chip;
1022 nand->dev_ready = nand_dev_ready;
1023 nand->priv = &nand_ctrl;
1025 /* Adjust controller clock rate */
1026 clock_start_periph_pll(PERIPH_ID_NDFLASH, CLOCK_ID_PERIPH, 52000000);
1028 /* Adjust timing for NAND device */
1029 setup_timing(config->timing, info->reg);
1031 fdtdec_setup_gpio(&config->wp_gpio);
1032 gpio_direction_output(config->wp_gpio.gpio, 1);
1034 our_mtd = &nand_info[devnum];
1035 our_mtd->priv = nand;
1036 ret = nand_scan_ident(our_mtd, CONFIG_SYS_NAND_MAX_CHIPS, NULL);
1040 nand->ecc.size = our_mtd->writesize;
1041 nand->ecc.bytes = our_mtd->oobsize;
1043 ret = nand_scan_tail(our_mtd);
1047 ret = nand_register(devnum);
1054 void board_nand_init(void)
1056 struct nand_chip *nand = &nand_chip[0];
1058 if (tegra_nand_init(nand, 0))
1059 puts("Tegra NAND init failed\n");