clk-pllv3.c 8.3 KB

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