blob: 69d26f1f79abb89700a740c4201e116664e8fa64 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +05302/*
3 * NAND boot for Freescale Integrated Flash Controller, NAND FCM
4 *
5 * Copyright 2011 Freescale Semiconductor, Inc.
6 * Author: Dipen Dudhat <dipen.dudhat@freescale.com>
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +05307 */
8
Tom Riniabb9a042024-05-18 20:20:43 -06009#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -070010#include <cpu_func.h>
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +053011#include <asm/io.h>
York Sun37562f62013-10-22 12:39:02 -070012#include <fsl_ifc.h>
Simon Glass655306c2020-05-10 11:39:58 -060013#include <part.h>
Masahiro Yamada2b7a8732017-11-30 13:45:24 +090014#include <linux/mtd/rawnand.h>
Sumit Gargf6d96cb2016-07-14 12:27:51 -040015#ifdef CONFIG_CHAIN_OF_TRUST
16#include <fsl_validate.h>
17#endif
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +053018
19static inline int is_blank(uchar *addr, int page_size)
20{
21 int i;
22
23 for (i = 0; i < page_size; i++) {
24 if (__raw_readb(&addr[i]) != 0xff)
25 return 0;
26 }
27
28 /*
29 * For the SPL, don't worry about uncorrectable errors
30 * where the main area is all FFs but shouldn't be.
31 */
32 return 1;
33}
34
35/* returns nonzero if entire page is blank */
36static inline int check_read_ecc(uchar *buf, u32 *eccstat,
37 unsigned int bufnum, int page_size)
38{
39 u32 reg = eccstat[bufnum / 4];
40 int errors = (reg >> ((3 - bufnum % 4) * 8)) & 0xf;
41
42 if (errors == 0xf) { /* uncorrectable */
43 /* Blank pages fail hw ECC checks */
44 if (is_blank(buf, page_size))
45 return 1;
46
47 puts("ecc error\n");
48 for (;;)
49 ;
50 }
51
52 return 0;
53}
54
Jaiprakash Singhdd888062015-03-20 19:28:27 -070055static inline struct fsl_ifc_runtime *runtime_regs_address(void)
56{
Tom Rini6a5dccc2022-11-16 13:10:41 -050057 struct fsl_ifc regs = {(void *)CFG_SYS_IFC_ADDR, NULL};
Jaiprakash Singhdd888062015-03-20 19:28:27 -070058 int ver = 0;
59
60 ver = ifc_in32(&regs.gregs->ifc_rev);
61 if (ver >= FSL_IFC_V2_0_0)
Tom Rini6a5dccc2022-11-16 13:10:41 -050062 regs.rregs = (void *)CFG_SYS_IFC_ADDR + IFC_RREGS_64KOFFSET;
Jaiprakash Singhdd888062015-03-20 19:28:27 -070063 else
Tom Rini6a5dccc2022-11-16 13:10:41 -050064 regs.rregs = (void *)CFG_SYS_IFC_ADDR + IFC_RREGS_4KOFFSET;
Jaiprakash Singhdd888062015-03-20 19:28:27 -070065
66 return regs.rregs;
67}
68
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +053069static inline void nand_wait(uchar *buf, int bufnum, int page_size)
70{
Jaiprakash Singhdd888062015-03-20 19:28:27 -070071 struct fsl_ifc_runtime *ifc = runtime_regs_address();
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +053072 u32 status;
Scott Wood8e728cd2015-03-24 13:25:02 -070073 u32 eccstat[8];
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +053074 int bufperpage = page_size / 512;
75 int bufnum_end, i;
76
77 bufnum *= bufperpage;
78 bufnum_end = bufnum + bufperpage - 1;
79
80 do {
Prabhakar Kushwaha62908c22014-01-18 12:28:30 +053081 status = ifc_in32(&ifc->ifc_nand.nand_evter_stat);
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +053082 } while (!(status & IFC_NAND_EVTER_STAT_OPC));
83
84 if (status & IFC_NAND_EVTER_STAT_FTOER) {
85 puts("flash time out error\n");
86 for (;;)
87 ;
88 }
89
90 for (i = bufnum / 4; i <= bufnum_end / 4; i++)
Prabhakar Kushwaha62908c22014-01-18 12:28:30 +053091 eccstat[i] = ifc_in32(&ifc->ifc_nand.nand_eccstat[i]);
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +053092
93 for (i = bufnum; i <= bufnum_end; i++) {
94 if (check_read_ecc(buf, eccstat, i, page_size))
95 break;
96 }
97
Prabhakar Kushwaha62908c22014-01-18 12:28:30 +053098 ifc_out32(&ifc->ifc_nand.nand_evter_stat, status);
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +053099}
100
101static inline int bad_block(uchar *marker, int port_size)
102{
103 if (port_size == 8)
104 return __raw_readb(marker) != 0xff;
105 else
106 return __raw_readw((u16 *)marker) != 0xffff;
107}
108
Sean Anderson8805f9d2023-11-04 16:37:44 -0400109static int saved_page_size;
110
Po Liuf6facca2014-01-10 10:10:58 +0800111int nand_spl_load_image(uint32_t offs, unsigned int uboot_size, void *vdst)
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +0530112{
Tom Rini6a5dccc2022-11-16 13:10:41 -0500113 struct fsl_ifc_fcm *gregs = (void *)CFG_SYS_IFC_ADDR;
Jaiprakash Singhdd888062015-03-20 19:28:27 -0700114 struct fsl_ifc_runtime *ifc = NULL;
Tom Rinib4213492022-11-12 17:36:51 -0500115 uchar *buf = (uchar *)CFG_SYS_NAND_BASE;
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +0530116 int page_size;
117 int port_size;
118 int pages_per_blk;
119 int blk_size;
120 int bad_marker = 0;
Prabhakar Kushwaha5c23a822014-06-14 08:48:19 +0530121 int bufnum_mask, bufnum, ver = 0;
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +0530122
123 int csor, cspr;
124 int pos = 0;
125 int j = 0;
126
127 int sram_addr;
128 int pg_no;
Po Liuf6facca2014-01-10 10:10:58 +0800129 uchar *dst = vdst;
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +0530130
Jaiprakash Singhdd888062015-03-20 19:28:27 -0700131 ifc = runtime_regs_address();
132
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +0530133 /* Get NAND Flash configuration */
Tom Rinib4213492022-11-12 17:36:51 -0500134 csor = CFG_SYS_NAND_CSOR;
135 cspr = CFG_SYS_NAND_CSPR;
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +0530136
137 port_size = (cspr & CSPR_PORT_SIZE_16) ? 16 : 8;
138
Prabhakar Kushwahaa3aaf1d2013-10-04 10:05:36 +0530139 if ((csor & CSOR_NAND_PGS_MASK) == CSOR_NAND_PGS_8K) {
140 page_size = 8192;
141 bufnum_mask = 0x0;
142 } else if ((csor & CSOR_NAND_PGS_MASK) == CSOR_NAND_PGS_4K) {
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +0530143 page_size = 4096;
144 bufnum_mask = 0x1;
Prabhakar Kushwahaa3aaf1d2013-10-04 10:05:36 +0530145 } else if ((csor & CSOR_NAND_PGS_MASK) == CSOR_NAND_PGS_2K) {
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +0530146 page_size = 2048;
147 bufnum_mask = 0x3;
148 } else {
149 page_size = 512;
150 bufnum_mask = 0xf;
151
152 if (port_size == 8)
153 bad_marker = 5;
154 }
Sean Anderson8805f9d2023-11-04 16:37:44 -0400155 saved_page_size = page_size;
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +0530156
Jaiprakash Singhdd888062015-03-20 19:28:27 -0700157 ver = ifc_in32(&gregs->ifc_rev);
Prabhakar Kushwaha5c23a822014-06-14 08:48:19 +0530158 if (ver >= FSL_IFC_V2_0_0)
159 bufnum_mask = (bufnum_mask * 2) + 1;
160
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +0530161 pages_per_blk =
162 32 << ((csor & CSOR_NAND_PB_MASK) >> CSOR_NAND_PB_SHIFT);
163
164 blk_size = pages_per_blk * page_size;
165
166 /* Open Full SRAM mapping for spare are access */
Prabhakar Kushwaha62908c22014-01-18 12:28:30 +0530167 ifc_out32(&ifc->ifc_nand.ncfgr, 0x0);
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +0530168
169 /* Clear Boot events */
Prabhakar Kushwaha62908c22014-01-18 12:28:30 +0530170 ifc_out32(&ifc->ifc_nand.nand_evter_stat, 0xffffffff);
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +0530171
172 /* Program FIR/FCR for Large/Small page */
173 if (page_size > 512) {
Prabhakar Kushwaha62908c22014-01-18 12:28:30 +0530174 ifc_out32(&ifc->ifc_nand.nand_fir0,
175 (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
176 (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
177 (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
178 (IFC_FIR_OP_CMD1 << IFC_NAND_FIR0_OP3_SHIFT) |
179 (IFC_FIR_OP_BTRD << IFC_NAND_FIR0_OP4_SHIFT));
180 ifc_out32(&ifc->ifc_nand.nand_fir1, 0x0);
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +0530181
Prabhakar Kushwaha62908c22014-01-18 12:28:30 +0530182 ifc_out32(&ifc->ifc_nand.nand_fcr0,
183 (NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT) |
184 (NAND_CMD_READSTART << IFC_NAND_FCR0_CMD1_SHIFT));
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +0530185 } else {
Prabhakar Kushwaha62908c22014-01-18 12:28:30 +0530186 ifc_out32(&ifc->ifc_nand.nand_fir0,
187 (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
188 (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
189 (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
190 (IFC_FIR_OP_BTRD << IFC_NAND_FIR0_OP3_SHIFT));
191 ifc_out32(&ifc->ifc_nand.nand_fir1, 0x0);
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +0530192
Prabhakar Kushwaha62908c22014-01-18 12:28:30 +0530193 ifc_out32(&ifc->ifc_nand.nand_fcr0,
194 NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT);
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +0530195 }
196
197 /* Program FBCR = 0 for full page read */
Prabhakar Kushwaha62908c22014-01-18 12:28:30 +0530198 ifc_out32(&ifc->ifc_nand.nand_fbcr, 0);
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +0530199
200 /* Read and copy u-boot on SDRAM from NAND device, In parallel
201 * check for Bad block if found skip it and read continue to
202 * next Block
203 */
204 while (pos < uboot_size) {
205 int i = 0;
206 do {
207 pg_no = offs / page_size;
208 bufnum = pg_no & bufnum_mask;
209 sram_addr = bufnum * page_size * 2;
210
Prabhakar Kushwaha62908c22014-01-18 12:28:30 +0530211 ifc_out32(&ifc->ifc_nand.row0, pg_no);
212 ifc_out32(&ifc->ifc_nand.col0, 0);
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +0530213 /* start read */
Prabhakar Kushwaha62908c22014-01-18 12:28:30 +0530214 ifc_out32(&ifc->ifc_nand.nandseq_strt,
215 IFC_NAND_SEQ_STRT_FIR_STRT);
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +0530216
217 /* wait for read to complete */
218 nand_wait(&buf[sram_addr], bufnum, page_size);
219
220 /*
221 * If either of the first two pages are marked bad,
222 * continue to the next block.
223 */
224 if (i++ < 2 &&
225 bad_block(&buf[sram_addr + page_size + bad_marker],
226 port_size)) {
227 puts("skipping\n");
228 offs = (offs + blk_size) & ~(blk_size - 1);
229 pos &= ~(blk_size - 1);
230 break;
231 }
232
233 for (j = 0; j < page_size; j++)
234 dst[pos + j] = __raw_readb(&buf[sram_addr + j]);
235
236 pos += page_size;
237 offs += page_size;
238 } while ((offs & (blk_size - 1)) && (pos < uboot_size));
239 }
Po Liuf6facca2014-01-10 10:10:58 +0800240
241 return 0;
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +0530242}
243
244/*
245 * Main entrypoint for NAND Boot. It's necessary that SDRAM is already
Bin Meng75574052016-02-05 19:30:11 -0800246 * configured and available since this code loads the main U-Boot image
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +0530247 * from NAND into SDRAM and starts from there.
248 */
249void nand_boot(void)
250{
251 __attribute__((noreturn)) void (*uboot)(void);
252 /*
253 * Load U-Boot image from NAND into RAM
254 */
Po Liuf6facca2014-01-10 10:10:58 +0800255 nand_spl_load_image(CONFIG_SYS_NAND_U_BOOT_OFFS,
Tom Rinib4213492022-11-12 17:36:51 -0500256 CFG_SYS_NAND_U_BOOT_SIZE,
257 (uchar *)CFG_SYS_NAND_U_BOOT_DST);
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +0530258
259#ifdef CONFIG_NAND_ENV_DST
Po Liuf6facca2014-01-10 10:10:58 +0800260 nand_spl_load_image(CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
261 (uchar *)CONFIG_NAND_ENV_DST);
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +0530262
263#ifdef CONFIG_ENV_OFFSET_REDUND
Po Liuf6facca2014-01-10 10:10:58 +0800264 nand_spl_load_image(CONFIG_ENV_OFFSET_REDUND, CONFIG_ENV_SIZE,
265 (uchar *)CONFIG_NAND_ENV_DST + CONFIG_ENV_SIZE);
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +0530266#endif
267#endif
268 /*
269 * Jump to U-Boot image
270 */
271#ifdef CONFIG_SPL_FLUSH_IMAGE
272 /*
273 * Clean d-cache and invalidate i-cache, to
274 * make sure that no stale data is executed.
275 */
Tom Rinib4213492022-11-12 17:36:51 -0500276 flush_cache(CFG_SYS_NAND_U_BOOT_DST, CFG_SYS_NAND_U_BOOT_SIZE);
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +0530277#endif
Sumit Gargf6d96cb2016-07-14 12:27:51 -0400278
279#ifdef CONFIG_CHAIN_OF_TRUST
280 /*
Michal Simek50fa1182023-05-17 09:17:16 +0200281 * U-Boot header is appended at end of U-Boot image, so
282 * calculate U-Boot header address using U-Boot header size.
Sumit Gargf6d96cb2016-07-14 12:27:51 -0400283 */
Tom Rinicb189262022-12-02 16:42:50 -0500284#define FSL_U_BOOT_HDR_ADDR \
Tom Rinib4213492022-11-12 17:36:51 -0500285 ((CFG_SYS_NAND_U_BOOT_START + \
286 CFG_SYS_NAND_U_BOOT_SIZE) - \
Tom Rinicb189262022-12-02 16:42:50 -0500287 FSL_U_BOOT_HDR_SIZE)
288 spl_validate_uboot(FSL_U_BOOT_HDR_ADDR,
Tom Rinib4213492022-11-12 17:36:51 -0500289 CFG_SYS_NAND_U_BOOT_START);
Sumit Gargf6d96cb2016-07-14 12:27:51 -0400290 /*
291 * In case of failure in validation, spl_validate_uboot would
292 * not return back in case of Production environment with ITS=1.
293 * Thus U-Boot will not start.
294 * In Development environment (ITS=0 and SB_EN=1), the function
295 * may return back in case of non-fatal failures.
296 */
297#endif
298
Tom Rinib4213492022-11-12 17:36:51 -0500299 uboot = (void *)CFG_SYS_NAND_U_BOOT_START;
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +0530300 uboot();
301}
Alison Wangab98bb52014-12-09 17:38:14 +0800302
Tom Rini1a9f23d2022-05-26 14:31:57 -0400303#ifndef CONFIG_TPL_NAND_INIT
Alison Wangab98bb52014-12-09 17:38:14 +0800304void nand_init(void)
305{
Sean Anderson8805f9d2023-11-04 16:37:44 -0400306}
307
308unsigned int nand_page_size(void)
309{
310 return saved_page_size;
Alison Wangab98bb52014-12-09 17:38:14 +0800311}
312
313void nand_deselect(void)
314{
315}
316#endif