developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1 | #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 | |
developer | 2076225 | 2021-05-13 16:38:03 +0800 | [diff] [blame^] | 18 | #include "mii_mgr.h" |
| 19 | |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 20 | void 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 | |
developer | 2076225 | 2021-05-13 16:38:03 +0800 | [diff] [blame^] | 34 | static 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 | |
| 44 | static 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 | |
| 53 | static int __phy_op(char *ifname, uint16_t phy_id, uint16_t reg_num, unsigned int *val, uint16_t cmd, int is_priv) |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 54 | { |
| 55 | static int sd = -1; |
| 56 | |
| 57 | struct ifreq ifr; |
developer | 2076225 | 2021-05-13 16:38:03 +0800 | [diff] [blame^] | 58 | struct mii_ioctl_data mii; |
| 59 | struct mtk_mii_ioctl_data mtk_mii; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 60 | int err; |
| 61 | |
| 62 | if (sd < 0) |
| 63 | sd = socket(AF_INET, SOCK_DGRAM, 0); |
| 64 | |
| 65 | if (sd < 0) |
| 66 | return sd; |
| 67 | |
| 68 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); |
| 69 | |
developer | 2076225 | 2021-05-13 16:38:03 +0800 | [diff] [blame^] | 70 | if (is_priv) { |
| 71 | fill_mtk_mii_ioctl(&mtk_mii, phy_id, reg_num, val); |
| 72 | ifr.ifr_data = (char *)&mtk_mii; |
| 73 | } else { |
| 74 | fill_mii_ioctl(&mii, phy_id, reg_num, (uint16_t *)val); |
| 75 | ifr.ifr_data = (char *)&mii; |
| 76 | } |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 77 | |
| 78 | err = ioctl(sd, cmd, &ifr); |
| 79 | if (err) |
| 80 | return -errno; |
| 81 | |
developer | 2076225 | 2021-05-13 16:38:03 +0800 | [diff] [blame^] | 82 | if ((cmd == MTKETH_MII_WRITE) || (cmd == MTKETH_MII_WRITE_CL45) || |
| 83 | (cmd == SIOCSMIIREG)) |
| 84 | *val = (is_priv) ? mtk_mii.val_in : mii.val_in; |
| 85 | else |
| 86 | *val = (is_priv) ? mtk_mii.val_out : mii.val_out; |
| 87 | |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | int main(int argc, char *argv[]) |
| 92 | { |
| 93 | int opt; |
developer | 2076225 | 2021-05-13 16:38:03 +0800 | [diff] [blame^] | 94 | char options[] = "gsui:p:d:r:v:?t"; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 95 | int is_write = 0,is_cl45 = 0; |
developer | 2076225 | 2021-05-13 16:38:03 +0800 | [diff] [blame^] | 96 | int is_priv = 1; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 97 | unsigned int port=0, dev=0,reg_num=0,val=0; |
| 98 | char ifname[IFNAMSIZ]="eth0"; |
| 99 | uint16_t phy_id=0; |
developer | 2076225 | 2021-05-13 16:38:03 +0800 | [diff] [blame^] | 100 | uint16_t cmd; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 101 | |
| 102 | |
| 103 | if (argc < 6) { |
| 104 | show_usage(); |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | while ((opt = getopt(argc, argv, options)) != -1) { |
| 109 | switch (opt) { |
| 110 | case 'g': |
| 111 | is_write=0; |
| 112 | break; |
| 113 | case 's': |
| 114 | is_write=1; |
| 115 | break; |
developer | 2076225 | 2021-05-13 16:38:03 +0800 | [diff] [blame^] | 116 | case 'u': |
| 117 | is_priv = 0; |
| 118 | break; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 119 | case 'i': |
| 120 | strncpy(ifname,optarg, 5); |
| 121 | break; |
| 122 | case 'p': |
| 123 | port = strtoul(optarg, NULL, 16); |
| 124 | break; |
| 125 | case 'd': |
| 126 | dev = strtoul(optarg, NULL, 16); |
| 127 | is_cl45 = 1; |
| 128 | break; |
| 129 | case 'r': |
| 130 | reg_num = strtoul(optarg, NULL, 16); |
| 131 | break; |
| 132 | |
| 133 | case 'v': |
| 134 | val = strtoul(optarg, NULL, 16); |
| 135 | break; |
| 136 | case '?': |
| 137 | show_usage(); |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | if(is_cl45) |
| 143 | phy_id = mdio_phy_id_c45(port, dev); |
| 144 | else |
| 145 | phy_id = port; |
| 146 | |
| 147 | if(is_write) { |
developer | 2076225 | 2021-05-13 16:38:03 +0800 | [diff] [blame^] | 148 | if (is_priv) |
| 149 | cmd = (is_cl45) ? MTKETH_MII_WRITE_CL45 : |
| 150 | MTKETH_MII_WRITE; |
| 151 | else |
| 152 | cmd = SIOCSMIIREG; |
| 153 | |
| 154 | __phy_op(ifname,phy_id,reg_num, &val, cmd, is_priv); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 155 | |
| 156 | if(is_cl45) |
| 157 | printf("Set: port%x dev%Xh_reg%Xh = 0x%04X\n",port, dev, reg_num, val); |
| 158 | else |
| 159 | printf("Set: phy[%x].reg[%x] = %04x\n",port, reg_num, val); |
| 160 | } |
| 161 | else { |
developer | 2076225 | 2021-05-13 16:38:03 +0800 | [diff] [blame^] | 162 | if (is_priv) |
| 163 | cmd = (is_cl45) ? MTKETH_MII_READ_CL45 : |
| 164 | MTKETH_MII_READ; |
| 165 | else |
| 166 | cmd = SIOCGMIIREG; |
| 167 | |
| 168 | __phy_op(ifname,phy_id,reg_num, &val, cmd, is_priv); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 169 | |
| 170 | if(is_cl45) |
| 171 | printf("Get: port%x dev%Xh_reg%Xh = 0x%04X\n",port, dev, reg_num, val); |
| 172 | else |
| 173 | printf("Get: phy[%x].reg[%x] = %04x\n",port, reg_num, val); |
| 174 | |
| 175 | } |
| 176 | |
| 177 | return 0; |
| 178 | } |