clk-pll.c 28 KB

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