blob: 125e7f3714cd8fe655b50b44d2ef9bb5722166d0 [file] [log] [blame]
Stefan Roese887e2ec2006-09-07 11:51:23 +02001/*
Stefan Roese46f37382008-04-08 10:31:00 +02002 * (C) Copyright 2006-2008
Stefan Roese887e2ec2006-09-07 11:51:23 +02003 * Stefan Roese, DENX Software Engineering, sr@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Stefan Roese887e2ec2006-09-07 11:51:23 +02006 */
7
8#include <common.h>
9#include <nand.h>
Stefan Roesec568f772008-01-05 16:49:37 +010010#include <asm/io.h>
Stefan Roese887e2ec2006-09-07 11:51:23 +020011
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020012static int nand_ecc_pos[] = CONFIG_SYS_NAND_ECCPOS;
Stefan Roese887e2ec2006-09-07 11:51:23 +020013
Scott Wood25efd992012-01-11 15:41:01 -060014#define ECCSTEPS (CONFIG_SYS_NAND_PAGE_SIZE / \
15 CONFIG_SYS_NAND_ECCSIZE)
16#define ECCTOTAL (ECCSTEPS * CONFIG_SYS_NAND_ECCBYTES)
17
18
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020019#if (CONFIG_SYS_NAND_PAGE_SIZE <= 512)
Stefan Roese46f37382008-04-08 10:31:00 +020020/*
21 * NAND command for small page NAND devices (512)
22 */
Stefan Roese42be56f2007-06-01 15:23:04 +020023static int nand_command(struct mtd_info *mtd, int block, int page, int offs, u8 cmd)
Stefan Roese887e2ec2006-09-07 11:51:23 +020024{
Wolfgang Denk511d0c72006-10-09 00:42:01 +020025 struct nand_chip *this = mtd->priv;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020026 int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
Stefan Roese42be56f2007-06-01 15:23:04 +020027
Stefan Roesee29816f2011-05-16 13:04:00 +020028 while (!this->dev_ready(mtd))
29 ;
Stefan Roese887e2ec2006-09-07 11:51:23 +020030
31 /* Begin command latch cycle */
Scott Wood4f32d772008-08-05 11:15:59 -050032 this->cmd_ctrl(mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Stefan Roese887e2ec2006-09-07 11:51:23 +020033 /* Set ALE and clear CLE to start address cycle */
Stefan Roese887e2ec2006-09-07 11:51:23 +020034 /* Column address */
Scott Wood4f32d772008-08-05 11:15:59 -050035 this->cmd_ctrl(mtd, offs, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
Scott Wood1dac3a52009-06-24 17:23:49 -050036 this->cmd_ctrl(mtd, page_addr & 0xff, NAND_CTRL_ALE); /* A[16:9] */
37 this->cmd_ctrl(mtd, (page_addr >> 8) & 0xff,
38 NAND_CTRL_ALE); /* A[24:17] */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020039#ifdef CONFIG_SYS_NAND_4_ADDR_CYCLE
Stefan Roese887e2ec2006-09-07 11:51:23 +020040 /* One more address cycle for devices > 32MiB */
Scott Wood1dac3a52009-06-24 17:23:49 -050041 this->cmd_ctrl(mtd, (page_addr >> 16) & 0x0f,
42 NAND_CTRL_ALE); /* A[28:25] */
Stefan Roese887e2ec2006-09-07 11:51:23 +020043#endif
44 /* Latch in address */
Stefan Roesec568f772008-01-05 16:49:37 +010045 this->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Stefan Roese887e2ec2006-09-07 11:51:23 +020046
47 /*
48 * Wait a while for the data to be ready
49 */
Stefan Roesea9c847c2011-05-04 11:44:14 +020050 while (!this->dev_ready(mtd))
51 ;
Stefan Roese887e2ec2006-09-07 11:51:23 +020052
Stefan Roese42be56f2007-06-01 15:23:04 +020053 return 0;
54}
Stefan Roese46f37382008-04-08 10:31:00 +020055#else
56/*
57 * NAND command for large page NAND devices (2k)
58 */
59static int nand_command(struct mtd_info *mtd, int block, int page, int offs, u8 cmd)
60{
61 struct nand_chip *this = mtd->priv;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020062 int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
Alex Waterman837097832011-05-04 09:10:15 -040063 void (*hwctrl)(struct mtd_info *mtd, int cmd,
64 unsigned int ctrl) = this->cmd_ctrl;
Stefan Roese46f37382008-04-08 10:31:00 +020065
Stefan Roesea9c847c2011-05-04 11:44:14 +020066 while (!this->dev_ready(mtd))
67 ;
Stefan Roese46f37382008-04-08 10:31:00 +020068
69 /* Emulate NAND_CMD_READOOB */
70 if (cmd == NAND_CMD_READOOB) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020071 offs += CONFIG_SYS_NAND_PAGE_SIZE;
Stefan Roese46f37382008-04-08 10:31:00 +020072 cmd = NAND_CMD_READ0;
73 }
74
Alex Waterman65a9db72011-04-06 16:01:52 -040075 /* Shift the offset from byte addressing to word addressing. */
76 if (this->options & NAND_BUSWIDTH_16)
77 offs >>= 1;
78
Stefan Roese46f37382008-04-08 10:31:00 +020079 /* Begin command latch cycle */
Alex Waterman837097832011-05-04 09:10:15 -040080 hwctrl(mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Stefan Roese46f37382008-04-08 10:31:00 +020081 /* Set ALE and clear CLE to start address cycle */
Stefan Roese46f37382008-04-08 10:31:00 +020082 /* Column address */
Alex Waterman837097832011-05-04 09:10:15 -040083 hwctrl(mtd, offs & 0xff,
Wolfgang Denk4b070802008-08-14 14:41:06 +020084 NAND_CTRL_ALE | NAND_CTRL_CHANGE); /* A[7:0] */
Alex Waterman837097832011-05-04 09:10:15 -040085 hwctrl(mtd, (offs >> 8) & 0xff, NAND_CTRL_ALE); /* A[11:9] */
Stefan Roese46f37382008-04-08 10:31:00 +020086 /* Row address */
Alex Waterman837097832011-05-04 09:10:15 -040087 hwctrl(mtd, (page_addr & 0xff), NAND_CTRL_ALE); /* A[19:12] */
88 hwctrl(mtd, ((page_addr >> 8) & 0xff),
Scott Wood1dac3a52009-06-24 17:23:49 -050089 NAND_CTRL_ALE); /* A[27:20] */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020090#ifdef CONFIG_SYS_NAND_5_ADDR_CYCLE
Stefan Roese46f37382008-04-08 10:31:00 +020091 /* One more address cycle for devices > 128MiB */
Alex Waterman837097832011-05-04 09:10:15 -040092 hwctrl(mtd, (page_addr >> 16) & 0x0f,
Scott Wood1dac3a52009-06-24 17:23:49 -050093 NAND_CTRL_ALE); /* A[31:28] */
Stefan Roese46f37382008-04-08 10:31:00 +020094#endif
95 /* Latch in address */
Alex Waterman837097832011-05-04 09:10:15 -040096 hwctrl(mtd, NAND_CMD_READSTART,
Wolfgang Denk4b070802008-08-14 14:41:06 +020097 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Alex Waterman837097832011-05-04 09:10:15 -040098 hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Stefan Roese46f37382008-04-08 10:31:00 +020099
100 /*
101 * Wait a while for the data to be ready
102 */
Stefan Roesea9c847c2011-05-04 11:44:14 +0200103 while (!this->dev_ready(mtd))
104 ;
Stefan Roese46f37382008-04-08 10:31:00 +0200105
106 return 0;
107}
108#endif
Stefan Roese42be56f2007-06-01 15:23:04 +0200109
110static int nand_is_bad_block(struct mtd_info *mtd, int block)
111{
112 struct nand_chip *this = mtd->priv;
113
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200114 nand_command(mtd, block, 0, CONFIG_SYS_NAND_BAD_BLOCK_POS, NAND_CMD_READOOB);
Stefan Roese42be56f2007-06-01 15:23:04 +0200115
Stefan Roese887e2ec2006-09-07 11:51:23 +0200116 /*
Alex Watermaneced4622011-05-19 15:08:36 -0400117 * Read one byte (or two if it's a 16 bit chip).
Stefan Roese887e2ec2006-09-07 11:51:23 +0200118 */
Alex Watermaneced4622011-05-19 15:08:36 -0400119 if (this->options & NAND_BUSWIDTH_16) {
120 if (readw(this->IO_ADDR_R) != 0xffff)
121 return 1;
122 } else {
123 if (readb(this->IO_ADDR_R) != 0xff)
124 return 1;
125 }
Stefan Roese887e2ec2006-09-07 11:51:23 +0200126
127 return 0;
128}
129
Heiko Schocherdc7cd8e2011-07-16 00:06:49 +0000130#if defined(CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST)
131static int nand_read_page(struct mtd_info *mtd, int block, int page, uchar *dst)
132{
133 struct nand_chip *this = mtd->priv;
Scott Wood25efd992012-01-11 15:41:01 -0600134 u_char ecc_calc[ECCTOTAL];
135 u_char ecc_code[ECCTOTAL];
136 u_char oob_data[CONFIG_SYS_NAND_OOBSIZE];
Heiko Schocherdc7cd8e2011-07-16 00:06:49 +0000137 int i;
138 int eccsize = CONFIG_SYS_NAND_ECCSIZE;
139 int eccbytes = CONFIG_SYS_NAND_ECCBYTES;
Scott Wood25efd992012-01-11 15:41:01 -0600140 int eccsteps = ECCSTEPS;
Heiko Schocherdc7cd8e2011-07-16 00:06:49 +0000141 uint8_t *p = dst;
Heiko Schocherdc7cd8e2011-07-16 00:06:49 +0000142
Heiko Schocherdc7cd8e2011-07-16 00:06:49 +0000143 nand_command(mtd, block, page, 0, NAND_CMD_READOOB);
144 this->read_buf(mtd, oob_data, CONFIG_SYS_NAND_OOBSIZE);
145 nand_command(mtd, block, page, 0, NAND_CMD_READ0);
146
147 /* Pick the ECC bytes out of the oob data */
Scott Wood25efd992012-01-11 15:41:01 -0600148 for (i = 0; i < ECCTOTAL; i++)
Heiko Schocherdc7cd8e2011-07-16 00:06:49 +0000149 ecc_code[i] = oob_data[nand_ecc_pos[i]];
150
151
152 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
153 this->ecc.hwctl(mtd, NAND_ECC_READ);
154 this->read_buf(mtd, p, eccsize);
155 this->ecc.calculate(mtd, p, &ecc_calc[i]);
Anatolij Gustschin40a06822011-12-07 11:58:56 +0000156 this->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Heiko Schocherdc7cd8e2011-07-16 00:06:49 +0000157 }
158
159 return 0;
160}
161#else
Stefan Roese887e2ec2006-09-07 11:51:23 +0200162static int nand_read_page(struct mtd_info *mtd, int block, int page, uchar *dst)
163{
Wolfgang Denk511d0c72006-10-09 00:42:01 +0200164 struct nand_chip *this = mtd->priv;
Scott Wood25efd992012-01-11 15:41:01 -0600165 u_char ecc_calc[ECCTOTAL];
166 u_char ecc_code[ECCTOTAL];
167 u_char oob_data[CONFIG_SYS_NAND_OOBSIZE];
Stefan Roese887e2ec2006-09-07 11:51:23 +0200168 int i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200169 int eccsize = CONFIG_SYS_NAND_ECCSIZE;
170 int eccbytes = CONFIG_SYS_NAND_ECCBYTES;
Scott Wood25efd992012-01-11 15:41:01 -0600171 int eccsteps = ECCSTEPS;
Stefan Roese42be56f2007-06-01 15:23:04 +0200172 uint8_t *p = dst;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200173
Stefan Roese42be56f2007-06-01 15:23:04 +0200174 nand_command(mtd, block, page, 0, NAND_CMD_READ0);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200175
Stefan Roese42be56f2007-06-01 15:23:04 +0200176 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Stefan Roesec568f772008-01-05 16:49:37 +0100177 this->ecc.hwctl(mtd, NAND_ECC_READ);
Stefan Roese42be56f2007-06-01 15:23:04 +0200178 this->read_buf(mtd, p, eccsize);
Stefan Roesec568f772008-01-05 16:49:37 +0100179 this->ecc.calculate(mtd, p, &ecc_calc[i]);
Stefan Roese42be56f2007-06-01 15:23:04 +0200180 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200181 this->read_buf(mtd, oob_data, CONFIG_SYS_NAND_OOBSIZE);
Stefan Roese42be56f2007-06-01 15:23:04 +0200182
183 /* Pick the ECC bytes out of the oob data */
Scott Wood25efd992012-01-11 15:41:01 -0600184 for (i = 0; i < ECCTOTAL; i++)
Stefan Roese42be56f2007-06-01 15:23:04 +0200185 ecc_code[i] = oob_data[nand_ecc_pos[i]];
186
Scott Wood25efd992012-01-11 15:41:01 -0600187 eccsteps = ECCSTEPS;
Stefan Roese42be56f2007-06-01 15:23:04 +0200188 p = dst;
189
190 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
191 /* No chance to do something with the possible error message
192 * from correct_data(). We just hope that all possible errors
193 * are corrected by this routine.
194 */
Stefan Roese6d686212011-11-15 08:02:54 +0000195 this->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Stefan Roese42be56f2007-06-01 15:23:04 +0200196 }
Stefan Roese887e2ec2006-09-07 11:51:23 +0200197
198 return 0;
199}
Heiko Schocherdc7cd8e2011-07-16 00:06:49 +0000200#endif /* #if defined(CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST) */
Stefan Roese887e2ec2006-09-07 11:51:23 +0200201
Guennadi Liakhovetskiaa646642008-08-06 21:42:07 +0200202static int nand_load(struct mtd_info *mtd, unsigned int offs,
Wolfgang Denk4b070802008-08-14 14:41:06 +0200203 unsigned int uboot_size, uchar *dst)
Stefan Roese887e2ec2006-09-07 11:51:23 +0200204{
Guennadi Liakhovetskiaa646642008-08-06 21:42:07 +0200205 unsigned int block, lastblock;
206 unsigned int page;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200207
208 /*
Guennadi Liakhovetskiaa646642008-08-06 21:42:07 +0200209 * offs has to be aligned to a page address!
Stefan Roese887e2ec2006-09-07 11:51:23 +0200210 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200211 block = offs / CONFIG_SYS_NAND_BLOCK_SIZE;
212 lastblock = (offs + uboot_size - 1) / CONFIG_SYS_NAND_BLOCK_SIZE;
213 page = (offs % CONFIG_SYS_NAND_BLOCK_SIZE) / CONFIG_SYS_NAND_PAGE_SIZE;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200214
Guennadi Liakhovetskiaa646642008-08-06 21:42:07 +0200215 while (block <= lastblock) {
Stefan Roese887e2ec2006-09-07 11:51:23 +0200216 if (!nand_is_bad_block(mtd, block)) {
217 /*
218 * Skip bad blocks
219 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200220 while (page < CONFIG_SYS_NAND_PAGE_COUNT) {
Stefan Roese887e2ec2006-09-07 11:51:23 +0200221 nand_read_page(mtd, block, page, dst);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200222 dst += CONFIG_SYS_NAND_PAGE_SIZE;
Guennadi Liakhovetskiaa646642008-08-06 21:42:07 +0200223 page++;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200224 }
225
Guennadi Liakhovetskiaa646642008-08-06 21:42:07 +0200226 page = 0;
227 } else {
228 lastblock++;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200229 }
230
231 block++;
232 }
233
234 return 0;
235}
236
Stefan Roese64852d02008-06-02 14:35:44 +0200237/*
238 * The main entry for NAND booting. It's necessary that SDRAM is already
239 * configured and available since this code loads the main U-Boot image
240 * from NAND into SDRAM and starts it from there.
241 */
Stefan Roese887e2ec2006-09-07 11:51:23 +0200242void nand_boot(void)
243{
Stefan Roese887e2ec2006-09-07 11:51:23 +0200244 struct nand_chip nand_chip;
245 nand_info_t nand_info;
Scott Woode4c09502008-06-30 14:13:28 -0500246 __attribute__((noreturn)) void (*uboot)(void);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200247
248 /*
Stefan Roese887e2ec2006-09-07 11:51:23 +0200249 * Init board specific nand support
250 */
Sughosh Ganu48571ff2010-11-30 11:25:01 -0500251 nand_chip.select_chip = NULL;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200252 nand_info.priv = &nand_chip;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200253 nand_chip.IO_ADDR_R = nand_chip.IO_ADDR_W = (void __iomem *)CONFIG_SYS_NAND_BASE;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200254 nand_chip.dev_ready = NULL; /* preset to NULL */
Stefan Roesea89a9902011-05-04 11:44:44 +0200255 nand_chip.options = 0;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200256 board_nand_init(&nand_chip);
257
Guennadi Liakhovetskiaa646642008-08-06 21:42:07 +0200258 if (nand_chip.select_chip)
259 nand_chip.select_chip(&nand_info, 0);
260
Stefan Roese887e2ec2006-09-07 11:51:23 +0200261 /*
262 * Load U-Boot image from NAND into RAM
263 */
Stefan Roese6d686212011-11-15 08:02:54 +0000264 nand_load(&nand_info, CONFIG_SYS_NAND_U_BOOT_OFFS, CONFIG_SYS_NAND_U_BOOT_SIZE,
265 (uchar *)CONFIG_SYS_NAND_U_BOOT_DST);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200266
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200267#ifdef CONFIG_NAND_ENV_DST
268 nand_load(&nand_info, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
269 (uchar *)CONFIG_NAND_ENV_DST);
270
271#ifdef CONFIG_ENV_OFFSET_REDUND
272 nand_load(&nand_info, CONFIG_ENV_OFFSET_REDUND, CONFIG_ENV_SIZE,
273 (uchar *)CONFIG_NAND_ENV_DST + CONFIG_ENV_SIZE);
274#endif
275#endif
276
Guennadi Liakhovetskiaa646642008-08-06 21:42:07 +0200277 if (nand_chip.select_chip)
278 nand_chip.select_chip(&nand_info, -1);
279
Stefan Roese887e2ec2006-09-07 11:51:23 +0200280 /*
281 * Jump to U-Boot image
282 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200283 uboot = (void *)CONFIG_SYS_NAND_U_BOOT_START;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200284 (*uboot)();
285}