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