3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
5 * Copyright 2010-2011 Freescale Semiconductor, Inc.
7 * SPDX-License-Identifier: GPL-2.0+
11 #include <stdio_dev.h>
12 #include <linux/ctype.h>
13 #include <linux/types.h>
14 #include <asm/global_data.h>
16 #include <fdt_support.h>
20 * Get cells len in bytes
21 * if #NNNN-cells property is 2 then len is 8
24 static int get_cells_len(const void *fdt, const char *nr_cells_name)
28 cell = fdt_getprop(fdt, 0, nr_cells_name, NULL);
29 if (cell && fdt32_to_cpu(*cell) == 2)
36 * fdt_getprop_u32_default_node - Return a node's property or a default
38 * @fdt: ptr to device tree
39 * @off: offset of node
40 * @cell: cell offset in property
41 * @prop: property name
42 * @dflt: default value if the property isn't found
44 * Convenience function to return a node's property or a default value if
45 * the property doesn't exist.
47 u32 fdt_getprop_u32_default_node(const void *fdt, int off, int cell,
48 const char *prop, const u32 dflt)
53 val = fdt_getprop(fdt, off, prop, &len);
55 /* Check if property exists */
59 /* Check if property is long enough */
60 if (len < ((cell + 1) * sizeof(uint32_t)))
63 return fdt32_to_cpu(*val);
67 * fdt_getprop_u32_default - Find a node and return it's property or a default
69 * @fdt: ptr to device tree
71 * @prop: property name
72 * @dflt: default value if the property isn't found
74 * Convenience function to find a node and return it's property or a
75 * default value if it doesn't exist.
77 u32 fdt_getprop_u32_default(const void *fdt, const char *path,
78 const char *prop, const u32 dflt)
82 off = fdt_path_offset(fdt, path);
86 return fdt_getprop_u32_default_node(fdt, off, 0, prop, dflt);
90 * fdt_find_and_setprop: Find a node and set it's property
92 * @fdt: ptr to device tree
94 * @prop: property name
95 * @val: ptr to new value
96 * @len: length of new property value
97 * @create: flag to create the property if it doesn't exist
99 * Convenience function to directly set a property given the path to the node.
101 int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
102 const void *val, int len, int create)
104 int nodeoff = fdt_path_offset(fdt, node);
109 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, NULL) == NULL))
110 return 0; /* create flag not set; so exit quietly */
112 return fdt_setprop(fdt, nodeoff, prop, val, len);
116 * fdt_find_or_add_subnode - find or possibly add a subnode of a given node
117 * @fdt: pointer to the device tree blob
118 * @parentoffset: structure block offset of a node
119 * @name: name of the subnode to locate
121 * fdt_subnode_offset() finds a subnode of the node with a given name.
122 * If the subnode does not exist, it will be created.
124 static int fdt_find_or_add_subnode(void *fdt, int parentoffset,
129 offset = fdt_subnode_offset(fdt, parentoffset, name);
131 if (offset == -FDT_ERR_NOTFOUND)
132 offset = fdt_add_subnode(fdt, parentoffset, name);
135 printf("%s: %s: %s\n", __func__, name, fdt_strerror(offset));
140 /* rename to CONFIG_OF_STDOUT_PATH ? */
141 #if defined(OF_STDOUT_PATH)
142 static int fdt_fixup_stdout(void *fdt, int chosenoff)
144 return fdt_setprop(fdt, chosenoff, "linux,stdout-path",
145 OF_STDOUT_PATH, strlen(OF_STDOUT_PATH) + 1);
147 #elif defined(CONFIG_OF_STDOUT_VIA_ALIAS) && defined(CONFIG_CONS_INDEX)
148 static void fdt_fill_multisername(char *sername, size_t maxlen)
150 const char *outname = stdio_devices[stdout]->name;
152 if (strcmp(outname, "serial") > 0)
153 strncpy(sername, outname, maxlen);
156 if (strcmp(outname + 1, "serial") > 0)
157 strncpy(sername, outname + 1, maxlen);
160 static int fdt_fixup_stdout(void *fdt, int chosenoff)
164 char sername[9] = { 0 };
167 char tmp[256]; /* long enough */
169 fdt_fill_multisername(sername, sizeof(sername) - 1);
171 sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
173 aliasoff = fdt_path_offset(fdt, "/aliases");
179 path = fdt_getprop(fdt, aliasoff, sername, &len);
185 /* fdt_setprop may break "path" so we copy it to tmp buffer */
186 memcpy(tmp, path, len);
188 err = fdt_setprop(fdt, chosenoff, "linux,stdout-path", tmp, len);
191 printf("WARNING: could not set linux,stdout-path %s.\n",
197 static int fdt_fixup_stdout(void *fdt, int chosenoff)
203 static inline int fdt_setprop_uxx(void *fdt, int nodeoffset, const char *name,
204 uint64_t val, int is_u64)
207 return fdt_setprop_u64(fdt, nodeoffset, name, val);
209 return fdt_setprop_u32(fdt, nodeoffset, name, (uint32_t)val);
213 int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end)
220 /* just return if the size of initrd is zero */
221 if (initrd_start == initrd_end)
224 /* find or create "/chosen" node. */
225 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
229 total = fdt_num_mem_rsv(fdt);
232 * Look for an existing entry and update it. If we don't find
233 * the entry, we will j be the next available slot.
235 for (j = 0; j < total; j++) {
236 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
237 if (addr == initrd_start) {
238 fdt_del_mem_rsv(fdt, j);
243 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start);
245 printf("fdt_initrd: %s\n", fdt_strerror(err));
249 is_u64 = (get_cells_len(fdt, "#address-cells") == 8);
251 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-start",
252 (uint64_t)initrd_start, is_u64);
255 printf("WARNING: could not set linux,initrd-start %s.\n",
260 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-end",
261 (uint64_t)initrd_end, is_u64);
264 printf("WARNING: could not set linux,initrd-end %s.\n",
273 int fdt_chosen(void *fdt)
277 char *str; /* used to set string properties */
279 err = fdt_check_header(fdt);
281 printf("fdt_chosen: %s\n", fdt_strerror(err));
285 /* find or create "/chosen" node. */
286 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
290 str = getenv("bootargs");
292 err = fdt_setprop(fdt, nodeoffset, "bootargs", str,
295 printf("WARNING: could not set bootargs %s.\n",
301 return fdt_fixup_stdout(fdt, nodeoffset);
304 void do_fixup_by_path(void *fdt, const char *path, const char *prop,
305 const void *val, int len, int create)
309 debug("Updating property '%s/%s' = ", path, prop);
310 for (i = 0; i < len; i++)
311 debug(" %.2x", *(u8*)(val+i));
314 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
316 printf("Unable to update property %s:%s, err=%s\n",
317 path, prop, fdt_strerror(rc));
320 void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
323 fdt32_t tmp = cpu_to_fdt32(val);
324 do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create);
327 void do_fixup_by_prop(void *fdt,
328 const char *pname, const void *pval, int plen,
329 const char *prop, const void *val, int len,
335 debug("Updating property '%s' = ", prop);
336 for (i = 0; i < len; i++)
337 debug(" %.2x", *(u8*)(val+i));
340 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
341 while (off != -FDT_ERR_NOTFOUND) {
342 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
343 fdt_setprop(fdt, off, prop, val, len);
344 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
348 void do_fixup_by_prop_u32(void *fdt,
349 const char *pname, const void *pval, int plen,
350 const char *prop, u32 val, int create)
352 fdt32_t tmp = cpu_to_fdt32(val);
353 do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create);
356 void do_fixup_by_compat(void *fdt, const char *compat,
357 const char *prop, const void *val, int len, int create)
362 debug("Updating property '%s' = ", prop);
363 for (i = 0; i < len; i++)
364 debug(" %.2x", *(u8*)(val+i));
367 off = fdt_node_offset_by_compatible(fdt, -1, compat);
368 while (off != -FDT_ERR_NOTFOUND) {
369 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
370 fdt_setprop(fdt, off, prop, val, len);
371 off = fdt_node_offset_by_compatible(fdt, off, compat);
375 void do_fixup_by_compat_u32(void *fdt, const char *compat,
376 const char *prop, u32 val, int create)
378 fdt32_t tmp = cpu_to_fdt32(val);
379 do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create);
383 * fdt_pack_reg - pack address and size array into the "reg"-suitable stream
385 static int fdt_pack_reg(const void *fdt, void *buf, uint64_t *address,
386 uint64_t *size, int n)
389 int address_len = get_cells_len(fdt, "#address-cells");
390 int size_len = get_cells_len(fdt, "#size-cells");
393 for (i = 0; i < n; i++) {
394 if (address_len == 8)
395 *(fdt64_t *)p = cpu_to_fdt64(address[i]);
397 *(fdt32_t *)p = cpu_to_fdt32(address[i]);
401 *(fdt64_t *)p = cpu_to_fdt64(size[i]);
403 *(fdt32_t *)p = cpu_to_fdt32(size[i]);
407 return p - (char *)buf;
410 #ifdef CONFIG_NR_DRAM_BANKS
411 #define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS
413 #define MEMORY_BANKS_MAX 4
415 int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
419 u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */
421 if (banks > MEMORY_BANKS_MAX) {
422 printf("%s: num banks %d exceeds hardcoded limit %d."
423 " Recompile with higher MEMORY_BANKS_MAX?\n",
424 __FUNCTION__, banks, MEMORY_BANKS_MAX);
428 err = fdt_check_header(blob);
430 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
434 /* find or create "/memory" node. */
435 nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
439 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
442 printf("WARNING: could not set %s %s.\n", "device_type",
447 len = fdt_pack_reg(blob, tmp, start, size, banks);
449 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
451 printf("WARNING: could not set %s %s.\n",
452 "reg", fdt_strerror(err));
458 int fdt_fixup_memory(void *blob, u64 start, u64 size)
460 return fdt_fixup_memory_banks(blob, &start, &size, 1);
463 void fdt_fixup_ethernet(void *fdt)
466 char enet[16], *tmp, *end;
469 unsigned char mac_addr[6];
471 node = fdt_path_offset(fdt, "/aliases");
475 if (!getenv("ethaddr")) {
476 if (getenv("usbethaddr")) {
477 strcpy(mac, "usbethaddr");
479 debug("No ethernet MAC Address defined\n");
483 strcpy(mac, "ethaddr");
487 while ((tmp = getenv(mac)) != NULL) {
488 sprintf(enet, "ethernet%d", i);
489 path = fdt_getprop(fdt, node, enet, NULL);
491 debug("No alias for %s\n", enet);
492 sprintf(mac, "eth%daddr", ++i);
496 for (j = 0; j < 6; j++) {
497 mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
499 tmp = (*end) ? end+1 : end;
502 do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0);
503 do_fixup_by_path(fdt, path, "local-mac-address",
506 sprintf(mac, "eth%daddr", ++i);
510 /* Resize the fdt to its actual size + a bit of padding */
511 int fdt_shrink_to_minimum(void *blob)
521 total = fdt_num_mem_rsv(blob);
522 for (i = 0; i < total; i++) {
523 fdt_get_mem_rsv(blob, i, &addr, &size);
524 if (addr == (uintptr_t)blob) {
525 fdt_del_mem_rsv(blob, i);
531 * Calculate the actual size of the fdt
532 * plus the size needed for 5 fdt_add_mem_rsv, one
533 * for the fdt itself and 4 for a possible initrd
534 * ((initrd-start + initrd-end) * 2 (name & value))
536 actualsize = fdt_off_dt_strings(blob) +
537 fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
539 /* Make it so the fdt ends on a page boundary */
540 actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
541 actualsize = actualsize - ((uintptr_t)blob & 0xfff);
543 /* Change the fdt header to reflect the correct size */
544 fdt_set_totalsize(blob, actualsize);
546 /* Add the new reservation */
547 ret = fdt_add_mem_rsv(blob, (uintptr_t)blob, actualsize);
555 #define CONFIG_SYS_PCI_NR_INBOUND_WIN 4
557 #define FDT_PCI_PREFETCH (0x40000000)
558 #define FDT_PCI_MEM32 (0x02000000)
559 #define FDT_PCI_IO (0x01000000)
560 #define FDT_PCI_MEM64 (0x03000000)
562 int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
564 int addrcell, sizecell, len, r;
566 /* sized based on pci addr cells, size-cells, & address-cells */
567 u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN];
569 addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
570 sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
572 dma_range = &dma_ranges[0];
573 for (r = 0; r < hose->region_count; r++) {
574 u64 bus_start, phys_start, size;
576 /* skip if !PCI_REGION_SYS_MEMORY */
577 if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
580 bus_start = (u64)hose->regions[r].bus_start;
581 phys_start = (u64)hose->regions[r].phys_start;
582 size = (u64)hose->regions[r].size;
585 if (size >= 0x100000000ull)
586 dma_range[0] |= FDT_PCI_MEM64;
588 dma_range[0] |= FDT_PCI_MEM32;
589 if (hose->regions[r].flags & PCI_REGION_PREFETCH)
590 dma_range[0] |= FDT_PCI_PREFETCH;
591 #ifdef CONFIG_SYS_PCI_64BIT
592 dma_range[1] = bus_start >> 32;
596 dma_range[2] = bus_start & 0xffffffff;
599 dma_range[3] = phys_start >> 32;
600 dma_range[4] = phys_start & 0xffffffff;
602 dma_range[3] = phys_start & 0xffffffff;
606 dma_range[3 + addrcell + 0] = size >> 32;
607 dma_range[3 + addrcell + 1] = size & 0xffffffff;
609 dma_range[3 + addrcell + 0] = size & 0xffffffff;
612 dma_range += (3 + addrcell + sizecell);
615 len = dma_range - &dma_ranges[0];
617 fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
623 #ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE
625 * Provide a weak default function to return the flash bank size.
626 * There might be multiple non-identical flash chips connected to one
627 * chip-select, so we need to pass an index as well.
629 u32 __flash_get_bank_size(int cs, int idx)
631 extern flash_info_t flash_info[];
634 * As default, a simple 1:1 mapping is provided. Boards with
635 * a different mapping need to supply a board specific mapping
638 return flash_info[cs].size;
640 u32 flash_get_bank_size(int cs, int idx)
641 __attribute__((weak, alias("__flash_get_bank_size")));
644 * This function can be used to update the size in the "reg" property
645 * of all NOR FLASH device nodes. This is necessary for boards with
646 * non-fixed NOR FLASH sizes.
648 int fdt_fixup_nor_flash_size(void *blob)
650 char compat[][16] = { "cfi-flash", "jedec-flash" };
653 struct fdt_property *prop;
657 for (i = 0; i < 2; i++) {
658 off = fdt_node_offset_by_compatible(blob, -1, compat[i]);
659 while (off != -FDT_ERR_NOTFOUND) {
663 * Found one compatible node, so fixup the size
664 * int its reg properties
666 prop = fdt_get_property_w(blob, off, "reg", &len);
668 int tuple_size = 3 * sizeof(reg);
671 * There might be multiple reg-tuples,
672 * so loop through them all
674 reg = reg2 = (u32 *)&prop->data[0];
675 for (idx = 0; idx < (len / tuple_size); idx++) {
677 * Update size in reg property
679 reg[2] = flash_get_bank_size(reg[0],
683 * Point to next reg tuple
688 fdt_setprop(blob, off, "reg", reg2, len);
691 /* Move to next compatible node */
692 off = fdt_node_offset_by_compatible(blob, off,
701 int fdt_increase_size(void *fdt, int add_len)
705 newlen = fdt_totalsize(fdt) + add_len;
707 /* Open in place with a new len */
708 return fdt_open_into(fdt, fdt, newlen);
711 #ifdef CONFIG_FDT_FIXUP_PARTITIONS
712 #include <jffs2/load_kernel.h>
713 #include <mtd_node.h>
720 int fdt_del_subnodes(const void *blob, int parent_offset)
725 for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
726 (off >= 0) && (ndepth > 0);
727 off = fdt_next_node(blob, off, &ndepth)) {
729 debug("delete %s: offset: %x\n",
730 fdt_get_name(blob, off, 0), off);
731 ret = fdt_del_node((void *)blob, off);
733 printf("Can't delete node: %s\n",
745 int fdt_del_partitions(void *blob, int parent_offset)
752 off = fdt_next_node(blob, parent_offset, &ndepth);
753 if (off > 0 && ndepth == 1) {
754 prop = fdt_getprop(blob, off, "label", NULL);
757 * Could not find label property, nand {}; node?
758 * Check subnode, delete partitions there if any.
760 return fdt_del_partitions(blob, off);
762 ret = fdt_del_subnodes(blob, parent_offset);
764 printf("Can't remove subnodes: %s\n",
773 int fdt_node_set_part_info(void *blob, int parent_offset,
774 struct mtd_device *dev)
776 struct list_head *pentry;
777 struct part_info *part;
778 struct reg_cell cell;
783 ret = fdt_del_partitions(blob, parent_offset);
788 * Check if it is nand {}; subnode, adjust
789 * the offset in this case
791 off = fdt_next_node(blob, parent_offset, &ndepth);
792 if (off > 0 && ndepth == 1)
796 list_for_each_prev(pentry, &dev->parts) {
799 part = list_entry(pentry, struct part_info, link);
801 debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
802 part_num, part->name, part->size,
803 part->offset, part->mask_flags);
805 sprintf(buf, "partition@%llx", part->offset);
807 ret = fdt_add_subnode(blob, parent_offset, buf);
808 if (ret == -FDT_ERR_NOSPACE) {
809 ret = fdt_increase_size(blob, 512);
814 } else if (ret < 0) {
815 printf("Can't add partition node: %s\n",
821 /* Check MTD_WRITEABLE_CMD flag */
822 if (part->mask_flags & 1) {
824 ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
825 if (ret == -FDT_ERR_NOSPACE) {
826 ret = fdt_increase_size(blob, 512);
835 cell.r0 = cpu_to_fdt32(part->offset);
836 cell.r1 = cpu_to_fdt32(part->size);
838 ret = fdt_setprop(blob, newoff, "reg", &cell, sizeof(cell));
839 if (ret == -FDT_ERR_NOSPACE) {
840 ret = fdt_increase_size(blob, 512);
849 ret = fdt_setprop_string(blob, newoff, "label", part->name);
850 if (ret == -FDT_ERR_NOSPACE) {
851 ret = fdt_increase_size(blob, 512);
863 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
866 printf("Can't add property: %s\n", fdt_strerror(ret));
871 * Update partitions in nor/nand nodes using info from
872 * mtdparts environment variable. The nodes to update are
873 * specified by node_info structure which contains mtd device
874 * type and compatible string: E. g. the board code in
875 * ft_board_setup() could use:
877 * struct node_info nodes[] = {
878 * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
879 * { "cfi-flash", MTD_DEV_TYPE_NOR, },
882 * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
884 void fdt_fixup_mtdparts(void *blob, void *node_info, int node_info_size)
886 struct node_info *ni = node_info;
887 struct mtd_device *dev;
892 parts = getenv("mtdparts");
896 if (mtdparts_init() != 0)
899 for (i = 0; i < node_info_size; i++) {
901 noff = fdt_node_offset_by_compatible(blob, -1, ni[i].compat);
902 while (noff != -FDT_ERR_NOTFOUND) {
903 debug("%s: %s, mtd dev type %d\n",
904 fdt_get_name(blob, noff, 0),
905 ni[i].compat, ni[i].type);
906 dev = device_find(ni[i].type, idx++);
908 if (fdt_node_set_part_info(blob, noff, dev))
909 return; /* return on error */
912 /* Jump to next flash node */
913 noff = fdt_node_offset_by_compatible(blob, noff,
920 void fdt_del_node_and_alias(void *blob, const char *alias)
922 int off = fdt_path_offset(blob, alias);
927 fdt_del_node(blob, off);
929 off = fdt_path_offset(blob, "/aliases");
930 fdt_delprop(blob, off, alias);
933 /* Helper to read a big number; size is in cells (not bytes) */
934 static inline u64 of_read_number(const fdt32_t *cell, int size)
938 r = (r << 32) | fdt32_to_cpu(*(cell++));
944 /* Max address size we deal with */
945 #define OF_MAX_ADDR_CELLS 4
946 #define OF_BAD_ADDR ((u64)-1)
947 #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
952 static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
956 printf(" %08x", *(addr++));
960 static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
963 /* Callbacks for bus specific translators */
966 const char *addresses;
967 void (*count_cells)(void *blob, int parentoffset,
968 int *addrc, int *sizec);
969 u64 (*map)(fdt32_t *addr, const fdt32_t *range,
970 int na, int ns, int pna);
971 int (*translate)(fdt32_t *addr, u64 offset, int na);
974 /* Default translator (generic bus) */
975 static void of_bus_default_count_cells(void *blob, int parentoffset,
976 int *addrc, int *sizec)
981 prop = fdt_getprop(blob, parentoffset, "#address-cells", NULL);
983 *addrc = be32_to_cpup(prop);
989 prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
991 *sizec = be32_to_cpup(prop);
997 static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
998 int na, int ns, int pna)
1002 cp = of_read_number(range, na);
1003 s = of_read_number(range + na + pna, ns);
1004 da = of_read_number(addr, na);
1006 debug("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
1009 if (da < cp || da >= (cp + s))
1014 static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
1016 u64 a = of_read_number(addr, na);
1017 memset(addr, 0, na * 4);
1020 addr[na - 2] = cpu_to_fdt32(a >> 32);
1021 addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
1026 /* Array of bus specific translators */
1027 static struct of_bus of_busses[] = {
1032 .count_cells = of_bus_default_count_cells,
1033 .map = of_bus_default_map,
1034 .translate = of_bus_default_translate,
1038 static int of_translate_one(void * blob, int parent, struct of_bus *bus,
1039 struct of_bus *pbus, fdt32_t *addr,
1040 int na, int ns, int pna, const char *rprop)
1042 const fdt32_t *ranges;
1045 u64 offset = OF_BAD_ADDR;
1047 /* Normally, an absence of a "ranges" property means we are
1048 * crossing a non-translatable boundary, and thus the addresses
1049 * below the current not cannot be converted to CPU physical ones.
1050 * Unfortunately, while this is very clear in the spec, it's not
1051 * what Apple understood, and they do have things like /uni-n or
1052 * /ht nodes with no "ranges" property and a lot of perfectly
1053 * useable mapped devices below them. Thus we treat the absence of
1054 * "ranges" as equivalent to an empty "ranges" property which means
1055 * a 1:1 translation at that level. It's up to the caller not to try
1056 * to translate addresses that aren't supposed to be translated in
1057 * the first place. --BenH.
1059 ranges = fdt_getprop(blob, parent, rprop, &rlen);
1060 if (ranges == NULL || rlen == 0) {
1061 offset = of_read_number(addr, na);
1062 memset(addr, 0, pna * 4);
1063 debug("OF: no ranges, 1:1 translation\n");
1067 debug("OF: walking ranges...\n");
1069 /* Now walk through the ranges */
1071 rone = na + pna + ns;
1072 for (; rlen >= rone; rlen -= rone, ranges += rone) {
1073 offset = bus->map(addr, ranges, na, ns, pna);
1074 if (offset != OF_BAD_ADDR)
1077 if (offset == OF_BAD_ADDR) {
1078 debug("OF: not found !\n");
1081 memcpy(addr, ranges + na, 4 * pna);
1084 of_dump_addr("OF: parent translation for:", addr, pna);
1085 debug("OF: with offset: "PRu64"\n", offset);
1087 /* Translate it into parent bus space */
1088 return pbus->translate(addr, offset, pna);
1092 * Translate an address from the device-tree into a CPU physical address,
1093 * this walks up the tree and applies the various bus mappings on the
1096 * Note: We consider that crossing any level with #size-cells == 0 to mean
1097 * that translation is impossible (that is we are not dealing with a value
1098 * that can be mapped to a cpu physical address). This is not really specified
1099 * that way, but this is traditionally the way IBM at least do things
1101 static u64 __of_translate_address(void *blob, int node_offset, const fdt32_t *in_addr,
1105 struct of_bus *bus, *pbus;
1106 fdt32_t addr[OF_MAX_ADDR_CELLS];
1107 int na, ns, pna, pns;
1108 u64 result = OF_BAD_ADDR;
1110 debug("OF: ** translation for device %s **\n",
1111 fdt_get_name(blob, node_offset, NULL));
1113 /* Get parent & match bus type */
1114 parent = fdt_parent_offset(blob, node_offset);
1117 bus = &of_busses[0];
1119 /* Cound address cells & copy address locally */
1120 bus->count_cells(blob, parent, &na, &ns);
1121 if (!OF_CHECK_COUNTS(na, ns)) {
1122 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1123 fdt_get_name(blob, node_offset, NULL));
1126 memcpy(addr, in_addr, na * 4);
1128 debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
1129 bus->name, na, ns, fdt_get_name(blob, parent, NULL));
1130 of_dump_addr("OF: translating address:", addr, na);
1134 /* Switch to parent bus */
1135 node_offset = parent;
1136 parent = fdt_parent_offset(blob, node_offset);
1138 /* If root, we have finished */
1140 debug("OF: reached root node\n");
1141 result = of_read_number(addr, na);
1145 /* Get new parent bus and counts */
1146 pbus = &of_busses[0];
1147 pbus->count_cells(blob, parent, &pna, &pns);
1148 if (!OF_CHECK_COUNTS(pna, pns)) {
1149 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1150 fdt_get_name(blob, node_offset, NULL));
1154 debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
1155 pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
1157 /* Apply bus translation */
1158 if (of_translate_one(blob, node_offset, bus, pbus,
1159 addr, na, ns, pna, rprop))
1162 /* Complete the move up one level */
1167 of_dump_addr("OF: one level translation:", addr, na);
1174 u64 fdt_translate_address(void *blob, int node_offset, const fdt32_t *in_addr)
1176 return __of_translate_address(blob, node_offset, in_addr, "ranges");
1180 * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and
1181 * who's reg property matches a physical cpu address
1183 * @blob: ptr to device tree
1184 * @compat: compatiable string to match
1185 * @compat_off: property name
1188 int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
1189 phys_addr_t compat_off)
1191 int len, off = fdt_node_offset_by_compatible(blob, -1, compat);
1192 while (off != -FDT_ERR_NOTFOUND) {
1193 const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
1195 if (compat_off == fdt_translate_address(blob, off, reg))
1198 off = fdt_node_offset_by_compatible(blob, off, compat);
1201 return -FDT_ERR_NOTFOUND;
1205 * fdt_alloc_phandle: Return next free phandle value
1207 * @blob: ptr to device tree
1209 int fdt_alloc_phandle(void *blob)
1211 int offset, phandle = 0;
1213 for (offset = fdt_next_node(blob, -1, NULL); offset >= 0;
1214 offset = fdt_next_node(blob, offset, NULL)) {
1215 phandle = max(phandle, fdt_get_phandle(blob, offset));
1222 * fdt_set_phandle: Create a phandle property for the given node
1224 * @fdt: ptr to device tree
1225 * @nodeoffset: node to update
1226 * @phandle: phandle value to set (must be unique)
1228 int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
1233 int off = fdt_node_offset_by_phandle(fdt, phandle);
1235 if ((off >= 0) && (off != nodeoffset)) {
1238 fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
1239 printf("Trying to update node %s with phandle %u ",
1242 fdt_get_path(fdt, off, buf, sizeof(buf));
1243 printf("that already exists in node %s.\n", buf);
1244 return -FDT_ERR_BADPHANDLE;
1248 ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
1253 * For now, also set the deprecated "linux,phandle" property, so that we
1254 * don't break older kernels.
1256 ret = fdt_setprop_cell(fdt, nodeoffset, "linux,phandle", phandle);
1262 * fdt_create_phandle: Create a phandle property for the given node
1264 * @fdt: ptr to device tree
1265 * @nodeoffset: node to update
1267 unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
1269 /* see if there is a phandle already */
1270 int phandle = fdt_get_phandle(fdt, nodeoffset);
1272 /* if we got 0, means no phandle so create one */
1276 phandle = fdt_alloc_phandle(fdt);
1277 ret = fdt_set_phandle(fdt, nodeoffset, phandle);
1279 printf("Can't set phandle %u: %s\n", phandle,
1289 * fdt_set_node_status: Set status for the given node
1291 * @fdt: ptr to device tree
1292 * @nodeoffset: node to update
1293 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1294 * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1295 * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1297 int fdt_set_node_status(void *fdt, int nodeoffset,
1298 enum fdt_status status, unsigned int error_code)
1307 case FDT_STATUS_OKAY:
1308 ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
1310 case FDT_STATUS_DISABLED:
1311 ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
1313 case FDT_STATUS_FAIL:
1314 ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
1316 case FDT_STATUS_FAIL_ERROR_CODE:
1317 sprintf(buf, "fail-%d", error_code);
1318 ret = fdt_setprop_string(fdt, nodeoffset, "status", buf);
1321 printf("Invalid fdt status: %x\n", status);
1330 * fdt_set_status_by_alias: Set status for the given node given an alias
1332 * @fdt: ptr to device tree
1333 * @alias: alias of node to update
1334 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1335 * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1336 * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1338 int fdt_set_status_by_alias(void *fdt, const char* alias,
1339 enum fdt_status status, unsigned int error_code)
1341 int offset = fdt_path_offset(fdt, alias);
1343 return fdt_set_node_status(fdt, offset, status, error_code);
1346 #if defined(CONFIG_VIDEO) || defined(CONFIG_LCD)
1347 int fdt_add_edid(void *blob, const char *compat, unsigned char *edid_buf)
1352 noff = fdt_node_offset_by_compatible(blob, -1, compat);
1353 if (noff != -FDT_ERR_NOTFOUND) {
1354 debug("%s: %s\n", fdt_get_name(blob, noff, 0), compat);
1356 ret = fdt_setprop(blob, noff, "edid", edid_buf, 128);
1357 if (ret == -FDT_ERR_NOSPACE) {
1358 ret = fdt_increase_size(blob, 512);
1363 } else if (ret < 0) {
1364 printf("Can't add property: %s\n", fdt_strerror(ret));
1370 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
1376 * Verify the physical address of device tree node for a given alias
1378 * This function locates the device tree node of a given alias, and then
1379 * verifies that the physical address of that device matches the given
1380 * parameter. It displays a message if there is a mismatch.
1382 * Returns 1 on success, 0 on failure
1384 int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
1391 path = fdt_getprop(fdt, anode, alias, NULL);
1393 /* If there's no such alias, then it's not a failure */
1397 node = fdt_path_offset(fdt, path);
1399 printf("Warning: device tree alias '%s' points to invalid "
1400 "node %s.\n", alias, path);
1404 reg = fdt_getprop(fdt, node, "reg", &len);
1406 printf("Warning: device tree node '%s' has no address.\n",
1411 dt_addr = fdt_translate_address(fdt, node, reg);
1412 if (addr != dt_addr) {
1413 printf("Warning: U-Boot configured device %s at address %llx,\n"
1414 " but the device tree has it address %llx.\n",
1415 alias, addr, dt_addr);
1423 * Returns the base address of an SOC or PCI node
1425 u64 fdt_get_base_address(void *fdt, int node)
1429 const fdt32_t *prop;
1431 prop = fdt_getprop(fdt, node, "#address-cells", &size);
1432 if (prop && size == 4)
1433 naddr = be32_to_cpup(prop);
1437 prop = fdt_getprop(fdt, node, "ranges", &size);
1439 return prop ? fdt_translate_address(fdt, node, prop + naddr) : 0;
1443 * Read a property of size <prop_len>. Currently only supports 1 or 2 cells.
1445 static int fdt_read_prop(const fdt32_t *prop, int prop_len, int cell_off,
1446 uint64_t *val, int cells)
1448 const fdt32_t *prop32 = &prop[cell_off];
1449 const fdt64_t *prop64 = (const fdt64_t *)&prop[cell_off];
1451 if ((cell_off + cells) > prop_len)
1452 return -FDT_ERR_NOSPACE;
1456 *val = fdt32_to_cpu(*prop32);
1459 *val = fdt64_to_cpu(*prop64);
1462 return -FDT_ERR_NOSPACE;
1469 * fdt_read_range - Read a node's n'th range property
1471 * @fdt: ptr to device tree
1472 * @node: offset of node
1474 * @child_addr: pointer to storage for the "child address" field
1475 * @addr: pointer to storage for the CPU view translated physical start
1476 * @len: pointer to storage for the range length
1478 * Convenience function that reads and interprets a specific range out of
1479 * a number of the "ranges" property array.
1481 int fdt_read_range(void *fdt, int node, int n, uint64_t *child_addr,
1482 uint64_t *addr, uint64_t *len)
1484 int pnode = fdt_parent_offset(fdt, node);
1485 const fdt32_t *ranges;
1494 * The "ranges" property is an array of
1495 * { <child address> <parent address> <size in child address space> }
1497 * All 3 elements can span a diffent number of cells. Fetch their size.
1499 pacells = fdt_getprop_u32_default_node(fdt, pnode, 0, "#address-cells", 1);
1500 acells = fdt_getprop_u32_default_node(fdt, node, 0, "#address-cells", 1);
1501 scells = fdt_getprop_u32_default_node(fdt, node, 0, "#size-cells", 1);
1503 /* Now try to get the ranges property */
1504 ranges = fdt_getprop(fdt, node, "ranges", &ranges_len);
1506 return -FDT_ERR_NOTFOUND;
1507 ranges_len /= sizeof(uint32_t);
1509 /* Jump to the n'th entry */
1510 cell = n * (pacells + acells + scells);
1512 /* Read <child address> */
1514 r = fdt_read_prop(ranges, ranges_len, cell, child_addr,
1521 /* Read <parent address> */
1523 *addr = fdt_translate_address(fdt, node, ranges + cell);
1526 /* Read <size in child address space> */
1528 r = fdt_read_prop(ranges, ranges_len, cell, len, scells);