sound: seq_midi_event: fix decoding of (N)RPN events
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / sparc / kernel / of_device_64.c
blob9013c4ba25e8142a56abf71407fb269741b6ac3b
1 #include <linux/string.h>
2 #include <linux/kernel.h>
3 #include <linux/of.h>
4 #include <linux/init.h>
5 #include <linux/module.h>
6 #include <linux/mod_devicetable.h>
7 #include <linux/slab.h>
8 #include <linux/errno.h>
9 #include <linux/irq.h>
10 #include <linux/of_device.h>
11 #include <linux/of_platform.h>
13 void __iomem *of_ioremap(struct resource *res, unsigned long offset, unsigned long size, char *name)
15 unsigned long ret = res->start + offset;
16 struct resource *r;
18 if (res->flags & IORESOURCE_MEM)
19 r = request_mem_region(ret, size, name);
20 else
21 r = request_region(ret, size, name);
22 if (!r)
23 ret = 0;
25 return (void __iomem *) ret;
27 EXPORT_SYMBOL(of_ioremap);
29 void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
31 if (res->flags & IORESOURCE_MEM)
32 release_mem_region((unsigned long) base, size);
33 else
34 release_region((unsigned long) base, size);
36 EXPORT_SYMBOL(of_iounmap);
38 static int node_match(struct device *dev, void *data)
40 struct of_device *op = to_of_device(dev);
41 struct device_node *dp = data;
43 return (op->node == dp);
46 struct of_device *of_find_device_by_node(struct device_node *dp)
48 struct device *dev = bus_find_device(&of_platform_bus_type, NULL,
49 dp, node_match);
51 if (dev)
52 return to_of_device(dev);
54 return NULL;
56 EXPORT_SYMBOL(of_find_device_by_node);
58 unsigned int irq_of_parse_and_map(struct device_node *node, int index)
60 struct of_device *op = of_find_device_by_node(node);
62 if (!op || index >= op->num_irqs)
63 return 0;
65 return op->irqs[index];
67 EXPORT_SYMBOL(irq_of_parse_and_map);
69 /* Take the archdata values for IOMMU, STC, and HOSTDATA found in
70 * BUS and propagate to all child of_device objects.
72 void of_propagate_archdata(struct of_device *bus)
74 struct dev_archdata *bus_sd = &bus->dev.archdata;
75 struct device_node *bus_dp = bus->node;
76 struct device_node *dp;
78 for (dp = bus_dp->child; dp; dp = dp->sibling) {
79 struct of_device *op = of_find_device_by_node(dp);
81 op->dev.archdata.iommu = bus_sd->iommu;
82 op->dev.archdata.stc = bus_sd->stc;
83 op->dev.archdata.host_controller = bus_sd->host_controller;
84 op->dev.archdata.numa_node = bus_sd->numa_node;
86 if (dp->child)
87 of_propagate_archdata(op);
91 struct bus_type of_platform_bus_type;
92 EXPORT_SYMBOL(of_platform_bus_type);
94 static inline u64 of_read_addr(const u32 *cell, int size)
96 u64 r = 0;
97 while (size--)
98 r = (r << 32) | *(cell++);
99 return r;
102 static void __init get_cells(struct device_node *dp,
103 int *addrc, int *sizec)
105 if (addrc)
106 *addrc = of_n_addr_cells(dp);
107 if (sizec)
108 *sizec = of_n_size_cells(dp);
111 /* Max address size we deal with */
112 #define OF_MAX_ADDR_CELLS 4
114 struct of_bus {
115 const char *name;
116 const char *addr_prop_name;
117 int (*match)(struct device_node *parent);
118 void (*count_cells)(struct device_node *child,
119 int *addrc, int *sizec);
120 int (*map)(u32 *addr, const u32 *range,
121 int na, int ns, int pna);
122 unsigned long (*get_flags)(const u32 *addr, unsigned long);
126 * Default translator (generic bus)
129 static void of_bus_default_count_cells(struct device_node *dev,
130 int *addrc, int *sizec)
132 get_cells(dev, addrc, sizec);
135 /* Make sure the least significant 64-bits are in-range. Even
136 * for 3 or 4 cell values it is a good enough approximation.
138 static int of_out_of_range(const u32 *addr, const u32 *base,
139 const u32 *size, int na, int ns)
141 u64 a = of_read_addr(addr, na);
142 u64 b = of_read_addr(base, na);
144 if (a < b)
145 return 1;
147 b += of_read_addr(size, ns);
148 if (a >= b)
149 return 1;
151 return 0;
154 static int of_bus_default_map(u32 *addr, const u32 *range,
155 int na, int ns, int pna)
157 u32 result[OF_MAX_ADDR_CELLS];
158 int i;
160 if (ns > 2) {
161 printk("of_device: Cannot handle size cells (%d) > 2.", ns);
162 return -EINVAL;
165 if (of_out_of_range(addr, range, range + na + pna, na, ns))
166 return -EINVAL;
168 /* Start with the parent range base. */
169 memcpy(result, range + na, pna * 4);
171 /* Add in the child address offset. */
172 for (i = 0; i < na; i++)
173 result[pna - 1 - i] +=
174 (addr[na - 1 - i] -
175 range[na - 1 - i]);
177 memcpy(addr, result, pna * 4);
179 return 0;
182 static unsigned long of_bus_default_get_flags(const u32 *addr, unsigned long flags)
184 if (flags)
185 return flags;
186 return IORESOURCE_MEM;
190 * PCI bus specific translator
193 static int of_bus_pci_match(struct device_node *np)
195 if (!strcmp(np->name, "pci")) {
196 const char *model = of_get_property(np, "model", NULL);
198 if (model && !strcmp(model, "SUNW,simba"))
199 return 0;
201 /* Do not do PCI specific frobbing if the
202 * PCI bridge lacks a ranges property. We
203 * want to pass it through up to the next
204 * parent as-is, not with the PCI translate
205 * method which chops off the top address cell.
207 if (!of_find_property(np, "ranges", NULL))
208 return 0;
210 return 1;
213 return 0;
216 static int of_bus_simba_match(struct device_node *np)
218 const char *model = of_get_property(np, "model", NULL);
220 if (model && !strcmp(model, "SUNW,simba"))
221 return 1;
223 /* Treat PCI busses lacking ranges property just like
224 * simba.
226 if (!strcmp(np->name, "pci")) {
227 if (!of_find_property(np, "ranges", NULL))
228 return 1;
231 return 0;
234 static int of_bus_simba_map(u32 *addr, const u32 *range,
235 int na, int ns, int pna)
237 return 0;
240 static void of_bus_pci_count_cells(struct device_node *np,
241 int *addrc, int *sizec)
243 if (addrc)
244 *addrc = 3;
245 if (sizec)
246 *sizec = 2;
249 static int of_bus_pci_map(u32 *addr, const u32 *range,
250 int na, int ns, int pna)
252 u32 result[OF_MAX_ADDR_CELLS];
253 int i;
255 /* Check address type match */
256 if ((addr[0] ^ range[0]) & 0x03000000)
257 return -EINVAL;
259 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
260 na - 1, ns))
261 return -EINVAL;
263 /* Start with the parent range base. */
264 memcpy(result, range + na, pna * 4);
266 /* Add in the child address offset, skipping high cell. */
267 for (i = 0; i < na - 1; i++)
268 result[pna - 1 - i] +=
269 (addr[na - 1 - i] -
270 range[na - 1 - i]);
272 memcpy(addr, result, pna * 4);
274 return 0;
277 static unsigned long of_bus_pci_get_flags(const u32 *addr, unsigned long flags)
279 u32 w = addr[0];
281 /* For PCI, we override whatever child busses may have used. */
282 flags = 0;
283 switch((w >> 24) & 0x03) {
284 case 0x01:
285 flags |= IORESOURCE_IO;
286 break;
288 case 0x02: /* 32 bits */
289 case 0x03: /* 64 bits */
290 flags |= IORESOURCE_MEM;
291 break;
293 if (w & 0x40000000)
294 flags |= IORESOURCE_PREFETCH;
295 return flags;
299 * SBUS bus specific translator
302 static int of_bus_sbus_match(struct device_node *np)
304 struct device_node *dp = np;
306 while (dp) {
307 if (!strcmp(dp->name, "sbus") ||
308 !strcmp(dp->name, "sbi"))
309 return 1;
311 /* Have a look at use_1to1_mapping(). We're trying
312 * to match SBUS if that's the top-level bus and we
313 * don't have some intervening real bus that provides
314 * ranges based translations.
316 if (of_find_property(dp, "ranges", NULL) != NULL)
317 break;
319 dp = dp->parent;
322 return 0;
325 static void of_bus_sbus_count_cells(struct device_node *child,
326 int *addrc, int *sizec)
328 if (addrc)
329 *addrc = 2;
330 if (sizec)
331 *sizec = 1;
335 * FHC/Central bus specific translator.
337 * This is just needed to hard-code the address and size cell
338 * counts. 'fhc' and 'central' nodes lack the #address-cells and
339 * #size-cells properties, and if you walk to the root on such
340 * Enterprise boxes all you'll get is a #size-cells of 2 which is
341 * not what we want to use.
343 static int of_bus_fhc_match(struct device_node *np)
345 return !strcmp(np->name, "fhc") ||
346 !strcmp(np->name, "central");
349 #define of_bus_fhc_count_cells of_bus_sbus_count_cells
352 * Array of bus specific translators
355 static struct of_bus of_busses[] = {
356 /* PCI */
358 .name = "pci",
359 .addr_prop_name = "assigned-addresses",
360 .match = of_bus_pci_match,
361 .count_cells = of_bus_pci_count_cells,
362 .map = of_bus_pci_map,
363 .get_flags = of_bus_pci_get_flags,
365 /* SIMBA */
367 .name = "simba",
368 .addr_prop_name = "assigned-addresses",
369 .match = of_bus_simba_match,
370 .count_cells = of_bus_pci_count_cells,
371 .map = of_bus_simba_map,
372 .get_flags = of_bus_pci_get_flags,
374 /* SBUS */
376 .name = "sbus",
377 .addr_prop_name = "reg",
378 .match = of_bus_sbus_match,
379 .count_cells = of_bus_sbus_count_cells,
380 .map = of_bus_default_map,
381 .get_flags = of_bus_default_get_flags,
383 /* FHC */
385 .name = "fhc",
386 .addr_prop_name = "reg",
387 .match = of_bus_fhc_match,
388 .count_cells = of_bus_fhc_count_cells,
389 .map = of_bus_default_map,
390 .get_flags = of_bus_default_get_flags,
392 /* Default */
394 .name = "default",
395 .addr_prop_name = "reg",
396 .match = NULL,
397 .count_cells = of_bus_default_count_cells,
398 .map = of_bus_default_map,
399 .get_flags = of_bus_default_get_flags,
403 static struct of_bus *of_match_bus(struct device_node *np)
405 int i;
407 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
408 if (!of_busses[i].match || of_busses[i].match(np))
409 return &of_busses[i];
410 BUG();
411 return NULL;
414 static int __init build_one_resource(struct device_node *parent,
415 struct of_bus *bus,
416 struct of_bus *pbus,
417 u32 *addr,
418 int na, int ns, int pna)
420 const u32 *ranges;
421 int rone, rlen;
423 ranges = of_get_property(parent, "ranges", &rlen);
424 if (ranges == NULL || rlen == 0) {
425 u32 result[OF_MAX_ADDR_CELLS];
426 int i;
428 memset(result, 0, pna * 4);
429 for (i = 0; i < na; i++)
430 result[pna - 1 - i] =
431 addr[na - 1 - i];
433 memcpy(addr, result, pna * 4);
434 return 0;
437 /* Now walk through the ranges */
438 rlen /= 4;
439 rone = na + pna + ns;
440 for (; rlen >= rone; rlen -= rone, ranges += rone) {
441 if (!bus->map(addr, ranges, na, ns, pna))
442 return 0;
445 /* When we miss an I/O space match on PCI, just pass it up
446 * to the next PCI bridge and/or controller.
448 if (!strcmp(bus->name, "pci") &&
449 (addr[0] & 0x03000000) == 0x01000000)
450 return 0;
452 return 1;
455 static int __init use_1to1_mapping(struct device_node *pp)
457 /* If we have a ranges property in the parent, use it. */
458 if (of_find_property(pp, "ranges", NULL) != NULL)
459 return 0;
461 /* If the parent is the dma node of an ISA bus, pass
462 * the translation up to the root.
464 * Some SBUS devices use intermediate nodes to express
465 * hierarchy within the device itself. These aren't
466 * real bus nodes, and don't have a 'ranges' property.
467 * But, we should still pass the translation work up
468 * to the SBUS itself.
470 if (!strcmp(pp->name, "dma") ||
471 !strcmp(pp->name, "espdma") ||
472 !strcmp(pp->name, "ledma") ||
473 !strcmp(pp->name, "lebuffer"))
474 return 0;
476 /* Similarly for all PCI bridges, if we get this far
477 * it lacks a ranges property, and this will include
478 * cases like Simba.
480 if (!strcmp(pp->name, "pci"))
481 return 0;
483 return 1;
486 static int of_resource_verbose;
488 static void __init build_device_resources(struct of_device *op,
489 struct device *parent)
491 struct of_device *p_op;
492 struct of_bus *bus;
493 int na, ns;
494 int index, num_reg;
495 const void *preg;
497 if (!parent)
498 return;
500 p_op = to_of_device(parent);
501 bus = of_match_bus(p_op->node);
502 bus->count_cells(op->node, &na, &ns);
504 preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
505 if (!preg || num_reg == 0)
506 return;
508 /* Convert to num-cells. */
509 num_reg /= 4;
511 /* Convert to num-entries. */
512 num_reg /= na + ns;
514 /* Prevent overrunning the op->resources[] array. */
515 if (num_reg > PROMREG_MAX) {
516 printk(KERN_WARNING "%s: Too many regs (%d), "
517 "limiting to %d.\n",
518 op->node->full_name, num_reg, PROMREG_MAX);
519 num_reg = PROMREG_MAX;
522 for (index = 0; index < num_reg; index++) {
523 struct resource *r = &op->resource[index];
524 u32 addr[OF_MAX_ADDR_CELLS];
525 const u32 *reg = (preg + (index * ((na + ns) * 4)));
526 struct device_node *dp = op->node;
527 struct device_node *pp = p_op->node;
528 struct of_bus *pbus, *dbus;
529 u64 size, result = OF_BAD_ADDR;
530 unsigned long flags;
531 int dna, dns;
532 int pna, pns;
534 size = of_read_addr(reg + na, ns);
535 memcpy(addr, reg, na * 4);
537 flags = bus->get_flags(addr, 0);
539 if (use_1to1_mapping(pp)) {
540 result = of_read_addr(addr, na);
541 goto build_res;
544 dna = na;
545 dns = ns;
546 dbus = bus;
548 while (1) {
549 dp = pp;
550 pp = dp->parent;
551 if (!pp) {
552 result = of_read_addr(addr, dna);
553 break;
556 pbus = of_match_bus(pp);
557 pbus->count_cells(dp, &pna, &pns);
559 if (build_one_resource(dp, dbus, pbus, addr,
560 dna, dns, pna))
561 break;
563 flags = pbus->get_flags(addr, flags);
565 dna = pna;
566 dns = pns;
567 dbus = pbus;
570 build_res:
571 memset(r, 0, sizeof(*r));
573 if (of_resource_verbose)
574 printk("%s reg[%d] -> %llx\n",
575 op->node->full_name, index,
576 result);
578 if (result != OF_BAD_ADDR) {
579 if (tlb_type == hypervisor)
580 result &= 0x0fffffffffffffffUL;
582 r->start = result;
583 r->end = result + size - 1;
584 r->flags = flags;
586 r->name = op->node->name;
590 static struct device_node * __init
591 apply_interrupt_map(struct device_node *dp, struct device_node *pp,
592 const u32 *imap, int imlen, const u32 *imask,
593 unsigned int *irq_p)
595 struct device_node *cp;
596 unsigned int irq = *irq_p;
597 struct of_bus *bus;
598 phandle handle;
599 const u32 *reg;
600 int na, num_reg, i;
602 bus = of_match_bus(pp);
603 bus->count_cells(dp, &na, NULL);
605 reg = of_get_property(dp, "reg", &num_reg);
606 if (!reg || !num_reg)
607 return NULL;
609 imlen /= ((na + 3) * 4);
610 handle = 0;
611 for (i = 0; i < imlen; i++) {
612 int j;
614 for (j = 0; j < na; j++) {
615 if ((reg[j] & imask[j]) != imap[j])
616 goto next;
618 if (imap[na] == irq) {
619 handle = imap[na + 1];
620 irq = imap[na + 2];
621 break;
624 next:
625 imap += (na + 3);
627 if (i == imlen) {
628 /* Psycho and Sabre PCI controllers can have 'interrupt-map'
629 * properties that do not include the on-board device
630 * interrupts. Instead, the device's 'interrupts' property
631 * is already a fully specified INO value.
633 * Handle this by deciding that, if we didn't get a
634 * match in the parent's 'interrupt-map', and the
635 * parent is an IRQ translater, then use the parent as
636 * our IRQ controller.
638 if (pp->irq_trans)
639 return pp;
641 return NULL;
644 *irq_p = irq;
645 cp = of_find_node_by_phandle(handle);
647 return cp;
650 static unsigned int __init pci_irq_swizzle(struct device_node *dp,
651 struct device_node *pp,
652 unsigned int irq)
654 const struct linux_prom_pci_registers *regs;
655 unsigned int bus, devfn, slot, ret;
657 if (irq < 1 || irq > 4)
658 return irq;
660 regs = of_get_property(dp, "reg", NULL);
661 if (!regs)
662 return irq;
664 bus = (regs->phys_hi >> 16) & 0xff;
665 devfn = (regs->phys_hi >> 8) & 0xff;
666 slot = (devfn >> 3) & 0x1f;
668 if (pp->irq_trans) {
669 /* Derived from Table 8-3, U2P User's Manual. This branch
670 * is handling a PCI controller that lacks a proper set of
671 * interrupt-map and interrupt-map-mask properties. The
672 * Ultra-E450 is one example.
674 * The bit layout is BSSLL, where:
675 * B: 0 on bus A, 1 on bus B
676 * D: 2-bit slot number, derived from PCI device number as
677 * (dev - 1) for bus A, or (dev - 2) for bus B
678 * L: 2-bit line number
680 if (bus & 0x80) {
681 /* PBM-A */
682 bus = 0x00;
683 slot = (slot - 1) << 2;
684 } else {
685 /* PBM-B */
686 bus = 0x10;
687 slot = (slot - 2) << 2;
689 irq -= 1;
691 ret = (bus | slot | irq);
692 } else {
693 /* Going through a PCI-PCI bridge that lacks a set of
694 * interrupt-map and interrupt-map-mask properties.
696 ret = ((irq - 1 + (slot & 3)) & 3) + 1;
699 return ret;
702 static int of_irq_verbose;
704 static unsigned int __init build_one_device_irq(struct of_device *op,
705 struct device *parent,
706 unsigned int irq)
708 struct device_node *dp = op->node;
709 struct device_node *pp, *ip;
710 unsigned int orig_irq = irq;
711 int nid;
713 if (irq == 0xffffffff)
714 return irq;
716 if (dp->irq_trans) {
717 irq = dp->irq_trans->irq_build(dp, irq,
718 dp->irq_trans->data);
720 if (of_irq_verbose)
721 printk("%s: direct translate %x --> %x\n",
722 dp->full_name, orig_irq, irq);
724 goto out;
727 /* Something more complicated. Walk up to the root, applying
728 * interrupt-map or bus specific translations, until we hit
729 * an IRQ translator.
731 * If we hit a bus type or situation we cannot handle, we
732 * stop and assume that the original IRQ number was in a
733 * format which has special meaning to it's immediate parent.
735 pp = dp->parent;
736 ip = NULL;
737 while (pp) {
738 const void *imap, *imsk;
739 int imlen;
741 imap = of_get_property(pp, "interrupt-map", &imlen);
742 imsk = of_get_property(pp, "interrupt-map-mask", NULL);
743 if (imap && imsk) {
744 struct device_node *iret;
745 int this_orig_irq = irq;
747 iret = apply_interrupt_map(dp, pp,
748 imap, imlen, imsk,
749 &irq);
751 if (of_irq_verbose)
752 printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
753 op->node->full_name,
754 pp->full_name, this_orig_irq,
755 (iret ? iret->full_name : "NULL"), irq);
757 if (!iret)
758 break;
760 if (iret->irq_trans) {
761 ip = iret;
762 break;
764 } else {
765 if (!strcmp(pp->name, "pci")) {
766 unsigned int this_orig_irq = irq;
768 irq = pci_irq_swizzle(dp, pp, irq);
769 if (of_irq_verbose)
770 printk("%s: PCI swizzle [%s] "
771 "%x --> %x\n",
772 op->node->full_name,
773 pp->full_name, this_orig_irq,
774 irq);
778 if (pp->irq_trans) {
779 ip = pp;
780 break;
783 dp = pp;
784 pp = pp->parent;
786 if (!ip)
787 return orig_irq;
789 irq = ip->irq_trans->irq_build(op->node, irq,
790 ip->irq_trans->data);
791 if (of_irq_verbose)
792 printk("%s: Apply IRQ trans [%s] %x --> %x\n",
793 op->node->full_name, ip->full_name, orig_irq, irq);
795 out:
796 nid = of_node_to_nid(dp);
797 if (nid != -1) {
798 cpumask_t numa_mask = *cpumask_of_node(nid);
800 irq_set_affinity(irq, &numa_mask);
803 return irq;
806 static struct of_device * __init scan_one_device(struct device_node *dp,
807 struct device *parent)
809 struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
810 const unsigned int *irq;
811 struct dev_archdata *sd;
812 int len, i;
814 if (!op)
815 return NULL;
817 sd = &op->dev.archdata;
818 sd->prom_node = dp;
819 sd->op = op;
821 op->node = dp;
823 op->clock_freq = of_getintprop_default(dp, "clock-frequency",
824 (25*1000*1000));
825 op->portid = of_getintprop_default(dp, "upa-portid", -1);
826 if (op->portid == -1)
827 op->portid = of_getintprop_default(dp, "portid", -1);
829 irq = of_get_property(dp, "interrupts", &len);
830 if (irq) {
831 op->num_irqs = len / 4;
833 /* Prevent overrunning the op->irqs[] array. */
834 if (op->num_irqs > PROMINTR_MAX) {
835 printk(KERN_WARNING "%s: Too many irqs (%d), "
836 "limiting to %d.\n",
837 dp->full_name, op->num_irqs, PROMINTR_MAX);
838 op->num_irqs = PROMINTR_MAX;
840 memcpy(op->irqs, irq, op->num_irqs * 4);
841 } else {
842 op->num_irqs = 0;
845 build_device_resources(op, parent);
846 for (i = 0; i < op->num_irqs; i++)
847 op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]);
849 op->dev.parent = parent;
850 op->dev.bus = &of_platform_bus_type;
851 if (!parent)
852 dev_set_name(&op->dev, "root");
853 else
854 dev_set_name(&op->dev, "%08x", dp->node);
856 if (of_device_register(op)) {
857 printk("%s: Could not register of device.\n",
858 dp->full_name);
859 kfree(op);
860 op = NULL;
863 return op;
866 static void __init scan_tree(struct device_node *dp, struct device *parent)
868 while (dp) {
869 struct of_device *op = scan_one_device(dp, parent);
871 if (op)
872 scan_tree(dp->child, &op->dev);
874 dp = dp->sibling;
878 static void __init scan_of_devices(void)
880 struct device_node *root = of_find_node_by_path("/");
881 struct of_device *parent;
883 parent = scan_one_device(root, NULL);
884 if (!parent)
885 return;
887 scan_tree(root->child, &parent->dev);
890 static int __init of_bus_driver_init(void)
892 int err;
894 err = of_bus_type_init(&of_platform_bus_type, "of");
895 if (!err)
896 scan_of_devices();
898 return err;
901 postcore_initcall(of_bus_driver_init);
903 static int __init of_debug(char *str)
905 int val = 0;
907 get_option(&str, &val);
908 if (val & 1)
909 of_resource_verbose = 1;
910 if (val & 2)
911 of_irq_verbose = 1;
912 return 1;
915 __setup("of_debug=", of_debug);