emac-sgmii.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. /* Copyright (c) 2015-2016, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. /* Qualcomm Technologies, Inc. EMAC SGMII Controller driver.
  13. */
  14. #include <linux/iopoll.h>
  15. #include <linux/acpi.h>
  16. #include <linux/of_device.h>
  17. #include "emac.h"
  18. #include "emac-mac.h"
  19. #include "emac-sgmii.h"
  20. /* EMAC_QSERDES register offsets */
  21. #define EMAC_QSERDES_COM_SYS_CLK_CTRL 0x000000
  22. #define EMAC_QSERDES_COM_PLL_CNTRL 0x000014
  23. #define EMAC_QSERDES_COM_PLL_IP_SETI 0x000018
  24. #define EMAC_QSERDES_COM_PLL_CP_SETI 0x000024
  25. #define EMAC_QSERDES_COM_PLL_IP_SETP 0x000028
  26. #define EMAC_QSERDES_COM_PLL_CP_SETP 0x00002c
  27. #define EMAC_QSERDES_COM_SYSCLK_EN_SEL 0x000038
  28. #define EMAC_QSERDES_COM_RESETSM_CNTRL 0x000040
  29. #define EMAC_QSERDES_COM_PLLLOCK_CMP1 0x000044
  30. #define EMAC_QSERDES_COM_PLLLOCK_CMP2 0x000048
  31. #define EMAC_QSERDES_COM_PLLLOCK_CMP3 0x00004c
  32. #define EMAC_QSERDES_COM_PLLLOCK_CMP_EN 0x000050
  33. #define EMAC_QSERDES_COM_DEC_START1 0x000064
  34. #define EMAC_QSERDES_COM_DIV_FRAC_START1 0x000098
  35. #define EMAC_QSERDES_COM_DIV_FRAC_START2 0x00009c
  36. #define EMAC_QSERDES_COM_DIV_FRAC_START3 0x0000a0
  37. #define EMAC_QSERDES_COM_DEC_START2 0x0000a4
  38. #define EMAC_QSERDES_COM_PLL_CRCTRL 0x0000ac
  39. #define EMAC_QSERDES_COM_RESET_SM 0x0000bc
  40. #define EMAC_QSERDES_TX_BIST_MODE_LANENO 0x000100
  41. #define EMAC_QSERDES_TX_TX_EMP_POST1_LVL 0x000108
  42. #define EMAC_QSERDES_TX_TX_DRV_LVL 0x00010c
  43. #define EMAC_QSERDES_TX_LANE_MODE 0x000150
  44. #define EMAC_QSERDES_TX_TRAN_DRVR_EMP_EN 0x000170
  45. #define EMAC_QSERDES_RX_CDR_CONTROL 0x000200
  46. #define EMAC_QSERDES_RX_CDR_CONTROL2 0x000210
  47. #define EMAC_QSERDES_RX_RX_EQ_GAIN12 0x000230
  48. /* EMAC_SGMII register offsets */
  49. #define EMAC_SGMII_PHY_SERDES_START 0x000000
  50. #define EMAC_SGMII_PHY_CMN_PWR_CTRL 0x000004
  51. #define EMAC_SGMII_PHY_RX_PWR_CTRL 0x000008
  52. #define EMAC_SGMII_PHY_TX_PWR_CTRL 0x00000C
  53. #define EMAC_SGMII_PHY_LANE_CTRL1 0x000018
  54. #define EMAC_SGMII_PHY_AUTONEG_CFG2 0x000048
  55. #define EMAC_SGMII_PHY_CDR_CTRL0 0x000058
  56. #define EMAC_SGMII_PHY_SPEED_CFG1 0x000074
  57. #define EMAC_SGMII_PHY_POW_DWN_CTRL0 0x000080
  58. #define EMAC_SGMII_PHY_RESET_CTRL 0x0000a8
  59. #define EMAC_SGMII_PHY_IRQ_CMD 0x0000ac
  60. #define EMAC_SGMII_PHY_INTERRUPT_CLEAR 0x0000b0
  61. #define EMAC_SGMII_PHY_INTERRUPT_MASK 0x0000b4
  62. #define EMAC_SGMII_PHY_INTERRUPT_STATUS 0x0000b8
  63. #define EMAC_SGMII_PHY_RX_CHK_STATUS 0x0000d4
  64. #define EMAC_SGMII_PHY_AUTONEG0_STATUS 0x0000e0
  65. #define EMAC_SGMII_PHY_AUTONEG1_STATUS 0x0000e4
  66. /* EMAC_QSERDES_COM_PLL_IP_SETI */
  67. #define PLL_IPSETI(x) ((x) & 0x3f)
  68. /* EMAC_QSERDES_COM_PLL_CP_SETI */
  69. #define PLL_CPSETI(x) ((x) & 0xff)
  70. /* EMAC_QSERDES_COM_PLL_IP_SETP */
  71. #define PLL_IPSETP(x) ((x) & 0x3f)
  72. /* EMAC_QSERDES_COM_PLL_CP_SETP */
  73. #define PLL_CPSETP(x) ((x) & 0x1f)
  74. /* EMAC_QSERDES_COM_PLL_CRCTRL */
  75. #define PLL_RCTRL(x) (((x) & 0xf) << 4)
  76. #define PLL_CCTRL(x) ((x) & 0xf)
  77. /* SGMII v2 PHY registers per lane */
  78. #define EMAC_SGMII_PHY_LN_OFFSET 0x0400
  79. /* SGMII v2 digital lane registers */
  80. #define EMAC_SGMII_LN_DRVR_CTRL0 0x00C
  81. #define EMAC_SGMII_LN_DRVR_TAP_EN 0x018
  82. #define EMAC_SGMII_LN_TX_MARGINING 0x01C
  83. #define EMAC_SGMII_LN_TX_PRE 0x020
  84. #define EMAC_SGMII_LN_TX_POST 0x024
  85. #define EMAC_SGMII_LN_TX_BAND_MODE 0x060
  86. #define EMAC_SGMII_LN_LANE_MODE 0x064
  87. #define EMAC_SGMII_LN_PARALLEL_RATE 0x078
  88. #define EMAC_SGMII_LN_CML_CTRL_MODE0 0x0B8
  89. #define EMAC_SGMII_LN_MIXER_CTRL_MODE0 0x0D0
  90. #define EMAC_SGMII_LN_VGA_INITVAL 0x134
  91. #define EMAC_SGMII_LN_UCDR_FO_GAIN_MODE0 0x17C
  92. #define EMAC_SGMII_LN_UCDR_SO_GAIN_MODE0 0x188
  93. #define EMAC_SGMII_LN_UCDR_SO_CONFIG 0x194
  94. #define EMAC_SGMII_LN_RX_BAND 0x19C
  95. #define EMAC_SGMII_LN_RX_RCVR_PATH1_MODE0 0x1B8
  96. #define EMAC_SGMII_LN_RSM_CONFIG 0x1F0
  97. #define EMAC_SGMII_LN_SIGDET_ENABLES 0x224
  98. #define EMAC_SGMII_LN_SIGDET_CNTRL 0x228
  99. #define EMAC_SGMII_LN_SIGDET_DEGLITCH_CNTRL 0x22C
  100. #define EMAC_SGMII_LN_RX_EN_SIGNAL 0x2A0
  101. #define EMAC_SGMII_LN_RX_MISC_CNTRL0 0x2AC
  102. #define EMAC_SGMII_LN_DRVR_LOGIC_CLKDIV 0x2BC
  103. /* SGMII v2 digital lane register values */
  104. #define UCDR_STEP_BY_TWO_MODE0 BIT(7)
  105. #define UCDR_xO_GAIN_MODE(x) ((x) & 0x7f)
  106. #define UCDR_ENABLE BIT(6)
  107. #define UCDR_SO_SATURATION(x) ((x) & 0x3f)
  108. #define SIGDET_LP_BYP_PS4 BIT(7)
  109. #define SIGDET_EN_PS0_TO_PS2 BIT(6)
  110. #define EN_ACCOUPLEVCM_SW_MUX BIT(5)
  111. #define EN_ACCOUPLEVCM_SW BIT(4)
  112. #define RX_SYNC_EN BIT(3)
  113. #define RXTERM_HIGHZ_PS5 BIT(2)
  114. #define SIGDET_EN_PS3 BIT(1)
  115. #define EN_ACCOUPLE_VCM_PS3 BIT(0)
  116. #define UFS_MODE BIT(5)
  117. #define TXVAL_VALID_INIT BIT(4)
  118. #define TXVAL_VALID_MUX BIT(3)
  119. #define TXVAL_VALID BIT(2)
  120. #define USB3P1_MODE BIT(1)
  121. #define KR_PCIGEN3_MODE BIT(0)
  122. #define PRE_EN BIT(3)
  123. #define POST_EN BIT(2)
  124. #define MAIN_EN_MUX BIT(1)
  125. #define MAIN_EN BIT(0)
  126. #define TX_MARGINING_MUX BIT(6)
  127. #define TX_MARGINING(x) ((x) & 0x3f)
  128. #define TX_PRE_MUX BIT(6)
  129. #define TX_PRE(x) ((x) & 0x3f)
  130. #define TX_POST_MUX BIT(6)
  131. #define TX_POST(x) ((x) & 0x3f)
  132. #define CML_GEAR_MODE(x) (((x) & 7) << 3)
  133. #define CML2CMOS_IBOOST_MODE(x) ((x) & 7)
  134. #define MIXER_LOADB_MODE(x) (((x) & 0xf) << 2)
  135. #define MIXER_DATARATE_MODE(x) ((x) & 3)
  136. #define VGA_THRESH_DFE(x) ((x) & 0x3f)
  137. #define SIGDET_LP_BYP_PS0_TO_PS2 BIT(5)
  138. #define SIGDET_LP_BYP_MUX BIT(4)
  139. #define SIGDET_LP_BYP BIT(3)
  140. #define SIGDET_EN_MUX BIT(2)
  141. #define SIGDET_EN BIT(1)
  142. #define SIGDET_FLT_BYP BIT(0)
  143. #define SIGDET_LVL(x) (((x) & 0xf) << 4)
  144. #define SIGDET_BW_CTRL(x) ((x) & 0xf)
  145. #define SIGDET_DEGLITCH_CTRL(x) (((x) & 0xf) << 1)
  146. #define SIGDET_DEGLITCH_BYP BIT(0)
  147. #define INVERT_PCS_RX_CLK BIT(7)
  148. #define PWM_EN BIT(6)
  149. #define RXBIAS_SEL(x) (((x) & 0x3) << 4)
  150. #define EBDAC_SIGN BIT(3)
  151. #define EDAC_SIGN BIT(2)
  152. #define EN_AUXTAP1SIGN_INVERT BIT(1)
  153. #define EN_DAC_CHOPPING BIT(0)
  154. #define DRVR_LOGIC_CLK_EN BIT(4)
  155. #define DRVR_LOGIC_CLK_DIV(x) ((x) & 0xf)
  156. #define PARALLEL_RATE_MODE2(x) (((x) & 0x3) << 4)
  157. #define PARALLEL_RATE_MODE1(x) (((x) & 0x3) << 2)
  158. #define PARALLEL_RATE_MODE0(x) ((x) & 0x3)
  159. #define BAND_MODE2(x) (((x) & 0x3) << 4)
  160. #define BAND_MODE1(x) (((x) & 0x3) << 2)
  161. #define BAND_MODE0(x) ((x) & 0x3)
  162. #define LANE_SYNC_MODE BIT(5)
  163. #define LANE_MODE(x) ((x) & 0x1f)
  164. #define CDR_PD_SEL_MODE0(x) (((x) & 0x3) << 5)
  165. #define EN_DLL_MODE0 BIT(4)
  166. #define EN_IQ_DCC_MODE0 BIT(3)
  167. #define EN_IQCAL_MODE0 BIT(2)
  168. #define EN_QPATH_MODE0 BIT(1)
  169. #define EN_EPATH_MODE0 BIT(0)
  170. #define FORCE_TSYNC_ACK BIT(7)
  171. #define FORCE_CMN_ACK BIT(6)
  172. #define FORCE_CMN_READY BIT(5)
  173. #define EN_RCLK_DEGLITCH BIT(4)
  174. #define BYPASS_RSM_CDR_RESET BIT(3)
  175. #define BYPASS_RSM_TSYNC BIT(2)
  176. #define BYPASS_RSM_SAMP_CAL BIT(1)
  177. #define BYPASS_RSM_DLL_CAL BIT(0)
  178. /* EMAC_QSERDES_COM_SYS_CLK_CTRL */
  179. #define SYSCLK_CM BIT(4)
  180. #define SYSCLK_AC_COUPLE BIT(3)
  181. /* EMAC_QSERDES_COM_PLL_CNTRL */
  182. #define OCP_EN BIT(5)
  183. #define PLL_DIV_FFEN BIT(2)
  184. #define PLL_DIV_ORD BIT(1)
  185. /* EMAC_QSERDES_COM_SYSCLK_EN_SEL */
  186. #define SYSCLK_SEL_CMOS BIT(3)
  187. /* EMAC_QSERDES_COM_RESETSM_CNTRL */
  188. #define FRQ_TUNE_MODE BIT(4)
  189. /* EMAC_QSERDES_COM_PLLLOCK_CMP_EN */
  190. #define PLLLOCK_CMP_EN BIT(0)
  191. /* EMAC_QSERDES_COM_DEC_START1 */
  192. #define DEC_START1_MUX BIT(7)
  193. #define DEC_START1(x) ((x) & 0x7f)
  194. /* EMAC_QSERDES_COM_DIV_FRAC_START1 * EMAC_QSERDES_COM_DIV_FRAC_START2 */
  195. #define DIV_FRAC_START_MUX BIT(7)
  196. #define DIV_FRAC_START(x) ((x) & 0x7f)
  197. /* EMAC_QSERDES_COM_DIV_FRAC_START3 */
  198. #define DIV_FRAC_START3_MUX BIT(4)
  199. #define DIV_FRAC_START3(x) ((x) & 0xf)
  200. /* EMAC_QSERDES_COM_DEC_START2 */
  201. #define DEC_START2_MUX BIT(1)
  202. #define DEC_START2 BIT(0)
  203. /* EMAC_QSERDES_COM_RESET_SM */
  204. #define READY BIT(5)
  205. /* EMAC_QSERDES_TX_TX_EMP_POST1_LVL */
  206. #define TX_EMP_POST1_LVL_MUX BIT(5)
  207. #define TX_EMP_POST1_LVL(x) ((x) & 0x1f)
  208. #define TX_EMP_POST1_LVL_BMSK 0x1f
  209. #define TX_EMP_POST1_LVL_SHFT 0
  210. /* EMAC_QSERDES_TX_TX_DRV_LVL */
  211. #define TX_DRV_LVL_MUX BIT(4)
  212. #define TX_DRV_LVL(x) ((x) & 0xf)
  213. /* EMAC_QSERDES_TX_TRAN_DRVR_EMP_EN */
  214. #define EMP_EN_MUX BIT(1)
  215. #define EMP_EN BIT(0)
  216. /* EMAC_QSERDES_RX_CDR_CONTROL & EMAC_QSERDES_RX_CDR_CONTROL2 */
  217. #define HBW_PD_EN BIT(7)
  218. #define SECONDORDERENABLE BIT(6)
  219. #define FIRSTORDER_THRESH(x) (((x) & 0x7) << 3)
  220. #define SECONDORDERGAIN(x) ((x) & 0x7)
  221. /* EMAC_QSERDES_RX_RX_EQ_GAIN12 */
  222. #define RX_EQ_GAIN2(x) (((x) & 0xf) << 4)
  223. #define RX_EQ_GAIN1(x) ((x) & 0xf)
  224. /* EMAC_SGMII_PHY_SERDES_START */
  225. #define SERDES_START BIT(0)
  226. /* EMAC_SGMII_PHY_CMN_PWR_CTRL */
  227. #define BIAS_EN BIT(6)
  228. #define PLL_EN BIT(5)
  229. #define SYSCLK_EN BIT(4)
  230. #define CLKBUF_L_EN BIT(3)
  231. #define PLL_TXCLK_EN BIT(1)
  232. #define PLL_RXCLK_EN BIT(0)
  233. /* EMAC_SGMII_PHY_RX_PWR_CTRL */
  234. #define L0_RX_SIGDET_EN BIT(7)
  235. #define L0_RX_TERM_MODE(x) (((x) & 3) << 4)
  236. #define L0_RX_I_EN BIT(1)
  237. /* EMAC_SGMII_PHY_TX_PWR_CTRL */
  238. #define L0_TX_EN BIT(5)
  239. #define L0_CLKBUF_EN BIT(4)
  240. #define L0_TRAN_BIAS_EN BIT(1)
  241. /* EMAC_SGMII_PHY_LANE_CTRL1 */
  242. #define L0_RX_EQUALIZE_ENABLE BIT(6)
  243. #define L0_RESET_TSYNC_EN BIT(4)
  244. #define L0_DRV_LVL(x) ((x) & 0xf)
  245. /* EMAC_SGMII_PHY_AUTONEG_CFG2 */
  246. #define FORCE_AN_TX_CFG BIT(5)
  247. #define FORCE_AN_RX_CFG BIT(4)
  248. #define AN_ENABLE BIT(0)
  249. /* EMAC_SGMII_PHY_SPEED_CFG1 */
  250. #define DUPLEX_MODE BIT(4)
  251. #define SPDMODE_1000 BIT(1)
  252. #define SPDMODE_100 BIT(0)
  253. #define SPDMODE_10 0
  254. #define SPDMODE_BMSK 3
  255. #define SPDMODE_SHFT 0
  256. /* EMAC_SGMII_PHY_POW_DWN_CTRL0 */
  257. #define PWRDN_B BIT(0)
  258. #define CDR_MAX_CNT(x) ((x) & 0xff)
  259. /* EMAC_QSERDES_TX_BIST_MODE_LANENO */
  260. #define BIST_LANE_NUMBER(x) (((x) & 3) << 5)
  261. #define BISTMODE(x) ((x) & 0x1f)
  262. /* EMAC_QSERDES_COM_PLLLOCK_CMPx */
  263. #define PLLLOCK_CMP(x) ((x) & 0xff)
  264. /* EMAC_SGMII_PHY_RESET_CTRL */
  265. #define PHY_SW_RESET BIT(0)
  266. /* EMAC_SGMII_PHY_IRQ_CMD */
  267. #define IRQ_GLOBAL_CLEAR BIT(0)
  268. /* EMAC_SGMII_PHY_INTERRUPT_MASK */
  269. #define DECODE_CODE_ERR BIT(7)
  270. #define DECODE_DISP_ERR BIT(6)
  271. #define PLL_UNLOCK BIT(5)
  272. #define AN_ILLEGAL_TERM BIT(4)
  273. #define SYNC_FAIL BIT(3)
  274. #define AN_START BIT(2)
  275. #define AN_END BIT(1)
  276. #define AN_REQUEST BIT(0)
  277. #define SGMII_PHY_IRQ_CLR_WAIT_TIME 10
  278. #define SGMII_PHY_INTERRUPT_ERR (\
  279. DECODE_CODE_ERR |\
  280. DECODE_DISP_ERR)
  281. #define SGMII_ISR_AN_MASK (\
  282. AN_REQUEST |\
  283. AN_START |\
  284. AN_END |\
  285. AN_ILLEGAL_TERM |\
  286. PLL_UNLOCK |\
  287. SYNC_FAIL)
  288. #define SGMII_ISR_MASK (\
  289. SGMII_PHY_INTERRUPT_ERR |\
  290. SGMII_ISR_AN_MASK)
  291. /* SGMII TX_CONFIG */
  292. #define TXCFG_LINK 0x8000
  293. #define TXCFG_MODE_BMSK 0x1c00
  294. #define TXCFG_1000_FULL 0x1800
  295. #define TXCFG_100_FULL 0x1400
  296. #define TXCFG_100_HALF 0x0400
  297. #define TXCFG_10_FULL 0x1000
  298. #define TXCFG_10_HALF 0x0000
  299. #define SERDES_START_WAIT_TIMES 100
  300. struct emac_reg_write {
  301. unsigned int offset;
  302. u32 val;
  303. };
  304. static void emac_reg_write_all(void __iomem *base,
  305. const struct emac_reg_write *itr, size_t size)
  306. {
  307. size_t i;
  308. for (i = 0; i < size; ++itr, ++i)
  309. writel(itr->val, base + itr->offset);
  310. }
  311. static const struct emac_reg_write physical_coding_sublayer_programming_v1[] = {
  312. {EMAC_SGMII_PHY_CDR_CTRL0, CDR_MAX_CNT(15)},
  313. {EMAC_SGMII_PHY_POW_DWN_CTRL0, PWRDN_B},
  314. {EMAC_SGMII_PHY_CMN_PWR_CTRL,
  315. BIAS_EN | SYSCLK_EN | CLKBUF_L_EN | PLL_TXCLK_EN | PLL_RXCLK_EN},
  316. {EMAC_SGMII_PHY_TX_PWR_CTRL, L0_TX_EN | L0_CLKBUF_EN | L0_TRAN_BIAS_EN},
  317. {EMAC_SGMII_PHY_RX_PWR_CTRL,
  318. L0_RX_SIGDET_EN | L0_RX_TERM_MODE(1) | L0_RX_I_EN},
  319. {EMAC_SGMII_PHY_CMN_PWR_CTRL,
  320. BIAS_EN | PLL_EN | SYSCLK_EN | CLKBUF_L_EN | PLL_TXCLK_EN |
  321. PLL_RXCLK_EN},
  322. {EMAC_SGMII_PHY_LANE_CTRL1,
  323. L0_RX_EQUALIZE_ENABLE | L0_RESET_TSYNC_EN | L0_DRV_LVL(15)},
  324. };
  325. static const struct emac_reg_write sysclk_refclk_setting[] = {
  326. {EMAC_QSERDES_COM_SYSCLK_EN_SEL, SYSCLK_SEL_CMOS},
  327. {EMAC_QSERDES_COM_SYS_CLK_CTRL, SYSCLK_CM | SYSCLK_AC_COUPLE},
  328. };
  329. static const struct emac_reg_write pll_setting[] = {
  330. {EMAC_QSERDES_COM_PLL_IP_SETI, PLL_IPSETI(1)},
  331. {EMAC_QSERDES_COM_PLL_CP_SETI, PLL_CPSETI(59)},
  332. {EMAC_QSERDES_COM_PLL_IP_SETP, PLL_IPSETP(10)},
  333. {EMAC_QSERDES_COM_PLL_CP_SETP, PLL_CPSETP(9)},
  334. {EMAC_QSERDES_COM_PLL_CRCTRL, PLL_RCTRL(15) | PLL_CCTRL(11)},
  335. {EMAC_QSERDES_COM_PLL_CNTRL, OCP_EN | PLL_DIV_FFEN | PLL_DIV_ORD},
  336. {EMAC_QSERDES_COM_DEC_START1, DEC_START1_MUX | DEC_START1(2)},
  337. {EMAC_QSERDES_COM_DEC_START2, DEC_START2_MUX | DEC_START2},
  338. {EMAC_QSERDES_COM_DIV_FRAC_START1,
  339. DIV_FRAC_START_MUX | DIV_FRAC_START(85)},
  340. {EMAC_QSERDES_COM_DIV_FRAC_START2,
  341. DIV_FRAC_START_MUX | DIV_FRAC_START(42)},
  342. {EMAC_QSERDES_COM_DIV_FRAC_START3,
  343. DIV_FRAC_START3_MUX | DIV_FRAC_START3(3)},
  344. {EMAC_QSERDES_COM_PLLLOCK_CMP1, PLLLOCK_CMP(43)},
  345. {EMAC_QSERDES_COM_PLLLOCK_CMP2, PLLLOCK_CMP(104)},
  346. {EMAC_QSERDES_COM_PLLLOCK_CMP3, PLLLOCK_CMP(0)},
  347. {EMAC_QSERDES_COM_PLLLOCK_CMP_EN, PLLLOCK_CMP_EN},
  348. {EMAC_QSERDES_COM_RESETSM_CNTRL, FRQ_TUNE_MODE},
  349. };
  350. static const struct emac_reg_write cdr_setting[] = {
  351. {EMAC_QSERDES_RX_CDR_CONTROL,
  352. SECONDORDERENABLE | FIRSTORDER_THRESH(3) | SECONDORDERGAIN(2)},
  353. {EMAC_QSERDES_RX_CDR_CONTROL2,
  354. SECONDORDERENABLE | FIRSTORDER_THRESH(3) | SECONDORDERGAIN(4)},
  355. };
  356. static const struct emac_reg_write tx_rx_setting[] = {
  357. {EMAC_QSERDES_TX_BIST_MODE_LANENO, 0},
  358. {EMAC_QSERDES_TX_TX_DRV_LVL, TX_DRV_LVL_MUX | TX_DRV_LVL(15)},
  359. {EMAC_QSERDES_TX_TRAN_DRVR_EMP_EN, EMP_EN_MUX | EMP_EN},
  360. {EMAC_QSERDES_TX_TX_EMP_POST1_LVL,
  361. TX_EMP_POST1_LVL_MUX | TX_EMP_POST1_LVL(1)},
  362. {EMAC_QSERDES_RX_RX_EQ_GAIN12, RX_EQ_GAIN2(15) | RX_EQ_GAIN1(15)},
  363. {EMAC_QSERDES_TX_LANE_MODE, LANE_MODE(8)},
  364. };
  365. static const struct emac_reg_write sgmii_v2_laned[] = {
  366. /* CDR Settings */
  367. {EMAC_SGMII_LN_UCDR_FO_GAIN_MODE0,
  368. UCDR_STEP_BY_TWO_MODE0 | UCDR_xO_GAIN_MODE(10)},
  369. {EMAC_SGMII_LN_UCDR_SO_GAIN_MODE0, UCDR_xO_GAIN_MODE(0)},
  370. {EMAC_SGMII_LN_UCDR_SO_CONFIG, UCDR_ENABLE | UCDR_SO_SATURATION(12)},
  371. /* TX/RX Settings */
  372. {EMAC_SGMII_LN_RX_EN_SIGNAL, SIGDET_LP_BYP_PS4 | SIGDET_EN_PS0_TO_PS2},
  373. {EMAC_SGMII_LN_DRVR_CTRL0, TXVAL_VALID_INIT | KR_PCIGEN3_MODE},
  374. {EMAC_SGMII_LN_DRVR_TAP_EN, MAIN_EN},
  375. {EMAC_SGMII_LN_TX_MARGINING, TX_MARGINING_MUX | TX_MARGINING(25)},
  376. {EMAC_SGMII_LN_TX_PRE, TX_PRE_MUX},
  377. {EMAC_SGMII_LN_TX_POST, TX_POST_MUX},
  378. {EMAC_SGMII_LN_CML_CTRL_MODE0,
  379. CML_GEAR_MODE(1) | CML2CMOS_IBOOST_MODE(1)},
  380. {EMAC_SGMII_LN_MIXER_CTRL_MODE0,
  381. MIXER_LOADB_MODE(12) | MIXER_DATARATE_MODE(1)},
  382. {EMAC_SGMII_LN_VGA_INITVAL, VGA_THRESH_DFE(31)},
  383. {EMAC_SGMII_LN_SIGDET_ENABLES,
  384. SIGDET_LP_BYP_PS0_TO_PS2 | SIGDET_FLT_BYP},
  385. {EMAC_SGMII_LN_SIGDET_CNTRL, SIGDET_LVL(8)},
  386. {EMAC_SGMII_LN_SIGDET_DEGLITCH_CNTRL, SIGDET_DEGLITCH_CTRL(4)},
  387. {EMAC_SGMII_LN_RX_MISC_CNTRL0, 0},
  388. {EMAC_SGMII_LN_DRVR_LOGIC_CLKDIV,
  389. DRVR_LOGIC_CLK_EN | DRVR_LOGIC_CLK_DIV(4)},
  390. {EMAC_SGMII_LN_PARALLEL_RATE, PARALLEL_RATE_MODE0(1)},
  391. {EMAC_SGMII_LN_TX_BAND_MODE, BAND_MODE0(2)},
  392. {EMAC_SGMII_LN_RX_BAND, BAND_MODE0(3)},
  393. {EMAC_SGMII_LN_LANE_MODE, LANE_MODE(26)},
  394. {EMAC_SGMII_LN_RX_RCVR_PATH1_MODE0, CDR_PD_SEL_MODE0(3)},
  395. {EMAC_SGMII_LN_RSM_CONFIG, BYPASS_RSM_SAMP_CAL | BYPASS_RSM_DLL_CAL},
  396. };
  397. static const struct emac_reg_write physical_coding_sublayer_programming_v2[] = {
  398. {EMAC_SGMII_PHY_POW_DWN_CTRL0, PWRDN_B},
  399. {EMAC_SGMII_PHY_CDR_CTRL0, CDR_MAX_CNT(15)},
  400. {EMAC_SGMII_PHY_TX_PWR_CTRL, 0},
  401. {EMAC_SGMII_PHY_LANE_CTRL1, L0_RX_EQUALIZE_ENABLE},
  402. };
  403. static int emac_sgmii_link_init(struct emac_adapter *adpt)
  404. {
  405. struct phy_device *phydev = adpt->phydev;
  406. struct emac_phy *phy = &adpt->phy;
  407. u32 val;
  408. val = readl(phy->base + EMAC_SGMII_PHY_AUTONEG_CFG2);
  409. if (phydev->autoneg == AUTONEG_ENABLE) {
  410. val &= ~(FORCE_AN_RX_CFG | FORCE_AN_TX_CFG);
  411. val |= AN_ENABLE;
  412. writel(val, phy->base + EMAC_SGMII_PHY_AUTONEG_CFG2);
  413. } else {
  414. u32 speed_cfg;
  415. switch (phydev->speed) {
  416. case SPEED_10:
  417. speed_cfg = SPDMODE_10;
  418. break;
  419. case SPEED_100:
  420. speed_cfg = SPDMODE_100;
  421. break;
  422. case SPEED_1000:
  423. speed_cfg = SPDMODE_1000;
  424. break;
  425. default:
  426. return -EINVAL;
  427. }
  428. if (phydev->duplex == DUPLEX_FULL)
  429. speed_cfg |= DUPLEX_MODE;
  430. val &= ~AN_ENABLE;
  431. writel(speed_cfg, phy->base + EMAC_SGMII_PHY_SPEED_CFG1);
  432. writel(val, phy->base + EMAC_SGMII_PHY_AUTONEG_CFG2);
  433. }
  434. return 0;
  435. }
  436. static int emac_sgmii_irq_clear(struct emac_adapter *adpt, u32 irq_bits)
  437. {
  438. struct emac_phy *phy = &adpt->phy;
  439. u32 status;
  440. writel_relaxed(irq_bits, phy->base + EMAC_SGMII_PHY_INTERRUPT_CLEAR);
  441. writel_relaxed(IRQ_GLOBAL_CLEAR, phy->base + EMAC_SGMII_PHY_IRQ_CMD);
  442. /* Ensure interrupt clear command is written to HW */
  443. wmb();
  444. /* After set the IRQ_GLOBAL_CLEAR bit, the status clearing must
  445. * be confirmed before clearing the bits in other registers.
  446. * It takes a few cycles for hw to clear the interrupt status.
  447. */
  448. if (readl_poll_timeout_atomic(phy->base +
  449. EMAC_SGMII_PHY_INTERRUPT_STATUS,
  450. status, !(status & irq_bits), 1,
  451. SGMII_PHY_IRQ_CLR_WAIT_TIME)) {
  452. netdev_err(adpt->netdev,
  453. "error: failed clear SGMII irq: status:0x%x bits:0x%x\n",
  454. status, irq_bits);
  455. return -EIO;
  456. }
  457. /* Finalize clearing procedure */
  458. writel_relaxed(0, phy->base + EMAC_SGMII_PHY_IRQ_CMD);
  459. writel_relaxed(0, phy->base + EMAC_SGMII_PHY_INTERRUPT_CLEAR);
  460. /* Ensure that clearing procedure finalization is written to HW */
  461. wmb();
  462. return 0;
  463. }
  464. int emac_sgmii_init_v1(struct emac_adapter *adpt)
  465. {
  466. struct emac_phy *phy = &adpt->phy;
  467. unsigned int i;
  468. int ret;
  469. ret = emac_sgmii_link_init(adpt);
  470. if (ret)
  471. return ret;
  472. emac_reg_write_all(phy->base, physical_coding_sublayer_programming_v1,
  473. ARRAY_SIZE(physical_coding_sublayer_programming_v1));
  474. emac_reg_write_all(phy->base, sysclk_refclk_setting,
  475. ARRAY_SIZE(sysclk_refclk_setting));
  476. emac_reg_write_all(phy->base, pll_setting, ARRAY_SIZE(pll_setting));
  477. emac_reg_write_all(phy->base, cdr_setting, ARRAY_SIZE(cdr_setting));
  478. emac_reg_write_all(phy->base, tx_rx_setting,
  479. ARRAY_SIZE(tx_rx_setting));
  480. /* Power up the Ser/Des engine */
  481. writel(SERDES_START, phy->base + EMAC_SGMII_PHY_SERDES_START);
  482. for (i = 0; i < SERDES_START_WAIT_TIMES; i++) {
  483. if (readl(phy->base + EMAC_QSERDES_COM_RESET_SM) & READY)
  484. break;
  485. usleep_range(100, 200);
  486. }
  487. if (i == SERDES_START_WAIT_TIMES) {
  488. netdev_err(adpt->netdev, "error: ser/des failed to start\n");
  489. return -EIO;
  490. }
  491. /* Mask out all the SGMII Interrupt */
  492. writel(0, phy->base + EMAC_SGMII_PHY_INTERRUPT_MASK);
  493. emac_sgmii_irq_clear(adpt, SGMII_PHY_INTERRUPT_ERR);
  494. return 0;
  495. }
  496. int emac_sgmii_init_v2(struct emac_adapter *adpt)
  497. {
  498. struct emac_phy *phy = &adpt->phy;
  499. void __iomem *phy_regs = phy->base;
  500. void __iomem *laned = phy->digital;
  501. unsigned int i;
  502. u32 lnstatus;
  503. int ret;
  504. ret = emac_sgmii_link_init(adpt);
  505. if (ret)
  506. return ret;
  507. /* PCS lane-x init */
  508. emac_reg_write_all(phy->base, physical_coding_sublayer_programming_v2,
  509. ARRAY_SIZE(physical_coding_sublayer_programming_v2));
  510. /* SGMII lane-x init */
  511. emac_reg_write_all(phy->digital,
  512. sgmii_v2_laned, ARRAY_SIZE(sgmii_v2_laned));
  513. /* Power up PCS and start reset lane state machine */
  514. writel(0, phy_regs + EMAC_SGMII_PHY_RESET_CTRL);
  515. writel(1, laned + SGMII_LN_RSM_START);
  516. /* Wait for c_ready assertion */
  517. for (i = 0; i < SERDES_START_WAIT_TIMES; i++) {
  518. lnstatus = readl(phy_regs + SGMII_PHY_LN_LANE_STATUS);
  519. if (lnstatus & BIT(1))
  520. break;
  521. usleep_range(100, 200);
  522. }
  523. if (i == SERDES_START_WAIT_TIMES) {
  524. netdev_err(adpt->netdev, "SGMII failed to start\n");
  525. return -EIO;
  526. }
  527. /* Disable digital and SERDES loopback */
  528. writel(0, phy_regs + SGMII_PHY_LN_BIST_GEN0);
  529. writel(0, phy_regs + SGMII_PHY_LN_BIST_GEN2);
  530. writel(0, phy_regs + SGMII_PHY_LN_CDR_CTRL1);
  531. /* Mask out all the SGMII Interrupt */
  532. writel(0, phy_regs + EMAC_SGMII_PHY_INTERRUPT_MASK);
  533. emac_sgmii_irq_clear(adpt, SGMII_PHY_INTERRUPT_ERR);
  534. return 0;
  535. }
  536. static void emac_sgmii_reset_prepare(struct emac_adapter *adpt)
  537. {
  538. struct emac_phy *phy = &adpt->phy;
  539. u32 val;
  540. /* Reset PHY */
  541. val = readl(phy->base + EMAC_EMAC_WRAPPER_CSR2);
  542. writel(((val & ~PHY_RESET) | PHY_RESET), phy->base +
  543. EMAC_EMAC_WRAPPER_CSR2);
  544. /* Ensure phy-reset command is written to HW before the release cmd */
  545. msleep(50);
  546. val = readl(phy->base + EMAC_EMAC_WRAPPER_CSR2);
  547. writel((val & ~PHY_RESET), phy->base + EMAC_EMAC_WRAPPER_CSR2);
  548. /* Ensure phy-reset release command is written to HW before initializing
  549. * SGMII
  550. */
  551. msleep(50);
  552. }
  553. void emac_sgmii_reset(struct emac_adapter *adpt)
  554. {
  555. int ret;
  556. clk_set_rate(adpt->clk[EMAC_CLK_HIGH_SPEED], 19200000);
  557. emac_sgmii_reset_prepare(adpt);
  558. ret = adpt->phy.initialize(adpt);
  559. if (ret)
  560. netdev_err(adpt->netdev,
  561. "could not reinitialize internal PHY (error=%i)\n",
  562. ret);
  563. clk_set_rate(adpt->clk[EMAC_CLK_HIGH_SPEED], 125000000);
  564. }
  565. static int emac_sgmii_acpi_match(struct device *dev, void *data)
  566. {
  567. static const struct acpi_device_id match_table[] = {
  568. {
  569. .id = "QCOM8071",
  570. .driver_data = (kernel_ulong_t)emac_sgmii_init_v2,
  571. },
  572. {}
  573. };
  574. const struct acpi_device_id *id = acpi_match_device(match_table, dev);
  575. emac_sgmii_initialize *initialize = data;
  576. if (id)
  577. *initialize = (emac_sgmii_initialize)id->driver_data;
  578. return !!id;
  579. }
  580. static const struct of_device_id emac_sgmii_dt_match[] = {
  581. {
  582. .compatible = "qcom,fsm9900-emac-sgmii",
  583. .data = emac_sgmii_init_v1,
  584. },
  585. {
  586. .compatible = "qcom,qdf2432-emac-sgmii",
  587. .data = emac_sgmii_init_v2,
  588. },
  589. {}
  590. };
  591. int emac_sgmii_config(struct platform_device *pdev, struct emac_adapter *adpt)
  592. {
  593. struct platform_device *sgmii_pdev = NULL;
  594. struct emac_phy *phy = &adpt->phy;
  595. struct resource *res;
  596. int ret;
  597. if (has_acpi_companion(&pdev->dev)) {
  598. struct device *dev;
  599. dev = device_find_child(&pdev->dev, &phy->initialize,
  600. emac_sgmii_acpi_match);
  601. if (!dev) {
  602. dev_err(&pdev->dev, "cannot find internal phy node\n");
  603. return -ENODEV;
  604. }
  605. sgmii_pdev = to_platform_device(dev);
  606. } else {
  607. const struct of_device_id *match;
  608. struct device_node *np;
  609. np = of_parse_phandle(pdev->dev.of_node, "internal-phy", 0);
  610. if (!np) {
  611. dev_err(&pdev->dev, "missing internal-phy property\n");
  612. return -ENODEV;
  613. }
  614. sgmii_pdev = of_find_device_by_node(np);
  615. if (!sgmii_pdev) {
  616. dev_err(&pdev->dev, "invalid internal-phy property\n");
  617. return -ENODEV;
  618. }
  619. match = of_match_device(emac_sgmii_dt_match, &sgmii_pdev->dev);
  620. if (!match) {
  621. dev_err(&pdev->dev, "unrecognized internal phy node\n");
  622. ret = -ENODEV;
  623. goto error_put_device;
  624. }
  625. phy->initialize = (emac_sgmii_initialize)match->data;
  626. }
  627. /* Base address is the first address */
  628. res = platform_get_resource(sgmii_pdev, IORESOURCE_MEM, 0);
  629. if (!res) {
  630. ret = -EINVAL;
  631. goto error_put_device;
  632. }
  633. phy->base = ioremap(res->start, resource_size(res));
  634. if (!phy->base) {
  635. ret = -ENOMEM;
  636. goto error_put_device;
  637. }
  638. /* v2 SGMII has a per-lane digital digital, so parse it if it exists */
  639. res = platform_get_resource(sgmii_pdev, IORESOURCE_MEM, 1);
  640. if (res) {
  641. phy->digital = ioremap(res->start, resource_size(res));
  642. if (!phy->digital) {
  643. ret = -ENOMEM;
  644. goto error_unmap_base;
  645. }
  646. }
  647. ret = phy->initialize(adpt);
  648. if (ret)
  649. goto error;
  650. /* We've remapped the addresses, so we don't need the device any
  651. * more. of_find_device_by_node() says we should release it.
  652. */
  653. put_device(&sgmii_pdev->dev);
  654. return 0;
  655. error:
  656. if (phy->digital)
  657. iounmap(phy->digital);
  658. error_unmap_base:
  659. iounmap(phy->base);
  660. error_put_device:
  661. put_device(&sgmii_pdev->dev);
  662. return ret;
  663. }