kernel: backport NVMEM changes queued for v6.8
[openwrt/staging/jow.git] / target / linux / generic / pending-5.15 / 804-nvmem-core-support-mac-base-fixed-layout-cells.patch
1 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
2 Date: Thu, 13 Jul 2023 18:29:19 +0200
3 Subject: [PATCH] nvmem: core: support "mac-base" fixed layout cells
4
5 Fixed layout binding allows specifying "mac-base" NVMEM cells. It's used
6 for base MAC address (that can be used for calculating relative
7 addresses). It can be stored in a raw binary format or as an ASCII
8 string.
9 ---
10
11 --- a/drivers/nvmem/Kconfig
12 +++ b/drivers/nvmem/Kconfig
13 @@ -1,6 +1,7 @@
14 # SPDX-License-Identifier: GPL-2.0-only
15 menuconfig NVMEM
16 bool "NVMEM Support"
17 + select GENERIC_NET_UTILS
18 imply NVMEM_LAYOUTS
19 help
20 Support for NVMEM(Non Volatile Memory) devices like EEPROM, EFUSES...
21 --- a/drivers/nvmem/core.c
22 +++ b/drivers/nvmem/core.c
23 @@ -7,9 +7,12 @@
24 */
25
26 #include <linux/device.h>
27 +#include <linux/ctype.h>
28 +#include <linux/etherdevice.h>
29 #include <linux/export.h>
30 #include <linux/fs.h>
31 #include <linux/idr.h>
32 +#include <linux/if_ether.h>
33 #include <linux/init.h>
34 #include <linux/kref.h>
35 #include <linux/module.h>
36 @@ -780,6 +783,62 @@ static int nvmem_validate_keepouts(struc
37 return 0;
38 }
39
40 +static int nvmem_mac_base_raw_read(void *context, const char *id, int index, unsigned int offset,
41 + void *buf, size_t bytes)
42 +{
43 + if (WARN_ON(bytes != ETH_ALEN))
44 + return -EINVAL;
45 +
46 + if (index)
47 + eth_addr_add(buf, index);
48 +
49 + return 0;
50 +}
51 +
52 +static int nvmem_mac_base_ascii_read(void *context, const char *id, int index, unsigned int offset,
53 + void *buf, size_t bytes)
54 +{
55 + u8 mac[ETH_ALEN];
56 +
57 + if (WARN_ON(bytes != 3 * ETH_ALEN - 1))
58 + return -EINVAL;
59 +
60 + if (!mac_pton(buf, mac))
61 + return -EINVAL;
62 +
63 + if (index)
64 + eth_addr_add(mac, index);
65 +
66 + ether_addr_copy(buf, mac);
67 +
68 + return 0;
69 +}
70 +
71 +static int nvmem_mac_base_hex_read(void *context, const char *id, int index, unsigned int offset,
72 + void *buf, size_t bytes)
73 +{
74 + u8 mac[ETH_ALEN], *hexstr;
75 + int i;
76 +
77 + if (WARN_ON(bytes != 2 * ETH_ALEN))
78 + return -EINVAL;
79 +
80 + hexstr = (u8 *)buf;
81 + for (i = 0; i < ETH_ALEN; i++) {
82 + if (!isxdigit(hexstr[i * 2]) || !isxdigit(hexstr[i * 2 + 1]))
83 + return -EINVAL;
84 +
85 + mac[i] = (hex_to_bin(hexstr[i * 2]) << 4) | hex_to_bin(hexstr[i * 2 + 1]);
86 + }
87 +
88 + if (index)
89 + eth_addr_add(mac, index);
90 +
91 + ether_addr_copy(buf, mac);
92 +
93 + return 0;
94 +}
95 +
96 static int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)
97 {
98 struct device *dev = &nvmem->dev;
99 @@ -814,6 +873,25 @@ static int nvmem_add_cells_from_dt(struc
100 if (nvmem->fixup_dt_cell_info)
101 nvmem->fixup_dt_cell_info(nvmem, &info);
102
103 + if (of_device_is_compatible(np, "fixed-layout")) {
104 + if (of_device_is_compatible(child, "mac-base")) {
105 + if (info.bytes == ETH_ALEN) {
106 + info.raw_len = info.bytes;
107 + info.bytes = ETH_ALEN;
108 + info.read_post_process = nvmem_mac_base_raw_read;
109 + } else if (info.bytes == 2 * ETH_ALEN) {
110 + info.raw_len = info.bytes;
111 + info.bytes = ETH_ALEN;
112 + info.read_post_process = nvmem_mac_base_hex_read;
113 + } else if (info.bytes == 3 * ETH_ALEN - 1) {
114 + info.raw_len = info.bytes;
115 + info.bytes = ETH_ALEN;
116 + info.read_post_process = nvmem_mac_base_ascii_read;
117 + }
118 +
119 + }
120 + }
121 +
122 ret = nvmem_add_one_cell(nvmem, &info);
123 kfree(info.name);
124 if (ret) {