1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
8 #include <environment.h>
13 #include <dm/pinctrl.h>
14 #include <dm/uclass-internal.h>
16 #include <asm/setup.h>
17 #include <asm/arch-rockchip/clock.h>
18 #include <asm/arch-rockchip/hardware.h>
19 #include <asm/arch-rockchip/grf_rk3399.h>
20 #include <asm/arch-rockchip/periph.h>
21 #include <power/regulator.h>
22 #include <u-boot/sha256.h>
29 * We need to call into regulators_enable_boot_on() again, as the call
30 * during SPL may have not included all regulators.
32 ret = regulators_enable_boot_on(false);
34 debug("%s: Cannot enable boot on regulator\n", __func__);
39 static void setup_macaddr(void)
41 #if CONFIG_IS_ENABLED(CMD_NET)
43 const char *cpuid = env_get("cpuid#");
44 u8 hash[SHA256_SUM_LEN];
45 int size = sizeof(hash);
48 /* Only generate a MAC address, if none is set in the environment */
49 if (env_get("ethaddr"))
53 debug("%s: could not retrieve 'cpuid#'\n", __func__);
57 ret = hash_block("sha256", (void *)cpuid, strlen(cpuid), hash, &size);
59 debug("%s: failed to calculate SHA256\n", __func__);
63 /* Copy 6 bytes of the hash to base the MAC address on */
64 memcpy(mac_addr, hash, 6);
66 /* Make this a valid MAC address and set it */
67 mac_addr[0] &= 0xfe; /* clear multicast bit */
68 mac_addr[0] |= 0x02; /* set local assignment bit (IEEE802) */
69 eth_env_set_enetaddr("ethaddr", mac_addr);
73 static void setup_serial(void)
75 #if CONFIG_IS_ENABLED(ROCKCHIP_EFUSE)
76 const u32 cpuid_offset = 0x7;
77 const u32 cpuid_length = 0x10;
81 u8 cpuid[cpuid_length];
82 u8 low[cpuid_length/2], high[cpuid_length/2];
83 char cpuid_str[cpuid_length * 2 + 1];
85 char serialno_str[17];
87 /* retrieve the device */
88 ret = uclass_get_device_by_driver(UCLASS_MISC,
89 DM_GET_DRIVER(rockchip_efuse), &dev);
91 debug("%s: could not find efuse device\n", __func__);
95 /* read the cpu_id range from the efuses */
96 ret = misc_read(dev, cpuid_offset, &cpuid, sizeof(cpuid));
98 debug("%s: reading cpuid from the efuses failed\n",
103 memset(cpuid_str, 0, sizeof(cpuid_str));
104 for (i = 0; i < 16; i++)
105 sprintf(&cpuid_str[i * 2], "%02x", cpuid[i]);
107 debug("cpuid: %s\n", cpuid_str);
110 * Mix the cpuid bytes using the same rules as in
111 * ${linux}/drivers/soc/rockchip/rockchip-cpuinfo.c
113 for (i = 0; i < 8; i++) {
114 low[i] = cpuid[1 + (i << 1)];
115 high[i] = cpuid[i << 1];
118 serialno = crc32_no_comp(0, low, 8);
119 serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32;
120 snprintf(serialno_str, sizeof(serialno_str), "%016llx", serialno);
122 env_set("cpuid#", cpuid_str);
123 env_set("serial#", serialno_str);
127 static void setup_iodomain(void)
129 const u32 GRF_IO_VSEL_GPIO4CD_SHIFT = 3;
130 struct rk3399_grf_regs *grf =
131 syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
134 * Set bit 3 in GRF_IO_VSEL so PCIE_RST# works (pin GPIO4_C6).
135 * Linux assumes that PCIE_RST# works out of the box as it probes
136 * PCIe before loading the iodomain driver.
138 rk_setreg(&grf->io_vsel, 1 << GRF_IO_VSEL_GPIO4CD_SHIFT);
142 * Swap mmc0 and mmc1 in boot_targets if booted from SD-Card.
144 * If bootsource is uSD-card we can assume that we want to use the
145 * SD-Card instead of the eMMC as first boot_target for distroboot.
146 * We only want to swap the defaults and not any custom environment a
147 * user has set. We exit early if a changed boot_targets environment
150 static int setup_boottargets(void)
152 const char *boot_device =
153 ofnode_get_chosen_prop("u-boot,spl-boot-device");
154 char *env_default, *env;
157 debug("%s: /chosen/u-boot,spl-boot-device not set\n",
161 debug("%s: booted from %s\n", __func__, boot_device);
163 env_default = env_get_default("boot_targets");
164 env = env_get("boot_targets");
166 debug("%s: boot_targets does not exist\n", __func__);
169 debug("%s: boot_targets current: %s - default: %s\n",
170 __func__, env, env_default);
172 if (strcmp(env_default, env) != 0) {
173 debug("%s: boot_targets not default, don't change it\n",
179 * Only run, if booting from mmc1 (i.e. /dwmmc@fe320000) and
180 * only consider cases where the default boot-order first
181 * tries to boot from mmc0 (eMMC) and then from mmc1
182 * (i.e. external SD).
184 * In other words: the SD card will be moved to earlier in the
185 * order, if U-Boot was also loaded from the SD-card.
187 if (!strcmp(boot_device, "/dwmmc@fe320000")) {
190 debug("%s: booted from SD-Card\n", __func__);
191 mmc0 = strstr(env, "mmc0");
192 mmc1 = strstr(env, "mmc1");
194 if (!mmc0 || !mmc1) {
195 debug("%s: only one mmc boot_target found\n", __func__);
200 * If mmc0 comes first in the boot order, we need to change
201 * the strings to make mmc1 first.
206 debug("%s: set boot_targets to: %s\n", __func__, env);
207 env_set("boot_targets", env);
214 int misc_init_r(void)
224 #ifdef CONFIG_SERIAL_TAG
225 void get_board_serial(struct tag_serialnr *serialnr)
230 serial_string = env_get("serial#");
233 serial = simple_strtoull(serial_string, NULL, 16);
235 serialnr->high = (u32)(serial >> 32);
236 serialnr->low = (u32)(serial & 0xffffffff);
241 * Switch power at an external regulator (for our root hub).
243 * @param ctrl pointer to the xHCI controller
244 * @param port port number as in the control message (one-based)
245 * @param enable boolean indicating whether to enable or disable power
246 * @return returns 0 on success, an error-code on failure
248 static int board_usb_port_power_set(struct udevice *dev, int port,
251 #if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_REGULATOR)
252 /* We start counting ports at 0, while USB counts from 1. */
253 int index = port - 1;
254 const char *regname = NULL;
255 struct udevice *regulator;
256 const char *prop = "tsd,usb-port-power";
259 debug("%s: ctrl '%s' port %d enable %s\n", __func__,
260 dev_read_name(dev), port, enable ? "true" : "false");
262 ret = dev_read_string_index(dev, prop, index, ®name);
264 debug("%s: ctrl '%s' port %d: no entry in '%s'\n",
265 __func__, dev_read_name(dev), port, prop);
269 ret = regulator_get_by_platname(regname, ®ulator);
271 debug("%s: ctrl '%s' port %d: could not get regulator '%s'\n",
272 __func__, dev_read_name(dev), port, regname);
276 regulator_set_enable(regulator, enable);
283 void usb_hub_reset_devices(struct usb_hub_device *hub, int port)
285 struct udevice *dev = hub->pusb_dev->dev;
286 struct udevice *ctrl;
288 /* We are only interested in our root-hubs */
289 if (usb_hub_is_root_hub(dev) == false)
292 ctrl = usb_get_bus(dev);
294 debug("%s: could not retrieve ctrl for hub\n", __func__);
299 * To work around an incompatibility between the single-threaded
300 * USB stack in U-Boot and (a strange low-power mode of) the USB
301 * hub we have on-module, we need to delay powering on the hub
302 * until the first time the port is probed.
304 board_usb_port_power_set(ctrl, port, true);