blob: 1f4598524bdd7a4702bd1b452ec06851af5b5428 [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
David S. Millere3a411a2006-12-28 21:01:32 -0800147void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
David S. Miller3ca9fab2006-06-29 14:35:33 -0700148{
David S. Millere3a411a2006-12-28 21:01:32 -0800149 if (res->flags & IORESOURCE_MEM)
150 release_mem_region((unsigned long) base, size);
151 else
152 release_region((unsigned long) base, size);
David S. Miller3ca9fab2006-06-29 14:35:33 -0700153}
154EXPORT_SYMBOL(of_iounmap);
155
David S. Miller2b1e5972006-06-29 15:07:37 -0700156static 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
David S. Millera2bd4fd2006-06-23 01:44:10 -0700176#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};
David S. Miller69853912006-06-25 01:21:38 -0700185EXPORT_SYMBOL(isa_bus_type);
David S. Millera2bd4fd2006-06-23 01:44:10 -0700186
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};
David S. Miller69853912006-06-25 01:21:38 -0700195EXPORT_SYMBOL(ebus_bus_type);
David S. Millera2bd4fd2006-06-23 01:44:10 -0700196#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};
David S. Miller69853912006-06-25 01:21:38 -0700207EXPORT_SYMBOL(sbus_bus_type);
David S. Millera2bd4fd2006-06-23 01:44:10 -0700208#endif
209
David S. Millercf44bbc2006-06-29 14:34:50 -0700210struct 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
David S. Millera83f9822006-07-12 23:19:31 -0700220static inline u64 of_read_addr(const u32 *cell, int size)
David S. Millercf44bbc2006-06-29 14:34:50 -0700221{
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);
David S. Millera83f9822006-07-12 23:19:31 -0700246 int (*map)(u32 *addr, const u32 *range,
247 int na, int ns, int pna);
David S. Millercf44bbc2006-06-29 14:34:50 -0700248 unsigned int (*get_flags)(u32 *addr);
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
David S. Millera83f9822006-07-12 23:19:31 -0700261/* 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)
David S. Millercf44bbc2006-06-29 14:34:50 -0700266{
267 u64 a = of_read_addr(addr, na);
David S. Millera83f9822006-07-12 23:19:31 -0700268 u64 b = of_read_addr(base, na);
269
270 if (a < b)
271 return 1;
272
273 b += of_read_addr(size, ns);
274 if (a >= b)
275 return 1;
276
277 return 0;
278}
279
280static int of_bus_default_map(u32 *addr, const u32 *range,
281 int na, int ns, int pna)
282{
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);
David S. Millercf44bbc2006-06-29 14:34:50 -0700304
305 return 0;
306}
307
308static unsigned int of_bus_default_get_flags(u32 *addr)
309{
310 return IORESOURCE_MEM;
311}
312
David S. Millercf44bbc2006-06-29 14:34:50 -0700313/*
314 * PCI bus specific translator
315 */
316
317static int of_bus_pci_match(struct device_node *np)
318{
David S. Millera83f9822006-07-12 23:19:31 -0700319 if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
320 /* Do not do PCI specific frobbing if the
321 * PCI bridge lacks a ranges property. We
322 * want to pass it through up to the next
323 * parent as-is, not with the PCI translate
324 * method which chops off the top address cell.
325 */
326 if (!of_find_property(np, "ranges", NULL))
327 return 0;
328
329 return 1;
330 }
331
332 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700333}
334
335static void of_bus_pci_count_cells(struct device_node *np,
336 int *addrc, int *sizec)
337{
338 if (addrc)
339 *addrc = 3;
340 if (sizec)
341 *sizec = 2;
342}
343
David S. Millera83f9822006-07-12 23:19:31 -0700344static int of_bus_pci_map(u32 *addr, const u32 *range,
345 int na, int ns, int pna)
David S. Millercf44bbc2006-06-29 14:34:50 -0700346{
David S. Millera83f9822006-07-12 23:19:31 -0700347 u32 result[OF_MAX_ADDR_CELLS];
348 int i;
David S. Millercf44bbc2006-06-29 14:34:50 -0700349
350 /* Check address type match */
351 if ((addr[0] ^ range[0]) & 0x03000000)
David S. Millera83f9822006-07-12 23:19:31 -0700352 return -EINVAL;
David S. Millercf44bbc2006-06-29 14:34:50 -0700353
David S. Millera83f9822006-07-12 23:19:31 -0700354 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
355 na - 1, ns))
356 return -EINVAL;
David S. Millercf44bbc2006-06-29 14:34:50 -0700357
David S. Millera83f9822006-07-12 23:19:31 -0700358 /* Start with the parent range base. */
359 memcpy(result, range + na, pna * 4);
David S. Millercf44bbc2006-06-29 14:34:50 -0700360
David S. Millera83f9822006-07-12 23:19:31 -0700361 /* Add in the child address offset, skipping high cell. */
362 for (i = 0; i < na - 1; i++)
363 result[pna - 1 - i] +=
364 (addr[na - 1 - i] -
365 range[na - 1 - i]);
366
367 memcpy(addr, result, pna * 4);
368
369 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700370}
371
372static unsigned int of_bus_pci_get_flags(u32 *addr)
373{
374 unsigned int flags = 0;
375 u32 w = addr[0];
376
377 switch((w >> 24) & 0x03) {
378 case 0x01:
379 flags |= IORESOURCE_IO;
380 case 0x02: /* 32 bits */
381 case 0x03: /* 64 bits */
382 flags |= IORESOURCE_MEM;
383 }
384 if (w & 0x40000000)
385 flags |= IORESOURCE_PREFETCH;
386 return flags;
387}
388
389/*
David S. Millercf44bbc2006-06-29 14:34:50 -0700390 * SBUS bus specific translator
391 */
392
393static int of_bus_sbus_match(struct device_node *np)
394{
395 return !strcmp(np->name, "sbus") ||
396 !strcmp(np->name, "sbi");
397}
398
399static void of_bus_sbus_count_cells(struct device_node *child,
400 int *addrc, int *sizec)
401{
402 if (addrc)
403 *addrc = 2;
404 if (sizec)
405 *sizec = 1;
406}
407
David S. Miller4130a4b2006-10-25 22:31:06 -0700408/*
409 * FHC/Central bus specific translator.
410 *
411 * This is just needed to hard-code the address and size cell
412 * counts. 'fhc' and 'central' nodes lack the #address-cells and
413 * #size-cells properties, and if you walk to the root on such
414 * Enterprise boxes all you'll get is a #size-cells of 2 which is
415 * not what we want to use.
416 */
417static int of_bus_fhc_match(struct device_node *np)
David S. Millercf44bbc2006-06-29 14:34:50 -0700418{
David S. Miller4130a4b2006-10-25 22:31:06 -0700419 return !strcmp(np->name, "fhc") ||
420 !strcmp(np->name, "central");
David S. Millercf44bbc2006-06-29 14:34:50 -0700421}
422
David S. Miller4130a4b2006-10-25 22:31:06 -0700423#define of_bus_fhc_count_cells of_bus_sbus_count_cells
David S. Millercf44bbc2006-06-29 14:34:50 -0700424
425/*
426 * Array of bus specific translators
427 */
428
429static struct of_bus of_busses[] = {
430 /* PCI */
431 {
432 .name = "pci",
433 .addr_prop_name = "assigned-addresses",
434 .match = of_bus_pci_match,
435 .count_cells = of_bus_pci_count_cells,
436 .map = of_bus_pci_map,
David S. Millercf44bbc2006-06-29 14:34:50 -0700437 .get_flags = of_bus_pci_get_flags,
438 },
David S. Millercf44bbc2006-06-29 14:34:50 -0700439 /* SBUS */
440 {
441 .name = "sbus",
442 .addr_prop_name = "reg",
443 .match = of_bus_sbus_match,
444 .count_cells = of_bus_sbus_count_cells,
David S. Miller4130a4b2006-10-25 22:31:06 -0700445 .map = of_bus_default_map,
446 .get_flags = of_bus_default_get_flags,
447 },
448 /* FHC */
449 {
450 .name = "fhc",
451 .addr_prop_name = "reg",
452 .match = of_bus_fhc_match,
453 .count_cells = of_bus_fhc_count_cells,
454 .map = of_bus_default_map,
455 .get_flags = of_bus_default_get_flags,
David S. Millercf44bbc2006-06-29 14:34:50 -0700456 },
457 /* Default */
458 {
459 .name = "default",
460 .addr_prop_name = "reg",
461 .match = NULL,
462 .count_cells = of_bus_default_count_cells,
463 .map = of_bus_default_map,
David S. Millercf44bbc2006-06-29 14:34:50 -0700464 .get_flags = of_bus_default_get_flags,
465 },
466};
467
468static struct of_bus *of_match_bus(struct device_node *np)
469{
470 int i;
471
472 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
473 if (!of_busses[i].match || of_busses[i].match(np))
474 return &of_busses[i];
475 BUG();
476 return NULL;
477}
478
479static int __init build_one_resource(struct device_node *parent,
480 struct of_bus *bus,
481 struct of_bus *pbus,
482 u32 *addr,
483 int na, int ns, int pna)
484{
485 u32 *ranges;
486 unsigned int rlen;
487 int rone;
David S. Millercf44bbc2006-06-29 14:34:50 -0700488
489 ranges = of_get_property(parent, "ranges", &rlen);
490 if (ranges == NULL || rlen == 0) {
David S. Millera83f9822006-07-12 23:19:31 -0700491 u32 result[OF_MAX_ADDR_CELLS];
492 int i;
493
494 memset(result, 0, pna * 4);
495 for (i = 0; i < na; i++)
496 result[pna - 1 - i] =
497 addr[na - 1 - i];
498
499 memcpy(addr, result, pna * 4);
500 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700501 }
502
503 /* Now walk through the ranges */
504 rlen /= 4;
505 rone = na + pna + ns;
506 for (; rlen >= rone; rlen -= rone, ranges += rone) {
David S. Millera83f9822006-07-12 23:19:31 -0700507 if (!bus->map(addr, ranges, na, ns, pna))
508 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700509 }
David S. Millera83f9822006-07-12 23:19:31 -0700510
511 return 1;
512}
513
514static int __init use_1to1_mapping(struct device_node *pp)
515{
516 char *model;
517
518 /* If this is on the PMU bus, don't try to translate it even
519 * if a ranges property exists.
520 */
521 if (!strcmp(pp->name, "pmu"))
David S. Millercf44bbc2006-06-29 14:34:50 -0700522 return 1;
523
David S. Millera83f9822006-07-12 23:19:31 -0700524 /* If we have a ranges property in the parent, use it. */
525 if (of_find_property(pp, "ranges", NULL) != NULL)
526 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700527
David S. Millera83f9822006-07-12 23:19:31 -0700528 /* If the parent is the dma node of an ISA bus, pass
529 * the translation up to the root.
530 */
531 if (!strcmp(pp->name, "dma"))
532 return 0;
533
534 /* Similarly for Simba PCI bridges. */
535 model = of_get_property(pp, "model", NULL);
536 if (model && !strcmp(model, "SUNW,simba"))
537 return 0;
538
539 return 1;
David S. Millercf44bbc2006-06-29 14:34:50 -0700540}
541
David S. Millera83f9822006-07-12 23:19:31 -0700542static int of_resource_verbose;
543
David S. Millercf44bbc2006-06-29 14:34:50 -0700544static void __init build_device_resources(struct of_device *op,
545 struct device *parent)
546{
547 struct of_device *p_op;
548 struct of_bus *bus;
549 int na, ns;
550 int index, num_reg;
551 void *preg;
552
553 if (!parent)
554 return;
555
556 p_op = to_of_device(parent);
557 bus = of_match_bus(p_op->node);
558 bus->count_cells(op->node, &na, &ns);
559
560 preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
561 if (!preg || num_reg == 0)
562 return;
563
564 /* Convert to num-cells. */
565 num_reg /= 4;
566
David S. Miller46ba6d72006-07-16 22:10:44 -0700567 /* Convert to num-entries. */
David S. Millercf44bbc2006-06-29 14:34:50 -0700568 num_reg /= na + ns;
569
David S. Miller46ba6d72006-07-16 22:10:44 -0700570 /* Prevent overruning the op->resources[] array. */
571 if (num_reg > PROMREG_MAX) {
572 printk(KERN_WARNING "%s: Too many regs (%d), "
573 "limiting to %d.\n",
574 op->node->full_name, num_reg, PROMREG_MAX);
575 num_reg = PROMREG_MAX;
576 }
577
David S. Millercf44bbc2006-06-29 14:34:50 -0700578 for (index = 0; index < num_reg; index++) {
579 struct resource *r = &op->resource[index];
580 u32 addr[OF_MAX_ADDR_CELLS];
581 u32 *reg = (preg + (index * ((na + ns) * 4)));
582 struct device_node *dp = op->node;
583 struct device_node *pp = p_op->node;
David S. Millerb85cdd42007-02-28 23:20:12 -0800584 struct of_bus *pbus, *dbus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700585 u64 size, result = OF_BAD_ADDR;
586 unsigned long flags;
587 int dna, dns;
588 int pna, pns;
589
590 size = of_read_addr(reg + na, ns);
591 flags = bus->get_flags(reg);
592
593 memcpy(addr, reg, na * 4);
594
David S. Millera83f9822006-07-12 23:19:31 -0700595 if (use_1to1_mapping(pp)) {
David S. Millercf44bbc2006-06-29 14:34:50 -0700596 result = of_read_addr(addr, na);
597 goto build_res;
598 }
599
600 dna = na;
601 dns = ns;
David S. Millerb85cdd42007-02-28 23:20:12 -0800602 dbus = bus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700603
604 while (1) {
605 dp = pp;
606 pp = dp->parent;
607 if (!pp) {
608 result = of_read_addr(addr, dna);
609 break;
610 }
611
612 pbus = of_match_bus(pp);
613 pbus->count_cells(dp, &pna, &pns);
614
David S. Millerb85cdd42007-02-28 23:20:12 -0800615 if (build_one_resource(dp, dbus, pbus, addr,
David S. Millera83f9822006-07-12 23:19:31 -0700616 dna, dns, pna))
David S. Millercf44bbc2006-06-29 14:34:50 -0700617 break;
618
619 dna = pna;
620 dns = pns;
David S. Millerb85cdd42007-02-28 23:20:12 -0800621 dbus = pbus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700622 }
623
624 build_res:
625 memset(r, 0, sizeof(*r));
David S. Millera83f9822006-07-12 23:19:31 -0700626
627 if (of_resource_verbose)
628 printk("%s reg[%d] -> %lx\n",
629 op->node->full_name, index,
630 result);
631
David S. Millercf44bbc2006-06-29 14:34:50 -0700632 if (result != OF_BAD_ADDR) {
David S. Miller1815aed2006-06-29 19:58:28 -0700633 if (tlb_type == hypervisor)
634 result &= 0x0fffffffffffffffUL;
635
David S. Millercf44bbc2006-06-29 14:34:50 -0700636 r->start = result;
637 r->end = result + size - 1;
638 r->flags = flags;
639 } else {
640 r->start = ~0UL;
641 r->end = ~0UL;
642 }
643 r->name = op->node->name;
644 }
645}
646
David S. Miller2b1e5972006-06-29 15:07:37 -0700647static struct device_node * __init
648apply_interrupt_map(struct device_node *dp, struct device_node *pp,
649 u32 *imap, int imlen, u32 *imask,
650 unsigned int *irq_p)
651{
652 struct device_node *cp;
653 unsigned int irq = *irq_p;
654 struct of_bus *bus;
655 phandle handle;
656 u32 *reg;
657 int na, num_reg, i;
658
659 bus = of_match_bus(pp);
660 bus->count_cells(dp, &na, NULL);
661
662 reg = of_get_property(dp, "reg", &num_reg);
663 if (!reg || !num_reg)
664 return NULL;
665
666 imlen /= ((na + 3) * 4);
667 handle = 0;
668 for (i = 0; i < imlen; i++) {
669 int j;
670
671 for (j = 0; j < na; j++) {
672 if ((reg[j] & imask[j]) != imap[j])
673 goto next;
674 }
675 if (imap[na] == irq) {
676 handle = imap[na + 1];
677 irq = imap[na + 2];
678 break;
679 }
680
681 next:
682 imap += (na + 3);
683 }
David S. Miller46ba6d72006-07-16 22:10:44 -0700684 if (i == imlen) {
685 /* Psycho and Sabre PCI controllers can have 'interrupt-map'
686 * properties that do not include the on-board device
687 * interrupts. Instead, the device's 'interrupts' property
688 * is already a fully specified INO value.
689 *
690 * Handle this by deciding that, if we didn't get a
691 * match in the parent's 'interrupt-map', and the
692 * parent is an IRQ translater, then use the parent as
693 * our IRQ controller.
694 */
695 if (pp->irq_trans)
696 return pp;
697
David S. Miller2b1e5972006-06-29 15:07:37 -0700698 return NULL;
David S. Miller46ba6d72006-07-16 22:10:44 -0700699 }
David S. Miller2b1e5972006-06-29 15:07:37 -0700700
701 *irq_p = irq;
702 cp = of_find_node_by_phandle(handle);
703
704 return cp;
705}
706
707static unsigned int __init pci_irq_swizzle(struct device_node *dp,
708 struct device_node *pp,
709 unsigned int irq)
710{
711 struct linux_prom_pci_registers *regs;
David S. Millerbb4c18c2007-02-26 14:55:06 -0800712 unsigned int bus, devfn, slot, ret;
David S. Miller2b1e5972006-06-29 15:07:37 -0700713
714 if (irq < 1 || irq > 4)
715 return irq;
716
717 regs = of_get_property(dp, "reg", NULL);
718 if (!regs)
719 return irq;
720
David S. Millerbb4c18c2007-02-26 14:55:06 -0800721 bus = (regs->phys_hi >> 16) & 0xff;
David S. Miller2b1e5972006-06-29 15:07:37 -0700722 devfn = (regs->phys_hi >> 8) & 0xff;
723 slot = (devfn >> 3) & 0x1f;
724
David S. Millerbb4c18c2007-02-26 14:55:06 -0800725 if (pp->irq_trans) {
726 /* Derived from Table 8-3, U2P User's Manual. This branch
727 * is handling a PCI controller that lacks a proper set of
728 * interrupt-map and interrupt-map-mask properties. The
729 * Ultra-E450 is one example.
730 *
731 * The bit layout is BSSLL, where:
732 * B: 0 on bus A, 1 on bus B
733 * D: 2-bit slot number, derived from PCI device number as
734 * (dev - 1) for bus A, or (dev - 2) for bus B
735 * L: 2-bit line number
736 *
737 * Actually, more "portable" way to calculate the funky
738 * slot number is to subtract pbm->pci_first_slot from the
739 * device number, and that's exactly what the pre-OF
740 * sparc64 code did, but we're building this stuff generically
741 * using the OBP tree, not in the PCI controller layer.
742 */
743 if (bus & 0x80) {
744 /* PBM-A */
745 bus = 0x00;
746 slot = (slot - 1) << 2;
747 } else {
748 /* PBM-B */
749 bus = 0x10;
750 slot = (slot - 2) << 2;
751 }
752 irq -= 1;
753
754 ret = (bus | slot | irq);
755 } else {
756 /* Going through a PCI-PCI bridge that lacks a set of
757 * interrupt-map and interrupt-map-mask properties.
758 */
759 ret = ((irq - 1 + (slot & 3)) & 3) + 1;
760 }
David S. Miller2b1e5972006-06-29 15:07:37 -0700761
762 return ret;
763}
764
David S. Millera83f9822006-07-12 23:19:31 -0700765static int of_irq_verbose;
766
David S. Miller2b1e5972006-06-29 15:07:37 -0700767static unsigned int __init build_one_device_irq(struct of_device *op,
768 struct device *parent,
769 unsigned int irq)
770{
771 struct device_node *dp = op->node;
772 struct device_node *pp, *ip;
773 unsigned int orig_irq = irq;
774
775 if (irq == 0xffffffff)
776 return irq;
777
778 if (dp->irq_trans) {
779 irq = dp->irq_trans->irq_build(dp, irq,
780 dp->irq_trans->data);
David S. Millera83f9822006-07-12 23:19:31 -0700781
782 if (of_irq_verbose)
783 printk("%s: direct translate %x --> %x\n",
784 dp->full_name, orig_irq, irq);
785
David S. Miller2b1e5972006-06-29 15:07:37 -0700786 return irq;
787 }
788
789 /* Something more complicated. Walk up to the root, applying
790 * interrupt-map or bus specific translations, until we hit
791 * an IRQ translator.
792 *
793 * If we hit a bus type or situation we cannot handle, we
794 * stop and assume that the original IRQ number was in a
795 * format which has special meaning to it's immediate parent.
796 */
797 pp = dp->parent;
798 ip = NULL;
799 while (pp) {
800 void *imap, *imsk;
801 int imlen;
802
803 imap = of_get_property(pp, "interrupt-map", &imlen);
804 imsk = of_get_property(pp, "interrupt-map-mask", NULL);
805 if (imap && imsk) {
806 struct device_node *iret;
807 int this_orig_irq = irq;
808
809 iret = apply_interrupt_map(dp, pp,
810 imap, imlen, imsk,
811 &irq);
David S. Millera83f9822006-07-12 23:19:31 -0700812
813 if (of_irq_verbose)
814 printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
815 op->node->full_name,
816 pp->full_name, this_orig_irq,
817 (iret ? iret->full_name : "NULL"), irq);
818
David S. Miller2b1e5972006-06-29 15:07:37 -0700819 if (!iret)
820 break;
821
822 if (iret->irq_trans) {
823 ip = iret;
824 break;
825 }
826 } else {
827 if (!strcmp(pp->type, "pci") ||
828 !strcmp(pp->type, "pciex")) {
829 unsigned int this_orig_irq = irq;
830
831 irq = pci_irq_swizzle(dp, pp, irq);
David S. Millera83f9822006-07-12 23:19:31 -0700832 if (of_irq_verbose)
833 printk("%s: PCI swizzle [%s] "
834 "%x --> %x\n",
835 op->node->full_name,
836 pp->full_name, this_orig_irq,
837 irq);
838
David S. Miller2b1e5972006-06-29 15:07:37 -0700839 }
840
841 if (pp->irq_trans) {
842 ip = pp;
843 break;
844 }
845 }
846 dp = pp;
847 pp = pp->parent;
848 }
849 if (!ip)
850 return orig_irq;
851
852 irq = ip->irq_trans->irq_build(op->node, irq,
853 ip->irq_trans->data);
David S. Millera83f9822006-07-12 23:19:31 -0700854 if (of_irq_verbose)
855 printk("%s: Apply IRQ trans [%s] %x --> %x\n",
856 op->node->full_name, ip->full_name, orig_irq, irq);
David S. Miller2b1e5972006-06-29 15:07:37 -0700857
858 return irq;
859}
860
David S. Millercf44bbc2006-06-29 14:34:50 -0700861static struct of_device * __init scan_one_device(struct device_node *dp,
862 struct device *parent)
863{
864 struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
865 unsigned int *irq;
David S. Miller2b1e5972006-06-29 15:07:37 -0700866 int len, i;
David S. Millercf44bbc2006-06-29 14:34:50 -0700867
868 if (!op)
869 return NULL;
870
871 op->node = dp;
872
873 op->clock_freq = of_getintprop_default(dp, "clock-frequency",
874 (25*1000*1000));
875 op->portid = of_getintprop_default(dp, "upa-portid", -1);
876 if (op->portid == -1)
877 op->portid = of_getintprop_default(dp, "portid", -1);
878
879 irq = of_get_property(dp, "interrupts", &len);
David S. Miller2b1e5972006-06-29 15:07:37 -0700880 if (irq) {
881 memcpy(op->irqs, irq, len);
882 op->num_irqs = len / 4;
883 } else {
884 op->num_irqs = 0;
885 }
David S. Millercf44bbc2006-06-29 14:34:50 -0700886
David S. Miller46ba6d72006-07-16 22:10:44 -0700887 /* Prevent overruning the op->irqs[] array. */
888 if (op->num_irqs > PROMINTR_MAX) {
889 printk(KERN_WARNING "%s: Too many irqs (%d), "
890 "limiting to %d.\n",
891 dp->full_name, op->num_irqs, PROMINTR_MAX);
892 op->num_irqs = PROMINTR_MAX;
893 }
894
David S. Millercf44bbc2006-06-29 14:34:50 -0700895 build_device_resources(op, parent);
David S. Miller2b1e5972006-06-29 15:07:37 -0700896 for (i = 0; i < op->num_irqs; i++)
897 op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]);
David S. Millercf44bbc2006-06-29 14:34:50 -0700898
899 op->dev.parent = parent;
900 op->dev.bus = &of_bus_type;
901 if (!parent)
902 strcpy(op->dev.bus_id, "root");
903 else
David S. Millerf5ef9d12006-10-27 01:03:31 -0700904 sprintf(op->dev.bus_id, "%08x", dp->node);
David S. Millercf44bbc2006-06-29 14:34:50 -0700905
906 if (of_device_register(op)) {
907 printk("%s: Could not register of device.\n",
908 dp->full_name);
909 kfree(op);
910 op = NULL;
911 }
912
913 return op;
914}
915
916static void __init scan_tree(struct device_node *dp, struct device *parent)
917{
918 while (dp) {
919 struct of_device *op = scan_one_device(dp, parent);
920
921 if (op)
922 scan_tree(dp->child, &op->dev);
923
924 dp = dp->sibling;
925 }
926}
927
928static void __init scan_of_devices(void)
929{
930 struct device_node *root = of_find_node_by_path("/");
931 struct of_device *parent;
932
933 parent = scan_one_device(root, NULL);
934 if (!parent)
935 return;
936
937 scan_tree(root->child, &parent->dev);
938}
939
David S. Millera2bd4fd2006-06-23 01:44:10 -0700940static int __init of_bus_driver_init(void)
941{
David S. Millercf44bbc2006-06-29 14:34:50 -0700942 int err;
David S. Millera2bd4fd2006-06-23 01:44:10 -0700943
David S. Millercf44bbc2006-06-29 14:34:50 -0700944 err = bus_register(&of_bus_type);
David S. Millera2bd4fd2006-06-23 01:44:10 -0700945#ifdef CONFIG_PCI
946 if (!err)
947 err = bus_register(&isa_bus_type);
948 if (!err)
949 err = bus_register(&ebus_bus_type);
950#endif
951#ifdef CONFIG_SBUS
952 if (!err)
953 err = bus_register(&sbus_bus_type);
954#endif
David S. Millercf44bbc2006-06-29 14:34:50 -0700955
956 if (!err)
957 scan_of_devices();
958
959 return err;
David S. Millera2bd4fd2006-06-23 01:44:10 -0700960}
961
962postcore_initcall(of_bus_driver_init);
963
David S. Millera83f9822006-07-12 23:19:31 -0700964static int __init of_debug(char *str)
965{
966 int val = 0;
967
968 get_option(&str, &val);
969 if (val & 1)
970 of_resource_verbose = 1;
971 if (val & 2)
972 of_irq_verbose = 1;
973 return 1;
974}
975
976__setup("of_debug=", of_debug);
977
David S. Millera2bd4fd2006-06-23 01:44:10 -0700978int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
979{
980 /* initialize common driver fields */
981 drv->driver.name = drv->name;
982 drv->driver.bus = bus;
983
984 /* register with core */
985 return driver_register(&drv->driver);
986}
987
988void of_unregister_driver(struct of_platform_driver *drv)
989{
990 driver_unregister(&drv->driver);
991}
992
993
994static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
995{
996 struct of_device *ofdev;
997
998 ofdev = to_of_device(dev);
999 return sprintf(buf, "%s", ofdev->node->full_name);
1000}
1001
1002static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
1003
1004/**
1005 * of_release_dev - free an of device structure when all users of it are finished.
1006 * @dev: device that's been disconnected
1007 *
1008 * Will be called only by the device core when all users of this of device are
1009 * done.
1010 */
1011void of_release_dev(struct device *dev)
1012{
1013 struct of_device *ofdev;
1014
1015 ofdev = to_of_device(dev);
1016
1017 kfree(ofdev);
1018}
1019
1020int of_device_register(struct of_device *ofdev)
1021{
1022 int rc;
1023
1024 BUG_ON(ofdev->node == NULL);
1025
1026 rc = device_register(&ofdev->dev);
1027 if (rc)
1028 return rc;
1029
Andrew Morton6cc8b6f2006-07-10 15:28:54 -07001030 rc = device_create_file(&ofdev->dev, &dev_attr_devspec);
1031 if (rc)
1032 device_unregister(&ofdev->dev);
David S. Millera2bd4fd2006-06-23 01:44:10 -07001033
Andrew Morton6cc8b6f2006-07-10 15:28:54 -07001034 return rc;
David S. Millera2bd4fd2006-06-23 01:44:10 -07001035}
1036
1037void of_device_unregister(struct of_device *ofdev)
1038{
1039 device_remove_file(&ofdev->dev, &dev_attr_devspec);
1040 device_unregister(&ofdev->dev);
1041}
1042
1043struct of_device* of_platform_device_create(struct device_node *np,
1044 const char *bus_id,
1045 struct device *parent,
1046 struct bus_type *bus)
1047{
1048 struct of_device *dev;
1049
Yan Burman982c2062006-11-30 17:13:09 -08001050 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
David S. Millera2bd4fd2006-06-23 01:44:10 -07001051 if (!dev)
1052 return NULL;
David S. Millera2bd4fd2006-06-23 01:44:10 -07001053
1054 dev->dev.parent = parent;
1055 dev->dev.bus = bus;
1056 dev->dev.release = of_release_dev;
1057
1058 strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
1059
1060 if (of_device_register(dev) != 0) {
1061 kfree(dev);
1062 return NULL;
1063 }
1064
1065 return dev;
1066}
1067
1068EXPORT_SYMBOL(of_match_device);
1069EXPORT_SYMBOL(of_register_driver);
1070EXPORT_SYMBOL(of_unregister_driver);
1071EXPORT_SYMBOL(of_device_register);
1072EXPORT_SYMBOL(of_device_unregister);
1073EXPORT_SYMBOL(of_dev_get);
1074EXPORT_SYMBOL(of_dev_put);
1075EXPORT_SYMBOL(of_platform_device_create);
1076EXPORT_SYMBOL(of_release_dev);