phy-qcom-qmp.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2017, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/clk-provider.h>
  7. #include <linux/delay.h>
  8. #include <linux/err.h>
  9. #include <linux/io.h>
  10. #include <linux/iopoll.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/of_device.h>
  15. #include <linux/of_address.h>
  16. #include <linux/phy/phy.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/regulator/consumer.h>
  19. #include <linux/reset.h>
  20. #include <linux/slab.h>
  21. #include <dt-bindings/phy/phy.h>
  22. #include "phy-qcom-qmp.h"
  23. /* QPHY_SW_RESET bit */
  24. #define SW_RESET BIT(0)
  25. /* QPHY_POWER_DOWN_CONTROL */
  26. #define SW_PWRDN BIT(0)
  27. #define REFCLK_DRV_DSBL BIT(1)
  28. /* QPHY_START_CONTROL bits */
  29. #define SERDES_START BIT(0)
  30. #define PCS_START BIT(1)
  31. #define PLL_READY_GATE_EN BIT(3)
  32. /* QPHY_PCS_STATUS bit */
  33. #define PHYSTATUS BIT(6)
  34. /* QPHY_COM_PCS_READY_STATUS bit */
  35. #define PCS_READY BIT(0)
  36. /* QPHY_V3_DP_COM_RESET_OVRD_CTRL register bits */
  37. /* DP PHY soft reset */
  38. #define SW_DPPHY_RESET BIT(0)
  39. /* mux to select DP PHY reset control, 0:HW control, 1: software reset */
  40. #define SW_DPPHY_RESET_MUX BIT(1)
  41. /* USB3 PHY soft reset */
  42. #define SW_USB3PHY_RESET BIT(2)
  43. /* mux to select USB3 PHY reset control, 0:HW control, 1: software reset */
  44. #define SW_USB3PHY_RESET_MUX BIT(3)
  45. /* QPHY_V3_DP_COM_PHY_MODE_CTRL register bits */
  46. #define USB3_MODE BIT(0) /* enables USB3 mode */
  47. #define DP_MODE BIT(1) /* enables DP mode */
  48. /* QPHY_PCS_AUTONOMOUS_MODE_CTRL register bits */
  49. #define ARCVR_DTCT_EN BIT(0)
  50. #define ALFPS_DTCT_EN BIT(1)
  51. #define ARCVR_DTCT_EVENT_SEL BIT(4)
  52. /* QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR register bits */
  53. #define IRQ_CLEAR BIT(0)
  54. /* QPHY_PCS_LFPS_RXTERM_IRQ_STATUS register bits */
  55. #define RCVR_DETECT BIT(0)
  56. /* QPHY_V3_PCS_MISC_CLAMP_ENABLE register bits */
  57. #define CLAMP_EN BIT(0) /* enables i/o clamp_n */
  58. #define PHY_INIT_COMPLETE_TIMEOUT 1000
  59. #define POWER_DOWN_DELAY_US_MIN 10
  60. #define POWER_DOWN_DELAY_US_MAX 11
  61. #define MAX_PROP_NAME 32
  62. struct qmp_phy_init_tbl {
  63. unsigned int offset;
  64. unsigned int val;
  65. /*
  66. * register part of layout ?
  67. * if yes, then offset gives index in the reg-layout
  68. */
  69. int in_layout;
  70. };
  71. #define QMP_PHY_INIT_CFG(o, v) \
  72. { \
  73. .offset = o, \
  74. .val = v, \
  75. }
  76. #define QMP_PHY_INIT_CFG_L(o, v) \
  77. { \
  78. .offset = o, \
  79. .val = v, \
  80. .in_layout = 1, \
  81. }
  82. /* set of registers with offsets different per-PHY */
  83. enum qphy_reg_layout {
  84. /* Common block control registers */
  85. QPHY_COM_SW_RESET,
  86. QPHY_COM_POWER_DOWN_CONTROL,
  87. QPHY_COM_START_CONTROL,
  88. QPHY_COM_PCS_READY_STATUS,
  89. /* PCS registers */
  90. QPHY_PLL_LOCK_CHK_DLY_TIME,
  91. QPHY_FLL_CNTRL1,
  92. QPHY_FLL_CNTRL2,
  93. QPHY_FLL_CNT_VAL_L,
  94. QPHY_FLL_CNT_VAL_H_TOL,
  95. QPHY_FLL_MAN_CODE,
  96. QPHY_SW_RESET,
  97. QPHY_START_CTRL,
  98. QPHY_PCS_READY_STATUS,
  99. QPHY_PCS_AUTONOMOUS_MODE_CTRL,
  100. QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR,
  101. QPHY_PCS_LFPS_RXTERM_IRQ_STATUS,
  102. };
  103. static const unsigned int pciephy_regs_layout[] = {
  104. [QPHY_COM_SW_RESET] = 0x400,
  105. [QPHY_COM_POWER_DOWN_CONTROL] = 0x404,
  106. [QPHY_COM_START_CONTROL] = 0x408,
  107. [QPHY_COM_PCS_READY_STATUS] = 0x448,
  108. [QPHY_PLL_LOCK_CHK_DLY_TIME] = 0xa8,
  109. [QPHY_FLL_CNTRL1] = 0xc4,
  110. [QPHY_FLL_CNTRL2] = 0xc8,
  111. [QPHY_FLL_CNT_VAL_L] = 0xcc,
  112. [QPHY_FLL_CNT_VAL_H_TOL] = 0xd0,
  113. [QPHY_FLL_MAN_CODE] = 0xd4,
  114. [QPHY_SW_RESET] = 0x00,
  115. [QPHY_START_CTRL] = 0x08,
  116. [QPHY_PCS_READY_STATUS] = 0x174,
  117. };
  118. static const unsigned int usb3phy_regs_layout[] = {
  119. [QPHY_FLL_CNTRL1] = 0xc0,
  120. [QPHY_FLL_CNTRL2] = 0xc4,
  121. [QPHY_FLL_CNT_VAL_L] = 0xc8,
  122. [QPHY_FLL_CNT_VAL_H_TOL] = 0xcc,
  123. [QPHY_FLL_MAN_CODE] = 0xd0,
  124. [QPHY_SW_RESET] = 0x00,
  125. [QPHY_START_CTRL] = 0x08,
  126. [QPHY_PCS_READY_STATUS] = 0x17c,
  127. [QPHY_PCS_AUTONOMOUS_MODE_CTRL] = 0x0d4,
  128. [QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR] = 0x0d8,
  129. [QPHY_PCS_LFPS_RXTERM_IRQ_STATUS] = 0x178,
  130. };
  131. static const unsigned int qmp_v3_usb3phy_regs_layout[] = {
  132. [QPHY_SW_RESET] = 0x00,
  133. [QPHY_START_CTRL] = 0x08,
  134. [QPHY_PCS_READY_STATUS] = 0x174,
  135. [QPHY_PCS_AUTONOMOUS_MODE_CTRL] = 0x0d8,
  136. [QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR] = 0x0dc,
  137. [QPHY_PCS_LFPS_RXTERM_IRQ_STATUS] = 0x170,
  138. };
  139. static const struct qmp_phy_init_tbl msm8996_pcie_serdes_tbl[] = {
  140. QMP_PHY_INIT_CFG(QSERDES_COM_BIAS_EN_CLKBUFLR_EN, 0x1c),
  141. QMP_PHY_INIT_CFG(QSERDES_COM_CLK_ENABLE1, 0x10),
  142. QMP_PHY_INIT_CFG(QSERDES_COM_CLK_SELECT, 0x33),
  143. QMP_PHY_INIT_CFG(QSERDES_COM_CMN_CONFIG, 0x06),
  144. QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP_EN, 0x42),
  145. QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_MAP, 0x00),
  146. QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_TIMER1, 0xff),
  147. QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_TIMER2, 0x1f),
  148. QMP_PHY_INIT_CFG(QSERDES_COM_HSCLK_SEL, 0x01),
  149. QMP_PHY_INIT_CFG(QSERDES_COM_SVS_MODE_CLK_SEL, 0x01),
  150. QMP_PHY_INIT_CFG(QSERDES_COM_CORE_CLK_EN, 0x00),
  151. QMP_PHY_INIT_CFG(QSERDES_COM_CORECLK_DIV, 0x0a),
  152. QMP_PHY_INIT_CFG(QSERDES_COM_BG_TIMER, 0x09),
  153. QMP_PHY_INIT_CFG(QSERDES_COM_DEC_START_MODE0, 0x82),
  154. QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START3_MODE0, 0x03),
  155. QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START2_MODE0, 0x55),
  156. QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START1_MODE0, 0x55),
  157. QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP3_MODE0, 0x00),
  158. QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP2_MODE0, 0x1a),
  159. QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP1_MODE0, 0x0a),
  160. QMP_PHY_INIT_CFG(QSERDES_COM_CLK_SELECT, 0x33),
  161. QMP_PHY_INIT_CFG(QSERDES_COM_SYS_CLK_CTRL, 0x02),
  162. QMP_PHY_INIT_CFG(QSERDES_COM_SYSCLK_BUF_ENABLE, 0x1f),
  163. QMP_PHY_INIT_CFG(QSERDES_COM_SYSCLK_EN_SEL, 0x04),
  164. QMP_PHY_INIT_CFG(QSERDES_COM_CP_CTRL_MODE0, 0x0b),
  165. QMP_PHY_INIT_CFG(QSERDES_COM_PLL_RCTRL_MODE0, 0x16),
  166. QMP_PHY_INIT_CFG(QSERDES_COM_PLL_CCTRL_MODE0, 0x28),
  167. QMP_PHY_INIT_CFG(QSERDES_COM_INTEGLOOP_GAIN1_MODE0, 0x00),
  168. QMP_PHY_INIT_CFG(QSERDES_COM_INTEGLOOP_GAIN0_MODE0, 0x80),
  169. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_EN_CENTER, 0x01),
  170. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_PER1, 0x31),
  171. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_PER2, 0x01),
  172. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_ADJ_PER1, 0x02),
  173. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_ADJ_PER2, 0x00),
  174. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_STEP_SIZE1, 0x2f),
  175. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_STEP_SIZE2, 0x19),
  176. QMP_PHY_INIT_CFG(QSERDES_COM_RESCODE_DIV_NUM, 0x15),
  177. QMP_PHY_INIT_CFG(QSERDES_COM_BG_TRIM, 0x0f),
  178. QMP_PHY_INIT_CFG(QSERDES_COM_PLL_IVCO, 0x0f),
  179. QMP_PHY_INIT_CFG(QSERDES_COM_CLK_EP_DIV, 0x19),
  180. QMP_PHY_INIT_CFG(QSERDES_COM_CLK_ENABLE1, 0x10),
  181. QMP_PHY_INIT_CFG(QSERDES_COM_HSCLK_SEL, 0x00),
  182. QMP_PHY_INIT_CFG(QSERDES_COM_RESCODE_DIV_NUM, 0x40),
  183. };
  184. static const struct qmp_phy_init_tbl msm8996_pcie_tx_tbl[] = {
  185. QMP_PHY_INIT_CFG(QSERDES_TX_HIGHZ_TRANSCEIVEREN_BIAS_DRVR_EN, 0x45),
  186. QMP_PHY_INIT_CFG(QSERDES_TX_LANE_MODE, 0x06),
  187. };
  188. static const struct qmp_phy_init_tbl msm8996_pcie_rx_tbl[] = {
  189. QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_ENABLES, 0x1c),
  190. QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL2, 0x01),
  191. QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL3, 0x00),
  192. QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL4, 0xdb),
  193. QMP_PHY_INIT_CFG(QSERDES_RX_RX_BAND, 0x18),
  194. QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_SO_GAIN, 0x04),
  195. QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_SO_GAIN_HALF, 0x04),
  196. QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_SO_SATURATION_AND_ENABLE, 0x4b),
  197. QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_DEGLITCH_CNTRL, 0x14),
  198. QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_LVL, 0x19),
  199. };
  200. static const struct qmp_phy_init_tbl msm8996_pcie_pcs_tbl[] = {
  201. QMP_PHY_INIT_CFG(QPHY_RX_IDLE_DTCT_CNTRL, 0x4c),
  202. QMP_PHY_INIT_CFG(QPHY_PWRUP_RESET_DLY_TIME_AUXCLK, 0x00),
  203. QMP_PHY_INIT_CFG(QPHY_LP_WAKEUP_DLY_TIME_AUXCLK, 0x01),
  204. QMP_PHY_INIT_CFG_L(QPHY_PLL_LOCK_CHK_DLY_TIME, 0x05),
  205. QMP_PHY_INIT_CFG(QPHY_ENDPOINT_REFCLK_DRIVE, 0x05),
  206. QMP_PHY_INIT_CFG(QPHY_POWER_DOWN_CONTROL, 0x02),
  207. QMP_PHY_INIT_CFG(QPHY_POWER_STATE_CONFIG4, 0x00),
  208. QMP_PHY_INIT_CFG(QPHY_POWER_STATE_CONFIG1, 0xa3),
  209. QMP_PHY_INIT_CFG(QPHY_TXDEEMPH_M3P5DB_V0, 0x0e),
  210. };
  211. static const struct qmp_phy_init_tbl msm8996_usb3_serdes_tbl[] = {
  212. QMP_PHY_INIT_CFG(QSERDES_COM_SYSCLK_EN_SEL, 0x14),
  213. QMP_PHY_INIT_CFG(QSERDES_COM_BIAS_EN_CLKBUFLR_EN, 0x08),
  214. QMP_PHY_INIT_CFG(QSERDES_COM_CLK_SELECT, 0x30),
  215. QMP_PHY_INIT_CFG(QSERDES_COM_CMN_CONFIG, 0x06),
  216. QMP_PHY_INIT_CFG(QSERDES_COM_SVS_MODE_CLK_SEL, 0x01),
  217. QMP_PHY_INIT_CFG(QSERDES_COM_HSCLK_SEL, 0x00),
  218. QMP_PHY_INIT_CFG(QSERDES_COM_BG_TRIM, 0x0f),
  219. QMP_PHY_INIT_CFG(QSERDES_COM_PLL_IVCO, 0x0f),
  220. QMP_PHY_INIT_CFG(QSERDES_COM_SYS_CLK_CTRL, 0x04),
  221. /* PLL and Loop filter settings */
  222. QMP_PHY_INIT_CFG(QSERDES_COM_DEC_START_MODE0, 0x82),
  223. QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START1_MODE0, 0x55),
  224. QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START2_MODE0, 0x55),
  225. QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START3_MODE0, 0x03),
  226. QMP_PHY_INIT_CFG(QSERDES_COM_CP_CTRL_MODE0, 0x0b),
  227. QMP_PHY_INIT_CFG(QSERDES_COM_PLL_RCTRL_MODE0, 0x16),
  228. QMP_PHY_INIT_CFG(QSERDES_COM_PLL_CCTRL_MODE0, 0x28),
  229. QMP_PHY_INIT_CFG(QSERDES_COM_INTEGLOOP_GAIN0_MODE0, 0x80),
  230. QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_CTRL, 0x00),
  231. QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP1_MODE0, 0x15),
  232. QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP2_MODE0, 0x34),
  233. QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP3_MODE0, 0x00),
  234. QMP_PHY_INIT_CFG(QSERDES_COM_CORE_CLK_EN, 0x00),
  235. QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP_CFG, 0x00),
  236. QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_MAP, 0x00),
  237. QMP_PHY_INIT_CFG(QSERDES_COM_BG_TIMER, 0x0a),
  238. /* SSC settings */
  239. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_EN_CENTER, 0x01),
  240. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_PER1, 0x31),
  241. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_PER2, 0x01),
  242. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_ADJ_PER1, 0x00),
  243. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_ADJ_PER2, 0x00),
  244. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_STEP_SIZE1, 0xde),
  245. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_STEP_SIZE2, 0x07),
  246. };
  247. static const struct qmp_phy_init_tbl msm8996_usb3_tx_tbl[] = {
  248. QMP_PHY_INIT_CFG(QSERDES_TX_HIGHZ_TRANSCEIVEREN_BIAS_DRVR_EN, 0x45),
  249. QMP_PHY_INIT_CFG(QSERDES_TX_RCV_DETECT_LVL_2, 0x12),
  250. QMP_PHY_INIT_CFG(QSERDES_TX_LANE_MODE, 0x06),
  251. };
  252. static const struct qmp_phy_init_tbl msm8996_usb3_rx_tbl[] = {
  253. QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_FASTLOCK_FO_GAIN, 0x0b),
  254. QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_SO_GAIN, 0x04),
  255. QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL2, 0x02),
  256. QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL3, 0x4c),
  257. QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL4, 0xbb),
  258. QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQ_OFFSET_ADAPTOR_CNTRL1, 0x77),
  259. QMP_PHY_INIT_CFG(QSERDES_RX_RX_OFFSET_ADAPTOR_CNTRL2, 0x80),
  260. QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_CNTRL, 0x03),
  261. QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_LVL, 0x18),
  262. QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_DEGLITCH_CNTRL, 0x16),
  263. };
  264. static const struct qmp_phy_init_tbl msm8996_usb3_pcs_tbl[] = {
  265. /* FLL settings */
  266. QMP_PHY_INIT_CFG_L(QPHY_FLL_CNTRL2, 0x03),
  267. QMP_PHY_INIT_CFG_L(QPHY_FLL_CNTRL1, 0x02),
  268. QMP_PHY_INIT_CFG_L(QPHY_FLL_CNT_VAL_L, 0x09),
  269. QMP_PHY_INIT_CFG_L(QPHY_FLL_CNT_VAL_H_TOL, 0x42),
  270. QMP_PHY_INIT_CFG_L(QPHY_FLL_MAN_CODE, 0x85),
  271. /* Lock Det settings */
  272. QMP_PHY_INIT_CFG(QPHY_LOCK_DETECT_CONFIG1, 0xd1),
  273. QMP_PHY_INIT_CFG(QPHY_LOCK_DETECT_CONFIG2, 0x1f),
  274. QMP_PHY_INIT_CFG(QPHY_LOCK_DETECT_CONFIG3, 0x47),
  275. QMP_PHY_INIT_CFG(QPHY_POWER_STATE_CONFIG2, 0x08),
  276. };
  277. static const struct qmp_phy_init_tbl ipq8074_pcie_serdes_tbl[] = {
  278. QMP_PHY_INIT_CFG(QSERDES_COM_BIAS_EN_CLKBUFLR_EN, 0x18),
  279. QMP_PHY_INIT_CFG(QSERDES_COM_CLK_ENABLE1, 0x10),
  280. QMP_PHY_INIT_CFG(QSERDES_COM_BG_TRIM, 0xf),
  281. QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP_EN, 0x1),
  282. QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_MAP, 0x0),
  283. QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_TIMER1, 0x1f),
  284. QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_TIMER2, 0x3f),
  285. QMP_PHY_INIT_CFG(QSERDES_COM_CMN_CONFIG, 0x6),
  286. QMP_PHY_INIT_CFG(QSERDES_COM_PLL_IVCO, 0xf),
  287. QMP_PHY_INIT_CFG(QSERDES_COM_HSCLK_SEL, 0x0),
  288. QMP_PHY_INIT_CFG(QSERDES_COM_SVS_MODE_CLK_SEL, 0x1),
  289. QMP_PHY_INIT_CFG(QSERDES_COM_CORE_CLK_EN, 0x20),
  290. QMP_PHY_INIT_CFG(QSERDES_COM_CORECLK_DIV, 0xa),
  291. QMP_PHY_INIT_CFG(QSERDES_COM_RESETSM_CNTRL, 0x20),
  292. QMP_PHY_INIT_CFG(QSERDES_COM_BG_TIMER, 0xa),
  293. QMP_PHY_INIT_CFG(QSERDES_COM_SYSCLK_EN_SEL, 0xa),
  294. QMP_PHY_INIT_CFG(QSERDES_COM_DEC_START_MODE0, 0x82),
  295. QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START3_MODE0, 0x3),
  296. QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START2_MODE0, 0x55),
  297. QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START1_MODE0, 0x55),
  298. QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP3_MODE0, 0x0),
  299. QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP2_MODE0, 0xD),
  300. QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP1_MODE0, 0xD04),
  301. QMP_PHY_INIT_CFG(QSERDES_COM_CLK_SELECT, 0x33),
  302. QMP_PHY_INIT_CFG(QSERDES_COM_SYS_CLK_CTRL, 0x2),
  303. QMP_PHY_INIT_CFG(QSERDES_COM_SYSCLK_BUF_ENABLE, 0x1f),
  304. QMP_PHY_INIT_CFG(QSERDES_COM_CP_CTRL_MODE0, 0xb),
  305. QMP_PHY_INIT_CFG(QSERDES_COM_PLL_RCTRL_MODE0, 0x16),
  306. QMP_PHY_INIT_CFG(QSERDES_COM_PLL_CCTRL_MODE0, 0x28),
  307. QMP_PHY_INIT_CFG(QSERDES_COM_INTEGLOOP_GAIN1_MODE0, 0x0),
  308. QMP_PHY_INIT_CFG(QSERDES_COM_INTEGLOOP_GAIN0_MODE0, 0x80),
  309. QMP_PHY_INIT_CFG(QSERDES_COM_BIAS_EN_CTRL_BY_PSM, 0x1),
  310. QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_CTRL, 0xa),
  311. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_EN_CENTER, 0x1),
  312. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_PER1, 0x31),
  313. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_PER2, 0x1),
  314. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_ADJ_PER1, 0x2),
  315. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_ADJ_PER2, 0x0),
  316. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_STEP_SIZE1, 0x2f),
  317. QMP_PHY_INIT_CFG(QSERDES_COM_SSC_STEP_SIZE2, 0x19),
  318. QMP_PHY_INIT_CFG(QSERDES_COM_CLK_EP_DIV, 0x19),
  319. QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_CNTRL, 0x7),
  320. };
  321. static const struct qmp_phy_init_tbl ipq8074_pcie_tx_tbl[] = {
  322. QMP_PHY_INIT_CFG(QSERDES_TX_HIGHZ_TRANSCEIVEREN_BIAS_DRVR_EN, 0x45),
  323. QMP_PHY_INIT_CFG(QSERDES_TX_LANE_MODE, 0x6),
  324. QMP_PHY_INIT_CFG(QSERDES_TX_RES_CODE_LANE_OFFSET, 0x2),
  325. QMP_PHY_INIT_CFG(QSERDES_TX_RCV_DETECT_LVL_2, 0x12),
  326. };
  327. static const struct qmp_phy_init_tbl ipq8074_pcie_rx_tbl[] = {
  328. QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_ENABLES, 0x1c),
  329. QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_DEGLITCH_CNTRL, 0x14),
  330. QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL2, 0x1),
  331. QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL3, 0x0),
  332. QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL4, 0xdb),
  333. QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_SO_SATURATION_AND_ENABLE, 0x4b),
  334. QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_SO_GAIN, 0x4),
  335. QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_SO_GAIN_HALF, 0x4),
  336. };
  337. static const struct qmp_phy_init_tbl ipq8074_pcie_pcs_tbl[] = {
  338. QMP_PHY_INIT_CFG(QPHY_ENDPOINT_REFCLK_DRIVE, 0x4),
  339. QMP_PHY_INIT_CFG(QPHY_OSC_DTCT_ACTIONS, 0x0),
  340. QMP_PHY_INIT_CFG(QPHY_PWRUP_RESET_DLY_TIME_AUXCLK, 0x40),
  341. QMP_PHY_INIT_CFG(QPHY_L1SS_WAKEUP_DLY_TIME_AUXCLK_MSB, 0x0),
  342. QMP_PHY_INIT_CFG(QPHY_L1SS_WAKEUP_DLY_TIME_AUXCLK_LSB, 0x40),
  343. QMP_PHY_INIT_CFG(QPHY_PLL_LOCK_CHK_DLY_TIME_AUXCLK_LSB, 0x0),
  344. QMP_PHY_INIT_CFG(QPHY_LP_WAKEUP_DLY_TIME_AUXCLK, 0x40),
  345. QMP_PHY_INIT_CFG_L(QPHY_PLL_LOCK_CHK_DLY_TIME, 0x73),
  346. QMP_PHY_INIT_CFG(QPHY_RX_SIGDET_LVL, 0x99),
  347. QMP_PHY_INIT_CFG(QPHY_TXDEEMPH_M6DB_V0, 0x15),
  348. QMP_PHY_INIT_CFG(QPHY_TXDEEMPH_M3P5DB_V0, 0xe),
  349. QMP_PHY_INIT_CFG_L(QPHY_SW_RESET, 0x0),
  350. QMP_PHY_INIT_CFG_L(QPHY_START_CTRL, 0x3),
  351. };
  352. static const struct qmp_phy_init_tbl qmp_v3_usb3_serdes_tbl[] = {
  353. QMP_PHY_INIT_CFG(QSERDES_V3_COM_PLL_IVCO, 0x07),
  354. QMP_PHY_INIT_CFG(QSERDES_V3_COM_SYSCLK_EN_SEL, 0x14),
  355. QMP_PHY_INIT_CFG(QSERDES_V3_COM_BIAS_EN_CLKBUFLR_EN, 0x08),
  356. QMP_PHY_INIT_CFG(QSERDES_V3_COM_CLK_SELECT, 0x30),
  357. QMP_PHY_INIT_CFG(QSERDES_V3_COM_SYS_CLK_CTRL, 0x02),
  358. QMP_PHY_INIT_CFG(QSERDES_V3_COM_RESETSM_CNTRL2, 0x08),
  359. QMP_PHY_INIT_CFG(QSERDES_V3_COM_CMN_CONFIG, 0x16),
  360. QMP_PHY_INIT_CFG(QSERDES_V3_COM_SVS_MODE_CLK_SEL, 0x01),
  361. QMP_PHY_INIT_CFG(QSERDES_V3_COM_HSCLK_SEL, 0x80),
  362. QMP_PHY_INIT_CFG(QSERDES_V3_COM_DEC_START_MODE0, 0x82),
  363. QMP_PHY_INIT_CFG(QSERDES_V3_COM_DIV_FRAC_START1_MODE0, 0xab),
  364. QMP_PHY_INIT_CFG(QSERDES_V3_COM_DIV_FRAC_START2_MODE0, 0xea),
  365. QMP_PHY_INIT_CFG(QSERDES_V3_COM_DIV_FRAC_START3_MODE0, 0x02),
  366. QMP_PHY_INIT_CFG(QSERDES_V3_COM_CP_CTRL_MODE0, 0x06),
  367. QMP_PHY_INIT_CFG(QSERDES_V3_COM_PLL_RCTRL_MODE0, 0x16),
  368. QMP_PHY_INIT_CFG(QSERDES_V3_COM_PLL_CCTRL_MODE0, 0x36),
  369. QMP_PHY_INIT_CFG(QSERDES_V3_COM_INTEGLOOP_GAIN1_MODE0, 0x00),
  370. QMP_PHY_INIT_CFG(QSERDES_V3_COM_INTEGLOOP_GAIN0_MODE0, 0x3f),
  371. QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE2_MODE0, 0x01),
  372. QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE1_MODE0, 0xc9),
  373. QMP_PHY_INIT_CFG(QSERDES_V3_COM_CORECLK_DIV_MODE0, 0x0a),
  374. QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP3_MODE0, 0x00),
  375. QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP2_MODE0, 0x34),
  376. QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP1_MODE0, 0x15),
  377. QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP_EN, 0x04),
  378. QMP_PHY_INIT_CFG(QSERDES_V3_COM_CORE_CLK_EN, 0x00),
  379. QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP_CFG, 0x00),
  380. QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE_MAP, 0x00),
  381. QMP_PHY_INIT_CFG(QSERDES_V3_COM_SYSCLK_BUF_ENABLE, 0x0a),
  382. QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_EN_CENTER, 0x01),
  383. QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_PER1, 0x31),
  384. QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_PER2, 0x01),
  385. QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_ADJ_PER1, 0x00),
  386. QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_ADJ_PER2, 0x00),
  387. QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_STEP_SIZE1, 0x85),
  388. QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_STEP_SIZE2, 0x07),
  389. };
  390. static const struct qmp_phy_init_tbl qmp_v3_usb3_tx_tbl[] = {
  391. QMP_PHY_INIT_CFG(QSERDES_V3_TX_HIGHZ_DRVR_EN, 0x10),
  392. QMP_PHY_INIT_CFG(QSERDES_V3_TX_RCV_DETECT_LVL_2, 0x12),
  393. QMP_PHY_INIT_CFG(QSERDES_V3_TX_LANE_MODE_1, 0x16),
  394. QMP_PHY_INIT_CFG(QSERDES_V3_TX_RES_CODE_LANE_OFFSET_RX, 0x09),
  395. QMP_PHY_INIT_CFG(QSERDES_V3_TX_RES_CODE_LANE_OFFSET_TX, 0x06),
  396. };
  397. static const struct qmp_phy_init_tbl qmp_v3_usb3_rx_tbl[] = {
  398. QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_FASTLOCK_FO_GAIN, 0x0b),
  399. QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL2, 0x0f),
  400. QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL3, 0x4e),
  401. QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL4, 0x18),
  402. QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQ_OFFSET_ADAPTOR_CNTRL1, 0x77),
  403. QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_OFFSET_ADAPTOR_CNTRL2, 0x80),
  404. QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_CNTRL, 0x03),
  405. QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_DEGLITCH_CNTRL, 0x16),
  406. QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_SO_SATURATION_AND_ENABLE, 0x75),
  407. };
  408. static const struct qmp_phy_init_tbl qmp_v3_usb3_pcs_tbl[] = {
  409. /* FLL settings */
  410. QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_CNTRL2, 0x83),
  411. QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_CNT_VAL_L, 0x09),
  412. QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_CNT_VAL_H_TOL, 0xa2),
  413. QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_MAN_CODE, 0x40),
  414. QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_CNTRL1, 0x02),
  415. /* Lock Det settings */
  416. QMP_PHY_INIT_CFG(QPHY_V3_PCS_LOCK_DETECT_CONFIG1, 0xd1),
  417. QMP_PHY_INIT_CFG(QPHY_V3_PCS_LOCK_DETECT_CONFIG2, 0x1f),
  418. QMP_PHY_INIT_CFG(QPHY_V3_PCS_LOCK_DETECT_CONFIG3, 0x47),
  419. QMP_PHY_INIT_CFG(QPHY_V3_PCS_POWER_STATE_CONFIG2, 0x1b),
  420. QMP_PHY_INIT_CFG(QPHY_V3_PCS_RX_SIGDET_LVL, 0xba),
  421. QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_V0, 0x9f),
  422. QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_V1, 0x9f),
  423. QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_V2, 0xb7),
  424. QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_V3, 0x4e),
  425. QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_V4, 0x65),
  426. QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_LS, 0x6b),
  427. QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_V0, 0x15),
  428. QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_V0, 0x0d),
  429. QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_V1, 0x15),
  430. QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_V1, 0x0d),
  431. QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_V2, 0x15),
  432. QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_V2, 0x0d),
  433. QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_V3, 0x15),
  434. QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_V3, 0x1d),
  435. QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_V4, 0x15),
  436. QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_V4, 0x0d),
  437. QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_LS, 0x15),
  438. QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_LS, 0x0d),
  439. QMP_PHY_INIT_CFG(QPHY_V3_PCS_RATE_SLEW_CNTRL, 0x02),
  440. QMP_PHY_INIT_CFG(QPHY_V3_PCS_PWRUP_RESET_DLY_TIME_AUXCLK, 0x04),
  441. QMP_PHY_INIT_CFG(QPHY_V3_PCS_TSYNC_RSYNC_TIME, 0x44),
  442. QMP_PHY_INIT_CFG(QPHY_V3_PCS_PWRUP_RESET_DLY_TIME_AUXCLK, 0x04),
  443. QMP_PHY_INIT_CFG(QPHY_V3_PCS_RCVR_DTCT_DLY_P1U2_L, 0xe7),
  444. QMP_PHY_INIT_CFG(QPHY_V3_PCS_RCVR_DTCT_DLY_P1U2_H, 0x03),
  445. QMP_PHY_INIT_CFG(QPHY_V3_PCS_RCVR_DTCT_DLY_U3_L, 0x40),
  446. QMP_PHY_INIT_CFG(QPHY_V3_PCS_RCVR_DTCT_DLY_U3_H, 0x00),
  447. QMP_PHY_INIT_CFG(QPHY_V3_PCS_RXEQTRAINING_WAIT_TIME, 0x75),
  448. QMP_PHY_INIT_CFG(QPHY_V3_PCS_LFPS_TX_ECSTART_EQTLOCK, 0x86),
  449. QMP_PHY_INIT_CFG(QPHY_V3_PCS_RXEQTRAINING_RUN_TIME, 0x13),
  450. };
  451. /* struct qmp_phy_cfg - per-PHY initialization config */
  452. struct qmp_phy_cfg {
  453. /* phy-type - PCIE/UFS/USB */
  454. unsigned int type;
  455. /* number of lanes provided by phy */
  456. int nlanes;
  457. /* Init sequence for PHY blocks - serdes, tx, rx, pcs */
  458. const struct qmp_phy_init_tbl *serdes_tbl;
  459. int serdes_tbl_num;
  460. const struct qmp_phy_init_tbl *tx_tbl;
  461. int tx_tbl_num;
  462. const struct qmp_phy_init_tbl *rx_tbl;
  463. int rx_tbl_num;
  464. const struct qmp_phy_init_tbl *pcs_tbl;
  465. int pcs_tbl_num;
  466. /* clock ids to be requested */
  467. const char * const *clk_list;
  468. int num_clks;
  469. /* resets to be requested */
  470. const char * const *reset_list;
  471. int num_resets;
  472. /* regulators to be requested */
  473. const char * const *vreg_list;
  474. int num_vregs;
  475. /* array of registers with different offsets */
  476. const unsigned int *regs;
  477. unsigned int start_ctrl;
  478. unsigned int pwrdn_ctrl;
  479. unsigned int mask_pcs_ready;
  480. unsigned int mask_com_pcs_ready;
  481. /* true, if PHY has a separate PHY_COM control block */
  482. bool has_phy_com_ctrl;
  483. /* true, if PHY has a reset for individual lanes */
  484. bool has_lane_rst;
  485. /* true, if PHY needs delay after POWER_DOWN */
  486. bool has_pwrdn_delay;
  487. /* power_down delay in usec */
  488. int pwrdn_delay_min;
  489. int pwrdn_delay_max;
  490. /* true, if PHY has a separate DP_COM control block */
  491. bool has_phy_dp_com_ctrl;
  492. /* Register offset of secondary tx/rx lanes for USB DP combo PHY */
  493. unsigned int tx_b_lane_offset;
  494. unsigned int rx_b_lane_offset;
  495. };
  496. /**
  497. * struct qmp_phy - per-lane phy descriptor
  498. *
  499. * @phy: generic phy
  500. * @tx: iomapped memory space for lane's tx
  501. * @rx: iomapped memory space for lane's rx
  502. * @pcs: iomapped memory space for lane's pcs
  503. * @pcs_misc: iomapped memory space for lane's pcs_misc
  504. * @pipe_clk: pipe lock
  505. * @index: lane index
  506. * @qmp: QMP phy to which this lane belongs
  507. * @lane_rst: lane's reset controller
  508. */
  509. struct qmp_phy {
  510. struct phy *phy;
  511. void __iomem *tx;
  512. void __iomem *rx;
  513. void __iomem *pcs;
  514. void __iomem *pcs_misc;
  515. struct clk *pipe_clk;
  516. unsigned int index;
  517. struct qcom_qmp *qmp;
  518. struct reset_control *lane_rst;
  519. };
  520. /**
  521. * struct qcom_qmp - structure holding QMP phy block attributes
  522. *
  523. * @dev: device
  524. * @serdes: iomapped memory space for phy's serdes
  525. * @dp_com: iomapped memory space for phy's dp_com control block
  526. *
  527. * @clks: array of clocks required by phy
  528. * @resets: array of resets required by phy
  529. * @vregs: regulator supplies bulk data
  530. *
  531. * @cfg: phy specific configuration
  532. * @phys: array of per-lane phy descriptors
  533. * @phy_mutex: mutex lock for PHY common block initialization
  534. * @init_count: phy common block initialization count
  535. * @phy_initialized: indicate if PHY has been initialized
  536. * @mode: current PHY mode
  537. */
  538. struct qcom_qmp {
  539. struct device *dev;
  540. void __iomem *serdes;
  541. void __iomem *dp_com;
  542. struct clk_bulk_data *clks;
  543. struct reset_control **resets;
  544. struct regulator_bulk_data *vregs;
  545. const struct qmp_phy_cfg *cfg;
  546. struct qmp_phy **phys;
  547. struct mutex phy_mutex;
  548. int init_count;
  549. bool phy_initialized;
  550. enum phy_mode mode;
  551. };
  552. static inline void qphy_setbits(void __iomem *base, u32 offset, u32 val)
  553. {
  554. u32 reg;
  555. reg = readl(base + offset);
  556. reg |= val;
  557. writel(reg, base + offset);
  558. /* ensure that above write is through */
  559. readl(base + offset);
  560. }
  561. static inline void qphy_clrbits(void __iomem *base, u32 offset, u32 val)
  562. {
  563. u32 reg;
  564. reg = readl(base + offset);
  565. reg &= ~val;
  566. writel(reg, base + offset);
  567. /* ensure that above write is through */
  568. readl(base + offset);
  569. }
  570. /* list of clocks required by phy */
  571. static const char * const msm8996_phy_clk_l[] = {
  572. "aux", "cfg_ahb", "ref",
  573. };
  574. static const char * const qmp_v3_phy_clk_l[] = {
  575. "aux", "cfg_ahb", "ref", "com_aux",
  576. };
  577. /* list of resets */
  578. static const char * const msm8996_pciephy_reset_l[] = {
  579. "phy", "common", "cfg",
  580. };
  581. static const char * const msm8996_usb3phy_reset_l[] = {
  582. "phy", "common",
  583. };
  584. /* list of regulators */
  585. static const char * const msm8996_phy_vreg_l[] = {
  586. "vdda-phy", "vdda-pll",
  587. };
  588. static const struct qmp_phy_cfg msm8996_pciephy_cfg = {
  589. .type = PHY_TYPE_PCIE,
  590. .nlanes = 3,
  591. .serdes_tbl = msm8996_pcie_serdes_tbl,
  592. .serdes_tbl_num = ARRAY_SIZE(msm8996_pcie_serdes_tbl),
  593. .tx_tbl = msm8996_pcie_tx_tbl,
  594. .tx_tbl_num = ARRAY_SIZE(msm8996_pcie_tx_tbl),
  595. .rx_tbl = msm8996_pcie_rx_tbl,
  596. .rx_tbl_num = ARRAY_SIZE(msm8996_pcie_rx_tbl),
  597. .pcs_tbl = msm8996_pcie_pcs_tbl,
  598. .pcs_tbl_num = ARRAY_SIZE(msm8996_pcie_pcs_tbl),
  599. .clk_list = msm8996_phy_clk_l,
  600. .num_clks = ARRAY_SIZE(msm8996_phy_clk_l),
  601. .reset_list = msm8996_pciephy_reset_l,
  602. .num_resets = ARRAY_SIZE(msm8996_pciephy_reset_l),
  603. .vreg_list = msm8996_phy_vreg_l,
  604. .num_vregs = ARRAY_SIZE(msm8996_phy_vreg_l),
  605. .regs = pciephy_regs_layout,
  606. .start_ctrl = PCS_START | PLL_READY_GATE_EN,
  607. .pwrdn_ctrl = SW_PWRDN | REFCLK_DRV_DSBL,
  608. .mask_com_pcs_ready = PCS_READY,
  609. .has_phy_com_ctrl = true,
  610. .has_lane_rst = true,
  611. .has_pwrdn_delay = true,
  612. .pwrdn_delay_min = POWER_DOWN_DELAY_US_MIN,
  613. .pwrdn_delay_max = POWER_DOWN_DELAY_US_MAX,
  614. };
  615. static const struct qmp_phy_cfg msm8996_usb3phy_cfg = {
  616. .type = PHY_TYPE_USB3,
  617. .nlanes = 1,
  618. .serdes_tbl = msm8996_usb3_serdes_tbl,
  619. .serdes_tbl_num = ARRAY_SIZE(msm8996_usb3_serdes_tbl),
  620. .tx_tbl = msm8996_usb3_tx_tbl,
  621. .tx_tbl_num = ARRAY_SIZE(msm8996_usb3_tx_tbl),
  622. .rx_tbl = msm8996_usb3_rx_tbl,
  623. .rx_tbl_num = ARRAY_SIZE(msm8996_usb3_rx_tbl),
  624. .pcs_tbl = msm8996_usb3_pcs_tbl,
  625. .pcs_tbl_num = ARRAY_SIZE(msm8996_usb3_pcs_tbl),
  626. .clk_list = msm8996_phy_clk_l,
  627. .num_clks = ARRAY_SIZE(msm8996_phy_clk_l),
  628. .reset_list = msm8996_usb3phy_reset_l,
  629. .num_resets = ARRAY_SIZE(msm8996_usb3phy_reset_l),
  630. .vreg_list = msm8996_phy_vreg_l,
  631. .num_vregs = ARRAY_SIZE(msm8996_phy_vreg_l),
  632. .regs = usb3phy_regs_layout,
  633. .start_ctrl = SERDES_START | PCS_START,
  634. .pwrdn_ctrl = SW_PWRDN,
  635. .mask_pcs_ready = PHYSTATUS,
  636. };
  637. /* list of resets */
  638. static const char * const ipq8074_pciephy_reset_l[] = {
  639. "phy", "common",
  640. };
  641. static const struct qmp_phy_cfg ipq8074_pciephy_cfg = {
  642. .type = PHY_TYPE_PCIE,
  643. .nlanes = 1,
  644. .serdes_tbl = ipq8074_pcie_serdes_tbl,
  645. .serdes_tbl_num = ARRAY_SIZE(ipq8074_pcie_serdes_tbl),
  646. .tx_tbl = ipq8074_pcie_tx_tbl,
  647. .tx_tbl_num = ARRAY_SIZE(ipq8074_pcie_tx_tbl),
  648. .rx_tbl = ipq8074_pcie_rx_tbl,
  649. .rx_tbl_num = ARRAY_SIZE(ipq8074_pcie_rx_tbl),
  650. .pcs_tbl = ipq8074_pcie_pcs_tbl,
  651. .pcs_tbl_num = ARRAY_SIZE(ipq8074_pcie_pcs_tbl),
  652. .clk_list = NULL,
  653. .num_clks = 0,
  654. .reset_list = ipq8074_pciephy_reset_l,
  655. .num_resets = ARRAY_SIZE(ipq8074_pciephy_reset_l),
  656. .vreg_list = NULL,
  657. .num_vregs = 0,
  658. .regs = pciephy_regs_layout,
  659. .start_ctrl = SERDES_START | PCS_START,
  660. .pwrdn_ctrl = SW_PWRDN | REFCLK_DRV_DSBL,
  661. .mask_pcs_ready = PHYSTATUS,
  662. .has_phy_com_ctrl = false,
  663. .has_lane_rst = false,
  664. .has_pwrdn_delay = true,
  665. .pwrdn_delay_min = 995, /* us */
  666. .pwrdn_delay_max = 1005, /* us */
  667. };
  668. static const struct qmp_phy_cfg qmp_v3_usb3phy_cfg = {
  669. .type = PHY_TYPE_USB3,
  670. .nlanes = 1,
  671. .serdes_tbl = qmp_v3_usb3_serdes_tbl,
  672. .serdes_tbl_num = ARRAY_SIZE(qmp_v3_usb3_serdes_tbl),
  673. .tx_tbl = qmp_v3_usb3_tx_tbl,
  674. .tx_tbl_num = ARRAY_SIZE(qmp_v3_usb3_tx_tbl),
  675. .rx_tbl = qmp_v3_usb3_rx_tbl,
  676. .rx_tbl_num = ARRAY_SIZE(qmp_v3_usb3_rx_tbl),
  677. .pcs_tbl = qmp_v3_usb3_pcs_tbl,
  678. .pcs_tbl_num = ARRAY_SIZE(qmp_v3_usb3_pcs_tbl),
  679. .clk_list = qmp_v3_phy_clk_l,
  680. .num_clks = ARRAY_SIZE(qmp_v3_phy_clk_l),
  681. .reset_list = msm8996_usb3phy_reset_l,
  682. .num_resets = ARRAY_SIZE(msm8996_usb3phy_reset_l),
  683. .vreg_list = msm8996_phy_vreg_l,
  684. .num_vregs = ARRAY_SIZE(msm8996_phy_vreg_l),
  685. .regs = qmp_v3_usb3phy_regs_layout,
  686. .start_ctrl = SERDES_START | PCS_START,
  687. .pwrdn_ctrl = SW_PWRDN,
  688. .mask_pcs_ready = PHYSTATUS,
  689. .pwrdn_delay_min = POWER_DOWN_DELAY_US_MIN,
  690. .pwrdn_delay_max = POWER_DOWN_DELAY_US_MAX,
  691. .has_phy_dp_com_ctrl = true,
  692. .tx_b_lane_offset = 0x400,
  693. .rx_b_lane_offset = 0x400,
  694. };
  695. static void qcom_qmp_phy_configure(void __iomem *base,
  696. const unsigned int *regs,
  697. const struct qmp_phy_init_tbl tbl[],
  698. int num)
  699. {
  700. int i;
  701. const struct qmp_phy_init_tbl *t = tbl;
  702. if (!t)
  703. return;
  704. for (i = 0; i < num; i++, t++) {
  705. if (t->in_layout)
  706. writel(t->val, base + regs[t->offset]);
  707. else
  708. writel(t->val, base + t->offset);
  709. }
  710. }
  711. static int qcom_qmp_phy_poweron(struct phy *phy)
  712. {
  713. struct qmp_phy *qphy = phy_get_drvdata(phy);
  714. struct qcom_qmp *qmp = qphy->qmp;
  715. int ret;
  716. ret = clk_prepare_enable(qphy->pipe_clk);
  717. if (ret)
  718. dev_err(qmp->dev, "pipe_clk enable failed, err=%d\n", ret);
  719. return ret;
  720. }
  721. static int qcom_qmp_phy_com_init(struct qcom_qmp *qmp)
  722. {
  723. const struct qmp_phy_cfg *cfg = qmp->cfg;
  724. void __iomem *serdes = qmp->serdes;
  725. void __iomem *dp_com = qmp->dp_com;
  726. int ret, i;
  727. mutex_lock(&qmp->phy_mutex);
  728. if (qmp->init_count++) {
  729. mutex_unlock(&qmp->phy_mutex);
  730. return 0;
  731. }
  732. /* turn on regulator supplies */
  733. ret = regulator_bulk_enable(cfg->num_vregs, qmp->vregs);
  734. if (ret) {
  735. dev_err(qmp->dev, "failed to enable regulators, err=%d\n", ret);
  736. goto err_reg_enable;
  737. }
  738. for (i = 0; i < cfg->num_resets; i++) {
  739. ret = reset_control_assert(qmp->resets[i]);
  740. if (ret) {
  741. dev_err(qmp->dev, "%s reset assert failed\n",
  742. cfg->reset_list[i]);
  743. goto err_rst_assert;
  744. }
  745. }
  746. for (i = cfg->num_resets - 1; i >= 0; i--) {
  747. ret = reset_control_deassert(qmp->resets[i]);
  748. if (ret) {
  749. dev_err(qmp->dev, "%s reset deassert failed\n",
  750. qmp->cfg->reset_list[i]);
  751. goto err_rst;
  752. }
  753. }
  754. ret = clk_bulk_prepare_enable(cfg->num_clks, qmp->clks);
  755. if (ret) {
  756. dev_err(qmp->dev, "failed to enable clks, err=%d\n", ret);
  757. goto err_rst;
  758. }
  759. if (cfg->has_phy_com_ctrl)
  760. qphy_setbits(serdes, cfg->regs[QPHY_COM_POWER_DOWN_CONTROL],
  761. SW_PWRDN);
  762. if (cfg->has_phy_dp_com_ctrl) {
  763. qphy_setbits(dp_com, QPHY_V3_DP_COM_POWER_DOWN_CTRL,
  764. SW_PWRDN);
  765. /* override hardware control for reset of qmp phy */
  766. qphy_setbits(dp_com, QPHY_V3_DP_COM_RESET_OVRD_CTRL,
  767. SW_DPPHY_RESET_MUX | SW_DPPHY_RESET |
  768. SW_USB3PHY_RESET_MUX | SW_USB3PHY_RESET);
  769. qphy_setbits(dp_com, QPHY_V3_DP_COM_PHY_MODE_CTRL,
  770. USB3_MODE | DP_MODE);
  771. /* bring both QMP USB and QMP DP PHYs PCS block out of reset */
  772. qphy_clrbits(dp_com, QPHY_V3_DP_COM_RESET_OVRD_CTRL,
  773. SW_DPPHY_RESET_MUX | SW_DPPHY_RESET |
  774. SW_USB3PHY_RESET_MUX | SW_USB3PHY_RESET);
  775. }
  776. /* Serdes configuration */
  777. qcom_qmp_phy_configure(serdes, cfg->regs, cfg->serdes_tbl,
  778. cfg->serdes_tbl_num);
  779. if (cfg->has_phy_com_ctrl) {
  780. void __iomem *status;
  781. unsigned int mask, val;
  782. qphy_clrbits(serdes, cfg->regs[QPHY_COM_SW_RESET], SW_RESET);
  783. qphy_setbits(serdes, cfg->regs[QPHY_COM_START_CONTROL],
  784. SERDES_START | PCS_START);
  785. status = serdes + cfg->regs[QPHY_COM_PCS_READY_STATUS];
  786. mask = cfg->mask_com_pcs_ready;
  787. ret = readl_poll_timeout(status, val, (val & mask), 10,
  788. PHY_INIT_COMPLETE_TIMEOUT);
  789. if (ret) {
  790. dev_err(qmp->dev,
  791. "phy common block init timed-out\n");
  792. goto err_com_init;
  793. }
  794. }
  795. mutex_unlock(&qmp->phy_mutex);
  796. return 0;
  797. err_com_init:
  798. clk_bulk_disable_unprepare(cfg->num_clks, qmp->clks);
  799. err_rst:
  800. while (++i < cfg->num_resets)
  801. reset_control_assert(qmp->resets[i]);
  802. err_rst_assert:
  803. regulator_bulk_disable(cfg->num_vregs, qmp->vregs);
  804. err_reg_enable:
  805. mutex_unlock(&qmp->phy_mutex);
  806. return ret;
  807. }
  808. static int qcom_qmp_phy_com_exit(struct qcom_qmp *qmp)
  809. {
  810. const struct qmp_phy_cfg *cfg = qmp->cfg;
  811. void __iomem *serdes = qmp->serdes;
  812. int i = cfg->num_resets;
  813. mutex_lock(&qmp->phy_mutex);
  814. if (--qmp->init_count) {
  815. mutex_unlock(&qmp->phy_mutex);
  816. return 0;
  817. }
  818. if (cfg->has_phy_com_ctrl) {
  819. qphy_setbits(serdes, cfg->regs[QPHY_COM_START_CONTROL],
  820. SERDES_START | PCS_START);
  821. qphy_clrbits(serdes, cfg->regs[QPHY_COM_SW_RESET],
  822. SW_RESET);
  823. qphy_setbits(serdes, cfg->regs[QPHY_COM_POWER_DOWN_CONTROL],
  824. SW_PWRDN);
  825. }
  826. while (--i >= 0)
  827. reset_control_assert(qmp->resets[i]);
  828. clk_bulk_disable_unprepare(cfg->num_clks, qmp->clks);
  829. regulator_bulk_disable(cfg->num_vregs, qmp->vregs);
  830. mutex_unlock(&qmp->phy_mutex);
  831. return 0;
  832. }
  833. /* PHY Initialization */
  834. static int qcom_qmp_phy_init(struct phy *phy)
  835. {
  836. struct qmp_phy *qphy = phy_get_drvdata(phy);
  837. struct qcom_qmp *qmp = qphy->qmp;
  838. const struct qmp_phy_cfg *cfg = qmp->cfg;
  839. void __iomem *tx = qphy->tx;
  840. void __iomem *rx = qphy->rx;
  841. void __iomem *pcs = qphy->pcs;
  842. void __iomem *dp_com = qmp->dp_com;
  843. void __iomem *status;
  844. unsigned int mask, val;
  845. int ret;
  846. dev_vdbg(qmp->dev, "Initializing QMP phy\n");
  847. ret = qcom_qmp_phy_com_init(qmp);
  848. if (ret)
  849. return ret;
  850. if (cfg->has_lane_rst) {
  851. ret = reset_control_deassert(qphy->lane_rst);
  852. if (ret) {
  853. dev_err(qmp->dev, "lane%d reset deassert failed\n",
  854. qphy->index);
  855. goto err_lane_rst;
  856. }
  857. }
  858. /* Tx, Rx, and PCS configurations */
  859. qcom_qmp_phy_configure(tx, cfg->regs, cfg->tx_tbl, cfg->tx_tbl_num);
  860. /* Configuration for other LANE for USB-DP combo PHY */
  861. if (cfg->has_phy_dp_com_ctrl)
  862. qcom_qmp_phy_configure(tx + cfg->tx_b_lane_offset, cfg->regs,
  863. cfg->tx_tbl, cfg->tx_tbl_num);
  864. qcom_qmp_phy_configure(rx, cfg->regs, cfg->rx_tbl, cfg->rx_tbl_num);
  865. if (cfg->has_phy_dp_com_ctrl)
  866. qcom_qmp_phy_configure(rx + cfg->rx_b_lane_offset, cfg->regs,
  867. cfg->rx_tbl, cfg->rx_tbl_num);
  868. qcom_qmp_phy_configure(pcs, cfg->regs, cfg->pcs_tbl, cfg->pcs_tbl_num);
  869. /*
  870. * Pull out PHY from POWER DOWN state.
  871. * This is active low enable signal to power-down PHY.
  872. */
  873. qphy_setbits(pcs, QPHY_POWER_DOWN_CONTROL, cfg->pwrdn_ctrl);
  874. if (cfg->has_pwrdn_delay)
  875. usleep_range(cfg->pwrdn_delay_min, cfg->pwrdn_delay_max);
  876. /* Pull PHY out of reset state */
  877. qphy_clrbits(pcs, cfg->regs[QPHY_SW_RESET], SW_RESET);
  878. if (cfg->has_phy_dp_com_ctrl)
  879. qphy_clrbits(dp_com, QPHY_V3_DP_COM_SW_RESET, SW_RESET);
  880. /* start SerDes and Phy-Coding-Sublayer */
  881. qphy_setbits(pcs, cfg->regs[QPHY_START_CTRL], cfg->start_ctrl);
  882. status = pcs + cfg->regs[QPHY_PCS_READY_STATUS];
  883. mask = cfg->mask_pcs_ready;
  884. ret = readl_poll_timeout(status, val, !(val & mask), 1,
  885. PHY_INIT_COMPLETE_TIMEOUT);
  886. if (ret) {
  887. dev_err(qmp->dev, "phy initialization timed-out\n");
  888. goto err_pcs_ready;
  889. }
  890. qmp->phy_initialized = true;
  891. return ret;
  892. err_pcs_ready:
  893. if (cfg->has_lane_rst)
  894. reset_control_assert(qphy->lane_rst);
  895. err_lane_rst:
  896. qcom_qmp_phy_com_exit(qmp);
  897. return ret;
  898. }
  899. static int qcom_qmp_phy_exit(struct phy *phy)
  900. {
  901. struct qmp_phy *qphy = phy_get_drvdata(phy);
  902. struct qcom_qmp *qmp = qphy->qmp;
  903. const struct qmp_phy_cfg *cfg = qmp->cfg;
  904. clk_disable_unprepare(qphy->pipe_clk);
  905. /* PHY reset */
  906. qphy_setbits(qphy->pcs, cfg->regs[QPHY_SW_RESET], SW_RESET);
  907. /* stop SerDes and Phy-Coding-Sublayer */
  908. qphy_clrbits(qphy->pcs, cfg->regs[QPHY_START_CTRL], cfg->start_ctrl);
  909. /* Put PHY into POWER DOWN state: active low */
  910. qphy_clrbits(qphy->pcs, QPHY_POWER_DOWN_CONTROL, cfg->pwrdn_ctrl);
  911. if (cfg->has_lane_rst)
  912. reset_control_assert(qphy->lane_rst);
  913. qcom_qmp_phy_com_exit(qmp);
  914. qmp->phy_initialized = false;
  915. return 0;
  916. }
  917. static int qcom_qmp_phy_set_mode(struct phy *phy, enum phy_mode mode)
  918. {
  919. struct qmp_phy *qphy = phy_get_drvdata(phy);
  920. struct qcom_qmp *qmp = qphy->qmp;
  921. qmp->mode = mode;
  922. return 0;
  923. }
  924. static void qcom_qmp_phy_enable_autonomous_mode(struct qmp_phy *qphy)
  925. {
  926. struct qcom_qmp *qmp = qphy->qmp;
  927. const struct qmp_phy_cfg *cfg = qmp->cfg;
  928. void __iomem *pcs = qphy->pcs;
  929. void __iomem *pcs_misc = qphy->pcs_misc;
  930. u32 intr_mask;
  931. if (qmp->mode == PHY_MODE_USB_HOST_SS ||
  932. qmp->mode == PHY_MODE_USB_DEVICE_SS)
  933. intr_mask = ARCVR_DTCT_EN | ALFPS_DTCT_EN;
  934. else
  935. intr_mask = ARCVR_DTCT_EN | ARCVR_DTCT_EVENT_SEL;
  936. /* Clear any pending interrupts status */
  937. qphy_setbits(pcs, cfg->regs[QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR], IRQ_CLEAR);
  938. /* Writing 1 followed by 0 clears the interrupt */
  939. qphy_clrbits(pcs, cfg->regs[QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR], IRQ_CLEAR);
  940. qphy_clrbits(pcs, cfg->regs[QPHY_PCS_AUTONOMOUS_MODE_CTRL],
  941. ARCVR_DTCT_EN | ALFPS_DTCT_EN | ARCVR_DTCT_EVENT_SEL);
  942. /* Enable required PHY autonomous mode interrupts */
  943. qphy_setbits(pcs, cfg->regs[QPHY_PCS_AUTONOMOUS_MODE_CTRL], intr_mask);
  944. /* Enable i/o clamp_n for autonomous mode */
  945. if (pcs_misc)
  946. qphy_clrbits(pcs_misc, QPHY_V3_PCS_MISC_CLAMP_ENABLE, CLAMP_EN);
  947. }
  948. static void qcom_qmp_phy_disable_autonomous_mode(struct qmp_phy *qphy)
  949. {
  950. struct qcom_qmp *qmp = qphy->qmp;
  951. const struct qmp_phy_cfg *cfg = qmp->cfg;
  952. void __iomem *pcs = qphy->pcs;
  953. void __iomem *pcs_misc = qphy->pcs_misc;
  954. /* Disable i/o clamp_n on resume for normal mode */
  955. if (pcs_misc)
  956. qphy_setbits(pcs_misc, QPHY_V3_PCS_MISC_CLAMP_ENABLE, CLAMP_EN);
  957. qphy_clrbits(pcs, cfg->regs[QPHY_PCS_AUTONOMOUS_MODE_CTRL],
  958. ARCVR_DTCT_EN | ARCVR_DTCT_EVENT_SEL | ALFPS_DTCT_EN);
  959. qphy_setbits(pcs, cfg->regs[QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR], IRQ_CLEAR);
  960. /* Writing 1 followed by 0 clears the interrupt */
  961. qphy_clrbits(pcs, cfg->regs[QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR], IRQ_CLEAR);
  962. }
  963. static int __maybe_unused qcom_qmp_phy_runtime_suspend(struct device *dev)
  964. {
  965. struct qcom_qmp *qmp = dev_get_drvdata(dev);
  966. struct qmp_phy *qphy = qmp->phys[0];
  967. const struct qmp_phy_cfg *cfg = qmp->cfg;
  968. dev_vdbg(dev, "Suspending QMP phy, mode:%d\n", qmp->mode);
  969. /* Supported only for USB3 PHY */
  970. if (cfg->type != PHY_TYPE_USB3)
  971. return 0;
  972. if (!qmp->phy_initialized) {
  973. dev_vdbg(dev, "PHY not initialized, bailing out\n");
  974. return 0;
  975. }
  976. qcom_qmp_phy_enable_autonomous_mode(qphy);
  977. clk_disable_unprepare(qphy->pipe_clk);
  978. clk_bulk_disable_unprepare(cfg->num_clks, qmp->clks);
  979. return 0;
  980. }
  981. static int __maybe_unused qcom_qmp_phy_runtime_resume(struct device *dev)
  982. {
  983. struct qcom_qmp *qmp = dev_get_drvdata(dev);
  984. struct qmp_phy *qphy = qmp->phys[0];
  985. const struct qmp_phy_cfg *cfg = qmp->cfg;
  986. int ret = 0;
  987. dev_vdbg(dev, "Resuming QMP phy, mode:%d\n", qmp->mode);
  988. /* Supported only for USB3 PHY */
  989. if (cfg->type != PHY_TYPE_USB3)
  990. return 0;
  991. if (!qmp->phy_initialized) {
  992. dev_vdbg(dev, "PHY not initialized, bailing out\n");
  993. return 0;
  994. }
  995. ret = clk_bulk_prepare_enable(cfg->num_clks, qmp->clks);
  996. if (ret) {
  997. dev_err(qmp->dev, "failed to enable clks, err=%d\n", ret);
  998. return ret;
  999. }
  1000. ret = clk_prepare_enable(qphy->pipe_clk);
  1001. if (ret) {
  1002. dev_err(dev, "pipe_clk enable failed, err=%d\n", ret);
  1003. clk_bulk_disable_unprepare(cfg->num_clks, qmp->clks);
  1004. return ret;
  1005. }
  1006. qcom_qmp_phy_disable_autonomous_mode(qphy);
  1007. return 0;
  1008. }
  1009. static int qcom_qmp_phy_vreg_init(struct device *dev)
  1010. {
  1011. struct qcom_qmp *qmp = dev_get_drvdata(dev);
  1012. int num = qmp->cfg->num_vregs;
  1013. int i;
  1014. qmp->vregs = devm_kcalloc(dev, num, sizeof(*qmp->vregs), GFP_KERNEL);
  1015. if (!qmp->vregs)
  1016. return -ENOMEM;
  1017. for (i = 0; i < num; i++)
  1018. qmp->vregs[i].supply = qmp->cfg->vreg_list[i];
  1019. return devm_regulator_bulk_get(dev, num, qmp->vregs);
  1020. }
  1021. static int qcom_qmp_phy_reset_init(struct device *dev)
  1022. {
  1023. struct qcom_qmp *qmp = dev_get_drvdata(dev);
  1024. int i;
  1025. qmp->resets = devm_kcalloc(dev, qmp->cfg->num_resets,
  1026. sizeof(*qmp->resets), GFP_KERNEL);
  1027. if (!qmp->resets)
  1028. return -ENOMEM;
  1029. for (i = 0; i < qmp->cfg->num_resets; i++) {
  1030. struct reset_control *rst;
  1031. const char *name = qmp->cfg->reset_list[i];
  1032. rst = devm_reset_control_get(dev, name);
  1033. if (IS_ERR(rst)) {
  1034. dev_err(dev, "failed to get %s reset\n", name);
  1035. return PTR_ERR(rst);
  1036. }
  1037. qmp->resets[i] = rst;
  1038. }
  1039. return 0;
  1040. }
  1041. static int qcom_qmp_phy_clk_init(struct device *dev)
  1042. {
  1043. struct qcom_qmp *qmp = dev_get_drvdata(dev);
  1044. int num = qmp->cfg->num_clks;
  1045. int i;
  1046. qmp->clks = devm_kcalloc(dev, num, sizeof(*qmp->clks), GFP_KERNEL);
  1047. if (!qmp->clks)
  1048. return -ENOMEM;
  1049. for (i = 0; i < num; i++)
  1050. qmp->clks[i].id = qmp->cfg->clk_list[i];
  1051. return devm_clk_bulk_get(dev, num, qmp->clks);
  1052. }
  1053. /*
  1054. * Register a fixed rate pipe clock.
  1055. *
  1056. * The <s>_pipe_clksrc generated by PHY goes to the GCC that gate
  1057. * controls it. The <s>_pipe_clk coming out of the GCC is requested
  1058. * by the PHY driver for its operations.
  1059. * We register the <s>_pipe_clksrc here. The gcc driver takes care
  1060. * of assigning this <s>_pipe_clksrc as parent to <s>_pipe_clk.
  1061. * Below picture shows this relationship.
  1062. *
  1063. * +---------------+
  1064. * | PHY block |<<---------------------------------------+
  1065. * | | |
  1066. * | +-------+ | +-----+ |
  1067. * I/P---^-->| PLL |---^--->pipe_clksrc--->| GCC |--->pipe_clk---+
  1068. * clk | +-------+ | +-----+
  1069. * +---------------+
  1070. */
  1071. static int phy_pipe_clk_register(struct qcom_qmp *qmp, struct device_node *np)
  1072. {
  1073. struct clk_fixed_rate *fixed;
  1074. struct clk_init_data init = { };
  1075. int ret;
  1076. if ((qmp->cfg->type != PHY_TYPE_USB3) &&
  1077. (qmp->cfg->type != PHY_TYPE_PCIE)) {
  1078. /* not all phys register pipe clocks, so return success */
  1079. return 0;
  1080. }
  1081. ret = of_property_read_string(np, "clock-output-names", &init.name);
  1082. if (ret) {
  1083. dev_err(qmp->dev, "%s: No clock-output-names\n", np->name);
  1084. return ret;
  1085. }
  1086. fixed = devm_kzalloc(qmp->dev, sizeof(*fixed), GFP_KERNEL);
  1087. if (!fixed)
  1088. return -ENOMEM;
  1089. init.ops = &clk_fixed_rate_ops;
  1090. /* controllers using QMP phys use 125MHz pipe clock interface */
  1091. fixed->fixed_rate = 125000000;
  1092. fixed->hw.init = &init;
  1093. return devm_clk_hw_register(qmp->dev, &fixed->hw);
  1094. }
  1095. static const struct phy_ops qcom_qmp_phy_gen_ops = {
  1096. .init = qcom_qmp_phy_init,
  1097. .exit = qcom_qmp_phy_exit,
  1098. .power_on = qcom_qmp_phy_poweron,
  1099. .set_mode = qcom_qmp_phy_set_mode,
  1100. .owner = THIS_MODULE,
  1101. };
  1102. static
  1103. int qcom_qmp_phy_create(struct device *dev, struct device_node *np, int id)
  1104. {
  1105. struct qcom_qmp *qmp = dev_get_drvdata(dev);
  1106. struct phy *generic_phy;
  1107. struct qmp_phy *qphy;
  1108. char prop_name[MAX_PROP_NAME];
  1109. int ret;
  1110. qphy = devm_kzalloc(dev, sizeof(*qphy), GFP_KERNEL);
  1111. if (!qphy)
  1112. return -ENOMEM;
  1113. /*
  1114. * Get memory resources for each phy lane:
  1115. * Resources are indexed as: tx -> 0; rx -> 1; pcs -> 2; and
  1116. * pcs_misc (optional) -> 3.
  1117. */
  1118. qphy->tx = of_iomap(np, 0);
  1119. if (!qphy->tx)
  1120. return -ENOMEM;
  1121. qphy->rx = of_iomap(np, 1);
  1122. if (!qphy->rx)
  1123. return -ENOMEM;
  1124. qphy->pcs = of_iomap(np, 2);
  1125. if (!qphy->pcs)
  1126. return -ENOMEM;
  1127. qphy->pcs_misc = of_iomap(np, 3);
  1128. if (!qphy->pcs_misc)
  1129. dev_vdbg(dev, "PHY pcs_misc-reg not used\n");
  1130. /*
  1131. * Get PHY's Pipe clock, if any. USB3 and PCIe are PIPE3
  1132. * based phys, so they essentially have pipe clock. So,
  1133. * we return error in case phy is USB3 or PIPE type.
  1134. * Otherwise, we initialize pipe clock to NULL for
  1135. * all phys that don't need this.
  1136. */
  1137. snprintf(prop_name, sizeof(prop_name), "pipe%d", id);
  1138. qphy->pipe_clk = of_clk_get_by_name(np, prop_name);
  1139. if (IS_ERR(qphy->pipe_clk)) {
  1140. if (qmp->cfg->type == PHY_TYPE_PCIE ||
  1141. qmp->cfg->type == PHY_TYPE_USB3) {
  1142. ret = PTR_ERR(qphy->pipe_clk);
  1143. if (ret != -EPROBE_DEFER)
  1144. dev_err(dev,
  1145. "failed to get lane%d pipe_clk, %d\n",
  1146. id, ret);
  1147. return ret;
  1148. }
  1149. qphy->pipe_clk = NULL;
  1150. }
  1151. /* Get lane reset, if any */
  1152. if (qmp->cfg->has_lane_rst) {
  1153. snprintf(prop_name, sizeof(prop_name), "lane%d", id);
  1154. qphy->lane_rst = of_reset_control_get(np, prop_name);
  1155. if (IS_ERR(qphy->lane_rst)) {
  1156. dev_err(dev, "failed to get lane%d reset\n", id);
  1157. return PTR_ERR(qphy->lane_rst);
  1158. }
  1159. }
  1160. generic_phy = devm_phy_create(dev, np, &qcom_qmp_phy_gen_ops);
  1161. if (IS_ERR(generic_phy)) {
  1162. ret = PTR_ERR(generic_phy);
  1163. dev_err(dev, "failed to create qphy %d\n", ret);
  1164. return ret;
  1165. }
  1166. qphy->phy = generic_phy;
  1167. qphy->index = id;
  1168. qphy->qmp = qmp;
  1169. qmp->phys[id] = qphy;
  1170. phy_set_drvdata(generic_phy, qphy);
  1171. return 0;
  1172. }
  1173. static const struct of_device_id qcom_qmp_phy_of_match_table[] = {
  1174. {
  1175. .compatible = "qcom,msm8996-qmp-pcie-phy",
  1176. .data = &msm8996_pciephy_cfg,
  1177. }, {
  1178. .compatible = "qcom,msm8996-qmp-usb3-phy",
  1179. .data = &msm8996_usb3phy_cfg,
  1180. }, {
  1181. .compatible = "qcom,ipq8074-qmp-pcie-phy",
  1182. .data = &ipq8074_pciephy_cfg,
  1183. }, {
  1184. .compatible = "qcom,qmp-v3-usb3-phy",
  1185. .data = &qmp_v3_usb3phy_cfg,
  1186. },
  1187. { },
  1188. };
  1189. MODULE_DEVICE_TABLE(of, qcom_qmp_phy_of_match_table);
  1190. static const struct dev_pm_ops qcom_qmp_phy_pm_ops = {
  1191. SET_RUNTIME_PM_OPS(qcom_qmp_phy_runtime_suspend,
  1192. qcom_qmp_phy_runtime_resume, NULL)
  1193. };
  1194. static int qcom_qmp_phy_probe(struct platform_device *pdev)
  1195. {
  1196. struct qcom_qmp *qmp;
  1197. struct device *dev = &pdev->dev;
  1198. struct resource *res;
  1199. struct device_node *child;
  1200. struct phy_provider *phy_provider;
  1201. void __iomem *base;
  1202. int num, id;
  1203. int ret;
  1204. qmp = devm_kzalloc(dev, sizeof(*qmp), GFP_KERNEL);
  1205. if (!qmp)
  1206. return -ENOMEM;
  1207. qmp->dev = dev;
  1208. dev_set_drvdata(dev, qmp);
  1209. /* Get the specific init parameters of QMP phy */
  1210. qmp->cfg = of_device_get_match_data(dev);
  1211. if (!qmp->cfg)
  1212. return -EINVAL;
  1213. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1214. base = devm_ioremap_resource(dev, res);
  1215. if (IS_ERR(base))
  1216. return PTR_ERR(base);
  1217. /* per PHY serdes; usually located at base address */
  1218. qmp->serdes = base;
  1219. /* per PHY dp_com; if PHY has dp_com control block */
  1220. if (qmp->cfg->has_phy_dp_com_ctrl) {
  1221. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1222. "dp_com");
  1223. base = devm_ioremap_resource(dev, res);
  1224. if (IS_ERR(base))
  1225. return PTR_ERR(base);
  1226. qmp->dp_com = base;
  1227. }
  1228. mutex_init(&qmp->phy_mutex);
  1229. ret = qcom_qmp_phy_clk_init(dev);
  1230. if (ret)
  1231. return ret;
  1232. ret = qcom_qmp_phy_reset_init(dev);
  1233. if (ret)
  1234. return ret;
  1235. ret = qcom_qmp_phy_vreg_init(dev);
  1236. if (ret) {
  1237. dev_err(dev, "failed to get regulator supplies\n");
  1238. return ret;
  1239. }
  1240. num = of_get_available_child_count(dev->of_node);
  1241. /* do we have a rogue child node ? */
  1242. if (num > qmp->cfg->nlanes)
  1243. return -EINVAL;
  1244. qmp->phys = devm_kcalloc(dev, num, sizeof(*qmp->phys), GFP_KERNEL);
  1245. if (!qmp->phys)
  1246. return -ENOMEM;
  1247. id = 0;
  1248. pm_runtime_set_active(dev);
  1249. pm_runtime_enable(dev);
  1250. /*
  1251. * Prevent runtime pm from being ON by default. Users can enable
  1252. * it using power/control in sysfs.
  1253. */
  1254. pm_runtime_forbid(dev);
  1255. for_each_available_child_of_node(dev->of_node, child) {
  1256. /* Create per-lane phy */
  1257. ret = qcom_qmp_phy_create(dev, child, id);
  1258. if (ret) {
  1259. dev_err(dev, "failed to create lane%d phy, %d\n",
  1260. id, ret);
  1261. pm_runtime_disable(dev);
  1262. return ret;
  1263. }
  1264. /*
  1265. * Register the pipe clock provided by phy.
  1266. * See function description to see details of this pipe clock.
  1267. */
  1268. ret = phy_pipe_clk_register(qmp, child);
  1269. if (ret) {
  1270. dev_err(qmp->dev,
  1271. "failed to register pipe clock source\n");
  1272. pm_runtime_disable(dev);
  1273. return ret;
  1274. }
  1275. id++;
  1276. }
  1277. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  1278. if (!IS_ERR(phy_provider))
  1279. dev_info(dev, "Registered Qcom-QMP phy\n");
  1280. else
  1281. pm_runtime_disable(dev);
  1282. return PTR_ERR_OR_ZERO(phy_provider);
  1283. }
  1284. static struct platform_driver qcom_qmp_phy_driver = {
  1285. .probe = qcom_qmp_phy_probe,
  1286. .driver = {
  1287. .name = "qcom-qmp-phy",
  1288. .pm = &qcom_qmp_phy_pm_ops,
  1289. .of_match_table = qcom_qmp_phy_of_match_table,
  1290. },
  1291. };
  1292. module_platform_driver(qcom_qmp_phy_driver);
  1293. MODULE_AUTHOR("Vivek Gautam <vivek.gautam@codeaurora.org>");
  1294. MODULE_DESCRIPTION("Qualcomm QMP PHY driver");
  1295. MODULE_LICENSE("GPL v2");