我正在Linux中为小型设备编写设备驱动程序。该设备具有一些特殊的内存限制,这迫使我不得不占用一块内存,而在我的驱动程序中,我需要知道保留内存的地址(和大小)
/ {
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>;
};
};
如何在设备驱动程序中读取保留内存的物理地址(即,如何读取值0xa000'0000
)?似乎有一个用于保留内存的API,但是我看不到任何返回struct reserved_mem *
答案 0 :(得分:2)
如果struct device *hwdev
指向您的硬件struct device
(例如,如果hwdev
指向dev
的{{1}}成员),则此代码段说明如何访问保留内存区域的设备树节点并将其转换为struct platform_device
。
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;
}
结尾,长度由mem_res.start
给出。