1 // SPDX-License-Identifier: GPL-2.0+
4 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
16 typedef struct _MII_field_desc_t {
23 static const MII_field_desc_t reg_0_desc_tbl[] = {
24 { 15, 15, 0x01, "reset" },
25 { 14, 14, 0x01, "loopback" },
26 { 13, 6, 0x81, "speed selection" }, /* special */
27 { 12, 12, 0x01, "A/N enable" },
28 { 11, 11, 0x01, "power-down" },
29 { 10, 10, 0x01, "isolate" },
30 { 9, 9, 0x01, "restart A/N" },
31 { 8, 8, 0x01, "duplex" }, /* special */
32 { 7, 7, 0x01, "collision test enable" },
33 { 5, 0, 0x3f, "(reserved)" }
36 static const MII_field_desc_t reg_1_desc_tbl[] = {
37 { 15, 15, 0x01, "100BASE-T4 able" },
38 { 14, 14, 0x01, "100BASE-X full duplex able" },
39 { 13, 13, 0x01, "100BASE-X half duplex able" },
40 { 12, 12, 0x01, "10 Mbps full duplex able" },
41 { 11, 11, 0x01, "10 Mbps half duplex able" },
42 { 10, 10, 0x01, "100BASE-T2 full duplex able" },
43 { 9, 9, 0x01, "100BASE-T2 half duplex able" },
44 { 8, 8, 0x01, "extended status" },
45 { 7, 7, 0x01, "(reserved)" },
46 { 6, 6, 0x01, "MF preamble suppression" },
47 { 5, 5, 0x01, "A/N complete" },
48 { 4, 4, 0x01, "remote fault" },
49 { 3, 3, 0x01, "A/N able" },
50 { 2, 2, 0x01, "link status" },
51 { 1, 1, 0x01, "jabber detect" },
52 { 0, 0, 0x01, "extended capabilities" },
55 static const MII_field_desc_t reg_2_desc_tbl[] = {
56 { 15, 0, 0xffff, "OUI portion" },
59 static const MII_field_desc_t reg_3_desc_tbl[] = {
60 { 15, 10, 0x3f, "OUI portion" },
61 { 9, 4, 0x3f, "manufacturer part number" },
62 { 3, 0, 0x0f, "manufacturer rev. number" },
65 static const MII_field_desc_t reg_4_desc_tbl[] = {
66 { 15, 15, 0x01, "next page able" },
67 { 14, 14, 0x01, "(reserved)" },
68 { 13, 13, 0x01, "remote fault" },
69 { 12, 12, 0x01, "(reserved)" },
70 { 11, 11, 0x01, "asymmetric pause" },
71 { 10, 10, 0x01, "pause enable" },
72 { 9, 9, 0x01, "100BASE-T4 able" },
73 { 8, 8, 0x01, "100BASE-TX full duplex able" },
74 { 7, 7, 0x01, "100BASE-TX able" },
75 { 6, 6, 0x01, "10BASE-T full duplex able" },
76 { 5, 5, 0x01, "10BASE-T able" },
77 { 4, 0, 0x1f, "selector" },
80 static const MII_field_desc_t reg_5_desc_tbl[] = {
81 { 15, 15, 0x01, "next page able" },
82 { 14, 14, 0x01, "acknowledge" },
83 { 13, 13, 0x01, "remote fault" },
84 { 12, 12, 0x01, "(reserved)" },
85 { 11, 11, 0x01, "asymmetric pause able" },
86 { 10, 10, 0x01, "pause able" },
87 { 9, 9, 0x01, "100BASE-T4 able" },
88 { 8, 8, 0x01, "100BASE-X full duplex able" },
89 { 7, 7, 0x01, "100BASE-TX able" },
90 { 6, 6, 0x01, "10BASE-T full duplex able" },
91 { 5, 5, 0x01, "10BASE-T able" },
92 { 4, 0, 0x1f, "partner selector" },
95 static const MII_field_desc_t reg_9_desc_tbl[] = {
96 { 15, 13, 0x07, "test mode" },
97 { 12, 12, 0x01, "manual master/slave enable" },
98 { 11, 11, 0x01, "manual master/slave value" },
99 { 10, 10, 0x01, "multi/single port" },
100 { 9, 9, 0x01, "1000BASE-T full duplex able" },
101 { 8, 8, 0x01, "1000BASE-T half duplex able" },
102 { 7, 7, 0x01, "automatic TDR on link down" },
103 { 6, 6, 0x7f, "(reserved)" },
106 static const MII_field_desc_t reg_10_desc_tbl[] = {
107 { 15, 15, 0x01, "master/slave config fault" },
108 { 14, 14, 0x01, "master/slave config result" },
109 { 13, 13, 0x01, "local receiver status OK" },
110 { 12, 12, 0x01, "remote receiver status OK" },
111 { 11, 11, 0x01, "1000BASE-T full duplex able" },
112 { 10, 10, 0x01, "1000BASE-T half duplex able" },
113 { 9, 8, 0x03, "(reserved)" },
114 { 7, 0, 0xff, "1000BASE-T idle error counter"},
117 typedef struct _MII_reg_desc_t {
119 const MII_field_desc_t *pdesc;
124 static const MII_reg_desc_t mii_reg_desc_tbl[] = {
125 { MII_BMCR, reg_0_desc_tbl, ARRAY_SIZE(reg_0_desc_tbl),
126 "PHY control register" },
127 { MII_BMSR, reg_1_desc_tbl, ARRAY_SIZE(reg_1_desc_tbl),
128 "PHY status register" },
129 { MII_PHYSID1, reg_2_desc_tbl, ARRAY_SIZE(reg_2_desc_tbl),
130 "PHY ID 1 register" },
131 { MII_PHYSID2, reg_3_desc_tbl, ARRAY_SIZE(reg_3_desc_tbl),
132 "PHY ID 2 register" },
133 { MII_ADVERTISE, reg_4_desc_tbl, ARRAY_SIZE(reg_4_desc_tbl),
134 "Autonegotiation advertisement register" },
135 { MII_LPA, reg_5_desc_tbl, ARRAY_SIZE(reg_5_desc_tbl),
136 "Autonegotiation partner abilities register" },
137 { MII_CTRL1000, reg_9_desc_tbl, ARRAY_SIZE(reg_9_desc_tbl),
138 "1000BASE-T control register" },
139 { MII_STAT1000, reg_10_desc_tbl, ARRAY_SIZE(reg_10_desc_tbl),
140 "1000BASE-T status register" },
143 static void dump_reg(
145 const MII_reg_desc_t *prd);
147 static bool special_field(ushort regno, const MII_field_desc_t *pdesc,
150 static void MII_dump(const ushort *regvals, uchar reglo, uchar reghi)
154 for (i = 0; i < ARRAY_SIZE(mii_reg_desc_tbl); i++) {
155 const uchar reg = mii_reg_desc_tbl[i].regno;
157 if (reg >= reglo && reg <= reghi)
158 dump_reg(regvals[reg - reglo], &mii_reg_desc_tbl[i]);
162 /* Print out field position, value, name */
163 static void dump_field(const MII_field_desc_t *pdesc, ushort regval)
165 if (pdesc->hi == pdesc->lo)
166 printf("%2u ", pdesc->lo);
168 printf("%2u-%2u", pdesc->hi, pdesc->lo);
170 printf(" = %5u %s", (regval >> pdesc->lo) & pdesc->mask,
174 static void dump_reg(
176 const MII_reg_desc_t *prd)
179 ushort mask_in_place;
180 const MII_field_desc_t *pdesc;
182 printf("%u. (%04hx) -- %s --\n",
183 prd->regno, regval, prd->name);
185 for (i = 0; i < prd->len; i++) {
186 pdesc = &prd->pdesc[i];
188 mask_in_place = pdesc->mask << pdesc->lo;
190 printf(" (%04hx:%04x) %u.",
192 regval & mask_in_place,
195 if (!special_field(prd->regno, pdesc, regval))
196 dump_field(pdesc, regval);
212 static bool special_field(ushort regno, const MII_field_desc_t *pdesc,
215 const ushort sel_bits = (regval >> pdesc->lo) & pdesc->mask;
217 if ((regno == MII_BMCR) && (pdesc->lo == 6)) {
218 ushort speed_bits = regval & (BMCR_SPEED1000 | BMCR_SPEED100);
219 printf("%2u,%2u = b%u%u speed selection = %s Mbps",
223 speed_bits == BMCR_SPEED1000 ? "1000" :
224 speed_bits == BMCR_SPEED100 ? "100" :
229 else if ((regno == MII_BMCR) && (pdesc->lo == 8)) {
230 dump_field(pdesc, regval);
231 printf(" = %s", ((regval >> pdesc->lo) & 1) ? "full" : "half");
235 else if ((regno == MII_ADVERTISE) && (pdesc->lo == 0)) {
236 dump_field(pdesc, regval);
238 sel_bits == PHY_ANLPAR_PSB_802_3 ? "IEEE 802.3 CSMA/CD" :
239 sel_bits == PHY_ANLPAR_PSB_802_9 ?
240 "IEEE 802.9 ISLAN-16T" : "???");
244 else if ((regno == MII_LPA) && (pdesc->lo == 0)) {
245 dump_field(pdesc, regval);
247 sel_bits == PHY_ANLPAR_PSB_802_3 ? "IEEE 802.3 CSMA/CD" :
248 sel_bits == PHY_ANLPAR_PSB_802_9 ?
249 "IEEE 802.9 ISLAN-16T" : "???");
256 static char last_op[2];
257 static uint last_data;
258 static uint last_addr_lo;
259 static uint last_addr_hi;
260 static uint last_reg_lo;
261 static uint last_reg_hi;
262 static uint last_mask;
264 static void extract_range(
270 *plo = simple_strtoul(input, &end, 16);
273 *phi = simple_strtoul(end, NULL, 16);
280 /* ---------------------------------------------------------------- */
281 static int do_mii(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
284 unsigned char addrlo, addrhi, reglo, reghi;
285 unsigned char addr, reg;
286 unsigned short data, mask;
291 return CMD_RET_USAGE;
293 #if defined(CONFIG_MII_INIT)
298 * We use the last specified parameters, unless new ones are
303 addrlo = last_addr_lo;
304 addrhi = last_addr_hi;
310 if ((flag & CMD_FLAG_REPEAT) == 0) {
312 if (strlen(argv[1]) > 1)
318 extract_range(argv[2], &addrlo, &addrhi);
320 extract_range(argv[3], ®lo, ®hi);
322 data = simple_strtoul(argv[4], NULL, 16);
324 mask = simple_strtoul(argv[5], NULL, 16);
327 if (addrhi > 31 && strncmp(op, "de", 2)) {
328 printf("Incorrect PHY address. Range should be 0-31\n");
329 return CMD_RET_USAGE;
332 /* use current device */
333 devname = miiphy_get_current_dev();
336 * check info/read/write.
339 unsigned char j, start, end;
345 * Look for any and all PHYs. Valid addresses are 0..31.
348 start = addrlo; end = addrhi;
353 for (j = start; j <= end; j++) {
354 if (miiphy_info (devname, j, &oui, &model, &rev) == 0) {
355 printf("PHY 0x%02X: "
361 miiphy_speed (devname, j),
362 miiphy_is_1000base_x (devname, j)
364 (miiphy_duplex (devname, j) == FULL)
368 } else if (op[0] == 'r') {
369 for (addr = addrlo; addr <= addrhi; addr++) {
370 for (reg = reglo; reg <= reghi; reg++) {
372 if (miiphy_read (devname, addr, reg, &data) != 0) {
374 "Error reading from the PHY addr=%02x reg=%02x\n",
378 if ((addrlo != addrhi) || (reglo != reghi))
379 printf("addr=%02x reg=%02x data=",
380 (uint)addr, (uint)reg);
381 printf("%04X\n", data & 0x0000FFFF);
384 if ((addrlo != addrhi) && (reglo != reghi))
387 } else if (op[0] == 'w') {
388 for (addr = addrlo; addr <= addrhi; addr++) {
389 for (reg = reglo; reg <= reghi; reg++) {
390 if (miiphy_write (devname, addr, reg, data) != 0) {
391 printf("Error writing to the PHY addr=%02x reg=%02x\n",
397 } else if (op[0] == 'm') {
398 for (addr = addrlo; addr <= addrhi; addr++) {
399 for (reg = reglo; reg <= reghi; reg++) {
400 unsigned short val = 0;
401 if (miiphy_read(devname, addr,
403 printf("Error reading from the PHY");
404 printf(" addr=%02x", addr);
405 printf(" reg=%02x\n", reg);
408 val = (val & ~mask) | (data & mask);
409 if (miiphy_write(devname, addr,
411 printf("Error writing to the PHY");
412 printf(" addr=%02x", addr);
413 printf(" reg=%02x\n", reg);
419 } else if (strncmp(op, "du", 2) == 0) {
420 ushort regs[MII_STAT1000 + 1]; /* Last reg is 0x0a */
422 if (reglo > MII_STAT1000 || reghi > MII_STAT1000) {
423 printf("The MII dump command only formats the standard MII registers, 0-5, 9-a.\n");
426 for (addr = addrlo; addr <= addrhi; addr++) {
427 for (reg = reglo; reg <= reghi; reg++) {
428 if (miiphy_read(devname, addr, reg,
429 ®s[reg - reglo]) != 0) {
432 "Error reading from the PHY addr=%02x reg=%02x\n",
438 MII_dump(regs, reglo, reghi);
441 } else if (strncmp(op, "de", 2) == 0) {
445 miiphy_set_current_dev (argv[2]);
447 return CMD_RET_USAGE;
451 * Save the parameters for repeats.
455 last_addr_lo = addrlo;
456 last_addr_hi = addrhi;
465 /***************************************************/
469 "MII utility commands",
470 "device - list available devices\n"
471 "mii device <devname> - set current device\n"
472 "mii info <addr> - display MII PHY info\n"
473 "mii read <addr> <reg> - read MII PHY <addr> register <reg>\n"
474 "mii write <addr> <reg> <data> - write MII PHY <addr> register <reg>\n"
475 "mii modify <addr> <reg> <data> <mask> - modify MII PHY <addr> register <reg>\n"
476 " updating bits identified in <mask>\n"
477 "mii dump <addr> <reg> - pretty-print <addr> <reg> (0-5 only)\n"
478 "Addr and/or reg may be ranges, e.g. 2-7."