blob: d3dfb2a36d477584d8722430e07f01f701f9f01a [file] [log] [blame]
David S. Millera2bd4fd2006-06-23 01:44:10 -07001#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
David S. Miller3ca9fab2006-06-29 14:35:33 -0700131void __iomem *of_ioremap(struct resource *res, unsigned long offset, unsigned long size, char *name)
132{
133 unsigned long ret = res->start + offset;
David S. Miller6bda5732006-10-18 23:00:35 -0700134 struct resource *r;
David S. Miller3ca9fab2006-06-29 14:35:33 -0700135
David S. Miller6bda5732006-10-18 23:00:35 -0700136 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)
David S. Miller3ca9fab2006-06-29 14:35:33 -0700141 ret = 0;
142
143 return (void __iomem *) ret;
144}
145EXPORT_SYMBOL(of_ioremap);
146
147void of_iounmap(void __iomem *base, unsigned long size)
148{
149 release_region((unsigned long) base, size);
150}
151EXPORT_SYMBOL(of_iounmap);
152
David S. Miller2b1e5972006-06-29 15:07:37 -0700153static int node_match(struct device *dev, void *data)
154{
155 struct of_device *op = to_of_device(dev);
156 struct device_node *dp = data;
157
158 return (op->node == dp);
159}
160
161struct of_device *of_find_device_by_node(struct device_node *dp)
162{
163 struct device *dev = bus_find_device(&of_bus_type, NULL,
164 dp, node_match);
165
166 if (dev)
167 return to_of_device(dev);
168
169 return NULL;
170}
171EXPORT_SYMBOL(of_find_device_by_node);
172
David S. Millera2bd4fd2006-06-23 01:44:10 -0700173#ifdef CONFIG_PCI
174struct bus_type isa_bus_type = {
175 .name = "isa",
176 .match = of_platform_bus_match,
177 .probe = of_device_probe,
178 .remove = of_device_remove,
179 .suspend = of_device_suspend,
180 .resume = of_device_resume,
181};
David S. Miller69853912006-06-25 01:21:38 -0700182EXPORT_SYMBOL(isa_bus_type);
David S. Millera2bd4fd2006-06-23 01:44:10 -0700183
184struct bus_type ebus_bus_type = {
185 .name = "ebus",
186 .match = of_platform_bus_match,
187 .probe = of_device_probe,
188 .remove = of_device_remove,
189 .suspend = of_device_suspend,
190 .resume = of_device_resume,
191};
David S. Miller69853912006-06-25 01:21:38 -0700192EXPORT_SYMBOL(ebus_bus_type);
David S. Millera2bd4fd2006-06-23 01:44:10 -0700193#endif
194
195#ifdef CONFIG_SBUS
196struct bus_type sbus_bus_type = {
197 .name = "sbus",
198 .match = of_platform_bus_match,
199 .probe = of_device_probe,
200 .remove = of_device_remove,
201 .suspend = of_device_suspend,
202 .resume = of_device_resume,
203};
David S. Miller69853912006-06-25 01:21:38 -0700204EXPORT_SYMBOL(sbus_bus_type);
David S. Millera2bd4fd2006-06-23 01:44:10 -0700205#endif
206
David S. Millercf44bbc2006-06-29 14:34:50 -0700207struct bus_type of_bus_type = {
208 .name = "of",
209 .match = of_platform_bus_match,
210 .probe = of_device_probe,
211 .remove = of_device_remove,
212 .suspend = of_device_suspend,
213 .resume = of_device_resume,
214};
215EXPORT_SYMBOL(of_bus_type);
216
David S. Millera83f9822006-07-12 23:19:31 -0700217static inline u64 of_read_addr(const u32 *cell, int size)
David S. Millercf44bbc2006-06-29 14:34:50 -0700218{
219 u64 r = 0;
220 while (size--)
221 r = (r << 32) | *(cell++);
222 return r;
223}
224
225static void __init get_cells(struct device_node *dp,
226 int *addrc, int *sizec)
227{
228 if (addrc)
229 *addrc = of_n_addr_cells(dp);
230 if (sizec)
231 *sizec = of_n_size_cells(dp);
232}
233
234/* Max address size we deal with */
235#define OF_MAX_ADDR_CELLS 4
236
237struct of_bus {
238 const char *name;
239 const char *addr_prop_name;
240 int (*match)(struct device_node *parent);
241 void (*count_cells)(struct device_node *child,
242 int *addrc, int *sizec);
David S. Millera83f9822006-07-12 23:19:31 -0700243 int (*map)(u32 *addr, const u32 *range,
244 int na, int ns, int pna);
David S. Millercf44bbc2006-06-29 14:34:50 -0700245 unsigned int (*get_flags)(u32 *addr);
246};
247
248/*
249 * Default translator (generic bus)
250 */
251
252static void of_bus_default_count_cells(struct device_node *dev,
253 int *addrc, int *sizec)
254{
255 get_cells(dev, addrc, sizec);
256}
257
David S. Millera83f9822006-07-12 23:19:31 -0700258/* Make sure the least significant 64-bits are in-range. Even
259 * for 3 or 4 cell values it is a good enough approximation.
260 */
261static int of_out_of_range(const u32 *addr, const u32 *base,
262 const u32 *size, int na, int ns)
David S. Millercf44bbc2006-06-29 14:34:50 -0700263{
264 u64 a = of_read_addr(addr, na);
David S. Millera83f9822006-07-12 23:19:31 -0700265 u64 b = of_read_addr(base, na);
266
267 if (a < b)
268 return 1;
269
270 b += of_read_addr(size, ns);
271 if (a >= b)
272 return 1;
273
274 return 0;
275}
276
277static int of_bus_default_map(u32 *addr, const u32 *range,
278 int na, int ns, int pna)
279{
280 u32 result[OF_MAX_ADDR_CELLS];
281 int i;
282
283 if (ns > 2) {
284 printk("of_device: Cannot handle size cells (%d) > 2.", ns);
285 return -EINVAL;
286 }
287
288 if (of_out_of_range(addr, range, range + na + pna, na, ns))
289 return -EINVAL;
290
291 /* Start with the parent range base. */
292 memcpy(result, range + na, pna * 4);
293
294 /* Add in the child address offset. */
295 for (i = 0; i < na; i++)
296 result[pna - 1 - i] +=
297 (addr[na - 1 - i] -
298 range[na - 1 - i]);
299
300 memcpy(addr, result, pna * 4);
David S. Millercf44bbc2006-06-29 14:34:50 -0700301
302 return 0;
303}
304
305static unsigned int of_bus_default_get_flags(u32 *addr)
306{
307 return IORESOURCE_MEM;
308}
309
David S. Millercf44bbc2006-06-29 14:34:50 -0700310/*
311 * PCI bus specific translator
312 */
313
314static int of_bus_pci_match(struct device_node *np)
315{
David S. Millera83f9822006-07-12 23:19:31 -0700316 if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
317 /* Do not do PCI specific frobbing if the
318 * PCI bridge lacks a ranges property. We
319 * want to pass it through up to the next
320 * parent as-is, not with the PCI translate
321 * method which chops off the top address cell.
322 */
323 if (!of_find_property(np, "ranges", NULL))
324 return 0;
325
326 return 1;
327 }
328
329 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700330}
331
332static void of_bus_pci_count_cells(struct device_node *np,
333 int *addrc, int *sizec)
334{
335 if (addrc)
336 *addrc = 3;
337 if (sizec)
338 *sizec = 2;
339}
340
David S. Millera83f9822006-07-12 23:19:31 -0700341static int of_bus_pci_map(u32 *addr, const u32 *range,
342 int na, int ns, int pna)
David S. Millercf44bbc2006-06-29 14:34:50 -0700343{
David S. Millera83f9822006-07-12 23:19:31 -0700344 u32 result[OF_MAX_ADDR_CELLS];
345 int i;
David S. Millercf44bbc2006-06-29 14:34:50 -0700346
347 /* Check address type match */
348 if ((addr[0] ^ range[0]) & 0x03000000)
David S. Millera83f9822006-07-12 23:19:31 -0700349 return -EINVAL;
David S. Millercf44bbc2006-06-29 14:34:50 -0700350
David S. Millera83f9822006-07-12 23:19:31 -0700351 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
352 na - 1, ns))
353 return -EINVAL;
David S. Millercf44bbc2006-06-29 14:34:50 -0700354
David S. Millera83f9822006-07-12 23:19:31 -0700355 /* Start with the parent range base. */
356 memcpy(result, range + na, pna * 4);
David S. Millercf44bbc2006-06-29 14:34:50 -0700357
David S. Millera83f9822006-07-12 23:19:31 -0700358 /* Add in the child address offset, skipping high cell. */
359 for (i = 0; i < na - 1; i++)
360 result[pna - 1 - i] +=
361 (addr[na - 1 - i] -
362 range[na - 1 - i]);
363
364 memcpy(addr, result, pna * 4);
365
366 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700367}
368
369static unsigned int of_bus_pci_get_flags(u32 *addr)
370{
371 unsigned int flags = 0;
372 u32 w = addr[0];
373
374 switch((w >> 24) & 0x03) {
375 case 0x01:
376 flags |= IORESOURCE_IO;
377 case 0x02: /* 32 bits */
378 case 0x03: /* 64 bits */
379 flags |= IORESOURCE_MEM;
380 }
381 if (w & 0x40000000)
382 flags |= IORESOURCE_PREFETCH;
383 return flags;
384}
385
386/*
David S. Millercf44bbc2006-06-29 14:34:50 -0700387 * SBUS bus specific translator
388 */
389
390static int of_bus_sbus_match(struct device_node *np)
391{
392 return !strcmp(np->name, "sbus") ||
393 !strcmp(np->name, "sbi");
394}
395
396static void of_bus_sbus_count_cells(struct device_node *child,
397 int *addrc, int *sizec)
398{
399 if (addrc)
400 *addrc = 2;
401 if (sizec)
402 *sizec = 1;
403}
404
David S. Millera83f9822006-07-12 23:19:31 -0700405static int of_bus_sbus_map(u32 *addr, const u32 *range, int na, int ns, int pna)
David S. Millercf44bbc2006-06-29 14:34:50 -0700406{
407 return of_bus_default_map(addr, range, na, ns, pna);
408}
409
David S. Millercf44bbc2006-06-29 14:34:50 -0700410static unsigned int of_bus_sbus_get_flags(u32 *addr)
411{
412 return IORESOURCE_MEM;
413}
414
415
416/*
417 * Array of bus specific translators
418 */
419
420static struct of_bus of_busses[] = {
421 /* PCI */
422 {
423 .name = "pci",
424 .addr_prop_name = "assigned-addresses",
425 .match = of_bus_pci_match,
426 .count_cells = of_bus_pci_count_cells,
427 .map = of_bus_pci_map,
David S. Millercf44bbc2006-06-29 14:34:50 -0700428 .get_flags = of_bus_pci_get_flags,
429 },
David S. Millercf44bbc2006-06-29 14:34:50 -0700430 /* SBUS */
431 {
432 .name = "sbus",
433 .addr_prop_name = "reg",
434 .match = of_bus_sbus_match,
435 .count_cells = of_bus_sbus_count_cells,
436 .map = of_bus_sbus_map,
David S. Millercf44bbc2006-06-29 14:34:50 -0700437 .get_flags = of_bus_sbus_get_flags,
438 },
439 /* Default */
440 {
441 .name = "default",
442 .addr_prop_name = "reg",
443 .match = NULL,
444 .count_cells = of_bus_default_count_cells,
445 .map = of_bus_default_map,
David S. Millercf44bbc2006-06-29 14:34:50 -0700446 .get_flags = of_bus_default_get_flags,
447 },
448};
449
450static struct of_bus *of_match_bus(struct device_node *np)
451{
452 int i;
453
454 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
455 if (!of_busses[i].match || of_busses[i].match(np))
456 return &of_busses[i];
457 BUG();
458 return NULL;
459}
460
461static int __init build_one_resource(struct device_node *parent,
462 struct of_bus *bus,
463 struct of_bus *pbus,
464 u32 *addr,
465 int na, int ns, int pna)
466{
467 u32 *ranges;
468 unsigned int rlen;
469 int rone;
David S. Millercf44bbc2006-06-29 14:34:50 -0700470
471 ranges = of_get_property(parent, "ranges", &rlen);
472 if (ranges == NULL || rlen == 0) {
David S. Millera83f9822006-07-12 23:19:31 -0700473 u32 result[OF_MAX_ADDR_CELLS];
474 int i;
475
476 memset(result, 0, pna * 4);
477 for (i = 0; i < na; i++)
478 result[pna - 1 - i] =
479 addr[na - 1 - i];
480
481 memcpy(addr, result, pna * 4);
482 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700483 }
484
485 /* Now walk through the ranges */
486 rlen /= 4;
487 rone = na + pna + ns;
488 for (; rlen >= rone; rlen -= rone, ranges += rone) {
David S. Millera83f9822006-07-12 23:19:31 -0700489 if (!bus->map(addr, ranges, na, ns, pna))
490 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700491 }
David S. Millera83f9822006-07-12 23:19:31 -0700492
493 return 1;
494}
495
496static int __init use_1to1_mapping(struct device_node *pp)
497{
498 char *model;
499
500 /* If this is on the PMU bus, don't try to translate it even
501 * if a ranges property exists.
502 */
503 if (!strcmp(pp->name, "pmu"))
David S. Millercf44bbc2006-06-29 14:34:50 -0700504 return 1;
505
David S. Millera83f9822006-07-12 23:19:31 -0700506 /* If we have a ranges property in the parent, use it. */
507 if (of_find_property(pp, "ranges", NULL) != NULL)
508 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700509
David S. Millera83f9822006-07-12 23:19:31 -0700510 /* If the parent is the dma node of an ISA bus, pass
511 * the translation up to the root.
512 */
513 if (!strcmp(pp->name, "dma"))
514 return 0;
515
516 /* Similarly for Simba PCI bridges. */
517 model = of_get_property(pp, "model", NULL);
518 if (model && !strcmp(model, "SUNW,simba"))
519 return 0;
520
521 return 1;
David S. Millercf44bbc2006-06-29 14:34:50 -0700522}
523
David S. Millera83f9822006-07-12 23:19:31 -0700524static int of_resource_verbose;
525
David S. Millercf44bbc2006-06-29 14:34:50 -0700526static void __init build_device_resources(struct of_device *op,
527 struct device *parent)
528{
529 struct of_device *p_op;
530 struct of_bus *bus;
531 int na, ns;
532 int index, num_reg;
533 void *preg;
534
535 if (!parent)
536 return;
537
538 p_op = to_of_device(parent);
539 bus = of_match_bus(p_op->node);
540 bus->count_cells(op->node, &na, &ns);
541
542 preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
543 if (!preg || num_reg == 0)
544 return;
545
546 /* Convert to num-cells. */
547 num_reg /= 4;
548
David S. Miller46ba6d72006-07-16 22:10:44 -0700549 /* Convert to num-entries. */
David S. Millercf44bbc2006-06-29 14:34:50 -0700550 num_reg /= na + ns;
551
David S. Miller46ba6d72006-07-16 22:10:44 -0700552 /* Prevent overruning the op->resources[] array. */
553 if (num_reg > PROMREG_MAX) {
554 printk(KERN_WARNING "%s: Too many regs (%d), "
555 "limiting to %d.\n",
556 op->node->full_name, num_reg, PROMREG_MAX);
557 num_reg = PROMREG_MAX;
558 }
559
David S. Millercf44bbc2006-06-29 14:34:50 -0700560 for (index = 0; index < num_reg; index++) {
561 struct resource *r = &op->resource[index];
562 u32 addr[OF_MAX_ADDR_CELLS];
563 u32 *reg = (preg + (index * ((na + ns) * 4)));
564 struct device_node *dp = op->node;
565 struct device_node *pp = p_op->node;
566 struct of_bus *pbus;
567 u64 size, result = OF_BAD_ADDR;
568 unsigned long flags;
569 int dna, dns;
570 int pna, pns;
571
572 size = of_read_addr(reg + na, ns);
573 flags = bus->get_flags(reg);
574
575 memcpy(addr, reg, na * 4);
576
David S. Millera83f9822006-07-12 23:19:31 -0700577 if (use_1to1_mapping(pp)) {
David S. Millercf44bbc2006-06-29 14:34:50 -0700578 result = of_read_addr(addr, na);
579 goto build_res;
580 }
581
582 dna = na;
583 dns = ns;
584
585 while (1) {
586 dp = pp;
587 pp = dp->parent;
588 if (!pp) {
589 result = of_read_addr(addr, dna);
590 break;
591 }
592
593 pbus = of_match_bus(pp);
594 pbus->count_cells(dp, &pna, &pns);
595
David S. Millera83f9822006-07-12 23:19:31 -0700596 if (build_one_resource(dp, bus, pbus, addr,
597 dna, dns, pna))
David S. Millercf44bbc2006-06-29 14:34:50 -0700598 break;
599
600 dna = pna;
601 dns = pns;
602 bus = pbus;
603 }
604
605 build_res:
606 memset(r, 0, sizeof(*r));
David S. Millera83f9822006-07-12 23:19:31 -0700607
608 if (of_resource_verbose)
609 printk("%s reg[%d] -> %lx\n",
610 op->node->full_name, index,
611 result);
612
David S. Millercf44bbc2006-06-29 14:34:50 -0700613 if (result != OF_BAD_ADDR) {
David S. Miller1815aed2006-06-29 19:58:28 -0700614 if (tlb_type == hypervisor)
615 result &= 0x0fffffffffffffffUL;
616
David S. Millercf44bbc2006-06-29 14:34:50 -0700617 r->start = result;
618 r->end = result + size - 1;
619 r->flags = flags;
620 } else {
621 r->start = ~0UL;
622 r->end = ~0UL;
623 }
624 r->name = op->node->name;
625 }
626}
627
David S. Miller2b1e5972006-06-29 15:07:37 -0700628static struct device_node * __init
629apply_interrupt_map(struct device_node *dp, struct device_node *pp,
630 u32 *imap, int imlen, u32 *imask,
631 unsigned int *irq_p)
632{
633 struct device_node *cp;
634 unsigned int irq = *irq_p;
635 struct of_bus *bus;
636 phandle handle;
637 u32 *reg;
638 int na, num_reg, i;
639
640 bus = of_match_bus(pp);
641 bus->count_cells(dp, &na, NULL);
642
643 reg = of_get_property(dp, "reg", &num_reg);
644 if (!reg || !num_reg)
645 return NULL;
646
647 imlen /= ((na + 3) * 4);
648 handle = 0;
649 for (i = 0; i < imlen; i++) {
650 int j;
651
652 for (j = 0; j < na; j++) {
653 if ((reg[j] & imask[j]) != imap[j])
654 goto next;
655 }
656 if (imap[na] == irq) {
657 handle = imap[na + 1];
658 irq = imap[na + 2];
659 break;
660 }
661
662 next:
663 imap += (na + 3);
664 }
David S. Miller46ba6d72006-07-16 22:10:44 -0700665 if (i == imlen) {
666 /* Psycho and Sabre PCI controllers can have 'interrupt-map'
667 * properties that do not include the on-board device
668 * interrupts. Instead, the device's 'interrupts' property
669 * is already a fully specified INO value.
670 *
671 * Handle this by deciding that, if we didn't get a
672 * match in the parent's 'interrupt-map', and the
673 * parent is an IRQ translater, then use the parent as
674 * our IRQ controller.
675 */
676 if (pp->irq_trans)
677 return pp;
678
David S. Miller2b1e5972006-06-29 15:07:37 -0700679 return NULL;
David S. Miller46ba6d72006-07-16 22:10:44 -0700680 }
David S. Miller2b1e5972006-06-29 15:07:37 -0700681
682 *irq_p = irq;
683 cp = of_find_node_by_phandle(handle);
684
685 return cp;
686}
687
688static unsigned int __init pci_irq_swizzle(struct device_node *dp,
689 struct device_node *pp,
690 unsigned int irq)
691{
692 struct linux_prom_pci_registers *regs;
693 unsigned int devfn, slot, ret;
694
695 if (irq < 1 || irq > 4)
696 return irq;
697
698 regs = of_get_property(dp, "reg", NULL);
699 if (!regs)
700 return irq;
701
702 devfn = (regs->phys_hi >> 8) & 0xff;
703 slot = (devfn >> 3) & 0x1f;
704
705 ret = ((irq - 1 + (slot & 3)) & 3) + 1;
706
707 return ret;
708}
709
David S. Millera83f9822006-07-12 23:19:31 -0700710static int of_irq_verbose;
711
David S. Miller2b1e5972006-06-29 15:07:37 -0700712static unsigned int __init build_one_device_irq(struct of_device *op,
713 struct device *parent,
714 unsigned int irq)
715{
716 struct device_node *dp = op->node;
717 struct device_node *pp, *ip;
718 unsigned int orig_irq = irq;
719
720 if (irq == 0xffffffff)
721 return irq;
722
723 if (dp->irq_trans) {
724 irq = dp->irq_trans->irq_build(dp, irq,
725 dp->irq_trans->data);
David S. Millera83f9822006-07-12 23:19:31 -0700726
727 if (of_irq_verbose)
728 printk("%s: direct translate %x --> %x\n",
729 dp->full_name, orig_irq, irq);
730
David S. Miller2b1e5972006-06-29 15:07:37 -0700731 return irq;
732 }
733
734 /* Something more complicated. Walk up to the root, applying
735 * interrupt-map or bus specific translations, until we hit
736 * an IRQ translator.
737 *
738 * If we hit a bus type or situation we cannot handle, we
739 * stop and assume that the original IRQ number was in a
740 * format which has special meaning to it's immediate parent.
741 */
742 pp = dp->parent;
743 ip = NULL;
744 while (pp) {
745 void *imap, *imsk;
746 int imlen;
747
748 imap = of_get_property(pp, "interrupt-map", &imlen);
749 imsk = of_get_property(pp, "interrupt-map-mask", NULL);
750 if (imap && imsk) {
751 struct device_node *iret;
752 int this_orig_irq = irq;
753
754 iret = apply_interrupt_map(dp, pp,
755 imap, imlen, imsk,
756 &irq);
David S. Millera83f9822006-07-12 23:19:31 -0700757
758 if (of_irq_verbose)
759 printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
760 op->node->full_name,
761 pp->full_name, this_orig_irq,
762 (iret ? iret->full_name : "NULL"), irq);
763
David S. Miller2b1e5972006-06-29 15:07:37 -0700764 if (!iret)
765 break;
766
767 if (iret->irq_trans) {
768 ip = iret;
769 break;
770 }
771 } else {
772 if (!strcmp(pp->type, "pci") ||
773 !strcmp(pp->type, "pciex")) {
774 unsigned int this_orig_irq = irq;
775
776 irq = pci_irq_swizzle(dp, pp, irq);
David S. Millera83f9822006-07-12 23:19:31 -0700777 if (of_irq_verbose)
778 printk("%s: PCI swizzle [%s] "
779 "%x --> %x\n",
780 op->node->full_name,
781 pp->full_name, this_orig_irq,
782 irq);
783
David S. Miller2b1e5972006-06-29 15:07:37 -0700784 }
785
786 if (pp->irq_trans) {
787 ip = pp;
788 break;
789 }
790 }
791 dp = pp;
792 pp = pp->parent;
793 }
794 if (!ip)
795 return orig_irq;
796
797 irq = ip->irq_trans->irq_build(op->node, irq,
798 ip->irq_trans->data);
David S. Millera83f9822006-07-12 23:19:31 -0700799 if (of_irq_verbose)
800 printk("%s: Apply IRQ trans [%s] %x --> %x\n",
801 op->node->full_name, ip->full_name, orig_irq, irq);
David S. Miller2b1e5972006-06-29 15:07:37 -0700802
803 return irq;
804}
805
David S. Millercf44bbc2006-06-29 14:34:50 -0700806static struct of_device * __init scan_one_device(struct device_node *dp,
807 struct device *parent)
808{
809 struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
810 unsigned int *irq;
David S. Miller2b1e5972006-06-29 15:07:37 -0700811 int len, i;
David S. Millercf44bbc2006-06-29 14:34:50 -0700812
813 if (!op)
814 return NULL;
815
816 op->node = dp;
817
818 op->clock_freq = of_getintprop_default(dp, "clock-frequency",
819 (25*1000*1000));
820 op->portid = of_getintprop_default(dp, "upa-portid", -1);
821 if (op->portid == -1)
822 op->portid = of_getintprop_default(dp, "portid", -1);
823
824 irq = of_get_property(dp, "interrupts", &len);
David S. Miller2b1e5972006-06-29 15:07:37 -0700825 if (irq) {
826 memcpy(op->irqs, irq, len);
827 op->num_irqs = len / 4;
828 } else {
829 op->num_irqs = 0;
830 }
David S. Millercf44bbc2006-06-29 14:34:50 -0700831
David S. Miller46ba6d72006-07-16 22:10:44 -0700832 /* Prevent overruning the op->irqs[] array. */
833 if (op->num_irqs > PROMINTR_MAX) {
834 printk(KERN_WARNING "%s: Too many irqs (%d), "
835 "limiting to %d.\n",
836 dp->full_name, op->num_irqs, PROMINTR_MAX);
837 op->num_irqs = PROMINTR_MAX;
838 }
839
David S. Millercf44bbc2006-06-29 14:34:50 -0700840 build_device_resources(op, parent);
David S. Miller2b1e5972006-06-29 15:07:37 -0700841 for (i = 0; i < op->num_irqs; i++)
842 op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]);
David S. Millercf44bbc2006-06-29 14:34:50 -0700843
844 op->dev.parent = parent;
845 op->dev.bus = &of_bus_type;
846 if (!parent)
847 strcpy(op->dev.bus_id, "root");
848 else
David S. Miller4b75eb22006-10-05 02:07:32 -0700849 sprintf(op->dev.bus_id, "%s@%08x", dp->name, dp->node);
David S. Millercf44bbc2006-06-29 14:34:50 -0700850
851 if (of_device_register(op)) {
852 printk("%s: Could not register of device.\n",
853 dp->full_name);
854 kfree(op);
855 op = NULL;
856 }
857
858 return op;
859}
860
861static void __init scan_tree(struct device_node *dp, struct device *parent)
862{
863 while (dp) {
864 struct of_device *op = scan_one_device(dp, parent);
865
866 if (op)
867 scan_tree(dp->child, &op->dev);
868
869 dp = dp->sibling;
870 }
871}
872
873static void __init scan_of_devices(void)
874{
875 struct device_node *root = of_find_node_by_path("/");
876 struct of_device *parent;
877
878 parent = scan_one_device(root, NULL);
879 if (!parent)
880 return;
881
882 scan_tree(root->child, &parent->dev);
883}
884
David S. Millera2bd4fd2006-06-23 01:44:10 -0700885static int __init of_bus_driver_init(void)
886{
David S. Millercf44bbc2006-06-29 14:34:50 -0700887 int err;
David S. Millera2bd4fd2006-06-23 01:44:10 -0700888
David S. Millercf44bbc2006-06-29 14:34:50 -0700889 err = bus_register(&of_bus_type);
David S. Millera2bd4fd2006-06-23 01:44:10 -0700890#ifdef CONFIG_PCI
891 if (!err)
892 err = bus_register(&isa_bus_type);
893 if (!err)
894 err = bus_register(&ebus_bus_type);
895#endif
896#ifdef CONFIG_SBUS
897 if (!err)
898 err = bus_register(&sbus_bus_type);
899#endif
David S. Millercf44bbc2006-06-29 14:34:50 -0700900
901 if (!err)
902 scan_of_devices();
903
904 return err;
David S. Millera2bd4fd2006-06-23 01:44:10 -0700905}
906
907postcore_initcall(of_bus_driver_init);
908
David S. Millera83f9822006-07-12 23:19:31 -0700909static int __init of_debug(char *str)
910{
911 int val = 0;
912
913 get_option(&str, &val);
914 if (val & 1)
915 of_resource_verbose = 1;
916 if (val & 2)
917 of_irq_verbose = 1;
918 return 1;
919}
920
921__setup("of_debug=", of_debug);
922
David S. Millera2bd4fd2006-06-23 01:44:10 -0700923int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
924{
925 /* initialize common driver fields */
926 drv->driver.name = drv->name;
927 drv->driver.bus = bus;
928
929 /* register with core */
930 return driver_register(&drv->driver);
931}
932
933void of_unregister_driver(struct of_platform_driver *drv)
934{
935 driver_unregister(&drv->driver);
936}
937
938
939static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
940{
941 struct of_device *ofdev;
942
943 ofdev = to_of_device(dev);
944 return sprintf(buf, "%s", ofdev->node->full_name);
945}
946
947static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
948
949/**
950 * of_release_dev - free an of device structure when all users of it are finished.
951 * @dev: device that's been disconnected
952 *
953 * Will be called only by the device core when all users of this of device are
954 * done.
955 */
956void of_release_dev(struct device *dev)
957{
958 struct of_device *ofdev;
959
960 ofdev = to_of_device(dev);
961
962 kfree(ofdev);
963}
964
965int of_device_register(struct of_device *ofdev)
966{
967 int rc;
968
969 BUG_ON(ofdev->node == NULL);
970
971 rc = device_register(&ofdev->dev);
972 if (rc)
973 return rc;
974
Andrew Morton6cc8b6f2006-07-10 15:28:54 -0700975 rc = device_create_file(&ofdev->dev, &dev_attr_devspec);
976 if (rc)
977 device_unregister(&ofdev->dev);
David S. Millera2bd4fd2006-06-23 01:44:10 -0700978
Andrew Morton6cc8b6f2006-07-10 15:28:54 -0700979 return rc;
David S. Millera2bd4fd2006-06-23 01:44:10 -0700980}
981
982void of_device_unregister(struct of_device *ofdev)
983{
984 device_remove_file(&ofdev->dev, &dev_attr_devspec);
985 device_unregister(&ofdev->dev);
986}
987
988struct of_device* of_platform_device_create(struct device_node *np,
989 const char *bus_id,
990 struct device *parent,
991 struct bus_type *bus)
992{
993 struct of_device *dev;
994
995 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
996 if (!dev)
997 return NULL;
998 memset(dev, 0, sizeof(*dev));
999
1000 dev->dev.parent = parent;
1001 dev->dev.bus = bus;
1002 dev->dev.release = of_release_dev;
1003
1004 strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
1005
1006 if (of_device_register(dev) != 0) {
1007 kfree(dev);
1008 return NULL;
1009 }
1010
1011 return dev;
1012}
1013
1014EXPORT_SYMBOL(of_match_device);
1015EXPORT_SYMBOL(of_register_driver);
1016EXPORT_SYMBOL(of_unregister_driver);
1017EXPORT_SYMBOL(of_device_register);
1018EXPORT_SYMBOL(of_device_unregister);
1019EXPORT_SYMBOL(of_dev_get);
1020EXPORT_SYMBOL(of_dev_put);
1021EXPORT_SYMBOL(of_platform_device_create);
1022EXPORT_SYMBOL(of_release_dev);