1b5220bc6SSimon Glass /* 2b5220bc6SSimon Glass * Copyright (c) 2011 The Chromium OS Authors. 3b5220bc6SSimon Glass * See file CREDITS for list of people who contributed to this 4b5220bc6SSimon Glass * project. 5b5220bc6SSimon Glass * 6b5220bc6SSimon Glass * This program is free software; you can redistribute it and/or 7b5220bc6SSimon Glass * modify it under the terms of the GNU General Public License as 8b5220bc6SSimon Glass * published by the Free Software Foundation; either version 2 of 9b5220bc6SSimon Glass * the License, or (at your option) any later version. 10b5220bc6SSimon Glass * 11b5220bc6SSimon Glass * This program is distributed in the hope that it will be useful, 12b5220bc6SSimon Glass * but WITHOUT ANY WARRANTY; without even the implied warranty of 13b5220bc6SSimon Glass * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14b5220bc6SSimon Glass * GNU General Public License for more details. 15b5220bc6SSimon Glass * 16b5220bc6SSimon Glass * You should have received a copy of the GNU General Public License 17b5220bc6SSimon Glass * along with this program; if not, write to the Free Software 18b5220bc6SSimon Glass * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 19b5220bc6SSimon Glass * MA 02111-1307 USA 20b5220bc6SSimon Glass */ 21b5220bc6SSimon Glass 22b5220bc6SSimon Glass #include <common.h> 23b5220bc6SSimon Glass #include <serial.h> 24b5220bc6SSimon Glass #include <libfdt.h> 25b5220bc6SSimon Glass #include <fdtdec.h> 26b5220bc6SSimon Glass 27b5220bc6SSimon Glass DECLARE_GLOBAL_DATA_PTR; 28b5220bc6SSimon Glass 29b5220bc6SSimon Glass /* 30b5220bc6SSimon Glass * Here are the type we know about. One day we might allow drivers to 31b5220bc6SSimon Glass * register. For now we just put them here. The COMPAT macro allows us to 32b5220bc6SSimon Glass * turn this into a sparse list later, and keeps the ID with the name. 33b5220bc6SSimon Glass */ 34b5220bc6SSimon Glass #define COMPAT(id, name) name 35b5220bc6SSimon Glass static const char * const compat_names[COMPAT_COUNT] = { 36b5220bc6SSimon Glass }; 37b5220bc6SSimon Glass 38*a53f4a29SSimon Glass const char *fdtdec_get_compatible(enum fdt_compat_id id) 39*a53f4a29SSimon Glass { 40*a53f4a29SSimon Glass /* We allow reading of the 'unknown' ID for testing purposes */ 41*a53f4a29SSimon Glass assert(id >= 0 && id < COMPAT_COUNT); 42*a53f4a29SSimon Glass return compat_names[id]; 43*a53f4a29SSimon Glass } 44*a53f4a29SSimon Glass 45b5220bc6SSimon Glass /** 46b5220bc6SSimon Glass * Look in the FDT for an alias with the given name and return its node. 47b5220bc6SSimon Glass * 48b5220bc6SSimon Glass * @param blob FDT blob 49b5220bc6SSimon Glass * @param name alias name to look up 50b5220bc6SSimon Glass * @return node offset if found, or an error code < 0 otherwise 51b5220bc6SSimon Glass */ 52b5220bc6SSimon Glass static int find_alias_node(const void *blob, const char *name) 53b5220bc6SSimon Glass { 54b5220bc6SSimon Glass const char *path; 55b5220bc6SSimon Glass int alias_node; 56b5220bc6SSimon Glass 57b5220bc6SSimon Glass debug("find_alias_node: %s\n", name); 58b5220bc6SSimon Glass alias_node = fdt_path_offset(blob, "/aliases"); 59b5220bc6SSimon Glass if (alias_node < 0) 60b5220bc6SSimon Glass return alias_node; 61b5220bc6SSimon Glass path = fdt_getprop(blob, alias_node, name, NULL); 62b5220bc6SSimon Glass if (!path) 63b5220bc6SSimon Glass return -FDT_ERR_NOTFOUND; 64b5220bc6SSimon Glass return fdt_path_offset(blob, path); 65b5220bc6SSimon Glass } 66b5220bc6SSimon Glass 67b5220bc6SSimon Glass fdt_addr_t fdtdec_get_addr(const void *blob, int node, 68b5220bc6SSimon Glass const char *prop_name) 69b5220bc6SSimon Glass { 70b5220bc6SSimon Glass const fdt_addr_t *cell; 71b5220bc6SSimon Glass int len; 72b5220bc6SSimon Glass 73b5220bc6SSimon Glass debug("get_addr: %s\n", prop_name); 74b5220bc6SSimon Glass cell = fdt_getprop(blob, node, prop_name, &len); 75b5220bc6SSimon Glass if (cell && (len == sizeof(fdt_addr_t) || 76b5220bc6SSimon Glass len == sizeof(fdt_addr_t) * 2)) 77b5220bc6SSimon Glass return fdt_addr_to_cpu(*cell); 78b5220bc6SSimon Glass return FDT_ADDR_T_NONE; 79b5220bc6SSimon Glass } 80b5220bc6SSimon Glass 81b5220bc6SSimon Glass s32 fdtdec_get_int(const void *blob, int node, const char *prop_name, 82b5220bc6SSimon Glass s32 default_val) 83b5220bc6SSimon Glass { 84b5220bc6SSimon Glass const s32 *cell; 85b5220bc6SSimon Glass int len; 86b5220bc6SSimon Glass 87b5220bc6SSimon Glass debug("get_size: %s\n", prop_name); 88b5220bc6SSimon Glass cell = fdt_getprop(blob, node, prop_name, &len); 89b5220bc6SSimon Glass if (cell && len >= sizeof(s32)) 90b5220bc6SSimon Glass return fdt32_to_cpu(cell[0]); 91b5220bc6SSimon Glass return default_val; 92b5220bc6SSimon Glass } 93b5220bc6SSimon Glass 94b5220bc6SSimon Glass int fdtdec_get_is_enabled(const void *blob, int node, int default_val) 95b5220bc6SSimon Glass { 96b5220bc6SSimon Glass const char *cell; 97b5220bc6SSimon Glass 98b5220bc6SSimon Glass cell = fdt_getprop(blob, node, "status", NULL); 99b5220bc6SSimon Glass if (cell) 100b5220bc6SSimon Glass return 0 == strcmp(cell, "ok"); 101b5220bc6SSimon Glass return default_val; 102b5220bc6SSimon Glass } 103b5220bc6SSimon Glass 104b5220bc6SSimon Glass enum fdt_compat_id fd_dec_lookup(const void *blob, int node) 105b5220bc6SSimon Glass { 106b5220bc6SSimon Glass enum fdt_compat_id id; 107b5220bc6SSimon Glass 108b5220bc6SSimon Glass /* Search our drivers */ 109b5220bc6SSimon Glass for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++) 110b5220bc6SSimon Glass if (0 == fdt_node_check_compatible(blob, node, 111b5220bc6SSimon Glass compat_names[id])) 112b5220bc6SSimon Glass return id; 113b5220bc6SSimon Glass return COMPAT_UNKNOWN; 114b5220bc6SSimon Glass } 115b5220bc6SSimon Glass 116b5220bc6SSimon Glass int fdtdec_next_compatible(const void *blob, int node, 117b5220bc6SSimon Glass enum fdt_compat_id id) 118b5220bc6SSimon Glass { 119b5220bc6SSimon Glass return fdt_node_offset_by_compatible(blob, node, compat_names[id]); 120b5220bc6SSimon Glass } 121b5220bc6SSimon Glass 122b5220bc6SSimon Glass int fdtdec_next_alias(const void *blob, const char *name, 123b5220bc6SSimon Glass enum fdt_compat_id id, int *upto) 124b5220bc6SSimon Glass { 125b5220bc6SSimon Glass #define MAX_STR_LEN 20 126b5220bc6SSimon Glass char str[MAX_STR_LEN + 20]; 127b5220bc6SSimon Glass int node, err; 128b5220bc6SSimon Glass 129b5220bc6SSimon Glass /* snprintf() is not available */ 130b5220bc6SSimon Glass assert(strlen(name) < MAX_STR_LEN); 131b5220bc6SSimon Glass sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto); 132b5220bc6SSimon Glass (*upto)++; 133b5220bc6SSimon Glass node = find_alias_node(blob, str); 134b5220bc6SSimon Glass if (node < 0) 135b5220bc6SSimon Glass return node; 136b5220bc6SSimon Glass err = fdt_node_check_compatible(blob, node, compat_names[id]); 137b5220bc6SSimon Glass if (err < 0) 138b5220bc6SSimon Glass return err; 139b5220bc6SSimon Glass return err ? -FDT_ERR_NOTFOUND : node; 140b5220bc6SSimon Glass } 141b5220bc6SSimon Glass 142*a53f4a29SSimon Glass /* TODO: Can we tighten this code up a little? */ 143*a53f4a29SSimon Glass int fdtdec_find_aliases_for_id(const void *blob, const char *name, 144*a53f4a29SSimon Glass enum fdt_compat_id id, int *node_list, int maxcount) 145*a53f4a29SSimon Glass { 146*a53f4a29SSimon Glass int name_len = strlen(name); 147*a53f4a29SSimon Glass int nodes[maxcount]; 148*a53f4a29SSimon Glass int num_found = 0; 149*a53f4a29SSimon Glass int offset, node; 150*a53f4a29SSimon Glass int alias_node; 151*a53f4a29SSimon Glass int count; 152*a53f4a29SSimon Glass int i, j; 153*a53f4a29SSimon Glass 154*a53f4a29SSimon Glass /* find the alias node if present */ 155*a53f4a29SSimon Glass alias_node = fdt_path_offset(blob, "/aliases"); 156*a53f4a29SSimon Glass 157*a53f4a29SSimon Glass /* 158*a53f4a29SSimon Glass * start with nothing, and we can assume that the root node can't 159*a53f4a29SSimon Glass * match 160*a53f4a29SSimon Glass */ 161*a53f4a29SSimon Glass memset(nodes, '\0', sizeof(nodes)); 162*a53f4a29SSimon Glass 163*a53f4a29SSimon Glass /* First find all the compatible nodes */ 164*a53f4a29SSimon Glass for (node = count = 0; node >= 0 && count < maxcount;) { 165*a53f4a29SSimon Glass node = fdtdec_next_compatible(blob, node, id); 166*a53f4a29SSimon Glass if (node >= 0) 167*a53f4a29SSimon Glass nodes[count++] = node; 168*a53f4a29SSimon Glass } 169*a53f4a29SSimon Glass if (node >= 0) 170*a53f4a29SSimon Glass debug("%s: warning: maxcount exceeded with alias '%s'\n", 171*a53f4a29SSimon Glass __func__, name); 172*a53f4a29SSimon Glass 173*a53f4a29SSimon Glass /* Now find all the aliases */ 174*a53f4a29SSimon Glass memset(node_list, '\0', sizeof(*node_list) * maxcount); 175*a53f4a29SSimon Glass 176*a53f4a29SSimon Glass for (offset = fdt_first_property_offset(blob, alias_node); 177*a53f4a29SSimon Glass offset > 0; 178*a53f4a29SSimon Glass offset = fdt_next_property_offset(blob, offset)) { 179*a53f4a29SSimon Glass const struct fdt_property *prop; 180*a53f4a29SSimon Glass const char *path; 181*a53f4a29SSimon Glass int number; 182*a53f4a29SSimon Glass int found; 183*a53f4a29SSimon Glass 184*a53f4a29SSimon Glass node = 0; 185*a53f4a29SSimon Glass prop = fdt_get_property_by_offset(blob, offset, NULL); 186*a53f4a29SSimon Glass path = fdt_string(blob, fdt32_to_cpu(prop->nameoff)); 187*a53f4a29SSimon Glass if (prop->len && 0 == strncmp(path, name, name_len)) 188*a53f4a29SSimon Glass node = fdt_path_offset(blob, prop->data); 189*a53f4a29SSimon Glass if (node <= 0) 190*a53f4a29SSimon Glass continue; 191*a53f4a29SSimon Glass 192*a53f4a29SSimon Glass /* Get the alias number */ 193*a53f4a29SSimon Glass number = simple_strtoul(path + name_len, NULL, 10); 194*a53f4a29SSimon Glass if (number < 0 || number >= maxcount) { 195*a53f4a29SSimon Glass debug("%s: warning: alias '%s' is out of range\n", 196*a53f4a29SSimon Glass __func__, path); 197*a53f4a29SSimon Glass continue; 198*a53f4a29SSimon Glass } 199*a53f4a29SSimon Glass 200*a53f4a29SSimon Glass /* Make sure the node we found is actually in our list! */ 201*a53f4a29SSimon Glass found = -1; 202*a53f4a29SSimon Glass for (j = 0; j < count; j++) 203*a53f4a29SSimon Glass if (nodes[j] == node) { 204*a53f4a29SSimon Glass found = j; 205*a53f4a29SSimon Glass break; 206*a53f4a29SSimon Glass } 207*a53f4a29SSimon Glass 208*a53f4a29SSimon Glass if (found == -1) { 209*a53f4a29SSimon Glass debug("%s: warning: alias '%s' points to a node " 210*a53f4a29SSimon Glass "'%s' that is missing or is not compatible " 211*a53f4a29SSimon Glass " with '%s'\n", __func__, path, 212*a53f4a29SSimon Glass fdt_get_name(blob, node, NULL), 213*a53f4a29SSimon Glass compat_names[id]); 214*a53f4a29SSimon Glass continue; 215*a53f4a29SSimon Glass } 216*a53f4a29SSimon Glass 217*a53f4a29SSimon Glass /* 218*a53f4a29SSimon Glass * Add this node to our list in the right place, and mark 219*a53f4a29SSimon Glass * it as done. 220*a53f4a29SSimon Glass */ 221*a53f4a29SSimon Glass if (fdtdec_get_is_enabled(blob, node)) { 222*a53f4a29SSimon Glass node_list[number] = node; 223*a53f4a29SSimon Glass if (number >= num_found) 224*a53f4a29SSimon Glass num_found = number + 1; 225*a53f4a29SSimon Glass } 226*a53f4a29SSimon Glass nodes[j] = 0; 227*a53f4a29SSimon Glass } 228*a53f4a29SSimon Glass 229*a53f4a29SSimon Glass /* Add any nodes not mentioned by an alias */ 230*a53f4a29SSimon Glass for (i = j = 0; i < maxcount; i++) { 231*a53f4a29SSimon Glass if (!node_list[i]) { 232*a53f4a29SSimon Glass for (; j < maxcount; j++) 233*a53f4a29SSimon Glass if (nodes[j] && 234*a53f4a29SSimon Glass fdtdec_get_is_enabled(blob, nodes[j])) 235*a53f4a29SSimon Glass break; 236*a53f4a29SSimon Glass 237*a53f4a29SSimon Glass /* Have we run out of nodes to add? */ 238*a53f4a29SSimon Glass if (j == maxcount) 239*a53f4a29SSimon Glass break; 240*a53f4a29SSimon Glass 241*a53f4a29SSimon Glass assert(!node_list[i]); 242*a53f4a29SSimon Glass node_list[i] = nodes[j++]; 243*a53f4a29SSimon Glass if (i >= num_found) 244*a53f4a29SSimon Glass num_found = i + 1; 245*a53f4a29SSimon Glass } 246*a53f4a29SSimon Glass } 247*a53f4a29SSimon Glass 248*a53f4a29SSimon Glass return num_found; 249*a53f4a29SSimon Glass } 250*a53f4a29SSimon Glass 251b5220bc6SSimon Glass /* 252b5220bc6SSimon Glass * This function is a little odd in that it accesses global data. At some 253b5220bc6SSimon Glass * point if the architecture board.c files merge this will make more sense. 254b5220bc6SSimon Glass * Even now, it is common code. 255b5220bc6SSimon Glass */ 256b5220bc6SSimon Glass int fdtdec_check_fdt(void) 257b5220bc6SSimon Glass { 258b5220bc6SSimon Glass /* We must have an fdt */ 259b5220bc6SSimon Glass if (fdt_check_header(gd->fdt_blob)) 260b5220bc6SSimon Glass panic("No valid fdt found - please append one to U-Boot\n" 261b5220bc6SSimon Glass "binary or define CONFIG_OF_EMBED\n"); 262b5220bc6SSimon Glass return 0; 263b5220bc6SSimon Glass } 264