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