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>
32 #include <fdt_support.h>
36 * Global data (for the gd->bd)
38 DECLARE_GLOBAL_DATA_PTR
;
41 * fdt_getprop_u32_default - Find a node and return it's property or a default
43 * @fdt: ptr to device tree
45 * @prop: property name
46 * @dflt: default value if the property isn't found
48 * Convenience function to find a node and return it's property or a
49 * default value if it doesn't exist.
51 u32
fdt_getprop_u32_default(const void *fdt
, const char *path
,
52 const char *prop
, const u32 dflt
)
57 off
= fdt_path_offset(fdt
, path
);
61 val
= fdt_getprop(fdt
, off
, prop
, NULL
);
63 return fdt32_to_cpu(*val
);
69 * fdt_find_and_setprop: Find a node and set it's property
71 * @fdt: ptr to device tree
73 * @prop: property name
74 * @val: ptr to new value
75 * @len: length of new property value
76 * @create: flag to create the property if it doesn't exist
78 * Convenience function to directly set a property given the path to the node.
80 int fdt_find_and_setprop(void *fdt
, const char *node
, const char *prop
,
81 const void *val
, int len
, int create
)
83 int nodeoff
= fdt_path_offset(fdt
, node
);
88 if ((!create
) && (fdt_get_property(fdt
, nodeoff
, prop
, NULL
) == NULL
))
89 return 0; /* create flag not set; so exit quietly */
91 return fdt_setprop(fdt
, nodeoff
, prop
, val
, len
);
94 #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
96 #ifdef CONFIG_CONS_INDEX
97 static void fdt_fill_multisername(char *sername
, size_t maxlen
)
99 const char *outname
= stdio_devices
[stdout
]->name
;
101 if (strcmp(outname
, "serial") > 0)
102 strncpy(sername
, outname
, maxlen
);
105 if (strcmp(outname
+ 1, "serial") > 0)
106 strncpy(sername
, outname
+ 1, maxlen
);
110 static int fdt_fixup_stdout(void *fdt
, int chosenoff
)
113 #ifdef CONFIG_CONS_INDEX
115 char sername
[9] = { 0 };
118 fdt_fill_multisername(sername
, sizeof(sername
) - 1);
120 sprintf(sername
, "serial%d", CONFIG_CONS_INDEX
- 1);
122 err
= node
= fdt_path_offset(fdt
, "/aliases");
125 path
= fdt_getprop(fdt
, node
, sername
, &len
);
127 char *p
= malloc(len
);
128 err
= -FDT_ERR_NOSPACE
;
130 memcpy(p
, path
, len
);
131 err
= fdt_setprop(fdt
, chosenoff
,
132 "linux,stdout-path", p
, len
);
141 printf("WARNING: could not set linux,stdout-path %s.\n",
148 int fdt_initrd(void *fdt
, ulong initrd_start
, ulong initrd_end
, int force
)
156 /* Find the "chosen" node. */
157 nodeoffset
= fdt_path_offset (fdt
, "/chosen");
159 /* If there is no "chosen" node in the blob return */
160 if (nodeoffset
< 0) {
161 printf("fdt_initrd: %s\n", fdt_strerror(nodeoffset
));
165 /* just return if initrd_start/end aren't valid */
166 if ((initrd_start
== 0) || (initrd_end
== 0))
169 total
= fdt_num_mem_rsv(fdt
);
172 * Look for an existing entry and update it. If we don't find
173 * the entry, we will j be the next available slot.
175 for (j
= 0; j
< total
; j
++) {
176 err
= fdt_get_mem_rsv(fdt
, j
, &addr
, &size
);
177 if (addr
== initrd_start
) {
178 fdt_del_mem_rsv(fdt
, j
);
183 err
= fdt_add_mem_rsv(fdt
, initrd_start
, initrd_end
- initrd_start
);
185 printf("fdt_initrd: %s\n", fdt_strerror(err
));
189 path
= fdt_getprop(fdt
, nodeoffset
, "linux,initrd-start", NULL
);
190 if ((path
== NULL
) || force
) {
191 tmp
= cpu_to_fdt32(initrd_start
);
192 err
= fdt_setprop(fdt
, nodeoffset
,
193 "linux,initrd-start", &tmp
, sizeof(tmp
));
196 "could not set linux,initrd-start %s.\n",
200 tmp
= cpu_to_fdt32(initrd_end
);
201 err
= fdt_setprop(fdt
, nodeoffset
,
202 "linux,initrd-end", &tmp
, sizeof(tmp
));
204 printf("WARNING: could not set linux,initrd-end %s.\n",
214 int fdt_chosen(void *fdt
, int force
)
218 char *str
; /* used to set string properties */
221 err
= fdt_check_header(fdt
);
223 printf("fdt_chosen: %s\n", fdt_strerror(err
));
228 * Find the "chosen" node.
230 nodeoffset
= fdt_path_offset (fdt
, "/chosen");
233 * If there is no "chosen" node in the blob, create it.
235 if (nodeoffset
< 0) {
237 * Create a new node "/chosen" (offset 0 is root level)
239 nodeoffset
= fdt_add_subnode(fdt
, 0, "chosen");
240 if (nodeoffset
< 0) {
241 printf("WARNING: could not create /chosen %s.\n",
242 fdt_strerror(nodeoffset
));
248 * Create /chosen properites that don't exist in the fdt.
249 * If the property exists, update it only if the "force" parameter
252 str
= getenv("bootargs");
254 path
= fdt_getprop(fdt
, nodeoffset
, "bootargs", NULL
);
255 if ((path
== NULL
) || force
) {
256 err
= fdt_setprop(fdt
, nodeoffset
,
257 "bootargs", str
, strlen(str
)+1);
259 printf("WARNING: could not set bootargs %s.\n",
264 #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
265 path
= fdt_getprop(fdt
, nodeoffset
, "linux,stdout-path", NULL
);
266 if ((path
== NULL
) || force
)
267 err
= fdt_fixup_stdout(fdt
, nodeoffset
);
270 #ifdef OF_STDOUT_PATH
271 path
= fdt_getprop(fdt
, nodeoffset
, "linux,stdout-path", NULL
);
272 if ((path
== NULL
) || force
) {
273 err
= fdt_setprop(fdt
, nodeoffset
,
274 "linux,stdout-path", OF_STDOUT_PATH
, strlen(OF_STDOUT_PATH
)+1);
276 printf("WARNING: could not set linux,stdout-path %s.\n",
284 void do_fixup_by_path(void *fdt
, const char *path
, const char *prop
,
285 const void *val
, int len
, int create
)
289 debug("Updating property '%s/%s' = ", path
, prop
);
290 for (i
= 0; i
< len
; i
++)
291 debug(" %.2x", *(u8
*)(val
+i
));
294 int rc
= fdt_find_and_setprop(fdt
, path
, prop
, val
, len
, create
);
296 printf("Unable to update property %s:%s, err=%s\n",
297 path
, prop
, fdt_strerror(rc
));
300 void do_fixup_by_path_u32(void *fdt
, const char *path
, const char *prop
,
303 fdt32_t tmp
= cpu_to_fdt32(val
);
304 do_fixup_by_path(fdt
, path
, prop
, &tmp
, sizeof(tmp
), create
);
307 void do_fixup_by_prop(void *fdt
,
308 const char *pname
, const void *pval
, int plen
,
309 const char *prop
, const void *val
, int len
,
315 debug("Updating property '%s' = ", prop
);
316 for (i
= 0; i
< len
; i
++)
317 debug(" %.2x", *(u8
*)(val
+i
));
320 off
= fdt_node_offset_by_prop_value(fdt
, -1, pname
, pval
, plen
);
321 while (off
!= -FDT_ERR_NOTFOUND
) {
322 if (create
|| (fdt_get_property(fdt
, off
, prop
, NULL
) != NULL
))
323 fdt_setprop(fdt
, off
, prop
, val
, len
);
324 off
= fdt_node_offset_by_prop_value(fdt
, off
, pname
, pval
, plen
);
328 void do_fixup_by_prop_u32(void *fdt
,
329 const char *pname
, const void *pval
, int plen
,
330 const char *prop
, u32 val
, int create
)
332 fdt32_t tmp
= cpu_to_fdt32(val
);
333 do_fixup_by_prop(fdt
, pname
, pval
, plen
, prop
, &tmp
, 4, create
);
336 void do_fixup_by_compat(void *fdt
, const char *compat
,
337 const char *prop
, const void *val
, int len
, int create
)
342 debug("Updating property '%s' = ", prop
);
343 for (i
= 0; i
< len
; i
++)
344 debug(" %.2x", *(u8
*)(val
+i
));
347 off
= fdt_node_offset_by_compatible(fdt
, -1, compat
);
348 while (off
!= -FDT_ERR_NOTFOUND
) {
349 if (create
|| (fdt_get_property(fdt
, off
, prop
, NULL
) != NULL
))
350 fdt_setprop(fdt
, off
, prop
, val
, len
);
351 off
= fdt_node_offset_by_compatible(fdt
, off
, compat
);
355 void do_fixup_by_compat_u32(void *fdt
, const char *compat
,
356 const char *prop
, u32 val
, int create
)
358 fdt32_t tmp
= cpu_to_fdt32(val
);
359 do_fixup_by_compat(fdt
, compat
, prop
, &tmp
, 4, create
);
363 * Get cells len in bytes
364 * if #NNNN-cells property is 2 then len is 8
367 static int get_cells_len(void *blob
, char *nr_cells_name
)
371 cell
= fdt_getprop(blob
, 0, nr_cells_name
, NULL
);
372 if (cell
&& fdt32_to_cpu(*cell
) == 2)
379 * Write a 4 or 8 byte big endian cell
381 static void write_cell(u8
*addr
, u64 val
, int size
)
383 int shift
= (size
- 1) * 8;
385 *addr
++ = (val
>> shift
) & 0xff;
390 #ifdef CONFIG_NR_DRAM_BANKS
391 #define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS
393 #define MEMORY_BANKS_MAX 4
395 int fdt_fixup_memory_banks(void *blob
, u64 start
[], u64 size
[], int banks
)
398 int addr_cell_len
, size_cell_len
, len
;
399 u8 tmp
[MEMORY_BANKS_MAX
* 16]; /* Up to 64-bit address + 64-bit size */
402 if (banks
> MEMORY_BANKS_MAX
) {
403 printf("%s: num banks %d exceeds hardcoded limit %d."
404 " Recompile with higher MEMORY_BANKS_MAX?\n",
405 __FUNCTION__
, banks
, MEMORY_BANKS_MAX
);
409 err
= fdt_check_header(blob
);
411 printf("%s: %s\n", __FUNCTION__
, fdt_strerror(err
));
415 /* update, or add and update /memory node */
416 nodeoffset
= fdt_path_offset(blob
, "/memory");
417 if (nodeoffset
< 0) {
418 nodeoffset
= fdt_add_subnode(blob
, 0, "memory");
420 printf("WARNING: could not create /memory: %s.\n",
421 fdt_strerror(nodeoffset
));
424 err
= fdt_setprop(blob
, nodeoffset
, "device_type", "memory",
427 printf("WARNING: could not set %s %s.\n", "device_type",
432 addr_cell_len
= get_cells_len(blob
, "#address-cells");
433 size_cell_len
= get_cells_len(blob
, "#size-cells");
435 for (bank
= 0, len
= 0; bank
< banks
; bank
++) {
436 write_cell(tmp
+ len
, start
[bank
], addr_cell_len
);
437 len
+= addr_cell_len
;
439 write_cell(tmp
+ len
, size
[bank
], size_cell_len
);
440 len
+= size_cell_len
;
443 err
= fdt_setprop(blob
, nodeoffset
, "reg", tmp
, len
);
445 printf("WARNING: could not set %s %s.\n",
446 "reg", fdt_strerror(err
));
452 int fdt_fixup_memory(void *blob
, u64 start
, u64 size
)
454 return fdt_fixup_memory_banks(blob
, &start
, &size
, 1);
457 void fdt_fixup_ethernet(void *fdt
)
460 char enet
[16], *tmp
, *end
;
463 unsigned char mac_addr
[6];
465 node
= fdt_path_offset(fdt
, "/aliases");
470 strcpy(mac
, "ethaddr");
471 while ((tmp
= getenv(mac
)) != NULL
) {
472 sprintf(enet
, "ethernet%d", i
);
473 path
= fdt_getprop(fdt
, node
, enet
, NULL
);
475 debug("No alias for %s\n", enet
);
476 sprintf(mac
, "eth%daddr", ++i
);
480 for (j
= 0; j
< 6; j
++) {
481 mac_addr
[j
] = tmp
? simple_strtoul(tmp
, &end
, 16) : 0;
483 tmp
= (*end
) ? end
+1 : end
;
486 do_fixup_by_path(fdt
, path
, "mac-address", &mac_addr
, 6, 0);
487 do_fixup_by_path(fdt
, path
, "local-mac-address",
490 sprintf(mac
, "eth%daddr", ++i
);
494 /* Resize the fdt to its actual size + a bit of padding */
495 int fdt_resize(void *blob
)
505 total
= fdt_num_mem_rsv(blob
);
506 for (i
= 0; i
< total
; i
++) {
507 fdt_get_mem_rsv(blob
, i
, &addr
, &size
);
508 if (addr
== (uintptr_t)blob
) {
509 fdt_del_mem_rsv(blob
, i
);
515 * Calculate the actual size of the fdt
516 * plus the size needed for 5 fdt_add_mem_rsv, one
517 * for the fdt itself and 4 for a possible initrd
518 * ((initrd-start + initrd-end) * 2 (name & value))
520 actualsize
= fdt_off_dt_strings(blob
) +
521 fdt_size_dt_strings(blob
) + 5 * sizeof(struct fdt_reserve_entry
);
523 /* Make it so the fdt ends on a page boundary */
524 actualsize
= ALIGN(actualsize
+ ((uintptr_t)blob
& 0xfff), 0x1000);
525 actualsize
= actualsize
- ((uintptr_t)blob
& 0xfff);
527 /* Change the fdt header to reflect the correct size */
528 fdt_set_totalsize(blob
, actualsize
);
530 /* Add the new reservation */
531 ret
= fdt_add_mem_rsv(blob
, (uintptr_t)blob
, actualsize
);
539 #define CONFIG_SYS_PCI_NR_INBOUND_WIN 4
541 #define FDT_PCI_PREFETCH (0x40000000)
542 #define FDT_PCI_MEM32 (0x02000000)
543 #define FDT_PCI_IO (0x01000000)
544 #define FDT_PCI_MEM64 (0x03000000)
546 int fdt_pci_dma_ranges(void *blob
, int phb_off
, struct pci_controller
*hose
) {
548 int addrcell
, sizecell
, len
, r
;
550 /* sized based on pci addr cells, size-cells, & address-cells */
551 u32 dma_ranges
[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN
];
553 addrcell
= fdt_getprop_u32_default(blob
, "/", "#address-cells", 1);
554 sizecell
= fdt_getprop_u32_default(blob
, "/", "#size-cells", 1);
556 dma_range
= &dma_ranges
[0];
557 for (r
= 0; r
< hose
->region_count
; r
++) {
558 u64 bus_start
, phys_start
, size
;
560 /* skip if !PCI_REGION_SYS_MEMORY */
561 if (!(hose
->regions
[r
].flags
& PCI_REGION_SYS_MEMORY
))
564 bus_start
= (u64
)hose
->regions
[r
].bus_start
;
565 phys_start
= (u64
)hose
->regions
[r
].phys_start
;
566 size
= (u64
)hose
->regions
[r
].size
;
569 if (size
>= 0x100000000ull
)
570 dma_range
[0] |= FDT_PCI_MEM64
;
572 dma_range
[0] |= FDT_PCI_MEM32
;
573 if (hose
->regions
[r
].flags
& PCI_REGION_PREFETCH
)
574 dma_range
[0] |= FDT_PCI_PREFETCH
;
575 #ifdef CONFIG_SYS_PCI_64BIT
576 dma_range
[1] = bus_start
>> 32;
580 dma_range
[2] = bus_start
& 0xffffffff;
583 dma_range
[3] = phys_start
>> 32;
584 dma_range
[4] = phys_start
& 0xffffffff;
586 dma_range
[3] = phys_start
& 0xffffffff;
590 dma_range
[3 + addrcell
+ 0] = size
>> 32;
591 dma_range
[3 + addrcell
+ 1] = size
& 0xffffffff;
593 dma_range
[3 + addrcell
+ 0] = size
& 0xffffffff;
596 dma_range
+= (3 + addrcell
+ sizecell
);
599 len
= dma_range
- &dma_ranges
[0];
601 fdt_setprop(blob
, phb_off
, "dma-ranges", &dma_ranges
[0], len
*4);
607 #ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE
609 * Provide a weak default function to return the flash bank size.
610 * There might be multiple non-identical flash chips connected to one
611 * chip-select, so we need to pass an index as well.
613 u32
__flash_get_bank_size(int cs
, int idx
)
615 extern flash_info_t flash_info
[];
618 * As default, a simple 1:1 mapping is provided. Boards with
619 * a different mapping need to supply a board specific mapping
622 return flash_info
[cs
].size
;
624 u32
flash_get_bank_size(int cs
, int idx
)
625 __attribute__((weak
, alias("__flash_get_bank_size")));
628 * This function can be used to update the size in the "reg" property
629 * of all NOR FLASH device nodes. This is necessary for boards with
630 * non-fixed NOR FLASH sizes.
632 int fdt_fixup_nor_flash_size(void *blob
)
634 char compat
[][16] = { "cfi-flash", "jedec-flash" };
637 struct fdt_property
*prop
;
641 for (i
= 0; i
< 2; i
++) {
642 off
= fdt_node_offset_by_compatible(blob
, -1, compat
[i
]);
643 while (off
!= -FDT_ERR_NOTFOUND
) {
647 * Found one compatible node, so fixup the size
648 * int its reg properties
650 prop
= fdt_get_property_w(blob
, off
, "reg", &len
);
652 int tuple_size
= 3 * sizeof(reg
);
655 * There might be multiple reg-tuples,
656 * so loop through them all
658 reg
= reg2
= (u32
*)&prop
->data
[0];
659 for (idx
= 0; idx
< (len
/ tuple_size
); idx
++) {
661 * Update size in reg property
663 reg
[2] = flash_get_bank_size(reg
[0],
667 * Point to next reg tuple
672 fdt_setprop(blob
, off
, "reg", reg2
, len
);
675 /* Move to next compatible node */
676 off
= fdt_node_offset_by_compatible(blob
, off
,
685 int fdt_increase_size(void *fdt
, int add_len
)
689 newlen
= fdt_totalsize(fdt
) + add_len
;
691 /* Open in place with a new len */
692 return fdt_open_into(fdt
, fdt
, newlen
);
695 #ifdef CONFIG_FDT_FIXUP_PARTITIONS
696 #include <jffs2/load_kernel.h>
697 #include <mtd_node.h>
704 int fdt_del_subnodes(const void *blob
, int parent_offset
)
709 for (ndepth
= 0, off
= fdt_next_node(blob
, parent_offset
, &ndepth
);
710 (off
>= 0) && (ndepth
> 0);
711 off
= fdt_next_node(blob
, off
, &ndepth
)) {
713 debug("delete %s: offset: %x\n",
714 fdt_get_name(blob
, off
, 0), off
);
715 ret
= fdt_del_node((void *)blob
, off
);
717 printf("Can't delete node: %s\n",
729 int fdt_del_partitions(void *blob
, int parent_offset
)
736 off
= fdt_next_node(blob
, parent_offset
, &ndepth
);
737 if (off
> 0 && ndepth
== 1) {
738 prop
= fdt_getprop(blob
, off
, "label", NULL
);
741 * Could not find label property, nand {}; node?
742 * Check subnode, delete partitions there if any.
744 return fdt_del_partitions(blob
, off
);
746 ret
= fdt_del_subnodes(blob
, parent_offset
);
748 printf("Can't remove subnodes: %s\n",
757 int fdt_node_set_part_info(void *blob
, int parent_offset
,
758 struct mtd_device
*dev
)
760 struct list_head
*pentry
;
761 struct part_info
*part
;
762 struct reg_cell cell
;
767 ret
= fdt_del_partitions(blob
, parent_offset
);
772 * Check if it is nand {}; subnode, adjust
773 * the offset in this case
775 off
= fdt_next_node(blob
, parent_offset
, &ndepth
);
776 if (off
> 0 && ndepth
== 1)
780 list_for_each_prev(pentry
, &dev
->parts
) {
783 part
= list_entry(pentry
, struct part_info
, link
);
785 debug("%2d: %-20s0x%08x\t0x%08x\t%d\n",
786 part_num
, part
->name
, part
->size
,
787 part
->offset
, part
->mask_flags
);
789 sprintf(buf
, "partition@%x", part
->offset
);
791 ret
= fdt_add_subnode(blob
, parent_offset
, buf
);
792 if (ret
== -FDT_ERR_NOSPACE
) {
793 ret
= fdt_increase_size(blob
, 512);
798 } else if (ret
< 0) {
799 printf("Can't add partition node: %s\n",
805 /* Check MTD_WRITEABLE_CMD flag */
806 if (part
->mask_flags
& 1) {
808 ret
= fdt_setprop(blob
, newoff
, "read_only", NULL
, 0);
809 if (ret
== -FDT_ERR_NOSPACE
) {
810 ret
= fdt_increase_size(blob
, 512);
819 cell
.r0
= cpu_to_fdt32(part
->offset
);
820 cell
.r1
= cpu_to_fdt32(part
->size
);
822 ret
= fdt_setprop(blob
, newoff
, "reg", &cell
, sizeof(cell
));
823 if (ret
== -FDT_ERR_NOSPACE
) {
824 ret
= fdt_increase_size(blob
, 512);
833 ret
= fdt_setprop_string(blob
, newoff
, "label", part
->name
);
834 if (ret
== -FDT_ERR_NOSPACE
) {
835 ret
= fdt_increase_size(blob
, 512);
847 printf("Can't increase blob size: %s\n", fdt_strerror(ret
));
850 printf("Can't add property: %s\n", fdt_strerror(ret
));
855 * Update partitions in nor/nand nodes using info from
856 * mtdparts environment variable. The nodes to update are
857 * specified by node_info structure which contains mtd device
858 * type and compatible string: E. g. the board code in
859 * ft_board_setup() could use:
861 * struct node_info nodes[] = {
862 * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
863 * { "cfi-flash", MTD_DEV_TYPE_NOR, },
866 * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
868 void fdt_fixup_mtdparts(void *blob
, void *node_info
, int node_info_size
)
870 struct node_info
*ni
= node_info
;
871 struct mtd_device
*dev
;
876 parts
= getenv("mtdparts");
880 if (mtdparts_init() != 0)
883 for (i
= 0; i
< node_info_size
; i
++) {
885 noff
= fdt_node_offset_by_compatible(blob
, -1, ni
[i
].compat
);
886 while (noff
!= -FDT_ERR_NOTFOUND
) {
887 debug("%s: %s, mtd dev type %d\n",
888 fdt_get_name(blob
, noff
, 0),
889 ni
[i
].compat
, ni
[i
].type
);
890 dev
= device_find(ni
[i
].type
, idx
++);
892 if (fdt_node_set_part_info(blob
, noff
, dev
))
893 return; /* return on error */
896 /* Jump to next flash node */
897 noff
= fdt_node_offset_by_compatible(blob
, noff
,
904 void fdt_del_node_and_alias(void *blob
, const char *alias
)
906 int off
= fdt_path_offset(blob
, alias
);
911 fdt_del_node(blob
, off
);
913 off
= fdt_path_offset(blob
, "/aliases");
914 fdt_delprop(blob
, off
, alias
);
917 /* Helper to read a big number; size is in cells (not bytes) */
918 static inline u64
of_read_number(const fdt32_t
*cell
, int size
)
922 r
= (r
<< 32) | fdt32_to_cpu(*(cell
++));
928 /* Max address size we deal with */
929 #define OF_MAX_ADDR_CELLS 4
930 #define OF_BAD_ADDR ((u64)-1)
931 #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
936 static void of_dump_addr(const char *s
, const fdt32_t
*addr
, int na
)
940 printf(" %08x", *(addr
++));
944 static void of_dump_addr(const char *s
, const fdt32_t
*addr
, int na
) { }
947 /* Callbacks for bus specific translators */
950 const char *addresses
;
951 void (*count_cells
)(void *blob
, int parentoffset
,
952 int *addrc
, int *sizec
);
953 u64 (*map
)(fdt32_t
*addr
, const fdt32_t
*range
,
954 int na
, int ns
, int pna
);
955 int (*translate
)(fdt32_t
*addr
, u64 offset
, int na
);
958 /* Default translator (generic bus) */
959 static void of_bus_default_count_cells(void *blob
, int parentoffset
,
960 int *addrc
, int *sizec
)
965 prop
= fdt_getprop(blob
, parentoffset
, "#address-cells", NULL
);
967 *addrc
= be32_to_cpup(prop
);
973 prop
= fdt_getprop(blob
, parentoffset
, "#size-cells", NULL
);
975 *sizec
= be32_to_cpup(prop
);
981 static u64
of_bus_default_map(fdt32_t
*addr
, const fdt32_t
*range
,
982 int na
, int ns
, int pna
)
986 cp
= of_read_number(range
, na
);
987 s
= of_read_number(range
+ na
+ pna
, ns
);
988 da
= of_read_number(addr
, na
);
990 debug("OF: default map, cp="PRu64
", s="PRu64
", da="PRu64
"\n",
993 if (da
< cp
|| da
>= (cp
+ s
))
998 static int of_bus_default_translate(fdt32_t
*addr
, u64 offset
, int na
)
1000 u64 a
= of_read_number(addr
, na
);
1001 memset(addr
, 0, na
* 4);
1004 addr
[na
- 2] = cpu_to_fdt32(a
>> 32);
1005 addr
[na
- 1] = cpu_to_fdt32(a
& 0xffffffffu
);
1010 /* Array of bus specific translators */
1011 static struct of_bus of_busses
[] = {
1016 .count_cells
= of_bus_default_count_cells
,
1017 .map
= of_bus_default_map
,
1018 .translate
= of_bus_default_translate
,
1022 static int of_translate_one(void * blob
, int parent
, struct of_bus
*bus
,
1023 struct of_bus
*pbus
, fdt32_t
*addr
,
1024 int na
, int ns
, int pna
, const char *rprop
)
1026 const fdt32_t
*ranges
;
1029 u64 offset
= OF_BAD_ADDR
;
1031 /* Normally, an absence of a "ranges" property means we are
1032 * crossing a non-translatable boundary, and thus the addresses
1033 * below the current not cannot be converted to CPU physical ones.
1034 * Unfortunately, while this is very clear in the spec, it's not
1035 * what Apple understood, and they do have things like /uni-n or
1036 * /ht nodes with no "ranges" property and a lot of perfectly
1037 * useable mapped devices below them. Thus we treat the absence of
1038 * "ranges" as equivalent to an empty "ranges" property which means
1039 * a 1:1 translation at that level. It's up to the caller not to try
1040 * to translate addresses that aren't supposed to be translated in
1041 * the first place. --BenH.
1043 ranges
= fdt_getprop(blob
, parent
, rprop
, &rlen
);
1044 if (ranges
== NULL
|| rlen
== 0) {
1045 offset
= of_read_number(addr
, na
);
1046 memset(addr
, 0, pna
* 4);
1047 debug("OF: no ranges, 1:1 translation\n");
1051 debug("OF: walking ranges...\n");
1053 /* Now walk through the ranges */
1055 rone
= na
+ pna
+ ns
;
1056 for (; rlen
>= rone
; rlen
-= rone
, ranges
+= rone
) {
1057 offset
= bus
->map(addr
, ranges
, na
, ns
, pna
);
1058 if (offset
!= OF_BAD_ADDR
)
1061 if (offset
== OF_BAD_ADDR
) {
1062 debug("OF: not found !\n");
1065 memcpy(addr
, ranges
+ na
, 4 * pna
);
1068 of_dump_addr("OF: parent translation for:", addr
, pna
);
1069 debug("OF: with offset: "PRu64
"\n", offset
);
1071 /* Translate it into parent bus space */
1072 return pbus
->translate(addr
, offset
, pna
);
1076 * Translate an address from the device-tree into a CPU physical address,
1077 * this walks up the tree and applies the various bus mappings on the
1080 * Note: We consider that crossing any level with #size-cells == 0 to mean
1081 * that translation is impossible (that is we are not dealing with a value
1082 * that can be mapped to a cpu physical address). This is not really specified
1083 * that way, but this is traditionally the way IBM at least do things
1085 static u64
__of_translate_address(void *blob
, int node_offset
, const fdt32_t
*in_addr
,
1089 struct of_bus
*bus
, *pbus
;
1090 fdt32_t addr
[OF_MAX_ADDR_CELLS
];
1091 int na
, ns
, pna
, pns
;
1092 u64 result
= OF_BAD_ADDR
;
1094 debug("OF: ** translation for device %s **\n",
1095 fdt_get_name(blob
, node_offset
, NULL
));
1097 /* Get parent & match bus type */
1098 parent
= fdt_parent_offset(blob
, node_offset
);
1101 bus
= &of_busses
[0];
1103 /* Cound address cells & copy address locally */
1104 bus
->count_cells(blob
, parent
, &na
, &ns
);
1105 if (!OF_CHECK_COUNTS(na
, ns
)) {
1106 printf("%s: Bad cell count for %s\n", __FUNCTION__
,
1107 fdt_get_name(blob
, node_offset
, NULL
));
1110 memcpy(addr
, in_addr
, na
* 4);
1112 debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
1113 bus
->name
, na
, ns
, fdt_get_name(blob
, parent
, NULL
));
1114 of_dump_addr("OF: translating address:", addr
, na
);
1118 /* Switch to parent bus */
1119 node_offset
= parent
;
1120 parent
= fdt_parent_offset(blob
, node_offset
);
1122 /* If root, we have finished */
1124 debug("OF: reached root node\n");
1125 result
= of_read_number(addr
, na
);
1129 /* Get new parent bus and counts */
1130 pbus
= &of_busses
[0];
1131 pbus
->count_cells(blob
, parent
, &pna
, &pns
);
1132 if (!OF_CHECK_COUNTS(pna
, pns
)) {
1133 printf("%s: Bad cell count for %s\n", __FUNCTION__
,
1134 fdt_get_name(blob
, node_offset
, NULL
));
1138 debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
1139 pbus
->name
, pna
, pns
, fdt_get_name(blob
, parent
, NULL
));
1141 /* Apply bus translation */
1142 if (of_translate_one(blob
, node_offset
, bus
, pbus
,
1143 addr
, na
, ns
, pna
, rprop
))
1146 /* Complete the move up one level */
1151 of_dump_addr("OF: one level translation:", addr
, na
);
1158 u64
fdt_translate_address(void *blob
, int node_offset
, const fdt32_t
*in_addr
)
1160 return __of_translate_address(blob
, node_offset
, in_addr
, "ranges");
1164 * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and
1165 * who's reg property matches a physical cpu address
1167 * @blob: ptr to device tree
1168 * @compat: compatiable string to match
1169 * @compat_off: property name
1172 int fdt_node_offset_by_compat_reg(void *blob
, const char *compat
,
1173 phys_addr_t compat_off
)
1175 int len
, off
= fdt_node_offset_by_compatible(blob
, -1, compat
);
1176 while (off
!= -FDT_ERR_NOTFOUND
) {
1177 const fdt32_t
*reg
= fdt_getprop(blob
, off
, "reg", &len
);
1179 if (compat_off
== fdt_translate_address(blob
, off
, reg
))
1182 off
= fdt_node_offset_by_compatible(blob
, off
, compat
);
1185 return -FDT_ERR_NOTFOUND
;
1189 * fdt_alloc_phandle: Return next free phandle value
1191 * @blob: ptr to device tree
1193 int fdt_alloc_phandle(void *blob
)
1195 int offset
, phandle
= 0;
1197 for (offset
= fdt_next_node(blob
, -1, NULL
); offset
>= 0;
1198 offset
= fdt_next_node(blob
, offset
, NULL
)) {
1199 phandle
= max(phandle
, fdt_get_phandle(blob
, offset
));
1206 * fdt_set_phandle: Create a phandle property for the given node
1208 * @fdt: ptr to device tree
1209 * @nodeoffset: node to update
1210 * @phandle: phandle value to set (must be unique)
1212 int fdt_set_phandle(void *fdt
, int nodeoffset
, uint32_t phandle
)
1217 int off
= fdt_node_offset_by_phandle(fdt
, phandle
);
1219 if ((off
>= 0) && (off
!= nodeoffset
)) {
1222 fdt_get_path(fdt
, nodeoffset
, buf
, sizeof(buf
));
1223 printf("Trying to update node %s with phandle %u ",
1226 fdt_get_path(fdt
, off
, buf
, sizeof(buf
));
1227 printf("that already exists in node %s.\n", buf
);
1228 return -FDT_ERR_BADPHANDLE
;
1232 ret
= fdt_setprop_cell(fdt
, nodeoffset
, "phandle", phandle
);
1237 * For now, also set the deprecated "linux,phandle" property, so that we
1238 * don't break older kernels.
1240 ret
= fdt_setprop_cell(fdt
, nodeoffset
, "linux,phandle", phandle
);
1246 * fdt_create_phandle: Create a phandle property for the given node
1248 * @fdt: ptr to device tree
1249 * @nodeoffset: node to update
1251 unsigned int fdt_create_phandle(void *fdt
, int nodeoffset
)
1253 /* see if there is a phandle already */
1254 int phandle
= fdt_get_phandle(fdt
, nodeoffset
);
1256 /* if we got 0, means no phandle so create one */
1260 phandle
= fdt_alloc_phandle(fdt
);
1261 ret
= fdt_set_phandle(fdt
, nodeoffset
, phandle
);
1263 printf("Can't set phandle %u: %s\n", phandle
,
1273 * fdt_set_node_status: Set status for the given node
1275 * @fdt: ptr to device tree
1276 * @nodeoffset: node to update
1277 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1278 * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1279 * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1281 int fdt_set_node_status(void *fdt
, int nodeoffset
,
1282 enum fdt_status status
, unsigned int error_code
)
1291 case FDT_STATUS_OKAY
:
1292 ret
= fdt_setprop_string(fdt
, nodeoffset
, "status", "okay");
1294 case FDT_STATUS_DISABLED
:
1295 ret
= fdt_setprop_string(fdt
, nodeoffset
, "status", "disabled");
1297 case FDT_STATUS_FAIL
:
1298 ret
= fdt_setprop_string(fdt
, nodeoffset
, "status", "fail");
1300 case FDT_STATUS_FAIL_ERROR_CODE
:
1301 sprintf(buf
, "fail-%d", error_code
);
1302 ret
= fdt_setprop_string(fdt
, nodeoffset
, "status", buf
);
1305 printf("Invalid fdt status: %x\n", status
);
1314 * fdt_set_status_by_alias: Set status for the given node given an alias
1316 * @fdt: ptr to device tree
1317 * @alias: alias of node to update
1318 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1319 * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1320 * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1322 int fdt_set_status_by_alias(void *fdt
, const char* alias
,
1323 enum fdt_status status
, unsigned int error_code
)
1325 int offset
= fdt_path_offset(fdt
, alias
);
1327 return fdt_set_node_status(fdt
, offset
, status
, error_code
);
1330 #if defined(CONFIG_VIDEO) || defined(CONFIG_LCD)
1331 int fdt_add_edid(void *blob
, const char *compat
, unsigned char *edid_buf
)
1336 noff
= fdt_node_offset_by_compatible(blob
, -1, compat
);
1337 if (noff
!= -FDT_ERR_NOTFOUND
) {
1338 debug("%s: %s\n", fdt_get_name(blob
, noff
, 0), compat
);
1340 ret
= fdt_setprop(blob
, noff
, "edid", edid_buf
, 128);
1341 if (ret
== -FDT_ERR_NOSPACE
) {
1342 ret
= fdt_increase_size(blob
, 512);
1347 } else if (ret
< 0) {
1348 printf("Can't add property: %s\n", fdt_strerror(ret
));
1354 printf("Can't increase blob size: %s\n", fdt_strerror(ret
));
1360 * Verify the physical address of device tree node for a given alias
1362 * This function locates the device tree node of a given alias, and then
1363 * verifies that the physical address of that device matches the given
1364 * parameter. It displays a message if there is a mismatch.
1366 * Returns 1 on success, 0 on failure
1368 int fdt_verify_alias_address(void *fdt
, int anode
, const char *alias
, u64 addr
)
1375 path
= fdt_getprop(fdt
, anode
, alias
, NULL
);
1377 /* If there's no such alias, then it's not a failure */
1381 node
= fdt_path_offset(fdt
, path
);
1383 printf("Warning: device tree alias '%s' points to invalid "
1384 "node %s.\n", alias
, path
);
1388 reg
= fdt_getprop(fdt
, node
, "reg", &len
);
1390 printf("Warning: device tree node '%s' has no address.\n",
1395 dt_addr
= fdt_translate_address(fdt
, node
, reg
);
1396 if (addr
!= dt_addr
) {
1397 printf("Warning: U-Boot configured device %s at address %llx,\n"
1398 " but the device tree has it address %llx.\n",
1399 alias
, addr
, dt_addr
);
1407 * Returns the base address of an SOC or PCI node
1409 u64
fdt_get_base_address(void *fdt
, int node
)
1413 const fdt32_t
*prop
;
1415 prop
= fdt_getprop(fdt
, node
, "#address-cells", &size
);
1416 if (prop
&& size
== 4)
1417 naddr
= be32_to_cpup(prop
);
1421 prop
= fdt_getprop(fdt
, node
, "ranges", &size
);
1423 return prop
? fdt_translate_address(fdt
, node
, prop
+ naddr
) : 0;