phy-qcom-qmp.c 38 KB

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