blob: b2c57292a75d55d39ff13046072107685f35231e [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{
32 const void *blob = gd->fdt_blob;
33 struct udevice *dev;
34 const char *bootdev;
35 int node;
36 int ret;
37
38 bootdev = fdtdec_get_config_string(blob, "u-boot,boot0");
39 debug("Boot device %s\n", bootdev);
40 if (!bootdev)
41 goto fallback;
42
43 node = fdt_path_offset(blob, bootdev);
44 if (node < 0) {
45 debug("node=%d\n", node);
46 goto fallback;
47 }
48 ret = device_get_global_by_of_offset(node, &dev);
49 if (ret) {
50 debug("device at node %s/%d not found: %d\n", bootdev, node,
51 ret);
52 goto fallback;
53 }
54 debug("Found device %s\n", dev->name);
55 switch (device_get_uclass_id(dev)) {
56 case UCLASS_SPI_FLASH:
57 return BOOT_DEVICE_SPI;
58 case UCLASS_MMC:
59 return BOOT_DEVICE_MMC1;
60 default:
61 debug("Booting from device uclass '%s' not supported\n",
62 dev_get_uclass_name(dev));
63 }
64
65fallback:
66 return BOOT_DEVICE_MMC1;
67}
68
69u32 spl_boot_mode(void)
70{
71 return MMCSD_MODE_RAW;
72}
73
74/* read L2 control register (L2CTLR) */
75static inline uint32_t read_l2ctlr(void)
76{
77 uint32_t val = 0;
78
79 asm volatile ("mrc p15, 1, %0, c9, c0, 2" : "=r" (val));
80
81 return val;
82}
83
84/* write L2 control register (L2CTLR) */
85static inline void write_l2ctlr(uint32_t val)
86{
87 /*
88 * Note: L2CTLR can only be written when the L2 memory system
89 * is idle, ie before the MMU is enabled.
90 */
91 asm volatile("mcr p15, 1, %0, c9, c0, 2" : : "r" (val) : "memory");
92 isb();
93}
94
95static void configure_l2ctlr(void)
96{
97 uint32_t l2ctlr;
98
99 l2ctlr = read_l2ctlr();
100 l2ctlr &= 0xfffc0000; /* clear bit0~bit17 */
101
102 /*
103 * Data RAM write latency: 2 cycles
104 * Data RAM read latency: 2 cycles
105 * Data RAM setup latency: 1 cycle
106 * Tag RAM write latency: 1 cycle
107 * Tag RAM read latency: 1 cycle
108 * Tag RAM setup latency: 1 cycle
109 */
110 l2ctlr |= (1 << 3 | 1 << 0);
111 write_l2ctlr(l2ctlr);
112}
113
Simon Glass2cffe662015-08-30 16:55:38 -0600114static int configure_emmc(struct udevice *pinctrl)
115{
Simon Glass873220a2016-01-21 19:43:32 -0700116#ifdef CONFIG_SPL_MMC_SUPPORT
Simon Glass2cffe662015-08-30 16:55:38 -0600117 struct gpio_desc desc;
118 int ret;
119
120 pinctrl_request_noflags(pinctrl, PERIPH_ID_EMMC);
121
122 /*
123 * TODO(sjg@chromium.org): Pick this up from device tree or perhaps
124 * use the EMMC_PWREN setting.
125 */
126 ret = dm_gpio_lookup_name("D9", &desc);
127 if (ret) {
128 debug("gpio ret=%d\n", ret);
129 return ret;
130 }
131 ret = dm_gpio_request(&desc, "emmc_pwren");
132 if (ret) {
133 debug("gpio_request ret=%d\n", ret);
134 return ret;
135 }
136 ret = dm_gpio_set_dir_flags(&desc, GPIOD_IS_OUT);
137 if (ret) {
138 debug("gpio dir ret=%d\n", ret);
139 return ret;
140 }
141 ret = dm_gpio_set_value(&desc, 1);
142 if (ret) {
143 debug("gpio value ret=%d\n", ret);
144 return ret;
145 }
Simon Glass873220a2016-01-21 19:43:32 -0700146#endif
Simon Glass2cffe662015-08-30 16:55:38 -0600147
148 return 0;
149}
150
151void board_init_f(ulong dummy)
152{
153 struct udevice *pinctrl;
154 struct udevice *dev;
155 int ret;
156
157 /* Example code showing how to enable the debug UART on RK3288 */
158#ifdef EARLY_UART
159#include <asm/arch/grf_rk3288.h>
160 /* Enable early UART on the RK3288 */
161#define GRF_BASE 0xff770000
162 struct rk3288_grf * const grf = (void *)GRF_BASE;
163
164 rk_clrsetreg(&grf->gpio7ch_iomux, GPIO7C7_MASK << GPIO7C7_SHIFT |
165 GPIO7C6_MASK << GPIO7C6_SHIFT,
166 GPIO7C7_UART2DBG_SOUT << GPIO7C7_SHIFT |
167 GPIO7C6_UART2DBG_SIN << GPIO7C6_SHIFT);
168 /*
169 * Debug UART can be used from here if required:
170 *
171 * debug_uart_init();
172 * printch('a');
173 * printhex8(0x1234);
174 * printascii("string");
175 */
176 debug_uart_init();
177#endif
178
179 ret = spl_init();
180 if (ret) {
181 debug("spl_init() failed: %d\n", ret);
182 hang();
183 }
184
huang lin8db3e242015-11-17 14:20:09 +0800185 rockchip_timer_init();
Simon Glass2cffe662015-08-30 16:55:38 -0600186 configure_l2ctlr();
187
188 ret = uclass_get_device(UCLASS_CLK, 0, &dev);
189 if (ret) {
190 debug("CLK init failed: %d\n", ret);
191 return;
192 }
193
194 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
195 if (ret) {
196 debug("Pinctrl init failed: %d\n", ret);
197 return;
198 }
199
200 ret = uclass_get_device(UCLASS_RAM, 0, &dev);
201 if (ret) {
202 debug("DRAM init failed: %d\n", ret);
203 return;
204 }
Simon Glass2cffe662015-08-30 16:55:38 -0600205}
206
207static int setup_led(void)
208{
209#ifdef CONFIG_SPL_LED
210 struct udevice *dev;
211 char *led_name;
212 int ret;
213
214 led_name = fdtdec_get_config_string(gd->fdt_blob, "u-boot,boot-led");
215 if (!led_name)
216 return 0;
217 ret = led_get_by_label(led_name, &dev);
218 if (ret) {
219 debug("%s: get=%d\n", __func__, ret);
220 return ret;
221 }
222 ret = led_set_on(dev, 1);
223 if (ret)
224 return ret;
225#endif
226
227 return 0;
228}
229
230void spl_board_init(void)
231{
232 struct udevice *pinctrl;
233 int ret;
234
235 ret = setup_led();
236
237 if (ret) {
238 debug("LED ret=%d\n", ret);
239 hang();
240 }
241
242 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
243 if (ret) {
244 debug("%s: Cannot find pinctrl device\n", __func__);
245 goto err;
246 }
247 ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_SDCARD);
248 if (ret) {
249 debug("%s: Failed to set up SD card\n", __func__);
250 goto err;
251 }
252 ret = configure_emmc(pinctrl);
253 if (ret) {
254 debug("%s: Failed to set up eMMC\n", __func__);
255 goto err;
256 }
257
258 /* Enable debug UART */
259 ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_UART_DBG);
260 if (ret) {
261 debug("%s: Failed to set up console UART\n", __func__);
262 goto err;
263 }
264
265 preloader_console_init();
266 return;
267err:
268 printf("spl_board_init: Error %d\n", ret);
269
270 /* No way to report error here */
271 hang();
272}