emac-sgmii-fsm9900.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2015-2016, The Linux Foundation. All rights reserved.
  3. */
  4. /* Qualcomm Technologies, Inc. FSM9900 EMAC SGMII Controller driver.
  5. */
  6. #include <linux/iopoll.h>
  7. #include "emac.h"
  8. /* EMAC_QSERDES register offsets */
  9. #define EMAC_QSERDES_COM_SYS_CLK_CTRL 0x0000
  10. #define EMAC_QSERDES_COM_PLL_CNTRL 0x0014
  11. #define EMAC_QSERDES_COM_PLL_IP_SETI 0x0018
  12. #define EMAC_QSERDES_COM_PLL_CP_SETI 0x0024
  13. #define EMAC_QSERDES_COM_PLL_IP_SETP 0x0028
  14. #define EMAC_QSERDES_COM_PLL_CP_SETP 0x002c
  15. #define EMAC_QSERDES_COM_SYSCLK_EN_SEL 0x0038
  16. #define EMAC_QSERDES_COM_RESETSM_CNTRL 0x0040
  17. #define EMAC_QSERDES_COM_PLLLOCK_CMP1 0x0044
  18. #define EMAC_QSERDES_COM_PLLLOCK_CMP2 0x0048
  19. #define EMAC_QSERDES_COM_PLLLOCK_CMP3 0x004c
  20. #define EMAC_QSERDES_COM_PLLLOCK_CMP_EN 0x0050
  21. #define EMAC_QSERDES_COM_DEC_START1 0x0064
  22. #define EMAC_QSERDES_COM_DIV_FRAC_START1 0x0098
  23. #define EMAC_QSERDES_COM_DIV_FRAC_START2 0x009c
  24. #define EMAC_QSERDES_COM_DIV_FRAC_START3 0x00a0
  25. #define EMAC_QSERDES_COM_DEC_START2 0x00a4
  26. #define EMAC_QSERDES_COM_PLL_CRCTRL 0x00ac
  27. #define EMAC_QSERDES_COM_RESET_SM 0x00bc
  28. #define EMAC_QSERDES_TX_BIST_MODE_LANENO 0x0100
  29. #define EMAC_QSERDES_TX_TX_EMP_POST1_LVL 0x0108
  30. #define EMAC_QSERDES_TX_TX_DRV_LVL 0x010c
  31. #define EMAC_QSERDES_TX_LANE_MODE 0x0150
  32. #define EMAC_QSERDES_TX_TRAN_DRVR_EMP_EN 0x0170
  33. #define EMAC_QSERDES_RX_CDR_CONTROL 0x0200
  34. #define EMAC_QSERDES_RX_CDR_CONTROL2 0x0210
  35. #define EMAC_QSERDES_RX_RX_EQ_GAIN12 0x0230
  36. /* EMAC_SGMII register offsets */
  37. #define EMAC_SGMII_PHY_SERDES_START 0x0000
  38. #define EMAC_SGMII_PHY_CMN_PWR_CTRL 0x0004
  39. #define EMAC_SGMII_PHY_RX_PWR_CTRL 0x0008
  40. #define EMAC_SGMII_PHY_TX_PWR_CTRL 0x000C
  41. #define EMAC_SGMII_PHY_LANE_CTRL1 0x0018
  42. #define EMAC_SGMII_PHY_CDR_CTRL0 0x0058
  43. #define EMAC_SGMII_PHY_POW_DWN_CTRL0 0x0080
  44. #define EMAC_SGMII_PHY_INTERRUPT_MASK 0x00b4
  45. #define PLL_IPSETI(x) ((x) & 0x3f)
  46. #define PLL_CPSETI(x) ((x) & 0xff)
  47. #define PLL_IPSETP(x) ((x) & 0x3f)
  48. #define PLL_CPSETP(x) ((x) & 0x1f)
  49. #define PLL_RCTRL(x) (((x) & 0xf) << 4)
  50. #define PLL_CCTRL(x) ((x) & 0xf)
  51. #define LANE_MODE(x) ((x) & 0x1f)
  52. #define SYSCLK_CM BIT(4)
  53. #define SYSCLK_AC_COUPLE BIT(3)
  54. #define OCP_EN BIT(5)
  55. #define PLL_DIV_FFEN BIT(2)
  56. #define PLL_DIV_ORD BIT(1)
  57. #define SYSCLK_SEL_CMOS BIT(3)
  58. #define FRQ_TUNE_MODE BIT(4)
  59. #define PLLLOCK_CMP_EN BIT(0)
  60. #define DEC_START1_MUX BIT(7)
  61. #define DEC_START1(x) ((x) & 0x7f)
  62. #define DIV_FRAC_START_MUX BIT(7)
  63. #define DIV_FRAC_START(x) ((x) & 0x7f)
  64. #define DIV_FRAC_START3_MUX BIT(4)
  65. #define DIV_FRAC_START3(x) ((x) & 0xf)
  66. #define DEC_START2_MUX BIT(1)
  67. #define DEC_START2 BIT(0)
  68. #define READY BIT(5)
  69. #define TX_EMP_POST1_LVL_MUX BIT(5)
  70. #define TX_EMP_POST1_LVL(x) ((x) & 0x1f)
  71. #define TX_DRV_LVL_MUX BIT(4)
  72. #define TX_DRV_LVL(x) ((x) & 0xf)
  73. #define EMP_EN_MUX BIT(1)
  74. #define EMP_EN BIT(0)
  75. #define SECONDORDERENABLE BIT(6)
  76. #define FIRSTORDER_THRESH(x) (((x) & 0x7) << 3)
  77. #define SECONDORDERGAIN(x) ((x) & 0x7)
  78. #define RX_EQ_GAIN2(x) (((x) & 0xf) << 4)
  79. #define RX_EQ_GAIN1(x) ((x) & 0xf)
  80. #define SERDES_START BIT(0)
  81. #define BIAS_EN BIT(6)
  82. #define PLL_EN BIT(5)
  83. #define SYSCLK_EN BIT(4)
  84. #define CLKBUF_L_EN BIT(3)
  85. #define PLL_TXCLK_EN BIT(1)
  86. #define PLL_RXCLK_EN BIT(0)
  87. #define L0_RX_SIGDET_EN BIT(7)
  88. #define L0_RX_TERM_MODE(x) (((x) & 3) << 4)
  89. #define L0_RX_I_EN BIT(1)
  90. #define L0_TX_EN BIT(5)
  91. #define L0_CLKBUF_EN BIT(4)
  92. #define L0_TRAN_BIAS_EN BIT(1)
  93. #define L0_RX_EQUALIZE_ENABLE BIT(6)
  94. #define L0_RESET_TSYNC_EN BIT(4)
  95. #define L0_DRV_LVL(x) ((x) & 0xf)
  96. #define PWRDN_B BIT(0)
  97. #define CDR_MAX_CNT(x) ((x) & 0xff)
  98. #define PLLLOCK_CMP(x) ((x) & 0xff)
  99. #define SERDES_START_WAIT_TIMES 100
  100. struct emac_reg_write {
  101. unsigned int offset;
  102. u32 val;
  103. };
  104. static void emac_reg_write_all(void __iomem *base,
  105. const struct emac_reg_write *itr, size_t size)
  106. {
  107. size_t i;
  108. for (i = 0; i < size; ++itr, ++i)
  109. writel(itr->val, base + itr->offset);
  110. }
  111. static const struct emac_reg_write physical_coding_sublayer_programming[] = {
  112. {EMAC_SGMII_PHY_CDR_CTRL0, CDR_MAX_CNT(15)},
  113. {EMAC_SGMII_PHY_POW_DWN_CTRL0, PWRDN_B},
  114. {EMAC_SGMII_PHY_CMN_PWR_CTRL,
  115. BIAS_EN | SYSCLK_EN | CLKBUF_L_EN | PLL_TXCLK_EN | PLL_RXCLK_EN},
  116. {EMAC_SGMII_PHY_TX_PWR_CTRL, L0_TX_EN | L0_CLKBUF_EN | L0_TRAN_BIAS_EN},
  117. {EMAC_SGMII_PHY_RX_PWR_CTRL,
  118. L0_RX_SIGDET_EN | L0_RX_TERM_MODE(1) | L0_RX_I_EN},
  119. {EMAC_SGMII_PHY_CMN_PWR_CTRL,
  120. BIAS_EN | PLL_EN | SYSCLK_EN | CLKBUF_L_EN | PLL_TXCLK_EN |
  121. PLL_RXCLK_EN},
  122. {EMAC_SGMII_PHY_LANE_CTRL1,
  123. L0_RX_EQUALIZE_ENABLE | L0_RESET_TSYNC_EN | L0_DRV_LVL(15)},
  124. };
  125. static const struct emac_reg_write sysclk_refclk_setting[] = {
  126. {EMAC_QSERDES_COM_SYSCLK_EN_SEL, SYSCLK_SEL_CMOS},
  127. {EMAC_QSERDES_COM_SYS_CLK_CTRL, SYSCLK_CM | SYSCLK_AC_COUPLE},
  128. };
  129. static const struct emac_reg_write pll_setting[] = {
  130. {EMAC_QSERDES_COM_PLL_IP_SETI, PLL_IPSETI(1)},
  131. {EMAC_QSERDES_COM_PLL_CP_SETI, PLL_CPSETI(59)},
  132. {EMAC_QSERDES_COM_PLL_IP_SETP, PLL_IPSETP(10)},
  133. {EMAC_QSERDES_COM_PLL_CP_SETP, PLL_CPSETP(9)},
  134. {EMAC_QSERDES_COM_PLL_CRCTRL, PLL_RCTRL(15) | PLL_CCTRL(11)},
  135. {EMAC_QSERDES_COM_PLL_CNTRL, OCP_EN | PLL_DIV_FFEN | PLL_DIV_ORD},
  136. {EMAC_QSERDES_COM_DEC_START1, DEC_START1_MUX | DEC_START1(2)},
  137. {EMAC_QSERDES_COM_DEC_START2, DEC_START2_MUX | DEC_START2},
  138. {EMAC_QSERDES_COM_DIV_FRAC_START1,
  139. DIV_FRAC_START_MUX | DIV_FRAC_START(85)},
  140. {EMAC_QSERDES_COM_DIV_FRAC_START2,
  141. DIV_FRAC_START_MUX | DIV_FRAC_START(42)},
  142. {EMAC_QSERDES_COM_DIV_FRAC_START3,
  143. DIV_FRAC_START3_MUX | DIV_FRAC_START3(3)},
  144. {EMAC_QSERDES_COM_PLLLOCK_CMP1, PLLLOCK_CMP(43)},
  145. {EMAC_QSERDES_COM_PLLLOCK_CMP2, PLLLOCK_CMP(104)},
  146. {EMAC_QSERDES_COM_PLLLOCK_CMP3, PLLLOCK_CMP(0)},
  147. {EMAC_QSERDES_COM_PLLLOCK_CMP_EN, PLLLOCK_CMP_EN},
  148. {EMAC_QSERDES_COM_RESETSM_CNTRL, FRQ_TUNE_MODE},
  149. };
  150. static const struct emac_reg_write cdr_setting[] = {
  151. {EMAC_QSERDES_RX_CDR_CONTROL,
  152. SECONDORDERENABLE | FIRSTORDER_THRESH(3) | SECONDORDERGAIN(2)},
  153. {EMAC_QSERDES_RX_CDR_CONTROL2,
  154. SECONDORDERENABLE | FIRSTORDER_THRESH(3) | SECONDORDERGAIN(4)},
  155. };
  156. static const struct emac_reg_write tx_rx_setting[] = {
  157. {EMAC_QSERDES_TX_BIST_MODE_LANENO, 0},
  158. {EMAC_QSERDES_TX_TX_DRV_LVL, TX_DRV_LVL_MUX | TX_DRV_LVL(15)},
  159. {EMAC_QSERDES_TX_TRAN_DRVR_EMP_EN, EMP_EN_MUX | EMP_EN},
  160. {EMAC_QSERDES_TX_TX_EMP_POST1_LVL,
  161. TX_EMP_POST1_LVL_MUX | TX_EMP_POST1_LVL(1)},
  162. {EMAC_QSERDES_RX_RX_EQ_GAIN12, RX_EQ_GAIN2(15) | RX_EQ_GAIN1(15)},
  163. {EMAC_QSERDES_TX_LANE_MODE, LANE_MODE(8)},
  164. };
  165. int emac_sgmii_init_fsm9900(struct emac_adapter *adpt)
  166. {
  167. struct emac_sgmii *phy = &adpt->phy;
  168. unsigned int i;
  169. emac_reg_write_all(phy->base, physical_coding_sublayer_programming,
  170. ARRAY_SIZE(physical_coding_sublayer_programming));
  171. emac_reg_write_all(phy->base, sysclk_refclk_setting,
  172. ARRAY_SIZE(sysclk_refclk_setting));
  173. emac_reg_write_all(phy->base, pll_setting, ARRAY_SIZE(pll_setting));
  174. emac_reg_write_all(phy->base, cdr_setting, ARRAY_SIZE(cdr_setting));
  175. emac_reg_write_all(phy->base, tx_rx_setting, ARRAY_SIZE(tx_rx_setting));
  176. /* Power up the Ser/Des engine */
  177. writel(SERDES_START, phy->base + EMAC_SGMII_PHY_SERDES_START);
  178. for (i = 0; i < SERDES_START_WAIT_TIMES; i++) {
  179. if (readl(phy->base + EMAC_QSERDES_COM_RESET_SM) & READY)
  180. break;
  181. usleep_range(100, 200);
  182. }
  183. if (i == SERDES_START_WAIT_TIMES) {
  184. netdev_err(adpt->netdev, "error: ser/des failed to start\n");
  185. return -EIO;
  186. }
  187. /* Mask out all the SGMII Interrupt */
  188. writel(0, phy->base + EMAC_SGMII_PHY_INTERRUPT_MASK);
  189. return 0;
  190. }