Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Wenyou Yang | d9b23e2 | 2017-09-01 16:26:16 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2017 Microchip |
| 4 | * Wenyou Yang <wenyou.yang@microchip.com> |
Wenyou Yang | d9b23e2 | 2017-09-01 16:26:16 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
Simon Glass | cb3ef68 | 2019-11-14 12:57:50 -0700 | [diff] [blame] | 9 | #include <eeprom.h> |
Simon Glass | 9fb625c | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 10 | #include <env.h> |
Wenyou Yang | d9b23e2 | 2017-09-01 16:26:16 +0800 | [diff] [blame] | 11 | #include <i2c_eeprom.h> |
Simon Glass | 90526e9 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 12 | #include <net.h> |
Wenyou Yang | d9b23e2 | 2017-09-01 16:26:16 +0800 | [diff] [blame] | 13 | #include <netdev.h> |
| 14 | |
| 15 | int at91_set_ethaddr(int offset) |
| 16 | { |
| 17 | const int ETH_ADDR_LEN = 6; |
| 18 | unsigned char ethaddr[ETH_ADDR_LEN]; |
| 19 | const char *ETHADDR_NAME = "ethaddr"; |
| 20 | struct udevice *dev; |
| 21 | int ret; |
| 22 | |
| 23 | if (env_get(ETHADDR_NAME)) |
| 24 | return 0; |
| 25 | |
| 26 | ret = uclass_first_device_err(UCLASS_I2C_EEPROM, &dev); |
| 27 | if (ret) |
| 28 | return ret; |
| 29 | |
| 30 | ret = i2c_eeprom_read(dev, offset, ethaddr, 6); |
| 31 | if (ret) |
| 32 | return ret; |
| 33 | |
| 34 | if (is_valid_ethaddr(ethaddr)) |
| 35 | eth_env_set_enetaddr(ETHADDR_NAME, ethaddr); |
| 36 | |
| 37 | return 0; |
| 38 | } |
Eugen Hristev | 68d3ec5 | 2020-08-05 15:30:34 +0300 | [diff] [blame] | 39 | |
| 40 | /* this function will set eth1addr from a second eeprom, if available */ |
| 41 | int at91_set_eth1addr(int offset) |
| 42 | { |
| 43 | const int ETH_ADDR_LEN = 6; |
| 44 | unsigned char ethaddr[ETH_ADDR_LEN]; |
| 45 | /* configure eth1addr for second interface */ |
| 46 | const char *ETHADDR_NAME = "eth1addr"; |
| 47 | struct udevice *dev; |
| 48 | int ret; |
| 49 | |
| 50 | if (env_get(ETHADDR_NAME)) |
| 51 | return 0; |
| 52 | |
| 53 | /* first eeprom is retrieved, this is for the first interface */ |
| 54 | ret = uclass_first_device_err(UCLASS_I2C_EEPROM, &dev); |
| 55 | if (ret) |
| 56 | return ret; |
| 57 | |
| 58 | /* attempt to obtain a second eeprom device */ |
| 59 | ret = uclass_next_device(&dev); |
| 60 | if (ret) |
| 61 | return ret; |
| 62 | |
| 63 | ret = i2c_eeprom_read(dev, offset, ethaddr, 6); |
| 64 | if (ret) |
| 65 | return ret; |
| 66 | |
| 67 | if (is_valid_ethaddr(ethaddr)) |
| 68 | eth_env_set_enetaddr(ETHADDR_NAME, ethaddr); |
| 69 | |
| 70 | return 0; |
| 71 | } |