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