xref: /rk3399_rockchip-uboot/board/rockchip/evb_rk3399/evb-rk3399.c (revision 5ce558eee1d84a2b85f2bbc4c4547c8ea1c1dae4)
1 /*
2  * (C) Copyright 2016 Rockchip Electronics Co., Ltd
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6 #include <common.h>
7 #include <dm.h>
8 #include <misc.h>
9 #include <ram.h>
10 #include <dm/pinctrl.h>
11 #include <dm/uclass-internal.h>
12 #include <asm/setup.h>
13 #include <asm/arch/periph.h>
14 #include <power/regulator.h>
15 #include <u-boot/sha256.h>
16 #include <usb.h>
17 #include <dwc3-uboot.h>
18 
19 DECLARE_GLOBAL_DATA_PTR;
20 
21 #define RK3399_CPUID_OFF  0x7
22 #define RK3399_CPUID_LEN  0x10
23 
24 int board_init(void)
25 {
26 	struct udevice *pinctrl, *regulator;
27 	int ret;
28 
29 	/*
30 	 * The PWM does not have decicated interrupt number in dts and can
31 	 * not get periph_id by pinctrl framework, so let's init them here.
32 	 * The PWM2 and PWM3 are for pwm regulators.
33 	 */
34 	ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
35 	if (ret) {
36 		debug("%s: Cannot find pinctrl device\n", __func__);
37 		goto out;
38 	}
39 
40 	/* Enable pwm0 for panel backlight */
41 	ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_PWM0);
42 	if (ret) {
43 		debug("%s PWM0 pinctrl init fail! (ret=%d)\n", __func__, ret);
44 		goto out;
45 	}
46 
47 	ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_PWM2);
48 	if (ret) {
49 		debug("%s PWM2 pinctrl init fail!\n", __func__);
50 		goto out;
51 	}
52 
53 	ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_PWM3);
54 	if (ret) {
55 		debug("%s PWM3 pinctrl init fail!\n", __func__);
56 		goto out;
57 	}
58 
59 	ret = regulators_enable_boot_on(false);
60 	if (ret)
61 		debug("%s: Cannot enable boot on regulator\n", __func__);
62 
63 	ret = regulator_get_by_platname("vcc5v0_host", &regulator);
64 	if (ret) {
65 		debug("%s vcc5v0_host init fail! ret %d\n", __func__, ret);
66 		goto out;
67 	}
68 
69 	ret = regulator_set_enable(regulator, true);
70 	if (ret) {
71 		debug("%s vcc5v0-host-en set fail!\n", __func__);
72 		goto out;
73 	}
74 
75 out:
76 	return 0;
77 }
78 
79 static void setup_macaddr(void)
80 {
81 #if CONFIG_IS_ENABLED(CMD_NET)
82 	int ret;
83 	const char *cpuid = env_get("cpuid#");
84 	u8 hash[SHA256_SUM_LEN];
85 	int size = sizeof(hash);
86 	u8 mac_addr[6];
87 
88 	/* Only generate a MAC address, if none is set in the environment */
89 	if (env_get("ethaddr"))
90 		return;
91 
92 	if (!cpuid) {
93 		debug("%s: could not retrieve 'cpuid#'\n", __func__);
94 		return;
95 	}
96 
97 	ret = hash_block("sha256", (void *)cpuid, strlen(cpuid), hash, &size);
98 	if (ret) {
99 		debug("%s: failed to calculate SHA256\n", __func__);
100 		return;
101 	}
102 
103 	/* Copy 6 bytes of the hash to base the MAC address on */
104 	memcpy(mac_addr, hash, 6);
105 
106 	/* Make this a valid MAC address and set it */
107 	mac_addr[0] &= 0xfe;  /* clear multicast bit */
108 	mac_addr[0] |= 0x02;  /* set local assignment bit (IEEE802) */
109 	eth_env_set_enetaddr("ethaddr", mac_addr);
110 #endif
111 
112 	return;
113 }
114 
115 static void setup_serial(void)
116 {
117 #if CONFIG_IS_ENABLED(ROCKCHIP_EFUSE)
118 	struct udevice *dev;
119 	int ret, i;
120 	u8 cpuid[RK3399_CPUID_LEN];
121 	u8 low[RK3399_CPUID_LEN/2], high[RK3399_CPUID_LEN/2];
122 	char cpuid_str[RK3399_CPUID_LEN * 2 + 1];
123 	u64 serialno;
124 	char serialno_str[16];
125 
126 	/* retrieve the device */
127 	ret = uclass_get_device_by_driver(UCLASS_MISC,
128 					  DM_GET_DRIVER(rockchip_efuse), &dev);
129 	if (ret) {
130 		debug("%s: could not find efuse device\n", __func__);
131 		return;
132 	}
133 
134 	/* read the cpu_id range from the efuses */
135 	ret = misc_read(dev, RK3399_CPUID_OFF, &cpuid, sizeof(cpuid));
136 	if (ret) {
137 		debug("%s: reading cpuid from the efuses failed\n",
138 		      __func__);
139 		return;
140 	}
141 
142 	memset(cpuid_str, 0, sizeof(cpuid_str));
143 	for (i = 0; i < 16; i++)
144 		sprintf(&cpuid_str[i * 2], "%02x", cpuid[i]);
145 
146 	debug("cpuid: %s\n", cpuid_str);
147 
148 	/*
149 	 * Mix the cpuid bytes using the same rules as in
150 	 *   ${linux}/drivers/soc/rockchip/rockchip-cpuinfo.c
151 	 */
152 	for (i = 0; i < 8; i++) {
153 		low[i] = cpuid[1 + (i << 1)];
154 		high[i] = cpuid[i << 1];
155 	}
156 
157 	serialno = crc32_no_comp(0, low, 8);
158 	serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32;
159 	snprintf(serialno_str, sizeof(serialno_str), "%llx", serialno);
160 
161 	env_set("cpuid#", cpuid_str);
162 	env_set("serial#", serialno_str);
163 #endif
164 
165 	return;
166 }
167 
168 int misc_init_r(void)
169 {
170 	setup_serial();
171 	setup_macaddr();
172 
173 	return 0;
174 }
175 
176 #ifdef CONFIG_SERIAL_TAG
177 void get_board_serial(struct tag_serialnr *serialnr)
178 {
179 	char *serial_string;
180 	u64 serial = 0;
181 
182 	serial_string = env_get("serial#");
183 
184 	if (serial_string)
185 		serial = simple_strtoull(serial_string, NULL, 16);
186 
187 	serialnr->high = (u32)(serial >> 32);
188 	serialnr->low = (u32)(serial & 0xffffffff);
189 }
190 #endif
191 
192 #ifdef CONFIG_USB_DWC3
193 static struct dwc3_device dwc3_device_data = {
194 	.maximum_speed = USB_SPEED_HIGH,
195 	.base = 0xfe800000,
196 	.dr_mode = USB_DR_MODE_PERIPHERAL,
197 	.index = 0,
198 	.dis_u2_susphy_quirk = 1,
199 };
200 
201 int usb_gadget_handle_interrupts(void)
202 {
203 	dwc3_uboot_handle_interrupt(0);
204 	return 0;
205 }
206 
207 int board_usb_init(int index, enum usb_init_type init)
208 {
209 	return dwc3_uboot_init(&dwc3_device_data);
210 }
211 #endif
212