3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
5 * Copyright 2010-2011 Freescale Semiconductor, Inc.
7 * See file CREDITS for list of people who contributed to this
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 #include <stdio_dev.h>
28 #include <linux/ctype.h>
29 #include <linux/types.h>
30 #include <asm/global_data.h>
33 #include <fdt_support.h>
37 * Global data (for the gd->bd)
39 DECLARE_GLOBAL_DATA_PTR
;
42 * fdt_getprop_u32_default - Find a node and return it's property or a default
44 * @fdt: ptr to device tree
46 * @prop: property name
47 * @dflt: default value if the property isn't found
49 * Convenience function to find a node and return it's property or a
50 * default value if it doesn't exist.
52 u32
fdt_getprop_u32_default(const void *fdt
, const char *path
,
53 const char *prop
, const u32 dflt
)
58 off
= fdt_path_offset(fdt
, path
);
62 val
= fdt_getprop(fdt
, off
, prop
, NULL
);
64 return fdt32_to_cpu(*val
);
70 * fdt_find_and_setprop: Find a node and set it's property
72 * @fdt: ptr to device tree
74 * @prop: property name
75 * @val: ptr to new value
76 * @len: length of new property value
77 * @create: flag to create the property if it doesn't exist
79 * Convenience function to directly set a property given the path to the node.
81 int fdt_find_and_setprop(void *fdt
, const char *node
, const char *prop
,
82 const void *val
, int len
, int create
)
84 int nodeoff
= fdt_path_offset(fdt
, node
);
89 if ((!create
) && (fdt_get_property(fdt
, nodeoff
, prop
, 0) == NULL
))
90 return 0; /* create flag not set; so exit quietly */
92 return fdt_setprop(fdt
, nodeoff
, prop
, val
, len
);
95 #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
97 #ifdef CONFIG_CONS_INDEX
98 static void fdt_fill_multisername(char *sername
, size_t maxlen
)
100 const char *outname
= stdio_devices
[stdout
]->name
;
102 if (strcmp(outname
, "serial") > 0)
103 strncpy(sername
, outname
, maxlen
);
106 if (strcmp(outname
+ 1, "serial") > 0)
107 strncpy(sername
, outname
+ 1, maxlen
);
111 static int fdt_fixup_stdout(void *fdt
, int chosenoff
)
114 #ifdef CONFIG_CONS_INDEX
116 char sername
[9] = { 0 };
119 fdt_fill_multisername(sername
, sizeof(sername
) - 1);
121 sprintf(sername
, "serial%d", CONFIG_CONS_INDEX
- 1);
123 err
= node
= fdt_path_offset(fdt
, "/aliases");
126 path
= fdt_getprop(fdt
, node
, sername
, &len
);
128 char *p
= malloc(len
);
129 err
= -FDT_ERR_NOSPACE
;
131 memcpy(p
, path
, len
);
132 err
= fdt_setprop(fdt
, chosenoff
,
133 "linux,stdout-path", p
, len
);
142 printf("WARNING: could not set linux,stdout-path %s.\n",
149 int fdt_initrd(void *fdt
, ulong initrd_start
, ulong initrd_end
, int force
)
157 /* Find the "chosen" node. */
158 nodeoffset
= fdt_path_offset (fdt
, "/chosen");
160 /* If there is no "chosen" node in the blob return */
161 if (nodeoffset
< 0) {
162 printf("fdt_initrd: %s\n", fdt_strerror(nodeoffset
));
166 /* just return if initrd_start/end aren't valid */
167 if ((initrd_start
== 0) || (initrd_end
== 0))
170 total
= fdt_num_mem_rsv(fdt
);
173 * Look for an existing entry and update it. If we don't find
174 * the entry, we will j be the next available slot.
176 for (j
= 0; j
< total
; j
++) {
177 err
= fdt_get_mem_rsv(fdt
, j
, &addr
, &size
);
178 if (addr
== initrd_start
) {
179 fdt_del_mem_rsv(fdt
, j
);
184 err
= fdt_add_mem_rsv(fdt
, initrd_start
, initrd_end
- initrd_start
);
186 printf("fdt_initrd: %s\n", fdt_strerror(err
));
190 path
= fdt_getprop(fdt
, nodeoffset
, "linux,initrd-start", NULL
);
191 if ((path
== NULL
) || force
) {
192 tmp
= __cpu_to_be32(initrd_start
);
193 err
= fdt_setprop(fdt
, nodeoffset
,
194 "linux,initrd-start", &tmp
, sizeof(tmp
));
197 "could not set linux,initrd-start %s.\n",
201 tmp
= __cpu_to_be32(initrd_end
);
202 err
= fdt_setprop(fdt
, nodeoffset
,
203 "linux,initrd-end", &tmp
, sizeof(tmp
));
205 printf("WARNING: could not set linux,initrd-end %s.\n",
215 int fdt_chosen(void *fdt
, int force
)
219 char *str
; /* used to set string properties */
222 err
= fdt_check_header(fdt
);
224 printf("fdt_chosen: %s\n", fdt_strerror(err
));
229 * Find the "chosen" node.
231 nodeoffset
= fdt_path_offset (fdt
, "/chosen");
234 * If there is no "chosen" node in the blob, create it.
236 if (nodeoffset
< 0) {
238 * Create a new node "/chosen" (offset 0 is root level)
240 nodeoffset
= fdt_add_subnode(fdt
, 0, "chosen");
241 if (nodeoffset
< 0) {
242 printf("WARNING: could not create /chosen %s.\n",
243 fdt_strerror(nodeoffset
));
249 * Create /chosen properites that don't exist in the fdt.
250 * If the property exists, update it only if the "force" parameter
253 str
= getenv("bootargs");
255 path
= fdt_getprop(fdt
, nodeoffset
, "bootargs", NULL
);
256 if ((path
== NULL
) || force
) {
257 err
= fdt_setprop(fdt
, nodeoffset
,
258 "bootargs", str
, strlen(str
)+1);
260 printf("WARNING: could not set bootargs %s.\n",
265 #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
266 path
= fdt_getprop(fdt
, nodeoffset
, "linux,stdout-path", NULL
);
267 if ((path
== NULL
) || force
)
268 err
= fdt_fixup_stdout(fdt
, nodeoffset
);
271 #ifdef OF_STDOUT_PATH
272 path
= fdt_getprop(fdt
, nodeoffset
, "linux,stdout-path", NULL
);
273 if ((path
== NULL
) || force
) {
274 err
= fdt_setprop(fdt
, nodeoffset
,
275 "linux,stdout-path", OF_STDOUT_PATH
, strlen(OF_STDOUT_PATH
)+1);
277 printf("WARNING: could not set linux,stdout-path %s.\n",
285 void do_fixup_by_path(void *fdt
, const char *path
, const char *prop
,
286 const void *val
, int len
, int create
)
290 debug("Updating property '%s/%s' = ", path
, prop
);
291 for (i
= 0; i
< len
; i
++)
292 debug(" %.2x", *(u8
*)(val
+i
));
295 int rc
= fdt_find_and_setprop(fdt
, path
, prop
, val
, len
, create
);
297 printf("Unable to update property %s:%s, err=%s\n",
298 path
, prop
, fdt_strerror(rc
));
301 void do_fixup_by_path_u32(void *fdt
, const char *path
, const char *prop
,
304 val
= cpu_to_fdt32(val
);
305 do_fixup_by_path(fdt
, path
, prop
, &val
, sizeof(val
), create
);
308 void do_fixup_by_prop(void *fdt
,
309 const char *pname
, const void *pval
, int plen
,
310 const char *prop
, const void *val
, int len
,
316 debug("Updating property '%s' = ", prop
);
317 for (i
= 0; i
< len
; i
++)
318 debug(" %.2x", *(u8
*)(val
+i
));
321 off
= fdt_node_offset_by_prop_value(fdt
, -1, pname
, pval
, plen
);
322 while (off
!= -FDT_ERR_NOTFOUND
) {
323 if (create
|| (fdt_get_property(fdt
, off
, prop
, 0) != NULL
))
324 fdt_setprop(fdt
, off
, prop
, val
, len
);
325 off
= fdt_node_offset_by_prop_value(fdt
, off
, pname
, pval
, plen
);
329 void do_fixup_by_prop_u32(void *fdt
,
330 const char *pname
, const void *pval
, int plen
,
331 const char *prop
, u32 val
, int create
)
333 val
= cpu_to_fdt32(val
);
334 do_fixup_by_prop(fdt
, pname
, pval
, plen
, prop
, &val
, 4, create
);
337 void do_fixup_by_compat(void *fdt
, const char *compat
,
338 const char *prop
, const void *val
, int len
, int create
)
343 debug("Updating property '%s' = ", prop
);
344 for (i
= 0; i
< len
; i
++)
345 debug(" %.2x", *(u8
*)(val
+i
));
348 off
= fdt_node_offset_by_compatible(fdt
, -1, compat
);
349 while (off
!= -FDT_ERR_NOTFOUND
) {
350 if (create
|| (fdt_get_property(fdt
, off
, prop
, 0) != NULL
))
351 fdt_setprop(fdt
, off
, prop
, val
, len
);
352 off
= fdt_node_offset_by_compatible(fdt
, off
, compat
);
356 void do_fixup_by_compat_u32(void *fdt
, const char *compat
,
357 const char *prop
, u32 val
, int create
)
359 val
= cpu_to_fdt32(val
);
360 do_fixup_by_compat(fdt
, compat
, prop
, &val
, 4, create
);
364 * Get cells len in bytes
365 * if #NNNN-cells property is 2 then len is 8
368 static int get_cells_len(void *blob
, char *nr_cells_name
)
372 cell
= fdt_getprop(blob
, 0, nr_cells_name
, NULL
);
373 if (cell
&& fdt32_to_cpu(*cell
) == 2)
380 * Write a 4 or 8 byte big endian cell
382 static void write_cell(u8
*addr
, u64 val
, int size
)
384 int shift
= (size
- 1) * 8;
386 *addr
++ = (val
>> shift
) & 0xff;
391 int fdt_fixup_memory_banks(void *blob
, u64 start
[], u64 size
[], int banks
)
394 int addr_cell_len
, size_cell_len
, len
;
395 u8 tmp
[banks
* 16]; /* Up to 64-bit address + 64-bit size */
398 err
= fdt_check_header(blob
);
400 printf("%s: %s\n", __FUNCTION__
, fdt_strerror(err
));
404 /* update, or add and update /memory node */
405 nodeoffset
= fdt_path_offset(blob
, "/memory");
406 if (nodeoffset
< 0) {
407 nodeoffset
= fdt_add_subnode(blob
, 0, "memory");
409 printf("WARNING: could not create /memory: %s.\n",
410 fdt_strerror(nodeoffset
));
413 err
= fdt_setprop(blob
, nodeoffset
, "device_type", "memory",
416 printf("WARNING: could not set %s %s.\n", "device_type",
421 addr_cell_len
= get_cells_len(blob
, "#address-cells");
422 size_cell_len
= get_cells_len(blob
, "#size-cells");
424 for (bank
= 0, len
= 0; bank
< banks
; bank
++) {
425 write_cell(tmp
+ len
, start
[bank
], addr_cell_len
);
426 len
+= addr_cell_len
;
428 write_cell(tmp
+ len
, size
[bank
], size_cell_len
);
429 len
+= size_cell_len
;
432 err
= fdt_setprop(blob
, nodeoffset
, "reg", tmp
, len
);
434 printf("WARNING: could not set %s %s.\n",
435 "reg", fdt_strerror(err
));
441 int fdt_fixup_memory(void *blob
, u64 start
, u64 size
)
443 return fdt_fixup_memory_banks(blob
, &start
, &size
, 1);
446 void fdt_fixup_ethernet(void *fdt
)
449 char enet
[16], *tmp
, *end
;
450 char mac
[16] = "ethaddr";
452 unsigned char mac_addr
[6];
454 node
= fdt_path_offset(fdt
, "/aliases");
459 while ((tmp
= getenv(mac
)) != NULL
) {
460 sprintf(enet
, "ethernet%d", i
);
461 path
= fdt_getprop(fdt
, node
, enet
, NULL
);
463 debug("No alias for %s\n", enet
);
464 sprintf(mac
, "eth%daddr", ++i
);
468 for (j
= 0; j
< 6; j
++) {
469 mac_addr
[j
] = tmp
? simple_strtoul(tmp
, &end
, 16) : 0;
471 tmp
= (*end
) ? end
+1 : end
;
474 do_fixup_by_path(fdt
, path
, "mac-address", &mac_addr
, 6, 0);
475 do_fixup_by_path(fdt
, path
, "local-mac-address",
478 sprintf(mac
, "eth%daddr", ++i
);
482 /* Resize the fdt to its actual size + a bit of padding */
483 int fdt_resize(void *blob
)
493 total
= fdt_num_mem_rsv(blob
);
494 for (i
= 0; i
< total
; i
++) {
495 fdt_get_mem_rsv(blob
, i
, &addr
, &size
);
496 if (addr
== (uintptr_t)blob
) {
497 fdt_del_mem_rsv(blob
, i
);
503 * Calculate the actual size of the fdt
504 * plus the size needed for 5 fdt_add_mem_rsv, one
505 * for the fdt itself and 4 for a possible initrd
506 * ((initrd-start + initrd-end) * 2 (name & value))
508 actualsize
= fdt_off_dt_strings(blob
) +
509 fdt_size_dt_strings(blob
) + 5 * sizeof(struct fdt_reserve_entry
);
511 /* Make it so the fdt ends on a page boundary */
512 actualsize
= ALIGN(actualsize
+ ((uintptr_t)blob
& 0xfff), 0x1000);
513 actualsize
= actualsize
- ((uintptr_t)blob
& 0xfff);
515 /* Change the fdt header to reflect the correct size */
516 fdt_set_totalsize(blob
, actualsize
);
518 /* Add the new reservation */
519 ret
= fdt_add_mem_rsv(blob
, (uintptr_t)blob
, actualsize
);
527 #define CONFIG_SYS_PCI_NR_INBOUND_WIN 4
529 #define FDT_PCI_PREFETCH (0x40000000)
530 #define FDT_PCI_MEM32 (0x02000000)
531 #define FDT_PCI_IO (0x01000000)
532 #define FDT_PCI_MEM64 (0x03000000)
534 int fdt_pci_dma_ranges(void *blob
, int phb_off
, struct pci_controller
*hose
) {
536 int addrcell
, sizecell
, len
, r
;
538 /* sized based on pci addr cells, size-cells, & address-cells */
539 u32 dma_ranges
[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN
];
541 addrcell
= fdt_getprop_u32_default(blob
, "/", "#address-cells", 1);
542 sizecell
= fdt_getprop_u32_default(blob
, "/", "#size-cells", 1);
544 dma_range
= &dma_ranges
[0];
545 for (r
= 0; r
< hose
->region_count
; r
++) {
546 u64 bus_start
, phys_start
, size
;
548 /* skip if !PCI_REGION_SYS_MEMORY */
549 if (!(hose
->regions
[r
].flags
& PCI_REGION_SYS_MEMORY
))
552 bus_start
= (u64
)hose
->regions
[r
].bus_start
;
553 phys_start
= (u64
)hose
->regions
[r
].phys_start
;
554 size
= (u64
)hose
->regions
[r
].size
;
557 if (size
>= 0x100000000ull
)
558 dma_range
[0] |= FDT_PCI_MEM64
;
560 dma_range
[0] |= FDT_PCI_MEM32
;
561 if (hose
->regions
[r
].flags
& PCI_REGION_PREFETCH
)
562 dma_range
[0] |= FDT_PCI_PREFETCH
;
563 #ifdef CONFIG_SYS_PCI_64BIT
564 dma_range
[1] = bus_start
>> 32;
568 dma_range
[2] = bus_start
& 0xffffffff;
571 dma_range
[3] = phys_start
>> 32;
572 dma_range
[4] = phys_start
& 0xffffffff;
574 dma_range
[3] = phys_start
& 0xffffffff;
578 dma_range
[3 + addrcell
+ 0] = size
>> 32;
579 dma_range
[3 + addrcell
+ 1] = size
& 0xffffffff;
581 dma_range
[3 + addrcell
+ 0] = size
& 0xffffffff;
584 dma_range
+= (3 + addrcell
+ sizecell
);
587 len
= dma_range
- &dma_ranges
[0];
589 fdt_setprop(blob
, phb_off
, "dma-ranges", &dma_ranges
[0], len
*4);
595 #ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE
597 * Provide a weak default function to return the flash bank size.
598 * There might be multiple non-identical flash chips connected to one
599 * chip-select, so we need to pass an index as well.
601 u32
__flash_get_bank_size(int cs
, int idx
)
603 extern flash_info_t flash_info
[];
606 * As default, a simple 1:1 mapping is provided. Boards with
607 * a different mapping need to supply a board specific mapping
610 return flash_info
[cs
].size
;
612 u32
flash_get_bank_size(int cs
, int idx
)
613 __attribute__((weak
, alias("__flash_get_bank_size")));
616 * This function can be used to update the size in the "reg" property
617 * of all NOR FLASH device nodes. This is necessary for boards with
618 * non-fixed NOR FLASH sizes.
620 int fdt_fixup_nor_flash_size(void *blob
)
622 char compat
[][16] = { "cfi-flash", "jedec-flash" };
625 struct fdt_property
*prop
;
629 for (i
= 0; i
< 2; i
++) {
630 off
= fdt_node_offset_by_compatible(blob
, -1, compat
[i
]);
631 while (off
!= -FDT_ERR_NOTFOUND
) {
635 * Found one compatible node, so fixup the size
636 * int its reg properties
638 prop
= fdt_get_property_w(blob
, off
, "reg", &len
);
640 int tuple_size
= 3 * sizeof(reg
);
643 * There might be multiple reg-tuples,
644 * so loop through them all
646 reg
= reg2
= (u32
*)&prop
->data
[0];
647 for (idx
= 0; idx
< (len
/ tuple_size
); idx
++) {
649 * Update size in reg property
651 reg
[2] = flash_get_bank_size(reg
[0],
655 * Point to next reg tuple
660 fdt_setprop(blob
, off
, "reg", reg2
, len
);
663 /* Move to next compatible node */
664 off
= fdt_node_offset_by_compatible(blob
, off
,
673 int fdt_increase_size(void *fdt
, int add_len
)
677 newlen
= fdt_totalsize(fdt
) + add_len
;
679 /* Open in place with a new len */
680 return fdt_open_into(fdt
, fdt
, newlen
);
683 #ifdef CONFIG_FDT_FIXUP_PARTITIONS
684 #include <jffs2/load_kernel.h>
685 #include <mtd_node.h>
692 int fdt_del_subnodes(const void *blob
, int parent_offset
)
697 for (ndepth
= 0, off
= fdt_next_node(blob
, parent_offset
, &ndepth
);
698 (off
>= 0) && (ndepth
> 0);
699 off
= fdt_next_node(blob
, off
, &ndepth
)) {
701 debug("delete %s: offset: %x\n",
702 fdt_get_name(blob
, off
, 0), off
);
703 ret
= fdt_del_node((void *)blob
, off
);
705 printf("Can't delete node: %s\n",
717 int fdt_del_partitions(void *blob
, int parent_offset
)
724 off
= fdt_next_node(blob
, parent_offset
, &ndepth
);
725 if (off
> 0 && ndepth
== 1) {
726 prop
= fdt_getprop(blob
, off
, "label", NULL
);
729 * Could not find label property, nand {}; node?
730 * Check subnode, delete partitions there if any.
732 return fdt_del_partitions(blob
, off
);
734 ret
= fdt_del_subnodes(blob
, parent_offset
);
736 printf("Can't remove subnodes: %s\n",
745 int fdt_node_set_part_info(void *blob
, int parent_offset
,
746 struct mtd_device
*dev
)
748 struct list_head
*pentry
;
749 struct part_info
*part
;
750 struct reg_cell cell
;
755 ret
= fdt_del_partitions(blob
, parent_offset
);
760 * Check if it is nand {}; subnode, adjust
761 * the offset in this case
763 off
= fdt_next_node(blob
, parent_offset
, &ndepth
);
764 if (off
> 0 && ndepth
== 1)
768 list_for_each_prev(pentry
, &dev
->parts
) {
771 part
= list_entry(pentry
, struct part_info
, link
);
773 debug("%2d: %-20s0x%08x\t0x%08x\t%d\n",
774 part_num
, part
->name
, part
->size
,
775 part
->offset
, part
->mask_flags
);
777 sprintf(buf
, "partition@%x", part
->offset
);
779 ret
= fdt_add_subnode(blob
, parent_offset
, buf
);
780 if (ret
== -FDT_ERR_NOSPACE
) {
781 ret
= fdt_increase_size(blob
, 512);
786 } else if (ret
< 0) {
787 printf("Can't add partition node: %s\n",
793 /* Check MTD_WRITEABLE_CMD flag */
794 if (part
->mask_flags
& 1) {
796 ret
= fdt_setprop(blob
, newoff
, "read_only", NULL
, 0);
797 if (ret
== -FDT_ERR_NOSPACE
) {
798 ret
= fdt_increase_size(blob
, 512);
807 cell
.r0
= cpu_to_fdt32(part
->offset
);
808 cell
.r1
= cpu_to_fdt32(part
->size
);
810 ret
= fdt_setprop(blob
, newoff
, "reg", &cell
, sizeof(cell
));
811 if (ret
== -FDT_ERR_NOSPACE
) {
812 ret
= fdt_increase_size(blob
, 512);
821 ret
= fdt_setprop_string(blob
, newoff
, "label", part
->name
);
822 if (ret
== -FDT_ERR_NOSPACE
) {
823 ret
= fdt_increase_size(blob
, 512);
835 printf("Can't increase blob size: %s\n", fdt_strerror(ret
));
838 printf("Can't add property: %s\n", fdt_strerror(ret
));
843 * Update partitions in nor/nand nodes using info from
844 * mtdparts environment variable. The nodes to update are
845 * specified by node_info structure which contains mtd device
846 * type and compatible string: E. g. the board code in
847 * ft_board_setup() could use:
849 * struct node_info nodes[] = {
850 * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
851 * { "cfi-flash", MTD_DEV_TYPE_NOR, },
854 * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
856 void fdt_fixup_mtdparts(void *blob
, void *node_info
, int node_info_size
)
858 struct node_info
*ni
= node_info
;
859 struct mtd_device
*dev
;
864 parts
= getenv("mtdparts");
868 if (mtdparts_init() != 0)
871 for (i
= 0; i
< node_info_size
; i
++) {
873 noff
= fdt_node_offset_by_compatible(blob
, -1, ni
[i
].compat
);
874 while (noff
!= -FDT_ERR_NOTFOUND
) {
875 debug("%s: %s, mtd dev type %d\n",
876 fdt_get_name(blob
, noff
, 0),
877 ni
[i
].compat
, ni
[i
].type
);
878 dev
= device_find(ni
[i
].type
, idx
++);
880 if (fdt_node_set_part_info(blob
, noff
, dev
))
881 return; /* return on error */
884 /* Jump to next flash node */
885 noff
= fdt_node_offset_by_compatible(blob
, noff
,
892 void fdt_del_node_and_alias(void *blob
, const char *alias
)
894 int off
= fdt_path_offset(blob
, alias
);
899 fdt_del_node(blob
, off
);
901 off
= fdt_path_offset(blob
, "/aliases");
902 fdt_delprop(blob
, off
, alias
);
905 /* Helper to read a big number; size is in cells (not bytes) */
906 static inline u64
of_read_number(const __be32
*cell
, int size
)
910 r
= (r
<< 32) | be32_to_cpu(*(cell
++));
916 /* Max address size we deal with */
917 #define OF_MAX_ADDR_CELLS 4
918 #define OF_BAD_ADDR ((u64)-1)
919 #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
924 static void of_dump_addr(const char *s
, const u32
*addr
, int na
)
928 printf(" %08x", *(addr
++));
932 static void of_dump_addr(const char *s
, const u32
*addr
, int na
) { }
935 /* Callbacks for bus specific translators */
938 const char *addresses
;
939 void (*count_cells
)(void *blob
, int parentoffset
,
940 int *addrc
, int *sizec
);
941 u64 (*map
)(u32
*addr
, const u32
*range
,
942 int na
, int ns
, int pna
);
943 int (*translate
)(u32
*addr
, u64 offset
, int na
);
946 /* Default translator (generic bus) */
947 static void of_bus_default_count_cells(void *blob
, int parentoffset
,
948 int *addrc
, int *sizec
)
953 prop
= fdt_getprop(blob
, parentoffset
, "#address-cells", NULL
);
955 *addrc
= be32_to_cpup((u32
*)prop
);
961 prop
= fdt_getprop(blob
, parentoffset
, "#size-cells", NULL
);
963 *sizec
= be32_to_cpup((u32
*)prop
);
969 static u64
of_bus_default_map(u32
*addr
, const u32
*range
,
970 int na
, int ns
, int pna
)
974 cp
= of_read_number(range
, na
);
975 s
= of_read_number(range
+ na
+ pna
, ns
);
976 da
= of_read_number(addr
, na
);
978 debug("OF: default map, cp="PRu64
", s="PRu64
", da="PRu64
"\n",
981 if (da
< cp
|| da
>= (cp
+ s
))
986 static int of_bus_default_translate(u32
*addr
, u64 offset
, int na
)
988 u64 a
= of_read_number(addr
, na
);
989 memset(addr
, 0, na
* 4);
992 addr
[na
- 2] = a
>> 32;
993 addr
[na
- 1] = a
& 0xffffffffu
;
998 /* Array of bus specific translators */
999 static struct of_bus of_busses
[] = {
1004 .count_cells
= of_bus_default_count_cells
,
1005 .map
= of_bus_default_map
,
1006 .translate
= of_bus_default_translate
,
1010 static int of_translate_one(void * blob
, int parent
, struct of_bus
*bus
,
1011 struct of_bus
*pbus
, u32
*addr
,
1012 int na
, int ns
, int pna
, const char *rprop
)
1017 u64 offset
= OF_BAD_ADDR
;
1019 /* Normally, an absence of a "ranges" property means we are
1020 * crossing a non-translatable boundary, and thus the addresses
1021 * below the current not cannot be converted to CPU physical ones.
1022 * Unfortunately, while this is very clear in the spec, it's not
1023 * what Apple understood, and they do have things like /uni-n or
1024 * /ht nodes with no "ranges" property and a lot of perfectly
1025 * useable mapped devices below them. Thus we treat the absence of
1026 * "ranges" as equivalent to an empty "ranges" property which means
1027 * a 1:1 translation at that level. It's up to the caller not to try
1028 * to translate addresses that aren't supposed to be translated in
1029 * the first place. --BenH.
1031 ranges
= (u32
*)fdt_getprop(blob
, parent
, rprop
, &rlen
);
1032 if (ranges
== NULL
|| rlen
== 0) {
1033 offset
= of_read_number(addr
, na
);
1034 memset(addr
, 0, pna
* 4);
1035 debug("OF: no ranges, 1:1 translation\n");
1039 debug("OF: walking ranges...\n");
1041 /* Now walk through the ranges */
1043 rone
= na
+ pna
+ ns
;
1044 for (; rlen
>= rone
; rlen
-= rone
, ranges
+= rone
) {
1045 offset
= bus
->map(addr
, ranges
, na
, ns
, pna
);
1046 if (offset
!= OF_BAD_ADDR
)
1049 if (offset
== OF_BAD_ADDR
) {
1050 debug("OF: not found !\n");
1053 memcpy(addr
, ranges
+ na
, 4 * pna
);
1056 of_dump_addr("OF: parent translation for:", addr
, pna
);
1057 debug("OF: with offset: "PRu64
"\n", offset
);
1059 /* Translate it into parent bus space */
1060 return pbus
->translate(addr
, offset
, pna
);
1064 * Translate an address from the device-tree into a CPU physical address,
1065 * this walks up the tree and applies the various bus mappings on the
1068 * Note: We consider that crossing any level with #size-cells == 0 to mean
1069 * that translation is impossible (that is we are not dealing with a value
1070 * that can be mapped to a cpu physical address). This is not really specified
1071 * that way, but this is traditionally the way IBM at least do things
1073 u64
__of_translate_address(void *blob
, int node_offset
, const u32
*in_addr
,
1077 struct of_bus
*bus
, *pbus
;
1078 u32 addr
[OF_MAX_ADDR_CELLS
];
1079 int na
, ns
, pna
, pns
;
1080 u64 result
= OF_BAD_ADDR
;
1082 debug("OF: ** translation for device %s **\n",
1083 fdt_get_name(blob
, node_offset
, NULL
));
1085 /* Get parent & match bus type */
1086 parent
= fdt_parent_offset(blob
, node_offset
);
1089 bus
= &of_busses
[0];
1091 /* Cound address cells & copy address locally */
1092 bus
->count_cells(blob
, parent
, &na
, &ns
);
1093 if (!OF_CHECK_COUNTS(na
, ns
)) {
1094 printf("%s: Bad cell count for %s\n", __FUNCTION__
,
1095 fdt_get_name(blob
, node_offset
, NULL
));
1098 memcpy(addr
, in_addr
, na
* 4);
1100 debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
1101 bus
->name
, na
, ns
, fdt_get_name(blob
, parent
, NULL
));
1102 of_dump_addr("OF: translating address:", addr
, na
);
1106 /* Switch to parent bus */
1107 node_offset
= parent
;
1108 parent
= fdt_parent_offset(blob
, node_offset
);
1110 /* If root, we have finished */
1112 debug("OF: reached root node\n");
1113 result
= of_read_number(addr
, na
);
1117 /* Get new parent bus and counts */
1118 pbus
= &of_busses
[0];
1119 pbus
->count_cells(blob
, parent
, &pna
, &pns
);
1120 if (!OF_CHECK_COUNTS(pna
, pns
)) {
1121 printf("%s: Bad cell count for %s\n", __FUNCTION__
,
1122 fdt_get_name(blob
, node_offset
, NULL
));
1126 debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
1127 pbus
->name
, pna
, pns
, fdt_get_name(blob
, parent
, NULL
));
1129 /* Apply bus translation */
1130 if (of_translate_one(blob
, node_offset
, bus
, pbus
,
1131 addr
, na
, ns
, pna
, rprop
))
1134 /* Complete the move up one level */
1139 of_dump_addr("OF: one level translation:", addr
, na
);
1146 u64
fdt_translate_address(void *blob
, int node_offset
, const u32
*in_addr
)
1148 return __of_translate_address(blob
, node_offset
, in_addr
, "ranges");
1152 * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and
1153 * who's reg property matches a physical cpu address
1155 * @blob: ptr to device tree
1156 * @compat: compatiable string to match
1157 * @compat_off: property name
1160 int fdt_node_offset_by_compat_reg(void *blob
, const char *compat
,
1161 phys_addr_t compat_off
)
1163 int len
, off
= fdt_node_offset_by_compatible(blob
, -1, compat
);
1164 while (off
!= -FDT_ERR_NOTFOUND
) {
1165 u32
*reg
= (u32
*)fdt_getprop(blob
, off
, "reg", &len
);
1167 if (compat_off
== fdt_translate_address(blob
, off
, reg
))
1170 off
= fdt_node_offset_by_compatible(blob
, off
, compat
);
1173 return -FDT_ERR_NOTFOUND
;
1177 * fdt_alloc_phandle: Return next free phandle value
1179 * @blob: ptr to device tree
1181 int fdt_alloc_phandle(void *blob
)
1183 int offset
, phandle
= 0;
1185 for (offset
= fdt_next_node(blob
, -1, NULL
); offset
>= 0;
1186 offset
= fdt_next_node(blob
, offset
, NULL
)) {
1187 phandle
= max(phandle
, fdt_get_phandle(blob
, offset
));
1194 * fdt_set_phandle: Create a phandle property for the given node
1196 * @fdt: ptr to device tree
1197 * @nodeoffset: node to update
1198 * @phandle: phandle value to set (must be unique)
1200 int fdt_set_phandle(void *fdt
, int nodeoffset
, uint32_t phandle
)
1205 int off
= fdt_node_offset_by_phandle(fdt
, phandle
);
1207 if ((off
>= 0) && (off
!= nodeoffset
)) {
1210 fdt_get_path(fdt
, nodeoffset
, buf
, sizeof(buf
));
1211 printf("Trying to update node %s with phandle %u ",
1214 fdt_get_path(fdt
, off
, buf
, sizeof(buf
));
1215 printf("that already exists in node %s.\n", buf
);
1216 return -FDT_ERR_BADPHANDLE
;
1220 ret
= fdt_setprop_cell(fdt
, nodeoffset
, "phandle", phandle
);
1225 * For now, also set the deprecated "linux,phandle" property, so that we
1226 * don't break older kernels.
1228 ret
= fdt_setprop_cell(fdt
, nodeoffset
, "linux,phandle", phandle
);
1234 * fdt_create_phandle: Create a phandle property for the given node
1236 * @fdt: ptr to device tree
1237 * @nodeoffset: node to update
1239 unsigned int fdt_create_phandle(void *fdt
, int nodeoffset
)
1241 /* see if there is a phandle already */
1242 int phandle
= fdt_get_phandle(fdt
, nodeoffset
);
1244 /* if we got 0, means no phandle so create one */
1248 phandle
= fdt_alloc_phandle(fdt
);
1249 ret
= fdt_set_phandle(fdt
, nodeoffset
, phandle
);
1251 printf("Can't set phandle %u: %s\n", phandle
,
1261 * fdt_set_node_status: Set status for the given node
1263 * @fdt: ptr to device tree
1264 * @nodeoffset: node to update
1265 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1266 * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1267 * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1269 int fdt_set_node_status(void *fdt
, int nodeoffset
,
1270 enum fdt_status status
, unsigned int error_code
)
1279 case FDT_STATUS_OKAY
:
1280 ret
= fdt_setprop_string(fdt
, nodeoffset
, "status", "okay");
1282 case FDT_STATUS_DISABLED
:
1283 ret
= fdt_setprop_string(fdt
, nodeoffset
, "status", "disabled");
1285 case FDT_STATUS_FAIL
:
1286 ret
= fdt_setprop_string(fdt
, nodeoffset
, "status", "fail");
1288 case FDT_STATUS_FAIL_ERROR_CODE
:
1289 sprintf(buf
, "fail-%d", error_code
);
1290 ret
= fdt_setprop_string(fdt
, nodeoffset
, "status", buf
);
1293 printf("Invalid fdt status: %x\n", status
);
1302 * fdt_set_status_by_alias: Set status for the given node given an alias
1304 * @fdt: ptr to device tree
1305 * @alias: alias of node to update
1306 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1307 * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1308 * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1310 int fdt_set_status_by_alias(void *fdt
, const char* alias
,
1311 enum fdt_status status
, unsigned int error_code
)
1313 int offset
= fdt_path_offset(fdt
, alias
);
1315 return fdt_set_node_status(fdt
, offset
, status
, error_code
);
1318 #if defined(CONFIG_VIDEO)
1319 int fdt_add_edid(void *blob
, const char *compat
, unsigned char *edid_buf
)
1324 noff
= fdt_node_offset_by_compatible(blob
, -1, compat
);
1325 if (noff
!= -FDT_ERR_NOTFOUND
) {
1326 debug("%s: %s\n", fdt_get_name(blob
, noff
, 0), compat
);
1328 ret
= fdt_setprop(blob
, noff
, "edid", edid_buf
, 128);
1329 if (ret
== -FDT_ERR_NOSPACE
) {
1330 ret
= fdt_increase_size(blob
, 512);
1335 } else if (ret
< 0) {
1336 printf("Can't add property: %s\n", fdt_strerror(ret
));
1342 printf("Can't increase blob size: %s\n", fdt_strerror(ret
));
1348 * Verify the physical address of device tree node for a given alias
1350 * This function locates the device tree node of a given alias, and then
1351 * verifies that the physical address of that device matches the given
1352 * parameter. It displays a message if there is a mismatch.
1354 * Returns 1 on success, 0 on failure
1356 int fdt_verify_alias_address(void *fdt
, int anode
, const char *alias
, u64 addr
)
1363 path
= fdt_getprop(fdt
, anode
, alias
, NULL
);
1365 /* If there's no such alias, then it's not a failure */
1369 node
= fdt_path_offset(fdt
, path
);
1371 printf("Warning: device tree alias '%s' points to invalid "
1372 "node %s.\n", alias
, path
);
1376 reg
= fdt_getprop(fdt
, node
, "reg", &len
);
1378 printf("Warning: device tree node '%s' has no address.\n",
1383 dt_addr
= fdt_translate_address(fdt
, node
, reg
);
1384 if (addr
!= dt_addr
) {
1385 printf("Warning: U-Boot configured device %s at address %llx,\n"
1386 " but the device tree has it address %llx.\n",
1387 alias
, addr
, dt_addr
);
1395 * Returns the base address of an SOC or PCI node
1397 u64
fdt_get_base_address(void *fdt
, int node
)
1403 prop
= fdt_getprop(fdt
, node
, "#address-cells", &size
);
1404 if (prop
&& size
== 4)
1409 prop
= fdt_getprop(fdt
, node
, "ranges", &size
);
1411 return prop
? fdt_translate_address(fdt
, node
, prop
+ naddr
) : 0;