xref: /openbmc/linux/drivers/bus/vexpress-config.c (revision 3ddc8b84)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *
4  * Copyright (C) 2014 ARM Limited
5  */
6 
7 #include <linux/err.h>
8 #include <linux/init.h>
9 #include <linux/io.h>
10 #include <linux/module.h>
11 #include <linux/of.h>
12 #include <linux/platform_device.h>
13 #include <linux/of_platform.h>
14 #include <linux/sched/signal.h>
15 #include <linux/slab.h>
16 #include <linux/vexpress.h>
17 
18 #define SYS_MISC		0x0
19 #define SYS_MISC_MASTERSITE	(1 << 14)
20 
21 #define SYS_PROCID0		0x24
22 #define SYS_PROCID1		0x28
23 #define SYS_HBI_MASK		0xfff
24 #define SYS_PROCIDx_HBI_SHIFT	0
25 
26 #define SYS_CFGDATA		0x40
27 
28 #define SYS_CFGCTRL		0x44
29 #define SYS_CFGCTRL_START	(1 << 31)
30 #define SYS_CFGCTRL_WRITE	(1 << 30)
31 #define SYS_CFGCTRL_DCC(n)	(((n) & 0xf) << 26)
32 #define SYS_CFGCTRL_FUNC(n)	(((n) & 0x3f) << 20)
33 #define SYS_CFGCTRL_SITE(n)	(((n) & 0x3) << 16)
34 #define SYS_CFGCTRL_POSITION(n)	(((n) & 0xf) << 12)
35 #define SYS_CFGCTRL_DEVICE(n)	(((n) & 0xfff) << 0)
36 
37 #define SYS_CFGSTAT		0x48
38 #define SYS_CFGSTAT_ERR		(1 << 1)
39 #define SYS_CFGSTAT_COMPLETE	(1 << 0)
40 
41 #define VEXPRESS_SITE_MB		0
42 #define VEXPRESS_SITE_DB1		1
43 #define VEXPRESS_SITE_DB2		2
44 #define VEXPRESS_SITE_MASTER		0xf
45 
46 struct vexpress_syscfg {
47 	struct device *dev;
48 	void __iomem *base;
49 	struct list_head funcs;
50 };
51 
52 struct vexpress_syscfg_func {
53 	struct list_head list;
54 	struct vexpress_syscfg *syscfg;
55 	struct regmap *regmap;
56 	int num_templates;
57 	u32 template[]; /* Keep it last! */
58 };
59 
60 struct vexpress_config_bridge_ops {
61 	struct regmap * (*regmap_init)(struct device *dev, void *context);
62 	void (*regmap_exit)(struct regmap *regmap, void *context);
63 };
64 
65 struct vexpress_config_bridge {
66 	struct vexpress_config_bridge_ops *ops;
67 	void *context;
68 };
69 
70 
71 static DEFINE_MUTEX(vexpress_config_mutex);
72 static u32 vexpress_config_site_master = VEXPRESS_SITE_MASTER;
73 
74 
75 static void vexpress_config_set_master(u32 site)
76 {
77 	vexpress_config_site_master = site;
78 }
79 
80 static void vexpress_config_lock(void *arg)
81 {
82 	mutex_lock(&vexpress_config_mutex);
83 }
84 
85 static void vexpress_config_unlock(void *arg)
86 {
87 	mutex_unlock(&vexpress_config_mutex);
88 }
89 
90 
91 static void vexpress_config_find_prop(struct device_node *node,
92 		const char *name, u32 *val)
93 {
94 	/* Default value */
95 	*val = 0;
96 
97 	of_node_get(node);
98 	while (node) {
99 		if (of_property_read_u32(node, name, val) == 0) {
100 			of_node_put(node);
101 			return;
102 		}
103 		node = of_get_next_parent(node);
104 	}
105 }
106 
107 static int vexpress_config_get_topo(struct device_node *node, u32 *site,
108 		u32 *position, u32 *dcc)
109 {
110 	vexpress_config_find_prop(node, "arm,vexpress,site", site);
111 	if (*site == VEXPRESS_SITE_MASTER)
112 		*site = vexpress_config_site_master;
113 	if (WARN_ON(vexpress_config_site_master == VEXPRESS_SITE_MASTER))
114 		return -EINVAL;
115 	vexpress_config_find_prop(node, "arm,vexpress,position", position);
116 	vexpress_config_find_prop(node, "arm,vexpress,dcc", dcc);
117 
118 	return 0;
119 }
120 
121 
122 static void vexpress_config_devres_release(struct device *dev, void *res)
123 {
124 	struct vexpress_config_bridge *bridge = dev_get_drvdata(dev->parent);
125 	struct regmap *regmap = res;
126 
127 	bridge->ops->regmap_exit(regmap, bridge->context);
128 }
129 
130 struct regmap *devm_regmap_init_vexpress_config(struct device *dev)
131 {
132 	struct vexpress_config_bridge *bridge;
133 	struct regmap *regmap;
134 	struct regmap **res;
135 
136 	bridge = dev_get_drvdata(dev->parent);
137 	if (WARN_ON(!bridge))
138 		return ERR_PTR(-EINVAL);
139 
140 	res = devres_alloc(vexpress_config_devres_release, sizeof(*res),
141 			GFP_KERNEL);
142 	if (!res)
143 		return ERR_PTR(-ENOMEM);
144 
145 	regmap = (bridge->ops->regmap_init)(dev, bridge->context);
146 	if (IS_ERR(regmap)) {
147 		devres_free(res);
148 		return regmap;
149 	}
150 
151 	*res = regmap;
152 	devres_add(dev, res);
153 
154 	return regmap;
155 }
156 EXPORT_SYMBOL_GPL(devm_regmap_init_vexpress_config);
157 
158 static int vexpress_syscfg_exec(struct vexpress_syscfg_func *func,
159 		int index, bool write, u32 *data)
160 {
161 	struct vexpress_syscfg *syscfg = func->syscfg;
162 	u32 command, status;
163 	int tries;
164 	long timeout;
165 
166 	if (WARN_ON(index >= func->num_templates))
167 		return -EINVAL;
168 
169 	command = readl(syscfg->base + SYS_CFGCTRL);
170 	if (WARN_ON(command & SYS_CFGCTRL_START))
171 		return -EBUSY;
172 
173 	command = func->template[index];
174 	command |= SYS_CFGCTRL_START;
175 	command |= write ? SYS_CFGCTRL_WRITE : 0;
176 
177 	/* Use a canary for reads */
178 	if (!write)
179 		*data = 0xdeadbeef;
180 
181 	dev_dbg(syscfg->dev, "func %p, command %x, data %x\n",
182 			func, command, *data);
183 	writel(*data, syscfg->base + SYS_CFGDATA);
184 	writel(0, syscfg->base + SYS_CFGSTAT);
185 	writel(command, syscfg->base + SYS_CFGCTRL);
186 	mb();
187 
188 	/* The operation can take ages... Go to sleep, 100us initially */
189 	tries = 100;
190 	timeout = 100;
191 	do {
192 		if (!irqs_disabled()) {
193 			set_current_state(TASK_INTERRUPTIBLE);
194 			schedule_timeout(usecs_to_jiffies(timeout));
195 			if (signal_pending(current))
196 				return -EINTR;
197 		} else {
198 			udelay(timeout);
199 		}
200 
201 		status = readl(syscfg->base + SYS_CFGSTAT);
202 		if (status & SYS_CFGSTAT_ERR)
203 			return -EFAULT;
204 
205 		if (timeout > 20)
206 			timeout -= 20;
207 	} while (--tries && !(status & SYS_CFGSTAT_COMPLETE));
208 	if (WARN_ON_ONCE(!tries))
209 		return -ETIMEDOUT;
210 
211 	if (!write) {
212 		*data = readl(syscfg->base + SYS_CFGDATA);
213 		dev_dbg(syscfg->dev, "func %p, read data %x\n", func, *data);
214 	}
215 
216 	return 0;
217 }
218 
219 static int vexpress_syscfg_read(void *context, unsigned int index,
220 		unsigned int *val)
221 {
222 	struct vexpress_syscfg_func *func = context;
223 
224 	return vexpress_syscfg_exec(func, index, false, val);
225 }
226 
227 static int vexpress_syscfg_write(void *context, unsigned int index,
228 		unsigned int val)
229 {
230 	struct vexpress_syscfg_func *func = context;
231 
232 	return vexpress_syscfg_exec(func, index, true, &val);
233 }
234 
235 static struct regmap_config vexpress_syscfg_regmap_config = {
236 	.lock = vexpress_config_lock,
237 	.unlock = vexpress_config_unlock,
238 	.reg_bits = 32,
239 	.val_bits = 32,
240 	.reg_read = vexpress_syscfg_read,
241 	.reg_write = vexpress_syscfg_write,
242 	.reg_format_endian = REGMAP_ENDIAN_LITTLE,
243 	.val_format_endian = REGMAP_ENDIAN_LITTLE,
244 };
245 
246 
247 static struct regmap *vexpress_syscfg_regmap_init(struct device *dev,
248 		void *context)
249 {
250 	int err;
251 	struct vexpress_syscfg *syscfg = context;
252 	struct vexpress_syscfg_func *func;
253 	struct property *prop;
254 	const __be32 *val = NULL;
255 	__be32 energy_quirk[4];
256 	int num;
257 	u32 site, position, dcc;
258 	int i;
259 
260 	err = vexpress_config_get_topo(dev->of_node, &site,
261 				&position, &dcc);
262 	if (err)
263 		return ERR_PTR(err);
264 
265 	prop = of_find_property(dev->of_node,
266 			"arm,vexpress-sysreg,func", NULL);
267 	if (!prop)
268 		return ERR_PTR(-EINVAL);
269 
270 	num = prop->length / sizeof(u32) / 2;
271 	val = prop->value;
272 
273 	/*
274 	 * "arm,vexpress-energy" function used to be described
275 	 * by its first device only, now it requires both
276 	 */
277 	if (num == 1 && of_device_is_compatible(dev->of_node,
278 			"arm,vexpress-energy")) {
279 		num = 2;
280 		energy_quirk[0] = *val;
281 		energy_quirk[2] = *val++;
282 		energy_quirk[1] = *val;
283 		energy_quirk[3] = cpu_to_be32(be32_to_cpup(val) + 1);
284 		val = energy_quirk;
285 	}
286 
287 	func = kzalloc(struct_size(func, template, num), GFP_KERNEL);
288 	if (!func)
289 		return ERR_PTR(-ENOMEM);
290 
291 	func->syscfg = syscfg;
292 	func->num_templates = num;
293 
294 	for (i = 0; i < num; i++) {
295 		u32 function, device;
296 
297 		function = be32_to_cpup(val++);
298 		device = be32_to_cpup(val++);
299 
300 		dev_dbg(dev, "func %p: %u/%u/%u/%u/%u\n",
301 				func, site, position, dcc,
302 				function, device);
303 
304 		func->template[i] = SYS_CFGCTRL_DCC(dcc);
305 		func->template[i] |= SYS_CFGCTRL_SITE(site);
306 		func->template[i] |= SYS_CFGCTRL_POSITION(position);
307 		func->template[i] |= SYS_CFGCTRL_FUNC(function);
308 		func->template[i] |= SYS_CFGCTRL_DEVICE(device);
309 	}
310 
311 	vexpress_syscfg_regmap_config.max_register = num - 1;
312 
313 	func->regmap = regmap_init(dev, NULL, func,
314 			&vexpress_syscfg_regmap_config);
315 
316 	if (IS_ERR(func->regmap)) {
317 		void *err = func->regmap;
318 
319 		kfree(func);
320 		return err;
321 	}
322 
323 	list_add(&func->list, &syscfg->funcs);
324 
325 	return func->regmap;
326 }
327 
328 static void vexpress_syscfg_regmap_exit(struct regmap *regmap, void *context)
329 {
330 	struct vexpress_syscfg *syscfg = context;
331 	struct vexpress_syscfg_func *func, *tmp;
332 
333 	regmap_exit(regmap);
334 
335 	list_for_each_entry_safe(func, tmp, &syscfg->funcs, list) {
336 		if (func->regmap == regmap) {
337 			list_del(&syscfg->funcs);
338 			kfree(func);
339 			break;
340 		}
341 	}
342 }
343 
344 static struct vexpress_config_bridge_ops vexpress_syscfg_bridge_ops = {
345 	.regmap_init = vexpress_syscfg_regmap_init,
346 	.regmap_exit = vexpress_syscfg_regmap_exit,
347 };
348 
349 
350 static int vexpress_syscfg_probe(struct platform_device *pdev)
351 {
352 	struct vexpress_syscfg *syscfg;
353 	struct vexpress_config_bridge *bridge;
354 	struct device_node *node;
355 	int master;
356 	u32 dt_hbi;
357 
358 	syscfg = devm_kzalloc(&pdev->dev, sizeof(*syscfg), GFP_KERNEL);
359 	if (!syscfg)
360 		return -ENOMEM;
361 	syscfg->dev = &pdev->dev;
362 	INIT_LIST_HEAD(&syscfg->funcs);
363 
364 	syscfg->base = devm_platform_ioremap_resource(pdev, 0);
365 	if (IS_ERR(syscfg->base))
366 		return PTR_ERR(syscfg->base);
367 
368 	bridge = devm_kmalloc(&pdev->dev, sizeof(*bridge), GFP_KERNEL);
369 	if (!bridge)
370 		return -ENOMEM;
371 
372 	bridge->ops = &vexpress_syscfg_bridge_ops;
373 	bridge->context = syscfg;
374 
375 	dev_set_drvdata(&pdev->dev, bridge);
376 
377 	master = readl(syscfg->base + SYS_MISC) & SYS_MISC_MASTERSITE ?
378 			VEXPRESS_SITE_DB2 : VEXPRESS_SITE_DB1;
379 	vexpress_config_set_master(master);
380 
381 	/* Confirm board type against DT property, if available */
382 	if (of_property_read_u32(of_root, "arm,hbi", &dt_hbi) == 0) {
383 		u32 id = readl(syscfg->base + (master == VEXPRESS_SITE_DB1 ?
384 				 SYS_PROCID0 : SYS_PROCID1));
385 		u32 hbi = (id >> SYS_PROCIDx_HBI_SHIFT) & SYS_HBI_MASK;
386 
387 		if (WARN_ON(dt_hbi != hbi))
388 			dev_warn(&pdev->dev, "DT HBI (%x) is not matching hardware (%x)!\n",
389 					dt_hbi, hbi);
390 	}
391 
392 	for_each_compatible_node(node, NULL, "arm,vexpress,config-bus") {
393 		struct device_node *bridge_np;
394 
395 		bridge_np = of_parse_phandle(node, "arm,vexpress,config-bridge", 0);
396 		if (bridge_np != pdev->dev.parent->of_node)
397 			continue;
398 
399 		of_platform_populate(node, NULL, NULL, &pdev->dev);
400 	}
401 
402 	return 0;
403 }
404 
405 static const struct platform_device_id vexpress_syscfg_id_table[] = {
406 	{ "vexpress-syscfg", },
407 	{},
408 };
409 MODULE_DEVICE_TABLE(platform, vexpress_syscfg_id_table);
410 
411 static struct platform_driver vexpress_syscfg_driver = {
412 	.driver.name = "vexpress-syscfg",
413 	.id_table = vexpress_syscfg_id_table,
414 	.probe = vexpress_syscfg_probe,
415 };
416 module_platform_driver(vexpress_syscfg_driver);
417 MODULE_LICENSE("GPL v2");
418