cmd_mii.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. { 0, "PHY control register" },
  35. { 1, "PHY status register" },
  36. { 2, "PHY ID 1 register" },
  37. { 3, "PHY ID 2 register" },
  38. { 4, "Autonegotiation advertisement register" },
  39. { 5, "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 == 0) && (pdesc->lo == 6)) {
  191. ushort speed_bits = regval & PHY_BMCR_SPEED_MASK;
  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 == PHY_BMCR_1000_MBPS ? "1000" :
  197. speed_bits == PHY_BMCR_100_MBPS ? "100" :
  198. speed_bits == PHY_BMCR_10_MBPS ? "10" :
  199. "???");
  200. return 1;
  201. }
  202. else if ((regno == 0) && (pdesc->lo == 8)) {
  203. printf("%2u = %5u duplex = %s",
  204. pdesc->lo,
  205. (regval >> pdesc->lo) & 1,
  206. ((regval >> pdesc->lo) & 1) ? "full" : "half");
  207. return 1;
  208. }
  209. else if ((regno == 4) && (pdesc->lo == 0)) {
  210. ushort sel_bits = (regval >> pdesc->lo) & pdesc->mask;
  211. printf("%2u-%2u = %5u selector = %s",
  212. pdesc->hi, pdesc->lo, sel_bits,
  213. sel_bits == PHY_ANLPAR_PSB_802_3 ?
  214. "IEEE 802.3" :
  215. sel_bits == PHY_ANLPAR_PSB_802_9 ?
  216. "IEEE 802.9 ISLAN-16T" :
  217. "???");
  218. return 1;
  219. }
  220. else if ((regno == 5) && (pdesc->lo == 0)) {
  221. ushort sel_bits = (regval >> pdesc->lo) & pdesc->mask;
  222. printf("%2u-%2u = %u selector = %s",
  223. pdesc->hi, pdesc->lo, sel_bits,
  224. sel_bits == PHY_ANLPAR_PSB_802_3 ?
  225. "IEEE 802.3" :
  226. sel_bits == PHY_ANLPAR_PSB_802_9 ?
  227. "IEEE 802.9 ISLAN-16T" :
  228. "???");
  229. return 1;
  230. }
  231. return 0;
  232. }
  233. static char last_op[2];
  234. static uint last_data;
  235. static uint last_addr_lo;
  236. static uint last_addr_hi;
  237. static uint last_reg_lo;
  238. static uint last_reg_hi;
  239. static void extract_range(
  240. char * input,
  241. unsigned char * plo,
  242. unsigned char * phi)
  243. {
  244. char * end;
  245. *plo = simple_strtoul(input, &end, 16);
  246. if (*end == '-') {
  247. end++;
  248. *phi = simple_strtoul(end, NULL, 16);
  249. }
  250. else {
  251. *phi = *plo;
  252. }
  253. }
  254. /* ---------------------------------------------------------------- */
  255. static int do_mii(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  256. {
  257. char op[2];
  258. unsigned char addrlo, addrhi, reglo, reghi;
  259. unsigned char addr, reg;
  260. unsigned short data;
  261. int rcode = 0;
  262. const char *devname;
  263. if (argc < 2)
  264. return cmd_usage(cmdtp);
  265. #if defined(CONFIG_MII_INIT)
  266. mii_init ();
  267. #endif
  268. /*
  269. * We use the last specified parameters, unless new ones are
  270. * entered.
  271. */
  272. op[0] = last_op[0];
  273. op[1] = last_op[1];
  274. addrlo = last_addr_lo;
  275. addrhi = last_addr_hi;
  276. reglo = last_reg_lo;
  277. reghi = last_reg_hi;
  278. data = last_data;
  279. if ((flag & CMD_FLAG_REPEAT) == 0) {
  280. op[0] = argv[1][0];
  281. if (strlen(argv[1]) > 1)
  282. op[1] = argv[1][1];
  283. else
  284. op[1] = '\0';
  285. if (argc >= 3)
  286. extract_range(argv[2], &addrlo, &addrhi);
  287. if (argc >= 4)
  288. extract_range(argv[3], &reglo, &reghi);
  289. if (argc >= 5)
  290. data = simple_strtoul (argv[4], NULL, 16);
  291. }
  292. /* use current device */
  293. devname = miiphy_get_current_dev();
  294. /*
  295. * check info/read/write.
  296. */
  297. if (op[0] == 'i') {
  298. unsigned char j, start, end;
  299. unsigned int oui;
  300. unsigned char model;
  301. unsigned char rev;
  302. /*
  303. * Look for any and all PHYs. Valid addresses are 0..31.
  304. */
  305. if (argc >= 3) {
  306. start = addrlo; end = addrhi;
  307. } else {
  308. start = 0; end = 31;
  309. }
  310. for (j = start; j <= end; j++) {
  311. if (miiphy_info (devname, j, &oui, &model, &rev) == 0) {
  312. printf("PHY 0x%02X: "
  313. "OUI = 0x%04X, "
  314. "Model = 0x%02X, "
  315. "Rev = 0x%02X, "
  316. "%3dbase%s, %s\n",
  317. j, oui, model, rev,
  318. miiphy_speed (devname, j),
  319. miiphy_is_1000base_x (devname, j)
  320. ? "X" : "T",
  321. (miiphy_duplex (devname, j) == FULL)
  322. ? "FDX" : "HDX");
  323. }
  324. }
  325. } else if (op[0] == 'r') {
  326. for (addr = addrlo; addr <= addrhi; addr++) {
  327. for (reg = reglo; reg <= reghi; reg++) {
  328. data = 0xffff;
  329. if (miiphy_read (devname, addr, reg, &data) != 0) {
  330. printf(
  331. "Error reading from the PHY addr=%02x reg=%02x\n",
  332. addr, reg);
  333. rcode = 1;
  334. } else {
  335. if ((addrlo != addrhi) || (reglo != reghi))
  336. printf("addr=%02x reg=%02x data=",
  337. (uint)addr, (uint)reg);
  338. printf("%04X\n", data & 0x0000FFFF);
  339. }
  340. }
  341. if ((addrlo != addrhi) && (reglo != reghi))
  342. printf("\n");
  343. }
  344. } else if (op[0] == 'w') {
  345. for (addr = addrlo; addr <= addrhi; addr++) {
  346. for (reg = reglo; reg <= reghi; reg++) {
  347. if (miiphy_write (devname, addr, reg, data) != 0) {
  348. printf("Error writing to the PHY addr=%02x reg=%02x\n",
  349. addr, reg);
  350. rcode = 1;
  351. }
  352. }
  353. }
  354. } else if (strncmp(op, "du", 2) == 0) {
  355. ushort regs[6];
  356. int ok = 1;
  357. if ((reglo > 5) || (reghi > 5)) {
  358. printf(
  359. "The MII dump command only formats the "
  360. "standard MII registers, 0-5.\n");
  361. return 1;
  362. }
  363. for (addr = addrlo; addr <= addrhi; addr++) {
  364. for (reg = reglo; reg < reghi + 1; reg++) {
  365. if (miiphy_read(devname, addr, reg, &regs[reg]) != 0) {
  366. ok = 0;
  367. printf(
  368. "Error reading from the PHY addr=%02x reg=%02x\n",
  369. addr, reg);
  370. rcode = 1;
  371. }
  372. }
  373. if (ok)
  374. MII_dump_0_to_5(regs, reglo, reghi);
  375. printf("\n");
  376. }
  377. } else if (strncmp(op, "de", 2) == 0) {
  378. if (argc == 2)
  379. miiphy_listdev ();
  380. else
  381. miiphy_set_current_dev (argv[2]);
  382. } else {
  383. return cmd_usage(cmdtp);
  384. }
  385. /*
  386. * Save the parameters for repeats.
  387. */
  388. last_op[0] = op[0];
  389. last_op[1] = op[1];
  390. last_addr_lo = addrlo;
  391. last_addr_hi = addrhi;
  392. last_reg_lo = reglo;
  393. last_reg_hi = reghi;
  394. last_data = data;
  395. return rcode;
  396. }
  397. /***************************************************/
  398. U_BOOT_CMD(
  399. mii, 5, 1, do_mii,
  400. "MII utility commands",
  401. "device - list available devices\n"
  402. "mii device <devname> - set current device\n"
  403. "mii info <addr> - display MII PHY info\n"
  404. "mii read <addr> <reg> - read MII PHY <addr> register <reg>\n"
  405. "mii write <addr> <reg> <data> - write MII PHY <addr> register <reg>\n"
  406. "mii dump <addr> <reg> - pretty-print <addr> <reg> (0-5 only)\n"
  407. "Addr and/or reg may be ranges, e.g. 2-7."
  408. );