clk-pll.c 34 KB

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