From 3d8196f41b38971c6dc837d908162da5f14e0ecf Mon Sep 17 00:00:00 2001 From: Declan Snyder Date: Tue, 10 Jun 2025 12:09:43 -0500 Subject: [PATCH] drivers: nxp_enet: Use hwinfo API for unique mac Use the HWINFO API for getting the unique mac address, and use the pre-existing hardcoded macros as fallbacks if hwinfo is not implemented properly. Signed-off-by: Declan Snyder --- drivers/ethernet/Kconfig.nxp_enet | 3 +++ drivers/ethernet/eth_nxp_enet.c | 22 +++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/drivers/ethernet/Kconfig.nxp_enet b/drivers/ethernet/Kconfig.nxp_enet index 3b94a159214..c035fbf686b 100644 --- a/drivers/ethernet/Kconfig.nxp_enet +++ b/drivers/ethernet/Kconfig.nxp_enet @@ -4,6 +4,8 @@ # Copyright 2024-2025 NXP # SPDX-License-Identifier: Apache-2.0 +DT_NXP_UNIQUE_MAC_PROP := nxp,unique-mac + config ETH_NXP_ENET bool "NXP ENET Ethernet driver" default y @@ -14,6 +16,7 @@ config ETH_NXP_ENET select NET_POWER_MANAGEMENT if (PM_DEVICE && SOC_FAMILY_KINETIS) select ETH_DSA_SUPPORT_DEPRECATED select PINCTRL + select HWINFO if $(dt_compat_any_has_prop,$(DT_COMPAT_NXP_ENET_MAC),$(DT_NXP_UNIQUE_MAC_PROP),True) help Enable NXP ENET Ethernet driver. diff --git a/drivers/ethernet/eth_nxp_enet.c b/drivers/ethernet/eth_nxp_enet.c index f336495f1ff..358c917cc50 100644 --- a/drivers/ethernet/eth_nxp_enet.c +++ b/drivers/ethernet/eth_nxp_enet.c @@ -34,6 +34,7 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME); #include #include +#include #ifdef CONFIG_PTP_CLOCK #include @@ -641,12 +642,28 @@ static const struct device *eth_nxp_enet_get_phy(const struct device *dev) return config->phy_dev; } +/* we enable HWINFO from kconfig if the relevant DT prop is in the tree */ +#ifdef CONFIG_HWINFO /* Note this is not universally unique, it just is probably unique on a network */ static inline void nxp_enet_unique_mac(uint8_t *mac_addr) { - uint32_t id = ETH_NXP_ENET_UNIQUE_ID; + uint8_t id_buf[3]; + uint32_t id = 0; + int ret; + + ret = hwinfo_get_device_id(id_buf, sizeof(id_buf)); + if (ret == sizeof(id_buf)) { + id |= FIELD_PREP(0xFF0000, id_buf[2]); + id |= FIELD_PREP(0x00FF00, id_buf[1]); + id |= FIELD_PREP(0x0000FF, id_buf[0]); + } else { + /* Either implemented wrong, implemented insufficiently, or not at all */ + /* This is fallback for platforms that don't have HWINFO properly */ + id = ETH_NXP_ENET_UNIQUE_ID; + } if (id == 0xFFFFFF) { + /* Allowed but should raise highest level error notice to user */ LOG_ERR("No unique MAC can be provided in this platform"); } @@ -658,6 +675,9 @@ static inline void nxp_enet_unique_mac(uint8_t *mac_addr) mac_addr[4] = FIELD_GET(0x00FF00, id); mac_addr[5] = FIELD_GET(0x0000FF, id); } +#else +#define nxp_enet_unique_mac(...) +#endif #ifdef CONFIG_SOC_FAMILY_NXP_IMXRT #include