treewide: convert mtd-mac-address-increment* to generic implementation
[openwrt/staging/wigyori.git] / target / linux / generic / pending-5.4 / 682-of_net-add-mac-address-increment-support.patch
1 From 639dba857aa554f2a78572adc4cf3c32de9ec2e2 Mon Sep 17 00:00:00 2001
2 From: Ansuel Smith <ansuelsmth@gmail.com>
3 Date: Tue, 30 Mar 2021 18:21:14 +0200
4 Subject: [PATCH 2/2] of_net: add mac-address-increment support
5
6 Lots of embedded devices use the mac-address of other interface
7 extracted from nvmem cells and increments it by one or two. Add two
8 bindings to integrate this and directly use the right mac-address for
9 the interface. Some example are some routers that use the gmac
10 mac-address stored in the art partition and increments it by one for the
11 wifi. mac-address-increment-byte bindings is used to tell what byte of
12 the mac-address has to be increased (if not defined the last byte is
13 increased) and mac-address-increment tells how much the byte decided
14 early has to be increased.
15
16 Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com>
17 ---
18 drivers/of/of_net.c | 59 ++++++++++++++++++++++++++++++++++-----------
19 1 file changed, 45 insertions(+), 14 deletions(-)
20
21 diff --git a/drivers/of/of_net.c b/drivers/of/of_net.c
22 index f072e2509cc9..0dbd1f7ef396 100644
23 --- a/drivers/of/of_net.c
24 +++ b/drivers/of/of_net.c
25 @@ -55,31 +55,36 @@ static void *of_get_mac_addr(struct device_node *np, const char *name)
26 return NULL;
27 }
28
29 -static const void *of_get_mac_addr_nvmem(struct device_node *np)
30 +static void *of_get_mac_addr_nvmem(struct device_node *np, int *err)
31 {
32 int ret;
33 - const void *mac;
34 + void *mac;
35 u8 nvmem_mac[ETH_ALEN];
36 struct platform_device *pdev = of_find_device_by_node(np);
37
38 - if (!pdev)
39 - return ERR_PTR(-ENODEV);
40 + if (!pdev) {
41 + *err = -ENODEV;
42 + return NULL;
43 + }
44
45 ret = nvmem_get_mac_address(&pdev->dev, &nvmem_mac);
46 if (ret) {
47 put_device(&pdev->dev);
48 - return ERR_PTR(ret);
49 + *err = ret;
50 + return NULL;
51 }
52
53 mac = devm_kmemdup(&pdev->dev, nvmem_mac, ETH_ALEN, GFP_KERNEL);
54 put_device(&pdev->dev);
55 - if (!mac)
56 - return ERR_PTR(-ENOMEM);
57 + if (!mac) {
58 + *err = -ENOMEM;
59 + return NULL;
60 + }
61
62 return mac;
63 }
64
65 -static const void *of_get_mac_address_mtd(struct device_node *np)
66 +static void *of_get_mac_address_mtd(struct device_node *np)
67 {
68 #ifdef CONFIG_MTD
69 struct device_node *mtd_np = NULL;
70 @@ -167,28 +172,54 @@ static const void *of_get_mac_address_mtd(struct device_node *np)
71 * If a mtd-mac-address property exists, try to fetch the MAC address from the
72 * specified mtd device, and store it as a 'mac-address' property
73 *
74 + * DT can tell the system to increment the mac-address after is extracted by
75 + * using:
76 + * - mac-address-increment-byte to decide what byte to increase
77 + * (if not defined is increased the last byte)
78 + * - mac-address-increment to decide how much to increase. The value will
79 + * not overflow to other bytes if the increment is over 255.
80 + * (example 00:01:02:03:04:ff + 1 == 00:01:02:03:04:00)
81 + *
82 * Return: Will be a valid pointer on success and ERR_PTR in case of error.
83 */
84 const void *of_get_mac_address(struct device_node *np)
85 {
86 - const void *addr;
87 + u32 inc_idx, mac_inc;
88 + int ret = 0;
89 + u8 *addr;
90 +
91 + /* Check first if the increment byte is present and valid.
92 + * If not set assume to increment the last byte if found.
93 + */
94 + if (of_property_read_u32(np, "mac-address-increment-byte", &inc_idx))
95 + inc_idx = 5;
96 + if (inc_idx < 3 || inc_idx > 5)
97 + return ERR_PTR(-EINVAL);
98
99 addr = of_get_mac_addr(np, "mac-address");
100 if (addr)
101 - return addr;
102 + goto found;
103
104 addr = of_get_mac_addr(np, "local-mac-address");
105 if (addr)
106 - return addr;
107 + goto found;
108
109 addr = of_get_mac_addr(np, "address");
110 if (addr)
111 - return addr;
112 + goto found;
113
114 addr = of_get_mac_address_mtd(np);
115 if (addr)
116 - return addr;
117 + goto found;
118 +
119 + addr = of_get_mac_addr_nvmem(np, &ret);
120 + if (ret)
121 + return ERR_PTR(ret);
122 +
123 +found:
124 + if (!of_property_read_u32(np, "mac-address-increment", &mac_inc))
125 + addr[inc_idx] += mac_inc;
126
127 - return of_get_mac_addr_nvmem(np);
128 + return addr;
129 }
130 EXPORT_SYMBOL(of_get_mac_address);
131 --
132 2.30.2
133