developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1 | #include <errno.h> |
developer | 0b2f4bb | 2022-11-17 23:23:28 +0800 | [diff] [blame] | 2 | #include <stdbool.h> |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 3 | #include <stdint.h> |
| 4 | #include <stdio.h> |
developer | 0b2f4bb | 2022-11-17 23:23:28 +0800 | [diff] [blame] | 5 | #include <stdarg.h> |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 6 | #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 | |
developer | 2076225 | 2021-05-13 16:38:03 +0800 | [diff] [blame] | 20 | #include "mii_mgr.h" |
| 21 | |
developer | 0b2f4bb | 2022-11-17 23:23:28 +0800 | [diff] [blame] | 22 | static bool is_silent; |
| 23 | |
| 24 | #define INFO(...) \ |
| 25 | do { \ |
| 26 | if (!is_silent) { \ |
| 27 | printf(__VA_ARGS__); \ |
| 28 | } \ |
| 29 | } while (0) |
| 30 | |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 31 | void show_usage(void) |
| 32 | { |
developer | 0b2f4bb | 2022-11-17 23:23:28 +0800 | [diff] [blame] | 33 | 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"); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 44 | } |
| 45 | |
developer | 2076225 | 2021-05-13 16:38:03 +0800 | [diff] [blame] | 46 | static 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 | |
| 56 | static 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 | |
| 65 | 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] | 66 | { |
developer | 0b2f4bb | 2022-11-17 23:23:28 +0800 | [diff] [blame] | 67 | static int sd = -1; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 68 | |
developer | 0b2f4bb | 2022-11-17 23:23:28 +0800 | [diff] [blame] | 69 | struct ifreq ifr; |
| 70 | struct mii_ioctl_data mii; |
developer | 2076225 | 2021-05-13 16:38:03 +0800 | [diff] [blame] | 71 | struct mtk_mii_ioctl_data mtk_mii; |
developer | 0b2f4bb | 2022-11-17 23:23:28 +0800 | [diff] [blame] | 72 | int err; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 73 | |
developer | 0b2f4bb | 2022-11-17 23:23:28 +0800 | [diff] [blame] | 74 | if (sd < 0) |
| 75 | sd = socket(AF_INET, SOCK_DGRAM, 0); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 76 | |
developer | 0b2f4bb | 2022-11-17 23:23:28 +0800 | [diff] [blame] | 77 | if (sd < 0) |
| 78 | return sd; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 79 | |
developer | 1827bda | 2021-12-16 12:31:20 +0800 | [diff] [blame] | 80 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1); |
| 81 | ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0'; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 82 | |
developer | 2076225 | 2021-05-13 16:38:03 +0800 | [diff] [blame] | 83 | 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 | } |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 90 | |
developer | 0b2f4bb | 2022-11-17 23:23:28 +0800 | [diff] [blame] | 91 | err = ioctl(sd, cmd, &ifr); |
| 92 | if (err) |
| 93 | return -errno; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 94 | |
developer | 2076225 | 2021-05-13 16:38:03 +0800 | [diff] [blame] | 95 | 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 | |
developer | 0b2f4bb | 2022-11-17 23:23:28 +0800 | [diff] [blame] | 101 | return 0; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | int main(int argc, char *argv[]) |
| 105 | { |
| 106 | int opt; |
developer | 2076225 | 2021-05-13 16:38:03 +0800 | [diff] [blame] | 107 | char options[] = "gsui:p:d:r:v:?t"; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 108 | int is_write = 0,is_cl45 = 0; |
developer | 2076225 | 2021-05-13 16:38:03 +0800 | [diff] [blame] | 109 | int is_priv = 1; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 110 | unsigned int port=0, dev=0,reg_num=0,val=0; |
developer | 0b2f4bb | 2022-11-17 23:23:28 +0800 | [diff] [blame] | 111 | char ifname[IFNAMSIZ] = "eth0"; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 112 | uint16_t phy_id=0; |
developer | 2076225 | 2021-05-13 16:38:03 +0800 | [diff] [blame] | 113 | uint16_t cmd; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 114 | |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 115 | 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; |
developer | 2076225 | 2021-05-13 16:38:03 +0800 | [diff] [blame] | 128 | case 'u': |
| 129 | is_priv = 0; |
| 130 | break; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 131 | case 'i': |
developer | 1827bda | 2021-12-16 12:31:20 +0800 | [diff] [blame] | 132 | strncpy(ifname, optarg, 5); |
| 133 | ifname[IFNAMSIZ - 1] = '\0'; |
developer | 0b2f4bb | 2022-11-17 23:23:28 +0800 | [diff] [blame] | 134 | break; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 135 | case 'p': |
| 136 | port = strtoul(optarg, NULL, 16); |
developer | fc35675 | 2022-04-24 21:37:49 +0800 | [diff] [blame] | 137 | if (port > INT_MAX) |
| 138 | return -EINVAL; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 139 | break; |
developer | 0b2f4bb | 2022-11-17 23:23:28 +0800 | [diff] [blame] | 140 | case 'd': |
| 141 | dev = strtoul(optarg, NULL, 16); |
developer | fc35675 | 2022-04-24 21:37:49 +0800 | [diff] [blame] | 142 | if (dev > INT_MAX) |
| 143 | return -EINVAL; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 144 | is_cl45 = 1; |
| 145 | break; |
| 146 | case 'r': |
| 147 | reg_num = strtoul(optarg, NULL, 16); |
developer | fc35675 | 2022-04-24 21:37:49 +0800 | [diff] [blame] | 148 | if (reg_num > INT_MAX) |
| 149 | return -EINVAL; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 150 | break; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 151 | case 'v': |
| 152 | val = strtoul(optarg, NULL, 16); |
developer | fc35675 | 2022-04-24 21:37:49 +0800 | [diff] [blame] | 153 | if (val > INT_MAX) |
| 154 | return -EINVAL; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 155 | break; |
developer | 0b2f4bb | 2022-11-17 23:23:28 +0800 | [diff] [blame] | 156 | case 'z': |
| 157 | is_silent = true; |
| 158 | break; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 159 | 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 | |
developer | 0b2f4bb | 2022-11-17 23:23:28 +0800 | [diff] [blame] | 170 | if (is_write) { |
developer | 2076225 | 2021-05-13 16:38:03 +0800 | [diff] [blame] | 171 | 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); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 178 | |
| 179 | if(is_cl45) |
developer | 0b2f4bb | 2022-11-17 23:23:28 +0800 | [diff] [blame] | 180 | INFO("Set: port%x dev%Xh_reg%Xh = 0x%04X\n", |
| 181 | port, dev, reg_num, val); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 182 | else |
developer | 0b2f4bb | 2022-11-17 23:23:28 +0800 | [diff] [blame] | 183 | INFO("Set: phy[%x].reg[%x] = %04x\n", |
| 184 | port, reg_num, val); |
| 185 | } |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 186 | else { |
developer | 2076225 | 2021-05-13 16:38:03 +0800 | [diff] [blame] | 187 | 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); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 194 | |
| 195 | if(is_cl45) |
developer | 0b2f4bb | 2022-11-17 23:23:28 +0800 | [diff] [blame] | 196 | INFO("Get: port%x dev%Xh_reg%Xh = 0x%04X\n", |
| 197 | port, dev, reg_num, val); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 198 | else |
developer | 0b2f4bb | 2022-11-17 23:23:28 +0800 | [diff] [blame] | 199 | INFO("Get: phy[%x].reg[%x] = %04x\n", |
| 200 | port, reg_num, val); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 201 | } |
| 202 | |
developer | 0b2f4bb | 2022-11-17 23:23:28 +0800 | [diff] [blame] | 203 | return 0; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 204 | } |