bcm87xx.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2011 - 2012 Cavium, Inc.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/phy.h>
  10. #include <linux/of.h>
  11. #define PHY_ID_BCM8706 0x0143bdc1
  12. #define PHY_ID_BCM8727 0x0143bff0
  13. #define BCM87XX_PMD_RX_SIGNAL_DETECT (MII_ADDR_C45 | 0x1000a)
  14. #define BCM87XX_10GBASER_PCS_STATUS (MII_ADDR_C45 | 0x30020)
  15. #define BCM87XX_XGXS_LANE_STATUS (MII_ADDR_C45 | 0x40018)
  16. #define BCM87XX_LASI_CONTROL (MII_ADDR_C45 | 0x39002)
  17. #define BCM87XX_LASI_STATUS (MII_ADDR_C45 | 0x39005)
  18. #if IS_ENABLED(CONFIG_OF_MDIO)
  19. /* Set and/or override some configuration registers based on the
  20. * marvell,reg-init property stored in the of_node for the phydev.
  21. *
  22. * broadcom,c45-reg-init = <devid reg mask value>,...;
  23. *
  24. * There may be one or more sets of <devid reg mask value>:
  25. *
  26. * devid: which sub-device to use.
  27. * reg: the register.
  28. * mask: if non-zero, ANDed with existing register value.
  29. * value: ORed with the masked value and written to the regiser.
  30. *
  31. */
  32. static int bcm87xx_of_reg_init(struct phy_device *phydev)
  33. {
  34. const __be32 *paddr;
  35. const __be32 *paddr_end;
  36. int len, ret;
  37. if (!phydev->dev.of_node)
  38. return 0;
  39. paddr = of_get_property(phydev->dev.of_node,
  40. "broadcom,c45-reg-init", &len);
  41. if (!paddr)
  42. return 0;
  43. paddr_end = paddr + (len /= sizeof(*paddr));
  44. ret = 0;
  45. while (paddr + 3 < paddr_end) {
  46. u16 devid = be32_to_cpup(paddr++);
  47. u16 reg = be32_to_cpup(paddr++);
  48. u16 mask = be32_to_cpup(paddr++);
  49. u16 val_bits = be32_to_cpup(paddr++);
  50. int val;
  51. u32 regnum = MII_ADDR_C45 | (devid << 16) | reg;
  52. val = 0;
  53. if (mask) {
  54. val = phy_read(phydev, regnum);
  55. if (val < 0) {
  56. ret = val;
  57. goto err;
  58. }
  59. val &= mask;
  60. }
  61. val |= val_bits;
  62. ret = phy_write(phydev, regnum, val);
  63. if (ret < 0)
  64. goto err;
  65. }
  66. err:
  67. return ret;
  68. }
  69. #else
  70. static int bcm87xx_of_reg_init(struct phy_device *phydev)
  71. {
  72. return 0;
  73. }
  74. #endif /* CONFIG_OF_MDIO */
  75. static int bcm87xx_config_init(struct phy_device *phydev)
  76. {
  77. phydev->supported = SUPPORTED_10000baseR_FEC;
  78. phydev->advertising = ADVERTISED_10000baseR_FEC;
  79. phydev->state = PHY_NOLINK;
  80. bcm87xx_of_reg_init(phydev);
  81. return 0;
  82. }
  83. static int bcm87xx_config_aneg(struct phy_device *phydev)
  84. {
  85. return -EINVAL;
  86. }
  87. static int bcm87xx_read_status(struct phy_device *phydev)
  88. {
  89. int rx_signal_detect;
  90. int pcs_status;
  91. int xgxs_lane_status;
  92. rx_signal_detect = phy_read(phydev, BCM87XX_PMD_RX_SIGNAL_DETECT);
  93. if (rx_signal_detect < 0)
  94. return rx_signal_detect;
  95. if ((rx_signal_detect & 1) == 0)
  96. goto no_link;
  97. pcs_status = phy_read(phydev, BCM87XX_10GBASER_PCS_STATUS);
  98. if (pcs_status < 0)
  99. return pcs_status;
  100. if ((pcs_status & 1) == 0)
  101. goto no_link;
  102. xgxs_lane_status = phy_read(phydev, BCM87XX_XGXS_LANE_STATUS);
  103. if (xgxs_lane_status < 0)
  104. return xgxs_lane_status;
  105. if ((xgxs_lane_status & 0x1000) == 0)
  106. goto no_link;
  107. phydev->speed = 10000;
  108. phydev->link = 1;
  109. phydev->duplex = 1;
  110. return 0;
  111. no_link:
  112. phydev->link = 0;
  113. return 0;
  114. }
  115. static int bcm87xx_config_intr(struct phy_device *phydev)
  116. {
  117. int reg, err;
  118. reg = phy_read(phydev, BCM87XX_LASI_CONTROL);
  119. if (reg < 0)
  120. return reg;
  121. if (phydev->interrupts == PHY_INTERRUPT_ENABLED)
  122. reg |= 1;
  123. else
  124. reg &= ~1;
  125. err = phy_write(phydev, BCM87XX_LASI_CONTROL, reg);
  126. return err;
  127. }
  128. static int bcm87xx_did_interrupt(struct phy_device *phydev)
  129. {
  130. int reg;
  131. reg = phy_read(phydev, BCM87XX_LASI_STATUS);
  132. if (reg < 0) {
  133. dev_err(&phydev->dev,
  134. "Error: Read of BCM87XX_LASI_STATUS failed: %d\n", 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 bcm8706_driver = {
  154. .phy_id = PHY_ID_BCM8706,
  155. .phy_id_mask = 0xffffffff,
  156. .name = "Broadcom BCM8706",
  157. .flags = PHY_HAS_INTERRUPT,
  158. .config_init = bcm87xx_config_init,
  159. .config_aneg = bcm87xx_config_aneg,
  160. .read_status = bcm87xx_read_status,
  161. .ack_interrupt = bcm87xx_ack_interrupt,
  162. .config_intr = bcm87xx_config_intr,
  163. .did_interrupt = bcm87xx_did_interrupt,
  164. .match_phy_device = bcm8706_match_phy_device,
  165. .driver = { .owner = THIS_MODULE },
  166. };
  167. static struct phy_driver bcm8727_driver = {
  168. .phy_id = PHY_ID_BCM8727,
  169. .phy_id_mask = 0xffffffff,
  170. .name = "Broadcom BCM8727",
  171. .flags = PHY_HAS_INTERRUPT,
  172. .config_init = bcm87xx_config_init,
  173. .config_aneg = bcm87xx_config_aneg,
  174. .read_status = bcm87xx_read_status,
  175. .ack_interrupt = bcm87xx_ack_interrupt,
  176. .config_intr = bcm87xx_config_intr,
  177. .did_interrupt = bcm87xx_did_interrupt,
  178. .match_phy_device = bcm8727_match_phy_device,
  179. .driver = { .owner = THIS_MODULE },
  180. };
  181. static int __init bcm87xx_init(void)
  182. {
  183. int ret;
  184. ret = phy_driver_register(&bcm8706_driver);
  185. if (ret)
  186. goto err;
  187. ret = phy_driver_register(&bcm8727_driver);
  188. err:
  189. return ret;
  190. }
  191. module_init(bcm87xx_init);
  192. static void __exit bcm87xx_exit(void)
  193. {
  194. phy_driver_unregister(&bcm8706_driver);
  195. phy_driver_unregister(&bcm8727_driver);
  196. }
  197. module_exit(bcm87xx_exit);