blob: 97edb7a549d42d79307e046a84b3d2911db17ce2 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Wenyou Yanga3b84f42017-09-01 16:26:16 +08002/*
3 * Copyright (C) 2017 Microchip
4 * Wenyou Yang <wenyou.yang@microchip.com>
Wenyou Yanga3b84f42017-09-01 16:26:16 +08005 */
6
Wenyou Yanga3b84f42017-09-01 16:26:16 +08007#include <dm.h>
Simon Glass5e6201b2019-08-01 09:46:51 -06008#include <env.h>
Wenyou Yanga3b84f42017-09-01 16:26:16 +08009#include <i2c_eeprom.h>
Simon Glass274e0b02020-05-10 11:39:56 -060010#include <net.h>
Wenyou Yanga3b84f42017-09-01 16:26:16 +080011#include <netdev.h>
12
13int at91_set_ethaddr(int offset)
14{
15 const int ETH_ADDR_LEN = 6;
16 unsigned char ethaddr[ETH_ADDR_LEN];
17 const char *ETHADDR_NAME = "ethaddr";
18 struct udevice *dev;
19 int ret;
20
21 if (env_get(ETHADDR_NAME))
22 return 0;
23
24 ret = uclass_first_device_err(UCLASS_I2C_EEPROM, &dev);
25 if (ret)
26 return ret;
27
28 ret = i2c_eeprom_read(dev, offset, ethaddr, 6);
29 if (ret)
30 return ret;
31
32 if (is_valid_ethaddr(ethaddr))
33 eth_env_set_enetaddr(ETHADDR_NAME, ethaddr);
34
35 return 0;
36}
Eugen Hristevfedbf302020-08-05 15:30:34 +030037
38/* this function will set eth1addr from a second eeprom, if available */
39int at91_set_eth1addr(int offset)
40{
41 const int ETH_ADDR_LEN = 6;
42 unsigned char ethaddr[ETH_ADDR_LEN];
43 /* configure eth1addr for second interface */
44 const char *ETHADDR_NAME = "eth1addr";
45 struct udevice *dev;
46 int ret;
47
48 if (env_get(ETHADDR_NAME))
49 return 0;
50
51 /* first eeprom is retrieved, this is for the first interface */
52 ret = uclass_first_device_err(UCLASS_I2C_EEPROM, &dev);
53 if (ret)
54 return ret;
55
56 /* attempt to obtain a second eeprom device */
Michal Suchanek93ccce52022-10-12 21:58:00 +020057 ret = uclass_next_device_err(&dev);
Eugen Hristevfedbf302020-08-05 15:30:34 +030058 if (ret)
59 return ret;
60
61 ret = i2c_eeprom_read(dev, offset, ethaddr, 6);
62 if (ret)
63 return ret;
64
65 if (is_valid_ethaddr(ethaddr))
66 eth_env_set_enetaddr(ETHADDR_NAME, ethaddr);
67
68 return 0;
69}