blob: ba2f33a96efa8c8ac27f8ece500e20a2257f933d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ilya Yanok15d67a52012-11-06 13:06:34 +00002/*
3 * (C) Copyright 2012
4 * Konstantin Kozhevnikov, Cogent Embedded
5 *
6 * based on nand_spl_simple code
7 *
8 * (C) Copyright 2006-2008
9 * Stefan Roese, DENX Software Engineering, sr@denx.de.
Ilya Yanok15d67a52012-11-06 13:06:34 +000010 */
11
12#include <common.h>
13#include <nand.h>
14#include <asm/io.h>
15#include <linux/mtd/nand_ecc.h>
16
17static int nand_ecc_pos[] = CONFIG_SYS_NAND_ECCPOS;
Scott Wood2c1b7e12016-05-30 13:57:55 -050018static struct mtd_info *mtd;
Ilya Yanok15d67a52012-11-06 13:06:34 +000019static struct nand_chip nand_chip;
20
21#define ECCSTEPS (CONFIG_SYS_NAND_PAGE_SIZE / \
22 CONFIG_SYS_NAND_ECCSIZE)
23#define ECCTOTAL (ECCSTEPS * CONFIG_SYS_NAND_ECCBYTES)
24
25
26/*
27 * NAND command for large page NAND devices (2k)
28 */
29static int nand_command(int block, int page, uint32_t offs,
30 u8 cmd)
31{
Scott Wood17fed142016-05-30 13:57:56 -050032 struct nand_chip *this = mtd_to_nand(mtd);
Ilya Yanok15d67a52012-11-06 13:06:34 +000033 int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
34 void (*hwctrl)(struct mtd_info *mtd, int cmd,
35 unsigned int ctrl) = this->cmd_ctrl;
36
Scott Wood2c1b7e12016-05-30 13:57:55 -050037 while (!this->dev_ready(mtd))
Ilya Yanok15d67a52012-11-06 13:06:34 +000038 ;
39
40 /* Emulate NAND_CMD_READOOB */
41 if (cmd == NAND_CMD_READOOB) {
42 offs += CONFIG_SYS_NAND_PAGE_SIZE;
43 cmd = NAND_CMD_READ0;
44 }
45
46 /* Begin command latch cycle */
Scott Wood2c1b7e12016-05-30 13:57:55 -050047 hwctrl(mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Ilya Yanok15d67a52012-11-06 13:06:34 +000048
49 if (cmd == NAND_CMD_RESET) {
Scott Wood2c1b7e12016-05-30 13:57:55 -050050 hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Cooper Jr., Franklindc2b9412017-04-06 15:54:14 -050051
52 /*
53 * Apply this short delay always to ensure that we do wait
54 * tWB in any case on any machine.
55 */
56 ndelay(150);
57
Scott Wood2c1b7e12016-05-30 13:57:55 -050058 while (!this->dev_ready(mtd))
Ilya Yanok15d67a52012-11-06 13:06:34 +000059 ;
60 return 0;
61 }
62
63 /* Shift the offset from byte addressing to word addressing. */
Brian Norris67675222014-05-06 00:46:17 +053064 if ((this->options & NAND_BUSWIDTH_16) && !nand_opcode_8bits(cmd))
Ilya Yanok15d67a52012-11-06 13:06:34 +000065 offs >>= 1;
66
67 /* Set ALE and clear CLE to start address cycle */
68 /* Column address */
Scott Wood2c1b7e12016-05-30 13:57:55 -050069 hwctrl(mtd, offs & 0xff,
Ilya Yanok15d67a52012-11-06 13:06:34 +000070 NAND_CTRL_ALE | NAND_CTRL_CHANGE); /* A[7:0] */
Scott Wood2c1b7e12016-05-30 13:57:55 -050071 hwctrl(mtd, (offs >> 8) & 0xff, NAND_CTRL_ALE); /* A[11:9] */
Ilya Yanok15d67a52012-11-06 13:06:34 +000072 /* Row address */
Rostislav Lisovy278d9032014-09-09 15:54:30 +020073 if (cmd != NAND_CMD_RNDOUT) {
Scott Wood2c1b7e12016-05-30 13:57:55 -050074 hwctrl(mtd, (page_addr & 0xff),
Rostislav Lisovy278d9032014-09-09 15:54:30 +020075 NAND_CTRL_ALE); /* A[19:12] */
Scott Wood2c1b7e12016-05-30 13:57:55 -050076 hwctrl(mtd, ((page_addr >> 8) & 0xff),
Ilya Yanok15d67a52012-11-06 13:06:34 +000077 NAND_CTRL_ALE); /* A[27:20] */
78#ifdef CONFIG_SYS_NAND_5_ADDR_CYCLE
Rostislav Lisovy278d9032014-09-09 15:54:30 +020079 /* One more address cycle for devices > 128MiB */
Scott Wood2c1b7e12016-05-30 13:57:55 -050080 hwctrl(mtd, (page_addr >> 16) & 0x0f,
Ilya Yanok15d67a52012-11-06 13:06:34 +000081 NAND_CTRL_ALE); /* A[31:28] */
82#endif
Rostislav Lisovy278d9032014-09-09 15:54:30 +020083 }
84
Scott Wood2c1b7e12016-05-30 13:57:55 -050085 hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Ilya Yanok15d67a52012-11-06 13:06:34 +000086
Ilya Yanok15d67a52012-11-06 13:06:34 +000087
Cooper Jr., Franklindc2b9412017-04-06 15:54:14 -050088 /*
89 * Program and erase have their own busy handlers status, sequential
90 * in and status need no delay.
91 */
92 switch (cmd) {
93 case NAND_CMD_CACHEDPROG:
94 case NAND_CMD_PAGEPROG:
95 case NAND_CMD_ERASE1:
96 case NAND_CMD_ERASE2:
97 case NAND_CMD_SEQIN:
98 case NAND_CMD_RNDIN:
99 case NAND_CMD_STATUS:
100 return 0;
101
102 case NAND_CMD_RNDOUT:
103 /* No ready / busy check necessary */
Scott Wood2c1b7e12016-05-30 13:57:55 -0500104 hwctrl(mtd, NAND_CMD_RNDOUTSTART, NAND_CTRL_CLE |
Cooper Jr., Franklindc2b9412017-04-06 15:54:14 -0500105 NAND_CTRL_CHANGE);
106 hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
107 return 0;
108
109 case NAND_CMD_READ0:
110 /* Latch in address */
111 hwctrl(mtd, NAND_CMD_READSTART,
112 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Scott Wood2c1b7e12016-05-30 13:57:55 -0500113 hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Ilya Yanok15d67a52012-11-06 13:06:34 +0000114 }
115
Cooper Jr., Franklindc2b9412017-04-06 15:54:14 -0500116 /*
117 * Apply this short delay always to ensure that we do wait tWB in
118 * any case on any machine.
119 */
120 ndelay(150);
121
122 while (!this->dev_ready(mtd))
123 ;
124
Ilya Yanok15d67a52012-11-06 13:06:34 +0000125 return 0;
126}
127
128static int nand_is_bad_block(int block)
129{
Scott Wood17fed142016-05-30 13:57:56 -0500130 struct nand_chip *this = mtd_to_nand(mtd);
Ilya Yanok15d67a52012-11-06 13:06:34 +0000131
132 nand_command(block, 0, CONFIG_SYS_NAND_BAD_BLOCK_POS,
133 NAND_CMD_READOOB);
134
135 /*
136 * Read one byte (or two if it's a 16 bit chip).
137 */
138 if (this->options & NAND_BUSWIDTH_16) {
139 if (readw(this->IO_ADDR_R) != 0xffff)
140 return 1;
141 } else {
142 if (readb(this->IO_ADDR_R) != 0xff)
143 return 1;
144 }
145
146 return 0;
147}
148
149static int nand_read_page(int block, int page, void *dst)
150{
Scott Wood17fed142016-05-30 13:57:56 -0500151 struct nand_chip *this = mtd_to_nand(mtd);
Ilya Yanok15d67a52012-11-06 13:06:34 +0000152 u_char ecc_calc[ECCTOTAL];
153 u_char ecc_code[ECCTOTAL];
154 u_char oob_data[CONFIG_SYS_NAND_OOBSIZE];
155 int i;
156 int eccsize = CONFIG_SYS_NAND_ECCSIZE;
157 int eccbytes = CONFIG_SYS_NAND_ECCBYTES;
158 int eccsteps = ECCSTEPS;
159 uint8_t *p = dst;
160 uint32_t data_pos = 0;
161 uint8_t *oob = &oob_data[0] + nand_ecc_pos[0];
162 uint32_t oob_pos = eccsize * eccsteps + nand_ecc_pos[0];
163
164 nand_command(block, page, 0, NAND_CMD_READ0);
165
166 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Scott Wood2c1b7e12016-05-30 13:57:55 -0500167 this->ecc.hwctl(mtd, NAND_ECC_READ);
Ilya Yanok15d67a52012-11-06 13:06:34 +0000168 nand_command(block, page, data_pos, NAND_CMD_RNDOUT);
169
Scott Wood2c1b7e12016-05-30 13:57:55 -0500170 this->read_buf(mtd, p, eccsize);
Ilya Yanok15d67a52012-11-06 13:06:34 +0000171
172 nand_command(block, page, oob_pos, NAND_CMD_RNDOUT);
173
Scott Wood2c1b7e12016-05-30 13:57:55 -0500174 this->read_buf(mtd, oob, eccbytes);
175 this->ecc.calculate(mtd, p, &ecc_calc[i]);
Ilya Yanok15d67a52012-11-06 13:06:34 +0000176
177 data_pos += eccsize;
178 oob_pos += eccbytes;
179 oob += eccbytes;
180 }
181
182 /* Pick the ECC bytes out of the oob data */
183 for (i = 0; i < ECCTOTAL; i++)
184 ecc_code[i] = oob_data[nand_ecc_pos[i]];
185
186 eccsteps = ECCSTEPS;
187 p = dst;
188
189 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
190 /* No chance to do something with the possible error message
191 * from correct_data(). We just hope that all possible errors
192 * are corrected by this routine.
193 */
Scott Wood2c1b7e12016-05-30 13:57:55 -0500194 this->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Ilya Yanok15d67a52012-11-06 13:06:34 +0000195 }
196
197 return 0;
198}
199
Ilya Yanok15d67a52012-11-06 13:06:34 +0000200/* nand_init() - initialize data to make nand usable by SPL */
201void nand_init(void)
202{
203 /*
204 * Init board specific nand support
205 */
Boris Brezillon3b5f8842016-06-15 20:56:10 +0200206 mtd = nand_to_mtd(&nand_chip);
Ilya Yanok15d67a52012-11-06 13:06:34 +0000207 nand_chip.IO_ADDR_R = nand_chip.IO_ADDR_W =
208 (void __iomem *)CONFIG_SYS_NAND_BASE;
209 board_nand_init(&nand_chip);
210
211 if (nand_chip.select_chip)
Scott Wood2c1b7e12016-05-30 13:57:55 -0500212 nand_chip.select_chip(mtd, 0);
Ilya Yanok15d67a52012-11-06 13:06:34 +0000213
214 /* NAND chip may require reset after power-on */
215 nand_command(0, 0, 0, NAND_CMD_RESET);
216}
217
218/* Unselect after operation */
219void nand_deselect(void)
220{
221 if (nand_chip.select_chip)
Scott Wood2c1b7e12016-05-30 13:57:55 -0500222 nand_chip.select_chip(mtd, -1);
Ilya Yanok15d67a52012-11-06 13:06:34 +0000223}
Ladislav Michlc6a42002017-04-16 15:31:59 +0200224
225#include "nand_spl_loaders.c"