blob: 6831af98b73b46e8971ee26938945db8bcd8f4b8 [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>
Sean Anderson11a4c702023-11-04 16:37:41 -040014#include <system-constants.h>
Ilya Yanok15d67a52012-11-06 13:06:34 +000015#include <asm/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060016#include <linux/delay.h>
Ilya Yanok15d67a52012-11-06 13:06:34 +000017#include <linux/mtd/nand_ecc.h>
Tom Rini3bde7e22021-09-22 14:50:35 -040018#include <linux/mtd/rawnand.h>
Ilya Yanok15d67a52012-11-06 13:06:34 +000019
Tom Rinib4213492022-11-12 17:36:51 -050020static int nand_ecc_pos[] = CFG_SYS_NAND_ECCPOS;
Scott Wood2c1b7e12016-05-30 13:57:55 -050021static struct mtd_info *mtd;
Ilya Yanok15d67a52012-11-06 13:06:34 +000022static struct nand_chip nand_chip;
23
24#define ECCSTEPS (CONFIG_SYS_NAND_PAGE_SIZE / \
Tom Rinib4213492022-11-12 17:36:51 -050025 CFG_SYS_NAND_ECCSIZE)
26#define ECCTOTAL (ECCSTEPS * CFG_SYS_NAND_ECCBYTES)
Ilya Yanok15d67a52012-11-06 13:06:34 +000027
28
29/*
30 * NAND command for large page NAND devices (2k)
31 */
32static int nand_command(int block, int page, uint32_t offs,
33 u8 cmd)
34{
Scott Wood17fed142016-05-30 13:57:56 -050035 struct nand_chip *this = mtd_to_nand(mtd);
Sean Anderson11a4c702023-11-04 16:37:41 -040036 int page_addr = page + block * SYS_NAND_BLOCK_PAGES;
Ilya Yanok15d67a52012-11-06 13:06:34 +000037 void (*hwctrl)(struct mtd_info *mtd, int cmd,
38 unsigned int ctrl) = this->cmd_ctrl;
39
Scott Wood2c1b7e12016-05-30 13:57:55 -050040 while (!this->dev_ready(mtd))
Ilya Yanok15d67a52012-11-06 13:06:34 +000041 ;
42
43 /* Emulate NAND_CMD_READOOB */
44 if (cmd == NAND_CMD_READOOB) {
45 offs += CONFIG_SYS_NAND_PAGE_SIZE;
46 cmd = NAND_CMD_READ0;
47 }
48
49 /* Begin command latch cycle */
Scott Wood2c1b7e12016-05-30 13:57:55 -050050 hwctrl(mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Ilya Yanok15d67a52012-11-06 13:06:34 +000051
52 if (cmd == NAND_CMD_RESET) {
Scott Wood2c1b7e12016-05-30 13:57:55 -050053 hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Cooper Jr., Franklindc2b9412017-04-06 15:54:14 -050054
55 /*
56 * Apply this short delay always to ensure that we do wait
57 * tWB in any case on any machine.
58 */
59 ndelay(150);
60
Scott Wood2c1b7e12016-05-30 13:57:55 -050061 while (!this->dev_ready(mtd))
Ilya Yanok15d67a52012-11-06 13:06:34 +000062 ;
63 return 0;
64 }
65
66 /* Shift the offset from byte addressing to word addressing. */
Brian Norris67675222014-05-06 00:46:17 +053067 if ((this->options & NAND_BUSWIDTH_16) && !nand_opcode_8bits(cmd))
Ilya Yanok15d67a52012-11-06 13:06:34 +000068 offs >>= 1;
69
70 /* Set ALE and clear CLE to start address cycle */
71 /* Column address */
Scott Wood2c1b7e12016-05-30 13:57:55 -050072 hwctrl(mtd, offs & 0xff,
Ilya Yanok15d67a52012-11-06 13:06:34 +000073 NAND_CTRL_ALE | NAND_CTRL_CHANGE); /* A[7:0] */
Scott Wood2c1b7e12016-05-30 13:57:55 -050074 hwctrl(mtd, (offs >> 8) & 0xff, NAND_CTRL_ALE); /* A[11:9] */
Ilya Yanok15d67a52012-11-06 13:06:34 +000075 /* Row address */
Rostislav Lisovy278d9032014-09-09 15:54:30 +020076 if (cmd != NAND_CMD_RNDOUT) {
Scott Wood2c1b7e12016-05-30 13:57:55 -050077 hwctrl(mtd, (page_addr & 0xff),
Rostislav Lisovy278d9032014-09-09 15:54:30 +020078 NAND_CTRL_ALE); /* A[19:12] */
Scott Wood2c1b7e12016-05-30 13:57:55 -050079 hwctrl(mtd, ((page_addr >> 8) & 0xff),
Ilya Yanok15d67a52012-11-06 13:06:34 +000080 NAND_CTRL_ALE); /* A[27:20] */
81#ifdef CONFIG_SYS_NAND_5_ADDR_CYCLE
Rostislav Lisovy278d9032014-09-09 15:54:30 +020082 /* One more address cycle for devices > 128MiB */
Scott Wood2c1b7e12016-05-30 13:57:55 -050083 hwctrl(mtd, (page_addr >> 16) & 0x0f,
Ilya Yanok15d67a52012-11-06 13:06:34 +000084 NAND_CTRL_ALE); /* A[31:28] */
85#endif
Rostislav Lisovy278d9032014-09-09 15:54:30 +020086 }
87
Scott Wood2c1b7e12016-05-30 13:57:55 -050088 hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Ilya Yanok15d67a52012-11-06 13:06:34 +000089
Ilya Yanok15d67a52012-11-06 13:06:34 +000090
Cooper Jr., Franklindc2b9412017-04-06 15:54:14 -050091 /*
92 * Program and erase have their own busy handlers status, sequential
93 * in and status need no delay.
94 */
95 switch (cmd) {
96 case NAND_CMD_CACHEDPROG:
97 case NAND_CMD_PAGEPROG:
98 case NAND_CMD_ERASE1:
99 case NAND_CMD_ERASE2:
100 case NAND_CMD_SEQIN:
101 case NAND_CMD_RNDIN:
102 case NAND_CMD_STATUS:
103 return 0;
104
105 case NAND_CMD_RNDOUT:
106 /* No ready / busy check necessary */
Scott Wood2c1b7e12016-05-30 13:57:55 -0500107 hwctrl(mtd, NAND_CMD_RNDOUTSTART, NAND_CTRL_CLE |
Cooper Jr., Franklindc2b9412017-04-06 15:54:14 -0500108 NAND_CTRL_CHANGE);
109 hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
110 return 0;
111
112 case NAND_CMD_READ0:
113 /* Latch in address */
114 hwctrl(mtd, NAND_CMD_READSTART,
115 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Scott Wood2c1b7e12016-05-30 13:57:55 -0500116 hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Ilya Yanok15d67a52012-11-06 13:06:34 +0000117 }
118
Cooper Jr., Franklindc2b9412017-04-06 15:54:14 -0500119 /*
120 * Apply this short delay always to ensure that we do wait tWB in
121 * any case on any machine.
122 */
123 ndelay(150);
124
125 while (!this->dev_ready(mtd))
126 ;
127
Ilya Yanok15d67a52012-11-06 13:06:34 +0000128 return 0;
129}
130
131static int nand_is_bad_block(int block)
132{
Scott Wood17fed142016-05-30 13:57:56 -0500133 struct nand_chip *this = mtd_to_nand(mtd);
Ilya Yanok15d67a52012-11-06 13:06:34 +0000134
135 nand_command(block, 0, CONFIG_SYS_NAND_BAD_BLOCK_POS,
136 NAND_CMD_READOOB);
137
138 /*
139 * Read one byte (or two if it's a 16 bit chip).
140 */
141 if (this->options & NAND_BUSWIDTH_16) {
142 if (readw(this->IO_ADDR_R) != 0xffff)
143 return 1;
144 } else {
145 if (readb(this->IO_ADDR_R) != 0xff)
146 return 1;
147 }
148
149 return 0;
150}
151
152static int nand_read_page(int block, int page, void *dst)
153{
Scott Wood17fed142016-05-30 13:57:56 -0500154 struct nand_chip *this = mtd_to_nand(mtd);
Ilya Yanok15d67a52012-11-06 13:06:34 +0000155 u_char ecc_calc[ECCTOTAL];
156 u_char ecc_code[ECCTOTAL];
157 u_char oob_data[CONFIG_SYS_NAND_OOBSIZE];
158 int i;
Tom Rinib4213492022-11-12 17:36:51 -0500159 int eccsize = CFG_SYS_NAND_ECCSIZE;
160 int eccbytes = CFG_SYS_NAND_ECCBYTES;
Ilya Yanok15d67a52012-11-06 13:06:34 +0000161 int eccsteps = ECCSTEPS;
162 uint8_t *p = dst;
163 uint32_t data_pos = 0;
164 uint8_t *oob = &oob_data[0] + nand_ecc_pos[0];
165 uint32_t oob_pos = eccsize * eccsteps + nand_ecc_pos[0];
166
167 nand_command(block, page, 0, NAND_CMD_READ0);
168
169 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Scott Wood2c1b7e12016-05-30 13:57:55 -0500170 this->ecc.hwctl(mtd, NAND_ECC_READ);
Ilya Yanok15d67a52012-11-06 13:06:34 +0000171 nand_command(block, page, data_pos, NAND_CMD_RNDOUT);
172
Scott Wood2c1b7e12016-05-30 13:57:55 -0500173 this->read_buf(mtd, p, eccsize);
Ilya Yanok15d67a52012-11-06 13:06:34 +0000174
175 nand_command(block, page, oob_pos, NAND_CMD_RNDOUT);
176
Scott Wood2c1b7e12016-05-30 13:57:55 -0500177 this->read_buf(mtd, oob, eccbytes);
178 this->ecc.calculate(mtd, p, &ecc_calc[i]);
Ilya Yanok15d67a52012-11-06 13:06:34 +0000179
180 data_pos += eccsize;
181 oob_pos += eccbytes;
182 oob += eccbytes;
183 }
184
185 /* Pick the ECC bytes out of the oob data */
186 for (i = 0; i < ECCTOTAL; i++)
187 ecc_code[i] = oob_data[nand_ecc_pos[i]];
188
189 eccsteps = ECCSTEPS;
190 p = dst;
191
192 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
193 /* No chance to do something with the possible error message
194 * from correct_data(). We just hope that all possible errors
195 * are corrected by this routine.
196 */
Scott Wood2c1b7e12016-05-30 13:57:55 -0500197 this->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Ilya Yanok15d67a52012-11-06 13:06:34 +0000198 }
199
200 return 0;
201}
202
Ilya Yanok15d67a52012-11-06 13:06:34 +0000203/* nand_init() - initialize data to make nand usable by SPL */
204void nand_init(void)
205{
206 /*
207 * Init board specific nand support
208 */
Boris Brezillon3b5f8842016-06-15 20:56:10 +0200209 mtd = nand_to_mtd(&nand_chip);
Ilya Yanok15d67a52012-11-06 13:06:34 +0000210 nand_chip.IO_ADDR_R = nand_chip.IO_ADDR_W =
Tom Rinib4213492022-11-12 17:36:51 -0500211 (void __iomem *)CFG_SYS_NAND_BASE;
Ilya Yanok15d67a52012-11-06 13:06:34 +0000212 board_nand_init(&nand_chip);
213
214 if (nand_chip.select_chip)
Scott Wood2c1b7e12016-05-30 13:57:55 -0500215 nand_chip.select_chip(mtd, 0);
Ilya Yanok15d67a52012-11-06 13:06:34 +0000216
217 /* NAND chip may require reset after power-on */
218 nand_command(0, 0, 0, NAND_CMD_RESET);
219}
220
Sean Anderson8805f9d2023-11-04 16:37:44 -0400221unsigned int nand_page_size(void)
222{
223 return nand_to_mtd(&nand_chip)->writesize;
224}
225
Ilya Yanok15d67a52012-11-06 13:06:34 +0000226/* Unselect after operation */
227void nand_deselect(void)
228{
229 if (nand_chip.select_chip)
Scott Wood2c1b7e12016-05-30 13:57:55 -0500230 nand_chip.select_chip(mtd, -1);
Ilya Yanok15d67a52012-11-06 13:06:34 +0000231}
Ladislav Michlc6a42002017-04-16 15:31:59 +0200232
233#include "nand_spl_loaders.c"