ce5211a21b83957bef67fbcd002ea35df3f77146
[openwrt/openwrt.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 --- a/drivers/of/of_net.c
22 +++ b/drivers/of/of_net.c
23 @@ -165,31 +165,56 @@ static int of_get_mac_address_mtd(struct
24 * If a mtd-mac-address property exists, try to fetch the MAC address from the
25 * specified mtd device.
26 *
27 + * DT can tell the system to increment the mac-address after is extracted by
28 + * using:
29 + * - mac-address-increment-byte to decide what byte to increase
30 + * (if not defined is increased the last byte)
31 + * - mac-address-increment to decide how much to increase. The value will
32 + * not overflow to other bytes if the increment is over 255.
33 + * (example 00:01:02:03:04:ff + 1 == 00:01:02:03:04:00)
34 + *
35 * Return: 0 on success and errno in case of error.
36 */
37 int of_get_mac_address(struct device_node *np, u8 *addr)
38 {
39 + u32 inc_idx, mac_inc;
40 int ret;
41
42 + /* Check first if the increment byte is present and valid.
43 + * If not set assume to increment the last byte if found.
44 + */
45 + if (of_property_read_u32(np, "mac-address-increment-byte", &inc_idx))
46 + inc_idx = 5;
47 + if (inc_idx < 3 || inc_idx > 5)
48 + return -EINVAL;
49 +
50 if (!np)
51 return -ENODEV;
52
53 ret = of_get_mac_addr(np, "mac-address", addr);
54 if (!ret)
55 - return 0;
56 + goto found;
57
58 ret = of_get_mac_addr(np, "local-mac-address", addr);
59 if (!ret)
60 - return 0;
61 + goto found;
62
63 ret = of_get_mac_addr(np, "address", addr);
64 if (!ret)
65 - return 0;
66 + goto found;
67
68 ret = of_get_mac_address_mtd(np, addr);
69 if (!ret)
70 - return 0;
71 + goto found;
72 +
73 + ret = of_get_mac_addr_nvmem(np, addr);
74 + if (ret)
75 + return ret;
76 +
77 +found:
78 + if (!of_property_read_u32(np, "mac-address-increment", &mac_inc))
79 + addr[inc_idx] += mac_inc;
80
81 - return of_get_mac_addr_nvmem(np, addr);
82 + return ret;
83 }
84 EXPORT_SYMBOL(of_get_mac_address);