Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E753FC07E9D for ; Mon, 26 Sep 2022 11:53:33 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 657E084DAD; Mon, 26 Sep 2022 13:53:23 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=kohlschutter.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=kohlschutter-com.20210112.gappssmtp.com header.i=@kohlschutter-com.20210112.gappssmtp.com header.b="GgCUVz9H"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 7FBA18043C; Sun, 25 Sep 2022 19:07:50 +0200 (CEST) Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 3E59984BE3 for ; Sun, 25 Sep 2022 19:07:47 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=kohlschutter.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=christian@kohlschuetter.com Received: by mail-ej1-x632.google.com with SMTP id dv25so9693407ejb.12 for ; Sun, 25 Sep 2022 10:07:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kohlschutter-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date; bh=Hd4DoYv0kYGPboGBcvYSyWvJj2gDXLqQu9ZqoQvnBmY=; b=GgCUVz9Hiv7ouwAu0hqYK5arJNiLPzQpoROLK/T6fzv+8QulsYhEXBED5k4TRiU4vF IKn4Hy92v87O+Tirhh4BOkGCu86hoemTWdBFlKdOyghCRXX5q6BzQICkQCWloF7v8zEP UU5fnbSGXP7DxpaJePgMy2NvUCEXqP8v6tmNMm2eoUdk2YkKIDHy+OogN4CBXM977Ull UzqzoUNTCb1Wx0bIzRYT2aElbH0G5t9zoXHsp4D/rmdPbFVY6LV7+FfdV9Kp7W07Vt3g KthfXbXoWl6di7DFR/ZeM9I0On725fKqUuj1EJvULESl9j+XpBmioOLAm5+byzRiPH5c 1Emg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date; bh=Hd4DoYv0kYGPboGBcvYSyWvJj2gDXLqQu9ZqoQvnBmY=; b=TOh3raWQn6f0+ACee79oQVuOFvP7dxc3xQiq+ljddCVHnwL8lSSZXj4TEr6kMKAS81 urQFcFvGOIcgoSUnaIjjSr7ciJtm3VMPdLFf23yanBJNtiuQ1tOKNwcckqqzVFHCN0tG 2WMLVxJmUH0LG6Xikv4+WIT5mZLeEO9ldi1CPXdwSdgO+ypLRCO9auODodPcOGfsb6iq /0a/enmSx0aIYyphzmc2Z8ICriRZGrTR0cVb0Fvm0KAOo0O5gFiyOFeUuIBFqn0o+RB8 Fg/J3LFvEQNkTxAJGWGNHb5lABCVgPmFt+Ez5Ndfh8vm3aH+3CBwL0baNHa8z53WG36t B5zA== X-Gm-Message-State: ACrzQf0U0XCRhfAnrvUWBMm/wVVQKdwSi7uertjiWJ197u0AN40TOhyI uzrg/ESLoGT+tA7TQ/xmxad1MBhbksQRaTMnGj8= X-Google-Smtp-Source: AMsMyM4gQAHtgU2MOLa9LU2NVZMH6R1Z94NHWFzig1CYoCT6vXxnEHy+Xzzk8e+CCzDoqTc7daOb4A== X-Received: by 2002:a17:906:fe46:b0:73d:939a:ec99 with SMTP id wz6-20020a170906fe4600b0073d939aec99mr15229382ejb.169.1664125666612; Sun, 25 Sep 2022 10:07:46 -0700 (PDT) Received: from plus1.kohl.home (ip5b434222.dynamic.kabel-deutschland.de. [91.67.66.34]) by smtp.gmail.com with ESMTPSA id i14-20020a50fc0e000000b0044792480994sm9711107edr.68.2022.09.25.10.07.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Sep 2022 10:07:46 -0700 (PDT) From: =?UTF-8?q?Christian=20Kohlschu=CC=88tter?= To: u-boot@lists.denx.de, Kever Yang , Simon Glass , Philipp Tomsich , Xiaobo Tian Cc: =?UTF-8?q?Christian=20Kohlschu=CC=88tter?= Subject: [PATCH] rockchip: rk3399: add ethaddr and serial# init, enable for R4S Date: Sun, 25 Sep 2022 17:07:31 +0000 Message-Id: <20220925170733.19787-1-christian@kohlschutter.com> X-Mailer: git-send-email 2.36.2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Mailman-Approved-At: Mon, 26 Sep 2022 13:53:20 +0200 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean Some RK3399 boards, such as newer revisions of NanoPi R4S, do not provide an EEPROM chip containing a globally unique MAC address. Currently, this means that a randomly generated temporary MAC address may be generated each time the device is rebooted, leading to ARP cache issues and other confusing bugs. Since RK3399 CPUs provide a built-in unique serial number, we can reliably derive a locally MAC address from it by reading the corresponding bits from the non-secure efuse block. Port from uboot-rockchip 0c294d0, fix compilation issues and adjust coding style. rockchip: board: puma_rk3399: derive ethaddr from cpuid rockchip: board: puma_rk3399: add support for serial# and cpuid# rockchip: efuse: add (misc) driver for RK3399 non-secure efuse block via efuses Signed-off-by: Christian Kohlschütter --- board/rockchip/evb_rk3399/evb-rk3399.c | 120 +++++++++++++++++++++++++ configs/nanopi-r4s-rk3399_defconfig | 4 + drivers/misc/Makefile | 2 +- 3 files changed, 125 insertions(+), 1 deletion(-) diff --git a/board/rockchip/evb_rk3399/evb-rk3399.c b/board/rockchip/evb_rk3399/evb-rk3399.c index abb76585cf..ab8738ccf3 100644 --- a/board/rockchip/evb_rk3399/evb-rk3399.c +++ b/board/rockchip/evb_rk3399/evb-rk3399.c @@ -5,11 +5,20 @@ #include #include +#include +#include +#include +#include #include #include #include +#include +#include #include +#define RK3399_CPUID_OFF 0x7 +#define RK3399_CPUID_LEN 0x10 + #ifndef CONFIG_SPL_BUILD int board_early_init_f(void) { @@ -30,3 +39,114 @@ out: return 0; } #endif +static void setup_macaddr(void) +{ + if (!IS_ENABLED(CONFIG_CMD_NET)) + return; + + 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; + + if (!cpuid) { + debug("%s: could not retrieve 'cpuid#'\n", __func__); + return; + } + + ret = hash_block("sha256", (void *)cpuid, strlen(cpuid), hash, &size); + if (ret) { + debug("%s: failed to calculate SHA256\n", __func__); + return; + } + + /* 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); +} + +static void setup_serial(void) +{ + if (!IS_ENABLED(CONFIG_ROCKCHIP_EFUSE)) + return; + + struct udevice *dev; + int ret, i; + u8 cpuid[RK3399_CPUID_LEN]; + u8 low[RK3399_CPUID_LEN / 2], high[RK3399_CPUID_LEN / 2]; + char cpuid_str[RK3399_CPUID_LEN * 2 + 1]; + u64 serialno; + char serialno_str[16]; + + /* retrieve the device */ + ret = uclass_get_device_by_driver(UCLASS_MISC, + DM_DRIVER_GET(rockchip_efuse), &dev); + if (ret) { + debug("%s: could not find efuse device\n", __func__); + return; + } + + /* read the cpu_id range from the efuses */ + ret = misc_read(dev, RK3399_CPUID_OFF, &cpuid, sizeof(cpuid)); + if (ret) { + debug("%s: reading cpuid from the efuses failed\n", + __func__); + return; + } + + 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), "%llx", serialno); + + env_set("cpuid#", cpuid_str); + env_set("serial#", serialno_str); +} + +int misc_init_r(void) +{ + setup_serial(); + setup_macaddr(); + + return 0; +} + +void get_board_serial(struct tag_serialnr *serialnr) +{ + u64 serial = 0; + + if (IS_ENABLED(CONFIG_SERIAL_TAG)) { + char *serial_string; + + serial_string = env_get("serial#"); + + if (serial_string) + serial = simple_strtoull(serial_string, NULL, 16); + } + + serialnr->high = (u32)(serial >> 32); + serialnr->low = (u32)(serial & 0xffffffff); +} diff --git a/configs/nanopi-r4s-rk3399_defconfig b/configs/nanopi-r4s-rk3399_defconfig index 46ba07f4d5..aa89ac5854 100644 --- a/configs/nanopi-r4s-rk3399_defconfig +++ b/configs/nanopi-r4s-rk3399_defconfig @@ -63,3 +63,7 @@ CONFIG_VIDEO_ROCKCHIP=y CONFIG_DISPLAY_ROCKCHIP_HDMI=y CONFIG_SPL_TINY_MEMSET=y CONFIG_ERRNO_STR=y +CONFIG_MISC=y +CONFIG_MISC_INIT_R=y +CONFIG_ROCKCHIP_EFUSE=y +CONFIG_ROCKCHIP_OTP=y diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile index b9c54bdd99..a2ce9e737c 100644 --- a/drivers/misc/Makefile +++ b/drivers/misc/Makefile @@ -64,7 +64,7 @@ obj-$(CONFIG_QFW_PIO) += qfw_pio.o obj-$(CONFIG_QFW_MMIO) += qfw_mmio.o obj-$(CONFIG_SANDBOX) += qfw_sandbox.o endif -obj-$(CONFIG_ROCKCHIP_EFUSE) += rockchip-efuse.o +obj-$(CONFIG_ROCKCHIP_EFUSE) += rockchip-efuse.o misc-uclass.o obj-$(CONFIG_ROCKCHIP_OTP) += rockchip-otp.o obj-$(CONFIG_SANDBOX) += syscon_sandbox.o misc_sandbox.o obj-$(CONFIG_SIFIVE_OTP) += sifive-otp.o -- 2.36.2