2 * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
4 * SPDX-License-Identifier: GPL-2.0+
10 #include <dm/pinctrl.h>
11 #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>
18 #define RK3399_CPUID_OFF 0x7
19 #define RK3399_CPUID_LEN 0x10
21 DECLARE_GLOBAL_DATA_PTR;
23 #define RK3399_CPUID_OFF 0x7
24 #define RK3399_CPUID_LEN 0x10
26 DECLARE_GLOBAL_DATA_PTR;
30 struct udevice *pinctrl, *regulator;
34 * The PWM does not have decicated interrupt number in dts and can
35 * not get periph_id by pinctrl framework, so let's init them here.
36 * The PWM2 and PWM3 are for pwm regulators.
38 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
40 debug("%s: Cannot find pinctrl device\n", __func__);
44 ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_PWM2);
46 debug("%s PWM2 pinctrl init fail!\n", __func__);
50 /* rk3399 need to init vdd_center to get the correct output voltage */
51 ret = regulator_get_by_platname("vdd_center", ®ulator);
53 debug("%s: Cannot get vdd_center regulator\n", __func__);
55 ret = regulator_get_by_platname("vcc5v0_host", ®ulator);
57 debug("%s vcc5v0_host init fail! ret %d\n", __func__, ret);
61 ret = regulator_set_enable(regulator, true);
63 debug("%s vcc5v0-host-en set fail!\n", __func__);
71 static void setup_macaddr(void)
73 #if CONFIG_IS_ENABLED(CMD_NET)
75 const char *cpuid = getenv("cpuid#");
76 u8 hash[SHA256_SUM_LEN];
77 int size = sizeof(hash);
80 /* Only generate a MAC address, if none is set in the environment */
81 if (getenv("ethaddr"))
85 debug("%s: could not retrieve 'cpuid#'\n", __func__);
89 ret = hash_block("sha256", (void *)cpuid, strlen(cpuid), hash, &size);
91 debug("%s: failed to calculate SHA256\n", __func__);
95 /* Copy 6 bytes of the hash to base the MAC address on */
96 memcpy(mac_addr, hash, 6);
98 /* Make this a valid MAC address and set it */
99 mac_addr[0] &= 0xfe; /* clear multicast bit */
100 mac_addr[0] |= 0x02; /* set local assignment bit (IEEE802) */
101 eth_setenv_enetaddr("ethaddr", mac_addr);
107 static void setup_serial(void)
109 #if CONFIG_IS_ENABLED(ROCKCHIP_EFUSE)
112 u8 cpuid[RK3399_CPUID_LEN];
113 u8 low[RK3399_CPUID_LEN/2], high[RK3399_CPUID_LEN/2];
114 char cpuid_str[RK3399_CPUID_LEN * 2 + 1];
116 char serialno_str[16];
118 /* retrieve the device */
119 ret = uclass_get_device_by_driver(UCLASS_MISC,
120 DM_GET_DRIVER(rockchip_efuse), &dev);
122 debug("%s: could not find efuse device\n", __func__);
126 /* read the cpu_id range from the efuses */
127 ret = misc_read(dev, RK3399_CPUID_OFF, &cpuid, sizeof(cpuid));
129 debug("%s: reading cpuid from the efuses failed\n",
134 memset(cpuid_str, 0, sizeof(cpuid_str));
135 for (i = 0; i < 16; i++)
136 sprintf(&cpuid_str[i * 2], "%02x", cpuid[i]);
138 debug("cpuid: %s\n", cpuid_str);
141 * Mix the cpuid bytes using the same rules as in
142 * ${linux}/drivers/soc/rockchip/rockchip-cpuinfo.c
144 for (i = 0; i < 8; i++) {
145 low[i] = cpuid[1 + (i << 1)];
146 high[i] = cpuid[i << 1];
149 serialno = crc32_no_comp(0, low, 8);
150 serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32;
151 snprintf(serialno_str, sizeof(serialno_str), "%llx", serialno);
153 setenv("cpuid#", cpuid_str);
154 setenv("serial#", serialno_str);
160 int misc_init_r(void)
168 #ifdef CONFIG_SERIAL_TAG
169 void get_board_serial(struct tag_serialnr *serialnr)
174 serial_string = getenv("serial#");
177 serial = simple_strtoull(serial_string, NULL, 16);
179 serialnr->high = (u32)(serial >> 32);
180 serialnr->low = (u32)(serial & 0xffffffff);