99781b3a7b3a791181e84ce9a1c8c99ca51b1628
[openwrt/staging/jogo.git] /
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
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 NVRAM consist of header and NUL separated key-value pairs. Parse it and
10 create NVMEM cell for every key-value entry.
11
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>
16 ---
17 drivers/nvmem/brcm_nvram.c | 90 ++++++++++++++++++++++++++++++++++++++
18 1 file changed, 90 insertions(+)
19
20 --- a/drivers/nvmem/brcm_nvram.c
21 +++ b/drivers/nvmem/brcm_nvram.c
22 @@ -6,12 +6,26 @@
23 #include <linux/io.h>
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>
30 +
31 +#define NVRAM_MAGIC "FLSH"
32
33 struct brcm_nvram {
34 struct device *dev;
35 void __iomem *base;
36 + struct nvmem_cell_info *cells;
37 + int ncells;
38 +};
39 +
40 +struct brcm_nvram_header {
41 + char magic[4];
42 + __le32 len;
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 */
46 };
47
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
50 return 0;
51 }
52
53 +static int brcm_nvram_add_cells(struct brcm_nvram *priv, uint8_t *data,
54 + size_t len)
55 +{
56 + struct device *dev = priv->dev;
57 + char *var, *value, *eq;
58 + int idx;
59 +
60 + priv->ncells = 0;
61 + for (var = data + sizeof(struct brcm_nvram_header);
62 + var < (char *)data + len && *var;
63 + var += strlen(var) + 1) {
64 + priv->ncells++;
65 + }
66 +
67 + priv->cells = devm_kcalloc(dev, priv->ncells, sizeof(*priv->cells), GFP_KERNEL);
68 + if (!priv->cells)
69 + return -ENOMEM;
70 +
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, '=');
75 + if (!eq)
76 + break;
77 + *eq = '\0';
78 + value = eq + 1;
79 +
80 + priv->cells[idx].name = devm_kstrdup(dev, var, GFP_KERNEL);
81 + if (!priv->cells[idx].name)
82 + return -ENOMEM;
83 + priv->cells[idx].offset = value - (char *)data;
84 + priv->cells[idx].bytes = strlen(value);
85 + }
86 +
87 + return 0;
88 +}
89 +
90 +static int brcm_nvram_parse(struct brcm_nvram *priv)
91 +{
92 + struct device *dev = priv->dev;
93 + struct brcm_nvram_header header;
94 + uint8_t *data;
95 + size_t len;
96 + int err;
97 +
98 + memcpy_fromio(&header, priv->base, sizeof(header));
99 +
100 + if (memcmp(header.magic, NVRAM_MAGIC, 4)) {
101 + dev_err(dev, "Invalid NVRAM magic\n");
102 + return -EINVAL;
103 + }
104 +
105 + len = le32_to_cpu(header.len);
106 +
107 + data = kcalloc(1, len, GFP_KERNEL);
108 + memcpy_fromio(data, priv->base, len);
109 + data[len - 1] = '\0';
110 +
111 + err = brcm_nvram_add_cells(priv, data, len);
112 + if (err) {
113 + dev_err(dev, "Failed to add cells: %d\n", err);
114 + return err;
115 + }
116 +
117 + kfree(data);
118 +
119 + return 0;
120 +}
121 +
122 static int brcm_nvram_probe(struct platform_device *pdev)
123 {
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;
129 + int err;
130
131 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
132 if (!priv)
133 @@ -46,7 +130,13 @@ static int brcm_nvram_probe(struct platf
134 if (IS_ERR(priv->base))
135 return PTR_ERR(priv->base);
136
137 + err = brcm_nvram_parse(priv);
138 + if (err)
139 + return err;
140 +
141 config.dev = dev;
142 + config.cells = priv->cells;
143 + config.ncells = priv->ncells;
144 config.priv = priv;
145 config.size = resource_size(res);
146