blob: a22c3e84b63857ea94fa6b35bdaecc7be110bac5 [file] [log] [blame]
developerfd40db22021-04-29 10:08:25 +08001#include <errno.h>
2#include <stdint.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6#include <unistd.h>
7
8#include <sys/ioctl.h>
9#include <net/if.h>
10#include <sys/socket.h>
11#include <netinet/in.h>
12#include <arpa/inet.h>
13
14#include <linux/ethtool.h>
15#include <linux/mdio.h>
16#include <linux/sockios.h>
17
developer20762252021-05-13 16:38:03 +080018#include "mii_mgr.h"
19
developerfd40db22021-04-29 10:08:25 +080020void show_usage(void)
21{
22 printf("mii_mgr -g -i [ifname] -p [phy number] -r [register number]\n");
23 printf(" Get: mii_mgr -g -p 3 -r 4\n\n");
24 printf("mii_mgr -s -p [phy number] -r [register number] -v [0xvalue]\n");
25 printf(" Set: mii_mgr -s -p 4 -r 1 -v 0xff11\n");
26 printf("#NOTE: Without -i , eth0 is default ifname!\n");
27 printf("----------------------------------------------------------------------------------------\n");
28 printf("Get: mii_mgr_cl45 -g -p [port number] -d [dev number] -r [register number]\n");
29 printf("Example: mii_mgr_cl45 -g -p 3 -d 0x5 -r 0x4\n\n");
30 printf("Set: mii_mgr_cl45 -s -p [port number] -d [dev number] -r [register number] -v [value]\n");
31 printf("Example: mii_mgr_cl45 -s -p 4 -d 0x6 -r 0x1 -v 0xff11\n\n");
32}
33
developer20762252021-05-13 16:38:03 +080034static void fill_mii_ioctl(struct mii_ioctl_data *mii, uint16_t phy_id,
35 uint16_t reg_num, uint16_t *val)
36{
37 mii->phy_id = phy_id;
38 mii->reg_num = reg_num;
39 mii->val_in = *val;
40 mii->val_out = 0;
41}
42
43
44static void fill_mtk_mii_ioctl(struct mtk_mii_ioctl_data *mtk_mii, uint16_t phy_id,
45 uint16_t reg_num, unsigned int *val)
46{
47 mtk_mii->phy_id = phy_id;
48 mtk_mii->reg_num = reg_num;
49 mtk_mii->val_in = *val;
50 mtk_mii->val_out = 0;
51}
52
53static int __phy_op(char *ifname, uint16_t phy_id, uint16_t reg_num, unsigned int *val, uint16_t cmd, int is_priv)
developerfd40db22021-04-29 10:08:25 +080054{
55 static int sd = -1;
56
57 struct ifreq ifr;
developer20762252021-05-13 16:38:03 +080058 struct mii_ioctl_data mii;
59 struct mtk_mii_ioctl_data mtk_mii;
developerfd40db22021-04-29 10:08:25 +080060 int err;
61
62 if (sd < 0)
63 sd = socket(AF_INET, SOCK_DGRAM, 0);
64
65 if (sd < 0)
66 return sd;
67
developer1827bda2021-12-16 12:31:20 +080068 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
69 ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
developerfd40db22021-04-29 10:08:25 +080070
developer20762252021-05-13 16:38:03 +080071 if (is_priv) {
72 fill_mtk_mii_ioctl(&mtk_mii, phy_id, reg_num, val);
73 ifr.ifr_data = (char *)&mtk_mii;
74 } else {
75 fill_mii_ioctl(&mii, phy_id, reg_num, (uint16_t *)val);
76 ifr.ifr_data = (char *)&mii;
77 }
developerfd40db22021-04-29 10:08:25 +080078
79 err = ioctl(sd, cmd, &ifr);
80 if (err)
81 return -errno;
82
developer20762252021-05-13 16:38:03 +080083 if ((cmd == MTKETH_MII_WRITE) || (cmd == MTKETH_MII_WRITE_CL45) ||
84 (cmd == SIOCSMIIREG))
85 *val = (is_priv) ? mtk_mii.val_in : mii.val_in;
86 else
87 *val = (is_priv) ? mtk_mii.val_out : mii.val_out;
88
developerfd40db22021-04-29 10:08:25 +080089 return 0;
90}
91
92int main(int argc, char *argv[])
93{
94 int opt;
developer20762252021-05-13 16:38:03 +080095 char options[] = "gsui:p:d:r:v:?t";
developerfd40db22021-04-29 10:08:25 +080096 int is_write = 0,is_cl45 = 0;
developer20762252021-05-13 16:38:03 +080097 int is_priv = 1;
developerfd40db22021-04-29 10:08:25 +080098 unsigned int port=0, dev=0,reg_num=0,val=0;
99 char ifname[IFNAMSIZ]="eth0";
100 uint16_t phy_id=0;
developer20762252021-05-13 16:38:03 +0800101 uint16_t cmd;
developerfd40db22021-04-29 10:08:25 +0800102
103
104 if (argc < 6) {
105 show_usage();
106 return 0;
107 }
108
109 while ((opt = getopt(argc, argv, options)) != -1) {
110 switch (opt) {
111 case 'g':
112 is_write=0;
113 break;
114 case 's':
115 is_write=1;
116 break;
developer20762252021-05-13 16:38:03 +0800117 case 'u':
118 is_priv = 0;
119 break;
developerfd40db22021-04-29 10:08:25 +0800120 case 'i':
developer1827bda2021-12-16 12:31:20 +0800121 strncpy(ifname, optarg, 5);
122 ifname[IFNAMSIZ - 1] = '\0';
developerfd40db22021-04-29 10:08:25 +0800123 break;
124 case 'p':
125 port = strtoul(optarg, NULL, 16);
developerfc356752022-04-24 21:37:49 +0800126 if (port > INT_MAX)
127 return -EINVAL;
developerfd40db22021-04-29 10:08:25 +0800128 break;
129 case 'd':
130 dev = strtoul(optarg, NULL, 16);
developerfc356752022-04-24 21:37:49 +0800131 if (dev > INT_MAX)
132 return -EINVAL;
developerfd40db22021-04-29 10:08:25 +0800133 is_cl45 = 1;
134 break;
135 case 'r':
136 reg_num = strtoul(optarg, NULL, 16);
developerfc356752022-04-24 21:37:49 +0800137 if (reg_num > INT_MAX)
138 return -EINVAL;
developerfd40db22021-04-29 10:08:25 +0800139 break;
140
141 case 'v':
142 val = strtoul(optarg, NULL, 16);
developerfc356752022-04-24 21:37:49 +0800143 if (val > INT_MAX)
144 return -EINVAL;
developerfd40db22021-04-29 10:08:25 +0800145 break;
146 case '?':
147 show_usage();
148 break;
149 }
150 }
151
152 if(is_cl45)
153 phy_id = mdio_phy_id_c45(port, dev);
154 else
155 phy_id = port;
156
157 if(is_write) {
developer20762252021-05-13 16:38:03 +0800158 if (is_priv)
159 cmd = (is_cl45) ? MTKETH_MII_WRITE_CL45 :
160 MTKETH_MII_WRITE;
161 else
162 cmd = SIOCSMIIREG;
163
164 __phy_op(ifname,phy_id,reg_num, &val, cmd, is_priv);
developerfd40db22021-04-29 10:08:25 +0800165
166 if(is_cl45)
167 printf("Set: port%x dev%Xh_reg%Xh = 0x%04X\n",port, dev, reg_num, val);
168 else
169 printf("Set: phy[%x].reg[%x] = %04x\n",port, reg_num, val);
170 }
171 else {
developer20762252021-05-13 16:38:03 +0800172 if (is_priv)
173 cmd = (is_cl45) ? MTKETH_MII_READ_CL45 :
174 MTKETH_MII_READ;
175 else
176 cmd = SIOCGMIIREG;
177
178 __phy_op(ifname,phy_id,reg_num, &val, cmd, is_priv);
developerfd40db22021-04-29 10:08:25 +0800179
180 if(is_cl45)
181 printf("Get: port%x dev%Xh_reg%Xh = 0x%04X\n",port, dev, reg_num, val);
182 else
183 printf("Get: phy[%x].reg[%x] = %04x\n",port, reg_num, val);
184
185 }
186
187 return 0;
188}