@@ -12,6 +12,7 @@
#define pr_fmt(fmt) "OF: reserved mem: " fmt
#include <linux/err.h>
+#include <linux/io.h>
#include <linux/of.h>
#include <linux/of_fdt.h>
#include <linux/of_platform.h>
@@ -407,3 +408,47 @@ struct reserved_mem *of_reserved_mem_lookup(struct device_node *np)
return NULL;
}
EXPORT_SYMBOL_GPL(of_reserved_mem_lookup);
+
+/**
+ * devm_memremap_reserved_mem() - acquire reserved_mem from a device node,
+ * request and memremap it
+ * @dev: device with node pointer of the desired reserved-memory region
+ * @flags: flags to pass to memremap()
+ *
+ * This function allows drivers to acquire a reference to the reserved_mem
+ * struct based on the device's device_node handle, request it and then
+ * memremap() it.
+ *
+ * Returns: A remapped reserved memory region, or an error pointer on failure.
+ */
+void *devm_memremap_reserved_mem(struct device *dev, unsigned long flags)
+{
+ void *dest_ptr;
+ struct reserved_mem *rmem;
+ struct resource *res;
+ const char *name;
+
+ rmem = of_reserved_mem_lookup(dev->of_node);
+ if (!rmem) {
+ dev_err(dev, "failed to acquire memory region\n");
+ return ERR_PTR(-ENODEV);
+ }
+
+ name = rmem->name ? : dev_name(dev);
+
+ res = devm_request_mem_region(dev, rmem->base, rmem->size, name);
+ if (!res) {
+ dev_err(dev, "can't request region for reserved memory\n");
+ return ERR_PTR(-EBUSY);
+ }
+
+ dest_ptr = devm_memremap(dev, rmem->base, rmem->size, flags);
+ if (!dest_ptr) {
+ dev_err(dev, "memremap failed for reserved memory\n");
+ devm_release_mem_region(dev, rmem->base, rmem->size);
+ dest_ptr = ERR_PTR(-ENOMEM);
+ }
+
+ return dest_ptr;
+}
+EXPORT_SYMBOL_GPL(devm_memremap_reserved_mem);
@@ -39,6 +39,7 @@ void fdt_init_reserved_mem(void);
void fdt_reserved_mem_save_node(unsigned long node, const char *uname,
phys_addr_t base, phys_addr_t size);
struct reserved_mem *of_reserved_mem_lookup(struct device_node *np);
+void *devm_memremap_reserved_mem(struct device *dev, unsigned long flags);
#else
static inline int of_reserved_mem_device_init_by_idx(struct device *dev,
struct device_node *np, int idx)
@@ -54,6 +55,11 @@ static inline struct reserved_mem *of_reserved_mem_lookup(struct device_node *np
{
return NULL;
}
+static inline void *devm_memremap_reserved_mem(struct device *dev,
+ unsigned long flags)
+{
+ return NULL;
+}
#endif
/**