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