clk-pll.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2014 MundoReader S.L.
  4. * Author: Heiko Stuebner <[email protected]>
  5. *
  6. * Copyright (c) 2015 Rockchip Electronics Co. Ltd.
  7. * Author: Xing Zheng <[email protected]>
  8. */
  9. #include <asm/div64.h>
  10. #include <linux/slab.h>
  11. #include <linux/io.h>
  12. #include <linux/delay.h>
  13. #include <linux/clk-provider.h>
  14. #include <linux/iopoll.h>
  15. #include <linux/regmap.h>
  16. #include <linux/clk.h>
  17. #include "clk.h"
  18. #define PLL_MODE_MASK 0x3
  19. #define PLL_MODE_SLOW 0x0
  20. #define PLL_MODE_NORM 0x1
  21. #define PLL_MODE_DEEP 0x2
  22. #define PLL_RK3328_MODE_MASK 0x1
  23. struct rockchip_clk_pll {
  24. struct clk_hw hw;
  25. struct clk_mux pll_mux;
  26. const struct clk_ops *pll_mux_ops;
  27. struct notifier_block clk_nb;
  28. void __iomem *reg_base;
  29. int lock_offset;
  30. unsigned int lock_shift;
  31. enum rockchip_pll_type type;
  32. u8 flags;
  33. const struct rockchip_pll_rate_table *rate_table;
  34. unsigned int rate_count;
  35. spinlock_t *lock;
  36. struct rockchip_clk_provider *ctx;
  37. };
  38. #define to_rockchip_clk_pll(_hw) container_of(_hw, struct rockchip_clk_pll, hw)
  39. #define to_rockchip_clk_pll_nb(nb) \
  40. container_of(nb, struct rockchip_clk_pll, clk_nb)
  41. static const struct rockchip_pll_rate_table *rockchip_get_pll_settings(
  42. struct rockchip_clk_pll *pll, unsigned long rate)
  43. {
  44. const struct rockchip_pll_rate_table *rate_table = pll->rate_table;
  45. int i;
  46. for (i = 0; i < pll->rate_count; i++) {
  47. if (rate == rate_table[i].rate)
  48. return &rate_table[i];
  49. }
  50. return NULL;
  51. }
  52. static long rockchip_pll_round_rate(struct clk_hw *hw,
  53. unsigned long drate, unsigned long *prate)
  54. {
  55. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  56. const struct rockchip_pll_rate_table *rate_table = pll->rate_table;
  57. int i;
  58. /* Assumming rate_table is in descending order */
  59. for (i = 0; i < pll->rate_count; i++) {
  60. if (drate >= rate_table[i].rate)
  61. return rate_table[i].rate;
  62. }
  63. /* return minimum supported value */
  64. return rate_table[i - 1].rate;
  65. }
  66. /*
  67. * Wait for the pll to reach the locked state.
  68. * The calling set_rate function is responsible for making sure the
  69. * grf regmap is available.
  70. */
  71. static int rockchip_pll_wait_lock(struct rockchip_clk_pll *pll)
  72. {
  73. struct regmap *grf = pll->ctx->grf;
  74. unsigned int val;
  75. int ret;
  76. ret = regmap_read_poll_timeout(grf, pll->lock_offset, val,
  77. val & BIT(pll->lock_shift), 0, 1000);
  78. if (ret)
  79. pr_err("%s: timeout waiting for pll to lock\n", __func__);
  80. return ret;
  81. }
  82. /*
  83. * PLL used in RK3036
  84. */
  85. #define RK3036_PLLCON(i) (i * 0x4)
  86. #define RK3036_PLLCON0_FBDIV_MASK 0xfff
  87. #define RK3036_PLLCON0_FBDIV_SHIFT 0
  88. #define RK3036_PLLCON0_POSTDIV1_MASK 0x7
  89. #define RK3036_PLLCON0_POSTDIV1_SHIFT 12
  90. #define RK3036_PLLCON1_REFDIV_MASK 0x3f
  91. #define RK3036_PLLCON1_REFDIV_SHIFT 0
  92. #define RK3036_PLLCON1_POSTDIV2_MASK 0x7
  93. #define RK3036_PLLCON1_POSTDIV2_SHIFT 6
  94. #define RK3036_PLLCON1_LOCK_STATUS BIT(10)
  95. #define RK3036_PLLCON1_DSMPD_MASK 0x1
  96. #define RK3036_PLLCON1_DSMPD_SHIFT 12
  97. #define RK3036_PLLCON1_PWRDOWN BIT(13)
  98. #define RK3036_PLLCON2_FRAC_MASK 0xffffff
  99. #define RK3036_PLLCON2_FRAC_SHIFT 0
  100. static int rockchip_rk3036_pll_wait_lock(struct rockchip_clk_pll *pll)
  101. {
  102. u32 pllcon;
  103. int ret;
  104. /*
  105. * Lock time typical 250, max 500 input clock cycles @24MHz
  106. * So define a very safe maximum of 1000us, meaning 24000 cycles.
  107. */
  108. ret = readl_relaxed_poll_timeout(pll->reg_base + RK3036_PLLCON(1),
  109. pllcon,
  110. pllcon & RK3036_PLLCON1_LOCK_STATUS,
  111. 0, 1000);
  112. if (ret)
  113. pr_err("%s: timeout waiting for pll to lock\n", __func__);
  114. return ret;
  115. }
  116. static void rockchip_rk3036_pll_get_params(struct rockchip_clk_pll *pll,
  117. struct rockchip_pll_rate_table *rate)
  118. {
  119. u32 pllcon;
  120. pllcon = readl_relaxed(pll->reg_base + RK3036_PLLCON(0));
  121. rate->fbdiv = ((pllcon >> RK3036_PLLCON0_FBDIV_SHIFT)
  122. & RK3036_PLLCON0_FBDIV_MASK);
  123. rate->postdiv1 = ((pllcon >> RK3036_PLLCON0_POSTDIV1_SHIFT)
  124. & RK3036_PLLCON0_POSTDIV1_MASK);
  125. pllcon = readl_relaxed(pll->reg_base + RK3036_PLLCON(1));
  126. rate->refdiv = ((pllcon >> RK3036_PLLCON1_REFDIV_SHIFT)
  127. & RK3036_PLLCON1_REFDIV_MASK);
  128. rate->postdiv2 = ((pllcon >> RK3036_PLLCON1_POSTDIV2_SHIFT)
  129. & RK3036_PLLCON1_POSTDIV2_MASK);
  130. rate->dsmpd = ((pllcon >> RK3036_PLLCON1_DSMPD_SHIFT)
  131. & RK3036_PLLCON1_DSMPD_MASK);
  132. pllcon = readl_relaxed(pll->reg_base + RK3036_PLLCON(2));
  133. rate->frac = ((pllcon >> RK3036_PLLCON2_FRAC_SHIFT)
  134. & RK3036_PLLCON2_FRAC_MASK);
  135. }
  136. static unsigned long rockchip_rk3036_pll_recalc_rate(struct clk_hw *hw,
  137. unsigned long prate)
  138. {
  139. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  140. struct rockchip_pll_rate_table cur;
  141. u64 rate64 = prate;
  142. rockchip_rk3036_pll_get_params(pll, &cur);
  143. rate64 *= cur.fbdiv;
  144. do_div(rate64, cur.refdiv);
  145. if (cur.dsmpd == 0) {
  146. /* fractional mode */
  147. u64 frac_rate64 = prate * cur.frac;
  148. do_div(frac_rate64, cur.refdiv);
  149. rate64 += frac_rate64 >> 24;
  150. }
  151. do_div(rate64, cur.postdiv1);
  152. do_div(rate64, cur.postdiv2);
  153. return (unsigned long)rate64;
  154. }
  155. static int rockchip_rk3036_pll_set_params(struct rockchip_clk_pll *pll,
  156. const struct rockchip_pll_rate_table *rate)
  157. {
  158. const struct clk_ops *pll_mux_ops = pll->pll_mux_ops;
  159. struct clk_mux *pll_mux = &pll->pll_mux;
  160. struct rockchip_pll_rate_table cur;
  161. u32 pllcon;
  162. int rate_change_remuxed = 0;
  163. int cur_parent;
  164. int ret;
  165. pr_debug("%s: rate settings for %lu fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
  166. __func__, rate->rate, rate->fbdiv, rate->postdiv1, rate->refdiv,
  167. rate->postdiv2, rate->dsmpd, rate->frac);
  168. rockchip_rk3036_pll_get_params(pll, &cur);
  169. cur.rate = 0;
  170. cur_parent = pll_mux_ops->get_parent(&pll_mux->hw);
  171. if (cur_parent == PLL_MODE_NORM) {
  172. pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_SLOW);
  173. rate_change_remuxed = 1;
  174. }
  175. /* update pll values */
  176. writel_relaxed(HIWORD_UPDATE(rate->fbdiv, RK3036_PLLCON0_FBDIV_MASK,
  177. RK3036_PLLCON0_FBDIV_SHIFT) |
  178. HIWORD_UPDATE(rate->postdiv1, RK3036_PLLCON0_POSTDIV1_MASK,
  179. RK3036_PLLCON0_POSTDIV1_SHIFT),
  180. pll->reg_base + RK3036_PLLCON(0));
  181. writel_relaxed(HIWORD_UPDATE(rate->refdiv, RK3036_PLLCON1_REFDIV_MASK,
  182. RK3036_PLLCON1_REFDIV_SHIFT) |
  183. HIWORD_UPDATE(rate->postdiv2, RK3036_PLLCON1_POSTDIV2_MASK,
  184. RK3036_PLLCON1_POSTDIV2_SHIFT) |
  185. HIWORD_UPDATE(rate->dsmpd, RK3036_PLLCON1_DSMPD_MASK,
  186. RK3036_PLLCON1_DSMPD_SHIFT),
  187. pll->reg_base + RK3036_PLLCON(1));
  188. /* GPLL CON2 is not HIWORD_MASK */
  189. pllcon = readl_relaxed(pll->reg_base + RK3036_PLLCON(2));
  190. pllcon &= ~(RK3036_PLLCON2_FRAC_MASK << RK3036_PLLCON2_FRAC_SHIFT);
  191. pllcon |= rate->frac << RK3036_PLLCON2_FRAC_SHIFT;
  192. writel_relaxed(pllcon, pll->reg_base + RK3036_PLLCON(2));
  193. /* wait for the pll to lock */
  194. ret = rockchip_rk3036_pll_wait_lock(pll);
  195. if (ret) {
  196. pr_warn("%s: pll update unsuccessful, trying to restore old params\n",
  197. __func__);
  198. rockchip_rk3036_pll_set_params(pll, &cur);
  199. }
  200. if (rate_change_remuxed)
  201. pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_NORM);
  202. return ret;
  203. }
  204. static int rockchip_rk3036_pll_set_rate(struct clk_hw *hw, unsigned long drate,
  205. unsigned long prate)
  206. {
  207. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  208. const struct rockchip_pll_rate_table *rate;
  209. pr_debug("%s: changing %s to %lu with a parent rate of %lu\n",
  210. __func__, __clk_get_name(hw->clk), drate, prate);
  211. /* Get required rate settings from table */
  212. rate = rockchip_get_pll_settings(pll, drate);
  213. if (!rate) {
  214. pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
  215. drate, __clk_get_name(hw->clk));
  216. return -EINVAL;
  217. }
  218. return rockchip_rk3036_pll_set_params(pll, rate);
  219. }
  220. static int rockchip_rk3036_pll_enable(struct clk_hw *hw)
  221. {
  222. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  223. writel(HIWORD_UPDATE(0, RK3036_PLLCON1_PWRDOWN, 0),
  224. pll->reg_base + RK3036_PLLCON(1));
  225. rockchip_rk3036_pll_wait_lock(pll);
  226. return 0;
  227. }
  228. static void rockchip_rk3036_pll_disable(struct clk_hw *hw)
  229. {
  230. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  231. writel(HIWORD_UPDATE(RK3036_PLLCON1_PWRDOWN,
  232. RK3036_PLLCON1_PWRDOWN, 0),
  233. pll->reg_base + RK3036_PLLCON(1));
  234. }
  235. static int rockchip_rk3036_pll_is_enabled(struct clk_hw *hw)
  236. {
  237. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  238. u32 pllcon = readl(pll->reg_base + RK3036_PLLCON(1));
  239. return !(pllcon & RK3036_PLLCON1_PWRDOWN);
  240. }
  241. static int rockchip_rk3036_pll_init(struct clk_hw *hw)
  242. {
  243. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  244. const struct rockchip_pll_rate_table *rate;
  245. struct rockchip_pll_rate_table cur;
  246. unsigned long drate;
  247. if (!(pll->flags & ROCKCHIP_PLL_SYNC_RATE))
  248. return 0;
  249. drate = clk_hw_get_rate(hw);
  250. rate = rockchip_get_pll_settings(pll, drate);
  251. /* when no rate setting for the current rate, rely on clk_set_rate */
  252. if (!rate)
  253. return 0;
  254. rockchip_rk3036_pll_get_params(pll, &cur);
  255. pr_debug("%s: pll %s@%lu: Hz\n", __func__, __clk_get_name(hw->clk),
  256. drate);
  257. pr_debug("old - fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
  258. cur.fbdiv, cur.postdiv1, cur.refdiv, cur.postdiv2,
  259. cur.dsmpd, cur.frac);
  260. pr_debug("new - fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
  261. rate->fbdiv, rate->postdiv1, rate->refdiv, rate->postdiv2,
  262. rate->dsmpd, rate->frac);
  263. if (rate->fbdiv != cur.fbdiv || rate->postdiv1 != cur.postdiv1 ||
  264. rate->refdiv != cur.refdiv || rate->postdiv2 != cur.postdiv2 ||
  265. rate->dsmpd != cur.dsmpd ||
  266. (!cur.dsmpd && (rate->frac != cur.frac))) {
  267. struct clk *parent = clk_get_parent(hw->clk);
  268. if (!parent) {
  269. pr_warn("%s: parent of %s not available\n",
  270. __func__, __clk_get_name(hw->clk));
  271. return 0;
  272. }
  273. pr_debug("%s: pll %s: rate params do not match rate table, adjusting\n",
  274. __func__, __clk_get_name(hw->clk));
  275. rockchip_rk3036_pll_set_params(pll, rate);
  276. }
  277. return 0;
  278. }
  279. static const struct clk_ops rockchip_rk3036_pll_clk_norate_ops = {
  280. .recalc_rate = rockchip_rk3036_pll_recalc_rate,
  281. .enable = rockchip_rk3036_pll_enable,
  282. .disable = rockchip_rk3036_pll_disable,
  283. .is_enabled = rockchip_rk3036_pll_is_enabled,
  284. };
  285. static const struct clk_ops rockchip_rk3036_pll_clk_ops = {
  286. .recalc_rate = rockchip_rk3036_pll_recalc_rate,
  287. .round_rate = rockchip_pll_round_rate,
  288. .set_rate = rockchip_rk3036_pll_set_rate,
  289. .enable = rockchip_rk3036_pll_enable,
  290. .disable = rockchip_rk3036_pll_disable,
  291. .is_enabled = rockchip_rk3036_pll_is_enabled,
  292. .init = rockchip_rk3036_pll_init,
  293. };
  294. /*
  295. * PLL used in RK3066, RK3188 and RK3288
  296. */
  297. #define RK3066_PLL_RESET_DELAY(nr) ((nr * 500) / 24 + 1)
  298. #define RK3066_PLLCON(i) (i * 0x4)
  299. #define RK3066_PLLCON0_OD_MASK 0xf
  300. #define RK3066_PLLCON0_OD_SHIFT 0
  301. #define RK3066_PLLCON0_NR_MASK 0x3f
  302. #define RK3066_PLLCON0_NR_SHIFT 8
  303. #define RK3066_PLLCON1_NF_MASK 0x1fff
  304. #define RK3066_PLLCON1_NF_SHIFT 0
  305. #define RK3066_PLLCON2_NB_MASK 0xfff
  306. #define RK3066_PLLCON2_NB_SHIFT 0
  307. #define RK3066_PLLCON3_RESET (1 << 5)
  308. #define RK3066_PLLCON3_PWRDOWN (1 << 1)
  309. #define RK3066_PLLCON3_BYPASS (1 << 0)
  310. static void rockchip_rk3066_pll_get_params(struct rockchip_clk_pll *pll,
  311. struct rockchip_pll_rate_table *rate)
  312. {
  313. u32 pllcon;
  314. pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(0));
  315. rate->nr = ((pllcon >> RK3066_PLLCON0_NR_SHIFT)
  316. & RK3066_PLLCON0_NR_MASK) + 1;
  317. rate->no = ((pllcon >> RK3066_PLLCON0_OD_SHIFT)
  318. & RK3066_PLLCON0_OD_MASK) + 1;
  319. pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(1));
  320. rate->nf = ((pllcon >> RK3066_PLLCON1_NF_SHIFT)
  321. & RK3066_PLLCON1_NF_MASK) + 1;
  322. pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(2));
  323. rate->nb = ((pllcon >> RK3066_PLLCON2_NB_SHIFT)
  324. & RK3066_PLLCON2_NB_MASK) + 1;
  325. }
  326. static unsigned long rockchip_rk3066_pll_recalc_rate(struct clk_hw *hw,
  327. unsigned long prate)
  328. {
  329. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  330. struct rockchip_pll_rate_table cur;
  331. u64 rate64 = prate;
  332. u32 pllcon;
  333. pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(3));
  334. if (pllcon & RK3066_PLLCON3_BYPASS) {
  335. pr_debug("%s: pll %s is bypassed\n", __func__,
  336. clk_hw_get_name(hw));
  337. return prate;
  338. }
  339. rockchip_rk3066_pll_get_params(pll, &cur);
  340. rate64 *= cur.nf;
  341. do_div(rate64, cur.nr);
  342. do_div(rate64, cur.no);
  343. return (unsigned long)rate64;
  344. }
  345. static int rockchip_rk3066_pll_set_params(struct rockchip_clk_pll *pll,
  346. const struct rockchip_pll_rate_table *rate)
  347. {
  348. const struct clk_ops *pll_mux_ops = pll->pll_mux_ops;
  349. struct clk_mux *pll_mux = &pll->pll_mux;
  350. struct rockchip_pll_rate_table cur;
  351. int rate_change_remuxed = 0;
  352. int cur_parent;
  353. int ret;
  354. pr_debug("%s: rate settings for %lu (nr, no, nf): (%d, %d, %d)\n",
  355. __func__, rate->rate, rate->nr, rate->no, rate->nf);
  356. rockchip_rk3066_pll_get_params(pll, &cur);
  357. cur.rate = 0;
  358. cur_parent = pll_mux_ops->get_parent(&pll_mux->hw);
  359. if (cur_parent == PLL_MODE_NORM) {
  360. pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_SLOW);
  361. rate_change_remuxed = 1;
  362. }
  363. /* enter reset mode */
  364. writel(HIWORD_UPDATE(RK3066_PLLCON3_RESET, RK3066_PLLCON3_RESET, 0),
  365. pll->reg_base + RK3066_PLLCON(3));
  366. /* update pll values */
  367. writel(HIWORD_UPDATE(rate->nr - 1, RK3066_PLLCON0_NR_MASK,
  368. RK3066_PLLCON0_NR_SHIFT) |
  369. HIWORD_UPDATE(rate->no - 1, RK3066_PLLCON0_OD_MASK,
  370. RK3066_PLLCON0_OD_SHIFT),
  371. pll->reg_base + RK3066_PLLCON(0));
  372. writel_relaxed(HIWORD_UPDATE(rate->nf - 1, RK3066_PLLCON1_NF_MASK,
  373. RK3066_PLLCON1_NF_SHIFT),
  374. pll->reg_base + RK3066_PLLCON(1));
  375. writel_relaxed(HIWORD_UPDATE(rate->nb - 1, RK3066_PLLCON2_NB_MASK,
  376. RK3066_PLLCON2_NB_SHIFT),
  377. pll->reg_base + RK3066_PLLCON(2));
  378. /* leave reset and wait the reset_delay */
  379. writel(HIWORD_UPDATE(0, RK3066_PLLCON3_RESET, 0),
  380. pll->reg_base + RK3066_PLLCON(3));
  381. udelay(RK3066_PLL_RESET_DELAY(rate->nr));
  382. /* wait for the pll to lock */
  383. ret = rockchip_pll_wait_lock(pll);
  384. if (ret) {
  385. pr_warn("%s: pll update unsuccessful, trying to restore old params\n",
  386. __func__);
  387. rockchip_rk3066_pll_set_params(pll, &cur);
  388. }
  389. if (rate_change_remuxed)
  390. pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_NORM);
  391. return ret;
  392. }
  393. static int rockchip_rk3066_pll_set_rate(struct clk_hw *hw, unsigned long drate,
  394. unsigned long prate)
  395. {
  396. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  397. const struct rockchip_pll_rate_table *rate;
  398. pr_debug("%s: changing %s to %lu with a parent rate of %lu\n",
  399. __func__, clk_hw_get_name(hw), drate, prate);
  400. /* Get required rate settings from table */
  401. rate = rockchip_get_pll_settings(pll, drate);
  402. if (!rate) {
  403. pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
  404. drate, clk_hw_get_name(hw));
  405. return -EINVAL;
  406. }
  407. return rockchip_rk3066_pll_set_params(pll, rate);
  408. }
  409. static int rockchip_rk3066_pll_enable(struct clk_hw *hw)
  410. {
  411. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  412. writel(HIWORD_UPDATE(0, RK3066_PLLCON3_PWRDOWN, 0),
  413. pll->reg_base + RK3066_PLLCON(3));
  414. rockchip_pll_wait_lock(pll);
  415. return 0;
  416. }
  417. static void rockchip_rk3066_pll_disable(struct clk_hw *hw)
  418. {
  419. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  420. writel(HIWORD_UPDATE(RK3066_PLLCON3_PWRDOWN,
  421. RK3066_PLLCON3_PWRDOWN, 0),
  422. pll->reg_base + RK3066_PLLCON(3));
  423. }
  424. static int rockchip_rk3066_pll_is_enabled(struct clk_hw *hw)
  425. {
  426. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  427. u32 pllcon = readl(pll->reg_base + RK3066_PLLCON(3));
  428. return !(pllcon & RK3066_PLLCON3_PWRDOWN);
  429. }
  430. static int rockchip_rk3066_pll_init(struct clk_hw *hw)
  431. {
  432. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  433. const struct rockchip_pll_rate_table *rate;
  434. struct rockchip_pll_rate_table cur;
  435. unsigned long drate;
  436. if (!(pll->flags & ROCKCHIP_PLL_SYNC_RATE))
  437. return 0;
  438. drate = clk_hw_get_rate(hw);
  439. rate = rockchip_get_pll_settings(pll, drate);
  440. /* when no rate setting for the current rate, rely on clk_set_rate */
  441. if (!rate)
  442. return 0;
  443. rockchip_rk3066_pll_get_params(pll, &cur);
  444. pr_debug("%s: pll %s@%lu: nr (%d:%d); no (%d:%d); nf(%d:%d), nb(%d:%d)\n",
  445. __func__, clk_hw_get_name(hw), drate, rate->nr, cur.nr,
  446. rate->no, cur.no, rate->nf, cur.nf, rate->nb, cur.nb);
  447. if (rate->nr != cur.nr || rate->no != cur.no || rate->nf != cur.nf
  448. || rate->nb != cur.nb) {
  449. pr_debug("%s: pll %s: rate params do not match rate table, adjusting\n",
  450. __func__, clk_hw_get_name(hw));
  451. rockchip_rk3066_pll_set_params(pll, rate);
  452. }
  453. return 0;
  454. }
  455. static const struct clk_ops rockchip_rk3066_pll_clk_norate_ops = {
  456. .recalc_rate = rockchip_rk3066_pll_recalc_rate,
  457. .enable = rockchip_rk3066_pll_enable,
  458. .disable = rockchip_rk3066_pll_disable,
  459. .is_enabled = rockchip_rk3066_pll_is_enabled,
  460. };
  461. static const struct clk_ops rockchip_rk3066_pll_clk_ops = {
  462. .recalc_rate = rockchip_rk3066_pll_recalc_rate,
  463. .round_rate = rockchip_pll_round_rate,
  464. .set_rate = rockchip_rk3066_pll_set_rate,
  465. .enable = rockchip_rk3066_pll_enable,
  466. .disable = rockchip_rk3066_pll_disable,
  467. .is_enabled = rockchip_rk3066_pll_is_enabled,
  468. .init = rockchip_rk3066_pll_init,
  469. };
  470. /*
  471. * PLL used in RK3399
  472. */
  473. #define RK3399_PLLCON(i) (i * 0x4)
  474. #define RK3399_PLLCON0_FBDIV_MASK 0xfff
  475. #define RK3399_PLLCON0_FBDIV_SHIFT 0
  476. #define RK3399_PLLCON1_REFDIV_MASK 0x3f
  477. #define RK3399_PLLCON1_REFDIV_SHIFT 0
  478. #define RK3399_PLLCON1_POSTDIV1_MASK 0x7
  479. #define RK3399_PLLCON1_POSTDIV1_SHIFT 8
  480. #define RK3399_PLLCON1_POSTDIV2_MASK 0x7
  481. #define RK3399_PLLCON1_POSTDIV2_SHIFT 12
  482. #define RK3399_PLLCON2_FRAC_MASK 0xffffff
  483. #define RK3399_PLLCON2_FRAC_SHIFT 0
  484. #define RK3399_PLLCON2_LOCK_STATUS BIT(31)
  485. #define RK3399_PLLCON3_PWRDOWN BIT(0)
  486. #define RK3399_PLLCON3_DSMPD_MASK 0x1
  487. #define RK3399_PLLCON3_DSMPD_SHIFT 3
  488. static int rockchip_rk3399_pll_wait_lock(struct rockchip_clk_pll *pll)
  489. {
  490. u32 pllcon;
  491. int ret;
  492. /*
  493. * Lock time typical 250, max 500 input clock cycles @24MHz
  494. * So define a very safe maximum of 1000us, meaning 24000 cycles.
  495. */
  496. ret = readl_relaxed_poll_timeout(pll->reg_base + RK3399_PLLCON(2),
  497. pllcon,
  498. pllcon & RK3399_PLLCON2_LOCK_STATUS,
  499. 0, 1000);
  500. if (ret)
  501. pr_err("%s: timeout waiting for pll to lock\n", __func__);
  502. return ret;
  503. }
  504. static void rockchip_rk3399_pll_get_params(struct rockchip_clk_pll *pll,
  505. struct rockchip_pll_rate_table *rate)
  506. {
  507. u32 pllcon;
  508. pllcon = readl_relaxed(pll->reg_base + RK3399_PLLCON(0));
  509. rate->fbdiv = ((pllcon >> RK3399_PLLCON0_FBDIV_SHIFT)
  510. & RK3399_PLLCON0_FBDIV_MASK);
  511. pllcon = readl_relaxed(pll->reg_base + RK3399_PLLCON(1));
  512. rate->refdiv = ((pllcon >> RK3399_PLLCON1_REFDIV_SHIFT)
  513. & RK3399_PLLCON1_REFDIV_MASK);
  514. rate->postdiv1 = ((pllcon >> RK3399_PLLCON1_POSTDIV1_SHIFT)
  515. & RK3399_PLLCON1_POSTDIV1_MASK);
  516. rate->postdiv2 = ((pllcon >> RK3399_PLLCON1_POSTDIV2_SHIFT)
  517. & RK3399_PLLCON1_POSTDIV2_MASK);
  518. pllcon = readl_relaxed(pll->reg_base + RK3399_PLLCON(2));
  519. rate->frac = ((pllcon >> RK3399_PLLCON2_FRAC_SHIFT)
  520. & RK3399_PLLCON2_FRAC_MASK);
  521. pllcon = readl_relaxed(pll->reg_base + RK3399_PLLCON(3));
  522. rate->dsmpd = ((pllcon >> RK3399_PLLCON3_DSMPD_SHIFT)
  523. & RK3399_PLLCON3_DSMPD_MASK);
  524. }
  525. static unsigned long rockchip_rk3399_pll_recalc_rate(struct clk_hw *hw,
  526. unsigned long prate)
  527. {
  528. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  529. struct rockchip_pll_rate_table cur;
  530. u64 rate64 = prate;
  531. rockchip_rk3399_pll_get_params(pll, &cur);
  532. rate64 *= cur.fbdiv;
  533. do_div(rate64, cur.refdiv);
  534. if (cur.dsmpd == 0) {
  535. /* fractional mode */
  536. u64 frac_rate64 = prate * cur.frac;
  537. do_div(frac_rate64, cur.refdiv);
  538. rate64 += frac_rate64 >> 24;
  539. }
  540. do_div(rate64, cur.postdiv1);
  541. do_div(rate64, cur.postdiv2);
  542. return (unsigned long)rate64;
  543. }
  544. static int rockchip_rk3399_pll_set_params(struct rockchip_clk_pll *pll,
  545. const struct rockchip_pll_rate_table *rate)
  546. {
  547. const struct clk_ops *pll_mux_ops = pll->pll_mux_ops;
  548. struct clk_mux *pll_mux = &pll->pll_mux;
  549. struct rockchip_pll_rate_table cur;
  550. u32 pllcon;
  551. int rate_change_remuxed = 0;
  552. int cur_parent;
  553. int ret;
  554. pr_debug("%s: rate settings for %lu fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
  555. __func__, rate->rate, rate->fbdiv, rate->postdiv1, rate->refdiv,
  556. rate->postdiv2, rate->dsmpd, rate->frac);
  557. rockchip_rk3399_pll_get_params(pll, &cur);
  558. cur.rate = 0;
  559. cur_parent = pll_mux_ops->get_parent(&pll_mux->hw);
  560. if (cur_parent == PLL_MODE_NORM) {
  561. pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_SLOW);
  562. rate_change_remuxed = 1;
  563. }
  564. /* update pll values */
  565. writel_relaxed(HIWORD_UPDATE(rate->fbdiv, RK3399_PLLCON0_FBDIV_MASK,
  566. RK3399_PLLCON0_FBDIV_SHIFT),
  567. pll->reg_base + RK3399_PLLCON(0));
  568. writel_relaxed(HIWORD_UPDATE(rate->refdiv, RK3399_PLLCON1_REFDIV_MASK,
  569. RK3399_PLLCON1_REFDIV_SHIFT) |
  570. HIWORD_UPDATE(rate->postdiv1, RK3399_PLLCON1_POSTDIV1_MASK,
  571. RK3399_PLLCON1_POSTDIV1_SHIFT) |
  572. HIWORD_UPDATE(rate->postdiv2, RK3399_PLLCON1_POSTDIV2_MASK,
  573. RK3399_PLLCON1_POSTDIV2_SHIFT),
  574. pll->reg_base + RK3399_PLLCON(1));
  575. /* xPLL CON2 is not HIWORD_MASK */
  576. pllcon = readl_relaxed(pll->reg_base + RK3399_PLLCON(2));
  577. pllcon &= ~(RK3399_PLLCON2_FRAC_MASK << RK3399_PLLCON2_FRAC_SHIFT);
  578. pllcon |= rate->frac << RK3399_PLLCON2_FRAC_SHIFT;
  579. writel_relaxed(pllcon, pll->reg_base + RK3399_PLLCON(2));
  580. writel_relaxed(HIWORD_UPDATE(rate->dsmpd, RK3399_PLLCON3_DSMPD_MASK,
  581. RK3399_PLLCON3_DSMPD_SHIFT),
  582. pll->reg_base + RK3399_PLLCON(3));
  583. /* wait for the pll to lock */
  584. ret = rockchip_rk3399_pll_wait_lock(pll);
  585. if (ret) {
  586. pr_warn("%s: pll update unsuccessful, trying to restore old params\n",
  587. __func__);
  588. rockchip_rk3399_pll_set_params(pll, &cur);
  589. }
  590. if (rate_change_remuxed)
  591. pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_NORM);
  592. return ret;
  593. }
  594. static int rockchip_rk3399_pll_set_rate(struct clk_hw *hw, unsigned long drate,
  595. unsigned long prate)
  596. {
  597. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  598. const struct rockchip_pll_rate_table *rate;
  599. pr_debug("%s: changing %s to %lu with a parent rate of %lu\n",
  600. __func__, __clk_get_name(hw->clk), drate, prate);
  601. /* Get required rate settings from table */
  602. rate = rockchip_get_pll_settings(pll, drate);
  603. if (!rate) {
  604. pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
  605. drate, __clk_get_name(hw->clk));
  606. return -EINVAL;
  607. }
  608. return rockchip_rk3399_pll_set_params(pll, rate);
  609. }
  610. static int rockchip_rk3399_pll_enable(struct clk_hw *hw)
  611. {
  612. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  613. writel(HIWORD_UPDATE(0, RK3399_PLLCON3_PWRDOWN, 0),
  614. pll->reg_base + RK3399_PLLCON(3));
  615. rockchip_rk3399_pll_wait_lock(pll);
  616. return 0;
  617. }
  618. static void rockchip_rk3399_pll_disable(struct clk_hw *hw)
  619. {
  620. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  621. writel(HIWORD_UPDATE(RK3399_PLLCON3_PWRDOWN,
  622. RK3399_PLLCON3_PWRDOWN, 0),
  623. pll->reg_base + RK3399_PLLCON(3));
  624. }
  625. static int rockchip_rk3399_pll_is_enabled(struct clk_hw *hw)
  626. {
  627. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  628. u32 pllcon = readl(pll->reg_base + RK3399_PLLCON(3));
  629. return !(pllcon & RK3399_PLLCON3_PWRDOWN);
  630. }
  631. static int rockchip_rk3399_pll_init(struct clk_hw *hw)
  632. {
  633. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  634. const struct rockchip_pll_rate_table *rate;
  635. struct rockchip_pll_rate_table cur;
  636. unsigned long drate;
  637. if (!(pll->flags & ROCKCHIP_PLL_SYNC_RATE))
  638. return 0;
  639. drate = clk_hw_get_rate(hw);
  640. rate = rockchip_get_pll_settings(pll, drate);
  641. /* when no rate setting for the current rate, rely on clk_set_rate */
  642. if (!rate)
  643. return 0;
  644. rockchip_rk3399_pll_get_params(pll, &cur);
  645. pr_debug("%s: pll %s@%lu: Hz\n", __func__, __clk_get_name(hw->clk),
  646. drate);
  647. pr_debug("old - fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
  648. cur.fbdiv, cur.postdiv1, cur.refdiv, cur.postdiv2,
  649. cur.dsmpd, cur.frac);
  650. pr_debug("new - fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
  651. rate->fbdiv, rate->postdiv1, rate->refdiv, rate->postdiv2,
  652. rate->dsmpd, rate->frac);
  653. if (rate->fbdiv != cur.fbdiv || rate->postdiv1 != cur.postdiv1 ||
  654. rate->refdiv != cur.refdiv || rate->postdiv2 != cur.postdiv2 ||
  655. rate->dsmpd != cur.dsmpd ||
  656. (!cur.dsmpd && (rate->frac != cur.frac))) {
  657. struct clk *parent = clk_get_parent(hw->clk);
  658. if (!parent) {
  659. pr_warn("%s: parent of %s not available\n",
  660. __func__, __clk_get_name(hw->clk));
  661. return 0;
  662. }
  663. pr_debug("%s: pll %s: rate params do not match rate table, adjusting\n",
  664. __func__, __clk_get_name(hw->clk));
  665. rockchip_rk3399_pll_set_params(pll, rate);
  666. }
  667. return 0;
  668. }
  669. static const struct clk_ops rockchip_rk3399_pll_clk_norate_ops = {
  670. .recalc_rate = rockchip_rk3399_pll_recalc_rate,
  671. .enable = rockchip_rk3399_pll_enable,
  672. .disable = rockchip_rk3399_pll_disable,
  673. .is_enabled = rockchip_rk3399_pll_is_enabled,
  674. };
  675. static const struct clk_ops rockchip_rk3399_pll_clk_ops = {
  676. .recalc_rate = rockchip_rk3399_pll_recalc_rate,
  677. .round_rate = rockchip_pll_round_rate,
  678. .set_rate = rockchip_rk3399_pll_set_rate,
  679. .enable = rockchip_rk3399_pll_enable,
  680. .disable = rockchip_rk3399_pll_disable,
  681. .is_enabled = rockchip_rk3399_pll_is_enabled,
  682. .init = rockchip_rk3399_pll_init,
  683. };
  684. /*
  685. * Common registering of pll clocks
  686. */
  687. struct clk *rockchip_clk_register_pll(struct rockchip_clk_provider *ctx,
  688. enum rockchip_pll_type pll_type,
  689. const char *name, const char *const *parent_names,
  690. u8 num_parents, int con_offset, int grf_lock_offset,
  691. int lock_shift, int mode_offset, int mode_shift,
  692. struct rockchip_pll_rate_table *rate_table,
  693. unsigned long flags, u8 clk_pll_flags)
  694. {
  695. const char *pll_parents[3];
  696. struct clk_init_data init;
  697. struct rockchip_clk_pll *pll;
  698. struct clk_mux *pll_mux;
  699. struct clk *pll_clk, *mux_clk;
  700. char pll_name[20];
  701. if ((pll_type != pll_rk3328 && num_parents != 2) ||
  702. (pll_type == pll_rk3328 && num_parents != 1)) {
  703. pr_err("%s: needs two parent clocks\n", __func__);
  704. return ERR_PTR(-EINVAL);
  705. }
  706. /* name the actual pll */
  707. snprintf(pll_name, sizeof(pll_name), "pll_%s", name);
  708. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  709. if (!pll)
  710. return ERR_PTR(-ENOMEM);
  711. /* create the mux on top of the real pll */
  712. pll->pll_mux_ops = &clk_mux_ops;
  713. pll_mux = &pll->pll_mux;
  714. pll_mux->reg = ctx->reg_base + mode_offset;
  715. pll_mux->shift = mode_shift;
  716. if (pll_type == pll_rk3328)
  717. pll_mux->mask = PLL_RK3328_MODE_MASK;
  718. else
  719. pll_mux->mask = PLL_MODE_MASK;
  720. pll_mux->flags = 0;
  721. pll_mux->lock = &ctx->lock;
  722. pll_mux->hw.init = &init;
  723. if (pll_type == pll_rk3036 ||
  724. pll_type == pll_rk3066 ||
  725. pll_type == pll_rk3328 ||
  726. pll_type == pll_rk3399)
  727. pll_mux->flags |= CLK_MUX_HIWORD_MASK;
  728. /* the actual muxing is xin24m, pll-output, xin32k */
  729. pll_parents[0] = parent_names[0];
  730. pll_parents[1] = pll_name;
  731. pll_parents[2] = parent_names[1];
  732. init.name = name;
  733. init.flags = CLK_SET_RATE_PARENT;
  734. init.ops = pll->pll_mux_ops;
  735. init.parent_names = pll_parents;
  736. if (pll_type == pll_rk3328)
  737. init.num_parents = 2;
  738. else
  739. init.num_parents = ARRAY_SIZE(pll_parents);
  740. mux_clk = clk_register(NULL, &pll_mux->hw);
  741. if (IS_ERR(mux_clk))
  742. goto err_mux;
  743. /* now create the actual pll */
  744. init.name = pll_name;
  745. /* keep all plls untouched for now */
  746. init.flags = flags | CLK_IGNORE_UNUSED;
  747. init.parent_names = &parent_names[0];
  748. init.num_parents = 1;
  749. if (rate_table) {
  750. int len;
  751. /* find count of rates in rate_table */
  752. for (len = 0; rate_table[len].rate != 0; )
  753. len++;
  754. pll->rate_count = len;
  755. pll->rate_table = kmemdup(rate_table,
  756. pll->rate_count *
  757. sizeof(struct rockchip_pll_rate_table),
  758. GFP_KERNEL);
  759. WARN(!pll->rate_table,
  760. "%s: could not allocate rate table for %s\n",
  761. __func__, name);
  762. }
  763. switch (pll_type) {
  764. case pll_rk3036:
  765. case pll_rk3328:
  766. if (!pll->rate_table)
  767. init.ops = &rockchip_rk3036_pll_clk_norate_ops;
  768. else
  769. init.ops = &rockchip_rk3036_pll_clk_ops;
  770. break;
  771. case pll_rk3066:
  772. if (!pll->rate_table || IS_ERR(ctx->grf))
  773. init.ops = &rockchip_rk3066_pll_clk_norate_ops;
  774. else
  775. init.ops = &rockchip_rk3066_pll_clk_ops;
  776. break;
  777. case pll_rk3399:
  778. if (!pll->rate_table)
  779. init.ops = &rockchip_rk3399_pll_clk_norate_ops;
  780. else
  781. init.ops = &rockchip_rk3399_pll_clk_ops;
  782. break;
  783. default:
  784. pr_warn("%s: Unknown pll type for pll clk %s\n",
  785. __func__, name);
  786. }
  787. pll->hw.init = &init;
  788. pll->type = pll_type;
  789. pll->reg_base = ctx->reg_base + con_offset;
  790. pll->lock_offset = grf_lock_offset;
  791. pll->lock_shift = lock_shift;
  792. pll->flags = clk_pll_flags;
  793. pll->lock = &ctx->lock;
  794. pll->ctx = ctx;
  795. pll_clk = clk_register(NULL, &pll->hw);
  796. if (IS_ERR(pll_clk)) {
  797. pr_err("%s: failed to register pll clock %s : %ld\n",
  798. __func__, name, PTR_ERR(pll_clk));
  799. goto err_pll;
  800. }
  801. return mux_clk;
  802. err_pll:
  803. kfree(pll->rate_table);
  804. clk_unregister(mux_clk);
  805. mux_clk = pll_clk;
  806. err_mux:
  807. kfree(pll);
  808. return mux_clk;
  809. }