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
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
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>
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
24 --- a/drivers/nvmem/layouts/Kconfig
25 +++ b/drivers/nvmem/layouts/Kconfig
30 +config NVMEM_LAYOUT_SL28_VPD
31 + tristate "Kontron sl28 VPD layout support"
34 + Say Y here if you want to support the VPD layout of the Kontron
40 --- a/drivers/nvmem/layouts/Makefile
41 +++ b/drivers/nvmem/layouts/Makefile
44 # Makefile for nvmem layouts.
47 +obj-$(CONFIG_NVMEM_LAYOUT_SL28_VPD) += sl28vpd.o
49 +++ b/drivers/nvmem/layouts/sl28vpd.c
51 +// SPDX-License-Identifier: GPL-2.0
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>
60 +#define SL28VPD_MAGIC 'V'
62 +struct sl28vpd_header {
68 + struct sl28vpd_header header;
69 + char serial_number[15];
70 + u8 base_mac_address[ETH_ALEN];
74 +static int sl28vpd_mac_address_pp(void *priv, const char *id, int index,
75 + unsigned int offset, void *buf,
78 + if (bytes != ETH_ALEN)
84 + if (!is_valid_ether_addr(buf))
87 + eth_addr_add(buf, index);
92 +static const struct nvmem_cell_info sl28vpd_v1_entries[] = {
94 + .name = "serial-number",
95 + .offset = offsetof(struct sl28vpd_v1, serial_number),
96 + .bytes = sizeof_field(struct sl28vpd_v1, serial_number),
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,
106 +static int sl28vpd_v1_check_crc(struct device *dev, struct nvmem_device *nvmem)
108 + struct sl28vpd_v1 data_v1;
109 + u8 table[CRC8_TABLE_SIZE];
113 + crc8_populate_msb(table, 0x07);
115 + ret = nvmem_device_read(nvmem, 0, sizeof(data_v1), &data_v1);
118 + else if (ret != sizeof(data_v1))
121 + crc = crc8(table, (void *)&data_v1, sizeof(data_v1) - 1, 0);
123 + if (crc != data_v1.crc8) {
125 + "Checksum is invalid (got %02x, expected %02x).\n",
126 + crc, data_v1.crc8);
133 +static int sl28vpd_add_cells(struct device *dev, struct nvmem_device *nvmem,
134 + struct nvmem_layout *layout)
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;
143 + ret = nvmem_device_read(nvmem, 0, sizeof(hdr), &hdr);
146 + else if (ret != sizeof(hdr))
149 + if (hdr.magic != SL28VPD_MAGIC) {
150 + dev_err(dev, "Invalid magic value (%02x)\n", hdr.magic);
154 + if (hdr.version != 1) {
155 + dev_err(dev, "Version %d is unsupported.\n", hdr.version);
159 + ret = sl28vpd_v1_check_crc(dev, nvmem);
163 + layout_np = of_nvmem_layout_get_container(nvmem);
167 + for (i = 0; i < ARRAY_SIZE(sl28vpd_v1_entries); i++) {
168 + pinfo = &sl28vpd_v1_entries[i];
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);
176 + ret = nvmem_add_one_cell(nvmem, &info);
178 + of_node_put(layout_np);
183 + of_node_put(layout_np);
188 +static const struct of_device_id sl28vpd_of_match_table[] = {
189 + { .compatible = "kontron,sl28-vpd" },
192 +MODULE_DEVICE_TABLE(of, sl28vpd_of_match_table);
194 +struct nvmem_layout sl28vpd_layout = {
195 + .name = "sl28-vpd",
196 + .of_match_table = sl28vpd_of_match_table,
197 + .add_cells = sl28vpd_add_cells,
200 +static int __init sl28vpd_init(void)
202 + return nvmem_layout_register(&sl28vpd_layout);
205 +static void __exit sl28vpd_exit(void)
207 + nvmem_layout_unregister(&sl28vpd_layout);
210 +module_init(sl28vpd_init);
211 +module_exit(sl28vpd_exit);
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");