1 // SPDX-License-Identifier: GPL-2.0+
4 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
6 * Copyright 2010-2011 Freescale Semiconductor, Inc.
15 #include <stdio_dev.h>
16 #include <linux/ctype.h>
17 #include <linux/types.h>
18 #include <asm/global_data.h>
19 #include <linux/libfdt.h>
20 #include <fdt_support.h>
26 * fdt_getprop_u32_default_node - Return a node's property or a default
28 * @fdt: ptr to device tree
29 * @off: offset of node
30 * @cell: cell offset in property
31 * @prop: property name
32 * @dflt: default value if the property isn't found
34 * Convenience function to return a node's property or a default value if
35 * the property doesn't exist.
37 u32 fdt_getprop_u32_default_node(const void *fdt, int off, int cell,
38 const char *prop, const u32 dflt)
43 val = fdt_getprop(fdt, off, prop, &len);
45 /* Check if property exists */
49 /* Check if property is long enough */
50 if (len < ((cell + 1) * sizeof(uint32_t)))
53 return fdt32_to_cpu(*val);
57 * fdt_getprop_u32_default - Find a node and return it's property or a default
59 * @fdt: ptr to device tree
61 * @prop: property name
62 * @dflt: default value if the property isn't found
64 * Convenience function to find a node and return it's property or a
65 * default value if it doesn't exist.
67 u32 fdt_getprop_u32_default(const void *fdt, const char *path,
68 const char *prop, const u32 dflt)
72 off = fdt_path_offset(fdt, path);
76 return fdt_getprop_u32_default_node(fdt, off, 0, prop, dflt);
80 * fdt_find_and_setprop: Find a node and set it's property
82 * @fdt: ptr to device tree
84 * @prop: property name
85 * @val: ptr to new value
86 * @len: length of new property value
87 * @create: flag to create the property if it doesn't exist
89 * Convenience function to directly set a property given the path to the node.
91 int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
92 const void *val, int len, int create)
94 int nodeoff = fdt_path_offset(fdt, node);
99 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, NULL) == NULL))
100 return 0; /* create flag not set; so exit quietly */
102 return fdt_setprop(fdt, nodeoff, prop, val, len);
106 * fdt_find_or_add_subnode() - find or possibly add a subnode of a given node
108 * @fdt: pointer to the device tree blob
109 * @parentoffset: structure block offset of a node
110 * @name: name of the subnode to locate
112 * fdt_subnode_offset() finds a subnode of the node with a given name.
113 * If the subnode does not exist, it will be created.
115 int fdt_find_or_add_subnode(void *fdt, int parentoffset, const char *name)
119 offset = fdt_subnode_offset(fdt, parentoffset, name);
121 if (offset == -FDT_ERR_NOTFOUND)
122 offset = fdt_add_subnode(fdt, parentoffset, name);
125 printf("%s: %s: %s\n", __func__, name, fdt_strerror(offset));
130 #if defined(CONFIG_OF_STDOUT_VIA_ALIAS) && defined(CONFIG_CONS_INDEX)
131 static int fdt_fixup_stdout(void *fdt, int chosenoff)
135 char sername[9] = { 0 };
138 char tmp[256]; /* long enough */
140 sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
142 aliasoff = fdt_path_offset(fdt, "/aliases");
148 path = fdt_getprop(fdt, aliasoff, sername, &len);
154 /* fdt_setprop may break "path" so we copy it to tmp buffer */
155 memcpy(tmp, path, len);
157 err = fdt_setprop(fdt, chosenoff, "linux,stdout-path", tmp, len);
159 printf("WARNING: could not set linux,stdout-path %s.\n",
165 printf("WARNING: %s: could not read %s alias: %s\n",
166 __func__, sername, fdt_strerror(err));
171 static int fdt_fixup_stdout(void *fdt, int chosenoff)
177 static inline int fdt_setprop_uxx(void *fdt, int nodeoffset, const char *name,
178 uint64_t val, int is_u64)
181 return fdt_setprop_u64(fdt, nodeoffset, name, val);
183 return fdt_setprop_u32(fdt, nodeoffset, name, (uint32_t)val);
186 int fdt_root(void *fdt)
191 err = fdt_check_header(fdt);
193 printf("fdt_root: %s\n", fdt_strerror(err));
197 serial = env_get("serial#");
199 err = fdt_setprop(fdt, 0, "serial-number", serial,
203 printf("WARNING: could not set serial-number %s.\n",
212 int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end)
219 /* just return if the size of initrd is zero */
220 if (initrd_start == initrd_end)
223 /* find or create "/chosen" node. */
224 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
228 total = fdt_num_mem_rsv(fdt);
231 * Look for an existing entry and update it. If we don't find
232 * the entry, we will j be the next available slot.
234 for (j = 0; j < total; j++) {
235 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
236 if (addr == initrd_start) {
237 fdt_del_mem_rsv(fdt, j);
242 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start);
244 printf("fdt_initrd: %s\n", fdt_strerror(err));
248 is_u64 = (fdt_address_cells(fdt, 0) == 2);
250 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-start",
251 (uint64_t)initrd_start, is_u64);
254 printf("WARNING: could not set linux,initrd-start %s.\n",
259 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-end",
260 (uint64_t)initrd_end, is_u64);
263 printf("WARNING: could not set linux,initrd-end %s.\n",
273 * board_fdt_chosen_bootargs - boards may override this function to use
274 * alternative kernel command line arguments
276 __weak char *board_fdt_chosen_bootargs(void)
278 return env_get("bootargs");
281 int fdt_chosen(void *fdt)
283 struct abuf buf = {};
286 char *str; /* used to set string properties */
288 err = fdt_check_header(fdt);
290 printf("fdt_chosen: %s\n", fdt_strerror(err));
294 /* find or create "/chosen" node. */
295 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
299 if (IS_ENABLED(CONFIG_BOARD_RNG_SEED) && !board_rng_seed(&buf)) {
300 err = fdt_setprop(fdt, nodeoffset, "rng-seed",
301 abuf_data(&buf), abuf_size(&buf));
304 printf("WARNING: could not set rng-seed %s.\n",
310 str = board_fdt_chosen_bootargs();
313 err = fdt_setprop(fdt, nodeoffset, "bootargs", str,
316 printf("WARNING: could not set bootargs %s.\n",
322 /* add u-boot version */
323 err = fdt_setprop(fdt, nodeoffset, "u-boot,version", PLAIN_VERSION,
324 strlen(PLAIN_VERSION) + 1);
326 printf("WARNING: could not set u-boot,version %s.\n",
331 return fdt_fixup_stdout(fdt, nodeoffset);
334 void do_fixup_by_path(void *fdt, const char *path, const char *prop,
335 const void *val, int len, int create)
339 debug("Updating property '%s/%s' = ", path, prop);
340 for (i = 0; i < len; i++)
341 debug(" %.2x", *(u8*)(val+i));
344 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
346 printf("Unable to update property %s:%s, err=%s\n",
347 path, prop, fdt_strerror(rc));
350 void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
353 fdt32_t tmp = cpu_to_fdt32(val);
354 do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create);
357 void do_fixup_by_prop(void *fdt,
358 const char *pname, const void *pval, int plen,
359 const char *prop, const void *val, int len,
365 debug("Updating property '%s' = ", prop);
366 for (i = 0; i < len; i++)
367 debug(" %.2x", *(u8*)(val+i));
370 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
371 while (off != -FDT_ERR_NOTFOUND) {
372 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
373 fdt_setprop(fdt, off, prop, val, len);
374 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
378 void do_fixup_by_prop_u32(void *fdt,
379 const char *pname, const void *pval, int plen,
380 const char *prop, u32 val, int create)
382 fdt32_t tmp = cpu_to_fdt32(val);
383 do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create);
386 void do_fixup_by_compat(void *fdt, const char *compat,
387 const char *prop, const void *val, int len, int create)
392 debug("Updating property '%s' = ", prop);
393 for (i = 0; i < len; i++)
394 debug(" %.2x", *(u8*)(val+i));
397 fdt_for_each_node_by_compatible(off, fdt, -1, compat)
398 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
399 fdt_setprop(fdt, off, prop, val, len);
402 void do_fixup_by_compat_u32(void *fdt, const char *compat,
403 const char *prop, u32 val, int create)
405 fdt32_t tmp = cpu_to_fdt32(val);
406 do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create);
409 #ifdef CONFIG_ARCH_FIXUP_FDT_MEMORY
411 * fdt_pack_reg - pack address and size array into the "reg"-suitable stream
413 static int fdt_pack_reg(const void *fdt, void *buf, u64 *address, u64 *size,
417 int address_cells = fdt_address_cells(fdt, 0);
418 int size_cells = fdt_size_cells(fdt, 0);
421 for (i = 0; i < n; i++) {
422 if (address_cells == 2)
423 *(fdt64_t *)p = cpu_to_fdt64(address[i]);
425 *(fdt32_t *)p = cpu_to_fdt32(address[i]);
426 p += 4 * address_cells;
429 *(fdt64_t *)p = cpu_to_fdt64(size[i]);
431 *(fdt32_t *)p = cpu_to_fdt32(size[i]);
435 return p - (char *)buf;
438 #if CONFIG_NR_DRAM_BANKS > 4
439 #define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS
441 #define MEMORY_BANKS_MAX 4
445 * fdt_fixup_memory_banks - Update DT memory node
446 * @blob: Pointer to DT blob
447 * @start: Pointer to memory start addresses array
448 * @size: Pointer to memory sizes array
449 * @banks: Number of memory banks
451 * Return: 0 on success, negative value on failure
453 * Based on the passed number of banks and arrays, the function is able to
454 * update existing DT memory nodes to match run time detected/changed memory
455 * configuration. Implementation is handling one specific case with only one
456 * memory node where multiple tuples could be added/updated.
457 * The case where multiple memory nodes with a single tuple (base, size) are
458 * used, this function is only updating the first memory node without removing
461 int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
465 u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */
467 if (banks > MEMORY_BANKS_MAX) {
468 printf("%s: num banks %d exceeds hardcoded limit %d."
469 " Recompile with higher MEMORY_BANKS_MAX?\n",
470 __FUNCTION__, banks, MEMORY_BANKS_MAX);
474 err = fdt_check_header(blob);
476 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
480 /* find or create "/memory" node. */
481 nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
485 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
488 printf("WARNING: could not set %s %s.\n", "device_type",
493 for (i = 0; i < banks; i++) {
494 if (start[i] == 0 && size[i] == 0)
503 len = fdt_pack_reg(blob, tmp, start, size, banks);
505 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
507 printf("WARNING: could not set %s %s.\n",
508 "reg", fdt_strerror(err));
514 int fdt_set_usable_memory(void *blob, u64 start[], u64 size[], int areas)
518 u8 tmp[8 * 16]; /* Up to 64-bit address + 64-bit size */
521 printf("%s: num areas %d exceeds hardcoded limit %d\n",
526 err = fdt_check_header(blob);
528 printf("%s: %s\n", __func__, fdt_strerror(err));
532 /* find or create "/memory" node. */
533 nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
537 len = fdt_pack_reg(blob, tmp, start, size, areas);
539 err = fdt_setprop(blob, nodeoffset, "linux,usable-memory", tmp, len);
541 printf("WARNING: could not set %s %s.\n",
542 "reg", fdt_strerror(err));
550 int fdt_fixup_memory(void *blob, u64 start, u64 size)
552 return fdt_fixup_memory_banks(blob, &start, &size, 1);
555 void fdt_fixup_ethernet(void *fdt)
561 unsigned char mac_addr[ARP_HLEN];
563 #ifdef FDT_SEQ_MACADDR_FROM_ENV
565 const struct fdt_property *fdt_prop;
568 if (fdt_path_offset(fdt, "/aliases") < 0)
571 /* Cycle through all aliases */
572 for (prop = 0; ; prop++) {
575 /* FDT might have been edited, recompute the offset */
576 offset = fdt_first_property_offset(fdt,
577 fdt_path_offset(fdt, "/aliases"));
578 /* Select property number 'prop' */
579 for (j = 0; j < prop; j++)
580 offset = fdt_next_property_offset(fdt, offset);
585 path = fdt_getprop_by_offset(fdt, offset, &name, NULL);
586 if (!strncmp(name, "ethernet", 8)) {
587 /* Treat plain "ethernet" same as "ethernet0". */
588 if (!strcmp(name, "ethernet")
589 #ifdef FDT_SEQ_MACADDR_FROM_ENV
590 || !strcmp(name, "ethernet0")
594 #ifndef FDT_SEQ_MACADDR_FROM_ENV
596 i = trailing_strtol(name);
600 strcpy(mac, "ethaddr");
602 sprintf(mac, "eth%daddr", i);
606 #ifdef FDT_SEQ_MACADDR_FROM_ENV
607 nodeoff = fdt_path_offset(fdt, path);
608 fdt_prop = fdt_get_property(fdt, nodeoff, "status",
610 if (fdt_prop && !strcmp(fdt_prop->data, "disabled"))
618 for (j = 0; j < 6; j++) {
620 hextoul(tmp, &end) : 0;
622 tmp = (*end) ? end + 1 : end;
625 do_fixup_by_path(fdt, path, "mac-address",
627 do_fixup_by_path(fdt, path, "local-mac-address",
633 int fdt_record_loadable(void *blob, u32 index, const char *name,
634 uintptr_t load_addr, u32 size, uintptr_t entry_point,
635 const char *type, const char *os, const char *arch)
639 err = fdt_check_header(blob);
641 printf("%s: %s\n", __func__, fdt_strerror(err));
645 /* find or create "/fit-images" node */
646 node = fdt_find_or_add_subnode(blob, 0, "fit-images");
650 /* find or create "/fit-images/<name>" node */
651 node = fdt_find_or_add_subnode(blob, node, name);
655 fdt_setprop_u64(blob, node, "load", load_addr);
656 if (entry_point != -1)
657 fdt_setprop_u64(blob, node, "entry", entry_point);
658 fdt_setprop_u32(blob, node, "size", size);
660 fdt_setprop_string(blob, node, "type", type);
662 fdt_setprop_string(blob, node, "os", os);
664 fdt_setprop_string(blob, node, "arch", arch);
669 /* Resize the fdt to its actual size + a bit of padding */
670 int fdt_shrink_to_minimum(void *blob, uint extrasize)
681 total = fdt_num_mem_rsv(blob);
682 for (i = 0; i < total; i++) {
683 fdt_get_mem_rsv(blob, i, &addr, &size);
684 if (addr == (uintptr_t)blob) {
685 fdt_del_mem_rsv(blob, i);
692 * Calculate the actual size of the fdt
693 * plus the size needed for 5 fdt_add_mem_rsv, one
694 * for the fdt itself and 4 for a possible initrd
695 * ((initrd-start + initrd-end) * 2 (name & value))
697 actualsize = fdt_off_dt_strings(blob) +
698 fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
700 actualsize += extrasize;
701 /* Make it so the fdt ends on a page boundary */
702 actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
703 actualsize = actualsize - ((uintptr_t)blob & 0xfff);
705 /* Change the fdt header to reflect the correct size */
706 fdt_set_totalsize(blob, actualsize);
709 /* Add the new reservation */
710 ret = fdt_add_mem_rsv(blob, map_to_sysmem(blob), actualsize);
719 * fdt_delete_disabled_nodes: Delete all nodes with status == "disabled"
721 * @blob: ptr to device tree
723 int fdt_delete_disabled_nodes(void *blob)
728 offset = fdt_node_offset_by_prop_value(blob, -1, "status",
733 ret = fdt_del_node(blob, offset);
742 #define CFG_SYS_PCI_NR_INBOUND_WIN 4
744 #define FDT_PCI_PREFETCH (0x40000000)
745 #define FDT_PCI_MEM32 (0x02000000)
746 #define FDT_PCI_IO (0x01000000)
747 #define FDT_PCI_MEM64 (0x03000000)
749 int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
751 int addrcell, sizecell, len, r;
753 /* sized based on pci addr cells, size-cells, & address-cells */
754 u32 dma_ranges[(3 + 2 + 2) * CFG_SYS_PCI_NR_INBOUND_WIN];
756 addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
757 sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
759 dma_range = &dma_ranges[0];
760 for (r = 0; r < hose->region_count; r++) {
761 u64 bus_start, phys_start, size;
763 /* skip if !PCI_REGION_SYS_MEMORY */
764 if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
767 bus_start = (u64)hose->regions[r].bus_start;
768 phys_start = (u64)hose->regions[r].phys_start;
769 size = (u64)hose->regions[r].size;
772 if (size >= 0x100000000ull)
773 dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM64);
775 dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM32);
776 if (hose->regions[r].flags & PCI_REGION_PREFETCH)
777 dma_range[0] |= cpu_to_fdt32(FDT_PCI_PREFETCH);
778 #ifdef CONFIG_SYS_PCI_64BIT
779 dma_range[1] = cpu_to_fdt32(bus_start >> 32);
783 dma_range[2] = cpu_to_fdt32(bus_start & 0xffffffff);
786 dma_range[3] = cpu_to_fdt32(phys_start >> 32);
787 dma_range[4] = cpu_to_fdt32(phys_start & 0xffffffff);
789 dma_range[3] = cpu_to_fdt32(phys_start & 0xffffffff);
793 dma_range[3 + addrcell + 0] =
794 cpu_to_fdt32(size >> 32);
795 dma_range[3 + addrcell + 1] =
796 cpu_to_fdt32(size & 0xffffffff);
798 dma_range[3 + addrcell + 0] =
799 cpu_to_fdt32(size & 0xffffffff);
802 dma_range += (3 + addrcell + sizecell);
805 len = dma_range - &dma_ranges[0];
807 fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
813 int fdt_increase_size(void *fdt, int add_len)
817 newlen = fdt_totalsize(fdt) + add_len;
819 /* Open in place with a new len */
820 return fdt_open_into(fdt, fdt, newlen);
823 #ifdef CONFIG_FDT_FIXUP_PARTITIONS
824 #include <jffs2/load_kernel.h>
825 #include <mtd_node.h>
827 static int fdt_del_subnodes(const void *blob, int parent_offset)
832 for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
833 (off >= 0) && (ndepth > 0);
834 off = fdt_next_node(blob, off, &ndepth)) {
836 debug("delete %s: offset: %x\n",
837 fdt_get_name(blob, off, 0), off);
838 ret = fdt_del_node((void *)blob, off);
840 printf("Can't delete node: %s\n",
852 static int fdt_del_partitions(void *blob, int parent_offset)
859 off = fdt_next_node(blob, parent_offset, &ndepth);
860 if (off > 0 && ndepth == 1) {
861 prop = fdt_getprop(blob, off, "label", NULL);
864 * Could not find label property, nand {}; node?
865 * Check subnode, delete partitions there if any.
867 return fdt_del_partitions(blob, off);
869 ret = fdt_del_subnodes(blob, parent_offset);
871 printf("Can't remove subnodes: %s\n",
880 static int fdt_node_set_part_info(void *blob, int parent_offset,
881 struct mtd_device *dev)
883 struct list_head *pentry;
884 struct part_info *part;
890 ret = fdt_del_partitions(blob, parent_offset);
895 * Check if size/address is 1 or 2 cells.
896 * We assume #address-cells and #size-cells have same value.
898 sizecell = fdt_getprop_u32_default_node(blob, parent_offset,
899 0, "#size-cells", 1);
902 * Check if it is nand {}; subnode, adjust
903 * the offset in this case
905 off = fdt_next_node(blob, parent_offset, &ndepth);
906 if (off > 0 && ndepth == 1)
910 list_for_each_prev(pentry, &dev->parts) {
913 part = list_entry(pentry, struct part_info, link);
915 debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
916 part_num, part->name, part->size,
917 part->offset, part->mask_flags);
919 sprintf(buf, "partition@%llx", part->offset);
921 ret = fdt_add_subnode(blob, parent_offset, buf);
922 if (ret == -FDT_ERR_NOSPACE) {
923 ret = fdt_increase_size(blob, 512);
928 } else if (ret < 0) {
929 printf("Can't add partition node: %s\n",
935 /* Check MTD_WRITEABLE_CMD flag */
936 if (part->mask_flags & 1) {
938 ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
939 if (ret == -FDT_ERR_NOSPACE) {
940 ret = fdt_increase_size(blob, 512);
951 ret = fdt_setprop_u64(blob, newoff,
952 "reg", part->offset);
954 ret = fdt_appendprop_u64(blob, newoff,
957 ret = fdt_setprop_u32(blob, newoff,
958 "reg", part->offset);
960 ret = fdt_appendprop_u32(blob, newoff,
964 if (ret == -FDT_ERR_NOSPACE) {
965 ret = fdt_increase_size(blob, 512);
974 ret = fdt_setprop_string(blob, newoff, "label", part->name);
975 if (ret == -FDT_ERR_NOSPACE) {
976 ret = fdt_increase_size(blob, 512);
988 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
991 printf("Can't add property: %s\n", fdt_strerror(ret));
996 * Update partitions in nor/nand nodes using info from
997 * mtdparts environment variable. The nodes to update are
998 * specified by node_info structure which contains mtd device
999 * type and compatible string: E. g. the board code in
1000 * ft_board_setup() could use:
1002 * struct node_info nodes[] = {
1003 * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
1004 * { "cfi-flash", MTD_DEV_TYPE_NOR, },
1007 * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
1009 void fdt_fixup_mtdparts(void *blob, const struct node_info *node_info,
1012 struct mtd_device *dev;
1015 bool inited = false;
1017 for (i = 0; i < node_info_size; i++) {
1020 fdt_for_each_node_by_compatible(noff, blob, -1,
1021 node_info[i].compat) {
1024 prop = fdt_getprop(blob, noff, "status", NULL);
1025 if (prop && !strcmp(prop, "disabled"))
1028 debug("%s: %s, mtd dev type %d\n",
1029 fdt_get_name(blob, noff, 0),
1030 node_info[i].compat, node_info[i].type);
1033 if (mtdparts_init() != 0)
1038 dev = device_find(node_info[i].type, idx++);
1040 parts = fdt_subnode_offset(blob, noff,
1045 if (fdt_node_set_part_info(blob, parts, dev))
1046 return; /* return on error */
1053 void fdt_del_node_and_alias(void *blob, const char *alias)
1055 int off = fdt_path_offset(blob, alias);
1060 fdt_del_node(blob, off);
1062 off = fdt_path_offset(blob, "/aliases");
1063 fdt_delprop(blob, off, alias);
1066 /* Max address size we deal with */
1067 #define OF_MAX_ADDR_CELLS 4
1068 #define OF_BAD_ADDR FDT_ADDR_T_NONE
1069 #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
1074 static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
1078 printf(" %08x", *(addr++));
1082 static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
1086 * struct of_bus - Callbacks for bus specific translators
1087 * @name: A string used to identify this bus in debug output.
1088 * @addresses: The name of the DT property from which addresses are
1089 * to be read, typically "reg".
1090 * @match: Return non-zero if the node whose parent is at
1091 * parentoffset in the FDT blob corresponds to a bus
1092 * of this type, otherwise return zero. If NULL a match
1094 * @count_cells:Count how many cells (be32 values) a node whose parent
1095 * is at parentoffset in the FDT blob will require to
1096 * represent its address (written to *addrc) & size
1097 * (written to *sizec).
1098 * @map: Map the address addr from the address space of this
1099 * bus to that of its parent, making use of the ranges
1100 * read from DT to an array at range. na and ns are the
1101 * number of cells (be32 values) used to hold and address
1102 * or size, respectively, for this bus. pna is the number
1103 * of cells used to hold an address for the parent bus.
1104 * Returns the address in the address space of the parent
1106 * @translate: Update the value of the address cells at addr within an
1107 * FDT by adding offset to it. na specifies the number of
1108 * cells used to hold the address being translated. Returns
1109 * zero on success, non-zero on error.
1111 * Each bus type will include a struct of_bus in the of_busses array,
1112 * providing implementations of some or all of the functions used to
1113 * match the bus & handle address translation for its children.
1117 const char *addresses;
1118 int (*match)(const void *blob, int parentoffset);
1119 void (*count_cells)(const void *blob, int parentoffset,
1120 int *addrc, int *sizec);
1121 u64 (*map)(fdt32_t *addr, const fdt32_t *range,
1122 int na, int ns, int pna);
1123 int (*translate)(fdt32_t *addr, u64 offset, int na);
1126 /* Default translator (generic bus) */
1127 void fdt_support_default_count_cells(const void *blob, int parentoffset,
1128 int *addrc, int *sizec)
1130 const fdt32_t *prop;
1133 *addrc = fdt_address_cells(blob, parentoffset);
1136 prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
1138 *sizec = be32_to_cpup(prop);
1144 static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
1145 int na, int ns, int pna)
1149 cp = fdt_read_number(range, na);
1150 s = fdt_read_number(range + na + pna, ns);
1151 da = fdt_read_number(addr, na);
1153 debug("OF: default map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
1155 if (da < cp || da >= (cp + s))
1160 static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
1162 u64 a = fdt_read_number(addr, na);
1163 memset(addr, 0, na * 4);
1166 addr[na - 2] = cpu_to_fdt32(a >> 32);
1167 addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
1172 #ifdef CONFIG_OF_ISA_BUS
1174 /* ISA bus translator */
1175 static int of_bus_isa_match(const void *blob, int parentoffset)
1179 name = fdt_get_name(blob, parentoffset, NULL);
1183 return !strcmp(name, "isa");
1186 static void of_bus_isa_count_cells(const void *blob, int parentoffset,
1187 int *addrc, int *sizec)
1195 static u64 of_bus_isa_map(fdt32_t *addr, const fdt32_t *range,
1196 int na, int ns, int pna)
1200 /* Check address type match */
1201 if ((addr[0] ^ range[0]) & cpu_to_be32(1))
1204 cp = fdt_read_number(range + 1, na - 1);
1205 s = fdt_read_number(range + na + pna, ns);
1206 da = fdt_read_number(addr + 1, na - 1);
1208 debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
1210 if (da < cp || da >= (cp + s))
1215 static int of_bus_isa_translate(fdt32_t *addr, u64 offset, int na)
1217 return of_bus_default_translate(addr + 1, offset, na - 1);
1220 #endif /* CONFIG_OF_ISA_BUS */
1222 /* Array of bus specific translators */
1223 static struct of_bus of_busses[] = {
1224 #ifdef CONFIG_OF_ISA_BUS
1229 .match = of_bus_isa_match,
1230 .count_cells = of_bus_isa_count_cells,
1231 .map = of_bus_isa_map,
1232 .translate = of_bus_isa_translate,
1234 #endif /* CONFIG_OF_ISA_BUS */
1239 .count_cells = fdt_support_default_count_cells,
1240 .map = of_bus_default_map,
1241 .translate = of_bus_default_translate,
1245 static struct of_bus *of_match_bus(const void *blob, int parentoffset)
1249 if (ARRAY_SIZE(of_busses) == 1)
1252 for (bus = of_busses; bus; bus++) {
1253 if (!bus->match || bus->match(blob, parentoffset))
1258 * We should always have matched the default bus at least, since
1259 * it has a NULL match field. If we didn't then it somehow isn't
1260 * in the of_busses array or something equally catastrophic has
1267 static int of_translate_one(const void *blob, int parent, struct of_bus *bus,
1268 struct of_bus *pbus, fdt32_t *addr,
1269 int na, int ns, int pna, const char *rprop)
1271 const fdt32_t *ranges;
1274 u64 offset = OF_BAD_ADDR;
1276 /* Normally, an absence of a "ranges" property means we are
1277 * crossing a non-translatable boundary, and thus the addresses
1278 * below the current not cannot be converted to CPU physical ones.
1279 * Unfortunately, while this is very clear in the spec, it's not
1280 * what Apple understood, and they do have things like /uni-n or
1281 * /ht nodes with no "ranges" property and a lot of perfectly
1282 * useable mapped devices below them. Thus we treat the absence of
1283 * "ranges" as equivalent to an empty "ranges" property which means
1284 * a 1:1 translation at that level. It's up to the caller not to try
1285 * to translate addresses that aren't supposed to be translated in
1286 * the first place. --BenH.
1288 ranges = fdt_getprop(blob, parent, rprop, &rlen);
1289 if (ranges == NULL || rlen == 0) {
1290 offset = fdt_read_number(addr, na);
1291 memset(addr, 0, pna * 4);
1292 debug("OF: no ranges, 1:1 translation\n");
1296 debug("OF: walking ranges...\n");
1298 /* Now walk through the ranges */
1300 rone = na + pna + ns;
1301 for (; rlen >= rone; rlen -= rone, ranges += rone) {
1302 offset = bus->map(addr, ranges, na, ns, pna);
1303 if (offset != OF_BAD_ADDR)
1306 if (offset == OF_BAD_ADDR) {
1307 debug("OF: not found !\n");
1310 memcpy(addr, ranges + na, 4 * pna);
1313 of_dump_addr("OF: parent translation for:", addr, pna);
1314 debug("OF: with offset: %llu\n", offset);
1316 /* Translate it into parent bus space */
1317 return pbus->translate(addr, offset, pna);
1321 * Translate an address from the device-tree into a CPU physical address,
1322 * this walks up the tree and applies the various bus mappings on the
1325 * Note: We consider that crossing any level with #size-cells == 0 to mean
1326 * that translation is impossible (that is we are not dealing with a value
1327 * that can be mapped to a cpu physical address). This is not really specified
1328 * that way, but this is traditionally the way IBM at least do things
1330 static u64 __of_translate_address(const void *blob, int node_offset,
1331 const fdt32_t *in_addr, const char *rprop)
1334 struct of_bus *bus, *pbus;
1335 fdt32_t addr[OF_MAX_ADDR_CELLS];
1336 int na, ns, pna, pns;
1337 u64 result = OF_BAD_ADDR;
1339 debug("OF: ** translation for device %s **\n",
1340 fdt_get_name(blob, node_offset, NULL));
1342 /* Get parent & match bus type */
1343 parent = fdt_parent_offset(blob, node_offset);
1346 bus = of_match_bus(blob, parent);
1348 /* Cound address cells & copy address locally */
1349 bus->count_cells(blob, parent, &na, &ns);
1350 if (!OF_CHECK_COUNTS(na, ns)) {
1351 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1352 fdt_get_name(blob, node_offset, NULL));
1355 memcpy(addr, in_addr, na * 4);
1357 debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
1358 bus->name, na, ns, fdt_get_name(blob, parent, NULL));
1359 of_dump_addr("OF: translating address:", addr, na);
1363 /* Switch to parent bus */
1364 node_offset = parent;
1365 parent = fdt_parent_offset(blob, node_offset);
1367 /* If root, we have finished */
1369 debug("OF: reached root node\n");
1370 result = fdt_read_number(addr, na);
1374 /* Get new parent bus and counts */
1375 pbus = of_match_bus(blob, parent);
1376 pbus->count_cells(blob, parent, &pna, &pns);
1377 if (!OF_CHECK_COUNTS(pna, pns)) {
1378 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1379 fdt_get_name(blob, node_offset, NULL));
1383 debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
1384 pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
1386 /* Apply bus translation */
1387 if (of_translate_one(blob, node_offset, bus, pbus,
1388 addr, na, ns, pna, rprop))
1391 /* Complete the move up one level */
1396 of_dump_addr("OF: one level translation:", addr, na);
1403 u64 fdt_translate_address(const void *blob, int node_offset,
1404 const fdt32_t *in_addr)
1406 return __of_translate_address(blob, node_offset, in_addr, "ranges");
1409 u64 fdt_translate_dma_address(const void *blob, int node_offset,
1410 const fdt32_t *in_addr)
1412 return __of_translate_address(blob, node_offset, in_addr, "dma-ranges");
1415 int fdt_get_dma_range(const void *blob, int node, phys_addr_t *cpu,
1416 dma_addr_t *bus, u64 *size)
1418 bool found_dma_ranges = false;
1419 struct of_bus *bus_node;
1420 const fdt32_t *ranges;
1421 int na, ns, pna, pns;
1426 /* Find the closest dma-ranges property */
1427 while (parent >= 0) {
1428 ranges = fdt_getprop(blob, parent, "dma-ranges", &len);
1430 /* Ignore empty ranges, they imply no translation required */
1431 if (ranges && len > 0)
1434 /* Once we find 'dma-ranges', then a missing one is an error */
1435 if (found_dma_ranges && !ranges) {
1441 found_dma_ranges = true;
1443 parent = fdt_parent_offset(blob, parent);
1446 if (!ranges || parent < 0) {
1447 debug("no dma-ranges found for node %s\n",
1448 fdt_get_name(blob, node, NULL));
1453 /* switch to that node */
1455 parent = fdt_parent_offset(blob, node);
1457 printf("Found dma-ranges in root node, shouldn't happen\n");
1462 /* Get the address sizes both for the bus and its parent */
1463 bus_node = of_match_bus(blob, node);
1464 bus_node->count_cells(blob, node, &na, &ns);
1465 if (!OF_CHECK_COUNTS(na, ns)) {
1466 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1467 fdt_get_name(blob, node, NULL));
1472 bus_node = of_match_bus(blob, parent);
1473 bus_node->count_cells(blob, parent, &pna, &pns);
1474 if (!OF_CHECK_COUNTS(pna, pns)) {
1475 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1476 fdt_get_name(blob, parent, NULL));
1481 *bus = fdt_read_number(ranges, na);
1482 *cpu = fdt_translate_dma_address(blob, node, ranges + na);
1483 *size = fdt_read_number(ranges + na + pna, ns);
1489 * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and
1490 * who's reg property matches a physical cpu address
1492 * @blob: ptr to device tree
1493 * @compat: compatiable string to match
1494 * @compat_off: property name
1497 int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
1498 phys_addr_t compat_off)
1502 fdt_for_each_node_by_compatible(off, blob, -1, compat) {
1503 const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
1504 if (reg && compat_off == fdt_translate_address(blob, off, reg))
1508 return -FDT_ERR_NOTFOUND;
1511 static int vnode_offset_by_pathf(void *blob, const char *fmt, va_list ap)
1516 len = vsnprintf(path, sizeof(path), fmt, ap);
1517 if (len < 0 || len + 1 > sizeof(path))
1518 return -FDT_ERR_NOSPACE;
1520 return fdt_path_offset(blob, path);
1524 * fdt_node_offset_by_pathf: Find node offset by sprintf formatted path
1526 * @blob: ptr to device tree
1528 * @ap: vsnprintf arguments
1530 int fdt_node_offset_by_pathf(void *blob, const char *fmt, ...)
1536 res = vnode_offset_by_pathf(blob, fmt, ap);
1543 * fdt_set_phandle: Create a phandle property for the given node
1545 * @fdt: ptr to device tree
1546 * @nodeoffset: node to update
1547 * @phandle: phandle value to set (must be unique)
1549 int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
1554 int off = fdt_node_offset_by_phandle(fdt, phandle);
1556 if ((off >= 0) && (off != nodeoffset)) {
1559 fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
1560 printf("Trying to update node %s with phandle %u ",
1563 fdt_get_path(fdt, off, buf, sizeof(buf));
1564 printf("that already exists in node %s.\n", buf);
1565 return -FDT_ERR_BADPHANDLE;
1569 ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
1575 * fdt_create_phandle: Get or create a phandle property for the given node
1577 * @fdt: ptr to device tree
1578 * @nodeoffset: node to update
1580 unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
1582 /* see if there is a phandle already */
1583 uint32_t phandle = fdt_get_phandle(fdt, nodeoffset);
1585 /* if we got 0, means no phandle so create one */
1589 ret = fdt_generate_phandle(fdt, &phandle);
1591 printf("Can't generate phandle: %s\n",
1596 ret = fdt_set_phandle(fdt, nodeoffset, phandle);
1598 printf("Can't set phandle %u: %s\n", phandle,
1608 * fdt_create_phandle_by_compatible: Get or create a phandle for first node with
1611 * @fdt: ptr to device tree
1612 * @compat: node's compatible string
1614 unsigned int fdt_create_phandle_by_compatible(void *fdt, const char *compat)
1616 int offset = fdt_node_offset_by_compatible(fdt, -1, compat);
1619 printf("Can't find node with compatible \"%s\": %s\n", compat,
1620 fdt_strerror(offset));
1624 return fdt_create_phandle(fdt, offset);
1628 * fdt_create_phandle_by_pathf: Get or create a phandle for node given by
1629 * sprintf-formatted path
1631 * @fdt: ptr to device tree
1632 * @fmt, ...: path format string and arguments to pass to sprintf
1634 unsigned int fdt_create_phandle_by_pathf(void *fdt, const char *fmt, ...)
1640 offset = vnode_offset_by_pathf(fdt, fmt, ap);
1644 printf("Can't find node by given path: %s\n",
1645 fdt_strerror(offset));
1649 return fdt_create_phandle(fdt, offset);
1653 * fdt_set_node_status: Set status for the given node
1655 * @fdt: ptr to device tree
1656 * @nodeoffset: node to update
1657 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
1659 int fdt_set_node_status(void *fdt, int nodeoffset, enum fdt_status status)
1667 case FDT_STATUS_OKAY:
1668 ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
1670 case FDT_STATUS_DISABLED:
1671 ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
1673 case FDT_STATUS_FAIL:
1674 ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
1677 printf("Invalid fdt status: %x\n", status);
1686 * fdt_set_status_by_alias: Set status for the given node given an alias
1688 * @fdt: ptr to device tree
1689 * @alias: alias of node to update
1690 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
1692 int fdt_set_status_by_alias(void *fdt, const char* alias,
1693 enum fdt_status status)
1695 int offset = fdt_path_offset(fdt, alias);
1697 return fdt_set_node_status(fdt, offset, status);
1701 * fdt_set_status_by_compatible: Set node status for first node with given
1704 * @fdt: ptr to device tree
1705 * @compat: node's compatible string
1706 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
1708 int fdt_set_status_by_compatible(void *fdt, const char *compat,
1709 enum fdt_status status)
1711 int offset = fdt_node_offset_by_compatible(fdt, -1, compat);
1716 return fdt_set_node_status(fdt, offset, status);
1720 * fdt_set_status_by_pathf: Set node status for node given by sprintf-formatted
1723 * @fdt: ptr to device tree
1724 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
1725 * @fmt, ...: path format string and arguments to pass to sprintf
1727 int fdt_set_status_by_pathf(void *fdt, enum fdt_status status, const char *fmt,
1734 offset = vnode_offset_by_pathf(fdt, fmt, ap);
1740 return fdt_set_node_status(fdt, offset, status);
1744 * Verify the physical address of device tree node for a given alias
1746 * This function locates the device tree node of a given alias, and then
1747 * verifies that the physical address of that device matches the given
1748 * parameter. It displays a message if there is a mismatch.
1750 * Returns 1 on success, 0 on failure
1752 int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
1759 path = fdt_getprop(fdt, anode, alias, NULL);
1761 /* If there's no such alias, then it's not a failure */
1765 node = fdt_path_offset(fdt, path);
1767 printf("Warning: device tree alias '%s' points to invalid "
1768 "node %s.\n", alias, path);
1772 reg = fdt_getprop(fdt, node, "reg", &len);
1774 printf("Warning: device tree node '%s' has no address.\n",
1779 dt_addr = fdt_translate_address(fdt, node, reg);
1780 if (addr != dt_addr) {
1781 printf("Warning: U-Boot configured device %s at address %llu,\n"
1782 "but the device tree has it address %llx.\n",
1783 alias, addr, dt_addr);
1791 * Returns the base address of an SOC or PCI node
1793 u64 fdt_get_base_address(const void *fdt, int node)
1796 const fdt32_t *prop;
1798 prop = fdt_getprop(fdt, node, "reg", &size);
1800 return prop ? fdt_translate_address(fdt, node, prop) : OF_BAD_ADDR;
1804 * Read a property of size <prop_len>. Currently only supports 1 or 2 cells,
1805 * or 3 cells specially for a PCI address.
1807 static int fdt_read_prop(const fdt32_t *prop, int prop_len, int cell_off,
1808 uint64_t *val, int cells)
1810 const fdt32_t *prop32;
1811 const unaligned_fdt64_t *prop64;
1813 if ((cell_off + cells) > prop_len)
1814 return -FDT_ERR_NOSPACE;
1816 prop32 = &prop[cell_off];
1819 * Special handling for PCI address in PCI bus <ranges>
1821 * PCI child address is made up of 3 cells. Advance the cell offset
1822 * by 1 so that the PCI child address can be correctly read.
1826 prop64 = (const fdt64_t *)&prop[cell_off];
1830 *val = fdt32_to_cpu(*prop32);
1834 *val = fdt64_to_cpu(*prop64);
1837 return -FDT_ERR_NOSPACE;
1844 * fdt_read_range - Read a node's n'th range property
1846 * @fdt: ptr to device tree
1847 * @node: offset of node
1849 * @child_addr: pointer to storage for the "child address" field
1850 * @addr: pointer to storage for the CPU view translated physical start
1851 * @len: pointer to storage for the range length
1853 * Convenience function that reads and interprets a specific range out of
1854 * a number of the "ranges" property array.
1856 int fdt_read_range(void *fdt, int node, int n, uint64_t *child_addr,
1857 uint64_t *addr, uint64_t *len)
1859 int pnode = fdt_parent_offset(fdt, node);
1860 const fdt32_t *ranges;
1869 * The "ranges" property is an array of
1870 * { <child address> <parent address> <size in child address space> }
1872 * All 3 elements can span a diffent number of cells. Fetch their size.
1874 pacells = fdt_getprop_u32_default_node(fdt, pnode, 0, "#address-cells", 1);
1875 acells = fdt_getprop_u32_default_node(fdt, node, 0, "#address-cells", 1);
1876 scells = fdt_getprop_u32_default_node(fdt, node, 0, "#size-cells", 1);
1878 /* Now try to get the ranges property */
1879 ranges = fdt_getprop(fdt, node, "ranges", &ranges_len);
1881 return -FDT_ERR_NOTFOUND;
1882 ranges_len /= sizeof(uint32_t);
1884 /* Jump to the n'th entry */
1885 cell = n * (pacells + acells + scells);
1887 /* Read <child address> */
1889 r = fdt_read_prop(ranges, ranges_len, cell, child_addr,
1896 /* Read <parent address> */
1898 *addr = fdt_translate_address(fdt, node, ranges + cell);
1901 /* Read <size in child address space> */
1903 r = fdt_read_prop(ranges, ranges_len, cell, len, scells);
1912 * fdt_setup_simplefb_node - Fill and enable a simplefb node
1914 * @fdt: ptr to device tree
1915 * @node: offset of the simplefb node
1916 * @base_address: framebuffer base address
1917 * @width: width in pixels
1918 * @height: height in pixels
1919 * @stride: bytes per line
1920 * @format: pixel format string
1922 * Convenience function to fill and enable a simplefb node.
1924 int fdt_setup_simplefb_node(void *fdt, int node, u64 base_address, u32 width,
1925 u32 height, u32 stride, const char *format)
1929 int i, addrc, sizec, ret;
1931 fdt_support_default_count_cells(fdt, fdt_parent_offset(fdt, node),
1935 cells[i++] = cpu_to_fdt32(base_address >> 32);
1936 cells[i++] = cpu_to_fdt32(base_address);
1939 cells[i++] = cpu_to_fdt32(height * stride);
1941 ret = fdt_setprop(fdt, node, "reg", cells, sizeof(cells[0]) * i);
1945 snprintf(name, sizeof(name), "framebuffer@%llx", base_address);
1946 ret = fdt_set_name(fdt, node, name);
1950 ret = fdt_setprop_u32(fdt, node, "width", width);
1954 ret = fdt_setprop_u32(fdt, node, "height", height);
1958 ret = fdt_setprop_u32(fdt, node, "stride", stride);
1962 ret = fdt_setprop_string(fdt, node, "format", format);
1966 ret = fdt_setprop_string(fdt, node, "status", "okay");
1974 * Update native-mode in display-timings from display environment variable.
1975 * The node to update are specified by path.
1977 int fdt_fixup_display(void *blob, const char *path, const char *display)
1981 if (!display || !path)
1982 return -FDT_ERR_NOTFOUND;
1984 toff = fdt_path_offset(blob, path);
1986 toff = fdt_subnode_offset(blob, toff, "display-timings");
1990 for (off = fdt_first_subnode(blob, toff);
1992 off = fdt_next_subnode(blob, off)) {
1993 uint32_t h = fdt_get_phandle(blob, off);
1994 debug("%s:0x%x\n", fdt_get_name(blob, off, NULL),
1996 if (strcasecmp(fdt_get_name(blob, off, NULL), display) == 0)
1997 return fdt_setprop_u32(blob, toff, "native-mode", h);
2002 #ifdef CONFIG_OF_LIBFDT_OVERLAY
2004 * fdt_overlay_apply_verbose - Apply an overlay with verbose error reporting
2006 * @fdt: ptr to device tree
2007 * @fdto: ptr to device tree overlay
2009 * Convenience function to apply an overlay and display helpful messages
2010 * in the case of an error
2012 int fdt_overlay_apply_verbose(void *fdt, void *fdto)
2017 err = fdt_path_offset(fdt, "/__symbols__");
2018 has_symbols = err >= 0;
2020 err = fdt_overlay_apply(fdt, fdto);
2022 printf("failed on fdt_overlay_apply(): %s\n",
2025 printf("base fdt does did not have a /__symbols__ node\n");
2026 printf("make sure you've compiled with -@\n");
2034 * fdt_valid() - Check if an FDT is valid. If not, change it to NULL
2036 * @blobp: Pointer to FDT pointer
2037 * Return: 1 if OK, 0 if bad (in which case *blobp is set to NULL)
2039 int fdt_valid(struct fdt_header **blobp)
2041 const void *blob = *blobp;
2045 printf("The address of the fdt is invalid (NULL).\n");
2049 err = fdt_check_header(blob);
2051 return 1; /* valid */
2054 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
2056 * Be more informative on bad version.
2058 if (err == -FDT_ERR_BADVERSION) {
2059 if (fdt_version(blob) <
2060 FDT_FIRST_SUPPORTED_VERSION) {
2061 printf(" - too old, fdt %d < %d",
2063 FDT_FIRST_SUPPORTED_VERSION);
2065 if (fdt_last_comp_version(blob) >
2066 FDT_LAST_SUPPORTED_VERSION) {
2067 printf(" - too new, fdt %d > %d",
2069 FDT_LAST_SUPPORTED_VERSION);