]> git.ipfire.org Git - people/ms/u-boot.git/blobdiff - common/fdt_support.c
ARM: zynq: Fix bootmode mask
[people/ms/u-boot.git] / common / fdt_support.c
index 9a6f6b7d8b16c822496c53a62df26d3850081b29..fcd252336cdf15d8cebe503e4025ef2f07d41b09 100644 (file)
@@ -4,23 +4,7 @@
  *
  * Copyright 2010-2011 Freescale Semiconductor, Inc.
  *
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- * MA 02111-1307 USA
+ * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
  */
 DECLARE_GLOBAL_DATA_PTR;
 
+/*
+ * Get cells len in bytes
+ *     if #NNNN-cells property is 2 then len is 8
+ *     otherwise len is 4
+ */
+static int get_cells_len(void *blob, char *nr_cells_name)
+{
+       const fdt32_t *cell;
+
+       cell = fdt_getprop(blob, 0, nr_cells_name, NULL);
+       if (cell && fdt32_to_cpu(*cell) == 2)
+               return 8;
+
+       return 4;
+}
+
+/*
+ * Write a 4 or 8 byte big endian cell
+ */
+static void write_cell(u8 *addr, u64 val, int size)
+{
+       int shift = (size - 1) * 8;
+       while (size-- > 0) {
+               *addr++ = (val >> shift) & 0xff;
+               shift -= 8;
+       }
+}
+
+/**
+ * fdt_getprop_u32_default_node - Return a node's property or a default
+ *
+ * @fdt: ptr to device tree
+ * @off: offset of node
+ * @cell: cell offset in property
+ * @prop: property name
+ * @dflt: default value if the property isn't found
+ *
+ * Convenience function to return a node's property or a default value if
+ * the property doesn't exist.
+ */
+u32 fdt_getprop_u32_default_node(const void *fdt, int off, int cell,
+                               const char *prop, const u32 dflt)
+{
+       const fdt32_t *val;
+       int len;
+
+       val = fdt_getprop(fdt, off, prop, &len);
+
+       /* Check if property exists */
+       if (!val)
+               return dflt;
+
+       /* Check if property is long enough */
+       if (len < ((cell + 1) * sizeof(uint32_t)))
+               return dflt;
+
+       return fdt32_to_cpu(*val);
+}
+
 /**
  * fdt_getprop_u32_default - Find a node and return it's property or a default
  *
@@ -51,18 +94,13 @@ DECLARE_GLOBAL_DATA_PTR;
 u32 fdt_getprop_u32_default(const void *fdt, const char *path,
                                const char *prop, const u32 dflt)
 {
-       const fdt32_t *val;
        int off;
 
        off = fdt_path_offset(fdt, path);
        if (off < 0)
                return dflt;
 
-       val = fdt_getprop(fdt, off, prop, NULL);
-       if (val)
-               return fdt32_to_cpu(*val);
-       else
-               return dflt;
+       return fdt_getprop_u32_default_node(fdt, off, 0, prop, dflt);
 }
 
 /**
@@ -147,9 +185,9 @@ static int fdt_fixup_stdout(void *fdt, int chosenoff)
 
 int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end, int force)
 {
-       int   nodeoffset;
+       int   nodeoffset, addr_cell_len;
        int   err, j, total;
-       fdt32_t  tmp;
+       fdt64_t  tmp;
        const char *path;
        uint64_t addr, size;
 
@@ -186,20 +224,22 @@ int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end, int force)
                return err;
        }
 
+       addr_cell_len = get_cells_len(fdt, "#address-cells");
+
        path = fdt_getprop(fdt, nodeoffset, "linux,initrd-start", NULL);
        if ((path == NULL) || force) {
-               tmp = cpu_to_fdt32(initrd_start);
+               write_cell((u8 *)&tmp, initrd_start, addr_cell_len);
                err = fdt_setprop(fdt, nodeoffset,
-                       "linux,initrd-start", &tmp, sizeof(tmp));
+                       "linux,initrd-start", &tmp, addr_cell_len);
                if (err < 0) {
                        printf("WARNING: "
                                "could not set linux,initrd-start %s.\n",
                                fdt_strerror(err));
                        return err;
                }
-               tmp = cpu_to_fdt32(initrd_end);
+               write_cell((u8 *)&tmp, initrd_end, addr_cell_len);
                err = fdt_setprop(fdt, nodeoffset,
-                       "linux,initrd-end", &tmp, sizeof(tmp));
+                       "linux,initrd-end", &tmp, addr_cell_len);
                if (err < 0) {
                        printf("WARNING: could not set linux,initrd-end %s.\n",
                                fdt_strerror(err));
@@ -359,34 +399,6 @@ void do_fixup_by_compat_u32(void *fdt, const char *compat,
        do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create);
 }
 
-/*
- * Get cells len in bytes
- *     if #NNNN-cells property is 2 then len is 8
- *     otherwise len is 4
- */
-static int get_cells_len(void *blob, char *nr_cells_name)
-{
-       const fdt32_t *cell;
-
-       cell = fdt_getprop(blob, 0, nr_cells_name, NULL);
-       if (cell && fdt32_to_cpu(*cell) == 2)
-               return 8;
-
-       return 4;
-}
-
-/*
- * Write a 4 or 8 byte big endian cell
- */
-static void write_cell(u8 *addr, u64 val, int size)
-{
-       int shift = (size - 1) * 8;
-       while (size-- > 0) {
-               *addr++ = (val >> shift) & 0xff;
-               shift -= 8;
-       }
-}
-
 #ifdef CONFIG_NR_DRAM_BANKS
 #define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS
 #else
@@ -416,10 +428,11 @@ int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
        nodeoffset = fdt_path_offset(blob, "/memory");
        if (nodeoffset < 0) {
                nodeoffset = fdt_add_subnode(blob, 0, "memory");
-               if (nodeoffset < 0)
+               if (nodeoffset < 0) {
                        printf("WARNING: could not create /memory: %s.\n",
                                        fdt_strerror(nodeoffset));
-               return nodeoffset;
+                       return nodeoffset;
+               }
        }
        err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
                        sizeof("memory"));
@@ -782,11 +795,11 @@ int fdt_node_set_part_info(void *blob, int parent_offset,
 
                part = list_entry(pentry, struct part_info, link);
 
-               debug("%2d: %-20s0x%08x\t0x%08x\t%d\n",
+               debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
                        part_num, part->name, part->size,
                        part->offset, part->mask_flags);
 
-               sprintf(buf, "partition@%x", part->offset);
+               sprintf(buf, "partition@%llx", part->offset);
 add_sub:
                ret = fdt_add_subnode(blob, parent_offset, buf);
                if (ret == -FDT_ERR_NOSPACE) {
@@ -1422,3 +1435,97 @@ u64 fdt_get_base_address(void *fdt, int node)
 
        return prop ? fdt_translate_address(fdt, node, prop + naddr) : 0;
 }
+
+/*
+ * Read a property of size <prop_len>. Currently only supports 1 or 2 cells.
+ */
+static int fdt_read_prop(const fdt32_t *prop, int prop_len, int cell_off,
+                        uint64_t *val, int cells)
+{
+       const fdt32_t *prop32 = &prop[cell_off];
+       const fdt64_t *prop64 = (const fdt64_t *)&prop[cell_off];
+
+       if ((cell_off + cells) > prop_len)
+               return -FDT_ERR_NOSPACE;
+
+       switch (cells) {
+       case 1:
+               *val = fdt32_to_cpu(*prop32);
+               break;
+       case 2:
+               *val = fdt64_to_cpu(*prop64);
+               break;
+       default:
+               return -FDT_ERR_NOSPACE;
+       }
+
+       return 0;
+}
+
+/**
+ * fdt_read_range - Read a node's n'th range property
+ *
+ * @fdt: ptr to device tree
+ * @node: offset of node
+ * @n: range index
+ * @child_addr: pointer to storage for the "child address" field
+ * @addr: pointer to storage for the CPU view translated physical start
+ * @len: pointer to storage for the range length
+ *
+ * Convenience function that reads and interprets a specific range out of
+ * a number of the "ranges" property array.
+ */
+int fdt_read_range(void *fdt, int node, int n, uint64_t *child_addr,
+                  uint64_t *addr, uint64_t *len)
+{
+       int pnode = fdt_parent_offset(fdt, node);
+       const fdt32_t *ranges;
+       int pacells;
+       int acells;
+       int scells;
+       int ranges_len;
+       int cell = 0;
+       int r = 0;
+
+       /*
+        * The "ranges" property is an array of
+        * { <child address> <parent address> <size in child address space> }
+        *
+        * All 3 elements can span a diffent number of cells. Fetch their size.
+        */
+       pacells = fdt_getprop_u32_default_node(fdt, pnode, 0, "#address-cells", 1);
+       acells = fdt_getprop_u32_default_node(fdt, node, 0, "#address-cells", 1);
+       scells = fdt_getprop_u32_default_node(fdt, node, 0, "#size-cells", 1);
+
+       /* Now try to get the ranges property */
+       ranges = fdt_getprop(fdt, node, "ranges", &ranges_len);
+       if (!ranges)
+               return -FDT_ERR_NOTFOUND;
+       ranges_len /= sizeof(uint32_t);
+
+       /* Jump to the n'th entry */
+       cell = n * (pacells + acells + scells);
+
+       /* Read <child address> */
+       if (child_addr) {
+               r = fdt_read_prop(ranges, ranges_len, cell, child_addr,
+                                 acells);
+               if (r)
+                       return r;
+       }
+       cell += acells;
+
+       /* Read <parent address> */
+       if (addr)
+               *addr = fdt_translate_address(fdt, node, ranges + cell);
+       cell += pacells;
+
+       /* Read <size in child address space> */
+       if (len) {
+               r = fdt_read_prop(ranges, ranges_len, cell, len, scells);
+               if (r)
+                       return r;
+       }
+
+       return 0;
+}