1 /*
2  * (C) Copyright 2015 Google, Inc
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 <fdtdec.h>
11 #include <i2c.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/sdram.h>
23 #include <asm/arch/sys_proto.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 <power/rk8xx_pmic.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 #elif defined(CONFIG_TARGET_CHROMEBOOK_JERRY) || \
72 		defined(CONFIG_TARGET_CHROMEBIT_MICKEY) || \
73 		defined(CONFIG_TARGET_CHROMEBOOK_MINNIE)
74 	return BOOT_DEVICE_SPI;
75 #endif
76 	return BOOT_DEVICE_MMC1;
77 }
78 
79 u32 spl_boot_mode(const u32 boot_device)
80 {
81 	return MMCSD_MODE_RAW;
82 }
83 
84 #ifdef CONFIG_SPL_MMC_SUPPORT
85 static int configure_emmc(struct udevice *pinctrl)
86 {
87 #if defined(CONFIG_TARGET_CHROMEBOOK_JERRY)
88 
89 	struct gpio_desc desc;
90 	int ret;
91 
92 	pinctrl_request_noflags(pinctrl, PERIPH_ID_EMMC);
93 
94 	/*
95 	 * TODO(sjg@chromium.org): Pick this up from device tree or perhaps
96 	 * use the EMMC_PWREN setting.
97 	 */
98 	ret = dm_gpio_lookup_name("D9", &desc);
99 	if (ret) {
100 		debug("gpio ret=%d\n", ret);
101 		return ret;
102 	}
103 	ret = dm_gpio_request(&desc, "emmc_pwren");
104 	if (ret) {
105 		debug("gpio_request ret=%d\n", ret);
106 		return ret;
107 	}
108 	ret = dm_gpio_set_dir_flags(&desc, GPIOD_IS_OUT);
109 	if (ret) {
110 		debug("gpio dir ret=%d\n", ret);
111 		return ret;
112 	}
113 	ret = dm_gpio_set_value(&desc, 1);
114 	if (ret) {
115 		debug("gpio value ret=%d\n", ret);
116 		return ret;
117 	}
118 #endif
119 	return 0;
120 }
121 #endif
122 
123 #if !defined(CONFIG_SPL_OF_PLATDATA)
124 static int phycore_init(void)
125 {
126 	struct udevice *pmic;
127 	int ret;
128 
129 	ret = uclass_first_device_err(UCLASS_PMIC, &pmic);
130 	if (ret)
131 		return ret;
132 
133 #if defined(CONFIG_SPL_POWER_SUPPORT)
134 	/* Increase USB input current to 2A */
135 	ret = rk818_spl_configure_usb_input_current(pmic, 2000);
136 	if (ret)
137 		return ret;
138 
139 	/* Close charger when USB lower then 3.26V */
140 	ret = rk818_spl_configure_usb_chrg_shutdown(pmic, 3260000);
141 	if (ret)
142 		return ret;
143 #endif
144 
145 	return 0;
146 }
147 #endif
148 
149 void board_init_f(ulong dummy)
150 {
151 	struct udevice *pinctrl;
152 	struct udevice *dev;
153 	int ret;
154 
155 	/* Example code showing how to enable the debug UART on RK3288 */
156 #include <asm/arch/grf_rk3288.h>
157 	/* Enable early UART on the RK3288 */
158 #define GRF_BASE	0xff770000
159 	struct rk3288_grf * const grf = (void *)GRF_BASE;
160 
161 	rk_clrsetreg(&grf->gpio7ch_iomux, GPIO7C7_MASK << GPIO7C7_SHIFT |
162 		     GPIO7C6_MASK << GPIO7C6_SHIFT,
163 		     GPIO7C7_UART2DBG_SOUT << GPIO7C7_SHIFT |
164 		     GPIO7C6_UART2DBG_SIN << GPIO7C6_SHIFT);
165 	/*
166 	 * Debug UART can be used from here if required:
167 	 *
168 	 * debug_uart_init();
169 	 * printch('a');
170 	 * printhex8(0x1234);
171 	 * printascii("string");
172 	 */
173 	debug_uart_init();
174 	debug("\nspl:debug uart enabled in %s\n", __func__);
175 	ret = spl_early_init();
176 	if (ret) {
177 		debug("spl_early_init() failed: %d\n", ret);
178 		hang();
179 	}
180 
181 	rockchip_timer_init();
182 	configure_l2ctlr();
183 
184 	ret = rockchip_get_clk(&dev);
185 	if (ret) {
186 		debug("CLK init failed: %d\n", ret);
187 		return;
188 	}
189 
190 	ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
191 	if (ret) {
192 		debug("Pinctrl init failed: %d\n", ret);
193 		return;
194 	}
195 
196 #if !defined(CONFIG_SPL_OF_PLATDATA)
197 	if (of_machine_is_compatible("phytec,rk3288-phycore-som")) {
198 		ret = phycore_init();
199 		if (ret) {
200 			debug("Failed to set up phycore power settings: %d\n",
201 			      ret);
202 			return;
203 		}
204 	}
205 #endif
206 
207 #if !defined(CONFIG_SUPPORT_TPL)
208 	debug("\nspl:init dram\n");
209 	ret = uclass_get_device(UCLASS_RAM, 0, &dev);
210 	if (ret) {
211 		debug("DRAM init failed: %d\n", ret);
212 		return;
213 	}
214 #endif
215 
216 #if CONFIG_IS_ENABLED(ROCKCHIP_BACK_TO_BROM) && !defined(CONFIG_SPL_BOARD_INIT)
217 	back_to_bootrom();
218 #endif
219 }
220 
221 static int setup_led(void)
222 {
223 #ifdef CONFIG_SPL_LED
224 	struct udevice *dev;
225 	char *led_name;
226 	int ret;
227 
228 	led_name = fdtdec_get_config_string(gd->fdt_blob, "u-boot,boot-led");
229 	if (!led_name)
230 		return 0;
231 	ret = led_get_by_label(led_name, &dev);
232 	if (ret) {
233 		debug("%s: get=%d\n", __func__, ret);
234 		return ret;
235 	}
236 	ret = led_set_on(dev, 1);
237 	if (ret)
238 		return ret;
239 #endif
240 
241 	return 0;
242 }
243 
244 void spl_board_init(void)
245 {
246 	struct udevice *pinctrl;
247 	int ret;
248 
249 	ret = setup_led();
250 
251 	if (ret) {
252 		debug("LED ret=%d\n", ret);
253 		hang();
254 	}
255 
256 	ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
257 	if (ret) {
258 		debug("%s: Cannot find pinctrl device\n", __func__);
259 		goto err;
260 	}
261 
262 #ifdef CONFIG_SPL_MMC_SUPPORT
263 	ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_SDCARD);
264 	if (ret) {
265 		debug("%s: Failed to set up SD card\n", __func__);
266 		goto err;
267 	}
268 	ret = configure_emmc(pinctrl);
269 	if (ret) {
270 		debug("%s: Failed to set up eMMC\n", __func__);
271 		goto err;
272 	}
273 #endif
274 
275 	/* Enable debug UART */
276 	ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_UART_DBG);
277 	if (ret) {
278 		debug("%s: Failed to set up console UART\n", __func__);
279 		goto err;
280 	}
281 
282 	preloader_console_init();
283 #if CONFIG_IS_ENABLED(ROCKCHIP_BACK_TO_BROM)
284 	back_to_bootrom();
285 #endif
286 	return;
287 err:
288 	printf("spl_board_init: Error %d\n", ret);
289 
290 	/* No way to report error here */
291 	hang();
292 }
293