1
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
45
46
47static int of_bus_pci_match(struct device_node *np)
48{
49 if (!strcmp(np->name, "pci")) {
50 const char *model = of_get_property(np, "model", NULL);
51
52 if (model && !strcmp(model, "SUNW,simba"))
53 return 0;
54
55
56
57
58
59
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
78
79
80 if (!strcmp(np->name, "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
110 if (!((addr[0] ^ range[0]) & 0x03000000))
111 goto type_match;
112
113
114
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
128 memcpy(result, range + na, pna * 4);
129
130
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
146 flags = 0;
147 switch((w >> 24) & 0x03) {
148 case 0x01:
149 flags |= IORESOURCE_IO;
150 break;
151
152 case 0x02:
153 case 0x03:
154 flags |= IORESOURCE_MEM;
155 break;
156 }
157 if (w & 0x40000000)
158 flags |= IORESOURCE_PREFETCH;
159 return flags;
160}
161
162
163
164
165
166
167
168
169
170
171static int of_bus_fhc_match(struct device_node *np)
172{
173 return !strcmp(np->name, "fhc") ||
174 !strcmp(np->name, "central");
175}
176
177#define of_bus_fhc_count_cells of_bus_sbus_count_cells
178
179
180
181
182
183static struct of_bus of_busses[] = {
184
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
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
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
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
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
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
274
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
286 if (of_find_property(pp, "ranges", NULL) != NULL)
287 return 0;
288
289
290
291
292
293
294
295
296
297
298 if (!strcmp(pp->name, "dma") ||
299 !strcmp(pp->name, "espdma") ||
300 !strcmp(pp->name, "ledma") ||
301 !strcmp(pp->name, "lebuffer"))
302 return 0;
303
304
305
306
307
308 if (!strcmp(pp->name, "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
337 num_reg /= 4;
338
339
340 num_reg /= na + ns;
341
342
343 if (num_reg > PROMREG_MAX) {
344 printk(KERN_WARNING "%s: Too many regs (%d), "
345 "limiting to %d.\n",
346 op->dev.of_node->full_name, 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("%s reg[%d] -> %llx\n",
405 op->dev.of_node->full_name, 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->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
459
460
461
462
463
464
465
466
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
500
501
502
503
504
505
506
507
508
509
510 if (bus & 0x80) {
511
512 bus = 0x00;
513 slot = (slot - 1) << 2;
514 } else {
515
516 bus = 0x10;
517 slot = (slot - 2) << 2;
518 }
519 irq -= 1;
520
521 ret = (bus | slot | irq);
522 } else {
523
524
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("%s: direct translate %x --> %x\n",
552 dp->full_name, orig_irq, irq);
553
554 goto out;
555 }
556
557
558
559
560
561
562
563
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("%s: Apply [%s:%x] imap --> [%s:%x]\n",
583 op->dev.of_node->full_name,
584 pp->full_name, this_orig_irq,
585 of_node_full_name(iret), irq);
586
587 if (!iret)
588 break;
589
590 if (iret->irq_trans) {
591 ip = iret;
592 break;
593 }
594 } else {
595 if (!strcmp(pp->name, "pci")) {
596 unsigned int this_orig_irq = irq;
597
598 irq = pci_irq_swizzle(dp, pp, irq);
599 if (of_irq_verbose)
600 printk("%s: PCI swizzle [%s] "
601 "%x --> %x\n",
602 op->dev.of_node->full_name,
603 pp->full_name, this_orig_irq,
604 irq);
605
606 }
607
608 if (pp->irq_trans) {
609 ip = pp;
610 break;
611 }
612 }
613 dp = pp;
614 pp = pp->parent;
615 }
616 if (!ip)
617 return orig_irq;
618
619 irq = ip->irq_trans->irq_build(op->dev.of_node, irq,
620 ip->irq_trans->data);
621 if (of_irq_verbose)
622 printk("%s: Apply IRQ trans [%s] %x --> %x\n",
623 op->dev.of_node->full_name, ip->full_name, orig_irq, irq);
624
625out:
626 nid = of_node_to_nid(dp);
627 if (nid != -1) {
628 cpumask_t numa_mask;
629
630 cpumask_copy(&numa_mask, cpumask_of_node(nid));
631 irq_set_affinity(irq, &numa_mask);
632 }
633
634 return irq;
635}
636
637static struct platform_device * __init scan_one_device(struct device_node *dp,
638 struct device *parent)
639{
640 struct platform_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
641 const unsigned int *irq;
642 struct dev_archdata *sd;
643 int len, i;
644
645 if (!op)
646 return NULL;
647
648 sd = &op->dev.archdata;
649 sd->op = op;
650
651 op->dev.of_node = dp;
652
653 irq = of_get_property(dp, "interrupts", &len);
654 if (irq) {
655 op->archdata.num_irqs = len / 4;
656
657
658 if (op->archdata.num_irqs > PROMINTR_MAX) {
659 printk(KERN_WARNING "%s: Too many irqs (%d), "
660 "limiting to %d.\n",
661 dp->full_name, op->archdata.num_irqs, PROMINTR_MAX);
662 op->archdata.num_irqs = PROMINTR_MAX;
663 }
664 memcpy(op->archdata.irqs, irq, op->archdata.num_irqs * 4);
665 } else {
666 op->archdata.num_irqs = 0;
667 }
668
669 build_device_resources(op, parent);
670 for (i = 0; i < op->archdata.num_irqs; i++)
671 op->archdata.irqs[i] = build_one_device_irq(op, parent, op->archdata.irqs[i]);
672
673 op->dev.parent = parent;
674 op->dev.bus = &platform_bus_type;
675 if (!parent)
676 dev_set_name(&op->dev, "root");
677 else
678 dev_set_name(&op->dev, "%08x", dp->phandle);
679 op->dev.coherent_dma_mask = DMA_BIT_MASK(32);
680 op->dev.dma_mask = &op->dev.coherent_dma_mask;
681
682 if (of_device_register(op)) {
683 printk("%s: Could not register of device.\n",
684 dp->full_name);
685 kfree(op);
686 op = NULL;
687 }
688
689 return op;
690}
691
692static void __init scan_tree(struct device_node *dp, struct device *parent)
693{
694 while (dp) {
695 struct platform_device *op = scan_one_device(dp, parent);
696
697 if (op)
698 scan_tree(dp->child, &op->dev);
699
700 dp = dp->sibling;
701 }
702}
703
704static int __init scan_of_devices(void)
705{
706 struct device_node *root = of_find_node_by_path("/");
707 struct platform_device *parent;
708
709 parent = scan_one_device(root, NULL);
710 if (!parent)
711 return 0;
712
713 scan_tree(root->child, &parent->dev);
714 return 0;
715}
716postcore_initcall(scan_of_devices);
717
718static int __init of_debug(char *str)
719{
720 int val = 0;
721
722 get_option(&str, &val);
723 if (val & 1)
724 of_resource_verbose = 1;
725 if (val & 2)
726 of_irq_verbose = 1;
727 return 1;
728}
729
730__setup("of_debug=", of_debug);
731