xref: /openbmc/linux/drivers/nvmem/layouts/sl28vpd.c (revision d37cf9b63113f13d742713881ce691fc615d8b3b)
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 
sl28vpd_mac_address_pp(void * priv,const char * id,int index,unsigned int offset,void * buf,size_t bytes)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 
sl28vpd_v1_check_crc(struct device * dev,struct nvmem_device * nvmem)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 
sl28vpd_add_cells(struct device * dev,struct nvmem_device * nvmem)83 static int sl28vpd_add_cells(struct device *dev, struct nvmem_device *nvmem)
84 {
85 	const struct nvmem_cell_info *pinfo;
86 	struct nvmem_cell_info info = {0};
87 	struct device_node *layout_np;
88 	struct sl28vpd_header hdr;
89 	int ret, i;
90 
91 	/* check header */
92 	ret = nvmem_device_read(nvmem, 0, sizeof(hdr), &hdr);
93 	if (ret < 0)
94 		return ret;
95 	else if (ret != sizeof(hdr))
96 		return -EIO;
97 
98 	if (hdr.magic != SL28VPD_MAGIC) {
99 		dev_err(dev, "Invalid magic value (%02x)\n", hdr.magic);
100 		return -EINVAL;
101 	}
102 
103 	if (hdr.version != 1) {
104 		dev_err(dev, "Version %d is unsupported.\n", hdr.version);
105 		return -EINVAL;
106 	}
107 
108 	ret = sl28vpd_v1_check_crc(dev, nvmem);
109 	if (ret)
110 		return ret;
111 
112 	layout_np = of_nvmem_layout_get_container(nvmem);
113 	if (!layout_np)
114 		return -ENOENT;
115 
116 	for (i = 0; i < ARRAY_SIZE(sl28vpd_v1_entries); i++) {
117 		pinfo = &sl28vpd_v1_entries[i];
118 
119 		info.name = pinfo->name;
120 		info.offset = pinfo->offset;
121 		info.bytes = pinfo->bytes;
122 		info.read_post_process = pinfo->read_post_process;
123 		info.np = of_get_child_by_name(layout_np, pinfo->name);
124 
125 		ret = nvmem_add_one_cell(nvmem, &info);
126 		if (ret) {
127 			of_node_put(layout_np);
128 			return ret;
129 		}
130 	}
131 
132 	of_node_put(layout_np);
133 
134 	return 0;
135 }
136 
137 static const struct of_device_id sl28vpd_of_match_table[] = {
138 	{ .compatible = "kontron,sl28-vpd" },
139 	{},
140 };
141 MODULE_DEVICE_TABLE(of, sl28vpd_of_match_table);
142 
143 static struct nvmem_layout sl28vpd_layout = {
144 	.name = "sl28-vpd",
145 	.of_match_table = sl28vpd_of_match_table,
146 	.add_cells = sl28vpd_add_cells,
147 };
148 module_nvmem_layout_driver(sl28vpd_layout);
149 
150 MODULE_LICENSE("GPL");
151 MODULE_AUTHOR("Michael Walle <michael@walle.cc>");
152 MODULE_DESCRIPTION("NVMEM layout driver for the VPD of Kontron sl28 boards");
153