]>
Commit | Line | Data |
---|---|---|
a2bd4fd1 DM |
1 | #include <linux/string.h> |
2 | #include <linux/kernel.h> | |
3 | #include <linux/init.h> | |
4 | #include <linux/module.h> | |
5 | #include <linux/mod_devicetable.h> | |
6 | #include <linux/slab.h> | |
7 | ||
8 | #include <asm/errno.h> | |
9 | #include <asm/of_device.h> | |
10 | ||
11 | /** | |
12 | * of_match_device - Tell if an of_device structure has a matching | |
13 | * of_match structure | |
14 | * @ids: array of of device match structures to search in | |
15 | * @dev: the of device structure to match against | |
16 | * | |
17 | * Used by a driver to check whether an of_device present in the | |
18 | * system is in its list of supported devices. | |
19 | */ | |
20 | const struct of_device_id *of_match_device(const struct of_device_id *matches, | |
21 | const struct of_device *dev) | |
22 | { | |
23 | if (!dev->node) | |
24 | return NULL; | |
25 | while (matches->name[0] || matches->type[0] || matches->compatible[0]) { | |
26 | int match = 1; | |
27 | if (matches->name[0]) | |
28 | match &= dev->node->name | |
29 | && !strcmp(matches->name, dev->node->name); | |
30 | if (matches->type[0]) | |
31 | match &= dev->node->type | |
32 | && !strcmp(matches->type, dev->node->type); | |
33 | if (matches->compatible[0]) | |
34 | match &= of_device_is_compatible(dev->node, | |
35 | matches->compatible); | |
36 | if (match) | |
37 | return matches; | |
38 | matches++; | |
39 | } | |
40 | return NULL; | |
41 | } | |
42 | ||
43 | static int of_platform_bus_match(struct device *dev, struct device_driver *drv) | |
44 | { | |
45 | struct of_device * of_dev = to_of_device(dev); | |
46 | struct of_platform_driver * of_drv = to_of_platform_driver(drv); | |
47 | const struct of_device_id * matches = of_drv->match_table; | |
48 | ||
49 | if (!matches) | |
50 | return 0; | |
51 | ||
52 | return of_match_device(matches, of_dev) != NULL; | |
53 | } | |
54 | ||
55 | struct of_device *of_dev_get(struct of_device *dev) | |
56 | { | |
57 | struct device *tmp; | |
58 | ||
59 | if (!dev) | |
60 | return NULL; | |
61 | tmp = get_device(&dev->dev); | |
62 | if (tmp) | |
63 | return to_of_device(tmp); | |
64 | else | |
65 | return NULL; | |
66 | } | |
67 | ||
68 | void of_dev_put(struct of_device *dev) | |
69 | { | |
70 | if (dev) | |
71 | put_device(&dev->dev); | |
72 | } | |
73 | ||
74 | ||
75 | static int of_device_probe(struct device *dev) | |
76 | { | |
77 | int error = -ENODEV; | |
78 | struct of_platform_driver *drv; | |
79 | struct of_device *of_dev; | |
80 | const struct of_device_id *match; | |
81 | ||
82 | drv = to_of_platform_driver(dev->driver); | |
83 | of_dev = to_of_device(dev); | |
84 | ||
85 | if (!drv->probe) | |
86 | return error; | |
87 | ||
88 | of_dev_get(of_dev); | |
89 | ||
90 | match = of_match_device(drv->match_table, of_dev); | |
91 | if (match) | |
92 | error = drv->probe(of_dev, match); | |
93 | if (error) | |
94 | of_dev_put(of_dev); | |
95 | ||
96 | return error; | |
97 | } | |
98 | ||
99 | static int of_device_remove(struct device *dev) | |
100 | { | |
101 | struct of_device * of_dev = to_of_device(dev); | |
102 | struct of_platform_driver * drv = to_of_platform_driver(dev->driver); | |
103 | ||
104 | if (dev->driver && drv->remove) | |
105 | drv->remove(of_dev); | |
106 | return 0; | |
107 | } | |
108 | ||
109 | static int of_device_suspend(struct device *dev, pm_message_t state) | |
110 | { | |
111 | struct of_device * of_dev = to_of_device(dev); | |
112 | struct of_platform_driver * drv = to_of_platform_driver(dev->driver); | |
113 | int error = 0; | |
114 | ||
115 | if (dev->driver && drv->suspend) | |
116 | error = drv->suspend(of_dev, state); | |
117 | return error; | |
118 | } | |
119 | ||
120 | static int of_device_resume(struct device * dev) | |
121 | { | |
122 | struct of_device * of_dev = to_of_device(dev); | |
123 | struct of_platform_driver * drv = to_of_platform_driver(dev->driver); | |
124 | int error = 0; | |
125 | ||
126 | if (dev->driver && drv->resume) | |
127 | error = drv->resume(of_dev); | |
128 | return error; | |
129 | } | |
130 | ||
3ca9fab4 DM |
131 | void __iomem *of_ioremap(struct resource *res, unsigned long offset, unsigned long size, char *name) |
132 | { | |
133 | unsigned long ret = res->start + offset; | |
6bda5736 | 134 | struct resource *r; |
3ca9fab4 | 135 | |
6bda5736 DM |
136 | if (res->flags & IORESOURCE_MEM) |
137 | r = request_mem_region(ret, size, name); | |
138 | else | |
139 | r = request_region(ret, size, name); | |
140 | if (!r) | |
3ca9fab4 DM |
141 | ret = 0; |
142 | ||
143 | return (void __iomem *) ret; | |
144 | } | |
145 | EXPORT_SYMBOL(of_ioremap); | |
146 | ||
e3a411a3 | 147 | void of_iounmap(struct resource *res, void __iomem *base, unsigned long size) |
3ca9fab4 | 148 | { |
e3a411a3 DM |
149 | if (res->flags & IORESOURCE_MEM) |
150 | release_mem_region((unsigned long) base, size); | |
151 | else | |
152 | release_region((unsigned long) base, size); | |
3ca9fab4 DM |
153 | } |
154 | EXPORT_SYMBOL(of_iounmap); | |
155 | ||
2b1e5978 DM |
156 | static int node_match(struct device *dev, void *data) |
157 | { | |
158 | struct of_device *op = to_of_device(dev); | |
159 | struct device_node *dp = data; | |
160 | ||
161 | return (op->node == dp); | |
162 | } | |
163 | ||
164 | struct of_device *of_find_device_by_node(struct device_node *dp) | |
165 | { | |
166 | struct device *dev = bus_find_device(&of_bus_type, NULL, | |
167 | dp, node_match); | |
168 | ||
169 | if (dev) | |
170 | return to_of_device(dev); | |
171 | ||
172 | return NULL; | |
173 | } | |
174 | EXPORT_SYMBOL(of_find_device_by_node); | |
175 | ||
a2bd4fd1 DM |
176 | #ifdef CONFIG_PCI |
177 | struct bus_type isa_bus_type = { | |
178 | .name = "isa", | |
179 | .match = of_platform_bus_match, | |
180 | .probe = of_device_probe, | |
181 | .remove = of_device_remove, | |
182 | .suspend = of_device_suspend, | |
183 | .resume = of_device_resume, | |
184 | }; | |
69853918 | 185 | EXPORT_SYMBOL(isa_bus_type); |
a2bd4fd1 DM |
186 | |
187 | struct bus_type ebus_bus_type = { | |
188 | .name = "ebus", | |
189 | .match = of_platform_bus_match, | |
190 | .probe = of_device_probe, | |
191 | .remove = of_device_remove, | |
192 | .suspend = of_device_suspend, | |
193 | .resume = of_device_resume, | |
194 | }; | |
69853918 | 195 | EXPORT_SYMBOL(ebus_bus_type); |
a2bd4fd1 DM |
196 | #endif |
197 | ||
198 | #ifdef CONFIG_SBUS | |
199 | struct bus_type sbus_bus_type = { | |
200 | .name = "sbus", | |
201 | .match = of_platform_bus_match, | |
202 | .probe = of_device_probe, | |
203 | .remove = of_device_remove, | |
204 | .suspend = of_device_suspend, | |
205 | .resume = of_device_resume, | |
206 | }; | |
69853918 | 207 | EXPORT_SYMBOL(sbus_bus_type); |
a2bd4fd1 DM |
208 | #endif |
209 | ||
cf44bbc2 DM |
210 | struct bus_type of_bus_type = { |
211 | .name = "of", | |
212 | .match = of_platform_bus_match, | |
213 | .probe = of_device_probe, | |
214 | .remove = of_device_remove, | |
215 | .suspend = of_device_suspend, | |
216 | .resume = of_device_resume, | |
217 | }; | |
218 | EXPORT_SYMBOL(of_bus_type); | |
219 | ||
a83f9823 | 220 | static inline u64 of_read_addr(const u32 *cell, int size) |
cf44bbc2 DM |
221 | { |
222 | u64 r = 0; | |
223 | while (size--) | |
224 | r = (r << 32) | *(cell++); | |
225 | return r; | |
226 | } | |
227 | ||
228 | static void __init get_cells(struct device_node *dp, | |
229 | int *addrc, int *sizec) | |
230 | { | |
231 | if (addrc) | |
232 | *addrc = of_n_addr_cells(dp); | |
233 | if (sizec) | |
234 | *sizec = of_n_size_cells(dp); | |
235 | } | |
236 | ||
237 | /* Max address size we deal with */ | |
238 | #define OF_MAX_ADDR_CELLS 4 | |
239 | ||
240 | struct of_bus { | |
241 | const char *name; | |
242 | const char *addr_prop_name; | |
243 | int (*match)(struct device_node *parent); | |
244 | void (*count_cells)(struct device_node *child, | |
245 | int *addrc, int *sizec); | |
a83f9823 DM |
246 | int (*map)(u32 *addr, const u32 *range, |
247 | int na, int ns, int pna); | |
6a23acf3 | 248 | unsigned int (*get_flags)(const u32 *addr); |
cf44bbc2 DM |
249 | }; |
250 | ||
251 | /* | |
252 | * Default translator (generic bus) | |
253 | */ | |
254 | ||
255 | static void of_bus_default_count_cells(struct device_node *dev, | |
256 | int *addrc, int *sizec) | |
257 | { | |
258 | get_cells(dev, addrc, sizec); | |
259 | } | |
260 | ||
a83f9823 DM |
261 | /* Make sure the least significant 64-bits are in-range. Even |
262 | * for 3 or 4 cell values it is a good enough approximation. | |
263 | */ | |
264 | static int of_out_of_range(const u32 *addr, const u32 *base, | |
265 | const u32 *size, int na, int ns) | |
cf44bbc2 | 266 | { |
a83f9823 DM |
267 | u64 a = of_read_addr(addr, na); |
268 | u64 b = of_read_addr(base, na); | |
cf44bbc2 | 269 | |
a83f9823 DM |
270 | if (a < b) |
271 | return 1; | |
cf44bbc2 | 272 | |
a83f9823 DM |
273 | b += of_read_addr(size, ns); |
274 | if (a >= b) | |
275 | return 1; | |
276 | ||
277 | return 0; | |
cf44bbc2 DM |
278 | } |
279 | ||
a83f9823 DM |
280 | static int of_bus_default_map(u32 *addr, const u32 *range, |
281 | int na, int ns, int pna) | |
cf44bbc2 | 282 | { |
a83f9823 DM |
283 | u32 result[OF_MAX_ADDR_CELLS]; |
284 | int i; | |
285 | ||
286 | if (ns > 2) { | |
287 | printk("of_device: Cannot handle size cells (%d) > 2.", ns); | |
288 | return -EINVAL; | |
289 | } | |
290 | ||
291 | if (of_out_of_range(addr, range, range + na + pna, na, ns)) | |
292 | return -EINVAL; | |
293 | ||
294 | /* Start with the parent range base. */ | |
295 | memcpy(result, range + na, pna * 4); | |
296 | ||
297 | /* Add in the child address offset. */ | |
298 | for (i = 0; i < na; i++) | |
299 | result[pna - 1 - i] += | |
300 | (addr[na - 1 - i] - | |
301 | range[na - 1 - i]); | |
302 | ||
303 | memcpy(addr, result, pna * 4); | |
cf44bbc2 DM |
304 | |
305 | return 0; | |
306 | } | |
307 | ||
6a23acf3 | 308 | static unsigned int of_bus_default_get_flags(const u32 *addr) |
cf44bbc2 DM |
309 | { |
310 | return IORESOURCE_MEM; | |
311 | } | |
312 | ||
cf44bbc2 DM |
313 | /* |
314 | * PCI bus specific translator | |
315 | */ | |
316 | ||
317 | static int of_bus_pci_match(struct device_node *np) | |
318 | { | |
a83f9823 | 319 | if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) { |
a165b420 | 320 | const char *model = of_get_property(np, "model", NULL); |
01f94c4a DM |
321 | |
322 | if (model && !strcmp(model, "SUNW,simba")) | |
323 | return 0; | |
324 | ||
a83f9823 DM |
325 | /* Do not do PCI specific frobbing if the |
326 | * PCI bridge lacks a ranges property. We | |
327 | * want to pass it through up to the next | |
328 | * parent as-is, not with the PCI translate | |
329 | * method which chops off the top address cell. | |
330 | */ | |
331 | if (!of_find_property(np, "ranges", NULL)) | |
332 | return 0; | |
333 | ||
334 | return 1; | |
335 | } | |
336 | ||
337 | return 0; | |
cf44bbc2 DM |
338 | } |
339 | ||
01f94c4a DM |
340 | static int of_bus_simba_match(struct device_node *np) |
341 | { | |
a165b420 | 342 | const char *model = of_get_property(np, "model", NULL); |
01f94c4a DM |
343 | |
344 | if (model && !strcmp(model, "SUNW,simba")) | |
345 | return 1; | |
8c2786cf DM |
346 | |
347 | /* Treat PCI busses lacking ranges property just like | |
348 | * simba. | |
349 | */ | |
350 | if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) { | |
351 | if (!of_find_property(np, "ranges", NULL)) | |
352 | return 1; | |
353 | } | |
354 | ||
01f94c4a DM |
355 | return 0; |
356 | } | |
357 | ||
358 | static int of_bus_simba_map(u32 *addr, const u32 *range, | |
359 | int na, int ns, int pna) | |
360 | { | |
361 | return 0; | |
362 | } | |
363 | ||
cf44bbc2 DM |
364 | static void of_bus_pci_count_cells(struct device_node *np, |
365 | int *addrc, int *sizec) | |
366 | { | |
367 | if (addrc) | |
368 | *addrc = 3; | |
369 | if (sizec) | |
370 | *sizec = 2; | |
371 | } | |
372 | ||
a83f9823 DM |
373 | static int of_bus_pci_map(u32 *addr, const u32 *range, |
374 | int na, int ns, int pna) | |
cf44bbc2 | 375 | { |
a83f9823 DM |
376 | u32 result[OF_MAX_ADDR_CELLS]; |
377 | int i; | |
cf44bbc2 DM |
378 | |
379 | /* Check address type match */ | |
380 | if ((addr[0] ^ range[0]) & 0x03000000) | |
a83f9823 | 381 | return -EINVAL; |
cf44bbc2 | 382 | |
a83f9823 DM |
383 | if (of_out_of_range(addr + 1, range + 1, range + na + pna, |
384 | na - 1, ns)) | |
385 | return -EINVAL; | |
cf44bbc2 | 386 | |
a83f9823 DM |
387 | /* Start with the parent range base. */ |
388 | memcpy(result, range + na, pna * 4); | |
cf44bbc2 | 389 | |
a83f9823 DM |
390 | /* Add in the child address offset, skipping high cell. */ |
391 | for (i = 0; i < na - 1; i++) | |
392 | result[pna - 1 - i] += | |
393 | (addr[na - 1 - i] - | |
394 | range[na - 1 - i]); | |
395 | ||
396 | memcpy(addr, result, pna * 4); | |
397 | ||
398 | return 0; | |
cf44bbc2 DM |
399 | } |
400 | ||
6a23acf3 | 401 | static unsigned int of_bus_pci_get_flags(const u32 *addr) |
cf44bbc2 DM |
402 | { |
403 | unsigned int flags = 0; | |
404 | u32 w = addr[0]; | |
405 | ||
406 | switch((w >> 24) & 0x03) { | |
407 | case 0x01: | |
408 | flags |= IORESOURCE_IO; | |
409 | case 0x02: /* 32 bits */ | |
410 | case 0x03: /* 64 bits */ | |
411 | flags |= IORESOURCE_MEM; | |
412 | } | |
413 | if (w & 0x40000000) | |
414 | flags |= IORESOURCE_PREFETCH; | |
415 | return flags; | |
416 | } | |
417 | ||
cf44bbc2 DM |
418 | /* |
419 | * SBUS bus specific translator | |
420 | */ | |
421 | ||
422 | static int of_bus_sbus_match(struct device_node *np) | |
423 | { | |
424 | return !strcmp(np->name, "sbus") || | |
425 | !strcmp(np->name, "sbi"); | |
426 | } | |
427 | ||
428 | static void of_bus_sbus_count_cells(struct device_node *child, | |
429 | int *addrc, int *sizec) | |
430 | { | |
431 | if (addrc) | |
432 | *addrc = 2; | |
433 | if (sizec) | |
434 | *sizec = 1; | |
435 | } | |
436 | ||
4130a4b2 DM |
437 | /* |
438 | * FHC/Central bus specific translator. | |
439 | * | |
440 | * This is just needed to hard-code the address and size cell | |
441 | * counts. 'fhc' and 'central' nodes lack the #address-cells and | |
442 | * #size-cells properties, and if you walk to the root on such | |
443 | * Enterprise boxes all you'll get is a #size-cells of 2 which is | |
444 | * not what we want to use. | |
445 | */ | |
446 | static int of_bus_fhc_match(struct device_node *np) | |
cf44bbc2 | 447 | { |
4130a4b2 DM |
448 | return !strcmp(np->name, "fhc") || |
449 | !strcmp(np->name, "central"); | |
cf44bbc2 DM |
450 | } |
451 | ||
4130a4b2 | 452 | #define of_bus_fhc_count_cells of_bus_sbus_count_cells |
cf44bbc2 DM |
453 | |
454 | /* | |
455 | * Array of bus specific translators | |
456 | */ | |
457 | ||
458 | static struct of_bus of_busses[] = { | |
459 | /* PCI */ | |
460 | { | |
461 | .name = "pci", | |
462 | .addr_prop_name = "assigned-addresses", | |
463 | .match = of_bus_pci_match, | |
464 | .count_cells = of_bus_pci_count_cells, | |
465 | .map = of_bus_pci_map, | |
cf44bbc2 DM |
466 | .get_flags = of_bus_pci_get_flags, |
467 | }, | |
01f94c4a DM |
468 | /* SIMBA */ |
469 | { | |
470 | .name = "simba", | |
471 | .addr_prop_name = "assigned-addresses", | |
472 | .match = of_bus_simba_match, | |
473 | .count_cells = of_bus_pci_count_cells, | |
474 | .map = of_bus_simba_map, | |
475 | .get_flags = of_bus_pci_get_flags, | |
476 | }, | |
cf44bbc2 DM |
477 | /* SBUS */ |
478 | { | |
479 | .name = "sbus", | |
480 | .addr_prop_name = "reg", | |
481 | .match = of_bus_sbus_match, | |
482 | .count_cells = of_bus_sbus_count_cells, | |
4130a4b2 DM |
483 | .map = of_bus_default_map, |
484 | .get_flags = of_bus_default_get_flags, | |
485 | }, | |
486 | /* FHC */ | |
487 | { | |
488 | .name = "fhc", | |
489 | .addr_prop_name = "reg", | |
490 | .match = of_bus_fhc_match, | |
491 | .count_cells = of_bus_fhc_count_cells, | |
492 | .map = of_bus_default_map, | |
493 | .get_flags = of_bus_default_get_flags, | |
cf44bbc2 DM |
494 | }, |
495 | /* Default */ | |
496 | { | |
497 | .name = "default", | |
498 | .addr_prop_name = "reg", | |
499 | .match = NULL, | |
500 | .count_cells = of_bus_default_count_cells, | |
501 | .map = of_bus_default_map, | |
cf44bbc2 DM |
502 | .get_flags = of_bus_default_get_flags, |
503 | }, | |
504 | }; | |
505 | ||
506 | static struct of_bus *of_match_bus(struct device_node *np) | |
507 | { | |
508 | int i; | |
509 | ||
510 | for (i = 0; i < ARRAY_SIZE(of_busses); i ++) | |
511 | if (!of_busses[i].match || of_busses[i].match(np)) | |
512 | return &of_busses[i]; | |
513 | BUG(); | |
514 | return NULL; | |
515 | } | |
516 | ||
517 | static int __init build_one_resource(struct device_node *parent, | |
518 | struct of_bus *bus, | |
519 | struct of_bus *pbus, | |
520 | u32 *addr, | |
521 | int na, int ns, int pna) | |
522 | { | |
6a23acf3 | 523 | const u32 *ranges; |
cf44bbc2 DM |
524 | unsigned int rlen; |
525 | int rone; | |
cf44bbc2 DM |
526 | |
527 | ranges = of_get_property(parent, "ranges", &rlen); | |
528 | if (ranges == NULL || rlen == 0) { | |
a83f9823 DM |
529 | u32 result[OF_MAX_ADDR_CELLS]; |
530 | int i; | |
531 | ||
532 | memset(result, 0, pna * 4); | |
533 | for (i = 0; i < na; i++) | |
534 | result[pna - 1 - i] = | |
535 | addr[na - 1 - i]; | |
536 | ||
537 | memcpy(addr, result, pna * 4); | |
538 | return 0; | |
cf44bbc2 DM |
539 | } |
540 | ||
541 | /* Now walk through the ranges */ | |
542 | rlen /= 4; | |
543 | rone = na + pna + ns; | |
544 | for (; rlen >= rone; rlen -= rone, ranges += rone) { | |
a83f9823 DM |
545 | if (!bus->map(addr, ranges, na, ns, pna)) |
546 | return 0; | |
cf44bbc2 | 547 | } |
a83f9823 | 548 | |
49d23cfc DM |
549 | /* When we miss an I/O space match on PCI, just pass it up |
550 | * to the next PCI bridge and/or controller. | |
551 | */ | |
552 | if (!strcmp(bus->name, "pci") && | |
553 | (addr[0] & 0x03000000) == 0x01000000) | |
554 | return 0; | |
555 | ||
a83f9823 DM |
556 | return 1; |
557 | } | |
558 | ||
559 | static int __init use_1to1_mapping(struct device_node *pp) | |
560 | { | |
a83f9823 DM |
561 | /* If this is on the PMU bus, don't try to translate it even |
562 | * if a ranges property exists. | |
563 | */ | |
564 | if (!strcmp(pp->name, "pmu")) | |
cf44bbc2 DM |
565 | return 1; |
566 | ||
a83f9823 DM |
567 | /* If we have a ranges property in the parent, use it. */ |
568 | if (of_find_property(pp, "ranges", NULL) != NULL) | |
569 | return 0; | |
cf44bbc2 | 570 | |
a83f9823 DM |
571 | /* If the parent is the dma node of an ISA bus, pass |
572 | * the translation up to the root. | |
573 | */ | |
574 | if (!strcmp(pp->name, "dma")) | |
575 | return 0; | |
576 | ||
8c2786cf DM |
577 | /* Similarly for all PCI bridges, if we get this far |
578 | * it lacks a ranges property, and this will include | |
579 | * cases like Simba. | |
580 | */ | |
581 | if (!strcmp(pp->type, "pci") || !strcmp(pp->type, "pciex")) | |
a83f9823 DM |
582 | return 0; |
583 | ||
584 | return 1; | |
cf44bbc2 DM |
585 | } |
586 | ||
a83f9823 DM |
587 | static int of_resource_verbose; |
588 | ||
cf44bbc2 DM |
589 | static void __init build_device_resources(struct of_device *op, |
590 | struct device *parent) | |
591 | { | |
592 | struct of_device *p_op; | |
593 | struct of_bus *bus; | |
594 | int na, ns; | |
595 | int index, num_reg; | |
6a23acf3 | 596 | const void *preg; |
cf44bbc2 DM |
597 | |
598 | if (!parent) | |
599 | return; | |
600 | ||
601 | p_op = to_of_device(parent); | |
602 | bus = of_match_bus(p_op->node); | |
603 | bus->count_cells(op->node, &na, &ns); | |
604 | ||
605 | preg = of_get_property(op->node, bus->addr_prop_name, &num_reg); | |
606 | if (!preg || num_reg == 0) | |
607 | return; | |
608 | ||
609 | /* Convert to num-cells. */ | |
610 | num_reg /= 4; | |
611 | ||
46ba6d7d | 612 | /* Convert to num-entries. */ |
cf44bbc2 DM |
613 | num_reg /= na + ns; |
614 | ||
e5dd42e4 | 615 | /* Prevent overrunning the op->resources[] array. */ |
46ba6d7d DM |
616 | if (num_reg > PROMREG_MAX) { |
617 | printk(KERN_WARNING "%s: Too many regs (%d), " | |
618 | "limiting to %d.\n", | |
619 | op->node->full_name, num_reg, PROMREG_MAX); | |
620 | num_reg = PROMREG_MAX; | |
621 | } | |
622 | ||
cf44bbc2 DM |
623 | for (index = 0; index < num_reg; index++) { |
624 | struct resource *r = &op->resource[index]; | |
625 | u32 addr[OF_MAX_ADDR_CELLS]; | |
6a23acf3 | 626 | const u32 *reg = (preg + (index * ((na + ns) * 4))); |
cf44bbc2 DM |
627 | struct device_node *dp = op->node; |
628 | struct device_node *pp = p_op->node; | |
b85cdd49 | 629 | struct of_bus *pbus, *dbus; |
cf44bbc2 DM |
630 | u64 size, result = OF_BAD_ADDR; |
631 | unsigned long flags; | |
632 | int dna, dns; | |
633 | int pna, pns; | |
634 | ||
635 | size = of_read_addr(reg + na, ns); | |
636 | flags = bus->get_flags(reg); | |
637 | ||
638 | memcpy(addr, reg, na * 4); | |
639 | ||
a83f9823 | 640 | if (use_1to1_mapping(pp)) { |
cf44bbc2 DM |
641 | result = of_read_addr(addr, na); |
642 | goto build_res; | |
643 | } | |
644 | ||
645 | dna = na; | |
646 | dns = ns; | |
b85cdd49 | 647 | dbus = bus; |
cf44bbc2 DM |
648 | |
649 | while (1) { | |
650 | dp = pp; | |
651 | pp = dp->parent; | |
652 | if (!pp) { | |
653 | result = of_read_addr(addr, dna); | |
654 | break; | |
655 | } | |
656 | ||
657 | pbus = of_match_bus(pp); | |
658 | pbus->count_cells(dp, &pna, &pns); | |
659 | ||
b85cdd49 | 660 | if (build_one_resource(dp, dbus, pbus, addr, |
a83f9823 | 661 | dna, dns, pna)) |
cf44bbc2 DM |
662 | break; |
663 | ||
664 | dna = pna; | |
665 | dns = pns; | |
b85cdd49 | 666 | dbus = pbus; |
cf44bbc2 DM |
667 | } |
668 | ||
669 | build_res: | |
670 | memset(r, 0, sizeof(*r)); | |
a83f9823 DM |
671 | |
672 | if (of_resource_verbose) | |
673 | printk("%s reg[%d] -> %lx\n", | |
674 | op->node->full_name, index, | |
675 | result); | |
676 | ||
cf44bbc2 | 677 | if (result != OF_BAD_ADDR) { |
1815aed5 DM |
678 | if (tlb_type == hypervisor) |
679 | result &= 0x0fffffffffffffffUL; | |
680 | ||
cf44bbc2 DM |
681 | r->start = result; |
682 | r->end = result + size - 1; | |
683 | r->flags = flags; | |
cf44bbc2 DM |
684 | } |
685 | r->name = op->node->name; | |
686 | } | |
687 | } | |
688 | ||
2b1e5978 DM |
689 | static struct device_node * __init |
690 | apply_interrupt_map(struct device_node *dp, struct device_node *pp, | |
6a23acf3 | 691 | const u32 *imap, int imlen, const u32 *imask, |
2b1e5978 DM |
692 | unsigned int *irq_p) |
693 | { | |
694 | struct device_node *cp; | |
695 | unsigned int irq = *irq_p; | |
696 | struct of_bus *bus; | |
697 | phandle handle; | |
6a23acf3 | 698 | const u32 *reg; |
2b1e5978 DM |
699 | int na, num_reg, i; |
700 | ||
701 | bus = of_match_bus(pp); | |
702 | bus->count_cells(dp, &na, NULL); | |
703 | ||
704 | reg = of_get_property(dp, "reg", &num_reg); | |
705 | if (!reg || !num_reg) | |
706 | return NULL; | |
707 | ||
708 | imlen /= ((na + 3) * 4); | |
709 | handle = 0; | |
710 | for (i = 0; i < imlen; i++) { | |
711 | int j; | |
712 | ||
713 | for (j = 0; j < na; j++) { | |
714 | if ((reg[j] & imask[j]) != imap[j]) | |
715 | goto next; | |
716 | } | |
717 | if (imap[na] == irq) { | |
718 | handle = imap[na + 1]; | |
719 | irq = imap[na + 2]; | |
720 | break; | |
721 | } | |
722 | ||
723 | next: | |
724 | imap += (na + 3); | |
725 | } | |
46ba6d7d DM |
726 | if (i == imlen) { |
727 | /* Psycho and Sabre PCI controllers can have 'interrupt-map' | |
728 | * properties that do not include the on-board device | |
729 | * interrupts. Instead, the device's 'interrupts' property | |
730 | * is already a fully specified INO value. | |
731 | * | |
732 | * Handle this by deciding that, if we didn't get a | |
733 | * match in the parent's 'interrupt-map', and the | |
734 | * parent is an IRQ translater, then use the parent as | |
735 | * our IRQ controller. | |
736 | */ | |
737 | if (pp->irq_trans) | |
738 | return pp; | |
739 | ||
2b1e5978 | 740 | return NULL; |
46ba6d7d | 741 | } |
2b1e5978 DM |
742 | |
743 | *irq_p = irq; | |
744 | cp = of_find_node_by_phandle(handle); | |
745 | ||
746 | return cp; | |
747 | } | |
748 | ||
749 | static unsigned int __init pci_irq_swizzle(struct device_node *dp, | |
750 | struct device_node *pp, | |
751 | unsigned int irq) | |
752 | { | |
6a23acf3 | 753 | const struct linux_prom_pci_registers *regs; |
bb4c18cb | 754 | unsigned int bus, devfn, slot, ret; |
2b1e5978 DM |
755 | |
756 | if (irq < 1 || irq > 4) | |
757 | return irq; | |
758 | ||
759 | regs = of_get_property(dp, "reg", NULL); | |
760 | if (!regs) | |
761 | return irq; | |
762 | ||
bb4c18cb | 763 | bus = (regs->phys_hi >> 16) & 0xff; |
2b1e5978 DM |
764 | devfn = (regs->phys_hi >> 8) & 0xff; |
765 | slot = (devfn >> 3) & 0x1f; | |
766 | ||
bb4c18cb DM |
767 | if (pp->irq_trans) { |
768 | /* Derived from Table 8-3, U2P User's Manual. This branch | |
769 | * is handling a PCI controller that lacks a proper set of | |
770 | * interrupt-map and interrupt-map-mask properties. The | |
771 | * Ultra-E450 is one example. | |
772 | * | |
773 | * The bit layout is BSSLL, where: | |
774 | * B: 0 on bus A, 1 on bus B | |
775 | * D: 2-bit slot number, derived from PCI device number as | |
776 | * (dev - 1) for bus A, or (dev - 2) for bus B | |
777 | * L: 2-bit line number | |
bb4c18cb DM |
778 | */ |
779 | if (bus & 0x80) { | |
780 | /* PBM-A */ | |
781 | bus = 0x00; | |
782 | slot = (slot - 1) << 2; | |
783 | } else { | |
784 | /* PBM-B */ | |
785 | bus = 0x10; | |
786 | slot = (slot - 2) << 2; | |
787 | } | |
788 | irq -= 1; | |
789 | ||
790 | ret = (bus | slot | irq); | |
791 | } else { | |
792 | /* Going through a PCI-PCI bridge that lacks a set of | |
793 | * interrupt-map and interrupt-map-mask properties. | |
794 | */ | |
795 | ret = ((irq - 1 + (slot & 3)) & 3) + 1; | |
796 | } | |
2b1e5978 DM |
797 | |
798 | return ret; | |
799 | } | |
800 | ||
a83f9823 DM |
801 | static int of_irq_verbose; |
802 | ||
2b1e5978 DM |
803 | static unsigned int __init build_one_device_irq(struct of_device *op, |
804 | struct device *parent, | |
805 | unsigned int irq) | |
806 | { | |
807 | struct device_node *dp = op->node; | |
808 | struct device_node *pp, *ip; | |
809 | unsigned int orig_irq = irq; | |
810 | ||
811 | if (irq == 0xffffffff) | |
812 | return irq; | |
813 | ||
814 | if (dp->irq_trans) { | |
815 | irq = dp->irq_trans->irq_build(dp, irq, | |
816 | dp->irq_trans->data); | |
a83f9823 DM |
817 | |
818 | if (of_irq_verbose) | |
819 | printk("%s: direct translate %x --> %x\n", | |
820 | dp->full_name, orig_irq, irq); | |
821 | ||
2b1e5978 DM |
822 | return irq; |
823 | } | |
824 | ||
825 | /* Something more complicated. Walk up to the root, applying | |
826 | * interrupt-map or bus specific translations, until we hit | |
827 | * an IRQ translator. | |
828 | * | |
829 | * If we hit a bus type or situation we cannot handle, we | |
830 | * stop and assume that the original IRQ number was in a | |
831 | * format which has special meaning to it's immediate parent. | |
832 | */ | |
833 | pp = dp->parent; | |
834 | ip = NULL; | |
835 | while (pp) { | |
6a23acf3 | 836 | const void *imap, *imsk; |
2b1e5978 DM |
837 | int imlen; |
838 | ||
839 | imap = of_get_property(pp, "interrupt-map", &imlen); | |
840 | imsk = of_get_property(pp, "interrupt-map-mask", NULL); | |
841 | if (imap && imsk) { | |
842 | struct device_node *iret; | |
843 | int this_orig_irq = irq; | |
844 | ||
845 | iret = apply_interrupt_map(dp, pp, | |
846 | imap, imlen, imsk, | |
847 | &irq); | |
a83f9823 DM |
848 | |
849 | if (of_irq_verbose) | |
850 | printk("%s: Apply [%s:%x] imap --> [%s:%x]\n", | |
851 | op->node->full_name, | |
852 | pp->full_name, this_orig_irq, | |
853 | (iret ? iret->full_name : "NULL"), irq); | |
854 | ||
2b1e5978 DM |
855 | if (!iret) |
856 | break; | |
857 | ||
858 | if (iret->irq_trans) { | |
859 | ip = iret; | |
860 | break; | |
861 | } | |
862 | } else { | |
863 | if (!strcmp(pp->type, "pci") || | |
864 | !strcmp(pp->type, "pciex")) { | |
865 | unsigned int this_orig_irq = irq; | |
866 | ||
867 | irq = pci_irq_swizzle(dp, pp, irq); | |
a83f9823 DM |
868 | if (of_irq_verbose) |
869 | printk("%s: PCI swizzle [%s] " | |
870 | "%x --> %x\n", | |
871 | op->node->full_name, | |
872 | pp->full_name, this_orig_irq, | |
873 | irq); | |
874 | ||
2b1e5978 DM |
875 | } |
876 | ||
877 | if (pp->irq_trans) { | |
878 | ip = pp; | |
879 | break; | |
880 | } | |
881 | } | |
882 | dp = pp; | |
883 | pp = pp->parent; | |
884 | } | |
885 | if (!ip) | |
886 | return orig_irq; | |
887 | ||
888 | irq = ip->irq_trans->irq_build(op->node, irq, | |
889 | ip->irq_trans->data); | |
a83f9823 DM |
890 | if (of_irq_verbose) |
891 | printk("%s: Apply IRQ trans [%s] %x --> %x\n", | |
892 | op->node->full_name, ip->full_name, orig_irq, irq); | |
2b1e5978 DM |
893 | |
894 | return irq; | |
895 | } | |
896 | ||
cf44bbc2 DM |
897 | static struct of_device * __init scan_one_device(struct device_node *dp, |
898 | struct device *parent) | |
899 | { | |
900 | struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL); | |
6a23acf3 | 901 | const unsigned int *irq; |
2b1e5978 | 902 | int len, i; |
cf44bbc2 DM |
903 | |
904 | if (!op) | |
905 | return NULL; | |
906 | ||
907 | op->node = dp; | |
908 | ||
909 | op->clock_freq = of_getintprop_default(dp, "clock-frequency", | |
910 | (25*1000*1000)); | |
911 | op->portid = of_getintprop_default(dp, "upa-portid", -1); | |
912 | if (op->portid == -1) | |
913 | op->portid = of_getintprop_default(dp, "portid", -1); | |
914 | ||
915 | irq = of_get_property(dp, "interrupts", &len); | |
2b1e5978 DM |
916 | if (irq) { |
917 | memcpy(op->irqs, irq, len); | |
918 | op->num_irqs = len / 4; | |
919 | } else { | |
920 | op->num_irqs = 0; | |
921 | } | |
cf44bbc2 | 922 | |
e5dd42e4 | 923 | /* Prevent overrunning the op->irqs[] array. */ |
46ba6d7d DM |
924 | if (op->num_irqs > PROMINTR_MAX) { |
925 | printk(KERN_WARNING "%s: Too many irqs (%d), " | |
926 | "limiting to %d.\n", | |
927 | dp->full_name, op->num_irqs, PROMINTR_MAX); | |
928 | op->num_irqs = PROMINTR_MAX; | |
929 | } | |
930 | ||
cf44bbc2 | 931 | build_device_resources(op, parent); |
2b1e5978 DM |
932 | for (i = 0; i < op->num_irqs; i++) |
933 | op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]); | |
cf44bbc2 DM |
934 | |
935 | op->dev.parent = parent; | |
936 | op->dev.bus = &of_bus_type; | |
937 | if (!parent) | |
938 | strcpy(op->dev.bus_id, "root"); | |
939 | else | |
f5ef9d11 | 940 | sprintf(op->dev.bus_id, "%08x", dp->node); |
cf44bbc2 DM |
941 | |
942 | if (of_device_register(op)) { | |
943 | printk("%s: Could not register of device.\n", | |
944 | dp->full_name); | |
945 | kfree(op); | |
946 | op = NULL; | |
947 | } | |
948 | ||
949 | return op; | |
950 | } | |
951 | ||
952 | static void __init scan_tree(struct device_node *dp, struct device *parent) | |
953 | { | |
954 | while (dp) { | |
955 | struct of_device *op = scan_one_device(dp, parent); | |
956 | ||
957 | if (op) | |
958 | scan_tree(dp->child, &op->dev); | |
959 | ||
960 | dp = dp->sibling; | |
961 | } | |
962 | } | |
963 | ||
964 | static void __init scan_of_devices(void) | |
965 | { | |
966 | struct device_node *root = of_find_node_by_path("/"); | |
967 | struct of_device *parent; | |
968 | ||
969 | parent = scan_one_device(root, NULL); | |
970 | if (!parent) | |
971 | return; | |
972 | ||
973 | scan_tree(root->child, &parent->dev); | |
974 | } | |
975 | ||
a2bd4fd1 DM |
976 | static int __init of_bus_driver_init(void) |
977 | { | |
cf44bbc2 | 978 | int err; |
a2bd4fd1 | 979 | |
cf44bbc2 | 980 | err = bus_register(&of_bus_type); |
a2bd4fd1 DM |
981 | #ifdef CONFIG_PCI |
982 | if (!err) | |
983 | err = bus_register(&isa_bus_type); | |
984 | if (!err) | |
985 | err = bus_register(&ebus_bus_type); | |
986 | #endif | |
987 | #ifdef CONFIG_SBUS | |
988 | if (!err) | |
989 | err = bus_register(&sbus_bus_type); | |
990 | #endif | |
cf44bbc2 DM |
991 | |
992 | if (!err) | |
993 | scan_of_devices(); | |
994 | ||
995 | return err; | |
a2bd4fd1 DM |
996 | } |
997 | ||
998 | postcore_initcall(of_bus_driver_init); | |
999 | ||
a83f9823 DM |
1000 | static int __init of_debug(char *str) |
1001 | { | |
1002 | int val = 0; | |
1003 | ||
1004 | get_option(&str, &val); | |
1005 | if (val & 1) | |
1006 | of_resource_verbose = 1; | |
1007 | if (val & 2) | |
1008 | of_irq_verbose = 1; | |
1009 | return 1; | |
1010 | } | |
1011 | ||
1012 | __setup("of_debug=", of_debug); | |
1013 | ||
a2bd4fd1 DM |
1014 | int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus) |
1015 | { | |
1016 | /* initialize common driver fields */ | |
1017 | drv->driver.name = drv->name; | |
1018 | drv->driver.bus = bus; | |
1019 | ||
1020 | /* register with core */ | |
1021 | return driver_register(&drv->driver); | |
1022 | } | |
1023 | ||
1024 | void of_unregister_driver(struct of_platform_driver *drv) | |
1025 | { | |
1026 | driver_unregister(&drv->driver); | |
1027 | } | |
1028 | ||
1029 | ||
1030 | static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf) | |
1031 | { | |
1032 | struct of_device *ofdev; | |
1033 | ||
1034 | ofdev = to_of_device(dev); | |
1035 | return sprintf(buf, "%s", ofdev->node->full_name); | |
1036 | } | |
1037 | ||
1038 | static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL); | |
1039 | ||
1040 | /** | |
1041 | * of_release_dev - free an of device structure when all users of it are finished. | |
1042 | * @dev: device that's been disconnected | |
1043 | * | |
1044 | * Will be called only by the device core when all users of this of device are | |
1045 | * done. | |
1046 | */ | |
1047 | void of_release_dev(struct device *dev) | |
1048 | { | |
1049 | struct of_device *ofdev; | |
1050 | ||
1051 | ofdev = to_of_device(dev); | |
1052 | ||
1053 | kfree(ofdev); | |
1054 | } | |
1055 | ||
1056 | int of_device_register(struct of_device *ofdev) | |
1057 | { | |
1058 | int rc; | |
1059 | ||
1060 | BUG_ON(ofdev->node == NULL); | |
1061 | ||
1062 | rc = device_register(&ofdev->dev); | |
1063 | if (rc) | |
1064 | return rc; | |
1065 | ||
6cc8b6f5 AM |
1066 | rc = device_create_file(&ofdev->dev, &dev_attr_devspec); |
1067 | if (rc) | |
1068 | device_unregister(&ofdev->dev); | |
a2bd4fd1 | 1069 | |
6cc8b6f5 | 1070 | return rc; |
a2bd4fd1 DM |
1071 | } |
1072 | ||
1073 | void of_device_unregister(struct of_device *ofdev) | |
1074 | { | |
1075 | device_remove_file(&ofdev->dev, &dev_attr_devspec); | |
1076 | device_unregister(&ofdev->dev); | |
1077 | } | |
1078 | ||
1079 | struct of_device* of_platform_device_create(struct device_node *np, | |
1080 | const char *bus_id, | |
1081 | struct device *parent, | |
1082 | struct bus_type *bus) | |
1083 | { | |
1084 | struct of_device *dev; | |
1085 | ||
982c2064 | 1086 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
a2bd4fd1 DM |
1087 | if (!dev) |
1088 | return NULL; | |
a2bd4fd1 DM |
1089 | |
1090 | dev->dev.parent = parent; | |
1091 | dev->dev.bus = bus; | |
1092 | dev->dev.release = of_release_dev; | |
1093 | ||
1094 | strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE); | |
1095 | ||
1096 | if (of_device_register(dev) != 0) { | |
1097 | kfree(dev); | |
1098 | return NULL; | |
1099 | } | |
1100 | ||
1101 | return dev; | |
1102 | } | |
1103 | ||
1104 | EXPORT_SYMBOL(of_match_device); | |
1105 | EXPORT_SYMBOL(of_register_driver); | |
1106 | EXPORT_SYMBOL(of_unregister_driver); | |
1107 | EXPORT_SYMBOL(of_device_register); | |
1108 | EXPORT_SYMBOL(of_device_unregister); | |
1109 | EXPORT_SYMBOL(of_dev_get); | |
1110 | EXPORT_SYMBOL(of_dev_put); | |
1111 | EXPORT_SYMBOL(of_platform_device_create); | |
1112 | EXPORT_SYMBOL(of_release_dev); |