clk-alpha-pll.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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(p) ((p)->offset + 0x0)
  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_UPDATE BIT(22)
  33. # define PLL_UPDATE_BYPASS BIT(23)
  34. # define PLL_OFFLINE_ACK BIT(28)
  35. # define ALPHA_PLL_ACK_LATCH BIT(29)
  36. # define PLL_ACTIVE_FLAG BIT(30)
  37. # define PLL_LOCK_DET BIT(31)
  38. #define PLL_L_VAL(p) ((p)->offset + (p)->regs[PLL_OFF_L_VAL])
  39. #define PLL_ALPHA_VAL(p) ((p)->offset + (p)->regs[PLL_OFF_ALPHA_VAL])
  40. #define PLL_ALPHA_VAL_U(p) ((p)->offset + (p)->regs[PLL_OFF_ALPHA_VAL_U])
  41. #define PLL_USER_CTL(p) ((p)->offset + (p)->regs[PLL_OFF_USER_CTL])
  42. # define PLL_POST_DIV_SHIFT 8
  43. # define PLL_POST_DIV_MASK(p) GENMASK((p)->width, 0)
  44. # define PLL_ALPHA_EN BIT(24)
  45. # define PLL_ALPHA_MODE BIT(25)
  46. # define PLL_VCO_SHIFT 20
  47. # define PLL_VCO_MASK 0x3
  48. #define PLL_USER_CTL_U(p) ((p)->offset + (p)->regs[PLL_OFF_USER_CTL_U])
  49. #define PLL_CONFIG_CTL(p) ((p)->offset + (p)->regs[PLL_OFF_CONFIG_CTL])
  50. #define PLL_CONFIG_CTL_U(p) ((p)->offset + (p)->regs[PLL_OFF_CONFIG_CTL_U])
  51. #define PLL_TEST_CTL(p) ((p)->offset + (p)->regs[PLL_OFF_TEST_CTL])
  52. #define PLL_TEST_CTL_U(p) ((p)->offset + (p)->regs[PLL_OFF_TEST_CTL_U])
  53. #define PLL_STATUS(p) ((p)->offset + (p)->regs[PLL_OFF_STATUS])
  54. const u8 clk_alpha_pll_regs[][PLL_OFF_MAX_REGS] = {
  55. [CLK_ALPHA_PLL_TYPE_DEFAULT] = {
  56. [PLL_OFF_L_VAL] = 0x04,
  57. [PLL_OFF_ALPHA_VAL] = 0x08,
  58. [PLL_OFF_ALPHA_VAL_U] = 0x0c,
  59. [PLL_OFF_USER_CTL] = 0x10,
  60. [PLL_OFF_USER_CTL_U] = 0x14,
  61. [PLL_OFF_CONFIG_CTL] = 0x18,
  62. [PLL_OFF_TEST_CTL] = 0x1c,
  63. [PLL_OFF_TEST_CTL_U] = 0x20,
  64. [PLL_OFF_STATUS] = 0x24,
  65. },
  66. [CLK_ALPHA_PLL_TYPE_HUAYRA] = {
  67. [PLL_OFF_L_VAL] = 0x04,
  68. [PLL_OFF_ALPHA_VAL] = 0x08,
  69. [PLL_OFF_USER_CTL] = 0x10,
  70. [PLL_OFF_CONFIG_CTL] = 0x14,
  71. [PLL_OFF_CONFIG_CTL_U] = 0x18,
  72. [PLL_OFF_TEST_CTL] = 0x1c,
  73. [PLL_OFF_TEST_CTL_U] = 0x20,
  74. [PLL_OFF_STATUS] = 0x24,
  75. },
  76. [CLK_ALPHA_PLL_TYPE_BRAMMO] = {
  77. [PLL_OFF_L_VAL] = 0x04,
  78. [PLL_OFF_ALPHA_VAL] = 0x08,
  79. [PLL_OFF_ALPHA_VAL_U] = 0x0c,
  80. [PLL_OFF_USER_CTL] = 0x10,
  81. [PLL_OFF_CONFIG_CTL] = 0x18,
  82. [PLL_OFF_TEST_CTL] = 0x1c,
  83. [PLL_OFF_STATUS] = 0x24,
  84. },
  85. };
  86. EXPORT_SYMBOL_GPL(clk_alpha_pll_regs);
  87. /*
  88. * Even though 40 bits are present, use only 32 for ease of calculation.
  89. */
  90. #define ALPHA_REG_BITWIDTH 40
  91. #define ALPHA_REG_16BIT_WIDTH 16
  92. #define ALPHA_BITWIDTH 32U
  93. #define ALPHA_SHIFT(w) min(w, ALPHA_BITWIDTH)
  94. #define PLL_HUAYRA_M_WIDTH 8
  95. #define PLL_HUAYRA_M_SHIFT 8
  96. #define PLL_HUAYRA_M_MASK 0xff
  97. #define PLL_HUAYRA_N_SHIFT 0
  98. #define PLL_HUAYRA_N_MASK 0xff
  99. #define PLL_HUAYRA_ALPHA_WIDTH 16
  100. #define pll_alpha_width(p) \
  101. ((PLL_ALPHA_VAL_U(p) - PLL_ALPHA_VAL(p) == 4) ? \
  102. ALPHA_REG_BITWIDTH : ALPHA_REG_16BIT_WIDTH)
  103. #define pll_has_64bit_config(p) ((PLL_CONFIG_CTL_U(p) - PLL_CONFIG_CTL(p)) == 4)
  104. #define to_clk_alpha_pll(_hw) container_of(to_clk_regmap(_hw), \
  105. struct clk_alpha_pll, clkr)
  106. #define to_clk_alpha_pll_postdiv(_hw) container_of(to_clk_regmap(_hw), \
  107. struct clk_alpha_pll_postdiv, clkr)
  108. static int wait_for_pll(struct clk_alpha_pll *pll, u32 mask, bool inverse,
  109. const char *action)
  110. {
  111. u32 val;
  112. int count;
  113. int ret;
  114. const char *name = clk_hw_get_name(&pll->clkr.hw);
  115. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  116. if (ret)
  117. return ret;
  118. for (count = 100; count > 0; count--) {
  119. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  120. if (ret)
  121. return ret;
  122. if (inverse && !(val & mask))
  123. return 0;
  124. else if ((val & mask) == mask)
  125. return 0;
  126. udelay(1);
  127. }
  128. WARN(1, "%s failed to %s!\n", name, action);
  129. return -ETIMEDOUT;
  130. }
  131. #define wait_for_pll_enable_active(pll) \
  132. wait_for_pll(pll, PLL_ACTIVE_FLAG, 0, "enable")
  133. #define wait_for_pll_enable_lock(pll) \
  134. wait_for_pll(pll, PLL_LOCK_DET, 0, "enable")
  135. #define wait_for_pll_disable(pll) \
  136. wait_for_pll(pll, PLL_ACTIVE_FLAG, 1, "disable")
  137. #define wait_for_pll_offline(pll) \
  138. wait_for_pll(pll, PLL_OFFLINE_ACK, 0, "offline")
  139. #define wait_for_pll_update(pll) \
  140. wait_for_pll(pll, PLL_UPDATE, 1, "update")
  141. #define wait_for_pll_update_ack_set(pll) \
  142. wait_for_pll(pll, ALPHA_PLL_ACK_LATCH, 0, "update_ack_set")
  143. #define wait_for_pll_update_ack_clear(pll) \
  144. wait_for_pll(pll, ALPHA_PLL_ACK_LATCH, 1, "update_ack_clear")
  145. void clk_alpha_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
  146. const struct alpha_pll_config *config)
  147. {
  148. u32 val, mask;
  149. regmap_write(regmap, PLL_L_VAL(pll), config->l);
  150. regmap_write(regmap, PLL_ALPHA_VAL(pll), config->alpha);
  151. regmap_write(regmap, PLL_CONFIG_CTL(pll), config->config_ctl_val);
  152. if (pll_has_64bit_config(pll))
  153. regmap_write(regmap, PLL_CONFIG_CTL_U(pll),
  154. config->config_ctl_hi_val);
  155. if (pll_alpha_width(pll) > 32)
  156. regmap_write(regmap, PLL_ALPHA_VAL_U(pll), config->alpha_hi);
  157. val = config->main_output_mask;
  158. val |= config->aux_output_mask;
  159. val |= config->aux2_output_mask;
  160. val |= config->early_output_mask;
  161. val |= config->pre_div_val;
  162. val |= config->post_div_val;
  163. val |= config->vco_val;
  164. val |= config->alpha_en_mask;
  165. val |= config->alpha_mode_mask;
  166. mask = config->main_output_mask;
  167. mask |= config->aux_output_mask;
  168. mask |= config->aux2_output_mask;
  169. mask |= config->early_output_mask;
  170. mask |= config->pre_div_mask;
  171. mask |= config->post_div_mask;
  172. mask |= config->vco_mask;
  173. regmap_update_bits(regmap, PLL_USER_CTL(pll), mask, val);
  174. if (pll->flags & SUPPORTS_FSM_MODE)
  175. qcom_pll_set_fsm_mode(regmap, PLL_MODE(pll), 6, 0);
  176. }
  177. static int clk_alpha_pll_hwfsm_enable(struct clk_hw *hw)
  178. {
  179. int ret;
  180. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  181. u32 val;
  182. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  183. if (ret)
  184. return ret;
  185. val |= PLL_FSM_ENA;
  186. if (pll->flags & SUPPORTS_OFFLINE_REQ)
  187. val &= ~PLL_OFFLINE_REQ;
  188. ret = regmap_write(pll->clkr.regmap, PLL_MODE(pll), val);
  189. if (ret)
  190. return ret;
  191. /* Make sure enable request goes through before waiting for update */
  192. mb();
  193. return wait_for_pll_enable_active(pll);
  194. }
  195. static void clk_alpha_pll_hwfsm_disable(struct clk_hw *hw)
  196. {
  197. int ret;
  198. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  199. u32 val;
  200. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  201. if (ret)
  202. return;
  203. if (pll->flags & SUPPORTS_OFFLINE_REQ) {
  204. ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
  205. PLL_OFFLINE_REQ, PLL_OFFLINE_REQ);
  206. if (ret)
  207. return;
  208. ret = wait_for_pll_offline(pll);
  209. if (ret)
  210. return;
  211. }
  212. /* Disable hwfsm */
  213. ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
  214. PLL_FSM_ENA, 0);
  215. if (ret)
  216. return;
  217. wait_for_pll_disable(pll);
  218. }
  219. static int pll_is_enabled(struct clk_hw *hw, u32 mask)
  220. {
  221. int ret;
  222. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  223. u32 val;
  224. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  225. if (ret)
  226. return ret;
  227. return !!(val & mask);
  228. }
  229. static int clk_alpha_pll_hwfsm_is_enabled(struct clk_hw *hw)
  230. {
  231. return pll_is_enabled(hw, PLL_ACTIVE_FLAG);
  232. }
  233. static int clk_alpha_pll_is_enabled(struct clk_hw *hw)
  234. {
  235. return pll_is_enabled(hw, PLL_LOCK_DET);
  236. }
  237. static int clk_alpha_pll_enable(struct clk_hw *hw)
  238. {
  239. int ret;
  240. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  241. u32 val, mask;
  242. mask = PLL_OUTCTRL | PLL_RESET_N | PLL_BYPASSNL;
  243. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  244. if (ret)
  245. return ret;
  246. /* If in FSM mode, just vote for it */
  247. if (val & PLL_VOTE_FSM_ENA) {
  248. ret = clk_enable_regmap(hw);
  249. if (ret)
  250. return ret;
  251. return wait_for_pll_enable_active(pll);
  252. }
  253. /* Skip if already enabled */
  254. if ((val & mask) == mask)
  255. return 0;
  256. ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
  257. PLL_BYPASSNL, PLL_BYPASSNL);
  258. if (ret)
  259. return ret;
  260. /*
  261. * H/W requires a 5us delay between disabling the bypass and
  262. * de-asserting the reset.
  263. */
  264. mb();
  265. udelay(5);
  266. ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
  267. PLL_RESET_N, PLL_RESET_N);
  268. if (ret)
  269. return ret;
  270. ret = wait_for_pll_enable_lock(pll);
  271. if (ret)
  272. return ret;
  273. ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
  274. PLL_OUTCTRL, PLL_OUTCTRL);
  275. /* Ensure that the write above goes through before returning. */
  276. mb();
  277. return ret;
  278. }
  279. static void clk_alpha_pll_disable(struct clk_hw *hw)
  280. {
  281. int ret;
  282. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  283. u32 val, mask;
  284. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  285. if (ret)
  286. return;
  287. /* If in FSM mode, just unvote it */
  288. if (val & PLL_VOTE_FSM_ENA) {
  289. clk_disable_regmap(hw);
  290. return;
  291. }
  292. mask = PLL_OUTCTRL;
  293. regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), mask, 0);
  294. /* Delay of 2 output clock ticks required until output is disabled */
  295. mb();
  296. udelay(1);
  297. mask = PLL_RESET_N | PLL_BYPASSNL;
  298. regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), mask, 0);
  299. }
  300. static unsigned long
  301. alpha_pll_calc_rate(u64 prate, u32 l, u32 a, u32 alpha_width)
  302. {
  303. return (prate * l) + ((prate * a) >> ALPHA_SHIFT(alpha_width));
  304. }
  305. static unsigned long
  306. alpha_pll_round_rate(unsigned long rate, unsigned long prate, u32 *l, u64 *a,
  307. u32 alpha_width)
  308. {
  309. u64 remainder;
  310. u64 quotient;
  311. quotient = rate;
  312. remainder = do_div(quotient, prate);
  313. *l = quotient;
  314. if (!remainder) {
  315. *a = 0;
  316. return rate;
  317. }
  318. /* Upper ALPHA_BITWIDTH bits of Alpha */
  319. quotient = remainder << ALPHA_SHIFT(alpha_width);
  320. remainder = do_div(quotient, prate);
  321. if (remainder)
  322. quotient++;
  323. *a = quotient;
  324. return alpha_pll_calc_rate(prate, *l, *a, alpha_width);
  325. }
  326. static const struct pll_vco *
  327. alpha_pll_find_vco(const struct clk_alpha_pll *pll, unsigned long rate)
  328. {
  329. const struct pll_vco *v = pll->vco_table;
  330. const struct pll_vco *end = v + pll->num_vco;
  331. for (; v < end; v++)
  332. if (rate >= v->min_freq && rate <= v->max_freq)
  333. return v;
  334. return NULL;
  335. }
  336. static unsigned long
  337. clk_alpha_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  338. {
  339. u32 l, low, high, ctl;
  340. u64 a = 0, prate = parent_rate;
  341. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  342. u32 alpha_width = pll_alpha_width(pll);
  343. regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
  344. regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
  345. if (ctl & PLL_ALPHA_EN) {
  346. regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &low);
  347. if (alpha_width > 32) {
  348. regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL_U(pll),
  349. &high);
  350. a = (u64)high << 32 | low;
  351. } else {
  352. a = low & GENMASK(alpha_width - 1, 0);
  353. }
  354. if (alpha_width > ALPHA_BITWIDTH)
  355. a >>= alpha_width - ALPHA_BITWIDTH;
  356. }
  357. return alpha_pll_calc_rate(prate, l, a, alpha_width);
  358. }
  359. static int clk_alpha_pll_update_latch(struct clk_alpha_pll *pll,
  360. int (*is_enabled)(struct clk_hw *))
  361. {
  362. int ret;
  363. u32 mode;
  364. if (!is_enabled(&pll->clkr.hw) ||
  365. !(pll->flags & SUPPORTS_DYNAMIC_UPDATE))
  366. return 0;
  367. regmap_read(pll->clkr.regmap, PLL_MODE(pll), &mode);
  368. /* Latch the input to the PLL */
  369. regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), PLL_UPDATE,
  370. PLL_UPDATE);
  371. /* Wait for 2 reference cycle before checking ACK bit */
  372. udelay(1);
  373. /*
  374. * PLL will latch the new L, Alpha and freq control word.
  375. * PLL will respond by raising PLL_ACK_LATCH output when new programming
  376. * has been latched in and PLL is being updated. When
  377. * UPDATE_LOGIC_BYPASS bit is not set, PLL_UPDATE will be cleared
  378. * automatically by hardware when PLL_ACK_LATCH is asserted by PLL.
  379. */
  380. if (mode & PLL_UPDATE_BYPASS) {
  381. ret = wait_for_pll_update_ack_set(pll);
  382. if (ret)
  383. return ret;
  384. regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), PLL_UPDATE, 0);
  385. } else {
  386. ret = wait_for_pll_update(pll);
  387. if (ret)
  388. return ret;
  389. }
  390. ret = wait_for_pll_update_ack_clear(pll);
  391. if (ret)
  392. return ret;
  393. /* Wait for PLL output to stabilize */
  394. udelay(10);
  395. return 0;
  396. }
  397. static int __clk_alpha_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  398. unsigned long prate,
  399. int (*is_enabled)(struct clk_hw *))
  400. {
  401. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  402. const struct pll_vco *vco;
  403. u32 l, alpha_width = pll_alpha_width(pll);
  404. u64 a;
  405. rate = alpha_pll_round_rate(rate, prate, &l, &a, alpha_width);
  406. vco = alpha_pll_find_vco(pll, rate);
  407. if (pll->vco_table && !vco) {
  408. pr_err("alpha pll not in a valid vco range\n");
  409. return -EINVAL;
  410. }
  411. regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
  412. if (alpha_width > ALPHA_BITWIDTH)
  413. a <<= alpha_width - ALPHA_BITWIDTH;
  414. if (alpha_width > 32)
  415. regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL_U(pll), a >> 32);
  416. regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
  417. if (vco) {
  418. regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
  419. PLL_VCO_MASK << PLL_VCO_SHIFT,
  420. vco->val << PLL_VCO_SHIFT);
  421. }
  422. regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
  423. PLL_ALPHA_EN, PLL_ALPHA_EN);
  424. return clk_alpha_pll_update_latch(pll, is_enabled);
  425. }
  426. static int clk_alpha_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  427. unsigned long prate)
  428. {
  429. return __clk_alpha_pll_set_rate(hw, rate, prate,
  430. clk_alpha_pll_is_enabled);
  431. }
  432. static int clk_alpha_pll_hwfsm_set_rate(struct clk_hw *hw, unsigned long rate,
  433. unsigned long prate)
  434. {
  435. return __clk_alpha_pll_set_rate(hw, rate, prate,
  436. clk_alpha_pll_hwfsm_is_enabled);
  437. }
  438. static long clk_alpha_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  439. unsigned long *prate)
  440. {
  441. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  442. u32 l, alpha_width = pll_alpha_width(pll);
  443. u64 a;
  444. unsigned long min_freq, max_freq;
  445. rate = alpha_pll_round_rate(rate, *prate, &l, &a, alpha_width);
  446. if (!pll->vco_table || alpha_pll_find_vco(pll, rate))
  447. return rate;
  448. min_freq = pll->vco_table[0].min_freq;
  449. max_freq = pll->vco_table[pll->num_vco - 1].max_freq;
  450. return clamp(rate, min_freq, max_freq);
  451. }
  452. static unsigned long
  453. alpha_huayra_pll_calc_rate(u64 prate, u32 l, u32 a)
  454. {
  455. /*
  456. * a contains 16 bit alpha_val in two’s compliment number in the range
  457. * of [-0.5, 0.5).
  458. */
  459. if (a >= BIT(PLL_HUAYRA_ALPHA_WIDTH - 1))
  460. l -= 1;
  461. return (prate * l) + (prate * a >> PLL_HUAYRA_ALPHA_WIDTH);
  462. }
  463. static unsigned long
  464. alpha_huayra_pll_round_rate(unsigned long rate, unsigned long prate,
  465. u32 *l, u32 *a)
  466. {
  467. u64 remainder;
  468. u64 quotient;
  469. quotient = rate;
  470. remainder = do_div(quotient, prate);
  471. *l = quotient;
  472. if (!remainder) {
  473. *a = 0;
  474. return rate;
  475. }
  476. quotient = remainder << PLL_HUAYRA_ALPHA_WIDTH;
  477. remainder = do_div(quotient, prate);
  478. if (remainder)
  479. quotient++;
  480. /*
  481. * alpha_val should be in two’s compliment number in the range
  482. * of [-0.5, 0.5) so if quotient >= 0.5 then increment the l value
  483. * since alpha value will be subtracted in this case.
  484. */
  485. if (quotient >= BIT(PLL_HUAYRA_ALPHA_WIDTH - 1))
  486. *l += 1;
  487. *a = quotient;
  488. return alpha_huayra_pll_calc_rate(prate, *l, *a);
  489. }
  490. static unsigned long
  491. alpha_pll_huayra_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  492. {
  493. u64 rate = parent_rate, tmp;
  494. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  495. u32 l, alpha = 0, ctl, alpha_m, alpha_n;
  496. regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
  497. regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
  498. if (ctl & PLL_ALPHA_EN) {
  499. regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &alpha);
  500. /*
  501. * Depending upon alpha_mode, it can be treated as M/N value or
  502. * as a two’s compliment number. When alpha_mode=1,
  503. * pll_alpha_val<15:8>=M and pll_apla_val<7:0>=N
  504. *
  505. * Fout=FIN*(L+(M/N))
  506. *
  507. * M is a signed number (-128 to 127) and N is unsigned
  508. * (0 to 255). M/N has to be within +/-0.5.
  509. *
  510. * When alpha_mode=0, it is a two’s compliment number in the
  511. * range [-0.5, 0.5).
  512. *
  513. * Fout=FIN*(L+(alpha_val)/2^16)
  514. *
  515. * where alpha_val is two’s compliment number.
  516. */
  517. if (!(ctl & PLL_ALPHA_MODE))
  518. return alpha_huayra_pll_calc_rate(rate, l, alpha);
  519. alpha_m = alpha >> PLL_HUAYRA_M_SHIFT & PLL_HUAYRA_M_MASK;
  520. alpha_n = alpha >> PLL_HUAYRA_N_SHIFT & PLL_HUAYRA_N_MASK;
  521. rate *= l;
  522. tmp = parent_rate;
  523. if (alpha_m >= BIT(PLL_HUAYRA_M_WIDTH - 1)) {
  524. alpha_m = BIT(PLL_HUAYRA_M_WIDTH) - alpha_m;
  525. tmp *= alpha_m;
  526. do_div(tmp, alpha_n);
  527. rate -= tmp;
  528. } else {
  529. tmp *= alpha_m;
  530. do_div(tmp, alpha_n);
  531. rate += tmp;
  532. }
  533. return rate;
  534. }
  535. return alpha_huayra_pll_calc_rate(rate, l, alpha);
  536. }
  537. static int alpha_pll_huayra_set_rate(struct clk_hw *hw, unsigned long rate,
  538. unsigned long prate)
  539. {
  540. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  541. u32 l, a, ctl, cur_alpha = 0;
  542. rate = alpha_huayra_pll_round_rate(rate, prate, &l, &a);
  543. regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
  544. if (ctl & PLL_ALPHA_EN)
  545. regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &cur_alpha);
  546. /*
  547. * Huayra PLL supports PLL dynamic programming. User can change L_VAL,
  548. * without having to go through the power on sequence.
  549. */
  550. if (clk_alpha_pll_is_enabled(hw)) {
  551. if (cur_alpha != a) {
  552. pr_err("clock needs to be gated %s\n",
  553. clk_hw_get_name(hw));
  554. return -EBUSY;
  555. }
  556. regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
  557. /* Ensure that the write above goes to detect L val change. */
  558. mb();
  559. return wait_for_pll_enable_lock(pll);
  560. }
  561. regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
  562. regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
  563. if (a == 0)
  564. regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
  565. PLL_ALPHA_EN, 0x0);
  566. else
  567. regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
  568. PLL_ALPHA_EN | PLL_ALPHA_MODE, PLL_ALPHA_EN);
  569. return 0;
  570. }
  571. static long alpha_pll_huayra_round_rate(struct clk_hw *hw, unsigned long rate,
  572. unsigned long *prate)
  573. {
  574. u32 l, a;
  575. return alpha_huayra_pll_round_rate(rate, *prate, &l, &a);
  576. }
  577. const struct clk_ops clk_alpha_pll_ops = {
  578. .enable = clk_alpha_pll_enable,
  579. .disable = clk_alpha_pll_disable,
  580. .is_enabled = clk_alpha_pll_is_enabled,
  581. .recalc_rate = clk_alpha_pll_recalc_rate,
  582. .round_rate = clk_alpha_pll_round_rate,
  583. .set_rate = clk_alpha_pll_set_rate,
  584. };
  585. EXPORT_SYMBOL_GPL(clk_alpha_pll_ops);
  586. const struct clk_ops clk_alpha_pll_huayra_ops = {
  587. .enable = clk_alpha_pll_enable,
  588. .disable = clk_alpha_pll_disable,
  589. .is_enabled = clk_alpha_pll_is_enabled,
  590. .recalc_rate = alpha_pll_huayra_recalc_rate,
  591. .round_rate = alpha_pll_huayra_round_rate,
  592. .set_rate = alpha_pll_huayra_set_rate,
  593. };
  594. EXPORT_SYMBOL_GPL(clk_alpha_pll_huayra_ops);
  595. const struct clk_ops clk_alpha_pll_hwfsm_ops = {
  596. .enable = clk_alpha_pll_hwfsm_enable,
  597. .disable = clk_alpha_pll_hwfsm_disable,
  598. .is_enabled = clk_alpha_pll_hwfsm_is_enabled,
  599. .recalc_rate = clk_alpha_pll_recalc_rate,
  600. .round_rate = clk_alpha_pll_round_rate,
  601. .set_rate = clk_alpha_pll_hwfsm_set_rate,
  602. };
  603. EXPORT_SYMBOL_GPL(clk_alpha_pll_hwfsm_ops);
  604. static unsigned long
  605. clk_alpha_pll_postdiv_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  606. {
  607. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  608. u32 ctl;
  609. regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
  610. ctl >>= PLL_POST_DIV_SHIFT;
  611. ctl &= PLL_POST_DIV_MASK(pll);
  612. return parent_rate >> fls(ctl);
  613. }
  614. static const struct clk_div_table clk_alpha_div_table[] = {
  615. { 0x0, 1 },
  616. { 0x1, 2 },
  617. { 0x3, 4 },
  618. { 0x7, 8 },
  619. { 0xf, 16 },
  620. { }
  621. };
  622. static const struct clk_div_table clk_alpha_2bit_div_table[] = {
  623. { 0x0, 1 },
  624. { 0x1, 2 },
  625. { 0x3, 4 },
  626. { }
  627. };
  628. static long
  629. clk_alpha_pll_postdiv_round_rate(struct clk_hw *hw, unsigned long rate,
  630. unsigned long *prate)
  631. {
  632. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  633. const struct clk_div_table *table;
  634. if (pll->width == 2)
  635. table = clk_alpha_2bit_div_table;
  636. else
  637. table = clk_alpha_div_table;
  638. return divider_round_rate(hw, rate, prate, table,
  639. pll->width, CLK_DIVIDER_POWER_OF_TWO);
  640. }
  641. static long
  642. clk_alpha_pll_postdiv_round_ro_rate(struct clk_hw *hw, unsigned long rate,
  643. unsigned long *prate)
  644. {
  645. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  646. u32 ctl, div;
  647. regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
  648. ctl >>= PLL_POST_DIV_SHIFT;
  649. ctl &= BIT(pll->width) - 1;
  650. div = 1 << fls(ctl);
  651. if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)
  652. *prate = clk_hw_round_rate(clk_hw_get_parent(hw), div * rate);
  653. return DIV_ROUND_UP_ULL((u64)*prate, div);
  654. }
  655. static int clk_alpha_pll_postdiv_set_rate(struct clk_hw *hw, unsigned long rate,
  656. unsigned long parent_rate)
  657. {
  658. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  659. int div;
  660. /* 16 -> 0xf, 8 -> 0x7, 4 -> 0x3, 2 -> 0x1, 1 -> 0x0 */
  661. div = DIV_ROUND_UP_ULL((u64)parent_rate, rate) - 1;
  662. return regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
  663. PLL_POST_DIV_MASK(pll) << PLL_POST_DIV_SHIFT,
  664. div << PLL_POST_DIV_SHIFT);
  665. }
  666. const struct clk_ops clk_alpha_pll_postdiv_ops = {
  667. .recalc_rate = clk_alpha_pll_postdiv_recalc_rate,
  668. .round_rate = clk_alpha_pll_postdiv_round_rate,
  669. .set_rate = clk_alpha_pll_postdiv_set_rate,
  670. };
  671. EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_ops);
  672. const struct clk_ops clk_alpha_pll_postdiv_ro_ops = {
  673. .round_rate = clk_alpha_pll_postdiv_round_ro_rate,
  674. .recalc_rate = clk_alpha_pll_postdiv_recalc_rate,
  675. };
  676. EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_ro_ops);