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