phy-qcom-qmp.c 33 KB

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