- 积分
- 32
贡献163
飞刀45 FD
注册时间2021-12-17
在线时间7 小时
|
楼主 |
发表于 2022-10-12 18:50:01
|
显示全部楼层
嗯,是的,我实现了,分享如下:
修改文件:drivers/net/ethernet/freescale/sdk_dpaa/dpaa_eth_common.c
修改接口:dpa_ioctl
修改的地方:在接口dpa_ioctl的最后一个return之前加入如下的代码:
if( cmd >= SIOCGMIIPHY && cmd <= SIOCSMIIREG)
{
struct dpa_priv_s *priv = netdev_priv(dev);
if( priv && priv->mac_dev && priv->mac_dev->phy_dev )
{
return phy_mii_ioctl(priv->mac_dev->phy_dev, rq, cmd);
}
else
{
netdev_warn(dev, "No phy device\n");
ret = -ENODEV;
}
}
上层使用如下工具就可以读取到网络接口的PHY寄存器了:
#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 <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <unistd.h>
#define reteck(ret) \
if(ret < 0){ \
printf("%m! \"%s\" : line: %d\n", __func__, __LINE__); \
goto lab; \
}
#define help() \
printf("mdio:\n"); \
printf("read all regs: mdio if\n"); \
printf("write regAddr to value : mdio if regAddr value\n"); \
printf("For example:\n"); \
printf("mdio fm1-mac5 \n"); \
printf("mdio fm1-mac5 0 0x1234\n\n"); \
exit(0);
int sockfd;
int main(int argc, char *argv[])
{
uint16_t data[32] = {0};
struct mii_ioctl_data *mii = NULL;
struct ifreq ifr;
int ret;
uint16_t i;
if(argc == 1 || !strcmp(argv[1], "-h"))
{
help();
}
if((argc != 2) && (argc != 4))
{
help();
}
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, argv[1], IFNAMSIZ - 1);
sockfd = socket(PF_LOCAL, SOCK_DGRAM, 0);
reteck(sockfd);
//get phy address in smi bus
ret = ioctl(sockfd, SIOCGMIIPHY, &ifr);
reteck(ret);
mii = (struct mii_ioctl_data*)&ifr.ifr_data;
if(argc == 2)
{
for( i =0 ;i < 32;i++)
{
mii->reg_num = i;
//printf("mii->reg_num = %d\n",mii->reg_num);
ret = ioctl(sockfd, SIOCGMIIREG, &ifr);
reteck(ret);
data = mii->val_out;
}
printf("%s\'s phyAddr = %d\n",ifr.ifr_name,mii->phy_id);
printf("All Phy Reg as follows:\n");
printf("--------------------------------------\n");
for(i = 0 ;i < 32; i++)
{
printf("regAddr = %2d,Val = 0x%04X ",i,data);
if( ((i+1) %2) == 0)
{
printf("\n");
}
}
printf("\n");
}
else if(argc == 4)
{
mii->reg_num = (uint16_t)strtoul(argv[2], NULL, 0);
mii->val_in = (uint16_t)strtoul(argv[3], NULL, 0);
ret = ioctl(sockfd, SIOCSMIIREG, &ifr);
reteck(ret);
printf("write phy addr: 0x%x reg: 0x%x value : 0x%x\n\n", mii->phy_id, mii->reg_num, mii->val_in);
}
lab:
close(sockfd);
return 0;
}
|
|