Klaus Goger | a13110a | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | */ |
Philipp Tomsich | fb74064 | 2017-09-29 19:27:54 +0200 | [diff] [blame] | 6 | |
Klaus Goger | a13110a | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 7 | #include <common.h> |
| 8 | #include <dm.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> |
| 11 | #include <usb.h> |
Klaus Goger | a13110a | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 12 | #include <dm/pinctrl.h> |
| 13 | #include <dm/uclass-internal.h> |
Philipp Tomsich | ae0d33a | 2017-11-28 17:56:11 +0100 | [diff] [blame^] | 14 | #include <asm/gpio.h> |
Philipp Tomsich | 9415b9a | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 15 | #include <asm/setup.h> |
Philipp Tomsich | ae0d33a | 2017-11-28 17:56:11 +0100 | [diff] [blame^] | 16 | #include <asm/arch/clock.h> |
| 17 | #include <asm/arch/cru_rk3399.h> |
Klaus Goger | a13110a | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 18 | #include <asm/arch/periph.h> |
| 19 | #include <power/regulator.h> |
Philipp Tomsich | e92e580 | 2017-04-28 17:31:44 +0200 | [diff] [blame] | 20 | #include <u-boot/sha256.h> |
| 21 | |
Klaus Goger | a13110a | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 22 | DECLARE_GLOBAL_DATA_PTR; |
| 23 | |
| 24 | int board_init(void) |
| 25 | { |
Klaus Goger | a13110a | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 26 | int ret; |
| 27 | |
| 28 | /* |
Philipp Tomsich | 0b5e7aa | 2017-09-29 19:28:00 +0200 | [diff] [blame] | 29 | * We need to call into regulators_enable_boot_on() again, as the call |
| 30 | * during SPL may have not included all regulators. |
Klaus Goger | a13110a | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 31 | */ |
Philipp Tomsich | 0b5e7aa | 2017-09-29 19:28:00 +0200 | [diff] [blame] | 32 | ret = regulators_enable_boot_on(false); |
Klaus Goger | a13110a | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 33 | if (ret) |
Philipp Tomsich | 0b5e7aa | 2017-09-29 19:28:00 +0200 | [diff] [blame] | 34 | debug("%s: Cannot enable boot on regulator\n", __func__); |
Klaus Goger | a13110a | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 35 | |
Klaus Goger | a13110a | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 36 | return 0; |
| 37 | } |
| 38 | |
Philipp Tomsich | ae0d33a | 2017-11-28 17:56:11 +0100 | [diff] [blame^] | 39 | static void rk3399_force_power_on_reset(void) |
| 40 | { |
| 41 | ofnode node; |
| 42 | struct gpio_desc sysreset_gpio; |
| 43 | |
| 44 | debug("%s: trying to force a power-on reset\n", __func__); |
| 45 | |
| 46 | node = ofnode_path("/config"); |
| 47 | if (!ofnode_valid(node)) { |
| 48 | debug("%s: no /config node?\n", __func__); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | if (gpio_request_by_name_nodev(node, "sysreset-gpio", 0, |
| 53 | &sysreset_gpio, GPIOD_IS_OUT)) { |
| 54 | debug("%s: could not find a /config/sysreset-gpio\n", __func__); |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | dm_gpio_set_value(&sysreset_gpio, 1); |
| 59 | } |
| 60 | |
Philipp Tomsich | fb74064 | 2017-09-29 19:27:54 +0200 | [diff] [blame] | 61 | void spl_board_init(void) |
| 62 | { |
Philipp Tomsich | 482cf22 | 2017-09-29 19:28:01 +0200 | [diff] [blame] | 63 | int ret; |
Philipp Tomsich | ae0d33a | 2017-11-28 17:56:11 +0100 | [diff] [blame^] | 64 | struct rk3399_cru *cru = rockchip_get_cru(); |
| 65 | |
| 66 | /* |
| 67 | * The RK3399 resets only 'almost all logic' (see also in the TRM |
| 68 | * "3.9.4 Global software reset"), when issuing a software reset. |
| 69 | * This may cause issues during boot-up for some configurations of |
| 70 | * the application software stack. |
| 71 | * |
| 72 | * To work around this, we test whether the last reset reason was |
| 73 | * a power-on reset and (if not) issue an overtemp-reset to reset |
| 74 | * the entire module. |
| 75 | * |
| 76 | * While this was previously fixed by modifying the various places |
| 77 | * that could generate a software reset (e.g. U-Boot's sysreset |
| 78 | * driver, the ATF or Linux), we now have it here to ensure that |
| 79 | * we no longer have to track this through the various components. |
| 80 | */ |
| 81 | if (cru->glb_rst_st != 0) |
| 82 | rk3399_force_power_on_reset(); |
Philipp Tomsich | 482cf22 | 2017-09-29 19:28:01 +0200 | [diff] [blame] | 83 | |
| 84 | /* |
| 85 | * Turning the eMMC and SPI back on (if disabled via the Qseven |
| 86 | * BIOS_ENABLE) signal is done through a always-on regulator). |
| 87 | */ |
| 88 | ret = regulators_enable_boot_on(false); |
| 89 | if (ret) |
| 90 | debug("%s: Cannot enable boot on regulator\n", __func__); |
| 91 | |
Philipp Tomsich | fb74064 | 2017-09-29 19:27:54 +0200 | [diff] [blame] | 92 | preloader_console_init(); |
| 93 | } |
| 94 | |
Klaus Goger | 8adc9d1 | 2017-05-05 19:21:40 +0200 | [diff] [blame] | 95 | static void setup_macaddr(void) |
| 96 | { |
| 97 | #if CONFIG_IS_ENABLED(CMD_NET) |
| 98 | int ret; |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 99 | const char *cpuid = env_get("cpuid#"); |
Klaus Goger | 8adc9d1 | 2017-05-05 19:21:40 +0200 | [diff] [blame] | 100 | u8 hash[SHA256_SUM_LEN]; |
| 101 | int size = sizeof(hash); |
| 102 | u8 mac_addr[6]; |
| 103 | |
| 104 | /* Only generate a MAC address, if none is set in the environment */ |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 105 | if (env_get("ethaddr")) |
Klaus Goger | 8adc9d1 | 2017-05-05 19:21:40 +0200 | [diff] [blame] | 106 | return; |
| 107 | |
| 108 | if (!cpuid) { |
| 109 | debug("%s: could not retrieve 'cpuid#'\n", __func__); |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | ret = hash_block("sha256", (void *)cpuid, strlen(cpuid), hash, &size); |
| 114 | if (ret) { |
| 115 | debug("%s: failed to calculate SHA256\n", __func__); |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | /* Copy 6 bytes of the hash to base the MAC address on */ |
| 120 | memcpy(mac_addr, hash, 6); |
| 121 | |
| 122 | /* Make this a valid MAC address and set it */ |
| 123 | mac_addr[0] &= 0xfe; /* clear multicast bit */ |
| 124 | mac_addr[0] |= 0x02; /* set local assignment bit (IEEE802) */ |
Simon Glass | fd1e959 | 2017-08-03 12:22:11 -0600 | [diff] [blame] | 125 | eth_env_set_enetaddr("ethaddr", mac_addr); |
Klaus Goger | 8adc9d1 | 2017-05-05 19:21:40 +0200 | [diff] [blame] | 126 | #endif |
Klaus Goger | 8adc9d1 | 2017-05-05 19:21:40 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Philipp Tomsich | 9415b9a | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 129 | static void setup_serial(void) |
| 130 | { |
| 131 | #if CONFIG_IS_ENABLED(ROCKCHIP_EFUSE) |
Kever Yang | 2672233 | 2017-07-27 19:59:03 +0800 | [diff] [blame] | 132 | const u32 cpuid_offset = 0x7; |
| 133 | const u32 cpuid_length = 0x10; |
| 134 | |
Philipp Tomsich | 9415b9a | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 135 | struct udevice *dev; |
| 136 | int ret, i; |
Kever Yang | 2672233 | 2017-07-27 19:59:03 +0800 | [diff] [blame] | 137 | u8 cpuid[cpuid_length]; |
| 138 | u8 low[cpuid_length/2], high[cpuid_length/2]; |
| 139 | char cpuid_str[cpuid_length * 2 + 1]; |
Philipp Tomsich | 9415b9a | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 140 | u64 serialno; |
Klaus Goger | 60d7c50 | 2017-09-15 14:46:04 +0200 | [diff] [blame] | 141 | char serialno_str[17]; |
Philipp Tomsich | 9415b9a | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 142 | |
Klaus Goger | 8adc9d1 | 2017-05-05 19:21:40 +0200 | [diff] [blame] | 143 | /* retrieve the device */ |
| 144 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
| 145 | DM_GET_DRIVER(rockchip_efuse), &dev); |
Philipp Tomsich | 9415b9a | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 146 | if (ret) { |
| 147 | debug("%s: could not find efuse device\n", __func__); |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | /* read the cpu_id range from the efuses */ |
Kever Yang | 2672233 | 2017-07-27 19:59:03 +0800 | [diff] [blame] | 152 | ret = misc_read(dev, cpuid_offset, &cpuid, sizeof(cpuid)); |
Philipp Tomsich | 9415b9a | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 153 | if (ret) { |
| 154 | debug("%s: reading cpuid from the efuses failed\n", |
| 155 | __func__); |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | memset(cpuid_str, 0, sizeof(cpuid_str)); |
| 160 | for (i = 0; i < 16; i++) |
| 161 | sprintf(&cpuid_str[i * 2], "%02x", cpuid[i]); |
| 162 | |
| 163 | debug("cpuid: %s\n", cpuid_str); |
| 164 | |
| 165 | /* |
| 166 | * Mix the cpuid bytes using the same rules as in |
| 167 | * ${linux}/drivers/soc/rockchip/rockchip-cpuinfo.c |
| 168 | */ |
| 169 | for (i = 0; i < 8; i++) { |
| 170 | low[i] = cpuid[1 + (i << 1)]; |
| 171 | high[i] = cpuid[i << 1]; |
| 172 | } |
| 173 | |
| 174 | serialno = crc32_no_comp(0, low, 8); |
| 175 | serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32; |
| 176 | snprintf(serialno_str, sizeof(serialno_str), "%llx", serialno); |
| 177 | |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 178 | env_set("cpuid#", cpuid_str); |
| 179 | env_set("serial#", serialno_str); |
Philipp Tomsich | 9415b9a | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 180 | #endif |
Philipp Tomsich | 9415b9a | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | int misc_init_r(void) |
| 184 | { |
| 185 | setup_serial(); |
Klaus Goger | 8adc9d1 | 2017-05-05 19:21:40 +0200 | [diff] [blame] | 186 | setup_macaddr(); |
Philipp Tomsich | 9415b9a | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | #ifdef CONFIG_SERIAL_TAG |
| 192 | void get_board_serial(struct tag_serialnr *serialnr) |
| 193 | { |
| 194 | char *serial_string; |
| 195 | u64 serial = 0; |
| 196 | |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 197 | serial_string = env_get("serial#"); |
Philipp Tomsich | 9415b9a | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 198 | |
| 199 | if (serial_string) |
| 200 | serial = simple_strtoull(serial_string, NULL, 16); |
| 201 | |
| 202 | serialnr->high = (u32)(serial >> 32); |
| 203 | serialnr->low = (u32)(serial & 0xffffffff); |
| 204 | } |
| 205 | #endif |
Philipp Tomsich | 614539d | 2017-11-22 17:15:18 +0100 | [diff] [blame] | 206 | |
| 207 | /** |
| 208 | * Switch power at an external regulator (for our root hub). |
| 209 | * |
| 210 | * @param ctrl pointer to the xHCI controller |
| 211 | * @param port port number as in the control message (one-based) |
| 212 | * @param enable boolean indicating whether to enable or disable power |
| 213 | * @return returns 0 on success, an error-code on failure |
| 214 | */ |
| 215 | static int board_usb_port_power_set(struct udevice *dev, int port, |
| 216 | bool enable) |
| 217 | { |
| 218 | #if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_REGULATOR) |
| 219 | /* We start counting ports at 0, while USB counts from 1. */ |
| 220 | int index = port - 1; |
| 221 | const char *regname = NULL; |
| 222 | struct udevice *regulator; |
| 223 | const char *prop = "tsd,usb-port-power"; |
| 224 | int ret; |
| 225 | |
| 226 | debug("%s: ctrl '%s' port %d enable %s\n", __func__, |
| 227 | dev_read_name(dev), port, enable ? "true" : "false"); |
| 228 | |
| 229 | ret = dev_read_string_index(dev, prop, index, ®name); |
| 230 | if (ret < 0) { |
| 231 | debug("%s: ctrl '%s' port %d: no entry in '%s'\n", |
| 232 | __func__, dev_read_name(dev), port, prop); |
| 233 | return ret; |
| 234 | } |
| 235 | |
| 236 | ret = regulator_get_by_platname(regname, ®ulator); |
| 237 | if (ret) { |
| 238 | debug("%s: ctrl '%s' port %d: could not get regulator '%s'\n", |
| 239 | __func__, dev_read_name(dev), port, regname); |
| 240 | return ret; |
| 241 | } |
| 242 | |
| 243 | regulator_set_enable(regulator, enable); |
| 244 | return 0; |
| 245 | #else |
| 246 | return -ENOTSUPP; |
| 247 | #endif |
| 248 | } |
| 249 | |
| 250 | void usb_hub_reset_devices(struct usb_hub_device *hub, int port) |
| 251 | { |
| 252 | struct udevice *dev = hub->pusb_dev->dev; |
| 253 | struct udevice *ctrl; |
| 254 | |
| 255 | /* We are only interested in our root-hubs */ |
| 256 | if (usb_hub_is_root_hub(dev) == false) |
| 257 | return; |
| 258 | |
| 259 | ctrl = usb_get_bus(dev); |
| 260 | if (!ctrl) { |
| 261 | debug("%s: could not retrieve ctrl for hub\n", __func__); |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * To work around an incompatibility between the single-threaded |
| 267 | * USB stack in U-Boot and (a strange low-power mode of) the USB |
| 268 | * hub we have on-module, we need to delay powering on the hub |
| 269 | * until the first time the port is probed. |
| 270 | */ |
| 271 | board_usb_port_power_set(ctrl, port, true); |
| 272 | } |