Michal Simek | 12e8414 | 2009-03-27 14:25:12 +0100 | [diff] [blame] | 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> |
| 7 | #include <linux/ioport.h> |
| 8 | #include <linux/etherdevice.h> |
Grant Likely | 1f5bef3 | 2010-06-08 07:48:09 -0600 | [diff] [blame^] | 9 | #include <linux/of_address.h> |
Michal Simek | 12e8414 | 2009-03-27 14:25:12 +0100 | [diff] [blame] | 10 | #include <asm/prom.h> |
| 11 | #include <asm/pci-bridge.h> |
| 12 | |
| 13 | #define PRu64 "%llx" |
| 14 | |
| 15 | /* Max address size we deal with */ |
| 16 | #define OF_MAX_ADDR_CELLS 4 |
| 17 | #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \ |
| 18 | (ns) > 0) |
| 19 | |
| 20 | static struct of_bus *of_match_bus(struct device_node *np); |
Michal Simek | 12e8414 | 2009-03-27 14:25:12 +0100 | [diff] [blame] | 21 | |
| 22 | /* Debug utility */ |
| 23 | #ifdef DEBUG |
| 24 | static void of_dump_addr(const char *s, const u32 *addr, int na) |
| 25 | { |
| 26 | printk(KERN_INFO "%s", s); |
| 27 | while (na--) |
| 28 | printk(KERN_INFO " %08x", *(addr++)); |
| 29 | printk(KERN_INFO "\n"); |
| 30 | } |
| 31 | #else |
| 32 | static void of_dump_addr(const char *s, const u32 *addr, int na) { } |
| 33 | #endif |
| 34 | |
| 35 | /* Callbacks for bus specific translators */ |
| 36 | struct of_bus { |
| 37 | const char *name; |
| 38 | const char *addresses; |
| 39 | int (*match)(struct device_node *parent); |
| 40 | void (*count_cells)(struct device_node *child, |
| 41 | int *addrc, int *sizec); |
| 42 | u64 (*map)(u32 *addr, const u32 *range, |
| 43 | int na, int ns, int pna); |
| 44 | int (*translate)(u32 *addr, u64 offset, int na); |
| 45 | unsigned int (*get_flags)(const u32 *addr); |
| 46 | }; |
| 47 | |
| 48 | /* |
| 49 | * Default translator (generic bus) |
| 50 | */ |
| 51 | |
| 52 | static void of_bus_default_count_cells(struct device_node *dev, |
| 53 | int *addrc, int *sizec) |
| 54 | { |
| 55 | if (addrc) |
| 56 | *addrc = of_n_addr_cells(dev); |
| 57 | if (sizec) |
| 58 | *sizec = of_n_size_cells(dev); |
| 59 | } |
| 60 | |
| 61 | static u64 of_bus_default_map(u32 *addr, const u32 *range, |
| 62 | int na, int ns, int pna) |
| 63 | { |
| 64 | u64 cp, s, da; |
| 65 | |
| 66 | cp = of_read_number(range, na); |
| 67 | s = of_read_number(range + na + pna, ns); |
| 68 | da = of_read_number(addr, na); |
| 69 | |
| 70 | pr_debug("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n", |
| 71 | cp, s, da); |
| 72 | |
| 73 | if (da < cp || da >= (cp + s)) |
| 74 | return OF_BAD_ADDR; |
| 75 | return da - cp; |
| 76 | } |
| 77 | |
| 78 | static int of_bus_default_translate(u32 *addr, u64 offset, int na) |
| 79 | { |
| 80 | u64 a = of_read_number(addr, na); |
| 81 | memset(addr, 0, na * 4); |
| 82 | a += offset; |
| 83 | if (na > 1) |
| 84 | addr[na - 2] = a >> 32; |
| 85 | addr[na - 1] = a & 0xffffffffu; |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static unsigned int of_bus_default_get_flags(const u32 *addr) |
| 91 | { |
| 92 | return IORESOURCE_MEM; |
| 93 | } |
| 94 | |
| 95 | #ifdef CONFIG_PCI |
| 96 | /* |
| 97 | * PCI bus specific translator |
| 98 | */ |
| 99 | |
| 100 | static int of_bus_pci_match(struct device_node *np) |
| 101 | { |
| 102 | /* "vci" is for the /chaos bridge on 1st-gen PCI powermacs */ |
| 103 | return !strcmp(np->type, "pci") || !strcmp(np->type, "vci"); |
| 104 | } |
| 105 | |
| 106 | static void of_bus_pci_count_cells(struct device_node *np, |
| 107 | int *addrc, int *sizec) |
| 108 | { |
| 109 | if (addrc) |
| 110 | *addrc = 3; |
| 111 | if (sizec) |
| 112 | *sizec = 2; |
| 113 | } |
| 114 | |
| 115 | static u64 of_bus_pci_map(u32 *addr, const u32 *range, int na, int ns, int pna) |
| 116 | { |
| 117 | u64 cp, s, da; |
| 118 | |
| 119 | /* Check address type match */ |
| 120 | if ((addr[0] ^ range[0]) & 0x03000000) |
| 121 | return OF_BAD_ADDR; |
| 122 | |
| 123 | /* Read address values, skipping high cell */ |
| 124 | cp = of_read_number(range + 1, na - 1); |
| 125 | s = of_read_number(range + na + pna, ns); |
| 126 | da = of_read_number(addr + 1, na - 1); |
| 127 | |
| 128 | pr_debug("OF: PCI map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da); |
| 129 | |
| 130 | if (da < cp || da >= (cp + s)) |
| 131 | return OF_BAD_ADDR; |
| 132 | return da - cp; |
| 133 | } |
| 134 | |
| 135 | static int of_bus_pci_translate(u32 *addr, u64 offset, int na) |
| 136 | { |
| 137 | return of_bus_default_translate(addr + 1, offset, na - 1); |
| 138 | } |
| 139 | |
| 140 | static unsigned int of_bus_pci_get_flags(const u32 *addr) |
| 141 | { |
| 142 | unsigned int flags = 0; |
| 143 | u32 w = addr[0]; |
| 144 | |
| 145 | switch ((w >> 24) & 0x03) { |
| 146 | case 0x01: |
| 147 | flags |= IORESOURCE_IO; |
| 148 | break; |
| 149 | case 0x02: /* 32 bits */ |
| 150 | case 0x03: /* 64 bits */ |
| 151 | flags |= IORESOURCE_MEM; |
| 152 | break; |
| 153 | } |
| 154 | if (w & 0x40000000) |
| 155 | flags |= IORESOURCE_PREFETCH; |
| 156 | return flags; |
| 157 | } |
| 158 | |
| 159 | const u32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size, |
| 160 | unsigned int *flags) |
| 161 | { |
| 162 | const u32 *prop; |
| 163 | unsigned int psize; |
| 164 | struct device_node *parent; |
| 165 | struct of_bus *bus; |
| 166 | int onesize, i, na, ns; |
| 167 | |
| 168 | /* Get parent & match bus type */ |
| 169 | parent = of_get_parent(dev); |
| 170 | if (parent == NULL) |
| 171 | return NULL; |
| 172 | bus = of_match_bus(parent); |
| 173 | if (strcmp(bus->name, "pci")) { |
| 174 | of_node_put(parent); |
| 175 | return NULL; |
| 176 | } |
| 177 | bus->count_cells(dev, &na, &ns); |
| 178 | of_node_put(parent); |
| 179 | if (!OF_CHECK_COUNTS(na, ns)) |
| 180 | return NULL; |
| 181 | |
| 182 | /* Get "reg" or "assigned-addresses" property */ |
| 183 | prop = of_get_property(dev, bus->addresses, &psize); |
| 184 | if (prop == NULL) |
| 185 | return NULL; |
| 186 | psize /= 4; |
| 187 | |
| 188 | onesize = na + ns; |
| 189 | for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) |
| 190 | if ((prop[0] & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) { |
| 191 | if (size) |
| 192 | *size = of_read_number(prop + na, ns); |
| 193 | if (flags) |
| 194 | *flags = bus->get_flags(prop); |
| 195 | return prop; |
| 196 | } |
| 197 | return NULL; |
| 198 | } |
| 199 | EXPORT_SYMBOL(of_get_pci_address); |
| 200 | |
| 201 | int of_pci_address_to_resource(struct device_node *dev, int bar, |
| 202 | struct resource *r) |
| 203 | { |
| 204 | const u32 *addrp; |
| 205 | u64 size; |
| 206 | unsigned int flags; |
| 207 | |
| 208 | addrp = of_get_pci_address(dev, bar, &size, &flags); |
| 209 | if (addrp == NULL) |
| 210 | return -EINVAL; |
| 211 | return __of_address_to_resource(dev, addrp, size, flags, r); |
| 212 | } |
| 213 | EXPORT_SYMBOL_GPL(of_pci_address_to_resource); |
| 214 | |
| 215 | static u8 of_irq_pci_swizzle(u8 slot, u8 pin) |
| 216 | { |
| 217 | return (((pin - 1) + slot) % 4) + 1; |
| 218 | } |
| 219 | |
| 220 | int of_irq_map_pci(struct pci_dev *pdev, struct of_irq *out_irq) |
| 221 | { |
| 222 | struct device_node *dn, *ppnode; |
| 223 | struct pci_dev *ppdev; |
| 224 | u32 lspec; |
| 225 | u32 laddr[3]; |
| 226 | u8 pin; |
| 227 | int rc; |
| 228 | |
| 229 | /* Check if we have a device node, if yes, fallback to standard OF |
| 230 | * parsing |
| 231 | */ |
| 232 | dn = pci_device_to_OF_node(pdev); |
| 233 | if (dn) |
| 234 | return of_irq_map_one(dn, 0, out_irq); |
| 235 | |
| 236 | /* Ok, we don't, time to have fun. Let's start by building up an |
| 237 | * interrupt spec. we assume #interrupt-cells is 1, which is standard |
| 238 | * for PCI. If you do different, then don't use that routine. |
| 239 | */ |
| 240 | rc = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin); |
| 241 | if (rc != 0) |
| 242 | return rc; |
| 243 | /* No pin, exit */ |
| 244 | if (pin == 0) |
| 245 | return -ENODEV; |
| 246 | |
| 247 | /* Now we walk up the PCI tree */ |
| 248 | lspec = pin; |
| 249 | for (;;) { |
| 250 | /* Get the pci_dev of our parent */ |
| 251 | ppdev = pdev->bus->self; |
| 252 | |
| 253 | /* Ouch, it's a host bridge... */ |
| 254 | if (ppdev == NULL) { |
| 255 | struct pci_controller *host; |
| 256 | host = pci_bus_to_host(pdev->bus); |
Michal Simek | 878194c | 2010-01-13 15:29:52 +0100 | [diff] [blame] | 257 | ppnode = host ? host->dn : NULL; |
Michal Simek | 12e8414 | 2009-03-27 14:25:12 +0100 | [diff] [blame] | 258 | /* No node for host bridge ? give up */ |
| 259 | if (ppnode == NULL) |
| 260 | return -EINVAL; |
| 261 | } else |
| 262 | /* We found a P2P bridge, check if it has a node */ |
| 263 | ppnode = pci_device_to_OF_node(ppdev); |
| 264 | |
| 265 | /* Ok, we have found a parent with a device-node, hand over to |
| 266 | * the OF parsing code. |
| 267 | * We build a unit address from the linux device to be used for |
| 268 | * resolution. Note that we use the linux bus number which may |
| 269 | * not match your firmware bus numbering. |
| 270 | * Fortunately, in most cases, interrupt-map-mask doesn't |
| 271 | * include the bus number as part of the matching. |
| 272 | * You should still be careful about that though if you intend |
| 273 | * to rely on this function (you ship a firmware that doesn't |
| 274 | * create device nodes for all PCI devices). |
| 275 | */ |
| 276 | if (ppnode) |
| 277 | break; |
| 278 | |
| 279 | /* We can only get here if we hit a P2P bridge with no node, |
| 280 | * let's do standard swizzling and try again |
| 281 | */ |
| 282 | lspec = of_irq_pci_swizzle(PCI_SLOT(pdev->devfn), lspec); |
| 283 | pdev = ppdev; |
| 284 | } |
| 285 | |
| 286 | laddr[0] = (pdev->bus->number << 16) |
| 287 | | (pdev->devfn << 8); |
| 288 | laddr[1] = laddr[2] = 0; |
| 289 | return of_irq_map_raw(ppnode, &lspec, 1, laddr, out_irq); |
| 290 | } |
| 291 | EXPORT_SYMBOL_GPL(of_irq_map_pci); |
| 292 | #endif /* CONFIG_PCI */ |
| 293 | |
| 294 | /* |
| 295 | * ISA bus specific translator |
| 296 | */ |
| 297 | |
| 298 | static int of_bus_isa_match(struct device_node *np) |
| 299 | { |
| 300 | return !strcmp(np->name, "isa"); |
| 301 | } |
| 302 | |
| 303 | static void of_bus_isa_count_cells(struct device_node *child, |
| 304 | int *addrc, int *sizec) |
| 305 | { |
| 306 | if (addrc) |
| 307 | *addrc = 2; |
| 308 | if (sizec) |
| 309 | *sizec = 1; |
| 310 | } |
| 311 | |
| 312 | static u64 of_bus_isa_map(u32 *addr, const u32 *range, int na, int ns, int pna) |
| 313 | { |
| 314 | u64 cp, s, da; |
| 315 | |
| 316 | /* Check address type match */ |
| 317 | if ((addr[0] ^ range[0]) & 0x00000001) |
| 318 | return OF_BAD_ADDR; |
| 319 | |
| 320 | /* Read address values, skipping high cell */ |
| 321 | cp = of_read_number(range + 1, na - 1); |
| 322 | s = of_read_number(range + na + pna, ns); |
| 323 | da = of_read_number(addr + 1, na - 1); |
| 324 | |
| 325 | pr_debug("OF: ISA map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da); |
| 326 | |
| 327 | if (da < cp || da >= (cp + s)) |
| 328 | return OF_BAD_ADDR; |
| 329 | return da - cp; |
| 330 | } |
| 331 | |
| 332 | static int of_bus_isa_translate(u32 *addr, u64 offset, int na) |
| 333 | { |
| 334 | return of_bus_default_translate(addr + 1, offset, na - 1); |
| 335 | } |
| 336 | |
| 337 | static unsigned int of_bus_isa_get_flags(const u32 *addr) |
| 338 | { |
| 339 | unsigned int flags = 0; |
| 340 | u32 w = addr[0]; |
| 341 | |
| 342 | if (w & 1) |
| 343 | flags |= IORESOURCE_IO; |
| 344 | else |
| 345 | flags |= IORESOURCE_MEM; |
| 346 | return flags; |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * Array of bus specific translators |
| 351 | */ |
| 352 | |
| 353 | static struct of_bus of_busses[] = { |
| 354 | #ifdef CONFIG_PCI |
| 355 | /* PCI */ |
| 356 | { |
| 357 | .name = "pci", |
| 358 | .addresses = "assigned-addresses", |
| 359 | .match = of_bus_pci_match, |
| 360 | .count_cells = of_bus_pci_count_cells, |
| 361 | .map = of_bus_pci_map, |
| 362 | .translate = of_bus_pci_translate, |
| 363 | .get_flags = of_bus_pci_get_flags, |
| 364 | }, |
| 365 | #endif /* CONFIG_PCI */ |
| 366 | /* ISA */ |
| 367 | { |
| 368 | .name = "isa", |
| 369 | .addresses = "reg", |
| 370 | .match = of_bus_isa_match, |
| 371 | .count_cells = of_bus_isa_count_cells, |
| 372 | .map = of_bus_isa_map, |
| 373 | .translate = of_bus_isa_translate, |
| 374 | .get_flags = of_bus_isa_get_flags, |
| 375 | }, |
| 376 | /* Default */ |
| 377 | { |
| 378 | .name = "default", |
| 379 | .addresses = "reg", |
| 380 | .match = NULL, |
| 381 | .count_cells = of_bus_default_count_cells, |
| 382 | .map = of_bus_default_map, |
| 383 | .translate = of_bus_default_translate, |
| 384 | .get_flags = of_bus_default_get_flags, |
| 385 | }, |
| 386 | }; |
| 387 | |
| 388 | static struct of_bus *of_match_bus(struct device_node *np) |
| 389 | { |
| 390 | int i; |
| 391 | |
| 392 | for (i = 0; i < ARRAY_SIZE(of_busses); i++) |
| 393 | if (!of_busses[i].match || of_busses[i].match(np)) |
| 394 | return &of_busses[i]; |
| 395 | BUG(); |
| 396 | return NULL; |
| 397 | } |
| 398 | |
| 399 | static int of_translate_one(struct device_node *parent, struct of_bus *bus, |
| 400 | struct of_bus *pbus, u32 *addr, |
| 401 | int na, int ns, int pna) |
| 402 | { |
| 403 | const u32 *ranges; |
| 404 | unsigned int rlen; |
| 405 | int rone; |
| 406 | u64 offset = OF_BAD_ADDR; |
| 407 | |
| 408 | /* Normally, an absence of a "ranges" property means we are |
| 409 | * crossing a non-translatable boundary, and thus the addresses |
| 410 | * below the current not cannot be converted to CPU physical ones. |
| 411 | * Unfortunately, while this is very clear in the spec, it's not |
| 412 | * what Apple understood, and they do have things like /uni-n or |
| 413 | * /ht nodes with no "ranges" property and a lot of perfectly |
| 414 | * useable mapped devices below them. Thus we treat the absence of |
| 415 | * "ranges" as equivalent to an empty "ranges" property which means |
| 416 | * a 1:1 translation at that level. It's up to the caller not to try |
| 417 | * to translate addresses that aren't supposed to be translated in |
| 418 | * the first place. --BenH. |
| 419 | */ |
| 420 | ranges = of_get_property(parent, "ranges", (int *) &rlen); |
| 421 | if (ranges == NULL || rlen == 0) { |
| 422 | offset = of_read_number(addr, na); |
| 423 | memset(addr, 0, pna * 4); |
| 424 | pr_debug("OF: no ranges, 1:1 translation\n"); |
| 425 | goto finish; |
| 426 | } |
| 427 | |
| 428 | pr_debug("OF: walking ranges...\n"); |
| 429 | |
| 430 | /* Now walk through the ranges */ |
| 431 | rlen /= 4; |
| 432 | rone = na + pna + ns; |
| 433 | for (; rlen >= rone; rlen -= rone, ranges += rone) { |
| 434 | offset = bus->map(addr, ranges, na, ns, pna); |
| 435 | if (offset != OF_BAD_ADDR) |
| 436 | break; |
| 437 | } |
| 438 | if (offset == OF_BAD_ADDR) { |
| 439 | pr_debug("OF: not found !\n"); |
| 440 | return 1; |
| 441 | } |
| 442 | memcpy(addr, ranges + na, 4 * pna); |
| 443 | |
| 444 | finish: |
| 445 | of_dump_addr("OF: parent translation for:", addr, pna); |
| 446 | pr_debug("OF: with offset: "PRu64"\n", offset); |
| 447 | |
| 448 | /* Translate it into parent bus space */ |
| 449 | return pbus->translate(addr, offset, pna); |
| 450 | } |
| 451 | |
| 452 | /* |
| 453 | * Translate an address from the device-tree into a CPU physical address, |
| 454 | * this walks up the tree and applies the various bus mappings on the |
| 455 | * way. |
| 456 | * |
| 457 | * Note: We consider that crossing any level with #size-cells == 0 to mean |
| 458 | * that translation is impossible (that is we are not dealing with a value |
| 459 | * that can be mapped to a cpu physical address). This is not really specified |
| 460 | * that way, but this is traditionally the way IBM at least do things |
| 461 | */ |
| 462 | u64 of_translate_address(struct device_node *dev, const u32 *in_addr) |
| 463 | { |
| 464 | struct device_node *parent = NULL; |
| 465 | struct of_bus *bus, *pbus; |
| 466 | u32 addr[OF_MAX_ADDR_CELLS]; |
| 467 | int na, ns, pna, pns; |
| 468 | u64 result = OF_BAD_ADDR; |
| 469 | |
| 470 | pr_debug("OF: ** translation for device %s **\n", dev->full_name); |
| 471 | |
| 472 | /* Increase refcount at current level */ |
| 473 | of_node_get(dev); |
| 474 | |
| 475 | /* Get parent & match bus type */ |
| 476 | parent = of_get_parent(dev); |
| 477 | if (parent == NULL) |
| 478 | goto bail; |
| 479 | bus = of_match_bus(parent); |
| 480 | |
| 481 | /* Cound address cells & copy address locally */ |
| 482 | bus->count_cells(dev, &na, &ns); |
| 483 | if (!OF_CHECK_COUNTS(na, ns)) { |
| 484 | printk(KERN_ERR "prom_parse: Bad cell count for %s\n", |
| 485 | dev->full_name); |
| 486 | goto bail; |
| 487 | } |
| 488 | memcpy(addr, in_addr, na * 4); |
| 489 | |
| 490 | pr_debug("OF: bus is %s (na=%d, ns=%d) on %s\n", |
| 491 | bus->name, na, ns, parent->full_name); |
| 492 | of_dump_addr("OF: translating address:", addr, na); |
| 493 | |
| 494 | /* Translate */ |
| 495 | for (;;) { |
| 496 | /* Switch to parent bus */ |
| 497 | of_node_put(dev); |
| 498 | dev = parent; |
| 499 | parent = of_get_parent(dev); |
| 500 | |
| 501 | /* If root, we have finished */ |
| 502 | if (parent == NULL) { |
| 503 | pr_debug("OF: reached root node\n"); |
| 504 | result = of_read_number(addr, na); |
| 505 | break; |
| 506 | } |
| 507 | |
| 508 | /* Get new parent bus and counts */ |
| 509 | pbus = of_match_bus(parent); |
| 510 | pbus->count_cells(dev, &pna, &pns); |
| 511 | if (!OF_CHECK_COUNTS(pna, pns)) { |
| 512 | printk(KERN_ERR "prom_parse: Bad cell count for %s\n", |
| 513 | dev->full_name); |
| 514 | break; |
| 515 | } |
| 516 | |
| 517 | pr_debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n", |
| 518 | pbus->name, pna, pns, parent->full_name); |
| 519 | |
| 520 | /* Apply bus translation */ |
| 521 | if (of_translate_one(dev, bus, pbus, addr, na, ns, pna)) |
| 522 | break; |
| 523 | |
| 524 | /* Complete the move up one level */ |
| 525 | na = pna; |
| 526 | ns = pns; |
| 527 | bus = pbus; |
| 528 | |
| 529 | of_dump_addr("OF: one level translation:", addr, na); |
| 530 | } |
| 531 | bail: |
| 532 | of_node_put(parent); |
| 533 | of_node_put(dev); |
| 534 | |
| 535 | return result; |
| 536 | } |
| 537 | EXPORT_SYMBOL(of_translate_address); |
| 538 | |
| 539 | const u32 *of_get_address(struct device_node *dev, int index, u64 *size, |
| 540 | unsigned int *flags) |
| 541 | { |
| 542 | const u32 *prop; |
| 543 | unsigned int psize; |
| 544 | struct device_node *parent; |
| 545 | struct of_bus *bus; |
| 546 | int onesize, i, na, ns; |
| 547 | |
| 548 | /* Get parent & match bus type */ |
| 549 | parent = of_get_parent(dev); |
| 550 | if (parent == NULL) |
| 551 | return NULL; |
| 552 | bus = of_match_bus(parent); |
| 553 | bus->count_cells(dev, &na, &ns); |
| 554 | of_node_put(parent); |
| 555 | if (!OF_CHECK_COUNTS(na, ns)) |
| 556 | return NULL; |
| 557 | |
| 558 | /* Get "reg" or "assigned-addresses" property */ |
| 559 | prop = of_get_property(dev, bus->addresses, (int *) &psize); |
| 560 | if (prop == NULL) |
| 561 | return NULL; |
| 562 | psize /= 4; |
| 563 | |
| 564 | onesize = na + ns; |
| 565 | for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) |
| 566 | if (i == index) { |
| 567 | if (size) |
| 568 | *size = of_read_number(prop + na, ns); |
| 569 | if (flags) |
| 570 | *flags = bus->get_flags(prop); |
| 571 | return prop; |
| 572 | } |
| 573 | return NULL; |
| 574 | } |
| 575 | EXPORT_SYMBOL(of_get_address); |
| 576 | |
Michal Simek | 12e8414 | 2009-03-27 14:25:12 +0100 | [diff] [blame] | 577 | void of_parse_dma_window(struct device_node *dn, const void *dma_window_prop, |
| 578 | unsigned long *busno, unsigned long *phys, unsigned long *size) |
| 579 | { |
| 580 | const u32 *dma_window; |
| 581 | u32 cells; |
| 582 | const unsigned char *prop; |
| 583 | |
| 584 | dma_window = dma_window_prop; |
| 585 | |
| 586 | /* busno is always one cell */ |
| 587 | *busno = *(dma_window++); |
| 588 | |
| 589 | prop = of_get_property(dn, "ibm,#dma-address-cells", NULL); |
| 590 | if (!prop) |
| 591 | prop = of_get_property(dn, "#address-cells", NULL); |
| 592 | |
| 593 | cells = prop ? *(u32 *)prop : of_n_addr_cells(dn); |
| 594 | *phys = of_read_number(dma_window, cells); |
| 595 | |
| 596 | dma_window += cells; |
| 597 | |
| 598 | prop = of_get_property(dn, "ibm,#dma-size-cells", NULL); |
| 599 | cells = prop ? *(u32 *)prop : of_n_size_cells(dn); |
| 600 | *size = of_read_number(dma_window, cells); |
| 601 | } |
| 602 | |
Michal Simek | 12e8414 | 2009-03-27 14:25:12 +0100 | [diff] [blame] | 603 | /** |
| 604 | * Search the device tree for the best MAC address to use. 'mac-address' is |
| 605 | * checked first, because that is supposed to contain to "most recent" MAC |
| 606 | * address. If that isn't set, then 'local-mac-address' is checked next, |
| 607 | * because that is the default address. If that isn't set, then the obsolete |
| 608 | * 'address' is checked, just in case we're using an old device tree. |
| 609 | * |
| 610 | * Note that the 'address' property is supposed to contain a virtual address of |
| 611 | * the register set, but some DTS files have redefined that property to be the |
| 612 | * MAC address. |
| 613 | * |
| 614 | * All-zero MAC addresses are rejected, because those could be properties that |
| 615 | * exist in the device tree, but were not set by U-Boot. For example, the |
| 616 | * DTS could define 'mac-address' and 'local-mac-address', with zero MAC |
| 617 | * addresses. Some older U-Boots only initialized 'local-mac-address'. In |
| 618 | * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists |
| 619 | * but is all zeros. |
| 620 | */ |
| 621 | const void *of_get_mac_address(struct device_node *np) |
| 622 | { |
| 623 | struct property *pp; |
| 624 | |
| 625 | pp = of_find_property(np, "mac-address", NULL); |
| 626 | if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value)) |
| 627 | return pp->value; |
| 628 | |
| 629 | pp = of_find_property(np, "local-mac-address", NULL); |
| 630 | if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value)) |
| 631 | return pp->value; |
| 632 | |
| 633 | pp = of_find_property(np, "address", NULL); |
| 634 | if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value)) |
| 635 | return pp->value; |
| 636 | |
| 637 | return NULL; |
| 638 | } |
| 639 | EXPORT_SYMBOL(of_get_mac_address); |