Loading...
1#include <linux/string.h>
2#include <linux/kernel.h>
3#include <linux/of.h>
4#include <linux/init.h>
5#include <linux/export.h>
6#include <linux/mod_devicetable.h>
7#include <linux/slab.h>
8#include <linux/errno.h>
9#include <linux/irq.h>
10#include <linux/of_device.h>
11#include <linux/of_platform.h>
12#include <asm/spitfire.h>
13
14#include "of_device_common.h"
15
16void __iomem *of_ioremap(struct resource *res, unsigned long offset, unsigned long size, char *name)
17{
18 unsigned long ret = res->start + offset;
19 struct resource *r;
20
21 if (res->flags & IORESOURCE_MEM)
22 r = request_mem_region(ret, size, name);
23 else
24 r = request_region(ret, size, name);
25 if (!r)
26 ret = 0;
27
28 return (void __iomem *) ret;
29}
30EXPORT_SYMBOL(of_ioremap);
31
32void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
33{
34 if (res->flags & IORESOURCE_MEM)
35 release_mem_region((unsigned long) base, size);
36 else
37 release_region((unsigned long) base, size);
38}
39EXPORT_SYMBOL(of_iounmap);
40
41/*
42 * PCI bus specific translator
43 */
44
45static int of_bus_pci_match(struct device_node *np)
46{
47 if (!strcmp(np->name, "pci")) {
48 const char *model = of_get_property(np, "model", NULL);
49
50 if (model && !strcmp(model, "SUNW,simba"))
51 return 0;
52
53 /* Do not do PCI specific frobbing if the
54 * PCI bridge lacks a ranges property. We
55 * want to pass it through up to the next
56 * parent as-is, not with the PCI translate
57 * method which chops off the top address cell.
58 */
59 if (!of_find_property(np, "ranges", NULL))
60 return 0;
61
62 return 1;
63 }
64
65 return 0;
66}
67
68static int of_bus_simba_match(struct device_node *np)
69{
70 const char *model = of_get_property(np, "model", NULL);
71
72 if (model && !strcmp(model, "SUNW,simba"))
73 return 1;
74
75 /* Treat PCI busses lacking ranges property just like
76 * simba.
77 */
78 if (!strcmp(np->name, "pci")) {
79 if (!of_find_property(np, "ranges", NULL))
80 return 1;
81 }
82
83 return 0;
84}
85
86static int of_bus_simba_map(u32 *addr, const u32 *range,
87 int na, int ns, int pna)
88{
89 return 0;
90}
91
92static void of_bus_pci_count_cells(struct device_node *np,
93 int *addrc, int *sizec)
94{
95 if (addrc)
96 *addrc = 3;
97 if (sizec)
98 *sizec = 2;
99}
100
101static int of_bus_pci_map(u32 *addr, const u32 *range,
102 int na, int ns, int pna)
103{
104 u32 result[OF_MAX_ADDR_CELLS];
105 int i;
106
107 /* Check address type match */
108 if (!((addr[0] ^ range[0]) & 0x03000000))
109 goto type_match;
110
111 /* Special exception, we can map a 64-bit address into
112 * a 32-bit range.
113 */
114 if ((addr[0] & 0x03000000) == 0x03000000 &&
115 (range[0] & 0x03000000) == 0x02000000)
116 goto type_match;
117
118 return -EINVAL;
119
120type_match:
121 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
122 na - 1, ns))
123 return -EINVAL;
124
125 /* Start with the parent range base. */
126 memcpy(result, range + na, pna * 4);
127
128 /* Add in the child address offset, skipping high cell. */
129 for (i = 0; i < na - 1; i++)
130 result[pna - 1 - i] +=
131 (addr[na - 1 - i] -
132 range[na - 1 - i]);
133
134 memcpy(addr, result, pna * 4);
135
136 return 0;
137}
138
139static unsigned long of_bus_pci_get_flags(const u32 *addr, unsigned long flags)
140{
141 u32 w = addr[0];
142
143 /* For PCI, we override whatever child busses may have used. */
144 flags = 0;
145 switch((w >> 24) & 0x03) {
146 case 0x01:
147 flags |= IORESOURCE_IO;
148 break;
149
150 case 0x02: /* 32 bits */
151 case 0x03: /* 64 bits */
152 flags |= IORESOURCE_MEM;
153 break;
154 }
155 if (w & 0x40000000)
156 flags |= IORESOURCE_PREFETCH;
157 return flags;
158}
159
160/*
161 * FHC/Central bus specific translator.
162 *
163 * This is just needed to hard-code the address and size cell
164 * counts. 'fhc' and 'central' nodes lack the #address-cells and
165 * #size-cells properties, and if you walk to the root on such
166 * Enterprise boxes all you'll get is a #size-cells of 2 which is
167 * not what we want to use.
168 */
169static int of_bus_fhc_match(struct device_node *np)
170{
171 return !strcmp(np->name, "fhc") ||
172 !strcmp(np->name, "central");
173}
174
175#define of_bus_fhc_count_cells of_bus_sbus_count_cells
176
177/*
178 * Array of bus specific translators
179 */
180
181static struct of_bus of_busses[] = {
182 /* PCI */
183 {
184 .name = "pci",
185 .addr_prop_name = "assigned-addresses",
186 .match = of_bus_pci_match,
187 .count_cells = of_bus_pci_count_cells,
188 .map = of_bus_pci_map,
189 .get_flags = of_bus_pci_get_flags,
190 },
191 /* SIMBA */
192 {
193 .name = "simba",
194 .addr_prop_name = "assigned-addresses",
195 .match = of_bus_simba_match,
196 .count_cells = of_bus_pci_count_cells,
197 .map = of_bus_simba_map,
198 .get_flags = of_bus_pci_get_flags,
199 },
200 /* SBUS */
201 {
202 .name = "sbus",
203 .addr_prop_name = "reg",
204 .match = of_bus_sbus_match,
205 .count_cells = of_bus_sbus_count_cells,
206 .map = of_bus_default_map,
207 .get_flags = of_bus_default_get_flags,
208 },
209 /* FHC */
210 {
211 .name = "fhc",
212 .addr_prop_name = "reg",
213 .match = of_bus_fhc_match,
214 .count_cells = of_bus_fhc_count_cells,
215 .map = of_bus_default_map,
216 .get_flags = of_bus_default_get_flags,
217 },
218 /* Default */
219 {
220 .name = "default",
221 .addr_prop_name = "reg",
222 .match = NULL,
223 .count_cells = of_bus_default_count_cells,
224 .map = of_bus_default_map,
225 .get_flags = of_bus_default_get_flags,
226 },
227};
228
229static struct of_bus *of_match_bus(struct device_node *np)
230{
231 int i;
232
233 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
234 if (!of_busses[i].match || of_busses[i].match(np))
235 return &of_busses[i];
236 BUG();
237 return NULL;
238}
239
240static int __init build_one_resource(struct device_node *parent,
241 struct of_bus *bus,
242 struct of_bus *pbus,
243 u32 *addr,
244 int na, int ns, int pna)
245{
246 const u32 *ranges;
247 int rone, rlen;
248
249 ranges = of_get_property(parent, "ranges", &rlen);
250 if (ranges == NULL || rlen == 0) {
251 u32 result[OF_MAX_ADDR_CELLS];
252 int i;
253
254 memset(result, 0, pna * 4);
255 for (i = 0; i < na; i++)
256 result[pna - 1 - i] =
257 addr[na - 1 - i];
258
259 memcpy(addr, result, pna * 4);
260 return 0;
261 }
262
263 /* Now walk through the ranges */
264 rlen /= 4;
265 rone = na + pna + ns;
266 for (; rlen >= rone; rlen -= rone, ranges += rone) {
267 if (!bus->map(addr, ranges, na, ns, pna))
268 return 0;
269 }
270
271 /* When we miss an I/O space match on PCI, just pass it up
272 * to the next PCI bridge and/or controller.
273 */
274 if (!strcmp(bus->name, "pci") &&
275 (addr[0] & 0x03000000) == 0x01000000)
276 return 0;
277
278 return 1;
279}
280
281static int __init use_1to1_mapping(struct device_node *pp)
282{
283 /* If we have a ranges property in the parent, use it. */
284 if (of_find_property(pp, "ranges", NULL) != NULL)
285 return 0;
286
287 /* If the parent is the dma node of an ISA bus, pass
288 * the translation up to the root.
289 *
290 * Some SBUS devices use intermediate nodes to express
291 * hierarchy within the device itself. These aren't
292 * real bus nodes, and don't have a 'ranges' property.
293 * But, we should still pass the translation work up
294 * to the SBUS itself.
295 */
296 if (!strcmp(pp->name, "dma") ||
297 !strcmp(pp->name, "espdma") ||
298 !strcmp(pp->name, "ledma") ||
299 !strcmp(pp->name, "lebuffer"))
300 return 0;
301
302 /* Similarly for all PCI bridges, if we get this far
303 * it lacks a ranges property, and this will include
304 * cases like Simba.
305 */
306 if (!strcmp(pp->name, "pci"))
307 return 0;
308
309 return 1;
310}
311
312static int of_resource_verbose;
313
314static void __init build_device_resources(struct platform_device *op,
315 struct device *parent)
316{
317 struct platform_device *p_op;
318 struct of_bus *bus;
319 int na, ns;
320 int index, num_reg;
321 const void *preg;
322
323 if (!parent)
324 return;
325
326 p_op = to_platform_device(parent);
327 bus = of_match_bus(p_op->dev.of_node);
328 bus->count_cells(op->dev.of_node, &na, &ns);
329
330 preg = of_get_property(op->dev.of_node, bus->addr_prop_name, &num_reg);
331 if (!preg || num_reg == 0)
332 return;
333
334 /* Convert to num-cells. */
335 num_reg /= 4;
336
337 /* Convert to num-entries. */
338 num_reg /= na + ns;
339
340 /* Prevent overrunning the op->resources[] array. */
341 if (num_reg > PROMREG_MAX) {
342 printk(KERN_WARNING "%s: Too many regs (%d), "
343 "limiting to %d.\n",
344 op->dev.of_node->full_name, num_reg, PROMREG_MAX);
345 num_reg = PROMREG_MAX;
346 }
347
348 op->resource = op->archdata.resource;
349 op->num_resources = num_reg;
350 for (index = 0; index < num_reg; index++) {
351 struct resource *r = &op->resource[index];
352 u32 addr[OF_MAX_ADDR_CELLS];
353 const u32 *reg = (preg + (index * ((na + ns) * 4)));
354 struct device_node *dp = op->dev.of_node;
355 struct device_node *pp = p_op->dev.of_node;
356 struct of_bus *pbus, *dbus;
357 u64 size, result = OF_BAD_ADDR;
358 unsigned long flags;
359 int dna, dns;
360 int pna, pns;
361
362 size = of_read_addr(reg + na, ns);
363 memcpy(addr, reg, na * 4);
364
365 flags = bus->get_flags(addr, 0);
366
367 if (use_1to1_mapping(pp)) {
368 result = of_read_addr(addr, na);
369 goto build_res;
370 }
371
372 dna = na;
373 dns = ns;
374 dbus = bus;
375
376 while (1) {
377 dp = pp;
378 pp = dp->parent;
379 if (!pp) {
380 result = of_read_addr(addr, dna);
381 break;
382 }
383
384 pbus = of_match_bus(pp);
385 pbus->count_cells(dp, &pna, &pns);
386
387 if (build_one_resource(dp, dbus, pbus, addr,
388 dna, dns, pna))
389 break;
390
391 flags = pbus->get_flags(addr, flags);
392
393 dna = pna;
394 dns = pns;
395 dbus = pbus;
396 }
397
398 build_res:
399 memset(r, 0, sizeof(*r));
400
401 if (of_resource_verbose)
402 printk("%s reg[%d] -> %llx\n",
403 op->dev.of_node->full_name, index,
404 result);
405
406 if (result != OF_BAD_ADDR) {
407 if (tlb_type == hypervisor)
408 result &= 0x0fffffffffffffffUL;
409
410 r->start = result;
411 r->end = result + size - 1;
412 r->flags = flags;
413 }
414 r->name = op->dev.of_node->name;
415 }
416}
417
418static struct device_node * __init
419apply_interrupt_map(struct device_node *dp, struct device_node *pp,
420 const u32 *imap, int imlen, const u32 *imask,
421 unsigned int *irq_p)
422{
423 struct device_node *cp;
424 unsigned int irq = *irq_p;
425 struct of_bus *bus;
426 phandle handle;
427 const u32 *reg;
428 int na, num_reg, i;
429
430 bus = of_match_bus(pp);
431 bus->count_cells(dp, &na, NULL);
432
433 reg = of_get_property(dp, "reg", &num_reg);
434 if (!reg || !num_reg)
435 return NULL;
436
437 imlen /= ((na + 3) * 4);
438 handle = 0;
439 for (i = 0; i < imlen; i++) {
440 int j;
441
442 for (j = 0; j < na; j++) {
443 if ((reg[j] & imask[j]) != imap[j])
444 goto next;
445 }
446 if (imap[na] == irq) {
447 handle = imap[na + 1];
448 irq = imap[na + 2];
449 break;
450 }
451
452 next:
453 imap += (na + 3);
454 }
455 if (i == imlen) {
456 /* Psycho and Sabre PCI controllers can have 'interrupt-map'
457 * properties that do not include the on-board device
458 * interrupts. Instead, the device's 'interrupts' property
459 * is already a fully specified INO value.
460 *
461 * Handle this by deciding that, if we didn't get a
462 * match in the parent's 'interrupt-map', and the
463 * parent is an IRQ translator, then use the parent as
464 * our IRQ controller.
465 */
466 if (pp->irq_trans)
467 return pp;
468
469 return NULL;
470 }
471
472 *irq_p = irq;
473 cp = of_find_node_by_phandle(handle);
474
475 return cp;
476}
477
478static unsigned int __init pci_irq_swizzle(struct device_node *dp,
479 struct device_node *pp,
480 unsigned int irq)
481{
482 const struct linux_prom_pci_registers *regs;
483 unsigned int bus, devfn, slot, ret;
484
485 if (irq < 1 || irq > 4)
486 return irq;
487
488 regs = of_get_property(dp, "reg", NULL);
489 if (!regs)
490 return irq;
491
492 bus = (regs->phys_hi >> 16) & 0xff;
493 devfn = (regs->phys_hi >> 8) & 0xff;
494 slot = (devfn >> 3) & 0x1f;
495
496 if (pp->irq_trans) {
497 /* Derived from Table 8-3, U2P User's Manual. This branch
498 * is handling a PCI controller that lacks a proper set of
499 * interrupt-map and interrupt-map-mask properties. The
500 * Ultra-E450 is one example.
501 *
502 * The bit layout is BSSLL, where:
503 * B: 0 on bus A, 1 on bus B
504 * D: 2-bit slot number, derived from PCI device number as
505 * (dev - 1) for bus A, or (dev - 2) for bus B
506 * L: 2-bit line number
507 */
508 if (bus & 0x80) {
509 /* PBM-A */
510 bus = 0x00;
511 slot = (slot - 1) << 2;
512 } else {
513 /* PBM-B */
514 bus = 0x10;
515 slot = (slot - 2) << 2;
516 }
517 irq -= 1;
518
519 ret = (bus | slot | irq);
520 } else {
521 /* Going through a PCI-PCI bridge that lacks a set of
522 * interrupt-map and interrupt-map-mask properties.
523 */
524 ret = ((irq - 1 + (slot & 3)) & 3) + 1;
525 }
526
527 return ret;
528}
529
530static int of_irq_verbose;
531
532static unsigned int __init build_one_device_irq(struct platform_device *op,
533 struct device *parent,
534 unsigned int irq)
535{
536 struct device_node *dp = op->dev.of_node;
537 struct device_node *pp, *ip;
538 unsigned int orig_irq = irq;
539 int nid;
540
541 if (irq == 0xffffffff)
542 return irq;
543
544 if (dp->irq_trans) {
545 irq = dp->irq_trans->irq_build(dp, irq,
546 dp->irq_trans->data);
547
548 if (of_irq_verbose)
549 printk("%s: direct translate %x --> %x\n",
550 dp->full_name, orig_irq, irq);
551
552 goto out;
553 }
554
555 /* Something more complicated. Walk up to the root, applying
556 * interrupt-map or bus specific translations, until we hit
557 * an IRQ translator.
558 *
559 * If we hit a bus type or situation we cannot handle, we
560 * stop and assume that the original IRQ number was in a
561 * format which has special meaning to it's immediate parent.
562 */
563 pp = dp->parent;
564 ip = NULL;
565 while (pp) {
566 const void *imap, *imsk;
567 int imlen;
568
569 imap = of_get_property(pp, "interrupt-map", &imlen);
570 imsk = of_get_property(pp, "interrupt-map-mask", NULL);
571 if (imap && imsk) {
572 struct device_node *iret;
573 int this_orig_irq = irq;
574
575 iret = apply_interrupt_map(dp, pp,
576 imap, imlen, imsk,
577 &irq);
578
579 if (of_irq_verbose)
580 printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
581 op->dev.of_node->full_name,
582 pp->full_name, this_orig_irq,
583 (iret ? iret->full_name : "NULL"), irq);
584
585 if (!iret)
586 break;
587
588 if (iret->irq_trans) {
589 ip = iret;
590 break;
591 }
592 } else {
593 if (!strcmp(pp->name, "pci")) {
594 unsigned int this_orig_irq = irq;
595
596 irq = pci_irq_swizzle(dp, pp, irq);
597 if (of_irq_verbose)
598 printk("%s: PCI swizzle [%s] "
599 "%x --> %x\n",
600 op->dev.of_node->full_name,
601 pp->full_name, this_orig_irq,
602 irq);
603
604 }
605
606 if (pp->irq_trans) {
607 ip = pp;
608 break;
609 }
610 }
611 dp = pp;
612 pp = pp->parent;
613 }
614 if (!ip)
615 return orig_irq;
616
617 irq = ip->irq_trans->irq_build(op->dev.of_node, irq,
618 ip->irq_trans->data);
619 if (of_irq_verbose)
620 printk("%s: Apply IRQ trans [%s] %x --> %x\n",
621 op->dev.of_node->full_name, ip->full_name, orig_irq, irq);
622
623out:
624 nid = of_node_to_nid(dp);
625 if (nid != -1) {
626 cpumask_t numa_mask;
627
628 cpumask_copy(&numa_mask, cpumask_of_node(nid));
629 irq_set_affinity(irq, &numa_mask);
630 }
631
632 return irq;
633}
634
635static struct platform_device * __init scan_one_device(struct device_node *dp,
636 struct device *parent)
637{
638 struct platform_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
639 const unsigned int *irq;
640 struct dev_archdata *sd;
641 int len, i;
642
643 if (!op)
644 return NULL;
645
646 sd = &op->dev.archdata;
647 sd->op = op;
648
649 op->dev.of_node = dp;
650
651 irq = of_get_property(dp, "interrupts", &len);
652 if (irq) {
653 op->archdata.num_irqs = len / 4;
654
655 /* Prevent overrunning the op->irqs[] array. */
656 if (op->archdata.num_irqs > PROMINTR_MAX) {
657 printk(KERN_WARNING "%s: Too many irqs (%d), "
658 "limiting to %d.\n",
659 dp->full_name, op->archdata.num_irqs, PROMINTR_MAX);
660 op->archdata.num_irqs = PROMINTR_MAX;
661 }
662 memcpy(op->archdata.irqs, irq, op->archdata.num_irqs * 4);
663 } else {
664 op->archdata.num_irqs = 0;
665 }
666
667 build_device_resources(op, parent);
668 for (i = 0; i < op->archdata.num_irqs; i++)
669 op->archdata.irqs[i] = build_one_device_irq(op, parent, op->archdata.irqs[i]);
670
671 op->dev.parent = parent;
672 op->dev.bus = &platform_bus_type;
673 if (!parent)
674 dev_set_name(&op->dev, "root");
675 else
676 dev_set_name(&op->dev, "%08x", dp->phandle);
677
678 if (of_device_register(op)) {
679 printk("%s: Could not register of device.\n",
680 dp->full_name);
681 kfree(op);
682 op = NULL;
683 }
684
685 return op;
686}
687
688static void __init scan_tree(struct device_node *dp, struct device *parent)
689{
690 while (dp) {
691 struct platform_device *op = scan_one_device(dp, parent);
692
693 if (op)
694 scan_tree(dp->child, &op->dev);
695
696 dp = dp->sibling;
697 }
698}
699
700static int __init scan_of_devices(void)
701{
702 struct device_node *root = of_find_node_by_path("/");
703 struct platform_device *parent;
704
705 parent = scan_one_device(root, NULL);
706 if (!parent)
707 return 0;
708
709 scan_tree(root->child, &parent->dev);
710 return 0;
711}
712postcore_initcall(scan_of_devices);
713
714static int __init of_debug(char *str)
715{
716 int val = 0;
717
718 get_option(&str, &val);
719 if (val & 1)
720 of_resource_verbose = 1;
721 if (val & 2)
722 of_irq_verbose = 1;
723 return 1;
724}
725
726__setup("of_debug=", of_debug);
1// SPDX-License-Identifier: GPL-2.0
2#include <linux/string.h>
3#include <linux/kernel.h>
4#include <linux/of.h>
5#include <linux/dma-mapping.h>
6#include <linux/init.h>
7#include <linux/export.h>
8#include <linux/mod_devicetable.h>
9#include <linux/slab.h>
10#include <linux/errno.h>
11#include <linux/irq.h>
12#include <linux/of_device.h>
13#include <linux/of_platform.h>
14#include <asm/spitfire.h>
15
16#include "of_device_common.h"
17
18void __iomem *of_ioremap(struct resource *res, unsigned long offset, unsigned long size, char *name)
19{
20 unsigned long ret = res->start + offset;
21 struct resource *r;
22
23 if (res->flags & IORESOURCE_MEM)
24 r = request_mem_region(ret, size, name);
25 else
26 r = request_region(ret, size, name);
27 if (!r)
28 ret = 0;
29
30 return (void __iomem *) ret;
31}
32EXPORT_SYMBOL(of_ioremap);
33
34void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
35{
36 if (res->flags & IORESOURCE_MEM)
37 release_mem_region((unsigned long) base, size);
38 else
39 release_region((unsigned long) base, size);
40}
41EXPORT_SYMBOL(of_iounmap);
42
43/*
44 * PCI bus specific translator
45 */
46
47static int of_bus_pci_match(struct device_node *np)
48{
49 if (of_node_name_eq(np, "pci")) {
50 const char *model = of_get_property(np, "model", NULL);
51
52 if (model && !strcmp(model, "SUNW,simba"))
53 return 0;
54
55 /* Do not do PCI specific frobbing if the
56 * PCI bridge lacks a ranges property. We
57 * want to pass it through up to the next
58 * parent as-is, not with the PCI translate
59 * method which chops off the top address cell.
60 */
61 if (!of_find_property(np, "ranges", NULL))
62 return 0;
63
64 return 1;
65 }
66
67 return 0;
68}
69
70static int of_bus_simba_match(struct device_node *np)
71{
72 const char *model = of_get_property(np, "model", NULL);
73
74 if (model && !strcmp(model, "SUNW,simba"))
75 return 1;
76
77 /* Treat PCI busses lacking ranges property just like
78 * simba.
79 */
80 if (of_node_name_eq(np, "pci")) {
81 if (!of_find_property(np, "ranges", NULL))
82 return 1;
83 }
84
85 return 0;
86}
87
88static int of_bus_simba_map(u32 *addr, const u32 *range,
89 int na, int ns, int pna)
90{
91 return 0;
92}
93
94static void of_bus_pci_count_cells(struct device_node *np,
95 int *addrc, int *sizec)
96{
97 if (addrc)
98 *addrc = 3;
99 if (sizec)
100 *sizec = 2;
101}
102
103static int of_bus_pci_map(u32 *addr, const u32 *range,
104 int na, int ns, int pna)
105{
106 u32 result[OF_MAX_ADDR_CELLS];
107 int i;
108
109 /* Check address type match */
110 if (!((addr[0] ^ range[0]) & 0x03000000))
111 goto type_match;
112
113 /* Special exception, we can map a 64-bit address into
114 * a 32-bit range.
115 */
116 if ((addr[0] & 0x03000000) == 0x03000000 &&
117 (range[0] & 0x03000000) == 0x02000000)
118 goto type_match;
119
120 return -EINVAL;
121
122type_match:
123 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
124 na - 1, ns))
125 return -EINVAL;
126
127 /* Start with the parent range base. */
128 memcpy(result, range + na, pna * 4);
129
130 /* Add in the child address offset, skipping high cell. */
131 for (i = 0; i < na - 1; i++)
132 result[pna - 1 - i] +=
133 (addr[na - 1 - i] -
134 range[na - 1 - i]);
135
136 memcpy(addr, result, pna * 4);
137
138 return 0;
139}
140
141static unsigned long of_bus_pci_get_flags(const u32 *addr, unsigned long flags)
142{
143 u32 w = addr[0];
144
145 /* For PCI, we override whatever child busses may have used. */
146 flags = 0;
147 switch((w >> 24) & 0x03) {
148 case 0x01:
149 flags |= IORESOURCE_IO;
150 break;
151
152 case 0x02: /* 32 bits */
153 case 0x03: /* 64 bits */
154 flags |= IORESOURCE_MEM;
155 break;
156 }
157 if (w & 0x40000000)
158 flags |= IORESOURCE_PREFETCH;
159 return flags;
160}
161
162/*
163 * FHC/Central bus specific translator.
164 *
165 * This is just needed to hard-code the address and size cell
166 * counts. 'fhc' and 'central' nodes lack the #address-cells and
167 * #size-cells properties, and if you walk to the root on such
168 * Enterprise boxes all you'll get is a #size-cells of 2 which is
169 * not what we want to use.
170 */
171static int of_bus_fhc_match(struct device_node *np)
172{
173 return of_node_name_eq(np, "fhc") ||
174 of_node_name_eq(np, "central");
175}
176
177#define of_bus_fhc_count_cells of_bus_sbus_count_cells
178
179/*
180 * Array of bus specific translators
181 */
182
183static struct of_bus of_busses[] = {
184 /* PCI */
185 {
186 .name = "pci",
187 .addr_prop_name = "assigned-addresses",
188 .match = of_bus_pci_match,
189 .count_cells = of_bus_pci_count_cells,
190 .map = of_bus_pci_map,
191 .get_flags = of_bus_pci_get_flags,
192 },
193 /* SIMBA */
194 {
195 .name = "simba",
196 .addr_prop_name = "assigned-addresses",
197 .match = of_bus_simba_match,
198 .count_cells = of_bus_pci_count_cells,
199 .map = of_bus_simba_map,
200 .get_flags = of_bus_pci_get_flags,
201 },
202 /* SBUS */
203 {
204 .name = "sbus",
205 .addr_prop_name = "reg",
206 .match = of_bus_sbus_match,
207 .count_cells = of_bus_sbus_count_cells,
208 .map = of_bus_default_map,
209 .get_flags = of_bus_default_get_flags,
210 },
211 /* FHC */
212 {
213 .name = "fhc",
214 .addr_prop_name = "reg",
215 .match = of_bus_fhc_match,
216 .count_cells = of_bus_fhc_count_cells,
217 .map = of_bus_default_map,
218 .get_flags = of_bus_default_get_flags,
219 },
220 /* Default */
221 {
222 .name = "default",
223 .addr_prop_name = "reg",
224 .match = NULL,
225 .count_cells = of_bus_default_count_cells,
226 .map = of_bus_default_map,
227 .get_flags = of_bus_default_get_flags,
228 },
229};
230
231static struct of_bus *of_match_bus(struct device_node *np)
232{
233 int i;
234
235 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
236 if (!of_busses[i].match || of_busses[i].match(np))
237 return &of_busses[i];
238 BUG();
239 return NULL;
240}
241
242static int __init build_one_resource(struct device_node *parent,
243 struct of_bus *bus,
244 struct of_bus *pbus,
245 u32 *addr,
246 int na, int ns, int pna)
247{
248 const u32 *ranges;
249 int rone, rlen;
250
251 ranges = of_get_property(parent, "ranges", &rlen);
252 if (ranges == NULL || rlen == 0) {
253 u32 result[OF_MAX_ADDR_CELLS];
254 int i;
255
256 memset(result, 0, pna * 4);
257 for (i = 0; i < na; i++)
258 result[pna - 1 - i] =
259 addr[na - 1 - i];
260
261 memcpy(addr, result, pna * 4);
262 return 0;
263 }
264
265 /* Now walk through the ranges */
266 rlen /= 4;
267 rone = na + pna + ns;
268 for (; rlen >= rone; rlen -= rone, ranges += rone) {
269 if (!bus->map(addr, ranges, na, ns, pna))
270 return 0;
271 }
272
273 /* When we miss an I/O space match on PCI, just pass it up
274 * to the next PCI bridge and/or controller.
275 */
276 if (!strcmp(bus->name, "pci") &&
277 (addr[0] & 0x03000000) == 0x01000000)
278 return 0;
279
280 return 1;
281}
282
283static int __init use_1to1_mapping(struct device_node *pp)
284{
285 /* If we have a ranges property in the parent, use it. */
286 if (of_find_property(pp, "ranges", NULL) != NULL)
287 return 0;
288
289 /* If the parent is the dma node of an ISA bus, pass
290 * the translation up to the root.
291 *
292 * Some SBUS devices use intermediate nodes to express
293 * hierarchy within the device itself. These aren't
294 * real bus nodes, and don't have a 'ranges' property.
295 * But, we should still pass the translation work up
296 * to the SBUS itself.
297 */
298 if (of_node_name_eq(pp, "dma") ||
299 of_node_name_eq(pp, "espdma") ||
300 of_node_name_eq(pp, "ledma") ||
301 of_node_name_eq(pp, "lebuffer"))
302 return 0;
303
304 /* Similarly for all PCI bridges, if we get this far
305 * it lacks a ranges property, and this will include
306 * cases like Simba.
307 */
308 if (of_node_name_eq(pp, "pci"))
309 return 0;
310
311 return 1;
312}
313
314static int of_resource_verbose;
315
316static void __init build_device_resources(struct platform_device *op,
317 struct device *parent)
318{
319 struct platform_device *p_op;
320 struct of_bus *bus;
321 int na, ns;
322 int index, num_reg;
323 const void *preg;
324
325 if (!parent)
326 return;
327
328 p_op = to_platform_device(parent);
329 bus = of_match_bus(p_op->dev.of_node);
330 bus->count_cells(op->dev.of_node, &na, &ns);
331
332 preg = of_get_property(op->dev.of_node, bus->addr_prop_name, &num_reg);
333 if (!preg || num_reg == 0)
334 return;
335
336 /* Convert to num-cells. */
337 num_reg /= 4;
338
339 /* Convert to num-entries. */
340 num_reg /= na + ns;
341
342 /* Prevent overrunning the op->resources[] array. */
343 if (num_reg > PROMREG_MAX) {
344 printk(KERN_WARNING "%pOF: Too many regs (%d), "
345 "limiting to %d.\n",
346 op->dev.of_node, num_reg, PROMREG_MAX);
347 num_reg = PROMREG_MAX;
348 }
349
350 op->resource = op->archdata.resource;
351 op->num_resources = num_reg;
352 for (index = 0; index < num_reg; index++) {
353 struct resource *r = &op->resource[index];
354 u32 addr[OF_MAX_ADDR_CELLS];
355 const u32 *reg = (preg + (index * ((na + ns) * 4)));
356 struct device_node *dp = op->dev.of_node;
357 struct device_node *pp = p_op->dev.of_node;
358 struct of_bus *pbus, *dbus;
359 u64 size, result = OF_BAD_ADDR;
360 unsigned long flags;
361 int dna, dns;
362 int pna, pns;
363
364 size = of_read_addr(reg + na, ns);
365 memcpy(addr, reg, na * 4);
366
367 flags = bus->get_flags(addr, 0);
368
369 if (use_1to1_mapping(pp)) {
370 result = of_read_addr(addr, na);
371 goto build_res;
372 }
373
374 dna = na;
375 dns = ns;
376 dbus = bus;
377
378 while (1) {
379 dp = pp;
380 pp = dp->parent;
381 if (!pp) {
382 result = of_read_addr(addr, dna);
383 break;
384 }
385
386 pbus = of_match_bus(pp);
387 pbus->count_cells(dp, &pna, &pns);
388
389 if (build_one_resource(dp, dbus, pbus, addr,
390 dna, dns, pna))
391 break;
392
393 flags = pbus->get_flags(addr, flags);
394
395 dna = pna;
396 dns = pns;
397 dbus = pbus;
398 }
399
400 build_res:
401 memset(r, 0, sizeof(*r));
402
403 if (of_resource_verbose)
404 printk("%pOF reg[%d] -> %llx\n",
405 op->dev.of_node, index,
406 result);
407
408 if (result != OF_BAD_ADDR) {
409 if (tlb_type == hypervisor)
410 result &= 0x0fffffffffffffffUL;
411
412 r->start = result;
413 r->end = result + size - 1;
414 r->flags = flags;
415 }
416 r->name = op->dev.of_node->full_name;
417 }
418}
419
420static struct device_node * __init
421apply_interrupt_map(struct device_node *dp, struct device_node *pp,
422 const u32 *imap, int imlen, const u32 *imask,
423 unsigned int *irq_p)
424{
425 struct device_node *cp;
426 unsigned int irq = *irq_p;
427 struct of_bus *bus;
428 phandle handle;
429 const u32 *reg;
430 int na, num_reg, i;
431
432 bus = of_match_bus(pp);
433 bus->count_cells(dp, &na, NULL);
434
435 reg = of_get_property(dp, "reg", &num_reg);
436 if (!reg || !num_reg)
437 return NULL;
438
439 imlen /= ((na + 3) * 4);
440 handle = 0;
441 for (i = 0; i < imlen; i++) {
442 int j;
443
444 for (j = 0; j < na; j++) {
445 if ((reg[j] & imask[j]) != imap[j])
446 goto next;
447 }
448 if (imap[na] == irq) {
449 handle = imap[na + 1];
450 irq = imap[na + 2];
451 break;
452 }
453
454 next:
455 imap += (na + 3);
456 }
457 if (i == imlen) {
458 /* Psycho and Sabre PCI controllers can have 'interrupt-map'
459 * properties that do not include the on-board device
460 * interrupts. Instead, the device's 'interrupts' property
461 * is already a fully specified INO value.
462 *
463 * Handle this by deciding that, if we didn't get a
464 * match in the parent's 'interrupt-map', and the
465 * parent is an IRQ translator, then use the parent as
466 * our IRQ controller.
467 */
468 if (pp->irq_trans)
469 return pp;
470
471 return NULL;
472 }
473
474 *irq_p = irq;
475 cp = of_find_node_by_phandle(handle);
476
477 return cp;
478}
479
480static unsigned int __init pci_irq_swizzle(struct device_node *dp,
481 struct device_node *pp,
482 unsigned int irq)
483{
484 const struct linux_prom_pci_registers *regs;
485 unsigned int bus, devfn, slot, ret;
486
487 if (irq < 1 || irq > 4)
488 return irq;
489
490 regs = of_get_property(dp, "reg", NULL);
491 if (!regs)
492 return irq;
493
494 bus = (regs->phys_hi >> 16) & 0xff;
495 devfn = (regs->phys_hi >> 8) & 0xff;
496 slot = (devfn >> 3) & 0x1f;
497
498 if (pp->irq_trans) {
499 /* Derived from Table 8-3, U2P User's Manual. This branch
500 * is handling a PCI controller that lacks a proper set of
501 * interrupt-map and interrupt-map-mask properties. The
502 * Ultra-E450 is one example.
503 *
504 * The bit layout is BSSLL, where:
505 * B: 0 on bus A, 1 on bus B
506 * D: 2-bit slot number, derived from PCI device number as
507 * (dev - 1) for bus A, or (dev - 2) for bus B
508 * L: 2-bit line number
509 */
510 if (bus & 0x80) {
511 /* PBM-A */
512 bus = 0x00;
513 slot = (slot - 1) << 2;
514 } else {
515 /* PBM-B */
516 bus = 0x10;
517 slot = (slot - 2) << 2;
518 }
519 irq -= 1;
520
521 ret = (bus | slot | irq);
522 } else {
523 /* Going through a PCI-PCI bridge that lacks a set of
524 * interrupt-map and interrupt-map-mask properties.
525 */
526 ret = ((irq - 1 + (slot & 3)) & 3) + 1;
527 }
528
529 return ret;
530}
531
532static int of_irq_verbose;
533
534static unsigned int __init build_one_device_irq(struct platform_device *op,
535 struct device *parent,
536 unsigned int irq)
537{
538 struct device_node *dp = op->dev.of_node;
539 struct device_node *pp, *ip;
540 unsigned int orig_irq = irq;
541 int nid;
542
543 if (irq == 0xffffffff)
544 return irq;
545
546 if (dp->irq_trans) {
547 irq = dp->irq_trans->irq_build(dp, irq,
548 dp->irq_trans->data);
549
550 if (of_irq_verbose)
551 printk("%pOF: direct translate %x --> %x\n",
552 dp, orig_irq, irq);
553
554 goto out;
555 }
556
557 /* Something more complicated. Walk up to the root, applying
558 * interrupt-map or bus specific translations, until we hit
559 * an IRQ translator.
560 *
561 * If we hit a bus type or situation we cannot handle, we
562 * stop and assume that the original IRQ number was in a
563 * format which has special meaning to it's immediate parent.
564 */
565 pp = dp->parent;
566 ip = NULL;
567 while (pp) {
568 const void *imap, *imsk;
569 int imlen;
570
571 imap = of_get_property(pp, "interrupt-map", &imlen);
572 imsk = of_get_property(pp, "interrupt-map-mask", NULL);
573 if (imap && imsk) {
574 struct device_node *iret;
575 int this_orig_irq = irq;
576
577 iret = apply_interrupt_map(dp, pp,
578 imap, imlen, imsk,
579 &irq);
580
581 if (of_irq_verbose)
582 printk("%pOF: Apply [%pOF:%x] imap --> [%pOF:%x]\n",
583 op->dev.of_node,
584 pp, this_orig_irq, iret, irq);
585
586 if (!iret)
587 break;
588
589 if (iret->irq_trans) {
590 ip = iret;
591 break;
592 }
593 } else {
594 if (of_node_name_eq(pp, "pci")) {
595 unsigned int this_orig_irq = irq;
596
597 irq = pci_irq_swizzle(dp, pp, irq);
598 if (of_irq_verbose)
599 printk("%pOF: PCI swizzle [%pOF] "
600 "%x --> %x\n",
601 op->dev.of_node,
602 pp, this_orig_irq,
603 irq);
604
605 }
606
607 if (pp->irq_trans) {
608 ip = pp;
609 break;
610 }
611 }
612 dp = pp;
613 pp = pp->parent;
614 }
615 if (!ip)
616 return orig_irq;
617
618 irq = ip->irq_trans->irq_build(op->dev.of_node, irq,
619 ip->irq_trans->data);
620 if (of_irq_verbose)
621 printk("%pOF: Apply IRQ trans [%pOF] %x --> %x\n",
622 op->dev.of_node, ip, orig_irq, irq);
623
624out:
625 nid = of_node_to_nid(dp);
626 if (nid != -1) {
627 cpumask_t numa_mask;
628
629 cpumask_copy(&numa_mask, cpumask_of_node(nid));
630 irq_set_affinity(irq, &numa_mask);
631 }
632
633 return irq;
634}
635
636static struct platform_device * __init scan_one_device(struct device_node *dp,
637 struct device *parent)
638{
639 struct platform_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
640 const unsigned int *irq;
641 struct dev_archdata *sd;
642 int len, i;
643
644 if (!op)
645 return NULL;
646
647 sd = &op->dev.archdata;
648 sd->op = op;
649
650 op->dev.of_node = dp;
651
652 irq = of_get_property(dp, "interrupts", &len);
653 if (irq) {
654 op->archdata.num_irqs = len / 4;
655
656 /* Prevent overrunning the op->irqs[] array. */
657 if (op->archdata.num_irqs > PROMINTR_MAX) {
658 printk(KERN_WARNING "%pOF: Too many irqs (%d), "
659 "limiting to %d.\n",
660 dp, op->archdata.num_irqs, PROMINTR_MAX);
661 op->archdata.num_irqs = PROMINTR_MAX;
662 }
663 memcpy(op->archdata.irqs, irq, op->archdata.num_irqs * 4);
664 } else {
665 op->archdata.num_irqs = 0;
666 }
667
668 build_device_resources(op, parent);
669 for (i = 0; i < op->archdata.num_irqs; i++)
670 op->archdata.irqs[i] = build_one_device_irq(op, parent, op->archdata.irqs[i]);
671
672 op->dev.parent = parent;
673 op->dev.bus = &platform_bus_type;
674 if (!parent)
675 dev_set_name(&op->dev, "root");
676 else
677 dev_set_name(&op->dev, "%08x", dp->phandle);
678 op->dev.coherent_dma_mask = DMA_BIT_MASK(32);
679 op->dev.dma_mask = &op->dev.coherent_dma_mask;
680
681 if (of_device_register(op)) {
682 printk("%pOF: Could not register of device.\n", dp);
683 kfree(op);
684 op = NULL;
685 }
686
687 return op;
688}
689
690static void __init scan_tree(struct device_node *dp, struct device *parent)
691{
692 while (dp) {
693 struct platform_device *op = scan_one_device(dp, parent);
694
695 if (op)
696 scan_tree(dp->child, &op->dev);
697
698 dp = dp->sibling;
699 }
700}
701
702static int __init scan_of_devices(void)
703{
704 struct device_node *root = of_find_node_by_path("/");
705 struct platform_device *parent;
706
707 parent = scan_one_device(root, NULL);
708 if (!parent)
709 return 0;
710
711 scan_tree(root->child, &parent->dev);
712 return 0;
713}
714postcore_initcall(scan_of_devices);
715
716static int __init of_debug(char *str)
717{
718 int val = 0;
719
720 get_option(&str, &val);
721 if (val & 1)
722 of_resource_verbose = 1;
723 if (val & 2)
724 of_irq_verbose = 1;
725 return 1;
726}
727
728__setup("of_debug=", of_debug);