Merge branch 'e1000-fixes' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik...
[pandora-kernel.git] / arch / sparc / kernel / of_device.c
index bc956c5..fd7f8cb 100644 (file)
@@ -1,4 +1,3 @@
-#include <linux/config.h>
 #include <linux/string.h>
 #include <linux/kernel.h>
 #include <linux/init.h>
@@ -183,7 +182,7 @@ struct bus_type of_bus_type = {
 };
 EXPORT_SYMBOL(of_bus_type);
 
-static inline u64 of_read_addr(u32 *cell, int size)
+static inline u64 of_read_addr(const u32 *cell, int size)
 {
        u64 r = 0;
        while (size--)
@@ -209,9 +208,9 @@ struct of_bus {
        int             (*match)(struct device_node *parent);
        void            (*count_cells)(struct device_node *child,
                                       int *addrc, int *sizec);
-       u64             (*map)(u32 *addr, u32 *range, int na, int ns, int pna);
-       int             (*translate)(u32 *addr, u64 offset, int na);
-       unsigned int    (*get_flags)(u32 *addr);
+       int             (*map)(u32 *addr, const u32 *range,
+                              int na, int ns, int pna);
+       unsigned int    (*get_flags)(const u32 *addr);
 };
 
 /*
@@ -224,44 +223,78 @@ static void of_bus_default_count_cells(struct device_node *dev,
        get_cells(dev, addrc, sizec);
 }
 
-static u64 of_bus_default_map(u32 *addr, u32 *range, int na, int ns, int pna)
+/* Make sure the least significant 64-bits are in-range.  Even
+ * for 3 or 4 cell values it is a good enough approximation.
+ */
+static int of_out_of_range(const u32 *addr, const u32 *base,
+                          const u32 *size, int na, int ns)
 {
-       u64 cp, s, da;
+       u64 a = of_read_addr(addr, na);
+       u64 b = of_read_addr(base, na);
 
-       cp = of_read_addr(range, na);
-       s  = of_read_addr(range + na + pna, ns);
-       da = of_read_addr(addr, na);
+       if (a < b)
+               return 1;
 
-       if (da < cp || da >= (cp + s))
-               return OF_BAD_ADDR;
-       return da - cp;
+       b += of_read_addr(size, ns);
+       if (a >= b)
+               return 1;
+
+       return 0;
 }
 
-static int of_bus_default_translate(u32 *addr, u64 offset, int na)
+static int of_bus_default_map(u32 *addr, const u32 *range,
+                             int na, int ns, int pna)
 {
-       u64 a = of_read_addr(addr, na);
-       memset(addr, 0, na * 4);
-       a += offset;
-       if (na > 1)
-               addr[na - 2] = a >> 32;
-       addr[na - 1] = a & 0xffffffffu;
+       u32 result[OF_MAX_ADDR_CELLS];
+       int i;
+
+       if (ns > 2) {
+               printk("of_device: Cannot handle size cells (%d) > 2.", ns);
+               return -EINVAL;
+       }
+
+       if (of_out_of_range(addr, range, range + na + pna, na, ns))
+               return -EINVAL;
+
+       /* Start with the parent range base.  */
+       memcpy(result, range + na, pna * 4);
+
+       /* Add in the child address offset.  */
+       for (i = 0; i < na; i++)
+               result[pna - 1 - i] +=
+                       (addr[na - 1 - i] -
+                        range[na - 1 - i]);
+
+       memcpy(addr, result, pna * 4);
 
        return 0;
 }
 
-static unsigned int of_bus_default_get_flags(u32 *addr)
+static unsigned int of_bus_default_get_flags(const u32 *addr)
 {
        return IORESOURCE_MEM;
 }
 
-
 /*
  * PCI bus specific translator
  */
 
 static int of_bus_pci_match(struct device_node *np)
 {
-       return !strcmp(np->type, "pci") || !strcmp(np->type, "pciex");
+       if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
+               /* Do not do PCI specific frobbing if the
+                * PCI bridge lacks a ranges property.  We
+                * want to pass it through up to the next
+                * parent as-is, not with the PCI translate
+                * method which chops off the top address cell.
+                */
+               if (!of_find_property(np, "ranges", NULL))
+                       return 0;
+
+               return 1;
+       }
+
+       return 0;
 }
 
 static void of_bus_pci_count_cells(struct device_node *np,
@@ -273,30 +306,35 @@ static void of_bus_pci_count_cells(struct device_node *np,
                *sizec = 2;
 }
 
-static u64 of_bus_pci_map(u32 *addr, u32 *range, int na, int ns, int pna)
+static int of_bus_pci_map(u32 *addr, const u32 *range,
+                         int na, int ns, int pna)
 {
-       u64 cp, s, da;
+       u32 result[OF_MAX_ADDR_CELLS];
+       int i;
 
        /* Check address type match */
        if ((addr[0] ^ range[0]) & 0x03000000)
-               return OF_BAD_ADDR;
+               return -EINVAL;
 
-       /* Read address values, skipping high cell */
-       cp = of_read_addr(range + 1, na - 1);
-       s  = of_read_addr(range + na + pna, ns);
-       da = of_read_addr(addr + 1, na - 1);
+       if (of_out_of_range(addr + 1, range + 1, range + na + pna,
+                           na - 1, ns))
+               return -EINVAL;
 
-       if (da < cp || da >= (cp + s))
-               return OF_BAD_ADDR;
-       return da - cp;
-}
+       /* Start with the parent range base.  */
+       memcpy(result, range + na, pna * 4);
 
-static int of_bus_pci_translate(u32 *addr, u64 offset, int na)
-{
-       return of_bus_default_translate(addr + 1, offset, na - 1);
+       /* Add in the child address offset, skipping high cell.  */
+       for (i = 0; i < na - 1; i++)
+               result[pna - 1 - i] +=
+                       (addr[na - 1 - i] -
+                        range[na - 1 - i]);
+
+       memcpy(addr, result, pna * 4);
+
+       return 0;
 }
 
-static unsigned int of_bus_pci_get_flags(u32 *addr)
+static unsigned int of_bus_pci_get_flags(const u32 *addr)
 {
        unsigned int flags = 0;
        u32 w = addr[0];
@@ -332,17 +370,12 @@ static void of_bus_sbus_count_cells(struct device_node *child,
                *sizec = 1;
 }
 
-static u64 of_bus_sbus_map(u32 *addr, u32 *range, int na, int ns, int pna)
+static int of_bus_sbus_map(u32 *addr, const u32 *range, int na, int ns, int pna)
 {
        return of_bus_default_map(addr, range, na, ns, pna);
 }
 
-static int of_bus_sbus_translate(u32 *addr, u64 offset, int na)
-{
-       return of_bus_default_translate(addr, offset, na);
-}
-
-static unsigned int of_bus_sbus_get_flags(u32 *addr)
+static unsigned int of_bus_sbus_get_flags(const u32 *addr)
 {
        return IORESOURCE_MEM;
 }
@@ -360,7 +393,6 @@ static struct of_bus of_busses[] = {
                .match = of_bus_pci_match,
                .count_cells = of_bus_pci_count_cells,
                .map = of_bus_pci_map,
-               .translate = of_bus_pci_translate,
                .get_flags = of_bus_pci_get_flags,
        },
        /* SBUS */
@@ -370,7 +402,6 @@ static struct of_bus of_busses[] = {
                .match = of_bus_sbus_match,
                .count_cells = of_bus_sbus_count_cells,
                .map = of_bus_sbus_map,
-               .translate = of_bus_sbus_translate,
                .get_flags = of_bus_sbus_get_flags,
        },
        /* Default */
@@ -380,7 +411,6 @@ static struct of_bus of_busses[] = {
                .match = NULL,
                .count_cells = of_bus_default_count_cells,
                .map = of_bus_default_map,
-               .translate = of_bus_default_translate,
                .get_flags = of_bus_default_get_flags,
        },
 };
@@ -402,36 +432,37 @@ static int __init build_one_resource(struct device_node *parent,
                                     u32 *addr,
                                     int na, int ns, int pna)
 {
-       u32 *ranges;
+       const u32 *ranges;
        unsigned int rlen;
        int rone;
-       u64 offset = OF_BAD_ADDR;
 
        ranges = of_get_property(parent, "ranges", &rlen);
        if (ranges == NULL || rlen == 0) {
-               offset = of_read_addr(addr, na);
-               memset(addr, 0, pna * 4);
-               goto finish;
+               u32 result[OF_MAX_ADDR_CELLS];
+               int i;
+
+               memset(result, 0, pna * 4);
+               for (i = 0; i < na; i++)
+                       result[pna - 1 - i] =
+                               addr[na - 1 - i];
+
+               memcpy(addr, result, pna * 4);
+               return 0;
        }
 
        /* Now walk through the ranges */
        rlen /= 4;
        rone = na + pna + ns;
        for (; rlen >= rone; rlen -= rone, ranges += rone) {
-               offset = bus->map(addr, ranges, na, ns, pna);
-               if (offset != OF_BAD_ADDR)
-                       break;
+               if (!bus->map(addr, ranges, na, ns, pna))
+                       return 0;
        }
-       if (offset == OF_BAD_ADDR)
-               return 1;
-
-       memcpy(addr, ranges + na, 4 * pna);
 
-finish:
-       /* Translate it into parent bus space */
-       return pbus->translate(addr, offset, pna);
+       return 1;
 }
 
+static int of_resource_verbose;
+
 static void __init build_device_resources(struct of_device *op,
                                          struct device *parent)
 {
@@ -439,7 +470,7 @@ static void __init build_device_resources(struct of_device *op,
        struct of_bus *bus;
        int na, ns;
        int index, num_reg;
-       void *preg;
+       const void *preg;
 
        if (!parent)
                return;
@@ -461,10 +492,10 @@ static void __init build_device_resources(struct of_device *op,
        for (index = 0; index < num_reg; index++) {
                struct resource *r = &op->resource[index];
                u32 addr[OF_MAX_ADDR_CELLS];
-               u32 *reg = (preg + (index * ((na + ns) * 4)));
+               const u32 *reg = (preg + (index * ((na + ns) * 4)));
                struct device_node *dp = op->node;
                struct device_node *pp = p_op->node;
-               struct of_bus *pbus;
+               struct of_bus *pbus, *dbus;
                u64 size, result = OF_BAD_ADDR;
                unsigned long flags;
                int dna, dns;
@@ -485,6 +516,7 @@ static void __init build_device_resources(struct of_device *op,
 
                dna = na;
                dns = ns;
+               dbus = bus;
 
                while (1) {
                        dp = pp;
@@ -497,23 +529,27 @@ static void __init build_device_resources(struct of_device *op,
                        pbus = of_match_bus(pp);
                        pbus->count_cells(dp, &pna, &pns);
 
-                       if (build_one_resource(dp, bus, pbus, addr, dna, dns, pna))
+                       if (build_one_resource(dp, dbus, pbus, addr,
+                                              dna, dns, pna))
                                break;
 
                        dna = pna;
                        dns = pns;
-                       bus = pbus;
+                       dbus = pbus;
                }
 
        build_res:
                memset(r, 0, sizeof(*r));
+
+               if (of_resource_verbose)
+                       printk("%s reg[%d] -> %llx\n",
+                              op->node->full_name, index,
+                              result);
+
                if (result != OF_BAD_ADDR) {
                        r->start = result & 0xffffffff;
                        r->end = result + size - 1;
                        r->flags = flags | ((result >> 32ULL) & 0xffUL);
-               } else {
-                       r->start = ~0UL;
-                       r->end = ~0UL;
                }
                r->name = op->node->name;
        }
@@ -523,7 +559,7 @@ static struct of_device * __init scan_one_device(struct device_node *dp,
                                                 struct device *parent)
 {
        struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
-       struct linux_prom_irqs *intr;
+       const struct linux_prom_irqs *intr;
        int len, i;
 
        if (!op)
@@ -543,7 +579,8 @@ static struct of_device * __init scan_one_device(struct device_node *dp,
                for (i = 0; i < op->num_irqs; i++)
                        op->irqs[i] = intr[i].pri;
        } else {
-               unsigned int *irq = of_get_property(dp, "interrupts", &len);
+               const unsigned int *irq =
+                       of_get_property(dp, "interrupts", &len);
 
                if (irq) {
                        op->num_irqs = len / sizeof(unsigned int);
@@ -557,14 +594,41 @@ static struct of_device * __init scan_one_device(struct device_node *dp,
                static int pil_to_sbus[] = {
                        0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0,
                };
-               struct device_node *busp = dp->parent;
-               struct linux_prom_registers *regs;
-               int board = of_getintprop_default(busp, "board#", 0);
-               int slot;
+               struct device_node *io_unit, *sbi = dp->parent;
+               const struct linux_prom_registers *regs;
+               int board, slot;
+
+               while (sbi) {
+                       if (!strcmp(sbi->name, "sbi"))
+                               break;
+
+                       sbi = sbi->parent;
+               }
+               if (!sbi)
+                       goto build_resources;
 
                regs = of_get_property(dp, "reg", NULL);
+               if (!regs)
+                       goto build_resources;
+
                slot = regs->which_io;
 
+               /* If SBI's parent is not io-unit or the io-unit lacks
+                * a "board#" property, something is very wrong.
+                */
+               if (!sbi->parent || strcmp(sbi->parent->name, "io-unit")) {
+                       printk("%s: Error, parent is not io-unit.\n",
+                              sbi->full_name);
+                       goto build_resources;
+               }
+               io_unit = sbi->parent;
+               board = of_getintprop_default(io_unit, "board#", -1);
+               if (board == -1) {
+                       printk("%s: Error, lacks board# property.\n",
+                              io_unit->full_name);
+                       goto build_resources;
+               }
+
                for (i = 0; i < op->num_irqs; i++) {
                        int this_irq = op->irqs[i];
                        int sbusl = pil_to_sbus[this_irq];
@@ -578,6 +642,7 @@ static struct of_device * __init scan_one_device(struct device_node *dp,
                }
        }
 
+build_resources:
        build_device_resources(op, parent);
 
        op->dev.parent = parent;
@@ -585,7 +650,7 @@ static struct of_device * __init scan_one_device(struct device_node *dp,
        if (!parent)
                strcpy(op->dev.bus_id, "root");
        else
-               strcpy(op->dev.bus_id, dp->path_component_name);
+               sprintf(op->dev.bus_id, "%08x", dp->node);
 
        if (of_device_register(op)) {
                printk("%s: Could not register of device.\n",
@@ -643,6 +708,18 @@ static int __init of_bus_driver_init(void)
 
 postcore_initcall(of_bus_driver_init);
 
+static int __init of_debug(char *str)
+{
+       int val = 0;
+
+       get_option(&str, &val);
+       if (val & 1)
+               of_resource_verbose = 1;
+       return 1;
+}
+
+__setup("of_debug=", of_debug);
+
 int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
 {
        /* initialize common driver fields */
@@ -695,9 +772,11 @@ int of_device_register(struct of_device *ofdev)
        if (rc)
                return rc;
 
-       device_create_file(&ofdev->dev, &dev_attr_devspec);
+       rc = device_create_file(&ofdev->dev, &dev_attr_devspec);
+       if (rc)
+               device_unregister(&ofdev->dev);
 
-       return 0;
+       return rc;
 }
 
 void of_device_unregister(struct of_device *ofdev)
@@ -713,10 +792,9 @@ struct of_device* of_platform_device_create(struct device_node *np,
 {
        struct of_device *dev;
 
-       dev = kmalloc(sizeof(*dev), GFP_KERNEL);
+       dev = kzalloc(sizeof(*dev), GFP_KERNEL);
        if (!dev)
                return NULL;
-       memset(dev, 0, sizeof(*dev));
 
        dev->dev.parent = parent;
        dev->dev.bus = bus;