1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * RK3399: Architecture common definitions
5 * Copyright (C) 2019 Collabora Inc - https://www.collabora.com/
6 * Rohan Garg <rohan.garg@collabora.com>
8 * Based on puma-rk3399.c:
9 * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
15 #include <dm/uclass-internal.h>
17 #include <u-boot/crc.h>
18 #include <u-boot/sha256.h>
20 #include <asm/arch-rockchip/misc.h>
22 int rockchip_setup_macaddr(void)
24 #if CONFIG_IS_ENABLED(CMD_NET)
26 const char *cpuid = env_get("cpuid#");
27 u8 hash[SHA256_SUM_LEN];
28 int size = sizeof(hash);
31 /* Only generate a MAC address, if none is set in the environment */
32 if (env_get("ethaddr"))
36 debug("%s: could not retrieve 'cpuid#'\n", __func__);
40 ret = hash_block("sha256", (void *)cpuid, strlen(cpuid), hash, &size);
42 debug("%s: failed to calculate SHA256\n", __func__);
46 /* Copy 6 bytes of the hash to base the MAC address on */
47 memcpy(mac_addr, hash, 6);
49 /* Make this a valid MAC address and set it */
50 mac_addr[0] &= 0xfe; /* clear multicast bit */
51 mac_addr[0] |= 0x02; /* set local assignment bit (IEEE802) */
52 eth_env_set_enetaddr("ethaddr", mac_addr);
57 int rockchip_cpuid_from_efuse(const u32 cpuid_offset,
58 const u32 cpuid_length,
61 #if CONFIG_IS_ENABLED(ROCKCHIP_EFUSE) || CONFIG_IS_ENABLED(ROCKCHIP_OTP)
65 /* retrieve the device */
66 #if CONFIG_IS_ENABLED(ROCKCHIP_EFUSE)
67 ret = uclass_get_device_by_driver(UCLASS_MISC,
68 DM_GET_DRIVER(rockchip_efuse), &dev);
69 #elif CONFIG_IS_ENABLED(ROCKCHIP_OTP)
70 ret = uclass_get_device_by_driver(UCLASS_MISC,
71 DM_GET_DRIVER(rockchip_otp), &dev);
74 debug("%s: could not find efuse device\n", __func__);
78 /* read the cpu_id range from the efuses */
79 ret = misc_read(dev, cpuid_offset, cpuid, cpuid_length);
81 debug("%s: reading cpuid from the efuses failed\n",
89 int rockchip_cpuid_set(const u8 *cpuid, const u32 cpuid_length)
91 u8 low[cpuid_length / 2], high[cpuid_length / 2];
92 char cpuid_str[cpuid_length * 2 + 1];
94 char serialno_str[17];
97 memset(cpuid_str, 0, sizeof(cpuid_str));
98 for (i = 0; i < 16; i++)
99 sprintf(&cpuid_str[i * 2], "%02x", cpuid[i]);
101 debug("cpuid: %s\n", cpuid_str);
104 * Mix the cpuid bytes using the same rules as in
105 * ${linux}/drivers/soc/rockchip/rockchip-cpuinfo.c
107 for (i = 0; i < 8; i++) {
108 low[i] = cpuid[1 + (i << 1)];
109 high[i] = cpuid[i << 1];
112 serialno = crc32_no_comp(0, low, 8);
113 serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32;
114 snprintf(serialno_str, sizeof(serialno_str), "%016llx", serialno);
116 env_set("cpuid#", cpuid_str);
117 env_set("serial#", serialno_str);