emac-sgmii-qdf2400.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2015-2016, The Linux Foundation. All rights reserved.
  3. */
  4. /* Qualcomm Technologies, Inc. QDF2400 EMAC SGMII Controller driver.
  5. */
  6. #include <linux/iopoll.h>
  7. #include "emac.h"
  8. /* EMAC_SGMII register offsets */
  9. #define EMAC_SGMII_PHY_TX_PWR_CTRL 0x000C
  10. #define EMAC_SGMII_PHY_LANE_CTRL1 0x0018
  11. #define EMAC_SGMII_PHY_CDR_CTRL0 0x0058
  12. #define EMAC_SGMII_PHY_POW_DWN_CTRL0 0x0080
  13. #define EMAC_SGMII_PHY_RESET_CTRL 0x00a8
  14. #define EMAC_SGMII_PHY_INTERRUPT_MASK 0x00b4
  15. /* SGMII digital lane registers */
  16. #define EMAC_SGMII_LN_DRVR_CTRL0 0x000C
  17. #define EMAC_SGMII_LN_DRVR_CTRL1 0x0010
  18. #define EMAC_SGMII_LN_DRVR_TAP_EN 0x0018
  19. #define EMAC_SGMII_LN_TX_MARGINING 0x001C
  20. #define EMAC_SGMII_LN_TX_PRE 0x0020
  21. #define EMAC_SGMII_LN_TX_POST 0x0024
  22. #define EMAC_SGMII_LN_TX_BAND_MODE 0x0060
  23. #define EMAC_SGMII_LN_LANE_MODE 0x0064
  24. #define EMAC_SGMII_LN_PARALLEL_RATE 0x007C
  25. #define EMAC_SGMII_LN_CML_CTRL_MODE0 0x00C0
  26. #define EMAC_SGMII_LN_MIXER_CTRL_MODE0 0x00D8
  27. #define EMAC_SGMII_LN_VGA_INITVAL 0x013C
  28. #define EMAC_SGMII_LN_UCDR_FO_GAIN_MODE0 0x0184
  29. #define EMAC_SGMII_LN_UCDR_SO_GAIN_MODE0 0x0190
  30. #define EMAC_SGMII_LN_UCDR_SO_CONFIG 0x019C
  31. #define EMAC_SGMII_LN_RX_BAND 0x01A4
  32. #define EMAC_SGMII_LN_RX_RCVR_PATH1_MODE0 0x01C0
  33. #define EMAC_SGMII_LN_RSM_CONFIG 0x01F8
  34. #define EMAC_SGMII_LN_SIGDET_ENABLES 0x0230
  35. #define EMAC_SGMII_LN_SIGDET_CNTRL 0x0234
  36. #define EMAC_SGMII_LN_SIGDET_DEGLITCH_CNTRL 0x0238
  37. #define EMAC_SGMII_LN_RX_EN_SIGNAL 0x02AC
  38. #define EMAC_SGMII_LN_RX_MISC_CNTRL0 0x02B8
  39. #define EMAC_SGMII_LN_DRVR_LOGIC_CLKDIV 0x02C8
  40. #define EMAC_SGMII_LN_RX_RESECODE_OFFSET 0x02CC
  41. /* SGMII digital lane register values */
  42. #define UCDR_STEP_BY_TWO_MODE0 BIT(7)
  43. #define UCDR_xO_GAIN_MODE(x) ((x) & 0x7f)
  44. #define UCDR_ENABLE BIT(6)
  45. #define UCDR_SO_SATURATION(x) ((x) & 0x3f)
  46. #define SIGDET_LP_BYP_PS4 BIT(7)
  47. #define SIGDET_EN_PS0_TO_PS2 BIT(6)
  48. #define TXVAL_VALID_INIT BIT(4)
  49. #define KR_PCIGEN3_MODE BIT(0)
  50. #define MAIN_EN BIT(0)
  51. #define TX_MARGINING_MUX BIT(6)
  52. #define TX_MARGINING(x) ((x) & 0x3f)
  53. #define TX_PRE_MUX BIT(6)
  54. #define TX_POST_MUX BIT(6)
  55. #define CML_GEAR_MODE(x) (((x) & 7) << 3)
  56. #define CML2CMOS_IBOOST_MODE(x) ((x) & 7)
  57. #define RESCODE_OFFSET(x) ((x) & 0x1f)
  58. #define MIXER_LOADB_MODE(x) (((x) & 0xf) << 2)
  59. #define MIXER_DATARATE_MODE(x) ((x) & 3)
  60. #define VGA_THRESH_DFE(x) ((x) & 0x3f)
  61. #define SIGDET_LP_BYP_PS0_TO_PS2 BIT(5)
  62. #define SIGDET_FLT_BYP BIT(0)
  63. #define SIGDET_LVL(x) (((x) & 0xf) << 4)
  64. #define SIGDET_DEGLITCH_CTRL(x) (((x) & 0xf) << 1)
  65. #define INVERT_PCS_RX_CLK BIT(7)
  66. #define DRVR_LOGIC_CLK_EN BIT(4)
  67. #define DRVR_LOGIC_CLK_DIV(x) ((x) & 0xf)
  68. #define PARALLEL_RATE_MODE0(x) ((x) & 0x3)
  69. #define BAND_MODE0(x) ((x) & 0x3)
  70. #define LANE_MODE(x) ((x) & 0x1f)
  71. #define CDR_PD_SEL_MODE0(x) (((x) & 0x3) << 5)
  72. #define EN_DLL_MODE0 BIT(4)
  73. #define EN_IQ_DCC_MODE0 BIT(3)
  74. #define EN_IQCAL_MODE0 BIT(2)
  75. #define BYPASS_RSM_SAMP_CAL BIT(1)
  76. #define BYPASS_RSM_DLL_CAL BIT(0)
  77. #define L0_RX_EQUALIZE_ENABLE BIT(6)
  78. #define PWRDN_B BIT(0)
  79. #define CDR_MAX_CNT(x) ((x) & 0xff)
  80. #define SERDES_START_WAIT_TIMES 100
  81. struct emac_reg_write {
  82. unsigned int offset;
  83. u32 val;
  84. };
  85. static void emac_reg_write_all(void __iomem *base,
  86. const struct emac_reg_write *itr, size_t size)
  87. {
  88. size_t i;
  89. for (i = 0; i < size; ++itr, ++i)
  90. writel(itr->val, base + itr->offset);
  91. }
  92. static const struct emac_reg_write sgmii_laned[] = {
  93. /* CDR Settings */
  94. {EMAC_SGMII_LN_UCDR_FO_GAIN_MODE0,
  95. UCDR_STEP_BY_TWO_MODE0 | UCDR_xO_GAIN_MODE(10)},
  96. {EMAC_SGMII_LN_UCDR_SO_GAIN_MODE0, UCDR_xO_GAIN_MODE(0)},
  97. {EMAC_SGMII_LN_UCDR_SO_CONFIG, UCDR_ENABLE | UCDR_SO_SATURATION(12)},
  98. /* TX/RX Settings */
  99. {EMAC_SGMII_LN_RX_EN_SIGNAL, SIGDET_LP_BYP_PS4 | SIGDET_EN_PS0_TO_PS2},
  100. {EMAC_SGMII_LN_DRVR_CTRL0, TXVAL_VALID_INIT | KR_PCIGEN3_MODE},
  101. {EMAC_SGMII_LN_DRVR_TAP_EN, MAIN_EN},
  102. {EMAC_SGMII_LN_TX_MARGINING, TX_MARGINING_MUX | TX_MARGINING(25)},
  103. {EMAC_SGMII_LN_TX_PRE, TX_PRE_MUX},
  104. {EMAC_SGMII_LN_TX_POST, TX_POST_MUX},
  105. {EMAC_SGMII_LN_CML_CTRL_MODE0,
  106. CML_GEAR_MODE(1) | CML2CMOS_IBOOST_MODE(1)},
  107. {EMAC_SGMII_LN_MIXER_CTRL_MODE0,
  108. MIXER_LOADB_MODE(12) | MIXER_DATARATE_MODE(1)},
  109. {EMAC_SGMII_LN_VGA_INITVAL, VGA_THRESH_DFE(31)},
  110. {EMAC_SGMII_LN_SIGDET_ENABLES,
  111. SIGDET_LP_BYP_PS0_TO_PS2 | SIGDET_FLT_BYP},
  112. {EMAC_SGMII_LN_SIGDET_CNTRL, SIGDET_LVL(8)},
  113. {EMAC_SGMII_LN_SIGDET_DEGLITCH_CNTRL, SIGDET_DEGLITCH_CTRL(4)},
  114. {EMAC_SGMII_LN_RX_MISC_CNTRL0, INVERT_PCS_RX_CLK},
  115. {EMAC_SGMII_LN_DRVR_LOGIC_CLKDIV,
  116. DRVR_LOGIC_CLK_EN | DRVR_LOGIC_CLK_DIV(4)},
  117. {EMAC_SGMII_LN_PARALLEL_RATE, PARALLEL_RATE_MODE0(1)},
  118. {EMAC_SGMII_LN_TX_BAND_MODE, BAND_MODE0(1)},
  119. {EMAC_SGMII_LN_RX_BAND, BAND_MODE0(2)},
  120. {EMAC_SGMII_LN_DRVR_CTRL1, RESCODE_OFFSET(7)},
  121. {EMAC_SGMII_LN_RX_RESECODE_OFFSET, RESCODE_OFFSET(9)},
  122. {EMAC_SGMII_LN_LANE_MODE, LANE_MODE(26)},
  123. {EMAC_SGMII_LN_RX_RCVR_PATH1_MODE0, CDR_PD_SEL_MODE0(2) |
  124. EN_DLL_MODE0 | EN_IQ_DCC_MODE0 | EN_IQCAL_MODE0},
  125. {EMAC_SGMII_LN_RSM_CONFIG, BYPASS_RSM_SAMP_CAL | BYPASS_RSM_DLL_CAL},
  126. };
  127. static const struct emac_reg_write physical_coding_sublayer_programming[] = {
  128. {EMAC_SGMII_PHY_POW_DWN_CTRL0, PWRDN_B},
  129. {EMAC_SGMII_PHY_CDR_CTRL0, CDR_MAX_CNT(15)},
  130. {EMAC_SGMII_PHY_TX_PWR_CTRL, 0},
  131. {EMAC_SGMII_PHY_LANE_CTRL1, L0_RX_EQUALIZE_ENABLE},
  132. };
  133. int emac_sgmii_init_qdf2400(struct emac_adapter *adpt)
  134. {
  135. struct emac_sgmii *phy = &adpt->phy;
  136. void __iomem *phy_regs = phy->base;
  137. void __iomem *laned = phy->digital;
  138. unsigned int i;
  139. u32 lnstatus;
  140. /* PCS lane-x init */
  141. emac_reg_write_all(phy->base, physical_coding_sublayer_programming,
  142. ARRAY_SIZE(physical_coding_sublayer_programming));
  143. /* SGMII lane-x init */
  144. emac_reg_write_all(phy->digital, sgmii_laned, ARRAY_SIZE(sgmii_laned));
  145. /* Power up PCS and start reset lane state machine */
  146. writel(0, phy_regs + EMAC_SGMII_PHY_RESET_CTRL);
  147. writel(1, laned + SGMII_LN_RSM_START);
  148. /* Wait for c_ready assertion */
  149. for (i = 0; i < SERDES_START_WAIT_TIMES; i++) {
  150. lnstatus = readl(phy_regs + SGMII_PHY_LN_LANE_STATUS);
  151. if (lnstatus & BIT(1))
  152. break;
  153. usleep_range(100, 200);
  154. }
  155. if (i == SERDES_START_WAIT_TIMES) {
  156. netdev_err(adpt->netdev, "SGMII failed to start\n");
  157. return -EIO;
  158. }
  159. /* Disable digital and SERDES loopback */
  160. writel(0, phy_regs + SGMII_PHY_LN_BIST_GEN0);
  161. writel(0, phy_regs + SGMII_PHY_LN_BIST_GEN2);
  162. writel(0, phy_regs + SGMII_PHY_LN_CDR_CTRL1);
  163. /* Mask out all the SGMII Interrupt */
  164. writel(0, phy_regs + EMAC_SGMII_PHY_INTERRUPT_MASK);
  165. return 0;
  166. }