From: Philip Oberfichtner <pro@denx.de>
To: u-boot@lists.denx.de
Cc: Marek Vasut <marex@denx.de>,
festevam@denx.de, peng.fan@nxp.com,
Christoph Niedermaier <cniedermaier@dh-electronics.com>,
u-boot@dh-electronics.com, sbabic@denx.de,
Patrice Chotard <patrice.chotard@foss.st.com>,
Simon Glass <sjg@chromium.org>,
Patrick Delaunay <patrick.delaunay@foss.st.com>,
Philip Oberfichtner <pro@denx.de>
Subject: [PATCH v4 1/4] board: dhelectronics: Implement common MAC address functions
Date: Tue, 26 Jul 2022 15:04:50 +0200 [thread overview]
Message-ID: <20220726130454.2829205-2-pro@denx.de> (raw)
In-Reply-To: <20220726130454.2829205-1-pro@denx.de>
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 <pro@denx.de>
Reviewed-by: Marek Vasut <marex@denx.de>
---
(no changes since v3)
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 <pro@denx.de>
+#
+
+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 <marex@denx.de>
+ * Copyright 2022 DENX Software Engineering GmbH, Philip Oberfichtner <pro@denx.de>
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <i2c_eeprom.h>
+#include <net.h>
+
+#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 <pro@denx.de>
+ */
+
+/*
+ * 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 <marex@denx.de>
+ * Copyright 2022 DENX Software Engineering GmbH, Philip Oberfichtner <pro@denx.de>
+ */
+
+#include <asm/arch/imx-regs.h>
+#include <asm/arch/sys_proto.h>
+#include <common.h>
+#include <net.h>
+#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 <pro@denx.de>
+ */
+
+/*
+ * 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);
--
2.37.1
next prev parent reply other threads:[~2022-07-26 13:05 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-26 13:04 [PATCH v4 0/4] Deduplicate dhelectronics board files Philip Oberfichtner
2022-07-26 13:04 ` Philip Oberfichtner [this message]
2022-08-13 1:41 ` [PATCH v4 1/4] board: dhelectronics: Implement common MAC address functions Tom Rini
2022-07-26 13:04 ` [PATCH v4 2/4] ARM: imx6: DH: Use " Philip Oberfichtner
2022-08-13 1:41 ` Tom Rini
2022-07-26 13:04 ` [PATCH v4 3/4] ARM: imx8: " Philip Oberfichtner
2022-08-13 1:41 ` Tom Rini
2022-07-26 13:04 ` [PATCH v4 4/4] ARM: stm32: " Philip Oberfichtner
2022-08-13 1:41 ` Tom Rini
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220726130454.2829205-2-pro@denx.de \
--to=pro@denx.de \
--cc=cniedermaier@dh-electronics.com \
--cc=festevam@denx.de \
--cc=marex@denx.de \
--cc=patrice.chotard@foss.st.com \
--cc=patrick.delaunay@foss.st.com \
--cc=peng.fan@nxp.com \
--cc=sbabic@denx.de \
--cc=sjg@chromium.org \
--cc=u-boot@dh-electronics.com \
--cc=u-boot@lists.denx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox