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> |
| 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 | 8db3e24 | 2015-11-17 14:20:09 +0800 | [diff] [blame^] | 21 | #include <asm/arch/timer.h> |
Simon Glass | 2cffe66 | 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 | { |
| 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 | |
| 65 | fallback: |
| 66 | return BOOT_DEVICE_MMC1; |
| 67 | } |
| 68 | |
| 69 | u32 spl_boot_mode(void) |
| 70 | { |
| 71 | return MMCSD_MODE_RAW; |
| 72 | } |
| 73 | |
| 74 | /* read L2 control register (L2CTLR) */ |
| 75 | static 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) */ |
| 85 | static 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 | |
| 95 | static 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 Glass | 2cffe66 | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 114 | static int configure_emmc(struct udevice *pinctrl) |
| 115 | { |
| 116 | struct gpio_desc desc; |
| 117 | int ret; |
| 118 | |
| 119 | pinctrl_request_noflags(pinctrl, PERIPH_ID_EMMC); |
| 120 | |
| 121 | /* |
| 122 | * TODO(sjg@chromium.org): Pick this up from device tree or perhaps |
| 123 | * use the EMMC_PWREN setting. |
| 124 | */ |
| 125 | ret = dm_gpio_lookup_name("D9", &desc); |
| 126 | if (ret) { |
| 127 | debug("gpio ret=%d\n", ret); |
| 128 | return ret; |
| 129 | } |
| 130 | ret = dm_gpio_request(&desc, "emmc_pwren"); |
| 131 | if (ret) { |
| 132 | debug("gpio_request ret=%d\n", ret); |
| 133 | return ret; |
| 134 | } |
| 135 | ret = dm_gpio_set_dir_flags(&desc, GPIOD_IS_OUT); |
| 136 | if (ret) { |
| 137 | debug("gpio dir ret=%d\n", ret); |
| 138 | return ret; |
| 139 | } |
| 140 | ret = dm_gpio_set_value(&desc, 1); |
| 141 | if (ret) { |
| 142 | debug("gpio value ret=%d\n", ret); |
| 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | void board_init_f(ulong dummy) |
| 150 | { |
| 151 | struct udevice *pinctrl; |
| 152 | struct udevice *dev; |
| 153 | int ret; |
| 154 | |
| 155 | /* Example code showing how to enable the debug UART on RK3288 */ |
| 156 | #ifdef EARLY_UART |
| 157 | #include <asm/arch/grf_rk3288.h> |
| 158 | /* Enable early UART on the RK3288 */ |
| 159 | #define GRF_BASE 0xff770000 |
| 160 | struct rk3288_grf * const grf = (void *)GRF_BASE; |
| 161 | |
| 162 | rk_clrsetreg(&grf->gpio7ch_iomux, GPIO7C7_MASK << GPIO7C7_SHIFT | |
| 163 | GPIO7C6_MASK << GPIO7C6_SHIFT, |
| 164 | GPIO7C7_UART2DBG_SOUT << GPIO7C7_SHIFT | |
| 165 | GPIO7C6_UART2DBG_SIN << GPIO7C6_SHIFT); |
| 166 | /* |
| 167 | * Debug UART can be used from here if required: |
| 168 | * |
| 169 | * debug_uart_init(); |
| 170 | * printch('a'); |
| 171 | * printhex8(0x1234); |
| 172 | * printascii("string"); |
| 173 | */ |
| 174 | debug_uart_init(); |
| 175 | #endif |
| 176 | |
| 177 | ret = spl_init(); |
| 178 | if (ret) { |
| 179 | debug("spl_init() failed: %d\n", ret); |
| 180 | hang(); |
| 181 | } |
| 182 | |
huang lin | 8db3e24 | 2015-11-17 14:20:09 +0800 | [diff] [blame^] | 183 | rockchip_timer_init(); |
Simon Glass | 2cffe66 | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 184 | configure_l2ctlr(); |
| 185 | |
| 186 | ret = uclass_get_device(UCLASS_CLK, 0, &dev); |
| 187 | if (ret) { |
| 188 | debug("CLK init failed: %d\n", ret); |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl); |
| 193 | if (ret) { |
| 194 | debug("Pinctrl init failed: %d\n", ret); |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | ret = uclass_get_device(UCLASS_RAM, 0, &dev); |
| 199 | if (ret) { |
| 200 | debug("DRAM init failed: %d\n", ret); |
| 201 | return; |
| 202 | } |
Sjoerd Simons | 2cd091d | 2015-10-03 13:25:01 +0200 | [diff] [blame] | 203 | |
| 204 | /* |
| 205 | * Now that DRAM is initialized setup base pointer for simple malloc |
| 206 | * into RAM. |
| 207 | */ |
| 208 | gd->malloc_base = CONFIG_SPL_STACK_R_ADDR; |
| 209 | gd->malloc_ptr = 0; |
Simon Glass | 2cffe66 | 2015-08-30 16:55:38 -0600 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static int setup_led(void) |
| 213 | { |
| 214 | #ifdef CONFIG_SPL_LED |
| 215 | struct udevice *dev; |
| 216 | char *led_name; |
| 217 | int ret; |
| 218 | |
| 219 | led_name = fdtdec_get_config_string(gd->fdt_blob, "u-boot,boot-led"); |
| 220 | if (!led_name) |
| 221 | return 0; |
| 222 | ret = led_get_by_label(led_name, &dev); |
| 223 | if (ret) { |
| 224 | debug("%s: get=%d\n", __func__, ret); |
| 225 | return ret; |
| 226 | } |
| 227 | ret = led_set_on(dev, 1); |
| 228 | if (ret) |
| 229 | return ret; |
| 230 | #endif |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | void spl_board_init(void) |
| 236 | { |
| 237 | struct udevice *pinctrl; |
| 238 | int ret; |
| 239 | |
| 240 | ret = setup_led(); |
| 241 | |
| 242 | if (ret) { |
| 243 | debug("LED ret=%d\n", ret); |
| 244 | hang(); |
| 245 | } |
| 246 | |
| 247 | ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl); |
| 248 | if (ret) { |
| 249 | debug("%s: Cannot find pinctrl device\n", __func__); |
| 250 | goto err; |
| 251 | } |
| 252 | ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_SDCARD); |
| 253 | if (ret) { |
| 254 | debug("%s: Failed to set up SD card\n", __func__); |
| 255 | goto err; |
| 256 | } |
| 257 | ret = configure_emmc(pinctrl); |
| 258 | if (ret) { |
| 259 | debug("%s: Failed to set up eMMC\n", __func__); |
| 260 | goto err; |
| 261 | } |
| 262 | |
| 263 | /* Enable debug UART */ |
| 264 | ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_UART_DBG); |
| 265 | if (ret) { |
| 266 | debug("%s: Failed to set up console UART\n", __func__); |
| 267 | goto err; |
| 268 | } |
| 269 | |
| 270 | preloader_console_init(); |
| 271 | return; |
| 272 | err: |
| 273 | printf("spl_board_init: Error %d\n", ret); |
| 274 | |
| 275 | /* No way to report error here */ |
| 276 | hang(); |
| 277 | } |