kernel: nvmem: fix "fixed-layout" & support "mac-base"
[openwrt/staging/stintel.git] / target / linux / generic / pending-6.1 / 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 help
19 Support for NVMEM(Non Volatile Memory) devices like EEPROM, EFUSES...
20
21 --- a/drivers/nvmem/core.c
22 +++ b/drivers/nvmem/core.c
23 @@ -7,9 +7,11 @@
24 */
25
26 #include <linux/device.h>
27 +#include <linux/etherdevice.h>
28 #include <linux/export.h>
29 #include <linux/fs.h>
30 #include <linux/idr.h>
31 +#include <linux/if_ether.h>
32 #include <linux/init.h>
33 #include <linux/kref.h>
34 #include <linux/module.h>
35 @@ -696,6 +698,37 @@ static int nvmem_validate_keepouts(struc
36 return 0;
37 }
38
39 +static int nvmem_mac_base_raw_read(void *context, const char *id, int index, unsigned int offset,
40 + void *buf, size_t bytes)
41 +{
42 + if (WARN_ON(bytes != ETH_ALEN))
43 + return -EINVAL;
44 +
45 + if (index)
46 + eth_addr_add(buf, index);
47 +
48 + return 0;
49 +}
50 +
51 +static int nvmem_mac_base_ascii_read(void *context, const char *id, int index, unsigned int offset,
52 + void *buf, size_t bytes)
53 +{
54 + u8 mac[ETH_ALEN];
55 +
56 + if (WARN_ON(bytes != 3 * ETH_ALEN - 1))
57 + return -EINVAL;
58 +
59 + if (!mac_pton(buf, mac))
60 + return -EINVAL;
61 +
62 + if (index)
63 + eth_addr_add(mac, index);
64 +
65 + ether_addr_copy(buf, mac);
66 +
67 + return 0;
68 +}
69 +
70 static int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)
71 {
72 struct nvmem_layout *layout = nvmem->layout;
73 @@ -731,6 +764,20 @@ static int nvmem_add_cells_from_dt(struc
74 if (layout && layout->fixup_cell_info)
75 layout->fixup_cell_info(nvmem, layout, &info);
76
77 + if (of_device_is_compatible(np, "fixed-layout")) {
78 + if (of_device_is_compatible(child, "mac-base")) {
79 + if (info.bytes == 6) {
80 + info.raw_len = info.bytes;
81 + info.bytes = ETH_ALEN;
82 + info.read_post_process = nvmem_mac_base_raw_read;
83 + } else if (info.bytes == 3 * ETH_ALEN - 1) {
84 + info.raw_len = info.bytes;
85 + info.bytes = ETH_ALEN;
86 + info.read_post_process = nvmem_mac_base_ascii_read;
87 + }
88 + }
89 + }
90 +
91 ret = nvmem_add_one_cell(nvmem, &info);
92 kfree(info.name);
93 if (ret) {