1 /* 2 * Copyright (c) 2017 Google, Inc 3 * Written by Simon Glass <sjg@chromium.org> 4 * 5 * SPDX-License-Identifier: GPL-2.0+ 6 */ 7 8 #include <common.h> 9 #include <dm.h> 10 #include <fdtdec.h> 11 #include <fdt_support.h> 12 #include <libfdt.h> 13 #include <dm/of_access.h> 14 #include <dm/of_addr.h> 15 #include <dm/ofnode.h> 16 #include <linux/err.h> 17 #include <linux/ioport.h> 18 19 int ofnode_read_u32(ofnode node, const char *propname, u32 *outp) 20 { 21 assert(ofnode_valid(node)); 22 debug("%s: %s: ", __func__, propname); 23 24 if (ofnode_is_np(node)) { 25 return of_read_u32(ofnode_to_np(node), propname, outp); 26 } else { 27 const fdt32_t *cell; 28 int len; 29 30 cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), 31 propname, &len); 32 if (!cell || len < sizeof(int)) { 33 debug("(not found)\n"); 34 return -EINVAL; 35 } 36 *outp = fdt32_to_cpu(cell[0]); 37 } 38 debug("%#x (%d)\n", *outp, *outp); 39 40 return 0; 41 } 42 43 int ofnode_read_u32_default(ofnode node, const char *propname, u32 def) 44 { 45 assert(ofnode_valid(node)); 46 ofnode_read_u32(node, propname, &def); 47 48 return def; 49 } 50 51 int ofnode_read_s32_default(ofnode node, const char *propname, s32 def) 52 { 53 assert(ofnode_valid(node)); 54 ofnode_read_u32(node, propname, (u32 *)&def); 55 56 return def; 57 } 58 59 bool ofnode_read_bool(ofnode node, const char *propname) 60 { 61 const void *prop; 62 63 assert(ofnode_valid(node)); 64 debug("%s: %s: ", __func__, propname); 65 66 prop = ofnode_get_property(node, propname, NULL); 67 68 debug("%s\n", prop ? "true" : "false"); 69 70 return prop ? true : false; 71 } 72 73 const char *ofnode_read_string(ofnode node, const char *propname) 74 { 75 const char *str = NULL; 76 int len = -1; 77 78 assert(ofnode_valid(node)); 79 debug("%s: %s: ", __func__, propname); 80 81 if (ofnode_is_np(node)) { 82 struct property *prop = of_find_property( 83 ofnode_to_np(node), propname, NULL); 84 85 if (prop) { 86 str = prop->value; 87 len = prop->length; 88 } 89 } else { 90 str = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), 91 propname, &len); 92 } 93 if (!str) { 94 debug("<not found>\n"); 95 return NULL; 96 } 97 if (strnlen(str, len) >= len) { 98 debug("<invalid>\n"); 99 return NULL; 100 } 101 debug("%s\n", str); 102 103 return str; 104 } 105 106 ofnode ofnode_find_subnode(ofnode node, const char *subnode_name) 107 { 108 ofnode subnode; 109 110 assert(ofnode_valid(node)); 111 debug("%s: %s: ", __func__, subnode_name); 112 113 if (ofnode_is_np(node)) { 114 const struct device_node *np = ofnode_to_np(node); 115 116 for (np = np->child; np; np = np->sibling) { 117 if (!strcmp(subnode_name, np->name)) 118 break; 119 } 120 subnode = np_to_ofnode(np); 121 } else { 122 int ooffset = fdt_subnode_offset(gd->fdt_blob, 123 ofnode_to_offset(node), subnode_name); 124 subnode = offset_to_ofnode(ooffset); 125 } 126 debug("%s\n", ofnode_valid(subnode) ? 127 ofnode_get_name(subnode) : "<none>"); 128 129 return subnode; 130 } 131 132 int ofnode_read_u32_array(ofnode node, const char *propname, 133 u32 *out_values, size_t sz) 134 { 135 assert(ofnode_valid(node)); 136 debug("%s: %s: ", __func__, propname); 137 138 if (ofnode_is_np(node)) { 139 return of_read_u32_array(ofnode_to_np(node), propname, 140 out_values, sz); 141 } else { 142 return fdtdec_get_int_array(gd->fdt_blob, 143 ofnode_to_offset(node), propname, 144 out_values, sz); 145 } 146 } 147 148 ofnode ofnode_first_subnode(ofnode node) 149 { 150 assert(ofnode_valid(node)); 151 if (ofnode_is_np(node)) 152 return np_to_ofnode(node.np->child); 153 154 return offset_to_ofnode( 155 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node))); 156 } 157 158 ofnode ofnode_next_subnode(ofnode node) 159 { 160 assert(ofnode_valid(node)); 161 if (ofnode_is_np(node)) 162 return np_to_ofnode(node.np->sibling); 163 164 return offset_to_ofnode( 165 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node))); 166 } 167 168 const char *ofnode_get_name(ofnode node) 169 { 170 assert(ofnode_valid(node)); 171 if (ofnode_is_np(node)) 172 return strrchr(node.np->full_name, '/') + 1; 173 174 return fdt_get_name(gd->fdt_blob, ofnode_to_offset(node), NULL); 175 } 176 177 int ofnode_read_size(ofnode node, const char *propname) 178 { 179 int len; 180 181 if (ofnode_is_np(node)) { 182 struct property *prop = of_find_property( 183 ofnode_to_np(node), propname, NULL); 184 185 if (prop) 186 return prop->length; 187 } else { 188 if (fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname, 189 &len)) 190 return len; 191 } 192 193 return -EINVAL; 194 } 195 196 fdt_addr_t ofnode_get_addr_index(ofnode node, int index) 197 { 198 if (ofnode_is_np(node)) { 199 const __be32 *prop_val; 200 uint flags; 201 u64 size; 202 int na; 203 204 prop_val = of_get_address(ofnode_to_np(node), index, &size, 205 &flags); 206 if (!prop_val) 207 return FDT_ADDR_T_NONE; 208 na = of_n_addr_cells(ofnode_to_np(node)); 209 return of_read_number(prop_val, na); 210 } else { 211 return fdt_get_base_address(gd->fdt_blob, 212 ofnode_to_offset(node)); 213 } 214 215 return FDT_ADDR_T_NONE; 216 } 217 218 fdt_addr_t ofnode_get_addr(ofnode node) 219 { 220 return ofnode_get_addr_index(node, 0); 221 } 222 223 int ofnode_stringlist_search(ofnode node, const char *property, 224 const char *string) 225 { 226 if (ofnode_is_np(node)) { 227 return of_property_match_string(ofnode_to_np(node), 228 property, string); 229 } else { 230 int ret; 231 232 ret = fdt_stringlist_search(gd->fdt_blob, 233 ofnode_to_offset(node), property, 234 string); 235 if (ret == -FDT_ERR_NOTFOUND) 236 return -ENODATA; 237 else if (ret < 0) 238 return -EINVAL; 239 240 return ret; 241 } 242 } 243 244 int ofnode_read_string_index(ofnode node, const char *property, int index, 245 const char **outp) 246 { 247 if (ofnode_is_np(node)) { 248 return of_property_read_string_index(ofnode_to_np(node), 249 property, index, outp); 250 } else { 251 int len; 252 253 *outp = fdt_stringlist_get(gd->fdt_blob, ofnode_to_offset(node), 254 property, index, &len); 255 if (len < 0) 256 return -EINVAL; 257 return 0; 258 } 259 } 260 261 int ofnode_read_string_count(ofnode node, const char *property) 262 { 263 if (ofnode_is_np(node)) { 264 return of_property_count_strings(ofnode_to_np(node), property); 265 } else { 266 return fdt_stringlist_count(gd->fdt_blob, 267 ofnode_to_offset(node), property); 268 } 269 } 270 271 static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in, 272 struct ofnode_phandle_args *out) 273 { 274 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS); 275 out->node = offset_to_ofnode(in->node); 276 out->args_count = in->args_count; 277 memcpy(out->args, in->args, sizeof(out->args)); 278 } 279 280 static void ofnode_from_of_phandle_args(struct of_phandle_args *in, 281 struct ofnode_phandle_args *out) 282 { 283 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS); 284 out->node = np_to_ofnode(in->np); 285 out->args_count = in->args_count; 286 memcpy(out->args, in->args, sizeof(out->args)); 287 } 288 289 int ofnode_parse_phandle_with_args(ofnode node, const char *list_name, 290 const char *cells_name, int cell_count, 291 int index, 292 struct ofnode_phandle_args *out_args) 293 { 294 if (ofnode_is_np(node)) { 295 struct of_phandle_args args; 296 int ret; 297 298 ret = of_parse_phandle_with_args(ofnode_to_np(node), 299 list_name, cells_name, index, &args); 300 if (ret) 301 return ret; 302 ofnode_from_of_phandle_args(&args, out_args); 303 } else { 304 struct fdtdec_phandle_args args; 305 int ret; 306 307 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, 308 ofnode_to_offset(node), list_name, cells_name, 309 cell_count, index, &args); 310 if (ret) 311 return ret; 312 ofnode_from_fdtdec_phandle_args(&args, out_args); 313 } 314 315 return 0; 316 } 317 318 ofnode ofnode_path(const char *path) 319 { 320 if (of_live_active()) 321 return np_to_ofnode(of_find_node_by_path(path)); 322 else 323 return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path)); 324 } 325 326 const char *ofnode_get_chosen_prop(const char *name) 327 { 328 ofnode chosen_node; 329 330 chosen_node = ofnode_path("/chosen"); 331 332 return ofnode_read_string(chosen_node, name); 333 } 334 335 ofnode ofnode_get_chosen_node(const char *name) 336 { 337 const char *prop; 338 339 prop = ofnode_get_chosen_prop(name); 340 if (!prop) 341 return ofnode_null(); 342 343 return ofnode_path(prop); 344 } 345 346 static int decode_timing_property(ofnode node, const char *name, 347 struct timing_entry *result) 348 { 349 int length, ret = 0; 350 351 length = ofnode_read_size(node, name); 352 if (length < 0) { 353 debug("%s: could not find property %s\n", 354 ofnode_get_name(node), name); 355 return length; 356 } 357 358 if (length == sizeof(u32)) { 359 result->typ = ofnode_read_u32_default(node, name, 0); 360 result->min = result->typ; 361 result->max = result->typ; 362 } else { 363 ret = ofnode_read_u32_array(node, name, &result->min, 3); 364 } 365 366 return ret; 367 } 368 369 int ofnode_decode_display_timing(ofnode parent, int index, 370 struct display_timing *dt) 371 { 372 int i; 373 ofnode timings, node; 374 u32 val = 0; 375 int ret = 0; 376 377 timings = ofnode_find_subnode(parent, "display-timings"); 378 if (!ofnode_valid(timings)) 379 return -EINVAL; 380 381 for (i = 0, node = ofnode_first_subnode(timings); 382 ofnode_valid(node) && i != index; 383 node = ofnode_first_subnode(node)) 384 i++; 385 386 if (!ofnode_valid(node)) 387 return -EINVAL; 388 389 memset(dt, 0, sizeof(*dt)); 390 391 ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch); 392 ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch); 393 ret |= decode_timing_property(node, "hactive", &dt->hactive); 394 ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len); 395 ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch); 396 ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch); 397 ret |= decode_timing_property(node, "vactive", &dt->vactive); 398 ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len); 399 ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock); 400 401 dt->flags = 0; 402 val = ofnode_read_u32_default(node, "vsync-active", -1); 403 if (val != -1) { 404 dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH : 405 DISPLAY_FLAGS_VSYNC_LOW; 406 } 407 val = ofnode_read_u32_default(node, "hsync-active", -1); 408 if (val != -1) { 409 dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH : 410 DISPLAY_FLAGS_HSYNC_LOW; 411 } 412 val = ofnode_read_u32_default(node, "de-active", -1); 413 if (val != -1) { 414 dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH : 415 DISPLAY_FLAGS_DE_LOW; 416 } 417 val = ofnode_read_u32_default(node, "pixelclk-active", -1); 418 if (val != -1) { 419 dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE : 420 DISPLAY_FLAGS_PIXDATA_NEGEDGE; 421 } 422 423 if (ofnode_read_bool(node, "interlaced")) 424 dt->flags |= DISPLAY_FLAGS_INTERLACED; 425 if (ofnode_read_bool(node, "doublescan")) 426 dt->flags |= DISPLAY_FLAGS_DOUBLESCAN; 427 if (ofnode_read_bool(node, "doubleclk")) 428 dt->flags |= DISPLAY_FLAGS_DOUBLECLK; 429 430 return ret; 431 } 432 433 const void *ofnode_get_property(ofnode node, const char *propname, int *lenp) 434 { 435 if (ofnode_is_np(node)) 436 return of_get_property(ofnode_to_np(node), propname, lenp); 437 else 438 return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), 439 propname, lenp); 440 } 441 442 bool ofnode_is_available(ofnode node) 443 { 444 if (ofnode_is_np(node)) 445 return of_device_is_available(ofnode_to_np(node)); 446 else 447 return fdtdec_get_is_enabled(gd->fdt_blob, 448 ofnode_to_offset(node)); 449 } 450 451 fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property, 452 fdt_size_t *sizep) 453 { 454 if (ofnode_is_np(node)) { 455 int na, ns; 456 int psize; 457 const struct device_node *np = ofnode_to_np(node); 458 const __be32 *prop = of_get_property(np, "reg", &psize); 459 460 na = of_n_addr_cells(np); 461 ns = of_n_addr_cells(np); 462 *sizep = of_read_number(prop + na, ns); 463 return of_read_number(prop, na); 464 } else { 465 return fdtdec_get_addr_size(gd->fdt_blob, 466 ofnode_to_offset(node), property, 467 sizep); 468 } 469 } 470 471 const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname, 472 size_t sz) 473 { 474 if (ofnode_is_np(node)) { 475 const struct device_node *np = ofnode_to_np(node); 476 int psize; 477 const __be32 *prop = of_get_property(np, propname, &psize); 478 479 if (!prop || sz != psize) 480 return NULL; 481 return (uint8_t *)prop; 482 483 } else { 484 return fdtdec_locate_byte_array(gd->fdt_blob, 485 ofnode_to_offset(node), propname, sz); 486 } 487 } 488 489 int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type, 490 const char *propname, struct fdt_pci_addr *addr) 491 { 492 const fdt32_t *cell; 493 int len; 494 int ret = -ENOENT; 495 496 debug("%s: %s: ", __func__, propname); 497 498 /* 499 * If we follow the pci bus bindings strictly, we should check 500 * the value of the node's parent node's #address-cells and 501 * #size-cells. They need to be 3 and 2 accordingly. However, 502 * for simplicity we skip the check here. 503 */ 504 cell = ofnode_get_property(node, propname, &len); 505 if (!cell) 506 goto fail; 507 508 if ((len % FDT_PCI_REG_SIZE) == 0) { 509 int num = len / FDT_PCI_REG_SIZE; 510 int i; 511 512 for (i = 0; i < num; i++) { 513 debug("pci address #%d: %08lx %08lx %08lx\n", i, 514 (ulong)fdt32_to_cpu(cell[0]), 515 (ulong)fdt32_to_cpu(cell[1]), 516 (ulong)fdt32_to_cpu(cell[2])); 517 if ((fdt32_to_cpu(*cell) & type) == type) { 518 addr->phys_hi = fdt32_to_cpu(cell[0]); 519 addr->phys_mid = fdt32_to_cpu(cell[1]); 520 addr->phys_lo = fdt32_to_cpu(cell[1]); 521 break; 522 } else { 523 cell += (FDT_PCI_ADDR_CELLS + 524 FDT_PCI_SIZE_CELLS); 525 } 526 } 527 528 if (i == num) { 529 ret = -ENXIO; 530 goto fail; 531 } 532 533 return 0; 534 } else { 535 ret = -EINVAL; 536 } 537 538 fail: 539 debug("(not found)\n"); 540 return ret; 541 } 542 543 int ofnode_read_addr_cells(ofnode node) 544 { 545 if (ofnode_is_np(node)) 546 return of_n_addr_cells(ofnode_to_np(node)); 547 else /* NOTE: this call should walk up the parent stack */ 548 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node)); 549 } 550 551 int ofnode_read_size_cells(ofnode node) 552 { 553 if (ofnode_is_np(node)) 554 return of_n_size_cells(ofnode_to_np(node)); 555 else /* NOTE: this call should walk up the parent stack */ 556 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node)); 557 } 558 559 int ofnode_read_simple_addr_cells(ofnode node) 560 { 561 if (ofnode_is_np(node)) 562 return of_simple_addr_cells(ofnode_to_np(node)); 563 else 564 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node)); 565 } 566 567 int ofnode_read_simple_size_cells(ofnode node) 568 { 569 if (ofnode_is_np(node)) 570 return of_simple_size_cells(ofnode_to_np(node)); 571 else 572 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node)); 573 } 574 575 bool ofnode_pre_reloc(ofnode node) 576 { 577 if (ofnode_read_bool(node, "u-boot,dm-pre-reloc")) 578 return true; 579 580 #ifdef CONFIG_TPL_BUILD 581 if (ofnode_read_bool(node, "u-boot,dm-tpl")) 582 return true; 583 #elif defined(CONFIG_SPL_BUILD) 584 if (ofnode_read_bool(node, "u-boot,dm-spl")) 585 return true; 586 #else 587 /* 588 * In regular builds individual spl and tpl handling both 589 * count as handled pre-relocation for later second init. 590 */ 591 if (ofnode_read_bool(node, "u-boot,dm-spl") || 592 ofnode_read_bool(node, "u-boot,dm-tpl")) 593 return true; 594 #endif 595 596 return false; 597 } 598 599 int ofnode_read_resource(ofnode node, uint index, struct resource *res) 600 { 601 if (ofnode_is_np(node)) { 602 return of_address_to_resource(ofnode_to_np(node), index, res); 603 } else { 604 struct fdt_resource fres; 605 int ret; 606 607 ret = fdt_get_resource(gd->fdt_blob, ofnode_to_offset(node), 608 "reg", index, &fres); 609 if (ret < 0) 610 return -EINVAL; 611 memset(res, '\0', sizeof(*res)); 612 res->start = fres.start; 613 res->end = fres.end; 614 615 return 0; 616 } 617 } 618