1*cfcc706cSMiquel Raynal /*
2*cfcc706cSMiquel Raynal * (C) Copyright 2006-2008
3*cfcc706cSMiquel Raynal * Stefan Roese, DENX Software Engineering, sr@denx.de.
4*cfcc706cSMiquel Raynal *
5*cfcc706cSMiquel Raynal * SPDX-License-Identifier: GPL-2.0+
6*cfcc706cSMiquel Raynal */
7*cfcc706cSMiquel Raynal
8*cfcc706cSMiquel Raynal #include <common.h>
9*cfcc706cSMiquel Raynal #include <nand.h>
10*cfcc706cSMiquel Raynal #include <asm/io.h>
11*cfcc706cSMiquel Raynal #include <linux/mtd/nand_ecc.h>
12*cfcc706cSMiquel Raynal
13*cfcc706cSMiquel Raynal static int nand_ecc_pos[] = CONFIG_SYS_NAND_ECCPOS;
14*cfcc706cSMiquel Raynal static struct mtd_info *mtd;
15*cfcc706cSMiquel Raynal static struct nand_chip nand_chip;
16*cfcc706cSMiquel Raynal
17*cfcc706cSMiquel Raynal #define ECCSTEPS (CONFIG_SYS_NAND_PAGE_SIZE / \
18*cfcc706cSMiquel Raynal CONFIG_SYS_NAND_ECCSIZE)
19*cfcc706cSMiquel Raynal #define ECCTOTAL (ECCSTEPS * CONFIG_SYS_NAND_ECCBYTES)
20*cfcc706cSMiquel Raynal
21*cfcc706cSMiquel Raynal
22*cfcc706cSMiquel Raynal #if (CONFIG_SYS_NAND_PAGE_SIZE <= 512)
23*cfcc706cSMiquel Raynal /*
24*cfcc706cSMiquel Raynal * NAND command for small page NAND devices (512)
25*cfcc706cSMiquel Raynal */
nand_command(int block,int page,uint32_t offs,u8 cmd)26*cfcc706cSMiquel Raynal static int nand_command(int block, int page, uint32_t offs,
27*cfcc706cSMiquel Raynal u8 cmd)
28*cfcc706cSMiquel Raynal {
29*cfcc706cSMiquel Raynal struct nand_chip *this = mtd_to_nand(mtd);
30*cfcc706cSMiquel Raynal int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
31*cfcc706cSMiquel Raynal
32*cfcc706cSMiquel Raynal while (!this->dev_ready(mtd))
33*cfcc706cSMiquel Raynal ;
34*cfcc706cSMiquel Raynal
35*cfcc706cSMiquel Raynal /* Begin command latch cycle */
36*cfcc706cSMiquel Raynal this->cmd_ctrl(mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
37*cfcc706cSMiquel Raynal /* Set ALE and clear CLE to start address cycle */
38*cfcc706cSMiquel Raynal /* Column address */
39*cfcc706cSMiquel Raynal this->cmd_ctrl(mtd, offs, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
40*cfcc706cSMiquel Raynal this->cmd_ctrl(mtd, page_addr & 0xff, NAND_CTRL_ALE); /* A[16:9] */
41*cfcc706cSMiquel Raynal this->cmd_ctrl(mtd, (page_addr >> 8) & 0xff,
42*cfcc706cSMiquel Raynal NAND_CTRL_ALE); /* A[24:17] */
43*cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_4_ADDR_CYCLE
44*cfcc706cSMiquel Raynal /* One more address cycle for devices > 32MiB */
45*cfcc706cSMiquel Raynal this->cmd_ctrl(mtd, (page_addr >> 16) & 0x0f,
46*cfcc706cSMiquel Raynal NAND_CTRL_ALE); /* A[28:25] */
47*cfcc706cSMiquel Raynal #endif
48*cfcc706cSMiquel Raynal /* Latch in address */
49*cfcc706cSMiquel Raynal this->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
50*cfcc706cSMiquel Raynal
51*cfcc706cSMiquel Raynal /*
52*cfcc706cSMiquel Raynal * Wait a while for the data to be ready
53*cfcc706cSMiquel Raynal */
54*cfcc706cSMiquel Raynal while (!this->dev_ready(mtd))
55*cfcc706cSMiquel Raynal ;
56*cfcc706cSMiquel Raynal
57*cfcc706cSMiquel Raynal return 0;
58*cfcc706cSMiquel Raynal }
59*cfcc706cSMiquel Raynal #else
60*cfcc706cSMiquel Raynal /*
61*cfcc706cSMiquel Raynal * NAND command for large page NAND devices (2k)
62*cfcc706cSMiquel Raynal */
nand_command(int block,int page,uint32_t offs,u8 cmd)63*cfcc706cSMiquel Raynal static int nand_command(int block, int page, uint32_t offs,
64*cfcc706cSMiquel Raynal u8 cmd)
65*cfcc706cSMiquel Raynal {
66*cfcc706cSMiquel Raynal struct nand_chip *this = mtd_to_nand(mtd);
67*cfcc706cSMiquel Raynal int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
68*cfcc706cSMiquel Raynal void (*hwctrl)(struct mtd_info *mtd, int cmd,
69*cfcc706cSMiquel Raynal unsigned int ctrl) = this->cmd_ctrl;
70*cfcc706cSMiquel Raynal
71*cfcc706cSMiquel Raynal while (!this->dev_ready(mtd))
72*cfcc706cSMiquel Raynal ;
73*cfcc706cSMiquel Raynal
74*cfcc706cSMiquel Raynal /* Emulate NAND_CMD_READOOB */
75*cfcc706cSMiquel Raynal if (cmd == NAND_CMD_READOOB) {
76*cfcc706cSMiquel Raynal offs += CONFIG_SYS_NAND_PAGE_SIZE;
77*cfcc706cSMiquel Raynal cmd = NAND_CMD_READ0;
78*cfcc706cSMiquel Raynal }
79*cfcc706cSMiquel Raynal
80*cfcc706cSMiquel Raynal /* Shift the offset from byte addressing to word addressing. */
81*cfcc706cSMiquel Raynal if ((this->options & NAND_BUSWIDTH_16) && !nand_opcode_8bits(cmd))
82*cfcc706cSMiquel Raynal offs >>= 1;
83*cfcc706cSMiquel Raynal
84*cfcc706cSMiquel Raynal /* Begin command latch cycle */
85*cfcc706cSMiquel Raynal hwctrl(mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
86*cfcc706cSMiquel Raynal /* Set ALE and clear CLE to start address cycle */
87*cfcc706cSMiquel Raynal /* Column address */
88*cfcc706cSMiquel Raynal hwctrl(mtd, offs & 0xff,
89*cfcc706cSMiquel Raynal NAND_CTRL_ALE | NAND_CTRL_CHANGE); /* A[7:0] */
90*cfcc706cSMiquel Raynal hwctrl(mtd, (offs >> 8) & 0xff, NAND_CTRL_ALE); /* A[11:9] */
91*cfcc706cSMiquel Raynal /* Row address */
92*cfcc706cSMiquel Raynal hwctrl(mtd, (page_addr & 0xff), NAND_CTRL_ALE); /* A[19:12] */
93*cfcc706cSMiquel Raynal hwctrl(mtd, ((page_addr >> 8) & 0xff),
94*cfcc706cSMiquel Raynal NAND_CTRL_ALE); /* A[27:20] */
95*cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_5_ADDR_CYCLE
96*cfcc706cSMiquel Raynal /* One more address cycle for devices > 128MiB */
97*cfcc706cSMiquel Raynal hwctrl(mtd, (page_addr >> 16) & 0x0f,
98*cfcc706cSMiquel Raynal NAND_CTRL_ALE); /* A[31:28] */
99*cfcc706cSMiquel Raynal #endif
100*cfcc706cSMiquel Raynal /* Latch in address */
101*cfcc706cSMiquel Raynal hwctrl(mtd, NAND_CMD_READSTART,
102*cfcc706cSMiquel Raynal NAND_CTRL_CLE | NAND_CTRL_CHANGE);
103*cfcc706cSMiquel Raynal hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
104*cfcc706cSMiquel Raynal
105*cfcc706cSMiquel Raynal /*
106*cfcc706cSMiquel Raynal * Wait a while for the data to be ready
107*cfcc706cSMiquel Raynal */
108*cfcc706cSMiquel Raynal while (!this->dev_ready(mtd))
109*cfcc706cSMiquel Raynal ;
110*cfcc706cSMiquel Raynal
111*cfcc706cSMiquel Raynal return 0;
112*cfcc706cSMiquel Raynal }
113*cfcc706cSMiquel Raynal #endif
114*cfcc706cSMiquel Raynal
nand_is_bad_block(int block)115*cfcc706cSMiquel Raynal static int nand_is_bad_block(int block)
116*cfcc706cSMiquel Raynal {
117*cfcc706cSMiquel Raynal struct nand_chip *this = mtd_to_nand(mtd);
118*cfcc706cSMiquel Raynal u_char bb_data[2];
119*cfcc706cSMiquel Raynal
120*cfcc706cSMiquel Raynal nand_command(block, 0, CONFIG_SYS_NAND_BAD_BLOCK_POS,
121*cfcc706cSMiquel Raynal NAND_CMD_READOOB);
122*cfcc706cSMiquel Raynal
123*cfcc706cSMiquel Raynal /*
124*cfcc706cSMiquel Raynal * Read one byte (or two if it's a 16 bit chip).
125*cfcc706cSMiquel Raynal */
126*cfcc706cSMiquel Raynal if (this->options & NAND_BUSWIDTH_16) {
127*cfcc706cSMiquel Raynal this->read_buf(mtd, bb_data, 2);
128*cfcc706cSMiquel Raynal if (bb_data[0] != 0xff || bb_data[1] != 0xff)
129*cfcc706cSMiquel Raynal return 1;
130*cfcc706cSMiquel Raynal } else {
131*cfcc706cSMiquel Raynal this->read_buf(mtd, bb_data, 1);
132*cfcc706cSMiquel Raynal if (bb_data[0] != 0xff)
133*cfcc706cSMiquel Raynal return 1;
134*cfcc706cSMiquel Raynal }
135*cfcc706cSMiquel Raynal
136*cfcc706cSMiquel Raynal return 0;
137*cfcc706cSMiquel Raynal }
138*cfcc706cSMiquel Raynal
139*cfcc706cSMiquel Raynal #if defined(CONFIG_SYS_NAND_HW_ECC_OOBFIRST)
nand_read_page(int block,int page,uchar * dst)140*cfcc706cSMiquel Raynal static int nand_read_page(int block, int page, uchar *dst)
141*cfcc706cSMiquel Raynal {
142*cfcc706cSMiquel Raynal struct nand_chip *this = mtd_to_nand(mtd);
143*cfcc706cSMiquel Raynal u_char ecc_calc[ECCTOTAL];
144*cfcc706cSMiquel Raynal u_char ecc_code[ECCTOTAL];
145*cfcc706cSMiquel Raynal u_char oob_data[CONFIG_SYS_NAND_OOBSIZE];
146*cfcc706cSMiquel Raynal int i;
147*cfcc706cSMiquel Raynal int eccsize = CONFIG_SYS_NAND_ECCSIZE;
148*cfcc706cSMiquel Raynal int eccbytes = CONFIG_SYS_NAND_ECCBYTES;
149*cfcc706cSMiquel Raynal int eccsteps = ECCSTEPS;
150*cfcc706cSMiquel Raynal uint8_t *p = dst;
151*cfcc706cSMiquel Raynal
152*cfcc706cSMiquel Raynal nand_command(block, page, 0, NAND_CMD_READOOB);
153*cfcc706cSMiquel Raynal this->read_buf(mtd, oob_data, CONFIG_SYS_NAND_OOBSIZE);
154*cfcc706cSMiquel Raynal nand_command(block, page, 0, NAND_CMD_READ0);
155*cfcc706cSMiquel Raynal
156*cfcc706cSMiquel Raynal /* Pick the ECC bytes out of the oob data */
157*cfcc706cSMiquel Raynal for (i = 0; i < ECCTOTAL; i++)
158*cfcc706cSMiquel Raynal ecc_code[i] = oob_data[nand_ecc_pos[i]];
159*cfcc706cSMiquel Raynal
160*cfcc706cSMiquel Raynal
161*cfcc706cSMiquel Raynal for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
162*cfcc706cSMiquel Raynal this->ecc.hwctl(mtd, NAND_ECC_READ);
163*cfcc706cSMiquel Raynal this->read_buf(mtd, p, eccsize);
164*cfcc706cSMiquel Raynal this->ecc.calculate(mtd, p, &ecc_calc[i]);
165*cfcc706cSMiquel Raynal this->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
166*cfcc706cSMiquel Raynal }
167*cfcc706cSMiquel Raynal
168*cfcc706cSMiquel Raynal return 0;
169*cfcc706cSMiquel Raynal }
170*cfcc706cSMiquel Raynal #else
nand_read_page(int block,int page,void * dst)171*cfcc706cSMiquel Raynal static int nand_read_page(int block, int page, void *dst)
172*cfcc706cSMiquel Raynal {
173*cfcc706cSMiquel Raynal struct nand_chip *this = mtd_to_nand(mtd);
174*cfcc706cSMiquel Raynal u_char ecc_calc[ECCTOTAL];
175*cfcc706cSMiquel Raynal u_char ecc_code[ECCTOTAL];
176*cfcc706cSMiquel Raynal u_char oob_data[CONFIG_SYS_NAND_OOBSIZE];
177*cfcc706cSMiquel Raynal int i;
178*cfcc706cSMiquel Raynal int eccsize = CONFIG_SYS_NAND_ECCSIZE;
179*cfcc706cSMiquel Raynal int eccbytes = CONFIG_SYS_NAND_ECCBYTES;
180*cfcc706cSMiquel Raynal int eccsteps = ECCSTEPS;
181*cfcc706cSMiquel Raynal uint8_t *p = dst;
182*cfcc706cSMiquel Raynal
183*cfcc706cSMiquel Raynal nand_command(block, page, 0, NAND_CMD_READ0);
184*cfcc706cSMiquel Raynal
185*cfcc706cSMiquel Raynal for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
186*cfcc706cSMiquel Raynal if (this->ecc.mode != NAND_ECC_SOFT)
187*cfcc706cSMiquel Raynal this->ecc.hwctl(mtd, NAND_ECC_READ);
188*cfcc706cSMiquel Raynal this->read_buf(mtd, p, eccsize);
189*cfcc706cSMiquel Raynal this->ecc.calculate(mtd, p, &ecc_calc[i]);
190*cfcc706cSMiquel Raynal }
191*cfcc706cSMiquel Raynal this->read_buf(mtd, oob_data, CONFIG_SYS_NAND_OOBSIZE);
192*cfcc706cSMiquel Raynal
193*cfcc706cSMiquel Raynal /* Pick the ECC bytes out of the oob data */
194*cfcc706cSMiquel Raynal for (i = 0; i < ECCTOTAL; i++)
195*cfcc706cSMiquel Raynal ecc_code[i] = oob_data[nand_ecc_pos[i]];
196*cfcc706cSMiquel Raynal
197*cfcc706cSMiquel Raynal eccsteps = ECCSTEPS;
198*cfcc706cSMiquel Raynal p = dst;
199*cfcc706cSMiquel Raynal
200*cfcc706cSMiquel Raynal for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
201*cfcc706cSMiquel Raynal /* No chance to do something with the possible error message
202*cfcc706cSMiquel Raynal * from correct_data(). We just hope that all possible errors
203*cfcc706cSMiquel Raynal * are corrected by this routine.
204*cfcc706cSMiquel Raynal */
205*cfcc706cSMiquel Raynal this->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
206*cfcc706cSMiquel Raynal }
207*cfcc706cSMiquel Raynal
208*cfcc706cSMiquel Raynal return 0;
209*cfcc706cSMiquel Raynal }
210*cfcc706cSMiquel Raynal #endif
211*cfcc706cSMiquel Raynal
212*cfcc706cSMiquel Raynal /* nand_init() - initialize data to make nand usable by SPL */
nand_init(void)213*cfcc706cSMiquel Raynal void nand_init(void)
214*cfcc706cSMiquel Raynal {
215*cfcc706cSMiquel Raynal /*
216*cfcc706cSMiquel Raynal * Init board specific nand support
217*cfcc706cSMiquel Raynal */
218*cfcc706cSMiquel Raynal mtd = nand_to_mtd(&nand_chip);
219*cfcc706cSMiquel Raynal nand_chip.IO_ADDR_R = nand_chip.IO_ADDR_W =
220*cfcc706cSMiquel Raynal (void __iomem *)CONFIG_SYS_NAND_BASE;
221*cfcc706cSMiquel Raynal board_nand_init(&nand_chip);
222*cfcc706cSMiquel Raynal
223*cfcc706cSMiquel Raynal #ifdef CONFIG_SPL_NAND_SOFTECC
224*cfcc706cSMiquel Raynal if (nand_chip.ecc.mode == NAND_ECC_SOFT) {
225*cfcc706cSMiquel Raynal nand_chip.ecc.calculate = nand_calculate_ecc;
226*cfcc706cSMiquel Raynal nand_chip.ecc.correct = nand_correct_data;
227*cfcc706cSMiquel Raynal }
228*cfcc706cSMiquel Raynal #endif
229*cfcc706cSMiquel Raynal
230*cfcc706cSMiquel Raynal if (nand_chip.select_chip)
231*cfcc706cSMiquel Raynal nand_chip.select_chip(mtd, 0);
232*cfcc706cSMiquel Raynal }
233*cfcc706cSMiquel Raynal
234*cfcc706cSMiquel Raynal /* Unselect after operation */
nand_deselect(void)235*cfcc706cSMiquel Raynal void nand_deselect(void)
236*cfcc706cSMiquel Raynal {
237*cfcc706cSMiquel Raynal if (nand_chip.select_chip)
238*cfcc706cSMiquel Raynal nand_chip.select_chip(mtd, -1);
239*cfcc706cSMiquel Raynal }
240*cfcc706cSMiquel Raynal
241*cfcc706cSMiquel Raynal #include "nand_spl_loaders.c"
242