xref: /openbmc/linux/drivers/nvmem/brcm_nvram.c (revision c4c3c32d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2021 Rafał Miłecki <rafal@milecki.pl>
4  */
5 
6 #include <linux/bcm47xx_nvram.h>
7 #include <linux/etherdevice.h>
8 #include <linux/if_ether.h>
9 #include <linux/io.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/module.h>
12 #include <linux/nvmem-consumer.h>
13 #include <linux/nvmem-provider.h>
14 #include <linux/of.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
17 
18 #define NVRAM_MAGIC			"FLSH"
19 
20 struct brcm_nvram {
21 	struct device *dev;
22 	void __iomem *base;
23 	struct nvmem_cell_info *cells;
24 	int ncells;
25 };
26 
27 struct brcm_nvram_header {
28 	char magic[4];
29 	__le32 len;
30 	__le32 crc_ver_init;	/* 0:7 crc, 8:15 ver, 16:31 sdram_init */
31 	__le32 config_refresh;	/* 0:15 sdram_config, 16:31 sdram_refresh */
32 	__le32 config_ncdl;	/* ncdl values for memc */
33 };
34 
35 static int brcm_nvram_read(void *context, unsigned int offset, void *val,
36 			   size_t bytes)
37 {
38 	struct brcm_nvram *priv = context;
39 	u8 *dst = val;
40 
41 	while (bytes--)
42 		*dst++ = readb(priv->base + offset++);
43 
44 	return 0;
45 }
46 
47 static int brcm_nvram_read_post_process_macaddr(void *context, const char *id, int index,
48 						unsigned int offset, void *buf, size_t bytes)
49 {
50 	u8 mac[ETH_ALEN];
51 
52 	if (bytes != 3 * ETH_ALEN - 1)
53 		return -EINVAL;
54 
55 	if (!mac_pton(buf, mac))
56 		return -EINVAL;
57 
58 	if (index)
59 		eth_addr_add(mac, index);
60 
61 	ether_addr_copy(buf, mac);
62 
63 	return 0;
64 }
65 
66 static int brcm_nvram_add_cells(struct brcm_nvram *priv, uint8_t *data,
67 				size_t len)
68 {
69 	struct device *dev = priv->dev;
70 	char *var, *value, *eq;
71 	int idx;
72 
73 	priv->ncells = 0;
74 	for (var = data + sizeof(struct brcm_nvram_header);
75 	     var < (char *)data + len && *var;
76 	     var += strlen(var) + 1) {
77 		priv->ncells++;
78 	}
79 
80 	priv->cells = devm_kcalloc(dev, priv->ncells, sizeof(*priv->cells), GFP_KERNEL);
81 	if (!priv->cells)
82 		return -ENOMEM;
83 
84 	for (var = data + sizeof(struct brcm_nvram_header), idx = 0;
85 	     var < (char *)data + len && *var;
86 	     var = value + strlen(value) + 1, idx++) {
87 		eq = strchr(var, '=');
88 		if (!eq)
89 			break;
90 		*eq = '\0';
91 		value = eq + 1;
92 
93 		priv->cells[idx].name = devm_kstrdup(dev, var, GFP_KERNEL);
94 		if (!priv->cells[idx].name)
95 			return -ENOMEM;
96 		priv->cells[idx].offset = value - (char *)data;
97 		priv->cells[idx].bytes = strlen(value);
98 		priv->cells[idx].np = of_get_child_by_name(dev->of_node, priv->cells[idx].name);
99 		if (!strcmp(var, "et0macaddr") ||
100 		    !strcmp(var, "et1macaddr") ||
101 		    !strcmp(var, "et2macaddr")) {
102 			priv->cells[idx].raw_len = strlen(value);
103 			priv->cells[idx].bytes = ETH_ALEN;
104 			priv->cells[idx].read_post_process = brcm_nvram_read_post_process_macaddr;
105 		}
106 	}
107 
108 	return 0;
109 }
110 
111 static int brcm_nvram_parse(struct brcm_nvram *priv)
112 {
113 	struct device *dev = priv->dev;
114 	struct brcm_nvram_header header;
115 	uint8_t *data;
116 	size_t len;
117 	int err;
118 
119 	memcpy_fromio(&header, priv->base, sizeof(header));
120 
121 	if (memcmp(header.magic, NVRAM_MAGIC, 4)) {
122 		dev_err(dev, "Invalid NVRAM magic\n");
123 		return -EINVAL;
124 	}
125 
126 	len = le32_to_cpu(header.len);
127 
128 	data = kzalloc(len, GFP_KERNEL);
129 	if (!data)
130 		return -ENOMEM;
131 
132 	memcpy_fromio(data, priv->base, len);
133 	data[len - 1] = '\0';
134 
135 	err = brcm_nvram_add_cells(priv, data, len);
136 	if (err) {
137 		dev_err(dev, "Failed to add cells: %d\n", err);
138 		return err;
139 	}
140 
141 	kfree(data);
142 
143 	return 0;
144 }
145 
146 static int brcm_nvram_probe(struct platform_device *pdev)
147 {
148 	struct nvmem_config config = {
149 		.name = "brcm-nvram",
150 		.reg_read = brcm_nvram_read,
151 	};
152 	struct device *dev = &pdev->dev;
153 	struct resource *res;
154 	struct brcm_nvram *priv;
155 	int err;
156 
157 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
158 	if (!priv)
159 		return -ENOMEM;
160 	priv->dev = dev;
161 
162 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
163 	priv->base = devm_ioremap_resource(dev, res);
164 	if (IS_ERR(priv->base))
165 		return PTR_ERR(priv->base);
166 
167 	err = brcm_nvram_parse(priv);
168 	if (err)
169 		return err;
170 
171 	bcm47xx_nvram_init_from_iomem(priv->base, resource_size(res));
172 
173 	config.dev = dev;
174 	config.cells = priv->cells;
175 	config.ncells = priv->ncells;
176 	config.priv = priv;
177 	config.size = resource_size(res);
178 
179 	return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
180 }
181 
182 static const struct of_device_id brcm_nvram_of_match_table[] = {
183 	{ .compatible = "brcm,nvram", },
184 	{},
185 };
186 
187 static struct platform_driver brcm_nvram_driver = {
188 	.probe = brcm_nvram_probe,
189 	.driver = {
190 		.name = "brcm_nvram",
191 		.of_match_table = brcm_nvram_of_match_table,
192 	},
193 };
194 
195 static int __init brcm_nvram_init(void)
196 {
197 	return platform_driver_register(&brcm_nvram_driver);
198 }
199 
200 subsys_initcall_sync(brcm_nvram_init);
201 
202 MODULE_AUTHOR("Rafał Miłecki");
203 MODULE_LICENSE("GPL");
204 MODULE_DEVICE_TABLE(of, brcm_nvram_of_match_table);
205