bcm84881.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Broadcom BCM84881 NBASE-T PHY driver, as found on a SFP+ module.
  3. // Copyright (C) 2019 Russell King, Deep Blue Solutions Ltd.
  4. //
  5. // Like the Marvell 88x3310, the Broadcom 84881 changes its host-side
  6. // interface according to the operating speed between 10GBASE-R,
  7. // 2500BASE-X and SGMII (but unlike the 88x3310, without the control
  8. // word).
  9. //
  10. // This driver only supports those aspects of the PHY that I'm able to
  11. // observe and test with the SFP+ module, which is an incomplete subset
  12. // of what this PHY is able to support. For example, I only assume it
  13. // supports a single lane Serdes connection, but it may be that the PHY
  14. // is able to support more than that.
  15. #include <linux/delay.h>
  16. #include <linux/module.h>
  17. #include <linux/phy.h>
  18. enum {
  19. MDIO_AN_C22 = 0xffe0,
  20. };
  21. static int bcm84881_wait_init(struct phy_device *phydev)
  22. {
  23. int val;
  24. return phy_read_mmd_poll_timeout(phydev, MDIO_MMD_PMAPMD, MDIO_CTRL1,
  25. val, !(val & MDIO_CTRL1_RESET),
  26. 100000, 2000000, false);
  27. }
  28. static void bcm84881_fill_possible_interfaces(struct phy_device *phydev)
  29. {
  30. unsigned long *possible = phydev->possible_interfaces;
  31. __set_bit(PHY_INTERFACE_MODE_SGMII, possible);
  32. __set_bit(PHY_INTERFACE_MODE_2500BASEX, possible);
  33. __set_bit(PHY_INTERFACE_MODE_10GBASER, possible);
  34. }
  35. static int bcm84881_config_init(struct phy_device *phydev)
  36. {
  37. bcm84881_fill_possible_interfaces(phydev);
  38. switch (phydev->interface) {
  39. case PHY_INTERFACE_MODE_SGMII:
  40. case PHY_INTERFACE_MODE_2500BASEX:
  41. case PHY_INTERFACE_MODE_10GBASER:
  42. break;
  43. default:
  44. return -ENODEV;
  45. }
  46. return 0;
  47. }
  48. static int bcm84881_probe(struct phy_device *phydev)
  49. {
  50. /* This driver requires PMAPMD and AN blocks */
  51. const u32 mmd_mask = MDIO_DEVS_PMAPMD | MDIO_DEVS_AN;
  52. if (!phydev->is_c45 ||
  53. (phydev->c45_ids.devices_in_package & mmd_mask) != mmd_mask)
  54. return -ENODEV;
  55. return 0;
  56. }
  57. static int bcm84881_get_features(struct phy_device *phydev)
  58. {
  59. int ret;
  60. ret = genphy_c45_pma_read_abilities(phydev);
  61. if (ret)
  62. return ret;
  63. /* Although the PHY sets bit 1.11.8, it does not support 10M modes */
  64. linkmode_clear_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT,
  65. phydev->supported);
  66. linkmode_clear_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT,
  67. phydev->supported);
  68. return 0;
  69. }
  70. static int bcm84881_config_aneg(struct phy_device *phydev)
  71. {
  72. bool changed = false;
  73. u32 adv;
  74. int ret;
  75. /* Wait for the PHY to finish initialising, otherwise our
  76. * advertisement may be overwritten.
  77. */
  78. ret = bcm84881_wait_init(phydev);
  79. if (ret)
  80. return ret;
  81. /* We don't support manual MDI control */
  82. phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
  83. /* disabled autoneg doesn't seem to work with this PHY */
  84. if (phydev->autoneg == AUTONEG_DISABLE)
  85. return -EINVAL;
  86. ret = genphy_c45_an_config_aneg(phydev);
  87. if (ret < 0)
  88. return ret;
  89. if (ret > 0)
  90. changed = true;
  91. adv = linkmode_adv_to_mii_ctrl1000_t(phydev->advertising);
  92. ret = phy_modify_mmd_changed(phydev, MDIO_MMD_AN,
  93. MDIO_AN_C22 + MII_CTRL1000,
  94. ADVERTISE_1000FULL | ADVERTISE_1000HALF,
  95. adv);
  96. if (ret < 0)
  97. return ret;
  98. if (ret > 0)
  99. changed = true;
  100. return genphy_c45_check_and_restart_aneg(phydev, changed);
  101. }
  102. static int bcm84881_aneg_done(struct phy_device *phydev)
  103. {
  104. int bmsr, val;
  105. val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1);
  106. if (val < 0)
  107. return val;
  108. bmsr = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_C22 + MII_BMSR);
  109. if (bmsr < 0)
  110. return bmsr;
  111. return !!(val & MDIO_AN_STAT1_COMPLETE) &&
  112. !!(bmsr & BMSR_ANEGCOMPLETE);
  113. }
  114. static int bcm84881_read_status(struct phy_device *phydev)
  115. {
  116. unsigned int mode;
  117. int bmsr, val;
  118. val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_CTRL1);
  119. if (val < 0)
  120. return val;
  121. if (val & MDIO_AN_CTRL1_RESTART) {
  122. phydev->link = 0;
  123. return 0;
  124. }
  125. val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1);
  126. if (val < 0)
  127. return val;
  128. bmsr = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_C22 + MII_BMSR);
  129. if (bmsr < 0)
  130. return bmsr;
  131. phydev->autoneg_complete = !!(val & MDIO_AN_STAT1_COMPLETE) &&
  132. !!(bmsr & BMSR_ANEGCOMPLETE);
  133. phydev->link = !!(val & MDIO_STAT1_LSTATUS) &&
  134. !!(bmsr & BMSR_LSTATUS);
  135. if (phydev->autoneg == AUTONEG_ENABLE && !phydev->autoneg_complete)
  136. phydev->link = false;
  137. linkmode_zero(phydev->lp_advertising);
  138. phydev->speed = SPEED_UNKNOWN;
  139. phydev->duplex = DUPLEX_UNKNOWN;
  140. phydev->pause = 0;
  141. phydev->asym_pause = 0;
  142. phydev->mdix = 0;
  143. if (!phydev->link)
  144. return 0;
  145. if (phydev->autoneg_complete) {
  146. val = genphy_c45_read_lpa(phydev);
  147. if (val < 0)
  148. return val;
  149. val = phy_read_mmd(phydev, MDIO_MMD_AN,
  150. MDIO_AN_C22 + MII_STAT1000);
  151. if (val < 0)
  152. return val;
  153. mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising, val);
  154. if (phydev->autoneg == AUTONEG_ENABLE)
  155. phy_resolve_aneg_linkmode(phydev);
  156. }
  157. if (phydev->autoneg == AUTONEG_DISABLE) {
  158. /* disabled autoneg doesn't seem to work, so force the link
  159. * down.
  160. */
  161. phydev->link = 0;
  162. return 0;
  163. }
  164. /* Set the host link mode - we set the phy interface mode and
  165. * the speed according to this register so that downshift works.
  166. * We leave the duplex setting as per the resolution from the
  167. * above.
  168. */
  169. val = phy_read_mmd(phydev, MDIO_MMD_VEND1, 0x4011);
  170. mode = (val & 0x1e) >> 1;
  171. if (mode == 1 || mode == 2)
  172. phydev->interface = PHY_INTERFACE_MODE_SGMII;
  173. else if (mode == 3)
  174. phydev->interface = PHY_INTERFACE_MODE_10GBASER;
  175. else if (mode == 4)
  176. phydev->interface = PHY_INTERFACE_MODE_2500BASEX;
  177. switch (mode & 7) {
  178. case 1:
  179. phydev->speed = SPEED_100;
  180. break;
  181. case 2:
  182. phydev->speed = SPEED_1000;
  183. break;
  184. case 3:
  185. phydev->speed = SPEED_10000;
  186. break;
  187. case 4:
  188. phydev->speed = SPEED_2500;
  189. break;
  190. case 5:
  191. phydev->speed = SPEED_5000;
  192. break;
  193. }
  194. return genphy_c45_read_mdix(phydev);
  195. }
  196. /* The Broadcom BCM84881 in the Methode DM7052 is unable to provide a SGMII
  197. * or 802.3z control word, so inband will not work.
  198. */
  199. static unsigned int bcm84881_inband_caps(struct phy_device *phydev,
  200. phy_interface_t interface)
  201. {
  202. return LINK_INBAND_DISABLE;
  203. }
  204. static struct phy_driver bcm84881_drivers[] = {
  205. {
  206. .phy_id = 0xae025150,
  207. .phy_id_mask = 0xfffffff0,
  208. .name = "Broadcom BCM84881",
  209. .inband_caps = bcm84881_inband_caps,
  210. .config_init = bcm84881_config_init,
  211. .probe = bcm84881_probe,
  212. .get_features = bcm84881_get_features,
  213. .config_aneg = bcm84881_config_aneg,
  214. .aneg_done = bcm84881_aneg_done,
  215. .read_status = bcm84881_read_status,
  216. },
  217. };
  218. module_phy_driver(bcm84881_drivers);
  219. /* FIXME: module auto-loading for Clause 45 PHYs seems non-functional */
  220. static const struct mdio_device_id __maybe_unused bcm84881_tbl[] = {
  221. { 0xae025150, 0xfffffff0 },
  222. { },
  223. };
  224. MODULE_AUTHOR("Russell King");
  225. MODULE_DESCRIPTION("Broadcom BCM84881 PHY driver");
  226. MODULE_DEVICE_TABLE(mdio, bcm84881_tbl);
  227. MODULE_LICENSE("GPL");