of: assume big-endian properties, adding conversions where necessary

Properties in the device tree are specified as big-endian. At present,
the only platforms to support device trees are also big-endian, so we've
been acessing the properties as raw values.

We'd like to add device tree support to little-endian platforms too, so
add endian conversion to the sites where we access property values in
the common of code.

Compiled on powerpc (ppc44x_defconfig & ppc64_defconfig) and arm (fdt
support only for now).

Signed-off-by: Jeremy Kerr <jeremy.kerr@canonical.com>
Signed-off-by: Grant Likely <grant.likely@secretlab.ca>
diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index 968a86a..5c5f03e 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -51,7 +51,7 @@
 	int depth = -1;
 
 	do {
-		u32 tag = *((u32 *)p);
+		u32 tag = be32_to_cpup((__be32 *)p);
 		char *pathp;
 
 		p += 4;
@@ -64,7 +64,7 @@
 		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)
 				p = _ALIGN(p, sz >= 8 ? 8 : 4);
@@ -103,9 +103,9 @@
 	unsigned long p = ((unsigned long)initial_boot_params) +
 		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);
 }
@@ -122,7 +122,7 @@
 	unsigned long p = node;
 
 	do {
-		u32 tag = *((u32 *)p);
+		u32 tag = be32_to_cpup((__be32 *)p);
 		u32 sz, noff;
 		const char *nstr;
 
@@ -132,8 +132,8 @@
 		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)
 			p = _ALIGN(p, sz >= 8 ? 8 : 4);
@@ -210,7 +210,7 @@
 	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;
@@ -285,7 +285,7 @@
 		u32 sz, noff;
 		char *pname;
 
-		tag = *((u32 *)(*p));
+		tag = be32_to_cpup((__be32 *)(*p));
 		if (tag == OF_DT_NOP) {
 			*p += 4;
 			continue;
@@ -293,8 +293,8 @@
 		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)
 			*p = _ALIGN(*p, sz >= 8 ? 8 : 4);
@@ -367,7 +367,7 @@
 	}
 	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);
@@ -385,7 +385,7 @@
 void __init early_init_dt_check_for_initrd(unsigned long node)
 {
 	unsigned long start, end, len;
-	u32 *prop;
+	__be32 *prop;
 
 	pr_debug("Looking for initrd properties... ");
 
@@ -414,17 +414,22 @@
 int __init early_init_dt_scan_root(unsigned long node, const char *uname,
 				   int depth, void *data)
 {
-	u32 *prop;
+	__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);
-	dt_root_size_cells = prop ? *prop : OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
+	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);
-	dt_root_addr_cells = prop ? *prop : OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
+	if (prop)
+		dt_root_addr_cells = be32_to_cpup(prop);
 	pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
 
 	/* break now */
@@ -549,7 +554,7 @@
 	mem = lmb_alloc(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);
 
@@ -557,11 +562,11 @@
 	start = ((unsigned long)initial_boot_params) +
 		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 */