在驱动层,设备树绑定mac与mdio总线的关系。
在Linux应用层,编写测试程序mdio 。代码如下:
如果是写寄存器操作,就传入四个参数./mdio ethX phyId addr value.。
ethX 表示是哪个网卡,如eth0 eth1 。
phyId是 phy的物理地址,一般0x00是广播地址.有些phy 的0x00不是广播地址,如marvell 的88e1512 ID 只能时0x00 0x11,0x00不是88e1512的广播地址。
addr 是phy手册的寄存器地址
value 是phy地址要写入的值
如果是读寄存器操作,就传入三个参数./mdio ethX phyId addr
include <stdio.h>
include <stdlib.h>
include <string.h>
include <linux/mii.h>
include <sys/types.h>
include <sys/socket.h>
include <sys/ioctl.h>
include <net/if.h>
include <linux/sockios.h>
include <linux/types.h>
include <netinet/in.h>
include <unistd.h>
//#include
int main(int argc, char *argv[])
{
int sockfd;
struct mii_ioctl_data *mii = NULL;
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, "eth0", IFNAMSIZ - 1);
sockfd = socket(PF_LOCAL, SOCK_DGRAM, 0);
ioctl(sockfd, SIOCGMIIPHY, &ifr);
mii = (struct mii_ioctl_data*)&ifr.ifr_data;
if(argc == 4)
{
strncpy(ifr.ifr_name, argv[1], IFNAMSIZ - 1);
sockfd = socket(PF_LOCAL, SOCK_DGRAM, 0);
ioctl(sockfd, SIOCGMIIPHY, &ifr);
mii = (struct mii_ioctl_data*)&ifr.ifr_data;
mii->phy_id = (uint16_t)strtoul(argv[2], NULL, 0);
mii->reg_num = (uint16_t)strtoul(argv[3], NULL, 0);
ioctl(sockfd, SIOCGMIIREG, &ifr);
printf("read --- value : 0x%x", mii->val_out);
}
else if(argc == 5)
{
strncpy(ifr.ifr_name, argv[1], IFNAMSIZ - 1);
sockfd = socket(PF_LOCAL, SOCK_DGRAM, 0);
ioctl(sockfd, SIOCGMIIPHY, &ifr);
mii = (struct mii_ioctl_data*)&ifr.ifr_data;
mii->phy_id = (uint16_t)strtoul(argv[2], NULL, 0);
mii->reg_num = (uint16_t)strtoul(argv[3], NULL, 0);
mii->val_in = (uint16_t)strtoul(argv[4], NULL, 0);
ioctl(sockfd, SIOCSMIIREG, &ifr);
}else{
printf("mdio ethX phyId addr value\n");
}
close(sockfd);
return 0;
}