1 /* 2 * Copyright (c) 2011 The Chromium OS Authors. 3 * SPDX-License-Identifier: GPL-2.0+ 4 */ 5 6 #ifndef USE_HOSTCC 7 #include <common.h> 8 #include <boot_fit.h> 9 #include <dm.h> 10 #include <dm/of_extra.h> 11 #include <errno.h> 12 #include <fdtdec.h> 13 #include <fdt_support.h> 14 #include <linux/libfdt.h> 15 #include <serial.h> 16 #include <asm/sections.h> 17 #include <linux/ctype.h> 18 #include <linux/lzo.h> 19 20 DECLARE_GLOBAL_DATA_PTR; 21 22 /* 23 * Here are the type we know about. One day we might allow drivers to 24 * register. For now we just put them here. The COMPAT macro allows us to 25 * turn this into a sparse list later, and keeps the ID with the name. 26 * 27 * NOTE: This list is basically a TODO list for things that need to be 28 * converted to driver model. So don't add new things here unless there is a 29 * good reason why driver-model conversion is infeasible. Examples include 30 * things which are used before driver model is available. 31 */ 32 #define COMPAT(id, name) name 33 static const char * const compat_names[COMPAT_COUNT] = { 34 COMPAT(UNKNOWN, "<none>"), 35 COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"), 36 COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"), 37 COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"), 38 COMPAT(NVIDIA_TEGRA124_XUSB_PADCTL, "nvidia,tegra124-xusb-padctl"), 39 COMPAT(NVIDIA_TEGRA210_XUSB_PADCTL, "nvidia,tegra210-xusb-padctl"), 40 COMPAT(SMSC_LAN9215, "smsc,lan9215"), 41 COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"), 42 COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"), 43 COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"), 44 COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"), 45 COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"), 46 COMPAT(SAMSUNG_EXYNOS5_USB3_PHY, "samsung,exynos5250-usb3-phy"), 47 COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"), 48 COMPAT(SAMSUNG_EXYNOS_MIPI_DSI, "samsung,exynos-mipi-dsi"), 49 COMPAT(SAMSUNG_EXYNOS_DWMMC, "samsung,exynos-dwmmc"), 50 COMPAT(SAMSUNG_EXYNOS_MMC, "samsung,exynos-mmc"), 51 COMPAT(MAXIM_MAX77686_PMIC, "maxim,max77686"), 52 COMPAT(GENERIC_SPI_FLASH, "spi-flash"), 53 COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"), 54 COMPAT(SAMSUNG_EXYNOS5_I2C, "samsung,exynos5-hsi2c"), 55 COMPAT(SAMSUNG_EXYNOS_SYSMMU, "samsung,sysmmu-v3.3"), 56 COMPAT(INTEL_MICROCODE, "intel,microcode"), 57 COMPAT(AMS_AS3722, "ams,as3722"), 58 COMPAT(INTEL_QRK_MRC, "intel,quark-mrc"), 59 COMPAT(ALTERA_SOCFPGA_DWMAC, "altr,socfpga-stmmac"), 60 COMPAT(ALTERA_SOCFPGA_DWMMC, "altr,socfpga-dw-mshc"), 61 COMPAT(ALTERA_SOCFPGA_DWC2USB, "snps,dwc2"), 62 COMPAT(INTEL_BAYTRAIL_FSP, "intel,baytrail-fsp"), 63 COMPAT(INTEL_BAYTRAIL_FSP_MDP, "intel,baytrail-fsp-mdp"), 64 COMPAT(INTEL_IVYBRIDGE_FSP, "intel,ivybridge-fsp"), 65 COMPAT(COMPAT_SUNXI_NAND, "allwinner,sun4i-a10-nand"), 66 COMPAT(ALTERA_SOCFPGA_CLK, "altr,clk-mgr"), 67 COMPAT(ALTERA_SOCFPGA_PINCTRL_SINGLE, "pinctrl-single"), 68 COMPAT(ALTERA_SOCFPGA_H2F_BRG, "altr,socfpga-hps2fpga-bridge"), 69 COMPAT(ALTERA_SOCFPGA_LWH2F_BRG, "altr,socfpga-lwhps2fpga-bridge"), 70 COMPAT(ALTERA_SOCFPGA_F2H_BRG, "altr,socfpga-fpga2hps-bridge"), 71 COMPAT(ALTERA_SOCFPGA_F2SDR0, "altr,socfpga-fpga2sdram0-bridge"), 72 COMPAT(ALTERA_SOCFPGA_F2SDR1, "altr,socfpga-fpga2sdram1-bridge"), 73 COMPAT(ALTERA_SOCFPGA_F2SDR2, "altr,socfpga-fpga2sdram2-bridge"), 74 COMPAT(ROCKCHIP_NANDC, "rockchip,nandc"), 75 }; 76 77 const char *fdtdec_get_compatible(enum fdt_compat_id id) 78 { 79 /* We allow reading of the 'unknown' ID for testing purposes */ 80 assert(id >= 0 && id < COMPAT_COUNT); 81 return compat_names[id]; 82 } 83 84 fdt_addr_t fdtdec_get_addr_size_fixed(const void *blob, int node, 85 const char *prop_name, int index, int na, int ns, 86 fdt_size_t *sizep, bool translate) 87 { 88 const fdt32_t *prop, *prop_end; 89 const fdt32_t *prop_addr, *prop_size, *prop_after_size; 90 int len; 91 fdt_addr_t addr; 92 93 debug("%s: %s: ", __func__, prop_name); 94 95 if (na > (sizeof(fdt_addr_t) / sizeof(fdt32_t))) { 96 debug("(na too large for fdt_addr_t type)\n"); 97 return FDT_ADDR_T_NONE; 98 } 99 100 if (ns > (sizeof(fdt_size_t) / sizeof(fdt32_t))) { 101 debug("(ns too large for fdt_size_t type)\n"); 102 return FDT_ADDR_T_NONE; 103 } 104 105 prop = fdt_getprop(blob, node, prop_name, &len); 106 if (!prop) { 107 debug("(not found)\n"); 108 return FDT_ADDR_T_NONE; 109 } 110 prop_end = prop + (len / sizeof(*prop)); 111 112 prop_addr = prop + (index * (na + ns)); 113 prop_size = prop_addr + na; 114 prop_after_size = prop_size + ns; 115 if (prop_after_size > prop_end) { 116 debug("(not enough data: expected >= %d cells, got %d cells)\n", 117 (u32)(prop_after_size - prop), ((u32)(prop_end - prop))); 118 return FDT_ADDR_T_NONE; 119 } 120 121 #if CONFIG_IS_ENABLED(OF_TRANSLATE) 122 if (translate) 123 addr = fdt_translate_address(blob, node, prop_addr); 124 else 125 #endif 126 addr = fdtdec_get_number(prop_addr, na); 127 128 if (sizep) { 129 *sizep = fdtdec_get_number(prop_size, ns); 130 debug("addr=%08llx, size=%llx\n", (unsigned long long)addr, 131 (unsigned long long)*sizep); 132 } else { 133 debug("addr=%08llx\n", (unsigned long long)addr); 134 } 135 136 return addr; 137 } 138 139 fdt_addr_t fdtdec_get_addr_size_auto_parent(const void *blob, int parent, 140 int node, const char *prop_name, int index, fdt_size_t *sizep, 141 bool translate) 142 { 143 int na, ns; 144 145 debug("%s: ", __func__); 146 147 na = fdt_address_cells(blob, parent); 148 if (na < 1) { 149 debug("(bad #address-cells)\n"); 150 return FDT_ADDR_T_NONE; 151 } 152 153 ns = fdt_size_cells(blob, parent); 154 if (ns < 0) { 155 debug("(bad #size-cells)\n"); 156 return FDT_ADDR_T_NONE; 157 } 158 159 debug("na=%d, ns=%d, ", na, ns); 160 161 return fdtdec_get_addr_size_fixed(blob, node, prop_name, index, na, 162 ns, sizep, translate); 163 } 164 165 fdt_addr_t fdtdec_get_addr_size_auto_noparent(const void *blob, int node, 166 const char *prop_name, int index, fdt_size_t *sizep, 167 bool translate) 168 { 169 int parent; 170 171 debug("%s: ", __func__); 172 173 parent = fdt_parent_offset(blob, node); 174 if (parent < 0) { 175 debug("(no parent found)\n"); 176 return FDT_ADDR_T_NONE; 177 } 178 179 return fdtdec_get_addr_size_auto_parent(blob, parent, node, prop_name, 180 index, sizep, translate); 181 } 182 183 fdt_addr_t fdtdec_get_addr_size(const void *blob, int node, 184 const char *prop_name, fdt_size_t *sizep) 185 { 186 int ns = sizep ? (sizeof(fdt_size_t) / sizeof(fdt32_t)) : 0; 187 188 return fdtdec_get_addr_size_fixed(blob, node, prop_name, 0, 189 sizeof(fdt_addr_t) / sizeof(fdt32_t), 190 ns, sizep, false); 191 } 192 193 fdt_addr_t fdtdec_get_addr(const void *blob, int node, 194 const char *prop_name) 195 { 196 return fdtdec_get_addr_size(blob, node, prop_name, NULL); 197 } 198 199 #if defined(CONFIG_PCI) && defined(CONFIG_DM_PCI) 200 int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type, 201 const char *prop_name, struct fdt_pci_addr *addr) 202 { 203 const u32 *cell; 204 int len; 205 int ret = -ENOENT; 206 207 debug("%s: %s: ", __func__, prop_name); 208 209 /* 210 * If we follow the pci bus bindings strictly, we should check 211 * the value of the node's parent node's #address-cells and 212 * #size-cells. They need to be 3 and 2 accordingly. However, 213 * for simplicity we skip the check here. 214 */ 215 cell = fdt_getprop(blob, node, prop_name, &len); 216 if (!cell) 217 goto fail; 218 219 if ((len % FDT_PCI_REG_SIZE) == 0) { 220 int num = len / FDT_PCI_REG_SIZE; 221 int i; 222 223 for (i = 0; i < num; i++) { 224 debug("pci address #%d: %08lx %08lx %08lx\n", i, 225 (ulong)fdt32_to_cpu(cell[0]), 226 (ulong)fdt32_to_cpu(cell[1]), 227 (ulong)fdt32_to_cpu(cell[2])); 228 if ((fdt32_to_cpu(*cell) & type) == type) { 229 addr->phys_hi = fdt32_to_cpu(cell[0]); 230 addr->phys_mid = fdt32_to_cpu(cell[1]); 231 addr->phys_lo = fdt32_to_cpu(cell[1]); 232 break; 233 } else { 234 cell += (FDT_PCI_ADDR_CELLS + 235 FDT_PCI_SIZE_CELLS); 236 } 237 } 238 239 if (i == num) { 240 ret = -ENXIO; 241 goto fail; 242 } 243 244 return 0; 245 } else { 246 ret = -EINVAL; 247 } 248 249 fail: 250 debug("(not found)\n"); 251 return ret; 252 } 253 254 int fdtdec_get_pci_vendev(const void *blob, int node, u16 *vendor, u16 *device) 255 { 256 const char *list, *end; 257 int len; 258 259 list = fdt_getprop(blob, node, "compatible", &len); 260 if (!list) 261 return -ENOENT; 262 263 end = list + len; 264 while (list < end) { 265 char *s; 266 267 len = strlen(list); 268 if (len >= strlen("pciVVVV,DDDD")) { 269 s = strstr(list, "pci"); 270 271 /* 272 * check if the string is something like pciVVVV,DDDD.RR 273 * or just pciVVVV,DDDD 274 */ 275 if (s && s[7] == ',' && 276 (s[12] == '.' || s[12] == 0)) { 277 s += 3; 278 *vendor = simple_strtol(s, NULL, 16); 279 280 s += 5; 281 *device = simple_strtol(s, NULL, 16); 282 283 return 0; 284 } 285 } 286 list += (len + 1); 287 } 288 289 return -ENOENT; 290 } 291 292 int fdtdec_get_pci_bar32(struct udevice *dev, struct fdt_pci_addr *addr, 293 u32 *bar) 294 { 295 int barnum; 296 297 /* extract the bar number from fdt_pci_addr */ 298 barnum = addr->phys_hi & 0xff; 299 if ((barnum < PCI_BASE_ADDRESS_0) || (barnum > PCI_CARDBUS_CIS)) 300 return -EINVAL; 301 302 barnum = (barnum - PCI_BASE_ADDRESS_0) / 4; 303 *bar = dm_pci_read_bar32(dev, barnum); 304 305 return 0; 306 } 307 #endif 308 309 uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name, 310 uint64_t default_val) 311 { 312 const uint64_t *cell64; 313 int length; 314 315 cell64 = fdt_getprop(blob, node, prop_name, &length); 316 if (!cell64 || length < sizeof(*cell64)) 317 return default_val; 318 319 return fdt64_to_cpu(*cell64); 320 } 321 322 int fdtdec_get_is_enabled(const void *blob, int node) 323 { 324 const char *cell; 325 326 /* 327 * It should say "okay", so only allow that. Some fdts use "ok" but 328 * this is a bug. Please fix your device tree source file. See here 329 * for discussion: 330 * 331 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html 332 */ 333 cell = fdt_getprop(blob, node, "status", NULL); 334 if (cell) 335 return 0 == strcmp(cell, "okay"); 336 return 1; 337 } 338 339 enum fdt_compat_id fdtdec_lookup(const void *blob, int node) 340 { 341 enum fdt_compat_id id; 342 343 /* Search our drivers */ 344 for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++) 345 if (0 == fdt_node_check_compatible(blob, node, 346 compat_names[id])) 347 return id; 348 return COMPAT_UNKNOWN; 349 } 350 351 int fdtdec_next_compatible(const void *blob, int node, 352 enum fdt_compat_id id) 353 { 354 return fdt_node_offset_by_compatible(blob, node, compat_names[id]); 355 } 356 357 int fdtdec_next_compatible_subnode(const void *blob, int node, 358 enum fdt_compat_id id, int *depthp) 359 { 360 do { 361 node = fdt_next_node(blob, node, depthp); 362 } while (*depthp > 1); 363 364 /* If this is a direct subnode, and compatible, return it */ 365 if (*depthp == 1 && 0 == fdt_node_check_compatible( 366 blob, node, compat_names[id])) 367 return node; 368 369 return -FDT_ERR_NOTFOUND; 370 } 371 372 int fdtdec_next_alias(const void *blob, const char *name, 373 enum fdt_compat_id id, int *upto) 374 { 375 #define MAX_STR_LEN 20 376 char str[MAX_STR_LEN + 20]; 377 int node, err; 378 379 /* snprintf() is not available */ 380 assert(strlen(name) < MAX_STR_LEN); 381 sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto); 382 node = fdt_path_offset(blob, str); 383 if (node < 0) 384 return node; 385 err = fdt_node_check_compatible(blob, node, compat_names[id]); 386 if (err < 0) 387 return err; 388 if (err) 389 return -FDT_ERR_NOTFOUND; 390 (*upto)++; 391 return node; 392 } 393 394 int fdtdec_find_aliases_for_id(const void *blob, const char *name, 395 enum fdt_compat_id id, int *node_list, int maxcount) 396 { 397 memset(node_list, '\0', sizeof(*node_list) * maxcount); 398 399 return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount); 400 } 401 402 /* TODO: Can we tighten this code up a little? */ 403 int fdtdec_add_aliases_for_id(const void *blob, const char *name, 404 enum fdt_compat_id id, int *node_list, int maxcount) 405 { 406 int name_len = strlen(name); 407 int nodes[maxcount]; 408 int num_found = 0; 409 int offset, node; 410 int alias_node; 411 int count; 412 int i, j; 413 414 /* find the alias node if present */ 415 alias_node = fdt_path_offset(blob, "/aliases"); 416 417 /* 418 * start with nothing, and we can assume that the root node can't 419 * match 420 */ 421 memset(nodes, '\0', sizeof(nodes)); 422 423 /* First find all the compatible nodes */ 424 for (node = count = 0; node >= 0 && count < maxcount;) { 425 node = fdtdec_next_compatible(blob, node, id); 426 if (node >= 0) 427 nodes[count++] = node; 428 } 429 if (node >= 0) 430 debug("%s: warning: maxcount exceeded with alias '%s'\n", 431 __func__, name); 432 433 /* Now find all the aliases */ 434 for (offset = fdt_first_property_offset(blob, alias_node); 435 offset > 0; 436 offset = fdt_next_property_offset(blob, offset)) { 437 const struct fdt_property *prop; 438 const char *path; 439 int number; 440 int found; 441 442 node = 0; 443 prop = fdt_get_property_by_offset(blob, offset, NULL); 444 path = fdt_string(blob, fdt32_to_cpu(prop->nameoff)); 445 if (prop->len && 0 == strncmp(path, name, name_len)) 446 node = fdt_path_offset(blob, prop->data); 447 if (node <= 0) 448 continue; 449 450 /* Get the alias number */ 451 number = simple_strtoul(path + name_len, NULL, 10); 452 if (number < 0 || number >= maxcount) { 453 debug("%s: warning: alias '%s' is out of range\n", 454 __func__, path); 455 continue; 456 } 457 458 /* Make sure the node we found is actually in our list! */ 459 found = -1; 460 for (j = 0; j < count; j++) 461 if (nodes[j] == node) { 462 found = j; 463 break; 464 } 465 466 if (found == -1) { 467 debug("%s: warning: alias '%s' points to a node " 468 "'%s' that is missing or is not compatible " 469 " with '%s'\n", __func__, path, 470 fdt_get_name(blob, node, NULL), 471 compat_names[id]); 472 continue; 473 } 474 475 /* 476 * Add this node to our list in the right place, and mark 477 * it as done. 478 */ 479 if (fdtdec_get_is_enabled(blob, node)) { 480 if (node_list[number]) { 481 debug("%s: warning: alias '%s' requires that " 482 "a node be placed in the list in a " 483 "position which is already filled by " 484 "node '%s'\n", __func__, path, 485 fdt_get_name(blob, node, NULL)); 486 continue; 487 } 488 node_list[number] = node; 489 if (number >= num_found) 490 num_found = number + 1; 491 } 492 nodes[found] = 0; 493 } 494 495 /* Add any nodes not mentioned by an alias */ 496 for (i = j = 0; i < maxcount; i++) { 497 if (!node_list[i]) { 498 for (; j < maxcount; j++) 499 if (nodes[j] && 500 fdtdec_get_is_enabled(blob, nodes[j])) 501 break; 502 503 /* Have we run out of nodes to add? */ 504 if (j == maxcount) 505 break; 506 507 assert(!node_list[i]); 508 node_list[i] = nodes[j++]; 509 if (i >= num_found) 510 num_found = i + 1; 511 } 512 } 513 514 return num_found; 515 } 516 517 int fdtdec_get_alias_seq(const void *blob, const char *base, int offset, 518 int *seqp) 519 { 520 int base_len = strlen(base); 521 const char *find_name; 522 int find_namelen; 523 int prop_offset; 524 int aliases; 525 526 find_name = fdt_get_name(blob, offset, &find_namelen); 527 debug("Looking for '%s' at %d, name %s\n", base, offset, find_name); 528 529 aliases = fdt_path_offset(blob, "/aliases"); 530 for (prop_offset = fdt_first_property_offset(blob, aliases); 531 prop_offset > 0; 532 prop_offset = fdt_next_property_offset(blob, prop_offset)) { 533 const char *prop; 534 const char *name; 535 const char *slash; 536 int len, val; 537 538 prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len); 539 debug(" - %s, %s\n", name, prop); 540 if (len < find_namelen || *prop != '/' || prop[len - 1] || 541 strncmp(name, base, base_len)) 542 continue; 543 544 slash = strrchr(prop, '/'); 545 if (strcmp(slash + 1, find_name)) 546 continue; 547 val = trailing_strtol(name); 548 if (val != -1) { 549 *seqp = val; 550 debug("Found seq %d\n", *seqp); 551 return 0; 552 } 553 } 554 555 debug("Not found\n"); 556 return -ENOENT; 557 } 558 559 const char *fdtdec_get_chosen_prop(const void *blob, const char *name) 560 { 561 int chosen_node; 562 563 if (!blob) 564 return NULL; 565 chosen_node = fdt_path_offset(blob, "/chosen"); 566 return fdt_getprop(blob, chosen_node, name, NULL); 567 } 568 569 int fdtdec_get_chosen_node(const void *blob, const char *name) 570 { 571 const char *prop; 572 573 prop = fdtdec_get_chosen_prop(blob, name); 574 if (!prop) 575 return -FDT_ERR_NOTFOUND; 576 return fdt_path_offset(blob, prop); 577 } 578 579 int fdtdec_check_fdt(void) 580 { 581 /* 582 * We must have an FDT, but we cannot panic() yet since the console 583 * is not ready. So for now, just assert(). Boards which need an early 584 * FDT (prior to console ready) will need to make their own 585 * arrangements and do their own checks. 586 */ 587 assert(!fdtdec_prepare_fdt()); 588 return 0; 589 } 590 591 /* 592 * This function is a little odd in that it accesses global data. At some 593 * point if the architecture board.c files merge this will make more sense. 594 * Even now, it is common code. 595 */ 596 int fdtdec_prepare_fdt(void) 597 { 598 if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) || 599 fdt_check_header(gd->fdt_blob)) { 600 #ifdef CONFIG_SPL_BUILD 601 puts("Missing DTB\n"); 602 #else 603 puts("No valid device tree binary found - please append one to U-Boot binary, use u-boot-dtb.bin or define CONFIG_OF_EMBED. For sandbox, use -d <file.dtb>\n"); 604 # ifdef DEBUG 605 if (gd->fdt_blob) { 606 printf("fdt_blob=%p\n", gd->fdt_blob); 607 print_buffer((ulong)gd->fdt_blob, gd->fdt_blob, 4, 608 32, 0); 609 } 610 # endif 611 #endif 612 return -1; 613 } 614 return 0; 615 } 616 617 int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name) 618 { 619 const u32 *phandle; 620 int lookup; 621 622 debug("%s: %s\n", __func__, prop_name); 623 phandle = fdt_getprop(blob, node, prop_name, NULL); 624 if (!phandle) 625 return -FDT_ERR_NOTFOUND; 626 627 lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle)); 628 return lookup; 629 } 630 631 /** 632 * Look up a property in a node and check that it has a minimum length. 633 * 634 * @param blob FDT blob 635 * @param node node to examine 636 * @param prop_name name of property to find 637 * @param min_len minimum property length in bytes 638 * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not 639 found, or -FDT_ERR_BADLAYOUT if not enough data 640 * @return pointer to cell, which is only valid if err == 0 641 */ 642 static const void *get_prop_check_min_len(const void *blob, int node, 643 const char *prop_name, int min_len, int *err) 644 { 645 const void *cell; 646 int len; 647 648 debug("%s: %s\n", __func__, prop_name); 649 cell = fdt_getprop(blob, node, prop_name, &len); 650 if (!cell) 651 *err = -FDT_ERR_NOTFOUND; 652 else if (len < min_len) 653 *err = -FDT_ERR_BADLAYOUT; 654 else 655 *err = 0; 656 return cell; 657 } 658 659 int fdtdec_get_int_array(const void *blob, int node, const char *prop_name, 660 u32 *array, int count) 661 { 662 const u32 *cell; 663 int i, err = 0; 664 665 debug("%s: %s\n", __func__, prop_name); 666 cell = get_prop_check_min_len(blob, node, prop_name, 667 sizeof(u32) * count, &err); 668 if (!err) { 669 for (i = 0; i < count; i++) 670 array[i] = fdt32_to_cpu(cell[i]); 671 } 672 return err; 673 } 674 675 int fdtdec_get_int_array_count(const void *blob, int node, 676 const char *prop_name, u32 *array, int count) 677 { 678 const u32 *cell; 679 int len, elems; 680 int i; 681 682 debug("%s: %s\n", __func__, prop_name); 683 cell = fdt_getprop(blob, node, prop_name, &len); 684 if (!cell) 685 return -FDT_ERR_NOTFOUND; 686 elems = len / sizeof(u32); 687 if (count > elems) 688 count = elems; 689 for (i = 0; i < count; i++) 690 array[i] = fdt32_to_cpu(cell[i]); 691 692 return count; 693 } 694 695 const u32 *fdtdec_locate_array(const void *blob, int node, 696 const char *prop_name, int count) 697 { 698 const u32 *cell; 699 int err; 700 701 cell = get_prop_check_min_len(blob, node, prop_name, 702 sizeof(u32) * count, &err); 703 return err ? NULL : cell; 704 } 705 706 int fdtdec_get_bool(const void *blob, int node, const char *prop_name) 707 { 708 const s32 *cell; 709 int len; 710 711 debug("%s: %s\n", __func__, prop_name); 712 cell = fdt_getprop(blob, node, prop_name, &len); 713 return cell != NULL; 714 } 715 716 int fdtdec_parse_phandle_with_args(const void *blob, int src_node, 717 const char *list_name, 718 const char *cells_name, 719 int cell_count, int index, 720 struct fdtdec_phandle_args *out_args) 721 { 722 const __be32 *list, *list_end; 723 int rc = 0, size, cur_index = 0; 724 uint32_t count = 0; 725 int node = -1; 726 int phandle; 727 728 /* Retrieve the phandle list property */ 729 list = fdt_getprop(blob, src_node, list_name, &size); 730 if (!list) 731 return -ENOENT; 732 list_end = list + size / sizeof(*list); 733 734 /* Loop over the phandles until all the requested entry is found */ 735 while (list < list_end) { 736 rc = -EINVAL; 737 count = 0; 738 739 /* 740 * If phandle is 0, then it is an empty entry with no 741 * arguments. Skip forward to the next entry. 742 */ 743 phandle = be32_to_cpup(list++); 744 if (phandle) { 745 /* 746 * Find the provider node and parse the #*-cells 747 * property to determine the argument length. 748 * 749 * This is not needed if the cell count is hard-coded 750 * (i.e. cells_name not set, but cell_count is set), 751 * except when we're going to return the found node 752 * below. 753 */ 754 if (cells_name || cur_index == index) { 755 node = fdt_node_offset_by_phandle(blob, 756 phandle); 757 if (!node) { 758 debug("%s: could not find phandle\n", 759 fdt_get_name(blob, src_node, 760 NULL)); 761 goto err; 762 } 763 } 764 765 if (cells_name) { 766 count = fdtdec_get_int(blob, node, cells_name, 767 -1); 768 if (count == -1) { 769 debug("%s: could not get %s for %s\n", 770 fdt_get_name(blob, src_node, 771 NULL), 772 cells_name, 773 fdt_get_name(blob, node, 774 NULL)); 775 goto err; 776 } 777 } else { 778 count = cell_count; 779 } 780 781 /* 782 * Make sure that the arguments actually fit in the 783 * remaining property data length 784 */ 785 if (list + count > list_end) { 786 debug("%s: arguments longer than property\n", 787 fdt_get_name(blob, src_node, NULL)); 788 goto err; 789 } 790 } 791 792 /* 793 * All of the error cases above bail out of the loop, so at 794 * this point, the parsing is successful. If the requested 795 * index matches, then fill the out_args structure and return, 796 * or return -ENOENT for an empty entry. 797 */ 798 rc = -ENOENT; 799 if (cur_index == index) { 800 if (!phandle) 801 goto err; 802 803 if (out_args) { 804 int i; 805 806 if (count > MAX_PHANDLE_ARGS) { 807 debug("%s: too many arguments %d\n", 808 fdt_get_name(blob, src_node, 809 NULL), count); 810 count = MAX_PHANDLE_ARGS; 811 } 812 out_args->node = node; 813 out_args->args_count = count; 814 for (i = 0; i < count; i++) { 815 out_args->args[i] = 816 be32_to_cpup(list++); 817 } 818 } 819 820 /* Found it! return success */ 821 return 0; 822 } 823 824 node = -1; 825 list += count; 826 cur_index++; 827 } 828 829 /* 830 * Result will be one of: 831 * -ENOENT : index is for empty phandle 832 * -EINVAL : parsing error on data 833 * [1..n] : Number of phandle (count mode; when index = -1) 834 */ 835 rc = index < 0 ? cur_index : -ENOENT; 836 err: 837 return rc; 838 } 839 840 int fdtdec_get_child_count(const void *blob, int node) 841 { 842 int subnode; 843 int num = 0; 844 845 fdt_for_each_subnode(subnode, blob, node) 846 num++; 847 848 return num; 849 } 850 851 int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name, 852 u8 *array, int count) 853 { 854 const u8 *cell; 855 int err; 856 857 cell = get_prop_check_min_len(blob, node, prop_name, count, &err); 858 if (!err) 859 memcpy(array, cell, count); 860 return err; 861 } 862 863 const u8 *fdtdec_locate_byte_array(const void *blob, int node, 864 const char *prop_name, int count) 865 { 866 const u8 *cell; 867 int err; 868 869 cell = get_prop_check_min_len(blob, node, prop_name, count, &err); 870 if (err) 871 return NULL; 872 return cell; 873 } 874 875 int fdtdec_get_config_int(const void *blob, const char *prop_name, 876 int default_val) 877 { 878 int config_node; 879 880 debug("%s: %s\n", __func__, prop_name); 881 config_node = fdt_path_offset(blob, "/config"); 882 if (config_node < 0) 883 return default_val; 884 return fdtdec_get_int(blob, config_node, prop_name, default_val); 885 } 886 887 int fdtdec_get_config_bool(const void *blob, const char *prop_name) 888 { 889 int config_node; 890 const void *prop; 891 892 debug("%s: %s\n", __func__, prop_name); 893 config_node = fdt_path_offset(blob, "/config"); 894 if (config_node < 0) 895 return 0; 896 prop = fdt_get_property(blob, config_node, prop_name, NULL); 897 898 return prop != NULL; 899 } 900 901 char *fdtdec_get_config_string(const void *blob, const char *prop_name) 902 { 903 const char *nodep; 904 int nodeoffset; 905 int len; 906 907 debug("%s: %s\n", __func__, prop_name); 908 nodeoffset = fdt_path_offset(blob, "/config"); 909 if (nodeoffset < 0) 910 return NULL; 911 912 nodep = fdt_getprop(blob, nodeoffset, prop_name, &len); 913 if (!nodep) 914 return NULL; 915 916 return (char *)nodep; 917 } 918 919 int fdtdec_decode_region(const void *blob, int node, const char *prop_name, 920 fdt_addr_t *basep, fdt_size_t *sizep) 921 { 922 const fdt_addr_t *cell; 923 int len; 924 925 debug("%s: %s: %s\n", __func__, fdt_get_name(blob, node, NULL), 926 prop_name); 927 cell = fdt_getprop(blob, node, prop_name, &len); 928 if (!cell || (len < sizeof(fdt_addr_t) * 2)) { 929 debug("cell=%p, len=%d\n", cell, len); 930 return -1; 931 } 932 933 *basep = fdt_addr_to_cpu(*cell); 934 *sizep = fdt_size_to_cpu(cell[1]); 935 debug("%s: base=%08lx, size=%lx\n", __func__, (ulong)*basep, 936 (ulong)*sizep); 937 938 return 0; 939 } 940 941 u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells) 942 { 943 u64 number = 0; 944 945 while (cells--) 946 number = (number << 32) | fdt32_to_cpu(*ptr++); 947 948 return number; 949 } 950 951 int fdt_get_resource(const void *fdt, int node, const char *property, 952 unsigned int index, struct fdt_resource *res) 953 { 954 const fdt32_t *ptr, *end; 955 int na, ns, len, parent; 956 unsigned int i = 0; 957 958 parent = fdt_parent_offset(fdt, node); 959 if (parent < 0) 960 return parent; 961 962 na = fdt_address_cells(fdt, parent); 963 ns = fdt_size_cells(fdt, parent); 964 965 ptr = fdt_getprop(fdt, node, property, &len); 966 if (!ptr) 967 return len; 968 969 end = ptr + len / sizeof(*ptr); 970 971 while (ptr + na + ns <= end) { 972 if (i == index) { 973 res->start = res->end = fdtdec_get_number(ptr, na); 974 res->end += fdtdec_get_number(&ptr[na], ns) - 1; 975 return 0; 976 } 977 978 ptr += na + ns; 979 i++; 980 } 981 982 return -FDT_ERR_NOTFOUND; 983 } 984 985 int fdt_get_named_resource(const void *fdt, int node, const char *property, 986 const char *prop_names, const char *name, 987 struct fdt_resource *res) 988 { 989 int index; 990 991 index = fdt_stringlist_search(fdt, node, prop_names, name); 992 if (index < 0) 993 return index; 994 995 return fdt_get_resource(fdt, node, property, index, res); 996 } 997 998 int fdtdec_decode_memory_region(const void *blob, int config_node, 999 const char *mem_type, const char *suffix, 1000 fdt_addr_t *basep, fdt_size_t *sizep) 1001 { 1002 char prop_name[50]; 1003 const char *mem; 1004 fdt_size_t size, offset_size; 1005 fdt_addr_t base, offset; 1006 int node; 1007 1008 if (config_node == -1) { 1009 config_node = fdt_path_offset(blob, "/config"); 1010 if (config_node < 0) { 1011 debug("%s: Cannot find /config node\n", __func__); 1012 return -ENOENT; 1013 } 1014 } 1015 if (!suffix) 1016 suffix = ""; 1017 1018 snprintf(prop_name, sizeof(prop_name), "%s-memory%s", mem_type, 1019 suffix); 1020 mem = fdt_getprop(blob, config_node, prop_name, NULL); 1021 if (!mem) { 1022 debug("%s: No memory type for '%s', using /memory\n", __func__, 1023 prop_name); 1024 mem = "/memory"; 1025 } 1026 1027 node = fdt_path_offset(blob, mem); 1028 if (node < 0) { 1029 debug("%s: Failed to find node '%s': %s\n", __func__, mem, 1030 fdt_strerror(node)); 1031 return -ENOENT; 1032 } 1033 1034 /* 1035 * Not strictly correct - the memory may have multiple banks. We just 1036 * use the first 1037 */ 1038 if (fdtdec_decode_region(blob, node, "reg", &base, &size)) { 1039 debug("%s: Failed to decode memory region %s\n", __func__, 1040 mem); 1041 return -EINVAL; 1042 } 1043 1044 snprintf(prop_name, sizeof(prop_name), "%s-offset%s", mem_type, 1045 suffix); 1046 if (fdtdec_decode_region(blob, config_node, prop_name, &offset, 1047 &offset_size)) { 1048 debug("%s: Failed to decode memory region '%s'\n", __func__, 1049 prop_name); 1050 return -EINVAL; 1051 } 1052 1053 *basep = base + offset; 1054 *sizep = offset_size; 1055 1056 return 0; 1057 } 1058 1059 static int decode_timing_property(const void *blob, int node, const char *name, 1060 struct timing_entry *result) 1061 { 1062 int length, ret = 0; 1063 const u32 *prop; 1064 1065 prop = fdt_getprop(blob, node, name, &length); 1066 if (!prop) { 1067 debug("%s: could not find property %s\n", 1068 fdt_get_name(blob, node, NULL), name); 1069 return length; 1070 } 1071 1072 if (length == sizeof(u32)) { 1073 result->typ = fdtdec_get_int(blob, node, name, 0); 1074 result->min = result->typ; 1075 result->max = result->typ; 1076 } else { 1077 ret = fdtdec_get_int_array(blob, node, name, &result->min, 3); 1078 } 1079 1080 return ret; 1081 } 1082 1083 int fdtdec_decode_display_timing(const void *blob, int parent, int index, 1084 struct display_timing *dt) 1085 { 1086 int i, node, timings_node; 1087 u32 val = 0; 1088 int ret = 0; 1089 1090 timings_node = fdt_subnode_offset(blob, parent, "display-timings"); 1091 if (timings_node < 0) 1092 return timings_node; 1093 1094 for (i = 0, node = fdt_first_subnode(blob, timings_node); 1095 node > 0 && i != index; 1096 node = fdt_next_subnode(blob, node)) 1097 i++; 1098 1099 if (node < 0) 1100 return node; 1101 1102 memset(dt, 0, sizeof(*dt)); 1103 1104 ret |= decode_timing_property(blob, node, "hback-porch", 1105 &dt->hback_porch); 1106 ret |= decode_timing_property(blob, node, "hfront-porch", 1107 &dt->hfront_porch); 1108 ret |= decode_timing_property(blob, node, "hactive", &dt->hactive); 1109 ret |= decode_timing_property(blob, node, "hsync-len", &dt->hsync_len); 1110 ret |= decode_timing_property(blob, node, "vback-porch", 1111 &dt->vback_porch); 1112 ret |= decode_timing_property(blob, node, "vfront-porch", 1113 &dt->vfront_porch); 1114 ret |= decode_timing_property(blob, node, "vactive", &dt->vactive); 1115 ret |= decode_timing_property(blob, node, "vsync-len", &dt->vsync_len); 1116 ret |= decode_timing_property(blob, node, "clock-frequency", 1117 &dt->pixelclock); 1118 1119 dt->flags = 0; 1120 val = fdtdec_get_int(blob, node, "vsync-active", -1); 1121 if (val != -1) { 1122 dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH : 1123 DISPLAY_FLAGS_VSYNC_LOW; 1124 } 1125 val = fdtdec_get_int(blob, node, "hsync-active", -1); 1126 if (val != -1) { 1127 dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH : 1128 DISPLAY_FLAGS_HSYNC_LOW; 1129 } 1130 val = fdtdec_get_int(blob, node, "de-active", -1); 1131 if (val != -1) { 1132 dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH : 1133 DISPLAY_FLAGS_DE_LOW; 1134 } 1135 val = fdtdec_get_int(blob, node, "pixelclk-active", -1); 1136 if (val != -1) { 1137 dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE : 1138 DISPLAY_FLAGS_PIXDATA_NEGEDGE; 1139 } 1140 1141 if (fdtdec_get_bool(blob, node, "interlaced")) 1142 dt->flags |= DISPLAY_FLAGS_INTERLACED; 1143 if (fdtdec_get_bool(blob, node, "doublescan")) 1144 dt->flags |= DISPLAY_FLAGS_DOUBLESCAN; 1145 if (fdtdec_get_bool(blob, node, "doubleclk")) 1146 dt->flags |= DISPLAY_FLAGS_DOUBLECLK; 1147 1148 return ret; 1149 } 1150 1151 int fdtdec_setup_memory_size(void) 1152 { 1153 int ret, mem; 1154 struct fdt_resource res; 1155 1156 mem = fdt_path_offset(gd->fdt_blob, "/memory"); 1157 if (mem < 0) { 1158 debug("%s: Missing /memory node\n", __func__); 1159 return -EINVAL; 1160 } 1161 1162 ret = fdt_get_resource(gd->fdt_blob, mem, "reg", 0, &res); 1163 if (ret != 0) { 1164 debug("%s: Unable to decode first memory bank\n", __func__); 1165 return -EINVAL; 1166 } 1167 1168 gd->ram_size = (phys_size_t)(res.end - res.start + 1); 1169 debug("%s: Initial DRAM size %llx\n", __func__, 1170 (unsigned long long)gd->ram_size); 1171 1172 return 0; 1173 } 1174 1175 #if defined(CONFIG_NR_DRAM_BANKS) 1176 int fdtdec_setup_memory_banksize(void) 1177 { 1178 int bank, ret, mem, reg = 0; 1179 struct fdt_resource res; 1180 1181 mem = fdt_node_offset_by_prop_value(gd->fdt_blob, -1, "device_type", 1182 "memory", 7); 1183 if (mem < 0) { 1184 debug("%s: Missing /memory node\n", __func__); 1185 return -EINVAL; 1186 } 1187 1188 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) { 1189 ret = fdt_get_resource(gd->fdt_blob, mem, "reg", reg++, &res); 1190 if (ret == -FDT_ERR_NOTFOUND) { 1191 reg = 0; 1192 mem = fdt_node_offset_by_prop_value(gd->fdt_blob, mem, 1193 "device_type", 1194 "memory", 7); 1195 if (mem == -FDT_ERR_NOTFOUND) 1196 break; 1197 1198 ret = fdt_get_resource(gd->fdt_blob, mem, "reg", reg++, &res); 1199 if (ret == -FDT_ERR_NOTFOUND) 1200 break; 1201 } 1202 if (ret != 0) { 1203 return -EINVAL; 1204 } 1205 1206 gd->bd->bi_dram[bank].start = (phys_addr_t)res.start; 1207 gd->bd->bi_dram[bank].size = 1208 (phys_size_t)(res.end - res.start + 1); 1209 1210 debug("%s: DRAM Bank #%d: start = 0x%llx, size = 0x%llx\n", 1211 __func__, bank, 1212 (unsigned long long)gd->bd->bi_dram[bank].start, 1213 (unsigned long long)gd->bd->bi_dram[bank].size); 1214 } 1215 1216 return 0; 1217 } 1218 #endif 1219 1220 #if CONFIG_IS_ENABLED(MULTI_DTB_FIT) 1221 # if CONFIG_IS_ENABLED(MULTI_DTB_FIT_GZIP) ||\ 1222 CONFIG_IS_ENABLED(MULTI_DTB_FIT_LZO) 1223 static int uncompress_blob(const void *src, ulong sz_src, void **dstp) 1224 { 1225 size_t sz_out = CONFIG_SPL_MULTI_DTB_FIT_UNCOMPRESS_SZ; 1226 ulong sz_in = sz_src; 1227 void *dst; 1228 int rc; 1229 1230 if (CONFIG_IS_ENABLED(GZIP)) 1231 if (gzip_parse_header(src, sz_in) < 0) 1232 return -1; 1233 if (CONFIG_IS_ENABLED(LZO)) 1234 if (!lzop_is_valid_header(src)) 1235 return -EBADMSG; 1236 1237 if (CONFIG_IS_ENABLED(MULTI_DTB_FIT_DYN_ALLOC)) { 1238 dst = malloc(sz_out); 1239 if (!dst) { 1240 puts("uncompress_blob: Unable to allocate memory\n"); 1241 return -ENOMEM; 1242 } 1243 } else { 1244 # if CONFIG_IS_ENABLED(MULTI_DTB_FIT_USER_DEFINED_AREA) 1245 dst = (void *)CONFIG_VAL(MULTI_DTB_FIT_USER_DEF_ADDR); 1246 # else 1247 return -ENOTSUPP; 1248 # endif 1249 } 1250 1251 if (CONFIG_IS_ENABLED(GZIP)) 1252 rc = gunzip(dst, sz_out, (u8 *)src, &sz_in); 1253 else if (CONFIG_IS_ENABLED(LZO)) 1254 rc = lzop_decompress(src, sz_in, dst, &sz_out); 1255 1256 if (rc < 0) { 1257 /* not a valid compressed blob */ 1258 puts("uncompress_blob: Unable to uncompress\n"); 1259 if (CONFIG_IS_ENABLED(MULTI_DTB_FIT_DYN_ALLOC)) 1260 free(dst); 1261 return -EBADMSG; 1262 } 1263 *dstp = dst; 1264 return 0; 1265 } 1266 # else 1267 static int uncompress_blob(const void *src, ulong sz_src, void **dstp) 1268 { 1269 return -ENOTSUPP; 1270 } 1271 # endif 1272 #endif 1273 1274 int fdtdec_setup(void) 1275 { 1276 #if CONFIG_IS_ENABLED(OF_CONTROL) 1277 # if CONFIG_IS_ENABLED(MULTI_DTB_FIT) 1278 void *fdt_blob; 1279 # endif 1280 # ifdef CONFIG_OF_EMBED 1281 /* Get a pointer to the FDT */ 1282 # ifdef CONFIG_SPL_BUILD 1283 gd->fdt_blob = __dtb_dt_spl_begin; 1284 # else 1285 gd->fdt_blob = __dtb_dt_begin; 1286 # endif 1287 # elif defined CONFIG_OF_SEPARATE 1288 # ifdef CONFIG_SPL_BUILD 1289 /* FDT is at end of BSS unless it is in a different memory region */ 1290 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS)) 1291 gd->fdt_blob = (ulong *)&_image_binary_end; 1292 else 1293 gd->fdt_blob = (ulong *)&__bss_end; 1294 # else 1295 /* FDT is at end of image */ 1296 gd->fdt_blob = (ulong *)&_end; 1297 # endif 1298 # elif defined(CONFIG_OF_BOARD) 1299 /* Allow the board to override the fdt address. */ 1300 gd->fdt_blob = board_fdt_blob_setup(); 1301 # elif defined(CONFIG_OF_HOSTFILE) 1302 if (sandbox_read_fdt_from_file()) { 1303 puts("Failed to read control FDT\n"); 1304 return -1; 1305 } 1306 # endif 1307 # ifndef CONFIG_SPL_BUILD 1308 /* Allow the early environment to override the fdt address */ 1309 gd->fdt_blob = (void *)env_get_ulong("fdtcontroladdr", 16, 1310 (uintptr_t)gd->fdt_blob); 1311 # endif 1312 1313 # if CONFIG_IS_ENABLED(MULTI_DTB_FIT) 1314 /* 1315 * Try and uncompress the blob. 1316 * Unfortunately there is no way to know how big the input blob really 1317 * is. So let us set the maximum input size arbitrarily high. 16MB 1318 * ought to be more than enough for packed DTBs. 1319 */ 1320 if (uncompress_blob(gd->fdt_blob, 0x1000000, &fdt_blob) == 0) 1321 gd->fdt_blob = fdt_blob; 1322 1323 /* 1324 * Check if blob is a FIT images containings DTBs. 1325 * If so, pick the most relevant 1326 */ 1327 fdt_blob = locate_dtb_in_fit(gd->fdt_blob); 1328 if (fdt_blob) 1329 gd->fdt_blob = fdt_blob; 1330 # endif 1331 #endif 1332 1333 return fdtdec_prepare_fdt(); 1334 } 1335 1336 #endif /* !USE_HOSTCC */ 1337