嵌入式linux开发,Linux下访问PHY芯片寄存器,获取phyID号,获取phy的link状态
| REGISTER INDEX (DECIMAL) | REGISTER NAME | GROUP |
|---|---|---|
| 0 | Basic Control Register | Basic |
| 1 | Basic Status Register | Basic |
| 2 | PHY Identifier 1 | Extended |
| 3 | PHY Identifier 2 | Extended |
| 4 | Auto-Negotiation Advertisement Register | Extended |
| 5 | Auto-Negotiation Link Partner Ability Register | Extended |
| 6 | Auto-Negotiation Expansion Register | Extended |
| 7 | ||
| 8 | ||
| 9 | ||
| 10 | ||
| 11 | ||
| 12 | ||
| 13 | ||
| 14 | ||
| 15 | ||
| 16 | ||
| 17 | Mode Control/Status Register | Vendor-specific |
| 18 | Special Modes | Vendor-specific |
| 19 | ||
| 20 | ||
| 21 | ||
| 22 | ||
| 23 | ||
| 24 | ||
| 25 | ||
| 26 | Symbol Error Counter Register | Vendor-specific |
| 27 | Control / Status Indication Register | Vendor-specific |
| 28 | ||
| 29 | Interrupt Source Register | Vendor-specific |
| 30 | Interrupt Mask Register | Vendor-specific |
| 31 | PHY Special Control/Status Register | Vendor-specific |
通过状态寄存器Basic Status Register获取,取值mii->val_out& 0x0004。
- Link Status
- 0 = link is down
- 1 = link is up
大多数phy芯片的寄存器0为控制寄存器, 寄存器1 为状态寄存器。寄存器3和4为 Identifiier Register,为phy芯片产商的识别码。
寄存器1都是 Status register,有16个bit, 第2个bit为link 状态, 第5个bit为自动协商, 一般这个状态寄存器的数值为: 0x796d。部分bit位说明如下
- 14bit : 有能力实现全双工100BASE-X工作模式
- 13bit : 有能力实现半双工 100BASE-X工作模式
- 12bit : 有能力实现全双工 10BASE-T工作模式
- 11bit : 有能力实现半双工 10BASE-T工作模式
- 8bit : 扩展信息描述在寄存器15.
- 6bit : MF报头抑制
- 5bit : 自动协商完成
- 3bit : 有能力自动协商
- 2bit : link 状态: up
- 0bit : 有扩展寄存器
./a.out -h
./a.out eth0 1 读取phy寄存器1的数值
sudo ./a.out ens33 0
./a.out eth0 0 0x1120 将0x1120写入 phy寄存器1
- /*************************************************************************
- > File Name:
- > Author:
- > Mail:
- > Created Time:
- ************************************************************************/
- /*
- Linux 下smi/mdio总线通信
- 下面代码描述了在用户层访问smi/mdio总线, 读写phy芯片寄存器的通用代码。Linux内核2.6以上通用。
- 将下面代码编译后,将可执行文件a.out 重命名为mdio
- mdio eth0 1 读取phy寄存器1的数值
- mdio eth0 0 0x1120 将0x1120写入 phy寄存器1
- eth0 为mac层控制器的名称, 一般为eth0或mgmt0或ens33。
- */
-
- #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>
-
- // /* This structure is used in all SIOCxMIIxxx ioctl calls */
- // struct mii_ioctl_data {
- // __u16 phy_id;
- // __u16 reg_num;
- // __u16 val_in;
- // __u16 val_out;
- // };
-
- #define reteck(ret) \
- if(ret < 0){ \
- printf("%m! \"%s\" : line: %d\n", __func__, __LINE__); \
- goto lab; \
- }
-
- #define help() \
- printf("mdio:\n"); \
- printf("read operation: mdio reg_addr\n"); \
- printf("write operation: mdio reg_addr value\n"); \
- printf("For example:\n"); \
- printf("mdio eth0 1\n"); \
- printf("mdio eth0 0 0x12\n\n"); \
- exit(0);
-
- int sockfd;
-
- int main(int argc, char *argv[]){
-
- if(argc == 1 || !strcmp(argv[1], "-h")){
- help();
- }
-
- struct mii_ioctl_data *mii = NULL;
- struct ifreq ifr;
- int ret;
-
- 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 == 3){
-
- mii->reg_num = (uint16_t)strtoul(argv[2], NULL, 0);
-
- ret = ioctl(sockfd, SIOCGMIIREG, &ifr);
- reteck(ret);
-
- printf("read phy addr: 0x%x reg: 0x%x value : 0x%x\n", mii->phy_id, mii->reg_num, mii->val_out);
-
- if (mii->reg_num == 0x1)
- {
- printf("Link Status\n");
-
- if(mii->val_out& 0x0004){
- printf("link is up\n");
- }else{
- printf("link is down\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", mii->phy_id, mii->reg_num, mii->val_in);
- }
-
- lab:
- close(sockfd);
- return 0;
- }