clk-pll.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  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. return 0;
  225. }
  226. static void rockchip_rk3036_pll_disable(struct clk_hw *hw)
  227. {
  228. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  229. writel(HIWORD_UPDATE(RK3036_PLLCON1_PWRDOWN,
  230. RK3036_PLLCON1_PWRDOWN, 0),
  231. pll->reg_base + RK3036_PLLCON(1));
  232. }
  233. static int rockchip_rk3036_pll_is_enabled(struct clk_hw *hw)
  234. {
  235. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  236. u32 pllcon = readl(pll->reg_base + RK3036_PLLCON(1));
  237. return !(pllcon & RK3036_PLLCON1_PWRDOWN);
  238. }
  239. static void rockchip_rk3036_pll_init(struct clk_hw *hw)
  240. {
  241. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  242. const struct rockchip_pll_rate_table *rate;
  243. struct rockchip_pll_rate_table cur;
  244. unsigned long drate;
  245. if (!(pll->flags & ROCKCHIP_PLL_SYNC_RATE))
  246. return;
  247. drate = clk_hw_get_rate(hw);
  248. rate = rockchip_get_pll_settings(pll, drate);
  249. /* when no rate setting for the current rate, rely on clk_set_rate */
  250. if (!rate)
  251. return;
  252. rockchip_rk3036_pll_get_params(pll, &cur);
  253. pr_debug("%s: pll %s@%lu: Hz\n", __func__, __clk_get_name(hw->clk),
  254. drate);
  255. pr_debug("old - fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
  256. cur.fbdiv, cur.postdiv1, cur.refdiv, cur.postdiv2,
  257. cur.dsmpd, cur.frac);
  258. pr_debug("new - fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
  259. rate->fbdiv, rate->postdiv1, rate->refdiv, rate->postdiv2,
  260. rate->dsmpd, rate->frac);
  261. if (rate->fbdiv != cur.fbdiv || rate->postdiv1 != cur.postdiv1 ||
  262. rate->refdiv != cur.refdiv || rate->postdiv2 != cur.postdiv2 ||
  263. rate->dsmpd != cur.dsmpd ||
  264. (!cur.dsmpd && (rate->frac != cur.frac))) {
  265. struct clk *parent = clk_get_parent(hw->clk);
  266. if (!parent) {
  267. pr_warn("%s: parent of %s not available\n",
  268. __func__, __clk_get_name(hw->clk));
  269. return;
  270. }
  271. pr_debug("%s: pll %s: rate params do not match rate table, adjusting\n",
  272. __func__, __clk_get_name(hw->clk));
  273. rockchip_rk3036_pll_set_params(pll, rate);
  274. }
  275. }
  276. static const struct clk_ops rockchip_rk3036_pll_clk_norate_ops = {
  277. .recalc_rate = rockchip_rk3036_pll_recalc_rate,
  278. .enable = rockchip_rk3036_pll_enable,
  279. .disable = rockchip_rk3036_pll_disable,
  280. .is_enabled = rockchip_rk3036_pll_is_enabled,
  281. };
  282. static const struct clk_ops rockchip_rk3036_pll_clk_ops = {
  283. .recalc_rate = rockchip_rk3036_pll_recalc_rate,
  284. .round_rate = rockchip_pll_round_rate,
  285. .set_rate = rockchip_rk3036_pll_set_rate,
  286. .enable = rockchip_rk3036_pll_enable,
  287. .disable = rockchip_rk3036_pll_disable,
  288. .is_enabled = rockchip_rk3036_pll_is_enabled,
  289. .init = rockchip_rk3036_pll_init,
  290. };
  291. /**
  292. * PLL used in RK3066, RK3188 and RK3288
  293. */
  294. #define RK3066_PLL_RESET_DELAY(nr) ((nr * 500) / 24 + 1)
  295. #define RK3066_PLLCON(i) (i * 0x4)
  296. #define RK3066_PLLCON0_OD_MASK 0xf
  297. #define RK3066_PLLCON0_OD_SHIFT 0
  298. #define RK3066_PLLCON0_NR_MASK 0x3f
  299. #define RK3066_PLLCON0_NR_SHIFT 8
  300. #define RK3066_PLLCON1_NF_MASK 0x1fff
  301. #define RK3066_PLLCON1_NF_SHIFT 0
  302. #define RK3066_PLLCON2_NB_MASK 0xfff
  303. #define RK3066_PLLCON2_NB_SHIFT 0
  304. #define RK3066_PLLCON3_RESET (1 << 5)
  305. #define RK3066_PLLCON3_PWRDOWN (1 << 1)
  306. #define RK3066_PLLCON3_BYPASS (1 << 0)
  307. static void rockchip_rk3066_pll_get_params(struct rockchip_clk_pll *pll,
  308. struct rockchip_pll_rate_table *rate)
  309. {
  310. u32 pllcon;
  311. pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(0));
  312. rate->nr = ((pllcon >> RK3066_PLLCON0_NR_SHIFT)
  313. & RK3066_PLLCON0_NR_MASK) + 1;
  314. rate->no = ((pllcon >> RK3066_PLLCON0_OD_SHIFT)
  315. & RK3066_PLLCON0_OD_MASK) + 1;
  316. pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(1));
  317. rate->nf = ((pllcon >> RK3066_PLLCON1_NF_SHIFT)
  318. & RK3066_PLLCON1_NF_MASK) + 1;
  319. pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(2));
  320. rate->nb = ((pllcon >> RK3066_PLLCON2_NB_SHIFT)
  321. & RK3066_PLLCON2_NB_MASK) + 1;
  322. }
  323. static unsigned long rockchip_rk3066_pll_recalc_rate(struct clk_hw *hw,
  324. unsigned long prate)
  325. {
  326. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  327. struct rockchip_pll_rate_table cur;
  328. u64 rate64 = prate;
  329. u32 pllcon;
  330. pllcon = readl_relaxed(pll->reg_base + RK3066_PLLCON(3));
  331. if (pllcon & RK3066_PLLCON3_BYPASS) {
  332. pr_debug("%s: pll %s is bypassed\n", __func__,
  333. clk_hw_get_name(hw));
  334. return prate;
  335. }
  336. rockchip_rk3066_pll_get_params(pll, &cur);
  337. rate64 *= cur.nf;
  338. do_div(rate64, cur.nr);
  339. do_div(rate64, cur.no);
  340. return (unsigned long)rate64;
  341. }
  342. static int rockchip_rk3066_pll_set_params(struct rockchip_clk_pll *pll,
  343. const struct rockchip_pll_rate_table *rate)
  344. {
  345. const struct clk_ops *pll_mux_ops = pll->pll_mux_ops;
  346. struct clk_mux *pll_mux = &pll->pll_mux;
  347. struct rockchip_pll_rate_table cur;
  348. int rate_change_remuxed = 0;
  349. int cur_parent;
  350. int ret;
  351. pr_debug("%s: rate settings for %lu (nr, no, nf): (%d, %d, %d)\n",
  352. __func__, rate->rate, rate->nr, rate->no, rate->nf);
  353. rockchip_rk3066_pll_get_params(pll, &cur);
  354. cur.rate = 0;
  355. cur_parent = pll_mux_ops->get_parent(&pll_mux->hw);
  356. if (cur_parent == PLL_MODE_NORM) {
  357. pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_SLOW);
  358. rate_change_remuxed = 1;
  359. }
  360. /* enter reset mode */
  361. writel(HIWORD_UPDATE(RK3066_PLLCON3_RESET, RK3066_PLLCON3_RESET, 0),
  362. pll->reg_base + RK3066_PLLCON(3));
  363. /* update pll values */
  364. writel(HIWORD_UPDATE(rate->nr - 1, RK3066_PLLCON0_NR_MASK,
  365. RK3066_PLLCON0_NR_SHIFT) |
  366. HIWORD_UPDATE(rate->no - 1, RK3066_PLLCON0_OD_MASK,
  367. RK3066_PLLCON0_OD_SHIFT),
  368. pll->reg_base + RK3066_PLLCON(0));
  369. writel_relaxed(HIWORD_UPDATE(rate->nf - 1, RK3066_PLLCON1_NF_MASK,
  370. RK3066_PLLCON1_NF_SHIFT),
  371. pll->reg_base + RK3066_PLLCON(1));
  372. writel_relaxed(HIWORD_UPDATE(rate->nb - 1, RK3066_PLLCON2_NB_MASK,
  373. RK3066_PLLCON2_NB_SHIFT),
  374. pll->reg_base + RK3066_PLLCON(2));
  375. /* leave reset and wait the reset_delay */
  376. writel(HIWORD_UPDATE(0, RK3066_PLLCON3_RESET, 0),
  377. pll->reg_base + RK3066_PLLCON(3));
  378. udelay(RK3066_PLL_RESET_DELAY(rate->nr));
  379. /* wait for the pll to lock */
  380. ret = rockchip_pll_wait_lock(pll);
  381. if (ret) {
  382. pr_warn("%s: pll update unsuccessful, trying to restore old params\n",
  383. __func__);
  384. rockchip_rk3066_pll_set_params(pll, &cur);
  385. }
  386. if (rate_change_remuxed)
  387. pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_NORM);
  388. return ret;
  389. }
  390. static int rockchip_rk3066_pll_set_rate(struct clk_hw *hw, unsigned long drate,
  391. unsigned long prate)
  392. {
  393. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  394. const struct rockchip_pll_rate_table *rate;
  395. pr_debug("%s: changing %s to %lu with a parent rate of %lu\n",
  396. __func__, clk_hw_get_name(hw), drate, prate);
  397. /* Get required rate settings from table */
  398. rate = rockchip_get_pll_settings(pll, drate);
  399. if (!rate) {
  400. pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
  401. drate, clk_hw_get_name(hw));
  402. return -EINVAL;
  403. }
  404. return rockchip_rk3066_pll_set_params(pll, rate);
  405. }
  406. static int rockchip_rk3066_pll_enable(struct clk_hw *hw)
  407. {
  408. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  409. writel(HIWORD_UPDATE(0, RK3066_PLLCON3_PWRDOWN, 0),
  410. pll->reg_base + RK3066_PLLCON(3));
  411. return 0;
  412. }
  413. static void rockchip_rk3066_pll_disable(struct clk_hw *hw)
  414. {
  415. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  416. writel(HIWORD_UPDATE(RK3066_PLLCON3_PWRDOWN,
  417. RK3066_PLLCON3_PWRDOWN, 0),
  418. pll->reg_base + RK3066_PLLCON(3));
  419. }
  420. static int rockchip_rk3066_pll_is_enabled(struct clk_hw *hw)
  421. {
  422. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  423. u32 pllcon = readl(pll->reg_base + RK3066_PLLCON(3));
  424. return !(pllcon & RK3066_PLLCON3_PWRDOWN);
  425. }
  426. static void rockchip_rk3066_pll_init(struct clk_hw *hw)
  427. {
  428. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  429. const struct rockchip_pll_rate_table *rate;
  430. struct rockchip_pll_rate_table cur;
  431. unsigned long drate;
  432. if (!(pll->flags & ROCKCHIP_PLL_SYNC_RATE))
  433. return;
  434. drate = clk_hw_get_rate(hw);
  435. rate = rockchip_get_pll_settings(pll, drate);
  436. /* when no rate setting for the current rate, rely on clk_set_rate */
  437. if (!rate)
  438. return;
  439. rockchip_rk3066_pll_get_params(pll, &cur);
  440. pr_debug("%s: pll %s@%lu: nr (%d:%d); no (%d:%d); nf(%d:%d), nb(%d:%d)\n",
  441. __func__, clk_hw_get_name(hw), drate, rate->nr, cur.nr,
  442. rate->no, cur.no, rate->nf, cur.nf, rate->nb, cur.nb);
  443. if (rate->nr != cur.nr || rate->no != cur.no || rate->nf != cur.nf
  444. || rate->nb != cur.nb) {
  445. pr_debug("%s: pll %s: rate params do not match rate table, adjusting\n",
  446. __func__, clk_hw_get_name(hw));
  447. rockchip_rk3066_pll_set_params(pll, rate);
  448. }
  449. }
  450. static const struct clk_ops rockchip_rk3066_pll_clk_norate_ops = {
  451. .recalc_rate = rockchip_rk3066_pll_recalc_rate,
  452. .enable = rockchip_rk3066_pll_enable,
  453. .disable = rockchip_rk3066_pll_disable,
  454. .is_enabled = rockchip_rk3066_pll_is_enabled,
  455. };
  456. static const struct clk_ops rockchip_rk3066_pll_clk_ops = {
  457. .recalc_rate = rockchip_rk3066_pll_recalc_rate,
  458. .round_rate = rockchip_pll_round_rate,
  459. .set_rate = rockchip_rk3066_pll_set_rate,
  460. .enable = rockchip_rk3066_pll_enable,
  461. .disable = rockchip_rk3066_pll_disable,
  462. .is_enabled = rockchip_rk3066_pll_is_enabled,
  463. .init = rockchip_rk3066_pll_init,
  464. };
  465. /**
  466. * PLL used in RK3399
  467. */
  468. #define RK3399_PLLCON(i) (i * 0x4)
  469. #define RK3399_PLLCON0_FBDIV_MASK 0xfff
  470. #define RK3399_PLLCON0_FBDIV_SHIFT 0
  471. #define RK3399_PLLCON1_REFDIV_MASK 0x3f
  472. #define RK3399_PLLCON1_REFDIV_SHIFT 0
  473. #define RK3399_PLLCON1_POSTDIV1_MASK 0x7
  474. #define RK3399_PLLCON1_POSTDIV1_SHIFT 8
  475. #define RK3399_PLLCON1_POSTDIV2_MASK 0x7
  476. #define RK3399_PLLCON1_POSTDIV2_SHIFT 12
  477. #define RK3399_PLLCON2_FRAC_MASK 0xffffff
  478. #define RK3399_PLLCON2_FRAC_SHIFT 0
  479. #define RK3399_PLLCON2_LOCK_STATUS BIT(31)
  480. #define RK3399_PLLCON3_PWRDOWN BIT(0)
  481. #define RK3399_PLLCON3_DSMPD_MASK 0x1
  482. #define RK3399_PLLCON3_DSMPD_SHIFT 3
  483. static int rockchip_rk3399_pll_wait_lock(struct rockchip_clk_pll *pll)
  484. {
  485. u32 pllcon;
  486. int delay = 24000000;
  487. /* poll check the lock status in rk3399 xPLLCON2 */
  488. while (delay > 0) {
  489. pllcon = readl_relaxed(pll->reg_base + RK3399_PLLCON(2));
  490. if (pllcon & RK3399_PLLCON2_LOCK_STATUS)
  491. return 0;
  492. delay--;
  493. }
  494. pr_err("%s: timeout waiting for pll to lock\n", __func__);
  495. return -ETIMEDOUT;
  496. }
  497. static void rockchip_rk3399_pll_get_params(struct rockchip_clk_pll *pll,
  498. struct rockchip_pll_rate_table *rate)
  499. {
  500. u32 pllcon;
  501. pllcon = readl_relaxed(pll->reg_base + RK3399_PLLCON(0));
  502. rate->fbdiv = ((pllcon >> RK3399_PLLCON0_FBDIV_SHIFT)
  503. & RK3399_PLLCON0_FBDIV_MASK);
  504. pllcon = readl_relaxed(pll->reg_base + RK3399_PLLCON(1));
  505. rate->refdiv = ((pllcon >> RK3399_PLLCON1_REFDIV_SHIFT)
  506. & RK3399_PLLCON1_REFDIV_MASK);
  507. rate->postdiv1 = ((pllcon >> RK3399_PLLCON1_POSTDIV1_SHIFT)
  508. & RK3399_PLLCON1_POSTDIV1_MASK);
  509. rate->postdiv2 = ((pllcon >> RK3399_PLLCON1_POSTDIV2_SHIFT)
  510. & RK3399_PLLCON1_POSTDIV2_MASK);
  511. pllcon = readl_relaxed(pll->reg_base + RK3399_PLLCON(2));
  512. rate->frac = ((pllcon >> RK3399_PLLCON2_FRAC_SHIFT)
  513. & RK3399_PLLCON2_FRAC_MASK);
  514. pllcon = readl_relaxed(pll->reg_base + RK3399_PLLCON(3));
  515. rate->dsmpd = ((pllcon >> RK3399_PLLCON3_DSMPD_SHIFT)
  516. & RK3399_PLLCON3_DSMPD_MASK);
  517. }
  518. static unsigned long rockchip_rk3399_pll_recalc_rate(struct clk_hw *hw,
  519. unsigned long prate)
  520. {
  521. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  522. struct rockchip_pll_rate_table cur;
  523. u64 rate64 = prate;
  524. rockchip_rk3399_pll_get_params(pll, &cur);
  525. rate64 *= cur.fbdiv;
  526. do_div(rate64, cur.refdiv);
  527. if (cur.dsmpd == 0) {
  528. /* fractional mode */
  529. u64 frac_rate64 = prate * cur.frac;
  530. do_div(frac_rate64, cur.refdiv);
  531. rate64 += frac_rate64 >> 24;
  532. }
  533. do_div(rate64, cur.postdiv1);
  534. do_div(rate64, cur.postdiv2);
  535. return (unsigned long)rate64;
  536. }
  537. static int rockchip_rk3399_pll_set_params(struct rockchip_clk_pll *pll,
  538. const struct rockchip_pll_rate_table *rate)
  539. {
  540. const struct clk_ops *pll_mux_ops = pll->pll_mux_ops;
  541. struct clk_mux *pll_mux = &pll->pll_mux;
  542. struct rockchip_pll_rate_table cur;
  543. u32 pllcon;
  544. int rate_change_remuxed = 0;
  545. int cur_parent;
  546. int ret;
  547. pr_debug("%s: rate settings for %lu fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
  548. __func__, rate->rate, rate->fbdiv, rate->postdiv1, rate->refdiv,
  549. rate->postdiv2, rate->dsmpd, rate->frac);
  550. rockchip_rk3399_pll_get_params(pll, &cur);
  551. cur.rate = 0;
  552. cur_parent = pll_mux_ops->get_parent(&pll_mux->hw);
  553. if (cur_parent == PLL_MODE_NORM) {
  554. pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_SLOW);
  555. rate_change_remuxed = 1;
  556. }
  557. /* update pll values */
  558. writel_relaxed(HIWORD_UPDATE(rate->fbdiv, RK3399_PLLCON0_FBDIV_MASK,
  559. RK3399_PLLCON0_FBDIV_SHIFT),
  560. pll->reg_base + RK3399_PLLCON(0));
  561. writel_relaxed(HIWORD_UPDATE(rate->refdiv, RK3399_PLLCON1_REFDIV_MASK,
  562. RK3399_PLLCON1_REFDIV_SHIFT) |
  563. HIWORD_UPDATE(rate->postdiv1, RK3399_PLLCON1_POSTDIV1_MASK,
  564. RK3399_PLLCON1_POSTDIV1_SHIFT) |
  565. HIWORD_UPDATE(rate->postdiv2, RK3399_PLLCON1_POSTDIV2_MASK,
  566. RK3399_PLLCON1_POSTDIV2_SHIFT),
  567. pll->reg_base + RK3399_PLLCON(1));
  568. /* xPLL CON2 is not HIWORD_MASK */
  569. pllcon = readl_relaxed(pll->reg_base + RK3399_PLLCON(2));
  570. pllcon &= ~(RK3399_PLLCON2_FRAC_MASK << RK3399_PLLCON2_FRAC_SHIFT);
  571. pllcon |= rate->frac << RK3399_PLLCON2_FRAC_SHIFT;
  572. writel_relaxed(pllcon, pll->reg_base + RK3399_PLLCON(2));
  573. writel_relaxed(HIWORD_UPDATE(rate->dsmpd, RK3399_PLLCON3_DSMPD_MASK,
  574. RK3399_PLLCON3_DSMPD_SHIFT),
  575. pll->reg_base + RK3399_PLLCON(3));
  576. /* wait for the pll to lock */
  577. ret = rockchip_rk3399_pll_wait_lock(pll);
  578. if (ret) {
  579. pr_warn("%s: pll update unsuccessful, trying to restore old params\n",
  580. __func__);
  581. rockchip_rk3399_pll_set_params(pll, &cur);
  582. }
  583. if (rate_change_remuxed)
  584. pll_mux_ops->set_parent(&pll_mux->hw, PLL_MODE_NORM);
  585. return ret;
  586. }
  587. static int rockchip_rk3399_pll_set_rate(struct clk_hw *hw, unsigned long drate,
  588. unsigned long prate)
  589. {
  590. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  591. const struct rockchip_pll_rate_table *rate;
  592. pr_debug("%s: changing %s to %lu with a parent rate of %lu\n",
  593. __func__, __clk_get_name(hw->clk), drate, prate);
  594. /* Get required rate settings from table */
  595. rate = rockchip_get_pll_settings(pll, drate);
  596. if (!rate) {
  597. pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
  598. drate, __clk_get_name(hw->clk));
  599. return -EINVAL;
  600. }
  601. return rockchip_rk3399_pll_set_params(pll, rate);
  602. }
  603. static int rockchip_rk3399_pll_enable(struct clk_hw *hw)
  604. {
  605. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  606. writel(HIWORD_UPDATE(0, RK3399_PLLCON3_PWRDOWN, 0),
  607. pll->reg_base + RK3399_PLLCON(3));
  608. return 0;
  609. }
  610. static void rockchip_rk3399_pll_disable(struct clk_hw *hw)
  611. {
  612. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  613. writel(HIWORD_UPDATE(RK3399_PLLCON3_PWRDOWN,
  614. RK3399_PLLCON3_PWRDOWN, 0),
  615. pll->reg_base + RK3399_PLLCON(3));
  616. }
  617. static int rockchip_rk3399_pll_is_enabled(struct clk_hw *hw)
  618. {
  619. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  620. u32 pllcon = readl(pll->reg_base + RK3399_PLLCON(3));
  621. return !(pllcon & RK3399_PLLCON3_PWRDOWN);
  622. }
  623. static void rockchip_rk3399_pll_init(struct clk_hw *hw)
  624. {
  625. struct rockchip_clk_pll *pll = to_rockchip_clk_pll(hw);
  626. const struct rockchip_pll_rate_table *rate;
  627. struct rockchip_pll_rate_table cur;
  628. unsigned long drate;
  629. if (!(pll->flags & ROCKCHIP_PLL_SYNC_RATE))
  630. return;
  631. drate = clk_hw_get_rate(hw);
  632. rate = rockchip_get_pll_settings(pll, drate);
  633. /* when no rate setting for the current rate, rely on clk_set_rate */
  634. if (!rate)
  635. return;
  636. rockchip_rk3399_pll_get_params(pll, &cur);
  637. pr_debug("%s: pll %s@%lu: Hz\n", __func__, __clk_get_name(hw->clk),
  638. drate);
  639. pr_debug("old - fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
  640. cur.fbdiv, cur.postdiv1, cur.refdiv, cur.postdiv2,
  641. cur.dsmpd, cur.frac);
  642. pr_debug("new - fbdiv: %d, postdiv1: %d, refdiv: %d, postdiv2: %d, dsmpd: %d, frac: %d\n",
  643. rate->fbdiv, rate->postdiv1, rate->refdiv, rate->postdiv2,
  644. rate->dsmpd, rate->frac);
  645. if (rate->fbdiv != cur.fbdiv || rate->postdiv1 != cur.postdiv1 ||
  646. rate->refdiv != cur.refdiv || rate->postdiv2 != cur.postdiv2 ||
  647. rate->dsmpd != cur.dsmpd ||
  648. (!cur.dsmpd && (rate->frac != cur.frac))) {
  649. struct clk *parent = clk_get_parent(hw->clk);
  650. if (!parent) {
  651. pr_warn("%s: parent of %s not available\n",
  652. __func__, __clk_get_name(hw->clk));
  653. return;
  654. }
  655. pr_debug("%s: pll %s: rate params do not match rate table, adjusting\n",
  656. __func__, __clk_get_name(hw->clk));
  657. rockchip_rk3399_pll_set_params(pll, rate);
  658. }
  659. }
  660. static const struct clk_ops rockchip_rk3399_pll_clk_norate_ops = {
  661. .recalc_rate = rockchip_rk3399_pll_recalc_rate,
  662. .enable = rockchip_rk3399_pll_enable,
  663. .disable = rockchip_rk3399_pll_disable,
  664. .is_enabled = rockchip_rk3399_pll_is_enabled,
  665. };
  666. static const struct clk_ops rockchip_rk3399_pll_clk_ops = {
  667. .recalc_rate = rockchip_rk3399_pll_recalc_rate,
  668. .round_rate = rockchip_pll_round_rate,
  669. .set_rate = rockchip_rk3399_pll_set_rate,
  670. .enable = rockchip_rk3399_pll_enable,
  671. .disable = rockchip_rk3399_pll_disable,
  672. .is_enabled = rockchip_rk3399_pll_is_enabled,
  673. .init = rockchip_rk3399_pll_init,
  674. };
  675. /*
  676. * Common registering of pll clocks
  677. */
  678. struct clk *rockchip_clk_register_pll(struct rockchip_clk_provider *ctx,
  679. enum rockchip_pll_type pll_type,
  680. const char *name, const char *const *parent_names,
  681. u8 num_parents, int con_offset, int grf_lock_offset,
  682. int lock_shift, int mode_offset, int mode_shift,
  683. struct rockchip_pll_rate_table *rate_table,
  684. unsigned long flags, u8 clk_pll_flags)
  685. {
  686. const char *pll_parents[3];
  687. struct clk_init_data init;
  688. struct rockchip_clk_pll *pll;
  689. struct clk_mux *pll_mux;
  690. struct clk *pll_clk, *mux_clk;
  691. char pll_name[20];
  692. if ((pll_type != pll_rk3328 && num_parents != 2) ||
  693. (pll_type == pll_rk3328 && num_parents != 1)) {
  694. pr_err("%s: needs two parent clocks\n", __func__);
  695. return ERR_PTR(-EINVAL);
  696. }
  697. /* name the actual pll */
  698. snprintf(pll_name, sizeof(pll_name), "pll_%s", name);
  699. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  700. if (!pll)
  701. return ERR_PTR(-ENOMEM);
  702. /* create the mux on top of the real pll */
  703. pll->pll_mux_ops = &clk_mux_ops;
  704. pll_mux = &pll->pll_mux;
  705. pll_mux->reg = ctx->reg_base + mode_offset;
  706. pll_mux->shift = mode_shift;
  707. if (pll_type == pll_rk3328)
  708. pll_mux->mask = PLL_RK3328_MODE_MASK;
  709. else
  710. pll_mux->mask = PLL_MODE_MASK;
  711. pll_mux->flags = 0;
  712. pll_mux->lock = &ctx->lock;
  713. pll_mux->hw.init = &init;
  714. if (pll_type == pll_rk3036 ||
  715. pll_type == pll_rk3066 ||
  716. pll_type == pll_rk3328 ||
  717. pll_type == pll_rk3399)
  718. pll_mux->flags |= CLK_MUX_HIWORD_MASK;
  719. /* the actual muxing is xin24m, pll-output, xin32k */
  720. pll_parents[0] = parent_names[0];
  721. pll_parents[1] = pll_name;
  722. pll_parents[2] = parent_names[1];
  723. init.name = name;
  724. init.flags = CLK_SET_RATE_PARENT;
  725. init.ops = pll->pll_mux_ops;
  726. init.parent_names = pll_parents;
  727. if (pll_type == pll_rk3328)
  728. init.num_parents = 2;
  729. else
  730. init.num_parents = ARRAY_SIZE(pll_parents);
  731. mux_clk = clk_register(NULL, &pll_mux->hw);
  732. if (IS_ERR(mux_clk))
  733. goto err_mux;
  734. /* now create the actual pll */
  735. init.name = pll_name;
  736. /* keep all plls untouched for now */
  737. init.flags = flags | CLK_IGNORE_UNUSED;
  738. init.parent_names = &parent_names[0];
  739. init.num_parents = 1;
  740. if (rate_table) {
  741. int len;
  742. /* find count of rates in rate_table */
  743. for (len = 0; rate_table[len].rate != 0; )
  744. len++;
  745. pll->rate_count = len;
  746. pll->rate_table = kmemdup(rate_table,
  747. pll->rate_count *
  748. sizeof(struct rockchip_pll_rate_table),
  749. GFP_KERNEL);
  750. WARN(!pll->rate_table,
  751. "%s: could not allocate rate table for %s\n",
  752. __func__, name);
  753. }
  754. switch (pll_type) {
  755. case pll_rk3036:
  756. case pll_rk3328:
  757. if (!pll->rate_table || IS_ERR(ctx->grf))
  758. init.ops = &rockchip_rk3036_pll_clk_norate_ops;
  759. else
  760. init.ops = &rockchip_rk3036_pll_clk_ops;
  761. break;
  762. case pll_rk3066:
  763. if (!pll->rate_table || IS_ERR(ctx->grf))
  764. init.ops = &rockchip_rk3066_pll_clk_norate_ops;
  765. else
  766. init.ops = &rockchip_rk3066_pll_clk_ops;
  767. break;
  768. case pll_rk3399:
  769. if (!pll->rate_table)
  770. init.ops = &rockchip_rk3399_pll_clk_norate_ops;
  771. else
  772. init.ops = &rockchip_rk3399_pll_clk_ops;
  773. break;
  774. default:
  775. pr_warn("%s: Unknown pll type for pll clk %s\n",
  776. __func__, name);
  777. }
  778. pll->hw.init = &init;
  779. pll->type = pll_type;
  780. pll->reg_base = ctx->reg_base + con_offset;
  781. pll->lock_offset = grf_lock_offset;
  782. pll->lock_shift = lock_shift;
  783. pll->flags = clk_pll_flags;
  784. pll->lock = &ctx->lock;
  785. pll->ctx = ctx;
  786. pll_clk = clk_register(NULL, &pll->hw);
  787. if (IS_ERR(pll_clk)) {
  788. pr_err("%s: failed to register pll clock %s : %ld\n",
  789. __func__, name, PTR_ERR(pll_clk));
  790. goto err_pll;
  791. }
  792. return mux_clk;
  793. err_pll:
  794. clk_unregister(mux_clk);
  795. mux_clk = pll_clk;
  796. err_mux:
  797. kfree(pll);
  798. return mux_clk;
  799. }