上篇讲了从dtb文件转换成device_node树,本篇分析device_node生成platform_device平台设备的过程。
平台设备生成入口:在arch/arm64/kernel/setup.c文件中,入口函数为:
arm64_device_init->of_platform_populate
of_platform_populate在drivers/of/platform.c中定义:
int of_platform_populate(struct device_node *root,
const struct of_device_id *matches,
const struct of_dev_auxdata *lookup,
struct device *parent)
{
struct device_node *child;
int rc = 0;
/*获取根device_node节点*/
root = root ? of_node_get(root) : of_find_node_by_path("/");
if (!root)
return -EINVAL;
/*遍历根device_node的所有子节点,这里只是遍历了根节点的儿子节点,没有对孙子节点和其他节点进行处理*/
for_each_child_of_node(root, child) {
/*根据device_node生成platform_device*/
rc = of_platform_bus_create(child, matches, lookup, parent, true);
if (rc) {
of_node_put(child);
break;
}
}
of_node_set_flag(root, OF_POPULATED_BUS);
of_node_put(root);
return rc;
}
static int of_platform_bus_create(struct device_node *bus,
const struct of_device_id *matches,
const struct of_dev_auxdata *lookup,
struct device *parent, bool strict)
{
const struct of_dev_auxdata *auxdata;
struct device_node *child;
struct platform_device *dev;
const char *bus_id = NULL;
void *platform_data = NULL;
int rc = 0;
/* Make sure it has a compatible property */
/*device_node节点必须具有“compatible”属性才会继续解析*/
if (strict && (!of_get_property(bus, "compatible", NULL))) {
pr_debug("%s() - skipping %s, no compatible prop\n",
__func__, bus->full_name);
return 0;
}
/*设备拓展数据暂时不讨论*/
auxdata = of_dev_lookup(lookup, bus);
if (auxdata) {
bus_id = auxdata->name;
platform_data = auxdata->platform_data;
}
/*对于compatible 属性为"arm,primecell"的节点需要特殊处理*/
if (of_device_is_compatible(bus, "arm,primecell")) {
/*
* Don't return an error here to keep compatibility with older
* device tree files.
*/
of_amba_device_create(bus, bus_id, platform_data, parent);
return 0;
}
/*将device_node 属性解析生成platform_device设备属性*/
dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
if (!dev || !of_match_node(matches, bus))
return 0;
/*之前提到第一次遍历时只处理的根节点的儿子节点,这里通过遍历当前节点的子节点,来处理根节点的孙子节点,实际上通过这种方式最终递归遍历所有节点*/
for_each_child_of_node(bus, child) {
pr_debug(" create child: %s\n", child->full_name);
rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
if (rc) {
of_node_put(child);
break;
}
}
of_node_set_flag(bus, OF_POPULATED_BUS);
return rc;
}
static struct platform_device *of_platform_device_create_pdata(
struct device_node *np,
const char *bus_id,
void *platform_data,
struct device *parent)
{
struct platform_device *dev;
/*如果device_node节点没有被创建过且节点状态有效则创建platform_device,节点是否有效是通过节点的status属性来判断的,如果status属性值为”ok”或者” okay”则有效*/
if (!of_device_is_available(np) ||
of_node_test_and_set_flag(np, OF_POPULATED))
return NULL;
/*申请platform_device空间,初始化platform_device的io和中断资源*/
dev = of_device_alloc(np, bus_id, parent);
if (!dev)
goto err_clear_flag;
dev->dev.bus = &platform_bus_type;
dev->dev.platform_data = platform_data;
/*设置platform_device的中dma*/
of_dma_configure(&dev->dev, dev->dev.of_node);
/*设置platform_device的msi中断*/
of_msi_configure(&dev->dev, dev->dev.of_node);
/*将platform_device挂到总线,到此平台设备已经创建完了,这是加载对应驱动后就可以对设备进行读写操作了*/
if (of_device_add(dev) != 0) {
of_dma_deconfigure(&dev->dev);
platform_device_put(dev);
goto err_clear_flag;
}
return dev;
err_clear_flag:
of_node_clear_flag(np, OF_POPULATED);
return NULL;
}
struct platform_device *of_device_alloc(struct device_node *np,
const char *bus_id,
struct device *parent)
{
struct platform_device *dev;
int rc, i, num_reg = 0, num_irq;
struct resource *res, temp_res;
/*申请platform_device内存空间*/
dev = platform_device_alloc("", -1);
if (!dev)
return NULL;
/* count the io and irq resources */
/*通过节点“reg”属性结算节点的io和mem资源数*/
while (of_address_to_resource(np, num_reg, &temp_res) == 0)
num_reg++;
/*通过节点“interrupts”和“#interrupt-cells”属性计算节点的中断数*/
num_irq = of_irq_count(np);
/* Populate the resource table */
/*根据资源和中断数申请内存,并初始化节点资源*/
if (num_irq || num_reg) {
/*申请platform_device存储资源的空间*/
res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL);
if (!res) {
platform_device_put(dev);
return NULL;
}
dev->num_resources = num_reg + num_irq;
dev->resource = res;
/*解析节点reg属性,生成platform_device的io或者mem资源,将其保存在resource 中*/
for (i = 0; i < num_reg; i++, res++) {
rc = of_address_to_resource(np, i, res);
WARN_ON(rc);
}
/*解析节点中断属性,生成platform_device的irq资源,将其保存在resource中*/
if (of_irq_to_resource_table(np, res, num_irq) != num_irq)
pr_debug("not all legacy IRQ resources mapped for %s\n",
np->name);
}
/*platform_device的of_node指针指向当前device_node*/
dev->dev.of_node = of_node_get(np);
/*初始化platform_device的父设备 */
dev->dev.parent = parent ? : &platform_bus;
/*设置platform_device设备名称*/
if (bus_id)
dev_set_name(&dev->dev, "%s", bus_id);
else
of_device_make_bus_id(&dev->dev);
return dev;
}