1 /* 2 * Copyright (c) 2011 The Chromium OS Authors. 3 * SPDX-License-Identifier: GPL-2.0+ 4 */ 5 6 #include <common.h> 7 #include <serial.h> 8 #include <libfdt.h> 9 #include <fdtdec.h> 10 11 #include <asm/gpio.h> 12 13 DECLARE_GLOBAL_DATA_PTR; 14 15 /* 16 * Here are the type we know about. One day we might allow drivers to 17 * register. For now we just put them here. The COMPAT macro allows us to 18 * turn this into a sparse list later, and keeps the ID with the name. 19 */ 20 #define COMPAT(id, name) name 21 static const char * const compat_names[COMPAT_COUNT] = { 22 COMPAT(UNKNOWN, "<none>"), 23 COMPAT(NVIDIA_TEGRA20_USB, "nvidia,tegra20-ehci"), 24 COMPAT(NVIDIA_TEGRA30_USB, "nvidia,tegra30-ehci"), 25 COMPAT(NVIDIA_TEGRA114_USB, "nvidia,tegra114-ehci"), 26 COMPAT(NVIDIA_TEGRA114_I2C, "nvidia,tegra114-i2c"), 27 COMPAT(NVIDIA_TEGRA20_I2C, "nvidia,tegra20-i2c"), 28 COMPAT(NVIDIA_TEGRA20_DVC, "nvidia,tegra20-i2c-dvc"), 29 COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"), 30 COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"), 31 COMPAT(NVIDIA_TEGRA20_KBC, "nvidia,tegra20-kbc"), 32 COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"), 33 COMPAT(NVIDIA_TEGRA20_PWM, "nvidia,tegra20-pwm"), 34 COMPAT(NVIDIA_TEGRA20_DC, "nvidia,tegra20-dc"), 35 COMPAT(NVIDIA_TEGRA30_SDMMC, "nvidia,tegra30-sdhci"), 36 COMPAT(NVIDIA_TEGRA20_SDMMC, "nvidia,tegra20-sdhci"), 37 COMPAT(NVIDIA_TEGRA20_SFLASH, "nvidia,tegra20-sflash"), 38 COMPAT(NVIDIA_TEGRA20_SLINK, "nvidia,tegra20-slink"), 39 COMPAT(NVIDIA_TEGRA114_SPI, "nvidia,tegra114-spi"), 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_SPI, "samsung,exynos-spi"), 46 COMPAT(GOOGLE_CROS_EC, "google,cros-ec"), 47 COMPAT(GOOGLE_CROS_EC_KEYB, "google,cros-ec-keyb"), 48 COMPAT(SAMSUNG_EXYNOS_EHCI, "samsung,exynos-ehci"), 49 COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"), 50 COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"), 51 COMPAT(SAMSUNG_EXYNOS_FIMD, "samsung,exynos-fimd"), 52 COMPAT(SAMSUNG_EXYNOS5_DP, "samsung,exynos5-dp"), 53 COMPAT(SAMSUNG_EXYNOS5_DWMMC, "samsung,exynos5250-dwmmc"), 54 COMPAT(SAMSUNG_EXYNOS_SERIAL, "samsung,exynos4210-uart"), 55 COMPAT(MAXIM_MAX77686_PMIC, "maxim,max77686_pmic"), 56 COMPAT(GENERIC_SPI_FLASH, "spi-flash"), 57 COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"), 58 COMPAT(INFINEON_SLB9635_TPM, "infineon,slb9635-tpm"), 59 COMPAT(INFINEON_SLB9645_TPM, "infineon,slb9645-tpm"), 60 }; 61 62 const char *fdtdec_get_compatible(enum fdt_compat_id id) 63 { 64 /* We allow reading of the 'unknown' ID for testing purposes */ 65 assert(id >= 0 && id < COMPAT_COUNT); 66 return compat_names[id]; 67 } 68 69 fdt_addr_t fdtdec_get_addr_size(const void *blob, int node, 70 const char *prop_name, fdt_size_t *sizep) 71 { 72 const fdt_addr_t *cell; 73 int len; 74 75 debug("%s: %s: ", __func__, prop_name); 76 cell = fdt_getprop(blob, node, prop_name, &len); 77 if (cell && ((!sizep && len == sizeof(fdt_addr_t)) || 78 len == sizeof(fdt_addr_t) * 2)) { 79 fdt_addr_t addr = fdt_addr_to_cpu(*cell); 80 if (sizep) { 81 const fdt_size_t *size; 82 83 size = (fdt_size_t *)((char *)cell + 84 sizeof(fdt_addr_t)); 85 *sizep = fdt_size_to_cpu(*size); 86 debug("addr=%p, size=%p\n", (void *)addr, 87 (void *)*sizep); 88 } else { 89 debug("%p\n", (void *)addr); 90 } 91 return addr; 92 } 93 debug("(not found)\n"); 94 return FDT_ADDR_T_NONE; 95 } 96 97 fdt_addr_t fdtdec_get_addr(const void *blob, int node, 98 const char *prop_name) 99 { 100 return fdtdec_get_addr_size(blob, node, prop_name, NULL); 101 } 102 103 s32 fdtdec_get_int(const void *blob, int node, const char *prop_name, 104 s32 default_val) 105 { 106 const s32 *cell; 107 int len; 108 109 debug("%s: %s: ", __func__, prop_name); 110 cell = fdt_getprop(blob, node, prop_name, &len); 111 if (cell && len >= sizeof(s32)) { 112 s32 val = fdt32_to_cpu(cell[0]); 113 114 debug("%#x (%d)\n", val, val); 115 return val; 116 } 117 debug("(not found)\n"); 118 return default_val; 119 } 120 121 uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name, 122 uint64_t default_val) 123 { 124 const uint64_t *cell64; 125 int length; 126 127 cell64 = fdt_getprop(blob, node, prop_name, &length); 128 if (!cell64 || length < sizeof(*cell64)) 129 return default_val; 130 131 return fdt64_to_cpu(*cell64); 132 } 133 134 int fdtdec_get_is_enabled(const void *blob, int node) 135 { 136 const char *cell; 137 138 /* 139 * It should say "okay", so only allow that. Some fdts use "ok" but 140 * this is a bug. Please fix your device tree source file. See here 141 * for discussion: 142 * 143 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html 144 */ 145 cell = fdt_getprop(blob, node, "status", NULL); 146 if (cell) 147 return 0 == strcmp(cell, "okay"); 148 return 1; 149 } 150 151 enum fdt_compat_id fdtdec_lookup(const void *blob, int node) 152 { 153 enum fdt_compat_id id; 154 155 /* Search our drivers */ 156 for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++) 157 if (0 == fdt_node_check_compatible(blob, node, 158 compat_names[id])) 159 return id; 160 return COMPAT_UNKNOWN; 161 } 162 163 int fdtdec_next_compatible(const void *blob, int node, 164 enum fdt_compat_id id) 165 { 166 return fdt_node_offset_by_compatible(blob, node, compat_names[id]); 167 } 168 169 int fdtdec_next_compatible_subnode(const void *blob, int node, 170 enum fdt_compat_id id, int *depthp) 171 { 172 do { 173 node = fdt_next_node(blob, node, depthp); 174 } while (*depthp > 1); 175 176 /* If this is a direct subnode, and compatible, return it */ 177 if (*depthp == 1 && 0 == fdt_node_check_compatible( 178 blob, node, compat_names[id])) 179 return node; 180 181 return -FDT_ERR_NOTFOUND; 182 } 183 184 int fdtdec_next_alias(const void *blob, const char *name, 185 enum fdt_compat_id id, int *upto) 186 { 187 #define MAX_STR_LEN 20 188 char str[MAX_STR_LEN + 20]; 189 int node, err; 190 191 /* snprintf() is not available */ 192 assert(strlen(name) < MAX_STR_LEN); 193 sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto); 194 node = fdt_path_offset(blob, str); 195 if (node < 0) 196 return node; 197 err = fdt_node_check_compatible(blob, node, compat_names[id]); 198 if (err < 0) 199 return err; 200 if (err) 201 return -FDT_ERR_NOTFOUND; 202 (*upto)++; 203 return node; 204 } 205 206 int fdtdec_find_aliases_for_id(const void *blob, const char *name, 207 enum fdt_compat_id id, int *node_list, int maxcount) 208 { 209 memset(node_list, '\0', sizeof(*node_list) * maxcount); 210 211 return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount); 212 } 213 214 /* TODO: Can we tighten this code up a little? */ 215 int fdtdec_add_aliases_for_id(const void *blob, const char *name, 216 enum fdt_compat_id id, int *node_list, int maxcount) 217 { 218 int name_len = strlen(name); 219 int nodes[maxcount]; 220 int num_found = 0; 221 int offset, node; 222 int alias_node; 223 int count; 224 int i, j; 225 226 /* find the alias node if present */ 227 alias_node = fdt_path_offset(blob, "/aliases"); 228 229 /* 230 * start with nothing, and we can assume that the root node can't 231 * match 232 */ 233 memset(nodes, '\0', sizeof(nodes)); 234 235 /* First find all the compatible nodes */ 236 for (node = count = 0; node >= 0 && count < maxcount;) { 237 node = fdtdec_next_compatible(blob, node, id); 238 if (node >= 0) 239 nodes[count++] = node; 240 } 241 if (node >= 0) 242 debug("%s: warning: maxcount exceeded with alias '%s'\n", 243 __func__, name); 244 245 /* Now find all the aliases */ 246 for (offset = fdt_first_property_offset(blob, alias_node); 247 offset > 0; 248 offset = fdt_next_property_offset(blob, offset)) { 249 const struct fdt_property *prop; 250 const char *path; 251 int number; 252 int found; 253 254 node = 0; 255 prop = fdt_get_property_by_offset(blob, offset, NULL); 256 path = fdt_string(blob, fdt32_to_cpu(prop->nameoff)); 257 if (prop->len && 0 == strncmp(path, name, name_len)) 258 node = fdt_path_offset(blob, prop->data); 259 if (node <= 0) 260 continue; 261 262 /* Get the alias number */ 263 number = simple_strtoul(path + name_len, NULL, 10); 264 if (number < 0 || number >= maxcount) { 265 debug("%s: warning: alias '%s' is out of range\n", 266 __func__, path); 267 continue; 268 } 269 270 /* Make sure the node we found is actually in our list! */ 271 found = -1; 272 for (j = 0; j < count; j++) 273 if (nodes[j] == node) { 274 found = j; 275 break; 276 } 277 278 if (found == -1) { 279 debug("%s: warning: alias '%s' points to a node " 280 "'%s' that is missing or is not compatible " 281 " with '%s'\n", __func__, path, 282 fdt_get_name(blob, node, NULL), 283 compat_names[id]); 284 continue; 285 } 286 287 /* 288 * Add this node to our list in the right place, and mark 289 * it as done. 290 */ 291 if (fdtdec_get_is_enabled(blob, node)) { 292 if (node_list[number]) { 293 debug("%s: warning: alias '%s' requires that " 294 "a node be placed in the list in a " 295 "position which is already filled by " 296 "node '%s'\n", __func__, path, 297 fdt_get_name(blob, node, NULL)); 298 continue; 299 } 300 node_list[number] = node; 301 if (number >= num_found) 302 num_found = number + 1; 303 } 304 nodes[found] = 0; 305 } 306 307 /* Add any nodes not mentioned by an alias */ 308 for (i = j = 0; i < maxcount; i++) { 309 if (!node_list[i]) { 310 for (; j < maxcount; j++) 311 if (nodes[j] && 312 fdtdec_get_is_enabled(blob, nodes[j])) 313 break; 314 315 /* Have we run out of nodes to add? */ 316 if (j == maxcount) 317 break; 318 319 assert(!node_list[i]); 320 node_list[i] = nodes[j++]; 321 if (i >= num_found) 322 num_found = i + 1; 323 } 324 } 325 326 return num_found; 327 } 328 329 int fdtdec_check_fdt(void) 330 { 331 /* 332 * We must have an FDT, but we cannot panic() yet since the console 333 * is not ready. So for now, just assert(). Boards which need an early 334 * FDT (prior to console ready) will need to make their own 335 * arrangements and do their own checks. 336 */ 337 assert(!fdtdec_prepare_fdt()); 338 return 0; 339 } 340 341 /* 342 * This function is a little odd in that it accesses global data. At some 343 * point if the architecture board.c files merge this will make more sense. 344 * Even now, it is common code. 345 */ 346 int fdtdec_prepare_fdt(void) 347 { 348 if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) || 349 fdt_check_header(gd->fdt_blob)) { 350 printf("No valid FDT found - please append one to U-Boot " 351 "binary, use u-boot-dtb.bin or define " 352 "CONFIG_OF_EMBED. For sandbox, use -d <file.dtb>\n"); 353 return -1; 354 } 355 return 0; 356 } 357 358 int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name) 359 { 360 const u32 *phandle; 361 int lookup; 362 363 debug("%s: %s\n", __func__, prop_name); 364 phandle = fdt_getprop(blob, node, prop_name, NULL); 365 if (!phandle) 366 return -FDT_ERR_NOTFOUND; 367 368 lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle)); 369 return lookup; 370 } 371 372 /** 373 * Look up a property in a node and check that it has a minimum length. 374 * 375 * @param blob FDT blob 376 * @param node node to examine 377 * @param prop_name name of property to find 378 * @param min_len minimum property length in bytes 379 * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not 380 found, or -FDT_ERR_BADLAYOUT if not enough data 381 * @return pointer to cell, which is only valid if err == 0 382 */ 383 static const void *get_prop_check_min_len(const void *blob, int node, 384 const char *prop_name, int min_len, int *err) 385 { 386 const void *cell; 387 int len; 388 389 debug("%s: %s\n", __func__, prop_name); 390 cell = fdt_getprop(blob, node, prop_name, &len); 391 if (!cell) 392 *err = -FDT_ERR_NOTFOUND; 393 else if (len < min_len) 394 *err = -FDT_ERR_BADLAYOUT; 395 else 396 *err = 0; 397 return cell; 398 } 399 400 int fdtdec_get_int_array(const void *blob, int node, const char *prop_name, 401 u32 *array, int count) 402 { 403 const u32 *cell; 404 int i, err = 0; 405 406 debug("%s: %s\n", __func__, prop_name); 407 cell = get_prop_check_min_len(blob, node, prop_name, 408 sizeof(u32) * count, &err); 409 if (!err) { 410 for (i = 0; i < count; i++) 411 array[i] = fdt32_to_cpu(cell[i]); 412 } 413 return err; 414 } 415 416 const u32 *fdtdec_locate_array(const void *blob, int node, 417 const char *prop_name, int count) 418 { 419 const u32 *cell; 420 int err; 421 422 cell = get_prop_check_min_len(blob, node, prop_name, 423 sizeof(u32) * count, &err); 424 return err ? NULL : cell; 425 } 426 427 int fdtdec_get_bool(const void *blob, int node, const char *prop_name) 428 { 429 const s32 *cell; 430 int len; 431 432 debug("%s: %s\n", __func__, prop_name); 433 cell = fdt_getprop(blob, node, prop_name, &len); 434 return cell != NULL; 435 } 436 437 /** 438 * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no 439 * terminating item. 440 * 441 * @param blob FDT blob to use 442 * @param node Node to look at 443 * @param prop_name Node property name 444 * @param gpio Array of gpio elements to fill from FDT. This will be 445 * untouched if either 0 or an error is returned 446 * @param max_count Maximum number of elements allowed 447 * @return number of GPIOs read if ok, -FDT_ERR_BADLAYOUT if max_count would 448 * be exceeded, or -FDT_ERR_NOTFOUND if the property is missing. 449 */ 450 int fdtdec_decode_gpios(const void *blob, int node, const char *prop_name, 451 struct fdt_gpio_state *gpio, int max_count) 452 { 453 const struct fdt_property *prop; 454 const u32 *cell; 455 const char *name; 456 int len, i; 457 458 debug("%s: %s\n", __func__, prop_name); 459 assert(max_count > 0); 460 prop = fdt_get_property(blob, node, prop_name, &len); 461 if (!prop) { 462 debug("%s: property '%s' missing\n", __func__, prop_name); 463 return -FDT_ERR_NOTFOUND; 464 } 465 466 /* We will use the name to tag the GPIO */ 467 name = fdt_string(blob, fdt32_to_cpu(prop->nameoff)); 468 cell = (u32 *)prop->data; 469 len /= sizeof(u32) * 3; /* 3 cells per GPIO record */ 470 if (len > max_count) { 471 debug(" %s: too many GPIOs / cells for " 472 "property '%s'\n", __func__, prop_name); 473 return -FDT_ERR_BADLAYOUT; 474 } 475 476 /* Read out the GPIO data from the cells */ 477 for (i = 0; i < len; i++, cell += 3) { 478 gpio[i].gpio = fdt32_to_cpu(cell[1]); 479 gpio[i].flags = fdt32_to_cpu(cell[2]); 480 gpio[i].name = name; 481 } 482 483 return len; 484 } 485 486 int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name, 487 struct fdt_gpio_state *gpio) 488 { 489 int err; 490 491 debug("%s: %s\n", __func__, prop_name); 492 gpio->gpio = FDT_GPIO_NONE; 493 gpio->name = NULL; 494 err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1); 495 return err == 1 ? 0 : err; 496 } 497 498 int fdtdec_get_gpio(struct fdt_gpio_state *gpio) 499 { 500 int val; 501 502 if (!fdt_gpio_isvalid(gpio)) 503 return -1; 504 505 val = gpio_get_value(gpio->gpio); 506 return gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val; 507 } 508 509 int fdtdec_set_gpio(struct fdt_gpio_state *gpio, int val) 510 { 511 if (!fdt_gpio_isvalid(gpio)) 512 return -1; 513 514 val = gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val; 515 return gpio_set_value(gpio->gpio, val); 516 } 517 518 int fdtdec_setup_gpio(struct fdt_gpio_state *gpio) 519 { 520 /* 521 * Return success if there is no GPIO defined. This is used for 522 * optional GPIOs) 523 */ 524 if (!fdt_gpio_isvalid(gpio)) 525 return 0; 526 527 if (gpio_request(gpio->gpio, gpio->name)) 528 return -1; 529 return 0; 530 } 531 532 int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name, 533 u8 *array, int count) 534 { 535 const u8 *cell; 536 int err; 537 538 cell = get_prop_check_min_len(blob, node, prop_name, count, &err); 539 if (!err) 540 memcpy(array, cell, count); 541 return err; 542 } 543 544 const u8 *fdtdec_locate_byte_array(const void *blob, int node, 545 const char *prop_name, int count) 546 { 547 const u8 *cell; 548 int err; 549 550 cell = get_prop_check_min_len(blob, node, prop_name, count, &err); 551 if (err) 552 return NULL; 553 return cell; 554 } 555 556 int fdtdec_get_config_int(const void *blob, const char *prop_name, 557 int default_val) 558 { 559 int config_node; 560 561 debug("%s: %s\n", __func__, prop_name); 562 config_node = fdt_path_offset(blob, "/config"); 563 if (config_node < 0) 564 return default_val; 565 return fdtdec_get_int(blob, config_node, prop_name, default_val); 566 } 567 568 int fdtdec_get_config_bool(const void *blob, const char *prop_name) 569 { 570 int config_node; 571 const void *prop; 572 573 debug("%s: %s\n", __func__, prop_name); 574 config_node = fdt_path_offset(blob, "/config"); 575 if (config_node < 0) 576 return 0; 577 prop = fdt_get_property(blob, config_node, prop_name, NULL); 578 579 return prop != NULL; 580 } 581 582 char *fdtdec_get_config_string(const void *blob, const char *prop_name) 583 { 584 const char *nodep; 585 int nodeoffset; 586 int len; 587 588 debug("%s: %s\n", __func__, prop_name); 589 nodeoffset = fdt_path_offset(blob, "/config"); 590 if (nodeoffset < 0) 591 return NULL; 592 593 nodep = fdt_getprop(blob, nodeoffset, prop_name, &len); 594 if (!nodep) 595 return NULL; 596 597 return (char *)nodep; 598 } 599 600 int fdtdec_decode_region(const void *blob, int node, 601 const char *prop_name, void **ptrp, size_t *size) 602 { 603 const fdt_addr_t *cell; 604 int len; 605 606 debug("%s: %s\n", __func__, prop_name); 607 cell = fdt_getprop(blob, node, prop_name, &len); 608 if (!cell || (len != sizeof(fdt_addr_t) * 2)) 609 return -1; 610 611 *ptrp = (void *)fdt_addr_to_cpu(*cell); 612 *size = fdt_size_to_cpu(cell[1]); 613 debug("%s: size=%zx\n", __func__, *size); 614 return 0; 615 } 616