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