[PATCH 16/19] driver: make dev_platform_ioremap_resource() device-managed
Ahmad Fatoum
a.fatoum at pengutronix.de
Wed Oct 28 02:14:50 PDT 2026
From: Ahmad Fatoum <a.fatoum at barebox.org>
Unlike devm_platform_ioremap_resource() in Linux, the barebox stand-in
never releases its region. Most users call clk_get() or regulator_get()
after it, so a deferred probe leaves the region requested and the retry
fails with -EBUSY.
Implement it on top of devm_request_mem_resource() and move it out of
line to devres.c.
Assisted-by: Claude:opus-5-1m
Signed-off-by: Ahmad Fatoum <a.fatoum at barebox.org>
---
drivers/base/devres.c | 32 ++++++++++++++++++++++++++++++++
include/linux/device.h | 11 +----------
2 files changed, 33 insertions(+), 10 deletions(-)
diff --git a/drivers/base/devres.c b/drivers/base/devres.c
index efc810a8898f..f68b8e7b6590 100644
--- a/drivers/base/devres.c
+++ b/drivers/base/devres.c
@@ -7,6 +7,7 @@
#include <barebox.h>
#include <driver.h>
+#include <io.h>
#include <dma.h>
#include <talloc.h>
#include <xfuncs.h>
@@ -204,3 +205,34 @@ struct resource *devm_request_mem_resource(struct device *dev, int num)
return res;
}
EXPORT_SYMBOL(devm_request_mem_resource);
+
+/**
+ * dev_platform_ioremap_resource() - request and map resource 'num' for a device
+ * @dev: device to request the memory resource for
+ * @num: index of the memory resource
+ *
+ * barebox maps everything outside the RAM banks suitably for MMIO, so this
+ * only requests the device-managed region.
+ *
+ * Return: a pointer to the registers or an ERR_PTR() on failure.
+ */
+void __iomem *dev_platform_ioremap_resource(struct device *dev, int num)
+{
+ struct resource *res;
+
+ res = devm_request_mem_resource(dev, num);
+ if (IS_ERR(res))
+ return IOMEM_ERR_PTR(PTR_ERR(res));
+
+ /* As everything is mapped 1:1 by default, drivers on unlucky
+ * platforms can end up successfully requesting memory, but
+ * getting a base address that looks like an error pointer.
+ * Let's warn loudly about this case, as these drivers
+ * should be using devm_request_mem_resource instead.
+ */
+ if (WARN_ON(IS_ERR_VALUE(res->start)))
+ return IOMEM_ERR_PTR(-EINVAL);
+
+ return IOMEM(res->start);
+}
+EXPORT_SYMBOL(dev_platform_ioremap_resource);
diff --git a/include/linux/device.h b/include/linux/device.h
index 9b14bc74fef6..2b33580cd573 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -146,16 +146,7 @@ static inline int devm_add_action_or_reset(struct device *dev,
#define driver_unregister unregister_driver
-static inline void __iomem *dev_platform_ioremap_resource(struct device *dev,
- int resource)
-{
- /*
- * barebox maps everything outside the RAM banks suitably for MMIO,
- * so we don't need to do anything besides requesting the regions
- * and can leave the memory attributes unchanged.
- */
- return dev_request_mem_region(dev, resource);
-}
+void __iomem *dev_platform_ioremap_resource(struct device *dev, int num);
static inline void __iomem *devm_ioremap(struct device *dev,
resource_size_t start,
--
2.47.3
More information about the barebox
mailing list