diff options
author | Robert Reif <reif@earthlink.net> | 2009-06-04 05:00:02 -0400 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2009-06-16 07:56:49 -0400 |
commit | c9f5b7e77c30da25104a3f7f26ac46c07d7b5cb6 (patch) | |
tree | e209e70377b4fbaf95168366430884b3b2474d2e /arch/sparc | |
parent | d69864158e24f323e818403c6b89ad4871aea6f6 (diff) |
sparc: move of_device common code to of_device_common
This patch moves code common to of_device_32.c and of_device_64.c into
of_device_common.h and of_device_common.c.
The only functional difference is in sparc32 where of_bus_default_map is
used in place of of_bus_sbus_map because they are equivelent.
There is still room for further code consolidation with some minor
refactoring.
Boot tested on sparc32 and compile tested on sparc64.
Signed-off-by: Robert Reif <reif@earthlink.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'arch/sparc')
-rw-r--r-- | arch/sparc/kernel/Makefile | 1 | ||||
-rw-r--r-- | arch/sparc/kernel/of_device_32.c | 195 | ||||
-rw-r--r-- | arch/sparc/kernel/of_device_64.c | 188 | ||||
-rw-r--r-- | arch/sparc/kernel/of_device_common.c | 174 | ||||
-rw-r--r-- | arch/sparc/kernel/of_device_common.h | 36 |
5 files changed, 216 insertions, 378 deletions
diff --git a/arch/sparc/kernel/Makefile b/arch/sparc/kernel/Makefile index 47029c66b17a..475ce4696acd 100644 --- a/arch/sparc/kernel/Makefile +++ b/arch/sparc/kernel/Makefile | |||
@@ -37,6 +37,7 @@ obj-y += una_asm_$(BITS).o | |||
37 | obj-$(CONFIG_SPARC32) += muldiv.o | 37 | obj-$(CONFIG_SPARC32) += muldiv.o |
38 | obj-y += prom_common.o | 38 | obj-y += prom_common.o |
39 | obj-y += prom_$(BITS).o | 39 | obj-y += prom_$(BITS).o |
40 | obj-y += of_device_common.o | ||
40 | obj-y += of_device_$(BITS).o | 41 | obj-y += of_device_$(BITS).o |
41 | obj-$(CONFIG_SPARC64) += prom_irqtrans.o | 42 | obj-$(CONFIG_SPARC64) += prom_irqtrans.o |
42 | 43 | ||
diff --git a/arch/sparc/kernel/of_device_32.c b/arch/sparc/kernel/of_device_32.c index c8f14c1dc521..90396702ea2c 100644 --- a/arch/sparc/kernel/of_device_32.c +++ b/arch/sparc/kernel/of_device_32.c | |||
@@ -6,159 +6,11 @@ | |||
6 | #include <linux/mod_devicetable.h> | 6 | #include <linux/mod_devicetable.h> |
7 | #include <linux/slab.h> | 7 | #include <linux/slab.h> |
8 | #include <linux/errno.h> | 8 | #include <linux/errno.h> |
9 | #include <linux/irq.h> | ||
9 | #include <linux/of_device.h> | 10 | #include <linux/of_device.h> |
10 | #include <linux/of_platform.h> | 11 | #include <linux/of_platform.h> |
11 | 12 | ||
12 | static int node_match(struct device *dev, void *data) | 13 | #include "of_device_common.h" |
13 | { | ||
14 | struct of_device *op = to_of_device(dev); | ||
15 | struct device_node *dp = data; | ||
16 | |||
17 | return (op->node == dp); | ||
18 | } | ||
19 | |||
20 | struct of_device *of_find_device_by_node(struct device_node *dp) | ||
21 | { | ||
22 | struct device *dev = bus_find_device(&of_platform_bus_type, NULL, | ||
23 | dp, node_match); | ||
24 | |||
25 | if (dev) | ||
26 | return to_of_device(dev); | ||
27 | |||
28 | return NULL; | ||
29 | } | ||
30 | EXPORT_SYMBOL(of_find_device_by_node); | ||
31 | |||
32 | unsigned int irq_of_parse_and_map(struct device_node *node, int index) | ||
33 | { | ||
34 | struct of_device *op = of_find_device_by_node(node); | ||
35 | |||
36 | if (!op || index >= op->num_irqs) | ||
37 | return 0; | ||
38 | |||
39 | return op->irqs[index]; | ||
40 | } | ||
41 | EXPORT_SYMBOL(irq_of_parse_and_map); | ||
42 | |||
43 | /* Take the archdata values for IOMMU, STC, and HOSTDATA found in | ||
44 | * BUS and propagate to all child of_device objects. | ||
45 | */ | ||
46 | void of_propagate_archdata(struct of_device *bus) | ||
47 | { | ||
48 | struct dev_archdata *bus_sd = &bus->dev.archdata; | ||
49 | struct device_node *bus_dp = bus->node; | ||
50 | struct device_node *dp; | ||
51 | |||
52 | for (dp = bus_dp->child; dp; dp = dp->sibling) { | ||
53 | struct of_device *op = of_find_device_by_node(dp); | ||
54 | |||
55 | op->dev.archdata.iommu = bus_sd->iommu; | ||
56 | op->dev.archdata.stc = bus_sd->stc; | ||
57 | op->dev.archdata.host_controller = bus_sd->host_controller; | ||
58 | op->dev.archdata.numa_node = bus_sd->numa_node; | ||
59 | |||
60 | if (dp->child) | ||
61 | of_propagate_archdata(op); | ||
62 | } | ||
63 | } | ||
64 | |||
65 | struct bus_type of_platform_bus_type; | ||
66 | EXPORT_SYMBOL(of_platform_bus_type); | ||
67 | |||
68 | static inline u64 of_read_addr(const u32 *cell, int size) | ||
69 | { | ||
70 | u64 r = 0; | ||
71 | while (size--) | ||
72 | r = (r << 32) | *(cell++); | ||
73 | return r; | ||
74 | } | ||
75 | |||
76 | static void __init get_cells(struct device_node *dp, | ||
77 | int *addrc, int *sizec) | ||
78 | { | ||
79 | if (addrc) | ||
80 | *addrc = of_n_addr_cells(dp); | ||
81 | if (sizec) | ||
82 | *sizec = of_n_size_cells(dp); | ||
83 | } | ||
84 | |||
85 | /* Max address size we deal with */ | ||
86 | #define OF_MAX_ADDR_CELLS 4 | ||
87 | |||
88 | struct of_bus { | ||
89 | const char *name; | ||
90 | const char *addr_prop_name; | ||
91 | int (*match)(struct device_node *parent); | ||
92 | void (*count_cells)(struct device_node *child, | ||
93 | int *addrc, int *sizec); | ||
94 | int (*map)(u32 *addr, const u32 *range, | ||
95 | int na, int ns, int pna); | ||
96 | unsigned long (*get_flags)(const u32 *addr, unsigned long); | ||
97 | }; | ||
98 | |||
99 | /* | ||
100 | * Default translator (generic bus) | ||
101 | */ | ||
102 | |||
103 | static void of_bus_default_count_cells(struct device_node *dev, | ||
104 | int *addrc, int *sizec) | ||
105 | { | ||
106 | get_cells(dev, addrc, sizec); | ||
107 | } | ||
108 | |||
109 | /* Make sure the least significant 64-bits are in-range. Even | ||
110 | * for 3 or 4 cell values it is a good enough approximation. | ||
111 | */ | ||
112 | static int of_out_of_range(const u32 *addr, const u32 *base, | ||
113 | const u32 *size, int na, int ns) | ||
114 | { | ||
115 | u64 a = of_read_addr(addr, na); | ||
116 | u64 b = of_read_addr(base, na); | ||
117 | |||
118 | if (a < b) | ||
119 | return 1; | ||
120 | |||
121 | b += of_read_addr(size, ns); | ||
122 | if (a >= b) | ||
123 | return 1; | ||
124 | |||
125 | return 0; | ||
126 | } | ||
127 | |||
128 | static int of_bus_default_map(u32 *addr, const u32 *range, | ||
129 | int na, int ns, int pna) | ||
130 | { | ||
131 | u32 result[OF_MAX_ADDR_CELLS]; | ||
132 | int i; | ||
133 | |||
134 | if (ns > 2) { | ||
135 | printk("of_device: Cannot handle size cells (%d) > 2.", ns); | ||
136 | return -EINVAL; | ||
137 | } | ||
138 | |||
139 | if (of_out_of_range(addr, range, range + na + pna, na, ns)) | ||
140 | return -EINVAL; | ||
141 | |||
142 | /* Start with the parent range base. */ | ||
143 | memcpy(result, range + na, pna * 4); | ||
144 | |||
145 | /* Add in the child address offset. */ | ||
146 | for (i = 0; i < na; i++) | ||
147 | result[pna - 1 - i] += | ||
148 | (addr[na - 1 - i] - | ||
149 | range[na - 1 - i]); | ||
150 | |||
151 | memcpy(addr, result, pna * 4); | ||
152 | |||
153 | return 0; | ||
154 | } | ||
155 | |||
156 | static unsigned long of_bus_default_get_flags(const u32 *addr, unsigned long flags) | ||
157 | { | ||
158 | if (flags) | ||
159 | return flags; | ||
160 | return IORESOURCE_MEM; | ||
161 | } | ||
162 | 14 | ||
163 | /* | 15 | /* |
164 | * PCI bus specific translator | 16 | * PCI bus specific translator |
@@ -240,47 +92,6 @@ static unsigned long of_bus_pci_get_flags(const u32 *addr, unsigned long flags) | |||
240 | return flags; | 92 | return flags; |
241 | } | 93 | } |
242 | 94 | ||
243 | /* | ||
244 | * SBUS bus specific translator | ||
245 | */ | ||
246 | |||
247 | static int of_bus_sbus_match(struct device_node *np) | ||
248 | { | ||
249 | struct device_node *dp = np; | ||
250 | |||
251 | while (dp) { | ||
252 | if (!strcmp(dp->name, "sbus") || | ||
253 | !strcmp(dp->name, "sbi")) | ||
254 | return 1; | ||
255 | |||
256 | /* Have a look at use_1to1_mapping(). We're trying | ||
257 | * to match SBUS if that's the top-level bus and we | ||
258 | * don't have some intervening real bus that provides | ||
259 | * ranges based translations. | ||
260 | */ | ||
261 | if (of_find_property(dp, "ranges", NULL) != NULL) | ||
262 | break; | ||
263 | |||
264 | dp = dp->parent; | ||
265 | } | ||
266 | |||
267 | return 0; | ||
268 | } | ||
269 | |||
270 | static void of_bus_sbus_count_cells(struct device_node *child, | ||
271 | int *addrc, int *sizec) | ||
272 | { | ||
273 | if (addrc) | ||
274 | *addrc = 2; | ||
275 | if (sizec) | ||
276 | *sizec = 1; | ||
277 | } | ||
278 | |||
279 | static int of_bus_sbus_map(u32 *addr, const u32 *range, int na, int ns, int pna) | ||
280 | { | ||
281 | return of_bus_default_map(addr, range, na, ns, pna); | ||
282 | } | ||
283 | |||
284 | static unsigned long of_bus_sbus_get_flags(const u32 *addr, unsigned long flags) | 95 | static unsigned long of_bus_sbus_get_flags(const u32 *addr, unsigned long flags) |
285 | { | 96 | { |
286 | return IORESOURCE_MEM; | 97 | return IORESOURCE_MEM; |
@@ -307,7 +118,7 @@ static struct of_bus of_busses[] = { | |||
307 | .addr_prop_name = "reg", | 118 | .addr_prop_name = "reg", |
308 | .match = of_bus_sbus_match, | 119 | .match = of_bus_sbus_match, |
309 | .count_cells = of_bus_sbus_count_cells, | 120 | .count_cells = of_bus_sbus_count_cells, |
310 | .map = of_bus_sbus_map, | 121 | .map = of_bus_default_map, |
311 | .get_flags = of_bus_sbus_get_flags, | 122 | .get_flags = of_bus_sbus_get_flags, |
312 | }, | 123 | }, |
313 | /* Default */ | 124 | /* Default */ |
diff --git a/arch/sparc/kernel/of_device_64.c b/arch/sparc/kernel/of_device_64.c index 5ac287ac03de..881947e59e95 100644 --- a/arch/sparc/kernel/of_device_64.c +++ b/arch/sparc/kernel/of_device_64.c | |||
@@ -10,6 +10,8 @@ | |||
10 | #include <linux/of_device.h> | 10 | #include <linux/of_device.h> |
11 | #include <linux/of_platform.h> | 11 | #include <linux/of_platform.h> |
12 | 12 | ||
13 | #include "of_device_common.h" | ||
14 | |||
13 | void __iomem *of_ioremap(struct resource *res, unsigned long offset, unsigned long size, char *name) | 15 | void __iomem *of_ioremap(struct resource *res, unsigned long offset, unsigned long size, char *name) |
14 | { | 16 | { |
15 | unsigned long ret = res->start + offset; | 17 | unsigned long ret = res->start + offset; |
@@ -35,156 +37,6 @@ void of_iounmap(struct resource *res, void __iomem *base, unsigned long size) | |||
35 | } | 37 | } |
36 | EXPORT_SYMBOL(of_iounmap); | 38 | EXPORT_SYMBOL(of_iounmap); |
37 | 39 | ||
38 | static int node_match(struct device *dev, void *data) | ||
39 | { | ||
40 | struct of_device *op = to_of_device(dev); | ||
41 | struct device_node *dp = data; | ||
42 | |||
43 | return (op->node == dp); | ||
44 | } | ||
45 | |||
46 | struct of_device *of_find_device_by_node(struct device_node *dp) | ||
47 | { | ||
48 | struct device *dev = bus_find_device(&of_platform_bus_type, NULL, | ||
49 | dp, node_match); | ||
50 | |||
51 | if (dev) | ||
52 | return to_of_device(dev); | ||
53 | |||
54 | return NULL; | ||
55 | } | ||
56 | EXPORT_SYMBOL(of_find_device_by_node); | ||
57 | |||
58 | unsigned int irq_of_parse_and_map(struct device_node *node, int index) | ||
59 | { | ||
60 | struct of_device *op = of_find_device_by_node(node); | ||
61 | |||
62 | if (!op || index >= op->num_irqs) | ||
63 | return 0; | ||
64 | |||
65 | return op->irqs[index]; | ||
66 | } | ||
67 | EXPORT_SYMBOL(irq_of_parse_and_map); | ||
68 | |||
69 | /* Take the archdata values for IOMMU, STC, and HOSTDATA found in | ||
70 | * BUS and propagate to all child of_device objects. | ||
71 | */ | ||
72 | void of_propagate_archdata(struct of_device *bus) | ||
73 | { | ||
74 | struct dev_archdata *bus_sd = &bus->dev.archdata; | ||
75 | struct device_node *bus_dp = bus->node; | ||
76 | struct device_node *dp; | ||
77 | |||
78 | for (dp = bus_dp->child; dp; dp = dp->sibling) { | ||
79 | struct of_device *op = of_find_device_by_node(dp); | ||
80 | |||
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; | ||
85 | |||
86 | if (dp->child) | ||
87 | of_propagate_archdata(op); | ||
88 | } | ||
89 | } | ||
90 | |||
91 | struct bus_type of_platform_bus_type; | ||
92 | EXPORT_SYMBOL(of_platform_bus_type); | ||
93 | |||
94 | static inline u64 of_read_addr(const u32 *cell, int size) | ||
95 | { | ||
96 | u64 r = 0; | ||
97 | while (size--) | ||
98 | r = (r << 32) | *(cell++); | ||
99 | return r; | ||
100 | } | ||
101 | |||
102 | static void get_cells(struct device_node *dp, int *addrc, int *sizec) | ||
103 | { | ||
104 | if (addrc) | ||
105 | *addrc = of_n_addr_cells(dp); | ||
106 | if (sizec) | ||
107 | *sizec = of_n_size_cells(dp); | ||
108 | } | ||
109 | |||
110 | /* Max address size we deal with */ | ||
111 | #define OF_MAX_ADDR_CELLS 4 | ||
112 | |||
113 | struct of_bus { | ||
114 | const char *name; | ||
115 | const char *addr_prop_name; | ||
116 | int (*match)(struct device_node *parent); | ||
117 | void (*count_cells)(struct device_node *child, | ||
118 | int *addrc, int *sizec); | ||
119 | int (*map)(u32 *addr, const u32 *range, | ||
120 | int na, int ns, int pna); | ||
121 | unsigned long (*get_flags)(const u32 *addr, unsigned long); | ||
122 | }; | ||
123 | |||
124 | /* | ||
125 | * Default translator (generic bus) | ||
126 | */ | ||
127 | |||
128 | static void of_bus_default_count_cells(struct device_node *dev, | ||
129 | int *addrc, int *sizec) | ||
130 | { | ||
131 | get_cells(dev, addrc, sizec); | ||
132 | } | ||
133 | |||
134 | /* Make sure the least significant 64-bits are in-range. Even | ||
135 | * for 3 or 4 cell values it is a good enough approximation. | ||
136 | */ | ||
137 | static int of_out_of_range(const u32 *addr, const u32 *base, | ||
138 | const u32 *size, int na, int ns) | ||
139 | { | ||
140 | u64 a = of_read_addr(addr, na); | ||
141 | u64 b = of_read_addr(base, na); | ||
142 | |||
143 | if (a < b) | ||
144 | return 1; | ||
145 | |||
146 | b += of_read_addr(size, ns); | ||
147 | if (a >= b) | ||
148 | return 1; | ||
149 | |||
150 | return 0; | ||
151 | } | ||
152 | |||
153 | static int of_bus_default_map(u32 *addr, const u32 *range, | ||
154 | int na, int ns, int pna) | ||
155 | { | ||
156 | u32 result[OF_MAX_ADDR_CELLS]; | ||
157 | int i; | ||
158 | |||
159 | if (ns > 2) { | ||
160 | printk("of_device: Cannot handle size cells (%d) > 2.", ns); | ||
161 | return -EINVAL; | ||
162 | } | ||
163 | |||
164 | if (of_out_of_range(addr, range, range + na + pna, na, ns)) | ||
165 | return -EINVAL; | ||
166 | |||
167 | /* Start with the parent range base. */ | ||
168 | memcpy(result, range + na, pna * 4); | ||
169 | |||
170 | /* Add in the child address offset. */ | ||
171 | for (i = 0; i < na; i++) | ||
172 | result[pna - 1 - i] += | ||
173 | (addr[na - 1 - i] - | ||
174 | range[na - 1 - i]); | ||
175 | |||
176 | memcpy(addr, result, pna * 4); | ||
177 | |||
178 | return 0; | ||
179 | } | ||
180 | |||
181 | static unsigned long of_bus_default_get_flags(const u32 *addr, unsigned long flags) | ||
182 | { | ||
183 | if (flags) | ||
184 | return flags; | ||
185 | return IORESOURCE_MEM; | ||
186 | } | ||
187 | |||
188 | /* | 40 | /* |
189 | * PCI bus specific translator | 41 | * PCI bus specific translator |
190 | */ | 42 | */ |
@@ -295,42 +147,6 @@ static unsigned long of_bus_pci_get_flags(const u32 *addr, unsigned long flags) | |||
295 | } | 147 | } |
296 | 148 | ||
297 | /* | 149 | /* |
298 | * SBUS bus specific translator | ||
299 | */ | ||
300 | |||
301 | static int of_bus_sbus_match(struct device_node *np) | ||
302 | { | ||
303 | struct device_node *dp = np; | ||
304 | |||
305 | while (dp) { | ||
306 | if (!strcmp(dp->name, "sbus") || | ||
307 | !strcmp(dp->name, "sbi")) | ||
308 | return 1; | ||
309 | |||
310 | /* Have a look at use_1to1_mapping(). We're trying | ||
311 | * to match SBUS if that's the top-level bus and we | ||
312 | * don't have some intervening real bus that provides | ||
313 | * ranges based translations. | ||
314 | */ | ||
315 | if (of_find_property(dp, "ranges", NULL) != NULL) | ||
316 | break; | ||
317 | |||
318 | dp = dp->parent; | ||
319 | } | ||
320 | |||
321 | return 0; | ||
322 | } | ||
323 | |||
324 | static void of_bus_sbus_count_cells(struct device_node *child, | ||
325 | int *addrc, int *sizec) | ||
326 | { | ||
327 | if (addrc) | ||
328 | *addrc = 2; | ||
329 | if (sizec) | ||
330 | *sizec = 1; | ||
331 | } | ||
332 | |||
333 | /* | ||
334 | * FHC/Central bus specific translator. | 150 | * FHC/Central bus specific translator. |
335 | * | 151 | * |
336 | * This is just needed to hard-code the address and size cell | 152 | * This is just needed to hard-code the address and size cell |
diff --git a/arch/sparc/kernel/of_device_common.c b/arch/sparc/kernel/of_device_common.c new file mode 100644 index 000000000000..cb8eb799bb6c --- /dev/null +++ b/arch/sparc/kernel/of_device_common.c | |||
@@ -0,0 +1,174 @@ | |||
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> | ||
12 | |||
13 | #include "of_device_common.h" | ||
14 | |||
15 | static int node_match(struct device *dev, void *data) | ||
16 | { | ||
17 | struct of_device *op = to_of_device(dev); | ||
18 | struct device_node *dp = data; | ||
19 | |||
20 | return (op->node == dp); | ||
21 | } | ||
22 | |||
23 | struct of_device *of_find_device_by_node(struct device_node *dp) | ||
24 | { | ||
25 | struct device *dev = bus_find_device(&of_platform_bus_type, NULL, | ||
26 | dp, node_match); | ||
27 | |||
28 | if (dev) | ||
29 | return to_of_device(dev); | ||
30 | |||
31 | return NULL; | ||
32 | } | ||
33 | EXPORT_SYMBOL(of_find_device_by_node); | ||
34 | |||
35 | unsigned int irq_of_parse_and_map(struct device_node *node, int index) | ||
36 | { | ||
37 | struct of_device *op = of_find_device_by_node(node); | ||
38 | |||
39 | if (!op || index >= op->num_irqs) | ||
40 | return 0; | ||
41 | |||
42 | return op->irqs[index]; | ||
43 | } | ||
44 | EXPORT_SYMBOL(irq_of_parse_and_map); | ||
45 | |||
46 | /* Take the archdata values for IOMMU, STC, and HOSTDATA found in | ||
47 | * BUS and propagate to all child of_device objects. | ||
48 | */ | ||
49 | void of_propagate_archdata(struct of_device *bus) | ||
50 | { | ||
51 | struct dev_archdata *bus_sd = &bus->dev.archdata; | ||
52 | struct device_node *bus_dp = bus->node; | ||
53 | struct device_node *dp; | ||
54 | |||
55 | for (dp = bus_dp->child; dp; dp = dp->sibling) { | ||
56 | struct of_device *op = of_find_device_by_node(dp); | ||
57 | |||
58 | op->dev.archdata.iommu = bus_sd->iommu; | ||
59 | op->dev.archdata.stc = bus_sd->stc; | ||
60 | op->dev.archdata.host_controller = bus_sd->host_controller; | ||
61 | op->dev.archdata.numa_node = bus_sd->numa_node; | ||
62 | |||
63 | if (dp->child) | ||
64 | of_propagate_archdata(op); | ||
65 | } | ||
66 | } | ||
67 | |||
68 | struct bus_type of_platform_bus_type; | ||
69 | EXPORT_SYMBOL(of_platform_bus_type); | ||
70 | |||
71 | static void get_cells(struct device_node *dp, int *addrc, int *sizec) | ||
72 | { | ||
73 | if (addrc) | ||
74 | *addrc = of_n_addr_cells(dp); | ||
75 | if (sizec) | ||
76 | *sizec = of_n_size_cells(dp); | ||
77 | } | ||
78 | |||
79 | /* | ||
80 | * Default translator (generic bus) | ||
81 | */ | ||
82 | |||
83 | void of_bus_default_count_cells(struct device_node *dev, int *addrc, int *sizec) | ||
84 | { | ||
85 | get_cells(dev, addrc, sizec); | ||
86 | } | ||
87 | |||
88 | /* Make sure the least significant 64-bits are in-range. Even | ||
89 | * for 3 or 4 cell values it is a good enough approximation. | ||
90 | */ | ||
91 | int of_out_of_range(const u32 *addr, const u32 *base, | ||
92 | const u32 *size, int na, int ns) | ||
93 | { | ||
94 | u64 a = of_read_addr(addr, na); | ||
95 | u64 b = of_read_addr(base, na); | ||
96 | |||
97 | if (a < b) | ||
98 | return 1; | ||
99 | |||
100 | b += of_read_addr(size, ns); | ||
101 | if (a >= b) | ||
102 | return 1; | ||
103 | |||
104 | return 0; | ||
105 | } | ||
106 | |||
107 | int of_bus_default_map(u32 *addr, const u32 *range, int na, int ns, int pna) | ||
108 | { | ||
109 | u32 result[OF_MAX_ADDR_CELLS]; | ||
110 | int i; | ||
111 | |||
112 | if (ns > 2) { | ||
113 | printk("of_device: Cannot handle size cells (%d) > 2.", ns); | ||
114 | return -EINVAL; | ||
115 | } | ||
116 | |||
117 | if (of_out_of_range(addr, range, range + na + pna, na, ns)) | ||
118 | return -EINVAL; | ||
119 | |||
120 | /* Start with the parent range base. */ | ||
121 | memcpy(result, range + na, pna * 4); | ||
122 | |||
123 | /* Add in the child address offset. */ | ||
124 | for (i = 0; i < na; i++) | ||
125 | result[pna - 1 - i] += | ||
126 | (addr[na - 1 - i] - | ||
127 | range[na - 1 - i]); | ||
128 | |||
129 | memcpy(addr, result, pna * 4); | ||
130 | |||
131 | return 0; | ||
132 | } | ||
133 | |||
134 | unsigned long of_bus_default_get_flags(const u32 *addr, unsigned long flags) | ||
135 | { | ||
136 | if (flags) | ||
137 | return flags; | ||
138 | return IORESOURCE_MEM; | ||
139 | } | ||
140 | |||
141 | /* | ||
142 | * SBUS bus specific translator | ||
143 | */ | ||
144 | |||
145 | int of_bus_sbus_match(struct device_node *np) | ||
146 | { | ||
147 | struct device_node *dp = np; | ||
148 | |||
149 | while (dp) { | ||
150 | if (!strcmp(dp->name, "sbus") || | ||
151 | !strcmp(dp->name, "sbi")) | ||
152 | return 1; | ||
153 | |||
154 | /* Have a look at use_1to1_mapping(). We're trying | ||
155 | * to match SBUS if that's the top-level bus and we | ||
156 | * don't have some intervening real bus that provides | ||
157 | * ranges based translations. | ||
158 | */ | ||
159 | if (of_find_property(dp, "ranges", NULL) != NULL) | ||
160 | break; | ||
161 | |||
162 | dp = dp->parent; | ||
163 | } | ||
164 | |||
165 | return 0; | ||
166 | } | ||
167 | |||
168 | void of_bus_sbus_count_cells(struct device_node *child, int *addrc, int *sizec) | ||
169 | { | ||
170 | if (addrc) | ||
171 | *addrc = 2; | ||
172 | if (sizec) | ||
173 | *sizec = 1; | ||
174 | } | ||
diff --git a/arch/sparc/kernel/of_device_common.h b/arch/sparc/kernel/of_device_common.h new file mode 100644 index 000000000000..cdfd23992841 --- /dev/null +++ b/arch/sparc/kernel/of_device_common.h | |||
@@ -0,0 +1,36 @@ | |||
1 | #ifndef _OF_DEVICE_COMMON_H | ||
2 | #define _OF_DEVICE_COMMON_H | ||
3 | |||
4 | static inline u64 of_read_addr(const u32 *cell, int size) | ||
5 | { | ||
6 | u64 r = 0; | ||
7 | while (size--) | ||
8 | r = (r << 32) | *(cell++); | ||
9 | return r; | ||
10 | } | ||
11 | |||
12 | void of_bus_default_count_cells(struct device_node *dev, int *addrc, | ||
13 | int *sizec); | ||
14 | int of_out_of_range(const u32 *addr, const u32 *base, | ||
15 | const u32 *size, int na, int ns); | ||
16 | int of_bus_default_map(u32 *addr, const u32 *range, int na, int ns, int pna); | ||
17 | unsigned long of_bus_default_get_flags(const u32 *addr, unsigned long flags); | ||
18 | |||
19 | int of_bus_sbus_match(struct device_node *np); | ||
20 | void of_bus_sbus_count_cells(struct device_node *child, int *addrc, int *sizec); | ||
21 | |||
22 | /* Max address size we deal with */ | ||
23 | #define OF_MAX_ADDR_CELLS 4 | ||
24 | |||
25 | struct of_bus { | ||
26 | const char *name; | ||
27 | const char *addr_prop_name; | ||
28 | int (*match)(struct device_node *parent); | ||
29 | void (*count_cells)(struct device_node *child, | ||
30 | int *addrc, int *sizec); | ||
31 | int (*map)(u32 *addr, const u32 *range, | ||
32 | int na, int ns, int pna); | ||
33 | unsigned long (*get_flags)(const u32 *addr, unsigned long); | ||
34 | }; | ||
35 | |||
36 | #endif /* _OF_DEVICE_COMMON_H */ | ||