blob: 930939ad24c4bc9a3b97850f3db32959819290ea [file] [log] [blame]
Simon Glass2cffe662015-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 lin8db3e242015-11-17 14:20:09 +080021#include <asm/arch/timer.h>
Simon Glass2cffe662015-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 Glass26158ef2016-07-04 11:58:32 -060032#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass2cffe662015-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 Glassbf8d7bf2016-11-13 14:22:16 -070067#elif defined(CONFIG_TARGET_CHROMEBOOK_JERRY) || \
Simon Glasse51b2e72016-11-13 14:24:54 -070068 defined(CONFIG_TARGET_CHROMEBIT_MICKEY) || \
69 defined(CONFIG_TARGET_CHROMEBOOK_MINNIE)
Simon Glass6f9087c2016-11-13 14:21:57 -070070 return BOOT_DEVICE_SPI;
Simon Glass26158ef2016-07-04 11:58:32 -060071#endif
Simon Glass2cffe662015-08-30 16:55:38 -060072 return BOOT_DEVICE_MMC1;
73}
74
Marek Vasut64d64bb2016-05-14 23:42:07 +020075u32 spl_boot_mode(const u32 boot_device)
Simon Glass2cffe662015-08-30 16:55:38 -060076{
77 return MMCSD_MODE_RAW;
78}
79
80/* read L2 control register (L2CTLR) */
81static inline uint32_t read_l2ctlr(void)
82{
83 uint32_t val = 0;
84
85 asm volatile ("mrc p15, 1, %0, c9, c0, 2" : "=r" (val));
86
87 return val;
88}
89
90/* write L2 control register (L2CTLR) */
91static inline void write_l2ctlr(uint32_t val)
92{
93 /*
94 * Note: L2CTLR can only be written when the L2 memory system
95 * is idle, ie before the MMU is enabled.
96 */
97 asm volatile("mcr p15, 1, %0, c9, c0, 2" : : "r" (val) : "memory");
98 isb();
99}
100
101static void configure_l2ctlr(void)
102{
103 uint32_t l2ctlr;
104
105 l2ctlr = read_l2ctlr();
106 l2ctlr &= 0xfffc0000; /* clear bit0~bit17 */
107
108 /*
109 * Data RAM write latency: 2 cycles
110 * Data RAM read latency: 2 cycles
111 * Data RAM setup latency: 1 cycle
112 * Tag RAM write latency: 1 cycle
113 * Tag RAM read latency: 1 cycle
114 * Tag RAM setup latency: 1 cycle
115 */
116 l2ctlr |= (1 << 3 | 1 << 0);
117 write_l2ctlr(l2ctlr);
118}
119
Simon Glass4ba12932016-01-21 19:45:13 -0700120#ifdef CONFIG_SPL_MMC_SUPPORT
Simon Glass2cffe662015-08-30 16:55:38 -0600121static int configure_emmc(struct udevice *pinctrl)
122{
jk.kernel@gmail.comb1aeb092016-07-26 18:28:29 +0800123#if defined(CONFIG_TARGET_CHROMEBOOK_JERRY)
jk.kernel@gmail.com2b87b932016-07-26 18:28:23 +0800124
Simon Glass2cffe662015-08-30 16:55:38 -0600125 struct gpio_desc desc;
126 int ret;
127
128 pinctrl_request_noflags(pinctrl, PERIPH_ID_EMMC);
129
130 /*
131 * TODO(sjg@chromium.org): Pick this up from device tree or perhaps
132 * use the EMMC_PWREN setting.
133 */
134 ret = dm_gpio_lookup_name("D9", &desc);
135 if (ret) {
136 debug("gpio ret=%d\n", ret);
137 return ret;
138 }
139 ret = dm_gpio_request(&desc, "emmc_pwren");
140 if (ret) {
141 debug("gpio_request ret=%d\n", ret);
142 return ret;
143 }
144 ret = dm_gpio_set_dir_flags(&desc, GPIOD_IS_OUT);
145 if (ret) {
146 debug("gpio dir ret=%d\n", ret);
147 return ret;
148 }
149 ret = dm_gpio_set_value(&desc, 1);
150 if (ret) {
151 debug("gpio value ret=%d\n", ret);
152 return ret;
153 }
jk.kernel@gmail.com2b87b932016-07-26 18:28:23 +0800154#endif
Simon Glass2cffe662015-08-30 16:55:38 -0600155 return 0;
156}
Simon Glass4ba12932016-01-21 19:45:13 -0700157#endif
Xu Ziyuan5401eb82016-07-12 19:09:49 +0800158extern void back_to_bootrom(void);
Simon Glass2cffe662015-08-30 16:55:38 -0600159void board_init_f(ulong dummy)
160{
161 struct udevice *pinctrl;
162 struct udevice *dev;
163 int ret;
164
165 /* Example code showing how to enable the debug UART on RK3288 */
166#ifdef EARLY_UART
167#include <asm/arch/grf_rk3288.h>
168 /* Enable early UART on the RK3288 */
169#define GRF_BASE 0xff770000
170 struct rk3288_grf * const grf = (void *)GRF_BASE;
171
172 rk_clrsetreg(&grf->gpio7ch_iomux, GPIO7C7_MASK << GPIO7C7_SHIFT |
173 GPIO7C6_MASK << GPIO7C6_SHIFT,
174 GPIO7C7_UART2DBG_SOUT << GPIO7C7_SHIFT |
175 GPIO7C6_UART2DBG_SIN << GPIO7C6_SHIFT);
176 /*
177 * Debug UART can be used from here if required:
178 *
179 * debug_uart_init();
180 * printch('a');
181 * printhex8(0x1234);
182 * printascii("string");
183 */
184 debug_uart_init();
185#endif
186
187 ret = spl_init();
188 if (ret) {
189 debug("spl_init() failed: %d\n", ret);
190 hang();
191 }
192
huang lin8db3e242015-11-17 14:20:09 +0800193 rockchip_timer_init();
Simon Glass2cffe662015-08-30 16:55:38 -0600194 configure_l2ctlr();
195
Simon Glassae8fe412016-07-17 15:23:17 -0600196 ret = rockchip_get_clk(&dev);
Simon Glass2cffe662015-08-30 16:55:38 -0600197 if (ret) {
198 debug("CLK init failed: %d\n", ret);
199 return;
200 }
201
202 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
203 if (ret) {
204 debug("Pinctrl init failed: %d\n", ret);
205 return;
206 }
207
208 ret = uclass_get_device(UCLASS_RAM, 0, &dev);
209 if (ret) {
210 debug("DRAM init failed: %d\n", ret);
211 return;
212 }
Sandy Pattersona9e92ee2016-08-10 10:21:47 -0400213#if defined(CONFIG_ROCKCHIP_SPL_BACK_TO_BROM) && !defined(CONFIG_SPL_BOARD_INIT)
Xu Ziyuan5401eb82016-07-12 19:09:49 +0800214 back_to_bootrom();
215#endif
Simon Glass2cffe662015-08-30 16:55:38 -0600216}
217
218static int setup_led(void)
219{
220#ifdef CONFIG_SPL_LED
221 struct udevice *dev;
222 char *led_name;
223 int ret;
224
225 led_name = fdtdec_get_config_string(gd->fdt_blob, "u-boot,boot-led");
226 if (!led_name)
227 return 0;
228 ret = led_get_by_label(led_name, &dev);
229 if (ret) {
230 debug("%s: get=%d\n", __func__, ret);
231 return ret;
232 }
233 ret = led_set_on(dev, 1);
234 if (ret)
235 return ret;
236#endif
237
238 return 0;
239}
240
241void spl_board_init(void)
242{
243 struct udevice *pinctrl;
244 int ret;
245
246 ret = setup_led();
247
248 if (ret) {
249 debug("LED ret=%d\n", ret);
250 hang();
251 }
252
253 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
254 if (ret) {
255 debug("%s: Cannot find pinctrl device\n", __func__);
256 goto err;
257 }
jk.kernel@gmail.com2b87b932016-07-26 18:28:23 +0800258
Simon Glass4ba12932016-01-21 19:45:13 -0700259#ifdef CONFIG_SPL_MMC_SUPPORT
jk.kernel@gmail.com2b87b932016-07-26 18:28:23 +0800260 ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_SDCARD);
261 if (ret) {
262 debug("%s: Failed to set up SD card\n", __func__);
263 goto err;
264 }
265 ret = configure_emmc(pinctrl);
266 if (ret) {
267 debug("%s: Failed to set up eMMC\n", __func__);
268 goto err;
Simon Glass2cffe662015-08-30 16:55:38 -0600269 }
Simon Glass4ba12932016-01-21 19:45:13 -0700270#endif
Simon Glass2cffe662015-08-30 16:55:38 -0600271
272 /* Enable debug UART */
273 ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_UART_DBG);
274 if (ret) {
275 debug("%s: Failed to set up console UART\n", __func__);
276 goto err;
277 }
278
279 preloader_console_init();
Sandy Pattersona9e92ee2016-08-10 10:21:47 -0400280#ifdef CONFIG_ROCKCHIP_SPL_BACK_TO_BROM
281 back_to_bootrom();
282#endif
Simon Glass2cffe662015-08-30 16:55:38 -0600283 return;
284err:
285 printf("spl_board_init: Error %d\n", ret);
286
287 /* No way to report error here */
288 hang();
289}