1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2a13110a9SKlaus Goger /*
3a13110a9SKlaus Goger  * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
4a13110a9SKlaus Goger  */
5fb740646SPhilipp Tomsich 
6a13110a9SKlaus Goger #include <common.h>
7a13110a9SKlaus Goger #include <dm.h>
89925f1dbSAlex Kiernan #include <environment.h>
9e92e5803SPhilipp Tomsich #include <misc.h>
10614539d4SPhilipp Tomsich #include <spl.h>
11aa41220fSJakob Unterwurzacher #include <syscon.h>
12614539d4SPhilipp Tomsich #include <usb.h>
13a13110a9SKlaus Goger #include <dm/pinctrl.h>
14a13110a9SKlaus Goger #include <dm/uclass-internal.h>
15aa41220fSJakob Unterwurzacher #include <asm/io.h>
16ae0d33a7SPhilipp Tomsich #include <asm/gpio.h>
179415b9a7SPhilipp Tomsich #include <asm/setup.h>
18ae0d33a7SPhilipp Tomsich #include <asm/arch/clock.h>
19ae0d33a7SPhilipp Tomsich #include <asm/arch/cru_rk3399.h>
20aa41220fSJakob Unterwurzacher #include <asm/arch/hardware.h>
21aa41220fSJakob Unterwurzacher #include <asm/arch/grf_rk3399.h>
22a13110a9SKlaus Goger #include <asm/arch/periph.h>
23a13110a9SKlaus Goger #include <power/regulator.h>
24e92e5803SPhilipp Tomsich #include <u-boot/sha256.h>
25e92e5803SPhilipp Tomsich 
26a13110a9SKlaus Goger int board_init(void)
27a13110a9SKlaus Goger {
28a13110a9SKlaus Goger 	int ret;
29a13110a9SKlaus Goger 
30a13110a9SKlaus Goger 	/*
310b5e7aabSPhilipp Tomsich 	 * We need to call into regulators_enable_boot_on() again, as the call
320b5e7aabSPhilipp Tomsich 	 * during SPL may have not included all regulators.
33a13110a9SKlaus Goger 	 */
340b5e7aabSPhilipp Tomsich 	ret = regulators_enable_boot_on(false);
35a13110a9SKlaus Goger 	if (ret)
360b5e7aabSPhilipp Tomsich 		debug("%s: Cannot enable boot on regulator\n", __func__);
37a13110a9SKlaus Goger 
38a13110a9SKlaus Goger 	return 0;
39a13110a9SKlaus Goger }
40a13110a9SKlaus Goger 
41ae0d33a7SPhilipp Tomsich static void rk3399_force_power_on_reset(void)
42ae0d33a7SPhilipp Tomsich {
43ae0d33a7SPhilipp Tomsich 	ofnode node;
44ae0d33a7SPhilipp Tomsich 	struct gpio_desc sysreset_gpio;
45ae0d33a7SPhilipp Tomsich 
46ae0d33a7SPhilipp Tomsich 	debug("%s: trying to force a power-on reset\n", __func__);
47ae0d33a7SPhilipp Tomsich 
48ae0d33a7SPhilipp Tomsich 	node = ofnode_path("/config");
49ae0d33a7SPhilipp Tomsich 	if (!ofnode_valid(node)) {
50ae0d33a7SPhilipp Tomsich 		debug("%s: no /config node?\n", __func__);
51ae0d33a7SPhilipp Tomsich 		return;
52ae0d33a7SPhilipp Tomsich 	}
53ae0d33a7SPhilipp Tomsich 
54ae0d33a7SPhilipp Tomsich 	if (gpio_request_by_name_nodev(node, "sysreset-gpio", 0,
55ae0d33a7SPhilipp Tomsich 				       &sysreset_gpio, GPIOD_IS_OUT)) {
56ae0d33a7SPhilipp Tomsich 		debug("%s: could not find a /config/sysreset-gpio\n", __func__);
57ae0d33a7SPhilipp Tomsich 		return;
58ae0d33a7SPhilipp Tomsich 	}
59ae0d33a7SPhilipp Tomsich 
60ae0d33a7SPhilipp Tomsich 	dm_gpio_set_value(&sysreset_gpio, 1);
61ae0d33a7SPhilipp Tomsich }
62ae0d33a7SPhilipp Tomsich 
63fb740646SPhilipp Tomsich void spl_board_init(void)
64fb740646SPhilipp Tomsich {
65482cf223SPhilipp Tomsich 	int  ret;
66ae0d33a7SPhilipp Tomsich 	struct rk3399_cru *cru = rockchip_get_cru();
67ae0d33a7SPhilipp Tomsich 
68ae0d33a7SPhilipp Tomsich 	/*
69ae0d33a7SPhilipp Tomsich 	 * The RK3399 resets only 'almost all logic' (see also in the TRM
70ae0d33a7SPhilipp Tomsich 	 * "3.9.4 Global software reset"), when issuing a software reset.
71ae0d33a7SPhilipp Tomsich 	 * This may cause issues during boot-up for some configurations of
72ae0d33a7SPhilipp Tomsich 	 * the application software stack.
73ae0d33a7SPhilipp Tomsich 	 *
74ae0d33a7SPhilipp Tomsich 	 * To work around this, we test whether the last reset reason was
75ae0d33a7SPhilipp Tomsich 	 * a power-on reset and (if not) issue an overtemp-reset to reset
76ae0d33a7SPhilipp Tomsich 	 * the entire module.
77ae0d33a7SPhilipp Tomsich 	 *
78ae0d33a7SPhilipp Tomsich 	 * While this was previously fixed by modifying the various places
79ae0d33a7SPhilipp Tomsich 	 * that could generate a software reset (e.g. U-Boot's sysreset
80ae0d33a7SPhilipp Tomsich 	 * driver, the ATF or Linux), we now have it here to ensure that
81ae0d33a7SPhilipp Tomsich 	 * we no longer have to track this through the various components.
82ae0d33a7SPhilipp Tomsich 	 */
83ae0d33a7SPhilipp Tomsich 	if (cru->glb_rst_st != 0)
84ae0d33a7SPhilipp Tomsich 		rk3399_force_power_on_reset();
85482cf223SPhilipp Tomsich 
86482cf223SPhilipp Tomsich 	/*
87482cf223SPhilipp Tomsich 	 * Turning the eMMC and SPI back on (if disabled via the Qseven
88482cf223SPhilipp Tomsich 	 * BIOS_ENABLE) signal is done through a always-on regulator).
89482cf223SPhilipp Tomsich 	 */
90482cf223SPhilipp Tomsich 	ret = regulators_enable_boot_on(false);
91482cf223SPhilipp Tomsich 	if (ret)
92482cf223SPhilipp Tomsich 		debug("%s: Cannot enable boot on regulator\n", __func__);
93482cf223SPhilipp Tomsich 
94fb740646SPhilipp Tomsich 	preloader_console_init();
95fb740646SPhilipp Tomsich }
96fb740646SPhilipp Tomsich 
978adc9d18SKlaus Goger static void setup_macaddr(void)
988adc9d18SKlaus Goger {
998adc9d18SKlaus Goger #if CONFIG_IS_ENABLED(CMD_NET)
1008adc9d18SKlaus Goger 	int ret;
10100caae6dSSimon Glass 	const char *cpuid = env_get("cpuid#");
1028adc9d18SKlaus Goger 	u8 hash[SHA256_SUM_LEN];
1038adc9d18SKlaus Goger 	int size = sizeof(hash);
1048adc9d18SKlaus Goger 	u8 mac_addr[6];
1058adc9d18SKlaus Goger 
1068adc9d18SKlaus Goger 	/* Only generate a MAC address, if none is set in the environment */
10700caae6dSSimon Glass 	if (env_get("ethaddr"))
1088adc9d18SKlaus Goger 		return;
1098adc9d18SKlaus Goger 
1108adc9d18SKlaus Goger 	if (!cpuid) {
1118adc9d18SKlaus Goger 		debug("%s: could not retrieve 'cpuid#'\n", __func__);
1128adc9d18SKlaus Goger 		return;
1138adc9d18SKlaus Goger 	}
1148adc9d18SKlaus Goger 
1158adc9d18SKlaus Goger 	ret = hash_block("sha256", (void *)cpuid, strlen(cpuid), hash, &size);
1168adc9d18SKlaus Goger 	if (ret) {
1178adc9d18SKlaus Goger 		debug("%s: failed to calculate SHA256\n", __func__);
1188adc9d18SKlaus Goger 		return;
1198adc9d18SKlaus Goger 	}
1208adc9d18SKlaus Goger 
1218adc9d18SKlaus Goger 	/* Copy 6 bytes of the hash to base the MAC address on */
1228adc9d18SKlaus Goger 	memcpy(mac_addr, hash, 6);
1238adc9d18SKlaus Goger 
1248adc9d18SKlaus Goger 	/* Make this a valid MAC address and set it */
1258adc9d18SKlaus Goger 	mac_addr[0] &= 0xfe;  /* clear multicast bit */
1268adc9d18SKlaus Goger 	mac_addr[0] |= 0x02;  /* set local assignment bit (IEEE802) */
127fd1e959eSSimon Glass 	eth_env_set_enetaddr("ethaddr", mac_addr);
1288adc9d18SKlaus Goger #endif
1298adc9d18SKlaus Goger }
1308adc9d18SKlaus Goger 
1319415b9a7SPhilipp Tomsich static void setup_serial(void)
1329415b9a7SPhilipp Tomsich {
1339415b9a7SPhilipp Tomsich #if CONFIG_IS_ENABLED(ROCKCHIP_EFUSE)
13426722335SKever Yang 	const u32 cpuid_offset = 0x7;
13526722335SKever Yang 	const u32 cpuid_length = 0x10;
13626722335SKever Yang 
1379415b9a7SPhilipp Tomsich 	struct udevice *dev;
1389415b9a7SPhilipp Tomsich 	int ret, i;
13926722335SKever Yang 	u8 cpuid[cpuid_length];
14026722335SKever Yang 	u8 low[cpuid_length/2], high[cpuid_length/2];
14126722335SKever Yang 	char cpuid_str[cpuid_length * 2 + 1];
1429415b9a7SPhilipp Tomsich 	u64 serialno;
14360d7c509SKlaus Goger 	char serialno_str[17];
1449415b9a7SPhilipp Tomsich 
1458adc9d18SKlaus Goger 	/* retrieve the device */
1468adc9d18SKlaus Goger 	ret = uclass_get_device_by_driver(UCLASS_MISC,
1478adc9d18SKlaus Goger 					  DM_GET_DRIVER(rockchip_efuse), &dev);
1489415b9a7SPhilipp Tomsich 	if (ret) {
1499415b9a7SPhilipp Tomsich 		debug("%s: could not find efuse device\n", __func__);
1509415b9a7SPhilipp Tomsich 		return;
1519415b9a7SPhilipp Tomsich 	}
1529415b9a7SPhilipp Tomsich 
1539415b9a7SPhilipp Tomsich 	/* read the cpu_id range from the efuses */
15426722335SKever Yang 	ret = misc_read(dev, cpuid_offset, &cpuid, sizeof(cpuid));
1559415b9a7SPhilipp Tomsich 	if (ret) {
1569415b9a7SPhilipp Tomsich 		debug("%s: reading cpuid from the efuses failed\n",
1579415b9a7SPhilipp Tomsich 		      __func__);
1589415b9a7SPhilipp Tomsich 		return;
1599415b9a7SPhilipp Tomsich 	}
1609415b9a7SPhilipp Tomsich 
1619415b9a7SPhilipp Tomsich 	memset(cpuid_str, 0, sizeof(cpuid_str));
1629415b9a7SPhilipp Tomsich 	for (i = 0; i < 16; i++)
1639415b9a7SPhilipp Tomsich 		sprintf(&cpuid_str[i * 2], "%02x", cpuid[i]);
1649415b9a7SPhilipp Tomsich 
1659415b9a7SPhilipp Tomsich 	debug("cpuid: %s\n", cpuid_str);
1669415b9a7SPhilipp Tomsich 
1679415b9a7SPhilipp Tomsich 	/*
1689415b9a7SPhilipp Tomsich 	 * Mix the cpuid bytes using the same rules as in
1699415b9a7SPhilipp Tomsich 	 *   ${linux}/drivers/soc/rockchip/rockchip-cpuinfo.c
1709415b9a7SPhilipp Tomsich 	 */
1719415b9a7SPhilipp Tomsich 	for (i = 0; i < 8; i++) {
1729415b9a7SPhilipp Tomsich 		low[i] = cpuid[1 + (i << 1)];
1739415b9a7SPhilipp Tomsich 		high[i] = cpuid[i << 1];
1749415b9a7SPhilipp Tomsich 	}
1759415b9a7SPhilipp Tomsich 
1769415b9a7SPhilipp Tomsich 	serialno = crc32_no_comp(0, low, 8);
1779415b9a7SPhilipp Tomsich 	serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32;
178b32b1bd1SJakob Unterwurzacher 	snprintf(serialno_str, sizeof(serialno_str), "%016llx", serialno);
1799415b9a7SPhilipp Tomsich 
180382bee57SSimon Glass 	env_set("cpuid#", cpuid_str);
181382bee57SSimon Glass 	env_set("serial#", serialno_str);
1829415b9a7SPhilipp Tomsich #endif
1839415b9a7SPhilipp Tomsich }
1849415b9a7SPhilipp Tomsich 
185aa41220fSJakob Unterwurzacher static void setup_iodomain(void)
186aa41220fSJakob Unterwurzacher {
187aa41220fSJakob Unterwurzacher 	const u32 GRF_IO_VSEL_GPIO4CD_SHIFT = 3;
188aa41220fSJakob Unterwurzacher 	struct rk3399_grf_regs *grf =
189aa41220fSJakob Unterwurzacher 	    syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
190aa41220fSJakob Unterwurzacher 
191aa41220fSJakob Unterwurzacher 	/*
192aa41220fSJakob Unterwurzacher 	 * Set bit 3 in GRF_IO_VSEL so PCIE_RST# works (pin GPIO4_C6).
193aa41220fSJakob Unterwurzacher 	 * Linux assumes that PCIE_RST# works out of the box as it probes
194aa41220fSJakob Unterwurzacher 	 * PCIe before loading the iodomain driver.
195aa41220fSJakob Unterwurzacher 	 */
196aa41220fSJakob Unterwurzacher 	rk_setreg(&grf->io_vsel, 1 << GRF_IO_VSEL_GPIO4CD_SHIFT);
197aa41220fSJakob Unterwurzacher }
198aa41220fSJakob Unterwurzacher 
1999415b9a7SPhilipp Tomsich int misc_init_r(void)
2009415b9a7SPhilipp Tomsich {
2019415b9a7SPhilipp Tomsich 	setup_serial();
2028adc9d18SKlaus Goger 	setup_macaddr();
203aa41220fSJakob Unterwurzacher 	setup_iodomain();
2049415b9a7SPhilipp Tomsich 
2059415b9a7SPhilipp Tomsich 	return 0;
2069415b9a7SPhilipp Tomsich }
2079415b9a7SPhilipp Tomsich 
2089415b9a7SPhilipp Tomsich #ifdef CONFIG_SERIAL_TAG
2099415b9a7SPhilipp Tomsich void get_board_serial(struct tag_serialnr *serialnr)
2109415b9a7SPhilipp Tomsich {
2119415b9a7SPhilipp Tomsich 	char *serial_string;
2129415b9a7SPhilipp Tomsich 	u64 serial = 0;
2139415b9a7SPhilipp Tomsich 
21400caae6dSSimon Glass 	serial_string = env_get("serial#");
2159415b9a7SPhilipp Tomsich 
2169415b9a7SPhilipp Tomsich 	if (serial_string)
2179415b9a7SPhilipp Tomsich 		serial = simple_strtoull(serial_string, NULL, 16);
2189415b9a7SPhilipp Tomsich 
2199415b9a7SPhilipp Tomsich 	serialnr->high = (u32)(serial >> 32);
2209415b9a7SPhilipp Tomsich 	serialnr->low = (u32)(serial & 0xffffffff);
2219415b9a7SPhilipp Tomsich }
2229415b9a7SPhilipp Tomsich #endif
223614539d4SPhilipp Tomsich 
224614539d4SPhilipp Tomsich /**
225614539d4SPhilipp Tomsich  * Switch power at an external regulator (for our root hub).
226614539d4SPhilipp Tomsich  *
227614539d4SPhilipp Tomsich  * @param ctrl pointer to the xHCI controller
228614539d4SPhilipp Tomsich  * @param port port number as in the control message (one-based)
229614539d4SPhilipp Tomsich  * @param enable boolean indicating whether to enable or disable power
230614539d4SPhilipp Tomsich  * @return returns 0 on success, an error-code on failure
231614539d4SPhilipp Tomsich  */
232614539d4SPhilipp Tomsich static int board_usb_port_power_set(struct udevice *dev, int port,
233614539d4SPhilipp Tomsich 				    bool enable)
234614539d4SPhilipp Tomsich {
235614539d4SPhilipp Tomsich #if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_REGULATOR)
236614539d4SPhilipp Tomsich 	/* We start counting ports at 0, while USB counts from 1. */
237614539d4SPhilipp Tomsich 	int index = port - 1;
238614539d4SPhilipp Tomsich 	const char *regname = NULL;
239614539d4SPhilipp Tomsich 	struct udevice *regulator;
240614539d4SPhilipp Tomsich 	const char *prop = "tsd,usb-port-power";
241614539d4SPhilipp Tomsich 	int ret;
242614539d4SPhilipp Tomsich 
243614539d4SPhilipp Tomsich 	debug("%s: ctrl '%s' port %d enable %s\n", __func__,
244614539d4SPhilipp Tomsich 	      dev_read_name(dev), port, enable ? "true" : "false");
245614539d4SPhilipp Tomsich 
246614539d4SPhilipp Tomsich 	ret = dev_read_string_index(dev, prop, index, &regname);
247614539d4SPhilipp Tomsich 	if (ret < 0) {
248614539d4SPhilipp Tomsich 		debug("%s: ctrl '%s' port %d: no entry in '%s'\n",
249614539d4SPhilipp Tomsich 		      __func__, dev_read_name(dev), port, prop);
250614539d4SPhilipp Tomsich 		return ret;
251614539d4SPhilipp Tomsich 	}
252614539d4SPhilipp Tomsich 
253614539d4SPhilipp Tomsich 	ret = regulator_get_by_platname(regname, &regulator);
254614539d4SPhilipp Tomsich 	if (ret) {
255614539d4SPhilipp Tomsich 		debug("%s: ctrl '%s' port %d: could not get regulator '%s'\n",
256614539d4SPhilipp Tomsich 		      __func__, dev_read_name(dev), port, regname);
257614539d4SPhilipp Tomsich 		return ret;
258614539d4SPhilipp Tomsich 	}
259614539d4SPhilipp Tomsich 
260614539d4SPhilipp Tomsich 	regulator_set_enable(regulator, enable);
261614539d4SPhilipp Tomsich 	return 0;
262614539d4SPhilipp Tomsich #else
263614539d4SPhilipp Tomsich 	return -ENOTSUPP;
264614539d4SPhilipp Tomsich #endif
265614539d4SPhilipp Tomsich }
266614539d4SPhilipp Tomsich 
267614539d4SPhilipp Tomsich void usb_hub_reset_devices(struct usb_hub_device *hub, int port)
268614539d4SPhilipp Tomsich {
269614539d4SPhilipp Tomsich 	struct udevice *dev = hub->pusb_dev->dev;
270614539d4SPhilipp Tomsich 	struct udevice *ctrl;
271614539d4SPhilipp Tomsich 
272614539d4SPhilipp Tomsich 	/* We are only interested in our root-hubs */
273614539d4SPhilipp Tomsich 	if (usb_hub_is_root_hub(dev) == false)
274614539d4SPhilipp Tomsich 		return;
275614539d4SPhilipp Tomsich 
276614539d4SPhilipp Tomsich 	ctrl = usb_get_bus(dev);
277614539d4SPhilipp Tomsich 	if (!ctrl) {
278614539d4SPhilipp Tomsich 		debug("%s: could not retrieve ctrl for hub\n", __func__);
279614539d4SPhilipp Tomsich 		return;
280614539d4SPhilipp Tomsich 	}
281614539d4SPhilipp Tomsich 
282614539d4SPhilipp Tomsich 	/*
283614539d4SPhilipp Tomsich 	 * To work around an incompatibility between the single-threaded
284614539d4SPhilipp Tomsich 	 * USB stack in U-Boot and (a strange low-power mode of) the USB
285614539d4SPhilipp Tomsich 	 * hub we have on-module, we need to delay powering on the hub
286614539d4SPhilipp Tomsich 	 * until the first time the port is probed.
287614539d4SPhilipp Tomsich 	 */
288614539d4SPhilipp Tomsich 	board_usb_port_power_set(ctrl, port, true);
289614539d4SPhilipp Tomsich }
290