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 
brcm_nvram_read(void * context,unsigned int offset,void * val,size_t bytes)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 
brcm_nvram_add_cells(struct brcm_nvram * priv,uint8_t * data,size_t len)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 
brcm_nvram_parse(struct brcm_nvram * priv)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 	if (!data)
102 		return -ENOMEM;
103 
104 	memcpy_fromio(data, priv->base, len);
105 	data[len - 1] = '\0';
106 
107 	err = brcm_nvram_add_cells(priv, data, len);
108 	if (err) {
109 		dev_err(dev, "Failed to add cells: %d\n", err);
110 		return err;
111 	}
112 
113 	kfree(data);
114 
115 	return 0;
116 }
117 
brcm_nvram_probe(struct platform_device * pdev)118 static int brcm_nvram_probe(struct platform_device *pdev)
119 {
120 	struct nvmem_config config = {
121 		.name = "brcm-nvram",
122 		.reg_read = brcm_nvram_read,
123 	};
124 	struct device *dev = &pdev->dev;
125 	struct resource *res;
126 	struct brcm_nvram *priv;
127 	int err;
128 
129 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
130 	if (!priv)
131 		return -ENOMEM;
132 	priv->dev = dev;
133 
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);
138 
139 	err = brcm_nvram_parse(priv);
140 	if (err)
141 		return err;
142 
143 	bcm47xx_nvram_init_from_iomem(priv->base, resource_size(res));
144 
145 	config.dev = dev;
146 	config.cells = priv->cells;
147 	config.ncells = priv->ncells;
148 	config.priv = priv;
149 	config.size = resource_size(res);
150 
151 	return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
152 }
153 
154 static const struct of_device_id brcm_nvram_of_match_table[] = {
155 	{ .compatible = "brcm,nvram", },
156 	{},
157 };
158 
159 static struct platform_driver brcm_nvram_driver = {
160 	.probe = brcm_nvram_probe,
161 	.driver = {
162 		.name = "brcm_nvram",
163 		.of_match_table = brcm_nvram_of_match_table,
164 	},
165 };
166 
brcm_nvram_init(void)167 static int __init brcm_nvram_init(void)
168 {
169 	return platform_driver_register(&brcm_nvram_driver);
170 }
171 
172 subsys_initcall_sync(brcm_nvram_init);
173 
174 MODULE_AUTHOR("Rafał Miłecki");
175 MODULE_LICENSE("GPL");
176 MODULE_DEVICE_TABLE(of, brcm_nvram_of_match_table);
177