1 #include <linux/string.h>
2 #include <linux/kernel.h>
3 #include <linux/of.h>
4 #include <linux/export.h>
5 #include <linux/mod_devicetable.h>
6 #include <linux/errno.h>
7 #include <linux/irq.h>
8 #include <linux/of_device.h>
9 #include <linux/of_platform.h>
10 
11 #include "of_device_common.h"
12 
13 unsigned int irq_of_parse_and_map(struct device_node *node, int index)
14 {
15 	struct platform_device *op = of_find_device_by_node(node);
16 
17 	if (!op || index >= op->archdata.num_irqs)
18 		return 0;
19 
20 	return op->archdata.irqs[index];
21 }
22 EXPORT_SYMBOL(irq_of_parse_and_map);
23 
24 int of_address_to_resource(struct device_node *node, int index,
25 			   struct resource *r)
26 {
27 	struct platform_device *op = of_find_device_by_node(node);
28 
29 	if (!op || index >= op->num_resources)
30 		return -EINVAL;
31 
32 	memcpy(r, &op->archdata.resource[index], sizeof(*r));
33 	return 0;
34 }
35 EXPORT_SYMBOL_GPL(of_address_to_resource);
36 
37 void __iomem *of_iomap(struct device_node *node, int index)
38 {
39 	struct platform_device *op = of_find_device_by_node(node);
40 	struct resource *r;
41 
42 	if (!op || index >= op->num_resources)
43 		return NULL;
44 
45 	r = &op->archdata.resource[index];
46 
47 	return of_ioremap(r, 0, resource_size(r), (char *) r->name);
48 }
49 EXPORT_SYMBOL(of_iomap);
50 
51 /* Take the archdata values for IOMMU, STC, and HOSTDATA found in
52  * BUS and propagate to all child platform_device objects.
53  */
54 void of_propagate_archdata(struct platform_device *bus)
55 {
56 	struct dev_archdata *bus_sd = &bus->dev.archdata;
57 	struct device_node *bus_dp = bus->dev.of_node;
58 	struct device_node *dp;
59 
60 	for (dp = bus_dp->child; dp; dp = dp->sibling) {
61 		struct platform_device *op = of_find_device_by_node(dp);
62 
63 		op->dev.archdata.iommu = bus_sd->iommu;
64 		op->dev.archdata.stc = bus_sd->stc;
65 		op->dev.archdata.host_controller = bus_sd->host_controller;
66 		op->dev.archdata.numa_node = bus_sd->numa_node;
67 
68 		if (dp->child)
69 			of_propagate_archdata(op);
70 	}
71 }
72 
73 static void get_cells(struct device_node *dp, int *addrc, int *sizec)
74 {
75 	if (addrc)
76 		*addrc = of_n_addr_cells(dp);
77 	if (sizec)
78 		*sizec = of_n_size_cells(dp);
79 }
80 
81 /*
82  * Default translator (generic bus)
83  */
84 
85 void of_bus_default_count_cells(struct device_node *dev, int *addrc, int *sizec)
86 {
87 	get_cells(dev, addrc, sizec);
88 }
89 
90 /* Make sure the least significant 64-bits are in-range.  Even
91  * for 3 or 4 cell values it is a good enough approximation.
92  */
93 int of_out_of_range(const u32 *addr, const u32 *base,
94 		    const u32 *size, int na, int ns)
95 {
96 	u64 a = of_read_addr(addr, na);
97 	u64 b = of_read_addr(base, na);
98 
99 	if (a < b)
100 		return 1;
101 
102 	b += of_read_addr(size, ns);
103 	if (a >= b)
104 		return 1;
105 
106 	return 0;
107 }
108 
109 int of_bus_default_map(u32 *addr, const u32 *range, int na, int ns, int pna)
110 {
111 	u32 result[OF_MAX_ADDR_CELLS];
112 	int i;
113 
114 	if (ns > 2) {
115 		printk("of_device: Cannot handle size cells (%d) > 2.", ns);
116 		return -EINVAL;
117 	}
118 
119 	if (of_out_of_range(addr, range, range + na + pna, na, ns))
120 		return -EINVAL;
121 
122 	/* Start with the parent range base.  */
123 	memcpy(result, range + na, pna * 4);
124 
125 	/* Add in the child address offset.  */
126 	for (i = 0; i < na; i++)
127 		result[pna - 1 - i] +=
128 			(addr[na - 1 - i] -
129 			 range[na - 1 - i]);
130 
131 	memcpy(addr, result, pna * 4);
132 
133 	return 0;
134 }
135 
136 unsigned long of_bus_default_get_flags(const u32 *addr, unsigned long flags)
137 {
138 	if (flags)
139 		return flags;
140 	return IORESOURCE_MEM;
141 }
142 
143 /*
144  * SBUS bus specific translator
145  */
146 
147 int of_bus_sbus_match(struct device_node *np)
148 {
149 	struct device_node *dp = np;
150 
151 	while (dp) {
152 		if (!strcmp(dp->name, "sbus") ||
153 		    !strcmp(dp->name, "sbi"))
154 			return 1;
155 
156 		/* Have a look at use_1to1_mapping().  We're trying
157 		 * to match SBUS if that's the top-level bus and we
158 		 * don't have some intervening real bus that provides
159 		 * ranges based translations.
160 		 */
161 		if (of_find_property(dp, "ranges", NULL) != NULL)
162 			break;
163 
164 		dp = dp->parent;
165 	}
166 
167 	return 0;
168 }
169 
170 void of_bus_sbus_count_cells(struct device_node *child, int *addrc, int *sizec)
171 {
172 	if (addrc)
173 		*addrc = 2;
174 	if (sizec)
175 		*sizec = 1;
176 }
177