Create linux/of_platorm.h
[deliverable/linux.git] / arch / sparc64 / kernel / of_device.c
CommitLineData
a2bd4fd1
DM
1#include <linux/string.h>
2#include <linux/kernel.h>
f85ff305 3#include <linux/of.h>
a2bd4fd1
DM
4#include <linux/init.h>
5#include <linux/module.h>
6#include <linux/mod_devicetable.h>
7#include <linux/slab.h>
8
9#include <asm/errno.h>
10#include <asm/of_device.h>
11
a2bd4fd1
DM
12static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
13{
14 struct of_device * of_dev = to_of_device(dev);
15 struct of_platform_driver * of_drv = to_of_platform_driver(drv);
16 const struct of_device_id * matches = of_drv->match_table;
17
18 if (!matches)
19 return 0;
20
21 return of_match_device(matches, of_dev) != NULL;
22}
23
37b7754a 24static int of_platform_device_probe(struct device *dev)
a2bd4fd1
DM
25{
26 int error = -ENODEV;
27 struct of_platform_driver *drv;
28 struct of_device *of_dev;
29 const struct of_device_id *match;
30
31 drv = to_of_platform_driver(dev->driver);
32 of_dev = to_of_device(dev);
33
34 if (!drv->probe)
35 return error;
36
37 of_dev_get(of_dev);
38
39 match = of_match_device(drv->match_table, of_dev);
40 if (match)
41 error = drv->probe(of_dev, match);
42 if (error)
43 of_dev_put(of_dev);
44
45 return error;
46}
47
37b7754a 48static int of_platform_device_remove(struct device *dev)
a2bd4fd1
DM
49{
50 struct of_device * of_dev = to_of_device(dev);
51 struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
52
53 if (dev->driver && drv->remove)
54 drv->remove(of_dev);
55 return 0;
56}
57
37b7754a 58static int of_platform_device_suspend(struct device *dev, pm_message_t state)
a2bd4fd1
DM
59{
60 struct of_device * of_dev = to_of_device(dev);
61 struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
62 int error = 0;
63
64 if (dev->driver && drv->suspend)
65 error = drv->suspend(of_dev, state);
66 return error;
67}
68
37b7754a 69static int of_platform_device_resume(struct device * dev)
a2bd4fd1
DM
70{
71 struct of_device * of_dev = to_of_device(dev);
72 struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
73 int error = 0;
74
75 if (dev->driver && drv->resume)
76 error = drv->resume(of_dev);
77 return error;
78}
79
3ca9fab4
DM
80void __iomem *of_ioremap(struct resource *res, unsigned long offset, unsigned long size, char *name)
81{
82 unsigned long ret = res->start + offset;
6bda5736 83 struct resource *r;
3ca9fab4 84
6bda5736
DM
85 if (res->flags & IORESOURCE_MEM)
86 r = request_mem_region(ret, size, name);
87 else
88 r = request_region(ret, size, name);
89 if (!r)
3ca9fab4
DM
90 ret = 0;
91
92 return (void __iomem *) ret;
93}
94EXPORT_SYMBOL(of_ioremap);
95
e3a411a3 96void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
3ca9fab4 97{
e3a411a3
DM
98 if (res->flags & IORESOURCE_MEM)
99 release_mem_region((unsigned long) base, size);
100 else
101 release_region((unsigned long) base, size);
3ca9fab4
DM
102}
103EXPORT_SYMBOL(of_iounmap);
104
2b1e5978
DM
105static int node_match(struct device *dev, void *data)
106{
107 struct of_device *op = to_of_device(dev);
108 struct device_node *dp = data;
109
110 return (op->node == dp);
111}
112
113struct of_device *of_find_device_by_node(struct device_node *dp)
114{
37b7754a 115 struct device *dev = bus_find_device(&of_platform_bus_type, NULL,
2b1e5978
DM
116 dp, node_match);
117
118 if (dev)
119 return to_of_device(dev);
120
121 return NULL;
122}
123EXPORT_SYMBOL(of_find_device_by_node);
124
a2bd4fd1
DM
125#ifdef CONFIG_PCI
126struct bus_type isa_bus_type = {
127 .name = "isa",
128 .match = of_platform_bus_match,
37b7754a
SR
129 .probe = of_platform_device_probe,
130 .remove = of_platform_device_remove,
131 .suspend = of_platform_device_suspend,
132 .resume = of_platform_device_resume,
a2bd4fd1 133};
69853918 134EXPORT_SYMBOL(isa_bus_type);
a2bd4fd1
DM
135
136struct bus_type ebus_bus_type = {
137 .name = "ebus",
138 .match = of_platform_bus_match,
37b7754a
SR
139 .probe = of_platform_device_probe,
140 .remove = of_platform_device_remove,
141 .suspend = of_platform_device_suspend,
142 .resume = of_platform_device_resume,
a2bd4fd1 143};
69853918 144EXPORT_SYMBOL(ebus_bus_type);
a2bd4fd1
DM
145#endif
146
147#ifdef CONFIG_SBUS
148struct bus_type sbus_bus_type = {
149 .name = "sbus",
150 .match = of_platform_bus_match,
37b7754a
SR
151 .probe = of_platform_device_probe,
152 .remove = of_platform_device_remove,
153 .suspend = of_platform_device_suspend,
154 .resume = of_platform_device_resume,
a2bd4fd1 155};
69853918 156EXPORT_SYMBOL(sbus_bus_type);
a2bd4fd1
DM
157#endif
158
37b7754a 159struct bus_type of_platform_bus_type = {
cf44bbc2
DM
160 .name = "of",
161 .match = of_platform_bus_match,
37b7754a
SR
162 .probe = of_platform_device_probe,
163 .remove = of_platform_device_remove,
164 .suspend = of_platform_device_suspend,
165 .resume = of_platform_device_resume,
cf44bbc2 166};
37b7754a 167EXPORT_SYMBOL(of_platform_bus_type);
cf44bbc2 168
a83f9823 169static inline u64 of_read_addr(const u32 *cell, int size)
cf44bbc2
DM
170{
171 u64 r = 0;
172 while (size--)
173 r = (r << 32) | *(cell++);
174 return r;
175}
176
177static void __init get_cells(struct device_node *dp,
178 int *addrc, int *sizec)
179{
180 if (addrc)
181 *addrc = of_n_addr_cells(dp);
182 if (sizec)
183 *sizec = of_n_size_cells(dp);
184}
185
186/* Max address size we deal with */
187#define OF_MAX_ADDR_CELLS 4
188
189struct of_bus {
190 const char *name;
191 const char *addr_prop_name;
192 int (*match)(struct device_node *parent);
193 void (*count_cells)(struct device_node *child,
194 int *addrc, int *sizec);
a83f9823
DM
195 int (*map)(u32 *addr, const u32 *range,
196 int na, int ns, int pna);
6a23acf3 197 unsigned int (*get_flags)(const u32 *addr);
cf44bbc2
DM
198};
199
200/*
201 * Default translator (generic bus)
202 */
203
204static void of_bus_default_count_cells(struct device_node *dev,
205 int *addrc, int *sizec)
206{
207 get_cells(dev, addrc, sizec);
208}
209
a83f9823
DM
210/* Make sure the least significant 64-bits are in-range. Even
211 * for 3 or 4 cell values it is a good enough approximation.
212 */
213static int of_out_of_range(const u32 *addr, const u32 *base,
214 const u32 *size, int na, int ns)
cf44bbc2 215{
a83f9823
DM
216 u64 a = of_read_addr(addr, na);
217 u64 b = of_read_addr(base, na);
cf44bbc2 218
a83f9823
DM
219 if (a < b)
220 return 1;
cf44bbc2 221
a83f9823
DM
222 b += of_read_addr(size, ns);
223 if (a >= b)
224 return 1;
225
226 return 0;
cf44bbc2
DM
227}
228
a83f9823
DM
229static int of_bus_default_map(u32 *addr, const u32 *range,
230 int na, int ns, int pna)
cf44bbc2 231{
a83f9823
DM
232 u32 result[OF_MAX_ADDR_CELLS];
233 int i;
234
235 if (ns > 2) {
236 printk("of_device: Cannot handle size cells (%d) > 2.", ns);
237 return -EINVAL;
238 }
239
240 if (of_out_of_range(addr, range, range + na + pna, na, ns))
241 return -EINVAL;
242
243 /* Start with the parent range base. */
244 memcpy(result, range + na, pna * 4);
245
246 /* Add in the child address offset. */
247 for (i = 0; i < na; i++)
248 result[pna - 1 - i] +=
249 (addr[na - 1 - i] -
250 range[na - 1 - i]);
251
252 memcpy(addr, result, pna * 4);
cf44bbc2
DM
253
254 return 0;
255}
256
6a23acf3 257static unsigned int of_bus_default_get_flags(const u32 *addr)
cf44bbc2
DM
258{
259 return IORESOURCE_MEM;
260}
261
cf44bbc2
DM
262/*
263 * PCI bus specific translator
264 */
265
266static int of_bus_pci_match(struct device_node *np)
267{
a83f9823 268 if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
a165b420 269 const char *model = of_get_property(np, "model", NULL);
01f94c4a
DM
270
271 if (model && !strcmp(model, "SUNW,simba"))
272 return 0;
273
a83f9823
DM
274 /* Do not do PCI specific frobbing if the
275 * PCI bridge lacks a ranges property. We
276 * want to pass it through up to the next
277 * parent as-is, not with the PCI translate
278 * method which chops off the top address cell.
279 */
280 if (!of_find_property(np, "ranges", NULL))
281 return 0;
282
283 return 1;
284 }
285
286 return 0;
cf44bbc2
DM
287}
288
01f94c4a
DM
289static int of_bus_simba_match(struct device_node *np)
290{
a165b420 291 const char *model = of_get_property(np, "model", NULL);
01f94c4a
DM
292
293 if (model && !strcmp(model, "SUNW,simba"))
294 return 1;
8c2786cf
DM
295
296 /* Treat PCI busses lacking ranges property just like
297 * simba.
298 */
299 if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
300 if (!of_find_property(np, "ranges", NULL))
301 return 1;
302 }
303
01f94c4a
DM
304 return 0;
305}
306
307static int of_bus_simba_map(u32 *addr, const u32 *range,
308 int na, int ns, int pna)
309{
310 return 0;
311}
312
cf44bbc2
DM
313static void of_bus_pci_count_cells(struct device_node *np,
314 int *addrc, int *sizec)
315{
316 if (addrc)
317 *addrc = 3;
318 if (sizec)
319 *sizec = 2;
320}
321
a83f9823
DM
322static int of_bus_pci_map(u32 *addr, const u32 *range,
323 int na, int ns, int pna)
cf44bbc2 324{
a83f9823
DM
325 u32 result[OF_MAX_ADDR_CELLS];
326 int i;
cf44bbc2
DM
327
328 /* Check address type match */
329 if ((addr[0] ^ range[0]) & 0x03000000)
a83f9823 330 return -EINVAL;
cf44bbc2 331
a83f9823
DM
332 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
333 na - 1, ns))
334 return -EINVAL;
cf44bbc2 335
a83f9823
DM
336 /* Start with the parent range base. */
337 memcpy(result, range + na, pna * 4);
cf44bbc2 338
a83f9823
DM
339 /* Add in the child address offset, skipping high cell. */
340 for (i = 0; i < na - 1; i++)
341 result[pna - 1 - i] +=
342 (addr[na - 1 - i] -
343 range[na - 1 - i]);
344
345 memcpy(addr, result, pna * 4);
346
347 return 0;
cf44bbc2
DM
348}
349
6a23acf3 350static unsigned int of_bus_pci_get_flags(const u32 *addr)
cf44bbc2
DM
351{
352 unsigned int flags = 0;
353 u32 w = addr[0];
354
355 switch((w >> 24) & 0x03) {
356 case 0x01:
357 flags |= IORESOURCE_IO;
358 case 0x02: /* 32 bits */
359 case 0x03: /* 64 bits */
360 flags |= IORESOURCE_MEM;
361 }
362 if (w & 0x40000000)
363 flags |= IORESOURCE_PREFETCH;
364 return flags;
365}
366
cf44bbc2
DM
367/*
368 * SBUS bus specific translator
369 */
370
371static int of_bus_sbus_match(struct device_node *np)
372{
373 return !strcmp(np->name, "sbus") ||
374 !strcmp(np->name, "sbi");
375}
376
377static void of_bus_sbus_count_cells(struct device_node *child,
378 int *addrc, int *sizec)
379{
380 if (addrc)
381 *addrc = 2;
382 if (sizec)
383 *sizec = 1;
384}
385
4130a4b2
DM
386/*
387 * FHC/Central bus specific translator.
388 *
389 * This is just needed to hard-code the address and size cell
390 * counts. 'fhc' and 'central' nodes lack the #address-cells and
391 * #size-cells properties, and if you walk to the root on such
392 * Enterprise boxes all you'll get is a #size-cells of 2 which is
393 * not what we want to use.
394 */
395static int of_bus_fhc_match(struct device_node *np)
cf44bbc2 396{
4130a4b2
DM
397 return !strcmp(np->name, "fhc") ||
398 !strcmp(np->name, "central");
cf44bbc2
DM
399}
400
4130a4b2 401#define of_bus_fhc_count_cells of_bus_sbus_count_cells
cf44bbc2
DM
402
403/*
404 * Array of bus specific translators
405 */
406
407static struct of_bus of_busses[] = {
408 /* PCI */
409 {
410 .name = "pci",
411 .addr_prop_name = "assigned-addresses",
412 .match = of_bus_pci_match,
413 .count_cells = of_bus_pci_count_cells,
414 .map = of_bus_pci_map,
cf44bbc2
DM
415 .get_flags = of_bus_pci_get_flags,
416 },
01f94c4a
DM
417 /* SIMBA */
418 {
419 .name = "simba",
420 .addr_prop_name = "assigned-addresses",
421 .match = of_bus_simba_match,
422 .count_cells = of_bus_pci_count_cells,
423 .map = of_bus_simba_map,
424 .get_flags = of_bus_pci_get_flags,
425 },
cf44bbc2
DM
426 /* SBUS */
427 {
428 .name = "sbus",
429 .addr_prop_name = "reg",
430 .match = of_bus_sbus_match,
431 .count_cells = of_bus_sbus_count_cells,
4130a4b2
DM
432 .map = of_bus_default_map,
433 .get_flags = of_bus_default_get_flags,
434 },
435 /* FHC */
436 {
437 .name = "fhc",
438 .addr_prop_name = "reg",
439 .match = of_bus_fhc_match,
440 .count_cells = of_bus_fhc_count_cells,
441 .map = of_bus_default_map,
442 .get_flags = of_bus_default_get_flags,
cf44bbc2
DM
443 },
444 /* Default */
445 {
446 .name = "default",
447 .addr_prop_name = "reg",
448 .match = NULL,
449 .count_cells = of_bus_default_count_cells,
450 .map = of_bus_default_map,
cf44bbc2
DM
451 .get_flags = of_bus_default_get_flags,
452 },
453};
454
455static struct of_bus *of_match_bus(struct device_node *np)
456{
457 int i;
458
459 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
460 if (!of_busses[i].match || of_busses[i].match(np))
461 return &of_busses[i];
462 BUG();
463 return NULL;
464}
465
466static int __init build_one_resource(struct device_node *parent,
467 struct of_bus *bus,
468 struct of_bus *pbus,
469 u32 *addr,
470 int na, int ns, int pna)
471{
6a23acf3 472 const u32 *ranges;
cf44bbc2
DM
473 unsigned int rlen;
474 int rone;
cf44bbc2
DM
475
476 ranges = of_get_property(parent, "ranges", &rlen);
477 if (ranges == NULL || rlen == 0) {
a83f9823
DM
478 u32 result[OF_MAX_ADDR_CELLS];
479 int i;
480
481 memset(result, 0, pna * 4);
482 for (i = 0; i < na; i++)
483 result[pna - 1 - i] =
484 addr[na - 1 - i];
485
486 memcpy(addr, result, pna * 4);
487 return 0;
cf44bbc2
DM
488 }
489
490 /* Now walk through the ranges */
491 rlen /= 4;
492 rone = na + pna + ns;
493 for (; rlen >= rone; rlen -= rone, ranges += rone) {
a83f9823
DM
494 if (!bus->map(addr, ranges, na, ns, pna))
495 return 0;
cf44bbc2 496 }
a83f9823 497
49d23cfc
DM
498 /* When we miss an I/O space match on PCI, just pass it up
499 * to the next PCI bridge and/or controller.
500 */
501 if (!strcmp(bus->name, "pci") &&
502 (addr[0] & 0x03000000) == 0x01000000)
503 return 0;
504
a83f9823
DM
505 return 1;
506}
507
508static int __init use_1to1_mapping(struct device_node *pp)
509{
a83f9823
DM
510 /* If this is on the PMU bus, don't try to translate it even
511 * if a ranges property exists.
512 */
513 if (!strcmp(pp->name, "pmu"))
cf44bbc2
DM
514 return 1;
515
a83f9823
DM
516 /* If we have a ranges property in the parent, use it. */
517 if (of_find_property(pp, "ranges", NULL) != NULL)
518 return 0;
cf44bbc2 519
a83f9823
DM
520 /* If the parent is the dma node of an ISA bus, pass
521 * the translation up to the root.
522 */
523 if (!strcmp(pp->name, "dma"))
524 return 0;
525
8c2786cf
DM
526 /* Similarly for all PCI bridges, if we get this far
527 * it lacks a ranges property, and this will include
528 * cases like Simba.
529 */
530 if (!strcmp(pp->type, "pci") || !strcmp(pp->type, "pciex"))
a83f9823
DM
531 return 0;
532
533 return 1;
cf44bbc2
DM
534}
535
a83f9823
DM
536static int of_resource_verbose;
537
cf44bbc2
DM
538static void __init build_device_resources(struct of_device *op,
539 struct device *parent)
540{
541 struct of_device *p_op;
542 struct of_bus *bus;
543 int na, ns;
544 int index, num_reg;
6a23acf3 545 const void *preg;
cf44bbc2
DM
546
547 if (!parent)
548 return;
549
550 p_op = to_of_device(parent);
551 bus = of_match_bus(p_op->node);
552 bus->count_cells(op->node, &na, &ns);
553
554 preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
555 if (!preg || num_reg == 0)
556 return;
557
558 /* Convert to num-cells. */
559 num_reg /= 4;
560
46ba6d7d 561 /* Convert to num-entries. */
cf44bbc2
DM
562 num_reg /= na + ns;
563
e5dd42e4 564 /* Prevent overrunning the op->resources[] array. */
46ba6d7d
DM
565 if (num_reg > PROMREG_MAX) {
566 printk(KERN_WARNING "%s: Too many regs (%d), "
567 "limiting to %d.\n",
568 op->node->full_name, num_reg, PROMREG_MAX);
569 num_reg = PROMREG_MAX;
570 }
571
cf44bbc2
DM
572 for (index = 0; index < num_reg; index++) {
573 struct resource *r = &op->resource[index];
574 u32 addr[OF_MAX_ADDR_CELLS];
6a23acf3 575 const u32 *reg = (preg + (index * ((na + ns) * 4)));
cf44bbc2
DM
576 struct device_node *dp = op->node;
577 struct device_node *pp = p_op->node;
b85cdd49 578 struct of_bus *pbus, *dbus;
cf44bbc2
DM
579 u64 size, result = OF_BAD_ADDR;
580 unsigned long flags;
581 int dna, dns;
582 int pna, pns;
583
584 size = of_read_addr(reg + na, ns);
585 flags = bus->get_flags(reg);
586
587 memcpy(addr, reg, na * 4);
588
a83f9823 589 if (use_1to1_mapping(pp)) {
cf44bbc2
DM
590 result = of_read_addr(addr, na);
591 goto build_res;
592 }
593
594 dna = na;
595 dns = ns;
b85cdd49 596 dbus = bus;
cf44bbc2
DM
597
598 while (1) {
599 dp = pp;
600 pp = dp->parent;
601 if (!pp) {
602 result = of_read_addr(addr, dna);
603 break;
604 }
605
606 pbus = of_match_bus(pp);
607 pbus->count_cells(dp, &pna, &pns);
608
b85cdd49 609 if (build_one_resource(dp, dbus, pbus, addr,
a83f9823 610 dna, dns, pna))
cf44bbc2
DM
611 break;
612
613 dna = pna;
614 dns = pns;
b85cdd49 615 dbus = pbus;
cf44bbc2
DM
616 }
617
618 build_res:
619 memset(r, 0, sizeof(*r));
a83f9823
DM
620
621 if (of_resource_verbose)
622 printk("%s reg[%d] -> %lx\n",
623 op->node->full_name, index,
624 result);
625
cf44bbc2 626 if (result != OF_BAD_ADDR) {
1815aed5
DM
627 if (tlb_type == hypervisor)
628 result &= 0x0fffffffffffffffUL;
629
cf44bbc2
DM
630 r->start = result;
631 r->end = result + size - 1;
632 r->flags = flags;
cf44bbc2
DM
633 }
634 r->name = op->node->name;
635 }
636}
637
2b1e5978
DM
638static struct device_node * __init
639apply_interrupt_map(struct device_node *dp, struct device_node *pp,
6a23acf3 640 const u32 *imap, int imlen, const u32 *imask,
2b1e5978
DM
641 unsigned int *irq_p)
642{
643 struct device_node *cp;
644 unsigned int irq = *irq_p;
645 struct of_bus *bus;
646 phandle handle;
6a23acf3 647 const u32 *reg;
2b1e5978
DM
648 int na, num_reg, i;
649
650 bus = of_match_bus(pp);
651 bus->count_cells(dp, &na, NULL);
652
653 reg = of_get_property(dp, "reg", &num_reg);
654 if (!reg || !num_reg)
655 return NULL;
656
657 imlen /= ((na + 3) * 4);
658 handle = 0;
659 for (i = 0; i < imlen; i++) {
660 int j;
661
662 for (j = 0; j < na; j++) {
663 if ((reg[j] & imask[j]) != imap[j])
664 goto next;
665 }
666 if (imap[na] == irq) {
667 handle = imap[na + 1];
668 irq = imap[na + 2];
669 break;
670 }
671
672 next:
673 imap += (na + 3);
674 }
46ba6d7d
DM
675 if (i == imlen) {
676 /* Psycho and Sabre PCI controllers can have 'interrupt-map'
677 * properties that do not include the on-board device
678 * interrupts. Instead, the device's 'interrupts' property
679 * is already a fully specified INO value.
680 *
681 * Handle this by deciding that, if we didn't get a
682 * match in the parent's 'interrupt-map', and the
683 * parent is an IRQ translater, then use the parent as
684 * our IRQ controller.
685 */
686 if (pp->irq_trans)
687 return pp;
688
2b1e5978 689 return NULL;
46ba6d7d 690 }
2b1e5978
DM
691
692 *irq_p = irq;
693 cp = of_find_node_by_phandle(handle);
694
695 return cp;
696}
697
698static unsigned int __init pci_irq_swizzle(struct device_node *dp,
699 struct device_node *pp,
700 unsigned int irq)
701{
6a23acf3 702 const struct linux_prom_pci_registers *regs;
bb4c18cb 703 unsigned int bus, devfn, slot, ret;
2b1e5978
DM
704
705 if (irq < 1 || irq > 4)
706 return irq;
707
708 regs = of_get_property(dp, "reg", NULL);
709 if (!regs)
710 return irq;
711
bb4c18cb 712 bus = (regs->phys_hi >> 16) & 0xff;
2b1e5978
DM
713 devfn = (regs->phys_hi >> 8) & 0xff;
714 slot = (devfn >> 3) & 0x1f;
715
bb4c18cb
DM
716 if (pp->irq_trans) {
717 /* Derived from Table 8-3, U2P User's Manual. This branch
718 * is handling a PCI controller that lacks a proper set of
719 * interrupt-map and interrupt-map-mask properties. The
720 * Ultra-E450 is one example.
721 *
722 * The bit layout is BSSLL, where:
723 * B: 0 on bus A, 1 on bus B
724 * D: 2-bit slot number, derived from PCI device number as
725 * (dev - 1) for bus A, or (dev - 2) for bus B
726 * L: 2-bit line number
bb4c18cb
DM
727 */
728 if (bus & 0x80) {
729 /* PBM-A */
730 bus = 0x00;
731 slot = (slot - 1) << 2;
732 } else {
733 /* PBM-B */
734 bus = 0x10;
735 slot = (slot - 2) << 2;
736 }
737 irq -= 1;
738
739 ret = (bus | slot | irq);
740 } else {
741 /* Going through a PCI-PCI bridge that lacks a set of
742 * interrupt-map and interrupt-map-mask properties.
743 */
744 ret = ((irq - 1 + (slot & 3)) & 3) + 1;
745 }
2b1e5978
DM
746
747 return ret;
748}
749
a83f9823
DM
750static int of_irq_verbose;
751
2b1e5978
DM
752static unsigned int __init build_one_device_irq(struct of_device *op,
753 struct device *parent,
754 unsigned int irq)
755{
756 struct device_node *dp = op->node;
757 struct device_node *pp, *ip;
758 unsigned int orig_irq = irq;
759
760 if (irq == 0xffffffff)
761 return irq;
762
763 if (dp->irq_trans) {
764 irq = dp->irq_trans->irq_build(dp, irq,
765 dp->irq_trans->data);
a83f9823
DM
766
767 if (of_irq_verbose)
768 printk("%s: direct translate %x --> %x\n",
769 dp->full_name, orig_irq, irq);
770
2b1e5978
DM
771 return irq;
772 }
773
774 /* Something more complicated. Walk up to the root, applying
775 * interrupt-map or bus specific translations, until we hit
776 * an IRQ translator.
777 *
778 * If we hit a bus type or situation we cannot handle, we
779 * stop and assume that the original IRQ number was in a
780 * format which has special meaning to it's immediate parent.
781 */
782 pp = dp->parent;
783 ip = NULL;
784 while (pp) {
6a23acf3 785 const void *imap, *imsk;
2b1e5978
DM
786 int imlen;
787
788 imap = of_get_property(pp, "interrupt-map", &imlen);
789 imsk = of_get_property(pp, "interrupt-map-mask", NULL);
790 if (imap && imsk) {
791 struct device_node *iret;
792 int this_orig_irq = irq;
793
794 iret = apply_interrupt_map(dp, pp,
795 imap, imlen, imsk,
796 &irq);
a83f9823
DM
797
798 if (of_irq_verbose)
799 printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
800 op->node->full_name,
801 pp->full_name, this_orig_irq,
802 (iret ? iret->full_name : "NULL"), irq);
803
2b1e5978
DM
804 if (!iret)
805 break;
806
807 if (iret->irq_trans) {
808 ip = iret;
809 break;
810 }
811 } else {
812 if (!strcmp(pp->type, "pci") ||
813 !strcmp(pp->type, "pciex")) {
814 unsigned int this_orig_irq = irq;
815
816 irq = pci_irq_swizzle(dp, pp, irq);
a83f9823
DM
817 if (of_irq_verbose)
818 printk("%s: PCI swizzle [%s] "
819 "%x --> %x\n",
820 op->node->full_name,
821 pp->full_name, this_orig_irq,
822 irq);
823
2b1e5978
DM
824 }
825
826 if (pp->irq_trans) {
827 ip = pp;
828 break;
829 }
830 }
831 dp = pp;
832 pp = pp->parent;
833 }
834 if (!ip)
835 return orig_irq;
836
837 irq = ip->irq_trans->irq_build(op->node, irq,
838 ip->irq_trans->data);
a83f9823
DM
839 if (of_irq_verbose)
840 printk("%s: Apply IRQ trans [%s] %x --> %x\n",
841 op->node->full_name, ip->full_name, orig_irq, irq);
2b1e5978
DM
842
843 return irq;
844}
845
cf44bbc2
DM
846static struct of_device * __init scan_one_device(struct device_node *dp,
847 struct device *parent)
848{
849 struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
6a23acf3 850 const unsigned int *irq;
2b1e5978 851 int len, i;
cf44bbc2
DM
852
853 if (!op)
854 return NULL;
855
856 op->node = dp;
857
858 op->clock_freq = of_getintprop_default(dp, "clock-frequency",
859 (25*1000*1000));
860 op->portid = of_getintprop_default(dp, "upa-portid", -1);
861 if (op->portid == -1)
862 op->portid = of_getintprop_default(dp, "portid", -1);
863
864 irq = of_get_property(dp, "interrupts", &len);
2b1e5978
DM
865 if (irq) {
866 memcpy(op->irqs, irq, len);
867 op->num_irqs = len / 4;
868 } else {
869 op->num_irqs = 0;
870 }
cf44bbc2 871
e5dd42e4 872 /* Prevent overrunning the op->irqs[] array. */
46ba6d7d
DM
873 if (op->num_irqs > PROMINTR_MAX) {
874 printk(KERN_WARNING "%s: Too many irqs (%d), "
875 "limiting to %d.\n",
876 dp->full_name, op->num_irqs, PROMINTR_MAX);
877 op->num_irqs = PROMINTR_MAX;
878 }
879
cf44bbc2 880 build_device_resources(op, parent);
2b1e5978
DM
881 for (i = 0; i < op->num_irqs; i++)
882 op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]);
cf44bbc2
DM
883
884 op->dev.parent = parent;
37b7754a 885 op->dev.bus = &of_platform_bus_type;
cf44bbc2
DM
886 if (!parent)
887 strcpy(op->dev.bus_id, "root");
888 else
f5ef9d11 889 sprintf(op->dev.bus_id, "%08x", dp->node);
cf44bbc2
DM
890
891 if (of_device_register(op)) {
892 printk("%s: Could not register of device.\n",
893 dp->full_name);
894 kfree(op);
895 op = NULL;
896 }
897
898 return op;
899}
900
901static void __init scan_tree(struct device_node *dp, struct device *parent)
902{
903 while (dp) {
904 struct of_device *op = scan_one_device(dp, parent);
905
906 if (op)
907 scan_tree(dp->child, &op->dev);
908
909 dp = dp->sibling;
910 }
911}
912
913static void __init scan_of_devices(void)
914{
915 struct device_node *root = of_find_node_by_path("/");
916 struct of_device *parent;
917
918 parent = scan_one_device(root, NULL);
919 if (!parent)
920 return;
921
922 scan_tree(root->child, &parent->dev);
923}
924
a2bd4fd1
DM
925static int __init of_bus_driver_init(void)
926{
cf44bbc2 927 int err;
a2bd4fd1 928
37b7754a 929 err = bus_register(&of_platform_bus_type);
a2bd4fd1
DM
930#ifdef CONFIG_PCI
931 if (!err)
932 err = bus_register(&isa_bus_type);
933 if (!err)
934 err = bus_register(&ebus_bus_type);
935#endif
936#ifdef CONFIG_SBUS
937 if (!err)
938 err = bus_register(&sbus_bus_type);
939#endif
cf44bbc2
DM
940
941 if (!err)
942 scan_of_devices();
943
944 return err;
a2bd4fd1
DM
945}
946
947postcore_initcall(of_bus_driver_init);
948
a83f9823
DM
949static int __init of_debug(char *str)
950{
951 int val = 0;
952
953 get_option(&str, &val);
954 if (val & 1)
955 of_resource_verbose = 1;
956 if (val & 2)
957 of_irq_verbose = 1;
958 return 1;
959}
960
961__setup("of_debug=", of_debug);
962
a2bd4fd1
DM
963int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
964{
965 /* initialize common driver fields */
966 drv->driver.name = drv->name;
967 drv->driver.bus = bus;
968
969 /* register with core */
970 return driver_register(&drv->driver);
971}
f85ff305 972EXPORT_SYMBOL(of_register_driver);
a2bd4fd1
DM
973
974void of_unregister_driver(struct of_platform_driver *drv)
975{
976 driver_unregister(&drv->driver);
977}
f85ff305 978EXPORT_SYMBOL(of_unregister_driver);
a2bd4fd1
DM
979
980struct of_device* of_platform_device_create(struct device_node *np,
981 const char *bus_id,
982 struct device *parent,
983 struct bus_type *bus)
984{
985 struct of_device *dev;
986
982c2064 987 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
a2bd4fd1
DM
988 if (!dev)
989 return NULL;
a2bd4fd1
DM
990
991 dev->dev.parent = parent;
992 dev->dev.bus = bus;
993 dev->dev.release = of_release_dev;
994
995 strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
996
997 if (of_device_register(dev) != 0) {
998 kfree(dev);
999 return NULL;
1000 }
1001
1002 return dev;
1003}
a2bd4fd1 1004EXPORT_SYMBOL(of_platform_device_create);
This page took 0.198973 seconds and 5 git commands to generate.