b53_serdes.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause
  2. /*
  3. * Northstar Plus switch SerDes/SGMII PHY main logic
  4. *
  5. * Copyright (C) 2018 Florian Fainelli <f.fainelli@gmail.com>
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/delay.h>
  9. #include <linux/kernel.h>
  10. #include <linux/phy.h>
  11. #include <linux/phylink.h>
  12. #include <net/dsa.h>
  13. #include "b53_priv.h"
  14. #include "b53_serdes.h"
  15. #include "b53_regs.h"
  16. static void b53_serdes_write_blk(struct b53_device *dev, u8 offset, u16 block,
  17. u16 value)
  18. {
  19. b53_write16(dev, B53_SERDES_PAGE, B53_SERDES_BLKADDR, block);
  20. b53_write16(dev, B53_SERDES_PAGE, offset, value);
  21. }
  22. static u16 b53_serdes_read_blk(struct b53_device *dev, u8 offset, u16 block)
  23. {
  24. u16 value;
  25. b53_write16(dev, B53_SERDES_PAGE, B53_SERDES_BLKADDR, block);
  26. b53_read16(dev, B53_SERDES_PAGE, offset, &value);
  27. return value;
  28. }
  29. static void b53_serdes_set_lane(struct b53_device *dev, u8 lane)
  30. {
  31. if (dev->serdes_lane == lane)
  32. return;
  33. WARN_ON(lane > 1);
  34. b53_serdes_write_blk(dev, B53_SERDES_LANE,
  35. SERDES_XGXSBLK0_BLOCKADDRESS, lane);
  36. dev->serdes_lane = lane;
  37. }
  38. static void b53_serdes_write(struct b53_device *dev, u8 lane,
  39. u8 offset, u16 block, u16 value)
  40. {
  41. b53_serdes_set_lane(dev, lane);
  42. b53_serdes_write_blk(dev, offset, block, value);
  43. }
  44. static u16 b53_serdes_read(struct b53_device *dev, u8 lane,
  45. u8 offset, u16 block)
  46. {
  47. b53_serdes_set_lane(dev, lane);
  48. return b53_serdes_read_blk(dev, offset, block);
  49. }
  50. void b53_serdes_config(struct b53_device *dev, int port, unsigned int mode,
  51. const struct phylink_link_state *state)
  52. {
  53. u8 lane = b53_serdes_map_lane(dev, port);
  54. u16 reg;
  55. if (lane == B53_INVALID_LANE)
  56. return;
  57. reg = b53_serdes_read(dev, lane, B53_SERDES_DIGITAL_CONTROL(1),
  58. SERDES_DIGITAL_BLK);
  59. if (state->interface == PHY_INTERFACE_MODE_1000BASEX)
  60. reg |= FIBER_MODE_1000X;
  61. else
  62. reg &= ~FIBER_MODE_1000X;
  63. b53_serdes_write(dev, lane, B53_SERDES_DIGITAL_CONTROL(1),
  64. SERDES_DIGITAL_BLK, reg);
  65. }
  66. EXPORT_SYMBOL(b53_serdes_config);
  67. void b53_serdes_an_restart(struct b53_device *dev, int port)
  68. {
  69. u8 lane = b53_serdes_map_lane(dev, port);
  70. u16 reg;
  71. if (lane == B53_INVALID_LANE)
  72. return;
  73. reg = b53_serdes_read(dev, lane, B53_SERDES_MII_REG(MII_BMCR),
  74. SERDES_MII_BLK);
  75. reg |= BMCR_ANRESTART;
  76. b53_serdes_write(dev, lane, B53_SERDES_MII_REG(MII_BMCR),
  77. SERDES_MII_BLK, reg);
  78. }
  79. EXPORT_SYMBOL(b53_serdes_an_restart);
  80. int b53_serdes_link_state(struct b53_device *dev, int port,
  81. struct phylink_link_state *state)
  82. {
  83. u8 lane = b53_serdes_map_lane(dev, port);
  84. u16 dig, bmsr;
  85. if (lane == B53_INVALID_LANE)
  86. return 1;
  87. dig = b53_serdes_read(dev, lane, B53_SERDES_DIGITAL_STATUS,
  88. SERDES_DIGITAL_BLK);
  89. bmsr = b53_serdes_read(dev, lane, B53_SERDES_MII_REG(MII_BMSR),
  90. SERDES_MII_BLK);
  91. switch ((dig >> SPEED_STATUS_SHIFT) & SPEED_STATUS_MASK) {
  92. case SPEED_STATUS_10:
  93. state->speed = SPEED_10;
  94. break;
  95. case SPEED_STATUS_100:
  96. state->speed = SPEED_100;
  97. break;
  98. case SPEED_STATUS_1000:
  99. state->speed = SPEED_1000;
  100. break;
  101. default:
  102. case SPEED_STATUS_2500:
  103. state->speed = SPEED_2500;
  104. break;
  105. }
  106. state->duplex = dig & DUPLEX_STATUS ? DUPLEX_FULL : DUPLEX_HALF;
  107. state->an_complete = !!(bmsr & BMSR_ANEGCOMPLETE);
  108. state->link = !!(dig & LINK_STATUS);
  109. if (dig & PAUSE_RESOLUTION_RX_SIDE)
  110. state->pause |= MLO_PAUSE_RX;
  111. if (dig & PAUSE_RESOLUTION_TX_SIDE)
  112. state->pause |= MLO_PAUSE_TX;
  113. return 0;
  114. }
  115. EXPORT_SYMBOL(b53_serdes_link_state);
  116. void b53_serdes_link_set(struct b53_device *dev, int port, unsigned int mode,
  117. phy_interface_t interface, bool link_up)
  118. {
  119. u8 lane = b53_serdes_map_lane(dev, port);
  120. u16 reg;
  121. if (lane == B53_INVALID_LANE)
  122. return;
  123. reg = b53_serdes_read(dev, lane, B53_SERDES_MII_REG(MII_BMCR),
  124. SERDES_MII_BLK);
  125. if (link_up)
  126. reg &= ~BMCR_PDOWN;
  127. else
  128. reg |= BMCR_PDOWN;
  129. b53_serdes_write(dev, lane, B53_SERDES_MII_REG(MII_BMCR),
  130. SERDES_MII_BLK, reg);
  131. }
  132. EXPORT_SYMBOL(b53_serdes_link_set);
  133. void b53_serdes_phylink_validate(struct b53_device *dev, int port,
  134. unsigned long *supported,
  135. struct phylink_link_state *state)
  136. {
  137. u8 lane = b53_serdes_map_lane(dev, port);
  138. if (lane == B53_INVALID_LANE)
  139. return;
  140. switch (lane) {
  141. case 0:
  142. phylink_set(supported, 2500baseX_Full);
  143. /* fallthrough */
  144. case 1:
  145. phylink_set(supported, 1000baseX_Full);
  146. break;
  147. default:
  148. break;
  149. }
  150. }
  151. EXPORT_SYMBOL(b53_serdes_phylink_validate);
  152. int b53_serdes_init(struct b53_device *dev, int port)
  153. {
  154. u8 lane = b53_serdes_map_lane(dev, port);
  155. u16 id0, msb, lsb;
  156. if (lane == B53_INVALID_LANE)
  157. return -EINVAL;
  158. id0 = b53_serdes_read(dev, lane, B53_SERDES_ID0, SERDES_ID0);
  159. msb = b53_serdes_read(dev, lane, B53_SERDES_MII_REG(MII_PHYSID1),
  160. SERDES_MII_BLK);
  161. lsb = b53_serdes_read(dev, lane, B53_SERDES_MII_REG(MII_PHYSID2),
  162. SERDES_MII_BLK);
  163. if (id0 == 0 || id0 == 0xffff) {
  164. dev_err(dev->dev, "SerDes not initialized, check settings\n");
  165. return -ENODEV;
  166. }
  167. dev_info(dev->dev,
  168. "SerDes lane %d, model: %d, rev %c%d (OUI: 0x%08x)\n",
  169. lane, id0 & SERDES_ID0_MODEL_MASK,
  170. (id0 >> SERDES_ID0_REV_LETTER_SHIFT) + 0x41,
  171. (id0 >> SERDES_ID0_REV_NUM_SHIFT) & SERDES_ID0_REV_NUM_MASK,
  172. (u32)msb << 16 | lsb);
  173. return 0;
  174. }
  175. EXPORT_SYMBOL(b53_serdes_init);
  176. MODULE_AUTHOR("Florian Fainelli <f.fainelli@gmail.com>");
  177. MODULE_DESCRIPTION("B53 Switch SerDes driver");
  178. MODULE_LICENSE("Dual BSD/GPL");