clk-pllv3.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 BM_PLL_POWER (0x1 << 12)
  22. #define BM_PLL_LOCK (0x1 << 31)
  23. #define IMX7_ENET_PLL_POWER (0x1 << 5)
  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. * @powerdown: pll powerdown offset bit
  30. * @div_mask: mask of divider bits
  31. * @div_shift: shift of divider bits
  32. *
  33. * IMX PLL clock version 3, found on i.MX6 series. Divider for pllv3
  34. * is actually a multiplier, and always sits at bit 0.
  35. */
  36. struct clk_pllv3 {
  37. struct clk_hw hw;
  38. void __iomem *base;
  39. bool powerup_set;
  40. u32 powerdown;
  41. u32 div_mask;
  42. u32 div_shift;
  43. };
  44. #define to_clk_pllv3(_hw) container_of(_hw, struct clk_pllv3, hw)
  45. static int clk_pllv3_wait_lock(struct clk_pllv3 *pll)
  46. {
  47. unsigned long timeout = jiffies + msecs_to_jiffies(10);
  48. u32 val = readl_relaxed(pll->base) & pll->powerdown;
  49. /* No need to wait for lock when pll is not powered up */
  50. if ((pll->powerup_set && !val) || (!pll->powerup_set && val))
  51. return 0;
  52. /* Wait for PLL to lock */
  53. do {
  54. if (readl_relaxed(pll->base) & BM_PLL_LOCK)
  55. break;
  56. if (time_after(jiffies, timeout))
  57. break;
  58. usleep_range(50, 500);
  59. } while (1);
  60. return readl_relaxed(pll->base) & BM_PLL_LOCK ? 0 : -ETIMEDOUT;
  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 |= BM_PLL_POWER;
  69. else
  70. val &= ~BM_PLL_POWER;
  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 &= ~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_shift) & 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 << pll->div_shift);
  112. val |= (div << pll->div_shift);
  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. u64 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. u64 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. pll->powerdown = BM_PLL_POWER;
  249. switch (type) {
  250. case IMX_PLLV3_SYS:
  251. ops = &clk_pllv3_sys_ops;
  252. break;
  253. case IMX_PLLV3_USB_VF610:
  254. pll->div_shift = 1;
  255. case IMX_PLLV3_USB:
  256. ops = &clk_pllv3_ops;
  257. pll->powerup_set = true;
  258. break;
  259. case IMX_PLLV3_AV:
  260. ops = &clk_pllv3_av_ops;
  261. break;
  262. case IMX_PLLV3_ENET_IMX7:
  263. pll->powerdown = IMX7_ENET_PLL_POWER;
  264. case IMX_PLLV3_ENET:
  265. ops = &clk_pllv3_enet_ops;
  266. break;
  267. default:
  268. ops = &clk_pllv3_ops;
  269. }
  270. pll->base = base;
  271. pll->div_mask = div_mask;
  272. init.name = name;
  273. init.ops = ops;
  274. init.flags = 0;
  275. init.parent_names = &parent_name;
  276. init.num_parents = 1;
  277. pll->hw.init = &init;
  278. clk = clk_register(NULL, &pll->hw);
  279. if (IS_ERR(clk))
  280. kfree(pll);
  281. return clk;
  282. }