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