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