blob: 03ac0b42fb02af152731def5b2b7858280f4fd3d [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>
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/clock.h>
18#include <asm/arch/hardware.h>
19#include <asm/arch/periph.h>
20#include <asm/arch/sdram.h>
huang lincc2244b2015-11-17 14:20:09 +080021#include <asm/arch/timer.h>
Simon Glass2444dae2015-08-30 16:55:38 -060022#include <dm/pinctrl.h>
23#include <dm/root.h>
24#include <dm/test.h>
25#include <dm/util.h>
26#include <power/regulator.h>
27
28DECLARE_GLOBAL_DATA_PTR;
29
30u32 spl_boot_device(void)
31{
Simon Glass6afc4662016-07-04 11:58:32 -060032#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass2444dae2015-08-30 16:55:38 -060033 const void *blob = gd->fdt_blob;
34 struct udevice *dev;
35 const char *bootdev;
36 int node;
37 int ret;
38
39 bootdev = fdtdec_get_config_string(blob, "u-boot,boot0");
40 debug("Boot device %s\n", bootdev);
41 if (!bootdev)
42 goto fallback;
43
44 node = fdt_path_offset(blob, bootdev);
45 if (node < 0) {
46 debug("node=%d\n", node);
47 goto fallback;
48 }
49 ret = device_get_global_by_of_offset(node, &dev);
50 if (ret) {
51 debug("device at node %s/%d not found: %d\n", bootdev, node,
52 ret);
53 goto fallback;
54 }
55 debug("Found device %s\n", dev->name);
56 switch (device_get_uclass_id(dev)) {
57 case UCLASS_SPI_FLASH:
58 return BOOT_DEVICE_SPI;
59 case UCLASS_MMC:
60 return BOOT_DEVICE_MMC1;
61 default:
62 debug("Booting from device uclass '%s' not supported\n",
63 dev_get_uclass_name(dev));
64 }
65
66fallback:
Simon Glasse70408c2016-11-13 14:22:16 -070067#elif defined(CONFIG_TARGET_CHROMEBOOK_JERRY) || \
68 defined(CONFIG_TARGET_CHROMEBIT_MICKEY)
Simon Glassc8816d12016-11-13 14:21:57 -070069 return BOOT_DEVICE_SPI;
Simon Glass6afc4662016-07-04 11:58:32 -060070#endif
Simon Glass2444dae2015-08-30 16:55:38 -060071 return BOOT_DEVICE_MMC1;
72}
73
Marek Vasut2b1cdaf2016-05-14 23:42:07 +020074u32 spl_boot_mode(const u32 boot_device)
Simon Glass2444dae2015-08-30 16:55:38 -060075{
76 return MMCSD_MODE_RAW;
77}
78
79/* read L2 control register (L2CTLR) */
80static inline uint32_t read_l2ctlr(void)
81{
82 uint32_t val = 0;
83
84 asm volatile ("mrc p15, 1, %0, c9, c0, 2" : "=r" (val));
85
86 return val;
87}
88
89/* write L2 control register (L2CTLR) */
90static inline void write_l2ctlr(uint32_t val)
91{
92 /*
93 * Note: L2CTLR can only be written when the L2 memory system
94 * is idle, ie before the MMU is enabled.
95 */
96 asm volatile("mcr p15, 1, %0, c9, c0, 2" : : "r" (val) : "memory");
97 isb();
98}
99
100static void configure_l2ctlr(void)
101{
102 uint32_t l2ctlr;
103
104 l2ctlr = read_l2ctlr();
105 l2ctlr &= 0xfffc0000; /* clear bit0~bit17 */
106
107 /*
108 * Data RAM write latency: 2 cycles
109 * Data RAM read latency: 2 cycles
110 * Data RAM setup latency: 1 cycle
111 * Tag RAM write latency: 1 cycle
112 * Tag RAM read latency: 1 cycle
113 * Tag RAM setup latency: 1 cycle
114 */
115 l2ctlr |= (1 << 3 | 1 << 0);
116 write_l2ctlr(l2ctlr);
117}
118
Simon Glassf23cf902016-01-21 19:45:13 -0700119#ifdef CONFIG_SPL_MMC_SUPPORT
Simon Glass2444dae2015-08-30 16:55:38 -0600120static int configure_emmc(struct udevice *pinctrl)
121{
jk.kernel@gmail.comd7ca67b2016-07-26 18:28:29 +0800122#if defined(CONFIG_TARGET_CHROMEBOOK_JERRY)
jk.kernel@gmail.com5051a772016-07-26 18:28:23 +0800123
Simon Glass2444dae2015-08-30 16:55:38 -0600124 struct gpio_desc desc;
125 int ret;
126
127 pinctrl_request_noflags(pinctrl, PERIPH_ID_EMMC);
128
129 /*
130 * TODO(sjg@chromium.org): Pick this up from device tree or perhaps
131 * use the EMMC_PWREN setting.
132 */
133 ret = dm_gpio_lookup_name("D9", &desc);
134 if (ret) {
135 debug("gpio ret=%d\n", ret);
136 return ret;
137 }
138 ret = dm_gpio_request(&desc, "emmc_pwren");
139 if (ret) {
140 debug("gpio_request ret=%d\n", ret);
141 return ret;
142 }
143 ret = dm_gpio_set_dir_flags(&desc, GPIOD_IS_OUT);
144 if (ret) {
145 debug("gpio dir ret=%d\n", ret);
146 return ret;
147 }
148 ret = dm_gpio_set_value(&desc, 1);
149 if (ret) {
150 debug("gpio value ret=%d\n", ret);
151 return ret;
152 }
jk.kernel@gmail.com5051a772016-07-26 18:28:23 +0800153#endif
Simon Glass2444dae2015-08-30 16:55:38 -0600154 return 0;
155}
Simon Glassf23cf902016-01-21 19:45:13 -0700156#endif
Xu Ziyuanb47ea792016-07-12 19:09:49 +0800157extern void back_to_bootrom(void);
Simon Glass2444dae2015-08-30 16:55:38 -0600158void board_init_f(ulong dummy)
159{
160 struct udevice *pinctrl;
161 struct udevice *dev;
162 int ret;
163
164 /* Example code showing how to enable the debug UART on RK3288 */
165#ifdef EARLY_UART
166#include <asm/arch/grf_rk3288.h>
167 /* Enable early UART on the RK3288 */
168#define GRF_BASE 0xff770000
169 struct rk3288_grf * const grf = (void *)GRF_BASE;
170
171 rk_clrsetreg(&grf->gpio7ch_iomux, GPIO7C7_MASK << GPIO7C7_SHIFT |
172 GPIO7C6_MASK << GPIO7C6_SHIFT,
173 GPIO7C7_UART2DBG_SOUT << GPIO7C7_SHIFT |
174 GPIO7C6_UART2DBG_SIN << GPIO7C6_SHIFT);
175 /*
176 * Debug UART can be used from here if required:
177 *
178 * debug_uart_init();
179 * printch('a');
180 * printhex8(0x1234);
181 * printascii("string");
182 */
183 debug_uart_init();
184#endif
185
186 ret = spl_init();
187 if (ret) {
188 debug("spl_init() failed: %d\n", ret);
189 hang();
190 }
191
huang lincc2244b2015-11-17 14:20:09 +0800192 rockchip_timer_init();
Simon Glass2444dae2015-08-30 16:55:38 -0600193 configure_l2ctlr();
194
Simon Glassc3aad6f2016-07-17 15:23:17 -0600195 ret = rockchip_get_clk(&dev);
Simon Glass2444dae2015-08-30 16:55:38 -0600196 if (ret) {
197 debug("CLK init failed: %d\n", ret);
198 return;
199 }
200
201 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
202 if (ret) {
203 debug("Pinctrl init failed: %d\n", ret);
204 return;
205 }
206
207 ret = uclass_get_device(UCLASS_RAM, 0, &dev);
208 if (ret) {
209 debug("DRAM init failed: %d\n", ret);
210 return;
211 }
Sandy Patterson427351d2016-08-10 10:21:47 -0400212#if defined(CONFIG_ROCKCHIP_SPL_BACK_TO_BROM) && !defined(CONFIG_SPL_BOARD_INIT)
Xu Ziyuanb47ea792016-07-12 19:09:49 +0800213 back_to_bootrom();
214#endif
Simon Glass2444dae2015-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.com5051a772016-07-26 18:28:23 +0800257
Simon Glassf23cf902016-01-21 19:45:13 -0700258#ifdef CONFIG_SPL_MMC_SUPPORT
jk.kernel@gmail.com5051a772016-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 Glass2444dae2015-08-30 16:55:38 -0600268 }
Simon Glassf23cf902016-01-21 19:45:13 -0700269#endif
Simon Glass2444dae2015-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();
Sandy Patterson427351d2016-08-10 10:21:47 -0400279#ifdef CONFIG_ROCKCHIP_SPL_BACK_TO_BROM
280 back_to_bootrom();
281#endif
Simon Glass2444dae2015-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}