Skip to content

Commit bc4ca67

Browse files
committed
drivers: net: sam_gmac: update get MAC address for multi instances support
Add variables to configuration struct for getting MAC address settings from device tree. Update generate_mac() to get proper MAC address for each GAMC interface. Signed-off-by: Tony Han <tony.han@microchip.com>
1 parent 6624901 commit bc4ca67

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed

drivers/ethernet/eth_sam_gmac.c

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,12 +1660,10 @@ static int eth_initialize(const struct device *dev)
16601660
return retval;
16611661
}
16621662

1663-
#if DT_INST_NODE_HAS_PROP(0, mac_eeprom)
1664-
static void get_mac_addr_from_i2c_eeprom(uint8_t mac_addr[6])
1663+
static void get_mac_addr_from_i2c_eeprom(uint8_t mac_addr[6], const struct i2c_dt_spec i2c)
16651664
{
16661665
uint32_t iaddr = CONFIG_ETH_SAM_GMAC_MAC_I2C_INT_ADDRESS;
16671666
int ret;
1668-
const struct i2c_dt_spec i2c = I2C_DT_SPEC_GET(DT_INST_PHANDLE(0, mac_eeprom));
16691667

16701668
if (!device_is_ready(i2c.bus)) {
16711669
LOG_ERR("Bus device is not ready");
@@ -1681,15 +1679,21 @@ static void get_mac_addr_from_i2c_eeprom(uint8_t mac_addr[6])
16811679
return;
16821680
}
16831681
}
1684-
#endif
16851682

1686-
static void generate_mac(uint8_t mac_addr[6])
1683+
static void generate_mac(uint8_t mac_addr[6],
1684+
enum mac_address_source mac_addr_src,
1685+
const struct i2c_dt_spec mac_eeprom)
16871686
{
1688-
#if DT_INST_NODE_HAS_PROP(0, mac_eeprom)
1689-
get_mac_addr_from_i2c_eeprom(mac_addr);
1690-
#elif DT_INST_PROP(0, zephyr_random_mac_address)
1691-
gen_random_mac(mac_addr, ATMEL_OUI_B0, ATMEL_OUI_B1, ATMEL_OUI_B2);
1692-
#endif
1687+
switch (mac_addr_src) {
1688+
case MAC_ADDR_SOURCE_EEPROM:
1689+
get_mac_addr_from_i2c_eeprom(mac_addr, mac_eeprom);
1690+
break;
1691+
case MAC_ADDR_SOURCE_RANDOM:
1692+
gen_random_mac(mac_addr, ATMEL_OUI_B0, ATMEL_OUI_B1, ATMEL_OUI_B2);
1693+
break;
1694+
default:
1695+
break;
1696+
}
16931697
}
16941698

16951699
static void phy_link_state_changed(const struct device *pdev,
@@ -1767,7 +1771,7 @@ static void eth_iface_init(struct net_if *iface)
17671771
return;
17681772
}
17691773

1770-
generate_mac(dev_data->mac_addr);
1774+
generate_mac(dev_data->mac_addr, cfg->mac_addr_src, cfg->mac_eeprom);
17711775

17721776
LOG_INF("%s MAC: %02x:%02x:%02x:%02x:%02x:%02x", dev->name,
17731777
dev_data->mac_addr[0], dev_data->mac_addr[1],
@@ -2089,6 +2093,21 @@ static const struct ethernet_api eth_api = {
20892093
#else
20902094
#define CFG_CLK_DEFN(n)
20912095
#endif
2096+
2097+
#define SAM_GMAC_MAC_EEPROM_I2C(n) \
2098+
COND_CODE_1(DT_NODE_HAS_PROP(DT_DRV_INST(n), mac_eeprom), \
2099+
(I2C_DT_SPEC_GET(DT_INST_PHANDLE(n, mac_eeprom))), \
2100+
({}))
2101+
2102+
#define SAM_GMAC_MAC_ADDR_SOURCE(n) \
2103+
COND_CODE_1(DT_NODE_HAS_PROP(DT_DRV_INST(n), mac_eeprom), \
2104+
(MAC_ADDR_SOURCE_EEPROM), \
2105+
(COND_CODE_1(DT_INST_PROP(n, zephyr_random_mac_address), \
2106+
(MAC_ADDR_SOURCE_RANDOM), \
2107+
(COND_CODE_1(DT_NODE_HAS_PROP(DT_DRV_INST(n), local_mac_address), \
2108+
(MAC_ADDR_SOURCE_LOCAL), \
2109+
(MAC_ADDR_SOURCE_INVALID))))))
2110+
20922111
#define SAM_GMAC_CFG_DEFN(n) \
20932112
static const struct eth_sam_dev_cfg eth##n##_config = { \
20942113
.regs = (Gmac *)DT_INST_REG_ADDR(n), \
@@ -2097,7 +2116,9 @@ static const struct ethernet_api eth_api = {
20972116
.config_func = eth##n##_irq_config, \
20982117
.phy_dev = DEVICE_DT_GET(DT_INST_PHANDLE(n, phy_handle)), \
20992118
.num_queues = DT_INST_PROP(n, num_queues), \
2100-
.phy_conn_type = DT_INST_ENUM_IDX(n, phy_connection_type) \
2119+
.phy_conn_type = DT_INST_ENUM_IDX(n, phy_connection_type), \
2120+
.mac_addr_src = SAM_GMAC_MAC_ADDR_SOURCE(n), \
2121+
.mac_eeprom = SAM_GMAC_MAC_EEPROM_I2C(n), \
21012122
};
21022123

21032124
#define DEFN_RX_FLAG_LIST_0(n) \

drivers/ethernet/eth_sam_gmac_priv.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,13 @@ struct gmac_queue {
262262
enum queue_idx que_idx;
263263
};
264264

265+
enum mac_address_source {
266+
MAC_ADDR_SOURCE_LOCAL,
267+
MAC_ADDR_SOURCE_RANDOM,
268+
MAC_ADDR_SOURCE_EEPROM,
269+
MAC_ADDR_SOURCE_INVALID,
270+
};
271+
265272
/* Device constant configuration parameters */
266273
struct eth_sam_dev_cfg {
267274
Gmac *regs;
@@ -271,8 +278,10 @@ struct eth_sam_dev_cfg {
271278
const struct pinctrl_dev_config *pcfg;
272279
void (*config_func)(void);
273280
const struct device *phy_dev;
274-
uint8_t num_queues;
275-
uint8_t phy_conn_type;
281+
const uint8_t num_queues;
282+
const uint8_t phy_conn_type;
283+
const enum mac_address_source mac_addr_src;
284+
const struct i2c_dt_spec mac_eeprom;
276285
};
277286

278287
/* Device run time data */

0 commit comments

Comments
 (0)