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 <misc.h> 10 #include <spl.h> 11 #include <usb.h> 12 #include <dm/pinctrl.h> 13 #include <dm/uclass-internal.h> 14 #include <asm/setup.h> 15 #include <asm/arch/periph.h> 16 #include <power/regulator.h> 17 #include <u-boot/sha256.h> 18 19 DECLARE_GLOBAL_DATA_PTR; 20 21 int board_init(void) 22 { 23 int ret; 24 25 /* 26 * We need to call into regulators_enable_boot_on() again, as the call 27 * during SPL may have not included all regulators. 28 */ 29 ret = regulators_enable_boot_on(false); 30 if (ret) 31 debug("%s: Cannot enable boot on regulator\n", __func__); 32 33 return 0; 34 } 35 36 void spl_board_init(void) 37 { 38 int ret; 39 40 /* 41 * Turning the eMMC and SPI back on (if disabled via the Qseven 42 * BIOS_ENABLE) signal is done through a always-on regulator). 43 */ 44 ret = regulators_enable_boot_on(false); 45 if (ret) 46 debug("%s: Cannot enable boot on regulator\n", __func__); 47 48 preloader_console_init(); 49 } 50 51 static void setup_macaddr(void) 52 { 53 #if CONFIG_IS_ENABLED(CMD_NET) 54 int ret; 55 const char *cpuid = env_get("cpuid#"); 56 u8 hash[SHA256_SUM_LEN]; 57 int size = sizeof(hash); 58 u8 mac_addr[6]; 59 60 /* Only generate a MAC address, if none is set in the environment */ 61 if (env_get("ethaddr")) 62 return; 63 64 if (!cpuid) { 65 debug("%s: could not retrieve 'cpuid#'\n", __func__); 66 return; 67 } 68 69 ret = hash_block("sha256", (void *)cpuid, strlen(cpuid), hash, &size); 70 if (ret) { 71 debug("%s: failed to calculate SHA256\n", __func__); 72 return; 73 } 74 75 /* Copy 6 bytes of the hash to base the MAC address on */ 76 memcpy(mac_addr, hash, 6); 77 78 /* Make this a valid MAC address and set it */ 79 mac_addr[0] &= 0xfe; /* clear multicast bit */ 80 mac_addr[0] |= 0x02; /* set local assignment bit (IEEE802) */ 81 eth_env_set_enetaddr("ethaddr", mac_addr); 82 #endif 83 } 84 85 static void setup_serial(void) 86 { 87 #if CONFIG_IS_ENABLED(ROCKCHIP_EFUSE) 88 const u32 cpuid_offset = 0x7; 89 const u32 cpuid_length = 0x10; 90 91 struct udevice *dev; 92 int ret, i; 93 u8 cpuid[cpuid_length]; 94 u8 low[cpuid_length/2], high[cpuid_length/2]; 95 char cpuid_str[cpuid_length * 2 + 1]; 96 u64 serialno; 97 char serialno_str[17]; 98 99 /* retrieve the device */ 100 ret = uclass_get_device_by_driver(UCLASS_MISC, 101 DM_GET_DRIVER(rockchip_efuse), &dev); 102 if (ret) { 103 debug("%s: could not find efuse device\n", __func__); 104 return; 105 } 106 107 /* read the cpu_id range from the efuses */ 108 ret = misc_read(dev, cpuid_offset, &cpuid, sizeof(cpuid)); 109 if (ret) { 110 debug("%s: reading cpuid from the efuses failed\n", 111 __func__); 112 return; 113 } 114 115 memset(cpuid_str, 0, sizeof(cpuid_str)); 116 for (i = 0; i < 16; i++) 117 sprintf(&cpuid_str[i * 2], "%02x", cpuid[i]); 118 119 debug("cpuid: %s\n", cpuid_str); 120 121 /* 122 * Mix the cpuid bytes using the same rules as in 123 * ${linux}/drivers/soc/rockchip/rockchip-cpuinfo.c 124 */ 125 for (i = 0; i < 8; i++) { 126 low[i] = cpuid[1 + (i << 1)]; 127 high[i] = cpuid[i << 1]; 128 } 129 130 serialno = crc32_no_comp(0, low, 8); 131 serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32; 132 snprintf(serialno_str, sizeof(serialno_str), "%llx", serialno); 133 134 env_set("cpuid#", cpuid_str); 135 env_set("serial#", serialno_str); 136 #endif 137 } 138 139 int misc_init_r(void) 140 { 141 setup_serial(); 142 setup_macaddr(); 143 144 return 0; 145 } 146 147 #ifdef CONFIG_SERIAL_TAG 148 void get_board_serial(struct tag_serialnr *serialnr) 149 { 150 char *serial_string; 151 u64 serial = 0; 152 153 serial_string = env_get("serial#"); 154 155 if (serial_string) 156 serial = simple_strtoull(serial_string, NULL, 16); 157 158 serialnr->high = (u32)(serial >> 32); 159 serialnr->low = (u32)(serial & 0xffffffff); 160 } 161 #endif 162 163 /** 164 * Switch power at an external regulator (for our root hub). 165 * 166 * @param ctrl pointer to the xHCI controller 167 * @param port port number as in the control message (one-based) 168 * @param enable boolean indicating whether to enable or disable power 169 * @return returns 0 on success, an error-code on failure 170 */ 171 static int board_usb_port_power_set(struct udevice *dev, int port, 172 bool enable) 173 { 174 #if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_REGULATOR) 175 /* We start counting ports at 0, while USB counts from 1. */ 176 int index = port - 1; 177 const char *regname = NULL; 178 struct udevice *regulator; 179 const char *prop = "tsd,usb-port-power"; 180 int ret; 181 182 debug("%s: ctrl '%s' port %d enable %s\n", __func__, 183 dev_read_name(dev), port, enable ? "true" : "false"); 184 185 ret = dev_read_string_index(dev, prop, index, ®name); 186 if (ret < 0) { 187 debug("%s: ctrl '%s' port %d: no entry in '%s'\n", 188 __func__, dev_read_name(dev), port, prop); 189 return ret; 190 } 191 192 ret = regulator_get_by_platname(regname, ®ulator); 193 if (ret) { 194 debug("%s: ctrl '%s' port %d: could not get regulator '%s'\n", 195 __func__, dev_read_name(dev), port, regname); 196 return ret; 197 } 198 199 regulator_set_enable(regulator, enable); 200 return 0; 201 #else 202 return -ENOTSUPP; 203 #endif 204 } 205 206 void usb_hub_reset_devices(struct usb_hub_device *hub, int port) 207 { 208 struct udevice *dev = hub->pusb_dev->dev; 209 struct udevice *ctrl; 210 211 /* We are only interested in our root-hubs */ 212 if (usb_hub_is_root_hub(dev) == false) 213 return; 214 215 ctrl = usb_get_bus(dev); 216 if (!ctrl) { 217 debug("%s: could not retrieve ctrl for hub\n", __func__); 218 return; 219 } 220 221 /* 222 * To work around an incompatibility between the single-threaded 223 * USB stack in U-Boot and (a strange low-power mode of) the USB 224 * hub we have on-module, we need to delay powering on the hub 225 * until the first time the port is probed. 226 */ 227 board_usb_port_power_set(ctrl, port, true); 228 } 229