xref: /openbmc/linux/net/core/of_net.c (revision 9ed319e411915e882bb4ed99be3ae78667a70022)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * OF helpers for network devices.
4  *
5  * Initially copied out of arch/powerpc/kernel/prom_parse.c
6  */
7 #include <linux/etherdevice.h>
8 #include <linux/kernel.h>
9 #include <linux/of_net.h>
10 #include <linux/of_platform.h>
11 #include <linux/phy.h>
12 #include <linux/export.h>
13 #include <linux/device.h>
14 #include <linux/nvmem-consumer.h>
15 
16 /**
17  * of_get_phy_mode - Get phy mode for given device_node
18  * @np:	Pointer to the given device_node
19  * @interface: Pointer to the result
20  *
21  * The function gets phy interface string from property 'phy-mode' or
22  * 'phy-connection-type'. The index in phy_modes table is set in
23  * interface and 0 returned. In case of error interface is set to
24  * PHY_INTERFACE_MODE_NA and an errno is returned, e.g. -ENODEV.
25  */
26 int of_get_phy_mode(struct device_node *np, phy_interface_t *interface)
27 {
28 	const char *pm;
29 	int err, i;
30 
31 	*interface = PHY_INTERFACE_MODE_NA;
32 
33 	err = of_property_read_string(np, "phy-mode", &pm);
34 	if (err < 0)
35 		err = of_property_read_string(np, "phy-connection-type", &pm);
36 	if (err < 0)
37 		return err;
38 
39 	for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
40 		if (!strcasecmp(pm, phy_modes(i))) {
41 			*interface = i;
42 			return 0;
43 		}
44 
45 	return -ENODEV;
46 }
47 EXPORT_SYMBOL_GPL(of_get_phy_mode);
48 
49 static int of_get_mac_addr(struct device_node *np, const char *name, u8 *addr)
50 {
51 	struct property *pp = of_find_property(np, name, NULL);
52 
53 	if (pp && pp->length == ETH_ALEN && is_valid_ether_addr(pp->value)) {
54 		memcpy(addr, pp->value, ETH_ALEN);
55 		return 0;
56 	}
57 	return -ENODEV;
58 }
59 
60 static int of_get_mac_addr_nvmem(struct device_node *np, u8 *addr)
61 {
62 	struct platform_device *pdev = of_find_device_by_node(np);
63 	struct nvmem_cell *cell;
64 	const void *buf;
65 	size_t len;
66 	int ret;
67 
68 	/* Try lookup by device first, there might be a nvmem_cell_lookup
69 	 * associated with a given device.
70 	 */
71 	if (pdev) {
72 		ret = nvmem_get_mac_address(&pdev->dev, addr);
73 		put_device(&pdev->dev);
74 		return ret;
75 	}
76 
77 	cell = of_nvmem_cell_get(np, "mac-address");
78 	if (IS_ERR(cell))
79 		return PTR_ERR(cell);
80 
81 	buf = nvmem_cell_read(cell, &len);
82 	nvmem_cell_put(cell);
83 
84 	if (IS_ERR(buf))
85 		return PTR_ERR(buf);
86 
87 	ret = 0;
88 	if (len == ETH_ALEN) {
89 		if (is_valid_ether_addr(buf))
90 			memcpy(addr, buf, ETH_ALEN);
91 		else
92 			ret = -EINVAL;
93 	} else if (len == 3 * ETH_ALEN - 1) {
94 		u8 mac[ETH_ALEN];
95 
96 		if (mac_pton(buf, mac))
97 			memcpy(addr, mac, ETH_ALEN);
98 		else
99 			ret = -EINVAL;
100 	} else {
101 		ret = -EINVAL;
102 	}
103 
104 	kfree(buf);
105 
106 	return ret;
107 }
108 
109 /**
110  * of_get_mac_address()
111  * @np:		Caller's Device Node
112  * @addr:	Pointer to a six-byte array for the result
113  *
114  * Search the device tree for the best MAC address to use.  'mac-address' is
115  * checked first, because that is supposed to contain to "most recent" MAC
116  * address. If that isn't set, then 'local-mac-address' is checked next,
117  * because that is the default address. If that isn't set, then the obsolete
118  * 'address' is checked, just in case we're using an old device tree. If any
119  * of the above isn't set, then try to get MAC address from nvmem cell named
120  * 'mac-address'.
121  *
122  * Note that the 'address' property is supposed to contain a virtual address of
123  * the register set, but some DTS files have redefined that property to be the
124  * MAC address.
125  *
126  * All-zero MAC addresses are rejected, because those could be properties that
127  * exist in the device tree, but were not set by U-Boot.  For example, the
128  * DTS could define 'mac-address' and 'local-mac-address', with zero MAC
129  * addresses.  Some older U-Boots only initialized 'local-mac-address'.  In
130  * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
131  * but is all zeros.
132  *
133  * Return: 0 on success and errno in case of error.
134 */
135 int of_get_mac_address(struct device_node *np, u8 *addr)
136 {
137 	int ret;
138 
139 	if (!np)
140 		return -ENODEV;
141 
142 	ret = of_get_mac_addr(np, "mac-address", addr);
143 	if (!ret)
144 		return 0;
145 
146 	ret = of_get_mac_addr(np, "local-mac-address", addr);
147 	if (!ret)
148 		return 0;
149 
150 	ret = of_get_mac_addr(np, "address", addr);
151 	if (!ret)
152 		return 0;
153 
154 	return of_get_mac_addr_nvmem(np, addr);
155 }
156 EXPORT_SYMBOL(of_get_mac_address);
157 
158 /**
159  * of_get_ethdev_address()
160  * @np:		Caller's Device Node
161  * @dev:	Pointer to netdevice which address will be updated
162  *
163  * Search the device tree for the best MAC address to use.
164  * If found set @dev->dev_addr to that address.
165  *
166  * See documentation of of_get_mac_address() for more information on how
167  * the best address is determined.
168  *
169  * Return: 0 on success and errno in case of error.
170  */
171 int of_get_ethdev_address(struct device_node *np, struct net_device *dev)
172 {
173 	u8 addr[ETH_ALEN];
174 	int ret;
175 
176 	ret = of_get_mac_address(np, addr);
177 	if (!ret)
178 		eth_hw_addr_set(dev, addr);
179 	return ret;
180 }
181 EXPORT_SYMBOL(of_get_ethdev_address);
182