clk-pllv3.c 7.8 KB

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