-
Notifications
You must be signed in to change notification settings - Fork 8k
drivers: eth_xmc4xxx: Add option to load MAC address from EEPROM #74835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -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); | ||
|
||
|
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this should include |
||
|
||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.