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