文章目录
- MAC驱动支持不同的PHY芯片
- 关于对PHY设备抽象的改进
- RT-Thread下PHY设备抽象接口的改进
- 关于对PHY设备抽象的改进
这是个人驱动开发过程中做的一些记录,仅代表个人意见和理解,不喜勿喷
- MAC驱动需要支持不同的PHY芯片
MAC驱动支持不同的PHY芯片
关于对PHY设备抽象的改进
系列文章2中有提及到,在RT-Thread下定义的PHY操作抽象接口并不是很合理,比如你的系统里面有2个PHY的时候,你需要对每个PHY的操作接口做独立的实现,否则你无法根据当前read操作所传入的参数来区分当前操作的是哪个PHY设备:
先让我们来看下按照现有RT-Thread PHY抽象接口的定义,现在需要实现一个获取当前link状态的操作接口的实现,抽象接口的定义和具体实现如下:
rt_phy_status (*get_link_status)(rt_bool_t *status);
static rt_phy_status h3_kszplib_read(rt_uint32_t reg, rt_uint32_t *data)
{rt_mdio_t *mdio_bus = h3_kszdev0.rt_phydev.bus;rt_uint32_t device_id = h3_kszdev0.rt_phydev.addr;if (4 != mdio_bus->ops->read(mdio_bus, device_id,reg, (void *)data, 4)){return RT_FALSE;}return PHY_STATUS_OK;
}
static rt_phy_status h3_kszplib_linkstatus(rt_bool_t *status)
{rt_phy_status result;rt_uint32_t data;/* Read the basic status register. */result = h3_kszplib_read(MII_MSR, &data);RESULT_MATCH_CHECK(result, PHY_STATUS_FAIL, outs)if (!(MII_MSR_LINKSTATUS & data)){*status = RT_FALSE; /* link down. */}else{*status = RT_TRUE; /* link up. */}outs:return result;
}
那对于h3_kszplib_read接口函数来说,它就无法区分是哪个PHY设备实例来调用它去读取PHY的寄存器,因为get_link_status()这个抽象接口也没法获取关于设备实例的信息。
RT-Thread下PHY设备抽象接口的改进
struct rt_phy_ops
{rt_phy_status (*init)(struct rt_phy_device *phy, void *object, rt_uint32_t phy_addr, rt_uint32_t src_clock_hz);rt_phy_status (*read)(struct rt_phy_device *phy, rt_uint32_t reg, rt_uint32_t *data);rt_phy_status (*write)(struct rt_phy_device *phy, rt_uint32_t reg, rt_uint32_t data);rt_phy_status (*loopback)(struct rt_phy_device *phy, rt_uint32_t mode, rt_uint32_t speed, rt_bool_t enable);rt_phy_status (*get_link_status)(struct rt_phy_device *phy, rt_bool_t *status);rt_phy_status (*get_link_speed_duplex)(struct rt_phy_device *phy, rt_uint32_t *speed, rt_uint32_t *duplex);
};
在RT-Thread下定义的PHY操作抽象接口中,传入struct rt_phy_device *phy这个参数,后续的驱动代码可以根据传入的参数,来进行进一步的操作,找到具体操作的请求来自于哪一个PHY设备的实例
static rt_phy_status h3_kszplib_read(struct rt_phy_device *phy,rt_uint32_t reg, rt_uint32_t *data)
{rt_mdio_t *mdio_bus = phy->bus;rt_uint32_t phy_addr = phy->addr;if (4 != mdio_bus->ops->read(mdio_bus, phy_addr, reg, (void *)data, 4)){return RT_FALSE;}return PHY_STATUS_OK;
}
static rt_phy_status h3_kszplib_linkstatus(struct rt_phy_device *phy, rt_bool_t *status)
{rt_phy_status result;rt_uint32_t data;RT_ASSERT(phy != RT_NULL);/* Read the basic status register. */result = h3_kszplib_read(phy, MII_MSR, &data);RESULT_MATCH_CHECK(result, PHY_STATUS_FAIL, outs)if (!(MII_MSR_LINKSTATUS & data)){*status = RT_FALSE; /* link down. */}else{*status = RT_TRUE; /* link up. */}outs:return result;
}
用户驱动可以通过以下方法去获取在自己驱动中定义的每个PHY设备的实例,进而获取更多驱动所需要的信息,来更好地实现PHY驱动的兼容性。
struct h3_kszplib_dev *kszplib_dev;RT_ASSERT(phy != RT_NULL);kszplib_dev = rt_container_of(phy, struct h3_kszplib_dev, rt_phydev);
关于对PHY设备抽象的改进
通过上述的修改后,可以通过BSP_USING_PHY0这样的宏定义(在Kconfig中进行定义和选择),来实现驱动中对多个PHY的驱动支持和灵活配置。
#ifdef BSP_USING_PHY0
static struct rt_phy_ops h3_ksz0plib_ops =
{.init = H3_KSZPLIB_PHY0INIT,.read = h3_kszplib_read,.write = h3_kszplib_write,.loopback = h3_kszplib_loopback,.get_link_status = h3_kszplib_linkstatus,.get_link_speed_duplex = H3_KSZPLIB_PHY0LINKS,
};static struct h3_kszplib_dev h3_kszdev0 =
{.name = PHY0_DEVICE_NAME,.phy_addr = PHY0_DEVICE_ADDRESS,.rt_phydev ={.ops = &h3_ksz0plib_ops,}
};
#endif // BSP_USING_PHY0
同样在PHY设备注册代码中,也可以采取非常灵活的方式,来实现对多个PHY设备的注册操作
int h3_kszplib_init(void)
{rt_uint32_t table_sz = sizeof(h3_kszplib_devtable)/sizeof(uint32_t);struct h3_kszplib_dev *kszplib_dev;for (uint32_t i = 1; i < table_sz; i++){kszplib_dev = h3_kszplib_devtable[i];rt_hw_phy_register(&kszplib_dev->rt_phydev, kszplib_dev->name);}return RT_EOK;
}