blob: 727861c8f7ee78d1c633cc9710bef37dc3da0f10 [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>
Tom Rini3bde7e22021-09-22 14:50:35 -040011#include <linux/mtd/rawnand.h>
Simon Schwarz4f62e982011-09-14 15:30:16 -040012
13static int nand_ecc_pos[] = CONFIG_SYS_NAND_ECCPOS;
Scott Wood2c1b7e12016-05-30 13:57:55 -050014static struct mtd_info *mtd;
Simon Schwarz4f62e982011-09-14 15:30:16 -040015static struct nand_chip nand_chip;
16
Stefano Babic533607c2011-12-15 10:55:37 +010017#define ECCSTEPS (CONFIG_SYS_NAND_PAGE_SIZE / \
18 CONFIG_SYS_NAND_ECCSIZE)
19#define ECCTOTAL (ECCSTEPS * CONFIG_SYS_NAND_ECCBYTES)
20
21
Simon Schwarz4f62e982011-09-14 15:30:16 -040022#if (CONFIG_SYS_NAND_PAGE_SIZE <= 512)
23/*
24 * NAND command for small page NAND devices (512)
25 */
26static int nand_command(int block, int page, uint32_t offs,
27 u8 cmd)
28{
Scott Wood17fed142016-05-30 13:57:56 -050029 struct nand_chip *this = mtd_to_nand(mtd);
Simon Schwarz4f62e982011-09-14 15:30:16 -040030 int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
31
Scott Wood2c1b7e12016-05-30 13:57:55 -050032 while (!this->dev_ready(mtd))
Simon Schwarz4f62e982011-09-14 15:30:16 -040033 ;
34
35 /* Begin command latch cycle */
Scott Wood2c1b7e12016-05-30 13:57:55 -050036 this->cmd_ctrl(mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Simon Schwarz4f62e982011-09-14 15:30:16 -040037 /* Set ALE and clear CLE to start address cycle */
38 /* Column address */
Scott Wood2c1b7e12016-05-30 13:57:55 -050039 this->cmd_ctrl(mtd, offs, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
40 this->cmd_ctrl(mtd, page_addr & 0xff, NAND_CTRL_ALE); /* A[16:9] */
41 this->cmd_ctrl(mtd, (page_addr >> 8) & 0xff,
Simon Schwarz4f62e982011-09-14 15:30:16 -040042 NAND_CTRL_ALE); /* A[24:17] */
Simon Schwarz4f62e982011-09-14 15:30:16 -040043 /* Latch in address */
Scott Wood2c1b7e12016-05-30 13:57:55 -050044 this->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Simon Schwarz4f62e982011-09-14 15:30:16 -040045
46 /*
47 * Wait a while for the data to be ready
48 */
Scott Wood2c1b7e12016-05-30 13:57:55 -050049 while (!this->dev_ready(mtd))
Simon Schwarz4f62e982011-09-14 15:30:16 -040050 ;
51
52 return 0;
53}
54#else
55/*
56 * NAND command for large page NAND devices (2k)
57 */
58static int nand_command(int block, int page, uint32_t offs,
59 u8 cmd)
60{
Scott Wood17fed142016-05-30 13:57:56 -050061 struct nand_chip *this = mtd_to_nand(mtd);
Simon Schwarz4f62e982011-09-14 15:30:16 -040062 int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
63 void (*hwctrl)(struct mtd_info *mtd, int cmd,
64 unsigned int ctrl) = this->cmd_ctrl;
65
Scott Wood2c1b7e12016-05-30 13:57:55 -050066 while (!this->dev_ready(mtd))
Simon Schwarz4f62e982011-09-14 15:30:16 -040067 ;
68
69 /* Emulate NAND_CMD_READOOB */
70 if (cmd == NAND_CMD_READOOB) {
71 offs += CONFIG_SYS_NAND_PAGE_SIZE;
72 cmd = NAND_CMD_READ0;
73 }
74
75 /* Shift the offset from byte addressing to word addressing. */
Brian Norris67675222014-05-06 00:46:17 +053076 if ((this->options & NAND_BUSWIDTH_16) && !nand_opcode_8bits(cmd))
Simon Schwarz4f62e982011-09-14 15:30:16 -040077 offs >>= 1;
78
79 /* Begin command latch cycle */
Scott Wood2c1b7e12016-05-30 13:57:55 -050080 hwctrl(mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Simon Schwarz4f62e982011-09-14 15:30:16 -040081 /* Set ALE and clear CLE to start address cycle */
82 /* Column address */
Scott Wood2c1b7e12016-05-30 13:57:55 -050083 hwctrl(mtd, offs & 0xff,
84 NAND_CTRL_ALE | NAND_CTRL_CHANGE); /* A[7:0] */
85 hwctrl(mtd, (offs >> 8) & 0xff, NAND_CTRL_ALE); /* A[11:9] */
Simon Schwarz4f62e982011-09-14 15:30:16 -040086 /* Row address */
Scott Wood2c1b7e12016-05-30 13:57:55 -050087 hwctrl(mtd, (page_addr & 0xff), NAND_CTRL_ALE); /* A[19:12] */
88 hwctrl(mtd, ((page_addr >> 8) & 0xff),
89 NAND_CTRL_ALE); /* A[27:20] */
Simon Schwarz4f62e982011-09-14 15:30:16 -040090#ifdef CONFIG_SYS_NAND_5_ADDR_CYCLE
91 /* One more address cycle for devices > 128MiB */
Scott Wood2c1b7e12016-05-30 13:57:55 -050092 hwctrl(mtd, (page_addr >> 16) & 0x0f,
Simon Schwarz4f62e982011-09-14 15:30:16 -040093 NAND_CTRL_ALE); /* A[31:28] */
94#endif
95 /* Latch in address */
Scott Wood2c1b7e12016-05-30 13:57:55 -050096 hwctrl(mtd, NAND_CMD_READSTART,
97 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
98 hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Simon Schwarz4f62e982011-09-14 15:30:16 -040099
100 /*
101 * Wait a while for the data to be ready
102 */
Scott Wood2c1b7e12016-05-30 13:57:55 -0500103 while (!this->dev_ready(mtd))
Simon Schwarz4f62e982011-09-14 15:30:16 -0400104 ;
105
106 return 0;
107}
108#endif
109
110static int nand_is_bad_block(int block)
111{
Scott Wood17fed142016-05-30 13:57:56 -0500112 struct nand_chip *this = mtd_to_nand(mtd);
Vladimir Zapolskiycb8183d2015-07-18 01:47:08 +0300113 u_char bb_data[2];
Simon Schwarz4f62e982011-09-14 15:30:16 -0400114
115 nand_command(block, 0, CONFIG_SYS_NAND_BAD_BLOCK_POS,
116 NAND_CMD_READOOB);
117
118 /*
119 * Read one byte (or two if it's a 16 bit chip).
120 */
121 if (this->options & NAND_BUSWIDTH_16) {
Scott Wood2c1b7e12016-05-30 13:57:55 -0500122 this->read_buf(mtd, bb_data, 2);
Vladimir Zapolskiycb8183d2015-07-18 01:47:08 +0300123 if (bb_data[0] != 0xff || bb_data[1] != 0xff)
Simon Schwarz4f62e982011-09-14 15:30:16 -0400124 return 1;
125 } else {
Scott Wood2c1b7e12016-05-30 13:57:55 -0500126 this->read_buf(mtd, bb_data, 1);
Vladimir Zapolskiycb8183d2015-07-18 01:47:08 +0300127 if (bb_data[0] != 0xff)
Simon Schwarz4f62e982011-09-14 15:30:16 -0400128 return 1;
129 }
130
131 return 0;
132}
133
Heiko Schocher44bd9b32011-11-01 20:00:30 +0000134#if defined(CONFIG_SYS_NAND_HW_ECC_OOBFIRST)
135static int nand_read_page(int block, int page, uchar *dst)
136{
Scott Wood17fed142016-05-30 13:57:56 -0500137 struct nand_chip *this = mtd_to_nand(mtd);
Stefano Babic533607c2011-12-15 10:55:37 +0100138 u_char ecc_calc[ECCTOTAL];
139 u_char ecc_code[ECCTOTAL];
140 u_char oob_data[CONFIG_SYS_NAND_OOBSIZE];
Heiko Schocher44bd9b32011-11-01 20:00:30 +0000141 int i;
142 int eccsize = CONFIG_SYS_NAND_ECCSIZE;
143 int eccbytes = CONFIG_SYS_NAND_ECCBYTES;
Stefano Babic533607c2011-12-15 10:55:37 +0100144 int eccsteps = ECCSTEPS;
Heiko Schocher44bd9b32011-11-01 20:00:30 +0000145 uint8_t *p = dst;
Heiko Schocher44bd9b32011-11-01 20:00:30 +0000146
Heiko Schocher44bd9b32011-11-01 20:00:30 +0000147 nand_command(block, page, 0, NAND_CMD_READOOB);
Scott Wood2c1b7e12016-05-30 13:57:55 -0500148 this->read_buf(mtd, oob_data, CONFIG_SYS_NAND_OOBSIZE);
Heiko Schocher44bd9b32011-11-01 20:00:30 +0000149 nand_command(block, page, 0, NAND_CMD_READ0);
150
151 /* Pick the ECC bytes out of the oob data */
Stefano Babic533607c2011-12-15 10:55:37 +0100152 for (i = 0; i < ECCTOTAL; i++)
Heiko Schocher44bd9b32011-11-01 20:00:30 +0000153 ecc_code[i] = oob_data[nand_ecc_pos[i]];
154
155
156 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Scott Wood2c1b7e12016-05-30 13:57:55 -0500157 this->ecc.hwctl(mtd, NAND_ECC_READ);
158 this->read_buf(mtd, p, eccsize);
159 this->ecc.calculate(mtd, p, &ecc_calc[i]);
160 this->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Heiko Schocher44bd9b32011-11-01 20:00:30 +0000161 }
162
163 return 0;
164}
165#else
Simon Schwarz4f62e982011-09-14 15:30:16 -0400166static int nand_read_page(int block, int page, void *dst)
167{
Scott Wood17fed142016-05-30 13:57:56 -0500168 struct nand_chip *this = mtd_to_nand(mtd);
Stefano Babic533607c2011-12-15 10:55:37 +0100169 u_char ecc_calc[ECCTOTAL];
170 u_char ecc_code[ECCTOTAL];
171 u_char oob_data[CONFIG_SYS_NAND_OOBSIZE];
Simon Schwarz4f62e982011-09-14 15:30:16 -0400172 int i;
173 int eccsize = CONFIG_SYS_NAND_ECCSIZE;
174 int eccbytes = CONFIG_SYS_NAND_ECCBYTES;
Stefano Babic533607c2011-12-15 10:55:37 +0100175 int eccsteps = ECCSTEPS;
Simon Schwarz4f62e982011-09-14 15:30:16 -0400176 uint8_t *p = dst;
Simon Schwarz4f62e982011-09-14 15:30:16 -0400177
178 nand_command(block, page, 0, NAND_CMD_READ0);
179
Simon Schwarz4f62e982011-09-14 15:30:16 -0400180 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Ilya Yanok4e699622011-11-28 06:37:37 +0000181 if (this->ecc.mode != NAND_ECC_SOFT)
Scott Wood2c1b7e12016-05-30 13:57:55 -0500182 this->ecc.hwctl(mtd, NAND_ECC_READ);
183 this->read_buf(mtd, p, eccsize);
184 this->ecc.calculate(mtd, p, &ecc_calc[i]);
Simon Schwarz4f62e982011-09-14 15:30:16 -0400185 }
Scott Wood2c1b7e12016-05-30 13:57:55 -0500186 this->read_buf(mtd, oob_data, CONFIG_SYS_NAND_OOBSIZE);
Simon Schwarz4f62e982011-09-14 15:30:16 -0400187
188 /* Pick the ECC bytes out of the oob data */
Stefano Babic533607c2011-12-15 10:55:37 +0100189 for (i = 0; i < ECCTOTAL; i++)
Simon Schwarz4f62e982011-09-14 15:30:16 -0400190 ecc_code[i] = oob_data[nand_ecc_pos[i]];
191
Stefano Babic533607c2011-12-15 10:55:37 +0100192 eccsteps = ECCSTEPS;
Simon Schwarz4f62e982011-09-14 15:30:16 -0400193 p = dst;
194
195 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
196 /* No chance to do something with the possible error message
197 * from correct_data(). We just hope that all possible errors
198 * are corrected by this routine.
199 */
Scott Wood2c1b7e12016-05-30 13:57:55 -0500200 this->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Simon Schwarz4f62e982011-09-14 15:30:16 -0400201 }
202
203 return 0;
204}
Heiko Schocher44bd9b32011-11-01 20:00:30 +0000205#endif
Simon Schwarz4f62e982011-09-14 15:30:16 -0400206
Simon Schwarz4f62e982011-09-14 15:30:16 -0400207/* nand_init() - initialize data to make nand usable by SPL */
208void nand_init(void)
209{
210 /*
211 * Init board specific nand support
212 */
Boris Brezillon3b5f8842016-06-15 20:56:10 +0200213 mtd = nand_to_mtd(&nand_chip);
Simon Schwarz4f62e982011-09-14 15:30:16 -0400214 nand_chip.IO_ADDR_R = nand_chip.IO_ADDR_W =
215 (void __iomem *)CONFIG_SYS_NAND_BASE;
Simon Schwarz4f62e982011-09-14 15:30:16 -0400216 board_nand_init(&nand_chip);
217
Ilya Yanok4e699622011-11-28 06:37:37 +0000218#ifdef CONFIG_SPL_NAND_SOFTECC
219 if (nand_chip.ecc.mode == NAND_ECC_SOFT) {
220 nand_chip.ecc.calculate = nand_calculate_ecc;
221 nand_chip.ecc.correct = nand_correct_data;
222 }
223#endif
224
Simon Schwarz4f62e982011-09-14 15:30:16 -0400225 if (nand_chip.select_chip)
Scott Wood2c1b7e12016-05-30 13:57:55 -0500226 nand_chip.select_chip(mtd, 0);
Simon Schwarz4f62e982011-09-14 15:30:16 -0400227}
228
229/* Unselect after operation */
230void nand_deselect(void)
231{
232 if (nand_chip.select_chip)
Scott Wood2c1b7e12016-05-30 13:57:55 -0500233 nand_chip.select_chip(mtd, -1);
Simon Schwarz4f62e982011-09-14 15:30:16 -0400234}
Ladislav Michlc6a42002017-04-16 15:31:59 +0200235
236#include "nand_spl_loaders.c"