blob: 32c50b4f0f59c53e430dc648bc7cdd6e272c67e9 [file] [log] [blame]
Philip Oberfichtner62a09922022-07-26 15:04:50 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2022 Marek Vasut <marex@denx.de>
4 * Copyright 2022 DENX Software Engineering GmbH, Philip Oberfichtner <pro@denx.de>
5 */
6
Philip Oberfichtner62a09922022-07-26 15:04:50 +02007#include <dm.h>
8#include <i2c_eeprom.h>
9#include <net.h>
10
11#include "dh_common.h"
12
13bool dh_mac_is_in_env(const char *env)
14{
15 unsigned char enetaddr[6];
16
17 return eth_env_get_enetaddr(env, enetaddr);
18}
19
Marek Vasut29ab1a92024-03-12 22:15:58 +010020int dh_get_mac_is_enabled(const char *alias)
21{
22 ofnode node = ofnode_path(alias);
23
24 if (!ofnode_valid(node))
25 return -EINVAL;
26
27 if (!ofnode_is_enabled(node))
28 return -ENODEV;
29
30 return 0;
31}
32
Philip Oberfichtner62a09922022-07-26 15:04:50 +020033int dh_get_mac_from_eeprom(unsigned char *enetaddr, const char *alias)
34{
35 struct udevice *dev;
36 int ret;
37 ofnode node;
38
39 node = ofnode_path(alias);
40 if (!ofnode_valid(node)) {
41 printf("%s: ofnode for %s not found!", __func__, alias);
42 return -ENOENT;
43 }
44
45 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, node, &dev);
46 if (ret) {
47 printf("%s: Cannot find EEPROM! ret = %d\n", __func__, ret);
48 return ret;
49 }
50
51 ret = i2c_eeprom_read(dev, 0xfa, enetaddr, 0x6);
52 if (ret) {
53 printf("%s: Error reading EEPROM! ret = %d\n", __func__, ret);
54 return ret;
55 }
56
57 if (!is_valid_ethaddr(enetaddr)) {
58 printf("%s: Address read from EEPROM is invalid!\n", __func__);
59 return -EINVAL;
60 }
61
62 return 0;
63}
64
65__weak int dh_setup_mac_address(void)
66{
67 unsigned char enetaddr[6];
68
69 if (dh_mac_is_in_env("ethaddr"))
70 return 0;
71
Marek Vasut29ab1a92024-03-12 22:15:58 +010072 if (dh_get_mac_is_enabled("ethernet0"))
73 return 0;
74
Philip Oberfichtner62a09922022-07-26 15:04:50 +020075 if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0"))
76 return eth_env_set_enetaddr("ethaddr", enetaddr);
77
78 printf("%s: Unable to set mac address!\n", __func__);
79 return -ENXIO;
80}