clk-pllv3.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * Copyright 2012 Freescale Semiconductor, Inc.
  3. * Copyright 2012 Linaro Ltd.
  4. *
  5. * The code contained herein is licensed under the GNU General Public
  6. * License. You may obtain a copy of the GNU General Public License
  7. * Version 2 or later at the following locations:
  8. *
  9. * http://www.opensource.org/licenses/gpl-license.html
  10. * http://www.gnu.org/copyleft/gpl.html
  11. */
  12. #include <linux/clk-provider.h>
  13. #include <linux/delay.h>
  14. #include <linux/io.h>
  15. #include <linux/slab.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/err.h>
  18. #include "clk.h"
  19. #define PLL_NUM_OFFSET 0x10
  20. #define PLL_DENOM_OFFSET 0x20
  21. #define PLL_VF610_NUM_OFFSET 0x20
  22. #define PLL_VF610_DENOM_OFFSET 0x30
  23. #define BM_PLL_POWER (0x1 << 12)
  24. #define BM_PLL_LOCK (0x1 << 31)
  25. #define IMX7_ENET_PLL_POWER (0x1 << 5)
  26. #define IMX7_DDR_PLL_POWER (0x1 << 20)
  27. /**
  28. * struct clk_pllv3 - IMX PLL clock version 3
  29. * @clk_hw: clock source
  30. * @base: base address of PLL registers
  31. * @power_bit: pll power bit mask
  32. * @powerup_set: set power_bit to power up the PLL
  33. * @div_mask: mask of divider bits
  34. * @div_shift: shift of divider bits
  35. *
  36. * IMX PLL clock version 3, found on i.MX6 series. Divider for pllv3
  37. * is actually a multiplier, and always sits at bit 0.
  38. */
  39. struct clk_pllv3 {
  40. struct clk_hw hw;
  41. void __iomem *base;
  42. u32 power_bit;
  43. bool powerup_set;
  44. u32 div_mask;
  45. u32 div_shift;
  46. unsigned long ref_clock;
  47. };
  48. #define to_clk_pllv3(_hw) container_of(_hw, struct clk_pllv3, hw)
  49. static int clk_pllv3_wait_lock(struct clk_pllv3 *pll)
  50. {
  51. unsigned long timeout = jiffies + msecs_to_jiffies(10);
  52. u32 val = readl_relaxed(pll->base) & pll->power_bit;
  53. /* No need to wait for lock when pll is not powered up */
  54. if ((pll->powerup_set && !val) || (!pll->powerup_set && val))
  55. return 0;
  56. /* Wait for PLL to lock */
  57. do {
  58. if (readl_relaxed(pll->base) & BM_PLL_LOCK)
  59. break;
  60. if (time_after(jiffies, timeout))
  61. break;
  62. usleep_range(50, 500);
  63. } while (1);
  64. return readl_relaxed(pll->base) & BM_PLL_LOCK ? 0 : -ETIMEDOUT;
  65. }
  66. static int clk_pllv3_prepare(struct clk_hw *hw)
  67. {
  68. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  69. u32 val;
  70. val = readl_relaxed(pll->base);
  71. if (pll->powerup_set)
  72. val |= pll->power_bit;
  73. else
  74. val &= ~pll->power_bit;
  75. writel_relaxed(val, pll->base);
  76. return clk_pllv3_wait_lock(pll);
  77. }
  78. static void clk_pllv3_unprepare(struct clk_hw *hw)
  79. {
  80. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  81. u32 val;
  82. val = readl_relaxed(pll->base);
  83. if (pll->powerup_set)
  84. val &= ~pll->power_bit;
  85. else
  86. val |= pll->power_bit;
  87. writel_relaxed(val, pll->base);
  88. }
  89. static int clk_pllv3_is_prepared(struct clk_hw *hw)
  90. {
  91. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  92. if (readl_relaxed(pll->base) & BM_PLL_LOCK)
  93. return 1;
  94. return 0;
  95. }
  96. static unsigned long clk_pllv3_recalc_rate(struct clk_hw *hw,
  97. unsigned long parent_rate)
  98. {
  99. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  100. u32 div = (readl_relaxed(pll->base) >> pll->div_shift) & pll->div_mask;
  101. return (div == 1) ? parent_rate * 22 : parent_rate * 20;
  102. }
  103. static long clk_pllv3_round_rate(struct clk_hw *hw, unsigned long rate,
  104. unsigned long *prate)
  105. {
  106. unsigned long parent_rate = *prate;
  107. return (rate >= parent_rate * 22) ? parent_rate * 22 :
  108. parent_rate * 20;
  109. }
  110. static int clk_pllv3_set_rate(struct clk_hw *hw, unsigned long rate,
  111. unsigned long parent_rate)
  112. {
  113. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  114. u32 val, div;
  115. if (rate == parent_rate * 22)
  116. div = 1;
  117. else if (rate == parent_rate * 20)
  118. div = 0;
  119. else
  120. return -EINVAL;
  121. val = readl_relaxed(pll->base);
  122. val &= ~(pll->div_mask << pll->div_shift);
  123. val |= (div << pll->div_shift);
  124. writel_relaxed(val, pll->base);
  125. return clk_pllv3_wait_lock(pll);
  126. }
  127. static const struct clk_ops clk_pllv3_ops = {
  128. .prepare = clk_pllv3_prepare,
  129. .unprepare = clk_pllv3_unprepare,
  130. .is_prepared = clk_pllv3_is_prepared,
  131. .recalc_rate = clk_pllv3_recalc_rate,
  132. .round_rate = clk_pllv3_round_rate,
  133. .set_rate = clk_pllv3_set_rate,
  134. };
  135. static unsigned long clk_pllv3_sys_recalc_rate(struct clk_hw *hw,
  136. unsigned long parent_rate)
  137. {
  138. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  139. u32 div = readl_relaxed(pll->base) & pll->div_mask;
  140. return parent_rate * div / 2;
  141. }
  142. static long clk_pllv3_sys_round_rate(struct clk_hw *hw, unsigned long rate,
  143. unsigned long *prate)
  144. {
  145. unsigned long parent_rate = *prate;
  146. unsigned long min_rate = parent_rate * 54 / 2;
  147. unsigned long max_rate = parent_rate * 108 / 2;
  148. u32 div;
  149. if (rate > max_rate)
  150. rate = max_rate;
  151. else if (rate < min_rate)
  152. rate = min_rate;
  153. div = rate * 2 / parent_rate;
  154. return parent_rate * div / 2;
  155. }
  156. static int clk_pllv3_sys_set_rate(struct clk_hw *hw, unsigned long rate,
  157. unsigned long parent_rate)
  158. {
  159. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  160. unsigned long min_rate = parent_rate * 54 / 2;
  161. unsigned long max_rate = parent_rate * 108 / 2;
  162. u32 val, div;
  163. if (rate < min_rate || rate > max_rate)
  164. return -EINVAL;
  165. div = rate * 2 / parent_rate;
  166. val = readl_relaxed(pll->base);
  167. val &= ~pll->div_mask;
  168. val |= div;
  169. writel_relaxed(val, pll->base);
  170. return clk_pllv3_wait_lock(pll);
  171. }
  172. static const struct clk_ops clk_pllv3_sys_ops = {
  173. .prepare = clk_pllv3_prepare,
  174. .unprepare = clk_pllv3_unprepare,
  175. .is_prepared = clk_pllv3_is_prepared,
  176. .recalc_rate = clk_pllv3_sys_recalc_rate,
  177. .round_rate = clk_pllv3_sys_round_rate,
  178. .set_rate = clk_pllv3_sys_set_rate,
  179. };
  180. static unsigned long clk_pllv3_av_recalc_rate(struct clk_hw *hw,
  181. unsigned long parent_rate)
  182. {
  183. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  184. u32 mfn = readl_relaxed(pll->base + PLL_NUM_OFFSET);
  185. u32 mfd = readl_relaxed(pll->base + PLL_DENOM_OFFSET);
  186. u32 div = readl_relaxed(pll->base) & pll->div_mask;
  187. u64 temp64 = (u64)parent_rate;
  188. temp64 *= mfn;
  189. do_div(temp64, mfd);
  190. return parent_rate * div + (unsigned long)temp64;
  191. }
  192. static long clk_pllv3_av_round_rate(struct clk_hw *hw, unsigned long rate,
  193. unsigned long *prate)
  194. {
  195. unsigned long parent_rate = *prate;
  196. unsigned long min_rate = parent_rate * 27;
  197. unsigned long max_rate = parent_rate * 54;
  198. u32 div;
  199. u32 mfn, mfd = 1000000;
  200. u32 max_mfd = 0x3FFFFFFF;
  201. u64 temp64;
  202. if (rate > max_rate)
  203. rate = max_rate;
  204. else if (rate < min_rate)
  205. rate = min_rate;
  206. if (parent_rate <= max_mfd)
  207. mfd = parent_rate;
  208. div = rate / parent_rate;
  209. temp64 = (u64) (rate - div * parent_rate);
  210. temp64 *= mfd;
  211. do_div(temp64, parent_rate);
  212. mfn = temp64;
  213. temp64 = (u64)parent_rate;
  214. temp64 *= mfn;
  215. do_div(temp64, mfd);
  216. return parent_rate * div + (unsigned long)temp64;
  217. }
  218. static int clk_pllv3_av_set_rate(struct clk_hw *hw, unsigned long rate,
  219. unsigned long parent_rate)
  220. {
  221. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  222. unsigned long min_rate = parent_rate * 27;
  223. unsigned long max_rate = parent_rate * 54;
  224. u32 val, div;
  225. u32 mfn, mfd = 1000000;
  226. u32 max_mfd = 0x3FFFFFFF;
  227. u64 temp64;
  228. if (rate < min_rate || rate > max_rate)
  229. return -EINVAL;
  230. if (parent_rate <= max_mfd)
  231. mfd = parent_rate;
  232. div = rate / parent_rate;
  233. temp64 = (u64) (rate - div * parent_rate);
  234. temp64 *= mfd;
  235. do_div(temp64, parent_rate);
  236. mfn = temp64;
  237. val = readl_relaxed(pll->base);
  238. val &= ~pll->div_mask;
  239. val |= div;
  240. writel_relaxed(val, pll->base);
  241. writel_relaxed(mfn, pll->base + PLL_NUM_OFFSET);
  242. writel_relaxed(mfd, pll->base + PLL_DENOM_OFFSET);
  243. return clk_pllv3_wait_lock(pll);
  244. }
  245. static const struct clk_ops clk_pllv3_av_ops = {
  246. .prepare = clk_pllv3_prepare,
  247. .unprepare = clk_pllv3_unprepare,
  248. .is_prepared = clk_pllv3_is_prepared,
  249. .recalc_rate = clk_pllv3_av_recalc_rate,
  250. .round_rate = clk_pllv3_av_round_rate,
  251. .set_rate = clk_pllv3_av_set_rate,
  252. };
  253. struct clk_pllv3_vf610_mf {
  254. u32 mfi; /* integer part, can be 20 or 22 */
  255. u32 mfn; /* numerator, 30-bit value */
  256. u32 mfd; /* denominator, 30-bit value, must be less than mfn */
  257. };
  258. static unsigned long clk_pllv3_vf610_mf_to_rate(unsigned long parent_rate,
  259. struct clk_pllv3_vf610_mf mf)
  260. {
  261. u64 temp64;
  262. temp64 = parent_rate;
  263. temp64 *= mf.mfn;
  264. do_div(temp64, mf.mfd);
  265. return (parent_rate * mf.mfi) + temp64;
  266. }
  267. static struct clk_pllv3_vf610_mf clk_pllv3_vf610_rate_to_mf(
  268. unsigned long parent_rate, unsigned long rate)
  269. {
  270. struct clk_pllv3_vf610_mf mf;
  271. u64 temp64;
  272. mf.mfi = (rate >= 22 * parent_rate) ? 22 : 20;
  273. mf.mfd = 0x3fffffff; /* use max supported value for best accuracy */
  274. if (rate <= parent_rate * mf.mfi)
  275. mf.mfn = 0;
  276. else if (rate >= parent_rate * (mf.mfi + 1))
  277. mf.mfn = mf.mfd - 1;
  278. else {
  279. /* rate = parent_rate * (mfi + mfn/mfd) */
  280. temp64 = rate - parent_rate * mf.mfi;
  281. temp64 *= mf.mfd;
  282. do_div(temp64, parent_rate);
  283. mf.mfn = temp64;
  284. }
  285. return mf;
  286. }
  287. static unsigned long clk_pllv3_vf610_recalc_rate(struct clk_hw *hw,
  288. unsigned long parent_rate)
  289. {
  290. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  291. struct clk_pllv3_vf610_mf mf;
  292. mf.mfn = readl_relaxed(pll->base + PLL_VF610_NUM_OFFSET);
  293. mf.mfd = readl_relaxed(pll->base + PLL_VF610_DENOM_OFFSET);
  294. mf.mfi = (readl_relaxed(pll->base) & pll->div_mask) ? 22 : 20;
  295. return clk_pllv3_vf610_mf_to_rate(parent_rate, mf);
  296. }
  297. static long clk_pllv3_vf610_round_rate(struct clk_hw *hw, unsigned long rate,
  298. unsigned long *prate)
  299. {
  300. struct clk_pllv3_vf610_mf mf = clk_pllv3_vf610_rate_to_mf(*prate, rate);
  301. return clk_pllv3_vf610_mf_to_rate(*prate, mf);
  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_VF610_NUM_OFFSET);
  317. writel_relaxed(mf.mfd, pll->base + PLL_VF610_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. .round_rate = clk_pllv3_vf610_round_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 *imx_clk_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 *clk;
  347. struct clk_init_data init;
  348. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  349. if (!pll)
  350. return ERR_PTR(-ENOMEM);
  351. pll->power_bit = BM_PLL_POWER;
  352. switch (type) {
  353. case IMX_PLLV3_SYS:
  354. ops = &clk_pllv3_sys_ops;
  355. break;
  356. case IMX_PLLV3_SYS_VF610:
  357. ops = &clk_pllv3_vf610_ops;
  358. break;
  359. case IMX_PLLV3_USB_VF610:
  360. pll->div_shift = 1;
  361. case IMX_PLLV3_USB:
  362. ops = &clk_pllv3_ops;
  363. pll->powerup_set = true;
  364. break;
  365. case IMX_PLLV3_AV:
  366. ops = &clk_pllv3_av_ops;
  367. break;
  368. case IMX_PLLV3_ENET_IMX7:
  369. pll->power_bit = IMX7_ENET_PLL_POWER;
  370. pll->ref_clock = 1000000000;
  371. ops = &clk_pllv3_enet_ops;
  372. break;
  373. case IMX_PLLV3_ENET:
  374. pll->ref_clock = 500000000;
  375. ops = &clk_pllv3_enet_ops;
  376. break;
  377. case IMX_PLLV3_DDR_IMX7:
  378. pll->power_bit = IMX7_DDR_PLL_POWER;
  379. ops = &clk_pllv3_av_ops;
  380. break;
  381. default:
  382. ops = &clk_pllv3_ops;
  383. }
  384. pll->base = base;
  385. pll->div_mask = div_mask;
  386. init.name = name;
  387. init.ops = ops;
  388. init.flags = 0;
  389. init.parent_names = &parent_name;
  390. init.num_parents = 1;
  391. pll->hw.init = &init;
  392. clk = clk_register(NULL, &pll->hw);
  393. if (IS_ERR(clk))
  394. kfree(pll);
  395. return clk;
  396. }