From ac58f3abe6e7e2305f3ae6212c904b0575e7332c Mon Sep 17 00:00:00 2001 From: Declan Snyder Date: Mon, 18 Mar 2024 16:40:42 -0500 Subject: [PATCH] drivers: nxp_enet: Generate MAC using eth.h The MAC address macros are ridiculous in this driver. Rewrite to be simpler and use eth.h common function. Also, clarify the mac address generation on the DT overlays. Signed-off-by: Declan Snyder --- .../dts/nxp,enet-experimental.overlay | 1 + .../dts/nxp,enet-experimental.overlay | 1 + .../dts/nxp,enet-experimental.overlay | 1 + .../dts/nxp,enet-experimental.overlay | 1 + .../dts/nxp,enet-experimental.overlay | 1 + .../dts/nxp,enet-experimental.overlay | 1 + .../dts/nxp,enet-experimental.overlay | 1 + drivers/ethernet/eth_nxp_enet.c | 117 +++++++----------- dts/bindings/ethernet/nxp,enet-mac.yaml | 7 ++ 9 files changed, 61 insertions(+), 70 deletions(-) diff --git a/boards/nxp/frdm_k64f/dts/nxp,enet-experimental.overlay b/boards/nxp/frdm_k64f/dts/nxp,enet-experimental.overlay index 9dfc07e9b0..67da05c453 100644 --- a/boards/nxp/frdm_k64f/dts/nxp,enet-experimental.overlay +++ b/boards/nxp/frdm_k64f/dts/nxp,enet-experimental.overlay @@ -42,6 +42,7 @@ pinctrl-0 = <&pinmux_enet>; pinctrl-names = "default"; phy-handle = <&phy>; + zephyr,random-mac-address; }; diff --git a/boards/nxp/mimxrt1024_evk/dts/nxp,enet-experimental.overlay b/boards/nxp/mimxrt1024_evk/dts/nxp,enet-experimental.overlay index 2229b2a9b7..eb36751f7e 100644 --- a/boards/nxp/mimxrt1024_evk/dts/nxp,enet-experimental.overlay +++ b/boards/nxp/mimxrt1024_evk/dts/nxp,enet-experimental.overlay @@ -43,6 +43,7 @@ pinctrl-0 = <&pinmux_enet>; pinctrl-names = "default"; phy-handle = <&phy>; + zephyr,random-mac-address; }; &enet_mdio { diff --git a/boards/nxp/mimxrt1050_evk/dts/nxp,enet-experimental.overlay b/boards/nxp/mimxrt1050_evk/dts/nxp,enet-experimental.overlay index 7fe69f0d52..6429b425a1 100644 --- a/boards/nxp/mimxrt1050_evk/dts/nxp,enet-experimental.overlay +++ b/boards/nxp/mimxrt1050_evk/dts/nxp,enet-experimental.overlay @@ -43,6 +43,7 @@ pinctrl-0 = <&pinmux_enet>; pinctrl-names = "default"; phy-handle = <&phy>; + zephyr,random-mac-address; }; &enet_mdio { diff --git a/boards/nxp/mimxrt1060_evk/dts/nxp,enet-experimental.overlay b/boards/nxp/mimxrt1060_evk/dts/nxp,enet-experimental.overlay index 7fe69f0d52..6429b425a1 100644 --- a/boards/nxp/mimxrt1060_evk/dts/nxp,enet-experimental.overlay +++ b/boards/nxp/mimxrt1060_evk/dts/nxp,enet-experimental.overlay @@ -43,6 +43,7 @@ pinctrl-0 = <&pinmux_enet>; pinctrl-names = "default"; phy-handle = <&phy>; + zephyr,random-mac-address; }; &enet_mdio { diff --git a/boards/nxp/mimxrt1064_evk/dts/nxp,enet-experimental.overlay b/boards/nxp/mimxrt1064_evk/dts/nxp,enet-experimental.overlay index 7fe69f0d52..6429b425a1 100644 --- a/boards/nxp/mimxrt1064_evk/dts/nxp,enet-experimental.overlay +++ b/boards/nxp/mimxrt1064_evk/dts/nxp,enet-experimental.overlay @@ -43,6 +43,7 @@ pinctrl-0 = <&pinmux_enet>; pinctrl-names = "default"; phy-handle = <&phy>; + zephyr,random-mac-address; }; &enet_mdio { diff --git a/boards/nxp/mimxrt1160_evk/dts/nxp,enet-experimental.overlay b/boards/nxp/mimxrt1160_evk/dts/nxp,enet-experimental.overlay index 58b8a80162..f6ed49f601 100644 --- a/boards/nxp/mimxrt1160_evk/dts/nxp,enet-experimental.overlay +++ b/boards/nxp/mimxrt1160_evk/dts/nxp,enet-experimental.overlay @@ -43,6 +43,7 @@ pinctrl-names = "default"; phy-handle = <&phy>; phy-connection-type = "rmii"; + zephyr,random-mac-address; }; &enet_mdio { diff --git a/boards/nxp/mimxrt1170_evk/dts/nxp,enet-experimental.overlay b/boards/nxp/mimxrt1170_evk/dts/nxp,enet-experimental.overlay index 58b8a80162..f6ed49f601 100644 --- a/boards/nxp/mimxrt1170_evk/dts/nxp,enet-experimental.overlay +++ b/boards/nxp/mimxrt1170_evk/dts/nxp,enet-experimental.overlay @@ -43,6 +43,7 @@ pinctrl-names = "default"; phy-handle = <&phy>; phy-connection-type = "rmii"; + zephyr,random-mac-address; }; &enet_mdio { diff --git a/drivers/ethernet/eth_nxp_enet.c b/drivers/ethernet/eth_nxp_enet.c index 7b60ba022f..f27a30ec24 100644 --- a/drivers/ethernet/eth_nxp_enet.c +++ b/drivers/ethernet/eth_nxp_enet.c @@ -47,17 +47,34 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME); #include #endif +#include "eth.h" #include #include #include +#define FREESCALE_OUI_B0 0x00 +#define FREESCALE_OUI_B1 0x04 +#define FREESCALE_OUI_B2 0x9f + +#if defined(CONFIG_SOC_SERIES_IMXRT10XX) +#define ETH_NXP_ENET_UNIQUE_ID (OCOTP->CFG1 ^ OCOTP->CFG2) +#elif defined(CONFIG_SOC_SERIES_IMXRT11XX) +#define ETH_NXP_ENET_UNIQUE_ID (OCOTP->FUSEN[40].FUSE) +#elif defined(CONFIG_SOC_SERIES_KINETIS_K6X) +#define ETH_NXP_ENET_UNIQUE_ID (SIM->UIDH ^ SIM->UIDMH ^ SIM->UIDML ^ SIM->UIDL) +#else +#define ETH_NXP_ENET_UNIQUE_ID 0xFFFFFF +#error "Unsupported SOC" +#endif + #define RING_ID 0 struct nxp_enet_mac_config { ENET_Type *base; const struct device *clock_dev; clock_control_subsys_t clock_subsys; - void (*generate_mac)(uint8_t *mac_addr); + bool generate_mac; + bool unique_mac; const struct pinctrl_dev_config *pincfg; enet_buffer_config_t buffer_config; uint8_t phy_mode; @@ -549,6 +566,18 @@ static void eth_nxp_enet_isr(const struct device *dev) irq_unlock(irq_lock_key); } +static inline void nxp_enet_unique_mac(uint8_t *mac_addr) +{ + uint32_t id = ETH_NXP_ENET_UNIQUE_ID; + + mac_addr[0] = FREESCALE_OUI_B0; + mac_addr[1] = FREESCALE_OUI_B1; + mac_addr[2] = FREESCALE_OUI_B2; + mac_addr[3] = FIELD_GET(0xFF0000, id); + mac_addr[4] = FIELD_GET(0x00FF00, id); + mac_addr[5] = FIELD_GET(0x0000FF, id); +} + static int eth_nxp_enet_init(const struct device *dev) { struct nxp_enet_mac_data *data = dev->data; @@ -572,8 +601,13 @@ static int eth_nxp_enet_init(const struct device *dev) #endif k_work_init(&data->rx_work, eth_nxp_enet_rx_thread); + if (config->unique_mac) { + nxp_enet_unique_mac(data->mac_addr); + } + if (config->generate_mac) { - config->generate_mac(data->mac_addr); + gen_random_mac(data->mac_addr, + FREESCALE_OUI_B0, FREESCALE_OUI_B1, FREESCALE_OUI_B2); } err = clock_control_get_rate(config->clock_dev, config->clock_subsys, @@ -714,71 +748,6 @@ static const struct ethernet_api api_funcs = { irq_enable(DT_IRQ_BY_IDX(node_id, idx, irq)); \ } while (false); -#define FREESCALE_OUI_B0 0x00 -#define FREESCALE_OUI_B1 0x04 -#define FREESCALE_OUI_B2 0x9f - -#if defined(CONFIG_SOC_SERIES_IMXRT10XX) -#define ETH_NXP_ENET_UNIQUE_ID (OCOTP->CFG1 ^ OCOTP->CFG2) -#elif defined(CONFIG_SOC_SERIES_IMXRT11XX) -#define ETH_NXP_ENET_UNIQUE_ID (OCOTP->FUSEN[40].FUSE) -#elif defined(CONFIG_SOC_SERIES_KINETIS_K6X) -#define ETH_NXP_ENET_UNIQUE_ID (SIM->UIDH ^ SIM->UIDMH ^ SIM->UIDML ^ SIM->UIDL) -#else -#error "Unsupported SOC" -#endif - -#define NXP_ENET_GENERATE_MAC_RANDOM(n) \ - static void generate_eth_##n##_mac(uint8_t *mac_addr) \ - { \ - gen_random_mac(mac_addr, \ - FREESCALE_OUI_B0, \ - FREESCALE_OUI_B1, \ - FREESCALE_OUI_B2); \ - } - -#define NXP_ENET_GENERATE_MAC_UNIQUE(n) \ - static void generate_eth_##n##_mac(uint8_t *mac_addr) \ - { \ - uint32_t id = ETH_NXP_ENET_UNIQUE_ID; \ - \ - mac_addr[0] = FREESCALE_OUI_B0; \ - mac_addr[0] |= 0x02; /* force LAA bit */ \ - mac_addr[1] = FREESCALE_OUI_B1; \ - mac_addr[2] = FREESCALE_OUI_B2; \ - mac_addr[3] = id >> 8; \ - mac_addr[4] = id >> 16; \ - mac_addr[5] = id >> 0; \ - mac_addr[5] += n; \ - } - -#define NXP_ENET_GENERATE_MAC(n) \ - COND_CODE_1(DT_INST_PROP(n, zephyr_random_mac_address), \ - (NXP_ENET_GENERATE_MAC_RANDOM(n)), \ - (NXP_ENET_GENERATE_MAC_UNIQUE(n))) - -#define NXP_ENET_DECIDE_MAC_ADDR(n) \ - COND_CODE_1(NODE_HAS_VALID_MAC_ADDR(DT_DRV_INST(n)), \ - (NXP_ENET_MAC_ADDR_LOCAL(n)), \ - (NXP_ENET_MAC_ADDR_GENERATED(n))) - -#define NXP_ENET_DECIDE_MAC_GEN_FUNC(n) \ - COND_CODE_1(NODE_HAS_VALID_MAC_ADDR(DT_DRV_INST(n)), \ - (NXP_ENET_GEN_MAC_FUNCTION_NO(n)), \ - (NXP_ENET_GEN_MAC_FUNCTION_YES(n))) - -#define NXP_ENET_MAC_ADDR_LOCAL(n) \ - .mac_addr = DT_INST_PROP(n, local_mac_address), - -#define NXP_ENET_MAC_ADDR_GENERATED(n) \ - .mac_addr = {0}, - -#define NXP_ENET_GEN_MAC_FUNCTION_NO(n) \ - .generate_mac = NULL, - -#define NXP_ENET_GEN_MAC_FUNCTION_YES(n) \ - .generate_mac = generate_eth_##n##_mac, - #define NXP_ENET_DT_PHY_DEV(node_id, phy_phandle, idx) \ DEVICE_DT_GET(DT_PHANDLE_BY_IDX(node_id, phy_phandle, idx)) @@ -830,8 +799,14 @@ static const struct ethernet_api api_funcs = { .txFrameInfo = NULL #endif +#define NXP_ENET_NODE_HAS_MAC_ADDR_CHECK(n) \ + BUILD_ASSERT(NODE_HAS_VALID_MAC_ADDR(DT_DRV_INST(n)) || \ + DT_INST_PROP(n, zephyr_random_mac_address) || \ + DT_INST_PROP(n, nxp_unique_mac), \ + "MAC address not specified on ENET DT node"); + #define NXP_ENET_MAC_INIT(n) \ - NXP_ENET_GENERATE_MAC(n) \ + NXP_ENET_NODE_HAS_MAC_ADDR_CHECK(n) \ \ PINCTRL_DT_INST_DEFINE(n); \ \ @@ -887,7 +862,9 @@ static const struct ethernet_api api_funcs = { .phy_dev = DEVICE_DT_GET(DT_INST_PHANDLE(n, phy_handle)), \ .mdio = DEVICE_DT_GET(DT_INST_PHANDLE(n, nxp_mdio)), \ NXP_ENET_PTP_DEV(n) \ - NXP_ENET_DECIDE_MAC_GEN_FUNC(n) \ + .generate_mac = DT_INST_PROP(n, \ + zephyr_random_mac_address), \ + .unique_mac = DT_INST_PROP(n, nxp_unique_mac), \ }; \ \ static _nxp_enet_driver_buffer_section uint8_t \ @@ -896,10 +873,10 @@ static const struct ethernet_api api_funcs = { nxp_enet_##n##_rx_frame_buf[NET_ETH_MAX_FRAME_SIZE]; \ \ struct nxp_enet_mac_data nxp_enet_##n##_data = { \ - NXP_ENET_DECIDE_MAC_ADDR(n) \ .tx_frame_buf = nxp_enet_##n##_tx_frame_buf, \ .rx_frame_buf = nxp_enet_##n##_rx_frame_buf, \ .dev = DEVICE_DT_INST_GET(n), \ + .mac_addr = DT_INST_PROP_OR(n, local_mac_address, {0}), \ }; \ \ ETH_NXP_ENET_PM_DEVICE_INIT(n) \ diff --git a/dts/bindings/ethernet/nxp,enet-mac.yaml b/dts/bindings/ethernet/nxp,enet-mac.yaml index bbcc2b4936..cf8ca151e4 100644 --- a/dts/bindings/ethernet/nxp,enet-mac.yaml +++ b/dts/bindings/ethernet/nxp,enet-mac.yaml @@ -22,3 +22,10 @@ properties: required: true description: | Corresponding ptp clock device + + nxp,unique-mac: + type: boolean + description: | + Use unique silicon ID to use UAA MAC. + This property will be overridden if the node has + zephyr,random-mac-address or local-mac-address also.