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 * Global data (for the gd->bd)
22 DECLARE_GLOBAL_DATA_PTR;
25 * fdt_getprop_u32_default - Find a node and return it's property or a default
27 * @fdt: ptr to device tree
29 * @prop: property name
30 * @dflt: default value if the property isn't found
32 * Convenience function to find a node and return it's property or a
33 * default value if it doesn't exist.
35 u32 fdt_getprop_u32_default(const void *fdt, const char *path,
36 const char *prop, const u32 dflt)
41 off = fdt_path_offset(fdt, path);
45 val = fdt_getprop(fdt, off, prop, NULL);
47 return fdt32_to_cpu(*val);
53 * fdt_find_and_setprop: Find a node and set it's property
55 * @fdt: ptr to device tree
57 * @prop: property name
58 * @val: ptr to new value
59 * @len: length of new property value
60 * @create: flag to create the property if it doesn't exist
62 * Convenience function to directly set a property given the path to the node.
64 int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
65 const void *val, int len, int create)
67 int nodeoff = fdt_path_offset(fdt, node);
72 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, NULL) == NULL))
73 return 0; /* create flag not set; so exit quietly */
75 return fdt_setprop(fdt, nodeoff, prop, val, len);
78 #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
80 #ifdef CONFIG_CONS_INDEX
81 static void fdt_fill_multisername(char *sername, size_t maxlen)
83 const char *outname = stdio_devices[stdout]->name;
85 if (strcmp(outname, "serial") > 0)
86 strncpy(sername, outname, maxlen);
89 if (strcmp(outname + 1, "serial") > 0)
90 strncpy(sername, outname + 1, maxlen);
94 static int fdt_fixup_stdout(void *fdt, int chosenoff)
97 #ifdef CONFIG_CONS_INDEX
99 char sername[9] = { 0 };
102 fdt_fill_multisername(sername, sizeof(sername) - 1);
104 sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
106 err = node = fdt_path_offset(fdt, "/aliases");
109 path = fdt_getprop(fdt, node, sername, &len);
111 char *p = malloc(len);
112 err = -FDT_ERR_NOSPACE;
114 memcpy(p, path, len);
115 err = fdt_setprop(fdt, chosenoff,
116 "linux,stdout-path", p, len);
125 printf("WARNING: could not set linux,stdout-path %s.\n",
132 int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end, int force)
140 /* Find the "chosen" node. */
141 nodeoffset = fdt_path_offset (fdt, "/chosen");
143 /* If there is no "chosen" node in the blob return */
144 if (nodeoffset < 0) {
145 printf("fdt_initrd: %s\n", fdt_strerror(nodeoffset));
149 /* just return if initrd_start/end aren't valid */
150 if ((initrd_start == 0) || (initrd_end == 0))
153 total = fdt_num_mem_rsv(fdt);
156 * Look for an existing entry and update it. If we don't find
157 * the entry, we will j be the next available slot.
159 for (j = 0; j < total; j++) {
160 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
161 if (addr == initrd_start) {
162 fdt_del_mem_rsv(fdt, j);
167 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start);
169 printf("fdt_initrd: %s\n", fdt_strerror(err));
173 path = fdt_getprop(fdt, nodeoffset, "linux,initrd-start", NULL);
174 if ((path == NULL) || force) {
175 tmp = cpu_to_fdt32(initrd_start);
176 err = fdt_setprop(fdt, nodeoffset,
177 "linux,initrd-start", &tmp, sizeof(tmp));
180 "could not set linux,initrd-start %s.\n",
184 tmp = cpu_to_fdt32(initrd_end);
185 err = fdt_setprop(fdt, nodeoffset,
186 "linux,initrd-end", &tmp, sizeof(tmp));
188 printf("WARNING: could not set linux,initrd-end %s.\n",
198 int fdt_chosen(void *fdt, int force)
202 char *str; /* used to set string properties */
205 err = fdt_check_header(fdt);
207 printf("fdt_chosen: %s\n", fdt_strerror(err));
212 * Find the "chosen" node.
214 nodeoffset = fdt_path_offset (fdt, "/chosen");
217 * If there is no "chosen" node in the blob, create it.
219 if (nodeoffset < 0) {
221 * Create a new node "/chosen" (offset 0 is root level)
223 nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
224 if (nodeoffset < 0) {
225 printf("WARNING: could not create /chosen %s.\n",
226 fdt_strerror(nodeoffset));
232 * Create /chosen properites that don't exist in the fdt.
233 * If the property exists, update it only if the "force" parameter
236 str = getenv("bootargs");
238 path = fdt_getprop(fdt, nodeoffset, "bootargs", NULL);
239 if ((path == NULL) || force) {
240 err = fdt_setprop(fdt, nodeoffset,
241 "bootargs", str, strlen(str)+1);
243 printf("WARNING: could not set bootargs %s.\n",
248 #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
249 path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
250 if ((path == NULL) || force)
251 err = fdt_fixup_stdout(fdt, nodeoffset);
254 #ifdef OF_STDOUT_PATH
255 path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
256 if ((path == NULL) || force) {
257 err = fdt_setprop(fdt, nodeoffset,
258 "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
260 printf("WARNING: could not set linux,stdout-path %s.\n",
268 void do_fixup_by_path(void *fdt, const char *path, const char *prop,
269 const void *val, int len, int create)
273 debug("Updating property '%s/%s' = ", path, prop);
274 for (i = 0; i < len; i++)
275 debug(" %.2x", *(u8*)(val+i));
278 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
280 printf("Unable to update property %s:%s, err=%s\n",
281 path, prop, fdt_strerror(rc));
284 void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
287 fdt32_t tmp = cpu_to_fdt32(val);
288 do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create);
291 void do_fixup_by_prop(void *fdt,
292 const char *pname, const void *pval, int plen,
293 const char *prop, const void *val, int len,
299 debug("Updating property '%s' = ", prop);
300 for (i = 0; i < len; i++)
301 debug(" %.2x", *(u8*)(val+i));
304 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
305 while (off != -FDT_ERR_NOTFOUND) {
306 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
307 fdt_setprop(fdt, off, prop, val, len);
308 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
312 void do_fixup_by_prop_u32(void *fdt,
313 const char *pname, const void *pval, int plen,
314 const char *prop, u32 val, int create)
316 fdt32_t tmp = cpu_to_fdt32(val);
317 do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create);
320 void do_fixup_by_compat(void *fdt, const char *compat,
321 const char *prop, const void *val, int len, int create)
326 debug("Updating property '%s' = ", prop);
327 for (i = 0; i < len; i++)
328 debug(" %.2x", *(u8*)(val+i));
331 off = fdt_node_offset_by_compatible(fdt, -1, compat);
332 while (off != -FDT_ERR_NOTFOUND) {
333 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
334 fdt_setprop(fdt, off, prop, val, len);
335 off = fdt_node_offset_by_compatible(fdt, off, compat);
339 void do_fixup_by_compat_u32(void *fdt, const char *compat,
340 const char *prop, u32 val, int create)
342 fdt32_t tmp = cpu_to_fdt32(val);
343 do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create);
347 * Get cells len in bytes
348 * if #NNNN-cells property is 2 then len is 8
351 static int get_cells_len(void *blob, char *nr_cells_name)
355 cell = fdt_getprop(blob, 0, nr_cells_name, NULL);
356 if (cell && fdt32_to_cpu(*cell) == 2)
363 * Write a 4 or 8 byte big endian cell
365 static void write_cell(u8 *addr, u64 val, int size)
367 int shift = (size - 1) * 8;
369 *addr++ = (val >> shift) & 0xff;
374 #ifdef CONFIG_NR_DRAM_BANKS
375 #define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS
377 #define MEMORY_BANKS_MAX 4
379 int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
382 int addr_cell_len, size_cell_len, len;
383 u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */
386 if (banks > MEMORY_BANKS_MAX) {
387 printf("%s: num banks %d exceeds hardcoded limit %d."
388 " Recompile with higher MEMORY_BANKS_MAX?\n",
389 __FUNCTION__, banks, MEMORY_BANKS_MAX);
393 err = fdt_check_header(blob);
395 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
399 /* update, or add and update /memory node */
400 nodeoffset = fdt_path_offset(blob, "/memory");
401 if (nodeoffset < 0) {
402 nodeoffset = fdt_add_subnode(blob, 0, "memory");
404 printf("WARNING: could not create /memory: %s.\n",
405 fdt_strerror(nodeoffset));
408 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
411 printf("WARNING: could not set %s %s.\n", "device_type",
416 addr_cell_len = get_cells_len(blob, "#address-cells");
417 size_cell_len = get_cells_len(blob, "#size-cells");
419 for (bank = 0, len = 0; bank < banks; bank++) {
420 write_cell(tmp + len, start[bank], addr_cell_len);
421 len += addr_cell_len;
423 write_cell(tmp + len, size[bank], size_cell_len);
424 len += size_cell_len;
427 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
429 printf("WARNING: could not set %s %s.\n",
430 "reg", fdt_strerror(err));
436 int fdt_fixup_memory(void *blob, u64 start, u64 size)
438 return fdt_fixup_memory_banks(blob, &start, &size, 1);
441 void fdt_fixup_ethernet(void *fdt)
444 char enet[16], *tmp, *end;
447 unsigned char mac_addr[6];
449 node = fdt_path_offset(fdt, "/aliases");
454 strcpy(mac, "ethaddr");
455 while ((tmp = getenv(mac)) != NULL) {
456 sprintf(enet, "ethernet%d", i);
457 path = fdt_getprop(fdt, node, enet, NULL);
459 debug("No alias for %s\n", enet);
460 sprintf(mac, "eth%daddr", ++i);
464 for (j = 0; j < 6; j++) {
465 mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
467 tmp = (*end) ? end+1 : end;
470 do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0);
471 do_fixup_by_path(fdt, path, "local-mac-address",
474 sprintf(mac, "eth%daddr", ++i);
478 /* Resize the fdt to its actual size + a bit of padding */
479 int fdt_resize(void *blob)
489 total = fdt_num_mem_rsv(blob);
490 for (i = 0; i < total; i++) {
491 fdt_get_mem_rsv(blob, i, &addr, &size);
492 if (addr == (uintptr_t)blob) {
493 fdt_del_mem_rsv(blob, i);
499 * Calculate the actual size of the fdt
500 * plus the size needed for 5 fdt_add_mem_rsv, one
501 * for the fdt itself and 4 for a possible initrd
502 * ((initrd-start + initrd-end) * 2 (name & value))
504 actualsize = fdt_off_dt_strings(blob) +
505 fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
507 /* Make it so the fdt ends on a page boundary */
508 actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
509 actualsize = actualsize - ((uintptr_t)blob & 0xfff);
511 /* Change the fdt header to reflect the correct size */
512 fdt_set_totalsize(blob, actualsize);
514 /* Add the new reservation */
515 ret = fdt_add_mem_rsv(blob, (uintptr_t)blob, actualsize);
523 #define CONFIG_SYS_PCI_NR_INBOUND_WIN 4
525 #define FDT_PCI_PREFETCH (0x40000000)
526 #define FDT_PCI_MEM32 (0x02000000)
527 #define FDT_PCI_IO (0x01000000)
528 #define FDT_PCI_MEM64 (0x03000000)
530 int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
532 int addrcell, sizecell, len, r;
534 /* sized based on pci addr cells, size-cells, & address-cells */
535 u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN];
537 addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
538 sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
540 dma_range = &dma_ranges[0];
541 for (r = 0; r < hose->region_count; r++) {
542 u64 bus_start, phys_start, size;
544 /* skip if !PCI_REGION_SYS_MEMORY */
545 if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
548 bus_start = (u64)hose->regions[r].bus_start;
549 phys_start = (u64)hose->regions[r].phys_start;
550 size = (u64)hose->regions[r].size;
553 if (size >= 0x100000000ull)
554 dma_range[0] |= FDT_PCI_MEM64;
556 dma_range[0] |= FDT_PCI_MEM32;
557 if (hose->regions[r].flags & PCI_REGION_PREFETCH)
558 dma_range[0] |= FDT_PCI_PREFETCH;
559 #ifdef CONFIG_SYS_PCI_64BIT
560 dma_range[1] = bus_start >> 32;
564 dma_range[2] = bus_start & 0xffffffff;
567 dma_range[3] = phys_start >> 32;
568 dma_range[4] = phys_start & 0xffffffff;
570 dma_range[3] = phys_start & 0xffffffff;
574 dma_range[3 + addrcell + 0] = size >> 32;
575 dma_range[3 + addrcell + 1] = size & 0xffffffff;
577 dma_range[3 + addrcell + 0] = size & 0xffffffff;
580 dma_range += (3 + addrcell + sizecell);
583 len = dma_range - &dma_ranges[0];
585 fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
591 #ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE
593 * Provide a weak default function to return the flash bank size.
594 * There might be multiple non-identical flash chips connected to one
595 * chip-select, so we need to pass an index as well.
597 u32 __flash_get_bank_size(int cs, int idx)
599 extern flash_info_t flash_info[];
602 * As default, a simple 1:1 mapping is provided. Boards with
603 * a different mapping need to supply a board specific mapping
606 return flash_info[cs].size;
608 u32 flash_get_bank_size(int cs, int idx)
609 __attribute__((weak, alias("__flash_get_bank_size")));
612 * This function can be used to update the size in the "reg" property
613 * of all NOR FLASH device nodes. This is necessary for boards with
614 * non-fixed NOR FLASH sizes.
616 int fdt_fixup_nor_flash_size(void *blob)
618 char compat[][16] = { "cfi-flash", "jedec-flash" };
621 struct fdt_property *prop;
625 for (i = 0; i < 2; i++) {
626 off = fdt_node_offset_by_compatible(blob, -1, compat[i]);
627 while (off != -FDT_ERR_NOTFOUND) {
631 * Found one compatible node, so fixup the size
632 * int its reg properties
634 prop = fdt_get_property_w(blob, off, "reg", &len);
636 int tuple_size = 3 * sizeof(reg);
639 * There might be multiple reg-tuples,
640 * so loop through them all
642 reg = reg2 = (u32 *)&prop->data[0];
643 for (idx = 0; idx < (len / tuple_size); idx++) {
645 * Update size in reg property
647 reg[2] = flash_get_bank_size(reg[0],
651 * Point to next reg tuple
656 fdt_setprop(blob, off, "reg", reg2, len);
659 /* Move to next compatible node */
660 off = fdt_node_offset_by_compatible(blob, off,
669 int fdt_increase_size(void *fdt, int add_len)
673 newlen = fdt_totalsize(fdt) + add_len;
675 /* Open in place with a new len */
676 return fdt_open_into(fdt, fdt, newlen);
679 #ifdef CONFIG_FDT_FIXUP_PARTITIONS
680 #include <jffs2/load_kernel.h>
681 #include <mtd_node.h>
688 int fdt_del_subnodes(const void *blob, int parent_offset)
693 for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
694 (off >= 0) && (ndepth > 0);
695 off = fdt_next_node(blob, off, &ndepth)) {
697 debug("delete %s: offset: %x\n",
698 fdt_get_name(blob, off, 0), off);
699 ret = fdt_del_node((void *)blob, off);
701 printf("Can't delete node: %s\n",
713 int fdt_del_partitions(void *blob, int parent_offset)
720 off = fdt_next_node(blob, parent_offset, &ndepth);
721 if (off > 0 && ndepth == 1) {
722 prop = fdt_getprop(blob, off, "label", NULL);
725 * Could not find label property, nand {}; node?
726 * Check subnode, delete partitions there if any.
728 return fdt_del_partitions(blob, off);
730 ret = fdt_del_subnodes(blob, parent_offset);
732 printf("Can't remove subnodes: %s\n",
741 int fdt_node_set_part_info(void *blob, int parent_offset,
742 struct mtd_device *dev)
744 struct list_head *pentry;
745 struct part_info *part;
746 struct reg_cell cell;
751 ret = fdt_del_partitions(blob, parent_offset);
756 * Check if it is nand {}; subnode, adjust
757 * the offset in this case
759 off = fdt_next_node(blob, parent_offset, &ndepth);
760 if (off > 0 && ndepth == 1)
764 list_for_each_prev(pentry, &dev->parts) {
767 part = list_entry(pentry, struct part_info, link);
769 debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
770 part_num, part->name, part->size,
771 part->offset, part->mask_flags);
773 sprintf(buf, "partition@%llx", part->offset);
775 ret = fdt_add_subnode(blob, parent_offset, buf);
776 if (ret == -FDT_ERR_NOSPACE) {
777 ret = fdt_increase_size(blob, 512);
782 } else if (ret < 0) {
783 printf("Can't add partition node: %s\n",
789 /* Check MTD_WRITEABLE_CMD flag */
790 if (part->mask_flags & 1) {
792 ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
793 if (ret == -FDT_ERR_NOSPACE) {
794 ret = fdt_increase_size(blob, 512);
803 cell.r0 = cpu_to_fdt32(part->offset);
804 cell.r1 = cpu_to_fdt32(part->size);
806 ret = fdt_setprop(blob, newoff, "reg", &cell, sizeof(cell));
807 if (ret == -FDT_ERR_NOSPACE) {
808 ret = fdt_increase_size(blob, 512);
817 ret = fdt_setprop_string(blob, newoff, "label", part->name);
818 if (ret == -FDT_ERR_NOSPACE) {
819 ret = fdt_increase_size(blob, 512);
831 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
834 printf("Can't add property: %s\n", fdt_strerror(ret));
839 * Update partitions in nor/nand nodes using info from
840 * mtdparts environment variable. The nodes to update are
841 * specified by node_info structure which contains mtd device
842 * type and compatible string: E. g. the board code in
843 * ft_board_setup() could use:
845 * struct node_info nodes[] = {
846 * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
847 * { "cfi-flash", MTD_DEV_TYPE_NOR, },
850 * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
852 void fdt_fixup_mtdparts(void *blob, void *node_info, int node_info_size)
854 struct node_info *ni = node_info;
855 struct mtd_device *dev;
860 parts = getenv("mtdparts");
864 if (mtdparts_init() != 0)
867 for (i = 0; i < node_info_size; i++) {
869 noff = fdt_node_offset_by_compatible(blob, -1, ni[i].compat);
870 while (noff != -FDT_ERR_NOTFOUND) {
871 debug("%s: %s, mtd dev type %d\n",
872 fdt_get_name(blob, noff, 0),
873 ni[i].compat, ni[i].type);
874 dev = device_find(ni[i].type, idx++);
876 if (fdt_node_set_part_info(blob, noff, dev))
877 return; /* return on error */
880 /* Jump to next flash node */
881 noff = fdt_node_offset_by_compatible(blob, noff,
888 void fdt_del_node_and_alias(void *blob, const char *alias)
890 int off = fdt_path_offset(blob, alias);
895 fdt_del_node(blob, off);
897 off = fdt_path_offset(blob, "/aliases");
898 fdt_delprop(blob, off, alias);
901 /* Helper to read a big number; size is in cells (not bytes) */
902 static inline u64 of_read_number(const fdt32_t *cell, int size)
906 r = (r << 32) | fdt32_to_cpu(*(cell++));
912 /* Max address size we deal with */
913 #define OF_MAX_ADDR_CELLS 4
914 #define OF_BAD_ADDR ((u64)-1)
915 #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
920 static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
924 printf(" %08x", *(addr++));
928 static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
931 /* Callbacks for bus specific translators */
934 const char *addresses;
935 void (*count_cells)(void *blob, int parentoffset,
936 int *addrc, int *sizec);
937 u64 (*map)(fdt32_t *addr, const fdt32_t *range,
938 int na, int ns, int pna);
939 int (*translate)(fdt32_t *addr, u64 offset, int na);
942 /* Default translator (generic bus) */
943 static void of_bus_default_count_cells(void *blob, int parentoffset,
944 int *addrc, int *sizec)
949 prop = fdt_getprop(blob, parentoffset, "#address-cells", NULL);
951 *addrc = be32_to_cpup(prop);
957 prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
959 *sizec = be32_to_cpup(prop);
965 static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
966 int na, int ns, int pna)
970 cp = of_read_number(range, na);
971 s = of_read_number(range + na + pna, ns);
972 da = of_read_number(addr, na);
974 debug("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
977 if (da < cp || da >= (cp + s))
982 static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
984 u64 a = of_read_number(addr, na);
985 memset(addr, 0, na * 4);
988 addr[na - 2] = cpu_to_fdt32(a >> 32);
989 addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
994 /* Array of bus specific translators */
995 static struct of_bus of_busses[] = {
1000 .count_cells = of_bus_default_count_cells,
1001 .map = of_bus_default_map,
1002 .translate = of_bus_default_translate,
1006 static int of_translate_one(void * blob, int parent, struct of_bus *bus,
1007 struct of_bus *pbus, fdt32_t *addr,
1008 int na, int ns, int pna, const char *rprop)
1010 const fdt32_t *ranges;
1013 u64 offset = OF_BAD_ADDR;
1015 /* Normally, an absence of a "ranges" property means we are
1016 * crossing a non-translatable boundary, and thus the addresses
1017 * below the current not cannot be converted to CPU physical ones.
1018 * Unfortunately, while this is very clear in the spec, it's not
1019 * what Apple understood, and they do have things like /uni-n or
1020 * /ht nodes with no "ranges" property and a lot of perfectly
1021 * useable mapped devices below them. Thus we treat the absence of
1022 * "ranges" as equivalent to an empty "ranges" property which means
1023 * a 1:1 translation at that level. It's up to the caller not to try
1024 * to translate addresses that aren't supposed to be translated in
1025 * the first place. --BenH.
1027 ranges = fdt_getprop(blob, parent, rprop, &rlen);
1028 if (ranges == NULL || rlen == 0) {
1029 offset = of_read_number(addr, na);
1030 memset(addr, 0, pna * 4);
1031 debug("OF: no ranges, 1:1 translation\n");
1035 debug("OF: walking ranges...\n");
1037 /* Now walk through the ranges */
1039 rone = na + pna + ns;
1040 for (; rlen >= rone; rlen -= rone, ranges += rone) {
1041 offset = bus->map(addr, ranges, na, ns, pna);
1042 if (offset != OF_BAD_ADDR)
1045 if (offset == OF_BAD_ADDR) {
1046 debug("OF: not found !\n");
1049 memcpy(addr, ranges + na, 4 * pna);
1052 of_dump_addr("OF: parent translation for:", addr, pna);
1053 debug("OF: with offset: "PRu64"\n", offset);
1055 /* Translate it into parent bus space */
1056 return pbus->translate(addr, offset, pna);
1060 * Translate an address from the device-tree into a CPU physical address,
1061 * this walks up the tree and applies the various bus mappings on the
1064 * Note: We consider that crossing any level with #size-cells == 0 to mean
1065 * that translation is impossible (that is we are not dealing with a value
1066 * that can be mapped to a cpu physical address). This is not really specified
1067 * that way, but this is traditionally the way IBM at least do things
1069 static u64 __of_translate_address(void *blob, int node_offset, const fdt32_t *in_addr,
1073 struct of_bus *bus, *pbus;
1074 fdt32_t addr[OF_MAX_ADDR_CELLS];
1075 int na, ns, pna, pns;
1076 u64 result = OF_BAD_ADDR;
1078 debug("OF: ** translation for device %s **\n",
1079 fdt_get_name(blob, node_offset, NULL));
1081 /* Get parent & match bus type */
1082 parent = fdt_parent_offset(blob, node_offset);
1085 bus = &of_busses[0];
1087 /* Cound address cells & copy address locally */
1088 bus->count_cells(blob, parent, &na, &ns);
1089 if (!OF_CHECK_COUNTS(na, ns)) {
1090 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1091 fdt_get_name(blob, node_offset, NULL));
1094 memcpy(addr, in_addr, na * 4);
1096 debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
1097 bus->name, na, ns, fdt_get_name(blob, parent, NULL));
1098 of_dump_addr("OF: translating address:", addr, na);
1102 /* Switch to parent bus */
1103 node_offset = parent;
1104 parent = fdt_parent_offset(blob, node_offset);
1106 /* If root, we have finished */
1108 debug("OF: reached root node\n");
1109 result = of_read_number(addr, na);
1113 /* Get new parent bus and counts */
1114 pbus = &of_busses[0];
1115 pbus->count_cells(blob, parent, &pna, &pns);
1116 if (!OF_CHECK_COUNTS(pna, pns)) {
1117 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1118 fdt_get_name(blob, node_offset, NULL));
1122 debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
1123 pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
1125 /* Apply bus translation */
1126 if (of_translate_one(blob, node_offset, bus, pbus,
1127 addr, na, ns, pna, rprop))
1130 /* Complete the move up one level */
1135 of_dump_addr("OF: one level translation:", addr, na);
1142 u64 fdt_translate_address(void *blob, int node_offset, const fdt32_t *in_addr)
1144 return __of_translate_address(blob, node_offset, in_addr, "ranges");
1148 * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and
1149 * who's reg property matches a physical cpu address
1151 * @blob: ptr to device tree
1152 * @compat: compatiable string to match
1153 * @compat_off: property name
1156 int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
1157 phys_addr_t compat_off)
1159 int len, off = fdt_node_offset_by_compatible(blob, -1, compat);
1160 while (off != -FDT_ERR_NOTFOUND) {
1161 const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
1163 if (compat_off == fdt_translate_address(blob, off, reg))
1166 off = fdt_node_offset_by_compatible(blob, off, compat);
1169 return -FDT_ERR_NOTFOUND;
1173 * fdt_alloc_phandle: Return next free phandle value
1175 * @blob: ptr to device tree
1177 int fdt_alloc_phandle(void *blob)
1179 int offset, phandle = 0;
1181 for (offset = fdt_next_node(blob, -1, NULL); offset >= 0;
1182 offset = fdt_next_node(blob, offset, NULL)) {
1183 phandle = max(phandle, fdt_get_phandle(blob, offset));
1190 * fdt_set_phandle: Create a phandle property for the given node
1192 * @fdt: ptr to device tree
1193 * @nodeoffset: node to update
1194 * @phandle: phandle value to set (must be unique)
1196 int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
1201 int off = fdt_node_offset_by_phandle(fdt, phandle);
1203 if ((off >= 0) && (off != nodeoffset)) {
1206 fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
1207 printf("Trying to update node %s with phandle %u ",
1210 fdt_get_path(fdt, off, buf, sizeof(buf));
1211 printf("that already exists in node %s.\n", buf);
1212 return -FDT_ERR_BADPHANDLE;
1216 ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
1221 * For now, also set the deprecated "linux,phandle" property, so that we
1222 * don't break older kernels.
1224 ret = fdt_setprop_cell(fdt, nodeoffset, "linux,phandle", phandle);
1230 * fdt_create_phandle: Create a phandle property for the given node
1232 * @fdt: ptr to device tree
1233 * @nodeoffset: node to update
1235 unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
1237 /* see if there is a phandle already */
1238 int phandle = fdt_get_phandle(fdt, nodeoffset);
1240 /* if we got 0, means no phandle so create one */
1244 phandle = fdt_alloc_phandle(fdt);
1245 ret = fdt_set_phandle(fdt, nodeoffset, phandle);
1247 printf("Can't set phandle %u: %s\n", phandle,
1257 * fdt_set_node_status: Set status for the given node
1259 * @fdt: ptr to device tree
1260 * @nodeoffset: node to update
1261 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1262 * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1263 * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1265 int fdt_set_node_status(void *fdt, int nodeoffset,
1266 enum fdt_status status, unsigned int error_code)
1275 case FDT_STATUS_OKAY:
1276 ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
1278 case FDT_STATUS_DISABLED:
1279 ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
1281 case FDT_STATUS_FAIL:
1282 ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
1284 case FDT_STATUS_FAIL_ERROR_CODE:
1285 sprintf(buf, "fail-%d", error_code);
1286 ret = fdt_setprop_string(fdt, nodeoffset, "status", buf);
1289 printf("Invalid fdt status: %x\n", status);
1298 * fdt_set_status_by_alias: Set status for the given node given an alias
1300 * @fdt: ptr to device tree
1301 * @alias: alias of node to update
1302 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1303 * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1304 * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1306 int fdt_set_status_by_alias(void *fdt, const char* alias,
1307 enum fdt_status status, unsigned int error_code)
1309 int offset = fdt_path_offset(fdt, alias);
1311 return fdt_set_node_status(fdt, offset, status, error_code);
1314 #if defined(CONFIG_VIDEO) || defined(CONFIG_LCD)
1315 int fdt_add_edid(void *blob, const char *compat, unsigned char *edid_buf)
1320 noff = fdt_node_offset_by_compatible(blob, -1, compat);
1321 if (noff != -FDT_ERR_NOTFOUND) {
1322 debug("%s: %s\n", fdt_get_name(blob, noff, 0), compat);
1324 ret = fdt_setprop(blob, noff, "edid", edid_buf, 128);
1325 if (ret == -FDT_ERR_NOSPACE) {
1326 ret = fdt_increase_size(blob, 512);
1331 } else if (ret < 0) {
1332 printf("Can't add property: %s\n", fdt_strerror(ret));
1338 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
1344 * Verify the physical address of device tree node for a given alias
1346 * This function locates the device tree node of a given alias, and then
1347 * verifies that the physical address of that device matches the given
1348 * parameter. It displays a message if there is a mismatch.
1350 * Returns 1 on success, 0 on failure
1352 int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
1359 path = fdt_getprop(fdt, anode, alias, NULL);
1361 /* If there's no such alias, then it's not a failure */
1365 node = fdt_path_offset(fdt, path);
1367 printf("Warning: device tree alias '%s' points to invalid "
1368 "node %s.\n", alias, path);
1372 reg = fdt_getprop(fdt, node, "reg", &len);
1374 printf("Warning: device tree node '%s' has no address.\n",
1379 dt_addr = fdt_translate_address(fdt, node, reg);
1380 if (addr != dt_addr) {
1381 printf("Warning: U-Boot configured device %s at address %llx,\n"
1382 " but the device tree has it address %llx.\n",
1383 alias, addr, dt_addr);
1391 * Returns the base address of an SOC or PCI node
1393 u64 fdt_get_base_address(void *fdt, int node)
1397 const fdt32_t *prop;
1399 prop = fdt_getprop(fdt, node, "#address-cells", &size);
1400 if (prop && size == 4)
1401 naddr = be32_to_cpup(prop);
1405 prop = fdt_getprop(fdt, node, "ranges", &size);
1407 return prop ? fdt_translate_address(fdt, node, prop + naddr) : 0;