Issue
Can somebody please explain why and how to use platform_get_resource
function ?
I've seen IORESOURCE_MEM
is used at many places, like the one href="http://lxr.free-electrons.com/source/drivers/spi/spi-omap2-mcspi.c?v=3.4#L1160" rel="noreferrer">here, as the second parameter, what does this mean?
I've gone through the links below but could not get the proper explanation.
- http://lwn.net/Articles/448499/
- http://www.gnugeneration.com/books/linux/2.6.20/kernel-api/re720.html
Solution
platform_get_resource()
is used in the __init
function of a driver to get information on the structure of the device resource, like start adress and end adress, in order to find the resource memory size so you can map it in memory.
the declaration of platform_get_resource
function is the following
struct resource * platform_get_resource ( struct platform_device * dev,
unsigned int type,
unsigned int num);
The first parameter tells the function which device we are interested in, so it can extract the info we need.
The second parameter depends on what kind of resource you are handling. If it is memory( or anything that can be mapped as memory :-)) then it's IORESOURCE_MEM. You can see all the macros at include/linux/ioport.h
For the last parameter, http://lwn.net/Articles/448499/ says:
The last parameter says which resource of that type is desired, with zero indicating the first one. Thus, for example, a driver could find its second MMIO region with:
r = platform_get_resource(pdev, IORESOURCE_MEM, 1);
The return value is a pointer to a type struct resource
var.
Here is an example
unsigned long *base_addr; /* Virtual Base Address */
struct resource *res; /* Device Resource Structure */
unsigned long remap_size; /* Device Memory Size */
static int __devinit bram_io_probe(struct platform_device *pdev)
{
res = platform_get_resource(pdev, IORESOURCE_MEM, 0); // get resource info
remap_size = res->end - res->start + 1; // get resource memory size
base_addr = ioremap(res->start, remap_size); // map it
}
Answered By - Manolis Ragkousis Answer Checked By - Timothy Miller (WPSolving Admin)