Commit | Line | Data |
---|---|---|
a2bd4fd1 DM |
1 | #include <linux/string.h> |
2 | #include <linux/kernel.h> | |
f85ff305 | 3 | #include <linux/of.h> |
a2bd4fd1 DM |
4 | #include <linux/init.h> |
5 | #include <linux/module.h> | |
6 | #include <linux/mod_devicetable.h> | |
7 | #include <linux/slab.h> | |
3f23de10 | 8 | #include <linux/errno.h> |
c1b1a5f1 | 9 | #include <linux/irq.h> |
3f23de10 SR |
10 | #include <linux/of_device.h> |
11 | #include <linux/of_platform.h> | |
a2bd4fd1 | 12 | |
3ca9fab4 DM |
13 | void __iomem *of_ioremap(struct resource *res, unsigned long offset, unsigned long size, char *name) |
14 | { | |
15 | unsigned long ret = res->start + offset; | |
6bda5736 | 16 | struct resource *r; |
3ca9fab4 | 17 | |
6bda5736 DM |
18 | if (res->flags & IORESOURCE_MEM) |
19 | r = request_mem_region(ret, size, name); | |
20 | else | |
21 | r = request_region(ret, size, name); | |
22 | if (!r) | |
3ca9fab4 DM |
23 | ret = 0; |
24 | ||
25 | return (void __iomem *) ret; | |
26 | } | |
27 | EXPORT_SYMBOL(of_ioremap); | |
28 | ||
e3a411a3 | 29 | void of_iounmap(struct resource *res, void __iomem *base, unsigned long size) |
3ca9fab4 | 30 | { |
e3a411a3 DM |
31 | if (res->flags & IORESOURCE_MEM) |
32 | release_mem_region((unsigned long) base, size); | |
33 | else | |
34 | release_region((unsigned long) base, size); | |
3ca9fab4 DM |
35 | } |
36 | EXPORT_SYMBOL(of_iounmap); | |
37 | ||
2b1e5978 DM |
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 | { | |
37b7754a | 48 | struct device *dev = bus_find_device(&of_platform_bus_type, NULL, |
2b1e5978 DM |
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 | ||
51e0f004 | 58 | unsigned int irq_of_parse_and_map(struct device_node *node, int index) |
44266215 DM |
59 | { |
60 | struct of_device *op = of_find_device_by_node(node); | |
61 | ||
62 | if (!op || index >= op->num_irqs) | |
51e0f004 | 63 | return 0; |
44266215 DM |
64 | |
65 | return op->irqs[index]; | |
66 | } | |
67 | EXPORT_SYMBOL(irq_of_parse_and_map); | |
68 | ||
5059625e DM |
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 | ||
3f23de10 | 91 | struct bus_type of_platform_bus_type; |
37b7754a | 92 | EXPORT_SYMBOL(of_platform_bus_type); |
cf44bbc2 | 93 | |
a83f9823 | 94 | static inline u64 of_read_addr(const u32 *cell, int size) |
cf44bbc2 DM |
95 | { |
96 | u64 r = 0; | |
97 | while (size--) | |
98 | r = (r << 32) | *(cell++); | |
99 | return r; | |
100 | } | |
101 | ||
102 | static void __init get_cells(struct device_node *dp, | |
103 | int *addrc, int *sizec) | |
104 | { | |
105 | if (addrc) | |
106 | *addrc = of_n_addr_cells(dp); | |
107 | if (sizec) | |
108 | *sizec = of_n_size_cells(dp); | |
109 | } | |
110 | ||
111 | /* Max address size we deal with */ | |
112 | #define OF_MAX_ADDR_CELLS 4 | |
113 | ||
114 | struct of_bus { | |
115 | const char *name; | |
116 | const char *addr_prop_name; | |
117 | int (*match)(struct device_node *parent); | |
118 | void (*count_cells)(struct device_node *child, | |
119 | int *addrc, int *sizec); | |
a83f9823 DM |
120 | int (*map)(u32 *addr, const u32 *range, |
121 | int na, int ns, int pna); | |
e3c71a32 | 122 | unsigned long (*get_flags)(const u32 *addr, unsigned long); |
cf44bbc2 DM |
123 | }; |
124 | ||
125 | /* | |
126 | * Default translator (generic bus) | |
127 | */ | |
128 | ||
129 | static void of_bus_default_count_cells(struct device_node *dev, | |
130 | int *addrc, int *sizec) | |
131 | { | |
132 | get_cells(dev, addrc, sizec); | |
133 | } | |
134 | ||
a83f9823 DM |
135 | /* Make sure the least significant 64-bits are in-range. Even |
136 | * for 3 or 4 cell values it is a good enough approximation. | |
137 | */ | |
138 | static int of_out_of_range(const u32 *addr, const u32 *base, | |
139 | const u32 *size, int na, int ns) | |
cf44bbc2 | 140 | { |
a83f9823 DM |
141 | u64 a = of_read_addr(addr, na); |
142 | u64 b = of_read_addr(base, na); | |
cf44bbc2 | 143 | |
a83f9823 DM |
144 | if (a < b) |
145 | return 1; | |
cf44bbc2 | 146 | |
a83f9823 DM |
147 | b += of_read_addr(size, ns); |
148 | if (a >= b) | |
149 | return 1; | |
150 | ||
151 | return 0; | |
cf44bbc2 DM |
152 | } |
153 | ||
a83f9823 DM |
154 | static int of_bus_default_map(u32 *addr, const u32 *range, |
155 | int na, int ns, int pna) | |
cf44bbc2 | 156 | { |
a83f9823 DM |
157 | u32 result[OF_MAX_ADDR_CELLS]; |
158 | int i; | |
159 | ||
160 | if (ns > 2) { | |
161 | printk("of_device: Cannot handle size cells (%d) > 2.", ns); | |
162 | return -EINVAL; | |
163 | } | |
164 | ||
165 | if (of_out_of_range(addr, range, range + na + pna, na, ns)) | |
166 | return -EINVAL; | |
167 | ||
168 | /* Start with the parent range base. */ | |
169 | memcpy(result, range + na, pna * 4); | |
170 | ||
171 | /* Add in the child address offset. */ | |
172 | for (i = 0; i < na; i++) | |
173 | result[pna - 1 - i] += | |
174 | (addr[na - 1 - i] - | |
175 | range[na - 1 - i]); | |
176 | ||
177 | memcpy(addr, result, pna * 4); | |
cf44bbc2 DM |
178 | |
179 | return 0; | |
180 | } | |
181 | ||
e3c71a32 | 182 | static unsigned long of_bus_default_get_flags(const u32 *addr, unsigned long flags) |
cf44bbc2 | 183 | { |
e3c71a32 DM |
184 | if (flags) |
185 | return flags; | |
cf44bbc2 DM |
186 | return IORESOURCE_MEM; |
187 | } | |
188 | ||
cf44bbc2 DM |
189 | /* |
190 | * PCI bus specific translator | |
191 | */ | |
192 | ||
193 | static int of_bus_pci_match(struct device_node *np) | |
194 | { | |
a83f9823 | 195 | if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) { |
a165b420 | 196 | const char *model = of_get_property(np, "model", NULL); |
01f94c4a DM |
197 | |
198 | if (model && !strcmp(model, "SUNW,simba")) | |
199 | return 0; | |
200 | ||
a83f9823 DM |
201 | /* Do not do PCI specific frobbing if the |
202 | * PCI bridge lacks a ranges property. We | |
203 | * want to pass it through up to the next | |
204 | * parent as-is, not with the PCI translate | |
205 | * method which chops off the top address cell. | |
206 | */ | |
207 | if (!of_find_property(np, "ranges", NULL)) | |
208 | return 0; | |
209 | ||
210 | return 1; | |
211 | } | |
212 | ||
213 | return 0; | |
cf44bbc2 DM |
214 | } |
215 | ||
01f94c4a DM |
216 | static int of_bus_simba_match(struct device_node *np) |
217 | { | |
a165b420 | 218 | const char *model = of_get_property(np, "model", NULL); |
01f94c4a DM |
219 | |
220 | if (model && !strcmp(model, "SUNW,simba")) | |
221 | return 1; | |
8c2786cf DM |
222 | |
223 | /* Treat PCI busses lacking ranges property just like | |
224 | * simba. | |
225 | */ | |
226 | if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) { | |
227 | if (!of_find_property(np, "ranges", NULL)) | |
228 | return 1; | |
229 | } | |
230 | ||
01f94c4a DM |
231 | return 0; |
232 | } | |
233 | ||
234 | static int of_bus_simba_map(u32 *addr, const u32 *range, | |
235 | int na, int ns, int pna) | |
236 | { | |
237 | return 0; | |
238 | } | |
239 | ||
cf44bbc2 DM |
240 | static void of_bus_pci_count_cells(struct device_node *np, |
241 | int *addrc, int *sizec) | |
242 | { | |
243 | if (addrc) | |
244 | *addrc = 3; | |
245 | if (sizec) | |
246 | *sizec = 2; | |
247 | } | |
248 | ||
a83f9823 DM |
249 | static int of_bus_pci_map(u32 *addr, const u32 *range, |
250 | int na, int ns, int pna) | |
cf44bbc2 | 251 | { |
a83f9823 DM |
252 | u32 result[OF_MAX_ADDR_CELLS]; |
253 | int i; | |
cf44bbc2 DM |
254 | |
255 | /* Check address type match */ | |
256 | if ((addr[0] ^ range[0]) & 0x03000000) | |
a83f9823 | 257 | return -EINVAL; |
cf44bbc2 | 258 | |
a83f9823 DM |
259 | if (of_out_of_range(addr + 1, range + 1, range + na + pna, |
260 | na - 1, ns)) | |
261 | return -EINVAL; | |
cf44bbc2 | 262 | |
a83f9823 DM |
263 | /* Start with the parent range base. */ |
264 | memcpy(result, range + na, pna * 4); | |
cf44bbc2 | 265 | |
a83f9823 DM |
266 | /* Add in the child address offset, skipping high cell. */ |
267 | for (i = 0; i < na - 1; i++) | |
268 | result[pna - 1 - i] += | |
269 | (addr[na - 1 - i] - | |
270 | range[na - 1 - i]); | |
271 | ||
272 | memcpy(addr, result, pna * 4); | |
273 | ||
274 | return 0; | |
cf44bbc2 DM |
275 | } |
276 | ||
e3c71a32 | 277 | static unsigned long of_bus_pci_get_flags(const u32 *addr, unsigned long flags) |
cf44bbc2 | 278 | { |
cf44bbc2 DM |
279 | u32 w = addr[0]; |
280 | ||
e3c71a32 DM |
281 | /* For PCI, we override whatever child busses may have used. */ |
282 | flags = 0; | |
cf44bbc2 DM |
283 | switch((w >> 24) & 0x03) { |
284 | case 0x01: | |
285 | flags |= IORESOURCE_IO; | |
e3c71a32 DM |
286 | break; |
287 | ||
cf44bbc2 DM |
288 | case 0x02: /* 32 bits */ |
289 | case 0x03: /* 64 bits */ | |
290 | flags |= IORESOURCE_MEM; | |
e3c71a32 | 291 | break; |
cf44bbc2 DM |
292 | } |
293 | if (w & 0x40000000) | |
294 | flags |= IORESOURCE_PREFETCH; | |
295 | return flags; | |
296 | } | |
297 | ||
cf44bbc2 DM |
298 | /* |
299 | * SBUS bus specific translator | |
300 | */ | |
301 | ||
302 | static int of_bus_sbus_match(struct device_node *np) | |
303 | { | |
304 | return !strcmp(np->name, "sbus") || | |
305 | !strcmp(np->name, "sbi"); | |
306 | } | |
307 | ||
308 | static void of_bus_sbus_count_cells(struct device_node *child, | |
309 | int *addrc, int *sizec) | |
310 | { | |
311 | if (addrc) | |
312 | *addrc = 2; | |
313 | if (sizec) | |
314 | *sizec = 1; | |
315 | } | |
316 | ||
4130a4b2 DM |
317 | /* |
318 | * FHC/Central bus specific translator. | |
319 | * | |
320 | * This is just needed to hard-code the address and size cell | |
321 | * counts. 'fhc' and 'central' nodes lack the #address-cells and | |
322 | * #size-cells properties, and if you walk to the root on such | |
323 | * Enterprise boxes all you'll get is a #size-cells of 2 which is | |
324 | * not what we want to use. | |
325 | */ | |
326 | static int of_bus_fhc_match(struct device_node *np) | |
cf44bbc2 | 327 | { |
4130a4b2 DM |
328 | return !strcmp(np->name, "fhc") || |
329 | !strcmp(np->name, "central"); | |
cf44bbc2 DM |
330 | } |
331 | ||
4130a4b2 | 332 | #define of_bus_fhc_count_cells of_bus_sbus_count_cells |
cf44bbc2 DM |
333 | |
334 | /* | |
335 | * Array of bus specific translators | |
336 | */ | |
337 | ||
338 | static struct of_bus of_busses[] = { | |
339 | /* PCI */ | |
340 | { | |
341 | .name = "pci", | |
342 | .addr_prop_name = "assigned-addresses", | |
343 | .match = of_bus_pci_match, | |
344 | .count_cells = of_bus_pci_count_cells, | |
345 | .map = of_bus_pci_map, | |
cf44bbc2 DM |
346 | .get_flags = of_bus_pci_get_flags, |
347 | }, | |
01f94c4a DM |
348 | /* SIMBA */ |
349 | { | |
350 | .name = "simba", | |
351 | .addr_prop_name = "assigned-addresses", | |
352 | .match = of_bus_simba_match, | |
353 | .count_cells = of_bus_pci_count_cells, | |
354 | .map = of_bus_simba_map, | |
355 | .get_flags = of_bus_pci_get_flags, | |
356 | }, | |
cf44bbc2 DM |
357 | /* SBUS */ |
358 | { | |
359 | .name = "sbus", | |
360 | .addr_prop_name = "reg", | |
361 | .match = of_bus_sbus_match, | |
362 | .count_cells = of_bus_sbus_count_cells, | |
4130a4b2 DM |
363 | .map = of_bus_default_map, |
364 | .get_flags = of_bus_default_get_flags, | |
365 | }, | |
366 | /* FHC */ | |
367 | { | |
368 | .name = "fhc", | |
369 | .addr_prop_name = "reg", | |
370 | .match = of_bus_fhc_match, | |
371 | .count_cells = of_bus_fhc_count_cells, | |
372 | .map = of_bus_default_map, | |
373 | .get_flags = of_bus_default_get_flags, | |
cf44bbc2 DM |
374 | }, |
375 | /* Default */ | |
376 | { | |
377 | .name = "default", | |
378 | .addr_prop_name = "reg", | |
379 | .match = NULL, | |
380 | .count_cells = of_bus_default_count_cells, | |
381 | .map = of_bus_default_map, | |
cf44bbc2 DM |
382 | .get_flags = of_bus_default_get_flags, |
383 | }, | |
384 | }; | |
385 | ||
386 | static struct of_bus *of_match_bus(struct device_node *np) | |
387 | { | |
388 | int i; | |
389 | ||
390 | for (i = 0; i < ARRAY_SIZE(of_busses); i ++) | |
391 | if (!of_busses[i].match || of_busses[i].match(np)) | |
392 | return &of_busses[i]; | |
393 | BUG(); | |
394 | return NULL; | |
395 | } | |
396 | ||
397 | static int __init build_one_resource(struct device_node *parent, | |
398 | struct of_bus *bus, | |
399 | struct of_bus *pbus, | |
400 | u32 *addr, | |
401 | int na, int ns, int pna) | |
402 | { | |
6a23acf3 | 403 | const u32 *ranges; |
cf44bbc2 DM |
404 | unsigned int rlen; |
405 | int rone; | |
cf44bbc2 DM |
406 | |
407 | ranges = of_get_property(parent, "ranges", &rlen); | |
408 | if (ranges == NULL || rlen == 0) { | |
a83f9823 DM |
409 | u32 result[OF_MAX_ADDR_CELLS]; |
410 | int i; | |
411 | ||
412 | memset(result, 0, pna * 4); | |
413 | for (i = 0; i < na; i++) | |
414 | result[pna - 1 - i] = | |
415 | addr[na - 1 - i]; | |
416 | ||
417 | memcpy(addr, result, pna * 4); | |
418 | return 0; | |
cf44bbc2 DM |
419 | } |
420 | ||
421 | /* Now walk through the ranges */ | |
422 | rlen /= 4; | |
423 | rone = na + pna + ns; | |
424 | for (; rlen >= rone; rlen -= rone, ranges += rone) { | |
a83f9823 DM |
425 | if (!bus->map(addr, ranges, na, ns, pna)) |
426 | return 0; | |
cf44bbc2 | 427 | } |
a83f9823 | 428 | |
49d23cfc DM |
429 | /* When we miss an I/O space match on PCI, just pass it up |
430 | * to the next PCI bridge and/or controller. | |
431 | */ | |
432 | if (!strcmp(bus->name, "pci") && | |
433 | (addr[0] & 0x03000000) == 0x01000000) | |
434 | return 0; | |
435 | ||
a83f9823 DM |
436 | return 1; |
437 | } | |
438 | ||
439 | static int __init use_1to1_mapping(struct device_node *pp) | |
440 | { | |
a83f9823 DM |
441 | /* If we have a ranges property in the parent, use it. */ |
442 | if (of_find_property(pp, "ranges", NULL) != NULL) | |
443 | return 0; | |
cf44bbc2 | 444 | |
a83f9823 DM |
445 | /* If the parent is the dma node of an ISA bus, pass |
446 | * the translation up to the root. | |
5280267c DM |
447 | * |
448 | * Some SBUS devices use intermediate nodes to express | |
449 | * hierarchy within the device itself. These aren't | |
450 | * real bus nodes, and don't have a 'ranges' property. | |
451 | * But, we should still pass the translation work up | |
452 | * to the SBUS itself. | |
a83f9823 | 453 | */ |
5280267c DM |
454 | if (!strcmp(pp->name, "dma") || |
455 | !strcmp(pp->name, "espdma") || | |
456 | !strcmp(pp->name, "ledma") || | |
457 | !strcmp(pp->name, "lebuffer")) | |
a83f9823 DM |
458 | return 0; |
459 | ||
8c2786cf DM |
460 | /* Similarly for all PCI bridges, if we get this far |
461 | * it lacks a ranges property, and this will include | |
462 | * cases like Simba. | |
463 | */ | |
464 | if (!strcmp(pp->type, "pci") || !strcmp(pp->type, "pciex")) | |
a83f9823 DM |
465 | return 0; |
466 | ||
467 | return 1; | |
cf44bbc2 DM |
468 | } |
469 | ||
a83f9823 DM |
470 | static int of_resource_verbose; |
471 | ||
cf44bbc2 DM |
472 | static void __init build_device_resources(struct of_device *op, |
473 | struct device *parent) | |
474 | { | |
475 | struct of_device *p_op; | |
476 | struct of_bus *bus; | |
477 | int na, ns; | |
478 | int index, num_reg; | |
6a23acf3 | 479 | const void *preg; |
cf44bbc2 DM |
480 | |
481 | if (!parent) | |
482 | return; | |
483 | ||
484 | p_op = to_of_device(parent); | |
485 | bus = of_match_bus(p_op->node); | |
486 | bus->count_cells(op->node, &na, &ns); | |
487 | ||
488 | preg = of_get_property(op->node, bus->addr_prop_name, &num_reg); | |
489 | if (!preg || num_reg == 0) | |
490 | return; | |
491 | ||
492 | /* Convert to num-cells. */ | |
493 | num_reg /= 4; | |
494 | ||
46ba6d7d | 495 | /* Convert to num-entries. */ |
cf44bbc2 DM |
496 | num_reg /= na + ns; |
497 | ||
e5dd42e4 | 498 | /* Prevent overrunning the op->resources[] array. */ |
46ba6d7d DM |
499 | if (num_reg > PROMREG_MAX) { |
500 | printk(KERN_WARNING "%s: Too many regs (%d), " | |
501 | "limiting to %d.\n", | |
502 | op->node->full_name, num_reg, PROMREG_MAX); | |
503 | num_reg = PROMREG_MAX; | |
504 | } | |
505 | ||
cf44bbc2 DM |
506 | for (index = 0; index < num_reg; index++) { |
507 | struct resource *r = &op->resource[index]; | |
508 | u32 addr[OF_MAX_ADDR_CELLS]; | |
6a23acf3 | 509 | const u32 *reg = (preg + (index * ((na + ns) * 4))); |
cf44bbc2 DM |
510 | struct device_node *dp = op->node; |
511 | struct device_node *pp = p_op->node; | |
b85cdd49 | 512 | struct of_bus *pbus, *dbus; |
cf44bbc2 DM |
513 | u64 size, result = OF_BAD_ADDR; |
514 | unsigned long flags; | |
515 | int dna, dns; | |
516 | int pna, pns; | |
517 | ||
518 | size = of_read_addr(reg + na, ns); | |
cf44bbc2 DM |
519 | memcpy(addr, reg, na * 4); |
520 | ||
e3c71a32 DM |
521 | flags = bus->get_flags(addr, 0); |
522 | ||
a83f9823 | 523 | if (use_1to1_mapping(pp)) { |
cf44bbc2 DM |
524 | result = of_read_addr(addr, na); |
525 | goto build_res; | |
526 | } | |
527 | ||
528 | dna = na; | |
529 | dns = ns; | |
b85cdd49 | 530 | dbus = bus; |
cf44bbc2 DM |
531 | |
532 | while (1) { | |
533 | dp = pp; | |
534 | pp = dp->parent; | |
535 | if (!pp) { | |
536 | result = of_read_addr(addr, dna); | |
537 | break; | |
538 | } | |
539 | ||
540 | pbus = of_match_bus(pp); | |
541 | pbus->count_cells(dp, &pna, &pns); | |
542 | ||
b85cdd49 | 543 | if (build_one_resource(dp, dbus, pbus, addr, |
a83f9823 | 544 | dna, dns, pna)) |
cf44bbc2 DM |
545 | break; |
546 | ||
e3c71a32 DM |
547 | flags = pbus->get_flags(addr, flags); |
548 | ||
cf44bbc2 DM |
549 | dna = pna; |
550 | dns = pns; | |
b85cdd49 | 551 | dbus = pbus; |
cf44bbc2 DM |
552 | } |
553 | ||
554 | build_res: | |
555 | memset(r, 0, sizeof(*r)); | |
a83f9823 DM |
556 | |
557 | if (of_resource_verbose) | |
558 | printk("%s reg[%d] -> %lx\n", | |
559 | op->node->full_name, index, | |
560 | result); | |
561 | ||
cf44bbc2 | 562 | if (result != OF_BAD_ADDR) { |
1815aed5 DM |
563 | if (tlb_type == hypervisor) |
564 | result &= 0x0fffffffffffffffUL; | |
565 | ||
cf44bbc2 DM |
566 | r->start = result; |
567 | r->end = result + size - 1; | |
568 | r->flags = flags; | |
cf44bbc2 DM |
569 | } |
570 | r->name = op->node->name; | |
571 | } | |
572 | } | |
573 | ||
2b1e5978 DM |
574 | static struct device_node * __init |
575 | apply_interrupt_map(struct device_node *dp, struct device_node *pp, | |
6a23acf3 | 576 | const u32 *imap, int imlen, const u32 *imask, |
2b1e5978 DM |
577 | unsigned int *irq_p) |
578 | { | |
579 | struct device_node *cp; | |
580 | unsigned int irq = *irq_p; | |
581 | struct of_bus *bus; | |
582 | phandle handle; | |
6a23acf3 | 583 | const u32 *reg; |
2b1e5978 DM |
584 | int na, num_reg, i; |
585 | ||
586 | bus = of_match_bus(pp); | |
587 | bus->count_cells(dp, &na, NULL); | |
588 | ||
589 | reg = of_get_property(dp, "reg", &num_reg); | |
590 | if (!reg || !num_reg) | |
591 | return NULL; | |
592 | ||
593 | imlen /= ((na + 3) * 4); | |
594 | handle = 0; | |
595 | for (i = 0; i < imlen; i++) { | |
596 | int j; | |
597 | ||
598 | for (j = 0; j < na; j++) { | |
599 | if ((reg[j] & imask[j]) != imap[j]) | |
600 | goto next; | |
601 | } | |
602 | if (imap[na] == irq) { | |
603 | handle = imap[na + 1]; | |
604 | irq = imap[na + 2]; | |
605 | break; | |
606 | } | |
607 | ||
608 | next: | |
609 | imap += (na + 3); | |
610 | } | |
46ba6d7d DM |
611 | if (i == imlen) { |
612 | /* Psycho and Sabre PCI controllers can have 'interrupt-map' | |
613 | * properties that do not include the on-board device | |
614 | * interrupts. Instead, the device's 'interrupts' property | |
615 | * is already a fully specified INO value. | |
616 | * | |
617 | * Handle this by deciding that, if we didn't get a | |
618 | * match in the parent's 'interrupt-map', and the | |
619 | * parent is an IRQ translater, then use the parent as | |
620 | * our IRQ controller. | |
621 | */ | |
622 | if (pp->irq_trans) | |
623 | return pp; | |
624 | ||
2b1e5978 | 625 | return NULL; |
46ba6d7d | 626 | } |
2b1e5978 DM |
627 | |
628 | *irq_p = irq; | |
629 | cp = of_find_node_by_phandle(handle); | |
630 | ||
631 | return cp; | |
632 | } | |
633 | ||
634 | static unsigned int __init pci_irq_swizzle(struct device_node *dp, | |
635 | struct device_node *pp, | |
636 | unsigned int irq) | |
637 | { | |
6a23acf3 | 638 | const struct linux_prom_pci_registers *regs; |
bb4c18cb | 639 | unsigned int bus, devfn, slot, ret; |
2b1e5978 DM |
640 | |
641 | if (irq < 1 || irq > 4) | |
642 | return irq; | |
643 | ||
644 | regs = of_get_property(dp, "reg", NULL); | |
645 | if (!regs) | |
646 | return irq; | |
647 | ||
bb4c18cb | 648 | bus = (regs->phys_hi >> 16) & 0xff; |
2b1e5978 DM |
649 | devfn = (regs->phys_hi >> 8) & 0xff; |
650 | slot = (devfn >> 3) & 0x1f; | |
651 | ||
bb4c18cb DM |
652 | if (pp->irq_trans) { |
653 | /* Derived from Table 8-3, U2P User's Manual. This branch | |
654 | * is handling a PCI controller that lacks a proper set of | |
655 | * interrupt-map and interrupt-map-mask properties. The | |
656 | * Ultra-E450 is one example. | |
657 | * | |
658 | * The bit layout is BSSLL, where: | |
659 | * B: 0 on bus A, 1 on bus B | |
660 | * D: 2-bit slot number, derived from PCI device number as | |
661 | * (dev - 1) for bus A, or (dev - 2) for bus B | |
662 | * L: 2-bit line number | |
bb4c18cb DM |
663 | */ |
664 | if (bus & 0x80) { | |
665 | /* PBM-A */ | |
666 | bus = 0x00; | |
667 | slot = (slot - 1) << 2; | |
668 | } else { | |
669 | /* PBM-B */ | |
670 | bus = 0x10; | |
671 | slot = (slot - 2) << 2; | |
672 | } | |
673 | irq -= 1; | |
674 | ||
675 | ret = (bus | slot | irq); | |
676 | } else { | |
677 | /* Going through a PCI-PCI bridge that lacks a set of | |
678 | * interrupt-map and interrupt-map-mask properties. | |
679 | */ | |
680 | ret = ((irq - 1 + (slot & 3)) & 3) + 1; | |
681 | } | |
2b1e5978 DM |
682 | |
683 | return ret; | |
684 | } | |
685 | ||
a83f9823 DM |
686 | static int of_irq_verbose; |
687 | ||
2b1e5978 DM |
688 | static unsigned int __init build_one_device_irq(struct of_device *op, |
689 | struct device *parent, | |
690 | unsigned int irq) | |
691 | { | |
692 | struct device_node *dp = op->node; | |
693 | struct device_node *pp, *ip; | |
694 | unsigned int orig_irq = irq; | |
c1b1a5f1 | 695 | int nid; |
2b1e5978 DM |
696 | |
697 | if (irq == 0xffffffff) | |
698 | return irq; | |
699 | ||
700 | if (dp->irq_trans) { | |
701 | irq = dp->irq_trans->irq_build(dp, irq, | |
702 | dp->irq_trans->data); | |
a83f9823 DM |
703 | |
704 | if (of_irq_verbose) | |
705 | printk("%s: direct translate %x --> %x\n", | |
706 | dp->full_name, orig_irq, irq); | |
707 | ||
c1b1a5f1 | 708 | goto out; |
2b1e5978 DM |
709 | } |
710 | ||
711 | /* Something more complicated. Walk up to the root, applying | |
712 | * interrupt-map or bus specific translations, until we hit | |
713 | * an IRQ translator. | |
714 | * | |
715 | * If we hit a bus type or situation we cannot handle, we | |
716 | * stop and assume that the original IRQ number was in a | |
717 | * format which has special meaning to it's immediate parent. | |
718 | */ | |
719 | pp = dp->parent; | |
720 | ip = NULL; | |
721 | while (pp) { | |
6a23acf3 | 722 | const void *imap, *imsk; |
2b1e5978 DM |
723 | int imlen; |
724 | ||
725 | imap = of_get_property(pp, "interrupt-map", &imlen); | |
726 | imsk = of_get_property(pp, "interrupt-map-mask", NULL); | |
727 | if (imap && imsk) { | |
728 | struct device_node *iret; | |
729 | int this_orig_irq = irq; | |
730 | ||
731 | iret = apply_interrupt_map(dp, pp, | |
732 | imap, imlen, imsk, | |
733 | &irq); | |
a83f9823 DM |
734 | |
735 | if (of_irq_verbose) | |
736 | printk("%s: Apply [%s:%x] imap --> [%s:%x]\n", | |
737 | op->node->full_name, | |
738 | pp->full_name, this_orig_irq, | |
739 | (iret ? iret->full_name : "NULL"), irq); | |
740 | ||
2b1e5978 DM |
741 | if (!iret) |
742 | break; | |
743 | ||
744 | if (iret->irq_trans) { | |
745 | ip = iret; | |
746 | break; | |
747 | } | |
748 | } else { | |
749 | if (!strcmp(pp->type, "pci") || | |
750 | !strcmp(pp->type, "pciex")) { | |
751 | unsigned int this_orig_irq = irq; | |
752 | ||
753 | irq = pci_irq_swizzle(dp, pp, irq); | |
a83f9823 DM |
754 | if (of_irq_verbose) |
755 | printk("%s: PCI swizzle [%s] " | |
756 | "%x --> %x\n", | |
757 | op->node->full_name, | |
758 | pp->full_name, this_orig_irq, | |
759 | irq); | |
760 | ||
2b1e5978 DM |
761 | } |
762 | ||
763 | if (pp->irq_trans) { | |
764 | ip = pp; | |
765 | break; | |
766 | } | |
767 | } | |
768 | dp = pp; | |
769 | pp = pp->parent; | |
770 | } | |
771 | if (!ip) | |
772 | return orig_irq; | |
773 | ||
774 | irq = ip->irq_trans->irq_build(op->node, irq, | |
775 | ip->irq_trans->data); | |
a83f9823 DM |
776 | if (of_irq_verbose) |
777 | printk("%s: Apply IRQ trans [%s] %x --> %x\n", | |
778 | op->node->full_name, ip->full_name, orig_irq, irq); | |
2b1e5978 | 779 | |
c1b1a5f1 DM |
780 | out: |
781 | nid = of_node_to_nid(dp); | |
782 | if (nid != -1) { | |
783 | cpumask_t numa_mask = node_to_cpumask(nid); | |
784 | ||
785 | irq_set_affinity(irq, numa_mask); | |
786 | } | |
787 | ||
2b1e5978 DM |
788 | return irq; |
789 | } | |
790 | ||
cf44bbc2 DM |
791 | static struct of_device * __init scan_one_device(struct device_node *dp, |
792 | struct device *parent) | |
793 | { | |
794 | struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL); | |
6a23acf3 | 795 | const unsigned int *irq; |
3d6e4702 | 796 | struct dev_archdata *sd; |
2b1e5978 | 797 | int len, i; |
cf44bbc2 DM |
798 | |
799 | if (!op) | |
800 | return NULL; | |
801 | ||
3d6e4702 DM |
802 | sd = &op->dev.archdata; |
803 | sd->prom_node = dp; | |
804 | sd->op = op; | |
805 | ||
cf44bbc2 DM |
806 | op->node = dp; |
807 | ||
808 | op->clock_freq = of_getintprop_default(dp, "clock-frequency", | |
809 | (25*1000*1000)); | |
810 | op->portid = of_getintprop_default(dp, "upa-portid", -1); | |
811 | if (op->portid == -1) | |
812 | op->portid = of_getintprop_default(dp, "portid", -1); | |
813 | ||
814 | irq = of_get_property(dp, "interrupts", &len); | |
2b1e5978 DM |
815 | if (irq) { |
816 | memcpy(op->irqs, irq, len); | |
817 | op->num_irqs = len / 4; | |
818 | } else { | |
819 | op->num_irqs = 0; | |
820 | } | |
cf44bbc2 | 821 | |
e5dd42e4 | 822 | /* Prevent overrunning the op->irqs[] array. */ |
46ba6d7d DM |
823 | if (op->num_irqs > PROMINTR_MAX) { |
824 | printk(KERN_WARNING "%s: Too many irqs (%d), " | |
825 | "limiting to %d.\n", | |
826 | dp->full_name, op->num_irqs, PROMINTR_MAX); | |
827 | op->num_irqs = PROMINTR_MAX; | |
828 | } | |
829 | ||
cf44bbc2 | 830 | build_device_resources(op, parent); |
2b1e5978 DM |
831 | for (i = 0; i < op->num_irqs; i++) |
832 | op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]); | |
cf44bbc2 DM |
833 | |
834 | op->dev.parent = parent; | |
37b7754a | 835 | op->dev.bus = &of_platform_bus_type; |
cf44bbc2 | 836 | if (!parent) |
2222c313 | 837 | dev_set_name(&op->dev, "root"); |
cf44bbc2 | 838 | else |
2222c313 | 839 | dev_set_name(&op->dev, "%08x", dp->node); |
cf44bbc2 DM |
840 | |
841 | if (of_device_register(op)) { | |
842 | printk("%s: Could not register of device.\n", | |
843 | dp->full_name); | |
844 | kfree(op); | |
845 | op = NULL; | |
846 | } | |
847 | ||
848 | return op; | |
849 | } | |
850 | ||
851 | static void __init scan_tree(struct device_node *dp, struct device *parent) | |
852 | { | |
853 | while (dp) { | |
854 | struct of_device *op = scan_one_device(dp, parent); | |
855 | ||
856 | if (op) | |
857 | scan_tree(dp->child, &op->dev); | |
858 | ||
859 | dp = dp->sibling; | |
860 | } | |
861 | } | |
862 | ||
863 | static void __init scan_of_devices(void) | |
864 | { | |
865 | struct device_node *root = of_find_node_by_path("/"); | |
866 | struct of_device *parent; | |
867 | ||
868 | parent = scan_one_device(root, NULL); | |
869 | if (!parent) | |
870 | return; | |
871 | ||
872 | scan_tree(root->child, &parent->dev); | |
873 | } | |
874 | ||
a2bd4fd1 DM |
875 | static int __init of_bus_driver_init(void) |
876 | { | |
cf44bbc2 | 877 | int err; |
a2bd4fd1 | 878 | |
3f23de10 | 879 | err = of_bus_type_init(&of_platform_bus_type, "of"); |
cf44bbc2 DM |
880 | if (!err) |
881 | scan_of_devices(); | |
882 | ||
883 | return err; | |
a2bd4fd1 DM |
884 | } |
885 | ||
886 | postcore_initcall(of_bus_driver_init); | |
887 | ||
a83f9823 DM |
888 | static int __init of_debug(char *str) |
889 | { | |
890 | int val = 0; | |
891 | ||
892 | get_option(&str, &val); | |
893 | if (val & 1) | |
894 | of_resource_verbose = 1; | |
895 | if (val & 2) | |
896 | of_irq_verbose = 1; | |
897 | return 1; | |
898 | } | |
899 | ||
900 | __setup("of_debug=", of_debug); |