1 From 6e977eaa8280e957b87904b536661550f2a6b3e8 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
3 Date: Fri, 25 Feb 2022 17:58:20 +0000
4 Subject: [PATCH] nvmem: brcm_nvram: parse NVRAM content into NVMEM cells
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 NVRAM consist of header and NUL separated key-value pairs. Parse it and
10 create NVMEM cell for every key-value entry.
12 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
13 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
14 Link: https://lore.kernel.org/r/20220225175822.8293-3-srinivas.kandagatla@linaro.org
15 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
17 drivers/nvmem/brcm_nvram.c | 90 ++++++++++++++++++++++++++++++++++++++
18 1 file changed, 90 insertions(+)
20 --- a/drivers/nvmem/brcm_nvram.c
21 +++ b/drivers/nvmem/brcm_nvram.c
24 #include <linux/mod_devicetable.h>
25 #include <linux/module.h>
26 +#include <linux/nvmem-consumer.h>
27 #include <linux/nvmem-provider.h>
28 #include <linux/platform_device.h>
29 +#include <linux/slab.h>
31 +#define NVRAM_MAGIC "FLSH"
36 + struct nvmem_cell_info *cells;
40 +struct brcm_nvram_header {
43 + __le32 crc_ver_init; /* 0:7 crc, 8:15 ver, 16:31 sdram_init */
44 + __le32 config_refresh; /* 0:15 sdram_config, 16:31 sdram_refresh */
45 + __le32 config_ncdl; /* ncdl values for memc */
48 static int brcm_nvram_read(void *context, unsigned int offset, void *val,
49 @@ -26,6 +40,75 @@ static int brcm_nvram_read(void *context
53 +static int brcm_nvram_add_cells(struct brcm_nvram *priv, uint8_t *data,
56 + struct device *dev = priv->dev;
57 + char *var, *value, *eq;
61 + for (var = data + sizeof(struct brcm_nvram_header);
62 + var < (char *)data + len && *var;
63 + var += strlen(var) + 1) {
67 + priv->cells = devm_kcalloc(dev, priv->ncells, sizeof(*priv->cells), GFP_KERNEL);
71 + for (var = data + sizeof(struct brcm_nvram_header), idx = 0;
72 + var < (char *)data + len && *var;
73 + var = value + strlen(value) + 1, idx++) {
74 + eq = strchr(var, '=');
80 + priv->cells[idx].name = devm_kstrdup(dev, var, GFP_KERNEL);
81 + if (!priv->cells[idx].name)
83 + priv->cells[idx].offset = value - (char *)data;
84 + priv->cells[idx].bytes = strlen(value);
90 +static int brcm_nvram_parse(struct brcm_nvram *priv)
92 + struct device *dev = priv->dev;
93 + struct brcm_nvram_header header;
98 + memcpy_fromio(&header, priv->base, sizeof(header));
100 + if (memcmp(header.magic, NVRAM_MAGIC, 4)) {
101 + dev_err(dev, "Invalid NVRAM magic\n");
105 + len = le32_to_cpu(header.len);
107 + data = kcalloc(1, len, GFP_KERNEL);
108 + memcpy_fromio(data, priv->base, len);
109 + data[len - 1] = '\0';
111 + err = brcm_nvram_add_cells(priv, data, len);
113 + dev_err(dev, "Failed to add cells: %d\n", err);
122 static int brcm_nvram_probe(struct platform_device *pdev)
124 struct nvmem_config config = {
125 @@ -35,6 +118,7 @@ static int brcm_nvram_probe(struct platf
126 struct device *dev = &pdev->dev;
127 struct resource *res;
128 struct brcm_nvram *priv;
131 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
133 @@ -46,7 +130,13 @@ static int brcm_nvram_probe(struct platf
134 if (IS_ERR(priv->base))
135 return PTR_ERR(priv->base);
137 + err = brcm_nvram_parse(priv);
142 + config.cells = priv->cells;
143 + config.ncells = priv->ncells;
145 config.size = resource_size(res);