cmd_mii.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. * (C) Copyright 2001
  3. * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * MII Utilities
  25. */
  26. #include <common.h>
  27. #include <command.h>
  28. #include <miiphy.h>
  29. typedef struct _MII_reg_desc_t {
  30. ushort regno;
  31. char * name;
  32. } MII_reg_desc_t;
  33. static const MII_reg_desc_t reg_0_5_desc_tbl[] = {
  34. { MII_BMCR, "PHY control register" },
  35. { MII_BMSR, "PHY status register" },
  36. { MII_PHYSID1, "PHY ID 1 register" },
  37. { MII_PHYSID2, "PHY ID 2 register" },
  38. { MII_ADVERTISE, "Autonegotiation advertisement register" },
  39. { MII_LPA, "Autonegotiation partner abilities register" },
  40. };
  41. typedef struct _MII_field_desc_t {
  42. ushort hi;
  43. ushort lo;
  44. ushort mask;
  45. char * name;
  46. } MII_field_desc_t;
  47. static const MII_field_desc_t reg_0_desc_tbl[] = {
  48. { 15, 15, 0x01, "reset" },
  49. { 14, 14, 0x01, "loopback" },
  50. { 13, 6, 0x81, "speed selection" }, /* special */
  51. { 12, 12, 0x01, "A/N enable" },
  52. { 11, 11, 0x01, "power-down" },
  53. { 10, 10, 0x01, "isolate" },
  54. { 9, 9, 0x01, "restart A/N" },
  55. { 8, 8, 0x01, "duplex" }, /* special */
  56. { 7, 7, 0x01, "collision test enable" },
  57. { 5, 0, 0x3f, "(reserved)" }
  58. };
  59. static const MII_field_desc_t reg_1_desc_tbl[] = {
  60. { 15, 15, 0x01, "100BASE-T4 able" },
  61. { 14, 14, 0x01, "100BASE-X full duplex able" },
  62. { 13, 13, 0x01, "100BASE-X half duplex able" },
  63. { 12, 12, 0x01, "10 Mbps full duplex able" },
  64. { 11, 11, 0x01, "10 Mbps half duplex able" },
  65. { 10, 10, 0x01, "100BASE-T2 full duplex able" },
  66. { 9, 9, 0x01, "100BASE-T2 half duplex able" },
  67. { 8, 8, 0x01, "extended status" },
  68. { 7, 7, 0x01, "(reserved)" },
  69. { 6, 6, 0x01, "MF preamble suppression" },
  70. { 5, 5, 0x01, "A/N complete" },
  71. { 4, 4, 0x01, "remote fault" },
  72. { 3, 3, 0x01, "A/N able" },
  73. { 2, 2, 0x01, "link status" },
  74. { 1, 1, 0x01, "jabber detect" },
  75. { 0, 0, 0x01, "extended capabilities" },
  76. };
  77. static const MII_field_desc_t reg_2_desc_tbl[] = {
  78. { 15, 0, 0xffff, "OUI portion" },
  79. };
  80. static const MII_field_desc_t reg_3_desc_tbl[] = {
  81. { 15, 10, 0x3f, "OUI portion" },
  82. { 9, 4, 0x3f, "manufacturer part number" },
  83. { 3, 0, 0x0f, "manufacturer rev. number" },
  84. };
  85. static const MII_field_desc_t reg_4_desc_tbl[] = {
  86. { 15, 15, 0x01, "next page able" },
  87. { 14, 14, 0x01, "reserved" },
  88. { 13, 13, 0x01, "remote fault" },
  89. { 12, 12, 0x01, "reserved" },
  90. { 11, 11, 0x01, "asymmetric pause" },
  91. { 10, 10, 0x01, "pause enable" },
  92. { 9, 9, 0x01, "100BASE-T4 able" },
  93. { 8, 8, 0x01, "100BASE-TX full duplex able" },
  94. { 7, 7, 0x01, "100BASE-TX able" },
  95. { 6, 6, 0x01, "10BASE-T full duplex able" },
  96. { 5, 5, 0x01, "10BASE-T able" },
  97. { 4, 0, 0x1f, "xxx to do" },
  98. };
  99. static const MII_field_desc_t reg_5_desc_tbl[] = {
  100. { 15, 15, 0x01, "next page able" },
  101. { 14, 14, 0x01, "acknowledge" },
  102. { 13, 13, 0x01, "remote fault" },
  103. { 12, 12, 0x01, "(reserved)" },
  104. { 11, 11, 0x01, "asymmetric pause able" },
  105. { 10, 10, 0x01, "pause able" },
  106. { 9, 9, 0x01, "100BASE-T4 able" },
  107. { 8, 8, 0x01, "100BASE-X full duplex able" },
  108. { 7, 7, 0x01, "100BASE-TX able" },
  109. { 6, 6, 0x01, "10BASE-T full duplex able" },
  110. { 5, 5, 0x01, "10BASE-T able" },
  111. { 4, 0, 0x1f, "xxx to do" },
  112. };
  113. typedef struct _MII_field_desc_and_len_t {
  114. const MII_field_desc_t *pdesc;
  115. ushort len;
  116. } MII_field_desc_and_len_t;
  117. static const MII_field_desc_and_len_t desc_and_len_tbl[] = {
  118. { reg_0_desc_tbl, ARRAY_SIZE(reg_0_desc_tbl) },
  119. { reg_1_desc_tbl, ARRAY_SIZE(reg_1_desc_tbl) },
  120. { reg_2_desc_tbl, ARRAY_SIZE(reg_2_desc_tbl) },
  121. { reg_3_desc_tbl, ARRAY_SIZE(reg_3_desc_tbl) },
  122. { reg_4_desc_tbl, ARRAY_SIZE(reg_4_desc_tbl) },
  123. { reg_5_desc_tbl, ARRAY_SIZE(reg_5_desc_tbl) },
  124. };
  125. static void dump_reg(
  126. ushort regval,
  127. const MII_reg_desc_t *prd,
  128. const MII_field_desc_and_len_t *pdl);
  129. static int special_field(
  130. ushort regno,
  131. const MII_field_desc_t *pdesc,
  132. ushort regval);
  133. static void MII_dump_0_to_5(
  134. ushort regvals[6],
  135. uchar reglo,
  136. uchar reghi)
  137. {
  138. ulong i;
  139. for (i = 0; i < 6; i++) {
  140. if ((reglo <= i) && (i <= reghi))
  141. dump_reg(regvals[i], &reg_0_5_desc_tbl[i],
  142. &desc_and_len_tbl[i]);
  143. }
  144. }
  145. static void dump_reg(
  146. ushort regval,
  147. const MII_reg_desc_t *prd,
  148. const MII_field_desc_and_len_t *pdl)
  149. {
  150. ulong i;
  151. ushort mask_in_place;
  152. const MII_field_desc_t *pdesc;
  153. printf("%u. (%04hx) -- %s --\n",
  154. prd->regno, regval, prd->name);
  155. for (i = 0; i < pdl->len; i++) {
  156. pdesc = &pdl->pdesc[i];
  157. mask_in_place = pdesc->mask << pdesc->lo;
  158. printf(" (%04hx:%04hx) %u.",
  159. mask_in_place,
  160. regval & mask_in_place,
  161. prd->regno);
  162. if (special_field(prd->regno, pdesc, regval)) {
  163. }
  164. else {
  165. if (pdesc->hi == pdesc->lo)
  166. printf("%2u ", pdesc->lo);
  167. else
  168. printf("%2u-%2u", pdesc->hi, pdesc->lo);
  169. printf(" = %5u %s",
  170. (regval & mask_in_place) >> pdesc->lo,
  171. pdesc->name);
  172. }
  173. printf("\n");
  174. }
  175. printf("\n");
  176. }
  177. /* Special fields:
  178. ** 0.6,13
  179. ** 0.8
  180. ** 2.15-0
  181. ** 3.15-0
  182. ** 4.4-0
  183. ** 5.4-0
  184. */
  185. static int special_field(
  186. ushort regno,
  187. const MII_field_desc_t *pdesc,
  188. ushort regval)
  189. {
  190. if ((regno == MII_BMCR) && (pdesc->lo == 6)) {
  191. ushort speed_bits = regval & (BMCR_SPEED1000 | BMCR_SPEED100);
  192. printf("%2u,%2u = b%u%u speed selection = %s Mbps",
  193. 6, 13,
  194. (regval >> 6) & 1,
  195. (regval >> 13) & 1,
  196. speed_bits == BMCR_SPEED1000 ? "1000" :
  197. speed_bits == BMCR_SPEED100 ? "100" :
  198. "10");
  199. return 1;
  200. }
  201. else if ((regno == MII_BMCR) && (pdesc->lo == 8)) {
  202. printf("%2u = %5u duplex = %s",
  203. pdesc->lo,
  204. (regval >> pdesc->lo) & 1,
  205. ((regval >> pdesc->lo) & 1) ? "full" : "half");
  206. return 1;
  207. }
  208. else if ((regno == MII_ADVERTISE) && (pdesc->lo == 0)) {
  209. ushort sel_bits = (regval >> pdesc->lo) & pdesc->mask;
  210. printf("%2u-%2u = %5u selector = %s",
  211. pdesc->hi, pdesc->lo, sel_bits,
  212. sel_bits == PHY_ANLPAR_PSB_802_3 ?
  213. "IEEE 802.3" :
  214. sel_bits == PHY_ANLPAR_PSB_802_9 ?
  215. "IEEE 802.9 ISLAN-16T" :
  216. "???");
  217. return 1;
  218. }
  219. else if ((regno == MII_LPA) && (pdesc->lo == 0)) {
  220. ushort sel_bits = (regval >> pdesc->lo) & pdesc->mask;
  221. printf("%2u-%2u = %u selector = %s",
  222. pdesc->hi, pdesc->lo, sel_bits,
  223. sel_bits == PHY_ANLPAR_PSB_802_3 ?
  224. "IEEE 802.3" :
  225. sel_bits == PHY_ANLPAR_PSB_802_9 ?
  226. "IEEE 802.9 ISLAN-16T" :
  227. "???");
  228. return 1;
  229. }
  230. return 0;
  231. }
  232. static char last_op[2];
  233. static uint last_data;
  234. static uint last_addr_lo;
  235. static uint last_addr_hi;
  236. static uint last_reg_lo;
  237. static uint last_reg_hi;
  238. static void extract_range(
  239. char * input,
  240. unsigned char * plo,
  241. unsigned char * phi)
  242. {
  243. char * end;
  244. *plo = simple_strtoul(input, &end, 16);
  245. if (*end == '-') {
  246. end++;
  247. *phi = simple_strtoul(end, NULL, 16);
  248. }
  249. else {
  250. *phi = *plo;
  251. }
  252. }
  253. /* ---------------------------------------------------------------- */
  254. static int do_mii(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  255. {
  256. char op[2];
  257. unsigned char addrlo, addrhi, reglo, reghi;
  258. unsigned char addr, reg;
  259. unsigned short data;
  260. int rcode = 0;
  261. const char *devname;
  262. if (argc < 2)
  263. return CMD_RET_USAGE;
  264. #if defined(CONFIG_MII_INIT)
  265. mii_init ();
  266. #endif
  267. /*
  268. * We use the last specified parameters, unless new ones are
  269. * entered.
  270. */
  271. op[0] = last_op[0];
  272. op[1] = last_op[1];
  273. addrlo = last_addr_lo;
  274. addrhi = last_addr_hi;
  275. reglo = last_reg_lo;
  276. reghi = last_reg_hi;
  277. data = last_data;
  278. if ((flag & CMD_FLAG_REPEAT) == 0) {
  279. op[0] = argv[1][0];
  280. if (strlen(argv[1]) > 1)
  281. op[1] = argv[1][1];
  282. else
  283. op[1] = '\0';
  284. if (argc >= 3)
  285. extract_range(argv[2], &addrlo, &addrhi);
  286. if (argc >= 4)
  287. extract_range(argv[3], &reglo, &reghi);
  288. if (argc >= 5)
  289. data = simple_strtoul (argv[4], NULL, 16);
  290. }
  291. /* use current device */
  292. devname = miiphy_get_current_dev();
  293. /*
  294. * check info/read/write.
  295. */
  296. if (op[0] == 'i') {
  297. unsigned char j, start, end;
  298. unsigned int oui;
  299. unsigned char model;
  300. unsigned char rev;
  301. /*
  302. * Look for any and all PHYs. Valid addresses are 0..31.
  303. */
  304. if (argc >= 3) {
  305. start = addrlo; end = addrhi;
  306. } else {
  307. start = 0; end = 31;
  308. }
  309. for (j = start; j <= end; j++) {
  310. if (miiphy_info (devname, j, &oui, &model, &rev) == 0) {
  311. printf("PHY 0x%02X: "
  312. "OUI = 0x%04X, "
  313. "Model = 0x%02X, "
  314. "Rev = 0x%02X, "
  315. "%3dbase%s, %s\n",
  316. j, oui, model, rev,
  317. miiphy_speed (devname, j),
  318. miiphy_is_1000base_x (devname, j)
  319. ? "X" : "T",
  320. (miiphy_duplex (devname, j) == FULL)
  321. ? "FDX" : "HDX");
  322. }
  323. }
  324. } else if (op[0] == 'r') {
  325. for (addr = addrlo; addr <= addrhi; addr++) {
  326. for (reg = reglo; reg <= reghi; reg++) {
  327. data = 0xffff;
  328. if (miiphy_read (devname, addr, reg, &data) != 0) {
  329. printf(
  330. "Error reading from the PHY addr=%02x reg=%02x\n",
  331. addr, reg);
  332. rcode = 1;
  333. } else {
  334. if ((addrlo != addrhi) || (reglo != reghi))
  335. printf("addr=%02x reg=%02x data=",
  336. (uint)addr, (uint)reg);
  337. printf("%04X\n", data & 0x0000FFFF);
  338. }
  339. }
  340. if ((addrlo != addrhi) && (reglo != reghi))
  341. printf("\n");
  342. }
  343. } else if (op[0] == 'w') {
  344. for (addr = addrlo; addr <= addrhi; addr++) {
  345. for (reg = reglo; reg <= reghi; reg++) {
  346. if (miiphy_write (devname, addr, reg, data) != 0) {
  347. printf("Error writing to the PHY addr=%02x reg=%02x\n",
  348. addr, reg);
  349. rcode = 1;
  350. }
  351. }
  352. }
  353. } else if (strncmp(op, "du", 2) == 0) {
  354. ushort regs[6];
  355. int ok = 1;
  356. if ((reglo > 5) || (reghi > 5)) {
  357. printf(
  358. "The MII dump command only formats the "
  359. "standard MII registers, 0-5.\n");
  360. return 1;
  361. }
  362. for (addr = addrlo; addr <= addrhi; addr++) {
  363. for (reg = reglo; reg < reghi + 1; reg++) {
  364. if (miiphy_read(devname, addr, reg, &regs[reg]) != 0) {
  365. ok = 0;
  366. printf(
  367. "Error reading from the PHY addr=%02x reg=%02x\n",
  368. addr, reg);
  369. rcode = 1;
  370. }
  371. }
  372. if (ok)
  373. MII_dump_0_to_5(regs, reglo, reghi);
  374. printf("\n");
  375. }
  376. } else if (strncmp(op, "de", 2) == 0) {
  377. if (argc == 2)
  378. miiphy_listdev ();
  379. else
  380. miiphy_set_current_dev (argv[2]);
  381. } else {
  382. return CMD_RET_USAGE;
  383. }
  384. /*
  385. * Save the parameters for repeats.
  386. */
  387. last_op[0] = op[0];
  388. last_op[1] = op[1];
  389. last_addr_lo = addrlo;
  390. last_addr_hi = addrhi;
  391. last_reg_lo = reglo;
  392. last_reg_hi = reghi;
  393. last_data = data;
  394. return rcode;
  395. }
  396. /***************************************************/
  397. U_BOOT_CMD(
  398. mii, 5, 1, do_mii,
  399. "MII utility commands",
  400. "device - list available devices\n"
  401. "mii device <devname> - set current device\n"
  402. "mii info <addr> - display MII PHY info\n"
  403. "mii read <addr> <reg> - read MII PHY <addr> register <reg>\n"
  404. "mii write <addr> <reg> <data> - write MII PHY <addr> register <reg>\n"
  405. "mii dump <addr> <reg> - pretty-print <addr> <reg> (0-5 only)\n"
  406. "Addr and/or reg may be ranges, e.g. 2-7."
  407. );