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