/src/barebox/drivers/of/of_net.c
Line | Count | Source |
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 <common.h> |
8 | | #include <net.h> |
9 | | #include <of_net.h> |
10 | | #include <linux/phy.h> |
11 | | #include <linux/nvmem-consumer.h> |
12 | | |
13 | | /** |
14 | | * of_get_phy_mode - Get phy mode for given device_node |
15 | | * @np: Pointer to the given device_node |
16 | | * |
17 | | * The function gets phy interface string from property 'phy-mode', |
18 | | * and return its index in phy_modes table, or errno in error case. |
19 | | */ |
20 | | int of_get_phy_mode(struct device_node *np) |
21 | 0 | { |
22 | 0 | const char *pm; |
23 | 0 | int err, i; |
24 | |
|
25 | 0 | err = of_property_read_string(np, "phy-mode", &pm); |
26 | 0 | if (err < 0) |
27 | 0 | err = of_property_read_string(np, "phy-connection-type", &pm); |
28 | 0 | if (err < 0) |
29 | 0 | return err; |
30 | | |
31 | 0 | for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++) |
32 | 0 | if (!strcasecmp(pm, phy_modes(i))) |
33 | 0 | return i; |
34 | | |
35 | 0 | return -ENODEV; |
36 | 0 | } |
37 | | EXPORT_SYMBOL_GPL(of_get_phy_mode); |
38 | | |
39 | | static int of_get_mac_addr(struct device_node *np, const char *name, u8 *addr) |
40 | 0 | { |
41 | 0 | struct property *pp = of_find_property(np, name, NULL); |
42 | |
|
43 | 0 | if (pp && pp->length == ETH_ALEN && is_valid_ether_addr(pp->value)) { |
44 | 0 | memcpy(addr, pp->value, ETH_ALEN); |
45 | 0 | return 0; |
46 | 0 | } |
47 | 0 | return -ENODEV; |
48 | 0 | } |
49 | | |
50 | | int of_get_mac_addr_nvmem(struct device_node *np, u8 *addr) |
51 | 0 | { |
52 | 0 | struct nvmem_cell *cell; |
53 | 0 | void *mac; |
54 | 0 | size_t len; |
55 | |
|
56 | 0 | if (!IS_ENABLED(CONFIG_NVMEM)) |
57 | 0 | return -ENODEV; |
58 | | |
59 | 0 | cell = of_nvmem_cell_get(np, "mac-address"); |
60 | 0 | if (IS_ERR(cell)) |
61 | 0 | return PTR_ERR(cell); |
62 | | |
63 | 0 | mac = nvmem_cell_read(cell, &len); |
64 | 0 | nvmem_cell_put(cell); |
65 | |
|
66 | 0 | if (IS_ERR(mac)) |
67 | 0 | return PTR_ERR(mac); |
68 | | |
69 | 0 | if (len != ETH_ALEN || !is_valid_ether_addr(mac)) { |
70 | 0 | kfree(mac); |
71 | 0 | return -EINVAL; |
72 | 0 | } |
73 | | |
74 | 0 | memcpy(addr, mac, ETH_ALEN); |
75 | 0 | kfree(mac); |
76 | |
|
77 | 0 | return 0; |
78 | 0 | } |
79 | | |
80 | | /** |
81 | | * Search the device tree for the best MAC address to use. 'mac-address' is |
82 | | * checked first, because that is supposed to contain to "most recent" MAC |
83 | | * address. If that isn't set, then 'local-mac-address' is checked next, |
84 | | * because that is the default address. If that isn't set, then the obsolete |
85 | | * 'address' is checked, just in case we're using an old device tree. If any |
86 | | * of the above isn't set, then try to get MAC address from nvmem cell named |
87 | | * 'mac-address'. |
88 | | * |
89 | | * Note that the 'address' property is supposed to contain a virtual address of |
90 | | * the register set, but some DTS files have redefined that property to be the |
91 | | * MAC address. |
92 | | * |
93 | | * All-zero MAC addresses are rejected, because those could be properties that |
94 | | * exist in the device tree, but were not set by U-Boot. For example, the |
95 | | * DTS could define 'mac-address' and 'local-mac-address', with zero MAC |
96 | | * addresses. Some older U-Boots only initialized 'local-mac-address'. In |
97 | | * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists |
98 | | * but is all zeros. |
99 | | */ |
100 | | int of_get_mac_address(struct device_node *np, u8 *addr) |
101 | 0 | { |
102 | 0 | int ret; |
103 | |
|
104 | 0 | if (!np) |
105 | 0 | return -ENODEV; |
106 | | |
107 | 0 | ret = of_get_mac_addr(np, "mac-address", addr); |
108 | 0 | if (!ret) |
109 | 0 | return 0; |
110 | | |
111 | 0 | ret = of_get_mac_addr(np, "local-mac-address", addr); |
112 | 0 | if (!ret) |
113 | 0 | return 0; |
114 | | |
115 | 0 | ret = of_get_mac_addr(np, "address", addr); |
116 | 0 | if (!ret) |
117 | 0 | return 0; |
118 | | |
119 | 0 | return of_get_mac_addr_nvmem(np, addr); |
120 | 0 | } |