pcs-mtk-lynxi.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2018-2019 MediaTek Inc.
  3. /* A library for MediaTek SGMII circuit
  4. *
  5. * Author: Sean Wang <sean.wang@mediatek.com>
  6. * Author: Alexander Couzens <lynxis@fe80.eu>
  7. * Author: Daniel Golle <daniel@makrotopia.org>
  8. *
  9. */
  10. #include <linux/mdio.h>
  11. #include <linux/of.h>
  12. #include <linux/pcs/pcs-mtk-lynxi.h>
  13. #include <linux/phylink.h>
  14. #include <linux/regmap.h>
  15. /* SGMII subsystem config registers */
  16. /* BMCR (low 16) BMSR (high 16) */
  17. #define SGMSYS_PCS_CONTROL_1 0x0
  18. #define SGMII_BMCR GENMASK(15, 0)
  19. #define SGMII_BMSR GENMASK(31, 16)
  20. #define SGMSYS_PCS_DEVICE_ID 0x4
  21. #define SGMII_LYNXI_DEV_ID 0x4d544950
  22. #define SGMSYS_PCS_ADVERTISE 0x8
  23. #define SGMII_ADVERTISE GENMASK(15, 0)
  24. #define SGMII_LPA GENMASK(31, 16)
  25. #define SGMSYS_PCS_SCRATCH 0x14
  26. #define SGMII_DEV_VERSION GENMASK(31, 16)
  27. /* Register to programmable link timer, the unit in 2 * 8ns */
  28. #define SGMSYS_PCS_LINK_TIMER 0x18
  29. #define SGMII_LINK_TIMER_MASK GENMASK(19, 0)
  30. #define SGMII_LINK_TIMER_VAL(ns) FIELD_PREP(SGMII_LINK_TIMER_MASK, \
  31. ((ns) / 2 / 8))
  32. /* Register to control remote fault */
  33. #define SGMSYS_SGMII_MODE 0x20
  34. #define SGMII_IF_MODE_SGMII BIT(0)
  35. #define SGMII_SPEED_DUPLEX_AN BIT(1)
  36. #define SGMII_SPEED_MASK GENMASK(3, 2)
  37. #define SGMII_SPEED_10 FIELD_PREP(SGMII_SPEED_MASK, 0)
  38. #define SGMII_SPEED_100 FIELD_PREP(SGMII_SPEED_MASK, 1)
  39. #define SGMII_SPEED_1000 FIELD_PREP(SGMII_SPEED_MASK, 2)
  40. #define SGMII_DUPLEX_HALF BIT(4)
  41. #define SGMII_REMOTE_FAULT_DIS BIT(8)
  42. /* Register to reset SGMII design */
  43. #define SGMSYS_RESERVED_0 0x34
  44. #define SGMII_SW_RESET BIT(0)
  45. /* Register to set SGMII speed, ANA RG_ Control Signals III */
  46. #define SGMII_PHY_SPEED_MASK GENMASK(3, 2)
  47. #define SGMII_PHY_SPEED_1_25G FIELD_PREP(SGMII_PHY_SPEED_MASK, 0)
  48. #define SGMII_PHY_SPEED_3_125G FIELD_PREP(SGMII_PHY_SPEED_MASK, 1)
  49. /* Register to power up QPHY */
  50. #define SGMSYS_QPHY_PWR_STATE_CTRL 0xe8
  51. #define SGMII_PHYA_PWD BIT(4)
  52. /* Register to QPHY wrapper control */
  53. #define SGMSYS_QPHY_WRAP_CTRL 0xec
  54. #define SGMII_PN_SWAP_MASK GENMASK(1, 0)
  55. #define SGMII_PN_SWAP_TX_RX (BIT(0) | BIT(1))
  56. /* struct mtk_pcs_lynxi - This structure holds each sgmii regmap andassociated
  57. * data
  58. * @regmap: The register map pointing at the range used to setup
  59. * SGMII modes
  60. * @dev: Pointer to device owning the PCS
  61. * @ana_rgc3: The offset of register ANA_RGC3 relative to regmap
  62. * @interface: Currently configured interface mode
  63. * @pcs: Phylink PCS structure
  64. * @flags: Flags indicating hardware properties
  65. */
  66. struct mtk_pcs_lynxi {
  67. struct regmap *regmap;
  68. u32 ana_rgc3;
  69. phy_interface_t interface;
  70. struct phylink_pcs pcs;
  71. u32 flags;
  72. };
  73. static struct mtk_pcs_lynxi *pcs_to_mtk_pcs_lynxi(struct phylink_pcs *pcs)
  74. {
  75. return container_of(pcs, struct mtk_pcs_lynxi, pcs);
  76. }
  77. static void mtk_pcs_lynxi_get_state(struct phylink_pcs *pcs,
  78. struct phylink_link_state *state)
  79. {
  80. struct mtk_pcs_lynxi *mpcs = pcs_to_mtk_pcs_lynxi(pcs);
  81. unsigned int bm, adv;
  82. /* Read the BMSR and LPA */
  83. regmap_read(mpcs->regmap, SGMSYS_PCS_CONTROL_1, &bm);
  84. regmap_read(mpcs->regmap, SGMSYS_PCS_ADVERTISE, &adv);
  85. phylink_mii_c22_pcs_decode_state(state, FIELD_GET(SGMII_BMSR, bm),
  86. FIELD_GET(SGMII_LPA, adv));
  87. }
  88. static int mtk_pcs_lynxi_config(struct phylink_pcs *pcs, unsigned int neg_mode,
  89. phy_interface_t interface,
  90. const unsigned long *advertising,
  91. bool permit_pause_to_mac)
  92. {
  93. struct mtk_pcs_lynxi *mpcs = pcs_to_mtk_pcs_lynxi(pcs);
  94. bool mode_changed = false, changed;
  95. unsigned int rgc3, sgm_mode, bmcr;
  96. int advertise, link_timer;
  97. advertise = phylink_mii_c22_pcs_encode_advertisement(interface,
  98. advertising);
  99. if (advertise < 0)
  100. return advertise;
  101. /* Clearing IF_MODE_BIT0 switches the PCS to BASE-X mode, and
  102. * we assume that fixes it's speed at bitrate = line rate (in
  103. * other words, 1000Mbps or 2500Mbps).
  104. */
  105. if (interface == PHY_INTERFACE_MODE_SGMII)
  106. sgm_mode = SGMII_IF_MODE_SGMII;
  107. else
  108. sgm_mode = 0;
  109. if (neg_mode & PHYLINK_PCS_NEG_INBAND)
  110. sgm_mode |= SGMII_REMOTE_FAULT_DIS;
  111. if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED) {
  112. if (interface == PHY_INTERFACE_MODE_SGMII)
  113. sgm_mode |= SGMII_SPEED_DUPLEX_AN;
  114. bmcr = BMCR_ANENABLE;
  115. } else {
  116. bmcr = 0;
  117. }
  118. if (mpcs->interface != interface) {
  119. link_timer = phylink_get_link_timer_ns(interface);
  120. if (link_timer < 0)
  121. return link_timer;
  122. /* PHYA power down */
  123. regmap_set_bits(mpcs->regmap, SGMSYS_QPHY_PWR_STATE_CTRL,
  124. SGMII_PHYA_PWD);
  125. /* Reset SGMII PCS state */
  126. regmap_set_bits(mpcs->regmap, SGMSYS_RESERVED_0,
  127. SGMII_SW_RESET);
  128. if (mpcs->flags & MTK_SGMII_FLAG_PN_SWAP)
  129. regmap_update_bits(mpcs->regmap, SGMSYS_QPHY_WRAP_CTRL,
  130. SGMII_PN_SWAP_MASK,
  131. SGMII_PN_SWAP_TX_RX);
  132. if (interface == PHY_INTERFACE_MODE_2500BASEX)
  133. rgc3 = SGMII_PHY_SPEED_3_125G;
  134. else
  135. rgc3 = SGMII_PHY_SPEED_1_25G;
  136. /* Configure the underlying interface speed */
  137. regmap_update_bits(mpcs->regmap, mpcs->ana_rgc3,
  138. SGMII_PHY_SPEED_MASK, rgc3);
  139. /* Setup the link timer */
  140. regmap_write(mpcs->regmap, SGMSYS_PCS_LINK_TIMER,
  141. SGMII_LINK_TIMER_VAL(link_timer));
  142. mpcs->interface = interface;
  143. mode_changed = true;
  144. }
  145. /* Update the advertisement, noting whether it has changed */
  146. regmap_update_bits_check(mpcs->regmap, SGMSYS_PCS_ADVERTISE,
  147. SGMII_ADVERTISE, advertise, &changed);
  148. /* Update the sgmsys mode register */
  149. regmap_update_bits(mpcs->regmap, SGMSYS_SGMII_MODE,
  150. SGMII_REMOTE_FAULT_DIS | SGMII_SPEED_DUPLEX_AN |
  151. SGMII_IF_MODE_SGMII, sgm_mode);
  152. /* Update the BMCR */
  153. regmap_update_bits(mpcs->regmap, SGMSYS_PCS_CONTROL_1,
  154. BMCR_ANENABLE, bmcr);
  155. /* Release PHYA power down state
  156. * Only removing bit SGMII_PHYA_PWD isn't enough.
  157. * There are cases when the SGMII_PHYA_PWD register contains 0x9 which
  158. * prevents SGMII from working. The SGMII still shows link but no traffic
  159. * can flow. Writing 0x0 to the PHYA_PWD register fix the issue. 0x0 was
  160. * taken from a good working state of the SGMII interface.
  161. * Unknown how much the QPHY needs but it is racy without a sleep.
  162. * Tested on mt7622 & mt7986.
  163. */
  164. usleep_range(50, 100);
  165. regmap_write(mpcs->regmap, SGMSYS_QPHY_PWR_STATE_CTRL, 0);
  166. return changed || mode_changed;
  167. }
  168. static void mtk_pcs_lynxi_restart_an(struct phylink_pcs *pcs)
  169. {
  170. struct mtk_pcs_lynxi *mpcs = pcs_to_mtk_pcs_lynxi(pcs);
  171. regmap_set_bits(mpcs->regmap, SGMSYS_PCS_CONTROL_1, BMCR_ANRESTART);
  172. }
  173. static void mtk_pcs_lynxi_link_up(struct phylink_pcs *pcs,
  174. unsigned int neg_mode,
  175. phy_interface_t interface, int speed,
  176. int duplex)
  177. {
  178. struct mtk_pcs_lynxi *mpcs = pcs_to_mtk_pcs_lynxi(pcs);
  179. unsigned int sgm_mode;
  180. if (neg_mode != PHYLINK_PCS_NEG_INBAND_ENABLED) {
  181. /* Force the speed and duplex setting */
  182. if (speed == SPEED_10)
  183. sgm_mode = SGMII_SPEED_10;
  184. else if (speed == SPEED_100)
  185. sgm_mode = SGMII_SPEED_100;
  186. else
  187. sgm_mode = SGMII_SPEED_1000;
  188. if (duplex != DUPLEX_FULL)
  189. sgm_mode |= SGMII_DUPLEX_HALF;
  190. regmap_update_bits(mpcs->regmap, SGMSYS_SGMII_MODE,
  191. SGMII_DUPLEX_HALF | SGMII_SPEED_MASK,
  192. sgm_mode);
  193. }
  194. }
  195. static void mtk_pcs_lynxi_disable(struct phylink_pcs *pcs)
  196. {
  197. struct mtk_pcs_lynxi *mpcs = pcs_to_mtk_pcs_lynxi(pcs);
  198. mpcs->interface = PHY_INTERFACE_MODE_NA;
  199. }
  200. static const struct phylink_pcs_ops mtk_pcs_lynxi_ops = {
  201. .pcs_get_state = mtk_pcs_lynxi_get_state,
  202. .pcs_config = mtk_pcs_lynxi_config,
  203. .pcs_an_restart = mtk_pcs_lynxi_restart_an,
  204. .pcs_link_up = mtk_pcs_lynxi_link_up,
  205. .pcs_disable = mtk_pcs_lynxi_disable,
  206. };
  207. struct phylink_pcs *mtk_pcs_lynxi_create(struct device *dev,
  208. struct regmap *regmap, u32 ana_rgc3,
  209. u32 flags)
  210. {
  211. struct mtk_pcs_lynxi *mpcs;
  212. u32 id, ver;
  213. int ret;
  214. ret = regmap_read(regmap, SGMSYS_PCS_DEVICE_ID, &id);
  215. if (ret < 0)
  216. return NULL;
  217. if (id != SGMII_LYNXI_DEV_ID) {
  218. dev_err(dev, "unknown PCS device id %08x\n", id);
  219. return NULL;
  220. }
  221. ret = regmap_read(regmap, SGMSYS_PCS_SCRATCH, &ver);
  222. if (ret < 0)
  223. return NULL;
  224. ver = FIELD_GET(SGMII_DEV_VERSION, ver);
  225. if (ver != 0x1) {
  226. dev_err(dev, "unknown PCS device version %04x\n", ver);
  227. return NULL;
  228. }
  229. dev_dbg(dev, "MediaTek LynxI SGMII PCS (id 0x%08x, ver 0x%04x)\n", id,
  230. ver);
  231. mpcs = kzalloc(sizeof(*mpcs), GFP_KERNEL);
  232. if (!mpcs)
  233. return NULL;
  234. mpcs->ana_rgc3 = ana_rgc3;
  235. mpcs->regmap = regmap;
  236. mpcs->flags = flags;
  237. mpcs->pcs.ops = &mtk_pcs_lynxi_ops;
  238. mpcs->pcs.neg_mode = true;
  239. mpcs->pcs.poll = true;
  240. mpcs->interface = PHY_INTERFACE_MODE_NA;
  241. return &mpcs->pcs;
  242. }
  243. EXPORT_SYMBOL(mtk_pcs_lynxi_create);
  244. void mtk_pcs_lynxi_destroy(struct phylink_pcs *pcs)
  245. {
  246. if (!pcs)
  247. return;
  248. kfree(pcs_to_mtk_pcs_lynxi(pcs));
  249. }
  250. EXPORT_SYMBOL(mtk_pcs_lynxi_destroy);
  251. MODULE_DESCRIPTION("MediaTek SGMII library for LynxI");
  252. MODULE_LICENSE("GPL");