1 /*- 2 * Copyright (c) 2009-2010 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Semihalf under sponsorship from 6 * the FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/kernel.h> 36 #include <sys/module.h> 37 #include <sys/bus.h> 38 #include <sys/limits.h> 39 40 #include <machine/resource.h> 41 42 #include <dev/fdt/fdt_common.h> 43 #include <dev/ofw/ofw_bus.h> 44 #include <dev/ofw/ofw_bus_subr.h> 45 #include <dev/ofw/openfirm.h> 46 47 #include "ofw_bus_if.h" 48 49 #ifdef DEBUG 50 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \ 51 printf(fmt,##args); } while (0) 52 #else 53 #define debugf(fmt, args...) 54 #endif 55 56 #define FDT_COMPAT_LEN 255 57 #define FDT_TYPE_LEN 64 58 59 #define FDT_REG_CELLS 4 60 61 vm_paddr_t fdt_immr_pa; 62 vm_offset_t fdt_immr_va; 63 vm_offset_t fdt_immr_size; 64 65 struct fdt_ic_list fdt_ic_list_head = SLIST_HEAD_INITIALIZER(fdt_ic_list_head); 66 67 int 68 fdt_get_range(phandle_t node, int range_id, u_long *base, u_long *size) 69 { 70 pcell_t ranges[6], *rangesptr; 71 pcell_t addr_cells, size_cells, par_addr_cells; 72 int len, tuple_size, tuples; 73 74 if ((fdt_addrsize_cells(node, &addr_cells, &size_cells)) != 0) 75 return (ENXIO); 76 /* 77 * Process 'ranges' property. 78 */ 79 par_addr_cells = fdt_parent_addr_cells(node); 80 if (par_addr_cells > 2) 81 return (ERANGE); 82 83 len = OF_getproplen(node, "ranges"); 84 if (len > sizeof(ranges)) 85 return (ENOMEM); 86 if (len == 0) { 87 *base = 0; 88 *size = ULONG_MAX; 89 return (0); 90 } 91 92 if (!(range_id < len)) 93 return (ERANGE); 94 95 if (OF_getprop(node, "ranges", ranges, sizeof(ranges)) <= 0) 96 return (EINVAL); 97 98 tuple_size = sizeof(pcell_t) * (addr_cells + par_addr_cells + 99 size_cells); 100 tuples = len / tuple_size; 101 102 if (fdt_ranges_verify(ranges, tuples, par_addr_cells, 103 addr_cells, size_cells)) { 104 return (ERANGE); 105 } 106 *base = 0; 107 *size = 0; 108 rangesptr = &ranges[range_id]; 109 110 *base = fdt_data_get((void *)rangesptr, addr_cells); 111 rangesptr += addr_cells; 112 *base += fdt_data_get((void *)rangesptr, par_addr_cells); 113 rangesptr += par_addr_cells; 114 *size = fdt_data_get((void *)rangesptr, size_cells); 115 return (0); 116 } 117 118 int 119 fdt_immr_addr(vm_offset_t immr_va) 120 { 121 phandle_t node; 122 u_long base, size; 123 int r; 124 125 /* 126 * Try to access the SOC node directly i.e. through /aliases/. 127 */ 128 if ((node = OF_finddevice("soc")) != 0) 129 if (fdt_is_compatible_strict(node, "simple-bus")) 130 goto moveon; 131 /* 132 * Find the node the long way. 133 */ 134 if ((node = OF_finddevice("/")) == 0) 135 return (ENXIO); 136 137 if ((node = fdt_find_compatible(node, "simple-bus", 1)) == 0) 138 return (ENXIO); 139 140 moveon: 141 if ((r = fdt_get_range(node, 0, &base, &size)) == 0) { 142 fdt_immr_pa = base; 143 fdt_immr_va = immr_va; 144 fdt_immr_size = size; 145 } 146 147 return (r); 148 } 149 150 /* 151 * This routine is an early-usage version of the ofw_bus_is_compatible() when 152 * the ofw_bus I/F is not available (like early console routines and similar). 153 * Note the buffer has to be on the stack since malloc() is usually not 154 * available in such cases either. 155 */ 156 int 157 fdt_is_compatible(phandle_t node, const char *compatstr) 158 { 159 char buf[FDT_COMPAT_LEN]; 160 char *compat; 161 int len, onelen, l, rv; 162 163 if ((len = OF_getproplen(node, "compatible")) <= 0) 164 return (0); 165 166 compat = (char *)&buf; 167 bzero(compat, FDT_COMPAT_LEN); 168 169 if (OF_getprop(node, "compatible", compat, FDT_COMPAT_LEN) < 0) 170 return (0); 171 172 onelen = strlen(compatstr); 173 rv = 0; 174 while (len > 0) { 175 if (strncasecmp(compat, compatstr, onelen) == 0) { 176 /* Found it. */ 177 rv = 1; 178 break; 179 } 180 /* Slide to the next sub-string. */ 181 l = strlen(compat) + 1; 182 compat += l; 183 len -= l; 184 } 185 186 return (rv); 187 } 188 189 int 190 fdt_is_compatible_strict(phandle_t node, const char *compatible) 191 { 192 char compat[FDT_COMPAT_LEN]; 193 194 if (OF_getproplen(node, "compatible") <= 0) 195 return (0); 196 197 if (OF_getprop(node, "compatible", compat, FDT_COMPAT_LEN) < 0) 198 return (0); 199 200 if (strncasecmp(compat, compatible, FDT_COMPAT_LEN) == 0) 201 /* This fits. */ 202 return (1); 203 204 return (0); 205 } 206 207 phandle_t 208 fdt_find_compatible(phandle_t start, const char *compat, int strict) 209 { 210 phandle_t child; 211 212 /* 213 * Traverse all children of 'start' node, and find first with 214 * matching 'compatible' property. 215 */ 216 for (child = OF_child(start); child != 0; child = OF_peer(child)) 217 if (fdt_is_compatible(child, compat)) { 218 if (strict) 219 if (!fdt_is_compatible_strict(child, compat)) 220 continue; 221 return (child); 222 } 223 return (0); 224 } 225 226 int 227 fdt_is_enabled(phandle_t node) 228 { 229 char *stat; 230 int ena, len; 231 232 len = OF_getprop_alloc(node, "status", sizeof(char), 233 (void **)&stat); 234 235 if (len <= 0) 236 /* It is OK if no 'status' property. */ 237 return (1); 238 239 /* Anything other than 'okay' means disabled. */ 240 ena = 0; 241 if (strncmp((char *)stat, "okay", len) == 0) 242 ena = 1; 243 244 free(stat, M_OFWPROP); 245 return (ena); 246 } 247 248 int 249 fdt_is_type(phandle_t node, const char *typestr) 250 { 251 char type[FDT_TYPE_LEN]; 252 253 if (OF_getproplen(node, "device_type") <= 0) 254 return (0); 255 256 if (OF_getprop(node, "device_type", type, FDT_TYPE_LEN) < 0) 257 return (0); 258 259 if (strncasecmp(type, typestr, FDT_TYPE_LEN) == 0) 260 /* This fits. */ 261 return (1); 262 263 return (0); 264 } 265 266 int 267 fdt_parent_addr_cells(phandle_t node) 268 { 269 pcell_t addr_cells; 270 271 /* Find out #address-cells of the superior bus. */ 272 if (OF_searchprop(OF_parent(node), "#address-cells", &addr_cells, 273 sizeof(addr_cells)) <= 0) 274 addr_cells = 2; 275 276 return ((int)fdt32_to_cpu(addr_cells)); 277 } 278 279 int 280 fdt_data_verify(void *data, int cells) 281 { 282 uint64_t d64; 283 284 if (cells > 1) { 285 d64 = fdt64_to_cpu(*((uint64_t *)data)); 286 if (((d64 >> 32) & 0xffffffffull) != 0 || cells > 2) 287 return (ERANGE); 288 } 289 290 return (0); 291 } 292 293 int 294 fdt_pm_is_enabled(phandle_t node) 295 { 296 int ret; 297 298 ret = 1; 299 300 #if defined(SOC_MV_KIRKWOOD) || defined(SOC_MV_DISCOVERY) 301 ret = fdt_pm(node); 302 #endif 303 return (ret); 304 } 305 306 u_long 307 fdt_data_get(void *data, int cells) 308 { 309 310 if (cells == 1) 311 return (fdt32_to_cpu(*((uint32_t *)data))); 312 313 return (fdt64_to_cpu(*((uint64_t *)data))); 314 } 315 316 int 317 fdt_addrsize_cells(phandle_t node, int *addr_cells, int *size_cells) 318 { 319 pcell_t cell; 320 int cell_size; 321 322 /* 323 * Retrieve #{address,size}-cells. 324 */ 325 cell_size = sizeof(cell); 326 if (OF_getprop(node, "#address-cells", &cell, cell_size) < cell_size) 327 cell = 2; 328 *addr_cells = fdt32_to_cpu((int)cell); 329 330 if (OF_getprop(node, "#size-cells", &cell, cell_size) < cell_size) 331 cell = 1; 332 *size_cells = fdt32_to_cpu((int)cell); 333 334 if (*addr_cells > 3 || *size_cells > 2) 335 return (ERANGE); 336 return (0); 337 } 338 339 int 340 fdt_ranges_verify(pcell_t *ranges, int tuples, int par_addr_cells, 341 int this_addr_cells, int this_size_cells) 342 { 343 int i, rv, ulsz; 344 345 if (par_addr_cells > 2 || this_addr_cells > 2 || this_size_cells > 2) 346 return (ERANGE); 347 348 /* 349 * This is the max size the resource manager can handle for addresses 350 * and sizes. 351 */ 352 ulsz = sizeof(u_long); 353 if (par_addr_cells <= ulsz && this_addr_cells <= ulsz && 354 this_size_cells <= ulsz) 355 /* We can handle everything */ 356 return (0); 357 358 rv = 0; 359 for (i = 0; i < tuples; i++) { 360 361 if (fdt_data_verify((void *)ranges, par_addr_cells)) 362 goto err; 363 ranges += par_addr_cells; 364 365 if (fdt_data_verify((void *)ranges, this_addr_cells)) 366 goto err; 367 ranges += this_addr_cells; 368 369 if (fdt_data_verify((void *)ranges, this_size_cells)) 370 goto err; 371 ranges += this_size_cells; 372 } 373 374 return (0); 375 376 err: 377 debugf("using address range >%d-bit not supported\n", ulsz * 8); 378 return (ERANGE); 379 } 380 381 int 382 fdt_data_to_res(pcell_t *data, int addr_cells, int size_cells, u_long *start, 383 u_long *count) 384 { 385 386 /* Address portion. */ 387 if (fdt_data_verify((void *)data, addr_cells)) 388 return (ERANGE); 389 390 *start = fdt_data_get((void *)data, addr_cells); 391 data += addr_cells; 392 393 /* Size portion. */ 394 if (fdt_data_verify((void *)data, size_cells)) 395 return (ERANGE); 396 397 *count = fdt_data_get((void *)data, size_cells); 398 return (0); 399 } 400 401 int 402 fdt_regsize(phandle_t node, u_long *base, u_long *size) 403 { 404 pcell_t reg[4]; 405 int addr_cells, len, size_cells; 406 407 if (fdt_addrsize_cells(OF_parent(node), &addr_cells, &size_cells)) 408 return (ENXIO); 409 410 if ((sizeof(pcell_t) * (addr_cells + size_cells)) > sizeof(reg)) 411 return (ENOMEM); 412 413 len = OF_getprop(node, "reg", ®, sizeof(reg)); 414 if (len <= 0) 415 return (EINVAL); 416 417 *base = fdt_data_get(®[0], addr_cells); 418 *size = fdt_data_get(®[addr_cells], size_cells); 419 return (0); 420 } 421 422 int 423 fdt_reg_to_rl(phandle_t node, struct resource_list *rl) 424 { 425 u_long end, count, start; 426 pcell_t *reg, *regptr; 427 pcell_t addr_cells, size_cells; 428 int tuple_size, tuples; 429 int i, rv; 430 long busaddr, bussize; 431 432 if (fdt_addrsize_cells(OF_parent(node), &addr_cells, &size_cells) != 0) 433 return (ENXIO); 434 if (fdt_get_range(OF_parent(node), 0, &busaddr, &bussize)) { 435 busaddr = 0; 436 bussize = 0; 437 } 438 439 tuple_size = sizeof(pcell_t) * (addr_cells + size_cells); 440 tuples = OF_getprop_alloc(node, "reg", tuple_size, (void **)®); 441 debugf("addr_cells = %d, size_cells = %d\n", addr_cells, size_cells); 442 debugf("tuples = %d, tuple size = %d\n", tuples, tuple_size); 443 if (tuples <= 0) 444 /* No 'reg' property in this node. */ 445 return (0); 446 447 regptr = reg; 448 for (i = 0; i < tuples; i++) { 449 450 rv = fdt_data_to_res(reg, addr_cells, size_cells, &start, 451 &count); 452 if (rv != 0) { 453 resource_list_free(rl); 454 goto out; 455 } 456 reg += addr_cells + size_cells; 457 458 /* Calculate address range relative to base. */ 459 start += busaddr; 460 end = start + count - 1; 461 462 debugf("reg addr start = %lx, end = %lx, count = %lx\n", start, 463 end, count); 464 465 resource_list_add(rl, SYS_RES_MEMORY, i, start, end, 466 count); 467 } 468 rv = 0; 469 470 out: 471 free(regptr, M_OFWPROP); 472 return (rv); 473 } 474 475 int 476 fdt_intr_to_rl(device_t dev, phandle_t node, struct resource_list *rl, 477 struct fdt_sense_level *intr_sl) 478 { 479 phandle_t iparent; 480 uint32_t *intr, icells; 481 int nintr, i, k; 482 483 nintr = OF_getencprop_alloc(node, "interrupts", sizeof(*intr), 484 (void **)&intr); 485 if (nintr > 0) { 486 iparent = 0; 487 OF_searchencprop(node, "interrupt-parent", &iparent, 488 sizeof(iparent)); 489 OF_searchencprop(OF_xref_phandle(iparent), "#interrupt-cells", 490 &icells, sizeof(icells)); 491 for (i = 0, k = 0; i < nintr; i += icells, k++) { 492 intr[i] = ofw_bus_map_intr(dev, iparent, intr[i]); 493 resource_list_add(rl, SYS_RES_IRQ, k, intr[i], intr[i], 494 1); 495 if (icells > 1) 496 ofw_bus_config_intr(dev, intr[i], intr[i+1]); 497 } 498 free(intr, M_OFWPROP); 499 } 500 501 return (0); 502 } 503 504 int 505 fdt_get_phyaddr(phandle_t node, device_t dev, int *phy_addr, void **phy_sc) 506 { 507 phandle_t phy_node; 508 pcell_t phy_handle, phy_reg; 509 uint32_t i; 510 device_t parent, child; 511 512 if (OF_getencprop(node, "phy-handle", (void *)&phy_handle, 513 sizeof(phy_handle)) <= 0) 514 return (ENXIO); 515 516 phy_node = OF_xref_phandle(phy_handle); 517 518 if (OF_getprop(phy_node, "reg", (void *)&phy_reg, 519 sizeof(phy_reg)) <= 0) 520 return (ENXIO); 521 522 *phy_addr = fdt32_to_cpu(phy_reg); 523 524 /* 525 * Search for softc used to communicate with phy. 526 */ 527 528 /* 529 * Step 1: Search for ancestor of the phy-node with a "phy-handle" 530 * property set. 531 */ 532 phy_node = OF_parent(phy_node); 533 while (phy_node != 0) { 534 if (OF_getprop(phy_node, "phy-handle", (void *)&phy_handle, 535 sizeof(phy_handle)) > 0) 536 break; 537 phy_node = OF_parent(phy_node); 538 } 539 if (phy_node == 0) 540 return (ENXIO); 541 542 /* 543 * Step 2: For each device with the same parent and name as ours 544 * compare its node with the one found in step 1, ancestor of phy 545 * node (stored in phy_node). 546 */ 547 parent = device_get_parent(dev); 548 i = 0; 549 child = device_find_child(parent, device_get_name(dev), i); 550 while (child != NULL) { 551 if (ofw_bus_get_node(child) == phy_node) 552 break; 553 i++; 554 child = device_find_child(parent, device_get_name(dev), i); 555 } 556 if (child == NULL) 557 return (ENXIO); 558 559 /* 560 * Use softc of the device found. 561 */ 562 *phy_sc = (void *)device_get_softc(child); 563 564 return (0); 565 } 566 567 int 568 fdt_get_reserved_regions(struct mem_region *mr, int *mrcnt) 569 { 570 pcell_t reserve[FDT_REG_CELLS * FDT_MEM_REGIONS]; 571 pcell_t *reservep; 572 phandle_t memory, root; 573 uint32_t memory_size; 574 int addr_cells, size_cells; 575 int i, max_size, res_len, rv, tuple_size, tuples; 576 577 max_size = sizeof(reserve); 578 root = OF_finddevice("/"); 579 memory = OF_finddevice("/memory"); 580 if (memory == -1) { 581 rv = ENXIO; 582 goto out; 583 } 584 585 if ((rv = fdt_addrsize_cells(OF_parent(memory), &addr_cells, 586 &size_cells)) != 0) 587 goto out; 588 589 if (addr_cells > 2) { 590 rv = ERANGE; 591 goto out; 592 } 593 594 tuple_size = sizeof(pcell_t) * (addr_cells + size_cells); 595 596 res_len = OF_getproplen(root, "memreserve"); 597 if (res_len <= 0 || res_len > sizeof(reserve)) { 598 rv = ERANGE; 599 goto out; 600 } 601 602 if (OF_getprop(root, "memreserve", reserve, res_len) <= 0) { 603 rv = ENXIO; 604 goto out; 605 } 606 607 memory_size = 0; 608 tuples = res_len / tuple_size; 609 reservep = (pcell_t *)&reserve; 610 for (i = 0; i < tuples; i++) { 611 612 rv = fdt_data_to_res(reservep, addr_cells, size_cells, 613 (u_long *)&mr[i].mr_start, (u_long *)&mr[i].mr_size); 614 615 if (rv != 0) 616 goto out; 617 618 reservep += addr_cells + size_cells; 619 } 620 621 *mrcnt = i; 622 rv = 0; 623 out: 624 return (rv); 625 } 626 627 int 628 fdt_get_mem_regions(struct mem_region *mr, int *mrcnt, uint32_t *memsize) 629 { 630 pcell_t reg[FDT_REG_CELLS * FDT_MEM_REGIONS]; 631 pcell_t *regp; 632 phandle_t memory; 633 uint32_t memory_size; 634 int addr_cells, size_cells; 635 int i, max_size, reg_len, rv, tuple_size, tuples; 636 637 max_size = sizeof(reg); 638 memory = OF_finddevice("/memory"); 639 if (memory == -1) { 640 rv = ENXIO; 641 goto out; 642 } 643 644 if ((rv = fdt_addrsize_cells(OF_parent(memory), &addr_cells, 645 &size_cells)) != 0) 646 goto out; 647 648 if (addr_cells > 2) { 649 rv = ERANGE; 650 goto out; 651 } 652 653 tuple_size = sizeof(pcell_t) * (addr_cells + size_cells); 654 reg_len = OF_getproplen(memory, "reg"); 655 if (reg_len <= 0 || reg_len > sizeof(reg)) { 656 rv = ERANGE; 657 goto out; 658 } 659 660 if (OF_getprop(memory, "reg", reg, reg_len) <= 0) { 661 rv = ENXIO; 662 goto out; 663 } 664 665 memory_size = 0; 666 tuples = reg_len / tuple_size; 667 regp = (pcell_t *)® 668 for (i = 0; i < tuples; i++) { 669 670 rv = fdt_data_to_res(regp, addr_cells, size_cells, 671 (u_long *)&mr[i].mr_start, (u_long *)&mr[i].mr_size); 672 673 if (rv != 0) 674 goto out; 675 676 regp += addr_cells + size_cells; 677 memory_size += mr[i].mr_size; 678 } 679 680 if (memory_size == 0) { 681 rv = ERANGE; 682 goto out; 683 } 684 685 *mrcnt = i; 686 *memsize = memory_size; 687 rv = 0; 688 out: 689 return (rv); 690 } 691 692 int 693 fdt_get_unit(device_t dev) 694 { 695 const char * name; 696 697 name = ofw_bus_get_name(dev); 698 name = strchr(name, '@') + 1; 699 700 return (strtol(name,NULL,0)); 701 } 702