clk-pll.c 34 KB

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