1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2021 Rafał Miłecki <rafal@milecki.pl>
7 #include <linux/mod_devicetable.h>
8 #include <linux/module.h>
9 #include <linux/nvmem-consumer.h>
10 #include <linux/nvmem-provider.h>
12 #include <linux/platform_device.h>
13 #include <linux/slab.h>
15 #define NVRAM_MAGIC "FLSH"
20 struct nvmem_cell_info *cells;
24 struct brcm_nvram_header {
27 __le32 crc_ver_init; /* 0:7 crc, 8:15 ver, 16:31 sdram_init */
28 __le32 config_refresh; /* 0:15 sdram_config, 16:31 sdram_refresh */
29 __le32 config_ncdl; /* ncdl values for memc */
32 static int brcm_nvram_read(void *context, unsigned int offset, void *val,
35 struct brcm_nvram *priv = context;
39 *dst++ = readb(priv->base + offset++);
44 static int brcm_nvram_add_cells(struct brcm_nvram *priv, uint8_t *data,
47 struct device *dev = priv->dev;
48 char *var, *value, *eq;
52 for (var = data + sizeof(struct brcm_nvram_header);
53 var < (char *)data + len && *var;
54 var += strlen(var) + 1) {
58 priv->cells = devm_kcalloc(dev, priv->ncells, sizeof(*priv->cells), GFP_KERNEL);
62 for (var = data + sizeof(struct brcm_nvram_header), idx = 0;
63 var < (char *)data + len && *var;
64 var = value + strlen(value) + 1, idx++) {
65 eq = strchr(var, '=');
71 priv->cells[idx].name = devm_kstrdup(dev, var, GFP_KERNEL);
72 if (!priv->cells[idx].name)
74 priv->cells[idx].offset = value - (char *)data;
75 priv->cells[idx].bytes = strlen(value);
76 priv->cells[idx].np = of_get_child_by_name(dev->of_node, priv->cells[idx].name);
82 static int brcm_nvram_parse(struct brcm_nvram *priv)
84 struct device *dev = priv->dev;
85 struct brcm_nvram_header header;
90 memcpy_fromio(&header, priv->base, sizeof(header));
92 if (memcmp(header.magic, NVRAM_MAGIC, 4)) {
93 dev_err(dev, "Invalid NVRAM magic\n");
97 len = le32_to_cpu(header.len);
99 data = kcalloc(1, len, GFP_KERNEL);
100 memcpy_fromio(data, priv->base, len);
101 data[len - 1] = '\0';
103 err = brcm_nvram_add_cells(priv, data, len);
105 dev_err(dev, "Failed to add cells: %d\n", err);
114 static int brcm_nvram_probe(struct platform_device *pdev)
116 struct nvmem_config config = {
117 .name = "brcm-nvram",
118 .reg_read = brcm_nvram_read,
120 struct device *dev = &pdev->dev;
121 struct resource *res;
122 struct brcm_nvram *priv;
125 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
130 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
131 priv->base = devm_ioremap_resource(dev, res);
132 if (IS_ERR(priv->base))
133 return PTR_ERR(priv->base);
135 err = brcm_nvram_parse(priv);
140 config.cells = priv->cells;
141 config.ncells = priv->ncells;
143 config.size = resource_size(res);
145 return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
148 static const struct of_device_id brcm_nvram_of_match_table[] = {
149 { .compatible = "brcm,nvram", },
153 static struct platform_driver brcm_nvram_driver = {
154 .probe = brcm_nvram_probe,
156 .name = "brcm_nvram",
157 .of_match_table = brcm_nvram_of_match_table,
161 static int __init brcm_nvram_init(void)
163 return platform_driver_register(&brcm_nvram_driver);
166 subsys_initcall_sync(brcm_nvram_init);
168 MODULE_AUTHOR("Rafał Miłecki");
169 MODULE_LICENSE("GPL");
170 MODULE_DEVICE_TABLE(of, brcm_nvram_of_match_table);