Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Procedures for interfacing to the Open Firmware PROM on |
| 3 | * Power Macintosh computers. |
| 4 | * |
| 5 | * In particular, we are interested in the device tree |
| 6 | * and in using some of its services (exit, write to stdout). |
| 7 | * |
| 8 | * Paul Mackerras August 1996. |
| 9 | * Copyright (C) 1996 Paul Mackerras. |
| 10 | */ |
| 11 | #include <stdarg.h> |
| 12 | #include <linux/config.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/string.h> |
| 15 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/threads.h> |
| 17 | #include <linux/spinlock.h> |
| 18 | #include <linux/ioport.h> |
| 19 | #include <linux/pci.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/bitops.h> |
| 22 | |
| 23 | #include <asm/sections.h> |
| 24 | #include <asm/prom.h> |
| 25 | #include <asm/page.h> |
| 26 | #include <asm/processor.h> |
| 27 | #include <asm/irq.h> |
| 28 | #include <asm/io.h> |
| 29 | #include <asm/smp.h> |
| 30 | #include <asm/bootx.h> |
| 31 | #include <asm/system.h> |
| 32 | #include <asm/mmu.h> |
| 33 | #include <asm/pgtable.h> |
| 34 | #include <asm/bootinfo.h> |
| 35 | #include <asm/btext.h> |
| 36 | #include <asm/pci-bridge.h> |
| 37 | #include <asm/open_pic.h> |
| 38 | |
| 39 | |
| 40 | struct pci_address { |
| 41 | unsigned a_hi; |
| 42 | unsigned a_mid; |
| 43 | unsigned a_lo; |
| 44 | }; |
| 45 | |
| 46 | struct pci_reg_property { |
| 47 | struct pci_address addr; |
| 48 | unsigned size_hi; |
| 49 | unsigned size_lo; |
| 50 | }; |
| 51 | |
| 52 | struct isa_reg_property { |
| 53 | unsigned space; |
| 54 | unsigned address; |
| 55 | unsigned size; |
| 56 | }; |
| 57 | |
| 58 | typedef unsigned long interpret_func(struct device_node *, unsigned long, |
| 59 | int, int); |
| 60 | static interpret_func interpret_pci_props; |
| 61 | static interpret_func interpret_dbdma_props; |
| 62 | static interpret_func interpret_isa_props; |
| 63 | static interpret_func interpret_macio_props; |
| 64 | static interpret_func interpret_root_props; |
| 65 | |
| 66 | extern char *klimit; |
| 67 | |
| 68 | /* Set for a newworld or CHRP machine */ |
| 69 | int use_of_interrupt_tree; |
| 70 | struct device_node *dflt_interrupt_controller; |
| 71 | int num_interrupt_controllers; |
| 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | extern unsigned int rtas_entry; /* physical pointer */ |
| 74 | |
| 75 | extern struct device_node *allnodes; |
| 76 | |
| 77 | static unsigned long finish_node(struct device_node *, unsigned long, |
| 78 | interpret_func *, int, int); |
| 79 | static unsigned long finish_node_interrupts(struct device_node *, unsigned long); |
| 80 | static struct device_node *find_phandle(phandle); |
| 81 | |
| 82 | extern void enter_rtas(void *); |
| 83 | void phys_call_rtas(int, int, int, ...); |
| 84 | |
| 85 | extern char cmd_line[512]; /* XXX */ |
| 86 | extern boot_infos_t *boot_infos; |
| 87 | unsigned long dev_tree_size; |
| 88 | |
Jon Loeliger | f495a8b | 2005-09-17 10:35:08 -0500 | [diff] [blame] | 89 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | phys_call_rtas(int service, int nargs, int nret, ...) |
| 91 | { |
| 92 | va_list list; |
| 93 | union { |
| 94 | unsigned long words[16]; |
| 95 | double align; |
| 96 | } u; |
| 97 | void (*rtas)(void *, unsigned long); |
| 98 | int i; |
| 99 | |
| 100 | u.words[0] = service; |
| 101 | u.words[1] = nargs; |
| 102 | u.words[2] = nret; |
| 103 | va_start(list, nret); |
| 104 | for (i = 0; i < nargs; ++i) |
| 105 | u.words[i+3] = va_arg(list, unsigned long); |
| 106 | va_end(list); |
| 107 | |
| 108 | rtas = (void (*)(void *, unsigned long)) rtas_entry; |
| 109 | rtas(&u, rtas_data); |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * finish_device_tree is called once things are running normally |
| 114 | * (i.e. with text and data mapped to the address they were linked at). |
| 115 | * It traverses the device tree and fills in the name, type, |
| 116 | * {n_}addrs and {n_}intrs fields of each node. |
| 117 | */ |
| 118 | void __init |
| 119 | finish_device_tree(void) |
| 120 | { |
| 121 | unsigned long mem = (unsigned long) klimit; |
| 122 | struct device_node *np; |
| 123 | |
Paul Mackerras | a7fdd90 | 2006-01-15 17:30:44 +1100 | [diff] [blame^] | 124 | /* All CHRPs now use the interrupt tree */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | for (np = allnodes; np != NULL; np = np->allnext) { |
| 126 | if (get_property(np, "interrupt-parent", NULL)) { |
| 127 | use_of_interrupt_tree = 1; |
| 128 | break; |
| 129 | } |
| 130 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
| 132 | if (use_of_interrupt_tree) { |
| 133 | /* |
| 134 | * We want to find out here how many interrupt-controller |
| 135 | * nodes there are, and if we are booted from BootX, |
| 136 | * we need a pointer to the first (and hopefully only) |
| 137 | * such node. But we can't use find_devices here since |
| 138 | * np->name has not been set yet. -- paulus |
| 139 | */ |
| 140 | int n = 0; |
| 141 | char *name, *ic; |
| 142 | int iclen; |
| 143 | |
| 144 | for (np = allnodes; np != NULL; np = np->allnext) { |
| 145 | ic = get_property(np, "interrupt-controller", &iclen); |
| 146 | name = get_property(np, "name", NULL); |
| 147 | /* checking iclen makes sure we don't get a false |
| 148 | match on /chosen.interrupt_controller */ |
| 149 | if ((name != NULL |
| 150 | && strcmp(name, "interrupt-controller") == 0) |
| 151 | || (ic != NULL && iclen == 0 && strcmp(name, "AppleKiwi"))) { |
| 152 | if (n == 0) |
| 153 | dflt_interrupt_controller = np; |
| 154 | ++n; |
| 155 | } |
| 156 | } |
| 157 | num_interrupt_controllers = n; |
| 158 | } |
| 159 | |
| 160 | mem = finish_node(allnodes, mem, NULL, 1, 1); |
| 161 | dev_tree_size = mem - (unsigned long) allnodes; |
| 162 | klimit = (char *) mem; |
| 163 | } |
| 164 | |
| 165 | static unsigned long __init |
| 166 | finish_node(struct device_node *np, unsigned long mem_start, |
| 167 | interpret_func *ifunc, int naddrc, int nsizec) |
| 168 | { |
| 169 | struct device_node *child; |
| 170 | int *ip; |
| 171 | |
| 172 | np->name = get_property(np, "name", NULL); |
| 173 | np->type = get_property(np, "device_type", NULL); |
| 174 | |
| 175 | if (!np->name) |
| 176 | np->name = "<NULL>"; |
| 177 | if (!np->type) |
| 178 | np->type = "<NULL>"; |
| 179 | |
| 180 | /* get the device addresses and interrupts */ |
| 181 | if (ifunc != NULL) |
| 182 | mem_start = ifunc(np, mem_start, naddrc, nsizec); |
| 183 | |
| 184 | if (use_of_interrupt_tree) |
| 185 | mem_start = finish_node_interrupts(np, mem_start); |
| 186 | |
| 187 | /* Look for #address-cells and #size-cells properties. */ |
| 188 | ip = (int *) get_property(np, "#address-cells", NULL); |
| 189 | if (ip != NULL) |
| 190 | naddrc = *ip; |
| 191 | ip = (int *) get_property(np, "#size-cells", NULL); |
| 192 | if (ip != NULL) |
| 193 | nsizec = *ip; |
| 194 | |
| 195 | if (np->parent == NULL) |
| 196 | ifunc = interpret_root_props; |
| 197 | else if (np->type == 0) |
| 198 | ifunc = NULL; |
| 199 | else if (!strcmp(np->type, "pci") || !strcmp(np->type, "vci")) |
| 200 | ifunc = interpret_pci_props; |
| 201 | else if (!strcmp(np->type, "dbdma")) |
| 202 | ifunc = interpret_dbdma_props; |
| 203 | else if (!strcmp(np->type, "mac-io") |
| 204 | || ifunc == interpret_macio_props) |
| 205 | ifunc = interpret_macio_props; |
| 206 | else if (!strcmp(np->type, "isa")) |
| 207 | ifunc = interpret_isa_props; |
| 208 | else if (!strcmp(np->name, "uni-n") || !strcmp(np->name, "u3")) |
| 209 | ifunc = interpret_root_props; |
| 210 | else if (!((ifunc == interpret_dbdma_props |
| 211 | || ifunc == interpret_macio_props) |
| 212 | && (!strcmp(np->type, "escc") |
| 213 | || !strcmp(np->type, "media-bay")))) |
| 214 | ifunc = NULL; |
| 215 | |
| 216 | /* if we were booted from BootX, convert the full name */ |
| 217 | if (boot_infos |
| 218 | && strncmp(np->full_name, "Devices:device-tree", 19) == 0) { |
| 219 | if (np->full_name[19] == 0) { |
| 220 | strcpy(np->full_name, "/"); |
| 221 | } else if (np->full_name[19] == ':') { |
| 222 | char *p = np->full_name + 19; |
| 223 | np->full_name = p; |
| 224 | for (; *p; ++p) |
| 225 | if (*p == ':') |
| 226 | *p = '/'; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | for (child = np->child; child != NULL; child = child->sibling) |
| 231 | mem_start = finish_node(child, mem_start, ifunc, |
| 232 | naddrc, nsizec); |
| 233 | |
| 234 | return mem_start; |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * Find the interrupt parent of a node. |
| 239 | */ |
| 240 | static struct device_node * __init |
| 241 | intr_parent(struct device_node *p) |
| 242 | { |
| 243 | phandle *parp; |
| 244 | |
| 245 | parp = (phandle *) get_property(p, "interrupt-parent", NULL); |
| 246 | if (parp == NULL) |
| 247 | return p->parent; |
| 248 | p = find_phandle(*parp); |
| 249 | if (p != NULL) |
| 250 | return p; |
| 251 | /* |
| 252 | * On a powermac booted with BootX, we don't get to know the |
| 253 | * phandles for any nodes, so find_phandle will return NULL. |
| 254 | * Fortunately these machines only have one interrupt controller |
| 255 | * so there isn't in fact any ambiguity. -- paulus |
| 256 | */ |
| 257 | if (num_interrupt_controllers == 1) |
| 258 | p = dflt_interrupt_controller; |
| 259 | return p; |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * Find out the size of each entry of the interrupts property |
| 264 | * for a node. |
| 265 | */ |
| 266 | static int __init |
| 267 | prom_n_intr_cells(struct device_node *np) |
| 268 | { |
| 269 | struct device_node *p; |
| 270 | unsigned int *icp; |
| 271 | |
| 272 | for (p = np; (p = intr_parent(p)) != NULL; ) { |
| 273 | icp = (unsigned int *) |
| 274 | get_property(p, "#interrupt-cells", NULL); |
| 275 | if (icp != NULL) |
| 276 | return *icp; |
| 277 | if (get_property(p, "interrupt-controller", NULL) != NULL |
| 278 | || get_property(p, "interrupt-map", NULL) != NULL) { |
| 279 | printk("oops, node %s doesn't have #interrupt-cells\n", |
| 280 | p->full_name); |
| 281 | return 1; |
| 282 | } |
| 283 | } |
| 284 | printk("prom_n_intr_cells failed for %s\n", np->full_name); |
| 285 | return 1; |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * Map an interrupt from a device up to the platform interrupt |
| 290 | * descriptor. |
| 291 | */ |
| 292 | static int __init |
| 293 | map_interrupt(unsigned int **irq, struct device_node **ictrler, |
| 294 | struct device_node *np, unsigned int *ints, int nintrc) |
| 295 | { |
| 296 | struct device_node *p, *ipar; |
| 297 | unsigned int *imap, *imask, *ip; |
| 298 | int i, imaplen, match; |
| 299 | int newintrc = 1, newaddrc = 1; |
| 300 | unsigned int *reg; |
| 301 | int naddrc; |
| 302 | |
| 303 | reg = (unsigned int *) get_property(np, "reg", NULL); |
| 304 | naddrc = prom_n_addr_cells(np); |
| 305 | p = intr_parent(np); |
| 306 | while (p != NULL) { |
| 307 | if (get_property(p, "interrupt-controller", NULL) != NULL) |
| 308 | /* this node is an interrupt controller, stop here */ |
| 309 | break; |
| 310 | imap = (unsigned int *) |
| 311 | get_property(p, "interrupt-map", &imaplen); |
| 312 | if (imap == NULL) { |
| 313 | p = intr_parent(p); |
| 314 | continue; |
| 315 | } |
| 316 | imask = (unsigned int *) |
| 317 | get_property(p, "interrupt-map-mask", NULL); |
| 318 | if (imask == NULL) { |
| 319 | printk("oops, %s has interrupt-map but no mask\n", |
| 320 | p->full_name); |
| 321 | return 0; |
| 322 | } |
| 323 | imaplen /= sizeof(unsigned int); |
| 324 | match = 0; |
| 325 | ipar = NULL; |
| 326 | while (imaplen > 0 && !match) { |
| 327 | /* check the child-interrupt field */ |
| 328 | match = 1; |
| 329 | for (i = 0; i < naddrc && match; ++i) |
| 330 | match = ((reg[i] ^ imap[i]) & imask[i]) == 0; |
| 331 | for (; i < naddrc + nintrc && match; ++i) |
| 332 | match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0; |
| 333 | imap += naddrc + nintrc; |
| 334 | imaplen -= naddrc + nintrc; |
| 335 | /* grab the interrupt parent */ |
| 336 | ipar = find_phandle((phandle) *imap++); |
| 337 | --imaplen; |
| 338 | if (ipar == NULL && num_interrupt_controllers == 1) |
| 339 | /* cope with BootX not giving us phandles */ |
| 340 | ipar = dflt_interrupt_controller; |
| 341 | if (ipar == NULL) { |
| 342 | printk("oops, no int parent %x in map of %s\n", |
| 343 | imap[-1], p->full_name); |
| 344 | return 0; |
| 345 | } |
| 346 | /* find the parent's # addr and intr cells */ |
| 347 | ip = (unsigned int *) |
| 348 | get_property(ipar, "#interrupt-cells", NULL); |
| 349 | if (ip == NULL) { |
| 350 | printk("oops, no #interrupt-cells on %s\n", |
| 351 | ipar->full_name); |
| 352 | return 0; |
| 353 | } |
| 354 | newintrc = *ip; |
| 355 | ip = (unsigned int *) |
| 356 | get_property(ipar, "#address-cells", NULL); |
| 357 | newaddrc = (ip == NULL)? 0: *ip; |
| 358 | imap += newaddrc + newintrc; |
| 359 | imaplen -= newaddrc + newintrc; |
| 360 | } |
| 361 | if (imaplen < 0) { |
| 362 | printk("oops, error decoding int-map on %s, len=%d\n", |
| 363 | p->full_name, imaplen); |
| 364 | return 0; |
| 365 | } |
| 366 | if (!match) { |
| 367 | printk("oops, no match in %s int-map for %s\n", |
| 368 | p->full_name, np->full_name); |
| 369 | return 0; |
| 370 | } |
| 371 | p = ipar; |
| 372 | naddrc = newaddrc; |
| 373 | nintrc = newintrc; |
| 374 | ints = imap - nintrc; |
| 375 | reg = ints - naddrc; |
| 376 | } |
| 377 | if (p == NULL) |
| 378 | printk("hmmm, int tree for %s doesn't have ctrler\n", |
| 379 | np->full_name); |
| 380 | *irq = ints; |
| 381 | *ictrler = p; |
| 382 | return nintrc; |
| 383 | } |
| 384 | |
| 385 | /* |
| 386 | * New version of finish_node_interrupts. |
| 387 | */ |
| 388 | static unsigned long __init |
| 389 | finish_node_interrupts(struct device_node *np, unsigned long mem_start) |
| 390 | { |
| 391 | unsigned int *ints; |
| 392 | int intlen, intrcells; |
| 393 | int i, j, n, offset; |
| 394 | unsigned int *irq; |
| 395 | struct device_node *ic; |
| 396 | |
| 397 | ints = (unsigned int *) get_property(np, "interrupts", &intlen); |
| 398 | if (ints == NULL) |
| 399 | return mem_start; |
| 400 | intrcells = prom_n_intr_cells(np); |
| 401 | intlen /= intrcells * sizeof(unsigned int); |
| 402 | np->n_intrs = intlen; |
| 403 | np->intrs = (struct interrupt_info *) mem_start; |
| 404 | mem_start += intlen * sizeof(struct interrupt_info); |
| 405 | |
| 406 | for (i = 0; i < intlen; ++i) { |
| 407 | np->intrs[i].line = 0; |
| 408 | np->intrs[i].sense = 1; |
| 409 | n = map_interrupt(&irq, &ic, np, ints, intrcells); |
| 410 | if (n <= 0) |
| 411 | continue; |
| 412 | offset = 0; |
| 413 | /* |
| 414 | * On a CHRP we have an 8259 which is subordinate to |
| 415 | * the openpic in the interrupt tree, but we want the |
| 416 | * openpic's interrupt numbers offsetted, not the 8259's. |
| 417 | * So we apply the offset if the controller is at the |
| 418 | * root of the interrupt tree, i.e. has no interrupt-parent. |
| 419 | * This doesn't cope with the general case of multiple |
| 420 | * cascaded interrupt controllers, but then neither will |
| 421 | * irq.c at the moment either. -- paulus |
| 422 | * The G5 triggers that code, I add a machine test. On |
| 423 | * those machines, we want to offset interrupts from the |
| 424 | * second openpic by 128 -- BenH |
| 425 | */ |
Paul Mackerras | a7fdd90 | 2006-01-15 17:30:44 +1100 | [diff] [blame^] | 426 | if (num_interrupt_controllers > 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | && ic != NULL |
| 428 | && get_property(ic, "interrupt-parent", NULL) == NULL) |
| 429 | offset = 16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
| 431 | np->intrs[i].line = irq[0] + offset; |
| 432 | if (n > 1) |
| 433 | np->intrs[i].sense = irq[1]; |
| 434 | if (n > 2) { |
| 435 | printk("hmmm, got %d intr cells for %s:", n, |
| 436 | np->full_name); |
| 437 | for (j = 0; j < n; ++j) |
| 438 | printk(" %d", irq[j]); |
| 439 | printk("\n"); |
| 440 | } |
| 441 | ints += intrcells; |
| 442 | } |
| 443 | |
| 444 | return mem_start; |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * When BootX makes a copy of the device tree from the MacOS |
| 449 | * Name Registry, it is in the format we use but all of the pointers |
| 450 | * are offsets from the start of the tree. |
| 451 | * This procedure updates the pointers. |
| 452 | */ |
| 453 | void __init |
| 454 | relocate_nodes(void) |
| 455 | { |
| 456 | unsigned long base; |
| 457 | struct device_node *np; |
| 458 | struct property *pp; |
| 459 | |
| 460 | #define ADDBASE(x) (x = (typeof (x))((x)? ((unsigned long)(x) + base): 0)) |
| 461 | |
| 462 | base = (unsigned long) boot_infos + boot_infos->deviceTreeOffset; |
| 463 | allnodes = (struct device_node *)(base + 4); |
| 464 | for (np = allnodes; np != 0; np = np->allnext) { |
| 465 | ADDBASE(np->full_name); |
| 466 | ADDBASE(np->properties); |
| 467 | ADDBASE(np->parent); |
| 468 | ADDBASE(np->child); |
| 469 | ADDBASE(np->sibling); |
| 470 | ADDBASE(np->allnext); |
| 471 | for (pp = np->properties; pp != 0; pp = pp->next) { |
| 472 | ADDBASE(pp->name); |
| 473 | ADDBASE(pp->value); |
| 474 | ADDBASE(pp->next); |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | int |
| 480 | prom_n_addr_cells(struct device_node* np) |
| 481 | { |
| 482 | int* ip; |
| 483 | do { |
| 484 | if (np->parent) |
| 485 | np = np->parent; |
| 486 | ip = (int *) get_property(np, "#address-cells", NULL); |
| 487 | if (ip != NULL) |
| 488 | return *ip; |
| 489 | } while (np->parent); |
| 490 | /* No #address-cells property for the root node, default to 1 */ |
| 491 | return 1; |
| 492 | } |
| 493 | |
| 494 | int |
| 495 | prom_n_size_cells(struct device_node* np) |
| 496 | { |
| 497 | int* ip; |
| 498 | do { |
| 499 | if (np->parent) |
| 500 | np = np->parent; |
| 501 | ip = (int *) get_property(np, "#size-cells", NULL); |
| 502 | if (ip != NULL) |
| 503 | return *ip; |
| 504 | } while (np->parent); |
| 505 | /* No #size-cells property for the root node, default to 1 */ |
| 506 | return 1; |
| 507 | } |
| 508 | |
| 509 | static unsigned long __init |
| 510 | map_addr(struct device_node *np, unsigned long space, unsigned long addr) |
| 511 | { |
| 512 | int na; |
| 513 | unsigned int *ranges; |
| 514 | int rlen = 0; |
| 515 | unsigned int type; |
| 516 | |
| 517 | type = (space >> 24) & 3; |
| 518 | if (type == 0) |
| 519 | return addr; |
| 520 | |
| 521 | while ((np = np->parent) != NULL) { |
| 522 | if (strcmp(np->type, "pci") != 0) |
| 523 | continue; |
| 524 | /* PCI bridge: map the address through the ranges property */ |
| 525 | na = prom_n_addr_cells(np); |
| 526 | ranges = (unsigned int *) get_property(np, "ranges", &rlen); |
| 527 | while ((rlen -= (na + 5) * sizeof(unsigned int)) >= 0) { |
| 528 | if (((ranges[0] >> 24) & 3) == type |
| 529 | && ranges[2] <= addr |
| 530 | && addr - ranges[2] < ranges[na+4]) { |
| 531 | /* ok, this matches, translate it */ |
| 532 | addr += ranges[na+2] - ranges[2]; |
| 533 | break; |
| 534 | } |
| 535 | ranges += na + 5; |
| 536 | } |
| 537 | } |
| 538 | return addr; |
| 539 | } |
| 540 | |
| 541 | static unsigned long __init |
| 542 | interpret_pci_props(struct device_node *np, unsigned long mem_start, |
| 543 | int naddrc, int nsizec) |
| 544 | { |
| 545 | struct address_range *adr; |
| 546 | struct pci_reg_property *pci_addrs; |
| 547 | int i, l, *ip; |
| 548 | |
| 549 | pci_addrs = (struct pci_reg_property *) |
| 550 | get_property(np, "assigned-addresses", &l); |
| 551 | if (pci_addrs != 0 && l >= sizeof(struct pci_reg_property)) { |
| 552 | i = 0; |
| 553 | adr = (struct address_range *) mem_start; |
| 554 | while ((l -= sizeof(struct pci_reg_property)) >= 0) { |
| 555 | adr[i].space = pci_addrs[i].addr.a_hi; |
| 556 | adr[i].address = map_addr(np, pci_addrs[i].addr.a_hi, |
| 557 | pci_addrs[i].addr.a_lo); |
| 558 | adr[i].size = pci_addrs[i].size_lo; |
| 559 | ++i; |
| 560 | } |
| 561 | np->addrs = adr; |
| 562 | np->n_addrs = i; |
| 563 | mem_start += i * sizeof(struct address_range); |
| 564 | } |
| 565 | |
| 566 | if (use_of_interrupt_tree) |
| 567 | return mem_start; |
| 568 | |
| 569 | ip = (int *) get_property(np, "AAPL,interrupts", &l); |
| 570 | if (ip == 0 && np->parent) |
| 571 | ip = (int *) get_property(np->parent, "AAPL,interrupts", &l); |
| 572 | if (ip == 0) |
| 573 | ip = (int *) get_property(np, "interrupts", &l); |
| 574 | if (ip != 0) { |
| 575 | np->intrs = (struct interrupt_info *) mem_start; |
| 576 | np->n_intrs = l / sizeof(int); |
| 577 | mem_start += np->n_intrs * sizeof(struct interrupt_info); |
| 578 | for (i = 0; i < np->n_intrs; ++i) { |
| 579 | np->intrs[i].line = *ip++; |
| 580 | np->intrs[i].sense = 1; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | return mem_start; |
| 585 | } |
| 586 | |
| 587 | static unsigned long __init |
| 588 | interpret_dbdma_props(struct device_node *np, unsigned long mem_start, |
| 589 | int naddrc, int nsizec) |
| 590 | { |
| 591 | struct reg_property *rp; |
| 592 | struct address_range *adr; |
| 593 | unsigned long base_address; |
| 594 | int i, l, *ip; |
| 595 | struct device_node *db; |
| 596 | |
| 597 | base_address = 0; |
| 598 | for (db = np->parent; db != NULL; db = db->parent) { |
| 599 | if (!strcmp(db->type, "dbdma") && db->n_addrs != 0) { |
| 600 | base_address = db->addrs[0].address; |
| 601 | break; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | rp = (struct reg_property *) get_property(np, "reg", &l); |
| 606 | if (rp != 0 && l >= sizeof(struct reg_property)) { |
| 607 | i = 0; |
| 608 | adr = (struct address_range *) mem_start; |
| 609 | while ((l -= sizeof(struct reg_property)) >= 0) { |
| 610 | adr[i].space = 2; |
| 611 | adr[i].address = rp[i].address + base_address; |
| 612 | adr[i].size = rp[i].size; |
| 613 | ++i; |
| 614 | } |
| 615 | np->addrs = adr; |
| 616 | np->n_addrs = i; |
| 617 | mem_start += i * sizeof(struct address_range); |
| 618 | } |
| 619 | |
| 620 | if (use_of_interrupt_tree) |
| 621 | return mem_start; |
| 622 | |
| 623 | ip = (int *) get_property(np, "AAPL,interrupts", &l); |
| 624 | if (ip == 0) |
| 625 | ip = (int *) get_property(np, "interrupts", &l); |
| 626 | if (ip != 0) { |
| 627 | np->intrs = (struct interrupt_info *) mem_start; |
| 628 | np->n_intrs = l / sizeof(int); |
| 629 | mem_start += np->n_intrs * sizeof(struct interrupt_info); |
| 630 | for (i = 0; i < np->n_intrs; ++i) { |
| 631 | np->intrs[i].line = *ip++; |
| 632 | np->intrs[i].sense = 1; |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | return mem_start; |
| 637 | } |
| 638 | |
| 639 | static unsigned long __init |
| 640 | interpret_macio_props(struct device_node *np, unsigned long mem_start, |
| 641 | int naddrc, int nsizec) |
| 642 | { |
| 643 | struct reg_property *rp; |
| 644 | struct address_range *adr; |
| 645 | unsigned long base_address; |
| 646 | int i, l, *ip; |
| 647 | struct device_node *db; |
| 648 | |
| 649 | base_address = 0; |
| 650 | for (db = np->parent; db != NULL; db = db->parent) { |
| 651 | if (!strcmp(db->type, "mac-io") && db->n_addrs != 0) { |
| 652 | base_address = db->addrs[0].address; |
| 653 | break; |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | rp = (struct reg_property *) get_property(np, "reg", &l); |
| 658 | if (rp != 0 && l >= sizeof(struct reg_property)) { |
| 659 | i = 0; |
| 660 | adr = (struct address_range *) mem_start; |
| 661 | while ((l -= sizeof(struct reg_property)) >= 0) { |
| 662 | adr[i].space = 2; |
| 663 | adr[i].address = rp[i].address + base_address; |
| 664 | adr[i].size = rp[i].size; |
| 665 | ++i; |
| 666 | } |
| 667 | np->addrs = adr; |
| 668 | np->n_addrs = i; |
| 669 | mem_start += i * sizeof(struct address_range); |
| 670 | } |
| 671 | |
| 672 | if (use_of_interrupt_tree) |
| 673 | return mem_start; |
| 674 | |
| 675 | ip = (int *) get_property(np, "interrupts", &l); |
| 676 | if (ip == 0) |
| 677 | ip = (int *) get_property(np, "AAPL,interrupts", &l); |
| 678 | if (ip != 0) { |
| 679 | np->intrs = (struct interrupt_info *) mem_start; |
| 680 | np->n_intrs = l / sizeof(int); |
| 681 | for (i = 0; i < np->n_intrs; ++i) { |
| 682 | np->intrs[i].line = *ip++; |
| 683 | np->intrs[i].sense = 1; |
| 684 | } |
| 685 | mem_start += np->n_intrs * sizeof(struct interrupt_info); |
| 686 | } |
| 687 | |
| 688 | return mem_start; |
| 689 | } |
| 690 | |
| 691 | static unsigned long __init |
| 692 | interpret_isa_props(struct device_node *np, unsigned long mem_start, |
| 693 | int naddrc, int nsizec) |
| 694 | { |
| 695 | struct isa_reg_property *rp; |
| 696 | struct address_range *adr; |
| 697 | int i, l, *ip; |
| 698 | |
| 699 | rp = (struct isa_reg_property *) get_property(np, "reg", &l); |
| 700 | if (rp != 0 && l >= sizeof(struct isa_reg_property)) { |
| 701 | i = 0; |
| 702 | adr = (struct address_range *) mem_start; |
| 703 | while ((l -= sizeof(struct reg_property)) >= 0) { |
| 704 | adr[i].space = rp[i].space; |
| 705 | adr[i].address = rp[i].address |
| 706 | + (adr[i].space? 0: _ISA_MEM_BASE); |
| 707 | adr[i].size = rp[i].size; |
| 708 | ++i; |
| 709 | } |
| 710 | np->addrs = adr; |
| 711 | np->n_addrs = i; |
| 712 | mem_start += i * sizeof(struct address_range); |
| 713 | } |
| 714 | |
| 715 | if (use_of_interrupt_tree) |
| 716 | return mem_start; |
| 717 | |
| 718 | ip = (int *) get_property(np, "interrupts", &l); |
| 719 | if (ip != 0) { |
| 720 | np->intrs = (struct interrupt_info *) mem_start; |
| 721 | np->n_intrs = l / (2 * sizeof(int)); |
| 722 | mem_start += np->n_intrs * sizeof(struct interrupt_info); |
| 723 | for (i = 0; i < np->n_intrs; ++i) { |
| 724 | np->intrs[i].line = *ip++; |
| 725 | np->intrs[i].sense = *ip++; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | return mem_start; |
| 730 | } |
| 731 | |
| 732 | static unsigned long __init |
| 733 | interpret_root_props(struct device_node *np, unsigned long mem_start, |
| 734 | int naddrc, int nsizec) |
| 735 | { |
| 736 | struct address_range *adr; |
| 737 | int i, l, *ip; |
| 738 | unsigned int *rp; |
| 739 | int rpsize = (naddrc + nsizec) * sizeof(unsigned int); |
| 740 | |
| 741 | rp = (unsigned int *) get_property(np, "reg", &l); |
| 742 | if (rp != 0 && l >= rpsize) { |
| 743 | i = 0; |
| 744 | adr = (struct address_range *) mem_start; |
| 745 | while ((l -= rpsize) >= 0) { |
| 746 | adr[i].space = (naddrc >= 2? rp[naddrc-2]: 2); |
| 747 | adr[i].address = rp[naddrc - 1]; |
| 748 | adr[i].size = rp[naddrc + nsizec - 1]; |
| 749 | ++i; |
| 750 | rp += naddrc + nsizec; |
| 751 | } |
| 752 | np->addrs = adr; |
| 753 | np->n_addrs = i; |
| 754 | mem_start += i * sizeof(struct address_range); |
| 755 | } |
| 756 | |
| 757 | if (use_of_interrupt_tree) |
| 758 | return mem_start; |
| 759 | |
| 760 | ip = (int *) get_property(np, "AAPL,interrupts", &l); |
| 761 | if (ip == 0) |
| 762 | ip = (int *) get_property(np, "interrupts", &l); |
| 763 | if (ip != 0) { |
| 764 | np->intrs = (struct interrupt_info *) mem_start; |
| 765 | np->n_intrs = l / sizeof(int); |
| 766 | mem_start += np->n_intrs * sizeof(struct interrupt_info); |
| 767 | for (i = 0; i < np->n_intrs; ++i) { |
| 768 | np->intrs[i].line = *ip++; |
| 769 | np->intrs[i].sense = 1; |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | return mem_start; |
| 774 | } |
| 775 | |
| 776 | /* |
| 777 | * Work out the sense (active-low level / active-high edge) |
| 778 | * of each interrupt from the device tree. |
| 779 | */ |
| 780 | void __init |
| 781 | prom_get_irq_senses(unsigned char *senses, int off, int max) |
| 782 | { |
| 783 | struct device_node *np; |
| 784 | int i, j; |
| 785 | |
| 786 | /* default to level-triggered */ |
| 787 | memset(senses, 1, max - off); |
| 788 | if (!use_of_interrupt_tree) |
| 789 | return; |
| 790 | |
| 791 | for (np = allnodes; np != 0; np = np->allnext) { |
| 792 | for (j = 0; j < np->n_intrs; j++) { |
| 793 | i = np->intrs[j].line; |
| 794 | if (i >= off && i < max) { |
| 795 | if (np->intrs[j].sense == 1) |
| 796 | senses[i-off] = (IRQ_SENSE_LEVEL |
| 797 | | IRQ_POLARITY_NEGATIVE); |
| 798 | else |
| 799 | senses[i-off] = (IRQ_SENSE_EDGE |
| 800 | | IRQ_POLARITY_POSITIVE); |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | /* |
| 807 | * Construct and return a list of the device_nodes with a given name. |
| 808 | */ |
| 809 | struct device_node * |
| 810 | find_devices(const char *name) |
| 811 | { |
| 812 | struct device_node *head, **prevp, *np; |
| 813 | |
| 814 | prevp = &head; |
| 815 | for (np = allnodes; np != 0; np = np->allnext) { |
| 816 | if (np->name != 0 && strcasecmp(np->name, name) == 0) { |
| 817 | *prevp = np; |
| 818 | prevp = &np->next; |
| 819 | } |
| 820 | } |
| 821 | *prevp = NULL; |
| 822 | return head; |
| 823 | } |
| 824 | |
| 825 | /* |
| 826 | * Construct and return a list of the device_nodes with a given type. |
| 827 | */ |
| 828 | struct device_node * |
| 829 | find_type_devices(const char *type) |
| 830 | { |
| 831 | struct device_node *head, **prevp, *np; |
| 832 | |
| 833 | prevp = &head; |
| 834 | for (np = allnodes; np != 0; np = np->allnext) { |
| 835 | if (np->type != 0 && strcasecmp(np->type, type) == 0) { |
| 836 | *prevp = np; |
| 837 | prevp = &np->next; |
| 838 | } |
| 839 | } |
| 840 | *prevp = NULL; |
| 841 | return head; |
| 842 | } |
| 843 | |
| 844 | /* |
| 845 | * Returns all nodes linked together |
| 846 | */ |
Jon Loeliger | f495a8b | 2005-09-17 10:35:08 -0500 | [diff] [blame] | 847 | struct device_node * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | find_all_nodes(void) |
| 849 | { |
| 850 | struct device_node *head, **prevp, *np; |
| 851 | |
| 852 | prevp = &head; |
| 853 | for (np = allnodes; np != 0; np = np->allnext) { |
| 854 | *prevp = np; |
| 855 | prevp = &np->next; |
| 856 | } |
| 857 | *prevp = NULL; |
| 858 | return head; |
| 859 | } |
| 860 | |
| 861 | /* Checks if the given "compat" string matches one of the strings in |
| 862 | * the device's "compatible" property |
| 863 | */ |
| 864 | int |
| 865 | device_is_compatible(struct device_node *device, const char *compat) |
| 866 | { |
| 867 | const char* cp; |
| 868 | int cplen, l; |
| 869 | |
| 870 | cp = (char *) get_property(device, "compatible", &cplen); |
| 871 | if (cp == NULL) |
| 872 | return 0; |
| 873 | while (cplen > 0) { |
| 874 | if (strncasecmp(cp, compat, strlen(compat)) == 0) |
| 875 | return 1; |
| 876 | l = strlen(cp) + 1; |
| 877 | cp += l; |
| 878 | cplen -= l; |
| 879 | } |
| 880 | |
| 881 | return 0; |
| 882 | } |
| 883 | |
| 884 | |
| 885 | /* |
| 886 | * Indicates whether the root node has a given value in its |
| 887 | * compatible property. |
| 888 | */ |
| 889 | int |
| 890 | machine_is_compatible(const char *compat) |
| 891 | { |
| 892 | struct device_node *root; |
| 893 | |
| 894 | root = find_path_device("/"); |
| 895 | if (root == 0) |
| 896 | return 0; |
| 897 | return device_is_compatible(root, compat); |
| 898 | } |
| 899 | |
| 900 | /* |
| 901 | * Construct and return a list of the device_nodes with a given type |
| 902 | * and compatible property. |
| 903 | */ |
| 904 | struct device_node * |
| 905 | find_compatible_devices(const char *type, const char *compat) |
| 906 | { |
| 907 | struct device_node *head, **prevp, *np; |
| 908 | |
| 909 | prevp = &head; |
| 910 | for (np = allnodes; np != 0; np = np->allnext) { |
| 911 | if (type != NULL |
| 912 | && !(np->type != 0 && strcasecmp(np->type, type) == 0)) |
| 913 | continue; |
| 914 | if (device_is_compatible(np, compat)) { |
| 915 | *prevp = np; |
| 916 | prevp = &np->next; |
| 917 | } |
| 918 | } |
| 919 | *prevp = NULL; |
| 920 | return head; |
| 921 | } |
| 922 | |
| 923 | /* |
| 924 | * Find the device_node with a given full_name. |
| 925 | */ |
| 926 | struct device_node * |
| 927 | find_path_device(const char *path) |
| 928 | { |
| 929 | struct device_node *np; |
| 930 | |
| 931 | for (np = allnodes; np != 0; np = np->allnext) |
| 932 | if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0) |
| 933 | return np; |
| 934 | return NULL; |
| 935 | } |
| 936 | |
| 937 | /******* |
| 938 | * |
| 939 | * New implementation of the OF "find" APIs, return a refcounted |
| 940 | * object, call of_node_put() when done. Currently, still lacks |
| 941 | * locking as old implementation, this is beeing done for ppc64. |
| 942 | * |
| 943 | * Note that property management will need some locking as well, |
| 944 | * this isn't dealt with yet |
| 945 | * |
| 946 | *******/ |
| 947 | |
| 948 | /** |
| 949 | * of_find_node_by_name - Find a node by it's "name" property |
| 950 | * @from: The node to start searching from or NULL, the node |
| 951 | * you pass will not be searched, only the next one |
| 952 | * will; typically, you pass what the previous call |
| 953 | * returned. of_node_put() will be called on it |
| 954 | * @name: The name string to match against |
| 955 | * |
| 956 | * Returns a node pointer with refcount incremented, use |
| 957 | * of_node_put() on it when done. |
| 958 | */ |
| 959 | struct device_node *of_find_node_by_name(struct device_node *from, |
| 960 | const char *name) |
| 961 | { |
| 962 | struct device_node *np = from ? from->allnext : allnodes; |
| 963 | |
| 964 | for (; np != 0; np = np->allnext) |
| 965 | if (np->name != 0 && strcasecmp(np->name, name) == 0) |
| 966 | break; |
| 967 | if (from) |
| 968 | of_node_put(from); |
| 969 | return of_node_get(np); |
| 970 | } |
| 971 | |
| 972 | /** |
| 973 | * of_find_node_by_type - Find a node by it's "device_type" property |
| 974 | * @from: The node to start searching from or NULL, the node |
| 975 | * you pass will not be searched, only the next one |
| 976 | * will; typically, you pass what the previous call |
| 977 | * returned. of_node_put() will be called on it |
| 978 | * @name: The type string to match against |
| 979 | * |
| 980 | * Returns a node pointer with refcount incremented, use |
| 981 | * of_node_put() on it when done. |
| 982 | */ |
| 983 | struct device_node *of_find_node_by_type(struct device_node *from, |
| 984 | const char *type) |
| 985 | { |
| 986 | struct device_node *np = from ? from->allnext : allnodes; |
| 987 | |
| 988 | for (; np != 0; np = np->allnext) |
| 989 | if (np->type != 0 && strcasecmp(np->type, type) == 0) |
| 990 | break; |
| 991 | if (from) |
| 992 | of_node_put(from); |
| 993 | return of_node_get(np); |
| 994 | } |
| 995 | |
| 996 | /** |
| 997 | * of_find_compatible_node - Find a node based on type and one of the |
| 998 | * tokens in it's "compatible" property |
| 999 | * @from: The node to start searching from or NULL, the node |
| 1000 | * you pass will not be searched, only the next one |
| 1001 | * will; typically, you pass what the previous call |
| 1002 | * returned. of_node_put() will be called on it |
| 1003 | * @type: The type string to match "device_type" or NULL to ignore |
| 1004 | * @compatible: The string to match to one of the tokens in the device |
| 1005 | * "compatible" list. |
| 1006 | * |
| 1007 | * Returns a node pointer with refcount incremented, use |
| 1008 | * of_node_put() on it when done. |
| 1009 | */ |
| 1010 | struct device_node *of_find_compatible_node(struct device_node *from, |
| 1011 | const char *type, const char *compatible) |
| 1012 | { |
| 1013 | struct device_node *np = from ? from->allnext : allnodes; |
| 1014 | |
| 1015 | for (; np != 0; np = np->allnext) { |
| 1016 | if (type != NULL |
| 1017 | && !(np->type != 0 && strcasecmp(np->type, type) == 0)) |
| 1018 | continue; |
| 1019 | if (device_is_compatible(np, compatible)) |
| 1020 | break; |
| 1021 | } |
| 1022 | if (from) |
| 1023 | of_node_put(from); |
| 1024 | return of_node_get(np); |
| 1025 | } |
| 1026 | |
| 1027 | /** |
| 1028 | * of_find_node_by_path - Find a node matching a full OF path |
| 1029 | * @path: The full path to match |
| 1030 | * |
| 1031 | * Returns a node pointer with refcount incremented, use |
| 1032 | * of_node_put() on it when done. |
| 1033 | */ |
| 1034 | struct device_node *of_find_node_by_path(const char *path) |
| 1035 | { |
| 1036 | struct device_node *np = allnodes; |
| 1037 | |
| 1038 | for (; np != 0; np = np->allnext) |
| 1039 | if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0) |
| 1040 | break; |
| 1041 | return of_node_get(np); |
| 1042 | } |
| 1043 | |
| 1044 | /** |
| 1045 | * of_find_all_nodes - Get next node in global list |
| 1046 | * @prev: Previous node or NULL to start iteration |
| 1047 | * of_node_put() will be called on it |
| 1048 | * |
| 1049 | * Returns a node pointer with refcount incremented, use |
| 1050 | * of_node_put() on it when done. |
| 1051 | */ |
| 1052 | struct device_node *of_find_all_nodes(struct device_node *prev) |
| 1053 | { |
| 1054 | return of_node_get(prev ? prev->allnext : allnodes); |
| 1055 | } |
| 1056 | |
| 1057 | /** |
| 1058 | * of_get_parent - Get a node's parent if any |
| 1059 | * @node: Node to get parent |
| 1060 | * |
| 1061 | * Returns a node pointer with refcount incremented, use |
| 1062 | * of_node_put() on it when done. |
| 1063 | */ |
| 1064 | struct device_node *of_get_parent(const struct device_node *node) |
| 1065 | { |
| 1066 | return node ? of_node_get(node->parent) : NULL; |
| 1067 | } |
| 1068 | |
| 1069 | /** |
| 1070 | * of_get_next_child - Iterate a node childs |
| 1071 | * @node: parent node |
| 1072 | * @prev: previous child of the parent node, or NULL to get first |
| 1073 | * |
| 1074 | * Returns a node pointer with refcount incremented, use |
| 1075 | * of_node_put() on it when done. |
| 1076 | */ |
| 1077 | struct device_node *of_get_next_child(const struct device_node *node, |
| 1078 | struct device_node *prev) |
| 1079 | { |
| 1080 | struct device_node *next = prev ? prev->sibling : node->child; |
| 1081 | |
| 1082 | for (; next != 0; next = next->sibling) |
| 1083 | if (of_node_get(next)) |
| 1084 | break; |
| 1085 | if (prev) |
| 1086 | of_node_put(prev); |
| 1087 | return next; |
| 1088 | } |
| 1089 | |
| 1090 | /** |
| 1091 | * of_node_get - Increment refcount of a node |
| 1092 | * @node: Node to inc refcount, NULL is supported to |
| 1093 | * simplify writing of callers |
| 1094 | * |
| 1095 | * Returns the node itself or NULL if gone. Current implementation |
| 1096 | * does nothing as we don't yet do dynamic node allocation on ppc32 |
| 1097 | */ |
| 1098 | struct device_node *of_node_get(struct device_node *node) |
| 1099 | { |
| 1100 | return node; |
| 1101 | } |
| 1102 | |
| 1103 | /** |
| 1104 | * of_node_put - Decrement refcount of a node |
| 1105 | * @node: Node to dec refcount, NULL is supported to |
| 1106 | * simplify writing of callers |
| 1107 | * |
| 1108 | * Current implementation does nothing as we don't yet do dynamic node |
| 1109 | * allocation on ppc32 |
| 1110 | */ |
| 1111 | void of_node_put(struct device_node *node) |
| 1112 | { |
| 1113 | } |
| 1114 | |
| 1115 | /* |
| 1116 | * Find the device_node with a given phandle. |
| 1117 | */ |
| 1118 | static struct device_node * __init |
| 1119 | find_phandle(phandle ph) |
| 1120 | { |
| 1121 | struct device_node *np; |
| 1122 | |
| 1123 | for (np = allnodes; np != 0; np = np->allnext) |
| 1124 | if (np->node == ph) |
| 1125 | return np; |
| 1126 | return NULL; |
| 1127 | } |
| 1128 | |
| 1129 | /* |
| 1130 | * Find a property with a given name for a given node |
| 1131 | * and return the value. |
| 1132 | */ |
| 1133 | unsigned char * |
| 1134 | get_property(struct device_node *np, const char *name, int *lenp) |
| 1135 | { |
| 1136 | struct property *pp; |
| 1137 | |
| 1138 | for (pp = np->properties; pp != 0; pp = pp->next) |
| 1139 | if (pp->name != NULL && strcmp(pp->name, name) == 0) { |
| 1140 | if (lenp != 0) |
| 1141 | *lenp = pp->length; |
| 1142 | return pp->value; |
| 1143 | } |
| 1144 | return NULL; |
| 1145 | } |
| 1146 | |
| 1147 | /* |
| 1148 | * Add a property to a node |
| 1149 | */ |
Benjamin Herrenschmidt | 183d020 | 2005-11-07 14:29:02 +1100 | [diff] [blame] | 1150 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | prom_add_property(struct device_node* np, struct property* prop) |
| 1152 | { |
| 1153 | struct property **next = &np->properties; |
| 1154 | |
| 1155 | prop->next = NULL; |
| 1156 | while (*next) |
| 1157 | next = &(*next)->next; |
| 1158 | *next = prop; |
Benjamin Herrenschmidt | 183d020 | 2005-11-07 14:29:02 +1100 | [diff] [blame] | 1159 | |
| 1160 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | /* I quickly hacked that one, check against spec ! */ |
Jon Loeliger | f495a8b | 2005-09-17 10:35:08 -0500 | [diff] [blame] | 1164 | static inline unsigned long |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | bus_space_to_resource_flags(unsigned int bus_space) |
| 1166 | { |
| 1167 | u8 space = (bus_space >> 24) & 0xf; |
| 1168 | if (space == 0) |
| 1169 | space = 0x02; |
| 1170 | if (space == 0x02) |
| 1171 | return IORESOURCE_MEM; |
| 1172 | else if (space == 0x01) |
| 1173 | return IORESOURCE_IO; |
| 1174 | else { |
| 1175 | printk(KERN_WARNING "prom.c: bus_space_to_resource_flags(), space: %x\n", |
| 1176 | bus_space); |
| 1177 | return 0; |
| 1178 | } |
| 1179 | } |
| 1180 | |
Jon Loeliger | f495a8b | 2005-09-17 10:35:08 -0500 | [diff] [blame] | 1181 | static struct resource* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | find_parent_pci_resource(struct pci_dev* pdev, struct address_range *range) |
| 1183 | { |
| 1184 | unsigned long mask; |
| 1185 | int i; |
| 1186 | |
| 1187 | /* Check this one */ |
| 1188 | mask = bus_space_to_resource_flags(range->space); |
| 1189 | for (i=0; i<DEVICE_COUNT_RESOURCE; i++) { |
| 1190 | if ((pdev->resource[i].flags & mask) == mask && |
| 1191 | pdev->resource[i].start <= range->address && |
| 1192 | pdev->resource[i].end > range->address) { |
| 1193 | if ((range->address + range->size - 1) > pdev->resource[i].end) { |
| 1194 | /* Add better message */ |
| 1195 | printk(KERN_WARNING "PCI/OF resource overlap !\n"); |
| 1196 | return NULL; |
| 1197 | } |
| 1198 | break; |
| 1199 | } |
| 1200 | } |
| 1201 | if (i == DEVICE_COUNT_RESOURCE) |
| 1202 | return NULL; |
| 1203 | return &pdev->resource[i]; |
| 1204 | } |
| 1205 | |
| 1206 | /* |
| 1207 | * Request an OF device resource. Currently handles child of PCI devices, |
| 1208 | * or other nodes attached to the root node. Ultimately, put some |
| 1209 | * link to resources in the OF node. |
| 1210 | */ |
Jon Loeliger | f495a8b | 2005-09-17 10:35:08 -0500 | [diff] [blame] | 1211 | struct resource* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | request_OF_resource(struct device_node* node, int index, const char* name_postfix) |
| 1213 | { |
| 1214 | struct pci_dev* pcidev; |
| 1215 | u8 pci_bus, pci_devfn; |
| 1216 | unsigned long iomask; |
| 1217 | struct device_node* nd; |
| 1218 | struct resource* parent; |
| 1219 | struct resource *res = NULL; |
| 1220 | int nlen, plen; |
| 1221 | |
| 1222 | if (index >= node->n_addrs) |
| 1223 | goto fail; |
| 1224 | |
| 1225 | /* Sanity check on bus space */ |
| 1226 | iomask = bus_space_to_resource_flags(node->addrs[index].space); |
| 1227 | if (iomask & IORESOURCE_MEM) |
| 1228 | parent = &iomem_resource; |
| 1229 | else if (iomask & IORESOURCE_IO) |
| 1230 | parent = &ioport_resource; |
| 1231 | else |
| 1232 | goto fail; |
| 1233 | |
| 1234 | /* Find a PCI parent if any */ |
| 1235 | nd = node; |
| 1236 | pcidev = NULL; |
| 1237 | while(nd) { |
| 1238 | if (!pci_device_from_OF_node(nd, &pci_bus, &pci_devfn)) |
| 1239 | pcidev = pci_find_slot(pci_bus, pci_devfn); |
| 1240 | if (pcidev) break; |
| 1241 | nd = nd->parent; |
| 1242 | } |
| 1243 | if (pcidev) |
| 1244 | parent = find_parent_pci_resource(pcidev, &node->addrs[index]); |
| 1245 | if (!parent) { |
| 1246 | printk(KERN_WARNING "request_OF_resource(%s), parent not found\n", |
| 1247 | node->name); |
| 1248 | goto fail; |
| 1249 | } |
| 1250 | |
| 1251 | res = __request_region(parent, node->addrs[index].address, node->addrs[index].size, NULL); |
| 1252 | if (!res) |
| 1253 | goto fail; |
| 1254 | nlen = strlen(node->name); |
| 1255 | plen = name_postfix ? strlen(name_postfix) : 0; |
| 1256 | res->name = (const char *)kmalloc(nlen+plen+1, GFP_KERNEL); |
| 1257 | if (res->name) { |
| 1258 | strcpy((char *)res->name, node->name); |
| 1259 | if (plen) |
| 1260 | strcpy((char *)res->name+nlen, name_postfix); |
| 1261 | } |
| 1262 | return res; |
| 1263 | fail: |
| 1264 | return NULL; |
| 1265 | } |
| 1266 | |
Jon Loeliger | f495a8b | 2005-09-17 10:35:08 -0500 | [diff] [blame] | 1267 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | release_OF_resource(struct device_node* node, int index) |
| 1269 | { |
| 1270 | struct pci_dev* pcidev; |
| 1271 | u8 pci_bus, pci_devfn; |
| 1272 | unsigned long iomask, start, end; |
| 1273 | struct device_node* nd; |
| 1274 | struct resource* parent; |
| 1275 | struct resource *res = NULL; |
| 1276 | |
| 1277 | if (index >= node->n_addrs) |
| 1278 | return -EINVAL; |
| 1279 | |
| 1280 | /* Sanity check on bus space */ |
| 1281 | iomask = bus_space_to_resource_flags(node->addrs[index].space); |
| 1282 | if (iomask & IORESOURCE_MEM) |
| 1283 | parent = &iomem_resource; |
| 1284 | else if (iomask & IORESOURCE_IO) |
| 1285 | parent = &ioport_resource; |
| 1286 | else |
| 1287 | return -EINVAL; |
| 1288 | |
| 1289 | /* Find a PCI parent if any */ |
| 1290 | nd = node; |
| 1291 | pcidev = NULL; |
| 1292 | while(nd) { |
| 1293 | if (!pci_device_from_OF_node(nd, &pci_bus, &pci_devfn)) |
| 1294 | pcidev = pci_find_slot(pci_bus, pci_devfn); |
| 1295 | if (pcidev) break; |
| 1296 | nd = nd->parent; |
| 1297 | } |
| 1298 | if (pcidev) |
| 1299 | parent = find_parent_pci_resource(pcidev, &node->addrs[index]); |
| 1300 | if (!parent) { |
| 1301 | printk(KERN_WARNING "release_OF_resource(%s), parent not found\n", |
| 1302 | node->name); |
| 1303 | return -ENODEV; |
| 1304 | } |
| 1305 | |
| 1306 | /* Find us in the parent and its childs */ |
| 1307 | res = parent->child; |
| 1308 | start = node->addrs[index].address; |
| 1309 | end = start + node->addrs[index].size - 1; |
| 1310 | while (res) { |
| 1311 | if (res->start == start && res->end == end && |
| 1312 | (res->flags & IORESOURCE_BUSY)) |
| 1313 | break; |
| 1314 | if (res->start <= start && res->end >= end) |
| 1315 | res = res->child; |
| 1316 | else |
| 1317 | res = res->sibling; |
| 1318 | } |
| 1319 | if (!res) |
| 1320 | return -ENODEV; |
| 1321 | |
Jesper Juhl | b2325fe | 2005-11-07 01:01:35 -0800 | [diff] [blame] | 1322 | kfree(res->name); |
| 1323 | res->name = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | release_resource(res); |
| 1325 | kfree(res); |
| 1326 | |
| 1327 | return 0; |
| 1328 | } |
| 1329 | |
| 1330 | #if 0 |
Jon Loeliger | f495a8b | 2005-09-17 10:35:08 -0500 | [diff] [blame] | 1331 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | print_properties(struct device_node *np) |
| 1333 | { |
| 1334 | struct property *pp; |
| 1335 | char *cp; |
| 1336 | int i, n; |
| 1337 | |
| 1338 | for (pp = np->properties; pp != 0; pp = pp->next) { |
| 1339 | printk(KERN_INFO "%s", pp->name); |
| 1340 | for (i = strlen(pp->name); i < 16; ++i) |
| 1341 | printk(" "); |
| 1342 | cp = (char *) pp->value; |
| 1343 | for (i = pp->length; i > 0; --i, ++cp) |
| 1344 | if ((i > 1 && (*cp < 0x20 || *cp > 0x7e)) |
| 1345 | || (i == 1 && *cp != 0)) |
| 1346 | break; |
| 1347 | if (i == 0 && pp->length > 1) { |
| 1348 | /* looks like a string */ |
| 1349 | printk(" %s\n", (char *) pp->value); |
| 1350 | } else { |
| 1351 | /* dump it in hex */ |
| 1352 | n = pp->length; |
| 1353 | if (n > 64) |
| 1354 | n = 64; |
| 1355 | if (pp->length % 4 == 0) { |
| 1356 | unsigned int *p = (unsigned int *) pp->value; |
| 1357 | |
| 1358 | n /= 4; |
| 1359 | for (i = 0; i < n; ++i) { |
| 1360 | if (i != 0 && (i % 4) == 0) |
| 1361 | printk("\n "); |
| 1362 | printk(" %08x", *p++); |
| 1363 | } |
| 1364 | } else { |
| 1365 | unsigned char *bp = pp->value; |
| 1366 | |
| 1367 | for (i = 0; i < n; ++i) { |
| 1368 | if (i != 0 && (i % 16) == 0) |
| 1369 | printk("\n "); |
| 1370 | printk(" %02x", *bp++); |
| 1371 | } |
| 1372 | } |
| 1373 | printk("\n"); |
| 1374 | if (pp->length > 64) |
| 1375 | printk(" ... (length = %d)\n", |
| 1376 | pp->length); |
| 1377 | } |
| 1378 | } |
| 1379 | } |
| 1380 | #endif |
| 1381 | |
| 1382 | static DEFINE_SPINLOCK(rtas_lock); |
| 1383 | |
| 1384 | /* this can be called after setup -- Cort */ |
Jon Loeliger | f495a8b | 2005-09-17 10:35:08 -0500 | [diff] [blame] | 1385 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | call_rtas(const char *service, int nargs, int nret, |
| 1387 | unsigned long *outputs, ...) |
| 1388 | { |
| 1389 | va_list list; |
| 1390 | int i; |
| 1391 | unsigned long s; |
| 1392 | struct device_node *rtas; |
| 1393 | int *tokp; |
| 1394 | union { |
| 1395 | unsigned long words[16]; |
| 1396 | double align; |
| 1397 | } u; |
| 1398 | |
| 1399 | rtas = find_devices("rtas"); |
| 1400 | if (rtas == NULL) |
| 1401 | return -1; |
| 1402 | tokp = (int *) get_property(rtas, service, NULL); |
| 1403 | if (tokp == NULL) { |
| 1404 | printk(KERN_ERR "No RTAS service called %s\n", service); |
| 1405 | return -1; |
| 1406 | } |
| 1407 | u.words[0] = *tokp; |
| 1408 | u.words[1] = nargs; |
| 1409 | u.words[2] = nret; |
| 1410 | va_start(list, outputs); |
| 1411 | for (i = 0; i < nargs; ++i) |
| 1412 | u.words[i+3] = va_arg(list, unsigned long); |
| 1413 | va_end(list); |
| 1414 | |
| 1415 | /* |
| 1416 | * RTAS doesn't use floating point. |
| 1417 | * Or at least, according to the CHRP spec we enter RTAS |
| 1418 | * with FP disabled, and it doesn't change the FP registers. |
| 1419 | * -- paulus. |
| 1420 | */ |
| 1421 | spin_lock_irqsave(&rtas_lock, s); |
| 1422 | enter_rtas((void *)__pa(&u)); |
| 1423 | spin_unlock_irqrestore(&rtas_lock, s); |
| 1424 | |
| 1425 | if (nret > 1 && outputs != NULL) |
| 1426 | for (i = 0; i < nret-1; ++i) |
| 1427 | outputs[i] = u.words[i+nargs+4]; |
| 1428 | return u.words[nargs+3]; |
| 1429 | } |