blob: 09e053541a9d48ed484a1a64681dd869138ced9d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Schwarz4f62e982011-09-14 15:30:16 -04002/*
3 * (C) Copyright 2006-2008
4 * Stefan Roese, DENX Software Engineering, sr@denx.de.
Simon Schwarz4f62e982011-09-14 15:30:16 -04005 */
6
7#include <common.h>
8#include <nand.h>
9#include <asm/io.h>
Ilya Yanok4e699622011-11-28 06:37:37 +000010#include <linux/mtd/nand_ecc.h>
Simon Schwarz4f62e982011-09-14 15:30:16 -040011
12static int nand_ecc_pos[] = CONFIG_SYS_NAND_ECCPOS;
Scott Wood2c1b7e12016-05-30 13:57:55 -050013static struct mtd_info *mtd;
Simon Schwarz4f62e982011-09-14 15:30:16 -040014static struct nand_chip nand_chip;
15
Stefano Babic533607c2011-12-15 10:55:37 +010016#define ECCSTEPS (CONFIG_SYS_NAND_PAGE_SIZE / \
17 CONFIG_SYS_NAND_ECCSIZE)
18#define ECCTOTAL (ECCSTEPS * CONFIG_SYS_NAND_ECCBYTES)
19
20
Simon Schwarz4f62e982011-09-14 15:30:16 -040021#if (CONFIG_SYS_NAND_PAGE_SIZE <= 512)
22/*
23 * NAND command for small page NAND devices (512)
24 */
25static int nand_command(int block, int page, uint32_t offs,
26 u8 cmd)
27{
Scott Wood17fed142016-05-30 13:57:56 -050028 struct nand_chip *this = mtd_to_nand(mtd);
Simon Schwarz4f62e982011-09-14 15:30:16 -040029 int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
30
Scott Wood2c1b7e12016-05-30 13:57:55 -050031 while (!this->dev_ready(mtd))
Simon Schwarz4f62e982011-09-14 15:30:16 -040032 ;
33
34 /* Begin command latch cycle */
Scott Wood2c1b7e12016-05-30 13:57:55 -050035 this->cmd_ctrl(mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Simon Schwarz4f62e982011-09-14 15:30:16 -040036 /* Set ALE and clear CLE to start address cycle */
37 /* Column address */
Scott Wood2c1b7e12016-05-30 13:57:55 -050038 this->cmd_ctrl(mtd, offs, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
39 this->cmd_ctrl(mtd, page_addr & 0xff, NAND_CTRL_ALE); /* A[16:9] */
40 this->cmd_ctrl(mtd, (page_addr >> 8) & 0xff,
Simon Schwarz4f62e982011-09-14 15:30:16 -040041 NAND_CTRL_ALE); /* A[24:17] */
42#ifdef CONFIG_SYS_NAND_4_ADDR_CYCLE
43 /* One more address cycle for devices > 32MiB */
Scott Wood2c1b7e12016-05-30 13:57:55 -050044 this->cmd_ctrl(mtd, (page_addr >> 16) & 0x0f,
Simon Schwarz4f62e982011-09-14 15:30:16 -040045 NAND_CTRL_ALE); /* A[28:25] */
46#endif
47 /* Latch in address */
Scott Wood2c1b7e12016-05-30 13:57:55 -050048 this->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Simon Schwarz4f62e982011-09-14 15:30:16 -040049
50 /*
51 * Wait a while for the data to be ready
52 */
Scott Wood2c1b7e12016-05-30 13:57:55 -050053 while (!this->dev_ready(mtd))
Simon Schwarz4f62e982011-09-14 15:30:16 -040054 ;
55
56 return 0;
57}
58#else
59/*
60 * NAND command for large page NAND devices (2k)
61 */
62static int nand_command(int block, int page, uint32_t offs,
63 u8 cmd)
64{
Scott Wood17fed142016-05-30 13:57:56 -050065 struct nand_chip *this = mtd_to_nand(mtd);
Simon Schwarz4f62e982011-09-14 15:30:16 -040066 int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
67 void (*hwctrl)(struct mtd_info *mtd, int cmd,
68 unsigned int ctrl) = this->cmd_ctrl;
69
Scott Wood2c1b7e12016-05-30 13:57:55 -050070 while (!this->dev_ready(mtd))
Simon Schwarz4f62e982011-09-14 15:30:16 -040071 ;
72
73 /* Emulate NAND_CMD_READOOB */
74 if (cmd == NAND_CMD_READOOB) {
75 offs += CONFIG_SYS_NAND_PAGE_SIZE;
76 cmd = NAND_CMD_READ0;
77 }
78
79 /* Shift the offset from byte addressing to word addressing. */
Brian Norris67675222014-05-06 00:46:17 +053080 if ((this->options & NAND_BUSWIDTH_16) && !nand_opcode_8bits(cmd))
Simon Schwarz4f62e982011-09-14 15:30:16 -040081 offs >>= 1;
82
83 /* Begin command latch cycle */
Scott Wood2c1b7e12016-05-30 13:57:55 -050084 hwctrl(mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Simon Schwarz4f62e982011-09-14 15:30:16 -040085 /* Set ALE and clear CLE to start address cycle */
86 /* Column address */
Scott Wood2c1b7e12016-05-30 13:57:55 -050087 hwctrl(mtd, offs & 0xff,
88 NAND_CTRL_ALE | NAND_CTRL_CHANGE); /* A[7:0] */
89 hwctrl(mtd, (offs >> 8) & 0xff, NAND_CTRL_ALE); /* A[11:9] */
Simon Schwarz4f62e982011-09-14 15:30:16 -040090 /* Row address */
Scott Wood2c1b7e12016-05-30 13:57:55 -050091 hwctrl(mtd, (page_addr & 0xff), NAND_CTRL_ALE); /* A[19:12] */
92 hwctrl(mtd, ((page_addr >> 8) & 0xff),
93 NAND_CTRL_ALE); /* A[27:20] */
Simon Schwarz4f62e982011-09-14 15:30:16 -040094#ifdef CONFIG_SYS_NAND_5_ADDR_CYCLE
95 /* One more address cycle for devices > 128MiB */
Scott Wood2c1b7e12016-05-30 13:57:55 -050096 hwctrl(mtd, (page_addr >> 16) & 0x0f,
Simon Schwarz4f62e982011-09-14 15:30:16 -040097 NAND_CTRL_ALE); /* A[31:28] */
98#endif
99 /* Latch in address */
Scott Wood2c1b7e12016-05-30 13:57:55 -0500100 hwctrl(mtd, NAND_CMD_READSTART,
101 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
102 hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Simon Schwarz4f62e982011-09-14 15:30:16 -0400103
104 /*
105 * Wait a while for the data to be ready
106 */
Scott Wood2c1b7e12016-05-30 13:57:55 -0500107 while (!this->dev_ready(mtd))
Simon Schwarz4f62e982011-09-14 15:30:16 -0400108 ;
109
110 return 0;
111}
112#endif
113
114static int nand_is_bad_block(int block)
115{
Scott Wood17fed142016-05-30 13:57:56 -0500116 struct nand_chip *this = mtd_to_nand(mtd);
Vladimir Zapolskiycb8183d2015-07-18 01:47:08 +0300117 u_char bb_data[2];
Simon Schwarz4f62e982011-09-14 15:30:16 -0400118
119 nand_command(block, 0, CONFIG_SYS_NAND_BAD_BLOCK_POS,
120 NAND_CMD_READOOB);
121
122 /*
123 * Read one byte (or two if it's a 16 bit chip).
124 */
125 if (this->options & NAND_BUSWIDTH_16) {
Scott Wood2c1b7e12016-05-30 13:57:55 -0500126 this->read_buf(mtd, bb_data, 2);
Vladimir Zapolskiycb8183d2015-07-18 01:47:08 +0300127 if (bb_data[0] != 0xff || bb_data[1] != 0xff)
Simon Schwarz4f62e982011-09-14 15:30:16 -0400128 return 1;
129 } else {
Scott Wood2c1b7e12016-05-30 13:57:55 -0500130 this->read_buf(mtd, bb_data, 1);
Vladimir Zapolskiycb8183d2015-07-18 01:47:08 +0300131 if (bb_data[0] != 0xff)
Simon Schwarz4f62e982011-09-14 15:30:16 -0400132 return 1;
133 }
134
135 return 0;
136}
137
Heiko Schocher44bd9b32011-11-01 20:00:30 +0000138#if defined(CONFIG_SYS_NAND_HW_ECC_OOBFIRST)
139static int nand_read_page(int block, int page, uchar *dst)
140{
Scott Wood17fed142016-05-30 13:57:56 -0500141 struct nand_chip *this = mtd_to_nand(mtd);
Stefano Babic533607c2011-12-15 10:55:37 +0100142 u_char ecc_calc[ECCTOTAL];
143 u_char ecc_code[ECCTOTAL];
144 u_char oob_data[CONFIG_SYS_NAND_OOBSIZE];
Heiko Schocher44bd9b32011-11-01 20:00:30 +0000145 int i;
146 int eccsize = CONFIG_SYS_NAND_ECCSIZE;
147 int eccbytes = CONFIG_SYS_NAND_ECCBYTES;
Stefano Babic533607c2011-12-15 10:55:37 +0100148 int eccsteps = ECCSTEPS;
Heiko Schocher44bd9b32011-11-01 20:00:30 +0000149 uint8_t *p = dst;
Heiko Schocher44bd9b32011-11-01 20:00:30 +0000150
Heiko Schocher44bd9b32011-11-01 20:00:30 +0000151 nand_command(block, page, 0, NAND_CMD_READOOB);
Scott Wood2c1b7e12016-05-30 13:57:55 -0500152 this->read_buf(mtd, oob_data, CONFIG_SYS_NAND_OOBSIZE);
Heiko Schocher44bd9b32011-11-01 20:00:30 +0000153 nand_command(block, page, 0, NAND_CMD_READ0);
154
155 /* Pick the ECC bytes out of the oob data */
Stefano Babic533607c2011-12-15 10:55:37 +0100156 for (i = 0; i < ECCTOTAL; i++)
Heiko Schocher44bd9b32011-11-01 20:00:30 +0000157 ecc_code[i] = oob_data[nand_ecc_pos[i]];
158
159
160 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Scott Wood2c1b7e12016-05-30 13:57:55 -0500161 this->ecc.hwctl(mtd, NAND_ECC_READ);
162 this->read_buf(mtd, p, eccsize);
163 this->ecc.calculate(mtd, p, &ecc_calc[i]);
164 this->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Heiko Schocher44bd9b32011-11-01 20:00:30 +0000165 }
166
167 return 0;
168}
169#else
Simon Schwarz4f62e982011-09-14 15:30:16 -0400170static int nand_read_page(int block, int page, void *dst)
171{
Scott Wood17fed142016-05-30 13:57:56 -0500172 struct nand_chip *this = mtd_to_nand(mtd);
Stefano Babic533607c2011-12-15 10:55:37 +0100173 u_char ecc_calc[ECCTOTAL];
174 u_char ecc_code[ECCTOTAL];
175 u_char oob_data[CONFIG_SYS_NAND_OOBSIZE];
Simon Schwarz4f62e982011-09-14 15:30:16 -0400176 int i;
177 int eccsize = CONFIG_SYS_NAND_ECCSIZE;
178 int eccbytes = CONFIG_SYS_NAND_ECCBYTES;
Stefano Babic533607c2011-12-15 10:55:37 +0100179 int eccsteps = ECCSTEPS;
Simon Schwarz4f62e982011-09-14 15:30:16 -0400180 uint8_t *p = dst;
Simon Schwarz4f62e982011-09-14 15:30:16 -0400181
182 nand_command(block, page, 0, NAND_CMD_READ0);
183
Simon Schwarz4f62e982011-09-14 15:30:16 -0400184 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Ilya Yanok4e699622011-11-28 06:37:37 +0000185 if (this->ecc.mode != NAND_ECC_SOFT)
Scott Wood2c1b7e12016-05-30 13:57:55 -0500186 this->ecc.hwctl(mtd, NAND_ECC_READ);
187 this->read_buf(mtd, p, eccsize);
188 this->ecc.calculate(mtd, p, &ecc_calc[i]);
Simon Schwarz4f62e982011-09-14 15:30:16 -0400189 }
Scott Wood2c1b7e12016-05-30 13:57:55 -0500190 this->read_buf(mtd, oob_data, CONFIG_SYS_NAND_OOBSIZE);
Simon Schwarz4f62e982011-09-14 15:30:16 -0400191
192 /* Pick the ECC bytes out of the oob data */
Stefano Babic533607c2011-12-15 10:55:37 +0100193 for (i = 0; i < ECCTOTAL; i++)
Simon Schwarz4f62e982011-09-14 15:30:16 -0400194 ecc_code[i] = oob_data[nand_ecc_pos[i]];
195
Stefano Babic533607c2011-12-15 10:55:37 +0100196 eccsteps = ECCSTEPS;
Simon Schwarz4f62e982011-09-14 15:30:16 -0400197 p = dst;
198
199 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
200 /* No chance to do something with the possible error message
201 * from correct_data(). We just hope that all possible errors
202 * are corrected by this routine.
203 */
Scott Wood2c1b7e12016-05-30 13:57:55 -0500204 this->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Simon Schwarz4f62e982011-09-14 15:30:16 -0400205 }
206
207 return 0;
208}
Heiko Schocher44bd9b32011-11-01 20:00:30 +0000209#endif
Simon Schwarz4f62e982011-09-14 15:30:16 -0400210
Simon Schwarz4f62e982011-09-14 15:30:16 -0400211/* nand_init() - initialize data to make nand usable by SPL */
212void nand_init(void)
213{
214 /*
215 * Init board specific nand support
216 */
Boris Brezillon3b5f8842016-06-15 20:56:10 +0200217 mtd = nand_to_mtd(&nand_chip);
Simon Schwarz4f62e982011-09-14 15:30:16 -0400218 nand_chip.IO_ADDR_R = nand_chip.IO_ADDR_W =
219 (void __iomem *)CONFIG_SYS_NAND_BASE;
Simon Schwarz4f62e982011-09-14 15:30:16 -0400220 board_nand_init(&nand_chip);
221
Ilya Yanok4e699622011-11-28 06:37:37 +0000222#ifdef CONFIG_SPL_NAND_SOFTECC
223 if (nand_chip.ecc.mode == NAND_ECC_SOFT) {
224 nand_chip.ecc.calculate = nand_calculate_ecc;
225 nand_chip.ecc.correct = nand_correct_data;
226 }
227#endif
228
Simon Schwarz4f62e982011-09-14 15:30:16 -0400229 if (nand_chip.select_chip)
Scott Wood2c1b7e12016-05-30 13:57:55 -0500230 nand_chip.select_chip(mtd, 0);
Simon Schwarz4f62e982011-09-14 15:30:16 -0400231}
232
233/* Unselect after operation */
234void nand_deselect(void)
235{
236 if (nand_chip.select_chip)
Scott Wood2c1b7e12016-05-30 13:57:55 -0500237 nand_chip.select_chip(mtd, -1);
Simon Schwarz4f62e982011-09-14 15:30:16 -0400238}
Ladislav Michlc6a42002017-04-16 15:31:59 +0200239
240#include "nand_spl_loaders.c"