From patchwork Mon Jul 25 08:19:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philip Oberfichtner X-Patchwork-Id: 1660259 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=denx.de header.i=@denx.de header.a=rsa-sha256 header.s=phobos-20191101 header.b=LHKbv/Ra; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LrtKC5TvJz9sB4 for ; Mon, 25 Jul 2022 18:20:47 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id BD04584183; Mon, 25 Jul 2022 10:20:21 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1658737222; bh=SDNroSF71H24pQZbE1Fhgn6A5JaMhBb49U1VLNdrYH0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=LHKbv/RaVgsugIoI61Cz8rRICLddWcLi1wdMs2QPZXgOTGEtgPdoAk0TVhY5xeu79 ezyoGHQcYHg31l0bu1y9tjpFLA2BELVli+fcQlbkZpkSrwVOBA3In583RYY7Vi/Yui shMlXp3YdFhcn7UyHx9kdXdpRTcAyeRRLV8Mb4DW2cXazvc6OJTEDVC1GyOZw/lcCB skBQtDQ2JFl6z5DiIXSzHTIW9oVhBASXr9KKmTCKA+yRdiLl+GsYIzTVpHYpZyUQfq QWYx+0Qf/Yv8keJGEU6GQgujh6Y0iOWAHiNEDSokVJ7qHJg9xiLVLtEc2rgQqDKX2t Q+V3M7ckYrRYw== Received: by phobos.denx.de (Postfix, from userid 109) id 73DD28415E; Mon, 25 Jul 2022 10:20:15 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00,RDNS_NONE, SPF_HELO_NONE,SPF_NEUTRAL autolearn=no autolearn_force=no version=3.4.2 Received: from xpert.denx.de (unknown [62.91.23.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 8A3BA8417B for ; Mon, 25 Jul 2022 10:20:07 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=pro@denx.de Received: by xpert.denx.de (Postfix, from userid 535) id 45B703E006E; Mon, 25 Jul 2022 10:20:07 +0200 (CEST) From: Philip Oberfichtner To: u-boot@lists.denx.de Cc: Patrice Chotard , festevam@denx.de, Simon Glass , Marek Vasut , Patrick Delaunay , Christoph Niedermaier , peng.fan@nxp.com, sbabic@denx.de, u-boot@dh-electronics.com, Philip Oberfichtner Subject: [PATCH v3 1/4] board: dhelectronics: Implement common MAC address functions Date: Mon, 25 Jul 2022 10:19:47 +0200 Message-Id: <20220725081950.25452-2-pro@denx.de> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220725081950.25452-1-pro@denx.de> References: <20220725081950.25452-1-pro@denx.de> MIME-Version: 1.0 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 This is a starting point for unifying duplicate code in the DH board files. The functions for setting up MAC addresses are very similar for the i.MX6, i.MX8 and stm32mp1 based boards. All pre-existing implementations follow the same logic: (1) Check if ethaddr is already set in the environment (2) If not, try to get it from a board specific location (e.g. fuse) (3) If not, try to get it from eeprom After this commit, (1) and (3) are implemented as common functions, ready to be used by board specific files. Furthermore there is an implementation of (2) for imx based boards. Signed-off-by: Philip Oberfichtner Reviewed-by: Marek Vasut --- Changes in v3: - Reviewed by Marek Changes in v2: - convert to livetree (rebase on commit 5a605b7c86152) board/dhelectronics/common/Makefile | 10 ++++ board/dhelectronics/common/dh_common.c | 65 ++++++++++++++++++++++++++ board/dhelectronics/common/dh_common.h | 28 +++++++++++ board/dhelectronics/common/dh_imx.c | 24 ++++++++++ board/dhelectronics/common/dh_imx.h | 12 +++++ 5 files changed, 139 insertions(+) create mode 100644 board/dhelectronics/common/Makefile create mode 100644 board/dhelectronics/common/dh_common.c create mode 100644 board/dhelectronics/common/dh_common.h create mode 100644 board/dhelectronics/common/dh_imx.c create mode 100644 board/dhelectronics/common/dh_imx.h diff --git a/board/dhelectronics/common/Makefile b/board/dhelectronics/common/Makefile new file mode 100644 index 0000000000..a472ea8d51 --- /dev/null +++ b/board/dhelectronics/common/Makefile @@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0+ +# +# Copyright 2022 DENX Software Engineering GmbH, Philip Oberfichtner +# + +obj-y += dh_common.o + +ifneq (,$(CONFIG_ARCH_MX6)$(CONFIG_ARCH_IMX8M)) +obj-y += dh_imx.o +endif diff --git a/board/dhelectronics/common/dh_common.c b/board/dhelectronics/common/dh_common.c new file mode 100644 index 0000000000..67e3d59b1f --- /dev/null +++ b/board/dhelectronics/common/dh_common.c @@ -0,0 +1,65 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright 2022 Marek Vasut + * Copyright 2022 DENX Software Engineering GmbH, Philip Oberfichtner + */ + +#include +#include +#include +#include + +#include "dh_common.h" + +bool dh_mac_is_in_env(const char *env) +{ + unsigned char enetaddr[6]; + + return eth_env_get_enetaddr(env, enetaddr); +} + +int dh_get_mac_from_eeprom(unsigned char *enetaddr, const char *alias) +{ + struct udevice *dev; + int ret; + ofnode node; + + node = ofnode_path(alias); + if (!ofnode_valid(node)) { + printf("%s: ofnode for %s not found!", __func__, alias); + return -ENOENT; + } + + ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, node, &dev); + if (ret) { + printf("%s: Cannot find EEPROM! ret = %d\n", __func__, ret); + return ret; + } + + ret = i2c_eeprom_read(dev, 0xfa, enetaddr, 0x6); + if (ret) { + printf("%s: Error reading EEPROM! ret = %d\n", __func__, ret); + return ret; + } + + if (!is_valid_ethaddr(enetaddr)) { + printf("%s: Address read from EEPROM is invalid!\n", __func__); + return -EINVAL; + } + + return 0; +} + +__weak int dh_setup_mac_address(void) +{ + unsigned char enetaddr[6]; + + if (dh_mac_is_in_env("ethaddr")) + return 0; + + if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0")) + return eth_env_set_enetaddr("ethaddr", enetaddr); + + printf("%s: Unable to set mac address!\n", __func__); + return -ENXIO; +} diff --git a/board/dhelectronics/common/dh_common.h b/board/dhelectronics/common/dh_common.h new file mode 100644 index 0000000000..2b24637d96 --- /dev/null +++ b/board/dhelectronics/common/dh_common.h @@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0+ + * + * Copyright 2022 DENX Software Engineering GmbH, Philip Oberfichtner + */ + +/* + * dh_mac_is_in_env - Check if MAC address is already set + * + * @env: name of environment variable + * Return: true if MAC is set, false otherwise + */ +bool dh_mac_is_in_env(const char *env); + +/* + * dh_get_mac_from_eeprom - Get MAC address from eeprom and write it to enetaddr + * + * @enetaddr: buffer where address is to be stored + * @alias: alias for EEPROM device tree node + * Return: 0 if OK, other value on error + */ +int dh_get_mac_from_eeprom(unsigned char *enetaddr, const char *alias); + +/* + * dh_setup_mac_address - Try to get MAC address from various locations and write it to env + * + * Return: 0 if OK, other value on error + */ +int dh_setup_mac_address(void); diff --git a/board/dhelectronics/common/dh_imx.c b/board/dhelectronics/common/dh_imx.c new file mode 100644 index 0000000000..7f451bad59 --- /dev/null +++ b/board/dhelectronics/common/dh_imx.c @@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright 2022 Marek Vasut + * Copyright 2022 DENX Software Engineering GmbH, Philip Oberfichtner + */ + +#include +#include +#include +#include +#include "dh_imx.h" + +int dh_imx_get_mac_from_fuse(unsigned char *enetaddr) +{ + /* + * If IIM fuses contain valid MAC address, use it. + * The IIM MAC address fuses are NOT programmed by default. + */ + imx_get_mac_from_fuse(0, enetaddr); + if (!is_valid_ethaddr(enetaddr)) + return -EINVAL; + + return 0; +} diff --git a/board/dhelectronics/common/dh_imx.h b/board/dhelectronics/common/dh_imx.h new file mode 100644 index 0000000000..284f8637fb --- /dev/null +++ b/board/dhelectronics/common/dh_imx.h @@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0+ + * + * Copyright 2022 DENX Software Engineering GmbH, Philip Oberfichtner + */ + +/* + * dh_imx_get_mac_from_fuse - Get MAC address from fuse and write it to env + * + * @enetaddr: buffer where address is to be stored + * Return: 0 if OK, other value on error + */ +int dh_imx_get_mac_from_fuse(unsigned char *enetaddr); From patchwork Mon Jul 25 08:19:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philip Oberfichtner X-Patchwork-Id: 1660257 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=denx.de header.i=@denx.de header.a=rsa-sha256 header.s=phobos-20191101 header.b=GZgnLlDE; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LrtJr0y8Fz9sB4 for ; Mon, 25 Jul 2022 18:20:28 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 7822C84180; Mon, 25 Jul 2022 10:20:13 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1658737213; bh=lleUcIMcDNieWscIA0S+4FeYfOnGQe28gN6cvV4fY9k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=GZgnLlDECJBPiTLprVRigZ1pHBft479RSK17KM+sXvgZ5W2efye610rRI1zAA668T SdUdv/TMyXNoaDD7Wfi0vPLGabmbggXilSxHgvQHF4jxLZQifS0QCB8ffvofNt9pRz SNzCAElHCgvXwQ+HkNtZykIcakDJt2poTVh5ybBW+chLuMbn4W7hVlWrOKWa3i0OK9 SYlex3mSELUCDmURSPdvhexvfoVFXfXCuUnzZ47XQRP4ZVtYg8yU6H4w32A0e2Cl7H edccgA7xXIwXJuVVwrHTzWAAkEbtN5gNBj6PBjeRwko7ppAJ85riftf+MuTfCyQjaz K7WXJsAR4dIlg== Received: by phobos.denx.de (Postfix, from userid 109) id 35A198415E; Mon, 25 Jul 2022 10:20:12 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00,RDNS_NONE, SPF_HELO_NONE,SPF_NEUTRAL autolearn=no autolearn_force=no version=3.4.2 Received: from xpert.denx.de (unknown [62.91.23.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id BE72B84158 for ; Mon, 25 Jul 2022 10:20:09 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=pro@denx.de Received: by xpert.denx.de (Postfix, from userid 535) id 84F0B3E006E; Mon, 25 Jul 2022 10:20:09 +0200 (CEST) From: Philip Oberfichtner To: u-boot@lists.denx.de Cc: Patrice Chotard , festevam@denx.de, Simon Glass , Marek Vasut , Patrick Delaunay , Christoph Niedermaier , peng.fan@nxp.com, sbabic@denx.de, u-boot@dh-electronics.com, Philip Oberfichtner , Andreas Geisreiter , Tom Rini Subject: [PATCH v3 2/4] ARM: imx6: DH: Use common MAC address functions Date: Mon, 25 Jul 2022 10:19:48 +0200 Message-Id: <20220725081950.25452-3-pro@denx.de> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220725081950.25452-1-pro@denx.de> References: <20220725081950.25452-1-pro@denx.de> MIME-Version: 1.0 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 To reduce code duplication, let the imx6 based DH boards use the common code for setting up their MAC addresses. Signed-off-by: Philip Oberfichtner Tested-by: Marek Vasut Reviewed-by: Marek Vasut --- Changes in v3: - Reviewed by Marek Changes in v2: - Tested-by Marek board/dhelectronics/dh_imx6/dh_imx6.c | 47 ++++++++------------------- 1 file changed, 14 insertions(+), 33 deletions(-) diff --git a/board/dhelectronics/dh_imx6/dh_imx6.c b/board/dhelectronics/dh_imx6/dh_imx6.c index e8aba83e1a..07fc9b1fe6 100644 --- a/board/dhelectronics/dh_imx6/dh_imx6.c +++ b/board/dhelectronics/dh_imx6/dh_imx6.c @@ -36,6 +36,9 @@ #include #include +#include "../common/dh_common.h" +#include "../common/dh_imx.h" + DECLARE_GLOBAL_DATA_PTR; int dram_init(void) @@ -82,46 +85,24 @@ int board_usb_phy_mode(int port) } #endif -static int setup_dhcom_mac_from_fuse(void) +int dh_setup_mac_address(void) { - struct udevice *dev; - ofnode eeprom; unsigned char enetaddr[6]; - int ret; - ret = eth_env_get_enetaddr("ethaddr", enetaddr); - if (ret) /* ethaddr is already set */ + if (dh_mac_is_in_env("ethaddr")) return 0; - imx_get_mac_from_fuse(0, enetaddr); - - if (is_valid_ethaddr(enetaddr)) { - eth_env_set_enetaddr("ethaddr", enetaddr); - return 0; - } - - eeprom = ofnode_get_aliases_node("eeprom0"); - if (!ofnode_valid(eeprom)) { - printf("Can't find eeprom0 alias!\n"); - return -ENODEV; - } + if (!dh_imx_get_mac_from_fuse(enetaddr)) + goto out; - ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev); - if (ret) { - printf("Cannot find EEPROM!\n"); - return ret; - } + if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0")) + goto out; - ret = i2c_eeprom_read(dev, 0xfa, enetaddr, 0x6); - if (ret) { - printf("Error reading configuration EEPROM!\n"); - return ret; - } + printf("%s: Unable to get MAC address!\n", __func__); + return -ENXIO; - if (is_valid_ethaddr(enetaddr)) - eth_env_set_enetaddr("ethaddr", enetaddr); - - return 0; +out: + return eth_env_set_enetaddr("ethaddr", enetaddr); } int board_early_init_f(void) @@ -188,7 +169,7 @@ int board_late_init(void) u32 hw_code; char buf[16]; - setup_dhcom_mac_from_fuse(); + dh_setup_mac_address(); hw_code = board_get_hwcode(); From patchwork Mon Jul 25 08:19:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philip Oberfichtner X-Patchwork-Id: 1660258 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=denx.de header.i=@denx.de header.a=rsa-sha256 header.s=phobos-20191101 header.b=XuoVB5J8; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LrtJz38p2z9sB4 for ; Mon, 25 Jul 2022 18:20:35 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id D74BB84196; Mon, 25 Jul 2022 10:20:16 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1658737217; bh=uyb/sggFAi+iqwyjnuNaspWOc+KuQSqA+v9JMA6ZIUU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=XuoVB5J8lUxEnXflL4kPA7Ma9wd0A5xVSndomL/eZ4PQBH6hGFuhq4SV6j0UVBpx8 BmHWmqCt78W5Ut1zHwbNXQRHPqQetrWDhyDVmRuVEZbm7bstdb8dA/4LpSv9HBMBuS EkcFtLqLlX2iDu/zjL2L5INSt6Xqp4StWXmSNCKBRbKZ4VpSlRoVI0rxCdoKPkR5wG rcx190kBOOY2lFNvQHpmbAbD46aY1MWht4YDhVsPvAUr0clkYFGJYNmnXlXdZTFd6U sjAgk4QZTBa6QmyvNTvwF/X10kg48+V1MPtFWVSAFazYO+OqYb78C0EWa0WlC8p56P otTTOLyhZazXQ== Received: by phobos.denx.de (Postfix, from userid 109) id C538A8418B; Mon, 25 Jul 2022 10:20:14 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00,RDNS_NONE, SPF_HELO_NONE,SPF_NEUTRAL autolearn=no autolearn_force=no version=3.4.2 Received: from xpert.denx.de (unknown [62.91.23.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 4AB2084164 for ; Mon, 25 Jul 2022 10:20:11 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=pro@denx.de Received: by xpert.denx.de (Postfix, from userid 535) id 06CEC3E006E; Mon, 25 Jul 2022 10:20:11 +0200 (CEST) From: Philip Oberfichtner To: u-boot@lists.denx.de Cc: Patrice Chotard , festevam@denx.de, Simon Glass , Marek Vasut , Patrick Delaunay , Christoph Niedermaier , peng.fan@nxp.com, sbabic@denx.de, u-boot@dh-electronics.com, Philip Oberfichtner , Tom Rini Subject: [PATCH v3 3/4] ARM: imx8: DH: Use common MAC address functions Date: Mon, 25 Jul 2022 10:19:49 +0200 Message-Id: <20220725081950.25452-4-pro@denx.de> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220725081950.25452-1-pro@denx.de> References: <20220725081950.25452-1-pro@denx.de> MIME-Version: 1.0 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 To reduce code duplication, let the imx8 based DH boards use the common code for setting up their MAC addresses. Signed-off-by: Philip Oberfichtner Tested-by: Marek Vasut Reviewed-by: Marek Vasut --- Changes in v3: - Reviewed by Marek Changes in v2: - Tested-by Marek .../dh_imx8mp/imx8mp_dhcom_pdk2.c | 121 +++++++----------- 1 file changed, 48 insertions(+), 73 deletions(-) diff --git a/board/dhelectronics/dh_imx8mp/imx8mp_dhcom_pdk2.c b/board/dhelectronics/dh_imx8mp/imx8mp_dhcom_pdk2.c index 8676c44d0d..6f06daf86f 100644 --- a/board/dhelectronics/dh_imx8mp/imx8mp_dhcom_pdk2.c +++ b/board/dhelectronics/dh_imx8mp/imx8mp_dhcom_pdk2.c @@ -16,6 +16,8 @@ #include #include "lpddr4_timing.h" +#include "../common/dh_common.h" +#include "../common/dh_imx.h" DECLARE_GLOBAL_DATA_PTR; @@ -75,95 +77,68 @@ static void setup_fec(void) set_clk_enet(ENET_125MHZ); } -static int setup_mac_address_from_eeprom(char *alias, char *env, bool odd) +static int dh_imx8_setup_ethaddr(void) { unsigned char enetaddr[6]; - struct udevice *dev; - int ret, offset; - - offset = fdt_path_offset(gd->fdt_blob, alias); - if (offset < 0) { - printf("%s: No eeprom0 path offset\n", __func__); - return offset; - } - - ret = uclass_get_device_by_of_offset(UCLASS_I2C_EEPROM, offset, &dev); - if (ret) { - printf("Cannot find EEPROM!\n"); - return ret; - } - - ret = i2c_eeprom_read(dev, 0xfa, enetaddr, 0x6); - if (ret) { - printf("Error reading configuration EEPROM!\n"); - return ret; - } + + if (dh_mac_is_in_env("ethaddr")) + return 0; + + if (!dh_imx_get_mac_from_fuse(enetaddr)) + goto out; + + if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0")) + goto out; + + return -ENXIO; + +out: + return eth_env_set_enetaddr("ethaddr", enetaddr); +} + +static int dh_imx8_setup_eth1addr(void) +{ + unsigned char enetaddr[6]; + + if (dh_mac_is_in_env("eth1addr")) + return 0; + + if (!dh_imx_get_mac_from_fuse(enetaddr)) + goto increment_out; + + if (!dh_get_mac_from_eeprom(enetaddr, "eeprom1")) + goto out; /* * Populate second ethernet MAC from first ethernet EEPROM with MAC * address LSByte incremented by 1. This is only used on SoMs without * second ethernet EEPROM, i.e. early prototypes. */ - if (odd) - enetaddr[5]++; + if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0")) + goto increment_out; - eth_env_set_enetaddr(env, enetaddr); + return -ENXIO; - return 0; +increment_out: + enetaddr[5]++; + +out: + return eth_env_set_enetaddr("eth1addr", enetaddr); } -static void setup_mac_address(void) +int dh_setup_mac_address(void) { - unsigned char enetaddr[6]; - bool skip_eth0 = false; - bool skip_eth1 = false; int ret; - ret = eth_env_get_enetaddr("ethaddr", enetaddr); - if (ret) /* ethaddr is already set */ - skip_eth0 = true; - - ret = eth_env_get_enetaddr("eth1addr", enetaddr); - if (ret) /* eth1addr is already set */ - skip_eth1 = true; + ret = dh_imx8_setup_ethaddr(); + if (ret) + printf("%s: Unable to setup ethaddr! ret = %d\n", __func__, ret); - /* Both MAC addresses are already set in U-Boot environment. */ - if (skip_eth0 && skip_eth1) - return; + ret = dh_imx8_setup_eth1addr(); + if (ret) + printf("%s: Unable to setup eth1addr! ret = %d\n", __func__, ret); - /* - * If IIM fuses contain valid MAC address, use it. - * The IIM MAC address fuses are NOT programmed by default. - */ - imx_get_mac_from_fuse(0, enetaddr); - if (is_valid_ethaddr(enetaddr)) { - if (!skip_eth0) - eth_env_set_enetaddr("ethaddr", enetaddr); - /* - * The LSbit of MAC address in fuses is always 0, use the - * next consecutive MAC address for the second ethernet. - */ - enetaddr[5]++; - if (!skip_eth1) - eth_env_set_enetaddr("eth1addr", enetaddr); - return; - } - - /* Use on-SoM EEPROMs with pre-programmed MAC address. */ - if (!skip_eth0) { - /* We cannot do much more if this returns -ve . */ - setup_mac_address_from_eeprom("eeprom0", "ethaddr", false); - } - - if (!skip_eth1) { - ret = setup_mac_address_from_eeprom("eeprom1", "eth1addr", - false); - if (ret) { /* Second EEPROM might not be populated. */ - /* We cannot do much more if this returns -ve . */ - setup_mac_address_from_eeprom("eeprom0", "eth1addr", - true); - } - } + return ret; } int board_init(void) @@ -176,7 +151,7 @@ int board_init(void) int board_late_init(void) { - setup_mac_address(); + dh_setup_mac_address(); return 0; } From patchwork Mon Jul 25 08:19:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philip Oberfichtner X-Patchwork-Id: 1660260 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=denx.de header.i=@denx.de header.a=rsa-sha256 header.s=phobos-20191101 header.b=z6W5Rbv5; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LrtKS4NJsz9sB4 for ; Mon, 25 Jul 2022 18:21:00 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 0E8C08417B; Mon, 25 Jul 2022 10:20:24 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1658737224; bh=RYJaoax6CTOghFvncoODiWc7E4KTNOu5FSB3Uz/9WoQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=z6W5Rbv5O9cTYy1KCaRlb5tP+bQKTfv+DaB2aSIYdc1BsUbKY1lwhuMRdkxf+qxdG 6QotowZkx+FzKO1MxFgu86boU2YnJHOvlXzvjPDZLeQxvoHFAV+ZCItRiB1gv6MX66 JkC1eVxYN+c5EcrZWbOLncRQ26vYDUVrXcSRbg7wJ9ItwJYfZhI2050N8jnHMeLwRd eynSq3n/BkjO+SfG1VQYYcbT3dBLw5wG4uigK1UwfitylRs4rmr8tg+dRq9PCECh2r 4z7+dkhqWP3hTUc08N5RiKX/hDovKuWlenvngzFb/mwWd6o+cW2/y45CLapFx5/qNu BqPjbkjA0/2+w== Received: by phobos.denx.de (Postfix, from userid 109) id 017CC8419B; Mon, 25 Jul 2022 10:20:17 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00,RDNS_NONE, SPF_HELO_NONE,SPF_NEUTRAL autolearn=no autolearn_force=no version=3.4.2 Received: from xpert.denx.de (unknown [62.91.23.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 3634E84185 for ; Mon, 25 Jul 2022 10:20:14 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=pro@denx.de Received: by xpert.denx.de (Postfix, from userid 535) id D3AA33E006E; Mon, 25 Jul 2022 10:20:13 +0200 (CEST) From: Philip Oberfichtner To: u-boot@lists.denx.de Cc: Patrice Chotard , festevam@denx.de, Simon Glass , Marek Vasut , Patrick Delaunay , Christoph Niedermaier , peng.fan@nxp.com, sbabic@denx.de, u-boot@dh-electronics.com, Philip Oberfichtner , Tom Rini , uboot-stm32@st-md-mailman.stormreply.com Subject: [PATCH v3 4/4] ARM: stm32: DH: Use common MAC address functions Date: Mon, 25 Jul 2022 10:19:50 +0200 Message-Id: <20220725081950.25452-5-pro@denx.de> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220725081950.25452-1-pro@denx.de> References: <20220725081950.25452-1-pro@denx.de> MIME-Version: 1.0 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 To reduce code duplication, let the stm32 based DH boards use the common code for setting up their MAC addresses. Signed-off-by: Philip Oberfichtner Tested-by: Marek Vasut Reviewed-by: Marek Vasut Reviewed-by: Patrick Delaunay --- Changes in v3: - Reviewed by Marek Changes in v2: - convert to livetree (rebase on commit 5a605b7c86152) - Tested-by Marek board/dhelectronics/dh_stm32mp1/board.c | 102 +++++++++++------------- 1 file changed, 45 insertions(+), 57 deletions(-) diff --git a/board/dhelectronics/dh_stm32mp1/board.c b/board/dhelectronics/dh_stm32mp1/board.c index 7a4c08cb7f..ab354e3e33 100644 --- a/board/dhelectronics/dh_stm32mp1/board.c +++ b/board/dhelectronics/dh_stm32mp1/board.c @@ -42,6 +42,7 @@ #include #include #include +#include "../common/dh_common.h" #include "../../st/common/stpmic1.h" /* SYSCFG registers */ @@ -84,36 +85,17 @@ #define KS_CIDER 0xC0 #define CIDER_ID 0x8870 -int setup_mac_address(void) +static bool dh_stm32_mac_is_in_ks8851(void) { - unsigned char enetaddr[6]; - bool skip_eth0 = false; - bool skip_eth1 = false; - struct udevice *dev; - int ret; ofnode node; - - ret = eth_env_get_enetaddr("ethaddr", enetaddr); - if (ret) /* ethaddr is already set */ - skip_eth0 = true; + u32 reg, cider, ccr; node = ofnode_path("ethernet1"); - if (!ofnode_valid(node)) { - /* ethernet1 is not present in the system */ - skip_eth1 = true; - goto out_set_ethaddr; - } + if (!ofnode_valid(node)) + return false; - ret = eth_env_get_enetaddr("eth1addr", enetaddr); - if (ret) { - /* eth1addr is already set */ - skip_eth1 = true; - goto out_set_ethaddr; - } - - ret = ofnode_device_is_compatible(node, "micrel,ks8851-mll"); - if (ret) - goto out_set_ethaddr; + if (ofnode_device_is_compatible(node, "micrel,ks8851-mll")) + return false; /* * KS8851 with EEPROM may use custom MAC from EEPROM, read @@ -121,56 +103,62 @@ int setup_mac_address(void) * is present. If EEPROM is present, it must contain valid * MAC address. */ - u32 reg, cider, ccr; reg = ofnode_get_addr(node); if (!reg) - goto out_set_ethaddr; + return false; writew(KS_BE0 | KS_BE1 | KS_CIDER, reg + 2); cider = readw(reg); - if ((cider & 0xfff0) != CIDER_ID) { - skip_eth1 = true; - goto out_set_ethaddr; - } + if ((cider & 0xfff0) != CIDER_ID) + return true; writew(KS_BE0 | KS_BE1 | KS_CCR, reg + 2); ccr = readw(reg); - if (ccr & KS_CCR_EEPROM) { - skip_eth1 = true; - goto out_set_ethaddr; - } + if (ccr & KS_CCR_EEPROM) + return true; + + return false; +} -out_set_ethaddr: - if (skip_eth0 && skip_eth1) +static int dh_stm32_setup_ethaddr(void) +{ + unsigned char enetaddr[6]; + + if (dh_mac_is_in_env("ethaddr")) return 0; - node = ofnode_path("eeprom0"); - if (!ofnode_valid(node)) { - printf("%s: No eeprom0 path offset\n", __func__); - return -ENOENT; - } + if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0")) + return eth_env_set_enetaddr("ethaddr", enetaddr); - ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, node, &dev); - if (ret) { - printf("Cannot find EEPROM!\n"); - return ret; - } + return -ENXIO; +} - ret = i2c_eeprom_read(dev, 0xfa, enetaddr, 0x6); - if (ret) { - printf("Error reading configuration EEPROM!\n"); - return ret; - } +static int dh_stm32_setup_eth1addr(void) +{ + unsigned char enetaddr[6]; - if (is_valid_ethaddr(enetaddr)) { - if (!skip_eth0) - eth_env_set_enetaddr("ethaddr", enetaddr); + if (dh_mac_is_in_env("eth1addr")) + return 0; + if (dh_stm32_mac_is_in_ks8851()) + return 0; + + if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0")) { enetaddr[5]++; - if (!skip_eth1) - eth_env_set_enetaddr("eth1addr", enetaddr); + return eth_env_set_enetaddr("eth1addr", enetaddr); } + return -ENXIO; +} + +int setup_mac_address(void) +{ + if (dh_stm32_setup_ethaddr()) + printf("%s: Unable to setup ethaddr!\n", __func__); + + if (dh_stm32_setup_eth1addr()) + printf("%s: Unable to setup eth1addr!\n", __func__); + return 0; }