Commit | Line | Data |
---|---|---|
d1405b86 BH |
1 | #undef DEBUG |
2 | ||
3 | #include <linux/kernel.h> | |
4 | #include <linux/string.h> | |
5 | #include <linux/pci_regs.h> | |
6 | #include <linux/module.h> | |
d2dd482b | 7 | #include <linux/ioport.h> |
29cfe6f4 | 8 | #include <linux/etherdevice.h> |
d1405b86 | 9 | #include <asm/prom.h> |
d2dd482b | 10 | #include <asm/pci-bridge.h> |
d1405b86 BH |
11 | |
12 | #ifdef DEBUG | |
13 | #define DBG(fmt...) do { printk(fmt); } while(0) | |
14 | #else | |
15 | #define DBG(fmt...) do { } while(0) | |
16 | #endif | |
17 | ||
18 | #ifdef CONFIG_PPC64 | |
19 | #define PRu64 "%lx" | |
20 | #else | |
21 | #define PRu64 "%llx" | |
22 | #endif | |
23 | ||
24 | /* Max address size we deal with */ | |
25 | #define OF_MAX_ADDR_CELLS 4 | |
26 | #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \ | |
77926826 | 27 | (ns) > 0) |
d1405b86 | 28 | |
83efafb3 AF |
29 | static struct of_bus *of_match_bus(struct device_node *np); |
30 | static int __of_address_to_resource(struct device_node *dev, | |
31 | const u32 *addrp, u64 size, unsigned int flags, | |
32 | struct resource *r); | |
33 | ||
34 | ||
d1405b86 BH |
35 | /* Debug utility */ |
36 | #ifdef DEBUG | |
b5a1a9ab | 37 | static void of_dump_addr(const char *s, const u32 *addr, int na) |
d1405b86 BH |
38 | { |
39 | printk("%s", s); | |
40 | while(na--) | |
41 | printk(" %08x", *(addr++)); | |
42 | printk("\n"); | |
43 | } | |
44 | #else | |
b5a1a9ab | 45 | static void of_dump_addr(const char *s, const u32 *addr, int na) { } |
d1405b86 BH |
46 | #endif |
47 | ||
d1405b86 BH |
48 | |
49 | /* Callbacks for bus specific translators */ | |
50 | struct of_bus { | |
51 | const char *name; | |
52 | const char *addresses; | |
53 | int (*match)(struct device_node *parent); | |
54 | void (*count_cells)(struct device_node *child, | |
55 | int *addrc, int *sizec); | |
b5a1a9ab JK |
56 | u64 (*map)(u32 *addr, const u32 *range, |
57 | int na, int ns, int pna); | |
d1405b86 | 58 | int (*translate)(u32 *addr, u64 offset, int na); |
b5a1a9ab | 59 | unsigned int (*get_flags)(const u32 *addr); |
d1405b86 BH |
60 | }; |
61 | ||
62 | ||
63 | /* | |
64 | * Default translator (generic bus) | |
65 | */ | |
66 | ||
d2dd482b BH |
67 | static void of_bus_default_count_cells(struct device_node *dev, |
68 | int *addrc, int *sizec) | |
d1405b86 BH |
69 | { |
70 | if (addrc) | |
a8bda5dd | 71 | *addrc = of_n_addr_cells(dev); |
d1405b86 | 72 | if (sizec) |
9213feea | 73 | *sizec = of_n_size_cells(dev); |
d1405b86 BH |
74 | } |
75 | ||
b5a1a9ab JK |
76 | static u64 of_bus_default_map(u32 *addr, const u32 *range, |
77 | int na, int ns, int pna) | |
d1405b86 BH |
78 | { |
79 | u64 cp, s, da; | |
80 | ||
cc9fd71c BH |
81 | cp = of_read_number(range, na); |
82 | s = of_read_number(range + na + pna, ns); | |
83 | da = of_read_number(addr, na); | |
d1405b86 BH |
84 | |
85 | DBG("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n", | |
86 | cp, s, da); | |
87 | ||
88 | if (da < cp || da >= (cp + s)) | |
89 | return OF_BAD_ADDR; | |
90 | return da - cp; | |
91 | } | |
92 | ||
d2dd482b | 93 | static int of_bus_default_translate(u32 *addr, u64 offset, int na) |
d1405b86 | 94 | { |
cc9fd71c | 95 | u64 a = of_read_number(addr, na); |
d1405b86 BH |
96 | memset(addr, 0, na * 4); |
97 | a += offset; | |
98 | if (na > 1) | |
99 | addr[na - 2] = a >> 32; | |
100 | addr[na - 1] = a & 0xffffffffu; | |
101 | ||
102 | return 0; | |
103 | } | |
104 | ||
b5a1a9ab | 105 | static unsigned int of_bus_default_get_flags(const u32 *addr) |
d2dd482b BH |
106 | { |
107 | return IORESOURCE_MEM; | |
108 | } | |
109 | ||
d1405b86 | 110 | |
83efafb3 | 111 | #ifdef CONFIG_PCI |
d1405b86 BH |
112 | /* |
113 | * PCI bus specific translator | |
114 | */ | |
115 | ||
116 | static int of_bus_pci_match(struct device_node *np) | |
117 | { | |
d5f07900 PM |
118 | /* "vci" is for the /chaos bridge on 1st-gen PCI powermacs */ |
119 | return !strcmp(np->type, "pci") || !strcmp(np->type, "vci"); | |
d1405b86 BH |
120 | } |
121 | ||
122 | static void of_bus_pci_count_cells(struct device_node *np, | |
123 | int *addrc, int *sizec) | |
124 | { | |
125 | if (addrc) | |
126 | *addrc = 3; | |
127 | if (sizec) | |
128 | *sizec = 2; | |
129 | } | |
130 | ||
67260ac9 BH |
131 | static unsigned int of_bus_pci_get_flags(const u32 *addr) |
132 | { | |
133 | unsigned int flags = 0; | |
134 | u32 w = addr[0]; | |
135 | ||
136 | switch((w >> 24) & 0x03) { | |
137 | case 0x01: | |
138 | flags |= IORESOURCE_IO; | |
139 | break; | |
140 | case 0x02: /* 32 bits */ | |
141 | case 0x03: /* 64 bits */ | |
142 | flags |= IORESOURCE_MEM; | |
143 | break; | |
144 | } | |
145 | if (w & 0x40000000) | |
146 | flags |= IORESOURCE_PREFETCH; | |
147 | return flags; | |
148 | } | |
149 | ||
b5a1a9ab | 150 | static u64 of_bus_pci_map(u32 *addr, const u32 *range, int na, int ns, int pna) |
d1405b86 BH |
151 | { |
152 | u64 cp, s, da; | |
67260ac9 BH |
153 | unsigned int af, rf; |
154 | ||
155 | af = of_bus_pci_get_flags(addr); | |
156 | rf = of_bus_pci_get_flags(range); | |
d1405b86 BH |
157 | |
158 | /* Check address type match */ | |
67260ac9 | 159 | if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO)) |
d1405b86 BH |
160 | return OF_BAD_ADDR; |
161 | ||
162 | /* Read address values, skipping high cell */ | |
cc9fd71c BH |
163 | cp = of_read_number(range + 1, na - 1); |
164 | s = of_read_number(range + na + pna, ns); | |
165 | da = of_read_number(addr + 1, na - 1); | |
d1405b86 BH |
166 | |
167 | DBG("OF: PCI map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da); | |
168 | ||
169 | if (da < cp || da >= (cp + s)) | |
170 | return OF_BAD_ADDR; | |
171 | return da - cp; | |
172 | } | |
173 | ||
174 | static int of_bus_pci_translate(u32 *addr, u64 offset, int na) | |
175 | { | |
d2dd482b BH |
176 | return of_bus_default_translate(addr + 1, offset, na - 1); |
177 | } | |
178 | ||
83efafb3 AF |
179 | const u32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size, |
180 | unsigned int *flags) | |
181 | { | |
182 | const u32 *prop; | |
183 | unsigned int psize; | |
184 | struct device_node *parent; | |
185 | struct of_bus *bus; | |
186 | int onesize, i, na, ns; | |
187 | ||
188 | /* Get parent & match bus type */ | |
189 | parent = of_get_parent(dev); | |
190 | if (parent == NULL) | |
191 | return NULL; | |
192 | bus = of_match_bus(parent); | |
193 | if (strcmp(bus->name, "pci")) { | |
194 | of_node_put(parent); | |
195 | return NULL; | |
196 | } | |
197 | bus->count_cells(dev, &na, &ns); | |
198 | of_node_put(parent); | |
199 | if (!OF_CHECK_COUNTS(na, ns)) | |
200 | return NULL; | |
201 | ||
202 | /* Get "reg" or "assigned-addresses" property */ | |
e2eb6392 | 203 | prop = of_get_property(dev, bus->addresses, &psize); |
83efafb3 AF |
204 | if (prop == NULL) |
205 | return NULL; | |
206 | psize /= 4; | |
207 | ||
208 | onesize = na + ns; | |
209 | for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) | |
210 | if ((prop[0] & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) { | |
211 | if (size) | |
212 | *size = of_read_number(prop + na, ns); | |
213 | if (flags) | |
214 | *flags = bus->get_flags(prop); | |
215 | return prop; | |
216 | } | |
217 | return NULL; | |
218 | } | |
219 | EXPORT_SYMBOL(of_get_pci_address); | |
220 | ||
221 | int of_pci_address_to_resource(struct device_node *dev, int bar, | |
222 | struct resource *r) | |
223 | { | |
224 | const u32 *addrp; | |
225 | u64 size; | |
226 | unsigned int flags; | |
227 | ||
228 | addrp = of_get_pci_address(dev, bar, &size, &flags); | |
229 | if (addrp == NULL) | |
230 | return -EINVAL; | |
231 | return __of_address_to_resource(dev, addrp, size, flags, r); | |
232 | } | |
233 | EXPORT_SYMBOL_GPL(of_pci_address_to_resource); | |
234 | ||
235 | static u8 of_irq_pci_swizzle(u8 slot, u8 pin) | |
236 | { | |
237 | return (((pin - 1) + slot) % 4) + 1; | |
238 | } | |
239 | ||
240 | int of_irq_map_pci(struct pci_dev *pdev, struct of_irq *out_irq) | |
241 | { | |
242 | struct device_node *dn, *ppnode; | |
243 | struct pci_dev *ppdev; | |
244 | u32 lspec; | |
245 | u32 laddr[3]; | |
246 | u8 pin; | |
247 | int rc; | |
248 | ||
249 | /* Check if we have a device node, if yes, fallback to standard OF | |
250 | * parsing | |
251 | */ | |
252 | dn = pci_device_to_OF_node(pdev); | |
4b824de9 AZ |
253 | if (dn) { |
254 | rc = of_irq_map_one(dn, 0, out_irq); | |
255 | if (!rc) | |
256 | return rc; | |
257 | } | |
83efafb3 AF |
258 | |
259 | /* Ok, we don't, time to have fun. Let's start by building up an | |
260 | * interrupt spec. we assume #interrupt-cells is 1, which is standard | |
261 | * for PCI. If you do different, then don't use that routine. | |
262 | */ | |
263 | rc = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin); | |
264 | if (rc != 0) | |
265 | return rc; | |
266 | /* No pin, exit */ | |
267 | if (pin == 0) | |
268 | return -ENODEV; | |
269 | ||
270 | /* Now we walk up the PCI tree */ | |
271 | lspec = pin; | |
272 | for (;;) { | |
273 | /* Get the pci_dev of our parent */ | |
274 | ppdev = pdev->bus->self; | |
275 | ||
276 | /* Ouch, it's a host bridge... */ | |
277 | if (ppdev == NULL) { | |
278 | #ifdef CONFIG_PPC64 | |
279 | ppnode = pci_bus_to_OF_node(pdev->bus); | |
280 | #else | |
281 | struct pci_controller *host; | |
282 | host = pci_bus_to_host(pdev->bus); | |
44ef3390 | 283 | ppnode = host ? host->dn : NULL; |
83efafb3 AF |
284 | #endif |
285 | /* No node for host bridge ? give up */ | |
286 | if (ppnode == NULL) | |
287 | return -EINVAL; | |
288 | } else | |
289 | /* We found a P2P bridge, check if it has a node */ | |
290 | ppnode = pci_device_to_OF_node(ppdev); | |
291 | ||
292 | /* Ok, we have found a parent with a device-node, hand over to | |
293 | * the OF parsing code. | |
294 | * We build a unit address from the linux device to be used for | |
295 | * resolution. Note that we use the linux bus number which may | |
296 | * not match your firmware bus numbering. | |
297 | * Fortunately, in most cases, interrupt-map-mask doesn't include | |
298 | * the bus number as part of the matching. | |
299 | * You should still be careful about that though if you intend | |
300 | * to rely on this function (you ship a firmware that doesn't | |
301 | * create device nodes for all PCI devices). | |
302 | */ | |
303 | if (ppnode) | |
304 | break; | |
305 | ||
306 | /* We can only get here if we hit a P2P bridge with no node, | |
307 | * let's do standard swizzling and try again | |
308 | */ | |
309 | lspec = of_irq_pci_swizzle(PCI_SLOT(pdev->devfn), lspec); | |
310 | pdev = ppdev; | |
311 | } | |
312 | ||
313 | laddr[0] = (pdev->bus->number << 16) | |
314 | | (pdev->devfn << 8); | |
315 | laddr[1] = laddr[2] = 0; | |
316 | return of_irq_map_raw(ppnode, &lspec, 1, laddr, out_irq); | |
317 | } | |
318 | EXPORT_SYMBOL_GPL(of_irq_map_pci); | |
319 | #endif /* CONFIG_PCI */ | |
320 | ||
d1405b86 BH |
321 | /* |
322 | * ISA bus specific translator | |
323 | */ | |
324 | ||
325 | static int of_bus_isa_match(struct device_node *np) | |
326 | { | |
327 | return !strcmp(np->name, "isa"); | |
328 | } | |
329 | ||
330 | static void of_bus_isa_count_cells(struct device_node *child, | |
331 | int *addrc, int *sizec) | |
332 | { | |
333 | if (addrc) | |
334 | *addrc = 2; | |
335 | if (sizec) | |
336 | *sizec = 1; | |
337 | } | |
338 | ||
b5a1a9ab | 339 | static u64 of_bus_isa_map(u32 *addr, const u32 *range, int na, int ns, int pna) |
d1405b86 BH |
340 | { |
341 | u64 cp, s, da; | |
342 | ||
343 | /* Check address type match */ | |
344 | if ((addr[0] ^ range[0]) & 0x00000001) | |
345 | return OF_BAD_ADDR; | |
346 | ||
347 | /* Read address values, skipping high cell */ | |
cc9fd71c BH |
348 | cp = of_read_number(range + 1, na - 1); |
349 | s = of_read_number(range + na + pna, ns); | |
350 | da = of_read_number(addr + 1, na - 1); | |
d1405b86 BH |
351 | |
352 | DBG("OF: ISA map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da); | |
353 | ||
354 | if (da < cp || da >= (cp + s)) | |
355 | return OF_BAD_ADDR; | |
356 | return da - cp; | |
357 | } | |
358 | ||
359 | static int of_bus_isa_translate(u32 *addr, u64 offset, int na) | |
360 | { | |
d2dd482b BH |
361 | return of_bus_default_translate(addr + 1, offset, na - 1); |
362 | } | |
363 | ||
b5a1a9ab | 364 | static unsigned int of_bus_isa_get_flags(const u32 *addr) |
d2dd482b BH |
365 | { |
366 | unsigned int flags = 0; | |
367 | u32 w = addr[0]; | |
368 | ||
369 | if (w & 1) | |
370 | flags |= IORESOURCE_IO; | |
371 | else | |
372 | flags |= IORESOURCE_MEM; | |
373 | return flags; | |
d1405b86 BH |
374 | } |
375 | ||
d2dd482b | 376 | |
d1405b86 BH |
377 | /* |
378 | * Array of bus specific translators | |
379 | */ | |
380 | ||
381 | static struct of_bus of_busses[] = { | |
83efafb3 | 382 | #ifdef CONFIG_PCI |
d1405b86 BH |
383 | /* PCI */ |
384 | { | |
385 | .name = "pci", | |
386 | .addresses = "assigned-addresses", | |
387 | .match = of_bus_pci_match, | |
388 | .count_cells = of_bus_pci_count_cells, | |
389 | .map = of_bus_pci_map, | |
390 | .translate = of_bus_pci_translate, | |
d2dd482b | 391 | .get_flags = of_bus_pci_get_flags, |
d1405b86 | 392 | }, |
83efafb3 | 393 | #endif /* CONFIG_PCI */ |
d1405b86 BH |
394 | /* ISA */ |
395 | { | |
396 | .name = "isa", | |
397 | .addresses = "reg", | |
398 | .match = of_bus_isa_match, | |
399 | .count_cells = of_bus_isa_count_cells, | |
400 | .map = of_bus_isa_map, | |
401 | .translate = of_bus_isa_translate, | |
d2dd482b | 402 | .get_flags = of_bus_isa_get_flags, |
d1405b86 BH |
403 | }, |
404 | /* Default */ | |
405 | { | |
406 | .name = "default", | |
407 | .addresses = "reg", | |
408 | .match = NULL, | |
d2dd482b BH |
409 | .count_cells = of_bus_default_count_cells, |
410 | .map = of_bus_default_map, | |
411 | .translate = of_bus_default_translate, | |
412 | .get_flags = of_bus_default_get_flags, | |
d1405b86 BH |
413 | }, |
414 | }; | |
415 | ||
416 | static struct of_bus *of_match_bus(struct device_node *np) | |
417 | { | |
418 | int i; | |
419 | ||
420 | for (i = 0; i < ARRAY_SIZE(of_busses); i ++) | |
421 | if (!of_busses[i].match || of_busses[i].match(np)) | |
422 | return &of_busses[i]; | |
423 | BUG(); | |
424 | return NULL; | |
425 | } | |
426 | ||
427 | static int of_translate_one(struct device_node *parent, struct of_bus *bus, | |
428 | struct of_bus *pbus, u32 *addr, | |
837c54db | 429 | int na, int ns, int pna, const char *rprop) |
d1405b86 | 430 | { |
a7f67bdf | 431 | const u32 *ranges; |
d1405b86 BH |
432 | unsigned int rlen; |
433 | int rone; | |
434 | u64 offset = OF_BAD_ADDR; | |
435 | ||
436 | /* Normally, an absence of a "ranges" property means we are | |
437 | * crossing a non-translatable boundary, and thus the addresses | |
438 | * below the current not cannot be converted to CPU physical ones. | |
439 | * Unfortunately, while this is very clear in the spec, it's not | |
440 | * what Apple understood, and they do have things like /uni-n or | |
441 | * /ht nodes with no "ranges" property and a lot of perfectly | |
442 | * useable mapped devices below them. Thus we treat the absence of | |
443 | * "ranges" as equivalent to an empty "ranges" property which means | |
444 | * a 1:1 translation at that level. It's up to the caller not to try | |
445 | * to translate addresses that aren't supposed to be translated in | |
446 | * the first place. --BenH. | |
447 | */ | |
837c54db | 448 | ranges = of_get_property(parent, rprop, &rlen); |
d1405b86 | 449 | if (ranges == NULL || rlen == 0) { |
cc9fd71c | 450 | offset = of_read_number(addr, na); |
d2dd482b BH |
451 | memset(addr, 0, pna * 4); |
452 | DBG("OF: no ranges, 1:1 translation\n"); | |
d1405b86 BH |
453 | goto finish; |
454 | } | |
455 | ||
456 | DBG("OF: walking ranges...\n"); | |
457 | ||
458 | /* Now walk through the ranges */ | |
459 | rlen /= 4; | |
460 | rone = na + pna + ns; | |
461 | for (; rlen >= rone; rlen -= rone, ranges += rone) { | |
462 | offset = bus->map(addr, ranges, na, ns, pna); | |
463 | if (offset != OF_BAD_ADDR) | |
464 | break; | |
465 | } | |
466 | if (offset == OF_BAD_ADDR) { | |
467 | DBG("OF: not found !\n"); | |
468 | return 1; | |
469 | } | |
470 | memcpy(addr, ranges + na, 4 * pna); | |
471 | ||
472 | finish: | |
473 | of_dump_addr("OF: parent translation for:", addr, pna); | |
bb6b9b28 | 474 | DBG("OF: with offset: "PRu64"\n", offset); |
d1405b86 BH |
475 | |
476 | /* Translate it into parent bus space */ | |
477 | return pbus->translate(addr, offset, pna); | |
478 | } | |
479 | ||
480 | ||
481 | /* | |
482 | * Translate an address from the device-tree into a CPU physical address, | |
483 | * this walks up the tree and applies the various bus mappings on the | |
484 | * way. | |
485 | * | |
486 | * Note: We consider that crossing any level with #size-cells == 0 to mean | |
487 | * that translation is impossible (that is we are not dealing with a value | |
488 | * that can be mapped to a cpu physical address). This is not really specified | |
489 | * that way, but this is traditionally the way IBM at least do things | |
490 | */ | |
837c54db BH |
491 | u64 __of_translate_address(struct device_node *dev, const u32 *in_addr, |
492 | const char *rprop) | |
d1405b86 BH |
493 | { |
494 | struct device_node *parent = NULL; | |
495 | struct of_bus *bus, *pbus; | |
496 | u32 addr[OF_MAX_ADDR_CELLS]; | |
497 | int na, ns, pna, pns; | |
498 | u64 result = OF_BAD_ADDR; | |
499 | ||
500 | DBG("OF: ** translation for device %s **\n", dev->full_name); | |
501 | ||
502 | /* Increase refcount at current level */ | |
503 | of_node_get(dev); | |
504 | ||
505 | /* Get parent & match bus type */ | |
506 | parent = of_get_parent(dev); | |
507 | if (parent == NULL) | |
508 | goto bail; | |
509 | bus = of_match_bus(parent); | |
510 | ||
511 | /* Cound address cells & copy address locally */ | |
512 | bus->count_cells(dev, &na, &ns); | |
513 | if (!OF_CHECK_COUNTS(na, ns)) { | |
514 | printk(KERN_ERR "prom_parse: Bad cell count for %s\n", | |
515 | dev->full_name); | |
516 | goto bail; | |
517 | } | |
518 | memcpy(addr, in_addr, na * 4); | |
519 | ||
520 | DBG("OF: bus is %s (na=%d, ns=%d) on %s\n", | |
521 | bus->name, na, ns, parent->full_name); | |
522 | of_dump_addr("OF: translating address:", addr, na); | |
523 | ||
524 | /* Translate */ | |
525 | for (;;) { | |
526 | /* Switch to parent bus */ | |
527 | of_node_put(dev); | |
528 | dev = parent; | |
529 | parent = of_get_parent(dev); | |
530 | ||
531 | /* If root, we have finished */ | |
532 | if (parent == NULL) { | |
533 | DBG("OF: reached root node\n"); | |
cc9fd71c | 534 | result = of_read_number(addr, na); |
d1405b86 BH |
535 | break; |
536 | } | |
537 | ||
538 | /* Get new parent bus and counts */ | |
539 | pbus = of_match_bus(parent); | |
540 | pbus->count_cells(dev, &pna, &pns); | |
541 | if (!OF_CHECK_COUNTS(pna, pns)) { | |
542 | printk(KERN_ERR "prom_parse: Bad cell count for %s\n", | |
543 | dev->full_name); | |
544 | break; | |
545 | } | |
546 | ||
547 | DBG("OF: parent bus is %s (na=%d, ns=%d) on %s\n", | |
548 | pbus->name, pna, pns, parent->full_name); | |
549 | ||
550 | /* Apply bus translation */ | |
837c54db | 551 | if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop)) |
d1405b86 BH |
552 | break; |
553 | ||
554 | /* Complete the move up one level */ | |
555 | na = pna; | |
556 | ns = pns; | |
557 | bus = pbus; | |
558 | ||
559 | of_dump_addr("OF: one level translation:", addr, na); | |
560 | } | |
561 | bail: | |
562 | of_node_put(parent); | |
563 | of_node_put(dev); | |
564 | ||
565 | return result; | |
566 | } | |
837c54db BH |
567 | |
568 | u64 of_translate_address(struct device_node *dev, const u32 *in_addr) | |
569 | { | |
570 | return __of_translate_address(dev, in_addr, "ranges"); | |
571 | } | |
d1405b86 BH |
572 | EXPORT_SYMBOL(of_translate_address); |
573 | ||
837c54db BH |
574 | u64 of_translate_dma_address(struct device_node *dev, const u32 *in_addr) |
575 | { | |
576 | return __of_translate_address(dev, in_addr, "dma-ranges"); | |
577 | } | |
578 | EXPORT_SYMBOL(of_translate_dma_address); | |
579 | ||
a7f67bdf | 580 | const u32 *of_get_address(struct device_node *dev, int index, u64 *size, |
d2dd482b | 581 | unsigned int *flags) |
d1405b86 | 582 | { |
a7f67bdf | 583 | const u32 *prop; |
d1405b86 BH |
584 | unsigned int psize; |
585 | struct device_node *parent; | |
586 | struct of_bus *bus; | |
587 | int onesize, i, na, ns; | |
588 | ||
589 | /* Get parent & match bus type */ | |
590 | parent = of_get_parent(dev); | |
591 | if (parent == NULL) | |
592 | return NULL; | |
593 | bus = of_match_bus(parent); | |
594 | bus->count_cells(dev, &na, &ns); | |
595 | of_node_put(parent); | |
596 | if (!OF_CHECK_COUNTS(na, ns)) | |
597 | return NULL; | |
598 | ||
599 | /* Get "reg" or "assigned-addresses" property */ | |
e2eb6392 | 600 | prop = of_get_property(dev, bus->addresses, &psize); |
d1405b86 BH |
601 | if (prop == NULL) |
602 | return NULL; | |
603 | psize /= 4; | |
604 | ||
605 | onesize = na + ns; | |
606 | for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) | |
607 | if (i == index) { | |
608 | if (size) | |
cc9fd71c | 609 | *size = of_read_number(prop + na, ns); |
d2dd482b BH |
610 | if (flags) |
611 | *flags = bus->get_flags(prop); | |
d1405b86 BH |
612 | return prop; |
613 | } | |
614 | return NULL; | |
615 | } | |
616 | EXPORT_SYMBOL(of_get_address); | |
617 | ||
a7f67bdf | 618 | static int __of_address_to_resource(struct device_node *dev, const u32 *addrp, |
d2dd482b BH |
619 | u64 size, unsigned int flags, |
620 | struct resource *r) | |
621 | { | |
622 | u64 taddr; | |
623 | ||
624 | if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0) | |
625 | return -EINVAL; | |
626 | taddr = of_translate_address(dev, addrp); | |
627 | if (taddr == OF_BAD_ADDR) | |
628 | return -EINVAL; | |
629 | memset(r, 0, sizeof(struct resource)); | |
630 | if (flags & IORESOURCE_IO) { | |
f2c4583a | 631 | unsigned long port; |
d2dd482b | 632 | port = pci_address_to_pio(taddr); |
f2c4583a | 633 | if (port == (unsigned long)-1) |
d2dd482b BH |
634 | return -EINVAL; |
635 | r->start = port; | |
636 | r->end = port + size - 1; | |
637 | } else { | |
638 | r->start = taddr; | |
639 | r->end = taddr + size - 1; | |
640 | } | |
641 | r->flags = flags; | |
642 | r->name = dev->name; | |
643 | return 0; | |
644 | } | |
645 | ||
646 | int of_address_to_resource(struct device_node *dev, int index, | |
647 | struct resource *r) | |
648 | { | |
a7f67bdf | 649 | const u32 *addrp; |
d2dd482b BH |
650 | u64 size; |
651 | unsigned int flags; | |
652 | ||
653 | addrp = of_get_address(dev, index, &size, &flags); | |
654 | if (addrp == NULL) | |
655 | return -EINVAL; | |
656 | return __of_address_to_resource(dev, addrp, size, flags, r); | |
657 | } | |
658 | EXPORT_SYMBOL_GPL(of_address_to_resource); | |
659 | ||
a7f67bdf | 660 | void of_parse_dma_window(struct device_node *dn, const void *dma_window_prop, |
d4ad66fa JK |
661 | unsigned long *busno, unsigned long *phys, unsigned long *size) |
662 | { | |
a7f67bdf JK |
663 | const u32 *dma_window; |
664 | u32 cells; | |
665 | const unsigned char *prop; | |
d4ad66fa | 666 | |
a7f67bdf | 667 | dma_window = dma_window_prop; |
d4ad66fa JK |
668 | |
669 | /* busno is always one cell */ | |
670 | *busno = *(dma_window++); | |
671 | ||
e2eb6392 | 672 | prop = of_get_property(dn, "ibm,#dma-address-cells", NULL); |
03ac829b | 673 | if (!prop) |
e2eb6392 | 674 | prop = of_get_property(dn, "#address-cells", NULL); |
03ac829b | 675 | |
a8bda5dd | 676 | cells = prop ? *(u32 *)prop : of_n_addr_cells(dn); |
cc9fd71c | 677 | *phys = of_read_number(dma_window, cells); |
d4ad66fa JK |
678 | |
679 | dma_window += cells; | |
680 | ||
e2eb6392 | 681 | prop = of_get_property(dn, "ibm,#dma-size-cells", NULL); |
9213feea | 682 | cells = prop ? *(u32 *)prop : of_n_size_cells(dn); |
cc9fd71c BH |
683 | *size = of_read_number(dma_window, cells); |
684 | } | |
685 | ||
686 | /* | |
687 | * Interrupt remapper | |
688 | */ | |
689 | ||
690 | static unsigned int of_irq_workarounds; | |
691 | static struct device_node *of_irq_dflt_pic; | |
692 | ||
693 | static struct device_node *of_irq_find_parent(struct device_node *child) | |
694 | { | |
695 | struct device_node *p; | |
a7f67bdf | 696 | const phandle *parp; |
cc9fd71c BH |
697 | |
698 | if (!of_node_get(child)) | |
699 | return NULL; | |
700 | ||
701 | do { | |
e2eb6392 | 702 | parp = of_get_property(child, "interrupt-parent", NULL); |
cc9fd71c BH |
703 | if (parp == NULL) |
704 | p = of_get_parent(child); | |
705 | else { | |
706 | if (of_irq_workarounds & OF_IMAP_NO_PHANDLE) | |
707 | p = of_node_get(of_irq_dflt_pic); | |
708 | else | |
709 | p = of_find_node_by_phandle(*parp); | |
710 | } | |
711 | of_node_put(child); | |
712 | child = p; | |
e2eb6392 | 713 | } while (p && of_get_property(p, "#interrupt-cells", NULL) == NULL); |
cc9fd71c BH |
714 | |
715 | return p; | |
716 | } | |
717 | ||
cc9fd71c BH |
718 | /* This doesn't need to be called if you don't have any special workaround |
719 | * flags to pass | |
720 | */ | |
721 | void of_irq_map_init(unsigned int flags) | |
722 | { | |
723 | of_irq_workarounds = flags; | |
724 | ||
725 | /* OldWorld, don't bother looking at other things */ | |
726 | if (flags & OF_IMAP_OLDWORLD_MAC) | |
727 | return; | |
728 | ||
729 | /* If we don't have phandles, let's try to locate a default interrupt | |
730 | * controller (happens when booting with BootX). We do a first match | |
731 | * here, hopefully, that only ever happens on machines with one | |
732 | * controller. | |
733 | */ | |
734 | if (flags & OF_IMAP_NO_PHANDLE) { | |
735 | struct device_node *np; | |
736 | ||
54018178 | 737 | for_each_node_with_property(np, "interrupt-controller") { |
cc9fd71c BH |
738 | /* Skip /chosen/interrupt-controller */ |
739 | if (strcmp(np->name, "chosen") == 0) | |
740 | continue; | |
741 | /* It seems like at least one person on this planet wants | |
742 | * to use BootX on a machine with an AppleKiwi controller | |
743 | * which happens to pretend to be an interrupt | |
744 | * controller too. | |
745 | */ | |
746 | if (strcmp(np->name, "AppleKiwi") == 0) | |
747 | continue; | |
748 | /* I think we found one ! */ | |
749 | of_irq_dflt_pic = np; | |
750 | break; | |
751 | } | |
752 | } | |
753 | ||
754 | } | |
755 | ||
aa43f779 | 756 | int of_irq_map_raw(struct device_node *parent, const u32 *intspec, u32 ointsize, |
a7f67bdf | 757 | const u32 *addr, struct of_irq *out_irq) |
cc9fd71c BH |
758 | { |
759 | struct device_node *ipar, *tnode, *old = NULL, *newpar = NULL; | |
a7f67bdf | 760 | const u32 *tmp, *imap, *imask; |
cc9fd71c BH |
761 | u32 intsize = 1, addrsize, newintsize = 0, newaddrsize = 0; |
762 | int imaplen, match, i; | |
763 | ||
006b64de BH |
764 | DBG("of_irq_map_raw: par=%s,intspec=[0x%08x 0x%08x...],ointsize=%d\n", |
765 | parent->full_name, intspec[0], intspec[1], ointsize); | |
766 | ||
cc9fd71c BH |
767 | ipar = of_node_get(parent); |
768 | ||
769 | /* First get the #interrupt-cells property of the current cursor | |
770 | * that tells us how to interpret the passed-in intspec. If there | |
771 | * is none, we are nice and just walk up the tree | |
772 | */ | |
773 | do { | |
e2eb6392 | 774 | tmp = of_get_property(ipar, "#interrupt-cells", NULL); |
cc9fd71c BH |
775 | if (tmp != NULL) { |
776 | intsize = *tmp; | |
777 | break; | |
778 | } | |
779 | tnode = ipar; | |
780 | ipar = of_irq_find_parent(ipar); | |
781 | of_node_put(tnode); | |
782 | } while (ipar); | |
783 | if (ipar == NULL) { | |
784 | DBG(" -> no parent found !\n"); | |
785 | goto fail; | |
786 | } | |
787 | ||
788 | DBG("of_irq_map_raw: ipar=%s, size=%d\n", ipar->full_name, intsize); | |
789 | ||
006b64de BH |
790 | if (ointsize != intsize) |
791 | return -EINVAL; | |
792 | ||
cc9fd71c BH |
793 | /* Look for this #address-cells. We have to implement the old linux |
794 | * trick of looking for the parent here as some device-trees rely on it | |
795 | */ | |
796 | old = of_node_get(ipar); | |
797 | do { | |
e2eb6392 | 798 | tmp = of_get_property(old, "#address-cells", NULL); |
cc9fd71c BH |
799 | tnode = of_get_parent(old); |
800 | of_node_put(old); | |
801 | old = tnode; | |
802 | } while(old && tmp == NULL); | |
803 | of_node_put(old); | |
804 | old = NULL; | |
805 | addrsize = (tmp == NULL) ? 2 : *tmp; | |
806 | ||
807 | DBG(" -> addrsize=%d\n", addrsize); | |
808 | ||
809 | /* Now start the actual "proper" walk of the interrupt tree */ | |
810 | while (ipar != NULL) { | |
811 | /* Now check if cursor is an interrupt-controller and if it is | |
812 | * then we are done | |
813 | */ | |
e2eb6392 SR |
814 | if (of_get_property(ipar, "interrupt-controller", NULL) != |
815 | NULL) { | |
cc9fd71c BH |
816 | DBG(" -> got it !\n"); |
817 | memcpy(out_irq->specifier, intspec, | |
818 | intsize * sizeof(u32)); | |
819 | out_irq->size = intsize; | |
820 | out_irq->controller = ipar; | |
821 | of_node_put(old); | |
822 | return 0; | |
823 | } | |
824 | ||
825 | /* Now look for an interrupt-map */ | |
e2eb6392 | 826 | imap = of_get_property(ipar, "interrupt-map", &imaplen); |
cc9fd71c BH |
827 | /* No interrupt map, check for an interrupt parent */ |
828 | if (imap == NULL) { | |
829 | DBG(" -> no map, getting parent\n"); | |
830 | newpar = of_irq_find_parent(ipar); | |
831 | goto skiplevel; | |
832 | } | |
833 | imaplen /= sizeof(u32); | |
834 | ||
835 | /* Look for a mask */ | |
e2eb6392 | 836 | imask = of_get_property(ipar, "interrupt-map-mask", NULL); |
cc9fd71c BH |
837 | |
838 | /* If we were passed no "reg" property and we attempt to parse | |
839 | * an interrupt-map, then #address-cells must be 0. | |
840 | * Fail if it's not. | |
841 | */ | |
842 | if (addr == NULL && addrsize != 0) { | |
843 | DBG(" -> no reg passed in when needed !\n"); | |
844 | goto fail; | |
845 | } | |
846 | ||
847 | /* Parse interrupt-map */ | |
848 | match = 0; | |
849 | while (imaplen > (addrsize + intsize + 1) && !match) { | |
850 | /* Compare specifiers */ | |
851 | match = 1; | |
852 | for (i = 0; i < addrsize && match; ++i) { | |
853 | u32 mask = imask ? imask[i] : 0xffffffffu; | |
854 | match = ((addr[i] ^ imap[i]) & mask) == 0; | |
855 | } | |
856 | for (; i < (addrsize + intsize) && match; ++i) { | |
857 | u32 mask = imask ? imask[i] : 0xffffffffu; | |
858 | match = | |
859 | ((intspec[i-addrsize] ^ imap[i]) & mask) == 0; | |
860 | } | |
861 | imap += addrsize + intsize; | |
862 | imaplen -= addrsize + intsize; | |
863 | ||
864 | DBG(" -> match=%d (imaplen=%d)\n", match, imaplen); | |
865 | ||
866 | /* Get the interrupt parent */ | |
867 | if (of_irq_workarounds & OF_IMAP_NO_PHANDLE) | |
868 | newpar = of_node_get(of_irq_dflt_pic); | |
869 | else | |
870 | newpar = of_find_node_by_phandle((phandle)*imap); | |
871 | imap++; | |
872 | --imaplen; | |
873 | ||
874 | /* Check if not found */ | |
875 | if (newpar == NULL) { | |
876 | DBG(" -> imap parent not found !\n"); | |
877 | goto fail; | |
878 | } | |
879 | ||
880 | /* Get #interrupt-cells and #address-cells of new | |
881 | * parent | |
882 | */ | |
e2eb6392 | 883 | tmp = of_get_property(newpar, "#interrupt-cells", NULL); |
cc9fd71c BH |
884 | if (tmp == NULL) { |
885 | DBG(" -> parent lacks #interrupt-cells !\n"); | |
886 | goto fail; | |
887 | } | |
888 | newintsize = *tmp; | |
e2eb6392 | 889 | tmp = of_get_property(newpar, "#address-cells", NULL); |
cc9fd71c BH |
890 | newaddrsize = (tmp == NULL) ? 0 : *tmp; |
891 | ||
892 | DBG(" -> newintsize=%d, newaddrsize=%d\n", | |
893 | newintsize, newaddrsize); | |
894 | ||
895 | /* Check for malformed properties */ | |
896 | if (imaplen < (newaddrsize + newintsize)) | |
897 | goto fail; | |
898 | ||
899 | imap += newaddrsize + newintsize; | |
900 | imaplen -= newaddrsize + newintsize; | |
901 | ||
902 | DBG(" -> imaplen=%d\n", imaplen); | |
903 | } | |
904 | if (!match) | |
905 | goto fail; | |
906 | ||
907 | of_node_put(old); | |
908 | old = of_node_get(newpar); | |
909 | addrsize = newaddrsize; | |
910 | intsize = newintsize; | |
911 | intspec = imap - intsize; | |
912 | addr = intspec - addrsize; | |
913 | ||
914 | skiplevel: | |
915 | /* Iterate again with new parent */ | |
916 | DBG(" -> new parent: %s\n", newpar ? newpar->full_name : "<>"); | |
917 | of_node_put(ipar); | |
918 | ipar = newpar; | |
919 | newpar = NULL; | |
920 | } | |
921 | fail: | |
922 | of_node_put(ipar); | |
923 | of_node_put(old); | |
924 | of_node_put(newpar); | |
925 | ||
926 | return -EINVAL; | |
927 | } | |
928 | EXPORT_SYMBOL_GPL(of_irq_map_raw); | |
929 | ||
930 | #if defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32) | |
931 | static int of_irq_map_oldworld(struct device_node *device, int index, | |
932 | struct of_irq *out_irq) | |
933 | { | |
84532c91 | 934 | const u32 *ints = NULL; |
cc9fd71c BH |
935 | int intlen; |
936 | ||
937 | /* | |
938 | * Old machines just have a list of interrupt numbers | |
6f67f9d2 BH |
939 | * and no interrupt-controller nodes. We also have dodgy |
940 | * cases where the APPL,interrupts property is completely | |
941 | * missing behind pci-pci bridges and we have to get it | |
942 | * from the parent (the bridge itself, as apple just wired | |
943 | * everything together on these) | |
cc9fd71c | 944 | */ |
6f67f9d2 | 945 | while (device) { |
e2eb6392 | 946 | ints = of_get_property(device, "AAPL,interrupts", &intlen); |
6f67f9d2 BH |
947 | if (ints != NULL) |
948 | break; | |
949 | device = device->parent; | |
950 | if (device && strcmp(device->type, "pci") != 0) | |
951 | break; | |
952 | } | |
cc9fd71c BH |
953 | if (ints == NULL) |
954 | return -EINVAL; | |
955 | intlen /= sizeof(u32); | |
956 | ||
957 | if (index >= intlen) | |
958 | return -EINVAL; | |
959 | ||
960 | out_irq->controller = NULL; | |
961 | out_irq->specifier[0] = ints[index]; | |
962 | out_irq->size = 1; | |
963 | ||
964 | return 0; | |
965 | } | |
966 | #else /* defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32) */ | |
967 | static int of_irq_map_oldworld(struct device_node *device, int index, | |
968 | struct of_irq *out_irq) | |
969 | { | |
970 | return -EINVAL; | |
971 | } | |
972 | #endif /* !(defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32)) */ | |
973 | ||
974 | int of_irq_map_one(struct device_node *device, int index, struct of_irq *out_irq) | |
975 | { | |
976 | struct device_node *p; | |
a7f67bdf JK |
977 | const u32 *intspec, *tmp, *addr; |
978 | u32 intsize, intlen; | |
cc9fd71c BH |
979 | int res; |
980 | ||
981 | DBG("of_irq_map_one: dev=%s, index=%d\n", device->full_name, index); | |
982 | ||
983 | /* OldWorld mac stuff is "special", handle out of line */ | |
984 | if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC) | |
985 | return of_irq_map_oldworld(device, index, out_irq); | |
986 | ||
987 | /* Get the interrupts property */ | |
e2eb6392 | 988 | intspec = of_get_property(device, "interrupts", &intlen); |
cc9fd71c BH |
989 | if (intspec == NULL) |
990 | return -EINVAL; | |
991 | intlen /= sizeof(u32); | |
992 | ||
993 | /* Get the reg property (if any) */ | |
e2eb6392 | 994 | addr = of_get_property(device, "reg", NULL); |
cc9fd71c BH |
995 | |
996 | /* Look for the interrupt parent. */ | |
997 | p = of_irq_find_parent(device); | |
998 | if (p == NULL) | |
999 | return -EINVAL; | |
1000 | ||
1001 | /* Get size of interrupt specifier */ | |
e2eb6392 | 1002 | tmp = of_get_property(p, "#interrupt-cells", NULL); |
cc9fd71c BH |
1003 | if (tmp == NULL) { |
1004 | of_node_put(p); | |
1005 | return -EINVAL; | |
1006 | } | |
1007 | intsize = *tmp; | |
1008 | ||
006b64de BH |
1009 | DBG(" intsize=%d intlen=%d\n", intsize, intlen); |
1010 | ||
cc9fd71c | 1011 | /* Check index */ |
58d383a6 | 1012 | if ((index + 1) * intsize > intlen) |
cc9fd71c BH |
1013 | return -EINVAL; |
1014 | ||
1015 | /* Get new specifier and map it */ | |
006b64de BH |
1016 | res = of_irq_map_raw(p, intspec + index * intsize, intsize, |
1017 | addr, out_irq); | |
cc9fd71c BH |
1018 | of_node_put(p); |
1019 | return res; | |
1020 | } | |
1021 | EXPORT_SYMBOL_GPL(of_irq_map_one); | |
29cfe6f4 TT |
1022 | |
1023 | /** | |
1024 | * Search the device tree for the best MAC address to use. 'mac-address' is | |
1025 | * checked first, because that is supposed to contain to "most recent" MAC | |
1026 | * address. If that isn't set, then 'local-mac-address' is checked next, | |
1027 | * because that is the default address. If that isn't set, then the obsolete | |
1028 | * 'address' is checked, just in case we're using an old device tree. | |
1029 | * | |
1030 | * Note that the 'address' property is supposed to contain a virtual address of | |
1031 | * the register set, but some DTS files have redefined that property to be the | |
1032 | * MAC address. | |
1033 | * | |
1034 | * All-zero MAC addresses are rejected, because those could be properties that | |
1035 | * exist in the device tree, but were not set by U-Boot. For example, the | |
1036 | * DTS could define 'mac-address' and 'local-mac-address', with zero MAC | |
1037 | * addresses. Some older U-Boots only initialized 'local-mac-address'. In | |
1038 | * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists | |
1039 | * but is all zeros. | |
1040 | */ | |
1041 | const void *of_get_mac_address(struct device_node *np) | |
1042 | { | |
1043 | struct property *pp; | |
1044 | ||
1045 | pp = of_find_property(np, "mac-address", NULL); | |
1046 | if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value)) | |
1047 | return pp->value; | |
1048 | ||
1049 | pp = of_find_property(np, "local-mac-address", NULL); | |
1050 | if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value)) | |
1051 | return pp->value; | |
1052 | ||
1053 | pp = of_find_property(np, "address", NULL); | |
1054 | if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value)) | |
1055 | return pp->value; | |
1056 | ||
1057 | return NULL; | |
1058 | } | |
1059 | EXPORT_SYMBOL(of_get_mac_address); | |
1060 | ||
c0b3ae14 MD |
1061 | int of_irq_to_resource(struct device_node *dev, int index, struct resource *r) |
1062 | { | |
1063 | int irq = irq_of_parse_and_map(dev, index); | |
1064 | ||
1065 | /* Only dereference the resource if both the | |
1066 | * resource and the irq are valid. */ | |
1067 | if (r && irq != NO_IRQ) { | |
1068 | r->start = r->end = irq; | |
1069 | r->flags = IORESOURCE_IRQ; | |
1070 | } | |
1071 | ||
1072 | return irq; | |
1073 | } | |
1074 | EXPORT_SYMBOL_GPL(of_irq_to_resource); | |
c3e8011a CK |
1075 | |
1076 | void __iomem *of_iomap(struct device_node *np, int index) | |
1077 | { | |
1078 | struct resource res; | |
1079 | ||
1080 | if (of_address_to_resource(np, index, &res)) | |
1081 | return NULL; | |
1082 | ||
1083 | return ioremap(res.start, 1 + res.end - res.start); | |
1084 | } | |
1085 | EXPORT_SYMBOL(of_iomap); |