uboot/board/dhelectronics/common/dh_common.c
<<
>>
Prefs
   1// 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
   7#include <common.h>
   8#include <dm.h>
   9#include <i2c_eeprom.h>
  10#include <net.h>
  11
  12#include "dh_common.h"
  13
  14bool dh_mac_is_in_env(const char *env)
  15{
  16        unsigned char enetaddr[6];
  17
  18        return eth_env_get_enetaddr(env, enetaddr);
  19}
  20
  21int dh_get_mac_from_eeprom(unsigned char *enetaddr, const char *alias)
  22{
  23        struct udevice *dev;
  24        int ret;
  25        ofnode node;
  26
  27        node = ofnode_path(alias);
  28        if (!ofnode_valid(node)) {
  29                printf("%s: ofnode for %s not found!", __func__, alias);
  30                return -ENOENT;
  31        }
  32
  33        ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, node, &dev);
  34        if (ret) {
  35                printf("%s: Cannot find EEPROM! ret = %d\n", __func__, ret);
  36                return ret;
  37        }
  38
  39        ret = i2c_eeprom_read(dev, 0xfa, enetaddr, 0x6);
  40        if (ret) {
  41                printf("%s: Error reading EEPROM! ret = %d\n", __func__, ret);
  42                return ret;
  43        }
  44
  45        if (!is_valid_ethaddr(enetaddr)) {
  46                printf("%s: Address read from EEPROM is invalid!\n", __func__);
  47                return -EINVAL;
  48        }
  49
  50        return 0;
  51}
  52
  53__weak int dh_setup_mac_address(void)
  54{
  55        unsigned char enetaddr[6];
  56
  57        if (dh_mac_is_in_env("ethaddr"))
  58                return 0;
  59
  60        if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0"))
  61                return eth_env_set_enetaddr("ethaddr", enetaddr);
  62
  63        printf("%s: Unable to set mac address!\n", __func__);
  64        return -ENXIO;
  65}
  66