blob: 9fa5ccbc562651a2e10a0010b8898815a82ba890 [file] [log] [blame]
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +05301/*
2 * NAND boot for Freescale Integrated Flash Controller, NAND FCM
3 *
4 * Copyright 2011 Freescale Semiconductor, Inc.
5 * Author: Dipen Dudhat <dipen.dudhat@freescale.com>
6 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +05308 */
9
10#include <common.h>
11#include <asm/io.h>
12#include <asm/fsl_ifc.h>
13#include <linux/mtd/nand.h>
14
15static inline int is_blank(uchar *addr, int page_size)
16{
17 int i;
18
19 for (i = 0; i < page_size; i++) {
20 if (__raw_readb(&addr[i]) != 0xff)
21 return 0;
22 }
23
24 /*
25 * For the SPL, don't worry about uncorrectable errors
26 * where the main area is all FFs but shouldn't be.
27 */
28 return 1;
29}
30
31/* returns nonzero if entire page is blank */
32static inline int check_read_ecc(uchar *buf, u32 *eccstat,
33 unsigned int bufnum, int page_size)
34{
35 u32 reg = eccstat[bufnum / 4];
36 int errors = (reg >> ((3 - bufnum % 4) * 8)) & 0xf;
37
38 if (errors == 0xf) { /* uncorrectable */
39 /* Blank pages fail hw ECC checks */
40 if (is_blank(buf, page_size))
41 return 1;
42
43 puts("ecc error\n");
44 for (;;)
45 ;
46 }
47
48 return 0;
49}
50
51static inline void nand_wait(uchar *buf, int bufnum, int page_size)
52{
53 struct fsl_ifc *ifc = IFC_BASE_ADDR;
54 u32 status;
55 u32 eccstat[4];
56 int bufperpage = page_size / 512;
57 int bufnum_end, i;
58
59 bufnum *= bufperpage;
60 bufnum_end = bufnum + bufperpage - 1;
61
62 do {
63 status = in_be32(&ifc->ifc_nand.nand_evter_stat);
64 } while (!(status & IFC_NAND_EVTER_STAT_OPC));
65
66 if (status & IFC_NAND_EVTER_STAT_FTOER) {
67 puts("flash time out error\n");
68 for (;;)
69 ;
70 }
71
72 for (i = bufnum / 4; i <= bufnum_end / 4; i++)
73 eccstat[i] = in_be32(&ifc->ifc_nand.nand_eccstat[i]);
74
75 for (i = bufnum; i <= bufnum_end; i++) {
76 if (check_read_ecc(buf, eccstat, i, page_size))
77 break;
78 }
79
80 out_be32(&ifc->ifc_nand.nand_evter_stat, status);
81}
82
83static inline int bad_block(uchar *marker, int port_size)
84{
85 if (port_size == 8)
86 return __raw_readb(marker) != 0xff;
87 else
88 return __raw_readw((u16 *)marker) != 0xffff;
89}
90
91static void nand_load(unsigned int offs, int uboot_size, uchar *dst)
92{
93 struct fsl_ifc *ifc = IFC_BASE_ADDR;
94 uchar *buf = (uchar *)CONFIG_SYS_NAND_BASE;
95 int page_size;
96 int port_size;
97 int pages_per_blk;
98 int blk_size;
99 int bad_marker = 0;
100 int bufnum_mask, bufnum;
101
102 int csor, cspr;
103 int pos = 0;
104 int j = 0;
105
106 int sram_addr;
107 int pg_no;
108
109 /* Get NAND Flash configuration */
110 csor = CONFIG_SYS_NAND_CSOR;
111 cspr = CONFIG_SYS_NAND_CSPR;
112
113 port_size = (cspr & CSPR_PORT_SIZE_16) ? 16 : 8;
114
Prabhakar Kushwahaa3aaf1d2013-10-04 10:05:36 +0530115 if ((csor & CSOR_NAND_PGS_MASK) == CSOR_NAND_PGS_8K) {
116 page_size = 8192;
117 bufnum_mask = 0x0;
118 } else if ((csor & CSOR_NAND_PGS_MASK) == CSOR_NAND_PGS_4K) {
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +0530119 page_size = 4096;
120 bufnum_mask = 0x1;
Prabhakar Kushwahaa3aaf1d2013-10-04 10:05:36 +0530121 } else if ((csor & CSOR_NAND_PGS_MASK) == CSOR_NAND_PGS_2K) {
Prabhakar Kushwahaab4ab012013-04-16 13:27:59 +0530122 page_size = 2048;
123 bufnum_mask = 0x3;
124 } else {
125 page_size = 512;
126 bufnum_mask = 0xf;
127
128 if (port_size == 8)
129 bad_marker = 5;
130 }
131
132 pages_per_blk =
133 32 << ((csor & CSOR_NAND_PB_MASK) >> CSOR_NAND_PB_SHIFT);
134
135 blk_size = pages_per_blk * page_size;
136
137 /* Open Full SRAM mapping for spare are access */
138 out_be32(&ifc->ifc_nand.ncfgr, 0x0);
139
140 /* Clear Boot events */
141 out_be32(&ifc->ifc_nand.nand_evter_stat, 0xffffffff);
142
143 /* Program FIR/FCR for Large/Small page */
144 if (page_size > 512) {
145 out_be32(&ifc->ifc_nand.nand_fir0,
146 (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
147 (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
148 (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
149 (IFC_FIR_OP_CMD1 << IFC_NAND_FIR0_OP3_SHIFT) |
150 (IFC_FIR_OP_BTRD << IFC_NAND_FIR0_OP4_SHIFT));
151 out_be32(&ifc->ifc_nand.nand_fir1, 0x0);
152
153 out_be32(&ifc->ifc_nand.nand_fcr0,
154 (NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT) |
155 (NAND_CMD_READSTART << IFC_NAND_FCR0_CMD1_SHIFT));
156 } else {
157 out_be32(&ifc->ifc_nand.nand_fir0,
158 (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
159 (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
160 (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
161 (IFC_FIR_OP_BTRD << IFC_NAND_FIR0_OP3_SHIFT));
162 out_be32(&ifc->ifc_nand.nand_fir1, 0x0);
163
164 out_be32(&ifc->ifc_nand.nand_fcr0,
165 NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT);
166 }
167
168 /* Program FBCR = 0 for full page read */
169 out_be32(&ifc->ifc_nand.nand_fbcr, 0);
170
171 /* Read and copy u-boot on SDRAM from NAND device, In parallel
172 * check for Bad block if found skip it and read continue to
173 * next Block
174 */
175 while (pos < uboot_size) {
176 int i = 0;
177 do {
178 pg_no = offs / page_size;
179 bufnum = pg_no & bufnum_mask;
180 sram_addr = bufnum * page_size * 2;
181
182 out_be32(&ifc->ifc_nand.row0, pg_no);
183 out_be32(&ifc->ifc_nand.col0, 0);
184 /* start read */
185 out_be32(&ifc->ifc_nand.nandseq_strt,
186 IFC_NAND_SEQ_STRT_FIR_STRT);
187
188 /* wait for read to complete */
189 nand_wait(&buf[sram_addr], bufnum, page_size);
190
191 /*
192 * If either of the first two pages are marked bad,
193 * continue to the next block.
194 */
195 if (i++ < 2 &&
196 bad_block(&buf[sram_addr + page_size + bad_marker],
197 port_size)) {
198 puts("skipping\n");
199 offs = (offs + blk_size) & ~(blk_size - 1);
200 pos &= ~(blk_size - 1);
201 break;
202 }
203
204 for (j = 0; j < page_size; j++)
205 dst[pos + j] = __raw_readb(&buf[sram_addr + j]);
206
207 pos += page_size;
208 offs += page_size;
209 } while ((offs & (blk_size - 1)) && (pos < uboot_size));
210 }
211}
212
213/*
214 * Main entrypoint for NAND Boot. It's necessary that SDRAM is already
215 * configured and available since this code loads the main U-boot image
216 * from NAND into SDRAM and starts from there.
217 */
218void nand_boot(void)
219{
220 __attribute__((noreturn)) void (*uboot)(void);
221 /*
222 * Load U-Boot image from NAND into RAM
223 */
224 nand_load(CONFIG_SYS_NAND_U_BOOT_OFFS, CONFIG_SYS_NAND_U_BOOT_SIZE,
225 (uchar *)CONFIG_SYS_NAND_U_BOOT_DST);
226
227#ifdef CONFIG_NAND_ENV_DST
228 nand_load(CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
229 (uchar *)CONFIG_NAND_ENV_DST);
230
231#ifdef CONFIG_ENV_OFFSET_REDUND
232 nand_load(CONFIG_ENV_OFFSET_REDUND, CONFIG_ENV_SIZE,
233 (uchar *)CONFIG_NAND_ENV_DST + CONFIG_ENV_SIZE);
234#endif
235#endif
236 /*
237 * Jump to U-Boot image
238 */
239#ifdef CONFIG_SPL_FLUSH_IMAGE
240 /*
241 * Clean d-cache and invalidate i-cache, to
242 * make sure that no stale data is executed.
243 */
244 flush_cache(CONFIG_SYS_NAND_U_BOOT_DST, CONFIG_SYS_NAND_U_BOOT_SIZE);
245#endif
246 uboot = (void *)CONFIG_SYS_NAND_U_BOOT_START;
247 uboot();
248}