Merge branch 'linux-next' of git://git.kernel.org/pub/scm/linux/kernel/git/konrad...
[pandora-kernel.git] / drivers / of / irq.c
index 6a5b5e7..0f0cfa3 100644 (file)
  */
 
 #include <linux/errno.h>
+#include <linux/list.h>
 #include <linux/module.h>
 #include <linux/of.h>
 #include <linux/of_irq.h>
 #include <linux/string.h>
-
-/* For archs that don't support NO_IRQ (such as x86), provide a dummy value */
-#ifndef NO_IRQ
-#define NO_IRQ 0
-#endif
+#include <linux/slab.h>
 
 /**
  * irq_of_parse_and_map - Parse and map an interrupt into linux virq space
@@ -42,7 +39,7 @@ unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
        struct of_irq oirq;
 
        if (of_irq_map_one(dev, index, &oirq))
-               return NO_IRQ;
+               return 0;
 
        return irq_create_of_mapping(oirq.controller, oirq.specifier,
                                     oirq.size);
@@ -58,27 +55,27 @@ EXPORT_SYMBOL_GPL(irq_of_parse_and_map);
  */
 struct device_node *of_irq_find_parent(struct device_node *child)
 {
-       struct device_node *p, *c = child;
+       struct device_node *p;
        const __be32 *parp;
 
-       if (!of_node_get(c))
+       if (!of_node_get(child))
                return NULL;
 
        do {
-               parp = of_get_property(c, "interrupt-parent", NULL);
+               parp = of_get_property(child, "interrupt-parent", NULL);
                if (parp == NULL)
-                       p = of_get_parent(c);
+                       p = of_get_parent(child);
                else {
                        if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
                                p = of_node_get(of_irq_dflt_pic);
                        else
                                p = of_find_node_by_phandle(be32_to_cpup(parp));
                }
-               of_node_put(c);
-               c = p;
+               of_node_put(child);
+               child = p;
        } while (p && of_get_property(p, "#interrupt-cells", NULL) == NULL);
 
-       return (p == child) ? NULL : p;
+       return p;
 }
 
 /**
@@ -343,7 +340,7 @@ int of_irq_to_resource(struct device_node *dev, int index, struct resource *r)
 
        /* Only dereference the resource if both the
         * resource and the irq are valid. */
-       if (r && irq != NO_IRQ) {
+       if (r && irq) {
                r->start = r->end = irq;
                r->flags = IORESOURCE_IRQ;
                r->name = dev->full_name;
@@ -361,7 +358,7 @@ int of_irq_count(struct device_node *dev)
 {
        int nr = 0;
 
-       while (of_irq_to_resource(dev, nr, NULL) != NO_IRQ)
+       while (of_irq_to_resource(dev, nr, NULL))
                nr++;
 
        return nr;
@@ -381,8 +378,115 @@ int of_irq_to_resource_table(struct device_node *dev, struct resource *res,
        int i;
 
        for (i = 0; i < nr_irqs; i++, res++)
-               if (of_irq_to_resource(dev, i, res) == NO_IRQ)
+               if (!of_irq_to_resource(dev, i, res))
                        break;
 
        return i;
 }
+
+struct intc_desc {
+       struct list_head        list;
+       struct device_node      *dev;
+       struct device_node      *interrupt_parent;
+};
+
+/**
+ * of_irq_init - Scan and init matching interrupt controllers in DT
+ * @matches: 0 terminated array of nodes to match and init function to call
+ *
+ * This function scans the device tree for matching interrupt controller nodes,
+ * and calls their initialization functions in order with parents first.
+ */
+void __init of_irq_init(const struct of_device_id *matches)
+{
+       struct device_node *np, *parent = NULL;
+       struct intc_desc *desc, *temp_desc;
+       struct list_head intc_desc_list, intc_parent_list;
+
+       INIT_LIST_HEAD(&intc_desc_list);
+       INIT_LIST_HEAD(&intc_parent_list);
+
+       for_each_matching_node(np, matches) {
+               if (!of_find_property(np, "interrupt-controller", NULL))
+                       continue;
+               /*
+                * Here, we allocate and populate an intc_desc with the node
+                * pointer, interrupt-parent device_node etc.
+                */
+               desc = kzalloc(sizeof(*desc), GFP_KERNEL);
+               if (WARN_ON(!desc))
+                       goto err;
+
+               desc->dev = np;
+               desc->interrupt_parent = of_irq_find_parent(np);
+               if (desc->interrupt_parent == np)
+                       desc->interrupt_parent = NULL;
+               list_add_tail(&desc->list, &intc_desc_list);
+       }
+
+       /*
+        * The root irq controller is the one without an interrupt-parent.
+        * That one goes first, followed by the controllers that reference it,
+        * followed by the ones that reference the 2nd level controllers, etc.
+        */
+       while (!list_empty(&intc_desc_list)) {
+               /*
+                * Process all controllers with the current 'parent'.
+                * First pass will be looking for NULL as the parent.
+                * The assumption is that NULL parent means a root controller.
+                */
+               list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) {
+                       const struct of_device_id *match;
+                       int ret;
+                       of_irq_init_cb_t irq_init_cb;
+
+                       if (desc->interrupt_parent != parent)
+                               continue;
+
+                       list_del(&desc->list);
+                       match = of_match_node(matches, desc->dev);
+                       if (WARN(!match->data,
+                           "of_irq_init: no init function for %s\n",
+                           match->compatible)) {
+                               kfree(desc);
+                               continue;
+                       }
+
+                       pr_debug("of_irq_init: init %s @ %p, parent %p\n",
+                                match->compatible,
+                                desc->dev, desc->interrupt_parent);
+                       irq_init_cb = match->data;
+                       ret = irq_init_cb(desc->dev, desc->interrupt_parent);
+                       if (ret) {
+                               kfree(desc);
+                               continue;
+                       }
+
+                       /*
+                        * This one is now set up; add it to the parent list so
+                        * its children can get processed in a subsequent pass.
+                        */
+                       list_add_tail(&desc->list, &intc_parent_list);
+               }
+
+               /* Get the next pending parent that might have children */
+               desc = list_first_entry(&intc_parent_list, typeof(*desc), list);
+               if (list_empty(&intc_parent_list) || !desc) {
+                       pr_err("of_irq_init: children remain, but no parents\n");
+                       break;
+               }
+               list_del(&desc->list);
+               parent = desc->dev;
+               kfree(desc);
+       }
+
+       list_for_each_entry_safe(desc, temp_desc, &intc_parent_list, list) {
+               list_del(&desc->list);
+               kfree(desc);
+       }
+err:
+       list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) {
+               list_del(&desc->list);
+               kfree(desc);
+       }
+}