1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2015 Google, Inc
4  */
5 
6 #include <clk.h>
7 #include <common.h>
8 #include <debug_uart.h>
9 #include <dm.h>
10 #include <fdtdec.h>
11 #include <led.h>
12 #include <malloc.h>
13 #include <ram.h>
14 #include <spl.h>
15 #include <asm/gpio.h>
16 #include <asm/io.h>
17 #include <asm/arch/bootrom.h>
18 #include <asm/arch/clock.h>
19 #include <asm/arch/hardware.h>
20 #include <asm/arch/periph.h>
21 #include <asm/arch/pmu_rk3188.h>
22 #include <asm/arch/sdram.h>
23 #include <asm/arch/timer.h>
24 #include <dm/pinctrl.h>
25 #include <dm/root.h>
26 #include <dm/test.h>
27 #include <dm/util.h>
28 #include <power/regulator.h>
29 #include <syscon.h>
30 
31 DECLARE_GLOBAL_DATA_PTR;
32 
33 u32 spl_boot_device(void)
34 {
35 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
36 	const void *blob = gd->fdt_blob;
37 	struct udevice *dev;
38 	const char *bootdev;
39 	int node;
40 	int ret;
41 
42 	bootdev = fdtdec_get_config_string(blob, "u-boot,boot0");
43 	debug("Boot device %s\n", bootdev);
44 	if (!bootdev)
45 		goto fallback;
46 
47 	node = fdt_path_offset(blob, bootdev);
48 	if (node < 0) {
49 		debug("node=%d\n", node);
50 		goto fallback;
51 	}
52 	ret = device_get_global_by_of_offset(node, &dev);
53 	if (ret) {
54 		debug("device at node %s/%d not found: %d\n", bootdev, node,
55 		      ret);
56 		goto fallback;
57 	}
58 	debug("Found device %s\n", dev->name);
59 	switch (device_get_uclass_id(dev)) {
60 	case UCLASS_SPI_FLASH:
61 		return BOOT_DEVICE_SPI;
62 	case UCLASS_MMC:
63 		return BOOT_DEVICE_MMC1;
64 	default:
65 		debug("Booting from device uclass '%s' not supported\n",
66 		      dev_get_uclass_name(dev));
67 	}
68 
69 fallback:
70 #endif
71 	return BOOT_DEVICE_MMC1;
72 }
73 
74 static int setup_arm_clock(void)
75 {
76 	struct udevice *dev;
77 	struct clk clk;
78 	int ret;
79 
80 	ret = rockchip_get_clk(&dev);
81 	if (ret)
82 		return ret;
83 
84 	clk.id = CLK_ARM;
85 	ret = clk_request(dev, &clk);
86 	if (ret < 0)
87 		return ret;
88 
89 	ret = clk_set_rate(&clk, 600000000);
90 
91 	clk_free(&clk);
92 	return ret;
93 }
94 
95 void board_init_f(ulong dummy)
96 {
97 	struct udevice *pinctrl, *dev;
98 	int ret;
99 
100 	/* Example code showing how to enable the debug UART on RK3188 */
101 #ifdef EARLY_UART
102 #include <asm/arch/grf_rk3188.h>
103 	/* Enable early UART on the RK3188 */
104 #define GRF_BASE	0x20008000
105 	struct rk3188_grf * const grf = (void *)GRF_BASE;
106 
107 	rk_clrsetreg(&grf->gpio1b_iomux,
108 		     GPIO1B1_MASK << GPIO1B1_SHIFT |
109 		     GPIO1B0_MASK << GPIO1B0_SHIFT,
110 		     GPIO1B1_UART2_SOUT << GPIO1B1_SHIFT |
111 		     GPIO1B0_UART2_SIN << GPIO1B0_SHIFT);
112 	/*
113 	 * Debug UART can be used from here if required:
114 	 *
115 	 * debug_uart_init();
116 	 * printch('a');
117 	 * printhex8(0x1234);
118 	 * printascii("string");
119 	 */
120 	debug_uart_init();
121 	printch('s');
122 	printch('p');
123 	printch('l');
124 	printch('\n');
125 #endif
126 
127 	ret = spl_early_init();
128 	if (ret) {
129 		debug("spl_early_init() failed: %d\n", ret);
130 		hang();
131 	}
132 
133 	ret = rockchip_get_clk(&dev);
134 	if (ret) {
135 		debug("CLK init failed: %d\n", ret);
136 		return;
137 	}
138 
139 	ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
140 	if (ret) {
141 		debug("Pinctrl init failed: %d\n", ret);
142 		return;
143 	}
144 
145 	ret = uclass_get_device(UCLASS_RAM, 0, &dev);
146 	if (ret) {
147 		debug("DRAM init failed: %d\n", ret);
148 		return;
149 	}
150 
151 	setup_arm_clock();
152 #if CONFIG_IS_ENABLED(ROCKCHIP_BACK_TO_BROM) && !defined(CONFIG_SPL_BOARD_INIT)
153 	back_to_bootrom(BROM_BOOT_NEXTSTAGE);
154 #endif
155 }
156 
157 static int setup_led(void)
158 {
159 #ifdef CONFIG_SPL_LED
160 	struct udevice *dev;
161 	char *led_name;
162 	int ret;
163 
164 	led_name = fdtdec_get_config_string(gd->fdt_blob, "u-boot,boot-led");
165 	if (!led_name)
166 		return 0;
167 	ret = led_get_by_label(led_name, &dev);
168 	if (ret) {
169 		debug("%s: get=%d\n", __func__, ret);
170 		return ret;
171 	}
172 	ret = led_set_on(dev, 1);
173 	if (ret)
174 		return ret;
175 #endif
176 
177 	return 0;
178 }
179 
180 void spl_board_init(void)
181 {
182 	struct udevice *pinctrl;
183 	int ret;
184 
185 	ret = setup_led();
186 	if (ret) {
187 		debug("LED ret=%d\n", ret);
188 		hang();
189 	}
190 
191 	ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
192 	if (ret) {
193 		debug("%s: Cannot find pinctrl device\n", __func__);
194 		goto err;
195 	}
196 
197 #ifdef CONFIG_SPL_MMC_SUPPORT
198 	ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_SDCARD);
199 	if (ret) {
200 		debug("%s: Failed to set up SD card\n", __func__);
201 		goto err;
202 	}
203 #endif
204 
205 	/* Enable debug UART */
206 	ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_UART_DBG);
207 	if (ret) {
208 		debug("%s: Failed to set up console UART\n", __func__);
209 		goto err;
210 	}
211 
212 	preloader_console_init();
213 #if CONFIG_IS_ENABLED(ROCKCHIP_BACK_TO_BROM)
214 	back_to_bootrom(BROM_BOOT_NEXTSTAGE);
215 #endif
216 	return;
217 
218 err:
219 	printf("spl_board_init: Error %d\n", ret);
220 
221 	/* No way to report error here */
222 	hang();
223 }
224