3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
5 * Copyright 2010 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(void *fdt, const char *path, const char *prop,
58 off = fdt_path_offset(fdt, path);
62 val = fdt_getprop(fdt, off, prop, NULL);
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_SERIAL_MULTI
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);
110 static inline void fdt_fill_multisername(char *sername, size_t maxlen) {}
111 #endif /* CONFIG_SERIAL_MULTI */
113 static int fdt_fixup_stdout(void *fdt, int chosenoff)
116 #ifdef CONFIG_CONS_INDEX
118 char sername[9] = { 0 };
121 fdt_fill_multisername(sername, sizeof(sername) - 1);
123 sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
125 err = node = fdt_path_offset(fdt, "/aliases");
128 path = fdt_getprop(fdt, node, sername, &len);
130 char *p = malloc(len);
131 err = -FDT_ERR_NOSPACE;
133 memcpy(p, path, len);
134 err = fdt_setprop(fdt, chosenoff,
135 "linux,stdout-path", p, len);
144 printf("WARNING: could not set linux,stdout-path %s.\n",
151 int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end, int force)
159 /* Find the "chosen" node. */
160 nodeoffset = fdt_path_offset (fdt, "/chosen");
162 /* If there is no "chosen" node in the blob return */
163 if (nodeoffset < 0) {
164 printf("fdt_initrd: %s\n", fdt_strerror(nodeoffset));
168 /* just return if initrd_start/end aren't valid */
169 if ((initrd_start == 0) || (initrd_end == 0))
172 total = fdt_num_mem_rsv(fdt);
175 * Look for an existing entry and update it. If we don't find
176 * the entry, we will j be the next available slot.
178 for (j = 0; j < total; j++) {
179 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
180 if (addr == initrd_start) {
181 fdt_del_mem_rsv(fdt, j);
186 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start + 1);
188 printf("fdt_initrd: %s\n", fdt_strerror(err));
192 path = fdt_getprop(fdt, nodeoffset, "linux,initrd-start", NULL);
193 if ((path == NULL) || force) {
194 tmp = __cpu_to_be32(initrd_start);
195 err = fdt_setprop(fdt, nodeoffset,
196 "linux,initrd-start", &tmp, sizeof(tmp));
199 "could not set linux,initrd-start %s.\n",
203 tmp = __cpu_to_be32(initrd_end);
204 err = fdt_setprop(fdt, nodeoffset,
205 "linux,initrd-end", &tmp, sizeof(tmp));
207 printf("WARNING: could not set linux,initrd-end %s.\n",
217 int fdt_chosen(void *fdt, int force)
221 char *str; /* used to set string properties */
224 err = fdt_check_header(fdt);
226 printf("fdt_chosen: %s\n", fdt_strerror(err));
231 * Find the "chosen" node.
233 nodeoffset = fdt_path_offset (fdt, "/chosen");
236 * If there is no "chosen" node in the blob, create it.
238 if (nodeoffset < 0) {
240 * Create a new node "/chosen" (offset 0 is root level)
242 nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
243 if (nodeoffset < 0) {
244 printf("WARNING: could not create /chosen %s.\n",
245 fdt_strerror(nodeoffset));
251 * Create /chosen properites that don't exist in the fdt.
252 * If the property exists, update it only if the "force" parameter
255 str = getenv("bootargs");
257 path = fdt_getprop(fdt, nodeoffset, "bootargs", NULL);
258 if ((path == NULL) || force) {
259 err = fdt_setprop(fdt, nodeoffset,
260 "bootargs", str, strlen(str)+1);
262 printf("WARNING: could not set bootargs %s.\n",
267 #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
268 path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
269 if ((path == NULL) || force)
270 err = fdt_fixup_stdout(fdt, nodeoffset);
273 #ifdef OF_STDOUT_PATH
274 path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
275 if ((path == NULL) || force) {
276 err = fdt_setprop(fdt, nodeoffset,
277 "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
279 printf("WARNING: could not set linux,stdout-path %s.\n",
287 void do_fixup_by_path(void *fdt, const char *path, const char *prop,
288 const void *val, int len, int create)
292 debug("Updating property '%s/%s' = ", path, prop);
293 for (i = 0; i < len; i++)
294 debug(" %.2x", *(u8*)(val+i));
297 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
299 printf("Unable to update property %s:%s, err=%s\n",
300 path, prop, fdt_strerror(rc));
303 void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
306 val = cpu_to_fdt32(val);
307 do_fixup_by_path(fdt, path, prop, &val, sizeof(val), create);
310 void do_fixup_by_prop(void *fdt,
311 const char *pname, const void *pval, int plen,
312 const char *prop, const void *val, int len,
318 debug("Updating property '%s' = ", prop);
319 for (i = 0; i < len; i++)
320 debug(" %.2x", *(u8*)(val+i));
323 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
324 while (off != -FDT_ERR_NOTFOUND) {
325 if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
326 fdt_setprop(fdt, off, prop, val, len);
327 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
331 void do_fixup_by_prop_u32(void *fdt,
332 const char *pname, const void *pval, int plen,
333 const char *prop, u32 val, int create)
335 val = cpu_to_fdt32(val);
336 do_fixup_by_prop(fdt, pname, pval, plen, prop, &val, 4, create);
339 void do_fixup_by_compat(void *fdt, const char *compat,
340 const char *prop, const void *val, int len, int create)
345 debug("Updating property '%s' = ", prop);
346 for (i = 0; i < len; i++)
347 debug(" %.2x", *(u8*)(val+i));
350 off = fdt_node_offset_by_compatible(fdt, -1, compat);
351 while (off != -FDT_ERR_NOTFOUND) {
352 if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
353 fdt_setprop(fdt, off, prop, val, len);
354 off = fdt_node_offset_by_compatible(fdt, off, compat);
358 void do_fixup_by_compat_u32(void *fdt, const char *compat,
359 const char *prop, u32 val, int create)
361 val = cpu_to_fdt32(val);
362 do_fixup_by_compat(fdt, compat, prop, &val, 4, create);
365 int fdt_fixup_memory(void *blob, u64 start, u64 size)
367 int err, nodeoffset, len = 0;
369 const u32 *addrcell, *sizecell;
371 err = fdt_check_header(blob);
373 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
377 /* update, or add and update /memory node */
378 nodeoffset = fdt_path_offset(blob, "/memory");
379 if (nodeoffset < 0) {
380 nodeoffset = fdt_add_subnode(blob, 0, "memory");
382 printf("WARNING: could not create /memory: %s.\n",
383 fdt_strerror(nodeoffset));
386 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
389 printf("WARNING: could not set %s %s.\n", "device_type",
394 addrcell = fdt_getprop(blob, 0, "#address-cells", NULL);
395 /* use shifts and mask to ensure endianness */
396 if ((addrcell) && (*addrcell == 2)) {
397 tmp[0] = (start >> 56) & 0xff;
398 tmp[1] = (start >> 48) & 0xff;
399 tmp[2] = (start >> 40) & 0xff;
400 tmp[3] = (start >> 32) & 0xff;
401 tmp[4] = (start >> 24) & 0xff;
402 tmp[5] = (start >> 16) & 0xff;
403 tmp[6] = (start >> 8) & 0xff;
404 tmp[7] = (start ) & 0xff;
407 tmp[0] = (start >> 24) & 0xff;
408 tmp[1] = (start >> 16) & 0xff;
409 tmp[2] = (start >> 8) & 0xff;
410 tmp[3] = (start ) & 0xff;
414 sizecell = fdt_getprop(blob, 0, "#size-cells", NULL);
415 /* use shifts and mask to ensure endianness */
416 if ((sizecell) && (*sizecell == 2)) {
417 tmp[0+len] = (size >> 56) & 0xff;
418 tmp[1+len] = (size >> 48) & 0xff;
419 tmp[2+len] = (size >> 40) & 0xff;
420 tmp[3+len] = (size >> 32) & 0xff;
421 tmp[4+len] = (size >> 24) & 0xff;
422 tmp[5+len] = (size >> 16) & 0xff;
423 tmp[6+len] = (size >> 8) & 0xff;
424 tmp[7+len] = (size ) & 0xff;
427 tmp[0+len] = (size >> 24) & 0xff;
428 tmp[1+len] = (size >> 16) & 0xff;
429 tmp[2+len] = (size >> 8) & 0xff;
430 tmp[3+len] = (size ) & 0xff;
434 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
436 printf("WARNING: could not set %s %s.\n",
437 "reg", fdt_strerror(err));
443 void fdt_fixup_ethernet(void *fdt)
446 char enet[16], *tmp, *end;
447 char mac[16] = "ethaddr";
449 unsigned char mac_addr[6];
451 node = fdt_path_offset(fdt, "/aliases");
456 while ((tmp = getenv(mac)) != NULL) {
457 sprintf(enet, "ethernet%d", i);
458 path = fdt_getprop(fdt, node, enet, NULL);
460 debug("No alias for %s\n", enet);
461 sprintf(mac, "eth%daddr", ++i);
465 for (j = 0; j < 6; j++) {
466 mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
468 tmp = (*end) ? end+1 : end;
471 do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0);
472 do_fixup_by_path(fdt, path, "local-mac-address",
475 sprintf(mac, "eth%daddr", ++i);
479 /* Resize the fdt to its actual size + a bit of padding */
480 int fdt_resize(void *blob)
490 total = fdt_num_mem_rsv(blob);
491 for (i = 0; i < total; i++) {
492 fdt_get_mem_rsv(blob, i, &addr, &size);
493 if (addr == (uint64_t)(u32)blob) {
494 fdt_del_mem_rsv(blob, i);
500 * Calculate the actual size of the fdt
501 * plus the size needed for 5 fdt_add_mem_rsv, one
502 * for the fdt itself and 4 for a possible initrd
503 * ((initrd-start + initrd-end) * 2 (name & value))
505 actualsize = fdt_off_dt_strings(blob) +
506 fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
508 /* Make it so the fdt ends on a page boundary */
509 actualsize = ALIGN(actualsize + ((uint)blob & 0xfff), 0x1000);
510 actualsize = actualsize - ((uint)blob & 0xfff);
512 /* Change the fdt header to reflect the correct size */
513 fdt_set_totalsize(blob, actualsize);
515 /* Add the new reservation */
516 ret = fdt_add_mem_rsv(blob, (uint)blob, actualsize);
524 #define CONFIG_SYS_PCI_NR_INBOUND_WIN 4
526 #define FDT_PCI_PREFETCH (0x40000000)
527 #define FDT_PCI_MEM32 (0x02000000)
528 #define FDT_PCI_IO (0x01000000)
529 #define FDT_PCI_MEM64 (0x03000000)
531 int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
533 int addrcell, sizecell, len, r;
535 /* sized based on pci addr cells, size-cells, & address-cells */
536 u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN];
538 addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
539 sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
541 dma_range = &dma_ranges[0];
542 for (r = 0; r < hose->region_count; r++) {
543 u64 bus_start, phys_start, size;
545 /* skip if !PCI_REGION_SYS_MEMORY */
546 if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
549 bus_start = (u64)hose->regions[r].bus_start;
550 phys_start = (u64)hose->regions[r].phys_start;
551 size = (u64)hose->regions[r].size;
554 if (size >= 0x100000000ull)
555 dma_range[0] |= FDT_PCI_MEM64;
557 dma_range[0] |= FDT_PCI_MEM32;
558 if (hose->regions[r].flags & PCI_REGION_PREFETCH)
559 dma_range[0] |= FDT_PCI_PREFETCH;
560 #ifdef CONFIG_SYS_PCI_64BIT
561 dma_range[1] = bus_start >> 32;
565 dma_range[2] = bus_start & 0xffffffff;
568 dma_range[3] = phys_start >> 32;
569 dma_range[4] = phys_start & 0xffffffff;
571 dma_range[3] = phys_start & 0xffffffff;
575 dma_range[3 + addrcell + 0] = size >> 32;
576 dma_range[3 + addrcell + 1] = size & 0xffffffff;
578 dma_range[3 + addrcell + 0] = size & 0xffffffff;
581 dma_range += (3 + addrcell + sizecell);
584 len = dma_range - &dma_ranges[0];
586 fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
592 #ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE
594 * This function can be used to update the size in the "reg" property
595 * of the NOR FLASH device nodes. This is necessary for boards with
596 * non-fixed NOR FLASH sizes.
598 int fdt_fixup_nor_flash_size(void *blob, int cs, u32 size)
600 char compat[][16] = { "cfi-flash", "jedec-flash" };
603 struct fdt_property *prop;
607 for (i = 0; i < 2; i++) {
608 off = fdt_node_offset_by_compatible(blob, -1, compat[i]);
609 while (off != -FDT_ERR_NOTFOUND) {
611 * Found one compatible node, now check if this one
614 prop = fdt_get_property_w(blob, off, "reg", &len);
616 reg = (u32 *)&prop->data[0];
619 fdt_setprop(blob, off, "reg", reg,
626 /* Move to next compatible node */
627 off = fdt_node_offset_by_compatible(blob, off,
636 #ifdef CONFIG_FDT_FIXUP_PARTITIONS
637 #include <jffs2/load_kernel.h>
638 #include <mtd_node.h>
645 int fdt_del_subnodes(const void *blob, int parent_offset)
650 for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
651 (off >= 0) && (ndepth > 0);
652 off = fdt_next_node(blob, off, &ndepth)) {
654 debug("delete %s: offset: %x\n",
655 fdt_get_name(blob, off, 0), off);
656 ret = fdt_del_node((void *)blob, off);
658 printf("Can't delete node: %s\n",
670 int fdt_increase_size(void *fdt, int add_len)
674 newlen = fdt_totalsize(fdt) + add_len;
676 /* Open in place with a new len */
677 return fdt_open_into(fdt, fdt, newlen);
680 int fdt_del_partitions(void *blob, int parent_offset)
687 off = fdt_next_node(blob, parent_offset, &ndepth);
688 if (off > 0 && ndepth == 1) {
689 prop = fdt_getprop(blob, off, "label", NULL);
692 * Could not find label property, nand {}; node?
693 * Check subnode, delete partitions there if any.
695 return fdt_del_partitions(blob, off);
697 ret = fdt_del_subnodes(blob, parent_offset);
699 printf("Can't remove subnodes: %s\n",
708 int fdt_node_set_part_info(void *blob, int parent_offset,
709 struct mtd_device *dev)
711 struct list_head *pentry;
712 struct part_info *part;
713 struct reg_cell cell;
718 ret = fdt_del_partitions(blob, parent_offset);
723 * Check if it is nand {}; subnode, adjust
724 * the offset in this case
726 off = fdt_next_node(blob, parent_offset, &ndepth);
727 if (off > 0 && ndepth == 1)
731 list_for_each_prev(pentry, &dev->parts) {
734 part = list_entry(pentry, struct part_info, link);
736 debug("%2d: %-20s0x%08x\t0x%08x\t%d\n",
737 part_num, part->name, part->size,
738 part->offset, part->mask_flags);
740 sprintf(buf, "partition@%x", part->offset);
742 ret = fdt_add_subnode(blob, parent_offset, buf);
743 if (ret == -FDT_ERR_NOSPACE) {
744 ret = fdt_increase_size(blob, 512);
749 } else if (ret < 0) {
750 printf("Can't add partition node: %s\n",
756 /* Check MTD_WRITEABLE_CMD flag */
757 if (part->mask_flags & 1) {
759 ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
760 if (ret == -FDT_ERR_NOSPACE) {
761 ret = fdt_increase_size(blob, 512);
770 cell.r0 = cpu_to_fdt32(part->offset);
771 cell.r1 = cpu_to_fdt32(part->size);
773 ret = fdt_setprop(blob, newoff, "reg", &cell, sizeof(cell));
774 if (ret == -FDT_ERR_NOSPACE) {
775 ret = fdt_increase_size(blob, 512);
784 ret = fdt_setprop_string(blob, newoff, "label", part->name);
785 if (ret == -FDT_ERR_NOSPACE) {
786 ret = fdt_increase_size(blob, 512);
798 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
801 printf("Can't add property: %s\n", fdt_strerror(ret));
806 * Update partitions in nor/nand nodes using info from
807 * mtdparts environment variable. The nodes to update are
808 * specified by node_info structure which contains mtd device
809 * type and compatible string: E. g. the board code in
810 * ft_board_setup() could use:
812 * struct node_info nodes[] = {
813 * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
814 * { "cfi-flash", MTD_DEV_TYPE_NOR, },
817 * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
819 void fdt_fixup_mtdparts(void *blob, void *node_info, int node_info_size)
821 struct node_info *ni = node_info;
822 struct mtd_device *dev;
827 parts = getenv("mtdparts");
831 if (mtdparts_init() != 0)
834 for (i = 0; i < node_info_size; i++) {
836 noff = fdt_node_offset_by_compatible(blob, -1, ni[i].compat);
837 while (noff != -FDT_ERR_NOTFOUND) {
838 debug("%s: %s, mtd dev type %d\n",
839 fdt_get_name(blob, noff, 0),
840 ni[i].compat, ni[i].type);
841 dev = device_find(ni[i].type, idx++);
843 if (fdt_node_set_part_info(blob, noff, dev))
844 return; /* return on error */
847 /* Jump to next flash node */
848 noff = fdt_node_offset_by_compatible(blob, noff,
855 void fdt_del_node_and_alias(void *blob, const char *alias)
857 int off = fdt_path_offset(blob, alias);
862 fdt_del_node(blob, off);
864 off = fdt_path_offset(blob, "/aliases");
865 fdt_delprop(blob, off, alias);
868 /* Helper to read a big number; size is in cells (not bytes) */
869 static inline u64 of_read_number(const __be32 *cell, int size)
873 r = (r << 32) | be32_to_cpu(*(cell++));
879 /* Max address size we deal with */
880 #define OF_MAX_ADDR_CELLS 4
881 #define OF_BAD_ADDR ((u64)-1)
882 #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
887 static void of_dump_addr(const char *s, const u32 *addr, int na)
891 printf(" %08x", *(addr++));
895 static void of_dump_addr(const char *s, const u32 *addr, int na) { }
898 /* Callbacks for bus specific translators */
901 const char *addresses;
902 void (*count_cells)(void *blob, int parentoffset,
903 int *addrc, int *sizec);
904 u64 (*map)(u32 *addr, const u32 *range,
905 int na, int ns, int pna);
906 int (*translate)(u32 *addr, u64 offset, int na);
909 /* Default translator (generic bus) */
910 static void of_bus_default_count_cells(void *blob, int parentoffset,
911 int *addrc, int *sizec)
916 prop = fdt_getprop(blob, parentoffset, "#address-cells", NULL);
918 *addrc = be32_to_cpup(prop);
924 prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
926 *sizec = be32_to_cpup(prop);
932 static u64 of_bus_default_map(u32 *addr, const u32 *range,
933 int na, int ns, int pna)
937 cp = of_read_number(range, na);
938 s = of_read_number(range + na + pna, ns);
939 da = of_read_number(addr, na);
941 debug("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
944 if (da < cp || da >= (cp + s))
949 static int of_bus_default_translate(u32 *addr, u64 offset, int na)
951 u64 a = of_read_number(addr, na);
952 memset(addr, 0, na * 4);
955 addr[na - 2] = a >> 32;
956 addr[na - 1] = a & 0xffffffffu;
961 /* Array of bus specific translators */
962 static struct of_bus of_busses[] = {
967 .count_cells = of_bus_default_count_cells,
968 .map = of_bus_default_map,
969 .translate = of_bus_default_translate,
973 static int of_translate_one(void * blob, int parent, struct of_bus *bus,
974 struct of_bus *pbus, u32 *addr,
975 int na, int ns, int pna, const char *rprop)
980 u64 offset = OF_BAD_ADDR;
982 /* Normally, an absence of a "ranges" property means we are
983 * crossing a non-translatable boundary, and thus the addresses
984 * below the current not cannot be converted to CPU physical ones.
985 * Unfortunately, while this is very clear in the spec, it's not
986 * what Apple understood, and they do have things like /uni-n or
987 * /ht nodes with no "ranges" property and a lot of perfectly
988 * useable mapped devices below them. Thus we treat the absence of
989 * "ranges" as equivalent to an empty "ranges" property which means
990 * a 1:1 translation at that level. It's up to the caller not to try
991 * to translate addresses that aren't supposed to be translated in
992 * the first place. --BenH.
994 ranges = (u32 *)fdt_getprop(blob, parent, rprop, &rlen);
995 if (ranges == NULL || rlen == 0) {
996 offset = of_read_number(addr, na);
997 memset(addr, 0, pna * 4);
998 debug("OF: no ranges, 1:1 translation\n");
1002 debug("OF: walking ranges...\n");
1004 /* Now walk through the ranges */
1006 rone = na + pna + ns;
1007 for (; rlen >= rone; rlen -= rone, ranges += rone) {
1008 offset = bus->map(addr, ranges, na, ns, pna);
1009 if (offset != OF_BAD_ADDR)
1012 if (offset == OF_BAD_ADDR) {
1013 debug("OF: not found !\n");
1016 memcpy(addr, ranges + na, 4 * pna);
1019 of_dump_addr("OF: parent translation for:", addr, pna);
1020 debug("OF: with offset: "PRu64"\n", offset);
1022 /* Translate it into parent bus space */
1023 return pbus->translate(addr, offset, pna);
1027 * Translate an address from the device-tree into a CPU physical address,
1028 * this walks up the tree and applies the various bus mappings on the
1031 * Note: We consider that crossing any level with #size-cells == 0 to mean
1032 * that translation is impossible (that is we are not dealing with a value
1033 * that can be mapped to a cpu physical address). This is not really specified
1034 * that way, but this is traditionally the way IBM at least do things
1036 u64 __of_translate_address(void *blob, int node_offset, const u32 *in_addr,
1040 struct of_bus *bus, *pbus;
1041 u32 addr[OF_MAX_ADDR_CELLS];
1042 int na, ns, pna, pns;
1043 u64 result = OF_BAD_ADDR;
1045 debug("OF: ** translation for device %s **\n",
1046 fdt_get_name(blob, node_offset, NULL));
1048 /* Get parent & match bus type */
1049 parent = fdt_parent_offset(blob, node_offset);
1052 bus = &of_busses[0];
1054 /* Cound address cells & copy address locally */
1055 bus->count_cells(blob, parent, &na, &ns);
1056 if (!OF_CHECK_COUNTS(na, ns)) {
1057 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1058 fdt_get_name(blob, node_offset, NULL));
1061 memcpy(addr, in_addr, na * 4);
1063 debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
1064 bus->name, na, ns, fdt_get_name(blob, parent, NULL));
1065 of_dump_addr("OF: translating address:", addr, na);
1069 /* Switch to parent bus */
1070 node_offset = parent;
1071 parent = fdt_parent_offset(blob, node_offset);
1073 /* If root, we have finished */
1075 debug("OF: reached root node\n");
1076 result = of_read_number(addr, na);
1080 /* Get new parent bus and counts */
1081 pbus = &of_busses[0];
1082 pbus->count_cells(blob, parent, &pna, &pns);
1083 if (!OF_CHECK_COUNTS(pna, pns)) {
1084 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1085 fdt_get_name(blob, node_offset, NULL));
1089 debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
1090 pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
1092 /* Apply bus translation */
1093 if (of_translate_one(blob, node_offset, bus, pbus,
1094 addr, na, ns, pna, rprop))
1097 /* Complete the move up one level */
1102 of_dump_addr("OF: one level translation:", addr, na);
1109 u64 fdt_translate_address(void *blob, int node_offset, const u32 *in_addr)
1111 return __of_translate_address(blob, node_offset, in_addr, "ranges");
1115 * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and
1116 * who's reg property matches a physical cpu address
1118 * @blob: ptr to device tree
1119 * @compat: compatiable string to match
1120 * @compat_off: property name
1123 int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
1124 phys_addr_t compat_off)
1126 int len, off = fdt_node_offset_by_compatible(blob, -1, compat);
1127 while (off != -FDT_ERR_NOTFOUND) {
1128 u32 *reg = (u32 *)fdt_getprop(blob, off, "reg", &len);
1130 if (compat_off == fdt_translate_address(blob, off, reg))
1133 off = fdt_node_offset_by_compatible(blob, off, compat);
1136 return -FDT_ERR_NOTFOUND;
1140 * fdt_alloc_phandle: Return next free phandle value
1142 * @blob: ptr to device tree
1144 int fdt_alloc_phandle(void *blob)
1146 int offset, len, phandle = 0;
1149 for (offset = fdt_next_node(blob, -1, NULL); offset >= 0;
1150 offset = fdt_next_node(blob, offset, NULL)) {
1151 val = fdt_getprop(blob, offset, "linux,phandle", &len);
1153 phandle = max(*val, phandle);