GPIO 模拟 MDIO 接口使用代码
如果要通过GPIO来模拟MDIO通信,那么我们就是通过模拟其帧格式的时序来通信,
前面介绍过一遍其帧格式了,MDIO上数据帧的格式如下:
mdio frame
内核中提供了一个mdio-bitbang.c,里面实现了一套软件实现的MDIO/MDC接口时序可供参考。
数据结构
struct mdiobb_ops {
struct module *owner;
/* Set the Management Data Clock high if level is one,
* low if level is zero.
*/
void (*set_mdc)(struct mdiobb_ctrl *ctrl, int level);
/* Configure the Management Data I/O pin as an input if
* "output" is zero, or an output if "output" is one.
*/
void (*set_mdio_dir)(struct mdiobb_ctrl *ctrl, int output);
/* Set the Management Data I/O pin high if value is one,
* low if "value" is zero. This may only be called
* when the MDIO pin is configured as an output.
*/
void (*set_mdio_data)(struct mdiobb_ctrl *ctrl, int value);
/* Retrieve the state Management Data I/O pin. */
int (*get_mdio_data)(struct mdiobb_ctrl *ctrl);
};
struct mdiobb_ctrl {
const struct mdiobb_ops *ops;
};
set_mdc,发送MDC 信号的函数,由具体使用情况来决定实际的mdc 信号是如何发送
set_mdio_dir,设置MDIO方向的函数,由具体使用情况来决定实际的mdc 信号是如何发送
set_mdio_data,发送MDIO数据到MMD,由具体使用情况来决定实际的mdc 信号是如何发送
get_mdio_data,从MMD接收MDIO数据,由具体使用情况来决定实际的mdc 信号是如何发送
使用方法
通过调用 alloc_mdio_bitbang 来获得一个mii_bus, 并且注册read/wirte 方法为 mdiobb_read/mdiobb_write,并且将mdiobb_ctrl 作为mii bus的私有成员来操作器后续的ops 函数
struct mii_bus *alloc_mdio_bitbang(struct m