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