Simon Glass | 2444dae | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 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> |
huang lin | cc2244b | 2015-11-17 14:20:09 +0800 | [diff] [blame] | 21 | #include <asm/arch/timer.h> |
Simon Glass | 2444dae | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 22 | #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 | |
| 28 | DECLARE_GLOBAL_DATA_PTR; |
| 29 | |
| 30 | u32 spl_boot_device(void) |
| 31 | { |
Simon Glass | 6afc466 | 2016-07-04 11:58:32 -0600 | [diff] [blame] | 32 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 2444dae | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 33 | 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 | |
| 66 | fallback: |
Simon Glass | 6afc466 | 2016-07-04 11:58:32 -0600 | [diff] [blame] | 67 | #endif |
Simon Glass | 2444dae | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 68 | return BOOT_DEVICE_MMC1; |
| 69 | } |
| 70 | |
Marek Vasut | 2b1cdaf | 2016-05-14 23:42:07 +0200 | [diff] [blame] | 71 | u32 spl_boot_mode(const u32 boot_device) |
Simon Glass | 2444dae | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 72 | { |
| 73 | return MMCSD_MODE_RAW; |
| 74 | } |
| 75 | |
| 76 | /* read L2 control register (L2CTLR) */ |
| 77 | static inline uint32_t read_l2ctlr(void) |
| 78 | { |
| 79 | uint32_t val = 0; |
| 80 | |
| 81 | asm volatile ("mrc p15, 1, %0, c9, c0, 2" : "=r" (val)); |
| 82 | |
| 83 | return val; |
| 84 | } |
| 85 | |
| 86 | /* write L2 control register (L2CTLR) */ |
| 87 | static inline void write_l2ctlr(uint32_t val) |
| 88 | { |
| 89 | /* |
| 90 | * Note: L2CTLR can only be written when the L2 memory system |
| 91 | * is idle, ie before the MMU is enabled. |
| 92 | */ |
| 93 | asm volatile("mcr p15, 1, %0, c9, c0, 2" : : "r" (val) : "memory"); |
| 94 | isb(); |
| 95 | } |
| 96 | |
| 97 | static void configure_l2ctlr(void) |
| 98 | { |
| 99 | uint32_t l2ctlr; |
| 100 | |
| 101 | l2ctlr = read_l2ctlr(); |
| 102 | l2ctlr &= 0xfffc0000; /* clear bit0~bit17 */ |
| 103 | |
| 104 | /* |
| 105 | * Data RAM write latency: 2 cycles |
| 106 | * Data RAM read latency: 2 cycles |
| 107 | * Data RAM setup latency: 1 cycle |
| 108 | * Tag RAM write latency: 1 cycle |
| 109 | * Tag RAM read latency: 1 cycle |
| 110 | * Tag RAM setup latency: 1 cycle |
| 111 | */ |
| 112 | l2ctlr |= (1 << 3 | 1 << 0); |
| 113 | write_l2ctlr(l2ctlr); |
| 114 | } |
| 115 | |
Simon Glass | f23cf90 | 2016-01-21 19:45:13 -0700 | [diff] [blame] | 116 | #ifdef CONFIG_SPL_MMC_SUPPORT |
Simon Glass | 2444dae | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 117 | static int configure_emmc(struct udevice *pinctrl) |
| 118 | { |
jk.kernel@gmail.com | 5051a77 | 2016-07-26 18:28:23 +0800 | [diff] [blame^] | 119 | #if !defined(CONFIG_TARGET_ROCK2) && !defined(CONFIG_TARGET_FIREFLY_RK3288) && \ |
| 120 | !defined(CONFIG_TARGET_EVB_RK3288) |
| 121 | |
Simon Glass | 2444dae | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 122 | struct gpio_desc desc; |
| 123 | int ret; |
| 124 | |
| 125 | pinctrl_request_noflags(pinctrl, PERIPH_ID_EMMC); |
| 126 | |
| 127 | /* |
| 128 | * TODO(sjg@chromium.org): Pick this up from device tree or perhaps |
| 129 | * use the EMMC_PWREN setting. |
| 130 | */ |
| 131 | ret = dm_gpio_lookup_name("D9", &desc); |
| 132 | if (ret) { |
| 133 | debug("gpio ret=%d\n", ret); |
| 134 | return ret; |
| 135 | } |
| 136 | ret = dm_gpio_request(&desc, "emmc_pwren"); |
| 137 | if (ret) { |
| 138 | debug("gpio_request ret=%d\n", ret); |
| 139 | return ret; |
| 140 | } |
| 141 | ret = dm_gpio_set_dir_flags(&desc, GPIOD_IS_OUT); |
| 142 | if (ret) { |
| 143 | debug("gpio dir ret=%d\n", ret); |
| 144 | return ret; |
| 145 | } |
| 146 | ret = dm_gpio_set_value(&desc, 1); |
| 147 | if (ret) { |
| 148 | debug("gpio value ret=%d\n", ret); |
| 149 | return ret; |
| 150 | } |
jk.kernel@gmail.com | 5051a77 | 2016-07-26 18:28:23 +0800 | [diff] [blame^] | 151 | #endif |
Simon Glass | 2444dae | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 152 | return 0; |
| 153 | } |
Simon Glass | f23cf90 | 2016-01-21 19:45:13 -0700 | [diff] [blame] | 154 | #endif |
Xu Ziyuan | b47ea79 | 2016-07-12 19:09:49 +0800 | [diff] [blame] | 155 | extern void back_to_bootrom(void); |
Simon Glass | 2444dae | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 156 | void board_init_f(ulong dummy) |
| 157 | { |
| 158 | struct udevice *pinctrl; |
| 159 | struct udevice *dev; |
| 160 | int ret; |
| 161 | |
| 162 | /* Example code showing how to enable the debug UART on RK3288 */ |
| 163 | #ifdef EARLY_UART |
| 164 | #include <asm/arch/grf_rk3288.h> |
| 165 | /* Enable early UART on the RK3288 */ |
| 166 | #define GRF_BASE 0xff770000 |
| 167 | struct rk3288_grf * const grf = (void *)GRF_BASE; |
| 168 | |
| 169 | rk_clrsetreg(&grf->gpio7ch_iomux, GPIO7C7_MASK << GPIO7C7_SHIFT | |
| 170 | GPIO7C6_MASK << GPIO7C6_SHIFT, |
| 171 | GPIO7C7_UART2DBG_SOUT << GPIO7C7_SHIFT | |
| 172 | GPIO7C6_UART2DBG_SIN << GPIO7C6_SHIFT); |
| 173 | /* |
| 174 | * Debug UART can be used from here if required: |
| 175 | * |
| 176 | * debug_uart_init(); |
| 177 | * printch('a'); |
| 178 | * printhex8(0x1234); |
| 179 | * printascii("string"); |
| 180 | */ |
| 181 | debug_uart_init(); |
| 182 | #endif |
| 183 | |
| 184 | ret = spl_init(); |
| 185 | if (ret) { |
| 186 | debug("spl_init() failed: %d\n", ret); |
| 187 | hang(); |
| 188 | } |
| 189 | |
huang lin | cc2244b | 2015-11-17 14:20:09 +0800 | [diff] [blame] | 190 | rockchip_timer_init(); |
Simon Glass | 2444dae | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 191 | configure_l2ctlr(); |
| 192 | |
Simon Glass | c3aad6f | 2016-07-17 15:23:17 -0600 | [diff] [blame] | 193 | ret = rockchip_get_clk(&dev); |
Simon Glass | 2444dae | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 194 | if (ret) { |
| 195 | debug("CLK init failed: %d\n", ret); |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl); |
| 200 | if (ret) { |
| 201 | debug("Pinctrl init failed: %d\n", ret); |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | ret = uclass_get_device(UCLASS_RAM, 0, &dev); |
| 206 | if (ret) { |
| 207 | debug("DRAM init failed: %d\n", ret); |
| 208 | return; |
| 209 | } |
Xu Ziyuan | b47ea79 | 2016-07-12 19:09:49 +0800 | [diff] [blame] | 210 | #ifdef CONFIG_ROCKCHIP_SPL_BACK_TO_BROM |
| 211 | back_to_bootrom(); |
| 212 | #endif |
Simon Glass | 2444dae | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static int setup_led(void) |
| 216 | { |
| 217 | #ifdef CONFIG_SPL_LED |
| 218 | struct udevice *dev; |
| 219 | char *led_name; |
| 220 | int ret; |
| 221 | |
| 222 | led_name = fdtdec_get_config_string(gd->fdt_blob, "u-boot,boot-led"); |
| 223 | if (!led_name) |
| 224 | return 0; |
| 225 | ret = led_get_by_label(led_name, &dev); |
| 226 | if (ret) { |
| 227 | debug("%s: get=%d\n", __func__, ret); |
| 228 | return ret; |
| 229 | } |
| 230 | ret = led_set_on(dev, 1); |
| 231 | if (ret) |
| 232 | return ret; |
| 233 | #endif |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | void spl_board_init(void) |
| 239 | { |
| 240 | struct udevice *pinctrl; |
| 241 | int ret; |
| 242 | |
| 243 | ret = setup_led(); |
| 244 | |
| 245 | if (ret) { |
| 246 | debug("LED ret=%d\n", ret); |
| 247 | hang(); |
| 248 | } |
| 249 | |
| 250 | ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl); |
| 251 | if (ret) { |
| 252 | debug("%s: Cannot find pinctrl device\n", __func__); |
| 253 | goto err; |
| 254 | } |
jk.kernel@gmail.com | 5051a77 | 2016-07-26 18:28:23 +0800 | [diff] [blame^] | 255 | |
Simon Glass | f23cf90 | 2016-01-21 19:45:13 -0700 | [diff] [blame] | 256 | #ifdef CONFIG_SPL_MMC_SUPPORT |
jk.kernel@gmail.com | 5051a77 | 2016-07-26 18:28:23 +0800 | [diff] [blame^] | 257 | ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_SDCARD); |
| 258 | if (ret) { |
| 259 | debug("%s: Failed to set up SD card\n", __func__); |
| 260 | goto err; |
| 261 | } |
| 262 | ret = configure_emmc(pinctrl); |
| 263 | if (ret) { |
| 264 | debug("%s: Failed to set up eMMC\n", __func__); |
| 265 | goto err; |
Simon Glass | 2444dae | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 266 | } |
Simon Glass | f23cf90 | 2016-01-21 19:45:13 -0700 | [diff] [blame] | 267 | #endif |
Simon Glass | 2444dae | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 268 | |
| 269 | /* Enable debug UART */ |
| 270 | ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_UART_DBG); |
| 271 | if (ret) { |
| 272 | debug("%s: Failed to set up console UART\n", __func__); |
| 273 | goto err; |
| 274 | } |
| 275 | |
| 276 | preloader_console_init(); |
| 277 | return; |
| 278 | err: |
| 279 | printf("spl_board_init: Error %d\n", ret); |
| 280 | |
| 281 | /* No way to report error here */ |
| 282 | hang(); |
| 283 | } |
Simon Glass | ad443b7 | 2016-01-21 19:45:06 -0700 | [diff] [blame] | 284 | |
| 285 | void lowlevel_init(void) |
| 286 | { |
| 287 | } |