xref: /openbmc/u-boot/common/spl/spl_nand.c (revision ec2c81c5)
1 /*
2  * Copyright (C) 2011
3  * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 #include <common.h>
8 #include <config.h>
9 #include <spl.h>
10 #include <asm/io.h>
11 #include <nand.h>
12 
13 #if defined(CONFIG_SPL_NAND_RAW_ONLY)
14 int spl_nand_load_image(void)
15 {
16 	nand_init();
17 
18 	nand_spl_load_image(CONFIG_SYS_NAND_U_BOOT_OFFS,
19 			    CONFIG_SYS_NAND_U_BOOT_SIZE,
20 			    (void *)CONFIG_SYS_NAND_U_BOOT_DST);
21 	spl_set_header_raw_uboot();
22 	nand_deselect();
23 
24 	return 0;
25 }
26 #else
27 static int spl_nand_load_element(int offset, struct image_header *header)
28 {
29 	int err;
30 
31 	err = nand_spl_load_image(offset, sizeof(*header), (void *)header);
32 	if (err)
33 		return err;
34 
35 	spl_parse_image_header(header);
36 	return nand_spl_load_image(offset, spl_image.size,
37 				   (void *)(unsigned long)spl_image.load_addr);
38 }
39 
40 int spl_nand_load_image(void)
41 {
42 	int err;
43 	struct image_header *header;
44 	int *src __attribute__((unused));
45 	int *dst __attribute__((unused));
46 
47 #ifdef CONFIG_SPL_NAND_SOFTECC
48 	debug("spl: nand - using sw ecc\n");
49 #else
50 	debug("spl: nand - using hw ecc\n");
51 #endif
52 	nand_init();
53 
54 	/*use CONFIG_SYS_TEXT_BASE as temporary storage area */
55 	header = (struct image_header *)(CONFIG_SYS_TEXT_BASE);
56 #ifdef CONFIG_SPL_OS_BOOT
57 	if (!spl_start_uboot()) {
58 		/*
59 		 * load parameter image
60 		 * load to temp position since nand_spl_load_image reads
61 		 * a whole block which is typically larger than
62 		 * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
63 		 * following sections like BSS
64 		 */
65 		nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS,
66 			CONFIG_CMD_SPL_WRITE_SIZE,
67 			(void *)CONFIG_SYS_TEXT_BASE);
68 		/* copy to destintion */
69 		for (dst = (int *)CONFIG_SYS_SPL_ARGS_ADDR,
70 				src = (int *)CONFIG_SYS_TEXT_BASE;
71 				src < (int *)(CONFIG_SYS_TEXT_BASE +
72 				CONFIG_CMD_SPL_WRITE_SIZE);
73 				src++, dst++) {
74 			writel(readl(src), dst);
75 		}
76 
77 		/* load linux */
78 		nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
79 			sizeof(*header), (void *)header);
80 		spl_parse_image_header(header);
81 		if (header->ih_os == IH_OS_LINUX) {
82 			/* happy - was a linux */
83 			err = nand_spl_load_image(
84 				CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
85 				spl_image.size,
86 				(void *)spl_image.load_addr);
87 			nand_deselect();
88 			return err;
89 		} else {
90 			puts("The Expected Linux image was not "
91 				"found. Please check your NAND "
92 				"configuration.\n");
93 			puts("Trying to start u-boot now...\n");
94 		}
95 	}
96 #endif
97 #ifdef CONFIG_NAND_ENV_DST
98 	spl_nand_load_element(CONFIG_ENV_OFFSET, header);
99 #ifdef CONFIG_ENV_OFFSET_REDUND
100 	spl_nand_load_element(CONFIG_ENV_OFFSET_REDUND, header);
101 #endif
102 #endif
103 	/* Load u-boot */
104 	err = spl_nand_load_element(CONFIG_SYS_NAND_U_BOOT_OFFS, header);
105 	nand_deselect();
106 	return err;
107 }
108 #endif
109