1 #include <linux/string.h>
2 #include <linux/kernel.h>
4 #include <linux/init.h>
5 #include <linux/module.h>
6 #include <linux/mod_devicetable.h>
7 #include <linux/errno.h>
9 #include <linux/of_device.h>
10 #include <linux/of_platform.h>
12 #include "of_device_common.h"
14 static int node_match(struct device *dev, void *data)
16 struct platform_device *op = to_platform_device(dev);
17 struct device_node *dp = data;
19 return (op->dev.of_node == dp);
22 struct platform_device *of_find_device_by_node(struct device_node *dp)
24 struct device *dev = bus_find_device(&platform_bus_type, NULL,
28 return to_platform_device(dev);
32 EXPORT_SYMBOL(of_find_device_by_node);
34 unsigned int irq_of_parse_and_map(struct device_node *node, int index)
36 struct platform_device *op = of_find_device_by_node(node);
38 if (!op || index >= op->archdata.num_irqs)
41 return op->archdata.irqs[index];
43 EXPORT_SYMBOL(irq_of_parse_and_map);
45 /* Take the archdata values for IOMMU, STC, and HOSTDATA found in
46 * BUS and propagate to all child platform_device objects.
48 void of_propagate_archdata(struct platform_device *bus)
50 struct dev_archdata *bus_sd = &bus->dev.archdata;
51 struct device_node *bus_dp = bus->dev.of_node;
52 struct device_node *dp;
54 for (dp = bus_dp->child; dp; dp = dp->sibling) {
55 struct platform_device *op = of_find_device_by_node(dp);
57 op->dev.archdata.iommu = bus_sd->iommu;
58 op->dev.archdata.stc = bus_sd->stc;
59 op->dev.archdata.host_controller = bus_sd->host_controller;
60 op->dev.archdata.numa_node = bus_sd->numa_node;
63 of_propagate_archdata(op);
67 static void get_cells(struct device_node *dp, int *addrc, int *sizec)
70 *addrc = of_n_addr_cells(dp);
72 *sizec = of_n_size_cells(dp);
76 * Default translator (generic bus)
79 void of_bus_default_count_cells(struct device_node *dev, int *addrc, int *sizec)
81 get_cells(dev, addrc, sizec);
84 /* Make sure the least significant 64-bits are in-range. Even
85 * for 3 or 4 cell values it is a good enough approximation.
87 int of_out_of_range(const u32 *addr, const u32 *base,
88 const u32 *size, int na, int ns)
90 u64 a = of_read_addr(addr, na);
91 u64 b = of_read_addr(base, na);
96 b += of_read_addr(size, ns);
103 int of_bus_default_map(u32 *addr, const u32 *range, int na, int ns, int pna)
105 u32 result[OF_MAX_ADDR_CELLS];
109 printk("of_device: Cannot handle size cells (%d) > 2.", ns);
113 if (of_out_of_range(addr, range, range + na + pna, na, ns))
116 /* Start with the parent range base. */
117 memcpy(result, range + na, pna * 4);
119 /* Add in the child address offset. */
120 for (i = 0; i < na; i++)
121 result[pna - 1 - i] +=
125 memcpy(addr, result, pna * 4);
130 unsigned long of_bus_default_get_flags(const u32 *addr, unsigned long flags)
134 return IORESOURCE_MEM;
138 * SBUS bus specific translator
141 int of_bus_sbus_match(struct device_node *np)
143 struct device_node *dp = np;
146 if (!strcmp(dp->name, "sbus") ||
147 !strcmp(dp->name, "sbi"))
150 /* Have a look at use_1to1_mapping(). We're trying
151 * to match SBUS if that's the top-level bus and we
152 * don't have some intervening real bus that provides
153 * ranges based translations.
155 if (of_find_property(dp, "ranges", NULL) != NULL)
164 void of_bus_sbus_count_cells(struct device_node *child, int *addrc, int *sizec)