blob: ede7dbf15b9032f797c90b0f555cb331573de9c8 [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] */
Simon Schwarz4f62e982011-09-14 15:30:16 -040042 /* Latch in address */
Scott Wood2c1b7e12016-05-30 13:57:55 -050043 this->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Simon Schwarz4f62e982011-09-14 15:30:16 -040044
45 /*
46 * Wait a while for the data to be ready
47 */
Scott Wood2c1b7e12016-05-30 13:57:55 -050048 while (!this->dev_ready(mtd))
Simon Schwarz4f62e982011-09-14 15:30:16 -040049 ;
50
51 return 0;
52}
53#else
54/*
55 * NAND command for large page NAND devices (2k)
56 */
57static int nand_command(int block, int page, uint32_t offs,
58 u8 cmd)
59{
Scott Wood17fed142016-05-30 13:57:56 -050060 struct nand_chip *this = mtd_to_nand(mtd);
Simon Schwarz4f62e982011-09-14 15:30:16 -040061 int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
62 void (*hwctrl)(struct mtd_info *mtd, int cmd,
63 unsigned int ctrl) = this->cmd_ctrl;
64
Scott Wood2c1b7e12016-05-30 13:57:55 -050065 while (!this->dev_ready(mtd))
Simon Schwarz4f62e982011-09-14 15:30:16 -040066 ;
67
68 /* Emulate NAND_CMD_READOOB */
69 if (cmd == NAND_CMD_READOOB) {
70 offs += CONFIG_SYS_NAND_PAGE_SIZE;
71 cmd = NAND_CMD_READ0;
72 }
73
74 /* Shift the offset from byte addressing to word addressing. */
Brian Norris67675222014-05-06 00:46:17 +053075 if ((this->options & NAND_BUSWIDTH_16) && !nand_opcode_8bits(cmd))
Simon Schwarz4f62e982011-09-14 15:30:16 -040076 offs >>= 1;
77
78 /* Begin command latch cycle */
Scott Wood2c1b7e12016-05-30 13:57:55 -050079 hwctrl(mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Simon Schwarz4f62e982011-09-14 15:30:16 -040080 /* Set ALE and clear CLE to start address cycle */
81 /* Column address */
Scott Wood2c1b7e12016-05-30 13:57:55 -050082 hwctrl(mtd, offs & 0xff,
83 NAND_CTRL_ALE | NAND_CTRL_CHANGE); /* A[7:0] */
84 hwctrl(mtd, (offs >> 8) & 0xff, NAND_CTRL_ALE); /* A[11:9] */
Simon Schwarz4f62e982011-09-14 15:30:16 -040085 /* Row address */
Scott Wood2c1b7e12016-05-30 13:57:55 -050086 hwctrl(mtd, (page_addr & 0xff), NAND_CTRL_ALE); /* A[19:12] */
87 hwctrl(mtd, ((page_addr >> 8) & 0xff),
88 NAND_CTRL_ALE); /* A[27:20] */
Simon Schwarz4f62e982011-09-14 15:30:16 -040089#ifdef CONFIG_SYS_NAND_5_ADDR_CYCLE
90 /* One more address cycle for devices > 128MiB */
Scott Wood2c1b7e12016-05-30 13:57:55 -050091 hwctrl(mtd, (page_addr >> 16) & 0x0f,
Simon Schwarz4f62e982011-09-14 15:30:16 -040092 NAND_CTRL_ALE); /* A[31:28] */
93#endif
94 /* Latch in address */
Scott Wood2c1b7e12016-05-30 13:57:55 -050095 hwctrl(mtd, NAND_CMD_READSTART,
96 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
97 hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Simon Schwarz4f62e982011-09-14 15:30:16 -040098
99 /*
100 * Wait a while for the data to be ready
101 */
Scott Wood2c1b7e12016-05-30 13:57:55 -0500102 while (!this->dev_ready(mtd))
Simon Schwarz4f62e982011-09-14 15:30:16 -0400103 ;
104
105 return 0;
106}
107#endif
108
109static int nand_is_bad_block(int block)
110{
Scott Wood17fed142016-05-30 13:57:56 -0500111 struct nand_chip *this = mtd_to_nand(mtd);
Vladimir Zapolskiycb8183d2015-07-18 01:47:08 +0300112 u_char bb_data[2];
Simon Schwarz4f62e982011-09-14 15:30:16 -0400113
114 nand_command(block, 0, CONFIG_SYS_NAND_BAD_BLOCK_POS,
115 NAND_CMD_READOOB);
116
117 /*
118 * Read one byte (or two if it's a 16 bit chip).
119 */
120 if (this->options & NAND_BUSWIDTH_16) {
Scott Wood2c1b7e12016-05-30 13:57:55 -0500121 this->read_buf(mtd, bb_data, 2);
Vladimir Zapolskiycb8183d2015-07-18 01:47:08 +0300122 if (bb_data[0] != 0xff || bb_data[1] != 0xff)
Simon Schwarz4f62e982011-09-14 15:30:16 -0400123 return 1;
124 } else {
Scott Wood2c1b7e12016-05-30 13:57:55 -0500125 this->read_buf(mtd, bb_data, 1);
Vladimir Zapolskiycb8183d2015-07-18 01:47:08 +0300126 if (bb_data[0] != 0xff)
Simon Schwarz4f62e982011-09-14 15:30:16 -0400127 return 1;
128 }
129
130 return 0;
131}
132
Heiko Schocher44bd9b32011-11-01 20:00:30 +0000133#if defined(CONFIG_SYS_NAND_HW_ECC_OOBFIRST)
134static int nand_read_page(int block, int page, uchar *dst)
135{
Scott Wood17fed142016-05-30 13:57:56 -0500136 struct nand_chip *this = mtd_to_nand(mtd);
Stefano Babic533607c2011-12-15 10:55:37 +0100137 u_char ecc_calc[ECCTOTAL];
138 u_char ecc_code[ECCTOTAL];
139 u_char oob_data[CONFIG_SYS_NAND_OOBSIZE];
Heiko Schocher44bd9b32011-11-01 20:00:30 +0000140 int i;
141 int eccsize = CONFIG_SYS_NAND_ECCSIZE;
142 int eccbytes = CONFIG_SYS_NAND_ECCBYTES;
Stefano Babic533607c2011-12-15 10:55:37 +0100143 int eccsteps = ECCSTEPS;
Heiko Schocher44bd9b32011-11-01 20:00:30 +0000144 uint8_t *p = dst;
Heiko Schocher44bd9b32011-11-01 20:00:30 +0000145
Heiko Schocher44bd9b32011-11-01 20:00:30 +0000146 nand_command(block, page, 0, NAND_CMD_READOOB);
Scott Wood2c1b7e12016-05-30 13:57:55 -0500147 this->read_buf(mtd, oob_data, CONFIG_SYS_NAND_OOBSIZE);
Heiko Schocher44bd9b32011-11-01 20:00:30 +0000148 nand_command(block, page, 0, NAND_CMD_READ0);
149
150 /* Pick the ECC bytes out of the oob data */
Stefano Babic533607c2011-12-15 10:55:37 +0100151 for (i = 0; i < ECCTOTAL; i++)
Heiko Schocher44bd9b32011-11-01 20:00:30 +0000152 ecc_code[i] = oob_data[nand_ecc_pos[i]];
153
154
155 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Scott Wood2c1b7e12016-05-30 13:57:55 -0500156 this->ecc.hwctl(mtd, NAND_ECC_READ);
157 this->read_buf(mtd, p, eccsize);
158 this->ecc.calculate(mtd, p, &ecc_calc[i]);
159 this->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Heiko Schocher44bd9b32011-11-01 20:00:30 +0000160 }
161
162 return 0;
163}
164#else
Simon Schwarz4f62e982011-09-14 15:30:16 -0400165static int nand_read_page(int block, int page, void *dst)
166{
Scott Wood17fed142016-05-30 13:57:56 -0500167 struct nand_chip *this = mtd_to_nand(mtd);
Stefano Babic533607c2011-12-15 10:55:37 +0100168 u_char ecc_calc[ECCTOTAL];
169 u_char ecc_code[ECCTOTAL];
170 u_char oob_data[CONFIG_SYS_NAND_OOBSIZE];
Simon Schwarz4f62e982011-09-14 15:30:16 -0400171 int i;
172 int eccsize = CONFIG_SYS_NAND_ECCSIZE;
173 int eccbytes = CONFIG_SYS_NAND_ECCBYTES;
Stefano Babic533607c2011-12-15 10:55:37 +0100174 int eccsteps = ECCSTEPS;
Simon Schwarz4f62e982011-09-14 15:30:16 -0400175 uint8_t *p = dst;
Simon Schwarz4f62e982011-09-14 15:30:16 -0400176
177 nand_command(block, page, 0, NAND_CMD_READ0);
178
Simon Schwarz4f62e982011-09-14 15:30:16 -0400179 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Ilya Yanok4e699622011-11-28 06:37:37 +0000180 if (this->ecc.mode != NAND_ECC_SOFT)
Scott Wood2c1b7e12016-05-30 13:57:55 -0500181 this->ecc.hwctl(mtd, NAND_ECC_READ);
182 this->read_buf(mtd, p, eccsize);
183 this->ecc.calculate(mtd, p, &ecc_calc[i]);
Simon Schwarz4f62e982011-09-14 15:30:16 -0400184 }
Scott Wood2c1b7e12016-05-30 13:57:55 -0500185 this->read_buf(mtd, oob_data, CONFIG_SYS_NAND_OOBSIZE);
Simon Schwarz4f62e982011-09-14 15:30:16 -0400186
187 /* Pick the ECC bytes out of the oob data */
Stefano Babic533607c2011-12-15 10:55:37 +0100188 for (i = 0; i < ECCTOTAL; i++)
Simon Schwarz4f62e982011-09-14 15:30:16 -0400189 ecc_code[i] = oob_data[nand_ecc_pos[i]];
190
Stefano Babic533607c2011-12-15 10:55:37 +0100191 eccsteps = ECCSTEPS;
Simon Schwarz4f62e982011-09-14 15:30:16 -0400192 p = dst;
193
194 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
195 /* No chance to do something with the possible error message
196 * from correct_data(). We just hope that all possible errors
197 * are corrected by this routine.
198 */
Scott Wood2c1b7e12016-05-30 13:57:55 -0500199 this->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Simon Schwarz4f62e982011-09-14 15:30:16 -0400200 }
201
202 return 0;
203}
Heiko Schocher44bd9b32011-11-01 20:00:30 +0000204#endif
Simon Schwarz4f62e982011-09-14 15:30:16 -0400205
Simon Schwarz4f62e982011-09-14 15:30:16 -0400206/* nand_init() - initialize data to make nand usable by SPL */
207void nand_init(void)
208{
209 /*
210 * Init board specific nand support
211 */
Boris Brezillon3b5f8842016-06-15 20:56:10 +0200212 mtd = nand_to_mtd(&nand_chip);
Simon Schwarz4f62e982011-09-14 15:30:16 -0400213 nand_chip.IO_ADDR_R = nand_chip.IO_ADDR_W =
214 (void __iomem *)CONFIG_SYS_NAND_BASE;
Simon Schwarz4f62e982011-09-14 15:30:16 -0400215 board_nand_init(&nand_chip);
216
Ilya Yanok4e699622011-11-28 06:37:37 +0000217#ifdef CONFIG_SPL_NAND_SOFTECC
218 if (nand_chip.ecc.mode == NAND_ECC_SOFT) {
219 nand_chip.ecc.calculate = nand_calculate_ecc;
220 nand_chip.ecc.correct = nand_correct_data;
221 }
222#endif
223
Simon Schwarz4f62e982011-09-14 15:30:16 -0400224 if (nand_chip.select_chip)
Scott Wood2c1b7e12016-05-30 13:57:55 -0500225 nand_chip.select_chip(mtd, 0);
Simon Schwarz4f62e982011-09-14 15:30:16 -0400226}
227
228/* Unselect after operation */
229void nand_deselect(void)
230{
231 if (nand_chip.select_chip)
Scott Wood2c1b7e12016-05-30 13:57:55 -0500232 nand_chip.select_chip(mtd, -1);
Simon Schwarz4f62e982011-09-14 15:30:16 -0400233}
Ladislav Michlc6a42002017-04-16 15:31:59 +0200234
235#include "nand_spl_loaders.c"