clk-pllv3.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. * @power_bit: pll power bit mask
  29. * @powerup_set: set power_bit to power up the PLL
  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. u32 power_bit;
  40. bool powerup_set;
  41. u32 div_mask;
  42. u32 div_shift;
  43. unsigned long ref_clock;
  44. };
  45. #define to_clk_pllv3(_hw) container_of(_hw, struct clk_pllv3, hw)
  46. static int clk_pllv3_wait_lock(struct clk_pllv3 *pll)
  47. {
  48. unsigned long timeout = jiffies + msecs_to_jiffies(10);
  49. u32 val = readl_relaxed(pll->base) & pll->power_bit;
  50. /* No need to wait for lock when pll is not powered up */
  51. if ((pll->powerup_set && !val) || (!pll->powerup_set && val))
  52. return 0;
  53. /* Wait for PLL to lock */
  54. do {
  55. if (readl_relaxed(pll->base) & BM_PLL_LOCK)
  56. break;
  57. if (time_after(jiffies, timeout))
  58. break;
  59. usleep_range(50, 500);
  60. } while (1);
  61. return readl_relaxed(pll->base) & BM_PLL_LOCK ? 0 : -ETIMEDOUT;
  62. }
  63. static int clk_pllv3_prepare(struct clk_hw *hw)
  64. {
  65. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  66. u32 val;
  67. val = readl_relaxed(pll->base);
  68. if (pll->powerup_set)
  69. val |= pll->power_bit;
  70. else
  71. val &= ~pll->power_bit;
  72. writel_relaxed(val, pll->base);
  73. return clk_pllv3_wait_lock(pll);
  74. }
  75. static void clk_pllv3_unprepare(struct clk_hw *hw)
  76. {
  77. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  78. u32 val;
  79. val = readl_relaxed(pll->base);
  80. if (pll->powerup_set)
  81. val &= ~pll->power_bit;
  82. else
  83. val |= pll->power_bit;
  84. writel_relaxed(val, pll->base);
  85. }
  86. static int clk_pllv3_is_prepared(struct clk_hw *hw)
  87. {
  88. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  89. if (readl_relaxed(pll->base) & BM_PLL_LOCK)
  90. return 1;
  91. return 0;
  92. }
  93. static unsigned long clk_pllv3_recalc_rate(struct clk_hw *hw,
  94. unsigned long parent_rate)
  95. {
  96. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  97. u32 div = (readl_relaxed(pll->base) >> pll->div_shift) & pll->div_mask;
  98. return (div == 1) ? parent_rate * 22 : parent_rate * 20;
  99. }
  100. static long clk_pllv3_round_rate(struct clk_hw *hw, unsigned long rate,
  101. unsigned long *prate)
  102. {
  103. unsigned long parent_rate = *prate;
  104. return (rate >= parent_rate * 22) ? parent_rate * 22 :
  105. parent_rate * 20;
  106. }
  107. static int clk_pllv3_set_rate(struct clk_hw *hw, unsigned long rate,
  108. unsigned long parent_rate)
  109. {
  110. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  111. u32 val, div;
  112. if (rate == parent_rate * 22)
  113. div = 1;
  114. else if (rate == parent_rate * 20)
  115. div = 0;
  116. else
  117. return -EINVAL;
  118. val = readl_relaxed(pll->base);
  119. val &= ~(pll->div_mask << pll->div_shift);
  120. val |= (div << pll->div_shift);
  121. writel_relaxed(val, pll->base);
  122. return clk_pllv3_wait_lock(pll);
  123. }
  124. static const struct clk_ops clk_pllv3_ops = {
  125. .prepare = clk_pllv3_prepare,
  126. .unprepare = clk_pllv3_unprepare,
  127. .is_prepared = clk_pllv3_is_prepared,
  128. .recalc_rate = clk_pllv3_recalc_rate,
  129. .round_rate = clk_pllv3_round_rate,
  130. .set_rate = clk_pllv3_set_rate,
  131. };
  132. static unsigned long clk_pllv3_sys_recalc_rate(struct clk_hw *hw,
  133. unsigned long parent_rate)
  134. {
  135. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  136. u32 div = readl_relaxed(pll->base) & pll->div_mask;
  137. return parent_rate * div / 2;
  138. }
  139. static long clk_pllv3_sys_round_rate(struct clk_hw *hw, unsigned long rate,
  140. unsigned long *prate)
  141. {
  142. unsigned long parent_rate = *prate;
  143. unsigned long min_rate = parent_rate * 54 / 2;
  144. unsigned long max_rate = parent_rate * 108 / 2;
  145. u32 div;
  146. if (rate > max_rate)
  147. rate = max_rate;
  148. else if (rate < min_rate)
  149. rate = min_rate;
  150. div = rate * 2 / parent_rate;
  151. return parent_rate * div / 2;
  152. }
  153. static int clk_pllv3_sys_set_rate(struct clk_hw *hw, unsigned long rate,
  154. unsigned long parent_rate)
  155. {
  156. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  157. unsigned long min_rate = parent_rate * 54 / 2;
  158. unsigned long max_rate = parent_rate * 108 / 2;
  159. u32 val, div;
  160. if (rate < min_rate || rate > max_rate)
  161. return -EINVAL;
  162. div = rate * 2 / parent_rate;
  163. val = readl_relaxed(pll->base);
  164. val &= ~pll->div_mask;
  165. val |= div;
  166. writel_relaxed(val, pll->base);
  167. return clk_pllv3_wait_lock(pll);
  168. }
  169. static const struct clk_ops clk_pllv3_sys_ops = {
  170. .prepare = clk_pllv3_prepare,
  171. .unprepare = clk_pllv3_unprepare,
  172. .is_prepared = clk_pllv3_is_prepared,
  173. .recalc_rate = clk_pllv3_sys_recalc_rate,
  174. .round_rate = clk_pllv3_sys_round_rate,
  175. .set_rate = clk_pllv3_sys_set_rate,
  176. };
  177. static unsigned long clk_pllv3_av_recalc_rate(struct clk_hw *hw,
  178. unsigned long parent_rate)
  179. {
  180. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  181. u32 mfn = readl_relaxed(pll->base + PLL_NUM_OFFSET);
  182. u32 mfd = readl_relaxed(pll->base + PLL_DENOM_OFFSET);
  183. u32 div = readl_relaxed(pll->base) & pll->div_mask;
  184. u64 temp64 = (u64)parent_rate;
  185. temp64 *= mfn;
  186. do_div(temp64, mfd);
  187. return parent_rate * div + (unsigned long)temp64;
  188. }
  189. static long clk_pllv3_av_round_rate(struct clk_hw *hw, unsigned long rate,
  190. unsigned long *prate)
  191. {
  192. unsigned long parent_rate = *prate;
  193. unsigned long min_rate = parent_rate * 27;
  194. unsigned long max_rate = parent_rate * 54;
  195. u32 div;
  196. u32 mfn, mfd = 1000000;
  197. u64 temp64;
  198. if (rate > max_rate)
  199. rate = max_rate;
  200. else if (rate < min_rate)
  201. rate = min_rate;
  202. div = rate / parent_rate;
  203. temp64 = (u64) (rate - div * parent_rate);
  204. temp64 *= mfd;
  205. do_div(temp64, parent_rate);
  206. mfn = temp64;
  207. temp64 = (u64)parent_rate;
  208. temp64 *= mfn;
  209. do_div(temp64, mfd);
  210. return parent_rate * div + (unsigned long)temp64;
  211. }
  212. static int clk_pllv3_av_set_rate(struct clk_hw *hw, unsigned long rate,
  213. unsigned long parent_rate)
  214. {
  215. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  216. unsigned long min_rate = parent_rate * 27;
  217. unsigned long max_rate = parent_rate * 54;
  218. u32 val, div;
  219. u32 mfn, mfd = 1000000;
  220. u64 temp64;
  221. if (rate < min_rate || rate > max_rate)
  222. return -EINVAL;
  223. div = rate / parent_rate;
  224. temp64 = (u64) (rate - div * parent_rate);
  225. temp64 *= mfd;
  226. do_div(temp64, parent_rate);
  227. mfn = temp64;
  228. val = readl_relaxed(pll->base);
  229. val &= ~pll->div_mask;
  230. val |= div;
  231. writel_relaxed(val, pll->base);
  232. writel_relaxed(mfn, pll->base + PLL_NUM_OFFSET);
  233. writel_relaxed(mfd, pll->base + PLL_DENOM_OFFSET);
  234. return clk_pllv3_wait_lock(pll);
  235. }
  236. static const struct clk_ops clk_pllv3_av_ops = {
  237. .prepare = clk_pllv3_prepare,
  238. .unprepare = clk_pllv3_unprepare,
  239. .is_prepared = clk_pllv3_is_prepared,
  240. .recalc_rate = clk_pllv3_av_recalc_rate,
  241. .round_rate = clk_pllv3_av_round_rate,
  242. .set_rate = clk_pllv3_av_set_rate,
  243. };
  244. static unsigned long clk_pllv3_enet_recalc_rate(struct clk_hw *hw,
  245. unsigned long parent_rate)
  246. {
  247. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  248. return pll->ref_clock;
  249. }
  250. static const struct clk_ops clk_pllv3_enet_ops = {
  251. .prepare = clk_pllv3_prepare,
  252. .unprepare = clk_pllv3_unprepare,
  253. .is_prepared = clk_pllv3_is_prepared,
  254. .recalc_rate = clk_pllv3_enet_recalc_rate,
  255. };
  256. struct clk *imx_clk_pllv3(enum imx_pllv3_type type, const char *name,
  257. const char *parent_name, void __iomem *base,
  258. u32 div_mask)
  259. {
  260. struct clk_pllv3 *pll;
  261. const struct clk_ops *ops;
  262. struct clk *clk;
  263. struct clk_init_data init;
  264. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  265. if (!pll)
  266. return ERR_PTR(-ENOMEM);
  267. pll->power_bit = BM_PLL_POWER;
  268. switch (type) {
  269. case IMX_PLLV3_SYS:
  270. ops = &clk_pllv3_sys_ops;
  271. break;
  272. case IMX_PLLV3_USB_VF610:
  273. pll->div_shift = 1;
  274. case IMX_PLLV3_USB:
  275. ops = &clk_pllv3_ops;
  276. pll->powerup_set = true;
  277. break;
  278. case IMX_PLLV3_AV:
  279. ops = &clk_pllv3_av_ops;
  280. break;
  281. case IMX_PLLV3_ENET_IMX7:
  282. pll->power_bit = IMX7_ENET_PLL_POWER;
  283. pll->ref_clock = 1000000000;
  284. ops = &clk_pllv3_enet_ops;
  285. break;
  286. case IMX_PLLV3_ENET:
  287. pll->ref_clock = 500000000;
  288. ops = &clk_pllv3_enet_ops;
  289. break;
  290. default:
  291. ops = &clk_pllv3_ops;
  292. }
  293. pll->base = base;
  294. pll->div_mask = div_mask;
  295. init.name = name;
  296. init.ops = ops;
  297. init.flags = 0;
  298. init.parent_names = &parent_name;
  299. init.num_parents = 1;
  300. pll->hw.init = &init;
  301. clk = clk_register(NULL, &pll->hw);
  302. if (IS_ERR(clk))
  303. kfree(pll);
  304. return clk;
  305. }