Mathieu Othacehe | 54e1aa2 | 2023-12-29 11:55:23 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2023 Variscite Ltd. |
| 4 | */ |
| 5 | #include <net.h> |
| 6 | #include <miiphy.h> |
| 7 | #include <env.h> |
| 8 | #include "../common/imx9_eeprom.h" |
| 9 | |
| 10 | #define CHAR_BIT 8 |
| 11 | |
| 12 | static u64 mac2int(const u8 hwaddr[]) |
| 13 | { |
| 14 | u8 i; |
| 15 | u64 ret = 0; |
| 16 | const u8 *p = hwaddr; |
| 17 | |
| 18 | for (i = 6; i > 0; i--) |
| 19 | ret |= (u64)*p++ << (CHAR_BIT * (i - 1)); |
| 20 | |
| 21 | return ret; |
| 22 | } |
| 23 | |
| 24 | static void int2mac(const u64 mac, u8 *hwaddr) |
| 25 | { |
| 26 | u8 i; |
| 27 | u8 *p = hwaddr; |
| 28 | |
| 29 | for (i = 6; i > 0; i--) |
| 30 | *p++ = mac >> (CHAR_BIT * (i - 1)); |
| 31 | } |
| 32 | |
| 33 | int var_setup_mac(struct var_eeprom *eeprom) |
| 34 | { |
| 35 | int ret; |
| 36 | unsigned char enetaddr[6]; |
| 37 | u64 addr; |
| 38 | unsigned char enet1addr[6]; |
| 39 | |
| 40 | ret = eth_env_get_enetaddr("ethaddr", enetaddr); |
| 41 | if (ret) |
| 42 | return 0; |
| 43 | |
| 44 | ret = var_eeprom_get_mac(eeprom, enetaddr); |
| 45 | if (ret) |
| 46 | return ret; |
| 47 | |
| 48 | if (!is_valid_ethaddr(enetaddr)) |
| 49 | return -EINVAL; |
| 50 | |
| 51 | eth_env_set_enetaddr("ethaddr", enetaddr); |
| 52 | |
| 53 | addr = mac2int(enetaddr); |
| 54 | int2mac(addr + 1, enet1addr); |
| 55 | eth_env_set_enetaddr("eth1addr", enet1addr); |
| 56 | |
| 57 | return 0; |
| 58 | } |