[U-Boot] [PATCHv6 12/28] net: Add ability to set MAC address via EEPROM
Olliver Schinagl
oliver at schinagl.nl
Mon May 15 08:02:28 UTC 2017
This patch allows Kconfig to enable and set parameters to make it
possible to read the MAC address from an EEPROM. The net core layer then
uses this information to read MAC addresses from this EEPROM.
Besides the various tuneables as to how to access the eeprom (bus,
address, addressing mode/length, 2 configurable that are EEPROM generic
(e.g. SPI or some other form of access) which are:
NET_ETHADDR_EEPROM_OFFSET, indicating where in the EEPROM the start of
the MAC address is. The default is 8 allowing for 8 bytes before the MAC
for other purposes (header MAGIC for example).
NET_ETHADDR_EEPROM_CRC8, indicating the MAC is appended with a CRC8-CCIT
checksum that should be verified.
Currently only I2C eeproms have been tested and thus only those options
are available, but shouldn't be a limit. NET_ETHADDR_EEPROM_SPI can be
just as created and added.
The code currently first checks if there is a non-zero MAC address in
the eeprom. If that fails to be the case, the read_rom_hwaddr can be
used by a board to supply the MAC in other ways.
If both these fails, the other code is still in place to query the
environent, which then can be used to override the hardware supplied
data.
Signed-off-by: Olliver Schinagl <oliver at schinagl.nl>
---
arch/arm/mach-sunxi/Kconfig | 8 +--
configs/A10-OLinuXino-Lime_defconfig | 1 -
configs/A20-OLinuXino-Lime2-eMMC_defconfig | 1 -
configs/A20-OLinuXino-Lime2_defconfig | 1 -
configs/A20-OLinuXino-Lime_defconfig | 1 -
configs/A20-OLinuXino_MICRO_defconfig | 1 -
doc/README.enetaddr | 95 ++++++++++++++++++++++++++++++
include/net.h | 14 +++++
net/Kconfig | 59 +++++++++++++++++++
net/eth-uclass.c | 42 +++++++------
net/eth_common.c | 37 ++++++++++++
net/eth_legacy.c | 2 +
12 files changed, 231 insertions(+), 31 deletions(-)
diff --git a/arch/arm/mach-sunxi/Kconfig b/arch/arm/mach-sunxi/Kconfig
index be060b65dc..cf5af4fe18 100644
--- a/arch/arm/mach-sunxi/Kconfig
+++ b/arch/arm/mach-sunxi/Kconfig
@@ -479,14 +479,14 @@ config I2C0_ENABLE
config I2C1_ENABLE
bool "Enable I2C/TWI controller 1"
- default n
+ default y if (NET_ETHADDR_EEPROM_I2C_BUS = 1)
select CMD_I2C
---help---
See I2C0_ENABLE help text.
config I2C2_ENABLE
bool "Enable I2C/TWI controller 2"
- default n
+ default y if (NET_ETHADDR_EEPROM_I2C_BUS = 2)
select CMD_I2C
---help---
See I2C0_ENABLE help text.
@@ -494,7 +494,7 @@ config I2C2_ENABLE
if MACH_SUN6I || MACH_SUN7I
config I2C3_ENABLE
bool "Enable I2C/TWI controller 3"
- default n
+ default y if (NET_ETHADDR_EEPROM_I2C_BUS = 3)
select CMD_I2C
---help---
See I2C0_ENABLE help text.
@@ -513,7 +513,7 @@ endif
if MACH_SUN7I
config I2C4_ENABLE
bool "Enable I2C/TWI controller 4"
- default n
+ default y if (NET_ETHADDR_EEPROM_I2C_BUS = 4)
select CMD_I2C
---help---
See I2C0_ENABLE help text.
diff --git a/configs/A10-OLinuXino-Lime_defconfig b/configs/A10-OLinuXino-Lime_defconfig
index ec16a44fbd..d4513144eb 100644
--- a/configs/A10-OLinuXino-Lime_defconfig
+++ b/configs/A10-OLinuXino-Lime_defconfig
@@ -5,7 +5,6 @@ CONFIG_DRAM_CLK=480
CONFIG_DRAM_EMR1=4
CONFIG_SYS_CLK_FREQ=912000000
CONFIG_MMC0_CD_PIN="PH1"
-CONFIG_I2C1_ENABLE=y
CONFIG_SATAPWR="PC3"
CONFIG_DEFAULT_DEVICE_TREE="sun4i-a10-olinuxino-lime"
CONFIG_AHCI=y
diff --git a/configs/A20-OLinuXino-Lime2-eMMC_defconfig b/configs/A20-OLinuXino-Lime2-eMMC_defconfig
index 76525f4603..fd743a5f45 100644
--- a/configs/A20-OLinuXino-Lime2-eMMC_defconfig
+++ b/configs/A20-OLinuXino-Lime2-eMMC_defconfig
@@ -6,7 +6,6 @@ CONFIG_MMC0_CD_PIN="PH1"
CONFIG_MMC_SUNXI_SLOT_EXTRA=2
CONFIG_USB0_VBUS_PIN="PC17"
CONFIG_USB0_VBUS_DET="PH5"
-CONFIG_I2C1_ENABLE=y
CONFIG_SATAPWR="PC3"
CONFIG_DEFAULT_DEVICE_TREE="sun7i-a20-olinuxino-lime2-emmc"
CONFIG_AHCI=y
diff --git a/configs/A20-OLinuXino-Lime2_defconfig b/configs/A20-OLinuXino-Lime2_defconfig
index 14a51ef3f9..a932ec7b7b 100644
--- a/configs/A20-OLinuXino-Lime2_defconfig
+++ b/configs/A20-OLinuXino-Lime2_defconfig
@@ -5,7 +5,6 @@ CONFIG_DRAM_CLK=384
CONFIG_MMC0_CD_PIN="PH1"
CONFIG_USB0_VBUS_PIN="PC17"
CONFIG_USB0_VBUS_DET="PH5"
-CONFIG_I2C1_ENABLE=y
CONFIG_SATAPWR="PC3"
CONFIG_DEFAULT_DEVICE_TREE="sun7i-a20-olinuxino-lime2"
CONFIG_AHCI=y
diff --git a/configs/A20-OLinuXino-Lime_defconfig b/configs/A20-OLinuXino-Lime_defconfig
index 3fdf7c64aa..41667384f4 100644
--- a/configs/A20-OLinuXino-Lime_defconfig
+++ b/configs/A20-OLinuXino-Lime_defconfig
@@ -3,7 +3,6 @@ CONFIG_ARCH_SUNXI=y
CONFIG_MACH_SUN7I=y
CONFIG_DRAM_CLK=384
CONFIG_MMC0_CD_PIN="PH1"
-CONFIG_I2C1_ENABLE=y
CONFIG_SATAPWR="PC3"
CONFIG_DEFAULT_DEVICE_TREE="sun7i-a20-olinuxino-lime"
CONFIG_AHCI=y
diff --git a/configs/A20-OLinuXino_MICRO_defconfig b/configs/A20-OLinuXino_MICRO_defconfig
index ea7bc73dda..b1f80835c9 100644
--- a/configs/A20-OLinuXino_MICRO_defconfig
+++ b/configs/A20-OLinuXino_MICRO_defconfig
@@ -5,7 +5,6 @@ CONFIG_DRAM_CLK=384
CONFIG_MMC0_CD_PIN="PH1"
CONFIG_MMC3_CD_PIN="PH11"
CONFIG_MMC_SUNXI_SLOT_EXTRA=3
-CONFIG_I2C1_ENABLE=y
CONFIG_VIDEO_VGA=y
CONFIG_SATAPWR="PB8"
CONFIG_DEFAULT_DEVICE_TREE="sun7i-a20-olinuxino-micro"
diff --git a/doc/README.enetaddr b/doc/README.enetaddr
index 50e4899787..6f51137710 100644
--- a/doc/README.enetaddr
+++ b/doc/README.enetaddr
@@ -47,6 +47,101 @@ Correct flow of setting up the MAC address (summarized):
Previous behavior had the MAC address always being programmed into hardware
in the device's init() function.
+--------
+ EEPROM
+--------
+
+Boards may come with an EEPROM specifically to store configuration bits, such
+as a MAC address. Using CONFIG_NET_ETHADDR_EEPROM enables this feature.
+Depending on the board, the EEPROM may be connected on various methods, but
+currently, only the I2C bus can be used via CONFIG_NET_ETHADDR_EEPROM_I2C.
+
+The following config options are available,
+CONFIG_NET_ETHADDR_EEPROM_I2C_BUS is the I2C bus on which the eeprom is present.
+CONFIG_NET_ETHADDR_EEPROM_I2C_ADDR sets the address of the EEPROM, which
+defaults to the very common 0x50. Small size EEPROM's generally use single byte
+addressing but larger EEPROM's may use double byte addressing, which can be
+configured using CONFIG_NET_ETHADDR_EEPROM_ADDRLEN.
+
+Within the EEPROM, the MAC address can be stored on any arbitrary offset,
+CONFIG_NET_ETHADDR_EEPROM_OFFSET sets this to 8 as a default however, allowing
+the first 8 bytes to be used for an optional data, for example a configuration
+struct where the mac address is part of.
+
+Appending the 6 (ARP_HLEN) bytes is a CRC8 byte over the previous ARP_HLEN
+bytes. Whether to check this CRC8 or not is dependent on
+CONFIG_NET_ETHADDR_EEPROM_CRC8.
+
+To keep things nicely aligned, a final 'reserved' byte is added to the mac
+address + crc8 combo.
+
+A board may want to store more information in its eeprom, using the following
+example layout, this can be achieved.
+
+struct mac_addr {
+ uint8_t mac[ARP_HLEN];
+ uint8_t crc8;
+ uint8_t reserved;
+};
+
+struct config_eeprom {
+ uint32_t magic;
+ uint8_t version;
+ uint8_t reserved[2];
+ uint8_t mac_cnt;
+ struct mac_addr[mac_cnt];
+};
+
+Filling this in:
+struct config_eeprom eeprom = {
+ .magic = { 'M', 'g', 'i', 'c' },
+ .reserved = { 0x00, 0x00 },
+ .mac_cnt = 2,
+ .mac_addr = {
+ {
+ .mac = {
+ 0x01, 0x23, 0x45,
+ 0x67, 0x89, 0xab,
+ },
+ .crc8 = 0xbe,
+ .reserved = 0x00,
+ }, {
+ .mac = {
+ 0xba, 0x98, 0x76,
+ 0x54, 0x32, 0x10,
+ },
+ .crc8 = 0x82,
+ .reserved = 0x00,
+ },
+ },
+};
+
+The eeprom content would look like this.
+
+00000000 4d 67 69 63 01 00 00 02 01 23 45 67 89 ab be 00 |Mgic.....#Eg....|
+00000010 ba 98 76 54 32 10 82 00 |..vT2...|
+
+This can be done from linux using the i2c-tools:
+
+i2cset I2CBUS 0x50 0x08 0x01
+i2cset I2CBUS 0x50 0x09 0x23
+i2cset I2CBUS 0x50 0x0a 0x45
+i2cset I2CBUS 0x50 0x0b 0x67
+i2cset I2CBUS 0x50 0x0c 0x89
+i2cset I2CBUS 0x50 0x0d 0xab
+i2cset I2CBUS 0x50 0x0e 0xbe
+
+Alternativly this can be done from the u-boot console as:
+
+u-boot> i2c dev 1
+u-boot> i2c md 0x50 0x08 0x01
+u-boot> i2c md 0x50 0x09 0x23
+u-boot> i2c md 0x50 0x0a 0x45
+u-boot> i2c md 0x50 0x0b 0x67
+u-boot> i2c md 0x50 0x0c 0x89
+u-boot> i2c md 0x50 0x0d 0xab
+u-boot> i2c md 0x50 0x0e 0xbe
+
-------
Usage
-------
diff --git a/include/net.h b/include/net.h
index 00677a0a89..392dc95fdc 100644
--- a/include/net.h
+++ b/include/net.h
@@ -248,6 +248,20 @@ int eth_getenv_enetaddr(const char *name, uchar *enetaddr);
int eth_setenv_enetaddr(const char *name, const uchar *enetaddr);
/**
+ * eeprom_read_enetaddr() - Read the hardware address from an eeprom
+ *
+ * This function tries to read the MAC address from an eeprom as can be read
+ * in docs/README.enetaddr.
+ *
+ * @index: index of the interface to get the hwaddr for
+ * @enetaddr: pointer for the found hwaddr. Needs to be atleast ARP_HLEN
+ * @return: 0 on success, non-zero is error status. Additionally hwaddr
+ * is set to 00:00:00:00:00. This is also the case if
+ * CONFIG_NET_ETHADDR_EEPROM is not set.
+ */
+int eeprom_read_enetaddr(const int index, unsigned char *enetaddr);
+
+/**
* eth_setenv_enetaddr_by_index() - set the MAC address environment variable
*
* This sets up an environment variable with the given MAC address (@enetaddr).
diff --git a/net/Kconfig b/net/Kconfig
index 414c5497c7..ed7706c428 100644
--- a/net/Kconfig
+++ b/net/Kconfig
@@ -7,6 +7,65 @@ menuconfig NET
if NET
+config NET_ETHADDR_EEPROM
+ bool "Get ethaddr from eeprom"
+ help
+ Selecting this will try to get the Ethernet address from an onboard
+ EEPROM and set into the environment if and only if the environment
+ does currently not already hold a MAC address. For more information
+ see doc/README.enetaddr.
+
+config NET_ETHADDR_EEPROM_I2C
+ depends on NET_ETHADDR_EEPROM
+ bool "EEPROM on I2C bus"
+ help
+ This switch enables checks for an EEPROM on the I2C bus. Naturally
+ this will only work if there is an actual EEPROM connected on the
+ I2C bus and the bus and device are properly configured via the
+ options below.
+
+config NET_ETHADDR_EEPROM_I2C_BUS
+ depends on NET_ETHADDR_EEPROM_I2C
+ int "I2C bus"
+ default 0
+ help
+ Select the bus on which the EEPROM is present, defaults to bus 0.
+ Remember to also make the selected bus available via I2Cn_ENABLE.
+
+config NET_ETHADDR_EEPROM_I2C_ADDR
+ depends on NET_ETHADDR_EEPROM_I2C
+ hex "EEPROM address"
+ default 0x50
+ help
+ Select the address of the EEPROM, defaults to address 0x50.
+
+config NET_ETHADDR_EEPROM_I2C_ADDRLEN
+ depends on NET_ETHADDR_EEPROM_I2C
+ int "EEPROM address length"
+ default 1
+ help
+ Number of bytes to be used for the I2C address length. Typically 1,
+ 2 for large memories, 0 for register type devices with only one
+ register.
+
+config NET_ETHADDR_EEPROM_OFFSET
+ depends on NET_ETHADDR_EEPROM
+ hex "EEPROM offset"
+ default 8
+ help
+ Select the byte offset of the MAC address within the page,
+ defaults to byte 8.
+
+config NET_ETHADDR_EEPROM_CRC8
+ depends on NET_ETHADDR_EEPROM
+ bool "Check CRC8 of MAC"
+ default y
+ help
+ Optionally, it is possible to run a CRC-8-CCITT check on the MAC
+ address. To do so, the MAC address is stored with a CRC8 byte append.
+ This option enables the CRC check of the MAC address against the CRC
+ byte.
+
config NET_RANDOM_ETHADDR
bool "Random ethaddr if unset"
select LIB_RAND
diff --git a/net/eth-uclass.c b/net/eth-uclass.c
index 781376955a..02ee926f74 100644
--- a/net/eth-uclass.c
+++ b/net/eth-uclass.c
@@ -459,7 +459,8 @@ static int eth_post_probe(struct udevice *dev)
{
struct eth_device_priv *priv = dev->uclass_priv;
struct eth_pdata *pdata = dev->platdata;
- unsigned char env_enetaddr[ARP_HLEN];
+ unsigned char env_enetaddr[ARP_HLEN] = { 0x00 };
+ int ret;
#if defined(CONFIG_NEEDS_MANUAL_RELOC)
struct eth_ops *ops = eth_get_ops(dev);
@@ -490,38 +491,35 @@ static int eth_post_probe(struct udevice *dev)
#endif
priv->state = ETH_STATE_INIT;
+ pdata->enetaddr_src = ENETADDR_SRC_UNKNOWN;
+
+ /* Check if the device has a MAC address in EEPROM */
+ ret = eeprom_read_enetaddr(dev->seq, pdata->enetaddr);
+ if (!ret && !is_zero_ethaddr(pdata->enetaddr))
+ pdata->enetaddr_src = ENETADDR_SRC_EEPROM;
+ else
+ /* Check if the device driver has a MAC address */
+ if (eth_get_ops(dev)->read_rom_hwaddr) {
+ eth_get_ops(dev)->read_rom_hwaddr(dev);
+ pdata->enetaddr_src = ENETADDR_SRC_DRIVER;
+ }
- /* Check if the device has a MAC address in ROM */
- if (eth_get_ops(dev)->read_rom_hwaddr)
- eth_get_ops(dev)->read_rom_hwaddr(dev);
-
+ /* Check the environment for a MAC address */
eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
if (!is_zero_ethaddr(env_enetaddr)) {
- if (!is_zero_ethaddr(pdata->enetaddr) &&
- memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
- printf("\nWarning: %s MAC addresses don't match:\n",
- dev->name);
- printf("Address in ROM is %pM\n",
- pdata->enetaddr);
- printf("Address in environment is %pM\n",
- env_enetaddr);
- }
-
- /* Override the ROM MAC address */
+ /* Override the (EEP)ROM MAC address */
memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
+ pdata->enetaddr_src = ENETADDR_SRC_ENV;
} else if (is_valid_ethaddr(pdata->enetaddr)) {
eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
- printf("\nWarning: %s using MAC address from ROM\n",
- dev->name);
} else if (is_zero_ethaddr(pdata->enetaddr) ||
!is_valid_ethaddr(pdata->enetaddr)) {
+ /* No valid MAC addresses found, fall back */
#ifdef CONFIG_NET_RANDOM_ETHADDR
net_random_ethaddr(pdata->enetaddr);
- printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
- dev->name, dev->seq, pdata->enetaddr);
+ pdata->enetaddr_src = ENETADDR_SRC_RANDOM;
#else
- printf("\nError: %s address not set.\n",
- dev->name);
+ pdata->enetaddr_src = ENETADDR_SRC_NONE;
return -EINVAL;
#endif
}
diff --git a/net/eth_common.c b/net/eth_common.c
index 0fe4d260d8..24488746c9 100644
--- a/net/eth_common.c
+++ b/net/eth_common.c
@@ -8,10 +8,47 @@
#include <common.h>
#include <dm.h>
+#include <i2c.h>
#include <miiphy.h>
#include <net.h>
#include "eth_internal.h"
+int eeprom_read_enetaddr(const int index, unsigned char *enetaddr)
+{
+ uint8_t eeprom[ARP_HLEN + 1] = { 0x00 };
+#if defined(CONFIG_NET_ETHADDR_EEPROM) && defined(CONFIG_NET_ETHADDR_EEPROM_I2C)
+ int old_i2c_bus;
+
+ if ((index < 0) && !enetaddr)
+ return -ENOSYS;
+
+ old_i2c_bus = i2c_get_bus_num();
+ if (old_i2c_bus != CONFIG_NET_ETHADDR_EEPROM_I2C_BUS)
+ i2c_set_bus_num(CONFIG_NET_ETHADDR_EEPROM_I2C_BUS);
+ /* Skip in blocks of 8 (ARP + CRC8 + pad), but read 7 from the eeprom */
+ if (i2c_read(CONFIG_NET_ETHADDR_EEPROM_I2C_ADDR,
+ CONFIG_NET_ETHADDR_EEPROM_OFFSET + (index * (ARP_HLEN + 2)),
+ CONFIG_NET_ETHADDR_EEPROM_I2C_ADDRLEN,
+ eeprom, ARP_HLEN + 1)) {
+ i2c_set_bus_num(old_i2c_bus);
+ puts("Could not read I2C EEPROM or EEPROM missing on device: ");
+ return -ENOSYS;
+ }
+ i2c_set_bus_num(old_i2c_bus);
+
+#ifdef CONFIG_NET_ETHADDR_EEPROM_CRC8
+ if (crc8(0, eeprom, ARP_HLEN) != eeprom[ARP_HLEN]) {
+ puts("CRC error on MAC address from I2C EEPROM on device: ");
+ return -EINVAL;
+ }
+#endif
+#endif
+
+ memcpy(enetaddr, eeprom, ARP_HLEN);
+
+ return 0;
+}
+
void eth_parse_enetaddr(const char *addr, uchar *enetaddr)
{
char *end;
diff --git a/net/eth_legacy.c b/net/eth_legacy.c
index e4bd0f4c1a..4276058800 100644
--- a/net/eth_legacy.c
+++ b/net/eth_legacy.c
@@ -137,6 +137,8 @@ int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
unsigned char env_enetaddr[ARP_HLEN];
int ret = 0;
+ eeprom_read_enetaddr(eth_number, dev->enetaddr);
+
eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
if (!is_zero_ethaddr(env_enetaddr)) {
--
2.11.0
More information about the U-Boot
mailing list