clk-pllv3.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. int ret;
  63. val = readl_relaxed(pll->base);
  64. if (pll->powerup_set)
  65. val |= BM_PLL_POWER;
  66. else
  67. val &= ~BM_PLL_POWER;
  68. writel_relaxed(val, pll->base);
  69. ret = clk_pllv3_wait_lock(pll);
  70. if (ret)
  71. return ret;
  72. return 0;
  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 &= ~BM_PLL_POWER;
  81. else
  82. val |= BM_PLL_POWER;
  83. writel_relaxed(val, pll->base);
  84. }
  85. static unsigned long clk_pllv3_recalc_rate(struct clk_hw *hw,
  86. unsigned long parent_rate)
  87. {
  88. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  89. u32 div = readl_relaxed(pll->base) & pll->div_mask;
  90. return (div == 1) ? parent_rate * 22 : parent_rate * 20;
  91. }
  92. static long clk_pllv3_round_rate(struct clk_hw *hw, unsigned long rate,
  93. unsigned long *prate)
  94. {
  95. unsigned long parent_rate = *prate;
  96. return (rate >= parent_rate * 22) ? parent_rate * 22 :
  97. parent_rate * 20;
  98. }
  99. static int clk_pllv3_set_rate(struct clk_hw *hw, unsigned long rate,
  100. unsigned long parent_rate)
  101. {
  102. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  103. u32 val, div;
  104. if (rate == parent_rate * 22)
  105. div = 1;
  106. else if (rate == parent_rate * 20)
  107. div = 0;
  108. else
  109. return -EINVAL;
  110. val = readl_relaxed(pll->base);
  111. val &= ~pll->div_mask;
  112. val |= div;
  113. writel_relaxed(val, pll->base);
  114. return clk_pllv3_wait_lock(pll);
  115. }
  116. static const struct clk_ops clk_pllv3_ops = {
  117. .prepare = clk_pllv3_prepare,
  118. .unprepare = clk_pllv3_unprepare,
  119. .recalc_rate = clk_pllv3_recalc_rate,
  120. .round_rate = clk_pllv3_round_rate,
  121. .set_rate = clk_pllv3_set_rate,
  122. };
  123. static unsigned long clk_pllv3_sys_recalc_rate(struct clk_hw *hw,
  124. unsigned long parent_rate)
  125. {
  126. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  127. u32 div = readl_relaxed(pll->base) & pll->div_mask;
  128. return parent_rate * div / 2;
  129. }
  130. static long clk_pllv3_sys_round_rate(struct clk_hw *hw, unsigned long rate,
  131. unsigned long *prate)
  132. {
  133. unsigned long parent_rate = *prate;
  134. unsigned long min_rate = parent_rate * 54 / 2;
  135. unsigned long max_rate = parent_rate * 108 / 2;
  136. u32 div;
  137. if (rate > max_rate)
  138. rate = max_rate;
  139. else if (rate < min_rate)
  140. rate = min_rate;
  141. div = rate * 2 / parent_rate;
  142. return parent_rate * div / 2;
  143. }
  144. static int clk_pllv3_sys_set_rate(struct clk_hw *hw, unsigned long rate,
  145. unsigned long parent_rate)
  146. {
  147. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  148. unsigned long min_rate = parent_rate * 54 / 2;
  149. unsigned long max_rate = parent_rate * 108 / 2;
  150. u32 val, div;
  151. if (rate < min_rate || rate > max_rate)
  152. return -EINVAL;
  153. div = rate * 2 / parent_rate;
  154. val = readl_relaxed(pll->base);
  155. val &= ~pll->div_mask;
  156. val |= div;
  157. writel_relaxed(val, pll->base);
  158. return clk_pllv3_wait_lock(pll);
  159. }
  160. static const struct clk_ops clk_pllv3_sys_ops = {
  161. .prepare = clk_pllv3_prepare,
  162. .unprepare = clk_pllv3_unprepare,
  163. .recalc_rate = clk_pllv3_sys_recalc_rate,
  164. .round_rate = clk_pllv3_sys_round_rate,
  165. .set_rate = clk_pllv3_sys_set_rate,
  166. };
  167. static unsigned long clk_pllv3_av_recalc_rate(struct clk_hw *hw,
  168. unsigned long parent_rate)
  169. {
  170. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  171. u32 mfn = readl_relaxed(pll->base + PLL_NUM_OFFSET);
  172. u32 mfd = readl_relaxed(pll->base + PLL_DENOM_OFFSET);
  173. u32 div = readl_relaxed(pll->base) & pll->div_mask;
  174. return (parent_rate * div) + ((parent_rate / mfd) * mfn);
  175. }
  176. static long clk_pllv3_av_round_rate(struct clk_hw *hw, unsigned long rate,
  177. unsigned long *prate)
  178. {
  179. unsigned long parent_rate = *prate;
  180. unsigned long min_rate = parent_rate * 27;
  181. unsigned long max_rate = parent_rate * 54;
  182. u32 div;
  183. u32 mfn, mfd = 1000000;
  184. s64 temp64;
  185. if (rate > max_rate)
  186. rate = max_rate;
  187. else if (rate < min_rate)
  188. rate = min_rate;
  189. div = rate / parent_rate;
  190. temp64 = (u64) (rate - div * parent_rate);
  191. temp64 *= mfd;
  192. do_div(temp64, parent_rate);
  193. mfn = temp64;
  194. return parent_rate * div + parent_rate / mfd * mfn;
  195. }
  196. static int clk_pllv3_av_set_rate(struct clk_hw *hw, unsigned long rate,
  197. unsigned long parent_rate)
  198. {
  199. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  200. unsigned long min_rate = parent_rate * 27;
  201. unsigned long max_rate = parent_rate * 54;
  202. u32 val, div;
  203. u32 mfn, mfd = 1000000;
  204. s64 temp64;
  205. if (rate < min_rate || rate > max_rate)
  206. return -EINVAL;
  207. div = rate / parent_rate;
  208. temp64 = (u64) (rate - div * parent_rate);
  209. temp64 *= mfd;
  210. do_div(temp64, parent_rate);
  211. mfn = temp64;
  212. val = readl_relaxed(pll->base);
  213. val &= ~pll->div_mask;
  214. val |= div;
  215. writel_relaxed(val, pll->base);
  216. writel_relaxed(mfn, pll->base + PLL_NUM_OFFSET);
  217. writel_relaxed(mfd, pll->base + PLL_DENOM_OFFSET);
  218. return clk_pllv3_wait_lock(pll);
  219. }
  220. static const struct clk_ops clk_pllv3_av_ops = {
  221. .prepare = clk_pllv3_prepare,
  222. .unprepare = clk_pllv3_unprepare,
  223. .recalc_rate = clk_pllv3_av_recalc_rate,
  224. .round_rate = clk_pllv3_av_round_rate,
  225. .set_rate = clk_pllv3_av_set_rate,
  226. };
  227. static unsigned long clk_pllv3_enet_recalc_rate(struct clk_hw *hw,
  228. unsigned long parent_rate)
  229. {
  230. return 500000000;
  231. }
  232. static const struct clk_ops clk_pllv3_enet_ops = {
  233. .prepare = clk_pllv3_prepare,
  234. .unprepare = clk_pllv3_unprepare,
  235. .recalc_rate = clk_pllv3_enet_recalc_rate,
  236. };
  237. struct clk *imx_clk_pllv3(enum imx_pllv3_type type, const char *name,
  238. const char *parent_name, void __iomem *base,
  239. u32 div_mask)
  240. {
  241. struct clk_pllv3 *pll;
  242. const struct clk_ops *ops;
  243. struct clk *clk;
  244. struct clk_init_data init;
  245. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  246. if (!pll)
  247. return ERR_PTR(-ENOMEM);
  248. switch (type) {
  249. case IMX_PLLV3_SYS:
  250. ops = &clk_pllv3_sys_ops;
  251. break;
  252. case IMX_PLLV3_USB:
  253. ops = &clk_pllv3_ops;
  254. pll->powerup_set = true;
  255. break;
  256. case IMX_PLLV3_AV:
  257. ops = &clk_pllv3_av_ops;
  258. break;
  259. case IMX_PLLV3_ENET:
  260. ops = &clk_pllv3_enet_ops;
  261. break;
  262. default:
  263. ops = &clk_pllv3_ops;
  264. }
  265. pll->base = base;
  266. pll->div_mask = div_mask;
  267. init.name = name;
  268. init.ops = ops;
  269. init.flags = 0;
  270. init.parent_names = &parent_name;
  271. init.num_parents = 1;
  272. pll->hw.init = &init;
  273. clk = clk_register(NULL, &pll->hw);
  274. if (IS_ERR(clk))
  275. kfree(pll);
  276. return clk;
  277. }