2 * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
4 * SPDX-License-Identifier: GPL-2.0+
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>
19 DECLARE_GLOBAL_DATA_PTR;
26 * We need to call into regulators_enable_boot_on() again, as the call
27 * during SPL may have not included all regulators.
29 ret = regulators_enable_boot_on(false);
31 debug("%s: Cannot enable boot on regulator\n", __func__);
36 void spl_board_init(void)
41 * Turning the eMMC and SPI back on (if disabled via the Qseven
42 * BIOS_ENABLE) signal is done through a always-on regulator).
44 ret = regulators_enable_boot_on(false);
46 debug("%s: Cannot enable boot on regulator\n", __func__);
48 preloader_console_init();
51 static void setup_macaddr(void)
53 #if CONFIG_IS_ENABLED(CMD_NET)
55 const char *cpuid = env_get("cpuid#");
56 u8 hash[SHA256_SUM_LEN];
57 int size = sizeof(hash);
60 /* Only generate a MAC address, if none is set in the environment */
61 if (env_get("ethaddr"))
65 debug("%s: could not retrieve 'cpuid#'\n", __func__);
69 ret = hash_block("sha256", (void *)cpuid, strlen(cpuid), hash, &size);
71 debug("%s: failed to calculate SHA256\n", __func__);
75 /* Copy 6 bytes of the hash to base the MAC address on */
76 memcpy(mac_addr, hash, 6);
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);
85 static void setup_serial(void)
87 #if CONFIG_IS_ENABLED(ROCKCHIP_EFUSE)
88 const u32 cpuid_offset = 0x7;
89 const u32 cpuid_length = 0x10;
93 u8 cpuid[cpuid_length];
94 u8 low[cpuid_length/2], high[cpuid_length/2];
95 char cpuid_str[cpuid_length * 2 + 1];
97 char serialno_str[17];
99 /* retrieve the device */
100 ret = uclass_get_device_by_driver(UCLASS_MISC,
101 DM_GET_DRIVER(rockchip_efuse), &dev);
103 debug("%s: could not find efuse device\n", __func__);
107 /* read the cpu_id range from the efuses */
108 ret = misc_read(dev, cpuid_offset, &cpuid, sizeof(cpuid));
110 debug("%s: reading cpuid from the efuses failed\n",
115 memset(cpuid_str, 0, sizeof(cpuid_str));
116 for (i = 0; i < 16; i++)
117 sprintf(&cpuid_str[i * 2], "%02x", cpuid[i]);
119 debug("cpuid: %s\n", cpuid_str);
122 * Mix the cpuid bytes using the same rules as in
123 * ${linux}/drivers/soc/rockchip/rockchip-cpuinfo.c
125 for (i = 0; i < 8; i++) {
126 low[i] = cpuid[1 + (i << 1)];
127 high[i] = cpuid[i << 1];
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);
134 env_set("cpuid#", cpuid_str);
135 env_set("serial#", serialno_str);
139 int misc_init_r(void)
147 #ifdef CONFIG_SERIAL_TAG
148 void get_board_serial(struct tag_serialnr *serialnr)
153 serial_string = env_get("serial#");
156 serial = simple_strtoull(serial_string, NULL, 16);
158 serialnr->high = (u32)(serial >> 32);
159 serialnr->low = (u32)(serial & 0xffffffff);
164 * Switch power at an external regulator (for our root hub).
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
171 static int board_usb_port_power_set(struct udevice *dev, int port,
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";
182 debug("%s: ctrl '%s' port %d enable %s\n", __func__,
183 dev_read_name(dev), port, enable ? "true" : "false");
185 ret = dev_read_string_index(dev, prop, index, ®name);
187 debug("%s: ctrl '%s' port %d: no entry in '%s'\n",
188 __func__, dev_read_name(dev), port, prop);
192 ret = regulator_get_by_platname(regname, ®ulator);
194 debug("%s: ctrl '%s' port %d: could not get regulator '%s'\n",
195 __func__, dev_read_name(dev), port, regname);
199 regulator_set_enable(regulator, enable);
206 void usb_hub_reset_devices(struct usb_hub_device *hub, int port)
208 struct udevice *dev = hub->pusb_dev->dev;
209 struct udevice *ctrl;
211 /* We are only interested in our root-hubs */
212 if (usb_hub_is_root_hub(dev) == false)
215 ctrl = usb_get_bus(dev);
217 debug("%s: could not retrieve ctrl for hub\n", __func__);
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.
227 board_usb_port_power_set(ctrl, port, true);