Skip to content

Commit efd342f

Browse files
mbggDaniel Lezcano
authored andcommitted
of: Provide a function to request and map memory
A call to of_iomap does not request the memory region. This patch adds the function of_io_request_and_map which requests the memory region before mapping it. Signed-off-by: Matthias Brugger <matthias.bgg@gmail.com> Suggested-by: Thomas Gleixner <tglx@linutronix.de> Suggested-by: Rob Herring <robh@kernel.org> Acked-by: Rob Herring <robh@kernel.org> Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
1 parent 7e13918 commit efd342f

4 files changed

Lines changed: 49 additions & 2 deletions

File tree

‎drivers/of/address.c‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,42 @@ void __iomem *of_iomap(struct device_node *np, int index)
702702
}
703703
EXPORT_SYMBOL(of_iomap);
704704

705+
/*
706+
* of_io_request_and_map - Requests a resource and maps the memory mapped IO
707+
* for a given device_node
708+
* @device: the device whose io range will be mapped
709+
* @index: index of the io range
710+
* @name: name of the resource
711+
*
712+
* Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded
713+
* error code on failure. Usage example:
714+
*
715+
* base = of_io_request_and_map(node, 0, "foo");
716+
* if (IS_ERR(base))
717+
* return PTR_ERR(base);
718+
*/
719+
void __iomem *of_io_request_and_map(struct device_node *np, int index,
720+
char *name)
721+
{
722+
struct resource res;
723+
void __iomem *mem;
724+
725+
if (of_address_to_resource(np, index, &res))
726+
return IOMEM_ERR_PTR(-EINVAL);
727+
728+
if (!request_mem_region(res.start, resource_size(&res), name))
729+
return IOMEM_ERR_PTR(-EBUSY);
730+
731+
mem = ioremap(res.start, resource_size(&res));
732+
if (!mem) {
733+
release_mem_region(res.start, resource_size(&res));
734+
return IOMEM_ERR_PTR(-ENOMEM);
735+
}
736+
737+
return mem;
738+
}
739+
EXPORT_SYMBOL(of_io_request_and_map);
740+
705741
/**
706742
* of_dma_get_range - Get DMA range info
707743
* @np: device node to get DMA range info

‎include/linux/io.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ static inline void devm_ioport_unmap(struct device *dev, void __iomem *addr)
5858
}
5959
#endif
6060

61+
#define IOMEM_ERR_PTR(err) (__force void __iomem *)ERR_PTR(err)
62+
6163
void __iomem *devm_ioremap(struct device *dev, resource_size_t offset,
6264
unsigned long size);
6365
void __iomem *devm_ioremap_nocache(struct device *dev, resource_size_t offset,

‎include/linux/of_address.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,12 @@ static inline bool of_dma_is_coherent(struct device_node *np)
109109
extern int of_address_to_resource(struct device_node *dev, int index,
110110
struct resource *r);
111111
void __iomem *of_iomap(struct device_node *node, int index);
112+
void __iomem *of_io_request_and_map(struct device_node *device,
113+
int index, char *name);
112114
#else
115+
116+
#include <linux/io.h>
117+
113118
static inline int of_address_to_resource(struct device_node *dev, int index,
114119
struct resource *r)
115120
{
@@ -120,6 +125,12 @@ static inline void __iomem *of_iomap(struct device_node *device, int index)
120125
{
121126
return NULL;
122127
}
128+
129+
static inline void __iomem *of_io_request_and_map(struct device_node *device,
130+
int index, char *name)
131+
{
132+
return IOMEM_ERR_PTR(-EINVAL);
133+
}
123134
#endif
124135

125136
#if defined(CONFIG_OF_ADDRESS) && defined(CONFIG_PCI)

‎lib/devres.c‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ void devm_iounmap(struct device *dev, void __iomem *addr)
8686
}
8787
EXPORT_SYMBOL(devm_iounmap);
8888

89-
#define IOMEM_ERR_PTR(err) (__force void __iomem *)ERR_PTR(err)
90-
9189
/**
9290
* devm_ioremap_resource() - check, request region, and ioremap resource
9391
* @dev: generic device to handle the resource for

0 commit comments

Comments
 (0)