xref: /openbmc/u-boot/board/theobroma-systems/puma_rk3399/puma-rk3399.c (revision fb7406469c012092d652741f103b0993103cf8e3)
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 <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 <spl.h>
16 #include <u-boot/sha256.h>
17 
18 DECLARE_GLOBAL_DATA_PTR;
19 
20 int board_init(void)
21 {
22 	struct udevice *pinctrl, *regulator;
23 	int ret;
24 
25 	/*
26 	 * The PWM does not have decicated interrupt number in dts and can
27 	 * not get periph_id by pinctrl framework, so let's init them here.
28 	 * The PWM2 and PWM3 are for pwm regulators.
29 	 */
30 	ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
31 	if (ret) {
32 		debug("%s: Cannot find pinctrl device\n", __func__);
33 		goto out;
34 	}
35 
36 	ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_PWM2);
37 	if (ret) {
38 		debug("%s PWM2 pinctrl init fail!\n", __func__);
39 		goto out;
40 	}
41 
42 	/* rk3399 need to init vdd_center to get the correct output voltage */
43 	ret = regulator_get_by_platname("vdd_center", &regulator);
44 	if (ret)
45 		debug("%s: Cannot get vdd_center regulator\n", __func__);
46 
47 	ret = regulator_get_by_platname("vcc5v0_host", &regulator);
48 	if (ret) {
49 		debug("%s vcc5v0_host init fail! ret %d\n", __func__, ret);
50 		goto out;
51 	}
52 
53 	ret = regulator_set_enable(regulator, true);
54 	if (ret) {
55 		debug("%s vcc5v0-host-en set fail!\n", __func__);
56 		goto out;
57 	}
58 
59 out:
60 	return 0;
61 }
62 
63 void spl_board_init(void)
64 {
65 	preloader_console_init();
66 }
67 
68 static void setup_macaddr(void)
69 {
70 #if CONFIG_IS_ENABLED(CMD_NET)
71 	int ret;
72 	const char *cpuid = env_get("cpuid#");
73 	u8 hash[SHA256_SUM_LEN];
74 	int size = sizeof(hash);
75 	u8 mac_addr[6];
76 
77 	/* Only generate a MAC address, if none is set in the environment */
78 	if (env_get("ethaddr"))
79 		return;
80 
81 	if (!cpuid) {
82 		debug("%s: could not retrieve 'cpuid#'\n", __func__);
83 		return;
84 	}
85 
86 	ret = hash_block("sha256", (void *)cpuid, strlen(cpuid), hash, &size);
87 	if (ret) {
88 		debug("%s: failed to calculate SHA256\n", __func__);
89 		return;
90 	}
91 
92 	/* Copy 6 bytes of the hash to base the MAC address on */
93 	memcpy(mac_addr, hash, 6);
94 
95 	/* Make this a valid MAC address and set it */
96 	mac_addr[0] &= 0xfe;  /* clear multicast bit */
97 	mac_addr[0] |= 0x02;  /* set local assignment bit (IEEE802) */
98 	eth_env_set_enetaddr("ethaddr", mac_addr);
99 #endif
100 }
101 
102 static void setup_serial(void)
103 {
104 #if CONFIG_IS_ENABLED(ROCKCHIP_EFUSE)
105 	const u32 cpuid_offset = 0x7;
106 	const u32 cpuid_length = 0x10;
107 
108 	struct udevice *dev;
109 	int ret, i;
110 	u8 cpuid[cpuid_length];
111 	u8 low[cpuid_length/2], high[cpuid_length/2];
112 	char cpuid_str[cpuid_length * 2 + 1];
113 	u64 serialno;
114 	char serialno_str[17];
115 
116 	/* retrieve the device */
117 	ret = uclass_get_device_by_driver(UCLASS_MISC,
118 					  DM_GET_DRIVER(rockchip_efuse), &dev);
119 	if (ret) {
120 		debug("%s: could not find efuse device\n", __func__);
121 		return;
122 	}
123 
124 	/* read the cpu_id range from the efuses */
125 	ret = misc_read(dev, cpuid_offset, &cpuid, sizeof(cpuid));
126 	if (ret) {
127 		debug("%s: reading cpuid from the efuses failed\n",
128 		      __func__);
129 		return;
130 	}
131 
132 	memset(cpuid_str, 0, sizeof(cpuid_str));
133 	for (i = 0; i < 16; i++)
134 		sprintf(&cpuid_str[i * 2], "%02x", cpuid[i]);
135 
136 	debug("cpuid: %s\n", cpuid_str);
137 
138 	/*
139 	 * Mix the cpuid bytes using the same rules as in
140 	 *   ${linux}/drivers/soc/rockchip/rockchip-cpuinfo.c
141 	 */
142 	for (i = 0; i < 8; i++) {
143 		low[i] = cpuid[1 + (i << 1)];
144 		high[i] = cpuid[i << 1];
145 	}
146 
147 	serialno = crc32_no_comp(0, low, 8);
148 	serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32;
149 	snprintf(serialno_str, sizeof(serialno_str), "%llx", serialno);
150 
151 	env_set("cpuid#", cpuid_str);
152 	env_set("serial#", serialno_str);
153 #endif
154 }
155 
156 int misc_init_r(void)
157 {
158 	setup_serial();
159 	setup_macaddr();
160 
161 	return 0;
162 }
163 
164 #ifdef CONFIG_SERIAL_TAG
165 void get_board_serial(struct tag_serialnr *serialnr)
166 {
167 	char *serial_string;
168 	u64 serial = 0;
169 
170 	serial_string = env_get("serial#");
171 
172 	if (serial_string)
173 		serial = simple_strtoull(serial_string, NULL, 16);
174 
175 	serialnr->high = (u32)(serial >> 32);
176 	serialnr->low = (u32)(serial & 0xffffffff);
177 }
178 #endif
179