kernel: backport NVMEM patches queued for the v6.4
[openwrt/staging/dedeckeh.git] / target / linux / generic / backport-5.15 / 811-v6.4-0010-nvmem-layouts-sl28vpd-Add-new-layout-driver.patch
1 From d9fae023fe86069750092fc1c2f3a73e2fb18512 Mon Sep 17 00:00:00 2001
2 From: Michael Walle <michael@walle.cc>
3 Date: Tue, 4 Apr 2023 18:21:29 +0100
4 Subject: [PATCH] nvmem: layouts: sl28vpd: Add new layout driver
5
6 This layout applies to the VPD of the Kontron sl28 boards. The VPD only
7 contains a base MAC address. Therefore, we have to add an individual
8 offset to it. This is done by taking the second argument of the nvmem
9 phandle into account. Also this let us checking the VPD version and the
10 checksum.
11
12 Signed-off-by: Michael Walle <michael@walle.cc>
13 Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
14 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
15 Link: https://lore.kernel.org/r/20230404172148.82422-22-srinivas.kandagatla@linaro.org
16 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
17 ---
18 drivers/nvmem/layouts/Kconfig | 9 ++
19 drivers/nvmem/layouts/Makefile | 2 +
20 drivers/nvmem/layouts/sl28vpd.c | 165 ++++++++++++++++++++++++++++++++
21 3 files changed, 176 insertions(+)
22 create mode 100644 drivers/nvmem/layouts/sl28vpd.c
23
24 --- a/drivers/nvmem/layouts/Kconfig
25 +++ b/drivers/nvmem/layouts/Kconfig
26 @@ -2,4 +2,13 @@
27
28 menu "Layout Types"
29
30 +config NVMEM_LAYOUT_SL28_VPD
31 + tristate "Kontron sl28 VPD layout support"
32 + select CRC8
33 + help
34 + Say Y here if you want to support the VPD layout of the Kontron
35 + SMARC-sAL28 boards.
36 +
37 + If unsure, say N.
38 +
39 endmenu
40 --- a/drivers/nvmem/layouts/Makefile
41 +++ b/drivers/nvmem/layouts/Makefile
42 @@ -2,3 +2,5 @@
43 #
44 # Makefile for nvmem layouts.
45 #
46 +
47 +obj-$(CONFIG_NVMEM_LAYOUT_SL28_VPD) += sl28vpd.o
48 --- /dev/null
49 +++ b/drivers/nvmem/layouts/sl28vpd.c
50 @@ -0,0 +1,165 @@
51 +// SPDX-License-Identifier: GPL-2.0
52 +
53 +#include <linux/crc8.h>
54 +#include <linux/etherdevice.h>
55 +#include <linux/nvmem-consumer.h>
56 +#include <linux/nvmem-provider.h>
57 +#include <linux/of.h>
58 +#include <uapi/linux/if_ether.h>
59 +
60 +#define SL28VPD_MAGIC 'V'
61 +
62 +struct sl28vpd_header {
63 + u8 magic;
64 + u8 version;
65 +} __packed;
66 +
67 +struct sl28vpd_v1 {
68 + struct sl28vpd_header header;
69 + char serial_number[15];
70 + u8 base_mac_address[ETH_ALEN];
71 + u8 crc8;
72 +} __packed;
73 +
74 +static int sl28vpd_mac_address_pp(void *priv, const char *id, int index,
75 + unsigned int offset, void *buf,
76 + size_t bytes)
77 +{
78 + if (bytes != ETH_ALEN)
79 + return -EINVAL;
80 +
81 + if (index < 0)
82 + return -EINVAL;
83 +
84 + if (!is_valid_ether_addr(buf))
85 + return -EINVAL;
86 +
87 + eth_addr_add(buf, index);
88 +
89 + return 0;
90 +}
91 +
92 +static const struct nvmem_cell_info sl28vpd_v1_entries[] = {
93 + {
94 + .name = "serial-number",
95 + .offset = offsetof(struct sl28vpd_v1, serial_number),
96 + .bytes = sizeof_field(struct sl28vpd_v1, serial_number),
97 + },
98 + {
99 + .name = "base-mac-address",
100 + .offset = offsetof(struct sl28vpd_v1, base_mac_address),
101 + .bytes = sizeof_field(struct sl28vpd_v1, base_mac_address),
102 + .read_post_process = sl28vpd_mac_address_pp,
103 + },
104 +};
105 +
106 +static int sl28vpd_v1_check_crc(struct device *dev, struct nvmem_device *nvmem)
107 +{
108 + struct sl28vpd_v1 data_v1;
109 + u8 table[CRC8_TABLE_SIZE];
110 + int ret;
111 + u8 crc;
112 +
113 + crc8_populate_msb(table, 0x07);
114 +
115 + ret = nvmem_device_read(nvmem, 0, sizeof(data_v1), &data_v1);
116 + if (ret < 0)
117 + return ret;
118 + else if (ret != sizeof(data_v1))
119 + return -EIO;
120 +
121 + crc = crc8(table, (void *)&data_v1, sizeof(data_v1) - 1, 0);
122 +
123 + if (crc != data_v1.crc8) {
124 + dev_err(dev,
125 + "Checksum is invalid (got %02x, expected %02x).\n",
126 + crc, data_v1.crc8);
127 + return -EINVAL;
128 + }
129 +
130 + return 0;
131 +}
132 +
133 +static int sl28vpd_add_cells(struct device *dev, struct nvmem_device *nvmem,
134 + struct nvmem_layout *layout)
135 +{
136 + const struct nvmem_cell_info *pinfo;
137 + struct nvmem_cell_info info = {0};
138 + struct device_node *layout_np;
139 + struct sl28vpd_header hdr;
140 + int ret, i;
141 +
142 + /* check header */
143 + ret = nvmem_device_read(nvmem, 0, sizeof(hdr), &hdr);
144 + if (ret < 0)
145 + return ret;
146 + else if (ret != sizeof(hdr))
147 + return -EIO;
148 +
149 + if (hdr.magic != SL28VPD_MAGIC) {
150 + dev_err(dev, "Invalid magic value (%02x)\n", hdr.magic);
151 + return -EINVAL;
152 + }
153 +
154 + if (hdr.version != 1) {
155 + dev_err(dev, "Version %d is unsupported.\n", hdr.version);
156 + return -EINVAL;
157 + }
158 +
159 + ret = sl28vpd_v1_check_crc(dev, nvmem);
160 + if (ret)
161 + return ret;
162 +
163 + layout_np = of_nvmem_layout_get_container(nvmem);
164 + if (!layout_np)
165 + return -ENOENT;
166 +
167 + for (i = 0; i < ARRAY_SIZE(sl28vpd_v1_entries); i++) {
168 + pinfo = &sl28vpd_v1_entries[i];
169 +
170 + info.name = pinfo->name;
171 + info.offset = pinfo->offset;
172 + info.bytes = pinfo->bytes;
173 + info.read_post_process = pinfo->read_post_process;
174 + info.np = of_get_child_by_name(layout_np, pinfo->name);
175 +
176 + ret = nvmem_add_one_cell(nvmem, &info);
177 + if (ret) {
178 + of_node_put(layout_np);
179 + return ret;
180 + }
181 + }
182 +
183 + of_node_put(layout_np);
184 +
185 + return 0;
186 +}
187 +
188 +static const struct of_device_id sl28vpd_of_match_table[] = {
189 + { .compatible = "kontron,sl28-vpd" },
190 + {},
191 +};
192 +MODULE_DEVICE_TABLE(of, sl28vpd_of_match_table);
193 +
194 +struct nvmem_layout sl28vpd_layout = {
195 + .name = "sl28-vpd",
196 + .of_match_table = sl28vpd_of_match_table,
197 + .add_cells = sl28vpd_add_cells,
198 +};
199 +
200 +static int __init sl28vpd_init(void)
201 +{
202 + return nvmem_layout_register(&sl28vpd_layout);
203 +}
204 +
205 +static void __exit sl28vpd_exit(void)
206 +{
207 + nvmem_layout_unregister(&sl28vpd_layout);
208 +}
209 +
210 +module_init(sl28vpd_init);
211 +module_exit(sl28vpd_exit);
212 +
213 +MODULE_LICENSE("GPL");
214 +MODULE_AUTHOR("Michael Walle <michael@walle.cc>");
215 +MODULE_DESCRIPTION("NVMEM layout driver for the VPD of Kontron sl28 boards");