1*cfcc706cSMiquel Raynal /*
2*cfcc706cSMiquel Raynal * NAND boot for Freescale Enhanced Local Bus Controller, Flash Control Machine
3*cfcc706cSMiquel Raynal *
4*cfcc706cSMiquel Raynal * (C) Copyright 2006-2008
5*cfcc706cSMiquel Raynal * Stefan Roese, DENX Software Engineering, sr@denx.de.
6*cfcc706cSMiquel Raynal *
7*cfcc706cSMiquel Raynal * Copyright (c) 2008 Freescale Semiconductor, Inc.
8*cfcc706cSMiquel Raynal * Author: Scott Wood <scottwood@freescale.com>
9*cfcc706cSMiquel Raynal *
10*cfcc706cSMiquel Raynal * SPDX-License-Identifier: GPL-2.0+
11*cfcc706cSMiquel Raynal */
12*cfcc706cSMiquel Raynal
13*cfcc706cSMiquel Raynal #include <common.h>
14*cfcc706cSMiquel Raynal #include <asm/io.h>
15*cfcc706cSMiquel Raynal #include <asm/fsl_lbc.h>
16*cfcc706cSMiquel Raynal #include <nand.h>
17*cfcc706cSMiquel Raynal
18*cfcc706cSMiquel Raynal #define WINDOW_SIZE 8192
19*cfcc706cSMiquel Raynal
nand_wait(void)20*cfcc706cSMiquel Raynal static void nand_wait(void)
21*cfcc706cSMiquel Raynal {
22*cfcc706cSMiquel Raynal fsl_lbc_t *regs = LBC_BASE_ADDR;
23*cfcc706cSMiquel Raynal
24*cfcc706cSMiquel Raynal for (;;) {
25*cfcc706cSMiquel Raynal uint32_t status = in_be32(®s->ltesr);
26*cfcc706cSMiquel Raynal
27*cfcc706cSMiquel Raynal if (status == 1)
28*cfcc706cSMiquel Raynal return;
29*cfcc706cSMiquel Raynal
30*cfcc706cSMiquel Raynal if (status & 1) {
31*cfcc706cSMiquel Raynal puts("read failed (ltesr)\n");
32*cfcc706cSMiquel Raynal for (;;);
33*cfcc706cSMiquel Raynal }
34*cfcc706cSMiquel Raynal }
35*cfcc706cSMiquel Raynal }
36*cfcc706cSMiquel Raynal
37*cfcc706cSMiquel Raynal #ifdef CONFIG_TPL_BUILD
nand_spl_load_image(uint32_t offs,unsigned int uboot_size,void * vdst)38*cfcc706cSMiquel Raynal int nand_spl_load_image(uint32_t offs, unsigned int uboot_size, void *vdst)
39*cfcc706cSMiquel Raynal #else
40*cfcc706cSMiquel Raynal static int nand_load_image(uint32_t offs, unsigned int uboot_size, void *vdst)
41*cfcc706cSMiquel Raynal #endif
42*cfcc706cSMiquel Raynal {
43*cfcc706cSMiquel Raynal fsl_lbc_t *regs = LBC_BASE_ADDR;
44*cfcc706cSMiquel Raynal uchar *buf = (uchar *)CONFIG_SYS_NAND_BASE;
45*cfcc706cSMiquel Raynal const int large = CONFIG_SYS_NAND_OR_PRELIM & OR_FCM_PGS;
46*cfcc706cSMiquel Raynal const int block_shift = large ? 17 : 14;
47*cfcc706cSMiquel Raynal const int block_size = 1 << block_shift;
48*cfcc706cSMiquel Raynal const int page_size = large ? 2048 : 512;
49*cfcc706cSMiquel Raynal const int bad_marker = large ? page_size + 0 : page_size + 5;
50*cfcc706cSMiquel Raynal int fmr = (15 << FMR_CWTO_SHIFT) | (2 << FMR_AL_SHIFT) | 2;
51*cfcc706cSMiquel Raynal int pos = 0;
52*cfcc706cSMiquel Raynal char *dst = vdst;
53*cfcc706cSMiquel Raynal
54*cfcc706cSMiquel Raynal if (offs & (block_size - 1)) {
55*cfcc706cSMiquel Raynal puts("bad offset\n");
56*cfcc706cSMiquel Raynal for (;;);
57*cfcc706cSMiquel Raynal }
58*cfcc706cSMiquel Raynal
59*cfcc706cSMiquel Raynal if (large) {
60*cfcc706cSMiquel Raynal fmr |= FMR_ECCM;
61*cfcc706cSMiquel Raynal out_be32(®s->fcr, (NAND_CMD_READ0 << FCR_CMD0_SHIFT) |
62*cfcc706cSMiquel Raynal (NAND_CMD_READSTART << FCR_CMD1_SHIFT));
63*cfcc706cSMiquel Raynal out_be32(®s->fir,
64*cfcc706cSMiquel Raynal (FIR_OP_CW0 << FIR_OP0_SHIFT) |
65*cfcc706cSMiquel Raynal (FIR_OP_CA << FIR_OP1_SHIFT) |
66*cfcc706cSMiquel Raynal (FIR_OP_PA << FIR_OP2_SHIFT) |
67*cfcc706cSMiquel Raynal (FIR_OP_CW1 << FIR_OP3_SHIFT) |
68*cfcc706cSMiquel Raynal (FIR_OP_RBW << FIR_OP4_SHIFT));
69*cfcc706cSMiquel Raynal } else {
70*cfcc706cSMiquel Raynal out_be32(®s->fcr, NAND_CMD_READ0 << FCR_CMD0_SHIFT);
71*cfcc706cSMiquel Raynal out_be32(®s->fir,
72*cfcc706cSMiquel Raynal (FIR_OP_CW0 << FIR_OP0_SHIFT) |
73*cfcc706cSMiquel Raynal (FIR_OP_CA << FIR_OP1_SHIFT) |
74*cfcc706cSMiquel Raynal (FIR_OP_PA << FIR_OP2_SHIFT) |
75*cfcc706cSMiquel Raynal (FIR_OP_RBW << FIR_OP3_SHIFT));
76*cfcc706cSMiquel Raynal }
77*cfcc706cSMiquel Raynal
78*cfcc706cSMiquel Raynal out_be32(®s->fbcr, 0);
79*cfcc706cSMiquel Raynal clrsetbits_be32(®s->bank[0].br, BR_DECC, BR_DECC_CHK_GEN);
80*cfcc706cSMiquel Raynal
81*cfcc706cSMiquel Raynal while (pos < uboot_size) {
82*cfcc706cSMiquel Raynal int i = 0;
83*cfcc706cSMiquel Raynal out_be32(®s->fbar, offs >> block_shift);
84*cfcc706cSMiquel Raynal
85*cfcc706cSMiquel Raynal do {
86*cfcc706cSMiquel Raynal int j;
87*cfcc706cSMiquel Raynal unsigned int page_offs = (offs & (block_size - 1)) << 1;
88*cfcc706cSMiquel Raynal
89*cfcc706cSMiquel Raynal out_be32(®s->ltesr, ~0);
90*cfcc706cSMiquel Raynal out_be32(®s->lteatr, 0);
91*cfcc706cSMiquel Raynal out_be32(®s->fpar, page_offs);
92*cfcc706cSMiquel Raynal out_be32(®s->fmr, fmr);
93*cfcc706cSMiquel Raynal out_be32(®s->lsor, 0);
94*cfcc706cSMiquel Raynal nand_wait();
95*cfcc706cSMiquel Raynal
96*cfcc706cSMiquel Raynal page_offs %= WINDOW_SIZE;
97*cfcc706cSMiquel Raynal
98*cfcc706cSMiquel Raynal /*
99*cfcc706cSMiquel Raynal * If either of the first two pages are marked bad,
100*cfcc706cSMiquel Raynal * continue to the next block.
101*cfcc706cSMiquel Raynal */
102*cfcc706cSMiquel Raynal if (i++ < 2 && buf[page_offs + bad_marker] != 0xff) {
103*cfcc706cSMiquel Raynal puts("skipping\n");
104*cfcc706cSMiquel Raynal offs = (offs + block_size) & ~(block_size - 1);
105*cfcc706cSMiquel Raynal pos &= ~(block_size - 1);
106*cfcc706cSMiquel Raynal break;
107*cfcc706cSMiquel Raynal }
108*cfcc706cSMiquel Raynal
109*cfcc706cSMiquel Raynal for (j = 0; j < page_size; j++)
110*cfcc706cSMiquel Raynal dst[pos + j] = buf[page_offs + j];
111*cfcc706cSMiquel Raynal
112*cfcc706cSMiquel Raynal pos += page_size;
113*cfcc706cSMiquel Raynal offs += page_size;
114*cfcc706cSMiquel Raynal } while ((offs & (block_size - 1)) && (pos < uboot_size));
115*cfcc706cSMiquel Raynal }
116*cfcc706cSMiquel Raynal
117*cfcc706cSMiquel Raynal return 0;
118*cfcc706cSMiquel Raynal }
119*cfcc706cSMiquel Raynal
120*cfcc706cSMiquel Raynal /*
121*cfcc706cSMiquel Raynal * Defines a static function nand_load_image() here, because non-static makes
122*cfcc706cSMiquel Raynal * the code too large for certain SPLs(minimal SPL, maximum size <= 4Kbytes)
123*cfcc706cSMiquel Raynal */
124*cfcc706cSMiquel Raynal #ifndef CONFIG_TPL_BUILD
125*cfcc706cSMiquel Raynal #define nand_spl_load_image(offs, uboot_size, vdst) \
126*cfcc706cSMiquel Raynal nand_load_image(offs, uboot_size, vdst)
127*cfcc706cSMiquel Raynal #endif
128*cfcc706cSMiquel Raynal
129*cfcc706cSMiquel Raynal /*
130*cfcc706cSMiquel Raynal * The main entry for NAND booting. It's necessary that SDRAM is already
131*cfcc706cSMiquel Raynal * configured and available since this code loads the main U-Boot image
132*cfcc706cSMiquel Raynal * from NAND into SDRAM and starts it from there.
133*cfcc706cSMiquel Raynal */
nand_boot(void)134*cfcc706cSMiquel Raynal void nand_boot(void)
135*cfcc706cSMiquel Raynal {
136*cfcc706cSMiquel Raynal __attribute__((noreturn)) void (*uboot)(void);
137*cfcc706cSMiquel Raynal /*
138*cfcc706cSMiquel Raynal * Load U-Boot image from NAND into RAM
139*cfcc706cSMiquel Raynal */
140*cfcc706cSMiquel Raynal nand_spl_load_image(CONFIG_SYS_NAND_U_BOOT_OFFS,
141*cfcc706cSMiquel Raynal CONFIG_SYS_NAND_U_BOOT_SIZE,
142*cfcc706cSMiquel Raynal (void *)CONFIG_SYS_NAND_U_BOOT_DST);
143*cfcc706cSMiquel Raynal
144*cfcc706cSMiquel Raynal #ifdef CONFIG_NAND_ENV_DST
145*cfcc706cSMiquel Raynal nand_spl_load_image(CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
146*cfcc706cSMiquel Raynal (void *)CONFIG_NAND_ENV_DST);
147*cfcc706cSMiquel Raynal
148*cfcc706cSMiquel Raynal #ifdef CONFIG_ENV_OFFSET_REDUND
149*cfcc706cSMiquel Raynal nand_spl_load_image(CONFIG_ENV_OFFSET_REDUND, CONFIG_ENV_SIZE,
150*cfcc706cSMiquel Raynal (void *)CONFIG_NAND_ENV_DST + CONFIG_ENV_SIZE);
151*cfcc706cSMiquel Raynal #endif
152*cfcc706cSMiquel Raynal #endif
153*cfcc706cSMiquel Raynal
154*cfcc706cSMiquel Raynal #ifdef CONFIG_SPL_FLUSH_IMAGE
155*cfcc706cSMiquel Raynal /*
156*cfcc706cSMiquel Raynal * Clean d-cache and invalidate i-cache, to
157*cfcc706cSMiquel Raynal * make sure that no stale data is executed.
158*cfcc706cSMiquel Raynal */
159*cfcc706cSMiquel Raynal flush_cache(CONFIG_SYS_NAND_U_BOOT_DST, CONFIG_SYS_NAND_U_BOOT_SIZE);
160*cfcc706cSMiquel Raynal #endif
161*cfcc706cSMiquel Raynal
162*cfcc706cSMiquel Raynal puts("transfering control\n");
163*cfcc706cSMiquel Raynal /*
164*cfcc706cSMiquel Raynal * Jump to U-Boot image
165*cfcc706cSMiquel Raynal */
166*cfcc706cSMiquel Raynal uboot = (void *)CONFIG_SYS_NAND_U_BOOT_START;
167*cfcc706cSMiquel Raynal (*uboot)();
168*cfcc706cSMiquel Raynal }
169