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