1a13110a9SKlaus Goger /*
2a13110a9SKlaus Goger  * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
3a13110a9SKlaus Goger  *
4a13110a9SKlaus Goger  * SPDX-License-Identifier:     GPL-2.0+
5a13110a9SKlaus Goger  */
6fb740646SPhilipp Tomsich 
7a13110a9SKlaus Goger #include <common.h>
8a13110a9SKlaus Goger #include <dm.h>
9e92e5803SPhilipp Tomsich #include <misc.h>
10614539d4SPhilipp Tomsich #include <spl.h>
11614539d4SPhilipp Tomsich #include <usb.h>
12a13110a9SKlaus Goger #include <dm/pinctrl.h>
13a13110a9SKlaus Goger #include <dm/uclass-internal.h>
14ae0d33a7SPhilipp Tomsich #include <asm/gpio.h>
159415b9a7SPhilipp Tomsich #include <asm/setup.h>
16ae0d33a7SPhilipp Tomsich #include <asm/arch/clock.h>
17ae0d33a7SPhilipp Tomsich #include <asm/arch/cru_rk3399.h>
18a13110a9SKlaus Goger #include <asm/arch/periph.h>
19a13110a9SKlaus Goger #include <power/regulator.h>
20e92e5803SPhilipp Tomsich #include <u-boot/sha256.h>
21e92e5803SPhilipp Tomsich 
22a13110a9SKlaus Goger DECLARE_GLOBAL_DATA_PTR;
23a13110a9SKlaus Goger 
24a13110a9SKlaus Goger int board_init(void)
25a13110a9SKlaus Goger {
26a13110a9SKlaus Goger 	int ret;
27a13110a9SKlaus Goger 
28a13110a9SKlaus Goger 	/*
290b5e7aabSPhilipp Tomsich 	 * We need to call into regulators_enable_boot_on() again, as the call
300b5e7aabSPhilipp Tomsich 	 * during SPL may have not included all regulators.
31a13110a9SKlaus Goger 	 */
320b5e7aabSPhilipp Tomsich 	ret = regulators_enable_boot_on(false);
33a13110a9SKlaus Goger 	if (ret)
340b5e7aabSPhilipp Tomsich 		debug("%s: Cannot enable boot on regulator\n", __func__);
35a13110a9SKlaus Goger 
36a13110a9SKlaus Goger 	return 0;
37a13110a9SKlaus Goger }
38a13110a9SKlaus Goger 
39ae0d33a7SPhilipp Tomsich static void rk3399_force_power_on_reset(void)
40ae0d33a7SPhilipp Tomsich {
41ae0d33a7SPhilipp Tomsich 	ofnode node;
42ae0d33a7SPhilipp Tomsich 	struct gpio_desc sysreset_gpio;
43ae0d33a7SPhilipp Tomsich 
44ae0d33a7SPhilipp Tomsich 	debug("%s: trying to force a power-on reset\n", __func__);
45ae0d33a7SPhilipp Tomsich 
46ae0d33a7SPhilipp Tomsich 	node = ofnode_path("/config");
47ae0d33a7SPhilipp Tomsich 	if (!ofnode_valid(node)) {
48ae0d33a7SPhilipp Tomsich 		debug("%s: no /config node?\n", __func__);
49ae0d33a7SPhilipp Tomsich 		return;
50ae0d33a7SPhilipp Tomsich 	}
51ae0d33a7SPhilipp Tomsich 
52ae0d33a7SPhilipp Tomsich 	if (gpio_request_by_name_nodev(node, "sysreset-gpio", 0,
53ae0d33a7SPhilipp Tomsich 				       &sysreset_gpio, GPIOD_IS_OUT)) {
54ae0d33a7SPhilipp Tomsich 		debug("%s: could not find a /config/sysreset-gpio\n", __func__);
55ae0d33a7SPhilipp Tomsich 		return;
56ae0d33a7SPhilipp Tomsich 	}
57ae0d33a7SPhilipp Tomsich 
58ae0d33a7SPhilipp Tomsich 	dm_gpio_set_value(&sysreset_gpio, 1);
59ae0d33a7SPhilipp Tomsich }
60ae0d33a7SPhilipp Tomsich 
61fb740646SPhilipp Tomsich void spl_board_init(void)
62fb740646SPhilipp Tomsich {
63482cf223SPhilipp Tomsich 	int  ret;
64ae0d33a7SPhilipp Tomsich 	struct rk3399_cru *cru = rockchip_get_cru();
65ae0d33a7SPhilipp Tomsich 
66ae0d33a7SPhilipp Tomsich 	/*
67ae0d33a7SPhilipp Tomsich 	 * The RK3399 resets only 'almost all logic' (see also in the TRM
68ae0d33a7SPhilipp Tomsich 	 * "3.9.4 Global software reset"), when issuing a software reset.
69ae0d33a7SPhilipp Tomsich 	 * This may cause issues during boot-up for some configurations of
70ae0d33a7SPhilipp Tomsich 	 * the application software stack.
71ae0d33a7SPhilipp Tomsich 	 *
72ae0d33a7SPhilipp Tomsich 	 * To work around this, we test whether the last reset reason was
73ae0d33a7SPhilipp Tomsich 	 * a power-on reset and (if not) issue an overtemp-reset to reset
74ae0d33a7SPhilipp Tomsich 	 * the entire module.
75ae0d33a7SPhilipp Tomsich 	 *
76ae0d33a7SPhilipp Tomsich 	 * While this was previously fixed by modifying the various places
77ae0d33a7SPhilipp Tomsich 	 * that could generate a software reset (e.g. U-Boot's sysreset
78ae0d33a7SPhilipp Tomsich 	 * driver, the ATF or Linux), we now have it here to ensure that
79ae0d33a7SPhilipp Tomsich 	 * we no longer have to track this through the various components.
80ae0d33a7SPhilipp Tomsich 	 */
81ae0d33a7SPhilipp Tomsich 	if (cru->glb_rst_st != 0)
82ae0d33a7SPhilipp Tomsich 		rk3399_force_power_on_reset();
83482cf223SPhilipp Tomsich 
84482cf223SPhilipp Tomsich 	/*
85482cf223SPhilipp Tomsich 	 * Turning the eMMC and SPI back on (if disabled via the Qseven
86482cf223SPhilipp Tomsich 	 * BIOS_ENABLE) signal is done through a always-on regulator).
87482cf223SPhilipp Tomsich 	 */
88482cf223SPhilipp Tomsich 	ret = regulators_enable_boot_on(false);
89482cf223SPhilipp Tomsich 	if (ret)
90482cf223SPhilipp Tomsich 		debug("%s: Cannot enable boot on regulator\n", __func__);
91482cf223SPhilipp Tomsich 
92fb740646SPhilipp Tomsich 	preloader_console_init();
93fb740646SPhilipp Tomsich }
94fb740646SPhilipp Tomsich 
958adc9d18SKlaus Goger static void setup_macaddr(void)
968adc9d18SKlaus Goger {
978adc9d18SKlaus Goger #if CONFIG_IS_ENABLED(CMD_NET)
988adc9d18SKlaus Goger 	int ret;
9900caae6dSSimon Glass 	const char *cpuid = env_get("cpuid#");
1008adc9d18SKlaus Goger 	u8 hash[SHA256_SUM_LEN];
1018adc9d18SKlaus Goger 	int size = sizeof(hash);
1028adc9d18SKlaus Goger 	u8 mac_addr[6];
1038adc9d18SKlaus Goger 
1048adc9d18SKlaus Goger 	/* Only generate a MAC address, if none is set in the environment */
10500caae6dSSimon Glass 	if (env_get("ethaddr"))
1068adc9d18SKlaus Goger 		return;
1078adc9d18SKlaus Goger 
1088adc9d18SKlaus Goger 	if (!cpuid) {
1098adc9d18SKlaus Goger 		debug("%s: could not retrieve 'cpuid#'\n", __func__);
1108adc9d18SKlaus Goger 		return;
1118adc9d18SKlaus Goger 	}
1128adc9d18SKlaus Goger 
1138adc9d18SKlaus Goger 	ret = hash_block("sha256", (void *)cpuid, strlen(cpuid), hash, &size);
1148adc9d18SKlaus Goger 	if (ret) {
1158adc9d18SKlaus Goger 		debug("%s: failed to calculate SHA256\n", __func__);
1168adc9d18SKlaus Goger 		return;
1178adc9d18SKlaus Goger 	}
1188adc9d18SKlaus Goger 
1198adc9d18SKlaus Goger 	/* Copy 6 bytes of the hash to base the MAC address on */
1208adc9d18SKlaus Goger 	memcpy(mac_addr, hash, 6);
1218adc9d18SKlaus Goger 
1228adc9d18SKlaus Goger 	/* Make this a valid MAC address and set it */
1238adc9d18SKlaus Goger 	mac_addr[0] &= 0xfe;  /* clear multicast bit */
1248adc9d18SKlaus Goger 	mac_addr[0] |= 0x02;  /* set local assignment bit (IEEE802) */
125fd1e959eSSimon Glass 	eth_env_set_enetaddr("ethaddr", mac_addr);
1268adc9d18SKlaus Goger #endif
1278adc9d18SKlaus Goger }
1288adc9d18SKlaus Goger 
1299415b9a7SPhilipp Tomsich static void setup_serial(void)
1309415b9a7SPhilipp Tomsich {
1319415b9a7SPhilipp Tomsich #if CONFIG_IS_ENABLED(ROCKCHIP_EFUSE)
13226722335SKever Yang 	const u32 cpuid_offset = 0x7;
13326722335SKever Yang 	const u32 cpuid_length = 0x10;
13426722335SKever Yang 
1359415b9a7SPhilipp Tomsich 	struct udevice *dev;
1369415b9a7SPhilipp Tomsich 	int ret, i;
13726722335SKever Yang 	u8 cpuid[cpuid_length];
13826722335SKever Yang 	u8 low[cpuid_length/2], high[cpuid_length/2];
13926722335SKever Yang 	char cpuid_str[cpuid_length * 2 + 1];
1409415b9a7SPhilipp Tomsich 	u64 serialno;
14160d7c509SKlaus Goger 	char serialno_str[17];
1429415b9a7SPhilipp Tomsich 
1438adc9d18SKlaus Goger 	/* retrieve the device */
1448adc9d18SKlaus Goger 	ret = uclass_get_device_by_driver(UCLASS_MISC,
1458adc9d18SKlaus Goger 					  DM_GET_DRIVER(rockchip_efuse), &dev);
1469415b9a7SPhilipp Tomsich 	if (ret) {
1479415b9a7SPhilipp Tomsich 		debug("%s: could not find efuse device\n", __func__);
1489415b9a7SPhilipp Tomsich 		return;
1499415b9a7SPhilipp Tomsich 	}
1509415b9a7SPhilipp Tomsich 
1519415b9a7SPhilipp Tomsich 	/* read the cpu_id range from the efuses */
15226722335SKever Yang 	ret = misc_read(dev, cpuid_offset, &cpuid, sizeof(cpuid));
1539415b9a7SPhilipp Tomsich 	if (ret) {
1549415b9a7SPhilipp Tomsich 		debug("%s: reading cpuid from the efuses failed\n",
1559415b9a7SPhilipp Tomsich 		      __func__);
1569415b9a7SPhilipp Tomsich 		return;
1579415b9a7SPhilipp Tomsich 	}
1589415b9a7SPhilipp Tomsich 
1599415b9a7SPhilipp Tomsich 	memset(cpuid_str, 0, sizeof(cpuid_str));
1609415b9a7SPhilipp Tomsich 	for (i = 0; i < 16; i++)
1619415b9a7SPhilipp Tomsich 		sprintf(&cpuid_str[i * 2], "%02x", cpuid[i]);
1629415b9a7SPhilipp Tomsich 
1639415b9a7SPhilipp Tomsich 	debug("cpuid: %s\n", cpuid_str);
1649415b9a7SPhilipp Tomsich 
1659415b9a7SPhilipp Tomsich 	/*
1669415b9a7SPhilipp Tomsich 	 * Mix the cpuid bytes using the same rules as in
1679415b9a7SPhilipp Tomsich 	 *   ${linux}/drivers/soc/rockchip/rockchip-cpuinfo.c
1689415b9a7SPhilipp Tomsich 	 */
1699415b9a7SPhilipp Tomsich 	for (i = 0; i < 8; i++) {
1709415b9a7SPhilipp Tomsich 		low[i] = cpuid[1 + (i << 1)];
1719415b9a7SPhilipp Tomsich 		high[i] = cpuid[i << 1];
1729415b9a7SPhilipp Tomsich 	}
1739415b9a7SPhilipp Tomsich 
1749415b9a7SPhilipp Tomsich 	serialno = crc32_no_comp(0, low, 8);
1759415b9a7SPhilipp Tomsich 	serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32;
176*b32b1bd1SJakob Unterwurzacher 	snprintf(serialno_str, sizeof(serialno_str), "%016llx", serialno);
1779415b9a7SPhilipp Tomsich 
178382bee57SSimon Glass 	env_set("cpuid#", cpuid_str);
179382bee57SSimon Glass 	env_set("serial#", serialno_str);
1809415b9a7SPhilipp Tomsich #endif
1819415b9a7SPhilipp Tomsich }
1829415b9a7SPhilipp Tomsich 
1839415b9a7SPhilipp Tomsich int misc_init_r(void)
1849415b9a7SPhilipp Tomsich {
1859415b9a7SPhilipp Tomsich 	setup_serial();
1868adc9d18SKlaus Goger 	setup_macaddr();
1879415b9a7SPhilipp Tomsich 
1889415b9a7SPhilipp Tomsich 	return 0;
1899415b9a7SPhilipp Tomsich }
1909415b9a7SPhilipp Tomsich 
1919415b9a7SPhilipp Tomsich #ifdef CONFIG_SERIAL_TAG
1929415b9a7SPhilipp Tomsich void get_board_serial(struct tag_serialnr *serialnr)
1939415b9a7SPhilipp Tomsich {
1949415b9a7SPhilipp Tomsich 	char *serial_string;
1959415b9a7SPhilipp Tomsich 	u64 serial = 0;
1969415b9a7SPhilipp Tomsich 
19700caae6dSSimon Glass 	serial_string = env_get("serial#");
1989415b9a7SPhilipp Tomsich 
1999415b9a7SPhilipp Tomsich 	if (serial_string)
2009415b9a7SPhilipp Tomsich 		serial = simple_strtoull(serial_string, NULL, 16);
2019415b9a7SPhilipp Tomsich 
2029415b9a7SPhilipp Tomsich 	serialnr->high = (u32)(serial >> 32);
2039415b9a7SPhilipp Tomsich 	serialnr->low = (u32)(serial & 0xffffffff);
2049415b9a7SPhilipp Tomsich }
2059415b9a7SPhilipp Tomsich #endif
206614539d4SPhilipp Tomsich 
207614539d4SPhilipp Tomsich /**
208614539d4SPhilipp Tomsich  * Switch power at an external regulator (for our root hub).
209614539d4SPhilipp Tomsich  *
210614539d4SPhilipp Tomsich  * @param ctrl pointer to the xHCI controller
211614539d4SPhilipp Tomsich  * @param port port number as in the control message (one-based)
212614539d4SPhilipp Tomsich  * @param enable boolean indicating whether to enable or disable power
213614539d4SPhilipp Tomsich  * @return returns 0 on success, an error-code on failure
214614539d4SPhilipp Tomsich  */
215614539d4SPhilipp Tomsich static int board_usb_port_power_set(struct udevice *dev, int port,
216614539d4SPhilipp Tomsich 				    bool enable)
217614539d4SPhilipp Tomsich {
218614539d4SPhilipp Tomsich #if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_REGULATOR)
219614539d4SPhilipp Tomsich 	/* We start counting ports at 0, while USB counts from 1. */
220614539d4SPhilipp Tomsich 	int index = port - 1;
221614539d4SPhilipp Tomsich 	const char *regname = NULL;
222614539d4SPhilipp Tomsich 	struct udevice *regulator;
223614539d4SPhilipp Tomsich 	const char *prop = "tsd,usb-port-power";
224614539d4SPhilipp Tomsich 	int ret;
225614539d4SPhilipp Tomsich 
226614539d4SPhilipp Tomsich 	debug("%s: ctrl '%s' port %d enable %s\n", __func__,
227614539d4SPhilipp Tomsich 	      dev_read_name(dev), port, enable ? "true" : "false");
228614539d4SPhilipp Tomsich 
229614539d4SPhilipp Tomsich 	ret = dev_read_string_index(dev, prop, index, &regname);
230614539d4SPhilipp Tomsich 	if (ret < 0) {
231614539d4SPhilipp Tomsich 		debug("%s: ctrl '%s' port %d: no entry in '%s'\n",
232614539d4SPhilipp Tomsich 		      __func__, dev_read_name(dev), port, prop);
233614539d4SPhilipp Tomsich 		return ret;
234614539d4SPhilipp Tomsich 	}
235614539d4SPhilipp Tomsich 
236614539d4SPhilipp Tomsich 	ret = regulator_get_by_platname(regname, &regulator);
237614539d4SPhilipp Tomsich 	if (ret) {
238614539d4SPhilipp Tomsich 		debug("%s: ctrl '%s' port %d: could not get regulator '%s'\n",
239614539d4SPhilipp Tomsich 		      __func__, dev_read_name(dev), port, regname);
240614539d4SPhilipp Tomsich 		return ret;
241614539d4SPhilipp Tomsich 	}
242614539d4SPhilipp Tomsich 
243614539d4SPhilipp Tomsich 	regulator_set_enable(regulator, enable);
244614539d4SPhilipp Tomsich 	return 0;
245614539d4SPhilipp Tomsich #else
246614539d4SPhilipp Tomsich 	return -ENOTSUPP;
247614539d4SPhilipp Tomsich #endif
248614539d4SPhilipp Tomsich }
249614539d4SPhilipp Tomsich 
250614539d4SPhilipp Tomsich void usb_hub_reset_devices(struct usb_hub_device *hub, int port)
251614539d4SPhilipp Tomsich {
252614539d4SPhilipp Tomsich 	struct udevice *dev = hub->pusb_dev->dev;
253614539d4SPhilipp Tomsich 	struct udevice *ctrl;
254614539d4SPhilipp Tomsich 
255614539d4SPhilipp Tomsich 	/* We are only interested in our root-hubs */
256614539d4SPhilipp Tomsich 	if (usb_hub_is_root_hub(dev) == false)
257614539d4SPhilipp Tomsich 		return;
258614539d4SPhilipp Tomsich 
259614539d4SPhilipp Tomsich 	ctrl = usb_get_bus(dev);
260614539d4SPhilipp Tomsich 	if (!ctrl) {
261614539d4SPhilipp Tomsich 		debug("%s: could not retrieve ctrl for hub\n", __func__);
262614539d4SPhilipp Tomsich 		return;
263614539d4SPhilipp Tomsich 	}
264614539d4SPhilipp Tomsich 
265614539d4SPhilipp Tomsich 	/*
266614539d4SPhilipp Tomsich 	 * To work around an incompatibility between the single-threaded
267614539d4SPhilipp Tomsich 	 * USB stack in U-Boot and (a strange low-power mode of) the USB
268614539d4SPhilipp Tomsich 	 * hub we have on-module, we need to delay powering on the hub
269614539d4SPhilipp Tomsich 	 * until the first time the port is probed.
270614539d4SPhilipp Tomsich 	 */
271614539d4SPhilipp Tomsich 	board_usb_port_power_set(ctrl, port, true);
272614539d4SPhilipp Tomsich }
273