xref: /rk3399_rockchip-uboot/common/spl/spl_nand.c (revision d97b4ce8056ff01ca8f1a63a7c69fa5316d33830)
1*d97b4ce8STom Rini /*
2*d97b4ce8STom Rini  * Copyright (C) 2011
3*d97b4ce8STom Rini  * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de>
4*d97b4ce8STom Rini  *
5*d97b4ce8STom Rini  * See file CREDITS for list of people who contributed to this
6*d97b4ce8STom Rini  * project.
7*d97b4ce8STom Rini  *
8*d97b4ce8STom Rini  * This program is free software; you can redistribute it and/or
9*d97b4ce8STom Rini  * modify it under the terms of the GNU General Public License as
10*d97b4ce8STom Rini  * published by the Free Software Foundation; either version 2 of
11*d97b4ce8STom Rini  * the License, or (at your option) any later version.
12*d97b4ce8STom Rini  *
13*d97b4ce8STom Rini  * This program is distributed in the hope that it will be useful,
14*d97b4ce8STom Rini  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15*d97b4ce8STom Rini  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16*d97b4ce8STom Rini  * GNU General Public License for more details.
17*d97b4ce8STom Rini  *
18*d97b4ce8STom Rini  * You should have received a copy of the GNU General Public License
19*d97b4ce8STom Rini  * along with this program; if not, write to the Free Software
20*d97b4ce8STom Rini  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21*d97b4ce8STom Rini  * MA 02111-1307 USA
22*d97b4ce8STom Rini  */
23*d97b4ce8STom Rini #include <common.h>
24*d97b4ce8STom Rini #include <config.h>
25*d97b4ce8STom Rini #include <spl.h>
26*d97b4ce8STom Rini #include <asm/io.h>
27*d97b4ce8STom Rini #include <nand.h>
28*d97b4ce8STom Rini 
29*d97b4ce8STom Rini void spl_nand_load_image(void)
30*d97b4ce8STom Rini {
31*d97b4ce8STom Rini 	struct image_header *header;
32*d97b4ce8STom Rini 	int *src __attribute__((unused));
33*d97b4ce8STom Rini 	int *dst __attribute__((unused));
34*d97b4ce8STom Rini 
35*d97b4ce8STom Rini 	debug("spl: nand - using hw ecc\n");
36*d97b4ce8STom Rini 	nand_init();
37*d97b4ce8STom Rini 
38*d97b4ce8STom Rini 	/*use CONFIG_SYS_TEXT_BASE as temporary storage area */
39*d97b4ce8STom Rini 	header = (struct image_header *)(CONFIG_SYS_TEXT_BASE);
40*d97b4ce8STom Rini #ifdef CONFIG_SPL_OS_BOOT
41*d97b4ce8STom Rini 	if (!spl_start_uboot()) {
42*d97b4ce8STom Rini 		/*
43*d97b4ce8STom Rini 		 * load parameter image
44*d97b4ce8STom Rini 		 * load to temp position since nand_spl_load_image reads
45*d97b4ce8STom Rini 		 * a whole block which is typically larger than
46*d97b4ce8STom Rini 		 * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
47*d97b4ce8STom Rini 		 * following sections like BSS
48*d97b4ce8STom Rini 		 */
49*d97b4ce8STom Rini 		nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS,
50*d97b4ce8STom Rini 			CONFIG_CMD_SPL_WRITE_SIZE,
51*d97b4ce8STom Rini 			(void *)CONFIG_SYS_TEXT_BASE);
52*d97b4ce8STom Rini 		/* copy to destintion */
53*d97b4ce8STom Rini 		for (dst = (int *)CONFIG_SYS_SPL_ARGS_ADDR,
54*d97b4ce8STom Rini 				src = (int *)CONFIG_SYS_TEXT_BASE;
55*d97b4ce8STom Rini 				src < (int *)(CONFIG_SYS_TEXT_BASE +
56*d97b4ce8STom Rini 				CONFIG_CMD_SPL_WRITE_SIZE);
57*d97b4ce8STom Rini 				src++, dst++) {
58*d97b4ce8STom Rini 			writel(readl(src), dst);
59*d97b4ce8STom Rini 		}
60*d97b4ce8STom Rini 
61*d97b4ce8STom Rini 		/* load linux */
62*d97b4ce8STom Rini 		nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
63*d97b4ce8STom Rini 			CONFIG_SYS_NAND_PAGE_SIZE, (void *)header);
64*d97b4ce8STom Rini 		spl_parse_image_header(header);
65*d97b4ce8STom Rini 		if (header->ih_os == IH_OS_LINUX) {
66*d97b4ce8STom Rini 			/* happy - was a linux */
67*d97b4ce8STom Rini 			nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
68*d97b4ce8STom Rini 				spl_image.size, (void *)spl_image.load_addr);
69*d97b4ce8STom Rini 			nand_deselect();
70*d97b4ce8STom Rini 			return;
71*d97b4ce8STom Rini 		} else {
72*d97b4ce8STom Rini 			puts("The Expected Linux image was not "
73*d97b4ce8STom Rini 				"found. Please check your NAND "
74*d97b4ce8STom Rini 				"configuration.\n");
75*d97b4ce8STom Rini 			puts("Trying to start u-boot now...\n");
76*d97b4ce8STom Rini 		}
77*d97b4ce8STom Rini 	}
78*d97b4ce8STom Rini #endif
79*d97b4ce8STom Rini #ifdef CONFIG_NAND_ENV_DST
80*d97b4ce8STom Rini 	nand_spl_load_image(CONFIG_ENV_OFFSET,
81*d97b4ce8STom Rini 		CONFIG_SYS_NAND_PAGE_SIZE, (void *)header);
82*d97b4ce8STom Rini 	spl_parse_image_header(header);
83*d97b4ce8STom Rini 	nand_spl_load_image(CONFIG_ENV_OFFSET, spl_image.size,
84*d97b4ce8STom Rini 		(void *)spl_image.load_addr);
85*d97b4ce8STom Rini #ifdef CONFIG_ENV_OFFSET_REDUND
86*d97b4ce8STom Rini 	nand_spl_load_image(CONFIG_ENV_OFFSET_REDUND,
87*d97b4ce8STom Rini 		CONFIG_SYS_NAND_PAGE_SIZE, (void *)header);
88*d97b4ce8STom Rini 	spl_parse_image_header(header);
89*d97b4ce8STom Rini 	nand_spl_load_image(CONFIG_ENV_OFFSET_REDUND, spl_image.size,
90*d97b4ce8STom Rini 		(void *)spl_image.load_addr);
91*d97b4ce8STom Rini #endif
92*d97b4ce8STom Rini #endif
93*d97b4ce8STom Rini 	/* Load u-boot */
94*d97b4ce8STom Rini 	nand_spl_load_image(CONFIG_SYS_NAND_U_BOOT_OFFS,
95*d97b4ce8STom Rini 		CONFIG_SYS_NAND_PAGE_SIZE, (void *)header);
96*d97b4ce8STom Rini 	spl_parse_image_header(header);
97*d97b4ce8STom Rini 	nand_spl_load_image(CONFIG_SYS_NAND_U_BOOT_OFFS,
98*d97b4ce8STom Rini 		spl_image.size, (void *)spl_image.load_addr);
99*d97b4ce8STom Rini 	nand_deselect();
100*d97b4ce8STom Rini }
101