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> |
| 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 | |
| 27 | DECLARE_GLOBAL_DATA_PTR; |
| 28 | |
| 29 | u32 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 | |
| 64 | fallback: |
| 65 | return BOOT_DEVICE_MMC1; |
| 66 | } |
| 67 | |
| 68 | u32 spl_boot_mode(void) |
| 69 | { |
| 70 | return MMCSD_MODE_RAW; |
| 71 | } |
| 72 | |
| 73 | /* read L2 control register (L2CTLR) */ |
| 74 | static 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) */ |
| 84 | static 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 | |
| 94 | static 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 | |
| 113 | struct 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 | |
| 122 | void 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 | |
| 131 | static 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 | |
| 166 | void 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 | } |
| 220 | } |
| 221 | |
| 222 | static int setup_led(void) |
| 223 | { |
| 224 | #ifdef CONFIG_SPL_LED |
| 225 | struct udevice *dev; |
| 226 | char *led_name; |
| 227 | int ret; |
| 228 | |
| 229 | led_name = fdtdec_get_config_string(gd->fdt_blob, "u-boot,boot-led"); |
| 230 | if (!led_name) |
| 231 | return 0; |
| 232 | ret = led_get_by_label(led_name, &dev); |
| 233 | if (ret) { |
| 234 | debug("%s: get=%d\n", __func__, ret); |
| 235 | return ret; |
| 236 | } |
| 237 | ret = led_set_on(dev, 1); |
| 238 | if (ret) |
| 239 | return ret; |
| 240 | #endif |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | void spl_board_init(void) |
| 246 | { |
| 247 | struct udevice *pinctrl; |
| 248 | int ret; |
| 249 | |
| 250 | ret = setup_led(); |
| 251 | |
| 252 | if (ret) { |
| 253 | debug("LED ret=%d\n", ret); |
| 254 | hang(); |
| 255 | } |
| 256 | |
| 257 | ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl); |
| 258 | if (ret) { |
| 259 | debug("%s: Cannot find pinctrl device\n", __func__); |
| 260 | goto err; |
| 261 | } |
| 262 | ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_SDCARD); |
| 263 | if (ret) { |
| 264 | debug("%s: Failed to set up SD card\n", __func__); |
| 265 | goto err; |
| 266 | } |
| 267 | ret = configure_emmc(pinctrl); |
| 268 | if (ret) { |
| 269 | debug("%s: Failed to set up eMMC\n", __func__); |
| 270 | goto err; |
| 271 | } |
| 272 | |
| 273 | /* Enable debug UART */ |
| 274 | ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_UART_DBG); |
| 275 | if (ret) { |
| 276 | debug("%s: Failed to set up console UART\n", __func__); |
| 277 | goto err; |
| 278 | } |
| 279 | |
| 280 | preloader_console_init(); |
| 281 | return; |
| 282 | err: |
| 283 | printf("spl_board_init: Error %d\n", ret); |
| 284 | |
| 285 | /* No way to report error here */ |
| 286 | hang(); |
| 287 | } |