xref: /rk3399_rockchip-uboot/arch/arm/mach-rockchip/spl.c (revision b283d2ae7bf9dec650d82c6b10cb92ddd9d9ce5d)
1 /*
2  * (C) Copyright 2018 Rockchip Electronics Co., Ltd
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <debug_uart.h>
9 #include <dm.h>
10 #include <ram.h>
11 #include <spl.h>
12 #include <asm/arch/bootrom.h>
13 #include <asm/io.h>
14 
15 DECLARE_GLOBAL_DATA_PTR;
16 
17 #define BROM_BOOTSOURCE_ID_ADDR (CONFIG_ROCKCHIP_IRAM_START_ADDR + 0x10)
18 void board_return_to_bootrom(void)
19 {
20 	back_to_bootrom(BROM_BOOT_NEXTSTAGE);
21 }
22 
23 __weak const char * const boot_devices[BROM_LAST_BOOTSOURCE + 1] = {
24 };
25 
26 const char *board_spl_was_booted_from(void)
27 {
28 	u32  bootdevice_brom_id = readl(BROM_BOOTSOURCE_ID_ADDR);
29 	const char *bootdevice_ofpath = NULL;
30 
31 	if (bootdevice_brom_id < ARRAY_SIZE(boot_devices))
32 		bootdevice_ofpath = boot_devices[bootdevice_brom_id];
33 
34 	if (bootdevice_ofpath)
35 		debug("%s: brom_bootdevice_id %x maps to '%s'\n",
36 		      __func__, bootdevice_brom_id, bootdevice_ofpath);
37 	else
38 		debug("%s: failed to resolve brom_bootdevice_id %x\n",
39 		      __func__, bootdevice_brom_id);
40 
41 	return bootdevice_ofpath;
42 }
43 
44 u32 spl_boot_device(void)
45 {
46 	u32 boot_device = BOOT_DEVICE_MMC1;
47 
48 #if defined(CONFIG_TARGET_CHROMEBOOK_JERRY) || \
49 		defined(CONFIG_TARGET_CHROMEBIT_MICKEY) || \
50 		defined(CONFIG_TARGET_CHROMEBOOK_MINNIE)
51 	return BOOT_DEVICE_SPI;
52 #endif
53 	if (CONFIG_IS_ENABLED(ROCKCHIP_BACK_TO_BROM))
54 		return BOOT_DEVICE_BOOTROM;
55 
56 	return boot_device;
57 }
58 
59 u32 spl_boot_mode(const u32 boot_device)
60 {
61 	return MMCSD_MODE_RAW;
62 }
63 
64 __weak void rockchip_stimer_init(void)
65 {
66 	writel(0, CONFIG_ROCKCHIP_STIMER_BASE + 0x10);
67 	writel(0xffffffff, CONFIG_ROCKCHIP_STIMER_BASE);
68 	writel(0xffffffff, CONFIG_ROCKCHIP_STIMER_BASE + 4);
69 	writel(1, CONFIG_ROCKCHIP_STIMER_BASE + 0x10);
70 }
71 
72 __weak int arch_cpu_init(void)
73 {
74 	return 0;
75 }
76 
77 __weak int rk_board_init_f(void)
78 {
79 	return 0;
80 }
81 
82 void board_init_f(ulong dummy)
83 {
84 #ifdef CONFIG_SPL_FRAMEWORK
85 	int ret;
86 #if !defined(CONFIG_SUPPORT_TPL)
87 	struct udevice *dev;
88 #endif
89 #endif
90 
91 #define EARLY_UART
92 #if defined(EARLY_UART) && defined(CONFIG_DEBUG_UART)
93 	/*
94 	 * Debug UART can be used from here if required:
95 	 *
96 	 * debug_uart_init();
97 	 * printch('a');
98 	 * printhex8(0x1234);
99 	 * printascii("string");
100 	 */
101 	debug_uart_init();
102 	printascii("U-Boot SPL board init");
103 #endif
104 
105 	arch_cpu_init();
106 	rockchip_stimer_init();
107 #ifdef CONFIG_SPL_FRAMEWORK
108 	ret = spl_early_init();
109 	if (ret) {
110 		printf("spl_early_init() failed: %d\n", ret);
111 		hang();
112 	}
113 #if !defined(CONFIG_SUPPORT_TPL)
114 	debug("\nspl:init dram\n");
115 	ret = uclass_get_device(UCLASS_RAM, 0, &dev);
116 	if (ret) {
117 		printf("DRAM init failed: %d\n", ret);
118 		return;
119 	}
120 #endif
121 	preloader_console_init();
122 #else
123 	/* Some SoCs like rk3036 does not use any frame work */
124 	sdram_init();
125 #endif
126 
127 	rk_board_init_f();
128 #if CONFIG_IS_ENABLED(ROCKCHIP_BACK_TO_BROM) && !defined(CONFIG_SPL_BOARD_INIT)
129 	back_to_bootrom(BROM_BOOT_NEXTSTAGE);
130 #endif
131 
132 }
133 
134 #ifdef CONFIG_SPL_LOAD_FIT
135 int board_fit_config_name_match(const char *name)
136 {
137 	/* Just empty function now - can't decide what to choose */
138 	debug("%s: %s\n", __func__, name);
139 
140 	return 0;
141 }
142 #endif
143 
144 #ifdef CONFIG_SPL_BOARD_INIT
145 __weak int rk_spl_board_init(void)
146 {
147 	return 0;
148 }
149 
150 static int setup_led(void)
151 {
152 #ifdef CONFIG_SPL_LED
153 	struct udevice *dev;
154 	char *led_name;
155 	int ret;
156 
157 	led_name = fdtdec_get_config_string(gd->fdt_blob, "u-boot,boot-led");
158 	if (!led_name)
159 		return 0;
160 	ret = led_get_by_label(led_name, &dev);
161 	if (ret) {
162 		debug("%s: get=%d\n", __func__, ret);
163 		return ret;
164 	}
165 	ret = led_set_on(dev, 1);
166 	if (ret)
167 		return ret;
168 #endif
169 
170 	return 0;
171 }
172 
173 void spl_board_init(void)
174 {
175 	int ret;
176 
177 	ret = setup_led();
178 
179 	if (ret) {
180 		debug("LED ret=%d\n", ret);
181 		hang();
182 	}
183 
184 	rk_spl_board_init();
185 #if CONFIG_IS_ENABLED(ROCKCHIP_BACK_TO_BROM)
186 	back_to_bootrom(BROM_BOOT_NEXTSTAGE);
187 #endif
188 	return;
189 }
190 #endif
191