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