]> git.ipfire.org Git - people/ms/u-boot.git/blobdiff - common/fdt_support.c
spl: mmc: raw: Try to load u-boot if Linux image is not found
[people/ms/u-boot.git] / common / fdt_support.c
index ea42c63eaafd74300374dd5734726f6a64aaf8e3..ced119e70d9f1010b5f6945d1e44e3d5c719dd44 100644 (file)
@@ -8,6 +8,7 @@
  */
 
 #include <common.h>
+#include <inttypes.h>
 #include <stdio_dev.h>
 #include <linux/ctype.h>
 #include <linux/types.h>
@@ -15,6 +16,7 @@
 #include <libfdt.h>
 #include <fdt_support.h>
 #include <exports.h>
+#include <fdtdec.h>
 
 /**
  * fdt_getprop_u32_default_node - Return a node's property or a default
@@ -129,18 +131,6 @@ static int fdt_fixup_stdout(void *fdt, int chosenoff)
                              OF_STDOUT_PATH, strlen(OF_STDOUT_PATH) + 1);
 }
 #elif defined(CONFIG_OF_STDOUT_VIA_ALIAS) && defined(CONFIG_CONS_INDEX)
-static void fdt_fill_multisername(char *sername, size_t maxlen)
-{
-       const char *outname = stdio_devices[stdout]->name;
-
-       if (strcmp(outname, "serial") > 0)
-               strncpy(sername, outname, maxlen);
-
-       /* eserial? */
-       if (strcmp(outname + 1, "serial") > 0)
-               strncpy(sername, outname + 1, maxlen);
-}
-
 static int fdt_fixup_stdout(void *fdt, int chosenoff)
 {
        int err;
@@ -150,32 +140,35 @@ static int fdt_fixup_stdout(void *fdt, int chosenoff)
        int len;
        char tmp[256]; /* long enough */
 
-       fdt_fill_multisername(sername, sizeof(sername) - 1);
-       if (!sername[0])
-               sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
+       sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
 
        aliasoff = fdt_path_offset(fdt, "/aliases");
        if (aliasoff < 0) {
                err = aliasoff;
-               goto error;
+               goto noalias;
        }
 
        path = fdt_getprop(fdt, aliasoff, sername, &len);
        if (!path) {
                err = len;
-               goto error;
+               goto noalias;
        }
 
        /* fdt_setprop may break "path" so we copy it to tmp buffer */
        memcpy(tmp, path, len);
 
        err = fdt_setprop(fdt, chosenoff, "linux,stdout-path", tmp, len);
-error:
        if (err < 0)
                printf("WARNING: could not set linux,stdout-path %s.\n",
                       fdt_strerror(err));
 
        return err;
+
+noalias:
+       printf("WARNING: %s: could not read %s alias: %s\n",
+              __func__, sername, fdt_strerror(err));
+
+       return 0;
 }
 #else
 static int fdt_fixup_stdout(void *fdt, int chosenoff)
@@ -193,6 +186,31 @@ static inline int fdt_setprop_uxx(void *fdt, int nodeoffset, const char *name,
                return fdt_setprop_u32(fdt, nodeoffset, name, (uint32_t)val);
 }
 
+int fdt_root(void *fdt)
+{
+       char *serial;
+       int err;
+
+       err = fdt_check_header(fdt);
+       if (err < 0) {
+               printf("fdt_root: %s\n", fdt_strerror(err));
+               return err;
+       }
+
+       serial = getenv("serial#");
+       if (serial) {
+               err = fdt_setprop(fdt, 0, "serial-number", serial,
+                                 strlen(serial) + 1);
+
+               if (err < 0) {
+                       printf("WARNING: could not set serial-number %s.\n",
+                              fdt_strerror(err));
+                       return err;
+               }
+       }
+
+       return 0;
+}
 
 int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end)
 {
@@ -370,22 +388,22 @@ static int fdt_pack_reg(const void *fdt, void *buf, u64 *address, u64 *size,
                        int n)
 {
        int i;
-       int address_len = fdt_address_cells(fdt, 0);
-       int size_len = fdt_size_cells(fdt, 0);
+       int address_cells = fdt_address_cells(fdt, 0);
+       int size_cells = fdt_size_cells(fdt, 0);
        char *p = buf;
 
        for (i = 0; i < n; i++) {
-               if (address_len == 2)
+               if (address_cells == 2)
                        *(fdt64_t *)p = cpu_to_fdt64(address[i]);
                else
                        *(fdt32_t *)p = cpu_to_fdt32(address[i]);
-               p += address_len;
+               p += 4 * address_cells;
 
-               if (size_len == 2)
+               if (size_cells == 2)
                        *(fdt64_t *)p = cpu_to_fdt64(size[i]);
                else
                        *(fdt32_t *)p = cpu_to_fdt32(size[i]);
-               p += size_len;
+               p += 4 * size_cells;
        }
 
        return p - (char *)buf;
@@ -428,6 +446,9 @@ int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
                return err;
        }
 
+       if (!banks)
+               return 0;
+
        len = fdt_pack_reg(blob, tmp, start, size, banks);
 
        err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
@@ -447,47 +468,49 @@ int fdt_fixup_memory(void *blob, u64 start, u64 size)
 void fdt_fixup_ethernet(void *fdt)
 {
        int node, i, j;
-       char enet[16], *tmp, *end;
+       char *tmp, *end;
        char mac[16];
        const char *path;
        unsigned char mac_addr[6];
+       int offset;
 
        node = fdt_path_offset(fdt, "/aliases");
        if (node < 0)
                return;
 
-       if (!getenv("ethaddr")) {
-               if (getenv("usbethaddr")) {
-                       strcpy(mac, "usbethaddr");
-               } else {
-                       debug("No ethernet MAC Address defined\n");
-                       return;
-               }
-       } else {
-               strcpy(mac, "ethaddr");
-       }
-
-       i = 0;
-       while ((tmp = getenv(mac)) != NULL) {
-               sprintf(enet, "ethernet%d", i);
-               path = fdt_getprop(fdt, node, enet, NULL);
-               if (!path) {
-                       debug("No alias for %s\n", enet);
-                       sprintf(mac, "eth%daddr", ++i);
-                       continue;
-               }
+       for (offset = fdt_first_property_offset(fdt, node);
+            offset > 0;
+            offset = fdt_next_property_offset(fdt, offset)) {
+               const char *name;
+               int len = strlen("ethernet");
+
+               path = fdt_getprop_by_offset(fdt, offset, &name, NULL);
+               if (!strncmp(name, "ethernet", len)) {
+                       i = trailing_strtol(name);
+                       if (i != -1) {
+                               if (i == 0)
+                                       strcpy(mac, "ethaddr");
+                               else
+                                       sprintf(mac, "eth%daddr", i);
+                       } else {
+                               continue;
+                       }
+                       tmp = getenv(mac);
+                       if (!tmp)
+                               continue;
+
+                       for (j = 0; j < 6; j++) {
+                               mac_addr[j] = tmp ?
+                                             simple_strtoul(tmp, &end, 16) : 0;
+                               if (tmp)
+                                       tmp = (*end) ? end + 1 : end;
+                       }
 
-               for (j = 0; j < 6; j++) {
-                       mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
-                       if (tmp)
-                               tmp = (*end) ? end+1 : end;
+                       do_fixup_by_path(fdt, path, "mac-address",
+                                        &mac_addr, 6, 0);
+                       do_fixup_by_path(fdt, path, "local-mac-address",
+                                        &mac_addr, 6, 1);
                }
-
-               do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0);
-               do_fixup_by_path(fdt, path, "local-mac-address",
-                               &mac_addr, 6, 1);
-
-               sprintf(mac, "eth%daddr", ++i);
        }
 }
 
@@ -914,11 +937,9 @@ void fdt_del_node_and_alias(void *blob, const char *alias)
        fdt_delprop(blob, off, alias);
 }
 
-#define PRu64  "%llx"
-
 /* Max address size we deal with */
 #define OF_MAX_ADDR_CELLS      4
-#define OF_BAD_ADDR    ((u64)-1)
+#define OF_BAD_ADDR    FDT_ADDR_T_NONE
 #define OF_CHECK_COUNTS(na, ns)        ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
                        (ns) > 0)
 
@@ -973,8 +994,8 @@ static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
        s  = of_read_number(range + na + pna, ns);
        da = of_read_number(addr, na);
 
-       debug("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
-           cp, s, da);
+       debug("OF: default map, cp=%" PRIu64 ", s=%" PRIu64
+             ", da=%" PRIu64 "\n", cp, s, da);
 
        if (da < cp || da >= (cp + s))
                return OF_BAD_ADDR;
@@ -1052,7 +1073,7 @@ static int of_translate_one(void * blob, int parent, struct of_bus *bus,
 
  finish:
        of_dump_addr("OF: parent translation for:", addr, pna);
-       debug("OF: with offset: "PRu64"\n", offset);
+       debug("OF: with offset: %" PRIu64 "\n", offset);
 
        /* Translate it into parent bus space */
        return pbus->translate(addr, offset, pna);
@@ -1381,9 +1402,9 @@ int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
 
        dt_addr = fdt_translate_address(fdt, node, reg);
        if (addr != dt_addr) {
-               printf("Warning: U-Boot configured device %s at address %llx,\n"
-                      " but the device tree has it address %llx.\n",
-                      alias, addr, dt_addr);
+               printf("Warning: U-Boot configured device %s at address %"
+                      PRIx64 ",\n but the device tree has it address %"
+                      PRIx64 ".\n", alias, addr, dt_addr);
                return 0;
        }
 
@@ -1534,7 +1555,7 @@ int fdt_setup_simplefb_node(void *fdt, int node, u64 base_address, u32 width,
        if (ret < 0)
                return ret;
 
-       snprintf(name, sizeof(name), "framebuffer@%llx", base_address);
+       snprintf(name, sizeof(name), "framebuffer@%" PRIx64, base_address);
        ret = fdt_set_name(fdt, node, name);
        if (ret < 0)
                return ret;
@@ -1561,3 +1582,32 @@ int fdt_setup_simplefb_node(void *fdt, int node, u64 base_address, u32 width,
 
        return 0;
 }
+
+/*
+ * Update native-mode in display-timings from display environment variable.
+ * The node to update are specified by path.
+ */
+int fdt_fixup_display(void *blob, const char *path, const char *display)
+{
+       int off, toff;
+
+       if (!display || !path)
+               return -FDT_ERR_NOTFOUND;
+
+       toff = fdt_path_offset(blob, path);
+       if (toff >= 0)
+               toff = fdt_subnode_offset(blob, toff, "display-timings");
+       if (toff < 0)
+               return toff;
+
+       for (off = fdt_first_subnode(blob, toff);
+            off >= 0;
+            off = fdt_next_subnode(blob, off)) {
+               uint32_t h = fdt_get_phandle(blob, off);
+               debug("%s:0x%x\n", fdt_get_name(blob, off, NULL),
+                     fdt32_to_cpu(h));
+               if (strcasecmp(fdt_get_name(blob, off, NULL), display) == 0)
+                       return fdt_setprop_u32(blob, toff, "native-mode", h);
+       }
+       return toff;
+}