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