clk-pllv3.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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.h>
  13. #include <linux/clk-provider.h>
  14. #include <linux/delay.h>
  15. #include <linux/io.h>
  16. #include <linux/slab.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/err.h>
  19. #include "clk.h"
  20. #define PLL_NUM_OFFSET 0x10
  21. #define PLL_DENOM_OFFSET 0x20
  22. #define BM_PLL_POWER (0x1 << 12)
  23. #define BM_PLL_LOCK (0x1 << 31)
  24. /**
  25. * struct clk_pllv3 - IMX PLL clock version 3
  26. * @clk_hw: clock source
  27. * @base: base address of PLL registers
  28. * @powerup_set: set POWER bit to power up the PLL
  29. * @div_mask: mask of divider bits
  30. *
  31. * IMX PLL clock version 3, found on i.MX6 series. Divider for pllv3
  32. * is actually a multiplier, and always sits at bit 0.
  33. */
  34. struct clk_pllv3 {
  35. struct clk_hw hw;
  36. void __iomem *base;
  37. bool powerup_set;
  38. u32 div_mask;
  39. };
  40. #define to_clk_pllv3(_hw) container_of(_hw, struct clk_pllv3, hw)
  41. static int clk_pllv3_wait_lock(struct clk_pllv3 *pll)
  42. {
  43. unsigned long timeout = jiffies + msecs_to_jiffies(10);
  44. u32 val = readl_relaxed(pll->base) & BM_PLL_POWER;
  45. /* No need to wait for lock when pll is not powered up */
  46. if ((pll->powerup_set && !val) || (!pll->powerup_set && val))
  47. return 0;
  48. /* Wait for PLL to lock */
  49. do {
  50. if (readl_relaxed(pll->base) & BM_PLL_LOCK)
  51. break;
  52. if (time_after(jiffies, timeout))
  53. break;
  54. usleep_range(50, 500);
  55. } while (1);
  56. return readl_relaxed(pll->base) & BM_PLL_LOCK ? 0 : -ETIMEDOUT;
  57. }
  58. static int clk_pllv3_prepare(struct clk_hw *hw)
  59. {
  60. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  61. u32 val;
  62. val = readl_relaxed(pll->base);
  63. if (pll->powerup_set)
  64. val |= BM_PLL_POWER;
  65. else
  66. val &= ~BM_PLL_POWER;
  67. writel_relaxed(val, pll->base);
  68. return clk_pllv3_wait_lock(pll);
  69. }
  70. static void clk_pllv3_unprepare(struct clk_hw *hw)
  71. {
  72. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  73. u32 val;
  74. val = readl_relaxed(pll->base);
  75. if (pll->powerup_set)
  76. val &= ~BM_PLL_POWER;
  77. else
  78. val |= BM_PLL_POWER;
  79. writel_relaxed(val, pll->base);
  80. }
  81. static unsigned long clk_pllv3_recalc_rate(struct clk_hw *hw,
  82. unsigned long parent_rate)
  83. {
  84. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  85. u32 div = readl_relaxed(pll->base) & pll->div_mask;
  86. return (div == 1) ? parent_rate * 22 : parent_rate * 20;
  87. }
  88. static long clk_pllv3_round_rate(struct clk_hw *hw, unsigned long rate,
  89. unsigned long *prate)
  90. {
  91. unsigned long parent_rate = *prate;
  92. return (rate >= parent_rate * 22) ? parent_rate * 22 :
  93. parent_rate * 20;
  94. }
  95. static int clk_pllv3_set_rate(struct clk_hw *hw, unsigned long rate,
  96. unsigned long parent_rate)
  97. {
  98. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  99. u32 val, div;
  100. if (rate == parent_rate * 22)
  101. div = 1;
  102. else if (rate == parent_rate * 20)
  103. div = 0;
  104. else
  105. return -EINVAL;
  106. val = readl_relaxed(pll->base);
  107. val &= ~pll->div_mask;
  108. val |= div;
  109. writel_relaxed(val, pll->base);
  110. return clk_pllv3_wait_lock(pll);
  111. }
  112. static const struct clk_ops clk_pllv3_ops = {
  113. .prepare = clk_pllv3_prepare,
  114. .unprepare = clk_pllv3_unprepare,
  115. .recalc_rate = clk_pllv3_recalc_rate,
  116. .round_rate = clk_pllv3_round_rate,
  117. .set_rate = clk_pllv3_set_rate,
  118. };
  119. static unsigned long clk_pllv3_sys_recalc_rate(struct clk_hw *hw,
  120. unsigned long parent_rate)
  121. {
  122. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  123. u32 div = readl_relaxed(pll->base) & pll->div_mask;
  124. return parent_rate * div / 2;
  125. }
  126. static long clk_pllv3_sys_round_rate(struct clk_hw *hw, unsigned long rate,
  127. unsigned long *prate)
  128. {
  129. unsigned long parent_rate = *prate;
  130. unsigned long min_rate = parent_rate * 54 / 2;
  131. unsigned long max_rate = parent_rate * 108 / 2;
  132. u32 div;
  133. if (rate > max_rate)
  134. rate = max_rate;
  135. else if (rate < min_rate)
  136. rate = min_rate;
  137. div = rate * 2 / parent_rate;
  138. return parent_rate * div / 2;
  139. }
  140. static int clk_pllv3_sys_set_rate(struct clk_hw *hw, unsigned long rate,
  141. unsigned long parent_rate)
  142. {
  143. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  144. unsigned long min_rate = parent_rate * 54 / 2;
  145. unsigned long max_rate = parent_rate * 108 / 2;
  146. u32 val, div;
  147. if (rate < min_rate || rate > max_rate)
  148. return -EINVAL;
  149. div = rate * 2 / parent_rate;
  150. val = readl_relaxed(pll->base);
  151. val &= ~pll->div_mask;
  152. val |= div;
  153. writel_relaxed(val, pll->base);
  154. return clk_pllv3_wait_lock(pll);
  155. }
  156. static const struct clk_ops clk_pllv3_sys_ops = {
  157. .prepare = clk_pllv3_prepare,
  158. .unprepare = clk_pllv3_unprepare,
  159. .recalc_rate = clk_pllv3_sys_recalc_rate,
  160. .round_rate = clk_pllv3_sys_round_rate,
  161. .set_rate = clk_pllv3_sys_set_rate,
  162. };
  163. static unsigned long clk_pllv3_av_recalc_rate(struct clk_hw *hw,
  164. unsigned long parent_rate)
  165. {
  166. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  167. u32 mfn = readl_relaxed(pll->base + PLL_NUM_OFFSET);
  168. u32 mfd = readl_relaxed(pll->base + PLL_DENOM_OFFSET);
  169. u32 div = readl_relaxed(pll->base) & pll->div_mask;
  170. return (parent_rate * div) + ((parent_rate / mfd) * mfn);
  171. }
  172. static long clk_pllv3_av_round_rate(struct clk_hw *hw, unsigned long rate,
  173. unsigned long *prate)
  174. {
  175. unsigned long parent_rate = *prate;
  176. unsigned long min_rate = parent_rate * 27;
  177. unsigned long max_rate = parent_rate * 54;
  178. u32 div;
  179. u32 mfn, mfd = 1000000;
  180. s64 temp64;
  181. if (rate > max_rate)
  182. rate = max_rate;
  183. else if (rate < min_rate)
  184. rate = min_rate;
  185. div = rate / parent_rate;
  186. temp64 = (u64) (rate - div * parent_rate);
  187. temp64 *= mfd;
  188. do_div(temp64, parent_rate);
  189. mfn = temp64;
  190. return parent_rate * div + parent_rate / mfd * mfn;
  191. }
  192. static int clk_pllv3_av_set_rate(struct clk_hw *hw, unsigned long rate,
  193. unsigned long parent_rate)
  194. {
  195. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  196. unsigned long min_rate = parent_rate * 27;
  197. unsigned long max_rate = parent_rate * 54;
  198. u32 val, div;
  199. u32 mfn, mfd = 1000000;
  200. s64 temp64;
  201. if (rate < min_rate || rate > max_rate)
  202. return -EINVAL;
  203. div = rate / parent_rate;
  204. temp64 = (u64) (rate - div * parent_rate);
  205. temp64 *= mfd;
  206. do_div(temp64, parent_rate);
  207. mfn = temp64;
  208. val = readl_relaxed(pll->base);
  209. val &= ~pll->div_mask;
  210. val |= div;
  211. writel_relaxed(val, pll->base);
  212. writel_relaxed(mfn, pll->base + PLL_NUM_OFFSET);
  213. writel_relaxed(mfd, pll->base + PLL_DENOM_OFFSET);
  214. return clk_pllv3_wait_lock(pll);
  215. }
  216. static const struct clk_ops clk_pllv3_av_ops = {
  217. .prepare = clk_pllv3_prepare,
  218. .unprepare = clk_pllv3_unprepare,
  219. .recalc_rate = clk_pllv3_av_recalc_rate,
  220. .round_rate = clk_pllv3_av_round_rate,
  221. .set_rate = clk_pllv3_av_set_rate,
  222. };
  223. static unsigned long clk_pllv3_enet_recalc_rate(struct clk_hw *hw,
  224. unsigned long parent_rate)
  225. {
  226. return 500000000;
  227. }
  228. static const struct clk_ops clk_pllv3_enet_ops = {
  229. .prepare = clk_pllv3_prepare,
  230. .unprepare = clk_pllv3_unprepare,
  231. .recalc_rate = clk_pllv3_enet_recalc_rate,
  232. };
  233. struct clk *imx_clk_pllv3(enum imx_pllv3_type type, const char *name,
  234. const char *parent_name, void __iomem *base,
  235. u32 div_mask)
  236. {
  237. struct clk_pllv3 *pll;
  238. const struct clk_ops *ops;
  239. struct clk *clk;
  240. struct clk_init_data init;
  241. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  242. if (!pll)
  243. return ERR_PTR(-ENOMEM);
  244. switch (type) {
  245. case IMX_PLLV3_SYS:
  246. ops = &clk_pllv3_sys_ops;
  247. break;
  248. case IMX_PLLV3_USB:
  249. ops = &clk_pllv3_ops;
  250. pll->powerup_set = true;
  251. break;
  252. case IMX_PLLV3_AV:
  253. ops = &clk_pllv3_av_ops;
  254. break;
  255. case IMX_PLLV3_ENET:
  256. ops = &clk_pllv3_enet_ops;
  257. break;
  258. default:
  259. ops = &clk_pllv3_ops;
  260. }
  261. pll->base = base;
  262. pll->div_mask = div_mask;
  263. init.name = name;
  264. init.ops = ops;
  265. init.flags = 0;
  266. init.parent_names = &parent_name;
  267. init.num_parents = 1;
  268. pll->hw.init = &init;
  269. clk = clk_register(NULL, &pll->hw);
  270. if (IS_ERR(clk))
  271. kfree(pll);
  272. return clk;
  273. }