platform devices创建实例

来看一个cortina Gemini ethernet driver的例子。

DTS file路径 /arch/arm/boot/dts/gemini/gemini.dtsi.

Driver 路径 drivers/net/ethernet/cortina/gemini.c

cpp 复制代码
 {
        soc {
                #address-cells = <1>;
                #size-cells = <1>;
                ranges;
                compatible = "simple-bus";
                ....
      
                ethernet: ethernet@60000000 {
                        compatible = "cortina,gemini-ethernet";
                        reg = <0x60000000 0x4000>, /* Global registers, queue */
                              <0x60004000 0x2000>, /* V-bit */
                              <0x60006000 0x2000>; /* A-bit */
                        pinctrl-names = "default";
                        pinctrl-0 = <&gmii_default_pins>;
                        status = "disabled";
                        #address-cells = <1>;
                        #size-cells = <1>;
                        ranges;

                        gmac0: ethernet-port@0 {
                                compatible = "cortina,gemini-ethernet-port";
                                reg = <0x60008000 0x2000>, /* Port 0 DMA/TOE */
                                      <0x6000a000 0x2000>; /* Port 0 GMAC */
                                interrupt-parent = <&intcon>;
                                interrupts = <1 IRQ_TYPE_LEVEL_HIGH>;
                                resets = <&syscon GEMINI_RESET_GMAC0>;
                                clocks = <&syscon GEMINI_CLK_GATE_GMAC0>;
                                clock-names = "PCLK";
                        };

                        gmac1: ethernet-port@1 {
                                compatible = "cortina,gemini-ethernet-port";
                                reg = <0x6000c000 0x2000>, /* Port 1 DMA/TOE */
                                      <0x6000e000 0x2000>; /* Port 1 GMAC */
                                interrupt-parent = <&intcon>;
                                interrupts = <2 IRQ_TYPE_LEVEL_HIGH>;
                                resets = <&syscon GEMINI_RESET_GMAC1>;
                                clocks = <&syscon GEMINI_CLK_GATE_GMAC1>;
                                clock-names = "PCLK";
                        };
                };
        };
 };

有之前的博文分析, 根节点下的含有compatible属性的节点在内核启动时会为其创建platform device, 因此 soc节点会有一个platform device与其对应。而soc节点的compatible为已知的特殊属性"simple-bus", 因此也会为其下面的子节点创建platform device, 故 "ethernet"也会有platform device 与其对应。那么"ethernet"厂商驱动只需写自己的平台设备驱动即可。对应以太网驱动,"ethernet"下的子节点 "gmac0" 通常是在ethernet 驱动下解析子节点然后为其创建netdevice。

但是我们看gemini driver, 其对于每个ethernet port 即"gmac"子节点也单独设计的platform driver. 那么ethernet port子节点是如何创建成平台devices的呢?

我们看其处理, 在"ethernet" driver probe函数的结尾使用 devm_of_platform_populate 为其子节点创建platfrom device.

cpp 复制代码
static int gemini_ethernet_port_probe(struct platform_device *pdev)
{
    struct gemini_ethernet_port *port;
    struct gemini_ethernet *geth;

    parent = dev->parent; //获取其parent "ethernet"device
	geth = dev_get_drvdata(parent);  // 获取 "etherent" private data

    .....

    port = netdev_priv(netdev);
	SET_NETDEV_DEV(netdev, dev);
	port->netdev = netdev;
	port->id = id;
	port->geth = geth; // ethernet port private与 ethernet private建立关系
}

static int gemini_ethernet_probe(struct platform_device *pdev)
{
    ....
    /* Spawn child devices for the two ports */
	return devm_of_platform_populate(dev); //为 ethernet port 子节点创建平台devices

}

static struct platform_driver gemini_ethernet_driver = {
	.driver = {
		.name = DRV_NAME,
		.of_match_table = gemini_ethernet_of_match,
	},
	.probe = gemini_ethernet_probe,
	.remove_new = gemini_ethernet_remove,
};

static int __init gemini_ethernet_module_init(void)
{
	int ret;

	ret = platform_driver_register(&gemini_ethernet_port_driver);
	if (ret)
		return ret;

	ret = platform_driver_register(&gemini_ethernet_driver);
	if (ret) {
		platform_driver_unregister(&gemini_ethernet_port_driver);
		return ret;
	}

	return 0;
}

思考: 如果dts设计成如下, 如何将ethernet-port 节点创建成platform devices呢?

             ethernet: ethernet@60000000 {
                        compatible = "cortina,gemini-ethernet";
                        ......
                       
                        ethernet-ports {
                            #address-cells = <1>
                            #size-cells = <0>;

                            gmac0: ethernet-port@0 {
                                compatible = "cortina,gemini-ethernet-port";
                                reg = <0x60008000 0x2000>, /* Port 0 DMA/TOE */
                                      <0x6000a000 0x2000>; /* Port 0 GMAC */
                                ...
                            };

                            gmac1: ethernet-port@1 {
                                compatible = "cortina,gemini-ethernet-port";
                                reg = <0x6000c000 0x2000>, /* Port 1 DMA/TOE */
                                      <0x6000e000 0x2000>; /* Port 1 GMAC */
                                ...
                            };
                        };
                };

方法一:为"ethernet-ports"创建平台device

const struct of_device_id of_ethernet_ports_match_table[] = {

{.compatible = "cortina,gemini-ethernet-ports", },

{}

};
of_platform_populate(pdev->dev.of_node, of_ethernet_ports_match_table, NULL, &pdev->dev);

在 ethernet probe 函数结尾使用上述, 同时DTS中为"ethernet-ports" 节点添加compatible 属性"cortina,gemini-ethernet-ports"

这样 of_platform_populate(pdev->dev.of_node, of_ethernet_ports_match_table, NULL, &pdev->dev)函数会为其子节点"ethernet-ports" 创建platform device. 同时为"ethernet-ports"的子节点"ethernet-port"创建platform device.

cat /sys/devices/platform/soc/60000000.ethernet/60000000.ethernet:ethernet-ports/

60000000.ethernet:ethernet-ports:ethernet-port@0/

60000000.ethernet:ethernet-ports:ethernet-port@1/

方法二: 绕过 "ethernet-ports" 为 "etherne-port"创建平台device

ethernet_ports_node = of_get_child_by_name(ppe->dev->of_node, "ethernet-ports");

of_platform_populate(ethernet_ports_node, NULL, NULL, &pdev->dev );

在ethernet driver probe结尾使用上述,即 ethernet driver中parse "ethernet-ports"自己点然后调用populate函数为 "ethernet-ports"自己点创建平台设备。注意这里parent device参数仍然使用ethernet devie, 因为"ethernet-ports"并没有创建任何device, "ethernet-port" device hook在 "ethernet" device上。

cat /sys/devices/platform/soc/60000000.ethernet/

60000000.ethernet:ethernet-ports:ethernet-port@0/

60000000.ethernet:ethernet-ports:ethernet-port@1/

再回顾popuate相关函数的实现, 可以看出最终都归结到of_platform_populate函数,第一个参数root为device node类型, 表示要为改device node下的node穿件平台device, 如果为NULL表示根节点(kernel 启动时就是指定的NULL)。第二个参数 matches 可以指定compatible属性,表示 root的子节点如果有子节点match该属性列表就位子节点的子节点继续创建平台device,为NULL则只会创建一级device。最后一个参数struct device *parent表示 root下第一级子节点device的parent device.

/**
 * of_platform_populate() - Populate platform_devices from device tree data
 * @root: parent of the first level to probe or NULL for the root of the tree
 * @matches: match table, NULL to use the default
 * @lookup: auxdata table for matching id and platform_data with device nodes
 * @parent: parent to hook devices from, NULL for toplevel
 *
 * Similar to of_platform_bus_probe(), this function walks the device tree
 * and creates devices from nodes.  It differs in that it follows the modern
 * convention of requiring all device nodes to have a 'compatible' property,
 * and it is suitable for creating devices which are children of the root
 * node (of_platform_bus_probe will only create children of the root which
 * are selected by the @matches argument).
 *
 * New board support should be using this function instead of
 * of_platform_bus_probe().
 *
 * Return: 0 on success, < 0 on failure.
 */
int of_platform_populate(struct device_node *root,
			const struct of_device_id *matches,
			const struct of_dev_auxdata *lookup,
			struct device *parent)
{
    ......
}


const struct of_device_id of_default_bus_match_table[] = {
	{ .compatible = "simple-bus", },
	{ .compatible = "simple-mfd", },
	{ .compatible = "isa", },
#ifdef CONFIG_ARM_AMBA
	{ .compatible = "arm,amba-bus", },
#endif /* CONFIG_ARM_AMBA */
	{} /* Empty terminated list */
};


int of_platform_default_populate(struct device_node *root,
				 const struct of_dev_auxdata *lookup,
				 struct device *parent)
{
	return of_platform_populate(root, of_default_bus_match_table, lookup,
				    parent);
}

static int __init of_platform_default_populate_init(void)
{
    /* Populate everything else. */
	of_platform_default_populate(NULL, NULL, NULL);
}

int devm_of_platform_populate(struct device *dev)
{
	struct device **ptr;
	int ret;

	if (!dev)
		return -EINVAL;

	ptr = devres_alloc(devm_of_platform_populate_release,
			   sizeof(*ptr), GFP_KERNEL);
	if (!ptr)
		return -ENOMEM;

	ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
	if (ret) {
		devres_free(ptr);
	} else {
		*ptr = dev;
		devres_add(dev, ptr);
	}

	return ret;
}
相关推荐
Jade-YYS几秒前
如何判断FPGA能够接入几个Camera
fpga开发
7yewh1 小时前
嵌入式硬件实战提升篇(一)-泰山派RK3566制作多功能小手机
linux·arm开发·驱动开发·嵌入式硬件·物联网·智能手机·硬件架构
apple_ttt18 小时前
SystemVerilog学习——虚拟接口(Virtual Interface)
fpga开发·fpga·systemverilog·uvm
学习路上_write1 天前
FPGA/Verilog,Quartus环境下if-else语句和case语句RT视图对比/学习记录
单片机·嵌入式硬件·qt·学习·fpga开发·github·硬件工程
jjjxxxhhh1231 天前
FPGA,使用场景,相比于单片机的优势
单片机·嵌入式硬件·fpga开发
7yewh1 天前
嵌入式硬件杂谈(一)-推挽 开漏 高阻态 上拉电阻
驱动开发·stm32·嵌入式硬件·mcu·物联网·硬件架构·pcb工艺
诚实可靠小郎君95271 天前
FPGA高速设计之Aurora64B/66B的应用与不足的修正
fpga开发·aurora·高速通信
百锦再1 天前
基于Zynq FPGA对雷龙SD NAND的测试
fpga开发
TeYiToKu2 天前
笔记整理—linux驱动开发部分(8)framebuffer类设备
linux·驱动开发·笔记·嵌入式硬件·arm
∑狸猫不是猫2 天前
HDLBIts习题(4):边沿检测、分频计数器、多位BCD计数器
fpga开发