Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions boards/atmel/sam/sam_e70_xplained/Kconfig.defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,10 @@
# Copyright (c) 2016 Piotr Mienkowski
# SPDX-License-Identifier: Apache-2.0

if ETH_SAM_GMAC

# Read MAC address from AT24MAC402 EEPROM

config ETH_SAM_GMAC_MAC_I2C_INT_ADDRESS
default 0x9A

config ETH_SAM_GMAC_MAC_I2C_INT_ADDRESS_SIZE
default 1

config ETH_SAM_GMAC_MAC_I2C_EEPROM
config ETH_SAM_GMAC_MAC_EEPROM
default y
select I2C

endif # ETH_SAM_GMAC
depends on ETH_SAM_GMAC

if NETWORKING

Expand Down
15 changes: 13 additions & 2 deletions boards/atmel/sam/sam_e70_xplained/sam_e70_xplained-common.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,18 @@
pinctrl-names = "default";

eeprom: eeprom@5f {
compatible = "atmel,24mac402";
compatible = "atmel,at24", "atmel,24mac402";
reg = <0x5f>;
size = <256>;
pagesize = <16>;
address-width = <8>;
timeout = <5>;

#address-cells = <1>;
#size-cells = <1>;
mac_address: mac_address@9a {
reg = <0x9a 0x06>;
};
};
};

Expand Down Expand Up @@ -126,7 +136,8 @@ zephyr_udc0: &usbhs {
pinctrl-0 = <&gmac_rmii>;
pinctrl-names = "default";

mac-eeprom = <&eeprom>;
nvmem-cells = <&mac_address>;
nvmem-cell-names = "mac-address";
phy-handle = <&phy>;
};

Expand Down
14 changes: 2 additions & 12 deletions boards/atmel/sam/sam_v71_xult/Kconfig.defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,11 @@
# Copyright (c) 2016 Piotr Mienkowski
# SPDX-License-Identifier: Apache-2.0

if ETH_SAM_GMAC

# Read MAC address from AT24MAC402 EEPROM

config ETH_SAM_GMAC_MAC_I2C_EEPROM
config ETH_SAM_GMAC_MAC_EEPROM
default y
select I2C

config ETH_SAM_GMAC_MAC_I2C_INT_ADDRESS
default 0x9A

config ETH_SAM_GMAC_MAC_I2C_INT_ADDRESS_SIZE
default 1

endif # ETH_SAM_GMAC
depends on ETH_SAM_GMAC

if NETWORKING

Expand Down
15 changes: 13 additions & 2 deletions boards/atmel/sam/sam_v71_xult/sam_v71_xult-common.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,18 @@
pinctrl-names = "default";

eeprom: eeprom@5f {
compatible = "atmel,24mac402";
compatible = "atmel,at24", "atmel,24mac402";
reg = <0x5f>;
size = <256>;
pagesize = <16>;
address-width = <8>;
timeout = <5>;

#address-cells = <1>;
#size-cells = <1>;
mac_address: mac_address@9a {
reg = <0x9a 0x06>;
};
};
};

Expand Down Expand Up @@ -235,7 +245,8 @@ zephyr_udc0: &usbhs {
pinctrl-0 = <&gmac_rmii>;
pinctrl-names = "default";

mac-eeprom = <&eeprom>;
nvmem-cells = <&mac_address>;
nvmem-cell-names = "mac-address";
phy-handle = <&phy>;
};

Expand Down
15 changes: 2 additions & 13 deletions boards/atmel/sam0/same54_xpro/Kconfig.defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,10 @@
# Copyright (c) 2024 Gerson Fernando Budke <[email protected]>
# SPDX-License-Identifier: Apache-2.0

if ETH_SAM_GMAC

# Read MAC address from AT24MAC402 EEPROM

config ETH_SAM_GMAC_MAC_I2C_INT_ADDRESS
default 0x9A

config ETH_SAM_GMAC_MAC_I2C_INT_ADDRESS_SIZE
default 1

config ETH_SAM_GMAC_MAC_I2C_EEPROM
config ETH_SAM_GMAC_MAC_EEPROM
default y
select I2C

endif # ETH_SAM_GMAC
depends on ETH_SAM_GMAC

if NETWORKING

Expand Down
15 changes: 13 additions & 2 deletions boards/atmel/sam0/same54_xpro/same54_xpro.dts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,18 @@
pinctrl-names = "default";

eeprom: eeprom@5e {
compatible = "atmel,24mac402";
compatible = "atmel,at24", "atmel,24mac402";
reg = <0x5e>;
size = <256>;
pagesize = <16>;
address-width = <8>;
timeout = <5>;

#address-cells = <1>;
#size-cells = <1>;
mac_address: mac_address@9a {
reg = <0x9a 0x06>;
};
};
};

Expand All @@ -126,7 +136,8 @@ zephyr_udc0: &usb0 {
pinctrl-0 = <&gmac_rmii>;
pinctrl-names = "default";

mac-eeprom = <&eeprom>;
nvmem-cells = <&mac_address>;
nvmem-cell-names = "mac-address";
phy-handle = <&phy>;
};

Expand Down
26 changes: 4 additions & 22 deletions drivers/ethernet/Kconfig.sam_gmac
Original file line number Diff line number Diff line change
Expand Up @@ -73,29 +73,11 @@ config ETH_SAM_GMAC_BUF_RX_COUNT
fit at least two Ethernet frames: one being received by the GMAC module
and the other being processed by the higher layer networking stack.

config ETH_SAM_GMAC_MAC_I2C_EEPROM
bool "Read from an I2C EEPROM"
config ETH_SAM_GMAC_MAC_EEPROM
bool "MAC eeprom"
select EEPROM
help
Read MAC address from an I2C EEPROM.

if ETH_SAM_GMAC_MAC_I2C_EEPROM

config ETH_SAM_GMAC_MAC_I2C_INT_ADDRESS
hex "I2C EEPROM internal address"
range 0 0xffffffff
help
Internal address of the EEPROM chip where the MAC address is stored.
Chips with 1 to 4 byte internal address size are supported. Address
size has to be configured in a separate Kconfig option.

config ETH_SAM_GMAC_MAC_I2C_INT_ADDRESS_SIZE
int "I2C EEPROM internal address size"
default 1
range 1 4
help
Size (in bytes) of the internal EEPROM address.

endif # ETH_SAM_GMAC_MAC_I2C_EEPROM
Enable reading MAC address from external flash memory.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Enable reading MAC address from external flash memory.
Enable reading MAC address from external EEPROM memory.


config PTP_CLOCK_SAM_GMAC
bool "SAM GMAC PTP clock driver support"
Expand Down
28 changes: 13 additions & 15 deletions drivers/ethernet/eth_sam_gmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
#include <zephyr/net/net_if.h>
#include <zephyr/net/ethernet.h>
#include <ethernet/eth_stats.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/devicetree/nvmem.h>
#include <zephyr/drivers/eeprom.h>
#include <zephyr/drivers/pinctrl.h>
#include <zephyr/drivers/clock_control/atmel_sam_pmc.h>
#include <soc.h>
Expand Down Expand Up @@ -1729,33 +1730,30 @@ static int eth_initialize(const struct device *dev)
return retval;
}

#if DT_INST_NODE_HAS_PROP(0, mac_eeprom)
static void get_mac_addr_from_i2c_eeprom(uint8_t mac_addr[6])
#if DT_INST_NODE_HAS_PROP(0, nvmem_cells) && defined(CONFIG_ETH_SAM_GMAC_MAC_EEPROM)
static void get_mac_addr_from_eeprom(uint8_t mac_addr[6])
{
uint32_t iaddr = CONFIG_ETH_SAM_GMAC_MAC_I2C_INT_ADDRESS;
int ret;
const struct i2c_dt_spec i2c = I2C_DT_SPEC_GET(DT_INST_PHANDLE(0, mac_eeprom));
const struct device *eeprom_dev = DT_NVMEM_DEV_BY_IDX(DT_DRV_INST(0), 0);
uint32_t addr = DT_NVMEM_ADDR_BY_IDX(DT_DRV_INST(0), 0);

if (!device_is_ready(i2c.bus)) {
LOG_ERR("Bus device is not ready");
if (!device_is_ready(eeprom_dev)) {
LOG_ERR("EEPROM device not ready.");
return;
}

ret = i2c_write_read_dt(&i2c,
&iaddr, CONFIG_ETH_SAM_GMAC_MAC_I2C_INT_ADDRESS_SIZE,
mac_addr, 6);

if (ret != 0) {
LOG_ERR("I2C: failed to read MAC addr");
ret = eeprom_read(eeprom_dev, addr, mac_addr, 6);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this at least check if the length matches that of the reg property in the devicetree instead of hardcoding 6 with no check?

if (ret < 0) {
LOG_ERR("Error reading MAC address from EEPROM [%d]", ret);
return;
}
}
#endif

static void generate_mac(uint8_t mac_addr[6])
{
#if DT_INST_NODE_HAS_PROP(0, mac_eeprom)
get_mac_addr_from_i2c_eeprom(mac_addr);
#if DT_INST_NODE_HAS_PROP(0, nvmem_cells) && defined(CONFIG_ETH_SAM_GMAC_MAC_EEPROM)
get_mac_addr_from_eeprom(mac_addr);
#elif DT_INST_PROP(0, zephyr_random_mac_address)
gen_random_mac(mac_addr, ATMEL_OUI_B0, ATMEL_OUI_B1, ATMEL_OUI_B2);
#endif
Expand Down
32 changes: 31 additions & 1 deletion drivers/ethernet/eth_xmc4xxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

#include <soc.h>
#include <zephyr/device.h>
#include <zephyr/devicetree/nvmem.h>
#include <zephyr/drivers/eeprom.h>
#include <zephyr/drivers/pinctrl.h>
#include <zephyr/drivers/ptp_clock.h>
#include <zephyr/net/ethernet.h>
Expand Down Expand Up @@ -799,6 +801,31 @@ static inline int eth_xmc4xxx_init_timestamp_control_reg(ETH_GLOBAL_TypeDef *reg
return 0;
}

static int eth_xmc4xxx_get_mac_address_eeprom(const struct device *dev)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of putting this in the drivers, wouldn't make sense to put this on a higher level and use eth_xmc4xxx_set_config to apply?

Copy link
Member

@maass-hamburg maass-hamburg Jun 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

maybe we can have some generic implementation for setting the mac address in
in net_if_post_init() just before bringing the iface up of subsys/net/ip/net_if.c.

What do you think? @jukkar @rlubos

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be after iface init, but before iface up

{
#if DT_INST_NODE_HAS_PROP(0, nvmem_cells)
struct eth_xmc4xxx_data *dev_data = dev->data;
const struct device *eeprom_dev = DT_NVMEM_DEV_BY_IDX(DT_DRV_INST(0), 0);
uint16_t addr = DT_NVMEM_ADDR_BY_IDX(DT_DRV_INST(0), 0);

int ret;

if (!device_is_ready(eeprom_dev)) {
LOG_ERR("EEPROM device not ready.");
return -ENODEV;
}

ret = eeprom_read(eeprom_dev, addr, dev_data->mac_addr, sizeof(dev_data->mac_addr));
if (ret < 0) {
LOG_ERR("Error reading MAC address from EEPROM [%d]", ret);
}

return ret;
#else
return -ENODEV;
#endif
}

static int eth_xmc4xxx_init(const struct device *dev)
{
struct eth_xmc4xxx_data *dev_data = dev->data;
Expand Down Expand Up @@ -867,7 +894,10 @@ static int eth_xmc4xxx_init(const struct device *dev)
eth_xmc4xxx_mask_unused_interrupts(dev_cfg->regs);

#if !DT_INST_NODE_HAS_PROP(0, local_mac_address)
gen_random_mac(dev_data->mac_addr, INFINEON_OUI_B0, INFINEON_OUI_B1, INFINEON_OUI_B2);
if (eth_xmc4xxx_get_mac_address_eeprom(dev) < 0) {
gen_random_mac(dev_data->mac_addr, INFINEON_OUI_B0, INFINEON_OUI_B1,
INFINEON_OUI_B2);
}
#endif
eth_xmc4xxx_set_mac_address(dev_cfg->regs, dev_data->mac_addr);

Expand Down
1 change: 1 addition & 0 deletions dts/bindings/ethernet/atmel,gmac-common.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
include:
- name: ethernet-controller.yaml
- name: pinctrl-device.yaml
- name: nvmem-consumer.yaml

properties:
reg:
Expand Down
1 change: 1 addition & 0 deletions dts/bindings/ethernet/infineon,xmc4xxx-ethernet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ compatible: "infineon,xmc4xxx-ethernet"
include:
- name: ethernet-controller.yaml
- name: pinctrl-device.yaml
- name: nvmem-consumer.yaml

properties:
interrupts:
Expand Down
15 changes: 15 additions & 0 deletions dts/bindings/mtd/nvmem-consumer.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright (c) 2024, Andriy Gelman <[email protected]>
# SPDX-License-Identifier: Apache-2.0

include: base.yaml
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should include base.yaml as it does not depend on it.


properties:
nvmem-cell-names:
type: string-array
description:
Names for each nvmem-cells specified.

nvmem-cells:
type: phandles
description:
List of phandle to the nvmem data cells.
Loading
Loading