Reputation: 831
I'm writing a device driver in Linux for a small device. The device has some particular memory constraints that forces me to carve out a piece of memory, and in my driver I need to know the address (and the size) of the reserved memory
/ {
reserved-memory {
my_reserve: my_reserve@a0000000 {
compatible = "shared-dma-pool";
reg = <0 0xa0000000 0 0x20000>;
no-map;
};
};
my_device {
compatible = "my_device";
memory-region = <&my_reserve>;
};
};
How do I read the physical address of the reserved memory (i.e. how do I read the value 0xa000'0000
) in my device driver? There seem to be a API for reserved memory, but nothing that I can see that returns a struct reserved_mem *
Upvotes: 2
Views: 3800
Reputation: 17403
If struct device *hwdev
points to your hardware struct device
(for example, if hwdev
points to the dev
member of a struct platform_device
), then this snippet illustrates how to access the device tree node of the reserved memory region and convert that to a struct resource
.
struct device_node *memnp;
struct resource mem_res;
int rc;
/* Get pointer to memory region device node from "memory-region" phandle. */
memnp = of_parse_phandle(hwdev->of_node, "memory-region", 0);
if (!memnp) {
dev_err(hwdev, "no memory-region node\n");
rc = -ENXIO;
goto err1;
}
/* Convert memory region to a struct resource */
rc = of_address_to_resource(memnp, 0, &mem_res);
/* finished with memnp */
of_node_put(memnp);
if (rc) {
dev_err(hwdev, "failed to translate memory-region to a resource\n");
goto err1;
}
The start address ends up in mem_res.start
and the length is given by resource_size(&mem_res);
.
Upvotes: 5