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