[U-Boot] [PATCH 09/14] net: Add ability to set MAC address via EEPROM

Joe Hershberger joe.hershberger at gmail.com
Fri Dec 9 19:11:47 CET 2016


On Fri, Dec 2, 2016 at 4:12 AM, Olliver Schinagl <oliver at schinagl.nl> wrote:
> Hey Joe,
>
>
>
> On 30-11-16 21:00, Joe Hershberger wrote:
>>
>> On Fri, Nov 25, 2016 at 9:30 AM, Olliver Schinagl <oliver at schinagl.nl>
>> wrote:
>>>
>>> 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>
>>> ---
>>>   doc/README.enetaddr | 99
>>> +++++++++++++++++++++++++++++++++++++++++++++++++++++
>>>   include/net.h       | 14 ++++++++
>>>   net/Kconfig         | 59 +++++++++++++++++++++++++++++++
>>>   net/eth-uclass.c    |  9 +++--
>>>   net/eth_common.c    | 34 ++++++++++++++++++
>>>   net/eth_legacy.c    |  2 ++
>>>   6 files changed, 214 insertions(+), 3 deletions(-)
>>>

...

>>> diff --git a/net/eth_common.c b/net/eth_common.c
>>> index 079be89..e0d8b62 100644
>>> --- a/net/eth_common.c
>>> +++ b/net/eth_common.c
>>> @@ -8,10 +8,44 @@
>>>
>>>   #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)
>>
>> Since it is easily reasonable that SPI PROM is a likely useful
>> support, why not keep the layout stuff separate from the I2C stuff so
>> that it is trivial to plug in a different bus later? It will also make
>> the code clearer by untangling these.
>
>
> I strongly agree, but I recommend a follow up patch series (and thus merge
> this as is for now) to use Maxime's EEPROM framework patches. So then this
> gets replaced by simple read from eeprom.
>
> So yes, I have contemplated in splitting it up now and have a simply
> read_from_i2c() kind of function, I figured this gets solved elsewhere
> anyway.
>
> Additionally, the layout stuff would ideally be replaced by Igor (i think it
> was) eeprom layout framework (if those two combine) which solves both
> problems in one go.
>
> Or you want to see it split now as the other is a bad plan (tm)?

It's fine to wait if there is a plan going forward with a dependency
that might make this throw-away work.

> Olliver
>
>>
>>> +       int old_i2c_bus;
>>> +
>>> +       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 the 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 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 bf4de37..8fb5844 100644
>>> --- a/net/eth_legacy.c
>>> +++ b/net/eth_legacy.c
>>> @@ -136,6 +136,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.10.2
>>>
>>> _______________________________________________
>>> U-Boot mailing list
>>> U-Boot at lists.denx.de
>>> http://lists.denx.de/mailman/listinfo/u-boot
>
>


More information about the U-Boot mailing list