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>
12 #include <asm/setup.h>
13 #include <asm/arch/periph.h>
14 #include <power/regulator.h>
15 #include <u-boot/sha256.h>
17 DECLARE_GLOBAL_DATA_PTR;
21 struct udevice *pinctrl, *regulator;
25 * The PWM does not have decicated interrupt number in dts and can
26 * not get periph_id by pinctrl framework, so let's init them here.
27 * The PWM2 and PWM3 are for pwm regulators.
29 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
31 debug("%s: Cannot find pinctrl device\n", __func__);
35 ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_PWM2);
37 debug("%s PWM2 pinctrl init fail!\n", __func__);
41 /* rk3399 need to init vdd_center to get the correct output voltage */
42 ret = regulator_get_by_platname("vdd_center", ®ulator);
44 debug("%s: Cannot get vdd_center regulator\n", __func__);
46 ret = regulator_get_by_platname("vcc5v0_host", ®ulator);
48 debug("%s vcc5v0_host init fail! ret %d\n", __func__, ret);
52 ret = regulator_set_enable(regulator, true);
54 debug("%s vcc5v0-host-en set fail!\n", __func__);
62 static void setup_macaddr(void)
64 #if CONFIG_IS_ENABLED(CMD_NET)
66 const char *cpuid = env_get("cpuid#");
67 u8 hash[SHA256_SUM_LEN];
68 int size = sizeof(hash);
71 /* Only generate a MAC address, if none is set in the environment */
72 if (env_get("ethaddr"))
76 debug("%s: could not retrieve 'cpuid#'\n", __func__);
80 ret = hash_block("sha256", (void *)cpuid, strlen(cpuid), hash, &size);
82 debug("%s: failed to calculate SHA256\n", __func__);
86 /* Copy 6 bytes of the hash to base the MAC address on */
87 memcpy(mac_addr, hash, 6);
89 /* Make this a valid MAC address and set it */
90 mac_addr[0] &= 0xfe; /* clear multicast bit */
91 mac_addr[0] |= 0x02; /* set local assignment bit (IEEE802) */
92 eth_env_set_enetaddr("ethaddr", mac_addr);
98 static void setup_serial(void)
100 #if CONFIG_IS_ENABLED(ROCKCHIP_EFUSE)
101 const u32 cpuid_offset = 0x7;
102 const u32 cpuid_length = 0x10;
106 u8 cpuid[cpuid_length];
107 u8 low[cpuid_length/2], high[cpuid_length/2];
108 char cpuid_str[cpuid_length * 2 + 1];
110 char serialno_str[17];
112 /* retrieve the device */
113 ret = uclass_get_device_by_driver(UCLASS_MISC,
114 DM_GET_DRIVER(rockchip_efuse), &dev);
116 debug("%s: could not find efuse device\n", __func__);
120 /* read the cpu_id range from the efuses */
121 ret = misc_read(dev, cpuid_offset, &cpuid, sizeof(cpuid));
123 debug("%s: reading cpuid from the efuses failed\n",
128 memset(cpuid_str, 0, sizeof(cpuid_str));
129 for (i = 0; i < 16; i++)
130 sprintf(&cpuid_str[i * 2], "%02x", cpuid[i]);
132 debug("cpuid: %s\n", cpuid_str);
135 * Mix the cpuid bytes using the same rules as in
136 * ${linux}/drivers/soc/rockchip/rockchip-cpuinfo.c
138 for (i = 0; i < 8; i++) {
139 low[i] = cpuid[1 + (i << 1)];
140 high[i] = cpuid[i << 1];
143 serialno = crc32_no_comp(0, low, 8);
144 serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32;
145 snprintf(serialno_str, sizeof(serialno_str), "%llx", serialno);
147 env_set("cpuid#", cpuid_str);
148 env_set("serial#", serialno_str);
154 int misc_init_r(void)
162 #ifdef CONFIG_SERIAL_TAG
163 void get_board_serial(struct tag_serialnr *serialnr)
168 serial_string = env_get("serial#");
171 serial = simple_strtoull(serial_string, NULL, 16);
173 serialnr->high = (u32)(serial >> 32);
174 serialnr->low = (u32)(serial & 0xffffffff);