// SPDX-License-Identifier: GPL-2.0-only1/*2* OF helpers for network devices.3*4* Initially copied out of arch/powerpc/kernel/prom_parse.c5*/6#include <linux/etherdevice.h>7#include <linux/kernel.h>8#include <linux/of_net.h>9#include <linux/of_platform.h>10#include <linux/platform_device.h>11#include <linux/phy.h>12#include <linux/export.h>13#include <linux/device.h>14#include <linux/nvmem-consumer.h>1516/**17* of_get_phy_mode - Get phy mode for given device_node18* @np: Pointer to the given device_node19* @interface: Pointer to the result20*21* The function gets phy interface string from property 'phy-mode' or22* 'phy-connection-type'. The index in phy_modes table is set in23* interface and 0 returned. In case of error interface is set to24* PHY_INTERFACE_MODE_NA and an errno is returned, e.g. -ENODEV.25*/26int of_get_phy_mode(struct device_node *np, phy_interface_t *interface)27{28const char *pm;29int err, i;3031*interface = PHY_INTERFACE_MODE_NA;3233err = of_property_read_string(np, "phy-mode", &pm);34if (err < 0)35err = of_property_read_string(np, "phy-connection-type", &pm);36if (err < 0)37return err;3839for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)40if (!strcasecmp(pm, phy_modes(i))) {41*interface = i;42return 0;43}4445return -ENODEV;46}47EXPORT_SYMBOL_GPL(of_get_phy_mode);4849static int of_get_mac_addr(struct device_node *np, const char *name, u8 *addr)50{51struct property *pp = of_find_property(np, name, NULL);5253if (pp && pp->length == ETH_ALEN && is_valid_ether_addr(pp->value)) {54memcpy(addr, pp->value, ETH_ALEN);55return 0;56}57return -ENODEV;58}5960int of_get_mac_address_nvmem(struct device_node *np, u8 *addr)61{62struct platform_device *pdev = of_find_device_by_node(np);63struct nvmem_cell *cell;64const void *mac;65size_t len;66int ret;6768/* Try lookup by device first, there might be a nvmem_cell_lookup69* associated with a given device.70*/71if (pdev) {72ret = nvmem_get_mac_address(&pdev->dev, addr);73put_device(&pdev->dev);74return ret;75}7677cell = of_nvmem_cell_get(np, "mac-address");78if (IS_ERR(cell))79return PTR_ERR(cell);8081mac = nvmem_cell_read(cell, &len);82nvmem_cell_put(cell);8384if (IS_ERR(mac))85return PTR_ERR(mac);8687if (len != ETH_ALEN || !is_valid_ether_addr(mac)) {88kfree(mac);89return -EINVAL;90}9192memcpy(addr, mac, ETH_ALEN);93kfree(mac);9495return 0;96}97EXPORT_SYMBOL(of_get_mac_address_nvmem);9899/**100* of_get_mac_address()101* @np: Caller's Device Node102* @addr: Pointer to a six-byte array for the result103*104* Search the device tree for the best MAC address to use. 'mac-address' is105* checked first, because that is supposed to contain to "most recent" MAC106* address. If that isn't set, then 'local-mac-address' is checked next,107* because that is the default address. If that isn't set, then the obsolete108* 'address' is checked, just in case we're using an old device tree. If any109* of the above isn't set, then try to get MAC address from nvmem cell named110* 'mac-address'.111*112* Note that the 'address' property is supposed to contain a virtual address of113* the register set, but some DTS files have redefined that property to be the114* MAC address.115*116* All-zero MAC addresses are rejected, because those could be properties that117* exist in the device tree, but were not set by U-Boot. For example, the118* DTS could define 'mac-address' and 'local-mac-address', with zero MAC119* addresses. Some older U-Boots only initialized 'local-mac-address'. In120* this case, the real MAC is in 'local-mac-address', and 'mac-address' exists121* but is all zeros.122*123* Return: 0 on success and errno in case of error.124*/125int of_get_mac_address(struct device_node *np, u8 *addr)126{127int ret;128129if (!np)130return -ENODEV;131132ret = of_get_mac_addr(np, "mac-address", addr);133if (!ret)134return 0;135136ret = of_get_mac_addr(np, "local-mac-address", addr);137if (!ret)138return 0;139140ret = of_get_mac_addr(np, "address", addr);141if (!ret)142return 0;143144return of_get_mac_address_nvmem(np, addr);145}146EXPORT_SYMBOL(of_get_mac_address);147148/**149* of_get_ethdev_address()150* @np: Caller's Device Node151* @dev: Pointer to netdevice which address will be updated152*153* Search the device tree for the best MAC address to use.154* If found set @dev->dev_addr to that address.155*156* See documentation of of_get_mac_address() for more information on how157* the best address is determined.158*159* Return: 0 on success and errno in case of error.160*/161int of_get_ethdev_address(struct device_node *np, struct net_device *dev)162{163u8 addr[ETH_ALEN];164int ret;165166ret = of_get_mac_address(np, addr);167if (!ret)168eth_hw_addr_set(dev, addr);169return ret;170}171EXPORT_SYMBOL(of_get_ethdev_address);172173174