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