1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2021 Rafał Miłecki <rafal@milecki.pl>
6 #include <linux/bcm47xx_nvram.h>
8 #include <linux/mod_devicetable.h>
9 #include <linux/module.h>
10 #include <linux/nvmem-consumer.h>
11 #include <linux/nvmem-provider.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
16 #define NVRAM_MAGIC "FLSH"
21 struct nvmem_cell_info *cells;
25 struct brcm_nvram_header {
28 __le32 crc_ver_init; /* 0:7 crc, 8:15 ver, 16:31 sdram_init */
29 __le32 config_refresh; /* 0:15 sdram_config, 16:31 sdram_refresh */
30 __le32 config_ncdl; /* ncdl values for memc */
33 static int brcm_nvram_read(void *context, unsigned int offset, void *val,
36 struct brcm_nvram *priv = context;
40 *dst++ = readb(priv->base + offset++);
45 static int brcm_nvram_add_cells(struct brcm_nvram *priv, uint8_t *data,
48 struct device *dev = priv->dev;
49 char *var, *value, *eq;
53 for (var = data + sizeof(struct brcm_nvram_header);
54 var < (char *)data + len && *var;
55 var += strlen(var) + 1) {
59 priv->cells = devm_kcalloc(dev, priv->ncells, sizeof(*priv->cells), GFP_KERNEL);
63 for (var = data + sizeof(struct brcm_nvram_header), idx = 0;
64 var < (char *)data + len && *var;
65 var = value + strlen(value) + 1, idx++) {
66 eq = strchr(var, '=');
72 priv->cells[idx].name = devm_kstrdup(dev, var, GFP_KERNEL);
73 if (!priv->cells[idx].name)
75 priv->cells[idx].offset = value - (char *)data;
76 priv->cells[idx].bytes = strlen(value);
77 priv->cells[idx].np = of_get_child_by_name(dev->of_node, priv->cells[idx].name);
83 static int brcm_nvram_parse(struct brcm_nvram *priv)
85 struct device *dev = priv->dev;
86 struct brcm_nvram_header header;
91 memcpy_fromio(&header, priv->base, sizeof(header));
93 if (memcmp(header.magic, NVRAM_MAGIC, 4)) {
94 dev_err(dev, "Invalid NVRAM magic\n");
98 len = le32_to_cpu(header.len);
100 data = kzalloc(len, GFP_KERNEL);
104 memcpy_fromio(data, priv->base, len);
105 data[len - 1] = '\0';
107 err = brcm_nvram_add_cells(priv, data, len);
109 dev_err(dev, "Failed to add cells: %d\n", err);
118 static int brcm_nvram_probe(struct platform_device *pdev)
120 struct nvmem_config config = {
121 .name = "brcm-nvram",
122 .reg_read = brcm_nvram_read,
124 struct device *dev = &pdev->dev;
125 struct resource *res;
126 struct brcm_nvram *priv;
129 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
134 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
135 priv->base = devm_ioremap_resource(dev, res);
136 if (IS_ERR(priv->base))
137 return PTR_ERR(priv->base);
139 err = brcm_nvram_parse(priv);
143 bcm47xx_nvram_init_from_iomem(priv->base, resource_size(res));
146 config.cells = priv->cells;
147 config.ncells = priv->ncells;
149 config.size = resource_size(res);
151 return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
154 static const struct of_device_id brcm_nvram_of_match_table[] = {
155 { .compatible = "brcm,nvram", },
159 static struct platform_driver brcm_nvram_driver = {
160 .probe = brcm_nvram_probe,
162 .name = "brcm_nvram",
163 .of_match_table = brcm_nvram_of_match_table,
167 static int __init brcm_nvram_init(void)
169 return platform_driver_register(&brcm_nvram_driver);
172 subsys_initcall_sync(brcm_nvram_init);
174 MODULE_AUTHOR("Rafał Miłecki");
175 MODULE_LICENSE("GPL");
176 MODULE_DEVICE_TABLE(of, brcm_nvram_of_match_table);