]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - lib/fdtdec.c
x86: gpio: add pinctrl support from the device tree
[karo-tx-uboot.git] / lib / fdtdec.c
1 /*
2  * Copyright (c) 2011 The Chromium OS Authors.
3  * SPDX-License-Identifier:     GPL-2.0+
4  */
5
6 #ifndef USE_HOSTCC
7 #include <common.h>
8 #include <errno.h>
9 #include <serial.h>
10 #include <libfdt.h>
11 #include <fdtdec.h>
12 #include <asm/sections.h>
13 #include <linux/ctype.h>
14
15 DECLARE_GLOBAL_DATA_PTR;
16
17 /*
18  * Here are the type we know about. One day we might allow drivers to
19  * register. For now we just put them here. The COMPAT macro allows us to
20  * turn this into a sparse list later, and keeps the ID with the name.
21  */
22 #define COMPAT(id, name) name
23 static const char * const compat_names[COMPAT_COUNT] = {
24         COMPAT(UNKNOWN, "<none>"),
25         COMPAT(NVIDIA_TEGRA20_USB, "nvidia,tegra20-ehci"),
26         COMPAT(NVIDIA_TEGRA30_USB, "nvidia,tegra30-ehci"),
27         COMPAT(NVIDIA_TEGRA114_USB, "nvidia,tegra114-ehci"),
28         COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
29         COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
30         COMPAT(NVIDIA_TEGRA20_KBC, "nvidia,tegra20-kbc"),
31         COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
32         COMPAT(NVIDIA_TEGRA20_PWM, "nvidia,tegra20-pwm"),
33         COMPAT(NVIDIA_TEGRA124_DC, "nvidia,tegra124-dc"),
34         COMPAT(NVIDIA_TEGRA124_SOR, "nvidia,tegra124-sor"),
35         COMPAT(NVIDIA_TEGRA124_PMC, "nvidia,tegra124-pmc"),
36         COMPAT(NVIDIA_TEGRA20_DC, "nvidia,tegra20-dc"),
37         COMPAT(NVIDIA_TEGRA124_SDMMC, "nvidia,tegra124-sdhci"),
38         COMPAT(NVIDIA_TEGRA30_SDMMC, "nvidia,tegra30-sdhci"),
39         COMPAT(NVIDIA_TEGRA20_SDMMC, "nvidia,tegra20-sdhci"),
40         COMPAT(NVIDIA_TEGRA124_PCIE, "nvidia,tegra124-pcie"),
41         COMPAT(NVIDIA_TEGRA30_PCIE, "nvidia,tegra30-pcie"),
42         COMPAT(NVIDIA_TEGRA20_PCIE, "nvidia,tegra20-pcie"),
43         COMPAT(NVIDIA_TEGRA124_XUSB_PADCTL, "nvidia,tegra124-xusb-padctl"),
44         COMPAT(SMSC_LAN9215, "smsc,lan9215"),
45         COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"),
46         COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"),
47         COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"),
48         COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"),
49         COMPAT(GOOGLE_CROS_EC_KEYB, "google,cros-ec-keyb"),
50         COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
51         COMPAT(SAMSUNG_EXYNOS5_USB3_PHY, "samsung,exynos5250-usb3-phy"),
52         COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"),
53         COMPAT(SAMSUNG_EXYNOS_FIMD, "samsung,exynos-fimd"),
54         COMPAT(SAMSUNG_EXYNOS_MIPI_DSI, "samsung,exynos-mipi-dsi"),
55         COMPAT(SAMSUNG_EXYNOS5_DP, "samsung,exynos5-dp"),
56         COMPAT(SAMSUNG_EXYNOS_DWMMC, "samsung,exynos-dwmmc"),
57         COMPAT(SAMSUNG_EXYNOS_MMC, "samsung,exynos-mmc"),
58         COMPAT(SAMSUNG_EXYNOS_SERIAL, "samsung,exynos4210-uart"),
59         COMPAT(MAXIM_MAX77686_PMIC, "maxim,max77686"),
60         COMPAT(GENERIC_SPI_FLASH, "spi-flash"),
61         COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"),
62         COMPAT(INFINEON_SLB9635_TPM, "infineon,slb9635-tpm"),
63         COMPAT(INFINEON_SLB9645_TPM, "infineon,slb9645tt"),
64         COMPAT(SAMSUNG_EXYNOS5_I2C, "samsung,exynos5-hsi2c"),
65         COMPAT(SANDBOX_LCD_SDL, "sandbox,lcd-sdl"),
66         COMPAT(TI_TPS65090, "ti,tps65090"),
67         COMPAT(COMPAT_NXP_PTN3460, "nxp,ptn3460"),
68         COMPAT(SAMSUNG_EXYNOS_SYSMMU, "samsung,sysmmu-v3.3"),
69         COMPAT(PARADE_PS8625, "parade,ps8625"),
70         COMPAT(INTEL_MICROCODE, "intel,microcode"),
71         COMPAT(MEMORY_SPD, "memory-spd"),
72         COMPAT(INTEL_PANTHERPOINT_AHCI, "intel,pantherpoint-ahci"),
73         COMPAT(INTEL_MODEL_206AX, "intel,model-206ax"),
74         COMPAT(INTEL_GMA, "intel,gma"),
75         COMPAT(AMS_AS3722, "ams,as3722"),
76         COMPAT(INTEL_ICH_SPI, "intel,ich-spi"),
77         COMPAT(INTEL_QRK_MRC, "intel,quark-mrc"),
78         COMPAT(INTEL_X86_PINCTRL, "intel,x86-pinctrl"),
79         COMPAT(SOCIONEXT_XHCI, "socionext,uniphier-xhci"),
80         COMPAT(COMPAT_INTEL_PCH, "intel,bd82x6x"),
81         COMPAT(COMPAT_INTEL_IRQ_ROUTER, "intel,irq-router"),
82 };
83
84 const char *fdtdec_get_compatible(enum fdt_compat_id id)
85 {
86         /* We allow reading of the 'unknown' ID for testing purposes */
87         assert(id >= 0 && id < COMPAT_COUNT);
88         return compat_names[id];
89 }
90
91 fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
92                 const char *prop_name, fdt_size_t *sizep)
93 {
94         const fdt_addr_t *cell;
95         int len;
96
97         debug("%s: %s: ", __func__, prop_name);
98         cell = fdt_getprop(blob, node, prop_name, &len);
99         if (cell && ((!sizep && len == sizeof(fdt_addr_t)) ||
100                      len == sizeof(fdt_addr_t) * 2)) {
101                 fdt_addr_t addr = fdt_addr_to_cpu(*cell);
102                 if (sizep) {
103                         const fdt_size_t *size;
104
105                         size = (fdt_size_t *)((char *)cell +
106                                         sizeof(fdt_addr_t));
107                         *sizep = fdt_size_to_cpu(*size);
108                         debug("addr=%08lx, size=%08x\n",
109                               (ulong)addr, *sizep);
110                 } else {
111                         debug("%08lx\n", (ulong)addr);
112                 }
113                 return addr;
114         }
115         debug("(not found)\n");
116         return FDT_ADDR_T_NONE;
117 }
118
119 fdt_addr_t fdtdec_get_addr(const void *blob, int node,
120                 const char *prop_name)
121 {
122         return fdtdec_get_addr_size(blob, node, prop_name, NULL);
123 }
124
125 #ifdef CONFIG_PCI
126 int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type,
127                 const char *prop_name, struct fdt_pci_addr *addr)
128 {
129         const u32 *cell;
130         int len;
131         int ret = -ENOENT;
132
133         debug("%s: %s: ", __func__, prop_name);
134
135         /*
136          * If we follow the pci bus bindings strictly, we should check
137          * the value of the node's parent node's #address-cells and
138          * #size-cells. They need to be 3 and 2 accordingly. However,
139          * for simplicity we skip the check here.
140          */
141         cell = fdt_getprop(blob, node, prop_name, &len);
142         if (!cell)
143                 goto fail;
144
145         if ((len % FDT_PCI_REG_SIZE) == 0) {
146                 int num = len / FDT_PCI_REG_SIZE;
147                 int i;
148
149                 for (i = 0; i < num; i++) {
150                         debug("pci address #%d: %08lx %08lx %08lx\n", i,
151                               (ulong)fdt_addr_to_cpu(cell[0]),
152                               (ulong)fdt_addr_to_cpu(cell[1]),
153                               (ulong)fdt_addr_to_cpu(cell[2]));
154                         if ((fdt_addr_to_cpu(*cell) & type) == type) {
155                                 addr->phys_hi = fdt_addr_to_cpu(cell[0]);
156                                 addr->phys_mid = fdt_addr_to_cpu(cell[1]);
157                                 addr->phys_lo = fdt_addr_to_cpu(cell[2]);
158                                 break;
159                         } else {
160                                 cell += (FDT_PCI_ADDR_CELLS +
161                                          FDT_PCI_SIZE_CELLS);
162                         }
163                 }
164
165                 if (i == num) {
166                         ret = -ENXIO;
167                         goto fail;
168                 }
169
170                 return 0;
171         } else {
172                 ret = -EINVAL;
173         }
174
175 fail:
176         debug("(not found)\n");
177         return ret;
178 }
179
180 int fdtdec_get_pci_vendev(const void *blob, int node, u16 *vendor, u16 *device)
181 {
182         const char *list, *end;
183         int len;
184
185         list = fdt_getprop(blob, node, "compatible", &len);
186         if (!list)
187                 return -ENOENT;
188
189         end = list + len;
190         while (list < end) {
191                 char *s;
192
193                 len = strlen(list);
194                 if (len >= strlen("pciVVVV,DDDD")) {
195                         s = strstr(list, "pci");
196
197                         /*
198                          * check if the string is something like pciVVVV,DDDD.RR
199                          * or just pciVVVV,DDDD
200                          */
201                         if (s && s[7] == ',' &&
202                             (s[12] == '.' || s[12] == 0)) {
203                                 s += 3;
204                                 *vendor = simple_strtol(s, NULL, 16);
205
206                                 s += 5;
207                                 *device = simple_strtol(s, NULL, 16);
208
209                                 return 0;
210                         }
211                 } else {
212                         list += (len + 1);
213                 }
214         }
215
216         return -ENOENT;
217 }
218
219 int fdtdec_get_pci_bdf(const void *blob, int node,
220                 struct fdt_pci_addr *addr, pci_dev_t *bdf)
221 {
222         u16 dt_vendor, dt_device, vendor, device;
223         int ret;
224
225         /* get vendor id & device id from the compatible string */
226         ret = fdtdec_get_pci_vendev(blob, node, &dt_vendor, &dt_device);
227         if (ret)
228                 return ret;
229
230         /* extract the bdf from fdt_pci_addr */
231         *bdf = addr->phys_hi & 0xffff00;
232
233         /* read vendor id & device id based on bdf */
234         pci_read_config_word(*bdf, PCI_VENDOR_ID, &vendor);
235         pci_read_config_word(*bdf, PCI_DEVICE_ID, &device);
236
237         /*
238          * Note there are two places in the device tree to fully describe
239          * a pci device: one is via compatible string with a format of
240          * "pciVVVV,DDDD" and the other one is the bdf numbers encoded in
241          * the device node's reg address property. We read the vendor id
242          * and device id based on bdf and compare the values with the
243          * "VVVV,DDDD". If they are the same, then we are good to use bdf
244          * to read device's bar. But if they are different, we have to rely
245          * on the vendor id and device id extracted from the compatible
246          * string and locate the real bdf by pci_find_device(). This is
247          * because normally we may only know device's device number and
248          * function number when writing device tree. The bus number is
249          * dynamically assigned during the pci enumeration process.
250          */
251         if ((dt_vendor != vendor) || (dt_device != device)) {
252                 *bdf = pci_find_device(dt_vendor, dt_device, 0);
253                 if (*bdf == -1)
254                         return -ENODEV;
255         }
256
257         return 0;
258 }
259
260 int fdtdec_get_pci_bar32(const void *blob, int node,
261                 struct fdt_pci_addr *addr, u32 *bar)
262 {
263         pci_dev_t bdf;
264         int barnum;
265         int ret;
266
267         /* get pci devices's bdf */
268         ret = fdtdec_get_pci_bdf(blob, node, addr, &bdf);
269         if (ret)
270                 return ret;
271
272         /* extract the bar number from fdt_pci_addr */
273         barnum = addr->phys_hi & 0xff;
274         if ((barnum < PCI_BASE_ADDRESS_0) || (barnum > PCI_CARDBUS_CIS))
275                 return -EINVAL;
276
277         barnum = (barnum - PCI_BASE_ADDRESS_0) / 4;
278         *bar = pci_read_bar32(pci_bus_to_hose(PCI_BUS(bdf)), bdf, barnum);
279
280         return 0;
281 }
282 #endif
283
284 uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
285                 uint64_t default_val)
286 {
287         const uint64_t *cell64;
288         int length;
289
290         cell64 = fdt_getprop(blob, node, prop_name, &length);
291         if (!cell64 || length < sizeof(*cell64))
292                 return default_val;
293
294         return fdt64_to_cpu(*cell64);
295 }
296
297 int fdtdec_get_is_enabled(const void *blob, int node)
298 {
299         const char *cell;
300
301         /*
302          * It should say "okay", so only allow that. Some fdts use "ok" but
303          * this is a bug. Please fix your device tree source file. See here
304          * for discussion:
305          *
306          * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
307          */
308         cell = fdt_getprop(blob, node, "status", NULL);
309         if (cell)
310                 return 0 == strcmp(cell, "okay");
311         return 1;
312 }
313
314 enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
315 {
316         enum fdt_compat_id id;
317
318         /* Search our drivers */
319         for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
320                 if (0 == fdt_node_check_compatible(blob, node,
321                                 compat_names[id]))
322                         return id;
323         return COMPAT_UNKNOWN;
324 }
325
326 int fdtdec_next_compatible(const void *blob, int node,
327                 enum fdt_compat_id id)
328 {
329         return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
330 }
331
332 int fdtdec_next_compatible_subnode(const void *blob, int node,
333                 enum fdt_compat_id id, int *depthp)
334 {
335         do {
336                 node = fdt_next_node(blob, node, depthp);
337         } while (*depthp > 1);
338
339         /* If this is a direct subnode, and compatible, return it */
340         if (*depthp == 1 && 0 == fdt_node_check_compatible(
341                                                 blob, node, compat_names[id]))
342                 return node;
343
344         return -FDT_ERR_NOTFOUND;
345 }
346
347 int fdtdec_next_alias(const void *blob, const char *name,
348                 enum fdt_compat_id id, int *upto)
349 {
350 #define MAX_STR_LEN 20
351         char str[MAX_STR_LEN + 20];
352         int node, err;
353
354         /* snprintf() is not available */
355         assert(strlen(name) < MAX_STR_LEN);
356         sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
357         node = fdt_path_offset(blob, str);
358         if (node < 0)
359                 return node;
360         err = fdt_node_check_compatible(blob, node, compat_names[id]);
361         if (err < 0)
362                 return err;
363         if (err)
364                 return -FDT_ERR_NOTFOUND;
365         (*upto)++;
366         return node;
367 }
368
369 int fdtdec_find_aliases_for_id(const void *blob, const char *name,
370                         enum fdt_compat_id id, int *node_list, int maxcount)
371 {
372         memset(node_list, '\0', sizeof(*node_list) * maxcount);
373
374         return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
375 }
376
377 /* TODO: Can we tighten this code up a little? */
378 int fdtdec_add_aliases_for_id(const void *blob, const char *name,
379                         enum fdt_compat_id id, int *node_list, int maxcount)
380 {
381         int name_len = strlen(name);
382         int nodes[maxcount];
383         int num_found = 0;
384         int offset, node;
385         int alias_node;
386         int count;
387         int i, j;
388
389         /* find the alias node if present */
390         alias_node = fdt_path_offset(blob, "/aliases");
391
392         /*
393          * start with nothing, and we can assume that the root node can't
394          * match
395          */
396         memset(nodes, '\0', sizeof(nodes));
397
398         /* First find all the compatible nodes */
399         for (node = count = 0; node >= 0 && count < maxcount;) {
400                 node = fdtdec_next_compatible(blob, node, id);
401                 if (node >= 0)
402                         nodes[count++] = node;
403         }
404         if (node >= 0)
405                 debug("%s: warning: maxcount exceeded with alias '%s'\n",
406                        __func__, name);
407
408         /* Now find all the aliases */
409         for (offset = fdt_first_property_offset(blob, alias_node);
410                         offset > 0;
411                         offset = fdt_next_property_offset(blob, offset)) {
412                 const struct fdt_property *prop;
413                 const char *path;
414                 int number;
415                 int found;
416
417                 node = 0;
418                 prop = fdt_get_property_by_offset(blob, offset, NULL);
419                 path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
420                 if (prop->len && 0 == strncmp(path, name, name_len))
421                         node = fdt_path_offset(blob, prop->data);
422                 if (node <= 0)
423                         continue;
424
425                 /* Get the alias number */
426                 number = simple_strtoul(path + name_len, NULL, 10);
427                 if (number < 0 || number >= maxcount) {
428                         debug("%s: warning: alias '%s' is out of range\n",
429                                __func__, path);
430                         continue;
431                 }
432
433                 /* Make sure the node we found is actually in our list! */
434                 found = -1;
435                 for (j = 0; j < count; j++)
436                         if (nodes[j] == node) {
437                                 found = j;
438                                 break;
439                         }
440
441                 if (found == -1) {
442                         debug("%s: warning: alias '%s' points to a node "
443                                 "'%s' that is missing or is not compatible "
444                                 " with '%s'\n", __func__, path,
445                                 fdt_get_name(blob, node, NULL),
446                                compat_names[id]);
447                         continue;
448                 }
449
450                 /*
451                  * Add this node to our list in the right place, and mark
452                  * it as done.
453                  */
454                 if (fdtdec_get_is_enabled(blob, node)) {
455                         if (node_list[number]) {
456                                 debug("%s: warning: alias '%s' requires that "
457                                       "a node be placed in the list in a "
458                                       "position which is already filled by "
459                                       "node '%s'\n", __func__, path,
460                                       fdt_get_name(blob, node, NULL));
461                                 continue;
462                         }
463                         node_list[number] = node;
464                         if (number >= num_found)
465                                 num_found = number + 1;
466                 }
467                 nodes[found] = 0;
468         }
469
470         /* Add any nodes not mentioned by an alias */
471         for (i = j = 0; i < maxcount; i++) {
472                 if (!node_list[i]) {
473                         for (; j < maxcount; j++)
474                                 if (nodes[j] &&
475                                         fdtdec_get_is_enabled(blob, nodes[j]))
476                                         break;
477
478                         /* Have we run out of nodes to add? */
479                         if (j == maxcount)
480                                 break;
481
482                         assert(!node_list[i]);
483                         node_list[i] = nodes[j++];
484                         if (i >= num_found)
485                                 num_found = i + 1;
486                 }
487         }
488
489         return num_found;
490 }
491
492 int fdtdec_get_alias_seq(const void *blob, const char *base, int offset,
493                          int *seqp)
494 {
495         int base_len = strlen(base);
496         const char *find_name;
497         int find_namelen;
498         int prop_offset;
499         int aliases;
500
501         find_name = fdt_get_name(blob, offset, &find_namelen);
502         debug("Looking for '%s' at %d, name %s\n", base, offset, find_name);
503
504         aliases = fdt_path_offset(blob, "/aliases");
505         for (prop_offset = fdt_first_property_offset(blob, aliases);
506              prop_offset > 0;
507              prop_offset = fdt_next_property_offset(blob, prop_offset)) {
508                 const char *prop;
509                 const char *name;
510                 const char *slash;
511                 const char *p;
512                 int len;
513
514                 prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len);
515                 debug("   - %s, %s\n", name, prop);
516                 if (len < find_namelen || *prop != '/' || prop[len - 1] ||
517                     strncmp(name, base, base_len))
518                         continue;
519
520                 slash = strrchr(prop, '/');
521                 if (strcmp(slash + 1, find_name))
522                         continue;
523                 for (p = name + strlen(name) - 1; p > name; p--) {
524                         if (!isdigit(*p)) {
525                                 *seqp = simple_strtoul(p + 1, NULL, 10);
526                                 debug("Found seq %d\n", *seqp);
527                                 return 0;
528                         }
529                 }
530         }
531
532         debug("Not found\n");
533         return -ENOENT;
534 }
535
536 int fdtdec_get_chosen_node(const void *blob, const char *name)
537 {
538         const char *prop;
539         int chosen_node;
540         int len;
541
542         if (!blob)
543                 return -FDT_ERR_NOTFOUND;
544         chosen_node = fdt_path_offset(blob, "/chosen");
545         prop = fdt_getprop(blob, chosen_node, name, &len);
546         if (!prop)
547                 return -FDT_ERR_NOTFOUND;
548         return fdt_path_offset(blob, prop);
549 }
550
551 int fdtdec_check_fdt(void)
552 {
553         /*
554          * We must have an FDT, but we cannot panic() yet since the console
555          * is not ready. So for now, just assert(). Boards which need an early
556          * FDT (prior to console ready) will need to make their own
557          * arrangements and do their own checks.
558          */
559         assert(!fdtdec_prepare_fdt());
560         return 0;
561 }
562
563 /*
564  * This function is a little odd in that it accesses global data. At some
565  * point if the architecture board.c files merge this will make more sense.
566  * Even now, it is common code.
567  */
568 int fdtdec_prepare_fdt(void)
569 {
570         if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) ||
571             fdt_check_header(gd->fdt_blob)) {
572 #ifdef CONFIG_SPL_BUILD
573                 puts("Missing DTB\n");
574 #else
575                 puts("No valid device tree binary found - please append one to U-Boot binary, use u-boot-dtb.bin or define CONFIG_OF_EMBED. For sandbox, use -d <file.dtb>\n");
576 #endif
577                 return -1;
578         }
579         return 0;
580 }
581
582 int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
583 {
584         const u32 *phandle;
585         int lookup;
586
587         debug("%s: %s\n", __func__, prop_name);
588         phandle = fdt_getprop(blob, node, prop_name, NULL);
589         if (!phandle)
590                 return -FDT_ERR_NOTFOUND;
591
592         lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
593         return lookup;
594 }
595
596 /**
597  * Look up a property in a node and check that it has a minimum length.
598  *
599  * @param blob          FDT blob
600  * @param node          node to examine
601  * @param prop_name     name of property to find
602  * @param min_len       minimum property length in bytes
603  * @param err           0 if ok, or -FDT_ERR_NOTFOUND if the property is not
604                         found, or -FDT_ERR_BADLAYOUT if not enough data
605  * @return pointer to cell, which is only valid if err == 0
606  */
607 static const void *get_prop_check_min_len(const void *blob, int node,
608                 const char *prop_name, int min_len, int *err)
609 {
610         const void *cell;
611         int len;
612
613         debug("%s: %s\n", __func__, prop_name);
614         cell = fdt_getprop(blob, node, prop_name, &len);
615         if (!cell)
616                 *err = -FDT_ERR_NOTFOUND;
617         else if (len < min_len)
618                 *err = -FDT_ERR_BADLAYOUT;
619         else
620                 *err = 0;
621         return cell;
622 }
623
624 int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
625                 u32 *array, int count)
626 {
627         const u32 *cell;
628         int i, err = 0;
629
630         debug("%s: %s\n", __func__, prop_name);
631         cell = get_prop_check_min_len(blob, node, prop_name,
632                                       sizeof(u32) * count, &err);
633         if (!err) {
634                 for (i = 0; i < count; i++)
635                         array[i] = fdt32_to_cpu(cell[i]);
636         }
637         return err;
638 }
639
640 int fdtdec_get_int_array_count(const void *blob, int node,
641                                const char *prop_name, u32 *array, int count)
642 {
643         const u32 *cell;
644         int len, elems;
645         int i;
646
647         debug("%s: %s\n", __func__, prop_name);
648         cell = fdt_getprop(blob, node, prop_name, &len);
649         if (!cell)
650                 return -FDT_ERR_NOTFOUND;
651         elems = len / sizeof(u32);
652         if (count > elems)
653                 count = elems;
654         for (i = 0; i < count; i++)
655                 array[i] = fdt32_to_cpu(cell[i]);
656
657         return count;
658 }
659
660 const u32 *fdtdec_locate_array(const void *blob, int node,
661                                const char *prop_name, int count)
662 {
663         const u32 *cell;
664         int err;
665
666         cell = get_prop_check_min_len(blob, node, prop_name,
667                                       sizeof(u32) * count, &err);
668         return err ? NULL : cell;
669 }
670
671 int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
672 {
673         const s32 *cell;
674         int len;
675
676         debug("%s: %s\n", __func__, prop_name);
677         cell = fdt_getprop(blob, node, prop_name, &len);
678         return cell != NULL;
679 }
680
681 int fdtdec_parse_phandle_with_args(const void *blob, int src_node,
682                                    const char *list_name,
683                                    const char *cells_name,
684                                    int cell_count, int index,
685                                    struct fdtdec_phandle_args *out_args)
686 {
687         const __be32 *list, *list_end;
688         int rc = 0, size, cur_index = 0;
689         uint32_t count = 0;
690         int node = -1;
691         int phandle;
692
693         /* Retrieve the phandle list property */
694         list = fdt_getprop(blob, src_node, list_name, &size);
695         if (!list)
696                 return -ENOENT;
697         list_end = list + size / sizeof(*list);
698
699         /* Loop over the phandles until all the requested entry is found */
700         while (list < list_end) {
701                 rc = -EINVAL;
702                 count = 0;
703
704                 /*
705                  * If phandle is 0, then it is an empty entry with no
706                  * arguments.  Skip forward to the next entry.
707                  */
708                 phandle = be32_to_cpup(list++);
709                 if (phandle) {
710                         /*
711                          * Find the provider node and parse the #*-cells
712                          * property to determine the argument length.
713                          *
714                          * This is not needed if the cell count is hard-coded
715                          * (i.e. cells_name not set, but cell_count is set),
716                          * except when we're going to return the found node
717                          * below.
718                          */
719                         if (cells_name || cur_index == index) {
720                                 node = fdt_node_offset_by_phandle(blob,
721                                                                   phandle);
722                                 if (!node) {
723                                         debug("%s: could not find phandle\n",
724                                               fdt_get_name(blob, src_node,
725                                                            NULL));
726                                         goto err;
727                                 }
728                         }
729
730                         if (cells_name) {
731                                 count = fdtdec_get_int(blob, node, cells_name,
732                                                        -1);
733                                 if (count == -1) {
734                                         debug("%s: could not get %s for %s\n",
735                                               fdt_get_name(blob, src_node,
736                                                            NULL),
737                                               cells_name,
738                                               fdt_get_name(blob, node,
739                                                            NULL));
740                                         goto err;
741                                 }
742                         } else {
743                                 count = cell_count;
744                         }
745
746                         /*
747                          * Make sure that the arguments actually fit in the
748                          * remaining property data length
749                          */
750                         if (list + count > list_end) {
751                                 debug("%s: arguments longer than property\n",
752                                       fdt_get_name(blob, src_node, NULL));
753                                 goto err;
754                         }
755                 }
756
757                 /*
758                  * All of the error cases above bail out of the loop, so at
759                  * this point, the parsing is successful. If the requested
760                  * index matches, then fill the out_args structure and return,
761                  * or return -ENOENT for an empty entry.
762                  */
763                 rc = -ENOENT;
764                 if (cur_index == index) {
765                         if (!phandle)
766                                 goto err;
767
768                         if (out_args) {
769                                 int i;
770
771                                 if (count > MAX_PHANDLE_ARGS) {
772                                         debug("%s: too many arguments %d\n",
773                                               fdt_get_name(blob, src_node,
774                                                            NULL), count);
775                                         count = MAX_PHANDLE_ARGS;
776                                 }
777                                 out_args->node = node;
778                                 out_args->args_count = count;
779                                 for (i = 0; i < count; i++) {
780                                         out_args->args[i] =
781                                                         be32_to_cpup(list++);
782                                 }
783                         }
784
785                         /* Found it! return success */
786                         return 0;
787                 }
788
789                 node = -1;
790                 list += count;
791                 cur_index++;
792         }
793
794         /*
795          * Result will be one of:
796          * -ENOENT : index is for empty phandle
797          * -EINVAL : parsing error on data
798          * [1..n]  : Number of phandle (count mode; when index = -1)
799          */
800         rc = index < 0 ? cur_index : -ENOENT;
801  err:
802         return rc;
803 }
804
805 int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
806                 u8 *array, int count)
807 {
808         const u8 *cell;
809         int err;
810
811         cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
812         if (!err)
813                 memcpy(array, cell, count);
814         return err;
815 }
816
817 const u8 *fdtdec_locate_byte_array(const void *blob, int node,
818                              const char *prop_name, int count)
819 {
820         const u8 *cell;
821         int err;
822
823         cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
824         if (err)
825                 return NULL;
826         return cell;
827 }
828
829 int fdtdec_get_config_int(const void *blob, const char *prop_name,
830                 int default_val)
831 {
832         int config_node;
833
834         debug("%s: %s\n", __func__, prop_name);
835         config_node = fdt_path_offset(blob, "/config");
836         if (config_node < 0)
837                 return default_val;
838         return fdtdec_get_int(blob, config_node, prop_name, default_val);
839 }
840
841 int fdtdec_get_config_bool(const void *blob, const char *prop_name)
842 {
843         int config_node;
844         const void *prop;
845
846         debug("%s: %s\n", __func__, prop_name);
847         config_node = fdt_path_offset(blob, "/config");
848         if (config_node < 0)
849                 return 0;
850         prop = fdt_get_property(blob, config_node, prop_name, NULL);
851
852         return prop != NULL;
853 }
854
855 char *fdtdec_get_config_string(const void *blob, const char *prop_name)
856 {
857         const char *nodep;
858         int nodeoffset;
859         int len;
860
861         debug("%s: %s\n", __func__, prop_name);
862         nodeoffset = fdt_path_offset(blob, "/config");
863         if (nodeoffset < 0)
864                 return NULL;
865
866         nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
867         if (!nodep)
868                 return NULL;
869
870         return (char *)nodep;
871 }
872
873 int fdtdec_decode_region(const void *blob, int node, const char *prop_name,
874                          fdt_addr_t *basep, fdt_size_t *sizep)
875 {
876         const fdt_addr_t *cell;
877         int len;
878
879         debug("%s: %s: %s\n", __func__, fdt_get_name(blob, node, NULL),
880               prop_name);
881         cell = fdt_getprop(blob, node, prop_name, &len);
882         if (!cell || (len < sizeof(fdt_addr_t) * 2)) {
883                 debug("cell=%p, len=%d\n", cell, len);
884                 return -1;
885         }
886
887         *basep = fdt_addr_to_cpu(*cell);
888         *sizep = fdt_size_to_cpu(cell[1]);
889         debug("%s: base=%08lx, size=%lx\n", __func__, (ulong)*basep,
890               (ulong)*sizep);
891
892         return 0;
893 }
894
895 /**
896  * Read a flash entry from the fdt
897  *
898  * @param blob          FDT blob
899  * @param node          Offset of node to read
900  * @param name          Name of node being read
901  * @param entry         Place to put offset and size of this node
902  * @return 0 if ok, -ve on error
903  */
904 int fdtdec_read_fmap_entry(const void *blob, int node, const char *name,
905                            struct fmap_entry *entry)
906 {
907         const char *prop;
908         u32 reg[2];
909
910         if (fdtdec_get_int_array(blob, node, "reg", reg, 2)) {
911                 debug("Node '%s' has bad/missing 'reg' property\n", name);
912                 return -FDT_ERR_NOTFOUND;
913         }
914         entry->offset = reg[0];
915         entry->length = reg[1];
916         entry->used = fdtdec_get_int(blob, node, "used", entry->length);
917         prop = fdt_getprop(blob, node, "compress", NULL);
918         entry->compress_algo = prop && !strcmp(prop, "lzo") ?
919                 FMAP_COMPRESS_LZO : FMAP_COMPRESS_NONE;
920         prop = fdt_getprop(blob, node, "hash", &entry->hash_size);
921         entry->hash_algo = prop ? FMAP_HASH_SHA256 : FMAP_HASH_NONE;
922         entry->hash = (uint8_t *)prop;
923
924         return 0;
925 }
926
927 u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells)
928 {
929         u64 number = 0;
930
931         while (cells--)
932                 number = (number << 32) | fdt32_to_cpu(*ptr++);
933
934         return number;
935 }
936
937 int fdt_get_resource(const void *fdt, int node, const char *property,
938                      unsigned int index, struct fdt_resource *res)
939 {
940         const fdt32_t *ptr, *end;
941         int na, ns, len, parent;
942         unsigned int i = 0;
943
944         parent = fdt_parent_offset(fdt, node);
945         if (parent < 0)
946                 return parent;
947
948         na = fdt_address_cells(fdt, parent);
949         ns = fdt_size_cells(fdt, parent);
950
951         ptr = fdt_getprop(fdt, node, property, &len);
952         if (!ptr)
953                 return len;
954
955         end = ptr + len / sizeof(*ptr);
956
957         while (ptr + na + ns <= end) {
958                 if (i == index) {
959                         res->start = res->end = fdtdec_get_number(ptr, na);
960                         res->end += fdtdec_get_number(&ptr[na], ns) - 1;
961                         return 0;
962                 }
963
964                 ptr += na + ns;
965                 i++;
966         }
967
968         return -FDT_ERR_NOTFOUND;
969 }
970
971 int fdt_get_named_resource(const void *fdt, int node, const char *property,
972                            const char *prop_names, const char *name,
973                            struct fdt_resource *res)
974 {
975         int index;
976
977         index = fdt_find_string(fdt, node, prop_names, name);
978         if (index < 0)
979                 return index;
980
981         return fdt_get_resource(fdt, node, property, index, res);
982 }
983
984 int fdtdec_decode_memory_region(const void *blob, int config_node,
985                                 const char *mem_type, const char *suffix,
986                                 fdt_addr_t *basep, fdt_size_t *sizep)
987 {
988         char prop_name[50];
989         const char *mem;
990         fdt_size_t size, offset_size;
991         fdt_addr_t base, offset;
992         int node;
993
994         if (config_node == -1) {
995                 config_node = fdt_path_offset(blob, "/config");
996                 if (config_node < 0) {
997                         debug("%s: Cannot find /config node\n", __func__);
998                         return -ENOENT;
999                 }
1000         }
1001         if (!suffix)
1002                 suffix = "";
1003
1004         snprintf(prop_name, sizeof(prop_name), "%s-memory%s", mem_type,
1005                  suffix);
1006         mem = fdt_getprop(blob, config_node, prop_name, NULL);
1007         if (!mem) {
1008                 debug("%s: No memory type for '%s', using /memory\n", __func__,
1009                       prop_name);
1010                 mem = "/memory";
1011         }
1012
1013         node = fdt_path_offset(blob, mem);
1014         if (node < 0) {
1015                 debug("%s: Failed to find node '%s': %s\n", __func__, mem,
1016                       fdt_strerror(node));
1017                 return -ENOENT;
1018         }
1019
1020         /*
1021          * Not strictly correct - the memory may have multiple banks. We just
1022          * use the first
1023          */
1024         if (fdtdec_decode_region(blob, node, "reg", &base, &size)) {
1025                 debug("%s: Failed to decode memory region %s\n", __func__,
1026                       mem);
1027                 return -EINVAL;
1028         }
1029
1030         snprintf(prop_name, sizeof(prop_name), "%s-offset%s", mem_type,
1031                  suffix);
1032         if (fdtdec_decode_region(blob, config_node, prop_name, &offset,
1033                                  &offset_size)) {
1034                 debug("%s: Failed to decode memory region '%s'\n", __func__,
1035                       prop_name);
1036                 return -EINVAL;
1037         }
1038
1039         *basep = base + offset;
1040         *sizep = offset_size;
1041
1042         return 0;
1043 }
1044
1045 static int decode_timing_property(const void *blob, int node, const char *name,
1046                                   struct timing_entry *result)
1047 {
1048         int length, ret = 0;
1049         const u32 *prop;
1050
1051         prop = fdt_getprop(blob, node, name, &length);
1052         if (!prop) {
1053                 debug("%s: could not find property %s\n",
1054                       fdt_get_name(blob, node, NULL), name);
1055                 return length;
1056         }
1057
1058         if (length == sizeof(u32)) {
1059                 result->typ = fdtdec_get_int(blob, node, name, 0);
1060                 result->min = result->typ;
1061                 result->max = result->typ;
1062         } else {
1063                 ret = fdtdec_get_int_array(blob, node, name, &result->min, 3);
1064         }
1065
1066         return ret;
1067 }
1068
1069 int fdtdec_decode_display_timing(const void *blob, int parent, int index,
1070                                  struct display_timing *dt)
1071 {
1072         int i, node, timings_node;
1073         u32 val = 0;
1074         int ret = 0;
1075
1076         timings_node = fdt_subnode_offset(blob, parent, "display-timings");
1077         if (timings_node < 0)
1078                 return timings_node;
1079
1080         for (i = 0, node = fdt_first_subnode(blob, timings_node);
1081              node > 0 && i != index;
1082              node = fdt_next_subnode(blob, node))
1083                 i++;
1084
1085         if (node < 0)
1086                 return node;
1087
1088         memset(dt, 0, sizeof(*dt));
1089
1090         ret |= decode_timing_property(blob, node, "hback-porch",
1091                                       &dt->hback_porch);
1092         ret |= decode_timing_property(blob, node, "hfront-porch",
1093                                       &dt->hfront_porch);
1094         ret |= decode_timing_property(blob, node, "hactive", &dt->hactive);
1095         ret |= decode_timing_property(blob, node, "hsync-len", &dt->hsync_len);
1096         ret |= decode_timing_property(blob, node, "vback-porch",
1097                                       &dt->vback_porch);
1098         ret |= decode_timing_property(blob, node, "vfront-porch",
1099                                       &dt->vfront_porch);
1100         ret |= decode_timing_property(blob, node, "vactive", &dt->vactive);
1101         ret |= decode_timing_property(blob, node, "vsync-len", &dt->vsync_len);
1102         ret |= decode_timing_property(blob, node, "clock-frequency",
1103                                       &dt->pixelclock);
1104
1105         dt->flags = 0;
1106         val = fdtdec_get_int(blob, node, "vsync-active", -1);
1107         if (val != -1) {
1108                 dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
1109                                 DISPLAY_FLAGS_VSYNC_LOW;
1110         }
1111         val = fdtdec_get_int(blob, node, "hsync-active", -1);
1112         if (val != -1) {
1113                 dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
1114                                 DISPLAY_FLAGS_HSYNC_LOW;
1115         }
1116         val = fdtdec_get_int(blob, node, "de-active", -1);
1117         if (val != -1) {
1118                 dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
1119                                 DISPLAY_FLAGS_DE_LOW;
1120         }
1121         val = fdtdec_get_int(blob, node, "pixelclk-active", -1);
1122         if (val != -1) {
1123                 dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
1124                                 DISPLAY_FLAGS_PIXDATA_NEGEDGE;
1125         }
1126
1127         if (fdtdec_get_bool(blob, node, "interlaced"))
1128                 dt->flags |= DISPLAY_FLAGS_INTERLACED;
1129         if (fdtdec_get_bool(blob, node, "doublescan"))
1130                 dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
1131         if (fdtdec_get_bool(blob, node, "doubleclk"))
1132                 dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
1133
1134         return 0;
1135 }
1136
1137 int fdtdec_setup(void)
1138 {
1139 #ifdef CONFIG_OF_CONTROL
1140 # ifdef CONFIG_OF_EMBED
1141         /* Get a pointer to the FDT */
1142         gd->fdt_blob = __dtb_dt_begin;
1143 # elif defined CONFIG_OF_SEPARATE
1144 #  ifdef CONFIG_SPL_BUILD
1145         /* FDT is at end of BSS */
1146         gd->fdt_blob = (ulong *)&__bss_end;
1147 #  else
1148         /* FDT is at end of image */
1149         gd->fdt_blob = (ulong *)&_end;
1150 #endif
1151 # elif defined(CONFIG_OF_HOSTFILE)
1152         if (sandbox_read_fdt_from_file()) {
1153                 puts("Failed to read control FDT\n");
1154                 return -1;
1155         }
1156 # endif
1157 # ifndef CONFIG_SPL_BUILD
1158         /* Allow the early environment to override the fdt address */
1159         gd->fdt_blob = (void *)getenv_ulong("fdtcontroladdr", 16,
1160                                                 (uintptr_t)gd->fdt_blob);
1161 # endif
1162 #endif
1163         return fdtdec_prepare_fdt();
1164 }
1165
1166 #endif /* !USE_HOSTCC */