Skip to content

Commit 1a5832d

Browse files
committed
drivers: ethernet: sam_gmac: Use NVMEM cell to get MAC address
Rework the Atmel SAM GMAC driver to read a MAC address from the provided NVMEM cell instead of using I2C commands directly. Signed-off-by: Pieter De Gendt <[email protected]>
1 parent fe37610 commit 1a5832d

File tree

11 files changed

+88
-79
lines changed

11 files changed

+88
-79
lines changed

boards/atmel/sam/sam_e70_xplained/Kconfig.defconfig

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,11 @@ if ETH_SAM_GMAC
77

88
# Read MAC address from AT24MAC402 EEPROM
99

10-
config ETH_SAM_GMAC_MAC_I2C_INT_ADDRESS
11-
default 0x9A
12-
13-
config ETH_SAM_GMAC_MAC_I2C_INT_ADDRESS_SIZE
14-
default 1
10+
config EEPROM
11+
default y
1512

16-
config ETH_SAM_GMAC_MAC_I2C_EEPROM
13+
config NVMEM
1714
default y
18-
select I2C
1915

2016
endif # ETH_SAM_GMAC
2117

boards/atmel/sam/sam_e70_xplained/sam_e70_xplained-common.dtsi

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,23 @@
8181
pinctrl-names = "default";
8282

8383
eeprom: eeprom@5f {
84-
compatible = "atmel,24mac402";
84+
compatible = "atmel,at24", "atmel,24mac402";
8585
reg = <0x5f>;
86+
size = <256>;
87+
pagesize = <16>;
88+
address-width = <8>;
89+
timeout = <5>;
90+
91+
nvmem-layout {
92+
compatible = "fixed-layout";
93+
#address-cells = <1>;
94+
#size-cells = <1>;
95+
96+
mac_address: mac-address@9a {
97+
reg = <0x9a 6>;
98+
#nvmem-cell-cells = <0>;
99+
};
100+
};
86101
};
87102
};
88103

@@ -128,7 +143,8 @@ zephyr_udc0: &usbhs {
128143
pinctrl-0 = <&gmac_rmii>;
129144
pinctrl-names = "default";
130145

131-
mac-eeprom = <&eeprom>;
146+
nvmem-cells = <&mac_address>;
147+
nvmem-cell-names = "mac-address";
132148
phy-handle = <&phy>;
133149
};
134150

boards/atmel/sam/sam_v71_xult/Kconfig.defconfig

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,11 @@ if ETH_SAM_GMAC
88

99
# Read MAC address from AT24MAC402 EEPROM
1010

11-
config ETH_SAM_GMAC_MAC_I2C_EEPROM
11+
config EEPROM
1212
default y
13-
select I2C
1413

15-
config ETH_SAM_GMAC_MAC_I2C_INT_ADDRESS
16-
default 0x9A
17-
18-
config ETH_SAM_GMAC_MAC_I2C_INT_ADDRESS_SIZE
19-
default 1
14+
config NVMEM
15+
default y
2016

2117
endif # ETH_SAM_GMAC
2218

boards/atmel/sam/sam_v71_xult/sam_v71_xult-common.dtsi

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,23 @@
198198
pinctrl-names = "default";
199199

200200
eeprom: eeprom@5f {
201-
compatible = "atmel,24mac402";
201+
compatible = "atmel,at24", "atmel,24mac402";
202202
reg = <0x5f>;
203+
size = <256>;
204+
pagesize = <16>;
205+
address-width = <8>;
206+
timeout = <5>;
207+
208+
nvmem-layout {
209+
compatible = "fixed-layout";
210+
#address-cells = <1>;
211+
#size-cells = <1>;
212+
213+
mac_address: mac-address@9a {
214+
reg = <0x9a 6>;
215+
#nvmem-cell-cells = <0>;
216+
};
217+
};
203218
};
204219
};
205220

@@ -253,7 +268,8 @@ zephyr_udc0: &usbhs {
253268
pinctrl-0 = <&gmac_rmii>;
254269
pinctrl-names = "default";
255270

256-
mac-eeprom = <&eeprom>;
271+
nvmem-cells = <&mac_address>;
272+
nvmem-cell-names = "mac-address";
257273
phy-handle = <&phy>;
258274
};
259275

boards/atmel/sam0/same54_xpro/Kconfig.defconfig

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,11 @@ if ETH_SAM_GMAC
88

99
# Read MAC address from AT24MAC402 EEPROM
1010

11-
config ETH_SAM_GMAC_MAC_I2C_INT_ADDRESS
12-
default 0x9A
13-
14-
config ETH_SAM_GMAC_MAC_I2C_INT_ADDRESS_SIZE
15-
default 1
11+
config EEPROM
12+
default y
1613

17-
config ETH_SAM_GMAC_MAC_I2C_EEPROM
14+
config NVMEM
1815
default y
19-
select I2C
2016

2117
endif # ETH_SAM_GMAC
2218

boards/atmel/sam0/same54_xpro/same54_xpro.dts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,23 @@
116116
pinctrl-names = "default";
117117

118118
eeprom: eeprom@5e {
119-
compatible = "atmel,24mac402";
119+
compatible = "atmel,at24", "atmel,24mac402";
120120
reg = <0x5e>;
121+
size = <256>;
122+
pagesize = <16>;
123+
address-width = <8>;
124+
timeout = <5>;
125+
126+
nvmem-layout {
127+
compatible = "fixed-layout";
128+
#address-cells = <1>;
129+
#size-cells = <1>;
130+
131+
mac_address: mac-address@9a {
132+
reg = <0x9a 6>;
133+
#nvmem-cell-cells = <0>;
134+
};
135+
};
121136
};
122137
};
123138

@@ -158,7 +173,8 @@ zephyr_udc0: &usb0 {
158173
pinctrl-0 = <&gmac_rmii>;
159174
pinctrl-names = "default";
160175

161-
mac-eeprom = <&eeprom>;
176+
nvmem-cells = <&mac_address>;
177+
nvmem-cell-names = "mac-address";
162178
phy-handle = <&phy>;
163179
};
164180

drivers/ethernet/Kconfig.sam_gmac

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,6 @@ config ETH_SAM_GMAC_BUF_RX_COUNT
7272
fit at least two Ethernet frames: one being received by the GMAC module
7373
and the other being processed by the higher layer networking stack.
7474

75-
config ETH_SAM_GMAC_MAC_I2C_EEPROM
76-
bool "Read from an I2C EEPROM"
77-
help
78-
Read MAC address from an I2C EEPROM.
79-
80-
if ETH_SAM_GMAC_MAC_I2C_EEPROM
81-
82-
config ETH_SAM_GMAC_MAC_I2C_INT_ADDRESS
83-
hex "I2C EEPROM internal address"
84-
range 0 0xffffffff
85-
help
86-
Internal address of the EEPROM chip where the MAC address is stored.
87-
Chips with 1 to 4 byte internal address size are supported. Address
88-
size has to be configured in a separate Kconfig option.
89-
90-
config ETH_SAM_GMAC_MAC_I2C_INT_ADDRESS_SIZE
91-
int "I2C EEPROM internal address size"
92-
default 1
93-
range 1 4
94-
help
95-
Size (in bytes) of the internal EEPROM address.
96-
97-
endif # ETH_SAM_GMAC_MAC_I2C_EEPROM
98-
9975
config PTP_CLOCK_SAM_GMAC
10076
bool "SAM GMAC PTP clock driver support"
10177
default y

drivers/ethernet/eth_sam_gmac.c

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
3333

3434
#include <zephyr/kernel.h>
3535
#include <zephyr/device.h>
36+
#include <zephyr/nvmem.h>
3637
#include <zephyr/sys/__assert.h>
3738
#include <zephyr/sys/barrier.h>
3839
#include <zephyr/sys/util.h>
@@ -1729,36 +1730,23 @@ static int eth_initialize(const struct device *dev)
17291730
return retval;
17301731
}
17311732

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)
17341734
{
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;
17431737

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;
17471742

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));
17511746
}
1752-
}
17531747
#endif
17541748

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;
17621750
}
17631751

17641752
static void phy_link_state_changed(const struct device *pdev,
@@ -1836,7 +1824,11 @@ static void eth0_iface_init(struct net_if *iface)
18361824
return;
18371825
}
18381826

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+
}
18401832

18411833
LOG_INF("MAC: %02x:%02x:%02x:%02x:%02x:%02x",
18421834
dev_data->mac_addr[0], dev_data->mac_addr[1],
@@ -2161,7 +2153,10 @@ static const struct eth_sam_dev_cfg eth0_config = {
21612153
.clock_cfg = SAM_DT_CLOCK_PMC_CFG(0, DT_INST_PARENT(0)),
21622154
#endif
21632155
.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
21652160
};
21662161

21672162
static struct eth_sam_dev_data eth0_data = {

drivers/ethernet/eth_sam_gmac_priv.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ struct eth_sam_dev_cfg {
263263
const struct pinctrl_dev_config *pcfg;
264264
void (*config_func)(void);
265265
const struct device *phy_dev;
266+
#ifdef CONFIG_NVMEM
267+
struct nvmem_cell mac_address_cell;
268+
#endif
266269
};
267270

268271
/* Device run time data */

dts/bindings/ethernet/atmel,gmac-common.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
include:
66
- name: ethernet-controller.yaml
77
- name: pinctrl-device.yaml
8+
- name: nvmem-consumer.yaml
89

910
properties:
1011
phy-handle:

0 commit comments

Comments
 (0)