xref: /openbmc/linux/drivers/nvmem/layouts/sl28vpd.c (revision fa0dadde)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/crc8.h>
4 #include <linux/etherdevice.h>
5 #include <linux/nvmem-consumer.h>
6 #include <linux/nvmem-provider.h>
7 #include <linux/of.h>
8 #include <uapi/linux/if_ether.h>
9 
10 #define SL28VPD_MAGIC 'V'
11 
12 struct sl28vpd_header {
13 	u8 magic;
14 	u8 version;
15 } __packed;
16 
17 struct sl28vpd_v1 {
18 	struct sl28vpd_header header;
19 	char serial_number[15];
20 	u8 base_mac_address[ETH_ALEN];
21 	u8 crc8;
22 } __packed;
23 
24 static int sl28vpd_mac_address_pp(void *priv, const char *id, int index,
25 				  unsigned int offset, void *buf,
26 				  size_t bytes)
27 {
28 	if (bytes != ETH_ALEN)
29 		return -EINVAL;
30 
31 	if (index < 0)
32 		return -EINVAL;
33 
34 	if (!is_valid_ether_addr(buf))
35 		return -EINVAL;
36 
37 	eth_addr_add(buf, index);
38 
39 	return 0;
40 }
41 
42 static const struct nvmem_cell_info sl28vpd_v1_entries[] = {
43 	{
44 		.name = "serial-number",
45 		.offset = offsetof(struct sl28vpd_v1, serial_number),
46 		.bytes = sizeof_field(struct sl28vpd_v1, serial_number),
47 	},
48 	{
49 		.name = "base-mac-address",
50 		.offset = offsetof(struct sl28vpd_v1, base_mac_address),
51 		.bytes = sizeof_field(struct sl28vpd_v1, base_mac_address),
52 		.read_post_process = sl28vpd_mac_address_pp,
53 	},
54 };
55 
56 static int sl28vpd_v1_check_crc(struct device *dev, struct nvmem_device *nvmem)
57 {
58 	struct sl28vpd_v1 data_v1;
59 	u8 table[CRC8_TABLE_SIZE];
60 	int ret;
61 	u8 crc;
62 
63 	crc8_populate_msb(table, 0x07);
64 
65 	ret = nvmem_device_read(nvmem, 0, sizeof(data_v1), &data_v1);
66 	if (ret < 0)
67 		return ret;
68 	else if (ret != sizeof(data_v1))
69 		return -EIO;
70 
71 	crc = crc8(table, (void *)&data_v1, sizeof(data_v1) - 1, 0);
72 
73 	if (crc != data_v1.crc8) {
74 		dev_err(dev,
75 			"Checksum is invalid (got %02x, expected %02x).\n",
76 			crc, data_v1.crc8);
77 		return -EINVAL;
78 	}
79 
80 	return 0;
81 }
82 
83 static int sl28vpd_add_cells(struct device *dev, struct nvmem_device *nvmem,
84 			     struct nvmem_layout *layout)
85 {
86 	const struct nvmem_cell_info *pinfo;
87 	struct nvmem_cell_info info = {0};
88 	struct device_node *layout_np;
89 	struct sl28vpd_header hdr;
90 	int ret, i;
91 
92 	/* check header */
93 	ret = nvmem_device_read(nvmem, 0, sizeof(hdr), &hdr);
94 	if (ret < 0)
95 		return ret;
96 	else if (ret != sizeof(hdr))
97 		return -EIO;
98 
99 	if (hdr.magic != SL28VPD_MAGIC) {
100 		dev_err(dev, "Invalid magic value (%02x)\n", hdr.magic);
101 		return -EINVAL;
102 	}
103 
104 	if (hdr.version != 1) {
105 		dev_err(dev, "Version %d is unsupported.\n", hdr.version);
106 		return -EINVAL;
107 	}
108 
109 	ret = sl28vpd_v1_check_crc(dev, nvmem);
110 	if (ret)
111 		return ret;
112 
113 	layout_np = of_nvmem_layout_get_container(nvmem);
114 	if (!layout_np)
115 		return -ENOENT;
116 
117 	for (i = 0; i < ARRAY_SIZE(sl28vpd_v1_entries); i++) {
118 		pinfo = &sl28vpd_v1_entries[i];
119 
120 		info.name = pinfo->name;
121 		info.offset = pinfo->offset;
122 		info.bytes = pinfo->bytes;
123 		info.read_post_process = pinfo->read_post_process;
124 		info.np = of_get_child_by_name(layout_np, pinfo->name);
125 
126 		ret = nvmem_add_one_cell(nvmem, &info);
127 		if (ret) {
128 			of_node_put(layout_np);
129 			return ret;
130 		}
131 	}
132 
133 	of_node_put(layout_np);
134 
135 	return 0;
136 }
137 
138 static const struct of_device_id sl28vpd_of_match_table[] = {
139 	{ .compatible = "kontron,sl28-vpd" },
140 	{},
141 };
142 MODULE_DEVICE_TABLE(of, sl28vpd_of_match_table);
143 
144 static struct nvmem_layout sl28vpd_layout = {
145 	.name = "sl28-vpd",
146 	.of_match_table = sl28vpd_of_match_table,
147 	.add_cells = sl28vpd_add_cells,
148 };
149 module_nvmem_layout_driver(sl28vpd_layout);
150 
151 MODULE_LICENSE("GPL");
152 MODULE_AUTHOR("Michael Walle <michael@walle.cc>");
153 MODULE_DESCRIPTION("NVMEM layout driver for the VPD of Kontron sl28 boards");
154