blob: bed4317f7ecee173538f8f56a19ac06a55d95d6c [file] [log] [blame]
Rohan Garg04825382019-08-12 17:04:34 +02001/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * RK3399: Architecture common definitions
4 *
5 * Copyright (C) 2019 Collabora Inc - https://www.collabora.com/
6 * Rohan Garg <rohan.garg@collabora.com>
7 *
8 * Based on puma-rk3399.c:
9 * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
10 */
11
12#include <common.h>
13#include <env.h>
14#include <dm.h>
15#include <dm/uclass-internal.h>
16#include <misc.h>
17#include <u-boot/sha256.h>
18
19#include <asm/arch-rockchip/misc.h>
20
21int rockchip_setup_macaddr(void)
22{
23#if CONFIG_IS_ENABLED(CMD_NET)
24 int ret;
25 const char *cpuid = env_get("cpuid#");
26 u8 hash[SHA256_SUM_LEN];
27 int size = sizeof(hash);
28 u8 mac_addr[6];
29
30 /* Only generate a MAC address, if none is set in the environment */
31 if (env_get("ethaddr"))
32 return -1;
33
34 if (!cpuid) {
35 debug("%s: could not retrieve 'cpuid#'\n", __func__);
36 return -1;
37 }
38
39 ret = hash_block("sha256", (void *)cpuid, strlen(cpuid), hash, &size);
40 if (ret) {
41 debug("%s: failed to calculate SHA256\n", __func__);
42 return -1;
43 }
44
45 /* Copy 6 bytes of the hash to base the MAC address on */
46 memcpy(mac_addr, hash, 6);
47
48 /* Make this a valid MAC address and set it */
49 mac_addr[0] &= 0xfe; /* clear multicast bit */
50 mac_addr[0] |= 0x02; /* set local assignment bit (IEEE802) */
51 eth_env_set_enetaddr("ethaddr", mac_addr);
52#endif
53 return 0;
54}
55
56int rockchip_cpuid_from_efuse(const u32 cpuid_offset,
57 const u32 cpuid_length,
58 u8 *cpuid)
59{
Heiko Stuebnere61350a2019-09-25 20:21:21 +020060#if CONFIG_IS_ENABLED(ROCKCHIP_EFUSE) || CONFIG_IS_ENABLED(ROCKCHIP_OTP)
Rohan Garg04825382019-08-12 17:04:34 +020061 struct udevice *dev;
62 int ret;
63
64 /* retrieve the device */
Heiko Stuebnere61350a2019-09-25 20:21:21 +020065#if CONFIG_IS_ENABLED(ROCKCHIP_EFUSE)
Rohan Garg04825382019-08-12 17:04:34 +020066 ret = uclass_get_device_by_driver(UCLASS_MISC,
67 DM_GET_DRIVER(rockchip_efuse), &dev);
Heiko Stuebnere61350a2019-09-25 20:21:21 +020068#elif CONFIG_IS_ENABLED(ROCKCHIP_OTP)
69 ret = uclass_get_device_by_driver(UCLASS_MISC,
70 DM_GET_DRIVER(rockchip_otp), &dev);
71#endif
Rohan Garg04825382019-08-12 17:04:34 +020072 if (ret) {
73 debug("%s: could not find efuse device\n", __func__);
74 return -1;
75 }
76
77 /* read the cpu_id range from the efuses */
Heiko Stuebner03f98b72019-09-25 20:40:56 +020078 ret = misc_read(dev, cpuid_offset, cpuid, cpuid_length);
Rohan Garg04825382019-08-12 17:04:34 +020079 if (ret) {
80 debug("%s: reading cpuid from the efuses failed\n",
81 __func__);
82 return -1;
83 }
84#endif
85 return 0;
86}
87
88int rockchip_cpuid_set(const u8 *cpuid, const u32 cpuid_length)
89{
90 u8 low[cpuid_length / 2], high[cpuid_length / 2];
91 char cpuid_str[cpuid_length * 2 + 1];
92 u64 serialno;
93 char serialno_str[17];
94 int i;
95
96 memset(cpuid_str, 0, sizeof(cpuid_str));
97 for (i = 0; i < 16; i++)
98 sprintf(&cpuid_str[i * 2], "%02x", cpuid[i]);
99
100 debug("cpuid: %s\n", cpuid_str);
101
102 /*
103 * Mix the cpuid bytes using the same rules as in
104 * ${linux}/drivers/soc/rockchip/rockchip-cpuinfo.c
105 */
106 for (i = 0; i < 8; i++) {
107 low[i] = cpuid[1 + (i << 1)];
108 high[i] = cpuid[i << 1];
109 }
110
111 serialno = crc32_no_comp(0, low, 8);
112 serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32;
113 snprintf(serialno_str, sizeof(serialno_str), "%016llx", serialno);
114
115 env_set("cpuid#", cpuid_str);
116 env_set("serial#", serialno_str);
117
118 return 0;
119}