@@ -33,6 +33,7 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
33
33
34
34
#include <zephyr/kernel.h>
35
35
#include <zephyr/device.h>
36
+ #include <zephyr/nvmem.h>
36
37
#include <zephyr/sys/__assert.h>
37
38
#include <zephyr/sys/barrier.h>
38
39
#include <zephyr/sys/util.h>
@@ -1729,36 +1730,23 @@ static int eth_initialize(const struct device *dev)
1729
1730
return retval ;
1730
1731
}
1731
1732
1732
- #if DT_INST_NODE_HAS_PROP (0 , mac_eeprom )
1733
- static void get_mac_addr_from_i2c_eeprom (uint8_t mac_addr [6 ])
1733
+ static int generate_mac (const struct device * dev )
1734
1734
{
1735
- uint32_t iaddr = CONFIG_ETH_SAM_GMAC_MAC_I2C_INT_ADDRESS ;
1736
- int ret ;
1737
- const struct i2c_dt_spec i2c = I2C_DT_SPEC_GET (DT_INST_PHANDLE (0 , mac_eeprom ));
1738
-
1739
- if (!device_is_ready (i2c .bus )) {
1740
- LOG_ERR ("Bus device is not ready" );
1741
- return ;
1742
- }
1735
+ struct eth_sam_dev_data * const dev_data = dev -> data ;
1736
+ int ret = 0 ;
1743
1737
1744
- ret = i2c_write_read_dt (& i2c ,
1745
- & iaddr , CONFIG_ETH_SAM_GMAC_MAC_I2C_INT_ADDRESS_SIZE ,
1746
- mac_addr , 6 );
1738
+ #if DT_INST_PROP (0 , zephyr_random_mac_address )
1739
+ gen_random_mac (dev_data -> mac_addr , ATMEL_OUI_B0 , ATMEL_OUI_B1 , ATMEL_OUI_B2 );
1740
+ #elif defined(CONFIG_NVMEM )
1741
+ const struct eth_sam_dev_cfg * const cfg = dev -> config ;
1747
1742
1748
- if (ret != 0 ) {
1749
- LOG_ERR ( "I2C: failed to read MAC addr" );
1750
- return ;
1743
+ if (nvmem_cell_is_ready ( & cfg -> mac_address_cell ) ) {
1744
+ ret = nvmem_cell_read ( & cfg -> mac_address_cell , dev_data -> mac_addr , 0 ,
1745
+ sizeof ( dev_data -> mac_addr )) ;
1751
1746
}
1752
- }
1753
1747
#endif
1754
1748
1755
- static void generate_mac (uint8_t mac_addr [6 ])
1756
- {
1757
- #if DT_INST_NODE_HAS_PROP (0 , mac_eeprom )
1758
- get_mac_addr_from_i2c_eeprom (mac_addr );
1759
- #elif DT_INST_PROP (0 , zephyr_random_mac_address )
1760
- gen_random_mac (mac_addr , ATMEL_OUI_B0 , ATMEL_OUI_B1 , ATMEL_OUI_B2 );
1761
- #endif
1749
+ return ret ;
1762
1750
}
1763
1751
1764
1752
static void phy_link_state_changed (const struct device * pdev ,
@@ -1836,7 +1824,11 @@ static void eth0_iface_init(struct net_if *iface)
1836
1824
return ;
1837
1825
}
1838
1826
1839
- generate_mac (dev_data -> mac_addr );
1827
+ result = generate_mac (dev );
1828
+ if (result < 0 ) {
1829
+ LOG_ERR ("Failed to generate MAC (%d)" , result );
1830
+ return ;
1831
+ }
1840
1832
1841
1833
LOG_INF ("MAC: %02x:%02x:%02x:%02x:%02x:%02x" ,
1842
1834
dev_data -> mac_addr [0 ], dev_data -> mac_addr [1 ],
@@ -2161,7 +2153,10 @@ static const struct eth_sam_dev_cfg eth0_config = {
2161
2153
.clock_cfg = SAM_DT_CLOCK_PMC_CFG (0 , DT_INST_PARENT (0 )),
2162
2154
#endif
2163
2155
.config_func = eth0_irq_config ,
2164
- .phy_dev = DEVICE_DT_GET (DT_INST_PHANDLE (0 , phy_handle ))
2156
+ .phy_dev = DEVICE_DT_GET (DT_INST_PHANDLE (0 , phy_handle )),
2157
+ #ifdef CONFIG_NVMEM
2158
+ .mac_address_cell = NVMEM_CELL_INST_GET_BY_NAME_OR (0 , mac_address , {0 }),
2159
+ #endif
2165
2160
};
2166
2161
2167
2162
static struct eth_sam_dev_data eth0_data = {
0 commit comments