hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/bus/vexpress-config.c
....@@ -1,22 +1,66 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
2
- * This program is free software; you can redistribute it and/or modify
3
- * it under the terms of the GNU General Public License version 2 as
4
- * published by the Free Software Foundation.
5
- *
6
- * This program is distributed in the hope that it will be useful,
7
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
8
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9
- * GNU General Public License for more details.
103 *
114 * Copyright (C) 2014 ARM Limited
125 */
136
147 #include <linux/err.h>
158 #include <linux/init.h>
9
+#include <linux/io.h>
10
+#include <linux/module.h>
1611 #include <linux/of.h>
12
+#include <linux/platform_device.h>
1713 #include <linux/of_device.h>
14
+#include <linux/sched/signal.h>
15
+#include <linux/slab.h>
1816 #include <linux/vexpress.h>
1917
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
+};
2064
2165 struct vexpress_config_bridge {
2266 struct vexpress_config_bridge_ops *ops;
....@@ -25,26 +69,20 @@
2569
2670
2771 static DEFINE_MUTEX(vexpress_config_mutex);
28
-static struct class *vexpress_config_class;
2972 static u32 vexpress_config_site_master = VEXPRESS_SITE_MASTER;
3073
3174
32
-void vexpress_config_set_master(u32 site)
75
+static void vexpress_config_set_master(u32 site)
3376 {
3477 vexpress_config_site_master = site;
3578 }
3679
37
-u32 vexpress_config_get_master(void)
38
-{
39
- return vexpress_config_site_master;
40
-}
41
-
42
-void vexpress_config_lock(void *arg)
80
+static void vexpress_config_lock(void *arg)
4381 {
4482 mutex_lock(&vexpress_config_mutex);
4583 }
4684
47
-void vexpress_config_unlock(void *arg)
85
+static void vexpress_config_unlock(void *arg)
4886 {
4987 mutex_unlock(&vexpress_config_mutex);
5088 }
....@@ -66,7 +104,7 @@
66104 }
67105 }
68106
69
-int vexpress_config_get_topo(struct device_node *node, u32 *site,
107
+static int vexpress_config_get_topo(struct device_node *node, u32 *site,
70108 u32 *position, u32 *dcc)
71109 {
72110 vexpress_config_find_prop(node, "arm,vexpress,site", site);
....@@ -95,9 +133,6 @@
95133 struct regmap *regmap;
96134 struct regmap **res;
97135
98
- if (WARN_ON(dev->parent->class != vexpress_config_class))
99
- return ERR_PTR(-ENODEV);
100
-
101136 bridge = dev_get_drvdata(dev->parent);
102137 if (WARN_ON(!bridge))
103138 return ERR_PTR(-EINVAL);
....@@ -120,91 +155,265 @@
120155 }
121156 EXPORT_SYMBOL_GPL(devm_regmap_init_vexpress_config);
122157
123
-struct device *vexpress_config_bridge_register(struct device *parent,
124
- struct vexpress_config_bridge_ops *ops, void *context)
158
+static int vexpress_syscfg_exec(struct vexpress_syscfg_func *func,
159
+ int index, bool write, u32 *data)
125160 {
126
- struct device *dev;
127
- struct vexpress_config_bridge *bridge;
161
+ struct vexpress_syscfg *syscfg = func->syscfg;
162
+ u32 command, status;
163
+ int tries;
164
+ long timeout;
128165
129
- if (!vexpress_config_class) {
130
- vexpress_config_class = class_create(THIS_MODULE,
131
- "vexpress-config");
132
- if (IS_ERR(vexpress_config_class))
133
- return (void *)vexpress_config_class;
134
- }
135
-
136
- dev = device_create(vexpress_config_class, parent, 0,
137
- NULL, "%s.bridge", dev_name(parent));
138
-
139
- if (IS_ERR(dev))
140
- return dev;
141
-
142
- bridge = devm_kmalloc(dev, sizeof(*bridge), GFP_KERNEL);
143
- if (!bridge) {
144
- put_device(dev);
145
- device_unregister(dev);
146
- return ERR_PTR(-ENOMEM);
147
- }
148
- bridge->ops = ops;
149
- bridge->context = context;
150
-
151
- dev_set_drvdata(dev, bridge);
152
-
153
- dev_dbg(parent, "Registered bridge '%s', parent node %p\n",
154
- dev_name(dev), parent->of_node);
155
-
156
- return dev;
157
-}
158
-
159
-
160
-static int vexpress_config_node_match(struct device *dev, const void *data)
161
-{
162
- const struct device_node *node = data;
163
-
164
- dev_dbg(dev, "Parent node %p, looking for %p\n",
165
- dev->parent->of_node, node);
166
-
167
- return dev->parent->of_node == node;
168
-}
169
-
170
-static int vexpress_config_populate(struct device_node *node)
171
-{
172
- struct device_node *bridge;
173
- struct device *parent;
174
- int ret;
175
-
176
- bridge = of_parse_phandle(node, "arm,vexpress,config-bridge", 0);
177
- if (!bridge)
166
+ if (WARN_ON(index >= func->num_templates))
178167 return -EINVAL;
179168
180
- parent = class_find_device(vexpress_config_class, NULL, bridge,
181
- vexpress_config_node_match);
182
- of_node_put(bridge);
183
- if (WARN_ON(!parent))
184
- return -ENODEV;
169
+ command = readl(syscfg->base + SYS_CFGCTRL);
170
+ if (WARN_ON(command & SYS_CFGCTRL_START))
171
+ return -EBUSY;
185172
186
- ret = of_platform_populate(node, NULL, NULL, parent);
173
+ command = func->template[index];
174
+ command |= SYS_CFGCTRL_START;
175
+ command |= write ? SYS_CFGCTRL_WRITE : 0;
187176
188
- put_device(parent);
177
+ /* Use a canary for reads */
178
+ if (!write)
179
+ *data = 0xdeadbeef;
189180
190
- return ret;
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;
191217 }
192218
193
-static int __init vexpress_config_init(void)
219
+static int vexpress_syscfg_read(void *context, unsigned int index,
220
+ unsigned int *val)
194221 {
195
- int err = 0;
196
- struct device_node *node;
222
+ struct vexpress_syscfg_func *func = context;
197223
198
- /* Need the config devices early, before the "normal" devices... */
199
- for_each_compatible_node(node, NULL, "arm,vexpress,config-bus") {
200
- err = vexpress_config_populate(node);
201
- if (err) {
202
- of_node_put(node);
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);
203339 break;
204340 }
205341 }
206
-
207
- return err;
208342 }
209
-postcore_initcall(vexpress_config_init);
210343
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 resource *res;
354
+ struct vexpress_config_bridge *bridge;
355
+ struct device_node *node;
356
+ int master;
357
+ u32 dt_hbi;
358
+
359
+ syscfg = devm_kzalloc(&pdev->dev, sizeof(*syscfg), GFP_KERNEL);
360
+ if (!syscfg)
361
+ return -ENOMEM;
362
+ syscfg->dev = &pdev->dev;
363
+ INIT_LIST_HEAD(&syscfg->funcs);
364
+
365
+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
366
+ syscfg->base = devm_ioremap_resource(&pdev->dev, res);
367
+ if (IS_ERR(syscfg->base))
368
+ return PTR_ERR(syscfg->base);
369
+
370
+ bridge = devm_kmalloc(&pdev->dev, sizeof(*bridge), GFP_KERNEL);
371
+ if (!bridge)
372
+ return -ENOMEM;
373
+
374
+ bridge->ops = &vexpress_syscfg_bridge_ops;
375
+ bridge->context = syscfg;
376
+
377
+ dev_set_drvdata(&pdev->dev, bridge);
378
+
379
+ master = readl(syscfg->base + SYS_MISC) & SYS_MISC_MASTERSITE ?
380
+ VEXPRESS_SITE_DB2 : VEXPRESS_SITE_DB1;
381
+ vexpress_config_set_master(master);
382
+
383
+ /* Confirm board type against DT property, if available */
384
+ if (of_property_read_u32(of_root, "arm,hbi", &dt_hbi) == 0) {
385
+ u32 id = readl(syscfg->base + (master == VEXPRESS_SITE_DB1 ?
386
+ SYS_PROCID0 : SYS_PROCID1));
387
+ u32 hbi = (id >> SYS_PROCIDx_HBI_SHIFT) & SYS_HBI_MASK;
388
+
389
+ if (WARN_ON(dt_hbi != hbi))
390
+ dev_warn(&pdev->dev, "DT HBI (%x) is not matching hardware (%x)!\n",
391
+ dt_hbi, hbi);
392
+ }
393
+
394
+ for_each_compatible_node(node, NULL, "arm,vexpress,config-bus") {
395
+ struct device_node *bridge_np;
396
+
397
+ bridge_np = of_parse_phandle(node, "arm,vexpress,config-bridge", 0);
398
+ if (bridge_np != pdev->dev.parent->of_node)
399
+ continue;
400
+
401
+ of_platform_populate(node, NULL, NULL, &pdev->dev);
402
+ }
403
+
404
+ return 0;
405
+}
406
+
407
+static const struct platform_device_id vexpress_syscfg_id_table[] = {
408
+ { "vexpress-syscfg", },
409
+ {},
410
+};
411
+MODULE_DEVICE_TABLE(platform, vexpress_syscfg_id_table);
412
+
413
+static struct platform_driver vexpress_syscfg_driver = {
414
+ .driver.name = "vexpress-syscfg",
415
+ .id_table = vexpress_syscfg_id_table,
416
+ .probe = vexpress_syscfg_probe,
417
+};
418
+module_platform_driver(vexpress_syscfg_driver);
419
+MODULE_LICENSE("GPL v2");