1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
14 #include <u-boot/crc.h>
16 #include <dm/pinctrl.h>
17 #include <dm/uclass-internal.h>
19 #include <asm/setup.h>
20 #include <asm/arch-rockchip/clock.h>
21 #include <asm/arch-rockchip/hardware.h>
22 #include <asm/arch-rockchip/grf_rk3399.h>
23 #include <asm/arch-rockchip/periph.h>
24 #include <asm/arch-rockchip/misc.h>
25 #include <power/regulator.h>
26 #include <u-boot/sha256.h>
28 static void setup_iodomain(void)
30 const u32 GRF_IO_VSEL_GPIO4CD_SHIFT = 3;
31 struct rk3399_grf_regs *grf =
32 syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
35 * Set bit 3 in GRF_IO_VSEL so PCIE_RST# works (pin GPIO4_C6).
36 * Linux assumes that PCIE_RST# works out of the box as it probes
37 * PCIe before loading the iodomain driver.
39 rk_setreg(&grf->io_vsel, 1 << GRF_IO_VSEL_GPIO4CD_SHIFT);
43 * Swap mmc0 and mmc1 in boot_targets if booted from SD-Card.
45 * If bootsource is uSD-card we can assume that we want to use the
46 * SD-Card instead of the eMMC as first boot_target for distroboot.
47 * We only want to swap the defaults and not any custom environment a
48 * user has set. We exit early if a changed boot_targets environment
51 static int setup_boottargets(void)
53 const char *boot_device =
54 ofnode_read_chosen_string("u-boot,spl-boot-device");
55 char *env_default, *env;
58 debug("%s: /chosen/u-boot,spl-boot-device not set\n",
62 debug("%s: booted from %s\n", __func__, boot_device);
64 env_default = env_get_default("boot_targets");
65 env = env_get("boot_targets");
67 debug("%s: boot_targets does not exist\n", __func__);
70 debug("%s: boot_targets current: %s - default: %s\n",
71 __func__, env, env_default);
73 if (strcmp(env_default, env) != 0) {
74 debug("%s: boot_targets not default, don't change it\n",
80 * Only run, if booting from mmc1 (i.e. /mmc@fe320000) and
81 * only consider cases where the default boot-order first
82 * tries to boot from mmc0 (eMMC) and then from mmc1
85 * In other words: the SD card will be moved to earlier in the
86 * order, if U-Boot was also loaded from the SD-card.
88 if (!strcmp(boot_device, "/mmc@fe320000")) {
91 debug("%s: booted from SD-Card\n", __func__);
92 mmc0 = strstr(env, "mmc0");
93 mmc1 = strstr(env, "mmc1");
96 debug("%s: only one mmc boot_target found\n", __func__);
101 * If mmc0 comes first in the boot order, we need to change
102 * the strings to make mmc1 first.
107 debug("%s: set boot_targets to: %s\n", __func__, env);
108 env_set("boot_targets", env);
115 int misc_init_r(void)
117 const u32 cpuid_offset = 0x7;
118 const u32 cpuid_length = 0x10;
119 u8 cpuid[cpuid_length];
122 ret = rockchip_cpuid_from_efuse(cpuid_offset, cpuid_length, cpuid);
126 ret = rockchip_cpuid_set(cpuid, cpuid_length);
130 ret = rockchip_setup_macaddr();
140 #ifdef CONFIG_SERIAL_TAG
141 void get_board_serial(struct tag_serialnr *serialnr)
146 serial_string = env_get("serial#");
149 serial = simple_strtoull(serial_string, NULL, 16);
151 serialnr->high = (u32)(serial >> 32);
152 serialnr->low = (u32)(serial & 0xffffffff);
157 * Switch power at an external regulator (for our root hub).
159 * @param ctrl pointer to the xHCI controller
160 * @param port port number as in the control message (one-based)
161 * @param enable boolean indicating whether to enable or disable power
162 * @return returns 0 on success, an error-code on failure
164 static int board_usb_port_power_set(struct udevice *dev, int port,
167 #if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_REGULATOR)
168 /* We start counting ports at 0, while USB counts from 1. */
169 int index = port - 1;
170 const char *regname = NULL;
171 struct udevice *regulator;
172 const char *prop = "tsd,usb-port-power";
175 debug("%s: ctrl '%s' port %d enable %s\n", __func__,
176 dev_read_name(dev), port, enable ? "true" : "false");
178 ret = dev_read_string_index(dev, prop, index, ®name);
180 debug("%s: ctrl '%s' port %d: no entry in '%s'\n",
181 __func__, dev_read_name(dev), port, prop);
185 ret = regulator_get_by_platname(regname, ®ulator);
187 debug("%s: ctrl '%s' port %d: could not get regulator '%s'\n",
188 __func__, dev_read_name(dev), port, regname);
192 regulator_set_enable(regulator, enable);
199 void usb_hub_reset_devices(struct usb_hub_device *hub, int port)
201 struct udevice *dev = hub->pusb_dev->dev;
202 struct udevice *ctrl;
204 /* We are only interested in our root-hubs */
205 if (usb_hub_is_root_hub(dev) == false)
208 ctrl = usb_get_bus(dev);
210 debug("%s: could not retrieve ctrl for hub\n", __func__);
215 * To work around an incompatibility between the single-threaded
216 * USB stack in U-Boot and (a strange low-power mode of) the USB
217 * hub we have on-module, we need to delay powering on the hub
218 * until the first time the port is probed.
220 board_usb_port_power_set(ctrl, port, true);