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