blob: 8e1ec392bea84e7877646f40581ebbdc449759ce [file] [log] [blame]
developerfd40db22021-04-29 10:08:25 +08001#include <errno.h>
developer0b2f4bb2022-11-17 23:23:28 +08002#include <stdbool.h>
developerfd40db22021-04-29 10:08:25 +08003#include <stdint.h>
4#include <stdio.h>
developer0b2f4bb2022-11-17 23:23:28 +08005#include <stdarg.h>
developerfd40db22021-04-29 10:08:25 +08006#include <stdlib.h>
7#include <string.h>
8#include <unistd.h>
9
10#include <sys/ioctl.h>
11#include <net/if.h>
12#include <sys/socket.h>
13#include <netinet/in.h>
14#include <arpa/inet.h>
15
16#include <linux/ethtool.h>
17#include <linux/mdio.h>
18#include <linux/sockios.h>
19
developer20762252021-05-13 16:38:03 +080020#include "mii_mgr.h"
21
developer0b2f4bb2022-11-17 23:23:28 +080022static bool is_silent;
23
24#define INFO(...) \
25do { \
26 if (!is_silent) { \
27 printf(__VA_ARGS__); \
28 } \
29} while (0)
30
developerfd40db22021-04-29 10:08:25 +080031void show_usage(void)
32{
developer0b2f4bb2022-11-17 23:23:28 +080033 INFO("mii_mgr -g -i [ifname] -p [phy number] -r [register number]\n"
34 " Get: mii_mgr -g -p 3 -r 4\n\n"
35 "mii_mgr -s -p [phy number] -r [register number] -v [0xvalue]\n"
36 " Set: mii_mgr -s -p 4 -r 1 -v 0xff11\n"
37 "#NOTE: Without -i , eth0 is default ifname!\n"
38 " -z: silent mode\n"
39 "----------------------------------------------------------------------------------------\n"
40 "Get: mii_mgr_cl45 -g -p [port number] -d [dev number] -r [register number]\n"
41 "Example: mii_mgr_cl45 -g -p 3 -d 0x5 -r 0x4\n\n"
42 "Set: mii_mgr_cl45 -s -p [port number] -d [dev number] -r [register number] -v [value]\n"
43 "Example: mii_mgr_cl45 -s -p 4 -d 0x6 -r 0x1 -v 0xff11\n\n");
developerfd40db22021-04-29 10:08:25 +080044}
45
developer20762252021-05-13 16:38:03 +080046static void fill_mii_ioctl(struct mii_ioctl_data *mii, uint16_t phy_id,
47 uint16_t reg_num, uint16_t *val)
48{
49 mii->phy_id = phy_id;
50 mii->reg_num = reg_num;
51 mii->val_in = *val;
52 mii->val_out = 0;
53}
54
55
56static void fill_mtk_mii_ioctl(struct mtk_mii_ioctl_data *mtk_mii, uint16_t phy_id,
57 uint16_t reg_num, unsigned int *val)
58{
59 mtk_mii->phy_id = phy_id;
60 mtk_mii->reg_num = reg_num;
61 mtk_mii->val_in = *val;
62 mtk_mii->val_out = 0;
63}
64
65static 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 +080066{
developer0b2f4bb2022-11-17 23:23:28 +080067 static int sd = -1;
developerfd40db22021-04-29 10:08:25 +080068
developer0b2f4bb2022-11-17 23:23:28 +080069 struct ifreq ifr;
70 struct mii_ioctl_data mii;
developer20762252021-05-13 16:38:03 +080071 struct mtk_mii_ioctl_data mtk_mii;
developer0b2f4bb2022-11-17 23:23:28 +080072 int err;
developerfd40db22021-04-29 10:08:25 +080073
developer0b2f4bb2022-11-17 23:23:28 +080074 if (sd < 0)
75 sd = socket(AF_INET, SOCK_DGRAM, 0);
developerfd40db22021-04-29 10:08:25 +080076
developer0b2f4bb2022-11-17 23:23:28 +080077 if (sd < 0)
78 return sd;
developerfd40db22021-04-29 10:08:25 +080079
developer1827bda2021-12-16 12:31:20 +080080 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
81 ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
developerfd40db22021-04-29 10:08:25 +080082
developer20762252021-05-13 16:38:03 +080083 if (is_priv) {
84 fill_mtk_mii_ioctl(&mtk_mii, phy_id, reg_num, val);
85 ifr.ifr_data = (char *)&mtk_mii;
86 } else {
87 fill_mii_ioctl(&mii, phy_id, reg_num, (uint16_t *)val);
88 ifr.ifr_data = (char *)&mii;
89 }
developerfd40db22021-04-29 10:08:25 +080090
developer0b2f4bb2022-11-17 23:23:28 +080091 err = ioctl(sd, cmd, &ifr);
92 if (err)
93 return -errno;
developerfd40db22021-04-29 10:08:25 +080094
developer20762252021-05-13 16:38:03 +080095 if ((cmd == MTKETH_MII_WRITE) || (cmd == MTKETH_MII_WRITE_CL45) ||
96 (cmd == SIOCSMIIREG))
97 *val = (is_priv) ? mtk_mii.val_in : mii.val_in;
98 else
99 *val = (is_priv) ? mtk_mii.val_out : mii.val_out;
100
developer0b2f4bb2022-11-17 23:23:28 +0800101 return 0;
developerfd40db22021-04-29 10:08:25 +0800102}
103
104int main(int argc, char *argv[])
105{
106 int opt;
developer20762252021-05-13 16:38:03 +0800107 char options[] = "gsui:p:d:r:v:?t";
developerfd40db22021-04-29 10:08:25 +0800108 int is_write = 0,is_cl45 = 0;
developer20762252021-05-13 16:38:03 +0800109 int is_priv = 1;
developerfd40db22021-04-29 10:08:25 +0800110 unsigned int port=0, dev=0,reg_num=0,val=0;
developer0b2f4bb2022-11-17 23:23:28 +0800111 char ifname[IFNAMSIZ] = "eth0";
developerfd40db22021-04-29 10:08:25 +0800112 uint16_t phy_id=0;
developer20762252021-05-13 16:38:03 +0800113 uint16_t cmd;
developerfd40db22021-04-29 10:08:25 +0800114
developerfd40db22021-04-29 10:08:25 +0800115 if (argc < 6) {
116 show_usage();
117 return 0;
118 }
119
120 while ((opt = getopt(argc, argv, options)) != -1) {
121 switch (opt) {
122 case 'g':
123 is_write=0;
124 break;
125 case 's':
126 is_write=1;
127 break;
developer20762252021-05-13 16:38:03 +0800128 case 'u':
129 is_priv = 0;
130 break;
developerfd40db22021-04-29 10:08:25 +0800131 case 'i':
developer1827bda2021-12-16 12:31:20 +0800132 strncpy(ifname, optarg, 5);
133 ifname[IFNAMSIZ - 1] = '\0';
developer0b2f4bb2022-11-17 23:23:28 +0800134 break;
developerfd40db22021-04-29 10:08:25 +0800135 case 'p':
136 port = strtoul(optarg, NULL, 16);
developerfc356752022-04-24 21:37:49 +0800137 if (port > INT_MAX)
138 return -EINVAL;
developerfd40db22021-04-29 10:08:25 +0800139 break;
developer0b2f4bb2022-11-17 23:23:28 +0800140 case 'd':
141 dev = strtoul(optarg, NULL, 16);
developerfc356752022-04-24 21:37:49 +0800142 if (dev > INT_MAX)
143 return -EINVAL;
developerfd40db22021-04-29 10:08:25 +0800144 is_cl45 = 1;
145 break;
146 case 'r':
147 reg_num = strtoul(optarg, NULL, 16);
developerfc356752022-04-24 21:37:49 +0800148 if (reg_num > INT_MAX)
149 return -EINVAL;
developerfd40db22021-04-29 10:08:25 +0800150 break;
developerfd40db22021-04-29 10:08:25 +0800151 case 'v':
152 val = strtoul(optarg, NULL, 16);
developerfc356752022-04-24 21:37:49 +0800153 if (val > INT_MAX)
154 return -EINVAL;
developerfd40db22021-04-29 10:08:25 +0800155 break;
developer0b2f4bb2022-11-17 23:23:28 +0800156 case 'z':
157 is_silent = true;
158 break;
developerfd40db22021-04-29 10:08:25 +0800159 case '?':
160 show_usage();
161 break;
162 }
163 }
164
165 if(is_cl45)
166 phy_id = mdio_phy_id_c45(port, dev);
167 else
168 phy_id = port;
169
developer0b2f4bb2022-11-17 23:23:28 +0800170 if (is_write) {
developer20762252021-05-13 16:38:03 +0800171 if (is_priv)
172 cmd = (is_cl45) ? MTKETH_MII_WRITE_CL45 :
173 MTKETH_MII_WRITE;
174 else
175 cmd = SIOCSMIIREG;
176
177 __phy_op(ifname,phy_id,reg_num, &val, cmd, is_priv);
developerfd40db22021-04-29 10:08:25 +0800178
179 if(is_cl45)
developer0b2f4bb2022-11-17 23:23:28 +0800180 INFO("Set: port%x dev%Xh_reg%Xh = 0x%04X\n",
181 port, dev, reg_num, val);
developerfd40db22021-04-29 10:08:25 +0800182 else
developer0b2f4bb2022-11-17 23:23:28 +0800183 INFO("Set: phy[%x].reg[%x] = %04x\n",
184 port, reg_num, val);
185 }
developerfd40db22021-04-29 10:08:25 +0800186 else {
developer20762252021-05-13 16:38:03 +0800187 if (is_priv)
188 cmd = (is_cl45) ? MTKETH_MII_READ_CL45 :
189 MTKETH_MII_READ;
190 else
191 cmd = SIOCGMIIREG;
192
193 __phy_op(ifname,phy_id,reg_num, &val, cmd, is_priv);
developerfd40db22021-04-29 10:08:25 +0800194
195 if(is_cl45)
developer0b2f4bb2022-11-17 23:23:28 +0800196 INFO("Get: port%x dev%Xh_reg%Xh = 0x%04X\n",
197 port, dev, reg_num, val);
developerfd40db22021-04-29 10:08:25 +0800198 else
developer0b2f4bb2022-11-17 23:23:28 +0800199 INFO("Get: phy[%x].reg[%x] = %04x\n",
200 port, reg_num, val);
developerfd40db22021-04-29 10:08:25 +0800201 }
202
developer0b2f4bb2022-11-17 23:23:28 +0800203 return 0;
developerfd40db22021-04-29 10:08:25 +0800204}