Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Wenyou Yang | a3b84f4 | 2017-09-01 16:26:16 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2017 Microchip |
| 4 | * Wenyou Yang <wenyou.yang@microchip.com> |
Wenyou Yang | a3b84f4 | 2017-09-01 16:26:16 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
Wenyou Yang | a3b84f4 | 2017-09-01 16:26:16 +0800 | [diff] [blame] | 7 | #include <dm.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 8 | #include <env.h> |
Wenyou Yang | a3b84f4 | 2017-09-01 16:26:16 +0800 | [diff] [blame] | 9 | #include <i2c_eeprom.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 10 | #include <net.h> |
Wenyou Yang | a3b84f4 | 2017-09-01 16:26:16 +0800 | [diff] [blame] | 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 | } |
Eugen Hristev | fedbf30 | 2020-08-05 15:30:34 +0300 | [diff] [blame] | 37 | |
| 38 | /* this function will set eth1addr from a second eeprom, if available */ |
| 39 | int 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 Suchanek | 93ccce5 | 2022-10-12 21:58:00 +0200 | [diff] [blame] | 57 | ret = uclass_next_device_err(&dev); |
Eugen Hristev | fedbf30 | 2020-08-05 15:30:34 +0300 | [diff] [blame] | 58 | 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 | } |