Kever Yang | 3012a84 | 2017-02-23 16:09:05 +0800 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2016 Rockchip Electronics Co., Ltd |
| 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> |
Philipp Tomsich | d02d11f | 2017-03-28 11:03:00 +0200 | [diff] [blame] | 13 | #include <mmc.h> |
Kever Yang | 3012a84 | 2017-02-23 16:09:05 +0800 | [diff] [blame] | 14 | #include <ram.h> |
| 15 | #include <spl.h> |
| 16 | #include <asm/gpio.h> |
| 17 | #include <asm/io.h> |
| 18 | #include <asm/arch/clock.h> |
| 19 | #include <asm/arch/hardware.h> |
| 20 | #include <asm/arch/periph.h> |
| 21 | #include <asm/arch/sdram.h> |
| 22 | #include <asm/arch/timer.h> |
| 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 | |
Philipp Tomsich | d02d11f | 2017-03-28 11:03:00 +0200 | [diff] [blame] | 31 | #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_OF_CONTROL) |
| 32 | static int spl_node_to_boot_device(int node) |
| 33 | { |
| 34 | struct udevice *parent; |
| 35 | |
| 36 | /* |
| 37 | * This should eventually move into the SPL code, once SPL becomes |
| 38 | * aware of the block-device layer. Until then (and to avoid unneeded |
| 39 | * delays in getting this feature out, it lives at the board-level). |
| 40 | */ |
| 41 | if (!uclass_get_device_by_of_offset(UCLASS_MMC, node, &parent)) { |
| 42 | struct udevice *dev; |
| 43 | struct blk_desc *desc = NULL; |
| 44 | |
| 45 | for (device_find_first_child(parent, &dev); |
| 46 | dev; |
| 47 | device_find_next_child(&dev)) { |
| 48 | if (device_get_uclass_id(dev) == UCLASS_BLK) { |
| 49 | desc = dev_get_uclass_platdata(dev); |
| 50 | break; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | if (!desc) |
| 55 | return -ENOENT; |
| 56 | |
| 57 | switch (desc->devnum) { |
| 58 | case 0: |
| 59 | return BOOT_DEVICE_MMC1; |
| 60 | case 1: |
| 61 | return BOOT_DEVICE_MMC2; |
| 62 | default: |
| 63 | return -ENOSYS; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * SPL doesn't differentiate SPI flashes, so we keep the detection |
| 69 | * brief and inaccurate... hopefully, the common SPL layer can be |
| 70 | * extended with awareness of the BLK layer (and matching OF_CONTROL) |
| 71 | * soon. |
| 72 | */ |
| 73 | if (!uclass_get_device_by_of_offset(UCLASS_SPI_FLASH, node, &parent)) |
| 74 | return BOOT_DEVICE_SPI; |
| 75 | |
| 76 | return -1; |
| 77 | } |
| 78 | |
| 79 | void board_boot_order(u32 *spl_boot_list) |
| 80 | { |
| 81 | const void *blob = gd->fdt_blob; |
| 82 | int chosen_node = fdt_path_offset(blob, "/chosen"); |
| 83 | int idx = 0; |
| 84 | int elem; |
| 85 | int boot_device; |
| 86 | int node; |
| 87 | const char *conf; |
| 88 | |
| 89 | if (chosen_node < 0) { |
| 90 | debug("%s: /chosen not found, using spl_boot_device()\n", |
| 91 | __func__); |
| 92 | spl_boot_list[0] = spl_boot_device(); |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | for (elem = 0; |
| 97 | (conf = fdt_stringlist_get(blob, chosen_node, |
| 98 | "u-boot,spl-boot-order", elem, NULL)); |
| 99 | elem++) { |
| 100 | /* First check if the list element is an alias */ |
| 101 | const char *alias = fdt_get_alias(blob, conf); |
| 102 | if (alias) |
| 103 | conf = alias; |
| 104 | |
| 105 | /* Try to resolve the config item (or alias) as a path */ |
| 106 | node = fdt_path_offset(blob, conf); |
| 107 | if (node < 0) { |
| 108 | debug("%s: could not find %s in FDT", __func__, conf); |
| 109 | continue; |
| 110 | } |
| 111 | |
| 112 | /* Try to map this back onto SPL boot devices */ |
| 113 | boot_device = spl_node_to_boot_device(node); |
| 114 | if (boot_device < 0) { |
| 115 | debug("%s: could not map node @%x to a boot-device\n", |
| 116 | __func__, node); |
| 117 | continue; |
| 118 | } |
| 119 | |
| 120 | spl_boot_list[idx++] = boot_device; |
| 121 | } |
| 122 | |
| 123 | /* If we had no matches, fall back to spl_boot_device */ |
| 124 | if (idx == 0) |
| 125 | spl_boot_list[0] = spl_boot_device(); |
| 126 | } |
| 127 | #endif |
| 128 | |
Kever Yang | 3012a84 | 2017-02-23 16:09:05 +0800 | [diff] [blame] | 129 | u32 spl_boot_device(void) |
| 130 | { |
| 131 | return BOOT_DEVICE_MMC1; |
| 132 | } |
| 133 | |
| 134 | u32 spl_boot_mode(const u32 boot_device) |
| 135 | { |
| 136 | return MMCSD_MODE_RAW; |
| 137 | } |
| 138 | |
| 139 | #define TIMER_CHN10_BASE 0xff8680a0 |
| 140 | #define TIMER_END_COUNT_L 0x00 |
| 141 | #define TIMER_END_COUNT_H 0x04 |
| 142 | #define TIMER_INIT_COUNT_L 0x10 |
| 143 | #define TIMER_INIT_COUNT_H 0x14 |
| 144 | #define TIMER_CONTROL_REG 0x1c |
| 145 | |
| 146 | #define TIMER_EN 0x1 |
| 147 | #define TIMER_FMODE (0 << 1) |
| 148 | #define TIMER_RMODE (1 << 1) |
| 149 | |
| 150 | void secure_timer_init(void) |
| 151 | { |
| 152 | writel(0xffffffff, TIMER_CHN10_BASE + TIMER_END_COUNT_L); |
| 153 | writel(0xffffffff, TIMER_CHN10_BASE + TIMER_END_COUNT_H); |
| 154 | writel(0, TIMER_CHN10_BASE + TIMER_INIT_COUNT_L); |
| 155 | writel(0, TIMER_CHN10_BASE + TIMER_INIT_COUNT_H); |
| 156 | writel(TIMER_EN | TIMER_FMODE, TIMER_CHN10_BASE + TIMER_CONTROL_REG); |
| 157 | } |
| 158 | |
| 159 | #define GRF_EMMCCORE_CON11 0xff77f02c |
Philipp Tomsich | 504b9f1 | 2017-03-29 21:20:28 +0200 | [diff] [blame^] | 160 | #define SGRF_DDR_RGN_CON16 0xff330040 |
Kever Yang | 3012a84 | 2017-02-23 16:09:05 +0800 | [diff] [blame] | 161 | void board_init_f(ulong dummy) |
| 162 | { |
| 163 | struct udevice *pinctrl; |
| 164 | struct udevice *dev; |
| 165 | int ret; |
| 166 | |
| 167 | /* Example code showing how to enable the debug UART on RK3288 */ |
| 168 | #include <asm/arch/grf_rk3399.h> |
| 169 | /* Enable early UART2 channel C on the RK3399 */ |
| 170 | #define GRF_BASE 0xff770000 |
| 171 | struct rk3399_grf_regs * const grf = (void *)GRF_BASE; |
| 172 | |
| 173 | rk_clrsetreg(&grf->gpio4c_iomux, |
| 174 | GRF_GPIO4C3_SEL_MASK, |
| 175 | GRF_UART2DGBC_SIN << GRF_GPIO4C3_SEL_SHIFT); |
| 176 | rk_clrsetreg(&grf->gpio4c_iomux, |
| 177 | GRF_GPIO4C4_SEL_MASK, |
| 178 | GRF_UART2DBGC_SOUT << GRF_GPIO4C4_SEL_SHIFT); |
| 179 | /* Set channel C as UART2 input */ |
| 180 | rk_clrsetreg(&grf->soc_con7, |
| 181 | GRF_UART_DBG_SEL_MASK, |
| 182 | GRF_UART_DBG_SEL_C << GRF_UART_DBG_SEL_SHIFT); |
| 183 | #define EARLY_UART |
| 184 | #ifdef EARLY_UART |
| 185 | /* |
| 186 | * Debug UART can be used from here if required: |
| 187 | * |
| 188 | * debug_uart_init(); |
| 189 | * printch('a'); |
| 190 | * printhex8(0x1234); |
| 191 | * printascii("string"); |
| 192 | */ |
| 193 | debug_uart_init(); |
| 194 | printascii("U-Boot SPL board init"); |
| 195 | #endif |
| 196 | /* Emmc clock generator: disable the clock multipilier */ |
| 197 | rk_clrreg(GRF_EMMCCORE_CON11, 0x0ff); |
| 198 | |
Kever Yang | 232cf96 | 2017-03-20 14:47:16 +0800 | [diff] [blame] | 199 | ret = spl_early_init(); |
Kever Yang | 3012a84 | 2017-02-23 16:09:05 +0800 | [diff] [blame] | 200 | if (ret) { |
Kever Yang | 232cf96 | 2017-03-20 14:47:16 +0800 | [diff] [blame] | 201 | debug("spl_early_init() failed: %d\n", ret); |
Kever Yang | 3012a84 | 2017-02-23 16:09:05 +0800 | [diff] [blame] | 202 | hang(); |
| 203 | } |
| 204 | |
Philipp Tomsich | 504b9f1 | 2017-03-29 21:20:28 +0200 | [diff] [blame^] | 205 | /* |
| 206 | * Disable DDR security regions. |
| 207 | * |
| 208 | * As we are entered from the BootROM, the region from |
| 209 | * 0x0 through 0xfffff (i.e. the first MB of memory) will |
| 210 | * be protected. This will cause issues with the DW_MMC |
| 211 | * driver, which tries to DMA from/to the stack (likely) |
| 212 | * located in this range. |
| 213 | */ |
| 214 | rk_clrsetreg(SGRF_DDR_RGN_CON16, 0x1FF, 0); |
| 215 | |
Kever Yang | 3012a84 | 2017-02-23 16:09:05 +0800 | [diff] [blame] | 216 | secure_timer_init(); |
| 217 | |
| 218 | ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl); |
| 219 | if (ret) { |
| 220 | debug("Pinctrl init failed: %d\n", ret); |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | ret = uclass_get_device(UCLASS_RAM, 0, &dev); |
| 225 | if (ret) { |
| 226 | debug("DRAM init failed: %d\n", ret); |
| 227 | return; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | void spl_board_init(void) |
| 232 | { |
| 233 | struct udevice *pinctrl; |
| 234 | int ret; |
| 235 | |
| 236 | ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl); |
| 237 | if (ret) { |
| 238 | debug("%s: Cannot find pinctrl device\n", __func__); |
| 239 | goto err; |
| 240 | } |
| 241 | |
| 242 | /* Enable debug UART */ |
| 243 | ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_UART_DBG); |
| 244 | if (ret) { |
| 245 | debug("%s: Failed to set up console UART\n", __func__); |
| 246 | goto err; |
| 247 | } |
| 248 | |
| 249 | preloader_console_init(); |
| 250 | #ifdef CONFIG_ROCKCHIP_SPL_BACK_TO_BROM |
| 251 | back_to_bootrom(); |
| 252 | #endif |
Philipp Tomsich | 504b9f1 | 2017-03-29 21:20:28 +0200 | [diff] [blame^] | 253 | |
Kever Yang | 3012a84 | 2017-02-23 16:09:05 +0800 | [diff] [blame] | 254 | return; |
| 255 | err: |
| 256 | printf("spl_board_init: Error %d\n", ret); |
| 257 | |
| 258 | /* No way to report error here */ |
| 259 | hang(); |
| 260 | } |
| 261 | |
| 262 | #ifdef CONFIG_SPL_LOAD_FIT |
| 263 | int board_fit_config_name_match(const char *name) |
| 264 | { |
| 265 | /* Just empty function now - can't decide what to choose */ |
| 266 | debug("%s: %s\n", __func__, name); |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | #endif |