Skip to content

Commit

Permalink
rockchip: rk3588: derive ethaddr from cpuid
Browse files Browse the repository at this point in the history
This is based on upstream u-boot commit 0482538
(rockchip: rk3399: derive ethaddr from cpuid).

Signed-off-by: Stephen Chen <stephen@radxa.com>
  • Loading branch information
RadxaStephen committed Oct 7, 2023
1 parent 609a77e commit 2e80b28
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 0 deletions.
14 changes: 14 additions & 0 deletions arch/arm/include/asm/arch-rockchip/misc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* RK3399: Architecture common definitions
*
* Copyright (C) 2019 Collabora Inc - https://www.collabora.com/
* Rohan Garg <rohan.garg@collabora.com>
*/

int rockchip_cpuid_from_efuse(const u32 cpuid_offset,
const u32 cpuid_length,
u8 *cpuid);
int rockchip_cpuid_set(const u8 *cpuid, const u32 cpuid_length);
int rockchip_setup_macaddr(void);
void rockchip_capsule_update_board_setup(void);
1 change: 1 addition & 0 deletions arch/arm/mach-rockchip/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ obj-$(CONFIG_FPGA_ROCKCHIP) += fpga.o
obj-$(CONFIG_RAM) += param.o
obj-$(CONFIG_$(SPL_TPL_)RAM) += sdram.o
obj-$(CONFIG_SPL_KERNEL_BOOT) += spl_resource_img.o
obj-$(CONFIG_MISC_INIT_R) += misc.o

obj-$(CONFIG_ROCKCHIP_PX30) += px30/
obj-$(CONFIG_ROCKCHIP_RK3036) += rk3036/
Expand Down
19 changes: 19 additions & 0 deletions arch/arm/mach-rockchip/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include <asm/arch/resource_img.h>
#include <asm/arch/rk_atags.h>
#include <asm/arch/vendor.h>
#include <asm/arch-rockchip/misc.h>
#ifdef CONFIG_ROCKCHIP_EINK_DISPLAY
#include <rk_eink.h>
#endif
Expand Down Expand Up @@ -1286,3 +1287,21 @@ int ft_verify_fdt(void *fdt)
return 1;
}

#ifdef CONFIG_MISC_INIT_R
__weak int misc_init_r(void)
{
const u32 cpuid_offset = CFG_CPUID_OFFSET;
const u32 cpuid_length = 0x10;
u8 cpuid[cpuid_length];
int ret;

ret = rockchip_cpuid_from_efuse(cpuid_offset, cpuid_length, cpuid);
if (ret)
return ret;
ret = rockchip_cpuid_set(cpuid, cpuid_length);
if (ret)
return ret;
ret = rockchip_setup_macaddr();
return ret;
}
#endif
134 changes: 134 additions & 0 deletions arch/arm/mach-rockchip/misc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* RK3399: Architecture common definitions
*
* Copyright (C) 2019 Collabora Inc - https://www.collabora.com/
* Rohan Garg <rohan.garg@collabora.com>
*
* Based on puma-rk3399.c:
* (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
*/

#include <common.h>
#include <dm.h>
#include <hash.h>
#include <log.h>
#include <dm/uclass-internal.h>
#include <misc.h>
#include <u-boot/crc.h>
#include <u-boot/sha256.h>

#include <asm/arch-rockchip/misc.h>

int rockchip_setup_macaddr(void)
{
#if CONFIG_IS_ENABLED(HASH) && CONFIG_IS_ENABLED(SHA256)
int ret;
const char *cpuid = env_get("cpuid#");
u8 hash[SHA256_SUM_LEN];
int size = sizeof(hash);
u8 mac_addr[6];

/* Only generate a MAC address, if none is set in the environment */
if (env_get("ethaddr"))
return 0;

if (!cpuid) {
debug("%s: could not retrieve 'cpuid#'\n", __func__);
return -1;
}

ret = hash_block("sha256", (void *)cpuid, strlen(cpuid), hash, &size);
if (ret) {
debug("%s: failed to calculate SHA256\n", __func__);
return -1;
}

/* Copy 6 bytes of the hash to base the MAC address on */
memcpy(mac_addr, hash, 6);

/* Make this a valid MAC address and set it */
mac_addr[0] &= 0xfe; /* clear multicast bit */
mac_addr[0] |= 0x02; /* set local assignment bit (IEEE802) */
eth_env_set_enetaddr("ethaddr", mac_addr);

/* Make a valid MAC address for ethernet1 */
mac_addr[5] ^= 0x01;
eth_env_set_enetaddr("eth1addr", mac_addr);
#endif
return 0;
}

int rockchip_cpuid_from_efuse(const u32 cpuid_offset,
const u32 cpuid_length,
u8 *cpuid)
{
#if IS_ENABLED(CONFIG_ROCKCHIP_EFUSE) || IS_ENABLED(CONFIG_ROCKCHIP_OTP)
struct udevice *dev;
int ret;

/* retrieve the device */
#if IS_ENABLED(CONFIG_ROCKCHIP_EFUSE)
ret = uclass_get_device_by_driver(UCLASS_MISC,
DM_GET_DRIVER(rockchip_efuse), &dev);
#elif IS_ENABLED(CONFIG_ROCKCHIP_OTP)
ret = uclass_get_device_by_driver(UCLASS_MISC,
DM_GET_DRIVER(rockchip_otp), &dev);
#endif
if (ret) {
debug("%s: could not find efuse device\n", __func__);
return -1;
}

/* read the cpu_id range from the efuses */
ret = misc_read(dev, cpuid_offset, cpuid, cpuid_length);
if (ret < 0) {
debug("%s: reading cpuid from the efuses failed\n",
__func__);
return -1;
}
#endif
return 0;
}

int rockchip_cpuid_set(const u8 *cpuid, const u32 cpuid_length)
{
u8 low[cpuid_length / 2], high[cpuid_length / 2];
char cpuid_str[cpuid_length * 2 + 1];
u64 serialno;
char serialno_str[17];
const char *oldid;
int i;

memset(cpuid_str, 0, sizeof(cpuid_str));
for (i = 0; i < 16; i++)
sprintf(&cpuid_str[i * 2], "%02x", cpuid[i]);

debug("cpuid: %s\n", cpuid_str);

/*
* Mix the cpuid bytes using the same rules as in
* ${linux}/drivers/soc/rockchip/rockchip-cpuinfo.c
*/
for (i = 0; i < 8; i++) {
low[i] = cpuid[1 + (i << 1)];
high[i] = cpuid[i << 1];
}

serialno = crc32_no_comp(0, low, 8);
serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32;
snprintf(serialno_str, sizeof(serialno_str), "%016llx", serialno);

oldid = env_get("cpuid#");
if (oldid && strcmp(oldid, cpuid_str) != 0)
printf("cpuid: value %s present in env does not match hardware %s\n",
oldid, cpuid_str);

env_set("cpuid#", cpuid_str);

/* Only generate serial# when none is set yet */
if (!env_get("serial#"))
env_set("serial#", serialno_str);

return 0;
}
2 changes: 2 additions & 0 deletions include/configs/rk3568_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#ifndef __CONFIG_RK3568_COMMON_H
#define __CONFIG_RK3568_COMMON_H

#define CFG_CPUID_OFFSET 0xa

#include "rockchip-common.h"

#define CONFIG_SPL_FRAMEWORK
Expand Down
2 changes: 2 additions & 0 deletions include/configs/rk3588_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
#define CONFIG_USB_FUNCTION_MASS_STORAGE
#define CONFIG_ROCKUSB_G_DNL_PID 0x350b

#define CONFIG_MISC_INIT_R

/*
* decompressed kernel: 4M ~ 84M
* Why not start from 2M ? if kernel < 5.10 in Android image,
Expand Down
4 changes: 4 additions & 0 deletions include/configs/rockchip-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#define _ROCKCHIP_COMMON_H_
#include <linux/sizes.h>

#ifndef CFG_CPUID_OFFSET
#define CFG_CPUID_OFFSET 0x7
#endif

#define COUNTER_FREQUENCY 24000000

#if CONFIG_IS_ENABLED(TINY_FRAMEWORK) && !defined(CONFIG_ARM64)
Expand Down

0 comments on commit 2e80b28

Please sign in to comment.