clk-alpha-pll.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  2. * Copyright (c) 2015, The Linux Foundation. All rights reserved.
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License version 2, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  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. #include <linux/kernel.h>
  14. #include <linux/export.h>
  15. #include <linux/clk-provider.h>
  16. #include <linux/regmap.h>
  17. #include <linux/delay.h>
  18. #include "clk-alpha-pll.h"
  19. #include "common.h"
  20. #define PLL_MODE 0x00
  21. # define PLL_OUTCTRL BIT(0)
  22. # define PLL_BYPASSNL BIT(1)
  23. # define PLL_RESET_N BIT(2)
  24. # define PLL_OFFLINE_REQ BIT(7)
  25. # define PLL_LOCK_COUNT_SHIFT 8
  26. # define PLL_LOCK_COUNT_MASK 0x3f
  27. # define PLL_BIAS_COUNT_SHIFT 14
  28. # define PLL_BIAS_COUNT_MASK 0x3f
  29. # define PLL_VOTE_FSM_ENA BIT(20)
  30. # define PLL_FSM_ENA BIT(20)
  31. # define PLL_VOTE_FSM_RESET BIT(21)
  32. # define PLL_OFFLINE_ACK BIT(28)
  33. # define PLL_ACTIVE_FLAG BIT(30)
  34. # define PLL_LOCK_DET BIT(31)
  35. #define PLL_L_VAL 0x04
  36. #define PLL_ALPHA_VAL 0x08
  37. #define PLL_ALPHA_VAL_U 0x0c
  38. #define PLL_USER_CTL 0x10
  39. # define PLL_POST_DIV_SHIFT 8
  40. # define PLL_POST_DIV_MASK 0xf
  41. # define PLL_ALPHA_EN BIT(24)
  42. # define PLL_VCO_SHIFT 20
  43. # define PLL_VCO_MASK 0x3
  44. #define PLL_USER_CTL_U 0x14
  45. #define PLL_CONFIG_CTL 0x18
  46. #define PLL_CONFIG_CTL_U 0x20
  47. #define PLL_TEST_CTL 0x1c
  48. #define PLL_TEST_CTL_U 0x20
  49. #define PLL_STATUS 0x24
  50. /*
  51. * Even though 40 bits are present, use only 32 for ease of calculation.
  52. */
  53. #define ALPHA_REG_BITWIDTH 40
  54. #define ALPHA_BITWIDTH 32
  55. #define ALPHA_16BIT_MASK 0xffff
  56. #define to_clk_alpha_pll(_hw) container_of(to_clk_regmap(_hw), \
  57. struct clk_alpha_pll, clkr)
  58. #define to_clk_alpha_pll_postdiv(_hw) container_of(to_clk_regmap(_hw), \
  59. struct clk_alpha_pll_postdiv, clkr)
  60. static int wait_for_pll(struct clk_alpha_pll *pll, u32 mask, bool inverse,
  61. const char *action)
  62. {
  63. u32 val, off;
  64. int count;
  65. int ret;
  66. const char *name = clk_hw_get_name(&pll->clkr.hw);
  67. off = pll->offset;
  68. ret = regmap_read(pll->clkr.regmap, off + PLL_MODE, &val);
  69. if (ret)
  70. return ret;
  71. for (count = 100; count > 0; count--) {
  72. ret = regmap_read(pll->clkr.regmap, off + PLL_MODE, &val);
  73. if (ret)
  74. return ret;
  75. if (inverse && !(val & mask))
  76. return 0;
  77. else if ((val & mask) == mask)
  78. return 0;
  79. udelay(1);
  80. }
  81. WARN(1, "%s failed to %s!\n", name, action);
  82. return -ETIMEDOUT;
  83. }
  84. #define wait_for_pll_enable_active(pll) \
  85. wait_for_pll(pll, PLL_ACTIVE_FLAG, 0, "enable")
  86. #define wait_for_pll_enable_lock(pll) \
  87. wait_for_pll(pll, PLL_LOCK_DET, 0, "enable")
  88. #define wait_for_pll_disable(pll) \
  89. wait_for_pll(pll, PLL_ACTIVE_FLAG, 1, "disable")
  90. #define wait_for_pll_offline(pll) \
  91. wait_for_pll(pll, PLL_OFFLINE_ACK, 0, "offline")
  92. void clk_alpha_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
  93. const struct alpha_pll_config *config)
  94. {
  95. u32 val, mask;
  96. u32 off = pll->offset;
  97. regmap_write(regmap, off + PLL_L_VAL, config->l);
  98. regmap_write(regmap, off + PLL_ALPHA_VAL, config->alpha);
  99. regmap_write(regmap, off + PLL_CONFIG_CTL, config->config_ctl_val);
  100. regmap_write(regmap, off + PLL_CONFIG_CTL_U, config->config_ctl_hi_val);
  101. val = config->main_output_mask;
  102. val |= config->aux_output_mask;
  103. val |= config->aux2_output_mask;
  104. val |= config->early_output_mask;
  105. val |= config->pre_div_val;
  106. val |= config->post_div_val;
  107. val |= config->vco_val;
  108. mask = config->main_output_mask;
  109. mask |= config->aux_output_mask;
  110. mask |= config->aux2_output_mask;
  111. mask |= config->early_output_mask;
  112. mask |= config->pre_div_mask;
  113. mask |= config->post_div_mask;
  114. mask |= config->vco_mask;
  115. regmap_update_bits(regmap, off + PLL_USER_CTL, mask, val);
  116. if (pll->flags & SUPPORTS_FSM_MODE)
  117. qcom_pll_set_fsm_mode(regmap, off + PLL_MODE, 6, 0);
  118. }
  119. static int clk_alpha_pll_hwfsm_enable(struct clk_hw *hw)
  120. {
  121. int ret;
  122. u32 val, off;
  123. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  124. off = pll->offset;
  125. ret = regmap_read(pll->clkr.regmap, off + PLL_MODE, &val);
  126. if (ret)
  127. return ret;
  128. val |= PLL_FSM_ENA;
  129. if (pll->flags & SUPPORTS_OFFLINE_REQ)
  130. val &= ~PLL_OFFLINE_REQ;
  131. ret = regmap_write(pll->clkr.regmap, off + PLL_MODE, val);
  132. if (ret)
  133. return ret;
  134. /* Make sure enable request goes through before waiting for update */
  135. mb();
  136. return wait_for_pll_enable_active(pll);
  137. }
  138. static void clk_alpha_pll_hwfsm_disable(struct clk_hw *hw)
  139. {
  140. int ret;
  141. u32 val, off;
  142. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  143. off = pll->offset;
  144. ret = regmap_read(pll->clkr.regmap, off + PLL_MODE, &val);
  145. if (ret)
  146. return;
  147. if (pll->flags & SUPPORTS_OFFLINE_REQ) {
  148. ret = regmap_update_bits(pll->clkr.regmap, off + PLL_MODE,
  149. PLL_OFFLINE_REQ, PLL_OFFLINE_REQ);
  150. if (ret)
  151. return;
  152. ret = wait_for_pll_offline(pll);
  153. if (ret)
  154. return;
  155. }
  156. /* Disable hwfsm */
  157. ret = regmap_update_bits(pll->clkr.regmap, off + PLL_MODE,
  158. PLL_FSM_ENA, 0);
  159. if (ret)
  160. return;
  161. wait_for_pll_disable(pll);
  162. }
  163. static int pll_is_enabled(struct clk_hw *hw, u32 mask)
  164. {
  165. int ret;
  166. u32 val, off;
  167. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  168. off = pll->offset;
  169. ret = regmap_read(pll->clkr.regmap, off + PLL_MODE, &val);
  170. if (ret)
  171. return ret;
  172. return !!(val & mask);
  173. }
  174. static int clk_alpha_pll_hwfsm_is_enabled(struct clk_hw *hw)
  175. {
  176. return pll_is_enabled(hw, PLL_ACTIVE_FLAG);
  177. }
  178. static int clk_alpha_pll_is_enabled(struct clk_hw *hw)
  179. {
  180. return pll_is_enabled(hw, PLL_LOCK_DET);
  181. }
  182. static int clk_alpha_pll_enable(struct clk_hw *hw)
  183. {
  184. int ret;
  185. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  186. u32 val, mask, off;
  187. off = pll->offset;
  188. mask = PLL_OUTCTRL | PLL_RESET_N | PLL_BYPASSNL;
  189. ret = regmap_read(pll->clkr.regmap, off + PLL_MODE, &val);
  190. if (ret)
  191. return ret;
  192. /* If in FSM mode, just vote for it */
  193. if (val & PLL_VOTE_FSM_ENA) {
  194. ret = clk_enable_regmap(hw);
  195. if (ret)
  196. return ret;
  197. return wait_for_pll_enable_active(pll);
  198. }
  199. /* Skip if already enabled */
  200. if ((val & mask) == mask)
  201. return 0;
  202. ret = regmap_update_bits(pll->clkr.regmap, off + PLL_MODE,
  203. PLL_BYPASSNL, PLL_BYPASSNL);
  204. if (ret)
  205. return ret;
  206. /*
  207. * H/W requires a 5us delay between disabling the bypass and
  208. * de-asserting the reset.
  209. */
  210. mb();
  211. udelay(5);
  212. ret = regmap_update_bits(pll->clkr.regmap, off + PLL_MODE,
  213. PLL_RESET_N, PLL_RESET_N);
  214. if (ret)
  215. return ret;
  216. ret = wait_for_pll_enable_lock(pll);
  217. if (ret)
  218. return ret;
  219. ret = regmap_update_bits(pll->clkr.regmap, off + PLL_MODE,
  220. PLL_OUTCTRL, PLL_OUTCTRL);
  221. /* Ensure that the write above goes through before returning. */
  222. mb();
  223. return ret;
  224. }
  225. static void clk_alpha_pll_disable(struct clk_hw *hw)
  226. {
  227. int ret;
  228. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  229. u32 val, mask, off;
  230. off = pll->offset;
  231. ret = regmap_read(pll->clkr.regmap, off + PLL_MODE, &val);
  232. if (ret)
  233. return;
  234. /* If in FSM mode, just unvote it */
  235. if (val & PLL_VOTE_FSM_ENA) {
  236. clk_disable_regmap(hw);
  237. return;
  238. }
  239. mask = PLL_OUTCTRL;
  240. regmap_update_bits(pll->clkr.regmap, off + PLL_MODE, mask, 0);
  241. /* Delay of 2 output clock ticks required until output is disabled */
  242. mb();
  243. udelay(1);
  244. mask = PLL_RESET_N | PLL_BYPASSNL;
  245. regmap_update_bits(pll->clkr.regmap, off + PLL_MODE, mask, 0);
  246. }
  247. static unsigned long alpha_pll_calc_rate(u64 prate, u32 l, u32 a)
  248. {
  249. return (prate * l) + ((prate * a) >> ALPHA_BITWIDTH);
  250. }
  251. static unsigned long
  252. alpha_pll_round_rate(unsigned long rate, unsigned long prate, u32 *l, u64 *a)
  253. {
  254. u64 remainder;
  255. u64 quotient;
  256. quotient = rate;
  257. remainder = do_div(quotient, prate);
  258. *l = quotient;
  259. if (!remainder) {
  260. *a = 0;
  261. return rate;
  262. }
  263. /* Upper ALPHA_BITWIDTH bits of Alpha */
  264. quotient = remainder << ALPHA_BITWIDTH;
  265. remainder = do_div(quotient, prate);
  266. if (remainder)
  267. quotient++;
  268. *a = quotient;
  269. return alpha_pll_calc_rate(prate, *l, *a);
  270. }
  271. static const struct pll_vco *
  272. alpha_pll_find_vco(const struct clk_alpha_pll *pll, unsigned long rate)
  273. {
  274. const struct pll_vco *v = pll->vco_table;
  275. const struct pll_vco *end = v + pll->num_vco;
  276. for (; v < end; v++)
  277. if (rate >= v->min_freq && rate <= v->max_freq)
  278. return v;
  279. return NULL;
  280. }
  281. static unsigned long
  282. clk_alpha_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  283. {
  284. u32 l, low, high, ctl;
  285. u64 a = 0, prate = parent_rate;
  286. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  287. u32 off = pll->offset;
  288. regmap_read(pll->clkr.regmap, off + PLL_L_VAL, &l);
  289. regmap_read(pll->clkr.regmap, off + PLL_USER_CTL, &ctl);
  290. if (ctl & PLL_ALPHA_EN) {
  291. regmap_read(pll->clkr.regmap, off + PLL_ALPHA_VAL, &low);
  292. if (pll->flags & SUPPORTS_16BIT_ALPHA) {
  293. a = low & ALPHA_16BIT_MASK;
  294. } else {
  295. regmap_read(pll->clkr.regmap, off + PLL_ALPHA_VAL_U,
  296. &high);
  297. a = (u64)high << 32 | low;
  298. a >>= ALPHA_REG_BITWIDTH - ALPHA_BITWIDTH;
  299. }
  300. }
  301. return alpha_pll_calc_rate(prate, l, a);
  302. }
  303. static int clk_alpha_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  304. unsigned long prate)
  305. {
  306. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  307. const struct pll_vco *vco;
  308. u32 l, off = pll->offset;
  309. u64 a;
  310. rate = alpha_pll_round_rate(rate, prate, &l, &a);
  311. vco = alpha_pll_find_vco(pll, rate);
  312. if (!vco) {
  313. pr_err("alpha pll not in a valid vco range\n");
  314. return -EINVAL;
  315. }
  316. regmap_write(pll->clkr.regmap, off + PLL_L_VAL, l);
  317. if (pll->flags & SUPPORTS_16BIT_ALPHA) {
  318. regmap_write(pll->clkr.regmap, off + PLL_ALPHA_VAL,
  319. a & ALPHA_16BIT_MASK);
  320. } else {
  321. a <<= (ALPHA_REG_BITWIDTH - ALPHA_BITWIDTH);
  322. regmap_write(pll->clkr.regmap, off + PLL_ALPHA_VAL_U, a >> 32);
  323. }
  324. regmap_update_bits(pll->clkr.regmap, off + PLL_USER_CTL,
  325. PLL_VCO_MASK << PLL_VCO_SHIFT,
  326. vco->val << PLL_VCO_SHIFT);
  327. regmap_update_bits(pll->clkr.regmap, off + PLL_USER_CTL, PLL_ALPHA_EN,
  328. PLL_ALPHA_EN);
  329. return 0;
  330. }
  331. static long clk_alpha_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  332. unsigned long *prate)
  333. {
  334. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  335. u32 l;
  336. u64 a;
  337. unsigned long min_freq, max_freq;
  338. rate = alpha_pll_round_rate(rate, *prate, &l, &a);
  339. if (alpha_pll_find_vco(pll, rate))
  340. return rate;
  341. min_freq = pll->vco_table[0].min_freq;
  342. max_freq = pll->vco_table[pll->num_vco - 1].max_freq;
  343. return clamp(rate, min_freq, max_freq);
  344. }
  345. const struct clk_ops clk_alpha_pll_ops = {
  346. .enable = clk_alpha_pll_enable,
  347. .disable = clk_alpha_pll_disable,
  348. .is_enabled = clk_alpha_pll_is_enabled,
  349. .recalc_rate = clk_alpha_pll_recalc_rate,
  350. .round_rate = clk_alpha_pll_round_rate,
  351. .set_rate = clk_alpha_pll_set_rate,
  352. };
  353. EXPORT_SYMBOL_GPL(clk_alpha_pll_ops);
  354. const struct clk_ops clk_alpha_pll_hwfsm_ops = {
  355. .enable = clk_alpha_pll_hwfsm_enable,
  356. .disable = clk_alpha_pll_hwfsm_disable,
  357. .is_enabled = clk_alpha_pll_hwfsm_is_enabled,
  358. .recalc_rate = clk_alpha_pll_recalc_rate,
  359. .round_rate = clk_alpha_pll_round_rate,
  360. .set_rate = clk_alpha_pll_set_rate,
  361. };
  362. EXPORT_SYMBOL_GPL(clk_alpha_pll_hwfsm_ops);
  363. static unsigned long
  364. clk_alpha_pll_postdiv_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  365. {
  366. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  367. u32 ctl;
  368. regmap_read(pll->clkr.regmap, pll->offset + PLL_USER_CTL, &ctl);
  369. ctl >>= PLL_POST_DIV_SHIFT;
  370. ctl &= PLL_POST_DIV_MASK;
  371. return parent_rate >> fls(ctl);
  372. }
  373. static const struct clk_div_table clk_alpha_div_table[] = {
  374. { 0x0, 1 },
  375. { 0x1, 2 },
  376. { 0x3, 4 },
  377. { 0x7, 8 },
  378. { 0xf, 16 },
  379. { }
  380. };
  381. static long
  382. clk_alpha_pll_postdiv_round_rate(struct clk_hw *hw, unsigned long rate,
  383. unsigned long *prate)
  384. {
  385. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  386. return divider_round_rate(hw, rate, prate, clk_alpha_div_table,
  387. pll->width, CLK_DIVIDER_POWER_OF_TWO);
  388. }
  389. static int clk_alpha_pll_postdiv_set_rate(struct clk_hw *hw, unsigned long rate,
  390. unsigned long parent_rate)
  391. {
  392. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  393. int div;
  394. /* 16 -> 0xf, 8 -> 0x7, 4 -> 0x3, 2 -> 0x1, 1 -> 0x0 */
  395. div = DIV_ROUND_UP_ULL((u64)parent_rate, rate) - 1;
  396. return regmap_update_bits(pll->clkr.regmap, pll->offset + PLL_USER_CTL,
  397. PLL_POST_DIV_MASK << PLL_POST_DIV_SHIFT,
  398. div << PLL_POST_DIV_SHIFT);
  399. }
  400. const struct clk_ops clk_alpha_pll_postdiv_ops = {
  401. .recalc_rate = clk_alpha_pll_postdiv_recalc_rate,
  402. .round_rate = clk_alpha_pll_postdiv_round_rate,
  403. .set_rate = clk_alpha_pll_postdiv_set_rate,
  404. };
  405. EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_ops);