xref: /openbmc/linux/drivers/nvmem/brcm_nvram.c (revision 162736b0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2021 Rafał Miłecki <rafal@milecki.pl>
4  */
5 
6 #include <linux/bcm47xx_nvram.h>
7 #include <linux/io.h>
8 #include <linux/mod_devicetable.h>
9 #include <linux/module.h>
10 #include <linux/nvmem-consumer.h>
11 #include <linux/nvmem-provider.h>
12 #include <linux/of.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 
16 #define NVRAM_MAGIC			"FLSH"
17 
18 struct brcm_nvram {
19 	struct device *dev;
20 	void __iomem *base;
21 	struct nvmem_cell_info *cells;
22 	int ncells;
23 };
24 
25 struct brcm_nvram_header {
26 	char magic[4];
27 	__le32 len;
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 */
31 };
32 
33 static int brcm_nvram_read(void *context, unsigned int offset, void *val,
34 			   size_t bytes)
35 {
36 	struct brcm_nvram *priv = context;
37 	u8 *dst = val;
38 
39 	while (bytes--)
40 		*dst++ = readb(priv->base + offset++);
41 
42 	return 0;
43 }
44 
45 static int brcm_nvram_add_cells(struct brcm_nvram *priv, uint8_t *data,
46 				size_t len)
47 {
48 	struct device *dev = priv->dev;
49 	char *var, *value, *eq;
50 	int idx;
51 
52 	priv->ncells = 0;
53 	for (var = data + sizeof(struct brcm_nvram_header);
54 	     var < (char *)data + len && *var;
55 	     var += strlen(var) + 1) {
56 		priv->ncells++;
57 	}
58 
59 	priv->cells = devm_kcalloc(dev, priv->ncells, sizeof(*priv->cells), GFP_KERNEL);
60 	if (!priv->cells)
61 		return -ENOMEM;
62 
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, '=');
67 		if (!eq)
68 			break;
69 		*eq = '\0';
70 		value = eq + 1;
71 
72 		priv->cells[idx].name = devm_kstrdup(dev, var, GFP_KERNEL);
73 		if (!priv->cells[idx].name)
74 			return -ENOMEM;
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);
78 	}
79 
80 	return 0;
81 }
82 
83 static int brcm_nvram_parse(struct brcm_nvram *priv)
84 {
85 	struct device *dev = priv->dev;
86 	struct brcm_nvram_header header;
87 	uint8_t *data;
88 	size_t len;
89 	int err;
90 
91 	memcpy_fromio(&header, priv->base, sizeof(header));
92 
93 	if (memcmp(header.magic, NVRAM_MAGIC, 4)) {
94 		dev_err(dev, "Invalid NVRAM magic\n");
95 		return -EINVAL;
96 	}
97 
98 	len = le32_to_cpu(header.len);
99 
100 	data = kzalloc(len, GFP_KERNEL);
101 	memcpy_fromio(data, priv->base, len);
102 	data[len - 1] = '\0';
103 
104 	err = brcm_nvram_add_cells(priv, data, len);
105 	if (err) {
106 		dev_err(dev, "Failed to add cells: %d\n", err);
107 		return err;
108 	}
109 
110 	kfree(data);
111 
112 	return 0;
113 }
114 
115 static int brcm_nvram_probe(struct platform_device *pdev)
116 {
117 	struct nvmem_config config = {
118 		.name = "brcm-nvram",
119 		.reg_read = brcm_nvram_read,
120 	};
121 	struct device *dev = &pdev->dev;
122 	struct resource *res;
123 	struct brcm_nvram *priv;
124 	int err;
125 
126 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
127 	if (!priv)
128 		return -ENOMEM;
129 	priv->dev = dev;
130 
131 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
132 	priv->base = devm_ioremap_resource(dev, res);
133 	if (IS_ERR(priv->base))
134 		return PTR_ERR(priv->base);
135 
136 	err = brcm_nvram_parse(priv);
137 	if (err)
138 		return err;
139 
140 	bcm47xx_nvram_init_from_iomem(priv->base, resource_size(res));
141 
142 	config.dev = dev;
143 	config.cells = priv->cells;
144 	config.ncells = priv->ncells;
145 	config.priv = priv;
146 	config.size = resource_size(res);
147 
148 	return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
149 }
150 
151 static const struct of_device_id brcm_nvram_of_match_table[] = {
152 	{ .compatible = "brcm,nvram", },
153 	{},
154 };
155 
156 static struct platform_driver brcm_nvram_driver = {
157 	.probe = brcm_nvram_probe,
158 	.driver = {
159 		.name = "brcm_nvram",
160 		.of_match_table = brcm_nvram_of_match_table,
161 	},
162 };
163 
164 static int __init brcm_nvram_init(void)
165 {
166 	return platform_driver_register(&brcm_nvram_driver);
167 }
168 
169 subsys_initcall_sync(brcm_nvram_init);
170 
171 MODULE_AUTHOR("Rafał Miłecki");
172 MODULE_LICENSE("GPL");
173 MODULE_DEVICE_TABLE(of, brcm_nvram_of_match_table);
174