pata_cypress: fix PIO timings underclocking
[firefly-linux-kernel-4.4.55.git] / drivers / of / fdt.c
index 6ad98e85dc930b61ecbad637e1bed1ee4d391e28..406757a9d7ea62056680808392acd02e2889cda9 100644 (file)
  */
 
 #include <linux/kernel.h>
-#include <linux/lmb.h>
 #include <linux/initrd.h>
 #include <linux/of.h>
 #include <linux/of_fdt.h>
+#include <linux/string.h>
+#include <linux/errno.h>
+
+#ifdef CONFIG_PPC
+#include <asm/machdep.h>
+#endif /* CONFIG_PPC */
+
+#include <asm/page.h>
+
+int __initdata dt_root_addr_cells;
+int __initdata dt_root_size_cells;
 
 struct boot_param_header *initial_boot_params;
 
 char *find_flat_dt_string(u32 offset)
 {
        return ((char *)initial_boot_params) +
-               initial_boot_params->off_dt_strings + offset;
+               be32_to_cpu(initial_boot_params->off_dt_strings) + offset;
 }
 
 /**
@@ -38,12 +48,12 @@ int __init of_scan_flat_dt(int (*it)(unsigned long node,
                           void *data)
 {
        unsigned long p = ((unsigned long)initial_boot_params) +
-               initial_boot_params->off_dt_struct;
+               be32_to_cpu(initial_boot_params->off_dt_struct);
        int rc = 0;
        int depth = -1;
 
        do {
-               u32 tag = *((u32 *)p);
+               u32 tag = be32_to_cpup((__be32 *)p);
                char *pathp;
 
                p += 4;
@@ -56,9 +66,9 @@ int __init of_scan_flat_dt(int (*it)(unsigned long node,
                if (tag == OF_DT_END)
                        break;
                if (tag == OF_DT_PROP) {
-                       u32 sz = *((u32 *)p);
+                       u32 sz = be32_to_cpup((__be32 *)p);
                        p += 8;
-                       if (initial_boot_params->version < 0x10)
+                       if (be32_to_cpu(initial_boot_params->version) < 0x10)
                                p = _ALIGN(p, sz >= 8 ? 8 : 4);
                        p += sz;
                        p = _ALIGN(p, 4);
@@ -93,11 +103,11 @@ int __init of_scan_flat_dt(int (*it)(unsigned long node,
 unsigned long __init of_get_flat_dt_root(void)
 {
        unsigned long p = ((unsigned long)initial_boot_params) +
-               initial_boot_params->off_dt_struct;
+               be32_to_cpu(initial_boot_params->off_dt_struct);
 
-       while (*((u32 *)p) == OF_DT_NOP)
+       while (be32_to_cpup((__be32 *)p) == OF_DT_NOP)
                p += 4;
-       BUG_ON(*((u32 *)p) != OF_DT_BEGIN_NODE);
+       BUG_ON(be32_to_cpup((__be32 *)p) != OF_DT_BEGIN_NODE);
        p += 4;
        return _ALIGN(p + strlen((char *)p) + 1, 4);
 }
@@ -114,7 +124,7 @@ void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
        unsigned long p = node;
 
        do {
-               u32 tag = *((u32 *)p);
+               u32 tag = be32_to_cpup((__be32 *)p);
                u32 sz, noff;
                const char *nstr;
 
@@ -124,10 +134,10 @@ void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
                if (tag != OF_DT_PROP)
                        return NULL;
 
-               sz = *((u32 *)p);
-               noff = *((u32 *)(p + 4));
+               sz = be32_to_cpup((__be32 *)p);
+               noff = be32_to_cpup((__be32 *)(p + 4));
                p += 8;
-               if (initial_boot_params->version < 0x10)
+               if (be32_to_cpu(initial_boot_params->version) < 0x10)
                        p = _ALIGN(p, sz >= 8 ? 8 : 4);
 
                nstr = find_flat_dt_string(noff);
@@ -202,7 +212,7 @@ unsigned long __init unflatten_dt_node(unsigned long mem,
        int has_name = 0;
        int new_format = 0;
 
-       tag = *((u32 *)(*p));
+       tag = be32_to_cpup((__be32 *)(*p));
        if (tag != OF_DT_BEGIN_NODE) {
                pr_err("Weird tag at start of node: %x\n", tag);
                return mem;
@@ -277,7 +287,7 @@ unsigned long __init unflatten_dt_node(unsigned long mem,
                u32 sz, noff;
                char *pname;
 
-               tag = *((u32 *)(*p));
+               tag = be32_to_cpup((__be32 *)(*p));
                if (tag == OF_DT_NOP) {
                        *p += 4;
                        continue;
@@ -285,10 +295,10 @@ unsigned long __init unflatten_dt_node(unsigned long mem,
                if (tag != OF_DT_PROP)
                        break;
                *p += 4;
-               sz = *((u32 *)(*p));
-               noff = *((u32 *)((*p) + 4));
+               sz = be32_to_cpup((__be32 *)(*p));
+               noff = be32_to_cpup((__be32 *)((*p) + 4));
                *p += 8;
-               if (initial_boot_params->version < 0x10)
+               if (be32_to_cpu(initial_boot_params->version) < 0x10)
                        *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
 
                pname = find_flat_dt_string(noff);
@@ -302,13 +312,21 @@ unsigned long __init unflatten_dt_node(unsigned long mem,
                pp = unflatten_dt_alloc(&mem, sizeof(struct property),
                                        __alignof__(struct property));
                if (allnextpp) {
-                       if (strcmp(pname, "linux,phandle") == 0) {
-                               np->node = *((u32 *)*p);
-                               if (np->linux_phandle == 0)
-                                       np->linux_phandle = np->node;
+                       /* We accept flattened tree phandles either in
+                        * ePAPR-style "phandle" properties, or the
+                        * legacy "linux,phandle" properties.  If both
+                        * appear and have different values, things
+                        * will get weird.  Don't do that. */
+                       if ((strcmp(pname, "phandle") == 0) ||
+                           (strcmp(pname, "linux,phandle") == 0)) {
+                               if (np->phandle == 0)
+                                       np->phandle = *((u32 *)*p);
                        }
+                       /* And we process the "ibm,phandle" property
+                        * used in pSeries dynamic device tree
+                        * stuff */
                        if (strcmp(pname, "ibm,phandle") == 0)
-                               np->linux_phandle = *((u32 *)*p);
+                               np->phandle = *((u32 *)*p);
                        pp->name = pname;
                        pp->length = sz;
                        pp->value = (void *)*p;
@@ -360,7 +378,7 @@ unsigned long __init unflatten_dt_node(unsigned long mem,
        }
        while (tag == OF_DT_BEGIN_NODE) {
                mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
-               tag = *((u32 *)(*p));
+               tag = be32_to_cpup((__be32 *)(*p));
        }
        if (tag != OF_DT_END_NODE) {
                pr_err("Weird tag at end of node: %x\n", tag);
@@ -377,28 +395,23 @@ unsigned long __init unflatten_dt_node(unsigned long mem,
  */
 void __init early_init_dt_check_for_initrd(unsigned long node)
 {
-       unsigned long len;
-       u32 *prop;
+       unsigned long start, end, len;
+       __be32 *prop;
 
        pr_debug("Looking for initrd properties... ");
 
        prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
-       if (prop) {
-               initrd_start = (unsigned long)
-                               __va(of_read_ulong(prop, len/4));
-
-               prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
-               if (prop) {
-                       initrd_end = (unsigned long)
-                               __va(of_read_ulong(prop, len/4));
-                       initrd_below_start_ok = 1;
-               } else {
-                       initrd_start = 0;
-               }
-       }
+       if (!prop)
+               return;
+       start = of_read_ulong(prop, len/4);
+
+       prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
+       if (!prop)
+               return;
+       end = of_read_ulong(prop, len/4);
 
-       pr_debug("initrd_start=0x%lx  initrd_end=0x%lx\n",
-                initrd_start, initrd_end);
+       early_init_dt_setup_initrd_arch(start, end);
+       pr_debug("initrd_start=0x%lx  initrd_end=0x%lx\n", start, end);
 }
 #else
 inline void early_init_dt_check_for_initrd(unsigned long node)
@@ -406,6 +419,125 @@ inline void early_init_dt_check_for_initrd(unsigned long node)
 }
 #endif /* CONFIG_BLK_DEV_INITRD */
 
+/**
+ * early_init_dt_scan_root - fetch the top level address and size cells
+ */
+int __init early_init_dt_scan_root(unsigned long node, const char *uname,
+                                  int depth, void *data)
+{
+       __be32 *prop;
+
+       if (depth != 0)
+               return 0;
+
+       dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
+       dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
+
+       prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
+       if (prop)
+               dt_root_size_cells = be32_to_cpup(prop);
+       pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
+
+       prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
+       if (prop)
+               dt_root_addr_cells = be32_to_cpup(prop);
+       pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
+
+       /* break now */
+       return 1;
+}
+
+u64 __init dt_mem_next_cell(int s, __be32 **cellp)
+{
+       __be32 *p = *cellp;
+
+       *cellp = p + s;
+       return of_read_number(p, s);
+}
+
+/**
+ * early_init_dt_scan_memory - Look for an parse memory nodes
+ */
+int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
+                                    int depth, void *data)
+{
+       char *type = of_get_flat_dt_prop(node, "device_type", NULL);
+       __be32 *reg, *endp;
+       unsigned long l;
+
+       /* We are scanning "memory" nodes only */
+       if (type == NULL) {
+               /*
+                * The longtrail doesn't have a device_type on the
+                * /memory node, so look for the node called /memory@0.
+                */
+               if (depth != 1 || strcmp(uname, "memory@0") != 0)
+                       return 0;
+       } else if (strcmp(type, "memory") != 0)
+               return 0;
+
+       reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
+       if (reg == NULL)
+               reg = of_get_flat_dt_prop(node, "reg", &l);
+       if (reg == NULL)
+               return 0;
+
+       endp = reg + (l / sizeof(__be32));
+
+       pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
+           uname, l, reg[0], reg[1], reg[2], reg[3]);
+
+       while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
+               u64 base, size;
+
+               base = dt_mem_next_cell(dt_root_addr_cells, &reg);
+               size = dt_mem_next_cell(dt_root_size_cells, &reg);
+
+               if (size == 0)
+                       continue;
+               pr_debug(" - %llx ,  %llx\n", (unsigned long long)base,
+                   (unsigned long long)size);
+
+               early_init_dt_add_memory_arch(base, size);
+       }
+
+       return 0;
+}
+
+int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
+                                    int depth, void *data)
+{
+       unsigned long l;
+       char *p;
+
+       pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
+
+       if (depth != 1 ||
+           (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
+               return 0;
+
+       early_init_dt_check_for_initrd(node);
+
+       /* Retreive command line */
+       p = of_get_flat_dt_prop(node, "bootargs", &l);
+       if (p != NULL && l > 0)
+               strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
+
+#ifdef CONFIG_CMDLINE
+#ifndef CONFIG_CMDLINE_FORCE
+       if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
+#endif
+               strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
+#endif /* CONFIG_CMDLINE */
+
+       early_init_dt_scan_chosen_arch(node);
+
+       pr_debug("Command line is: %s\n", cmd_line);
+
+       /* break now */
+       return 1;
+}
+
 /**
  * unflatten_device_tree - create tree of device_nodes from flat blob
  *
@@ -423,29 +555,30 @@ void __init unflatten_device_tree(void)
 
        /* First pass, scan for size */
        start = ((unsigned long)initial_boot_params) +
-               initial_boot_params->off_dt_struct;
+               be32_to_cpu(initial_boot_params->off_dt_struct);
        size = unflatten_dt_node(0, &start, NULL, NULL, 0);
        size = (size | 3) + 1;
 
        pr_debug("  size is %lx, allocating...\n", size);
 
        /* Allocate memory for the expanded device tree */
-       mem = lmb_alloc(size + 4, __alignof__(struct device_node));
+       mem = early_init_dt_alloc_memory_arch(size + 4,
+                       __alignof__(struct device_node));
        mem = (unsigned long) __va(mem);
 
-       ((u32 *)mem)[size / 4] = 0xdeadbeef;
+       ((__be32 *)mem)[size / 4] = cpu_to_be32(0xdeadbeef);
 
        pr_debug("  unflattening %lx...\n", mem);
 
        /* Second pass, do actual unflattening */
        start = ((unsigned long)initial_boot_params) +
-               initial_boot_params->off_dt_struct;
+               be32_to_cpu(initial_boot_params->off_dt_struct);
        unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
-       if (*((u32 *)start) != OF_DT_END)
+       if (be32_to_cpup((__be32 *)start) != OF_DT_END)
                pr_warning("Weird tag at end of tree: %08x\n", *((u32 *)start));
-       if (((u32 *)mem)[size / 4] != 0xdeadbeef)
+       if (be32_to_cpu(((__be32 *)mem)[size / 4]) != 0xdeadbeef)
                pr_warning("End of tree marker overwritten: %08x\n",
-                          ((u32 *)mem)[size / 4]);
+                          be32_to_cpu(((__be32 *)mem)[size / 4]));
        *allnextp = NULL;
 
        /* Get pointer to OF "/chosen" node for use everywhere */