[PATCH V4 1/5] of: Add helper function to check MMIO register endianness

From: Kevin Cernekee
Date: Thu Apr 09 2015 - 16:07:12 EST

SoC peripherals can come in several different flavors:

- little-endian: registers always need to be accessed in LE mode (so the
kernel should perform a swap if the CPU is running BE)

- big-endian: registers always need to be accessed in BE mode (so the
kernel should perform a swap if the CPU is running LE)

- native-endian: the bus will automatically swap accesses, so the kernel
should never swap

Introduce a function that checks an OF device node to see whether it
contains a "big-endian" or "native-endian" property. For the former case,
always return true. For the latter case, return true iff the kernel was
built for BE (implying that the BE MMIO accessors do not perform a swap).
Otherwise return false, assuming LE registers.

LE registers are assumed by default because most existing drivers (libahci,
serial8250, usb) always use readl/writel in the absence of instructions
to the contrary, so that will be our fallback.

Signed-off-by: Kevin Cernekee <cernekee@xxxxxxxxx>
drivers/of/base.c | 23 +++++++++++++++++++++++
include/linux/of.h | 6 ++++++
2 files changed, 29 insertions(+)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index 8f165b112e03..3a10d1ed37f8 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -568,6 +568,29 @@ bool of_device_is_available(const struct device_node *device)

+ * of_device_is_big_endian - check if a device has BE registers
+ *
+ * @device: Node to check for endianness
+ *
+ * Returns true if the device has a "big-endian" property, or if the kernel
+ * was compiled for BE *and* the device has a "native-endian" property.
+ * Returns false otherwise.
+ *
+ * Callers would nominally use ioread32be/iowrite32be if
+ * of_device_is_big_endian() == true, or readl/writel otherwise.
+ */
+bool of_device_is_big_endian(const struct device_node *device)
+ if (of_property_read_bool(device, "big-endian"))
+ return true;
+ of_property_read_bool(device, "native-endian"))
+ return true;
+ return false;
* of_get_parent - Get a node's parent if any
* @node: Node to get parent
diff --git a/include/linux/of.h b/include/linux/of.h
index dfde07e77a63..a0cd62ef22db 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -305,6 +305,7 @@ extern int of_property_read_string_helper(struct device_node *np,
extern int of_device_is_compatible(const struct device_node *device,
const char *);
extern bool of_device_is_available(const struct device_node *device);
+extern bool of_device_is_big_endian(const struct device_node *device);
extern const void *of_get_property(const struct device_node *node,
const char *name,
int *lenp);
@@ -466,6 +467,11 @@ static inline bool of_device_is_available(const struct device_node *device)
return false;

+static inline bool of_device_is_big_endian(const struct device_node *device)
+ return false;
static inline struct property *of_find_property(const struct device_node *np,
const char *name,
int *lenp)

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/