]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/sparc/kernel/of_device_32.c
License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[mirror_ubuntu-bionic-kernel.git] / arch / sparc / kernel / of_device_32.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
fd531431
DM
2#include <linux/string.h>
3#include <linux/kernel.h>
f85ff305 4#include <linux/of.h>
fd531431 5#include <linux/init.h>
fd531431
DM
6#include <linux/mod_devicetable.h>
7#include <linux/slab.h>
3f23de10 8#include <linux/errno.h>
c9f5b7e7 9#include <linux/irq.h>
3f23de10
SR
10#include <linux/of_device.h>
11#include <linux/of_platform.h>
e63829de
KE
12#include <asm/leon.h>
13#include <asm/leon_amba.h>
fd531431 14
c9f5b7e7 15#include "of_device_common.h"
1d05995b 16#include "irq.h"
cf44bbc2 17
cf44bbc2
DM
18/*
19 * PCI bus specific translator
20 */
21
22static int of_bus_pci_match(struct device_node *np)
23{
a83f9823
DM
24 if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
25 /* Do not do PCI specific frobbing if the
26 * PCI bridge lacks a ranges property. We
27 * want to pass it through up to the next
28 * parent as-is, not with the PCI translate
29 * method which chops off the top address cell.
30 */
31 if (!of_find_property(np, "ranges", NULL))
32 return 0;
33
34 return 1;
35 }
36
37 return 0;
cf44bbc2
DM
38}
39
40static void of_bus_pci_count_cells(struct device_node *np,
41 int *addrc, int *sizec)
42{
43 if (addrc)
44 *addrc = 3;
45 if (sizec)
46 *sizec = 2;
47}
48
a83f9823
DM
49static int of_bus_pci_map(u32 *addr, const u32 *range,
50 int na, int ns, int pna)
cf44bbc2 51{
a83f9823
DM
52 u32 result[OF_MAX_ADDR_CELLS];
53 int i;
cf44bbc2
DM
54
55 /* Check address type match */
56 if ((addr[0] ^ range[0]) & 0x03000000)
a83f9823 57 return -EINVAL;
cf44bbc2 58
a83f9823
DM
59 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
60 na - 1, ns))
61 return -EINVAL;
cf44bbc2 62
a83f9823
DM
63 /* Start with the parent range base. */
64 memcpy(result, range + na, pna * 4);
cf44bbc2 65
a83f9823
DM
66 /* Add in the child address offset, skipping high cell. */
67 for (i = 0; i < na - 1; i++)
68 result[pna - 1 - i] +=
69 (addr[na - 1 - i] -
70 range[na - 1 - i]);
71
72 memcpy(addr, result, pna * 4);
73
74 return 0;
cf44bbc2
DM
75}
76
e3c71a32 77static unsigned long of_bus_pci_get_flags(const u32 *addr, unsigned long flags)
cf44bbc2 78{
cf44bbc2
DM
79 u32 w = addr[0];
80
e3c71a32
DM
81 /* For PCI, we override whatever child busses may have used. */
82 flags = 0;
cf44bbc2
DM
83 switch((w >> 24) & 0x03) {
84 case 0x01:
85 flags |= IORESOURCE_IO;
e3c71a32
DM
86 break;
87
cf44bbc2
DM
88 case 0x02: /* 32 bits */
89 case 0x03: /* 64 bits */
90 flags |= IORESOURCE_MEM;
e3c71a32 91 break;
cf44bbc2
DM
92 }
93 if (w & 0x40000000)
94 flags |= IORESOURCE_PREFETCH;
95 return flags;
96}
97
bdba4d6b 98static unsigned long of_bus_sbus_get_flags(const u32 *addr, unsigned long flags)
cf44bbc2
DM
99{
100 return IORESOURCE_MEM;
101}
102
e63829de
KE
103 /*
104 * AMBAPP bus specific translator
105 */
106
107static int of_bus_ambapp_match(struct device_node *np)
108{
d7ecfb3c 109 return !strcmp(np->type, "ambapp");
e63829de
KE
110}
111
112static void of_bus_ambapp_count_cells(struct device_node *child,
113 int *addrc, int *sizec)
114{
115 if (addrc)
116 *addrc = 1;
117 if (sizec)
118 *sizec = 1;
119}
120
121static int of_bus_ambapp_map(u32 *addr, const u32 *range,
122 int na, int ns, int pna)
123{
124 return of_bus_default_map(addr, range, na, ns, pna);
125}
126
127static unsigned long of_bus_ambapp_get_flags(const u32 *addr,
128 unsigned long flags)
129{
130 return IORESOURCE_MEM;
131}
cf44bbc2
DM
132
133/*
134 * Array of bus specific translators
135 */
136
137static struct of_bus of_busses[] = {
138 /* PCI */
139 {
140 .name = "pci",
141 .addr_prop_name = "assigned-addresses",
142 .match = of_bus_pci_match,
143 .count_cells = of_bus_pci_count_cells,
144 .map = of_bus_pci_map,
cf44bbc2
DM
145 .get_flags = of_bus_pci_get_flags,
146 },
147 /* SBUS */
148 {
149 .name = "sbus",
150 .addr_prop_name = "reg",
151 .match = of_bus_sbus_match,
152 .count_cells = of_bus_sbus_count_cells,
c9f5b7e7 153 .map = of_bus_default_map,
cf44bbc2
DM
154 .get_flags = of_bus_sbus_get_flags,
155 },
e63829de
KE
156 /* AMBA */
157 {
158 .name = "ambapp",
159 .addr_prop_name = "reg",
160 .match = of_bus_ambapp_match,
161 .count_cells = of_bus_ambapp_count_cells,
162 .map = of_bus_ambapp_map,
163 .get_flags = of_bus_ambapp_get_flags,
164 },
cf44bbc2
DM
165 /* Default */
166 {
167 .name = "default",
168 .addr_prop_name = "reg",
169 .match = NULL,
170 .count_cells = of_bus_default_count_cells,
171 .map = of_bus_default_map,
cf44bbc2
DM
172 .get_flags = of_bus_default_get_flags,
173 },
174};
175
176static struct of_bus *of_match_bus(struct device_node *np)
177{
178 int i;
179
180 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
181 if (!of_busses[i].match || of_busses[i].match(np))
182 return &of_busses[i];
183 BUG();
184 return NULL;
185}
186
187static int __init build_one_resource(struct device_node *parent,
188 struct of_bus *bus,
189 struct of_bus *pbus,
190 u32 *addr,
191 int na, int ns, int pna)
192{
8271f042 193 const u32 *ranges;
cf44bbc2
DM
194 unsigned int rlen;
195 int rone;
cf44bbc2
DM
196
197 ranges = of_get_property(parent, "ranges", &rlen);
198 if (ranges == NULL || rlen == 0) {
a83f9823
DM
199 u32 result[OF_MAX_ADDR_CELLS];
200 int i;
201
202 memset(result, 0, pna * 4);
203 for (i = 0; i < na; i++)
204 result[pna - 1 - i] =
205 addr[na - 1 - i];
206
207 memcpy(addr, result, pna * 4);
208 return 0;
cf44bbc2
DM
209 }
210
211 /* Now walk through the ranges */
212 rlen /= 4;
213 rone = na + pna + ns;
214 for (; rlen >= rone; rlen -= rone, ranges += rone) {
a83f9823
DM
215 if (!bus->map(addr, ranges, na, ns, pna))
216 return 0;
cf44bbc2 217 }
cf44bbc2 218
a83f9823 219 return 1;
cf44bbc2
DM
220}
221
5280267c
DM
222static int __init use_1to1_mapping(struct device_node *pp)
223{
224 /* If we have a ranges property in the parent, use it. */
225 if (of_find_property(pp, "ranges", NULL) != NULL)
226 return 0;
227
228 /* Some SBUS devices use intermediate nodes to express
229 * hierarchy within the device itself. These aren't
230 * real bus nodes, and don't have a 'ranges' property.
231 * But, we should still pass the translation work up
232 * to the SBUS itself.
233 */
234 if (!strcmp(pp->name, "dma") ||
235 !strcmp(pp->name, "espdma") ||
236 !strcmp(pp->name, "ledma") ||
237 !strcmp(pp->name, "lebuffer"))
238 return 0;
239
240 return 1;
241}
242
a83f9823
DM
243static int of_resource_verbose;
244
cd4cd730 245static void __init build_device_resources(struct platform_device *op,
cf44bbc2
DM
246 struct device *parent)
247{
cd4cd730 248 struct platform_device *p_op;
cf44bbc2
DM
249 struct of_bus *bus;
250 int na, ns;
251 int index, num_reg;
8271f042 252 const void *preg;
cf44bbc2
DM
253
254 if (!parent)
255 return;
256
cd4cd730 257 p_op = to_platform_device(parent);
61c7a080
GL
258 bus = of_match_bus(p_op->dev.of_node);
259 bus->count_cells(op->dev.of_node, &na, &ns);
cf44bbc2 260
61c7a080 261 preg = of_get_property(op->dev.of_node, bus->addr_prop_name, &num_reg);
cf44bbc2
DM
262 if (!preg || num_reg == 0)
263 return;
264
265 /* Convert to num-cells. */
266 num_reg /= 4;
267
268 /* Conver to num-entries. */
269 num_reg /= na + ns;
270
1636f8ac
GL
271 op->resource = op->archdata.resource;
272 op->num_resources = num_reg;
cf44bbc2
DM
273 for (index = 0; index < num_reg; index++) {
274 struct resource *r = &op->resource[index];
275 u32 addr[OF_MAX_ADDR_CELLS];
8271f042 276 const u32 *reg = (preg + (index * ((na + ns) * 4)));
61c7a080
GL
277 struct device_node *dp = op->dev.of_node;
278 struct device_node *pp = p_op->dev.of_node;
b85cdd49 279 struct of_bus *pbus, *dbus;
cf44bbc2
DM
280 u64 size, result = OF_BAD_ADDR;
281 unsigned long flags;
282 int dna, dns;
283 int pna, pns;
284
285 size = of_read_addr(reg + na, ns);
cf44bbc2
DM
286
287 memcpy(addr, reg, na * 4);
288
e3c71a32
DM
289 flags = bus->get_flags(reg, 0);
290
5280267c 291 if (use_1to1_mapping(pp)) {
cf44bbc2
DM
292 result = of_read_addr(addr, na);
293 goto build_res;
294 }
295
296 dna = na;
297 dns = ns;
b85cdd49 298 dbus = bus;
cf44bbc2
DM
299
300 while (1) {
301 dp = pp;
302 pp = dp->parent;
303 if (!pp) {
304 result = of_read_addr(addr, dna);
305 break;
306 }
307
308 pbus = of_match_bus(pp);
309 pbus->count_cells(dp, &pna, &pns);
310
b85cdd49 311 if (build_one_resource(dp, dbus, pbus, addr,
a83f9823 312 dna, dns, pna))
cf44bbc2
DM
313 break;
314
e3c71a32
DM
315 flags = pbus->get_flags(addr, flags);
316
cf44bbc2
DM
317 dna = pna;
318 dns = pns;
b85cdd49 319 dbus = pbus;
cf44bbc2
DM
320 }
321
322 build_res:
323 memset(r, 0, sizeof(*r));
a83f9823
DM
324
325 if (of_resource_verbose)
326 printk("%s reg[%d] -> %llx\n",
61c7a080 327 op->dev.of_node->full_name, index,
a83f9823
DM
328 result);
329
cf44bbc2 330 if (result != OF_BAD_ADDR) {
95714e12 331 r->start = result & 0xffffffff;
cf44bbc2 332 r->end = result + size - 1;
95714e12 333 r->flags = flags | ((result >> 32ULL) & 0xffUL);
cf44bbc2 334 }
61c7a080 335 r->name = op->dev.of_node->name;
cf44bbc2
DM
336 }
337}
338
cd4cd730 339static struct platform_device * __init scan_one_device(struct device_node *dp,
cf44bbc2
DM
340 struct device *parent)
341{
cd4cd730 342 struct platform_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
8271f042 343 const struct linux_prom_irqs *intr;
3d6e4702 344 struct dev_archdata *sd;
8f96cd1a 345 int len, i;
cf44bbc2
DM
346
347 if (!op)
348 return NULL;
349
3d6e4702 350 sd = &op->dev.archdata;
3d6e4702
DM
351 sd->op = op;
352
d706c1b0 353 op->dev.of_node = dp;
cf44bbc2 354
8f96cd1a
DM
355 intr = of_get_property(dp, "intr", &len);
356 if (intr) {
1636f8ac
GL
357 op->archdata.num_irqs = len / sizeof(struct linux_prom_irqs);
358 for (i = 0; i < op->archdata.num_irqs; i++)
1d05995b 359 op->archdata.irqs[i] =
472bc4f2 360 sparc_config.build_device_irq(op, intr[i].pri);
8f96cd1a 361 } else {
8271f042
SR
362 const unsigned int *irq =
363 of_get_property(dp, "interrupts", &len);
8f96cd1a
DM
364
365 if (irq) {
1636f8ac
GL
366 op->archdata.num_irqs = len / sizeof(unsigned int);
367 for (i = 0; i < op->archdata.num_irqs; i++)
1d05995b 368 op->archdata.irqs[i] =
472bc4f2 369 sparc_config.build_device_irq(op, irq[i]);
8f96cd1a 370 } else {
1636f8ac 371 op->archdata.num_irqs = 0;
8f96cd1a
DM
372 }
373 }
cf44bbc2
DM
374
375 build_device_resources(op, parent);
376
377 op->dev.parent = parent;
eca39301 378 op->dev.bus = &platform_bus_type;
cf44bbc2 379 if (!parent)
5acdc1fa 380 dev_set_name(&op->dev, "root");
cf44bbc2 381 else
6016a363 382 dev_set_name(&op->dev, "%08x", dp->phandle);
cf44bbc2
DM
383
384 if (of_device_register(op)) {
385 printk("%s: Could not register of device.\n",
386 dp->full_name);
387 kfree(op);
388 op = NULL;
389 }
390
391 return op;
392}
393
394static void __init scan_tree(struct device_node *dp, struct device *parent)
395{
396 while (dp) {
cd4cd730 397 struct platform_device *op = scan_one_device(dp, parent);
cf44bbc2
DM
398
399 if (op)
400 scan_tree(dp->child, &op->dev);
401
402 dp = dp->sibling;
403 }
404}
405
eca39301 406static int __init scan_of_devices(void)
cf44bbc2
DM
407{
408 struct device_node *root = of_find_node_by_path("/");
cd4cd730 409 struct platform_device *parent;
cf44bbc2
DM
410
411 parent = scan_one_device(root, NULL);
412 if (!parent)
eca39301 413 return 0;
cf44bbc2
DM
414
415 scan_tree(root->child, &parent->dev);
eca39301 416 return 0;
cf44bbc2 417}
eca39301 418postcore_initcall(scan_of_devices);
fd531431 419
a83f9823
DM
420static int __init of_debug(char *str)
421{
422 int val = 0;
423
424 get_option(&str, &val);
425 if (val & 1)
426 of_resource_verbose = 1;
427 return 1;
428}
429
430__setup("of_debug=", of_debug);