blob: 3c6c3d3c09b182d7b32054ce262f6d43eca12f8b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Stübner0b3c26a2017-02-18 19:46:38 +01002/*
3 * (C) Copyright 2015 Google, Inc
Heiko Stübner0b3c26a2017-02-18 19:46:38 +01004 */
5
Heiko Stübnerba188bb2017-03-20 12:40:33 +01006#include <clk.h>
Heiko Stübner0b3c26a2017-02-18 19:46:38 +01007#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>
Heiko Stuebner9cc8feb2018-10-08 13:01:56 +020019#include <asm/arch/grf_rk3188.h>
Heiko Stübner0b3c26a2017-02-18 19:46:38 +010020#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
32DECLARE_GLOBAL_DATA_PTR;
33
34u32 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 }
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +020053 ret = device_get_global_by_ofnode(offset_to_ofnode(node), &dev);
Heiko Stübner0b3c26a2017-02-18 19:46:38 +010054 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
70fallback:
71#endif
72 return BOOT_DEVICE_MMC1;
73}
74
Heiko Stübnerba188bb2017-03-20 12:40:33 +010075static int setup_arm_clock(void)
76{
77 struct udevice *dev;
78 struct clk clk;
79 int ret;
80
81 ret = rockchip_get_clk(&dev);
82 if (ret)
83 return ret;
84
85 clk.id = CLK_ARM;
86 ret = clk_request(dev, &clk);
87 if (ret < 0)
88 return ret;
89
90 ret = clk_set_rate(&clk, 600000000);
91
92 clk_free(&clk);
93 return ret;
94}
95
Kever Yangd2af98b2018-11-29 10:07:38 +080096void board_debug_uart_init(void)
Heiko Stübner0b3c26a2017-02-18 19:46:38 +010097{
Kever Yangd2af98b2018-11-29 10:07:38 +080098 /* Enable early UART on the RK3188 */
99#define GRF_BASE 0x20008000
100 struct rk3188_grf * const grf = (void *)GRF_BASE;
Heiko Stuebnerc524eeb2018-10-08 13:01:57 +0200101 enum {
102 GPIO1B1_SHIFT = 2,
103 GPIO1B1_MASK = 3,
Kever Yangd2af98b2018-11-29 10:07:38 +0800104 GPIO1B1_GPIO = 0,
105 GPIO1B1_UART2_SOUT,
Heiko Stuebnerc524eeb2018-10-08 13:01:57 +0200106
107 GPIO1B0_SHIFT = 0,
108 GPIO1B0_MASK = 3,
Kever Yangd2af98b2018-11-29 10:07:38 +0800109 GPIO1B0_GPIO = 0,
110 GPIO1B0_UART2_SIN,
Heiko Stuebnerc524eeb2018-10-08 13:01:57 +0200111 };
112
Heiko Stübner0b3c26a2017-02-18 19:46:38 +0100113 /* Enable early UART on the RK3188 */
Heiko Stübner0b3c26a2017-02-18 19:46:38 +0100114 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);
Kever Yangd2af98b2018-11-29 10:07:38 +0800119}
120
121void board_init_f(ulong dummy)
122{
123 struct udevice *pinctrl, *dev;
124 int ret;
125
126#define EARLY_UART
127#ifdef EARLY_UART
Heiko Stübner0b3c26a2017-02-18 19:46:38 +0100128 /*
129 * Debug UART can be used from here if required:
130 *
131 * debug_uart_init();
132 * printch('a');
133 * printhex8(0x1234);
134 * printascii("string");
135 */
136 debug_uart_init();
137 printch('s');
138 printch('p');
139 printch('l');
140 printch('\n');
141#endif
142
Heiko Stuebner9cc8feb2018-10-08 13:01:56 +0200143#ifdef CONFIG_ROCKCHIP_USB_UART
144 rk_clrsetreg(&grf->uoc0_con[0],
145 SIDDQ_MASK | UOC_DISABLE_MASK | COMMON_ON_N_MASK,
146 1 << SIDDQ_SHIFT | 1 << UOC_DISABLE_SHIFT |
147 1 << COMMON_ON_N_SHIFT);
148 rk_clrsetreg(&grf->uoc0_con[2],
149 SOFT_CON_SEL_MASK, 1 << SOFT_CON_SEL_SHIFT);
150 rk_clrsetreg(&grf->uoc0_con[3],
151 OPMODE_MASK | XCVRSELECT_MASK |
152 TERMSEL_FULLSPEED_MASK | SUSPENDN_MASK,
153 OPMODE_NODRIVING << OPMODE_SHIFT |
154 XCVRSELECT_FSTRANSC << XCVRSELECT_SHIFT |
155 1 << TERMSEL_FULLSPEED_SHIFT |
156 1 << SUSPENDN_SHIFT);
157 rk_clrsetreg(&grf->uoc0_con[0],
158 BYPASSSEL_MASK | BYPASSDMEN_MASK,
159 1 << BYPASSSEL_SHIFT | 1 << BYPASSDMEN_SHIFT);
160#endif
161
Kever Yange603a3d2017-03-20 14:47:16 +0800162 ret = spl_early_init();
Heiko Stübner0b3c26a2017-02-18 19:46:38 +0100163 if (ret) {
Kever Yange603a3d2017-03-20 14:47:16 +0800164 debug("spl_early_init() failed: %d\n", ret);
Heiko Stübner0b3c26a2017-02-18 19:46:38 +0100165 hang();
166 }
167
Heiko Stübner0b3c26a2017-02-18 19:46:38 +0100168 ret = rockchip_get_clk(&dev);
169 if (ret) {
170 debug("CLK init failed: %d\n", ret);
171 return;
172 }
173
Heiko Stübner0b3c26a2017-02-18 19:46:38 +0100174 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
175 if (ret) {
176 debug("Pinctrl init failed: %d\n", ret);
177 return;
178 }
179
180 ret = uclass_get_device(UCLASS_RAM, 0, &dev);
181 if (ret) {
182 debug("DRAM init failed: %d\n", ret);
183 return;
184 }
185
Heiko Stübnerba188bb2017-03-20 12:40:33 +0100186 setup_arm_clock();
Philipp Tomsich798370f2017-06-29 11:21:15 +0200187#if CONFIG_IS_ENABLED(ROCKCHIP_BACK_TO_BROM) && !defined(CONFIG_SPL_BOARD_INIT)
Philipp Tomsich7234c732017-10-10 16:21:16 +0200188 back_to_bootrom(BROM_BOOT_NEXTSTAGE);
Heiko Stübner0b3c26a2017-02-18 19:46:38 +0100189#endif
190}
191
192static int setup_led(void)
193{
194#ifdef CONFIG_SPL_LED
195 struct udevice *dev;
196 char *led_name;
197 int ret;
198
199 led_name = fdtdec_get_config_string(gd->fdt_blob, "u-boot,boot-led");
200 if (!led_name)
201 return 0;
202 ret = led_get_by_label(led_name, &dev);
203 if (ret) {
204 debug("%s: get=%d\n", __func__, ret);
205 return ret;
206 }
207 ret = led_set_on(dev, 1);
208 if (ret)
209 return ret;
210#endif
211
212 return 0;
213}
214
215void spl_board_init(void)
216{
217 struct udevice *pinctrl;
218 int ret;
219
220 ret = setup_led();
221 if (ret) {
222 debug("LED ret=%d\n", ret);
223 hang();
224 }
225
226 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
227 if (ret) {
228 debug("%s: Cannot find pinctrl device\n", __func__);
229 goto err;
230 }
231
232#ifdef CONFIG_SPL_MMC_SUPPORT
233 ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_SDCARD);
234 if (ret) {
235 debug("%s: Failed to set up SD card\n", __func__);
236 goto err;
237 }
238#endif
239
240 /* Enable debug UART */
241 ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_UART_DBG);
242 if (ret) {
243 debug("%s: Failed to set up console UART\n", __func__);
244 goto err;
245 }
246
247 preloader_console_init();
Philipp Tomsich798370f2017-06-29 11:21:15 +0200248#if CONFIG_IS_ENABLED(ROCKCHIP_BACK_TO_BROM)
Philipp Tomsich7234c732017-10-10 16:21:16 +0200249 back_to_bootrom(BROM_BOOT_NEXTSTAGE);
Heiko Stübner0b3c26a2017-02-18 19:46:38 +0100250#endif
251 return;
252
253err:
254 printf("spl_board_init: Error %d\n", ret);
255
256 /* No way to report error here */
257 hang();
258}