Linux 6.13-rc4
[linux.git] / drivers / nvmem / brcm_nvram.c
blobb810df727b446b1762a1851750f743e0de6e8788
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2021 Rafał Miłecki <rafal@milecki.pl>
4 */
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>
18 #define NVRAM_MAGIC "FLSH"
20 /**
21 * struct brcm_nvram - driver state internal struct
23 * @dev: NVMEM device pointer
24 * @nvmem_size: Size of the whole space available for NVRAM
25 * @data: NVRAM data copy stored to avoid poking underlying flash controller
26 * @data_len: NVRAM data size
27 * @padding_byte: Padding value used to fill remaining space
28 * @cells: Array of discovered NVMEM cells
29 * @ncells: Number of elements in cells
31 struct brcm_nvram {
32 struct device *dev;
33 size_t nvmem_size;
34 uint8_t *data;
35 size_t data_len;
36 uint8_t padding_byte;
37 struct nvmem_cell_info *cells;
38 int ncells;
41 struct brcm_nvram_header {
42 char magic[4];
43 __le32 len;
44 __le32 crc_ver_init; /* 0:7 crc, 8:15 ver, 16:31 sdram_init */
45 __le32 config_refresh; /* 0:15 sdram_config, 16:31 sdram_refresh */
46 __le32 config_ncdl; /* ncdl values for memc */
49 static int brcm_nvram_read(void *context, unsigned int offset, void *val,
50 size_t bytes)
52 struct brcm_nvram *priv = context;
53 size_t to_copy;
55 if (offset + bytes > priv->data_len)
56 to_copy = max_t(ssize_t, (ssize_t)priv->data_len - offset, 0);
57 else
58 to_copy = bytes;
60 memcpy(val, priv->data + offset, to_copy);
62 memset((uint8_t *)val + to_copy, priv->padding_byte, bytes - to_copy);
64 return 0;
67 static int brcm_nvram_copy_data(struct brcm_nvram *priv, struct platform_device *pdev)
69 struct resource *res;
70 void __iomem *base;
72 base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
73 if (IS_ERR(base))
74 return PTR_ERR(base);
76 priv->nvmem_size = resource_size(res);
78 priv->padding_byte = readb(base + priv->nvmem_size - 1);
79 for (priv->data_len = priv->nvmem_size;
80 priv->data_len;
81 priv->data_len--) {
82 if (readb(base + priv->data_len - 1) != priv->padding_byte)
83 break;
85 WARN(priv->data_len > SZ_128K, "Unexpected (big) NVRAM size: %zu B\n", priv->data_len);
87 priv->data = devm_kzalloc(priv->dev, priv->data_len, GFP_KERNEL);
88 if (!priv->data)
89 return -ENOMEM;
91 memcpy_fromio(priv->data, base, priv->data_len);
93 bcm47xx_nvram_init_from_iomem(base, priv->data_len);
95 return 0;
98 static int brcm_nvram_read_post_process_macaddr(void *context, const char *id, int index,
99 unsigned int offset, void *buf, size_t bytes)
101 u8 mac[ETH_ALEN];
103 if (bytes != 3 * ETH_ALEN - 1)
104 return -EINVAL;
106 if (!mac_pton(buf, mac))
107 return -EINVAL;
109 if (index)
110 eth_addr_add(mac, index);
112 ether_addr_copy(buf, mac);
114 return 0;
117 static int brcm_nvram_add_cells(struct brcm_nvram *priv, uint8_t *data,
118 size_t len)
120 struct device *dev = priv->dev;
121 char *var, *value;
122 uint8_t tmp;
123 int idx;
124 int err = 0;
126 tmp = priv->data[len - 1];
127 priv->data[len - 1] = '\0';
129 priv->ncells = 0;
130 for (var = data + sizeof(struct brcm_nvram_header);
131 var < (char *)data + len && *var;
132 var += strlen(var) + 1) {
133 priv->ncells++;
136 priv->cells = devm_kcalloc(dev, priv->ncells, sizeof(*priv->cells), GFP_KERNEL);
137 if (!priv->cells) {
138 err = -ENOMEM;
139 goto out;
142 for (var = data + sizeof(struct brcm_nvram_header), idx = 0;
143 var < (char *)data + len && *var;
144 var = value + strlen(value) + 1, idx++) {
145 char *eq, *name;
147 eq = strchr(var, '=');
148 if (!eq)
149 break;
150 *eq = '\0';
151 name = devm_kstrdup(dev, var, GFP_KERNEL);
152 *eq = '=';
153 if (!name) {
154 err = -ENOMEM;
155 goto out;
157 value = eq + 1;
159 priv->cells[idx].name = name;
160 priv->cells[idx].offset = value - (char *)data;
161 priv->cells[idx].bytes = strlen(value);
162 priv->cells[idx].np = of_get_child_by_name(dev->of_node, priv->cells[idx].name);
163 if (!strcmp(name, "et0macaddr") ||
164 !strcmp(name, "et1macaddr") ||
165 !strcmp(name, "et2macaddr")) {
166 priv->cells[idx].raw_len = strlen(value);
167 priv->cells[idx].bytes = ETH_ALEN;
168 priv->cells[idx].read_post_process = brcm_nvram_read_post_process_macaddr;
172 out:
173 priv->data[len - 1] = tmp;
174 return err;
177 static int brcm_nvram_parse(struct brcm_nvram *priv)
179 struct brcm_nvram_header *header = (struct brcm_nvram_header *)priv->data;
180 struct device *dev = priv->dev;
181 size_t len;
182 int err;
184 if (memcmp(header->magic, NVRAM_MAGIC, 4)) {
185 dev_err(dev, "Invalid NVRAM magic\n");
186 return -EINVAL;
189 len = le32_to_cpu(header->len);
190 if (len > priv->nvmem_size) {
191 dev_err(dev, "NVRAM length (%zd) exceeds mapped size (%zd)\n", len,
192 priv->nvmem_size);
193 return -EINVAL;
196 err = brcm_nvram_add_cells(priv, priv->data, len);
197 if (err)
198 dev_err(dev, "Failed to add cells: %d\n", err);
200 return 0;
203 static int brcm_nvram_probe(struct platform_device *pdev)
205 struct nvmem_config config = {
206 .name = "brcm-nvram",
207 .reg_read = brcm_nvram_read,
209 struct device *dev = &pdev->dev;
210 struct brcm_nvram *priv;
211 int err;
213 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
214 if (!priv)
215 return -ENOMEM;
216 priv->dev = dev;
218 err = brcm_nvram_copy_data(priv, pdev);
219 if (err)
220 return err;
222 err = brcm_nvram_parse(priv);
223 if (err)
224 return err;
226 config.dev = dev;
227 config.cells = priv->cells;
228 config.ncells = priv->ncells;
229 config.priv = priv;
230 config.size = priv->nvmem_size;
232 return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
235 static const struct of_device_id brcm_nvram_of_match_table[] = {
236 { .compatible = "brcm,nvram", },
240 static struct platform_driver brcm_nvram_driver = {
241 .probe = brcm_nvram_probe,
242 .driver = {
243 .name = "brcm_nvram",
244 .of_match_table = brcm_nvram_of_match_table,
248 static int __init brcm_nvram_init(void)
250 return platform_driver_register(&brcm_nvram_driver);
253 subsys_initcall_sync(brcm_nvram_init);
255 MODULE_AUTHOR("Rafał Miłecki");
256 MODULE_DESCRIPTION("Broadcom I/O-mapped NVRAM support driver");
257 MODULE_LICENSE("GPL");
258 MODULE_DEVICE_TABLE(of, brcm_nvram_of_match_table);