bcm87xx.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2011 - 2012 Cavium, Inc.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/phy.h>
  7. #include <linux/of.h>
  8. #define PHY_ID_BCM8706 0x0143bdc1
  9. #define PHY_ID_BCM8727 0x0143bff0
  10. #define BCM87XX_PMD_RX_SIGNAL_DETECT (MII_ADDR_C45 | 0x1000a)
  11. #define BCM87XX_10GBASER_PCS_STATUS (MII_ADDR_C45 | 0x30020)
  12. #define BCM87XX_XGXS_LANE_STATUS (MII_ADDR_C45 | 0x40018)
  13. #define BCM87XX_LASI_CONTROL (MII_ADDR_C45 | 0x39002)
  14. #define BCM87XX_LASI_STATUS (MII_ADDR_C45 | 0x39005)
  15. #if IS_ENABLED(CONFIG_OF_MDIO)
  16. /* Set and/or override some configuration registers based on the
  17. * broadcom,c45-reg-init property stored in the of_node for the phydev.
  18. *
  19. * broadcom,c45-reg-init = <devid reg mask value>,...;
  20. *
  21. * There may be one or more sets of <devid reg mask value>:
  22. *
  23. * devid: which sub-device to use.
  24. * reg: the register.
  25. * mask: if non-zero, ANDed with existing register value.
  26. * value: ORed with the masked value and written to the regiser.
  27. *
  28. */
  29. static int bcm87xx_of_reg_init(struct phy_device *phydev)
  30. {
  31. const __be32 *paddr;
  32. const __be32 *paddr_end;
  33. int len, ret;
  34. if (!phydev->mdio.dev.of_node)
  35. return 0;
  36. paddr = of_get_property(phydev->mdio.dev.of_node,
  37. "broadcom,c45-reg-init", &len);
  38. if (!paddr)
  39. return 0;
  40. paddr_end = paddr + (len /= sizeof(*paddr));
  41. ret = 0;
  42. while (paddr + 3 < paddr_end) {
  43. u16 devid = be32_to_cpup(paddr++);
  44. u16 reg = be32_to_cpup(paddr++);
  45. u16 mask = be32_to_cpup(paddr++);
  46. u16 val_bits = be32_to_cpup(paddr++);
  47. int val;
  48. u32 regnum = MII_ADDR_C45 | (devid << 16) | reg;
  49. val = 0;
  50. if (mask) {
  51. val = phy_read(phydev, regnum);
  52. if (val < 0) {
  53. ret = val;
  54. goto err;
  55. }
  56. val &= mask;
  57. }
  58. val |= val_bits;
  59. ret = phy_write(phydev, regnum, val);
  60. if (ret < 0)
  61. goto err;
  62. }
  63. err:
  64. return ret;
  65. }
  66. #else
  67. static int bcm87xx_of_reg_init(struct phy_device *phydev)
  68. {
  69. return 0;
  70. }
  71. #endif /* CONFIG_OF_MDIO */
  72. static int bcm87xx_get_features(struct phy_device *phydev)
  73. {
  74. linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT,
  75. phydev->supported);
  76. return 0;
  77. }
  78. static int bcm87xx_config_init(struct phy_device *phydev)
  79. {
  80. return bcm87xx_of_reg_init(phydev);
  81. }
  82. static int bcm87xx_config_aneg(struct phy_device *phydev)
  83. {
  84. return -EINVAL;
  85. }
  86. static int bcm87xx_read_status(struct phy_device *phydev)
  87. {
  88. int rx_signal_detect;
  89. int pcs_status;
  90. int xgxs_lane_status;
  91. rx_signal_detect = phy_read(phydev, BCM87XX_PMD_RX_SIGNAL_DETECT);
  92. if (rx_signal_detect < 0)
  93. return rx_signal_detect;
  94. if ((rx_signal_detect & 1) == 0)
  95. goto no_link;
  96. pcs_status = phy_read(phydev, BCM87XX_10GBASER_PCS_STATUS);
  97. if (pcs_status < 0)
  98. return pcs_status;
  99. if ((pcs_status & 1) == 0)
  100. goto no_link;
  101. xgxs_lane_status = phy_read(phydev, BCM87XX_XGXS_LANE_STATUS);
  102. if (xgxs_lane_status < 0)
  103. return xgxs_lane_status;
  104. if ((xgxs_lane_status & 0x1000) == 0)
  105. goto no_link;
  106. phydev->speed = 10000;
  107. phydev->link = 1;
  108. phydev->duplex = 1;
  109. return 0;
  110. no_link:
  111. phydev->link = 0;
  112. return 0;
  113. }
  114. static int bcm87xx_config_intr(struct phy_device *phydev)
  115. {
  116. int reg, err;
  117. reg = phy_read(phydev, BCM87XX_LASI_CONTROL);
  118. if (reg < 0)
  119. return reg;
  120. if (phydev->interrupts == PHY_INTERRUPT_ENABLED)
  121. reg |= 1;
  122. else
  123. reg &= ~1;
  124. err = phy_write(phydev, BCM87XX_LASI_CONTROL, reg);
  125. return err;
  126. }
  127. static int bcm87xx_did_interrupt(struct phy_device *phydev)
  128. {
  129. int reg;
  130. reg = phy_read(phydev, BCM87XX_LASI_STATUS);
  131. if (reg < 0) {
  132. phydev_err(phydev,
  133. "Error: Read of BCM87XX_LASI_STATUS failed: %d\n",
  134. reg);
  135. return 0;
  136. }
  137. return (reg & 1) != 0;
  138. }
  139. static int bcm87xx_ack_interrupt(struct phy_device *phydev)
  140. {
  141. /* Reading the LASI status clears it. */
  142. bcm87xx_did_interrupt(phydev);
  143. return 0;
  144. }
  145. static int bcm8706_match_phy_device(struct phy_device *phydev)
  146. {
  147. return phydev->c45_ids.device_ids[4] == PHY_ID_BCM8706;
  148. }
  149. static int bcm8727_match_phy_device(struct phy_device *phydev)
  150. {
  151. return phydev->c45_ids.device_ids[4] == PHY_ID_BCM8727;
  152. }
  153. static struct phy_driver bcm87xx_driver[] = {
  154. {
  155. .phy_id = PHY_ID_BCM8706,
  156. .phy_id_mask = 0xffffffff,
  157. .name = "Broadcom BCM8706",
  158. .get_features = bcm87xx_get_features,
  159. .config_init = bcm87xx_config_init,
  160. .config_aneg = bcm87xx_config_aneg,
  161. .read_status = bcm87xx_read_status,
  162. .ack_interrupt = bcm87xx_ack_interrupt,
  163. .config_intr = bcm87xx_config_intr,
  164. .did_interrupt = bcm87xx_did_interrupt,
  165. .match_phy_device = bcm8706_match_phy_device,
  166. }, {
  167. .phy_id = PHY_ID_BCM8727,
  168. .phy_id_mask = 0xffffffff,
  169. .name = "Broadcom BCM8727",
  170. .get_features = bcm87xx_get_features,
  171. .config_init = bcm87xx_config_init,
  172. .config_aneg = bcm87xx_config_aneg,
  173. .read_status = bcm87xx_read_status,
  174. .ack_interrupt = bcm87xx_ack_interrupt,
  175. .config_intr = bcm87xx_config_intr,
  176. .did_interrupt = bcm87xx_did_interrupt,
  177. .match_phy_device = bcm8727_match_phy_device,
  178. } };
  179. module_phy_driver(bcm87xx_driver);
  180. MODULE_LICENSE("GPL v2");