phy-qcom-qmp.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. /*
  2. * Copyright (c) 2017, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/clk-provider.h>
  16. #include <linux/delay.h>
  17. #include <linux/err.h>
  18. #include <linux/io.h>
  19. #include <linux/iopoll.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/of_address.h>
  25. #include <linux/phy/phy.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/regulator/consumer.h>
  28. #include <linux/reset.h>
  29. #include <linux/slab.h>
  30. #include <dt-bindings/phy/phy.h>
  31. /* QMP PHY QSERDES COM registers */
  32. #define QSERDES_COM_BG_TIMER 0x00c
  33. #define QSERDES_COM_SSC_EN_CENTER 0x010
  34. #define QSERDES_COM_SSC_ADJ_PER1 0x014
  35. #define QSERDES_COM_SSC_ADJ_PER2 0x018
  36. #define QSERDES_COM_SSC_PER1 0x01c
  37. #define QSERDES_COM_SSC_PER2 0x020
  38. #define QSERDES_COM_SSC_STEP_SIZE1 0x024
  39. #define QSERDES_COM_SSC_STEP_SIZE2 0x028
  40. #define QSERDES_COM_BIAS_EN_CLKBUFLR_EN 0x034
  41. #define QSERDES_COM_CLK_ENABLE1 0x038
  42. #define QSERDES_COM_SYS_CLK_CTRL 0x03c
  43. #define QSERDES_COM_SYSCLK_BUF_ENABLE 0x040
  44. #define QSERDES_COM_PLL_IVCO 0x048
  45. #define QSERDES_COM_LOCK_CMP1_MODE0 0x04c
  46. #define QSERDES_COM_LOCK_CMP2_MODE0 0x050
  47. #define QSERDES_COM_LOCK_CMP3_MODE0 0x054
  48. #define QSERDES_COM_LOCK_CMP1_MODE1 0x058
  49. #define QSERDES_COM_LOCK_CMP2_MODE1 0x05c
  50. #define QSERDES_COM_LOCK_CMP3_MODE1 0x060
  51. #define QSERDES_COM_BG_TRIM 0x070
  52. #define QSERDES_COM_CLK_EP_DIV 0x074
  53. #define QSERDES_COM_CP_CTRL_MODE0 0x078
  54. #define QSERDES_COM_CP_CTRL_MODE1 0x07c
  55. #define QSERDES_COM_PLL_RCTRL_MODE0 0x084
  56. #define QSERDES_COM_PLL_RCTRL_MODE1 0x088
  57. #define QSERDES_COM_PLL_CCTRL_MODE0 0x090
  58. #define QSERDES_COM_PLL_CCTRL_MODE1 0x094
  59. #define QSERDES_COM_BIAS_EN_CTRL_BY_PSM 0x0a8
  60. #define QSERDES_COM_SYSCLK_EN_SEL 0x0ac
  61. #define QSERDES_COM_RESETSM_CNTRL 0x0b4
  62. #define QSERDES_COM_RESTRIM_CTRL 0x0bc
  63. #define QSERDES_COM_RESCODE_DIV_NUM 0x0c4
  64. #define QSERDES_COM_LOCK_CMP_EN 0x0c8
  65. #define QSERDES_COM_LOCK_CMP_CFG 0x0cc
  66. #define QSERDES_COM_DEC_START_MODE0 0x0d0
  67. #define QSERDES_COM_DEC_START_MODE1 0x0d4
  68. #define QSERDES_COM_DIV_FRAC_START1_MODE0 0x0dc
  69. #define QSERDES_COM_DIV_FRAC_START2_MODE0 0x0e0
  70. #define QSERDES_COM_DIV_FRAC_START3_MODE0 0x0e4
  71. #define QSERDES_COM_DIV_FRAC_START1_MODE1 0x0e8
  72. #define QSERDES_COM_DIV_FRAC_START2_MODE1 0x0ec
  73. #define QSERDES_COM_DIV_FRAC_START3_MODE1 0x0f0
  74. #define QSERDES_COM_INTEGLOOP_GAIN0_MODE0 0x108
  75. #define QSERDES_COM_INTEGLOOP_GAIN1_MODE0 0x10c
  76. #define QSERDES_COM_INTEGLOOP_GAIN0_MODE1 0x110
  77. #define QSERDES_COM_INTEGLOOP_GAIN1_MODE1 0x114
  78. #define QSERDES_COM_VCO_TUNE_CTRL 0x124
  79. #define QSERDES_COM_VCO_TUNE_MAP 0x128
  80. #define QSERDES_COM_VCO_TUNE1_MODE0 0x12c
  81. #define QSERDES_COM_VCO_TUNE2_MODE0 0x130
  82. #define QSERDES_COM_VCO_TUNE1_MODE1 0x134
  83. #define QSERDES_COM_VCO_TUNE2_MODE1 0x138
  84. #define QSERDES_COM_VCO_TUNE_TIMER1 0x144
  85. #define QSERDES_COM_VCO_TUNE_TIMER2 0x148
  86. #define QSERDES_COM_BG_CTRL 0x170
  87. #define QSERDES_COM_CLK_SELECT 0x174
  88. #define QSERDES_COM_HSCLK_SEL 0x178
  89. #define QSERDES_COM_CORECLK_DIV 0x184
  90. #define QSERDES_COM_CORE_CLK_EN 0x18c
  91. #define QSERDES_COM_C_READY_STATUS 0x190
  92. #define QSERDES_COM_CMN_CONFIG 0x194
  93. #define QSERDES_COM_SVS_MODE_CLK_SEL 0x19c
  94. #define QSERDES_COM_DEBUG_BUS0 0x1a0
  95. #define QSERDES_COM_DEBUG_BUS1 0x1a4
  96. #define QSERDES_COM_DEBUG_BUS2 0x1a8
  97. #define QSERDES_COM_DEBUG_BUS3 0x1ac
  98. #define QSERDES_COM_DEBUG_BUS_SEL 0x1b0
  99. #define QSERDES_COM_CORECLK_DIV_MODE1 0x1bc
  100. /* QMP PHY TX registers */
  101. #define QSERDES_TX_RES_CODE_LANE_OFFSET 0x054
  102. #define QSERDES_TX_DEBUG_BUS_SEL 0x064
  103. #define QSERDES_TX_HIGHZ_TRANSCEIVEREN_BIAS_DRVR_EN 0x068
  104. #define QSERDES_TX_LANE_MODE 0x094
  105. #define QSERDES_TX_RCV_DETECT_LVL_2 0x0ac
  106. /* QMP PHY RX registers */
  107. #define QSERDES_RX_UCDR_SO_GAIN_HALF 0x010
  108. #define QSERDES_RX_UCDR_SO_GAIN 0x01c
  109. #define QSERDES_RX_UCDR_FASTLOCK_FO_GAIN 0x040
  110. #define QSERDES_RX_UCDR_SO_SATURATION_AND_ENABLE 0x048
  111. #define QSERDES_RX_RX_TERM_BW 0x090
  112. #define QSERDES_RX_RX_EQ_GAIN1_LSB 0x0c4
  113. #define QSERDES_RX_RX_EQ_GAIN1_MSB 0x0c8
  114. #define QSERDES_RX_RX_EQ_GAIN2_LSB 0x0cc
  115. #define QSERDES_RX_RX_EQ_GAIN2_MSB 0x0d0
  116. #define QSERDES_RX_RX_EQU_ADAPTOR_CNTRL2 0x0d8
  117. #define QSERDES_RX_RX_EQU_ADAPTOR_CNTRL3 0x0dc
  118. #define QSERDES_RX_RX_EQU_ADAPTOR_CNTRL4 0x0e0
  119. #define QSERDES_RX_RX_EQ_OFFSET_ADAPTOR_CNTRL1 0x108
  120. #define QSERDES_RX_RX_OFFSET_ADAPTOR_CNTRL2 0x10c
  121. #define QSERDES_RX_SIGDET_ENABLES 0x110
  122. #define QSERDES_RX_SIGDET_CNTRL 0x114
  123. #define QSERDES_RX_SIGDET_LVL 0x118
  124. #define QSERDES_RX_SIGDET_DEGLITCH_CNTRL 0x11c
  125. #define QSERDES_RX_RX_BAND 0x120
  126. #define QSERDES_RX_RX_INTERFACE_MODE 0x12c
  127. /* QMP PHY PCS registers */
  128. #define QPHY_POWER_DOWN_CONTROL 0x04
  129. #define QPHY_TXDEEMPH_M6DB_V0 0x24
  130. #define QPHY_TXDEEMPH_M3P5DB_V0 0x28
  131. #define QPHY_ENDPOINT_REFCLK_DRIVE 0x54
  132. #define QPHY_RX_IDLE_DTCT_CNTRL 0x58
  133. #define QPHY_POWER_STATE_CONFIG1 0x60
  134. #define QPHY_POWER_STATE_CONFIG2 0x64
  135. #define QPHY_POWER_STATE_CONFIG4 0x6c
  136. #define QPHY_LOCK_DETECT_CONFIG1 0x80
  137. #define QPHY_LOCK_DETECT_CONFIG2 0x84
  138. #define QPHY_LOCK_DETECT_CONFIG3 0x88
  139. #define QPHY_PWRUP_RESET_DLY_TIME_AUXCLK 0xa0
  140. #define QPHY_LP_WAKEUP_DLY_TIME_AUXCLK 0xa4
  141. #define QPHY_PLL_LOCK_CHK_DLY_TIME_AUXCLK_LSB 0x1A8
  142. #define QPHY_OSC_DTCT_ACTIONS 0x1AC
  143. #define QPHY_RX_SIGDET_LVL 0x1D8
  144. #define QPHY_L1SS_WAKEUP_DLY_TIME_AUXCLK_LSB 0x1DC
  145. #define QPHY_L1SS_WAKEUP_DLY_TIME_AUXCLK_MSB 0x1E0
  146. /* QPHY_SW_RESET bit */
  147. #define SW_RESET BIT(0)
  148. /* QPHY_POWER_DOWN_CONTROL */
  149. #define SW_PWRDN BIT(0)
  150. #define REFCLK_DRV_DSBL BIT(1)
  151. /* QPHY_START_CONTROL bits */
  152. #define SERDES_START BIT(0)
  153. #define PCS_START BIT(1)
  154. #define PLL_READY_GATE_EN BIT(3)
  155. /* QPHY_PCS_STATUS bit */
  156. #define PHYSTATUS BIT(6)
  157. /* QPHY_COM_PCS_READY_STATUS bit */
  158. #define PCS_READY BIT(0)
  159. #define PHY_INIT_COMPLETE_TIMEOUT 1000
  160. #define POWER_DOWN_DELAY_US_MIN 10
  161. #define POWER_DOWN_DELAY_US_MAX 11
  162. #define MAX_PROP_NAME 32
  163. struct qmp_phy_init_tbl {
  164. unsigned int offset;
  165. unsigned int val;
  166. /*
  167. * register part of layout ?
  168. * if yes, then offset gives index in the reg-layout
  169. */
  170. int in_layout;
  171. };
  172. #define QMP_PHY_INIT_CFG(o, v) \
  173. { \
  174. .offset = o, \
  175. .val = v, \
  176. }
  177. #define QMP_PHY_INIT_CFG_L(o, v) \
  178. { \
  179. .offset = o, \
  180. .val = v, \
  181. .in_layout = 1, \
  182. }
  183. /* set of registers with offsets different per-PHY */
  184. enum qphy_reg_layout {
  185. /* Common block control registers */
  186. QPHY_COM_SW_RESET,
  187. QPHY_COM_POWER_DOWN_CONTROL,
  188. QPHY_COM_START_CONTROL,
  189. QPHY_COM_PCS_READY_STATUS,
  190. /* PCS registers */
  191. QPHY_PLL_LOCK_CHK_DLY_TIME,
  192. QPHY_FLL_CNTRL1,
  193. QPHY_FLL_CNTRL2,
  194. QPHY_FLL_CNT_VAL_L,
  195. QPHY_FLL_CNT_VAL_H_TOL,
  196. QPHY_FLL_MAN_CODE,
  197. QPHY_SW_RESET,
  198. QPHY_START_CTRL,
  199. QPHY_PCS_READY_STATUS,
  200. };
  201. static const unsigned int pciephy_regs_layout[] = {
  202. [QPHY_COM_SW_RESET] = 0x400,
  203. [QPHY_COM_POWER_DOWN_CONTROL] = 0x404,
  204. [QPHY_COM_START_CONTROL] = 0x408,
  205. [QPHY_COM_PCS_READY_STATUS] = 0x448,
  206. [QPHY_PLL_LOCK_CHK_DLY_TIME] = 0xa8,
  207. [QPHY_FLL_CNTRL1] = 0xc4,
  208. [QPHY_FLL_CNTRL2] = 0xc8,
  209. [QPHY_FLL_CNT_VAL_L] = 0xcc,
  210. [QPHY_FLL_CNT_VAL_H_TOL] = 0xd0,
  211. [QPHY_FLL_MAN_CODE] = 0xd4,
  212. [QPHY_SW_RESET] = 0x00,
  213. [QPHY_START_CTRL] = 0x08,
  214. [QPHY_PCS_READY_STATUS] = 0x174,
  215. };
  216. static const unsigned int usb3phy_regs_layout[] = {
  217. [QPHY_FLL_CNTRL1] = 0xc0,
  218. [QPHY_FLL_CNTRL2] = 0xc4,
  219. [QPHY_FLL_CNT_VAL_L] = 0xc8,
  220. [QPHY_FLL_CNT_VAL_H_TOL] = 0xcc,
  221. [QPHY_FLL_MAN_CODE] = 0xd0,
  222. [QPHY_SW_RESET] = 0x00,
  223. [QPHY_START_CTRL] = 0x08,
  224. [QPHY_PCS_READY_STATUS] = 0x17c,
  225. };
  226. static const struct qmp_phy_init_tbl msm8996_pcie_serdes_tbl[] = {
  227. QMP_PHY_INIT_CFG(QSERDES_COM_BIAS_EN_CLKBUFLR_EN, 0x1c),
  228. QMP_PHY_INIT_CFG(QSERDES_COM_CLK_ENABLE1, 0x10),
  229. QMP_PHY_INIT_CFG(QSERDES_COM_CLK_SELECT, 0x33),
  230. QMP_PHY_INIT_CFG(QSERDES_COM_CMN_CONFIG, 0x06),
  231. QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP_EN, 0x42),
  232. QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_MAP, 0x00),
  233. QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_TIMER1, 0xff),
  234. QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_TIMER2, 0x1f),
  235. QMP_PHY_INIT_CFG(QSERDES_COM_HSCLK_SEL, 0x01),
  236. QMP_PHY_INIT_CFG(QSERDES_COM_SVS_MODE_CLK_SEL, 0x01),
  237. QMP_PHY_INIT_CFG(QSERDES_COM_CORE_CLK_EN, 0x00),
  238. QMP_PHY_INIT_CFG(QSERDES_COM_CORECLK_DIV, 0x0a),
  239. QMP_PHY_INIT_CFG(QSERDES_COM_BG_TIMER, 0x09),
  240. QMP_PHY_INIT_CFG(QSERDES_COM_DEC_START_MODE0, 0x82),
  241. QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START3_MODE0, 0x03),
  242. QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START2_MODE0, 0x55),
  243. QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START1_MODE0, 0x55),
  244. QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP3_MODE0, 0x00),
  245. QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP2_MODE0, 0x1a),
  246. QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP1_MODE0, 0x0a),
  247. QMP_PHY_INIT_CFG(QSERDES_COM_CLK_SELECT, 0x33),
  248. QMP_PHY_INIT_CFG(QSERDES_COM_SYS_CLK_CTRL, 0x02),
  249. QMP_PHY_INIT_CFG(QSERDES_COM_SYSCLK_BUF_ENABLE, 0x1f),
  250. QMP_PHY_INIT_CFG(QSERDES_COM_SYSCLK_EN_SEL, 0x04),
  251. QMP_PHY_INIT_CFG(QSERDES_COM_CP_CTRL_MODE0, 0x0b),
  252. QMP_PHY_INIT_CFG(QSERDES_COM_PLL_RCTRL_MODE0, 0x16),
  253. QMP_PHY_INIT_CFG(QSERDES_COM_PLL_CCTRL_MODE0, 0x28),
  254. QMP_PHY_INIT_CFG(QSERDES_COM_INTEGLOOP_GAIN1_MODE0, 0x00),
  255. QMP_PHY_INIT_CFG(QSERDES_COM_INTEGLOOP_GAIN0_MODE0, 0x80),
  256. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_EN_CENTER, 0x01),
  257. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_PER1, 0x31),
  258. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_PER2, 0x01),
  259. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_ADJ_PER1, 0x02),
  260. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_ADJ_PER2, 0x00),
  261. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_STEP_SIZE1, 0x2f),
  262. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_STEP_SIZE2, 0x19),
  263. QMP_PHY_INIT_CFG(QSERDES_COM_RESCODE_DIV_NUM, 0x15),
  264. QMP_PHY_INIT_CFG(QSERDES_COM_BG_TRIM, 0x0f),
  265. QMP_PHY_INIT_CFG(QSERDES_COM_PLL_IVCO, 0x0f),
  266. QMP_PHY_INIT_CFG(QSERDES_COM_CLK_EP_DIV, 0x19),
  267. QMP_PHY_INIT_CFG(QSERDES_COM_CLK_ENABLE1, 0x10),
  268. QMP_PHY_INIT_CFG(QSERDES_COM_HSCLK_SEL, 0x00),
  269. QMP_PHY_INIT_CFG(QSERDES_COM_RESCODE_DIV_NUM, 0x40),
  270. };
  271. static const struct qmp_phy_init_tbl msm8996_pcie_tx_tbl[] = {
  272. QMP_PHY_INIT_CFG(QSERDES_TX_HIGHZ_TRANSCEIVEREN_BIAS_DRVR_EN, 0x45),
  273. QMP_PHY_INIT_CFG(QSERDES_TX_LANE_MODE, 0x06),
  274. };
  275. static const struct qmp_phy_init_tbl msm8996_pcie_rx_tbl[] = {
  276. QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_ENABLES, 0x1c),
  277. QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL2, 0x01),
  278. QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL3, 0x00),
  279. QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL4, 0xdb),
  280. QMP_PHY_INIT_CFG(QSERDES_RX_RX_BAND, 0x18),
  281. QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_SO_GAIN, 0x04),
  282. QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_SO_GAIN_HALF, 0x04),
  283. QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_SO_SATURATION_AND_ENABLE, 0x4b),
  284. QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_DEGLITCH_CNTRL, 0x14),
  285. QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_LVL, 0x19),
  286. };
  287. static const struct qmp_phy_init_tbl msm8996_pcie_pcs_tbl[] = {
  288. QMP_PHY_INIT_CFG(QPHY_RX_IDLE_DTCT_CNTRL, 0x4c),
  289. QMP_PHY_INIT_CFG(QPHY_PWRUP_RESET_DLY_TIME_AUXCLK, 0x00),
  290. QMP_PHY_INIT_CFG(QPHY_LP_WAKEUP_DLY_TIME_AUXCLK, 0x01),
  291. QMP_PHY_INIT_CFG_L(QPHY_PLL_LOCK_CHK_DLY_TIME, 0x05),
  292. QMP_PHY_INIT_CFG(QPHY_ENDPOINT_REFCLK_DRIVE, 0x05),
  293. QMP_PHY_INIT_CFG(QPHY_POWER_DOWN_CONTROL, 0x02),
  294. QMP_PHY_INIT_CFG(QPHY_POWER_STATE_CONFIG4, 0x00),
  295. QMP_PHY_INIT_CFG(QPHY_POWER_STATE_CONFIG1, 0xa3),
  296. QMP_PHY_INIT_CFG(QPHY_TXDEEMPH_M3P5DB_V0, 0x0e),
  297. };
  298. static const struct qmp_phy_init_tbl msm8996_usb3_serdes_tbl[] = {
  299. QMP_PHY_INIT_CFG(QSERDES_COM_SYSCLK_EN_SEL, 0x14),
  300. QMP_PHY_INIT_CFG(QSERDES_COM_BIAS_EN_CLKBUFLR_EN, 0x08),
  301. QMP_PHY_INIT_CFG(QSERDES_COM_CLK_SELECT, 0x30),
  302. QMP_PHY_INIT_CFG(QSERDES_COM_CMN_CONFIG, 0x06),
  303. QMP_PHY_INIT_CFG(QSERDES_COM_SVS_MODE_CLK_SEL, 0x01),
  304. QMP_PHY_INIT_CFG(QSERDES_COM_HSCLK_SEL, 0x00),
  305. QMP_PHY_INIT_CFG(QSERDES_COM_BG_TRIM, 0x0f),
  306. QMP_PHY_INIT_CFG(QSERDES_COM_PLL_IVCO, 0x0f),
  307. QMP_PHY_INIT_CFG(QSERDES_COM_SYS_CLK_CTRL, 0x04),
  308. /* PLL and Loop filter settings */
  309. QMP_PHY_INIT_CFG(QSERDES_COM_DEC_START_MODE0, 0x82),
  310. QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START1_MODE0, 0x55),
  311. QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START2_MODE0, 0x55),
  312. QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START3_MODE0, 0x03),
  313. QMP_PHY_INIT_CFG(QSERDES_COM_CP_CTRL_MODE0, 0x0b),
  314. QMP_PHY_INIT_CFG(QSERDES_COM_PLL_RCTRL_MODE0, 0x16),
  315. QMP_PHY_INIT_CFG(QSERDES_COM_PLL_CCTRL_MODE0, 0x28),
  316. QMP_PHY_INIT_CFG(QSERDES_COM_INTEGLOOP_GAIN0_MODE0, 0x80),
  317. QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_CTRL, 0x00),
  318. QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP1_MODE0, 0x15),
  319. QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP2_MODE0, 0x34),
  320. QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP3_MODE0, 0x00),
  321. QMP_PHY_INIT_CFG(QSERDES_COM_CORE_CLK_EN, 0x00),
  322. QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP_CFG, 0x00),
  323. QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_MAP, 0x00),
  324. QMP_PHY_INIT_CFG(QSERDES_COM_BG_TIMER, 0x0a),
  325. /* SSC settings */
  326. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_EN_CENTER, 0x01),
  327. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_PER1, 0x31),
  328. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_PER2, 0x01),
  329. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_ADJ_PER1, 0x00),
  330. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_ADJ_PER2, 0x00),
  331. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_STEP_SIZE1, 0xde),
  332. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_STEP_SIZE2, 0x07),
  333. };
  334. static const struct qmp_phy_init_tbl msm8996_usb3_tx_tbl[] = {
  335. QMP_PHY_INIT_CFG(QSERDES_TX_HIGHZ_TRANSCEIVEREN_BIAS_DRVR_EN, 0x45),
  336. QMP_PHY_INIT_CFG(QSERDES_TX_RCV_DETECT_LVL_2, 0x12),
  337. QMP_PHY_INIT_CFG(QSERDES_TX_LANE_MODE, 0x06),
  338. };
  339. static const struct qmp_phy_init_tbl msm8996_usb3_rx_tbl[] = {
  340. QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_FASTLOCK_FO_GAIN, 0x0b),
  341. QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_SO_GAIN, 0x04),
  342. QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL2, 0x02),
  343. QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL3, 0x4c),
  344. QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL4, 0xbb),
  345. QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQ_OFFSET_ADAPTOR_CNTRL1, 0x77),
  346. QMP_PHY_INIT_CFG(QSERDES_RX_RX_OFFSET_ADAPTOR_CNTRL2, 0x80),
  347. QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_CNTRL, 0x03),
  348. QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_LVL, 0x18),
  349. QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_DEGLITCH_CNTRL, 0x16),
  350. };
  351. static const struct qmp_phy_init_tbl msm8996_usb3_pcs_tbl[] = {
  352. /* FLL settings */
  353. QMP_PHY_INIT_CFG_L(QPHY_FLL_CNTRL2, 0x03),
  354. QMP_PHY_INIT_CFG_L(QPHY_FLL_CNTRL1, 0x02),
  355. QMP_PHY_INIT_CFG_L(QPHY_FLL_CNT_VAL_L, 0x09),
  356. QMP_PHY_INIT_CFG_L(QPHY_FLL_CNT_VAL_H_TOL, 0x42),
  357. QMP_PHY_INIT_CFG_L(QPHY_FLL_MAN_CODE, 0x85),
  358. /* Lock Det settings */
  359. QMP_PHY_INIT_CFG(QPHY_LOCK_DETECT_CONFIG1, 0xd1),
  360. QMP_PHY_INIT_CFG(QPHY_LOCK_DETECT_CONFIG2, 0x1f),
  361. QMP_PHY_INIT_CFG(QPHY_LOCK_DETECT_CONFIG3, 0x47),
  362. QMP_PHY_INIT_CFG(QPHY_POWER_STATE_CONFIG2, 0x08),
  363. };
  364. static const struct qmp_phy_init_tbl ipq8074_pcie_serdes_tbl[] = {
  365. QMP_PHY_INIT_CFG(QSERDES_COM_BIAS_EN_CLKBUFLR_EN, 0x18),
  366. QMP_PHY_INIT_CFG(QSERDES_COM_CLK_ENABLE1, 0x10),
  367. QMP_PHY_INIT_CFG(QSERDES_COM_BG_TRIM, 0xf),
  368. QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP_EN, 0x1),
  369. QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_MAP, 0x0),
  370. QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_TIMER1, 0x1f),
  371. QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_TIMER2, 0x3f),
  372. QMP_PHY_INIT_CFG(QSERDES_COM_CMN_CONFIG, 0x6),
  373. QMP_PHY_INIT_CFG(QSERDES_COM_PLL_IVCO, 0xf),
  374. QMP_PHY_INIT_CFG(QSERDES_COM_HSCLK_SEL, 0x0),
  375. QMP_PHY_INIT_CFG(QSERDES_COM_SVS_MODE_CLK_SEL, 0x1),
  376. QMP_PHY_INIT_CFG(QSERDES_COM_CORE_CLK_EN, 0x20),
  377. QMP_PHY_INIT_CFG(QSERDES_COM_CORECLK_DIV, 0xa),
  378. QMP_PHY_INIT_CFG(QSERDES_COM_RESETSM_CNTRL, 0x20),
  379. QMP_PHY_INIT_CFG(QSERDES_COM_BG_TIMER, 0xa),
  380. QMP_PHY_INIT_CFG(QSERDES_COM_SYSCLK_EN_SEL, 0xa),
  381. QMP_PHY_INIT_CFG(QSERDES_COM_DEC_START_MODE0, 0x82),
  382. QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START3_MODE0, 0x3),
  383. QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START2_MODE0, 0x55),
  384. QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START1_MODE0, 0x55),
  385. QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP3_MODE0, 0x0),
  386. QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP2_MODE0, 0xD),
  387. QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP1_MODE0, 0xD04),
  388. QMP_PHY_INIT_CFG(QSERDES_COM_CLK_SELECT, 0x33),
  389. QMP_PHY_INIT_CFG(QSERDES_COM_SYS_CLK_CTRL, 0x2),
  390. QMP_PHY_INIT_CFG(QSERDES_COM_SYSCLK_BUF_ENABLE, 0x1f),
  391. QMP_PHY_INIT_CFG(QSERDES_COM_CP_CTRL_MODE0, 0xb),
  392. QMP_PHY_INIT_CFG(QSERDES_COM_PLL_RCTRL_MODE0, 0x16),
  393. QMP_PHY_INIT_CFG(QSERDES_COM_PLL_CCTRL_MODE0, 0x28),
  394. QMP_PHY_INIT_CFG(QSERDES_COM_INTEGLOOP_GAIN1_MODE0, 0x0),
  395. QMP_PHY_INIT_CFG(QSERDES_COM_INTEGLOOP_GAIN0_MODE0, 0x80),
  396. QMP_PHY_INIT_CFG(QSERDES_COM_BIAS_EN_CTRL_BY_PSM, 0x1),
  397. QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_CTRL, 0xa),
  398. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_EN_CENTER, 0x1),
  399. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_PER1, 0x31),
  400. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_PER2, 0x1),
  401. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_ADJ_PER1, 0x2),
  402. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_ADJ_PER2, 0x0),
  403. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_STEP_SIZE1, 0x2f),
  404. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_STEP_SIZE2, 0x19),
  405. QMP_PHY_INIT_CFG(QSERDES_COM_CLK_EP_DIV, 0x19),
  406. QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_CNTRL, 0x7),
  407. };
  408. static const struct qmp_phy_init_tbl ipq8074_pcie_tx_tbl[] = {
  409. QMP_PHY_INIT_CFG(QSERDES_TX_HIGHZ_TRANSCEIVEREN_BIAS_DRVR_EN, 0x45),
  410. QMP_PHY_INIT_CFG(QSERDES_TX_LANE_MODE, 0x6),
  411. QMP_PHY_INIT_CFG(QSERDES_TX_RES_CODE_LANE_OFFSET, 0x2),
  412. QMP_PHY_INIT_CFG(QSERDES_TX_RCV_DETECT_LVL_2, 0x12),
  413. };
  414. static const struct qmp_phy_init_tbl ipq8074_pcie_rx_tbl[] = {
  415. QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_ENABLES, 0x1c),
  416. QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_DEGLITCH_CNTRL, 0x14),
  417. QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL2, 0x1),
  418. QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL3, 0x0),
  419. QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL4, 0xdb),
  420. QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_SO_SATURATION_AND_ENABLE, 0x4b),
  421. QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_SO_GAIN, 0x4),
  422. QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_SO_GAIN_HALF, 0x4),
  423. };
  424. static const struct qmp_phy_init_tbl ipq8074_pcie_pcs_tbl[] = {
  425. QMP_PHY_INIT_CFG(QPHY_ENDPOINT_REFCLK_DRIVE, 0x4),
  426. QMP_PHY_INIT_CFG(QPHY_OSC_DTCT_ACTIONS, 0x0),
  427. QMP_PHY_INIT_CFG(QPHY_PWRUP_RESET_DLY_TIME_AUXCLK, 0x40),
  428. QMP_PHY_INIT_CFG(QPHY_L1SS_WAKEUP_DLY_TIME_AUXCLK_MSB, 0x0),
  429. QMP_PHY_INIT_CFG(QPHY_L1SS_WAKEUP_DLY_TIME_AUXCLK_LSB, 0x40),
  430. QMP_PHY_INIT_CFG(QPHY_PLL_LOCK_CHK_DLY_TIME_AUXCLK_LSB, 0x0),
  431. QMP_PHY_INIT_CFG(QPHY_LP_WAKEUP_DLY_TIME_AUXCLK, 0x40),
  432. QMP_PHY_INIT_CFG_L(QPHY_PLL_LOCK_CHK_DLY_TIME, 0x73),
  433. QMP_PHY_INIT_CFG(QPHY_RX_SIGDET_LVL, 0x99),
  434. QMP_PHY_INIT_CFG(QPHY_TXDEEMPH_M6DB_V0, 0x15),
  435. QMP_PHY_INIT_CFG(QPHY_TXDEEMPH_M3P5DB_V0, 0xe),
  436. QMP_PHY_INIT_CFG_L(QPHY_SW_RESET, 0x0),
  437. QMP_PHY_INIT_CFG_L(QPHY_START_CTRL, 0x3),
  438. };
  439. /* struct qmp_phy_cfg - per-PHY initialization config */
  440. struct qmp_phy_cfg {
  441. /* phy-type - PCIE/UFS/USB */
  442. unsigned int type;
  443. /* number of lanes provided by phy */
  444. int nlanes;
  445. /* Init sequence for PHY blocks - serdes, tx, rx, pcs */
  446. const struct qmp_phy_init_tbl *serdes_tbl;
  447. int serdes_tbl_num;
  448. const struct qmp_phy_init_tbl *tx_tbl;
  449. int tx_tbl_num;
  450. const struct qmp_phy_init_tbl *rx_tbl;
  451. int rx_tbl_num;
  452. const struct qmp_phy_init_tbl *pcs_tbl;
  453. int pcs_tbl_num;
  454. /* clock ids to be requested */
  455. const char * const *clk_list;
  456. int num_clks;
  457. /* resets to be requested */
  458. const char * const *reset_list;
  459. int num_resets;
  460. /* regulators to be requested */
  461. const char * const *vreg_list;
  462. int num_vregs;
  463. /* array of registers with different offsets */
  464. const unsigned int *regs;
  465. unsigned int start_ctrl;
  466. unsigned int pwrdn_ctrl;
  467. unsigned int mask_pcs_ready;
  468. unsigned int mask_com_pcs_ready;
  469. /* true, if PHY has a separate PHY_COM control block */
  470. bool has_phy_com_ctrl;
  471. /* true, if PHY has a reset for individual lanes */
  472. bool has_lane_rst;
  473. /* true, if PHY needs delay after POWER_DOWN */
  474. bool has_pwrdn_delay;
  475. /* power_down delay in usec */
  476. int pwrdn_delay_min;
  477. int pwrdn_delay_max;
  478. };
  479. /**
  480. * struct qmp_phy - per-lane phy descriptor
  481. *
  482. * @phy: generic phy
  483. * @tx: iomapped memory space for lane's tx
  484. * @rx: iomapped memory space for lane's rx
  485. * @pcs: iomapped memory space for lane's pcs
  486. * @pipe_clk: pipe lock
  487. * @index: lane index
  488. * @qmp: QMP phy to which this lane belongs
  489. * @lane_rst: lane's reset controller
  490. */
  491. struct qmp_phy {
  492. struct phy *phy;
  493. void __iomem *tx;
  494. void __iomem *rx;
  495. void __iomem *pcs;
  496. struct clk *pipe_clk;
  497. unsigned int index;
  498. struct qcom_qmp *qmp;
  499. struct reset_control *lane_rst;
  500. };
  501. /**
  502. * struct qcom_qmp - structure holding QMP phy block attributes
  503. *
  504. * @dev: device
  505. * @serdes: iomapped memory space for phy's serdes
  506. *
  507. * @clks: array of clocks required by phy
  508. * @resets: array of resets required by phy
  509. * @vregs: regulator supplies bulk data
  510. *
  511. * @cfg: phy specific configuration
  512. * @phys: array of per-lane phy descriptors
  513. * @phy_mutex: mutex lock for PHY common block initialization
  514. * @init_count: phy common block initialization count
  515. */
  516. struct qcom_qmp {
  517. struct device *dev;
  518. void __iomem *serdes;
  519. struct clk **clks;
  520. struct reset_control **resets;
  521. struct regulator_bulk_data *vregs;
  522. const struct qmp_phy_cfg *cfg;
  523. struct qmp_phy **phys;
  524. struct mutex phy_mutex;
  525. int init_count;
  526. };
  527. static inline void qphy_setbits(void __iomem *base, u32 offset, u32 val)
  528. {
  529. u32 reg;
  530. reg = readl(base + offset);
  531. reg |= val;
  532. writel(reg, base + offset);
  533. /* ensure that above write is through */
  534. readl(base + offset);
  535. }
  536. static inline void qphy_clrbits(void __iomem *base, u32 offset, u32 val)
  537. {
  538. u32 reg;
  539. reg = readl(base + offset);
  540. reg &= ~val;
  541. writel(reg, base + offset);
  542. /* ensure that above write is through */
  543. readl(base + offset);
  544. }
  545. /* list of clocks required by phy */
  546. static const char * const msm8996_phy_clk_l[] = {
  547. "aux", "cfg_ahb", "ref",
  548. };
  549. /* list of resets */
  550. static const char * const msm8996_pciephy_reset_l[] = {
  551. "phy", "common", "cfg",
  552. };
  553. static const char * const msm8996_usb3phy_reset_l[] = {
  554. "phy", "common",
  555. };
  556. /* list of regulators */
  557. static const char * const msm8996_phy_vreg_l[] = {
  558. "vdda-phy", "vdda-pll",
  559. };
  560. static const struct qmp_phy_cfg msm8996_pciephy_cfg = {
  561. .type = PHY_TYPE_PCIE,
  562. .nlanes = 3,
  563. .serdes_tbl = msm8996_pcie_serdes_tbl,
  564. .serdes_tbl_num = ARRAY_SIZE(msm8996_pcie_serdes_tbl),
  565. .tx_tbl = msm8996_pcie_tx_tbl,
  566. .tx_tbl_num = ARRAY_SIZE(msm8996_pcie_tx_tbl),
  567. .rx_tbl = msm8996_pcie_rx_tbl,
  568. .rx_tbl_num = ARRAY_SIZE(msm8996_pcie_rx_tbl),
  569. .pcs_tbl = msm8996_pcie_pcs_tbl,
  570. .pcs_tbl_num = ARRAY_SIZE(msm8996_pcie_pcs_tbl),
  571. .clk_list = msm8996_phy_clk_l,
  572. .num_clks = ARRAY_SIZE(msm8996_phy_clk_l),
  573. .reset_list = msm8996_pciephy_reset_l,
  574. .num_resets = ARRAY_SIZE(msm8996_pciephy_reset_l),
  575. .vreg_list = msm8996_phy_vreg_l,
  576. .num_vregs = ARRAY_SIZE(msm8996_phy_vreg_l),
  577. .regs = pciephy_regs_layout,
  578. .start_ctrl = PCS_START | PLL_READY_GATE_EN,
  579. .pwrdn_ctrl = SW_PWRDN | REFCLK_DRV_DSBL,
  580. .mask_com_pcs_ready = PCS_READY,
  581. .has_phy_com_ctrl = true,
  582. .has_lane_rst = true,
  583. .has_pwrdn_delay = true,
  584. .pwrdn_delay_min = POWER_DOWN_DELAY_US_MIN,
  585. .pwrdn_delay_max = POWER_DOWN_DELAY_US_MAX,
  586. };
  587. static const struct qmp_phy_cfg msm8996_usb3phy_cfg = {
  588. .type = PHY_TYPE_USB3,
  589. .nlanes = 1,
  590. .serdes_tbl = msm8996_usb3_serdes_tbl,
  591. .serdes_tbl_num = ARRAY_SIZE(msm8996_usb3_serdes_tbl),
  592. .tx_tbl = msm8996_usb3_tx_tbl,
  593. .tx_tbl_num = ARRAY_SIZE(msm8996_usb3_tx_tbl),
  594. .rx_tbl = msm8996_usb3_rx_tbl,
  595. .rx_tbl_num = ARRAY_SIZE(msm8996_usb3_rx_tbl),
  596. .pcs_tbl = msm8996_usb3_pcs_tbl,
  597. .pcs_tbl_num = ARRAY_SIZE(msm8996_usb3_pcs_tbl),
  598. .clk_list = msm8996_phy_clk_l,
  599. .num_clks = ARRAY_SIZE(msm8996_phy_clk_l),
  600. .reset_list = msm8996_usb3phy_reset_l,
  601. .num_resets = ARRAY_SIZE(msm8996_usb3phy_reset_l),
  602. .vreg_list = msm8996_phy_vreg_l,
  603. .num_vregs = ARRAY_SIZE(msm8996_phy_vreg_l),
  604. .regs = usb3phy_regs_layout,
  605. .start_ctrl = SERDES_START | PCS_START,
  606. .pwrdn_ctrl = SW_PWRDN,
  607. .mask_pcs_ready = PHYSTATUS,
  608. };
  609. /* list of resets */
  610. static const char * const ipq8074_pciephy_reset_l[] = {
  611. "phy", "common",
  612. };
  613. static const struct qmp_phy_cfg ipq8074_pciephy_cfg = {
  614. .type = PHY_TYPE_PCIE,
  615. .nlanes = 1,
  616. .serdes_tbl = ipq8074_pcie_serdes_tbl,
  617. .serdes_tbl_num = ARRAY_SIZE(ipq8074_pcie_serdes_tbl),
  618. .tx_tbl = ipq8074_pcie_tx_tbl,
  619. .tx_tbl_num = ARRAY_SIZE(ipq8074_pcie_tx_tbl),
  620. .rx_tbl = ipq8074_pcie_rx_tbl,
  621. .rx_tbl_num = ARRAY_SIZE(ipq8074_pcie_rx_tbl),
  622. .pcs_tbl = ipq8074_pcie_pcs_tbl,
  623. .pcs_tbl_num = ARRAY_SIZE(ipq8074_pcie_pcs_tbl),
  624. .clk_list = NULL,
  625. .num_clks = 0,
  626. .reset_list = ipq8074_pciephy_reset_l,
  627. .num_resets = ARRAY_SIZE(ipq8074_pciephy_reset_l),
  628. .vreg_list = NULL,
  629. .num_vregs = 0,
  630. .regs = pciephy_regs_layout,
  631. .start_ctrl = SERDES_START | PCS_START,
  632. .pwrdn_ctrl = SW_PWRDN | REFCLK_DRV_DSBL,
  633. .mask_pcs_ready = PHYSTATUS,
  634. .has_phy_com_ctrl = false,
  635. .has_lane_rst = false,
  636. .has_pwrdn_delay = true,
  637. .pwrdn_delay_min = 995, /* us */
  638. .pwrdn_delay_max = 1005, /* us */
  639. };
  640. static void qcom_qmp_phy_configure(void __iomem *base,
  641. const unsigned int *regs,
  642. const struct qmp_phy_init_tbl tbl[],
  643. int num)
  644. {
  645. int i;
  646. const struct qmp_phy_init_tbl *t = tbl;
  647. if (!t)
  648. return;
  649. for (i = 0; i < num; i++, t++) {
  650. if (t->in_layout)
  651. writel(t->val, base + regs[t->offset]);
  652. else
  653. writel(t->val, base + t->offset);
  654. }
  655. }
  656. static int qcom_qmp_phy_poweron(struct phy *phy)
  657. {
  658. struct qmp_phy *qphy = phy_get_drvdata(phy);
  659. struct qcom_qmp *qmp = qphy->qmp;
  660. int num = qmp->cfg->num_vregs;
  661. int ret;
  662. dev_vdbg(&phy->dev, "Powering on QMP phy\n");
  663. /* turn on regulator supplies */
  664. ret = regulator_bulk_enable(num, qmp->vregs);
  665. if (ret) {
  666. dev_err(qmp->dev, "failed to enable regulators, err=%d\n", ret);
  667. return ret;
  668. }
  669. ret = clk_prepare_enable(qphy->pipe_clk);
  670. if (ret) {
  671. dev_err(qmp->dev, "pipe_clk enable failed, err=%d\n", ret);
  672. regulator_bulk_disable(num, qmp->vregs);
  673. return ret;
  674. }
  675. return 0;
  676. }
  677. static int qcom_qmp_phy_poweroff(struct phy *phy)
  678. {
  679. struct qmp_phy *qphy = phy_get_drvdata(phy);
  680. struct qcom_qmp *qmp = qphy->qmp;
  681. clk_disable_unprepare(qphy->pipe_clk);
  682. regulator_bulk_disable(qmp->cfg->num_vregs, qmp->vregs);
  683. return 0;
  684. }
  685. static int qcom_qmp_phy_com_init(struct qcom_qmp *qmp)
  686. {
  687. const struct qmp_phy_cfg *cfg = qmp->cfg;
  688. void __iomem *serdes = qmp->serdes;
  689. int ret, i;
  690. mutex_lock(&qmp->phy_mutex);
  691. if (qmp->init_count++) {
  692. mutex_unlock(&qmp->phy_mutex);
  693. return 0;
  694. }
  695. for (i = 0; i < cfg->num_resets; i++) {
  696. ret = reset_control_deassert(qmp->resets[i]);
  697. if (ret) {
  698. dev_err(qmp->dev, "%s reset deassert failed\n",
  699. qmp->cfg->reset_list[i]);
  700. goto err_rst;
  701. }
  702. }
  703. if (cfg->has_phy_com_ctrl)
  704. qphy_setbits(serdes, cfg->regs[QPHY_COM_POWER_DOWN_CONTROL],
  705. SW_PWRDN);
  706. /* Serdes configuration */
  707. qcom_qmp_phy_configure(serdes, cfg->regs, cfg->serdes_tbl,
  708. cfg->serdes_tbl_num);
  709. if (cfg->has_phy_com_ctrl) {
  710. void __iomem *status;
  711. unsigned int mask, val;
  712. qphy_clrbits(serdes, cfg->regs[QPHY_COM_SW_RESET], SW_RESET);
  713. qphy_setbits(serdes, cfg->regs[QPHY_COM_START_CONTROL],
  714. SERDES_START | PCS_START);
  715. status = serdes + cfg->regs[QPHY_COM_PCS_READY_STATUS];
  716. mask = cfg->mask_com_pcs_ready;
  717. ret = readl_poll_timeout(status, val, (val & mask), 10,
  718. PHY_INIT_COMPLETE_TIMEOUT);
  719. if (ret) {
  720. dev_err(qmp->dev,
  721. "phy common block init timed-out\n");
  722. goto err_rst;
  723. }
  724. }
  725. mutex_unlock(&qmp->phy_mutex);
  726. return 0;
  727. err_rst:
  728. while (--i >= 0)
  729. reset_control_assert(qmp->resets[i]);
  730. mutex_unlock(&qmp->phy_mutex);
  731. return ret;
  732. }
  733. static int qcom_qmp_phy_com_exit(struct qcom_qmp *qmp)
  734. {
  735. const struct qmp_phy_cfg *cfg = qmp->cfg;
  736. void __iomem *serdes = qmp->serdes;
  737. int i = cfg->num_resets;
  738. mutex_lock(&qmp->phy_mutex);
  739. if (--qmp->init_count) {
  740. mutex_unlock(&qmp->phy_mutex);
  741. return 0;
  742. }
  743. if (cfg->has_phy_com_ctrl) {
  744. qphy_setbits(serdes, cfg->regs[QPHY_COM_START_CONTROL],
  745. SERDES_START | PCS_START);
  746. qphy_clrbits(serdes, cfg->regs[QPHY_COM_SW_RESET],
  747. SW_RESET);
  748. qphy_setbits(serdes, cfg->regs[QPHY_COM_POWER_DOWN_CONTROL],
  749. SW_PWRDN);
  750. }
  751. while (--i >= 0)
  752. reset_control_assert(qmp->resets[i]);
  753. mutex_unlock(&qmp->phy_mutex);
  754. return 0;
  755. }
  756. /* PHY Initialization */
  757. static int qcom_qmp_phy_init(struct phy *phy)
  758. {
  759. struct qmp_phy *qphy = phy_get_drvdata(phy);
  760. struct qcom_qmp *qmp = qphy->qmp;
  761. const struct qmp_phy_cfg *cfg = qmp->cfg;
  762. void __iomem *tx = qphy->tx;
  763. void __iomem *rx = qphy->rx;
  764. void __iomem *pcs = qphy->pcs;
  765. void __iomem *status;
  766. unsigned int mask, val;
  767. int ret, i;
  768. dev_vdbg(qmp->dev, "Initializing QMP phy\n");
  769. for (i = 0; i < qmp->cfg->num_clks; i++) {
  770. ret = clk_prepare_enable(qmp->clks[i]);
  771. if (ret) {
  772. dev_err(qmp->dev, "failed to enable %s clk, err=%d\n",
  773. qmp->cfg->clk_list[i], ret);
  774. goto err_clk;
  775. }
  776. }
  777. ret = qcom_qmp_phy_com_init(qmp);
  778. if (ret)
  779. goto err_clk;
  780. if (cfg->has_lane_rst) {
  781. ret = reset_control_deassert(qphy->lane_rst);
  782. if (ret) {
  783. dev_err(qmp->dev, "lane%d reset deassert failed\n",
  784. qphy->index);
  785. goto err_lane_rst;
  786. }
  787. }
  788. /* Tx, Rx, and PCS configurations */
  789. qcom_qmp_phy_configure(tx, cfg->regs, cfg->tx_tbl, cfg->tx_tbl_num);
  790. qcom_qmp_phy_configure(rx, cfg->regs, cfg->rx_tbl, cfg->rx_tbl_num);
  791. qcom_qmp_phy_configure(pcs, cfg->regs, cfg->pcs_tbl, cfg->pcs_tbl_num);
  792. /*
  793. * Pull out PHY from POWER DOWN state.
  794. * This is active low enable signal to power-down PHY.
  795. */
  796. qphy_setbits(pcs, QPHY_POWER_DOWN_CONTROL, cfg->pwrdn_ctrl);
  797. if (cfg->has_pwrdn_delay)
  798. usleep_range(cfg->pwrdn_delay_min, cfg->pwrdn_delay_max);
  799. /* start SerDes and Phy-Coding-Sublayer */
  800. qphy_setbits(pcs, cfg->regs[QPHY_START_CTRL], cfg->start_ctrl);
  801. /* Pull PHY out of reset state */
  802. qphy_clrbits(pcs, cfg->regs[QPHY_SW_RESET], SW_RESET);
  803. status = pcs + cfg->regs[QPHY_PCS_READY_STATUS];
  804. mask = cfg->mask_pcs_ready;
  805. ret = readl_poll_timeout(status, val, !(val & mask), 1,
  806. PHY_INIT_COMPLETE_TIMEOUT);
  807. if (ret) {
  808. dev_err(qmp->dev, "phy initialization timed-out\n");
  809. goto err_pcs_ready;
  810. }
  811. return ret;
  812. err_pcs_ready:
  813. if (cfg->has_lane_rst)
  814. reset_control_assert(qphy->lane_rst);
  815. err_lane_rst:
  816. qcom_qmp_phy_com_exit(qmp);
  817. err_clk:
  818. while (--i >= 0)
  819. clk_disable_unprepare(qmp->clks[i]);
  820. return ret;
  821. }
  822. static int qcom_qmp_phy_exit(struct phy *phy)
  823. {
  824. struct qmp_phy *qphy = phy_get_drvdata(phy);
  825. struct qcom_qmp *qmp = qphy->qmp;
  826. const struct qmp_phy_cfg *cfg = qmp->cfg;
  827. int i = cfg->num_clks;
  828. /* PHY reset */
  829. qphy_setbits(qphy->pcs, cfg->regs[QPHY_SW_RESET], SW_RESET);
  830. /* stop SerDes and Phy-Coding-Sublayer */
  831. qphy_clrbits(qphy->pcs, cfg->regs[QPHY_START_CTRL], cfg->start_ctrl);
  832. /* Put PHY into POWER DOWN state: active low */
  833. qphy_clrbits(qphy->pcs, QPHY_POWER_DOWN_CONTROL, cfg->pwrdn_ctrl);
  834. if (cfg->has_lane_rst)
  835. reset_control_assert(qphy->lane_rst);
  836. qcom_qmp_phy_com_exit(qmp);
  837. while (--i >= 0)
  838. clk_disable_unprepare(qmp->clks[i]);
  839. return 0;
  840. }
  841. static int qcom_qmp_phy_vreg_init(struct device *dev)
  842. {
  843. struct qcom_qmp *qmp = dev_get_drvdata(dev);
  844. int num = qmp->cfg->num_vregs;
  845. int i;
  846. qmp->vregs = devm_kcalloc(dev, num, sizeof(*qmp->vregs), GFP_KERNEL);
  847. if (!qmp->vregs)
  848. return -ENOMEM;
  849. for (i = 0; i < num; i++)
  850. qmp->vregs[i].supply = qmp->cfg->vreg_list[i];
  851. return devm_regulator_bulk_get(dev, num, qmp->vregs);
  852. }
  853. static int qcom_qmp_phy_reset_init(struct device *dev)
  854. {
  855. struct qcom_qmp *qmp = dev_get_drvdata(dev);
  856. int i;
  857. qmp->resets = devm_kcalloc(dev, qmp->cfg->num_resets,
  858. sizeof(*qmp->resets), GFP_KERNEL);
  859. if (!qmp->resets)
  860. return -ENOMEM;
  861. for (i = 0; i < qmp->cfg->num_resets; i++) {
  862. struct reset_control *rst;
  863. const char *name = qmp->cfg->reset_list[i];
  864. rst = devm_reset_control_get(dev, name);
  865. if (IS_ERR(rst)) {
  866. dev_err(dev, "failed to get %s reset\n", name);
  867. return PTR_ERR(rst);
  868. }
  869. qmp->resets[i] = rst;
  870. }
  871. return 0;
  872. }
  873. static int qcom_qmp_phy_clk_init(struct device *dev)
  874. {
  875. struct qcom_qmp *qmp = dev_get_drvdata(dev);
  876. int ret, i;
  877. qmp->clks = devm_kcalloc(dev, qmp->cfg->num_clks,
  878. sizeof(*qmp->clks), GFP_KERNEL);
  879. if (!qmp->clks)
  880. return -ENOMEM;
  881. for (i = 0; i < qmp->cfg->num_clks; i++) {
  882. struct clk *_clk;
  883. const char *name = qmp->cfg->clk_list[i];
  884. _clk = devm_clk_get(dev, name);
  885. if (IS_ERR(_clk)) {
  886. ret = PTR_ERR(_clk);
  887. if (ret != -EPROBE_DEFER)
  888. dev_err(dev, "failed to get %s clk, %d\n",
  889. name, ret);
  890. return ret;
  891. }
  892. qmp->clks[i] = _clk;
  893. }
  894. return 0;
  895. }
  896. /*
  897. * Register a fixed rate pipe clock.
  898. *
  899. * The <s>_pipe_clksrc generated by PHY goes to the GCC that gate
  900. * controls it. The <s>_pipe_clk coming out of the GCC is requested
  901. * by the PHY driver for its operations.
  902. * We register the <s>_pipe_clksrc here. The gcc driver takes care
  903. * of assigning this <s>_pipe_clksrc as parent to <s>_pipe_clk.
  904. * Below picture shows this relationship.
  905. *
  906. * +---------------+
  907. * | PHY block |<<---------------------------------------+
  908. * | | |
  909. * | +-------+ | +-----+ |
  910. * I/P---^-->| PLL |---^--->pipe_clksrc--->| GCC |--->pipe_clk---+
  911. * clk | +-------+ | +-----+
  912. * +---------------+
  913. */
  914. static int phy_pipe_clk_register(struct qcom_qmp *qmp, struct device_node *np)
  915. {
  916. struct clk_fixed_rate *fixed;
  917. struct clk_init_data init = { };
  918. int ret;
  919. if ((qmp->cfg->type != PHY_TYPE_USB3) &&
  920. (qmp->cfg->type != PHY_TYPE_PCIE)) {
  921. /* not all phys register pipe clocks, so return success */
  922. return 0;
  923. }
  924. ret = of_property_read_string(np, "clock-output-names", &init.name);
  925. if (ret) {
  926. dev_err(qmp->dev, "%s: No clock-output-names\n", np->name);
  927. return ret;
  928. }
  929. fixed = devm_kzalloc(qmp->dev, sizeof(*fixed), GFP_KERNEL);
  930. if (!fixed)
  931. return -ENOMEM;
  932. init.ops = &clk_fixed_rate_ops;
  933. /* controllers using QMP phys use 125MHz pipe clock interface */
  934. fixed->fixed_rate = 125000000;
  935. fixed->hw.init = &init;
  936. return devm_clk_hw_register(qmp->dev, &fixed->hw);
  937. }
  938. static const struct phy_ops qcom_qmp_phy_gen_ops = {
  939. .init = qcom_qmp_phy_init,
  940. .exit = qcom_qmp_phy_exit,
  941. .power_on = qcom_qmp_phy_poweron,
  942. .power_off = qcom_qmp_phy_poweroff,
  943. .owner = THIS_MODULE,
  944. };
  945. static
  946. int qcom_qmp_phy_create(struct device *dev, struct device_node *np, int id)
  947. {
  948. struct qcom_qmp *qmp = dev_get_drvdata(dev);
  949. struct phy *generic_phy;
  950. struct qmp_phy *qphy;
  951. char prop_name[MAX_PROP_NAME];
  952. int ret;
  953. qphy = devm_kzalloc(dev, sizeof(*qphy), GFP_KERNEL);
  954. if (!qphy)
  955. return -ENOMEM;
  956. /*
  957. * Get memory resources for each phy lane:
  958. * Resources are indexed as: tx -> 0; rx -> 1; pcs -> 2.
  959. */
  960. qphy->tx = of_iomap(np, 0);
  961. if (!qphy->tx)
  962. return -ENOMEM;
  963. qphy->rx = of_iomap(np, 1);
  964. if (!qphy->rx)
  965. return -ENOMEM;
  966. qphy->pcs = of_iomap(np, 2);
  967. if (!qphy->pcs)
  968. return -ENOMEM;
  969. /*
  970. * Get PHY's Pipe clock, if any. USB3 and PCIe are PIPE3
  971. * based phys, so they essentially have pipe clock. So,
  972. * we return error in case phy is USB3 or PIPE type.
  973. * Otherwise, we initialize pipe clock to NULL for
  974. * all phys that don't need this.
  975. */
  976. snprintf(prop_name, sizeof(prop_name), "pipe%d", id);
  977. qphy->pipe_clk = of_clk_get_by_name(np, prop_name);
  978. if (IS_ERR(qphy->pipe_clk)) {
  979. if (qmp->cfg->type == PHY_TYPE_PCIE ||
  980. qmp->cfg->type == PHY_TYPE_USB3) {
  981. ret = PTR_ERR(qphy->pipe_clk);
  982. if (ret != -EPROBE_DEFER)
  983. dev_err(dev,
  984. "failed to get lane%d pipe_clk, %d\n",
  985. id, ret);
  986. return ret;
  987. }
  988. qphy->pipe_clk = NULL;
  989. }
  990. /* Get lane reset, if any */
  991. if (qmp->cfg->has_lane_rst) {
  992. snprintf(prop_name, sizeof(prop_name), "lane%d", id);
  993. qphy->lane_rst = of_reset_control_get(np, prop_name);
  994. if (IS_ERR(qphy->lane_rst)) {
  995. dev_err(dev, "failed to get lane%d reset\n", id);
  996. return PTR_ERR(qphy->lane_rst);
  997. }
  998. }
  999. generic_phy = devm_phy_create(dev, np, &qcom_qmp_phy_gen_ops);
  1000. if (IS_ERR(generic_phy)) {
  1001. ret = PTR_ERR(generic_phy);
  1002. dev_err(dev, "failed to create qphy %d\n", ret);
  1003. return ret;
  1004. }
  1005. qphy->phy = generic_phy;
  1006. qphy->index = id;
  1007. qphy->qmp = qmp;
  1008. qmp->phys[id] = qphy;
  1009. phy_set_drvdata(generic_phy, qphy);
  1010. return 0;
  1011. }
  1012. static const struct of_device_id qcom_qmp_phy_of_match_table[] = {
  1013. {
  1014. .compatible = "qcom,msm8996-qmp-pcie-phy",
  1015. .data = &msm8996_pciephy_cfg,
  1016. }, {
  1017. .compatible = "qcom,msm8996-qmp-usb3-phy",
  1018. .data = &msm8996_usb3phy_cfg,
  1019. }, {
  1020. .compatible = "qcom,ipq8074-qmp-pcie-phy",
  1021. .data = &ipq8074_pciephy_cfg,
  1022. },
  1023. { },
  1024. };
  1025. MODULE_DEVICE_TABLE(of, qcom_qmp_phy_of_match_table);
  1026. static int qcom_qmp_phy_probe(struct platform_device *pdev)
  1027. {
  1028. struct qcom_qmp *qmp;
  1029. struct device *dev = &pdev->dev;
  1030. struct resource *res;
  1031. struct device_node *child;
  1032. struct phy_provider *phy_provider;
  1033. void __iomem *base;
  1034. int num, id;
  1035. int ret;
  1036. qmp = devm_kzalloc(dev, sizeof(*qmp), GFP_KERNEL);
  1037. if (!qmp)
  1038. return -ENOMEM;
  1039. qmp->dev = dev;
  1040. dev_set_drvdata(dev, qmp);
  1041. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1042. base = devm_ioremap_resource(dev, res);
  1043. if (IS_ERR(base))
  1044. return PTR_ERR(base);
  1045. /* per PHY serdes; usually located at base address */
  1046. qmp->serdes = base;
  1047. mutex_init(&qmp->phy_mutex);
  1048. /* Get the specific init parameters of QMP phy */
  1049. qmp->cfg = of_device_get_match_data(dev);
  1050. ret = qcom_qmp_phy_clk_init(dev);
  1051. if (ret)
  1052. return ret;
  1053. ret = qcom_qmp_phy_reset_init(dev);
  1054. if (ret)
  1055. return ret;
  1056. ret = qcom_qmp_phy_vreg_init(dev);
  1057. if (ret) {
  1058. dev_err(dev, "failed to get regulator supplies\n");
  1059. return ret;
  1060. }
  1061. num = of_get_available_child_count(dev->of_node);
  1062. /* do we have a rogue child node ? */
  1063. if (num > qmp->cfg->nlanes)
  1064. return -EINVAL;
  1065. qmp->phys = devm_kcalloc(dev, num, sizeof(*qmp->phys), GFP_KERNEL);
  1066. if (!qmp->phys)
  1067. return -ENOMEM;
  1068. id = 0;
  1069. for_each_available_child_of_node(dev->of_node, child) {
  1070. /* Create per-lane phy */
  1071. ret = qcom_qmp_phy_create(dev, child, id);
  1072. if (ret) {
  1073. dev_err(dev, "failed to create lane%d phy, %d\n",
  1074. id, ret);
  1075. return ret;
  1076. }
  1077. /*
  1078. * Register the pipe clock provided by phy.
  1079. * See function description to see details of this pipe clock.
  1080. */
  1081. ret = phy_pipe_clk_register(qmp, child);
  1082. if (ret) {
  1083. dev_err(qmp->dev,
  1084. "failed to register pipe clock source\n");
  1085. return ret;
  1086. }
  1087. id++;
  1088. }
  1089. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  1090. if (!IS_ERR(phy_provider))
  1091. dev_info(dev, "Registered Qcom-QMP phy\n");
  1092. return PTR_ERR_OR_ZERO(phy_provider);
  1093. }
  1094. static struct platform_driver qcom_qmp_phy_driver = {
  1095. .probe = qcom_qmp_phy_probe,
  1096. .driver = {
  1097. .name = "qcom-qmp-phy",
  1098. .of_match_table = qcom_qmp_phy_of_match_table,
  1099. },
  1100. };
  1101. module_platform_driver(qcom_qmp_phy_driver);
  1102. MODULE_AUTHOR("Vivek Gautam <vivek.gautam@codeaurora.org>");
  1103. MODULE_DESCRIPTION("Qualcomm QMP PHY driver");
  1104. MODULE_LICENSE("GPL v2");