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