clk-pll.c 28 KB

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