Simon Glass | 2cffe66 | 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> |
Heiko Stübner | 5c43acb | 2017-02-18 19:46:26 +0100 | [diff] [blame] | 17 | #include <asm/arch/bootrom.h> |
Simon Glass | 2cffe66 | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 18 | #include <asm/arch/clock.h> |
| 19 | #include <asm/arch/hardware.h> |
| 20 | #include <asm/arch/periph.h> |
| 21 | #include <asm/arch/sdram.h> |
huang lin | 8db3e24 | 2015-11-17 14:20:09 +0800 | [diff] [blame] | 22 | #include <asm/arch/timer.h> |
Simon Glass | 2cffe66 | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 23 | #include <dm/pinctrl.h> |
| 24 | #include <dm/root.h> |
| 25 | #include <dm/test.h> |
| 26 | #include <dm/util.h> |
| 27 | #include <power/regulator.h> |
| 28 | |
| 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
| 31 | u32 spl_boot_device(void) |
| 32 | { |
Simon Glass | 26158ef | 2016-07-04 11:58:32 -0600 | [diff] [blame] | 33 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 2cffe66 | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 34 | const void *blob = gd->fdt_blob; |
| 35 | struct udevice *dev; |
| 36 | const char *bootdev; |
| 37 | int node; |
| 38 | int ret; |
| 39 | |
| 40 | bootdev = fdtdec_get_config_string(blob, "u-boot,boot0"); |
| 41 | debug("Boot device %s\n", bootdev); |
| 42 | if (!bootdev) |
| 43 | goto fallback; |
| 44 | |
| 45 | node = fdt_path_offset(blob, bootdev); |
| 46 | if (node < 0) { |
| 47 | debug("node=%d\n", node); |
| 48 | goto fallback; |
| 49 | } |
| 50 | ret = device_get_global_by_of_offset(node, &dev); |
| 51 | if (ret) { |
| 52 | debug("device at node %s/%d not found: %d\n", bootdev, node, |
| 53 | ret); |
| 54 | goto fallback; |
| 55 | } |
| 56 | debug("Found device %s\n", dev->name); |
| 57 | switch (device_get_uclass_id(dev)) { |
| 58 | case UCLASS_SPI_FLASH: |
| 59 | return BOOT_DEVICE_SPI; |
| 60 | case UCLASS_MMC: |
| 61 | return BOOT_DEVICE_MMC1; |
| 62 | default: |
| 63 | debug("Booting from device uclass '%s' not supported\n", |
| 64 | dev_get_uclass_name(dev)); |
| 65 | } |
| 66 | |
| 67 | fallback: |
Simon Glass | bf8d7bf | 2016-11-13 14:22:16 -0700 | [diff] [blame] | 68 | #elif defined(CONFIG_TARGET_CHROMEBOOK_JERRY) || \ |
Simon Glass | e51b2e7 | 2016-11-13 14:24:54 -0700 | [diff] [blame] | 69 | defined(CONFIG_TARGET_CHROMEBIT_MICKEY) || \ |
| 70 | defined(CONFIG_TARGET_CHROMEBOOK_MINNIE) |
Simon Glass | 6f9087c | 2016-11-13 14:21:57 -0700 | [diff] [blame] | 71 | return BOOT_DEVICE_SPI; |
Simon Glass | 26158ef | 2016-07-04 11:58:32 -0600 | [diff] [blame] | 72 | #endif |
Simon Glass | 2cffe66 | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 73 | return BOOT_DEVICE_MMC1; |
| 74 | } |
| 75 | |
Marek Vasut | 64d64bb | 2016-05-14 23:42:07 +0200 | [diff] [blame] | 76 | u32 spl_boot_mode(const u32 boot_device) |
Simon Glass | 2cffe66 | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 77 | { |
| 78 | return MMCSD_MODE_RAW; |
| 79 | } |
| 80 | |
| 81 | /* read L2 control register (L2CTLR) */ |
| 82 | static inline uint32_t read_l2ctlr(void) |
| 83 | { |
| 84 | uint32_t val = 0; |
| 85 | |
| 86 | asm volatile ("mrc p15, 1, %0, c9, c0, 2" : "=r" (val)); |
| 87 | |
| 88 | return val; |
| 89 | } |
| 90 | |
| 91 | /* write L2 control register (L2CTLR) */ |
| 92 | static inline void write_l2ctlr(uint32_t val) |
| 93 | { |
| 94 | /* |
| 95 | * Note: L2CTLR can only be written when the L2 memory system |
| 96 | * is idle, ie before the MMU is enabled. |
| 97 | */ |
| 98 | asm volatile("mcr p15, 1, %0, c9, c0, 2" : : "r" (val) : "memory"); |
| 99 | isb(); |
| 100 | } |
| 101 | |
| 102 | static void configure_l2ctlr(void) |
| 103 | { |
| 104 | uint32_t l2ctlr; |
| 105 | |
| 106 | l2ctlr = read_l2ctlr(); |
| 107 | l2ctlr &= 0xfffc0000; /* clear bit0~bit17 */ |
| 108 | |
| 109 | /* |
| 110 | * Data RAM write latency: 2 cycles |
| 111 | * Data RAM read latency: 2 cycles |
| 112 | * Data RAM setup latency: 1 cycle |
| 113 | * Tag RAM write latency: 1 cycle |
| 114 | * Tag RAM read latency: 1 cycle |
| 115 | * Tag RAM setup latency: 1 cycle |
| 116 | */ |
| 117 | l2ctlr |= (1 << 3 | 1 << 0); |
| 118 | write_l2ctlr(l2ctlr); |
| 119 | } |
| 120 | |
Simon Glass | 4ba1293 | 2016-01-21 19:45:13 -0700 | [diff] [blame] | 121 | #ifdef CONFIG_SPL_MMC_SUPPORT |
Simon Glass | 2cffe66 | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 122 | static int configure_emmc(struct udevice *pinctrl) |
| 123 | { |
jk.kernel@gmail.com | b1aeb09 | 2016-07-26 18:28:29 +0800 | [diff] [blame] | 124 | #if defined(CONFIG_TARGET_CHROMEBOOK_JERRY) |
jk.kernel@gmail.com | 2b87b93 | 2016-07-26 18:28:23 +0800 | [diff] [blame] | 125 | |
Simon Glass | 2cffe66 | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 126 | struct gpio_desc desc; |
| 127 | int ret; |
| 128 | |
| 129 | pinctrl_request_noflags(pinctrl, PERIPH_ID_EMMC); |
| 130 | |
| 131 | /* |
| 132 | * TODO(sjg@chromium.org): Pick this up from device tree or perhaps |
| 133 | * use the EMMC_PWREN setting. |
| 134 | */ |
| 135 | ret = dm_gpio_lookup_name("D9", &desc); |
| 136 | if (ret) { |
| 137 | debug("gpio ret=%d\n", ret); |
| 138 | return ret; |
| 139 | } |
| 140 | ret = dm_gpio_request(&desc, "emmc_pwren"); |
| 141 | if (ret) { |
| 142 | debug("gpio_request ret=%d\n", ret); |
| 143 | return ret; |
| 144 | } |
| 145 | ret = dm_gpio_set_dir_flags(&desc, GPIOD_IS_OUT); |
| 146 | if (ret) { |
| 147 | debug("gpio dir ret=%d\n", ret); |
| 148 | return ret; |
| 149 | } |
| 150 | ret = dm_gpio_set_value(&desc, 1); |
| 151 | if (ret) { |
| 152 | debug("gpio value ret=%d\n", ret); |
| 153 | return ret; |
| 154 | } |
jk.kernel@gmail.com | 2b87b93 | 2016-07-26 18:28:23 +0800 | [diff] [blame] | 155 | #endif |
Simon Glass | 2cffe66 | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 156 | return 0; |
| 157 | } |
Simon Glass | 4ba1293 | 2016-01-21 19:45:13 -0700 | [diff] [blame] | 158 | #endif |
Heiko Stübner | 5c43acb | 2017-02-18 19:46:26 +0100 | [diff] [blame] | 159 | |
Simon Glass | 2cffe66 | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 160 | void board_init_f(ulong dummy) |
| 161 | { |
| 162 | struct udevice *pinctrl; |
| 163 | struct udevice *dev; |
| 164 | int ret; |
| 165 | |
| 166 | /* Example code showing how to enable the debug UART on RK3288 */ |
Simon Glass | 2cffe66 | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 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(); |
Eddie Cai | 9d62e82 | 2017-04-18 19:17:27 +0800 | [diff] [blame] | 185 | debug("\nspl:debug uart enabled in %s\n", __func__); |
Eddie Cai | 3e2b61c | 2017-03-15 08:43:29 -0600 | [diff] [blame] | 186 | ret = spl_early_init(); |
Simon Glass | 2cffe66 | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 187 | if (ret) { |
Eddie Cai | 3e2b61c | 2017-03-15 08:43:29 -0600 | [diff] [blame] | 188 | debug("spl_early_init() failed: %d\n", ret); |
Simon Glass | 2cffe66 | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 189 | hang(); |
| 190 | } |
| 191 | |
huang lin | 8db3e24 | 2015-11-17 14:20:09 +0800 | [diff] [blame] | 192 | rockchip_timer_init(); |
Simon Glass | 2cffe66 | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 193 | configure_l2ctlr(); |
| 194 | |
Simon Glass | ae8fe41 | 2016-07-17 15:23:17 -0600 | [diff] [blame] | 195 | ret = rockchip_get_clk(&dev); |
Simon Glass | 2cffe66 | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 196 | 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 | } |
Eddie Cai | 9d62e82 | 2017-04-18 19:17:27 +0800 | [diff] [blame] | 206 | debug("\nspl:init dram\n"); |
Simon Glass | 2cffe66 | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 207 | ret = uclass_get_device(UCLASS_RAM, 0, &dev); |
| 208 | if (ret) { |
| 209 | debug("DRAM init failed: %d\n", ret); |
| 210 | return; |
| 211 | } |
Sandy Patterson | a9e92ee | 2016-08-10 10:21:47 -0400 | [diff] [blame] | 212 | #if defined(CONFIG_ROCKCHIP_SPL_BACK_TO_BROM) && !defined(CONFIG_SPL_BOARD_INIT) |
Xu Ziyuan | 5401eb8 | 2016-07-12 19:09:49 +0800 | [diff] [blame] | 213 | back_to_bootrom(); |
| 214 | #endif |
Simon Glass | 2cffe66 | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | static 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 | |
| 240 | void 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.com | 2b87b93 | 2016-07-26 18:28:23 +0800 | [diff] [blame] | 257 | |
Simon Glass | 4ba1293 | 2016-01-21 19:45:13 -0700 | [diff] [blame] | 258 | #ifdef CONFIG_SPL_MMC_SUPPORT |
jk.kernel@gmail.com | 2b87b93 | 2016-07-26 18:28:23 +0800 | [diff] [blame] | 259 | 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 Glass | 2cffe66 | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 268 | } |
Simon Glass | 4ba1293 | 2016-01-21 19:45:13 -0700 | [diff] [blame] | 269 | #endif |
Simon Glass | 2cffe66 | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 270 | |
| 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 Patterson | a9e92ee | 2016-08-10 10:21:47 -0400 | [diff] [blame] | 279 | #ifdef CONFIG_ROCKCHIP_SPL_BACK_TO_BROM |
| 280 | back_to_bootrom(); |
| 281 | #endif |
Simon Glass | 2cffe66 | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 282 | return; |
| 283 | err: |
| 284 | printf("spl_board_init: Error %d\n", ret); |
| 285 | |
| 286 | /* No way to report error here */ |
| 287 | hang(); |
| 288 | } |