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