1*cfcc706cSMiquel Raynal /*
2*cfcc706cSMiquel Raynal * NAND boot for Freescale Integrated Flash Controller, NAND FCM
3*cfcc706cSMiquel Raynal *
4*cfcc706cSMiquel Raynal * Copyright 2011 Freescale Semiconductor, Inc.
5*cfcc706cSMiquel Raynal * Author: Dipen Dudhat <dipen.dudhat@freescale.com>
6*cfcc706cSMiquel Raynal *
7*cfcc706cSMiquel Raynal * SPDX-License-Identifier: GPL-2.0+
8*cfcc706cSMiquel Raynal */
9*cfcc706cSMiquel Raynal
10*cfcc706cSMiquel Raynal #include <common.h>
11*cfcc706cSMiquel Raynal #include <asm/io.h>
12*cfcc706cSMiquel Raynal #include <fsl_ifc.h>
13*cfcc706cSMiquel Raynal #include <linux/mtd/rawnand.h>
14*cfcc706cSMiquel Raynal #ifdef CONFIG_CHAIN_OF_TRUST
15*cfcc706cSMiquel Raynal #include <fsl_validate.h>
16*cfcc706cSMiquel Raynal #endif
17*cfcc706cSMiquel Raynal
is_blank(uchar * addr,int page_size)18*cfcc706cSMiquel Raynal static inline int is_blank(uchar *addr, int page_size)
19*cfcc706cSMiquel Raynal {
20*cfcc706cSMiquel Raynal int i;
21*cfcc706cSMiquel Raynal
22*cfcc706cSMiquel Raynal for (i = 0; i < page_size; i++) {
23*cfcc706cSMiquel Raynal if (__raw_readb(&addr[i]) != 0xff)
24*cfcc706cSMiquel Raynal return 0;
25*cfcc706cSMiquel Raynal }
26*cfcc706cSMiquel Raynal
27*cfcc706cSMiquel Raynal /*
28*cfcc706cSMiquel Raynal * For the SPL, don't worry about uncorrectable errors
29*cfcc706cSMiquel Raynal * where the main area is all FFs but shouldn't be.
30*cfcc706cSMiquel Raynal */
31*cfcc706cSMiquel Raynal return 1;
32*cfcc706cSMiquel Raynal }
33*cfcc706cSMiquel Raynal
34*cfcc706cSMiquel Raynal /* returns nonzero if entire page is blank */
check_read_ecc(uchar * buf,u32 * eccstat,unsigned int bufnum,int page_size)35*cfcc706cSMiquel Raynal static inline int check_read_ecc(uchar *buf, u32 *eccstat,
36*cfcc706cSMiquel Raynal unsigned int bufnum, int page_size)
37*cfcc706cSMiquel Raynal {
38*cfcc706cSMiquel Raynal u32 reg = eccstat[bufnum / 4];
39*cfcc706cSMiquel Raynal int errors = (reg >> ((3 - bufnum % 4) * 8)) & 0xf;
40*cfcc706cSMiquel Raynal
41*cfcc706cSMiquel Raynal if (errors == 0xf) { /* uncorrectable */
42*cfcc706cSMiquel Raynal /* Blank pages fail hw ECC checks */
43*cfcc706cSMiquel Raynal if (is_blank(buf, page_size))
44*cfcc706cSMiquel Raynal return 1;
45*cfcc706cSMiquel Raynal
46*cfcc706cSMiquel Raynal puts("ecc error\n");
47*cfcc706cSMiquel Raynal for (;;)
48*cfcc706cSMiquel Raynal ;
49*cfcc706cSMiquel Raynal }
50*cfcc706cSMiquel Raynal
51*cfcc706cSMiquel Raynal return 0;
52*cfcc706cSMiquel Raynal }
53*cfcc706cSMiquel Raynal
runtime_regs_address(void)54*cfcc706cSMiquel Raynal static inline struct fsl_ifc_runtime *runtime_regs_address(void)
55*cfcc706cSMiquel Raynal {
56*cfcc706cSMiquel Raynal struct fsl_ifc regs = {(void *)CONFIG_SYS_IFC_ADDR, NULL};
57*cfcc706cSMiquel Raynal int ver = 0;
58*cfcc706cSMiquel Raynal
59*cfcc706cSMiquel Raynal ver = ifc_in32(®s.gregs->ifc_rev);
60*cfcc706cSMiquel Raynal if (ver >= FSL_IFC_V2_0_0)
61*cfcc706cSMiquel Raynal regs.rregs = (void *)CONFIG_SYS_IFC_ADDR + IFC_RREGS_64KOFFSET;
62*cfcc706cSMiquel Raynal else
63*cfcc706cSMiquel Raynal regs.rregs = (void *)CONFIG_SYS_IFC_ADDR + IFC_RREGS_4KOFFSET;
64*cfcc706cSMiquel Raynal
65*cfcc706cSMiquel Raynal return regs.rregs;
66*cfcc706cSMiquel Raynal }
67*cfcc706cSMiquel Raynal
nand_wait(uchar * buf,int bufnum,int page_size)68*cfcc706cSMiquel Raynal static inline void nand_wait(uchar *buf, int bufnum, int page_size)
69*cfcc706cSMiquel Raynal {
70*cfcc706cSMiquel Raynal struct fsl_ifc_runtime *ifc = runtime_regs_address();
71*cfcc706cSMiquel Raynal u32 status;
72*cfcc706cSMiquel Raynal u32 eccstat[8];
73*cfcc706cSMiquel Raynal int bufperpage = page_size / 512;
74*cfcc706cSMiquel Raynal int bufnum_end, i;
75*cfcc706cSMiquel Raynal
76*cfcc706cSMiquel Raynal bufnum *= bufperpage;
77*cfcc706cSMiquel Raynal bufnum_end = bufnum + bufperpage - 1;
78*cfcc706cSMiquel Raynal
79*cfcc706cSMiquel Raynal do {
80*cfcc706cSMiquel Raynal status = ifc_in32(&ifc->ifc_nand.nand_evter_stat);
81*cfcc706cSMiquel Raynal } while (!(status & IFC_NAND_EVTER_STAT_OPC));
82*cfcc706cSMiquel Raynal
83*cfcc706cSMiquel Raynal if (status & IFC_NAND_EVTER_STAT_FTOER) {
84*cfcc706cSMiquel Raynal puts("flash time out error\n");
85*cfcc706cSMiquel Raynal for (;;)
86*cfcc706cSMiquel Raynal ;
87*cfcc706cSMiquel Raynal }
88*cfcc706cSMiquel Raynal
89*cfcc706cSMiquel Raynal for (i = bufnum / 4; i <= bufnum_end / 4; i++)
90*cfcc706cSMiquel Raynal eccstat[i] = ifc_in32(&ifc->ifc_nand.nand_eccstat[i]);
91*cfcc706cSMiquel Raynal
92*cfcc706cSMiquel Raynal for (i = bufnum; i <= bufnum_end; i++) {
93*cfcc706cSMiquel Raynal if (check_read_ecc(buf, eccstat, i, page_size))
94*cfcc706cSMiquel Raynal break;
95*cfcc706cSMiquel Raynal }
96*cfcc706cSMiquel Raynal
97*cfcc706cSMiquel Raynal ifc_out32(&ifc->ifc_nand.nand_evter_stat, status);
98*cfcc706cSMiquel Raynal }
99*cfcc706cSMiquel Raynal
bad_block(uchar * marker,int port_size)100*cfcc706cSMiquel Raynal static inline int bad_block(uchar *marker, int port_size)
101*cfcc706cSMiquel Raynal {
102*cfcc706cSMiquel Raynal if (port_size == 8)
103*cfcc706cSMiquel Raynal return __raw_readb(marker) != 0xff;
104*cfcc706cSMiquel Raynal else
105*cfcc706cSMiquel Raynal return __raw_readw((u16 *)marker) != 0xffff;
106*cfcc706cSMiquel Raynal }
107*cfcc706cSMiquel Raynal
nand_spl_load_image(uint32_t offs,unsigned int uboot_size,void * vdst)108*cfcc706cSMiquel Raynal int nand_spl_load_image(uint32_t offs, unsigned int uboot_size, void *vdst)
109*cfcc706cSMiquel Raynal {
110*cfcc706cSMiquel Raynal struct fsl_ifc_fcm *gregs = (void *)CONFIG_SYS_IFC_ADDR;
111*cfcc706cSMiquel Raynal struct fsl_ifc_runtime *ifc = NULL;
112*cfcc706cSMiquel Raynal uchar *buf = (uchar *)CONFIG_SYS_NAND_BASE;
113*cfcc706cSMiquel Raynal int page_size;
114*cfcc706cSMiquel Raynal int port_size;
115*cfcc706cSMiquel Raynal int pages_per_blk;
116*cfcc706cSMiquel Raynal int blk_size;
117*cfcc706cSMiquel Raynal int bad_marker = 0;
118*cfcc706cSMiquel Raynal int bufnum_mask, bufnum, ver = 0;
119*cfcc706cSMiquel Raynal
120*cfcc706cSMiquel Raynal int csor, cspr;
121*cfcc706cSMiquel Raynal int pos = 0;
122*cfcc706cSMiquel Raynal int j = 0;
123*cfcc706cSMiquel Raynal
124*cfcc706cSMiquel Raynal int sram_addr;
125*cfcc706cSMiquel Raynal int pg_no;
126*cfcc706cSMiquel Raynal uchar *dst = vdst;
127*cfcc706cSMiquel Raynal
128*cfcc706cSMiquel Raynal ifc = runtime_regs_address();
129*cfcc706cSMiquel Raynal
130*cfcc706cSMiquel Raynal /* Get NAND Flash configuration */
131*cfcc706cSMiquel Raynal csor = CONFIG_SYS_NAND_CSOR;
132*cfcc706cSMiquel Raynal cspr = CONFIG_SYS_NAND_CSPR;
133*cfcc706cSMiquel Raynal
134*cfcc706cSMiquel Raynal port_size = (cspr & CSPR_PORT_SIZE_16) ? 16 : 8;
135*cfcc706cSMiquel Raynal
136*cfcc706cSMiquel Raynal if ((csor & CSOR_NAND_PGS_MASK) == CSOR_NAND_PGS_8K) {
137*cfcc706cSMiquel Raynal page_size = 8192;
138*cfcc706cSMiquel Raynal bufnum_mask = 0x0;
139*cfcc706cSMiquel Raynal } else if ((csor & CSOR_NAND_PGS_MASK) == CSOR_NAND_PGS_4K) {
140*cfcc706cSMiquel Raynal page_size = 4096;
141*cfcc706cSMiquel Raynal bufnum_mask = 0x1;
142*cfcc706cSMiquel Raynal } else if ((csor & CSOR_NAND_PGS_MASK) == CSOR_NAND_PGS_2K) {
143*cfcc706cSMiquel Raynal page_size = 2048;
144*cfcc706cSMiquel Raynal bufnum_mask = 0x3;
145*cfcc706cSMiquel Raynal } else {
146*cfcc706cSMiquel Raynal page_size = 512;
147*cfcc706cSMiquel Raynal bufnum_mask = 0xf;
148*cfcc706cSMiquel Raynal
149*cfcc706cSMiquel Raynal if (port_size == 8)
150*cfcc706cSMiquel Raynal bad_marker = 5;
151*cfcc706cSMiquel Raynal }
152*cfcc706cSMiquel Raynal
153*cfcc706cSMiquel Raynal ver = ifc_in32(&gregs->ifc_rev);
154*cfcc706cSMiquel Raynal if (ver >= FSL_IFC_V2_0_0)
155*cfcc706cSMiquel Raynal bufnum_mask = (bufnum_mask * 2) + 1;
156*cfcc706cSMiquel Raynal
157*cfcc706cSMiquel Raynal pages_per_blk =
158*cfcc706cSMiquel Raynal 32 << ((csor & CSOR_NAND_PB_MASK) >> CSOR_NAND_PB_SHIFT);
159*cfcc706cSMiquel Raynal
160*cfcc706cSMiquel Raynal blk_size = pages_per_blk * page_size;
161*cfcc706cSMiquel Raynal
162*cfcc706cSMiquel Raynal /* Open Full SRAM mapping for spare are access */
163*cfcc706cSMiquel Raynal ifc_out32(&ifc->ifc_nand.ncfgr, 0x0);
164*cfcc706cSMiquel Raynal
165*cfcc706cSMiquel Raynal /* Clear Boot events */
166*cfcc706cSMiquel Raynal ifc_out32(&ifc->ifc_nand.nand_evter_stat, 0xffffffff);
167*cfcc706cSMiquel Raynal
168*cfcc706cSMiquel Raynal /* Program FIR/FCR for Large/Small page */
169*cfcc706cSMiquel Raynal if (page_size > 512) {
170*cfcc706cSMiquel Raynal ifc_out32(&ifc->ifc_nand.nand_fir0,
171*cfcc706cSMiquel Raynal (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
172*cfcc706cSMiquel Raynal (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
173*cfcc706cSMiquel Raynal (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
174*cfcc706cSMiquel Raynal (IFC_FIR_OP_CMD1 << IFC_NAND_FIR0_OP3_SHIFT) |
175*cfcc706cSMiquel Raynal (IFC_FIR_OP_BTRD << IFC_NAND_FIR0_OP4_SHIFT));
176*cfcc706cSMiquel Raynal ifc_out32(&ifc->ifc_nand.nand_fir1, 0x0);
177*cfcc706cSMiquel Raynal
178*cfcc706cSMiquel Raynal ifc_out32(&ifc->ifc_nand.nand_fcr0,
179*cfcc706cSMiquel Raynal (NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT) |
180*cfcc706cSMiquel Raynal (NAND_CMD_READSTART << IFC_NAND_FCR0_CMD1_SHIFT));
181*cfcc706cSMiquel Raynal } else {
182*cfcc706cSMiquel Raynal ifc_out32(&ifc->ifc_nand.nand_fir0,
183*cfcc706cSMiquel Raynal (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
184*cfcc706cSMiquel Raynal (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
185*cfcc706cSMiquel Raynal (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
186*cfcc706cSMiquel Raynal (IFC_FIR_OP_BTRD << IFC_NAND_FIR0_OP3_SHIFT));
187*cfcc706cSMiquel Raynal ifc_out32(&ifc->ifc_nand.nand_fir1, 0x0);
188*cfcc706cSMiquel Raynal
189*cfcc706cSMiquel Raynal ifc_out32(&ifc->ifc_nand.nand_fcr0,
190*cfcc706cSMiquel Raynal NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT);
191*cfcc706cSMiquel Raynal }
192*cfcc706cSMiquel Raynal
193*cfcc706cSMiquel Raynal /* Program FBCR = 0 for full page read */
194*cfcc706cSMiquel Raynal ifc_out32(&ifc->ifc_nand.nand_fbcr, 0);
195*cfcc706cSMiquel Raynal
196*cfcc706cSMiquel Raynal /* Read and copy u-boot on SDRAM from NAND device, In parallel
197*cfcc706cSMiquel Raynal * check for Bad block if found skip it and read continue to
198*cfcc706cSMiquel Raynal * next Block
199*cfcc706cSMiquel Raynal */
200*cfcc706cSMiquel Raynal while (pos < uboot_size) {
201*cfcc706cSMiquel Raynal int i = 0;
202*cfcc706cSMiquel Raynal do {
203*cfcc706cSMiquel Raynal pg_no = offs / page_size;
204*cfcc706cSMiquel Raynal bufnum = pg_no & bufnum_mask;
205*cfcc706cSMiquel Raynal sram_addr = bufnum * page_size * 2;
206*cfcc706cSMiquel Raynal
207*cfcc706cSMiquel Raynal ifc_out32(&ifc->ifc_nand.row0, pg_no);
208*cfcc706cSMiquel Raynal ifc_out32(&ifc->ifc_nand.col0, 0);
209*cfcc706cSMiquel Raynal /* start read */
210*cfcc706cSMiquel Raynal ifc_out32(&ifc->ifc_nand.nandseq_strt,
211*cfcc706cSMiquel Raynal IFC_NAND_SEQ_STRT_FIR_STRT);
212*cfcc706cSMiquel Raynal
213*cfcc706cSMiquel Raynal /* wait for read to complete */
214*cfcc706cSMiquel Raynal nand_wait(&buf[sram_addr], bufnum, page_size);
215*cfcc706cSMiquel Raynal
216*cfcc706cSMiquel Raynal /*
217*cfcc706cSMiquel Raynal * If either of the first two pages are marked bad,
218*cfcc706cSMiquel Raynal * continue to the next block.
219*cfcc706cSMiquel Raynal */
220*cfcc706cSMiquel Raynal if (i++ < 2 &&
221*cfcc706cSMiquel Raynal bad_block(&buf[sram_addr + page_size + bad_marker],
222*cfcc706cSMiquel Raynal port_size)) {
223*cfcc706cSMiquel Raynal puts("skipping\n");
224*cfcc706cSMiquel Raynal offs = (offs + blk_size) & ~(blk_size - 1);
225*cfcc706cSMiquel Raynal pos &= ~(blk_size - 1);
226*cfcc706cSMiquel Raynal break;
227*cfcc706cSMiquel Raynal }
228*cfcc706cSMiquel Raynal
229*cfcc706cSMiquel Raynal for (j = 0; j < page_size; j++)
230*cfcc706cSMiquel Raynal dst[pos + j] = __raw_readb(&buf[sram_addr + j]);
231*cfcc706cSMiquel Raynal
232*cfcc706cSMiquel Raynal pos += page_size;
233*cfcc706cSMiquel Raynal offs += page_size;
234*cfcc706cSMiquel Raynal } while ((offs & (blk_size - 1)) && (pos < uboot_size));
235*cfcc706cSMiquel Raynal }
236*cfcc706cSMiquel Raynal
237*cfcc706cSMiquel Raynal return 0;
238*cfcc706cSMiquel Raynal }
239*cfcc706cSMiquel Raynal
240*cfcc706cSMiquel Raynal /*
241*cfcc706cSMiquel Raynal * Main entrypoint for NAND Boot. It's necessary that SDRAM is already
242*cfcc706cSMiquel Raynal * configured and available since this code loads the main U-Boot image
243*cfcc706cSMiquel Raynal * from NAND into SDRAM and starts from there.
244*cfcc706cSMiquel Raynal */
nand_boot(void)245*cfcc706cSMiquel Raynal void nand_boot(void)
246*cfcc706cSMiquel Raynal {
247*cfcc706cSMiquel Raynal __attribute__((noreturn)) void (*uboot)(void);
248*cfcc706cSMiquel Raynal /*
249*cfcc706cSMiquel Raynal * Load U-Boot image from NAND into RAM
250*cfcc706cSMiquel Raynal */
251*cfcc706cSMiquel Raynal nand_spl_load_image(CONFIG_SYS_NAND_U_BOOT_OFFS,
252*cfcc706cSMiquel Raynal CONFIG_SYS_NAND_U_BOOT_SIZE,
253*cfcc706cSMiquel Raynal (uchar *)CONFIG_SYS_NAND_U_BOOT_DST);
254*cfcc706cSMiquel Raynal
255*cfcc706cSMiquel Raynal #ifdef CONFIG_NAND_ENV_DST
256*cfcc706cSMiquel Raynal nand_spl_load_image(CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
257*cfcc706cSMiquel Raynal (uchar *)CONFIG_NAND_ENV_DST);
258*cfcc706cSMiquel Raynal
259*cfcc706cSMiquel Raynal #ifdef CONFIG_ENV_OFFSET_REDUND
260*cfcc706cSMiquel Raynal nand_spl_load_image(CONFIG_ENV_OFFSET_REDUND, CONFIG_ENV_SIZE,
261*cfcc706cSMiquel Raynal (uchar *)CONFIG_NAND_ENV_DST + CONFIG_ENV_SIZE);
262*cfcc706cSMiquel Raynal #endif
263*cfcc706cSMiquel Raynal #endif
264*cfcc706cSMiquel Raynal /*
265*cfcc706cSMiquel Raynal * Jump to U-Boot image
266*cfcc706cSMiquel Raynal */
267*cfcc706cSMiquel Raynal #ifdef CONFIG_SPL_FLUSH_IMAGE
268*cfcc706cSMiquel Raynal /*
269*cfcc706cSMiquel Raynal * Clean d-cache and invalidate i-cache, to
270*cfcc706cSMiquel Raynal * make sure that no stale data is executed.
271*cfcc706cSMiquel Raynal */
272*cfcc706cSMiquel Raynal flush_cache(CONFIG_SYS_NAND_U_BOOT_DST, CONFIG_SYS_NAND_U_BOOT_SIZE);
273*cfcc706cSMiquel Raynal #endif
274*cfcc706cSMiquel Raynal
275*cfcc706cSMiquel Raynal #ifdef CONFIG_CHAIN_OF_TRUST
276*cfcc706cSMiquel Raynal /*
277*cfcc706cSMiquel Raynal * U-Boot header is appended at end of U-boot image, so
278*cfcc706cSMiquel Raynal * calculate U-boot header address using U-boot header size.
279*cfcc706cSMiquel Raynal */
280*cfcc706cSMiquel Raynal #define CONFIG_U_BOOT_HDR_ADDR \
281*cfcc706cSMiquel Raynal ((CONFIG_SYS_NAND_U_BOOT_START + \
282*cfcc706cSMiquel Raynal CONFIG_SYS_NAND_U_BOOT_SIZE) - \
283*cfcc706cSMiquel Raynal CONFIG_U_BOOT_HDR_SIZE)
284*cfcc706cSMiquel Raynal spl_validate_uboot(CONFIG_U_BOOT_HDR_ADDR,
285*cfcc706cSMiquel Raynal CONFIG_SYS_NAND_U_BOOT_START);
286*cfcc706cSMiquel Raynal /*
287*cfcc706cSMiquel Raynal * In case of failure in validation, spl_validate_uboot would
288*cfcc706cSMiquel Raynal * not return back in case of Production environment with ITS=1.
289*cfcc706cSMiquel Raynal * Thus U-Boot will not start.
290*cfcc706cSMiquel Raynal * In Development environment (ITS=0 and SB_EN=1), the function
291*cfcc706cSMiquel Raynal * may return back in case of non-fatal failures.
292*cfcc706cSMiquel Raynal */
293*cfcc706cSMiquel Raynal #endif
294*cfcc706cSMiquel Raynal
295*cfcc706cSMiquel Raynal uboot = (void *)CONFIG_SYS_NAND_U_BOOT_START;
296*cfcc706cSMiquel Raynal uboot();
297*cfcc706cSMiquel Raynal }
298*cfcc706cSMiquel Raynal
299*cfcc706cSMiquel Raynal #ifndef CONFIG_SPL_NAND_INIT
nand_init(void)300*cfcc706cSMiquel Raynal void nand_init(void)
301*cfcc706cSMiquel Raynal {
302*cfcc706cSMiquel Raynal }
303*cfcc706cSMiquel Raynal
nand_deselect(void)304*cfcc706cSMiquel Raynal void nand_deselect(void)
305*cfcc706cSMiquel Raynal {
306*cfcc706cSMiquel Raynal }
307*cfcc706cSMiquel Raynal #endif
308