mdio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * mdio.c: Generic support for MDIO-compatible transceivers
  3. * Copyright 2006-2009 Solarflare Communications Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published
  7. * by the Free Software Foundation, incorporated herein by reference.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/capability.h>
  11. #include <linux/errno.h>
  12. #include <linux/ethtool.h>
  13. #include <linux/mdio.h>
  14. #include <linux/module.h>
  15. MODULE_DESCRIPTION("Generic support for MDIO-compatible transceivers");
  16. MODULE_AUTHOR("Copyright 2006-2009 Solarflare Communications Inc.");
  17. MODULE_LICENSE("GPL");
  18. /**
  19. * mdio45_probe - probe for an MDIO (clause 45) device
  20. * @mdio: MDIO interface
  21. * @prtad: Expected PHY address
  22. *
  23. * This sets @prtad and @mmds in the MDIO interface if successful.
  24. * Returns 0 on success, negative on error.
  25. */
  26. int mdio45_probe(struct mdio_if_info *mdio, int prtad)
  27. {
  28. int mmd, stat2, devs1, devs2;
  29. /* Assume PHY must have at least one of PMA/PMD, WIS, PCS, PHY
  30. * XS or DTE XS; give up if none is present. */
  31. for (mmd = 1; mmd <= 5; mmd++) {
  32. /* Is this MMD present? */
  33. stat2 = mdio->mdio_read(mdio->dev, prtad, mmd, MDIO_STAT2);
  34. if (stat2 < 0 ||
  35. (stat2 & MDIO_STAT2_DEVPRST) != MDIO_STAT2_DEVPRST_VAL)
  36. continue;
  37. /* It should tell us about all the other MMDs */
  38. devs1 = mdio->mdio_read(mdio->dev, prtad, mmd, MDIO_DEVS1);
  39. devs2 = mdio->mdio_read(mdio->dev, prtad, mmd, MDIO_DEVS2);
  40. if (devs1 < 0 || devs2 < 0)
  41. continue;
  42. mdio->prtad = prtad;
  43. mdio->mmds = devs1 | (devs2 << 16);
  44. return 0;
  45. }
  46. return -ENODEV;
  47. }
  48. EXPORT_SYMBOL(mdio45_probe);
  49. /**
  50. * mdio_set_flag - set or clear flag in an MDIO register
  51. * @mdio: MDIO interface
  52. * @prtad: PHY address
  53. * @devad: MMD address
  54. * @addr: Register address
  55. * @mask: Mask for flag (single bit set)
  56. * @sense: New value of flag
  57. *
  58. * This debounces changes: it does not write the register if the flag
  59. * already has the proper value. Returns 0 on success, negative on error.
  60. */
  61. int mdio_set_flag(const struct mdio_if_info *mdio,
  62. int prtad, int devad, u16 addr, int mask,
  63. bool sense)
  64. {
  65. int old_val = mdio->mdio_read(mdio->dev, prtad, devad, addr);
  66. int new_val;
  67. if (old_val < 0)
  68. return old_val;
  69. if (sense)
  70. new_val = old_val | mask;
  71. else
  72. new_val = old_val & ~mask;
  73. if (old_val == new_val)
  74. return 0;
  75. return mdio->mdio_write(mdio->dev, prtad, devad, addr, new_val);
  76. }
  77. EXPORT_SYMBOL(mdio_set_flag);
  78. /**
  79. * mdio_link_ok - is link status up/OK
  80. * @mdio: MDIO interface
  81. * @mmd_mask: Mask for MMDs to check
  82. *
  83. * Returns 1 if the PHY reports link status up/OK, 0 otherwise.
  84. * @mmd_mask is normally @mdio->mmds, but if loopback is enabled
  85. * the MMDs being bypassed should be excluded from the mask.
  86. */
  87. int mdio45_links_ok(const struct mdio_if_info *mdio, u32 mmd_mask)
  88. {
  89. int devad, reg;
  90. if (!mmd_mask) {
  91. /* Use absence of XGMII faults in lieu of link state */
  92. reg = mdio->mdio_read(mdio->dev, mdio->prtad,
  93. MDIO_MMD_PHYXS, MDIO_STAT2);
  94. return reg >= 0 && !(reg & MDIO_STAT2_RXFAULT);
  95. }
  96. for (devad = 0; mmd_mask; devad++) {
  97. if (mmd_mask & (1 << devad)) {
  98. mmd_mask &= ~(1 << devad);
  99. /* Reset the latched status and fault flags */
  100. mdio->mdio_read(mdio->dev, mdio->prtad,
  101. devad, MDIO_STAT1);
  102. if (devad == MDIO_MMD_PMAPMD || devad == MDIO_MMD_PCS ||
  103. devad == MDIO_MMD_PHYXS || devad == MDIO_MMD_DTEXS)
  104. mdio->mdio_read(mdio->dev, mdio->prtad,
  105. devad, MDIO_STAT2);
  106. /* Check the current status and fault flags */
  107. reg = mdio->mdio_read(mdio->dev, mdio->prtad,
  108. devad, MDIO_STAT1);
  109. if (reg < 0 ||
  110. (reg & (MDIO_STAT1_FAULT | MDIO_STAT1_LSTATUS)) !=
  111. MDIO_STAT1_LSTATUS)
  112. return false;
  113. }
  114. }
  115. return true;
  116. }
  117. EXPORT_SYMBOL(mdio45_links_ok);
  118. /**
  119. * mdio45_nway_restart - restart auto-negotiation for this interface
  120. * @mdio: MDIO interface
  121. *
  122. * Returns 0 on success, negative on error.
  123. */
  124. int mdio45_nway_restart(const struct mdio_if_info *mdio)
  125. {
  126. if (!(mdio->mmds & MDIO_DEVS_AN))
  127. return -EOPNOTSUPP;
  128. mdio_set_flag(mdio, mdio->prtad, MDIO_MMD_AN, MDIO_CTRL1,
  129. MDIO_AN_CTRL1_RESTART, true);
  130. return 0;
  131. }
  132. EXPORT_SYMBOL(mdio45_nway_restart);
  133. static u32 mdio45_get_an(const struct mdio_if_info *mdio, u16 addr)
  134. {
  135. u32 result = 0;
  136. int reg;
  137. reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_AN, addr);
  138. if (reg & ADVERTISE_10HALF)
  139. result |= ADVERTISED_10baseT_Half;
  140. if (reg & ADVERTISE_10FULL)
  141. result |= ADVERTISED_10baseT_Full;
  142. if (reg & ADVERTISE_100HALF)
  143. result |= ADVERTISED_100baseT_Half;
  144. if (reg & ADVERTISE_100FULL)
  145. result |= ADVERTISED_100baseT_Full;
  146. if (reg & ADVERTISE_PAUSE_CAP)
  147. result |= ADVERTISED_Pause;
  148. if (reg & ADVERTISE_PAUSE_ASYM)
  149. result |= ADVERTISED_Asym_Pause;
  150. return result;
  151. }
  152. /**
  153. * mdio45_ethtool_gset_npage - get settings for ETHTOOL_GSET
  154. * @mdio: MDIO interface
  155. * @ecmd: Ethtool request structure
  156. * @npage_adv: Modes currently advertised on next pages
  157. * @npage_lpa: Modes advertised by link partner on next pages
  158. *
  159. * The @ecmd parameter is expected to have been cleared before calling
  160. * mdio45_ethtool_gset_npage().
  161. *
  162. * Since the CSRs for auto-negotiation using next pages are not fully
  163. * standardised, this function does not attempt to decode them. The
  164. * caller must pass them in.
  165. */
  166. void mdio45_ethtool_gset_npage(const struct mdio_if_info *mdio,
  167. struct ethtool_cmd *ecmd,
  168. u32 npage_adv, u32 npage_lpa)
  169. {
  170. int reg;
  171. u32 speed;
  172. BUILD_BUG_ON(MDIO_SUPPORTS_C22 != ETH_MDIO_SUPPORTS_C22);
  173. BUILD_BUG_ON(MDIO_SUPPORTS_C45 != ETH_MDIO_SUPPORTS_C45);
  174. ecmd->transceiver = XCVR_INTERNAL;
  175. ecmd->phy_address = mdio->prtad;
  176. ecmd->mdio_support =
  177. mdio->mode_support & (MDIO_SUPPORTS_C45 | MDIO_SUPPORTS_C22);
  178. reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD,
  179. MDIO_CTRL2);
  180. switch (reg & MDIO_PMA_CTRL2_TYPE) {
  181. case MDIO_PMA_CTRL2_10GBT:
  182. case MDIO_PMA_CTRL2_1000BT:
  183. case MDIO_PMA_CTRL2_100BTX:
  184. case MDIO_PMA_CTRL2_10BT:
  185. ecmd->port = PORT_TP;
  186. ecmd->supported = SUPPORTED_TP;
  187. reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD,
  188. MDIO_SPEED);
  189. if (reg & MDIO_SPEED_10G)
  190. ecmd->supported |= SUPPORTED_10000baseT_Full;
  191. if (reg & MDIO_PMA_SPEED_1000)
  192. ecmd->supported |= (SUPPORTED_1000baseT_Full |
  193. SUPPORTED_1000baseT_Half);
  194. if (reg & MDIO_PMA_SPEED_100)
  195. ecmd->supported |= (SUPPORTED_100baseT_Full |
  196. SUPPORTED_100baseT_Half);
  197. if (reg & MDIO_PMA_SPEED_10)
  198. ecmd->supported |= (SUPPORTED_10baseT_Full |
  199. SUPPORTED_10baseT_Half);
  200. ecmd->advertising = ADVERTISED_TP;
  201. break;
  202. case MDIO_PMA_CTRL2_10GBCX4:
  203. ecmd->port = PORT_OTHER;
  204. ecmd->supported = 0;
  205. ecmd->advertising = 0;
  206. break;
  207. case MDIO_PMA_CTRL2_10GBKX4:
  208. case MDIO_PMA_CTRL2_10GBKR:
  209. case MDIO_PMA_CTRL2_1000BKX:
  210. ecmd->port = PORT_OTHER;
  211. ecmd->supported = SUPPORTED_Backplane;
  212. reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD,
  213. MDIO_PMA_EXTABLE);
  214. if (reg & MDIO_PMA_EXTABLE_10GBKX4)
  215. ecmd->supported |= SUPPORTED_10000baseKX4_Full;
  216. if (reg & MDIO_PMA_EXTABLE_10GBKR)
  217. ecmd->supported |= SUPPORTED_10000baseKR_Full;
  218. if (reg & MDIO_PMA_EXTABLE_1000BKX)
  219. ecmd->supported |= SUPPORTED_1000baseKX_Full;
  220. reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD,
  221. MDIO_PMA_10GBR_FECABLE);
  222. if (reg & MDIO_PMA_10GBR_FECABLE_ABLE)
  223. ecmd->supported |= SUPPORTED_10000baseR_FEC;
  224. ecmd->advertising = ADVERTISED_Backplane;
  225. break;
  226. /* All the other defined modes are flavours of optical */
  227. default:
  228. ecmd->port = PORT_FIBRE;
  229. ecmd->supported = SUPPORTED_FIBRE;
  230. ecmd->advertising = ADVERTISED_FIBRE;
  231. break;
  232. }
  233. if (mdio->mmds & MDIO_DEVS_AN) {
  234. ecmd->supported |= SUPPORTED_Autoneg;
  235. reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_AN,
  236. MDIO_CTRL1);
  237. if (reg & MDIO_AN_CTRL1_ENABLE) {
  238. ecmd->autoneg = AUTONEG_ENABLE;
  239. ecmd->advertising |=
  240. ADVERTISED_Autoneg |
  241. mdio45_get_an(mdio, MDIO_AN_ADVERTISE) |
  242. npage_adv;
  243. } else {
  244. ecmd->autoneg = AUTONEG_DISABLE;
  245. }
  246. } else {
  247. ecmd->autoneg = AUTONEG_DISABLE;
  248. }
  249. if (ecmd->autoneg) {
  250. u32 modes = 0;
  251. int an_stat = mdio->mdio_read(mdio->dev, mdio->prtad,
  252. MDIO_MMD_AN, MDIO_STAT1);
  253. /* If AN is complete and successful, report best common
  254. * mode, otherwise report best advertised mode. */
  255. if (an_stat & MDIO_AN_STAT1_COMPLETE) {
  256. ecmd->lp_advertising =
  257. mdio45_get_an(mdio, MDIO_AN_LPA) | npage_lpa;
  258. if (an_stat & MDIO_AN_STAT1_LPABLE)
  259. ecmd->lp_advertising |= ADVERTISED_Autoneg;
  260. modes = ecmd->advertising & ecmd->lp_advertising;
  261. }
  262. if ((modes & ~ADVERTISED_Autoneg) == 0)
  263. modes = ecmd->advertising;
  264. if (modes & (ADVERTISED_10000baseT_Full |
  265. ADVERTISED_10000baseKX4_Full |
  266. ADVERTISED_10000baseKR_Full)) {
  267. speed = SPEED_10000;
  268. ecmd->duplex = DUPLEX_FULL;
  269. } else if (modes & (ADVERTISED_1000baseT_Full |
  270. ADVERTISED_1000baseT_Half |
  271. ADVERTISED_1000baseKX_Full)) {
  272. speed = SPEED_1000;
  273. ecmd->duplex = !(modes & ADVERTISED_1000baseT_Half);
  274. } else if (modes & (ADVERTISED_100baseT_Full |
  275. ADVERTISED_100baseT_Half)) {
  276. speed = SPEED_100;
  277. ecmd->duplex = !!(modes & ADVERTISED_100baseT_Full);
  278. } else {
  279. speed = SPEED_10;
  280. ecmd->duplex = !!(modes & ADVERTISED_10baseT_Full);
  281. }
  282. } else {
  283. /* Report forced settings */
  284. reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD,
  285. MDIO_CTRL1);
  286. speed = (((reg & MDIO_PMA_CTRL1_SPEED1000) ? 100 : 1)
  287. * ((reg & MDIO_PMA_CTRL1_SPEED100) ? 100 : 10));
  288. ecmd->duplex = (reg & MDIO_CTRL1_FULLDPLX ||
  289. speed == SPEED_10000);
  290. }
  291. ethtool_cmd_speed_set(ecmd, speed);
  292. /* 10GBASE-T MDI/MDI-X */
  293. if (ecmd->port == PORT_TP
  294. && (ethtool_cmd_speed(ecmd) == SPEED_10000)) {
  295. switch (mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD,
  296. MDIO_PMA_10GBT_SWAPPOL)) {
  297. case MDIO_PMA_10GBT_SWAPPOL_ABNX | MDIO_PMA_10GBT_SWAPPOL_CDNX:
  298. ecmd->eth_tp_mdix = ETH_TP_MDI;
  299. break;
  300. case 0:
  301. ecmd->eth_tp_mdix = ETH_TP_MDI_X;
  302. break;
  303. default:
  304. /* It's complicated... */
  305. ecmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
  306. break;
  307. }
  308. }
  309. }
  310. EXPORT_SYMBOL(mdio45_ethtool_gset_npage);
  311. /**
  312. * mdio_mii_ioctl - MII ioctl interface for MDIO (clause 22 or 45) PHYs
  313. * @mdio: MDIO interface
  314. * @mii_data: MII ioctl data structure
  315. * @cmd: MII ioctl command
  316. *
  317. * Returns 0 on success, negative on error.
  318. */
  319. int mdio_mii_ioctl(const struct mdio_if_info *mdio,
  320. struct mii_ioctl_data *mii_data, int cmd)
  321. {
  322. int prtad, devad;
  323. u16 addr = mii_data->reg_num;
  324. /* Validate/convert cmd to one of SIOC{G,S}MIIREG */
  325. switch (cmd) {
  326. case SIOCGMIIPHY:
  327. if (mdio->prtad == MDIO_PRTAD_NONE)
  328. return -EOPNOTSUPP;
  329. mii_data->phy_id = mdio->prtad;
  330. cmd = SIOCGMIIREG;
  331. break;
  332. case SIOCGMIIREG:
  333. case SIOCSMIIREG:
  334. break;
  335. default:
  336. return -EOPNOTSUPP;
  337. }
  338. /* Validate/convert phy_id */
  339. if ((mdio->mode_support & MDIO_SUPPORTS_C45) &&
  340. mdio_phy_id_is_c45(mii_data->phy_id)) {
  341. prtad = mdio_phy_id_prtad(mii_data->phy_id);
  342. devad = mdio_phy_id_devad(mii_data->phy_id);
  343. } else if ((mdio->mode_support & MDIO_SUPPORTS_C22) &&
  344. mii_data->phy_id < 0x20) {
  345. prtad = mii_data->phy_id;
  346. devad = MDIO_DEVAD_NONE;
  347. addr &= 0x1f;
  348. } else if ((mdio->mode_support & MDIO_EMULATE_C22) &&
  349. mdio->prtad != MDIO_PRTAD_NONE &&
  350. mii_data->phy_id == mdio->prtad) {
  351. /* Remap commonly-used MII registers. */
  352. prtad = mdio->prtad;
  353. switch (addr) {
  354. case MII_BMCR:
  355. case MII_BMSR:
  356. case MII_PHYSID1:
  357. case MII_PHYSID2:
  358. devad = __ffs(mdio->mmds);
  359. break;
  360. case MII_ADVERTISE:
  361. case MII_LPA:
  362. if (!(mdio->mmds & MDIO_DEVS_AN))
  363. return -EINVAL;
  364. devad = MDIO_MMD_AN;
  365. if (addr == MII_ADVERTISE)
  366. addr = MDIO_AN_ADVERTISE;
  367. else
  368. addr = MDIO_AN_LPA;
  369. break;
  370. default:
  371. return -EINVAL;
  372. }
  373. } else {
  374. return -EINVAL;
  375. }
  376. if (cmd == SIOCGMIIREG) {
  377. int rc = mdio->mdio_read(mdio->dev, prtad, devad, addr);
  378. if (rc < 0)
  379. return rc;
  380. mii_data->val_out = rc;
  381. return 0;
  382. } else {
  383. return mdio->mdio_write(mdio->dev, prtad, devad, addr,
  384. mii_data->val_in);
  385. }
  386. }
  387. EXPORT_SYMBOL(mdio_mii_ioctl);