Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 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 | |
Michal Simek | bcd35ce | 2012-07-11 02:26:38 +0000 | [diff] [blame] | 27 | #include <asm/gpio.h> |
Simon Glass | 0d83c4d | 2012-02-27 10:52:36 +0000 | [diff] [blame] | 28 | |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 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] = { |
Simon Glass | 6544498 | 2012-02-27 10:52:34 +0000 | [diff] [blame] | 38 | COMPAT(UNKNOWN, "<none>"), |
Simon Glass | 08d6ec2 | 2012-02-27 10:52:49 +0000 | [diff] [blame] | 39 | COMPAT(NVIDIA_TEGRA20_USB, "nvidia,tegra20-ehci"), |
Jim Lin | 2fefb8b | 2013-06-21 19:05:47 +0800 | [diff] [blame] | 40 | COMPAT(NVIDIA_TEGRA30_USB, "nvidia,tegra30-ehci"), |
| 41 | COMPAT(NVIDIA_TEGRA114_USB, "nvidia,tegra114-ehci"), |
Tom Warren | f8cf4b2 | 2013-02-08 07:25:30 +0000 | [diff] [blame] | 42 | COMPAT(NVIDIA_TEGRA114_I2C, "nvidia,tegra114-i2c"), |
Yen Lin | ccf7e90 | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 43 | COMPAT(NVIDIA_TEGRA20_I2C, "nvidia,tegra20-i2c"), |
| 44 | COMPAT(NVIDIA_TEGRA20_DVC, "nvidia,tegra20-i2c-dvc"), |
Jimmy Zhang | 5983ef8 | 2012-04-02 13:18:52 +0000 | [diff] [blame] | 45 | COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"), |
| 46 | COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"), |
Rakesh Iyer | 382abbd | 2012-04-17 09:01:35 +0000 | [diff] [blame] | 47 | COMPAT(NVIDIA_TEGRA20_KBC, "nvidia,tegra20-kbc"), |
Jim Lin | 5d309e6 | 2012-07-29 20:53:29 +0000 | [diff] [blame] | 48 | COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"), |
Simon Glass | 1564f34 | 2012-10-17 13:24:49 +0000 | [diff] [blame] | 49 | COMPAT(NVIDIA_TEGRA20_PWM, "nvidia,tegra20-pwm"), |
Wei Ni | 6555e69 | 2012-10-17 13:24:50 +0000 | [diff] [blame] | 50 | COMPAT(NVIDIA_TEGRA20_DC, "nvidia,tegra20-dc"), |
Tom Warren | 7db0e2d | 2013-03-04 14:07:18 -0700 | [diff] [blame] | 51 | COMPAT(NVIDIA_TEGRA30_SDMMC, "nvidia,tegra30-sdhci"), |
Tom Warren | 9745cf8 | 2013-02-21 12:31:30 +0000 | [diff] [blame] | 52 | COMPAT(NVIDIA_TEGRA20_SDMMC, "nvidia,tegra20-sdhci"), |
Allen Martin | e765952 | 2013-01-29 13:51:24 +0000 | [diff] [blame] | 53 | COMPAT(NVIDIA_TEGRA20_SFLASH, "nvidia,tegra20-sflash"), |
Allen Martin | ba4fb9b | 2013-01-29 13:51:28 +0000 | [diff] [blame] | 54 | COMPAT(NVIDIA_TEGRA20_SLINK, "nvidia,tegra20-slink"), |
Allen Martin | b54f0d9 | 2013-03-16 18:58:09 +0000 | [diff] [blame] | 55 | COMPAT(NVIDIA_TEGRA114_SPI, "nvidia,tegra114-spi"), |
Hatim RV | be5ad5a | 2012-12-11 00:52:46 +0000 | [diff] [blame] | 56 | COMPAT(SMSC_LAN9215, "smsc,lan9215"), |
| 57 | COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"), |
Rajeshwari Shinde | 2b71e9d | 2012-12-26 20:03:10 +0000 | [diff] [blame] | 58 | COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"), |
Rajeshwari Shinde | 9d11673 | 2012-12-26 20:03:16 +0000 | [diff] [blame] | 59 | COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"), |
| 60 | COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"), |
Rajeshwari Shinde | d95beaf | 2012-12-26 20:03:20 +0000 | [diff] [blame] | 61 | COMPAT(SAMSUNG_EXYNOS_SPI, "samsung,exynos-spi"), |
Hung-ying Tyan | c48ca88f | 2013-05-15 18:27:28 +0800 | [diff] [blame] | 62 | COMPAT(GOOGLE_CROS_EC, "google,cros-ec"), |
Hung-ying Tyan | 4a48bcf | 2013-05-15 18:27:32 +0800 | [diff] [blame] | 63 | COMPAT(GOOGLE_CROS_EC_KEYB, "google,cros-ec-keyb"), |
Rajeshwari Shinde | d6e9f44 | 2013-01-07 23:35:05 +0000 | [diff] [blame] | 64 | COMPAT(SAMSUNG_EXYNOS_EHCI, "samsung,exynos-ehci"), |
| 65 | COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"), |
Akshay Saraswat | aa77318 | 2013-02-25 01:13:01 +0000 | [diff] [blame] | 66 | COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"), |
Ajay Kumar | 035a1cd | 2013-02-21 23:53:00 +0000 | [diff] [blame] | 67 | COMPAT(SAMSUNG_EXYNOS_FIMD, "samsung,exynos-fimd"), |
Ajay Kumar | 39bac15 | 2013-02-21 23:53:05 +0000 | [diff] [blame] | 68 | COMPAT(SAMSUNG_EXYNOS5_DP, "samsung,exynos5-dp"), |
Amar | 4b538c5 | 2013-04-27 11:42:52 +0530 | [diff] [blame] | 69 | COMPAT(SAMSUNG_EXYNOS5_DWMMC, "samsung,exynos5250-dwmmc"), |
Rajeshwari Shinde | ffff161 | 2013-06-24 16:47:20 +0530 | [diff] [blame] | 70 | COMPAT(SAMSUNG_EXYNOS_SERIAL, "samsung,exynos4210-uart"), |
Rajeshwari Shinde | 1e9eeb0 | 2013-01-08 21:03:38 +0000 | [diff] [blame] | 71 | COMPAT(MAXIM_MAX77686_PMIC, "maxim,max77686_pmic"), |
Simon Glass | 60956094 | 2013-03-11 06:08:08 +0000 | [diff] [blame] | 72 | COMPAT(GENERIC_SPI_FLASH, "spi-flash"), |
Rajeshwari Shinde | 003c28c | 2013-02-14 19:46:15 +0000 | [diff] [blame] | 73 | COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"), |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 74 | COMPAT(INFINEON_SLB9635_TPM, "infineon,slb9635-tpm"), |
Vincent Palatin | 06883eb | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 75 | COMPAT(INFINEON_SLB9645_TPM, "infineon,slb9645-tpm"), |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
Simon Glass | b022ead | 2012-01-17 08:20:50 +0000 | [diff] [blame] | 78 | const char *fdtdec_get_compatible(enum fdt_compat_id id) |
| 79 | { |
| 80 | /* We allow reading of the 'unknown' ID for testing purposes */ |
| 81 | assert(id >= 0 && id < COMPAT_COUNT); |
| 82 | return compat_names[id]; |
| 83 | } |
| 84 | |
Simon Glass | f429955 | 2013-03-19 04:58:51 +0000 | [diff] [blame] | 85 | fdt_addr_t fdtdec_get_addr_size(const void *blob, int node, |
| 86 | const char *prop_name, fdt_size_t *sizep) |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 87 | { |
| 88 | const fdt_addr_t *cell; |
| 89 | int len; |
| 90 | |
Simon Glass | c8c86dd | 2012-07-12 05:25:01 +0000 | [diff] [blame] | 91 | debug("%s: %s: ", __func__, prop_name); |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 92 | cell = fdt_getprop(blob, node, prop_name, &len); |
Simon Glass | f429955 | 2013-03-19 04:58:51 +0000 | [diff] [blame] | 93 | if (cell && ((!sizep && len == sizeof(fdt_addr_t)) || |
| 94 | len == sizeof(fdt_addr_t) * 2)) { |
Simon Glass | c8c86dd | 2012-07-12 05:25:01 +0000 | [diff] [blame] | 95 | fdt_addr_t addr = fdt_addr_to_cpu(*cell); |
Simon Glass | f429955 | 2013-03-19 04:58:51 +0000 | [diff] [blame] | 96 | if (sizep) { |
| 97 | const fdt_size_t *size; |
Simon Glass | c8c86dd | 2012-07-12 05:25:01 +0000 | [diff] [blame] | 98 | |
Simon Glass | f429955 | 2013-03-19 04:58:51 +0000 | [diff] [blame] | 99 | size = (fdt_size_t *)((char *)cell + |
| 100 | sizeof(fdt_addr_t)); |
| 101 | *sizep = fdt_size_to_cpu(*size); |
| 102 | debug("addr=%p, size=%p\n", (void *)addr, |
| 103 | (void *)*sizep); |
| 104 | } else { |
| 105 | debug("%p\n", (void *)addr); |
| 106 | } |
Simon Glass | c8c86dd | 2012-07-12 05:25:01 +0000 | [diff] [blame] | 107 | return addr; |
| 108 | } |
| 109 | debug("(not found)\n"); |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 110 | return FDT_ADDR_T_NONE; |
| 111 | } |
| 112 | |
Simon Glass | f429955 | 2013-03-19 04:58:51 +0000 | [diff] [blame] | 113 | fdt_addr_t fdtdec_get_addr(const void *blob, int node, |
| 114 | const char *prop_name) |
| 115 | { |
| 116 | return fdtdec_get_addr_size(blob, node, prop_name, NULL); |
| 117 | } |
| 118 | |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 119 | s32 fdtdec_get_int(const void *blob, int node, const char *prop_name, |
| 120 | s32 default_val) |
| 121 | { |
| 122 | const s32 *cell; |
| 123 | int len; |
| 124 | |
Simon Glass | c8c86dd | 2012-07-12 05:25:01 +0000 | [diff] [blame] | 125 | debug("%s: %s: ", __func__, prop_name); |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 126 | cell = fdt_getprop(blob, node, prop_name, &len); |
Simon Glass | c8c86dd | 2012-07-12 05:25:01 +0000 | [diff] [blame] | 127 | if (cell && len >= sizeof(s32)) { |
| 128 | s32 val = fdt32_to_cpu(cell[0]); |
| 129 | |
| 130 | debug("%#x (%d)\n", val, val); |
| 131 | return val; |
| 132 | } |
| 133 | debug("(not found)\n"); |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 134 | return default_val; |
| 135 | } |
| 136 | |
Che-Liang Chiou | d81d017 | 2012-10-25 16:31:05 +0000 | [diff] [blame] | 137 | uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name, |
| 138 | uint64_t default_val) |
| 139 | { |
| 140 | const uint64_t *cell64; |
| 141 | int length; |
| 142 | |
| 143 | cell64 = fdt_getprop(blob, node, prop_name, &length); |
| 144 | if (!cell64 || length < sizeof(*cell64)) |
| 145 | return default_val; |
| 146 | |
| 147 | return fdt64_to_cpu(*cell64); |
| 148 | } |
| 149 | |
Simon Glass | 6544498 | 2012-02-27 10:52:34 +0000 | [diff] [blame] | 150 | int fdtdec_get_is_enabled(const void *blob, int node) |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 151 | { |
| 152 | const char *cell; |
| 153 | |
Simon Glass | 6544498 | 2012-02-27 10:52:34 +0000 | [diff] [blame] | 154 | /* |
| 155 | * It should say "okay", so only allow that. Some fdts use "ok" but |
| 156 | * this is a bug. Please fix your device tree source file. See here |
| 157 | * for discussion: |
| 158 | * |
| 159 | * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html |
| 160 | */ |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 161 | cell = fdt_getprop(blob, node, "status", NULL); |
| 162 | if (cell) |
Simon Glass | 6544498 | 2012-02-27 10:52:34 +0000 | [diff] [blame] | 163 | return 0 == strcmp(cell, "okay"); |
| 164 | return 1; |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Gerald Van Baren | cdac339 | 2012-11-12 23:13:54 -0500 | [diff] [blame] | 167 | enum fdt_compat_id fdtdec_lookup(const void *blob, int node) |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 168 | { |
| 169 | enum fdt_compat_id id; |
| 170 | |
| 171 | /* Search our drivers */ |
| 172 | for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++) |
| 173 | if (0 == fdt_node_check_compatible(blob, node, |
| 174 | compat_names[id])) |
| 175 | return id; |
| 176 | return COMPAT_UNKNOWN; |
| 177 | } |
| 178 | |
| 179 | int fdtdec_next_compatible(const void *blob, int node, |
| 180 | enum fdt_compat_id id) |
| 181 | { |
| 182 | return fdt_node_offset_by_compatible(blob, node, compat_names[id]); |
| 183 | } |
| 184 | |
Simon Glass | 2e979b1 | 2012-04-02 13:18:42 +0000 | [diff] [blame] | 185 | int fdtdec_next_compatible_subnode(const void *blob, int node, |
| 186 | enum fdt_compat_id id, int *depthp) |
| 187 | { |
| 188 | do { |
| 189 | node = fdt_next_node(blob, node, depthp); |
| 190 | } while (*depthp > 1); |
| 191 | |
| 192 | /* If this is a direct subnode, and compatible, return it */ |
| 193 | if (*depthp == 1 && 0 == fdt_node_check_compatible( |
| 194 | blob, node, compat_names[id])) |
| 195 | return node; |
| 196 | |
| 197 | return -FDT_ERR_NOTFOUND; |
| 198 | } |
| 199 | |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 200 | int fdtdec_next_alias(const void *blob, const char *name, |
| 201 | enum fdt_compat_id id, int *upto) |
| 202 | { |
| 203 | #define MAX_STR_LEN 20 |
| 204 | char str[MAX_STR_LEN + 20]; |
| 205 | int node, err; |
| 206 | |
| 207 | /* snprintf() is not available */ |
| 208 | assert(strlen(name) < MAX_STR_LEN); |
| 209 | sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto); |
Simon Glass | e1d15ca | 2012-10-31 14:02:42 +0000 | [diff] [blame] | 210 | node = fdt_path_offset(blob, str); |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 211 | if (node < 0) |
| 212 | return node; |
| 213 | err = fdt_node_check_compatible(blob, node, compat_names[id]); |
| 214 | if (err < 0) |
| 215 | return err; |
Simon Glass | 6544498 | 2012-02-27 10:52:34 +0000 | [diff] [blame] | 216 | if (err) |
| 217 | return -FDT_ERR_NOTFOUND; |
| 218 | (*upto)++; |
| 219 | return node; |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Simon Glass | b022ead | 2012-01-17 08:20:50 +0000 | [diff] [blame] | 222 | int fdtdec_find_aliases_for_id(const void *blob, const char *name, |
| 223 | enum fdt_compat_id id, int *node_list, int maxcount) |
| 224 | { |
Simon Glass | 1f77266 | 2012-02-03 15:13:53 +0000 | [diff] [blame] | 225 | memset(node_list, '\0', sizeof(*node_list) * maxcount); |
| 226 | |
| 227 | return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount); |
| 228 | } |
| 229 | |
| 230 | /* TODO: Can we tighten this code up a little? */ |
| 231 | int fdtdec_add_aliases_for_id(const void *blob, const char *name, |
| 232 | enum fdt_compat_id id, int *node_list, int maxcount) |
| 233 | { |
Simon Glass | b022ead | 2012-01-17 08:20:50 +0000 | [diff] [blame] | 234 | int name_len = strlen(name); |
| 235 | int nodes[maxcount]; |
| 236 | int num_found = 0; |
| 237 | int offset, node; |
| 238 | int alias_node; |
| 239 | int count; |
| 240 | int i, j; |
| 241 | |
| 242 | /* find the alias node if present */ |
| 243 | alias_node = fdt_path_offset(blob, "/aliases"); |
| 244 | |
| 245 | /* |
| 246 | * start with nothing, and we can assume that the root node can't |
| 247 | * match |
| 248 | */ |
| 249 | memset(nodes, '\0', sizeof(nodes)); |
| 250 | |
| 251 | /* First find all the compatible nodes */ |
| 252 | for (node = count = 0; node >= 0 && count < maxcount;) { |
| 253 | node = fdtdec_next_compatible(blob, node, id); |
| 254 | if (node >= 0) |
| 255 | nodes[count++] = node; |
| 256 | } |
| 257 | if (node >= 0) |
| 258 | debug("%s: warning: maxcount exceeded with alias '%s'\n", |
| 259 | __func__, name); |
| 260 | |
| 261 | /* Now find all the aliases */ |
Simon Glass | b022ead | 2012-01-17 08:20:50 +0000 | [diff] [blame] | 262 | for (offset = fdt_first_property_offset(blob, alias_node); |
| 263 | offset > 0; |
| 264 | offset = fdt_next_property_offset(blob, offset)) { |
| 265 | const struct fdt_property *prop; |
| 266 | const char *path; |
| 267 | int number; |
| 268 | int found; |
| 269 | |
| 270 | node = 0; |
| 271 | prop = fdt_get_property_by_offset(blob, offset, NULL); |
| 272 | path = fdt_string(blob, fdt32_to_cpu(prop->nameoff)); |
| 273 | if (prop->len && 0 == strncmp(path, name, name_len)) |
| 274 | node = fdt_path_offset(blob, prop->data); |
| 275 | if (node <= 0) |
| 276 | continue; |
| 277 | |
| 278 | /* Get the alias number */ |
| 279 | number = simple_strtoul(path + name_len, NULL, 10); |
| 280 | if (number < 0 || number >= maxcount) { |
| 281 | debug("%s: warning: alias '%s' is out of range\n", |
| 282 | __func__, path); |
| 283 | continue; |
| 284 | } |
| 285 | |
| 286 | /* Make sure the node we found is actually in our list! */ |
| 287 | found = -1; |
| 288 | for (j = 0; j < count; j++) |
| 289 | if (nodes[j] == node) { |
| 290 | found = j; |
| 291 | break; |
| 292 | } |
| 293 | |
| 294 | if (found == -1) { |
| 295 | debug("%s: warning: alias '%s' points to a node " |
| 296 | "'%s' that is missing or is not compatible " |
| 297 | " with '%s'\n", __func__, path, |
| 298 | fdt_get_name(blob, node, NULL), |
| 299 | compat_names[id]); |
| 300 | continue; |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | * Add this node to our list in the right place, and mark |
| 305 | * it as done. |
| 306 | */ |
| 307 | if (fdtdec_get_is_enabled(blob, node)) { |
Simon Glass | 1f77266 | 2012-02-03 15:13:53 +0000 | [diff] [blame] | 308 | if (node_list[number]) { |
| 309 | debug("%s: warning: alias '%s' requires that " |
| 310 | "a node be placed in the list in a " |
| 311 | "position which is already filled by " |
| 312 | "node '%s'\n", __func__, path, |
| 313 | fdt_get_name(blob, node, NULL)); |
| 314 | continue; |
| 315 | } |
Simon Glass | b022ead | 2012-01-17 08:20:50 +0000 | [diff] [blame] | 316 | node_list[number] = node; |
| 317 | if (number >= num_found) |
| 318 | num_found = number + 1; |
| 319 | } |
Simon Glass | 1f77266 | 2012-02-03 15:13:53 +0000 | [diff] [blame] | 320 | nodes[found] = 0; |
Simon Glass | b022ead | 2012-01-17 08:20:50 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /* Add any nodes not mentioned by an alias */ |
| 324 | for (i = j = 0; i < maxcount; i++) { |
| 325 | if (!node_list[i]) { |
| 326 | for (; j < maxcount; j++) |
| 327 | if (nodes[j] && |
| 328 | fdtdec_get_is_enabled(blob, nodes[j])) |
| 329 | break; |
| 330 | |
| 331 | /* Have we run out of nodes to add? */ |
| 332 | if (j == maxcount) |
| 333 | break; |
| 334 | |
| 335 | assert(!node_list[i]); |
| 336 | node_list[i] = nodes[j++]; |
| 337 | if (i >= num_found) |
| 338 | num_found = i + 1; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | return num_found; |
| 343 | } |
| 344 | |
Simon Glass | 1691f69 | 2012-03-28 10:08:24 +0000 | [diff] [blame] | 345 | int fdtdec_check_fdt(void) |
| 346 | { |
| 347 | /* |
| 348 | * We must have an FDT, but we cannot panic() yet since the console |
| 349 | * is not ready. So for now, just assert(). Boards which need an early |
| 350 | * FDT (prior to console ready) will need to make their own |
| 351 | * arrangements and do their own checks. |
| 352 | */ |
| 353 | assert(!fdtdec_prepare_fdt()); |
| 354 | return 0; |
| 355 | } |
| 356 | |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 357 | /* |
| 358 | * This function is a little odd in that it accesses global data. At some |
| 359 | * point if the architecture board.c files merge this will make more sense. |
| 360 | * Even now, it is common code. |
| 361 | */ |
Simon Glass | 1691f69 | 2012-03-28 10:08:24 +0000 | [diff] [blame] | 362 | int fdtdec_prepare_fdt(void) |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 363 | { |
Simon Glass | 7113685 | 2013-04-20 08:42:46 +0000 | [diff] [blame] | 364 | if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) || |
| 365 | fdt_check_header(gd->fdt_blob)) { |
Simon Glass | 1691f69 | 2012-03-28 10:08:24 +0000 | [diff] [blame] | 366 | printf("No valid FDT found - please append one to U-Boot " |
| 367 | "binary, use u-boot-dtb.bin or define " |
Simon Glass | 62cf912 | 2013-04-26 02:53:43 +0000 | [diff] [blame] | 368 | "CONFIG_OF_EMBED. For sandbox, use -d <file.dtb>\n"); |
Simon Glass | 1691f69 | 2012-03-28 10:08:24 +0000 | [diff] [blame] | 369 | return -1; |
| 370 | } |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 371 | return 0; |
| 372 | } |
Simon Glass | c469736 | 2012-02-27 10:52:35 +0000 | [diff] [blame] | 373 | |
| 374 | int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name) |
| 375 | { |
| 376 | const u32 *phandle; |
| 377 | int lookup; |
| 378 | |
Simon Glass | c8c86dd | 2012-07-12 05:25:01 +0000 | [diff] [blame] | 379 | debug("%s: %s\n", __func__, prop_name); |
Simon Glass | c469736 | 2012-02-27 10:52:35 +0000 | [diff] [blame] | 380 | phandle = fdt_getprop(blob, node, prop_name, NULL); |
| 381 | if (!phandle) |
| 382 | return -FDT_ERR_NOTFOUND; |
| 383 | |
| 384 | lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle)); |
| 385 | return lookup; |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Look up a property in a node and check that it has a minimum length. |
| 390 | * |
| 391 | * @param blob FDT blob |
| 392 | * @param node node to examine |
| 393 | * @param prop_name name of property to find |
| 394 | * @param min_len minimum property length in bytes |
| 395 | * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not |
| 396 | found, or -FDT_ERR_BADLAYOUT if not enough data |
| 397 | * @return pointer to cell, which is only valid if err == 0 |
| 398 | */ |
| 399 | static const void *get_prop_check_min_len(const void *blob, int node, |
| 400 | const char *prop_name, int min_len, int *err) |
| 401 | { |
| 402 | const void *cell; |
| 403 | int len; |
| 404 | |
| 405 | debug("%s: %s\n", __func__, prop_name); |
| 406 | cell = fdt_getprop(blob, node, prop_name, &len); |
| 407 | if (!cell) |
| 408 | *err = -FDT_ERR_NOTFOUND; |
| 409 | else if (len < min_len) |
| 410 | *err = -FDT_ERR_BADLAYOUT; |
| 411 | else |
| 412 | *err = 0; |
| 413 | return cell; |
| 414 | } |
| 415 | |
| 416 | int fdtdec_get_int_array(const void *blob, int node, const char *prop_name, |
| 417 | u32 *array, int count) |
| 418 | { |
| 419 | const u32 *cell; |
| 420 | int i, err = 0; |
| 421 | |
| 422 | debug("%s: %s\n", __func__, prop_name); |
| 423 | cell = get_prop_check_min_len(blob, node, prop_name, |
| 424 | sizeof(u32) * count, &err); |
| 425 | if (!err) { |
| 426 | for (i = 0; i < count; i++) |
| 427 | array[i] = fdt32_to_cpu(cell[i]); |
| 428 | } |
| 429 | return err; |
| 430 | } |
| 431 | |
Simon Glass | 744ec23 | 2012-04-02 13:18:41 +0000 | [diff] [blame] | 432 | const u32 *fdtdec_locate_array(const void *blob, int node, |
| 433 | const char *prop_name, int count) |
| 434 | { |
| 435 | const u32 *cell; |
| 436 | int err; |
| 437 | |
| 438 | cell = get_prop_check_min_len(blob, node, prop_name, |
| 439 | sizeof(u32) * count, &err); |
| 440 | return err ? NULL : cell; |
| 441 | } |
| 442 | |
Simon Glass | c469736 | 2012-02-27 10:52:35 +0000 | [diff] [blame] | 443 | int fdtdec_get_bool(const void *blob, int node, const char *prop_name) |
| 444 | { |
| 445 | const s32 *cell; |
| 446 | int len; |
| 447 | |
| 448 | debug("%s: %s\n", __func__, prop_name); |
| 449 | cell = fdt_getprop(blob, node, prop_name, &len); |
| 450 | return cell != NULL; |
| 451 | } |
Simon Glass | 0d83c4d | 2012-02-27 10:52:36 +0000 | [diff] [blame] | 452 | |
| 453 | /** |
| 454 | * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no |
| 455 | * terminating item. |
| 456 | * |
| 457 | * @param blob FDT blob to use |
| 458 | * @param node Node to look at |
| 459 | * @param prop_name Node property name |
| 460 | * @param gpio Array of gpio elements to fill from FDT. This will be |
| 461 | * untouched if either 0 or an error is returned |
| 462 | * @param max_count Maximum number of elements allowed |
| 463 | * @return number of GPIOs read if ok, -FDT_ERR_BADLAYOUT if max_count would |
| 464 | * be exceeded, or -FDT_ERR_NOTFOUND if the property is missing. |
| 465 | */ |
Abhilash Kesavan | 4353ffc | 2012-10-25 16:31:01 +0000 | [diff] [blame] | 466 | int fdtdec_decode_gpios(const void *blob, int node, const char *prop_name, |
| 467 | struct fdt_gpio_state *gpio, int max_count) |
Simon Glass | 0d83c4d | 2012-02-27 10:52:36 +0000 | [diff] [blame] | 468 | { |
| 469 | const struct fdt_property *prop; |
| 470 | const u32 *cell; |
| 471 | const char *name; |
| 472 | int len, i; |
| 473 | |
| 474 | debug("%s: %s\n", __func__, prop_name); |
| 475 | assert(max_count > 0); |
| 476 | prop = fdt_get_property(blob, node, prop_name, &len); |
| 477 | if (!prop) { |
Simon Glass | c8c86dd | 2012-07-12 05:25:01 +0000 | [diff] [blame] | 478 | debug("%s: property '%s' missing\n", __func__, prop_name); |
Simon Glass | 0d83c4d | 2012-02-27 10:52:36 +0000 | [diff] [blame] | 479 | return -FDT_ERR_NOTFOUND; |
| 480 | } |
| 481 | |
| 482 | /* We will use the name to tag the GPIO */ |
| 483 | name = fdt_string(blob, fdt32_to_cpu(prop->nameoff)); |
| 484 | cell = (u32 *)prop->data; |
| 485 | len /= sizeof(u32) * 3; /* 3 cells per GPIO record */ |
| 486 | if (len > max_count) { |
Simon Glass | c8c86dd | 2012-07-12 05:25:01 +0000 | [diff] [blame] | 487 | debug(" %s: too many GPIOs / cells for " |
Simon Glass | 0d83c4d | 2012-02-27 10:52:36 +0000 | [diff] [blame] | 488 | "property '%s'\n", __func__, prop_name); |
| 489 | return -FDT_ERR_BADLAYOUT; |
| 490 | } |
| 491 | |
| 492 | /* Read out the GPIO data from the cells */ |
| 493 | for (i = 0; i < len; i++, cell += 3) { |
| 494 | gpio[i].gpio = fdt32_to_cpu(cell[1]); |
| 495 | gpio[i].flags = fdt32_to_cpu(cell[2]); |
| 496 | gpio[i].name = name; |
| 497 | } |
| 498 | |
| 499 | return len; |
| 500 | } |
| 501 | |
| 502 | int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name, |
| 503 | struct fdt_gpio_state *gpio) |
| 504 | { |
| 505 | int err; |
| 506 | |
| 507 | debug("%s: %s\n", __func__, prop_name); |
| 508 | gpio->gpio = FDT_GPIO_NONE; |
| 509 | gpio->name = NULL; |
| 510 | err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1); |
| 511 | return err == 1 ? 0 : err; |
| 512 | } |
| 513 | |
Sean Paul | adbcfc9 | 2012-10-25 16:31:06 +0000 | [diff] [blame] | 514 | int fdtdec_get_gpio(struct fdt_gpio_state *gpio) |
| 515 | { |
| 516 | int val; |
| 517 | |
| 518 | if (!fdt_gpio_isvalid(gpio)) |
| 519 | return -1; |
| 520 | |
| 521 | val = gpio_get_value(gpio->gpio); |
| 522 | return gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val; |
| 523 | } |
| 524 | |
| 525 | int fdtdec_set_gpio(struct fdt_gpio_state *gpio, int val) |
| 526 | { |
| 527 | if (!fdt_gpio_isvalid(gpio)) |
| 528 | return -1; |
| 529 | |
| 530 | val = gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val; |
| 531 | return gpio_set_value(gpio->gpio, val); |
| 532 | } |
| 533 | |
Simon Glass | 0d83c4d | 2012-02-27 10:52:36 +0000 | [diff] [blame] | 534 | int fdtdec_setup_gpio(struct fdt_gpio_state *gpio) |
| 535 | { |
| 536 | /* |
| 537 | * Return success if there is no GPIO defined. This is used for |
| 538 | * optional GPIOs) |
| 539 | */ |
| 540 | if (!fdt_gpio_isvalid(gpio)) |
| 541 | return 0; |
| 542 | |
| 543 | if (gpio_request(gpio->gpio, gpio->name)) |
| 544 | return -1; |
| 545 | return 0; |
| 546 | } |
Anton Staff | f2305ec | 2012-04-17 09:01:28 +0000 | [diff] [blame] | 547 | |
| 548 | int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name, |
| 549 | u8 *array, int count) |
| 550 | { |
| 551 | const u8 *cell; |
| 552 | int err; |
| 553 | |
| 554 | cell = get_prop_check_min_len(blob, node, prop_name, count, &err); |
| 555 | if (!err) |
| 556 | memcpy(array, cell, count); |
| 557 | return err; |
| 558 | } |
| 559 | |
| 560 | const u8 *fdtdec_locate_byte_array(const void *blob, int node, |
| 561 | const char *prop_name, int count) |
| 562 | { |
| 563 | const u8 *cell; |
| 564 | int err; |
| 565 | |
| 566 | cell = get_prop_check_min_len(blob, node, prop_name, count, &err); |
| 567 | if (err) |
| 568 | return NULL; |
| 569 | return cell; |
| 570 | } |
Abhilash Kesavan | 67217af | 2012-10-25 16:30:58 +0000 | [diff] [blame] | 571 | |
Abhilash Kesavan | 67217af | 2012-10-25 16:30:58 +0000 | [diff] [blame] | 572 | int fdtdec_get_config_int(const void *blob, const char *prop_name, |
| 573 | int default_val) |
| 574 | { |
| 575 | int config_node; |
| 576 | |
| 577 | debug("%s: %s\n", __func__, prop_name); |
| 578 | config_node = fdt_path_offset(blob, "/config"); |
| 579 | if (config_node < 0) |
| 580 | return default_val; |
| 581 | return fdtdec_get_int(blob, config_node, prop_name, default_val); |
| 582 | } |
Simon Glass | b049447 | 2012-10-25 16:30:59 +0000 | [diff] [blame] | 583 | |
Gabe Black | 3e1c151 | 2012-10-25 16:31:04 +0000 | [diff] [blame] | 584 | int fdtdec_get_config_bool(const void *blob, const char *prop_name) |
| 585 | { |
| 586 | int config_node; |
| 587 | const void *prop; |
| 588 | |
| 589 | debug("%s: %s\n", __func__, prop_name); |
| 590 | config_node = fdt_path_offset(blob, "/config"); |
| 591 | if (config_node < 0) |
| 592 | return 0; |
| 593 | prop = fdt_get_property(blob, config_node, prop_name, NULL); |
| 594 | |
| 595 | return prop != NULL; |
| 596 | } |
| 597 | |
Simon Glass | b049447 | 2012-10-25 16:30:59 +0000 | [diff] [blame] | 598 | char *fdtdec_get_config_string(const void *blob, const char *prop_name) |
| 599 | { |
| 600 | const char *nodep; |
| 601 | int nodeoffset; |
| 602 | int len; |
| 603 | |
| 604 | debug("%s: %s\n", __func__, prop_name); |
| 605 | nodeoffset = fdt_path_offset(blob, "/config"); |
| 606 | if (nodeoffset < 0) |
| 607 | return NULL; |
| 608 | |
| 609 | nodep = fdt_getprop(blob, nodeoffset, prop_name, &len); |
| 610 | if (!nodep) |
| 611 | return NULL; |
| 612 | |
| 613 | return (char *)nodep; |
| 614 | } |
Simon Glass | f909b9c | 2012-10-25 16:31:00 +0000 | [diff] [blame] | 615 | |
| 616 | int fdtdec_decode_region(const void *blob, int node, |
| 617 | const char *prop_name, void **ptrp, size_t *size) |
| 618 | { |
| 619 | const fdt_addr_t *cell; |
| 620 | int len; |
| 621 | |
| 622 | debug("%s: %s\n", __func__, prop_name); |
| 623 | cell = fdt_getprop(blob, node, prop_name, &len); |
| 624 | if (!cell || (len != sizeof(fdt_addr_t) * 2)) |
| 625 | return -1; |
| 626 | |
| 627 | *ptrp = (void *)fdt_addr_to_cpu(*cell); |
| 628 | *size = fdt_size_to_cpu(cell[1]); |
| 629 | debug("%s: size=%zx\n", __func__, *size); |
| 630 | return 0; |
| 631 | } |