clk-pllv3.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2012 Freescale Semiconductor, Inc.
  4. * Copyright 2012 Linaro Ltd.
  5. */
  6. #include <linux/clk-provider.h>
  7. #include <linux/delay.h>
  8. #include <linux/export.h>
  9. #include <linux/io.h>
  10. #include <linux/iopoll.h>
  11. #include <linux/slab.h>
  12. #include <linux/jiffies.h>
  13. #include <linux/err.h>
  14. #include "clk.h"
  15. #define PLL_NUM_OFFSET 0x10
  16. #define PLL_DENOM_OFFSET 0x20
  17. #define PLL_IMX7_NUM_OFFSET 0x20
  18. #define PLL_IMX7_DENOM_OFFSET 0x30
  19. #define PLL_VF610_NUM_OFFSET 0x20
  20. #define PLL_VF610_DENOM_OFFSET 0x30
  21. #define BM_PLL_POWER (0x1 << 12)
  22. #define BM_PLL_LOCK (0x1 << 31)
  23. #define IMX7_ENET_PLL_POWER (0x1 << 5)
  24. #define IMX7_DDR_PLL_POWER (0x1 << 20)
  25. #define PLL_LOCK_TIMEOUT 10000
  26. /**
  27. * struct clk_pllv3 - IMX PLL clock version 3
  28. * @hw: clock source
  29. * @base: base address of PLL registers
  30. * @power_bit: pll power bit mask
  31. * @powerup_set: set power_bit to power up the PLL
  32. * @div_mask: mask of divider bits
  33. * @div_shift: shift of divider bits
  34. * @ref_clock: reference clock rate
  35. * @num_offset: num register offset
  36. * @denom_offset: denom register offset
  37. *
  38. * IMX PLL clock version 3, found on i.MX6 series. Divider for pllv3
  39. * is actually a multiplier, and always sits at bit 0.
  40. */
  41. struct clk_pllv3 {
  42. struct clk_hw hw;
  43. void __iomem *base;
  44. u32 power_bit;
  45. bool powerup_set;
  46. u32 div_mask;
  47. u32 div_shift;
  48. unsigned long ref_clock;
  49. u32 num_offset;
  50. u32 denom_offset;
  51. };
  52. #define to_clk_pllv3(_hw) container_of(_hw, struct clk_pllv3, hw)
  53. static int clk_pllv3_wait_lock(struct clk_pllv3 *pll)
  54. {
  55. u32 val = readl_relaxed(pll->base) & pll->power_bit;
  56. /* No need to wait for lock when pll is not powered up */
  57. if ((pll->powerup_set && !val) || (!pll->powerup_set && val))
  58. return 0;
  59. return readl_relaxed_poll_timeout(pll->base, val, val & BM_PLL_LOCK,
  60. 500, PLL_LOCK_TIMEOUT);
  61. }
  62. static int clk_pllv3_prepare(struct clk_hw *hw)
  63. {
  64. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  65. u32 val;
  66. val = readl_relaxed(pll->base);
  67. if (pll->powerup_set)
  68. val |= pll->power_bit;
  69. else
  70. val &= ~pll->power_bit;
  71. writel_relaxed(val, pll->base);
  72. return clk_pllv3_wait_lock(pll);
  73. }
  74. static void clk_pllv3_unprepare(struct clk_hw *hw)
  75. {
  76. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  77. u32 val;
  78. val = readl_relaxed(pll->base);
  79. if (pll->powerup_set)
  80. val &= ~pll->power_bit;
  81. else
  82. val |= pll->power_bit;
  83. writel_relaxed(val, pll->base);
  84. }
  85. static int clk_pllv3_is_prepared(struct clk_hw *hw)
  86. {
  87. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  88. if (readl_relaxed(pll->base) & BM_PLL_LOCK)
  89. return 1;
  90. return 0;
  91. }
  92. static unsigned long clk_pllv3_recalc_rate(struct clk_hw *hw,
  93. unsigned long parent_rate)
  94. {
  95. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  96. u32 div = (readl_relaxed(pll->base) >> pll->div_shift) & pll->div_mask;
  97. return (div == 1) ? parent_rate * 22 : parent_rate * 20;
  98. }
  99. static int clk_pllv3_determine_rate(struct clk_hw *hw,
  100. struct clk_rate_request *req)
  101. {
  102. unsigned long parent_rate = req->best_parent_rate;
  103. req->rate = (req->rate >= parent_rate * 22) ? parent_rate * 22 : parent_rate * 20;
  104. return 0;
  105. }
  106. static int clk_pllv3_set_rate(struct clk_hw *hw, unsigned long rate,
  107. unsigned long parent_rate)
  108. {
  109. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  110. u32 val, div;
  111. if (rate == parent_rate * 22)
  112. div = 1;
  113. else if (rate == parent_rate * 20)
  114. div = 0;
  115. else
  116. return -EINVAL;
  117. val = readl_relaxed(pll->base);
  118. val &= ~(pll->div_mask << pll->div_shift);
  119. val |= (div << pll->div_shift);
  120. writel_relaxed(val, pll->base);
  121. return clk_pllv3_wait_lock(pll);
  122. }
  123. static const struct clk_ops clk_pllv3_ops = {
  124. .prepare = clk_pllv3_prepare,
  125. .unprepare = clk_pllv3_unprepare,
  126. .is_prepared = clk_pllv3_is_prepared,
  127. .recalc_rate = clk_pllv3_recalc_rate,
  128. .determine_rate = clk_pllv3_determine_rate,
  129. .set_rate = clk_pllv3_set_rate,
  130. };
  131. static unsigned long clk_pllv3_sys_recalc_rate(struct clk_hw *hw,
  132. unsigned long parent_rate)
  133. {
  134. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  135. u32 div = readl_relaxed(pll->base) & pll->div_mask;
  136. return parent_rate * div / 2;
  137. }
  138. static int clk_pllv3_sys_determine_rate(struct clk_hw *hw,
  139. struct clk_rate_request *req)
  140. {
  141. unsigned long parent_rate = req->best_parent_rate;
  142. unsigned long min_rate = parent_rate * 54 / 2;
  143. unsigned long max_rate = parent_rate * 108 / 2;
  144. u32 div;
  145. if (req->rate > max_rate)
  146. req->rate = max_rate;
  147. else if (req->rate < min_rate)
  148. req->rate = min_rate;
  149. div = req->rate * 2 / parent_rate;
  150. req->rate = parent_rate * div / 2;
  151. return 0;
  152. }
  153. static int clk_pllv3_sys_set_rate(struct clk_hw *hw, unsigned long rate,
  154. unsigned long parent_rate)
  155. {
  156. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  157. unsigned long min_rate = parent_rate * 54 / 2;
  158. unsigned long max_rate = parent_rate * 108 / 2;
  159. u32 val, div;
  160. if (rate < min_rate || rate > max_rate)
  161. return -EINVAL;
  162. div = rate * 2 / parent_rate;
  163. val = readl_relaxed(pll->base);
  164. val &= ~pll->div_mask;
  165. val |= div;
  166. writel_relaxed(val, pll->base);
  167. return clk_pllv3_wait_lock(pll);
  168. }
  169. static const struct clk_ops clk_pllv3_sys_ops = {
  170. .prepare = clk_pllv3_prepare,
  171. .unprepare = clk_pllv3_unprepare,
  172. .is_prepared = clk_pllv3_is_prepared,
  173. .recalc_rate = clk_pllv3_sys_recalc_rate,
  174. .determine_rate = clk_pllv3_sys_determine_rate,
  175. .set_rate = clk_pllv3_sys_set_rate,
  176. };
  177. static unsigned long clk_pllv3_av_recalc_rate(struct clk_hw *hw,
  178. unsigned long parent_rate)
  179. {
  180. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  181. u32 mfn = readl_relaxed(pll->base + pll->num_offset);
  182. u32 mfd = readl_relaxed(pll->base + pll->denom_offset);
  183. u32 div = readl_relaxed(pll->base) & pll->div_mask;
  184. u64 temp64 = (u64)parent_rate;
  185. temp64 *= mfn;
  186. do_div(temp64, mfd);
  187. return parent_rate * div + (unsigned long)temp64;
  188. }
  189. static int clk_pllv3_av_determine_rate(struct clk_hw *hw,
  190. struct clk_rate_request *req)
  191. {
  192. unsigned long parent_rate = req->best_parent_rate;
  193. unsigned long min_rate = parent_rate * 27;
  194. unsigned long max_rate = parent_rate * 54;
  195. u32 div;
  196. u32 mfn, mfd = 1000000;
  197. u32 max_mfd = 0x3FFFFFFF;
  198. u64 temp64;
  199. if (req->rate > max_rate)
  200. req->rate = max_rate;
  201. else if (req->rate < min_rate)
  202. req->rate = min_rate;
  203. if (parent_rate <= max_mfd)
  204. mfd = parent_rate;
  205. div = req->rate / parent_rate;
  206. temp64 = (u64) (req->rate - div * parent_rate);
  207. temp64 *= mfd;
  208. temp64 = div64_ul(temp64, parent_rate);
  209. mfn = temp64;
  210. temp64 = (u64)parent_rate;
  211. temp64 *= mfn;
  212. do_div(temp64, mfd);
  213. req->rate = parent_rate * div + (unsigned long)temp64;
  214. return 0;
  215. }
  216. static int clk_pllv3_av_set_rate(struct clk_hw *hw, unsigned long rate,
  217. unsigned long parent_rate)
  218. {
  219. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  220. unsigned long min_rate = parent_rate * 27;
  221. unsigned long max_rate = parent_rate * 54;
  222. u32 val, div;
  223. u32 mfn, mfd = 1000000;
  224. u32 max_mfd = 0x3FFFFFFF;
  225. u64 temp64;
  226. if (rate < min_rate || rate > max_rate)
  227. return -EINVAL;
  228. if (parent_rate <= max_mfd)
  229. mfd = parent_rate;
  230. div = rate / parent_rate;
  231. temp64 = (u64) (rate - div * parent_rate);
  232. temp64 *= mfd;
  233. temp64 = div64_ul(temp64, parent_rate);
  234. mfn = temp64;
  235. val = readl_relaxed(pll->base);
  236. val &= ~pll->div_mask;
  237. val |= div;
  238. writel_relaxed(val, pll->base);
  239. writel_relaxed(mfn, pll->base + pll->num_offset);
  240. writel_relaxed(mfd, pll->base + pll->denom_offset);
  241. return clk_pllv3_wait_lock(pll);
  242. }
  243. static const struct clk_ops clk_pllv3_av_ops = {
  244. .prepare = clk_pllv3_prepare,
  245. .unprepare = clk_pllv3_unprepare,
  246. .is_prepared = clk_pllv3_is_prepared,
  247. .recalc_rate = clk_pllv3_av_recalc_rate,
  248. .determine_rate = clk_pllv3_av_determine_rate,
  249. .set_rate = clk_pllv3_av_set_rate,
  250. };
  251. struct clk_pllv3_vf610_mf {
  252. u32 mfi; /* integer part, can be 20 or 22 */
  253. u32 mfn; /* numerator, 30-bit value */
  254. u32 mfd; /* denominator, 30-bit value, must be less than mfn */
  255. };
  256. static unsigned long clk_pllv3_vf610_mf_to_rate(unsigned long parent_rate,
  257. struct clk_pllv3_vf610_mf mf)
  258. {
  259. u64 temp64;
  260. temp64 = parent_rate;
  261. temp64 *= mf.mfn;
  262. do_div(temp64, mf.mfd);
  263. return (parent_rate * mf.mfi) + temp64;
  264. }
  265. static struct clk_pllv3_vf610_mf clk_pllv3_vf610_rate_to_mf(
  266. unsigned long parent_rate, unsigned long rate)
  267. {
  268. struct clk_pllv3_vf610_mf mf;
  269. u64 temp64;
  270. mf.mfi = (rate >= 22 * parent_rate) ? 22 : 20;
  271. mf.mfd = 0x3fffffff; /* use max supported value for best accuracy */
  272. if (rate <= parent_rate * mf.mfi)
  273. mf.mfn = 0;
  274. else if (rate >= parent_rate * (mf.mfi + 1))
  275. mf.mfn = mf.mfd - 1;
  276. else {
  277. /* rate = parent_rate * (mfi + mfn/mfd) */
  278. temp64 = rate - parent_rate * mf.mfi;
  279. temp64 *= mf.mfd;
  280. temp64 = div64_ul(temp64, parent_rate);
  281. mf.mfn = temp64;
  282. }
  283. return mf;
  284. }
  285. static unsigned long clk_pllv3_vf610_recalc_rate(struct clk_hw *hw,
  286. unsigned long parent_rate)
  287. {
  288. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  289. struct clk_pllv3_vf610_mf mf;
  290. mf.mfn = readl_relaxed(pll->base + pll->num_offset);
  291. mf.mfd = readl_relaxed(pll->base + pll->denom_offset);
  292. mf.mfi = (readl_relaxed(pll->base) & pll->div_mask) ? 22 : 20;
  293. return clk_pllv3_vf610_mf_to_rate(parent_rate, mf);
  294. }
  295. static int clk_pllv3_vf610_determine_rate(struct clk_hw *hw,
  296. struct clk_rate_request *req)
  297. {
  298. struct clk_pllv3_vf610_mf mf = clk_pllv3_vf610_rate_to_mf(req->best_parent_rate,
  299. req->rate);
  300. req->rate = clk_pllv3_vf610_mf_to_rate(req->best_parent_rate, mf);
  301. return 0;
  302. }
  303. static int clk_pllv3_vf610_set_rate(struct clk_hw *hw, unsigned long rate,
  304. unsigned long parent_rate)
  305. {
  306. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  307. struct clk_pllv3_vf610_mf mf =
  308. clk_pllv3_vf610_rate_to_mf(parent_rate, rate);
  309. u32 val;
  310. val = readl_relaxed(pll->base);
  311. if (mf.mfi == 20)
  312. val &= ~pll->div_mask; /* clear bit for mfi=20 */
  313. else
  314. val |= pll->div_mask; /* set bit for mfi=22 */
  315. writel_relaxed(val, pll->base);
  316. writel_relaxed(mf.mfn, pll->base + pll->num_offset);
  317. writel_relaxed(mf.mfd, pll->base + pll->denom_offset);
  318. return clk_pllv3_wait_lock(pll);
  319. }
  320. static const struct clk_ops clk_pllv3_vf610_ops = {
  321. .prepare = clk_pllv3_prepare,
  322. .unprepare = clk_pllv3_unprepare,
  323. .is_prepared = clk_pllv3_is_prepared,
  324. .recalc_rate = clk_pllv3_vf610_recalc_rate,
  325. .determine_rate = clk_pllv3_vf610_determine_rate,
  326. .set_rate = clk_pllv3_vf610_set_rate,
  327. };
  328. static unsigned long clk_pllv3_enet_recalc_rate(struct clk_hw *hw,
  329. unsigned long parent_rate)
  330. {
  331. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  332. return pll->ref_clock;
  333. }
  334. static const struct clk_ops clk_pllv3_enet_ops = {
  335. .prepare = clk_pllv3_prepare,
  336. .unprepare = clk_pllv3_unprepare,
  337. .is_prepared = clk_pllv3_is_prepared,
  338. .recalc_rate = clk_pllv3_enet_recalc_rate,
  339. };
  340. struct clk_hw *imx_clk_hw_pllv3(enum imx_pllv3_type type, const char *name,
  341. const char *parent_name, void __iomem *base,
  342. u32 div_mask)
  343. {
  344. struct clk_pllv3 *pll;
  345. const struct clk_ops *ops;
  346. struct clk_hw *hw;
  347. struct clk_init_data init;
  348. int ret;
  349. pll = kzalloc_obj(*pll);
  350. if (!pll)
  351. return ERR_PTR(-ENOMEM);
  352. pll->power_bit = BM_PLL_POWER;
  353. pll->num_offset = PLL_NUM_OFFSET;
  354. pll->denom_offset = PLL_DENOM_OFFSET;
  355. switch (type) {
  356. case IMX_PLLV3_SYS:
  357. ops = &clk_pllv3_sys_ops;
  358. break;
  359. case IMX_PLLV3_SYS_VF610:
  360. ops = &clk_pllv3_vf610_ops;
  361. pll->num_offset = PLL_VF610_NUM_OFFSET;
  362. pll->denom_offset = PLL_VF610_DENOM_OFFSET;
  363. break;
  364. case IMX_PLLV3_USB_VF610:
  365. pll->div_shift = 1;
  366. fallthrough;
  367. case IMX_PLLV3_USB:
  368. ops = &clk_pllv3_ops;
  369. pll->powerup_set = true;
  370. break;
  371. case IMX_PLLV3_AV_IMX7:
  372. pll->num_offset = PLL_IMX7_NUM_OFFSET;
  373. pll->denom_offset = PLL_IMX7_DENOM_OFFSET;
  374. fallthrough;
  375. case IMX_PLLV3_AV:
  376. ops = &clk_pllv3_av_ops;
  377. break;
  378. case IMX_PLLV3_ENET_IMX7:
  379. pll->power_bit = IMX7_ENET_PLL_POWER;
  380. pll->ref_clock = 1000000000;
  381. ops = &clk_pllv3_enet_ops;
  382. break;
  383. case IMX_PLLV3_ENET:
  384. pll->ref_clock = 500000000;
  385. ops = &clk_pllv3_enet_ops;
  386. break;
  387. case IMX_PLLV3_DDR_IMX7:
  388. pll->power_bit = IMX7_DDR_PLL_POWER;
  389. pll->num_offset = PLL_IMX7_NUM_OFFSET;
  390. pll->denom_offset = PLL_IMX7_DENOM_OFFSET;
  391. ops = &clk_pllv3_av_ops;
  392. break;
  393. default:
  394. ops = &clk_pllv3_ops;
  395. }
  396. pll->base = base;
  397. pll->div_mask = div_mask;
  398. init.name = name;
  399. init.ops = ops;
  400. init.flags = 0;
  401. init.parent_names = &parent_name;
  402. init.num_parents = 1;
  403. pll->hw.init = &init;
  404. hw = &pll->hw;
  405. ret = clk_hw_register(NULL, hw);
  406. if (ret) {
  407. kfree(pll);
  408. return ERR_PTR(ret);
  409. }
  410. return hw;
  411. }
  412. EXPORT_SYMBOL_GPL(imx_clk_hw_pllv3);