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);