Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Klaus Goger | a13110a | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH |
Klaus Goger | a13110a | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 4 | */ |
Philipp Tomsich | fb74064 | 2017-09-29 19:27:54 +0200 | [diff] [blame] | 5 | |
Klaus Goger | a13110a | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 6 | #include <common.h> |
| 7 | #include <dm.h> |
Alex Kiernan | 9925f1d | 2018-04-01 09:22:38 +0000 | [diff] [blame] | 8 | #include <environment.h> |
Philipp Tomsich | e92e580 | 2017-04-28 17:31:44 +0200 | [diff] [blame] | 9 | #include <misc.h> |
Philipp Tomsich | 614539d | 2017-11-22 17:15:18 +0100 | [diff] [blame] | 10 | #include <spl.h> |
Jakob Unterwurzacher | aa41220 | 2017-12-15 16:23:14 +0100 | [diff] [blame] | 11 | #include <syscon.h> |
Philipp Tomsich | 614539d | 2017-11-22 17:15:18 +0100 | [diff] [blame] | 12 | #include <usb.h> |
Klaus Goger | a13110a | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 13 | #include <dm/pinctrl.h> |
| 14 | #include <dm/uclass-internal.h> |
Jakob Unterwurzacher | aa41220 | 2017-12-15 16:23:14 +0100 | [diff] [blame] | 15 | #include <asm/io.h> |
Philipp Tomsich | ae0d33a | 2017-11-28 17:56:11 +0100 | [diff] [blame] | 16 | #include <asm/gpio.h> |
Philipp Tomsich | 9415b9a | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 17 | #include <asm/setup.h> |
Philipp Tomsich | ae0d33a | 2017-11-28 17:56:11 +0100 | [diff] [blame] | 18 | #include <asm/arch/clock.h> |
| 19 | #include <asm/arch/cru_rk3399.h> |
Jakob Unterwurzacher | aa41220 | 2017-12-15 16:23:14 +0100 | [diff] [blame] | 20 | #include <asm/arch/hardware.h> |
| 21 | #include <asm/arch/grf_rk3399.h> |
Klaus Goger | a13110a | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 22 | #include <asm/arch/periph.h> |
| 23 | #include <power/regulator.h> |
Philipp Tomsich | e92e580 | 2017-04-28 17:31:44 +0200 | [diff] [blame] | 24 | #include <u-boot/sha256.h> |
| 25 | |
Klaus Goger | a13110a | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 26 | int board_init(void) |
| 27 | { |
Klaus Goger | a13110a | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 28 | int ret; |
| 29 | |
| 30 | /* |
Philipp Tomsich | 0b5e7aa | 2017-09-29 19:28:00 +0200 | [diff] [blame] | 31 | * We need to call into regulators_enable_boot_on() again, as the call |
| 32 | * during SPL may have not included all regulators. |
Klaus Goger | a13110a | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 33 | */ |
Philipp Tomsich | 0b5e7aa | 2017-09-29 19:28:00 +0200 | [diff] [blame] | 34 | ret = regulators_enable_boot_on(false); |
Klaus Goger | a13110a | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 35 | if (ret) |
Philipp Tomsich | 0b5e7aa | 2017-09-29 19:28:00 +0200 | [diff] [blame] | 36 | debug("%s: Cannot enable boot on regulator\n", __func__); |
Klaus Goger | a13110a | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 37 | |
Klaus Goger | a13110a | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 38 | return 0; |
| 39 | } |
| 40 | |
Philipp Tomsich | ae0d33a | 2017-11-28 17:56:11 +0100 | [diff] [blame] | 41 | static void rk3399_force_power_on_reset(void) |
| 42 | { |
| 43 | ofnode node; |
| 44 | struct gpio_desc sysreset_gpio; |
| 45 | |
| 46 | debug("%s: trying to force a power-on reset\n", __func__); |
| 47 | |
| 48 | node = ofnode_path("/config"); |
| 49 | if (!ofnode_valid(node)) { |
| 50 | debug("%s: no /config node?\n", __func__); |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | if (gpio_request_by_name_nodev(node, "sysreset-gpio", 0, |
| 55 | &sysreset_gpio, GPIOD_IS_OUT)) { |
| 56 | debug("%s: could not find a /config/sysreset-gpio\n", __func__); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | dm_gpio_set_value(&sysreset_gpio, 1); |
| 61 | } |
| 62 | |
Philipp Tomsich | fb74064 | 2017-09-29 19:27:54 +0200 | [diff] [blame] | 63 | void spl_board_init(void) |
| 64 | { |
Philipp Tomsich | 482cf22 | 2017-09-29 19:28:01 +0200 | [diff] [blame] | 65 | int ret; |
Philipp Tomsich | ae0d33a | 2017-11-28 17:56:11 +0100 | [diff] [blame] | 66 | struct rk3399_cru *cru = rockchip_get_cru(); |
| 67 | |
| 68 | /* |
| 69 | * The RK3399 resets only 'almost all logic' (see also in the TRM |
| 70 | * "3.9.4 Global software reset"), when issuing a software reset. |
| 71 | * This may cause issues during boot-up for some configurations of |
| 72 | * the application software stack. |
| 73 | * |
| 74 | * To work around this, we test whether the last reset reason was |
| 75 | * a power-on reset and (if not) issue an overtemp-reset to reset |
| 76 | * the entire module. |
| 77 | * |
| 78 | * While this was previously fixed by modifying the various places |
| 79 | * that could generate a software reset (e.g. U-Boot's sysreset |
| 80 | * driver, the ATF or Linux), we now have it here to ensure that |
| 81 | * we no longer have to track this through the various components. |
| 82 | */ |
| 83 | if (cru->glb_rst_st != 0) |
| 84 | rk3399_force_power_on_reset(); |
Philipp Tomsich | 482cf22 | 2017-09-29 19:28:01 +0200 | [diff] [blame] | 85 | |
| 86 | /* |
| 87 | * Turning the eMMC and SPI back on (if disabled via the Qseven |
| 88 | * BIOS_ENABLE) signal is done through a always-on regulator). |
| 89 | */ |
| 90 | ret = regulators_enable_boot_on(false); |
| 91 | if (ret) |
| 92 | debug("%s: Cannot enable boot on regulator\n", __func__); |
| 93 | |
Philipp Tomsich | fb74064 | 2017-09-29 19:27:54 +0200 | [diff] [blame] | 94 | preloader_console_init(); |
| 95 | } |
| 96 | |
Klaus Goger | 8adc9d1 | 2017-05-05 19:21:40 +0200 | [diff] [blame] | 97 | static void setup_macaddr(void) |
| 98 | { |
| 99 | #if CONFIG_IS_ENABLED(CMD_NET) |
| 100 | int ret; |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 101 | const char *cpuid = env_get("cpuid#"); |
Klaus Goger | 8adc9d1 | 2017-05-05 19:21:40 +0200 | [diff] [blame] | 102 | u8 hash[SHA256_SUM_LEN]; |
| 103 | int size = sizeof(hash); |
| 104 | u8 mac_addr[6]; |
| 105 | |
| 106 | /* Only generate a MAC address, if none is set in the environment */ |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 107 | if (env_get("ethaddr")) |
Klaus Goger | 8adc9d1 | 2017-05-05 19:21:40 +0200 | [diff] [blame] | 108 | return; |
| 109 | |
| 110 | if (!cpuid) { |
| 111 | debug("%s: could not retrieve 'cpuid#'\n", __func__); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | ret = hash_block("sha256", (void *)cpuid, strlen(cpuid), hash, &size); |
| 116 | if (ret) { |
| 117 | debug("%s: failed to calculate SHA256\n", __func__); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | /* Copy 6 bytes of the hash to base the MAC address on */ |
| 122 | memcpy(mac_addr, hash, 6); |
| 123 | |
| 124 | /* Make this a valid MAC address and set it */ |
| 125 | mac_addr[0] &= 0xfe; /* clear multicast bit */ |
| 126 | mac_addr[0] |= 0x02; /* set local assignment bit (IEEE802) */ |
Simon Glass | fd1e959 | 2017-08-03 12:22:11 -0600 | [diff] [blame] | 127 | eth_env_set_enetaddr("ethaddr", mac_addr); |
Klaus Goger | 8adc9d1 | 2017-05-05 19:21:40 +0200 | [diff] [blame] | 128 | #endif |
Klaus Goger | 8adc9d1 | 2017-05-05 19:21:40 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Philipp Tomsich | 9415b9a | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 131 | static void setup_serial(void) |
| 132 | { |
| 133 | #if CONFIG_IS_ENABLED(ROCKCHIP_EFUSE) |
Kever Yang | 2672233 | 2017-07-27 19:59:03 +0800 | [diff] [blame] | 134 | const u32 cpuid_offset = 0x7; |
| 135 | const u32 cpuid_length = 0x10; |
| 136 | |
Philipp Tomsich | 9415b9a | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 137 | struct udevice *dev; |
| 138 | int ret, i; |
Kever Yang | 2672233 | 2017-07-27 19:59:03 +0800 | [diff] [blame] | 139 | u8 cpuid[cpuid_length]; |
| 140 | u8 low[cpuid_length/2], high[cpuid_length/2]; |
| 141 | char cpuid_str[cpuid_length * 2 + 1]; |
Philipp Tomsich | 9415b9a | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 142 | u64 serialno; |
Klaus Goger | 60d7c50 | 2017-09-15 14:46:04 +0200 | [diff] [blame] | 143 | char serialno_str[17]; |
Philipp Tomsich | 9415b9a | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 144 | |
Klaus Goger | 8adc9d1 | 2017-05-05 19:21:40 +0200 | [diff] [blame] | 145 | /* retrieve the device */ |
| 146 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
| 147 | DM_GET_DRIVER(rockchip_efuse), &dev); |
Philipp Tomsich | 9415b9a | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 148 | if (ret) { |
| 149 | debug("%s: could not find efuse device\n", __func__); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | /* read the cpu_id range from the efuses */ |
Kever Yang | 2672233 | 2017-07-27 19:59:03 +0800 | [diff] [blame] | 154 | ret = misc_read(dev, cpuid_offset, &cpuid, sizeof(cpuid)); |
Philipp Tomsich | 9415b9a | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 155 | if (ret) { |
| 156 | debug("%s: reading cpuid from the efuses failed\n", |
| 157 | __func__); |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | memset(cpuid_str, 0, sizeof(cpuid_str)); |
| 162 | for (i = 0; i < 16; i++) |
| 163 | sprintf(&cpuid_str[i * 2], "%02x", cpuid[i]); |
| 164 | |
| 165 | debug("cpuid: %s\n", cpuid_str); |
| 166 | |
| 167 | /* |
| 168 | * Mix the cpuid bytes using the same rules as in |
| 169 | * ${linux}/drivers/soc/rockchip/rockchip-cpuinfo.c |
| 170 | */ |
| 171 | for (i = 0; i < 8; i++) { |
| 172 | low[i] = cpuid[1 + (i << 1)]; |
| 173 | high[i] = cpuid[i << 1]; |
| 174 | } |
| 175 | |
| 176 | serialno = crc32_no_comp(0, low, 8); |
| 177 | serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32; |
Jakob Unterwurzacher | b32b1bd | 2017-12-07 16:20:44 +0100 | [diff] [blame] | 178 | snprintf(serialno_str, sizeof(serialno_str), "%016llx", serialno); |
Philipp Tomsich | 9415b9a | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 179 | |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 180 | env_set("cpuid#", cpuid_str); |
| 181 | env_set("serial#", serialno_str); |
Philipp Tomsich | 9415b9a | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 182 | #endif |
Philipp Tomsich | 9415b9a | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 183 | } |
| 184 | |
Jakob Unterwurzacher | aa41220 | 2017-12-15 16:23:14 +0100 | [diff] [blame] | 185 | static void setup_iodomain(void) |
| 186 | { |
| 187 | const u32 GRF_IO_VSEL_GPIO4CD_SHIFT = 3; |
| 188 | struct rk3399_grf_regs *grf = |
| 189 | syscon_get_first_range(ROCKCHIP_SYSCON_GRF); |
| 190 | |
| 191 | /* |
| 192 | * Set bit 3 in GRF_IO_VSEL so PCIE_RST# works (pin GPIO4_C6). |
| 193 | * Linux assumes that PCIE_RST# works out of the box as it probes |
| 194 | * PCIe before loading the iodomain driver. |
| 195 | */ |
| 196 | rk_setreg(&grf->io_vsel, 1 << GRF_IO_VSEL_GPIO4CD_SHIFT); |
| 197 | } |
| 198 | |
Klaus Goger | 41826f6 | 2018-05-24 17:15:53 +0200 | [diff] [blame] | 199 | /* |
| 200 | * Swap mmc0 and mmc1 in boot_targets if booted from SD-Card. |
| 201 | * |
| 202 | * If bootsource is uSD-card we can assume that we want to use the |
| 203 | * SD-Card instead of the eMMC as first boot_target for distroboot. |
| 204 | * We only want to swap the defaults and not any custom environment a |
| 205 | * user has set. We exit early if a changed boot_targets environment |
| 206 | * is detected. |
| 207 | */ |
| 208 | static int setup_boottargets(void) |
| 209 | { |
| 210 | const char *boot_device = |
| 211 | ofnode_get_chosen_prop("u-boot,spl-boot-device"); |
| 212 | char *env_default, *env; |
| 213 | |
| 214 | if (!boot_device) { |
| 215 | debug("%s: /chosen/u-boot,spl-boot-device not set\n", |
| 216 | __func__); |
| 217 | return -1; |
| 218 | } |
| 219 | debug("%s: booted from %s\n", __func__, boot_device); |
| 220 | |
| 221 | env_default = env_get_default("boot_targets"); |
| 222 | env = env_get("boot_targets"); |
| 223 | if (!env) { |
| 224 | debug("%s: boot_targets does not exist\n", __func__); |
| 225 | return -1; |
| 226 | } |
| 227 | debug("%s: boot_targets current: %s - default: %s\n", |
| 228 | __func__, env, env_default); |
| 229 | |
| 230 | if (strcmp(env_default, env) != 0) { |
| 231 | debug("%s: boot_targets not default, don't change it\n", |
| 232 | __func__); |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * Only run, if booting from mmc1 (i.e. /dwmmc@fe320000) and |
| 238 | * only consider cases where the default boot-order first |
| 239 | * tries to boot from mmc0 (eMMC) and then from mmc1 |
| 240 | * (i.e. external SD). |
| 241 | * |
| 242 | * In other words: the SD card will be moved to earlier in the |
| 243 | * order, if U-Boot was also loaded from the SD-card. |
| 244 | */ |
| 245 | if (!strcmp(boot_device, "/dwmmc@fe320000")) { |
| 246 | char *mmc0, *mmc1; |
| 247 | |
| 248 | debug("%s: booted from SD-Card\n", __func__); |
| 249 | mmc0 = strstr(env, "mmc0"); |
| 250 | mmc1 = strstr(env, "mmc1"); |
| 251 | |
| 252 | if (!mmc0 || !mmc1) { |
| 253 | debug("%s: only one mmc boot_target found\n", __func__); |
| 254 | return -1; |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * If mmc0 comes first in the boot order, we need to change |
| 259 | * the strings to make mmc1 first. |
| 260 | */ |
| 261 | if (mmc0 < mmc1) { |
| 262 | mmc0[3] = '1'; |
| 263 | mmc1[3] = '0'; |
| 264 | debug("%s: set boot_targets to: %s\n", __func__, env); |
| 265 | env_set("boot_targets", env); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
Philipp Tomsich | 9415b9a | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 272 | int misc_init_r(void) |
| 273 | { |
| 274 | setup_serial(); |
Klaus Goger | 8adc9d1 | 2017-05-05 19:21:40 +0200 | [diff] [blame] | 275 | setup_macaddr(); |
Jakob Unterwurzacher | aa41220 | 2017-12-15 16:23:14 +0100 | [diff] [blame] | 276 | setup_iodomain(); |
Klaus Goger | 41826f6 | 2018-05-24 17:15:53 +0200 | [diff] [blame] | 277 | setup_boottargets(); |
Philipp Tomsich | 9415b9a | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | #ifdef CONFIG_SERIAL_TAG |
| 283 | void get_board_serial(struct tag_serialnr *serialnr) |
| 284 | { |
| 285 | char *serial_string; |
| 286 | u64 serial = 0; |
| 287 | |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 288 | serial_string = env_get("serial#"); |
Philipp Tomsich | 9415b9a | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 289 | |
| 290 | if (serial_string) |
| 291 | serial = simple_strtoull(serial_string, NULL, 16); |
| 292 | |
| 293 | serialnr->high = (u32)(serial >> 32); |
| 294 | serialnr->low = (u32)(serial & 0xffffffff); |
| 295 | } |
| 296 | #endif |
Philipp Tomsich | 614539d | 2017-11-22 17:15:18 +0100 | [diff] [blame] | 297 | |
| 298 | /** |
| 299 | * Switch power at an external regulator (for our root hub). |
| 300 | * |
| 301 | * @param ctrl pointer to the xHCI controller |
| 302 | * @param port port number as in the control message (one-based) |
| 303 | * @param enable boolean indicating whether to enable or disable power |
| 304 | * @return returns 0 on success, an error-code on failure |
| 305 | */ |
| 306 | static int board_usb_port_power_set(struct udevice *dev, int port, |
| 307 | bool enable) |
| 308 | { |
| 309 | #if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_REGULATOR) |
| 310 | /* We start counting ports at 0, while USB counts from 1. */ |
| 311 | int index = port - 1; |
| 312 | const char *regname = NULL; |
| 313 | struct udevice *regulator; |
| 314 | const char *prop = "tsd,usb-port-power"; |
| 315 | int ret; |
| 316 | |
| 317 | debug("%s: ctrl '%s' port %d enable %s\n", __func__, |
| 318 | dev_read_name(dev), port, enable ? "true" : "false"); |
| 319 | |
| 320 | ret = dev_read_string_index(dev, prop, index, ®name); |
| 321 | if (ret < 0) { |
| 322 | debug("%s: ctrl '%s' port %d: no entry in '%s'\n", |
| 323 | __func__, dev_read_name(dev), port, prop); |
| 324 | return ret; |
| 325 | } |
| 326 | |
| 327 | ret = regulator_get_by_platname(regname, ®ulator); |
| 328 | if (ret) { |
| 329 | debug("%s: ctrl '%s' port %d: could not get regulator '%s'\n", |
| 330 | __func__, dev_read_name(dev), port, regname); |
| 331 | return ret; |
| 332 | } |
| 333 | |
| 334 | regulator_set_enable(regulator, enable); |
| 335 | return 0; |
| 336 | #else |
| 337 | return -ENOTSUPP; |
| 338 | #endif |
| 339 | } |
| 340 | |
| 341 | void usb_hub_reset_devices(struct usb_hub_device *hub, int port) |
| 342 | { |
| 343 | struct udevice *dev = hub->pusb_dev->dev; |
| 344 | struct udevice *ctrl; |
| 345 | |
| 346 | /* We are only interested in our root-hubs */ |
| 347 | if (usb_hub_is_root_hub(dev) == false) |
| 348 | return; |
| 349 | |
| 350 | ctrl = usb_get_bus(dev); |
| 351 | if (!ctrl) { |
| 352 | debug("%s: could not retrieve ctrl for hub\n", __func__); |
| 353 | return; |
| 354 | } |
| 355 | |
| 356 | /* |
| 357 | * To work around an incompatibility between the single-threaded |
| 358 | * USB stack in U-Boot and (a strange low-power mode of) the USB |
| 359 | * hub we have on-module, we need to delay powering on the hub |
| 360 | * until the first time the port is probed. |
| 361 | */ |
| 362 | board_usb_port_power_set(ctrl, port, true); |
| 363 | } |