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