blob: 6b7bf85d8d3347aaa3f2363935f971978f1830c0 [file] [log] [blame]
Simon Glass2444dae2015-08-30 16:55:38 -06001/*
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>
Wadim Egorovbafcf2d2017-06-19 12:36:40 +020011#include <i2c.h>
Simon Glass2444dae2015-08-30 16:55:38 -060012#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>
Heiko Stübneraade0772017-02-18 19:46:26 +010018#include <asm/arch/bootrom.h>
Simon Glass2444dae2015-08-30 16:55:38 -060019#include <asm/arch/clock.h>
20#include <asm/arch/hardware.h>
21#include <asm/arch/periph.h>
22#include <asm/arch/sdram.h>
huang lincc2244b2015-11-17 14:20:09 +080023#include <asm/arch/timer.h>
Simon Glass2444dae2015-08-30 16:55:38 -060024#include <dm/pinctrl.h>
25#include <dm/root.h>
26#include <dm/test.h>
27#include <dm/util.h>
28#include <power/regulator.h>
Wadim Egorovbafcf2d2017-06-19 12:36:40 +020029#include <power/rk8xx_pmic.h>
Simon Glass2444dae2015-08-30 16:55:38 -060030
31DECLARE_GLOBAL_DATA_PTR;
32
33u32 spl_boot_device(void)
34{
Simon Glass6afc4662016-07-04 11:58:32 -060035#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass2444dae2015-08-30 16:55:38 -060036 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
69fallback:
Simon Glasse70408c2016-11-13 14:22:16 -070070#elif defined(CONFIG_TARGET_CHROMEBOOK_JERRY) || \
Simon Glassc420ef62016-11-13 14:24:54 -070071 defined(CONFIG_TARGET_CHROMEBIT_MICKEY) || \
72 defined(CONFIG_TARGET_CHROMEBOOK_MINNIE)
Simon Glassc8816d12016-11-13 14:21:57 -070073 return BOOT_DEVICE_SPI;
Simon Glass6afc4662016-07-04 11:58:32 -060074#endif
Simon Glass2444dae2015-08-30 16:55:38 -060075 return BOOT_DEVICE_MMC1;
76}
77
Marek Vasut2b1cdaf2016-05-14 23:42:07 +020078u32 spl_boot_mode(const u32 boot_device)
Simon Glass2444dae2015-08-30 16:55:38 -060079{
80 return MMCSD_MODE_RAW;
81}
82
83/* read L2 control register (L2CTLR) */
84static inline uint32_t read_l2ctlr(void)
85{
86 uint32_t val = 0;
87
88 asm volatile ("mrc p15, 1, %0, c9, c0, 2" : "=r" (val));
89
90 return val;
91}
92
93/* write L2 control register (L2CTLR) */
94static inline void write_l2ctlr(uint32_t val)
95{
96 /*
97 * Note: L2CTLR can only be written when the L2 memory system
98 * is idle, ie before the MMU is enabled.
99 */
100 asm volatile("mcr p15, 1, %0, c9, c0, 2" : : "r" (val) : "memory");
101 isb();
102}
103
104static void configure_l2ctlr(void)
105{
106 uint32_t l2ctlr;
107
108 l2ctlr = read_l2ctlr();
109 l2ctlr &= 0xfffc0000; /* clear bit0~bit17 */
110
111 /*
112 * Data RAM write latency: 2 cycles
113 * Data RAM read latency: 2 cycles
114 * Data RAM setup latency: 1 cycle
115 * Tag RAM write latency: 1 cycle
116 * Tag RAM read latency: 1 cycle
117 * Tag RAM setup latency: 1 cycle
118 */
119 l2ctlr |= (1 << 3 | 1 << 0);
120 write_l2ctlr(l2ctlr);
121}
122
Simon Glassf23cf902016-01-21 19:45:13 -0700123#ifdef CONFIG_SPL_MMC_SUPPORT
Simon Glass2444dae2015-08-30 16:55:38 -0600124static int configure_emmc(struct udevice *pinctrl)
125{
jk.kernel@gmail.comd7ca67b2016-07-26 18:28:29 +0800126#if defined(CONFIG_TARGET_CHROMEBOOK_JERRY)
jk.kernel@gmail.com5051a772016-07-26 18:28:23 +0800127
Simon Glass2444dae2015-08-30 16:55:38 -0600128 struct gpio_desc desc;
129 int ret;
130
131 pinctrl_request_noflags(pinctrl, PERIPH_ID_EMMC);
132
133 /*
134 * TODO(sjg@chromium.org): Pick this up from device tree or perhaps
135 * use the EMMC_PWREN setting.
136 */
137 ret = dm_gpio_lookup_name("D9", &desc);
138 if (ret) {
139 debug("gpio ret=%d\n", ret);
140 return ret;
141 }
142 ret = dm_gpio_request(&desc, "emmc_pwren");
143 if (ret) {
144 debug("gpio_request ret=%d\n", ret);
145 return ret;
146 }
147 ret = dm_gpio_set_dir_flags(&desc, GPIOD_IS_OUT);
148 if (ret) {
149 debug("gpio dir ret=%d\n", ret);
150 return ret;
151 }
152 ret = dm_gpio_set_value(&desc, 1);
153 if (ret) {
154 debug("gpio value ret=%d\n", ret);
155 return ret;
156 }
jk.kernel@gmail.com5051a772016-07-26 18:28:23 +0800157#endif
Simon Glass2444dae2015-08-30 16:55:38 -0600158 return 0;
159}
Simon Glassf23cf902016-01-21 19:45:13 -0700160#endif
Heiko Stübneraade0772017-02-18 19:46:26 +0100161
Wadim Egorovbafcf2d2017-06-19 12:36:40 +0200162#if !defined(CONFIG_SPL_OF_PLATDATA)
163static int phycore_init(void)
164{
165 struct udevice *pmic;
166 int ret;
167
168 ret = uclass_first_device_err(UCLASS_PMIC, &pmic);
169 if (ret)
170 return ret;
171
172#if defined(CONFIG_SPL_POWER_SUPPORT)
173 /* Increase USB input current to 2A */
174 ret = rk818_spl_configure_usb_input_current(pmic, 2000);
175 if (ret)
176 return ret;
177
178 /* Close charger when USB lower then 3.26V */
179 ret = rk818_spl_configure_usb_chrg_shutdown(pmic, 3260000);
180 if (ret)
181 return ret;
182#endif
183
184 return 0;
185}
186#endif
187
Simon Glass2444dae2015-08-30 16:55:38 -0600188void board_init_f(ulong dummy)
189{
190 struct udevice *pinctrl;
191 struct udevice *dev;
192 int ret;
193
194 /* Example code showing how to enable the debug UART on RK3288 */
Simon Glass2444dae2015-08-30 16:55:38 -0600195#include <asm/arch/grf_rk3288.h>
196 /* Enable early UART on the RK3288 */
197#define GRF_BASE 0xff770000
198 struct rk3288_grf * const grf = (void *)GRF_BASE;
199
200 rk_clrsetreg(&grf->gpio7ch_iomux, GPIO7C7_MASK << GPIO7C7_SHIFT |
201 GPIO7C6_MASK << GPIO7C6_SHIFT,
202 GPIO7C7_UART2DBG_SOUT << GPIO7C7_SHIFT |
203 GPIO7C6_UART2DBG_SIN << GPIO7C6_SHIFT);
204 /*
205 * Debug UART can be used from here if required:
206 *
207 * debug_uart_init();
208 * printch('a');
209 * printhex8(0x1234);
210 * printascii("string");
211 */
212 debug_uart_init();
Eddie Cai7474bbe2017-04-18 19:17:27 +0800213 debug("\nspl:debug uart enabled in %s\n", __func__);
Eddie Cai73976052017-03-15 08:43:29 -0600214 ret = spl_early_init();
Simon Glass2444dae2015-08-30 16:55:38 -0600215 if (ret) {
Eddie Cai73976052017-03-15 08:43:29 -0600216 debug("spl_early_init() failed: %d\n", ret);
Simon Glass2444dae2015-08-30 16:55:38 -0600217 hang();
218 }
219
huang lincc2244b2015-11-17 14:20:09 +0800220 rockchip_timer_init();
Simon Glass2444dae2015-08-30 16:55:38 -0600221 configure_l2ctlr();
222
Simon Glassc3aad6f2016-07-17 15:23:17 -0600223 ret = rockchip_get_clk(&dev);
Simon Glass2444dae2015-08-30 16:55:38 -0600224 if (ret) {
225 debug("CLK init failed: %d\n", ret);
226 return;
227 }
228
229 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
230 if (ret) {
231 debug("Pinctrl init failed: %d\n", ret);
232 return;
233 }
Wadim Egorovbafcf2d2017-06-19 12:36:40 +0200234
235#if !defined(CONFIG_SPL_OF_PLATDATA)
236 if (of_machine_is_compatible("phytec,rk3288-phycore-som")) {
237 ret = phycore_init();
238 if (ret) {
239 debug("Failed to set up phycore power settings: %d\n",
240 ret);
241 return;
242 }
243 }
244#endif
245
Eddie Cai7474bbe2017-04-18 19:17:27 +0800246 debug("\nspl:init dram\n");
Simon Glass2444dae2015-08-30 16:55:38 -0600247 ret = uclass_get_device(UCLASS_RAM, 0, &dev);
248 if (ret) {
249 debug("DRAM init failed: %d\n", ret);
250 return;
251 }
Philipp Tomsichee14d292017-06-29 11:21:15 +0200252#if CONFIG_IS_ENABLED(ROCKCHIP_BACK_TO_BROM) && !defined(CONFIG_SPL_BOARD_INIT)
Xu Ziyuanb47ea792016-07-12 19:09:49 +0800253 back_to_bootrom();
254#endif
Simon Glass2444dae2015-08-30 16:55:38 -0600255}
256
257static int setup_led(void)
258{
259#ifdef CONFIG_SPL_LED
260 struct udevice *dev;
261 char *led_name;
262 int ret;
263
264 led_name = fdtdec_get_config_string(gd->fdt_blob, "u-boot,boot-led");
265 if (!led_name)
266 return 0;
267 ret = led_get_by_label(led_name, &dev);
268 if (ret) {
269 debug("%s: get=%d\n", __func__, ret);
270 return ret;
271 }
272 ret = led_set_on(dev, 1);
273 if (ret)
274 return ret;
275#endif
276
277 return 0;
278}
279
280void spl_board_init(void)
281{
282 struct udevice *pinctrl;
283 int ret;
284
285 ret = setup_led();
286
287 if (ret) {
288 debug("LED ret=%d\n", ret);
289 hang();
290 }
291
292 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
293 if (ret) {
294 debug("%s: Cannot find pinctrl device\n", __func__);
295 goto err;
296 }
jk.kernel@gmail.com5051a772016-07-26 18:28:23 +0800297
Simon Glassf23cf902016-01-21 19:45:13 -0700298#ifdef CONFIG_SPL_MMC_SUPPORT
jk.kernel@gmail.com5051a772016-07-26 18:28:23 +0800299 ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_SDCARD);
300 if (ret) {
301 debug("%s: Failed to set up SD card\n", __func__);
302 goto err;
303 }
304 ret = configure_emmc(pinctrl);
305 if (ret) {
306 debug("%s: Failed to set up eMMC\n", __func__);
307 goto err;
Simon Glass2444dae2015-08-30 16:55:38 -0600308 }
Simon Glassf23cf902016-01-21 19:45:13 -0700309#endif
Simon Glass2444dae2015-08-30 16:55:38 -0600310
311 /* Enable debug UART */
312 ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_UART_DBG);
313 if (ret) {
314 debug("%s: Failed to set up console UART\n", __func__);
315 goto err;
316 }
317
318 preloader_console_init();
Philipp Tomsichee14d292017-06-29 11:21:15 +0200319#if CONFIG_IS_ENABLED(ROCKCHIP_BACK_TO_BROM)
Sandy Patterson427351d2016-08-10 10:21:47 -0400320 back_to_bootrom();
321#endif
Simon Glass2444dae2015-08-30 16:55:38 -0600322 return;
323err:
324 printf("spl_board_init: Error %d\n", ret);
325
326 /* No way to report error here */
327 hang();
328}