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 oob_required
, 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 int nand_write_page_hwecc(struct mtd_info
*mtd
,
723 struct nand_chip
*chip
, const uint8_t *buf
, int oob_required
)
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);
739 * Read raw page data without ecc
741 * @param mtd mtd info structure
742 * @param chip nand chip info structure
743 * @param buf buffer to store read data
744 * @param page page number to read
745 * @return 0 when successfully completed
746 * -EINVAL when chip->oob_poi is not double-word aligned
747 * -EIO when command timeout
749 static int nand_read_page_raw(struct mtd_info
*mtd
,
750 struct nand_chip
*chip
, uint8_t *buf
, int oob_required
, int page
)
752 return nand_rw_page(mtd
, chip
, buf
, page
, 0, 0);
756 * Raw page write function
758 * @param mtd mtd info structure
759 * @param chip nand chip info structure
760 * @param buf data buffer
762 static int nand_write_page_raw(struct mtd_info
*mtd
,
763 struct nand_chip
*chip
, const uint8_t *buf
, int oob_required
)
766 struct nand_drv
*info
;
768 info
= (struct nand_drv
*)chip
->priv
;
769 page
= (readl(&info
->reg
->addr_reg1
) >> 16) |
770 (readl(&info
->reg
->addr_reg2
) << 16);
772 nand_rw_page(mtd
, chip
, (uint8_t *)buf
, page
, 0, 1);
777 * OOB data read/write function
779 * @param mtd mtd info structure
780 * @param chip nand chip info structure
781 * @param page page number to read
782 * @param with_ecc 1 to enable ECC, 0 to disable ECC
783 * @param is_writing 0 for read, 1 for write
784 * @return 0 when successfully completed
785 * -EINVAL when chip->oob_poi is not double-word aligned
786 * -EIO when command timeout
788 static int nand_rw_oob(struct mtd_info
*mtd
, struct nand_chip
*chip
,
789 int page
, int with_ecc
, int is_writing
)
793 struct nand_oobfree
*free
= chip
->ecc
.layout
->oobfree
;
794 struct nand_drv
*info
;
796 if (((int)chip
->oob_poi
) & 0x03)
798 info
= (struct nand_drv
*)chip
->priv
;
799 if (set_bus_width_page_size(&info
->config
, ®_val
))
802 stop_command(info
->reg
);
804 writel(virt_to_phys(chip
->oob_poi
), &info
->reg
->tag_ptr
);
806 /* Set ECC selection */
807 tag_size
= mtd
->oobsize
;
809 reg_val
|= CFG_ECC_EN_TAG_ENABLE
;
811 reg_val
|= (CFG_ECC_EN_TAG_DISABLE
);
813 reg_val
|= ((tag_size
- 1) |
814 CFG_SKIP_SPARE_DISABLE
|
815 CFG_HW_ECC_CORRECTION_DISABLE
|
817 writel(reg_val
, &info
->reg
->config
);
819 dma_prepare(chip
->oob_poi
, tag_size
, is_writing
);
821 writel(BCH_CONFIG_BCH_ECC_DISABLE
, &info
->reg
->bch_config
);
823 if (is_writing
&& with_ecc
)
824 tag_size
-= TAG_ECC_BYTES
;
826 writel(tag_size
- 1, &info
->reg
->dma_cfg_b
);
828 nand_clear_interrupt_status(info
->reg
);
830 reg_val
= CMD_CLE
| CMD_ALE
832 | (CMD_ALE_BYTES5
<< CMD_ALE_BYTE_SIZE_SHIFT
)
836 reg_val
|= (CMD_AFT_DAT_DISABLE
| CMD_RX
);
838 reg_val
|= (CMD_AFT_DAT_ENABLE
| CMD_TX
);
839 writel(reg_val
, &info
->reg
->command
);
841 /* Setup DMA engine */
842 reg_val
= DMA_MST_CTRL_GO_ENABLE
843 | DMA_MST_CTRL_BURST_8WORDS
844 | DMA_MST_CTRL_EN_B_ENABLE
;
846 reg_val
|= DMA_MST_CTRL_DIR_READ
;
848 reg_val
|= DMA_MST_CTRL_DIR_WRITE
;
850 writel(reg_val
, &info
->reg
->dma_mst_ctrl
);
852 start_command(info
->reg
);
854 if (!nand_waitfor_cmd_completion(info
->reg
)) {
856 printf("Read OOB of Page 0x%X timeout\n", page
);
858 printf("Write OOB of Page 0x%X timeout\n", page
);
862 if (with_ecc
&& !is_writing
) {
863 reg_val
= (u32
)check_ecc_error(info
->reg
, 0, 0,
864 (u8
*)(chip
->oob_poi
+ free
->offset
),
865 chip
->ecc
.layout
->oobavail
);
866 if (reg_val
& ECC_TAG_ERROR
)
867 printf("Read OOB of Page 0x%X tag ECC error\n", page
);
873 * OOB data read function
875 * @param mtd mtd info structure
876 * @param chip nand chip info structure
877 * @param page page number to read
879 static int nand_read_oob(struct mtd_info
*mtd
, struct nand_chip
*chip
,
882 chip
->cmdfunc(mtd
, NAND_CMD_READOOB
, 0, page
);
883 nand_rw_oob(mtd
, chip
, page
, 0, 0);
888 * OOB data write function
890 * @param mtd mtd info structure
891 * @param chip nand chip info structure
892 * @param page page number to write
893 * @return 0 when successfully completed
894 * -EINVAL when chip->oob_poi is not double-word aligned
895 * -EIO when command timeout
897 static int nand_write_oob(struct mtd_info
*mtd
, struct nand_chip
*chip
,
900 chip
->cmdfunc(mtd
, NAND_CMD_SEQIN
, mtd
->writesize
, page
);
902 return nand_rw_oob(mtd
, chip
, page
, 0, 1);
906 * Set up NAND memory timings according to the provided parameters
908 * @param timing Timing parameters
909 * @param reg NAND controller register address
911 static void setup_timing(unsigned timing
[FDT_NAND_TIMING_COUNT
],
912 struct nand_ctlr
*reg
)
914 u32 reg_val
, clk_rate
, clk_period
, time_val
;
916 clk_rate
= (u32
)clock_get_periph_rate(PERIPH_ID_NDFLASH
,
917 CLOCK_ID_PERIPH
) / 1000000;
918 clk_period
= 1000 / clk_rate
;
919 reg_val
= ((timing
[FDT_NAND_MAX_TRP_TREA
] / clk_period
) <<
920 TIMING_TRP_RESP_CNT_SHIFT
) & TIMING_TRP_RESP_CNT_MASK
;
921 reg_val
|= ((timing
[FDT_NAND_TWB
] / clk_period
) <<
922 TIMING_TWB_CNT_SHIFT
) & TIMING_TWB_CNT_MASK
;
923 time_val
= timing
[FDT_NAND_MAX_TCR_TAR_TRR
] / clk_period
;
925 reg_val
|= ((time_val
- 2) << TIMING_TCR_TAR_TRR_CNT_SHIFT
) &
926 TIMING_TCR_TAR_TRR_CNT_MASK
;
927 reg_val
|= ((timing
[FDT_NAND_TWHR
] / clk_period
) <<
928 TIMING_TWHR_CNT_SHIFT
) & TIMING_TWHR_CNT_MASK
;
929 time_val
= timing
[FDT_NAND_MAX_TCS_TCH_TALS_TALH
] / clk_period
;
931 reg_val
|= ((time_val
- 1) << TIMING_TCS_CNT_SHIFT
) &
933 reg_val
|= ((timing
[FDT_NAND_TWH
] / clk_period
) <<
934 TIMING_TWH_CNT_SHIFT
) & TIMING_TWH_CNT_MASK
;
935 reg_val
|= ((timing
[FDT_NAND_TWP
] / clk_period
) <<
936 TIMING_TWP_CNT_SHIFT
) & TIMING_TWP_CNT_MASK
;
937 reg_val
|= ((timing
[FDT_NAND_TRH
] / clk_period
) <<
938 TIMING_TRH_CNT_SHIFT
) & TIMING_TRH_CNT_MASK
;
939 reg_val
|= ((timing
[FDT_NAND_MAX_TRP_TREA
] / clk_period
) <<
940 TIMING_TRP_CNT_SHIFT
) & TIMING_TRP_CNT_MASK
;
941 writel(reg_val
, ®
->timing
);
944 time_val
= timing
[FDT_NAND_TADL
] / clk_period
;
946 reg_val
= (time_val
- 2) & TIMING2_TADL_CNT_MASK
;
947 writel(reg_val
, ®
->timing2
);
951 * Decode NAND parameters from the device tree
953 * @param blob Device tree blob
954 * @param node Node containing "nand-flash" compatble node
955 * @return 0 if ok, -ve on error (FDT_ERR_...)
957 static int fdt_decode_nand(const void *blob
, int node
, struct fdt_nand
*config
)
961 config
->reg
= (struct nand_ctlr
*)fdtdec_get_addr(blob
, node
, "reg");
962 config
->enabled
= fdtdec_get_is_enabled(blob
, node
);
963 config
->width
= fdtdec_get_int(blob
, node
, "nvidia,nand-width", 8);
964 err
= fdtdec_decode_gpio(blob
, node
, "nvidia,wp-gpios",
968 err
= fdtdec_get_int_array(blob
, node
, "nvidia,timing",
969 config
->timing
, FDT_NAND_TIMING_COUNT
);
973 /* Now look up the controller and decode that */
974 node
= fdt_next_node(blob
, node
, NULL
);
982 * Board-specific NAND initialization
984 * @param nand nand chip info structure
985 * @return 0, after initialized, -1 on error
987 int tegra_nand_init(struct nand_chip
*nand
, int devnum
)
989 struct nand_drv
*info
= &nand_ctrl
;
990 struct fdt_nand
*config
= &info
->config
;
993 node
= fdtdec_next_compatible(gd
->fdt_blob
, 0,
994 COMPAT_NVIDIA_TEGRA20_NAND
);
997 if (fdt_decode_nand(gd
->fdt_blob
, node
, config
)) {
998 printf("Could not decode nand-flash in device tree\n");
1001 if (!config
->enabled
)
1003 info
->reg
= config
->reg
;
1004 nand
->ecc
.mode
= NAND_ECC_HW
;
1005 nand
->ecc
.layout
= &eccoob
;
1007 nand
->options
= LP_OPTIONS
;
1008 nand
->cmdfunc
= nand_command
;
1009 nand
->read_byte
= read_byte
;
1010 nand
->read_buf
= read_buf
;
1011 nand
->ecc
.read_page
= nand_read_page_hwecc
;
1012 nand
->ecc
.write_page
= nand_write_page_hwecc
;
1013 nand
->ecc
.read_page_raw
= nand_read_page_raw
;
1014 nand
->ecc
.write_page_raw
= nand_write_page_raw
;
1015 nand
->ecc
.read_oob
= nand_read_oob
;
1016 nand
->ecc
.write_oob
= nand_write_oob
;
1017 nand
->ecc
.strength
= 1;
1018 nand
->select_chip
= nand_select_chip
;
1019 nand
->dev_ready
= nand_dev_ready
;
1020 nand
->priv
= &nand_ctrl
;
1022 /* Adjust controller clock rate */
1023 clock_start_periph_pll(PERIPH_ID_NDFLASH
, CLOCK_ID_PERIPH
, 52000000);
1025 /* Adjust timing for NAND device */
1026 setup_timing(config
->timing
, info
->reg
);
1028 fdtdec_setup_gpio(&config
->wp_gpio
);
1029 gpio_direction_output(config
->wp_gpio
.gpio
, 1);
1031 our_mtd
= &nand_info
[devnum
];
1032 our_mtd
->priv
= nand
;
1033 ret
= nand_scan_ident(our_mtd
, CONFIG_SYS_NAND_MAX_CHIPS
, NULL
);
1037 nand
->ecc
.size
= our_mtd
->writesize
;
1038 nand
->ecc
.bytes
= our_mtd
->oobsize
;
1040 ret
= nand_scan_tail(our_mtd
);
1044 ret
= nand_register(devnum
);
1051 void board_nand_init(void)
1053 struct nand_chip
*nand
= &nand_chip
[0];
1055 if (tegra_nand_init(nand
, 0))
1056 puts("Tegra NAND init failed\n");