blob: abd62e520fada7ff920ecf0fee9b463201062ab8 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass2cffe662015-08-30 16:55:38 -06002/*
3 * (C) Copyright 2015 Google, Inc
Simon Glass2cffe662015-08-30 16:55:38 -06004 */
5
6#include <common.h>
7#include <debug_uart.h>
8#include <dm.h>
9#include <fdtdec.h>
Wadim Egorov6ee2d012017-06-19 12:36:40 +020010#include <i2c.h>
Simon Glass2cffe662015-08-30 16:55:38 -060011#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>
Heiko Stübner5c43acb2017-02-18 19:46:26 +010017#include <asm/arch/bootrom.h>
Simon Glass2cffe662015-08-30 16:55:38 -060018#include <asm/arch/clock.h>
19#include <asm/arch/hardware.h>
20#include <asm/arch/periph.h>
Jagan Teki536e4d32017-09-27 23:03:14 +053021#include <asm/arch/pmu_rk3288.h>
Simon Glass2cffe662015-08-30 16:55:38 -060022#include <asm/arch/sdram.h>
Jagan Teki536e4d32017-09-27 23:03:14 +053023#include <asm/arch/sdram_common.h>
Jagan Tekib0f52952017-09-27 23:03:11 +053024#include <asm/arch/sys_proto.h>
huang lin8db3e242015-11-17 14:20:09 +080025#include <asm/arch/timer.h>
Simon Glass2cffe662015-08-30 16:55:38 -060026#include <dm/pinctrl.h>
27#include <dm/root.h>
28#include <dm/test.h>
29#include <dm/util.h>
30#include <power/regulator.h>
Wadim Egorov6ee2d012017-06-19 12:36:40 +020031#include <power/rk8xx_pmic.h>
Simon Glass2cffe662015-08-30 16:55:38 -060032
33DECLARE_GLOBAL_DATA_PTR;
34
35u32 spl_boot_device(void)
36{
Simon Glass26158ef2016-07-04 11:58:32 -060037#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass2cffe662015-08-30 16:55:38 -060038 const void *blob = gd->fdt_blob;
39 struct udevice *dev;
40 const char *bootdev;
41 int node;
42 int ret;
43
44 bootdev = fdtdec_get_config_string(blob, "u-boot,boot0");
45 debug("Boot device %s\n", bootdev);
46 if (!bootdev)
47 goto fallback;
48
49 node = fdt_path_offset(blob, bootdev);
50 if (node < 0) {
51 debug("node=%d\n", node);
52 goto fallback;
53 }
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +020054 ret = device_get_global_by_ofnode(offset_to_ofnode(node), &dev);
Simon Glass2cffe662015-08-30 16:55:38 -060055 if (ret) {
56 debug("device at node %s/%d not found: %d\n", bootdev, node,
57 ret);
58 goto fallback;
59 }
60 debug("Found device %s\n", dev->name);
61 switch (device_get_uclass_id(dev)) {
62 case UCLASS_SPI_FLASH:
63 return BOOT_DEVICE_SPI;
64 case UCLASS_MMC:
65 return BOOT_DEVICE_MMC1;
66 default:
67 debug("Booting from device uclass '%s' not supported\n",
68 dev_get_uclass_name(dev));
69 }
70
71fallback:
Simon Glassbf8d7bf2016-11-13 14:22:16 -070072#elif defined(CONFIG_TARGET_CHROMEBOOK_JERRY) || \
Simon Glasse51b2e72016-11-13 14:24:54 -070073 defined(CONFIG_TARGET_CHROMEBIT_MICKEY) || \
74 defined(CONFIG_TARGET_CHROMEBOOK_MINNIE)
Simon Glass6f9087c2016-11-13 14:21:57 -070075 return BOOT_DEVICE_SPI;
Simon Glass26158ef2016-07-04 11:58:32 -060076#endif
Simon Glass2cffe662015-08-30 16:55:38 -060077 return BOOT_DEVICE_MMC1;
78}
79
Simon Glass4ba12932016-01-21 19:45:13 -070080#ifdef CONFIG_SPL_MMC_SUPPORT
Simon Glass2cffe662015-08-30 16:55:38 -060081static int configure_emmc(struct udevice *pinctrl)
82{
jk.kernel@gmail.comb1aeb092016-07-26 18:28:29 +080083#if defined(CONFIG_TARGET_CHROMEBOOK_JERRY)
jk.kernel@gmail.com2b87b932016-07-26 18:28:23 +080084
Simon Glass2cffe662015-08-30 16:55:38 -060085 struct gpio_desc desc;
86 int ret;
87
88 pinctrl_request_noflags(pinctrl, PERIPH_ID_EMMC);
89
90 /*
91 * TODO(sjg@chromium.org): Pick this up from device tree or perhaps
92 * use the EMMC_PWREN setting.
93 */
94 ret = dm_gpio_lookup_name("D9", &desc);
95 if (ret) {
96 debug("gpio ret=%d\n", ret);
97 return ret;
98 }
99 ret = dm_gpio_request(&desc, "emmc_pwren");
100 if (ret) {
101 debug("gpio_request ret=%d\n", ret);
102 return ret;
103 }
104 ret = dm_gpio_set_dir_flags(&desc, GPIOD_IS_OUT);
105 if (ret) {
106 debug("gpio dir ret=%d\n", ret);
107 return ret;
108 }
109 ret = dm_gpio_set_value(&desc, 1);
110 if (ret) {
111 debug("gpio value ret=%d\n", ret);
112 return ret;
113 }
jk.kernel@gmail.com2b87b932016-07-26 18:28:23 +0800114#endif
Simon Glass2cffe662015-08-30 16:55:38 -0600115 return 0;
116}
Simon Glass4ba12932016-01-21 19:45:13 -0700117#endif
Heiko Stübner5c43acb2017-02-18 19:46:26 +0100118
Wadim Egorov6ee2d012017-06-19 12:36:40 +0200119#if !defined(CONFIG_SPL_OF_PLATDATA)
120static int phycore_init(void)
121{
122 struct udevice *pmic;
123 int ret;
124
125 ret = uclass_first_device_err(UCLASS_PMIC, &pmic);
126 if (ret)
127 return ret;
128
129#if defined(CONFIG_SPL_POWER_SUPPORT)
130 /* Increase USB input current to 2A */
131 ret = rk818_spl_configure_usb_input_current(pmic, 2000);
132 if (ret)
133 return ret;
134
135 /* Close charger when USB lower then 3.26V */
136 ret = rk818_spl_configure_usb_chrg_shutdown(pmic, 3260000);
137 if (ret)
138 return ret;
139#endif
140
141 return 0;
142}
143#endif
144
Simon Glass2cffe662015-08-30 16:55:38 -0600145void board_init_f(ulong dummy)
146{
147 struct udevice *pinctrl;
148 struct udevice *dev;
149 int ret;
150
151 /* Example code showing how to enable the debug UART on RK3288 */
Simon Glass2cffe662015-08-30 16:55:38 -0600152#include <asm/arch/grf_rk3288.h>
153 /* Enable early UART on the RK3288 */
154#define GRF_BASE 0xff770000
155 struct rk3288_grf * const grf = (void *)GRF_BASE;
156
157 rk_clrsetreg(&grf->gpio7ch_iomux, GPIO7C7_MASK << GPIO7C7_SHIFT |
158 GPIO7C6_MASK << GPIO7C6_SHIFT,
159 GPIO7C7_UART2DBG_SOUT << GPIO7C7_SHIFT |
160 GPIO7C6_UART2DBG_SIN << GPIO7C6_SHIFT);
161 /*
162 * Debug UART can be used from here if required:
163 *
164 * debug_uart_init();
165 * printch('a');
166 * printhex8(0x1234);
167 * printascii("string");
168 */
169 debug_uart_init();
Eddie Cai9d62e822017-04-18 19:17:27 +0800170 debug("\nspl:debug uart enabled in %s\n", __func__);
Eddie Cai3e2b61c2017-03-15 08:43:29 -0600171 ret = spl_early_init();
Simon Glass2cffe662015-08-30 16:55:38 -0600172 if (ret) {
Eddie Cai3e2b61c2017-03-15 08:43:29 -0600173 debug("spl_early_init() failed: %d\n", ret);
Simon Glass2cffe662015-08-30 16:55:38 -0600174 hang();
175 }
176
huang lin8db3e242015-11-17 14:20:09 +0800177 rockchip_timer_init();
Simon Glass2cffe662015-08-30 16:55:38 -0600178 configure_l2ctlr();
179
Simon Glassae8fe412016-07-17 15:23:17 -0600180 ret = rockchip_get_clk(&dev);
Simon Glass2cffe662015-08-30 16:55:38 -0600181 if (ret) {
182 debug("CLK init failed: %d\n", ret);
183 return;
184 }
185
186 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
187 if (ret) {
188 debug("Pinctrl init failed: %d\n", ret);
189 return;
190 }
Wadim Egorov6ee2d012017-06-19 12:36:40 +0200191
192#if !defined(CONFIG_SPL_OF_PLATDATA)
193 if (of_machine_is_compatible("phytec,rk3288-phycore-som")) {
194 ret = phycore_init();
195 if (ret) {
196 debug("Failed to set up phycore power settings: %d\n",
197 ret);
198 return;
199 }
200 }
201#endif
202
Jagan Teki387fd4b2017-09-27 23:03:12 +0530203#if !defined(CONFIG_SUPPORT_TPL)
Eddie Cai9d62e822017-04-18 19:17:27 +0800204 debug("\nspl:init dram\n");
Simon Glass2cffe662015-08-30 16:55:38 -0600205 ret = uclass_get_device(UCLASS_RAM, 0, &dev);
206 if (ret) {
207 debug("DRAM init failed: %d\n", ret);
208 return;
209 }
Jagan Teki387fd4b2017-09-27 23:03:12 +0530210#endif
211
Philipp Tomsich798370f2017-06-29 11:21:15 +0200212#if CONFIG_IS_ENABLED(ROCKCHIP_BACK_TO_BROM) && !defined(CONFIG_SPL_BOARD_INIT)
Philipp Tomsich7234c732017-10-10 16:21:16 +0200213 back_to_bootrom(BROM_BOOT_NEXTSTAGE);
Xu Ziyuan5401eb82016-07-12 19:09:49 +0800214#endif
Simon Glass2cffe662015-08-30 16:55:38 -0600215}
216
217static int setup_led(void)
218{
219#ifdef CONFIG_SPL_LED
220 struct udevice *dev;
221 char *led_name;
222 int ret;
223
224 led_name = fdtdec_get_config_string(gd->fdt_blob, "u-boot,boot-led");
225 if (!led_name)
226 return 0;
227 ret = led_get_by_label(led_name, &dev);
228 if (ret) {
229 debug("%s: get=%d\n", __func__, ret);
230 return ret;
231 }
232 ret = led_set_on(dev, 1);
233 if (ret)
234 return ret;
235#endif
236
237 return 0;
238}
239
240void spl_board_init(void)
241{
242 struct udevice *pinctrl;
243 int ret;
244
245 ret = setup_led();
246
247 if (ret) {
248 debug("LED ret=%d\n", ret);
249 hang();
250 }
251
252 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
253 if (ret) {
254 debug("%s: Cannot find pinctrl device\n", __func__);
255 goto err;
256 }
jk.kernel@gmail.com2b87b932016-07-26 18:28:23 +0800257
Simon Glass4ba12932016-01-21 19:45:13 -0700258#ifdef CONFIG_SPL_MMC_SUPPORT
jk.kernel@gmail.com2b87b932016-07-26 18:28:23 +0800259 ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_SDCARD);
260 if (ret) {
261 debug("%s: Failed to set up SD card\n", __func__);
262 goto err;
263 }
264 ret = configure_emmc(pinctrl);
265 if (ret) {
266 debug("%s: Failed to set up eMMC\n", __func__);
267 goto err;
Simon Glass2cffe662015-08-30 16:55:38 -0600268 }
Simon Glass4ba12932016-01-21 19:45:13 -0700269#endif
Simon Glass2cffe662015-08-30 16:55:38 -0600270
271 /* Enable debug UART */
272 ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_UART_DBG);
273 if (ret) {
274 debug("%s: Failed to set up console UART\n", __func__);
275 goto err;
276 }
277
278 preloader_console_init();
Philipp Tomsich798370f2017-06-29 11:21:15 +0200279#if CONFIG_IS_ENABLED(ROCKCHIP_BACK_TO_BROM)
Philipp Tomsich7234c732017-10-10 16:21:16 +0200280 back_to_bootrom(BROM_BOOT_NEXTSTAGE);
Sandy Pattersona9e92ee2016-08-10 10:21:47 -0400281#endif
Simon Glass2cffe662015-08-30 16:55:38 -0600282 return;
283err:
284 printf("spl_board_init: Error %d\n", ret);
285
286 /* No way to report error here */
287 hang();
288}
Jagan Teki536e4d32017-09-27 23:03:14 +0530289
290#ifdef CONFIG_SPL_OS_BOOT
291
292#define PMU_BASE 0xff730000
293int dram_init_banksize(void)
294{
295 struct rk3288_pmu *const pmu = (void *)PMU_BASE;
296 size_t size = rockchip_sdram_size((phys_addr_t)&pmu->sys_reg[2]);
297
298 gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
299 gd->bd->bi_dram[0].size = size;
300
301 return 0;
302}
303#endif