dp_pll_5nm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2021-2024 Qualcomm Innovation Center, Inc. All rights reserved.
  4. * Copyright (c) 2016-2021, The Linux Foundation. All rights reserved.
  5. */
  6. /*
  7. * Display Port PLL driver block diagram for branch clocks
  8. *
  9. * +------------------------+ +------------------------+
  10. * | dp_phy_pll_link_clk | | dp_phy_pll_vco_div_clk |
  11. * +------------------------+ +------------------------+
  12. * | |
  13. * | |
  14. * V V
  15. * dp_link_clk dp_pixel_clk
  16. *
  17. *
  18. */
  19. #include <linux/clk.h>
  20. #include <linux/delay.h>
  21. #include <linux/err.h>
  22. #include <linux/iopoll.h>
  23. #include <linux/kernel.h>
  24. #include <linux/regmap.h>
  25. #include "dp_hpd.h"
  26. #include "dp_debug.h"
  27. #include "dp_pll.h"
  28. #define DP_PHY_CFG 0x0010
  29. #define DP_PHY_CFG_1 0x0014
  30. #define DP_PHY_PD_CTL 0x0018
  31. #define DP_PHY_MODE 0x001C
  32. #define DP_PHY_AUX_CFG1 0x0024
  33. #define DP_PHY_AUX_CFG2 0x0028
  34. #define DP_PHY_TX0_TX1_LANE_CTL 0x0078
  35. #define DP_PHY_TX2_TX3_LANE_CTL 0x009C
  36. #define DP_PHY_SPARE0 0x00C8
  37. #define DP_PHY_STATUS 0x00DC
  38. /* Tx registers */
  39. #define TXn_CLKBUF_ENABLE 0x0008
  40. #define TXn_TX_EMP_POST1_LVL 0x000C
  41. #define TXn_TX_DRV_LVL 0x0014
  42. #define TXn_RESET_TSYNC_EN 0x001C
  43. #define TXn_PRE_STALL_LDO_BOOST_EN 0x0020
  44. #define TXn_TX_BAND 0x0024
  45. #define TXn_INTERFACE_SELECT 0x002C
  46. #define TXn_RES_CODE_LANE_OFFSET_TX 0x003C
  47. #define TXn_RES_CODE_LANE_OFFSET_RX 0x0040
  48. #define TXn_TRANSCEIVER_BIAS_EN 0x0054
  49. #define TXn_HIGHZ_DRVR_EN 0x0058
  50. #define TXn_TX_POL_INV 0x005C
  51. #define TXn_PARRATE_REC_DETECT_IDLE_EN 0x0060
  52. /* PLL register offset */
  53. #define QSERDES_COM_BG_TIMER 0x000C
  54. #define QSERDES_COM_SSC_EN_CENTER 0x0010
  55. #define QSERDES_COM_SSC_ADJ_PER1 0x0014
  56. #define QSERDES_COM_SSC_PER1 0x001C
  57. #define QSERDES_COM_SSC_PER2 0x0020
  58. #define QSERDES_COM_SSC_STEP_SIZE1_MODE0 0x0024
  59. #define QSERDES_COM_SSC_STEP_SIZE2_MODE0 0X0028
  60. #define QSERDES_COM_BIAS_EN_CLKBUFLR_EN 0x0044
  61. #define QSERDES_COM_CLK_ENABLE1 0x0048
  62. #define QSERDES_COM_SYS_CLK_CTRL 0x004C
  63. #define QSERDES_COM_SYSCLK_BUF_ENABLE 0x0050
  64. #define QSERDES_COM_PLL_IVCO 0x0058
  65. #define QSERDES_COM_CP_CTRL_MODE0 0x0074
  66. #define QSERDES_COM_PLL_RCTRL_MODE0 0x007C
  67. #define QSERDES_COM_PLL_CCTRL_MODE0 0x0084
  68. #define QSERDES_COM_SYSCLK_EN_SEL 0x0094
  69. #define QSERDES_COM_RESETSM_CNTRL 0x009C
  70. #define QSERDES_COM_LOCK_CMP_EN 0x00A4
  71. #define QSERDES_COM_LOCK_CMP1_MODE0 0x00AC
  72. #define QSERDES_COM_LOCK_CMP2_MODE0 0x00B0
  73. #define QSERDES_COM_DEC_START_MODE0 0x00BC
  74. #define QSERDES_COM_DIV_FRAC_START1_MODE0 0x00CC
  75. #define QSERDES_COM_DIV_FRAC_START2_MODE0 0x00D0
  76. #define QSERDES_COM_DIV_FRAC_START3_MODE0 0x00D4
  77. #define QSERDES_COM_INTEGLOOP_GAIN0_MODE0 0x00EC
  78. #define QSERDES_COM_INTEGLOOP_GAIN1_MODE0 0x00F0
  79. #define QSERDES_COM_VCO_TUNE_CTRL 0x0108
  80. #define QSERDES_COM_VCO_TUNE_MAP 0x010C
  81. #define QSERDES_COM_CMN_STATUS 0x0140
  82. #define QSERDES_COM_CLK_SEL 0x0154
  83. #define QSERDES_COM_HSCLK_SEL 0x0158
  84. #define QSERDES_COM_CORECLK_DIV_MODE0 0x0168
  85. #define QSERDES_COM_CORE_CLK_EN 0x0174
  86. #define QSERDES_COM_C_READY_STATUS 0x0178
  87. #define QSERDES_COM_CMN_CONFIG 0x017C
  88. #define QSERDES_COM_SVS_MODE_CLK_SEL 0x0184
  89. /* Tx tran offsets */
  90. #define DP_TRAN_DRVR_EMP_EN 0x00C0
  91. #define DP_TX_INTERFACE_MODE 0x00C4
  92. /* Tx VMODE offsets */
  93. #define DP_VMODE_CTRL1 0x00C8
  94. #define DP_PHY_PLL_POLL_SLEEP_US 500
  95. #define DP_PHY_PLL_POLL_TIMEOUT_US 10000
  96. #define DP_VCO_RATE_8100MHZDIV1000 8100000UL
  97. #define DP_VCO_RATE_9720MHZDIV1000 9720000UL
  98. #define DP_VCO_RATE_10800MHZDIV1000 10800000UL
  99. #define DP_PLL_NUM_CLKS 2
  100. #define DP_5NM_C_READY BIT(0)
  101. #define DP_5NM_FREQ_DONE BIT(0)
  102. #define DP_5NM_PLL_LOCKED BIT(1)
  103. #define DP_5NM_PHY_READY BIT(1)
  104. #define DP_5NM_TSYNC_DONE BIT(0)
  105. static const struct dp_pll_params pll_params[HSCLK_RATE_MAX] = {
  106. {0x05, 0x3f, 0x00, 0x04, 0x01, 0x69, 0x00, 0x80, 0x07, 0x6f, 0x08, 0x45, 0x06, 0x36, 0x01,
  107. 0x00, 0x00, 0x0f, 0x0a, 0x1f, 0x0a, 0x11},
  108. {0x03, 0x3f, 0x00, 0x08, 0x01, 0x69, 0x00, 0x80, 0x07, 0x0f, 0x0e, 0x45, 0x06, 0x36, 0x01,
  109. 0x00, 0x00, 0x0f, 0x0a, 0x1f, 0x0a, 0x11},
  110. {0x01, 0x3f, 0x00, 0x08, 0x02, 0x8c, 0x00, 0x00, 0x0a, 0x1f, 0x1c, 0x5c, 0x08, 0x36, 0x01,
  111. 0x00, 0x00, 0x0f, 0x0a, 0x1f, 0x0a, 0x11},
  112. {0x00, 0x3f, 0x00, 0x08, 0x00, 0x69, 0x00, 0x80, 0x07, 0x2f, 0x2a, 0x45, 0x06, 0x36, 0x01,
  113. 0x00, 0x00, 0x0f, 0x0a, 0x1f, 0x0a, 0x11},
  114. };
  115. static int set_vco_div(struct dp_pll *pll, unsigned long rate)
  116. {
  117. u32 div, val;
  118. if (!pll)
  119. return -EINVAL;
  120. if (is_gdsc_disabled(pll))
  121. return -EINVAL;
  122. val = dp_pll_read(dp_phy, DP_PHY_VCO_DIV);
  123. val &= ~0x03;
  124. switch (rate) {
  125. case DP_VCO_HSCLK_RATE_1620MHZDIV1000:
  126. case DP_VCO_HSCLK_RATE_2700MHZDIV1000:
  127. div = 2;
  128. val |= 1;
  129. break;
  130. case DP_VCO_HSCLK_RATE_5400MHZDIV1000:
  131. div = 4;
  132. val |= 2;
  133. break;
  134. case DP_VCO_HSCLK_RATE_8100MHZDIV1000:
  135. div = 6;
  136. /* val = 0 for this case, so no update needed */
  137. break;
  138. default:
  139. /* No other link rates are supported */
  140. return -EINVAL;
  141. }
  142. dp_pll_write(dp_phy, DP_PHY_VCO_DIV, val);
  143. /* Make sure the PHY registers writes are done */
  144. wmb();
  145. /*
  146. * Set the rate for the link and pixel clock sources so that the
  147. * linux clock framework can appropriately compute the MND values
  148. * whenever the pixel clock rate is set.
  149. */
  150. clk_set_rate(pll->clk_data->clks[0], pll->vco_rate / 10);
  151. clk_set_rate(pll->clk_data->clks[1], pll->vco_rate / div);
  152. DP_DEBUG("val=%#x div=%x link_clk rate=%lu vco_div_clk rate=%lu\n",
  153. val, div, pll->vco_rate / 10, pll->vco_rate / div);
  154. return 0;
  155. }
  156. static int dp_vco_pll_init_db_5nm(struct dp_pll_db *pdb,
  157. unsigned long rate)
  158. {
  159. struct dp_pll *pll = pdb->pll;
  160. u32 spare_value = 0;
  161. spare_value = dp_pll_read(dp_phy, DP_PHY_SPARE0);
  162. pdb->lane_cnt = spare_value & 0x0F;
  163. pdb->orientation = (spare_value & 0xF0) >> 4;
  164. DP_DEBUG("spare_value=0x%x, ln_cnt=0x%x, orientation=0x%x\n",
  165. spare_value, pdb->lane_cnt, pdb->orientation);
  166. switch (rate) {
  167. case DP_VCO_HSCLK_RATE_1620MHZDIV1000:
  168. DP_DEBUG("VCO rate: %ld\n", DP_VCO_RATE_9720MHZDIV1000);
  169. pdb->rate_idx = HSCLK_RATE_1620MHZ;
  170. break;
  171. case DP_VCO_HSCLK_RATE_2700MHZDIV1000:
  172. DP_DEBUG("VCO rate: %ld\n", DP_VCO_RATE_10800MHZDIV1000);
  173. pdb->rate_idx = HSCLK_RATE_2700MHZ;
  174. break;
  175. case DP_VCO_HSCLK_RATE_5400MHZDIV1000:
  176. DP_DEBUG("VCO rate: %ld\n", DP_VCO_RATE_10800MHZDIV1000);
  177. pdb->rate_idx = HSCLK_RATE_5400MHZ;
  178. break;
  179. case DP_VCO_HSCLK_RATE_8100MHZDIV1000:
  180. DP_DEBUG("VCO rate: %ld\n", DP_VCO_RATE_8100MHZDIV1000);
  181. pdb->rate_idx = HSCLK_RATE_8100MHZ;
  182. break;
  183. default:
  184. DP_ERR("unsupported rate %ld\n", rate);
  185. return -EINVAL;
  186. }
  187. return 0;
  188. }
  189. static int dp_config_vco_rate_5nm(struct dp_pll *pll,
  190. unsigned long rate)
  191. {
  192. int rc = 0;
  193. struct dp_pll_db *pdb = (struct dp_pll_db *)pll->priv;
  194. const struct dp_pll_params *params;
  195. rc = dp_vco_pll_init_db_5nm(pdb, rate);
  196. if (rc < 0) {
  197. DP_ERR("VCO Init DB failed\n");
  198. return rc;
  199. }
  200. dp_pll_write(dp_phy, DP_PHY_CFG_1, 0x0F);
  201. if (pdb->lane_cnt != 4) {
  202. if (pdb->orientation == ORIENTATION_CC2)
  203. dp_pll_write(dp_phy, DP_PHY_PD_CTL, 0x6d);
  204. else
  205. dp_pll_write(dp_phy, DP_PHY_PD_CTL, 0x75);
  206. } else {
  207. dp_pll_write(dp_phy, DP_PHY_PD_CTL, 0x7d);
  208. }
  209. /* Make sure the PHY register writes are done */
  210. wmb();
  211. if (pdb->rate_idx < HSCLK_RATE_MAX) {
  212. params = &pdb->pll_params[pdb->rate_idx];
  213. } else {
  214. DP_ERR("link rate not set\n");
  215. return -EINVAL;
  216. }
  217. dp_pll_write(dp_pll, QSERDES_COM_SVS_MODE_CLK_SEL, 0x05);
  218. dp_pll_write(dp_pll, QSERDES_COM_SYSCLK_EN_SEL, 0x3b);
  219. dp_pll_write(dp_pll, QSERDES_COM_SYS_CLK_CTRL, 0x02);
  220. dp_pll_write(dp_pll, QSERDES_COM_CLK_ENABLE1, 0x0c);
  221. dp_pll_write(dp_pll, QSERDES_COM_SYSCLK_BUF_ENABLE, 0x06);
  222. dp_pll_write(dp_pll, QSERDES_COM_CLK_SEL, 0x30);
  223. /* Make sure the PHY register writes are done */
  224. wmb();
  225. /* PLL Optimization */
  226. dp_pll_write(dp_pll, QSERDES_COM_PLL_IVCO, params->pll_ivco);
  227. dp_pll_write(dp_pll, QSERDES_COM_PLL_CCTRL_MODE0, 0x36);
  228. dp_pll_write(dp_pll, QSERDES_COM_PLL_RCTRL_MODE0, 0x16);
  229. dp_pll_write(dp_pll, QSERDES_COM_CP_CTRL_MODE0, 0x06);
  230. /* Make sure the PLL register writes are done */
  231. wmb();
  232. /* link rate dependent params */
  233. dp_pll_write(dp_pll, QSERDES_COM_HSCLK_SEL, params->hsclk_sel);
  234. dp_pll_write(dp_pll, QSERDES_COM_DEC_START_MODE0, params->dec_start_mode0);
  235. dp_pll_write(dp_pll,
  236. QSERDES_COM_DIV_FRAC_START1_MODE0, params->div_frac_start1_mode0);
  237. dp_pll_write(dp_pll,
  238. QSERDES_COM_DIV_FRAC_START2_MODE0, params->div_frac_start2_mode0);
  239. dp_pll_write(dp_pll,
  240. QSERDES_COM_DIV_FRAC_START3_MODE0, params->div_frac_start3_mode0);
  241. dp_pll_write(dp_pll, QSERDES_COM_LOCK_CMP1_MODE0, params->lock_cmp1_mode0);
  242. dp_pll_write(dp_pll, QSERDES_COM_LOCK_CMP2_MODE0, params->lock_cmp2_mode0);
  243. dp_pll_write(dp_pll, QSERDES_COM_LOCK_CMP_EN, params->lock_cmp_en);
  244. dp_pll_write(dp_phy, DP_PHY_VCO_DIV, params->phy_vco_div);
  245. /* Make sure the PLL register writes are done */
  246. wmb();
  247. dp_pll_write(dp_pll, QSERDES_COM_CMN_CONFIG, 0x02);
  248. dp_pll_write(dp_pll, QSERDES_COM_INTEGLOOP_GAIN0_MODE0,
  249. params->integloop_gain0_mode0);
  250. dp_pll_write(dp_pll, QSERDES_COM_INTEGLOOP_GAIN1_MODE0,
  251. params->integloop_gain1_mode0);
  252. dp_pll_write(dp_pll, QSERDES_COM_VCO_TUNE_MAP, 0x00);
  253. /* Make sure the PHY register writes are done */
  254. wmb();
  255. dp_pll_write(dp_pll, QSERDES_COM_BG_TIMER, params->bg_timer);
  256. dp_pll_write(dp_pll, QSERDES_COM_CORECLK_DIV_MODE0, 0x0a);
  257. dp_pll_write(dp_pll, QSERDES_COM_VCO_TUNE_CTRL, 0x00);
  258. if (pll->bonding_en)
  259. dp_pll_write(dp_pll, QSERDES_COM_BIAS_EN_CLKBUFLR_EN, 0x1f);
  260. else
  261. dp_pll_write(dp_pll, QSERDES_COM_BIAS_EN_CLKBUFLR_EN, 0x17);
  262. dp_pll_write(dp_pll, QSERDES_COM_CORE_CLK_EN, params->core_clk_en);
  263. /* Make sure the PHY register writes are done */
  264. wmb();
  265. if (pll->ssc_en) {
  266. dp_pll_write(dp_pll, QSERDES_COM_SSC_EN_CENTER, 0x01);
  267. dp_pll_write(dp_pll, QSERDES_COM_SSC_ADJ_PER1, 0x00);
  268. dp_pll_write(dp_pll, QSERDES_COM_SSC_PER1, params->ssc_per1);
  269. dp_pll_write(dp_pll, QSERDES_COM_SSC_PER2, params->ssc_per2);
  270. dp_pll_write(dp_pll, QSERDES_COM_SSC_STEP_SIZE1_MODE0,
  271. params->ssc_step_size1_mode0);
  272. dp_pll_write(dp_pll, QSERDES_COM_SSC_STEP_SIZE2_MODE0,
  273. params->ssc_step_size2_mode0);
  274. }
  275. if (pdb->orientation == ORIENTATION_CC2)
  276. dp_pll_write(dp_phy, DP_PHY_MODE, 0x4c);
  277. else
  278. dp_pll_write(dp_phy, DP_PHY_MODE, 0x5c);
  279. dp_pll_write(dp_phy, DP_PHY_AUX_CFG1, 0x13);
  280. dp_pll_write(dp_phy, DP_PHY_AUX_CFG2, 0xA4);
  281. /* Make sure the PLL register writes are done */
  282. wmb();
  283. /* TX-0 register configuration */
  284. dp_pll_write(dp_phy, DP_PHY_TX0_TX1_LANE_CTL, 0x05);
  285. dp_pll_write(dp_ln_tx0, DP_VMODE_CTRL1, 0x40);
  286. dp_pll_write(dp_ln_tx0, TXn_PRE_STALL_LDO_BOOST_EN, 0x30);
  287. dp_pll_write(dp_ln_tx0, TXn_INTERFACE_SELECT, 0x3b);
  288. dp_pll_write(dp_ln_tx0, TXn_CLKBUF_ENABLE, 0x0f);
  289. dp_pll_write(dp_ln_tx0, TXn_RESET_TSYNC_EN, 0x03);
  290. dp_pll_write(dp_ln_tx0, DP_TRAN_DRVR_EMP_EN, 0xf);
  291. dp_pll_write(dp_ln_tx0, TXn_PARRATE_REC_DETECT_IDLE_EN, 0x00);
  292. dp_pll_write(dp_ln_tx0, DP_TX_INTERFACE_MODE, 0x00);
  293. dp_pll_write(dp_ln_tx0, TXn_RES_CODE_LANE_OFFSET_TX, params->lane_offset_tx);
  294. dp_pll_write(dp_ln_tx0, TXn_RES_CODE_LANE_OFFSET_RX, params->lane_offset_rx);
  295. dp_pll_write(dp_ln_tx0, TXn_TX_BAND, 0x04);
  296. /* Make sure the PLL register writes are done */
  297. wmb();
  298. /* TX-1 register configuration */
  299. dp_pll_write(dp_phy, DP_PHY_TX2_TX3_LANE_CTL, 0x05);
  300. dp_pll_write(dp_ln_tx1, DP_VMODE_CTRL1, 0x40);
  301. dp_pll_write(dp_ln_tx1, TXn_PRE_STALL_LDO_BOOST_EN, 0x30);
  302. dp_pll_write(dp_ln_tx1, TXn_INTERFACE_SELECT, 0x3b);
  303. dp_pll_write(dp_ln_tx1, TXn_CLKBUF_ENABLE, 0x0f);
  304. dp_pll_write(dp_ln_tx1, TXn_RESET_TSYNC_EN, 0x03);
  305. dp_pll_write(dp_ln_tx1, DP_TRAN_DRVR_EMP_EN, 0xf);
  306. dp_pll_write(dp_ln_tx1, TXn_PARRATE_REC_DETECT_IDLE_EN, 0x00);
  307. dp_pll_write(dp_ln_tx1, DP_TX_INTERFACE_MODE, 0x00);
  308. dp_pll_write(dp_ln_tx1, TXn_RES_CODE_LANE_OFFSET_TX, params->lane_offset_tx);
  309. dp_pll_write(dp_ln_tx1, TXn_RES_CODE_LANE_OFFSET_RX, params->lane_offset_rx);
  310. dp_pll_write(dp_ln_tx1, TXn_TX_BAND, 0x04);
  311. /* Make sure the PHY register writes are done */
  312. wmb();
  313. return set_vco_div(pll, rate);
  314. }
  315. enum dp_5nm_pll_status {
  316. C_READY,
  317. FREQ_DONE,
  318. PLL_LOCKED,
  319. PHY_READY,
  320. TSYNC_DONE,
  321. };
  322. char *dp_5nm_pll_get_status_name(enum dp_5nm_pll_status status)
  323. {
  324. switch (status) {
  325. case C_READY:
  326. return "C_READY";
  327. case FREQ_DONE:
  328. return "FREQ_DONE";
  329. case PLL_LOCKED:
  330. return "PLL_LOCKED";
  331. case PHY_READY:
  332. return "PHY_READY";
  333. case TSYNC_DONE:
  334. return "TSYNC_DONE";
  335. default:
  336. return "unknown";
  337. }
  338. }
  339. static bool dp_5nm_pll_get_status(struct dp_pll *pll,
  340. enum dp_5nm_pll_status status)
  341. {
  342. u32 reg, state, bit;
  343. void __iomem *base;
  344. bool success = true;
  345. switch (status) {
  346. case C_READY:
  347. base = dp_pll_get_base(dp_pll);
  348. reg = QSERDES_COM_C_READY_STATUS;
  349. bit = DP_5NM_C_READY;
  350. break;
  351. case FREQ_DONE:
  352. base = dp_pll_get_base(dp_pll);
  353. reg = QSERDES_COM_CMN_STATUS;
  354. bit = DP_5NM_FREQ_DONE;
  355. break;
  356. case PLL_LOCKED:
  357. base = dp_pll_get_base(dp_pll);
  358. reg = QSERDES_COM_CMN_STATUS;
  359. bit = DP_5NM_PLL_LOCKED;
  360. break;
  361. case PHY_READY:
  362. base = dp_pll_get_base(dp_phy);
  363. reg = DP_PHY_STATUS;
  364. bit = DP_5NM_PHY_READY;
  365. break;
  366. case TSYNC_DONE:
  367. base = dp_pll_get_base(dp_phy);
  368. reg = DP_PHY_STATUS;
  369. bit = DP_5NM_TSYNC_DONE;
  370. break;
  371. default:
  372. return false;
  373. }
  374. if (readl_poll_timeout_atomic((base + reg), state,
  375. ((state & bit) > 0),
  376. DP_PHY_PLL_POLL_SLEEP_US,
  377. DP_PHY_PLL_POLL_TIMEOUT_US)) {
  378. DP_ERR("%s failed, status=%x\n",
  379. dp_5nm_pll_get_status_name(status), state);
  380. success = false;
  381. }
  382. return success;
  383. }
  384. static int dp_pll_enable_5nm(struct dp_pll *pll)
  385. {
  386. int rc = 0;
  387. pll->aux->state &= ~DP_STATE_PLL_LOCKED;
  388. dp_pll_write(dp_phy, DP_PHY_CFG, 0x01);
  389. dp_pll_write(dp_phy, DP_PHY_CFG, 0x05);
  390. dp_pll_write(dp_phy, DP_PHY_CFG, 0x01);
  391. dp_pll_write(dp_phy, DP_PHY_CFG, 0x09);
  392. dp_pll_write(dp_pll, QSERDES_COM_RESETSM_CNTRL, 0x20);
  393. wmb(); /* Make sure the PLL register writes are done */
  394. if (!dp_5nm_pll_get_status(pll, C_READY)) {
  395. rc = -EINVAL;
  396. goto lock_err;
  397. }
  398. if (!dp_5nm_pll_get_status(pll, FREQ_DONE)) {
  399. rc = -EINVAL;
  400. goto lock_err;
  401. }
  402. if (!dp_5nm_pll_get_status(pll, PLL_LOCKED)) {
  403. rc = -EINVAL;
  404. goto lock_err;
  405. }
  406. dp_pll_write(dp_phy, DP_PHY_CFG, 0x19);
  407. /* Make sure the PHY register writes are done */
  408. wmb();
  409. if (!dp_5nm_pll_get_status(pll, TSYNC_DONE)) {
  410. rc = -EINVAL;
  411. goto lock_err;
  412. }
  413. if (!dp_5nm_pll_get_status(pll, PHY_READY)) {
  414. rc = -EINVAL;
  415. goto lock_err;
  416. }
  417. pll->aux->state |= DP_STATE_PLL_LOCKED;
  418. DP_DEBUG("PLL is locked\n");
  419. lock_err:
  420. return rc;
  421. }
  422. static void dp_pll_disable_5nm(struct dp_pll *pll)
  423. {
  424. /* Assert DP PHY power down */
  425. dp_pll_write(dp_phy, DP_PHY_PD_CTL, 0x2);
  426. /*
  427. * Make sure all the register writes to disable PLL are
  428. * completed before doing any other operation
  429. */
  430. wmb();
  431. }
  432. static int dp_vco_set_rate_5nm(struct dp_pll *pll, unsigned long rate)
  433. {
  434. int rc = 0;
  435. if (!pll) {
  436. DP_ERR("invalid input parameters\n");
  437. return -EINVAL;
  438. }
  439. DP_DEBUG("DP lane CLK rate=%ld\n", rate);
  440. rc = dp_config_vco_rate_5nm(pll, rate);
  441. if (rc < 0) {
  442. DP_ERR("Failed to set clk rate\n");
  443. return rc;
  444. }
  445. return rc;
  446. }
  447. static int dp_regulator_enable_5nm(struct dp_parser *parser,
  448. enum dp_pm_type pm_type, bool enable)
  449. {
  450. int rc = 0;
  451. struct dss_module_power mp;
  452. if (pm_type < DP_CORE_PM || pm_type >= DP_MAX_PM) {
  453. DP_ERR("invalid resource: %d %s\n", pm_type,
  454. dp_parser_pm_name(pm_type));
  455. return -EINVAL;
  456. }
  457. mp = parser->mp[pm_type];
  458. rc = msm_dss_enable_vreg(mp.vreg_config, mp.num_vreg, enable);
  459. if (rc) {
  460. DP_ERR("failed to '%s' vregs for %s\n",
  461. enable ? "enable" : "disable",
  462. dp_parser_pm_name(pm_type));
  463. return rc;
  464. }
  465. DP_DEBUG("success: '%s' vregs for %s\n", enable ? "enable" : "disable",
  466. dp_parser_pm_name(pm_type));
  467. return rc;
  468. }
  469. static int dp_pll_configure(struct dp_pll *pll, unsigned long rate)
  470. {
  471. int rc = 0;
  472. if (!pll || !rate) {
  473. DP_ERR("invalid input parameters rate = %lu\n", rate);
  474. return -EINVAL;
  475. }
  476. rate = rate * 10;
  477. if (rate <= DP_VCO_HSCLK_RATE_1620MHZDIV1000)
  478. rate = DP_VCO_HSCLK_RATE_1620MHZDIV1000;
  479. else if (rate <= DP_VCO_HSCLK_RATE_2700MHZDIV1000)
  480. rate = DP_VCO_HSCLK_RATE_2700MHZDIV1000;
  481. else if (rate <= DP_VCO_HSCLK_RATE_5400MHZDIV1000)
  482. rate = DP_VCO_HSCLK_RATE_5400MHZDIV1000;
  483. else
  484. rate = DP_VCO_HSCLK_RATE_8100MHZDIV1000;
  485. pll->vco_rate = rate;
  486. rc = dp_vco_set_rate_5nm(pll, rate);
  487. if (rc < 0) {
  488. DP_ERR("pll rate %s set failed\n", rate);
  489. pll->vco_rate = 0;
  490. return rc;
  491. }
  492. DP_DEBUG("pll rate %lu set success\n", rate);
  493. return rc;
  494. }
  495. static int dp_pll_prepare(struct dp_pll *pll)
  496. {
  497. int rc = 0;
  498. if (!pll) {
  499. DP_ERR("invalid input parameters\n");
  500. return -EINVAL;
  501. }
  502. /*
  503. * Enable DP_PM_PLL regulator if the PLL revision is 5nm-V1 and the
  504. * link rate is 8.1Gbps. This will result in voting to place Mx rail in
  505. * turbo as required for V1 hardware PLL functionality.
  506. */
  507. if (pll->revision == DP_PLL_5NM_V1 &&
  508. pll->vco_rate == DP_VCO_HSCLK_RATE_8100MHZDIV1000) {
  509. rc = dp_regulator_enable_5nm(pll->parser, DP_PLL_PM, true);
  510. if (rc < 0) {
  511. DP_ERR("enable pll power failed\n");
  512. return rc;
  513. }
  514. }
  515. rc = dp_pll_enable_5nm(pll);
  516. if (rc < 0)
  517. DP_ERR("ndx=%d failed to enable dp pll\n", pll->index);
  518. return rc;
  519. }
  520. static int dp_pll_unprepare(struct dp_pll *pll)
  521. {
  522. int rc = 0;
  523. if (!pll) {
  524. DP_ERR("invalid input parameter\n");
  525. return -EINVAL;
  526. }
  527. if (pll->revision == DP_PLL_5NM_V1 &&
  528. pll->vco_rate == DP_VCO_HSCLK_RATE_8100MHZDIV1000) {
  529. rc = dp_regulator_enable_5nm(pll->parser, DP_PLL_PM, false);
  530. if (rc < 0) {
  531. DP_ERR("disable pll power failed\n");
  532. return rc;
  533. }
  534. }
  535. dp_pll_disable_5nm(pll);
  536. pll->vco_rate = 0;
  537. return rc;
  538. }
  539. unsigned long dp_vco_recalc_rate_5nm(struct dp_pll *pll)
  540. {
  541. u32 hsclk_sel, link_clk_divsel, hsclk_div, link_clk_div = 0;
  542. unsigned long vco_rate = 0;
  543. if (!pll) {
  544. DP_ERR("invalid input parameters\n");
  545. return -EINVAL;
  546. }
  547. if (is_gdsc_disabled(pll))
  548. return 0;
  549. hsclk_sel = dp_pll_read(dp_pll, QSERDES_COM_HSCLK_SEL);
  550. hsclk_sel &= 0x0f;
  551. switch (hsclk_sel) {
  552. case 5:
  553. hsclk_div = 5;
  554. break;
  555. case 3:
  556. hsclk_div = 3;
  557. break;
  558. case 1:
  559. hsclk_div = 2;
  560. break;
  561. case 0:
  562. hsclk_div = 1;
  563. break;
  564. default:
  565. DP_DEBUG("unknown divider. forcing to default\n");
  566. hsclk_div = 5;
  567. break;
  568. }
  569. link_clk_divsel = dp_pll_read(dp_phy, DP_PHY_AUX_CFG2);
  570. link_clk_divsel >>= 2;
  571. link_clk_divsel &= 0x3;
  572. if (link_clk_divsel == 0)
  573. link_clk_div = 5;
  574. else if (link_clk_divsel == 1)
  575. link_clk_div = 10;
  576. else if (link_clk_divsel == 2)
  577. link_clk_div = 20;
  578. else
  579. DP_ERR("unsupported div. Phy_mode: %d\n", link_clk_divsel);
  580. if (link_clk_div == 20) {
  581. vco_rate = DP_VCO_HSCLK_RATE_2700MHZDIV1000;
  582. } else {
  583. if (hsclk_div == 5)
  584. vco_rate = DP_VCO_HSCLK_RATE_1620MHZDIV1000;
  585. else if (hsclk_div == 3)
  586. vco_rate = DP_VCO_HSCLK_RATE_2700MHZDIV1000;
  587. else if (hsclk_div == 2)
  588. vco_rate = DP_VCO_HSCLK_RATE_5400MHZDIV1000;
  589. else
  590. vco_rate = DP_VCO_HSCLK_RATE_8100MHZDIV1000;
  591. }
  592. DP_DEBUG("hsclk: sel=0x%x, div=0x%x; lclk: sel=%u, div=%u, rate=%lu\n",
  593. hsclk_sel, hsclk_div, link_clk_divsel, link_clk_div, vco_rate);
  594. return vco_rate;
  595. }
  596. static unsigned long dp_pll_link_clk_recalc_rate(struct clk_hw *hw,
  597. unsigned long parent_rate)
  598. {
  599. struct dp_pll *pll = NULL;
  600. struct dp_pll_vco_clk *pll_link = NULL;
  601. unsigned long rate = 0;
  602. if (!hw) {
  603. DP_ERR("invalid input parameters\n");
  604. return -EINVAL;
  605. }
  606. pll_link = to_dp_vco_hw(hw);
  607. pll = pll_link->priv;
  608. rate = pll->vco_rate * pll->clk_factor / 10;
  609. return rate;
  610. }
  611. static long dp_pll_link_clk_round(struct clk_hw *hw, unsigned long rate,
  612. unsigned long *parent_rate)
  613. {
  614. struct dp_pll *pll = NULL;
  615. struct dp_pll_vco_clk *pll_link = NULL;
  616. if (!hw) {
  617. DP_ERR("invalid input parameters\n");
  618. return -EINVAL;
  619. }
  620. pll_link = to_dp_vco_hw(hw);
  621. pll = pll_link->priv;
  622. rate = pll->vco_rate * pll->clk_factor / 10;
  623. return rate;
  624. }
  625. static unsigned long dp_pll_vco_div_clk_get_rate(struct dp_pll *pll)
  626. {
  627. if (pll->vco_rate == DP_VCO_HSCLK_RATE_8100MHZDIV1000)
  628. return (pll->vco_rate / (6 * pll->clk_factor));
  629. else if (pll->vco_rate == DP_VCO_HSCLK_RATE_5400MHZDIV1000)
  630. return (pll->vco_rate / (4 * pll->clk_factor));
  631. else
  632. return (pll->vco_rate / (2 * pll->clk_factor));
  633. }
  634. static unsigned long dp_pll_vco_div_clk_recalc_rate(struct clk_hw *hw,
  635. unsigned long parent_rate)
  636. {
  637. struct dp_pll *pll = NULL;
  638. struct dp_pll_vco_clk *pll_link = NULL;
  639. if (!hw) {
  640. DP_ERR("invalid input parameters\n");
  641. return -EINVAL;
  642. }
  643. pll_link = to_dp_vco_hw(hw);
  644. pll = pll_link->priv;
  645. return dp_pll_vco_div_clk_get_rate(pll);
  646. }
  647. static long dp_pll_vco_div_clk_round(struct clk_hw *hw, unsigned long rate,
  648. unsigned long *parent_rate)
  649. {
  650. return dp_pll_vco_div_clk_recalc_rate(hw, *parent_rate);
  651. }
  652. static const struct clk_ops pll_link_clk_ops = {
  653. .recalc_rate = dp_pll_link_clk_recalc_rate,
  654. .round_rate = dp_pll_link_clk_round,
  655. };
  656. static const struct clk_ops pll_vco_div_clk_ops = {
  657. .recalc_rate = dp_pll_vco_div_clk_recalc_rate,
  658. .round_rate = dp_pll_vco_div_clk_round,
  659. };
  660. static struct dp_pll_vco_clk dp0_phy_pll_clks[DP_PLL_NUM_CLKS] = {
  661. {
  662. .hw.init = &(struct clk_init_data) {
  663. .name = "dp0_phy_pll_link_clk",
  664. .ops = &pll_link_clk_ops,
  665. },
  666. },
  667. {
  668. .hw.init = &(struct clk_init_data) {
  669. .name = "dp0_phy_pll_vco_div_clk",
  670. .ops = &pll_vco_div_clk_ops,
  671. },
  672. },
  673. };
  674. static struct dp_pll_vco_clk dp_phy_pll_clks[DP_PLL_NUM_CLKS] = {
  675. {
  676. .hw.init = &(struct clk_init_data) {
  677. .name = "dp_phy_pll_link_clk",
  678. .ops = &pll_link_clk_ops,
  679. },
  680. },
  681. {
  682. .hw.init = &(struct clk_init_data) {
  683. .name = "dp_phy_pll_vco_div_clk",
  684. .ops = &pll_vco_div_clk_ops,
  685. },
  686. },
  687. };
  688. static struct dp_pll_db dp_pdb;
  689. int dp_pll_clock_register_5nm(struct dp_pll *pll)
  690. {
  691. int rc = 0;
  692. struct platform_device *pdev;
  693. struct dp_pll_vco_clk *pll_clks;
  694. if (!pll) {
  695. DP_ERR("pll data not initialized\n");
  696. return -EINVAL;
  697. }
  698. pdev = pll->pdev;
  699. pll->clk_data = kzalloc(sizeof(*pll->clk_data), GFP_KERNEL);
  700. if (!pll->clk_data)
  701. return -ENOMEM;
  702. pll->clk_data->clks = kcalloc(DP_PLL_NUM_CLKS, sizeof(struct clk *),
  703. GFP_KERNEL);
  704. if (!pll->clk_data->clks) {
  705. kfree(pll->clk_data);
  706. return -ENOMEM;
  707. }
  708. pll->clk_data->clk_num = DP_PLL_NUM_CLKS;
  709. pll->priv = &dp_pdb;
  710. dp_pdb.pll = pll;
  711. dp_pdb.pll_params = pll_params;
  712. pll->pll_cfg = dp_pll_configure;
  713. pll->pll_prepare = dp_pll_prepare;
  714. pll->pll_unprepare = dp_pll_unprepare;
  715. if (pll->dp_core_revision >= 0x10040000)
  716. pll_clks = dp0_phy_pll_clks;
  717. else
  718. pll_clks = dp_phy_pll_clks;
  719. rc = dp_pll_clock_register_helper(pll, pll_clks, DP_PLL_NUM_CLKS);
  720. if (rc) {
  721. DP_ERR("Clock register failed rc=%d\n", rc);
  722. goto clk_reg_fail;
  723. }
  724. rc = of_clk_add_provider(pdev->dev.of_node,
  725. of_clk_src_onecell_get, pll->clk_data);
  726. if (rc) {
  727. DP_ERR("Clock add provider failed rc=%d\n", rc);
  728. goto clk_reg_fail;
  729. }
  730. DP_DEBUG("success\n");
  731. return rc;
  732. clk_reg_fail:
  733. dp_pll_clock_unregister_5nm(pll);
  734. return rc;
  735. }
  736. void dp_pll_clock_unregister_5nm(struct dp_pll *pll)
  737. {
  738. kfree(pll->clk_data->clks);
  739. kfree(pll->clk_data);
  740. }