xref: /openbmc/u-boot/board/atmel/common/mac_eeprom.c (revision d9b23e26)
1 /*
2  * Copyright (C) 2017 Microchip
3  *		      Wenyou Yang <wenyou.yang@microchip.com>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <dm.h>
10 #include <i2c_eeprom.h>
11 #include <netdev.h>
12 
13 int 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 }
37