clk-pllv3.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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 PLL_VF610_NUM_OFFSET 0x20
  22. #define PLL_VF610_DENOM_OFFSET 0x30
  23. #define BM_PLL_POWER (0x1 << 12)
  24. #define BM_PLL_LOCK (0x1 << 31)
  25. #define IMX7_ENET_PLL_POWER (0x1 << 5)
  26. /**
  27. * struct clk_pllv3 - IMX PLL clock version 3
  28. * @clk_hw: clock source
  29. * @base: base address of PLL registers
  30. * @power_bit: pll power bit mask
  31. * @powerup_set: set power_bit to power up the PLL
  32. * @div_mask: mask of divider bits
  33. * @div_shift: shift of divider bits
  34. *
  35. * IMX PLL clock version 3, found on i.MX6 series. Divider for pllv3
  36. * is actually a multiplier, and always sits at bit 0.
  37. */
  38. struct clk_pllv3 {
  39. struct clk_hw hw;
  40. void __iomem *base;
  41. u32 power_bit;
  42. bool powerup_set;
  43. u32 div_mask;
  44. u32 div_shift;
  45. unsigned long ref_clock;
  46. };
  47. #define to_clk_pllv3(_hw) container_of(_hw, struct clk_pllv3, hw)
  48. static int clk_pllv3_wait_lock(struct clk_pllv3 *pll)
  49. {
  50. unsigned long timeout = jiffies + msecs_to_jiffies(10);
  51. u32 val = readl_relaxed(pll->base) & pll->power_bit;
  52. /* No need to wait for lock when pll is not powered up */
  53. if ((pll->powerup_set && !val) || (!pll->powerup_set && val))
  54. return 0;
  55. /* Wait for PLL to lock */
  56. do {
  57. if (readl_relaxed(pll->base) & BM_PLL_LOCK)
  58. break;
  59. if (time_after(jiffies, timeout))
  60. break;
  61. usleep_range(50, 500);
  62. } while (1);
  63. return readl_relaxed(pll->base) & BM_PLL_LOCK ? 0 : -ETIMEDOUT;
  64. }
  65. static int clk_pllv3_prepare(struct clk_hw *hw)
  66. {
  67. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  68. u32 val;
  69. val = readl_relaxed(pll->base);
  70. if (pll->powerup_set)
  71. val |= pll->power_bit;
  72. else
  73. val &= ~pll->power_bit;
  74. writel_relaxed(val, pll->base);
  75. return clk_pllv3_wait_lock(pll);
  76. }
  77. static void clk_pllv3_unprepare(struct clk_hw *hw)
  78. {
  79. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  80. u32 val;
  81. val = readl_relaxed(pll->base);
  82. if (pll->powerup_set)
  83. val &= ~pll->power_bit;
  84. else
  85. val |= pll->power_bit;
  86. writel_relaxed(val, pll->base);
  87. }
  88. static int clk_pllv3_is_prepared(struct clk_hw *hw)
  89. {
  90. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  91. if (readl_relaxed(pll->base) & BM_PLL_LOCK)
  92. return 1;
  93. return 0;
  94. }
  95. static unsigned long clk_pllv3_recalc_rate(struct clk_hw *hw,
  96. unsigned long parent_rate)
  97. {
  98. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  99. u32 div = (readl_relaxed(pll->base) >> pll->div_shift) & pll->div_mask;
  100. return (div == 1) ? parent_rate * 22 : parent_rate * 20;
  101. }
  102. static long clk_pllv3_round_rate(struct clk_hw *hw, unsigned long rate,
  103. unsigned long *prate)
  104. {
  105. unsigned long parent_rate = *prate;
  106. return (rate >= parent_rate * 22) ? parent_rate * 22 :
  107. parent_rate * 20;
  108. }
  109. static int clk_pllv3_set_rate(struct clk_hw *hw, unsigned long rate,
  110. unsigned long parent_rate)
  111. {
  112. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  113. u32 val, div;
  114. if (rate == parent_rate * 22)
  115. div = 1;
  116. else if (rate == parent_rate * 20)
  117. div = 0;
  118. else
  119. return -EINVAL;
  120. val = readl_relaxed(pll->base);
  121. val &= ~(pll->div_mask << pll->div_shift);
  122. val |= (div << pll->div_shift);
  123. writel_relaxed(val, pll->base);
  124. return clk_pllv3_wait_lock(pll);
  125. }
  126. static const struct clk_ops clk_pllv3_ops = {
  127. .prepare = clk_pllv3_prepare,
  128. .unprepare = clk_pllv3_unprepare,
  129. .is_prepared = clk_pllv3_is_prepared,
  130. .recalc_rate = clk_pllv3_recalc_rate,
  131. .round_rate = clk_pllv3_round_rate,
  132. .set_rate = clk_pllv3_set_rate,
  133. };
  134. static unsigned long clk_pllv3_sys_recalc_rate(struct clk_hw *hw,
  135. unsigned long parent_rate)
  136. {
  137. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  138. u32 div = readl_relaxed(pll->base) & pll->div_mask;
  139. return parent_rate * div / 2;
  140. }
  141. static long clk_pllv3_sys_round_rate(struct clk_hw *hw, unsigned long rate,
  142. unsigned long *prate)
  143. {
  144. unsigned long parent_rate = *prate;
  145. unsigned long min_rate = parent_rate * 54 / 2;
  146. unsigned long max_rate = parent_rate * 108 / 2;
  147. u32 div;
  148. if (rate > max_rate)
  149. rate = max_rate;
  150. else if (rate < min_rate)
  151. rate = min_rate;
  152. div = rate * 2 / parent_rate;
  153. return parent_rate * div / 2;
  154. }
  155. static int clk_pllv3_sys_set_rate(struct clk_hw *hw, unsigned long rate,
  156. unsigned long parent_rate)
  157. {
  158. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  159. unsigned long min_rate = parent_rate * 54 / 2;
  160. unsigned long max_rate = parent_rate * 108 / 2;
  161. u32 val, div;
  162. if (rate < min_rate || rate > max_rate)
  163. return -EINVAL;
  164. div = rate * 2 / parent_rate;
  165. val = readl_relaxed(pll->base);
  166. val &= ~pll->div_mask;
  167. val |= div;
  168. writel_relaxed(val, pll->base);
  169. return clk_pllv3_wait_lock(pll);
  170. }
  171. static const struct clk_ops clk_pllv3_sys_ops = {
  172. .prepare = clk_pllv3_prepare,
  173. .unprepare = clk_pllv3_unprepare,
  174. .is_prepared = clk_pllv3_is_prepared,
  175. .recalc_rate = clk_pllv3_sys_recalc_rate,
  176. .round_rate = clk_pllv3_sys_round_rate,
  177. .set_rate = clk_pllv3_sys_set_rate,
  178. };
  179. static unsigned long clk_pllv3_av_recalc_rate(struct clk_hw *hw,
  180. unsigned long parent_rate)
  181. {
  182. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  183. u32 mfn = readl_relaxed(pll->base + PLL_NUM_OFFSET);
  184. u32 mfd = readl_relaxed(pll->base + PLL_DENOM_OFFSET);
  185. u32 div = readl_relaxed(pll->base) & pll->div_mask;
  186. u64 temp64 = (u64)parent_rate;
  187. temp64 *= mfn;
  188. do_div(temp64, mfd);
  189. return parent_rate * div + (unsigned long)temp64;
  190. }
  191. static long clk_pllv3_av_round_rate(struct clk_hw *hw, unsigned long rate,
  192. unsigned long *prate)
  193. {
  194. unsigned long parent_rate = *prate;
  195. unsigned long min_rate = parent_rate * 27;
  196. unsigned long max_rate = parent_rate * 54;
  197. u32 div;
  198. u32 mfn, mfd = 1000000;
  199. u32 max_mfd = 0x3FFFFFFF;
  200. u64 temp64;
  201. if (rate > max_rate)
  202. rate = max_rate;
  203. else if (rate < min_rate)
  204. rate = min_rate;
  205. if (parent_rate <= max_mfd)
  206. mfd = parent_rate;
  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. temp64 = (u64)parent_rate;
  213. temp64 *= mfn;
  214. do_div(temp64, mfd);
  215. return parent_rate * div + (unsigned long)temp64;
  216. }
  217. static int clk_pllv3_av_set_rate(struct clk_hw *hw, unsigned long rate,
  218. unsigned long parent_rate)
  219. {
  220. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  221. unsigned long min_rate = parent_rate * 27;
  222. unsigned long max_rate = parent_rate * 54;
  223. u32 val, div;
  224. u32 mfn, mfd = 1000000;
  225. u32 max_mfd = 0x3FFFFFFF;
  226. u64 temp64;
  227. if (rate < min_rate || rate > max_rate)
  228. return -EINVAL;
  229. if (parent_rate <= max_mfd)
  230. mfd = parent_rate;
  231. div = rate / parent_rate;
  232. temp64 = (u64) (rate - div * parent_rate);
  233. temp64 *= mfd;
  234. do_div(temp64, parent_rate);
  235. mfn = temp64;
  236. val = readl_relaxed(pll->base);
  237. val &= ~pll->div_mask;
  238. val |= div;
  239. writel_relaxed(val, pll->base);
  240. writel_relaxed(mfn, pll->base + PLL_NUM_OFFSET);
  241. writel_relaxed(mfd, pll->base + PLL_DENOM_OFFSET);
  242. return clk_pllv3_wait_lock(pll);
  243. }
  244. static const struct clk_ops clk_pllv3_av_ops = {
  245. .prepare = clk_pllv3_prepare,
  246. .unprepare = clk_pllv3_unprepare,
  247. .is_prepared = clk_pllv3_is_prepared,
  248. .recalc_rate = clk_pllv3_av_recalc_rate,
  249. .round_rate = clk_pllv3_av_round_rate,
  250. .set_rate = clk_pllv3_av_set_rate,
  251. };
  252. struct clk_pllv3_vf610_mf {
  253. u32 mfi; /* integer part, can be 20 or 22 */
  254. u32 mfn; /* numerator, 30-bit value */
  255. u32 mfd; /* denominator, 30-bit value, must be less than mfn */
  256. };
  257. static unsigned long clk_pllv3_vf610_mf_to_rate(unsigned long parent_rate,
  258. struct clk_pllv3_vf610_mf mf)
  259. {
  260. u64 temp64;
  261. temp64 = parent_rate;
  262. temp64 *= mf.mfn;
  263. do_div(temp64, mf.mfd);
  264. return (parent_rate * mf.mfi) + temp64;
  265. }
  266. static struct clk_pllv3_vf610_mf clk_pllv3_vf610_rate_to_mf(
  267. unsigned long parent_rate, unsigned long rate)
  268. {
  269. struct clk_pllv3_vf610_mf mf;
  270. u64 temp64;
  271. mf.mfi = (rate >= 22 * parent_rate) ? 22 : 20;
  272. mf.mfd = 0x3fffffff; /* use max supported value for best accuracy */
  273. if (rate <= parent_rate * mf.mfi)
  274. mf.mfn = 0;
  275. else if (rate >= parent_rate * (mf.mfi + 1))
  276. mf.mfn = mf.mfd - 1;
  277. else {
  278. /* rate = parent_rate * (mfi + mfn/mfd) */
  279. temp64 = rate - parent_rate * mf.mfi;
  280. temp64 *= mf.mfd;
  281. do_div(temp64, parent_rate);
  282. mf.mfn = temp64;
  283. }
  284. return mf;
  285. }
  286. static unsigned long clk_pllv3_vf610_recalc_rate(struct clk_hw *hw,
  287. unsigned long parent_rate)
  288. {
  289. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  290. struct clk_pllv3_vf610_mf mf;
  291. mf.mfn = readl_relaxed(pll->base + PLL_VF610_NUM_OFFSET);
  292. mf.mfd = readl_relaxed(pll->base + PLL_VF610_DENOM_OFFSET);
  293. mf.mfi = (readl_relaxed(pll->base) & pll->div_mask) ? 22 : 20;
  294. return clk_pllv3_vf610_mf_to_rate(parent_rate, mf);
  295. }
  296. static long clk_pllv3_vf610_round_rate(struct clk_hw *hw, unsigned long rate,
  297. unsigned long *prate)
  298. {
  299. struct clk_pllv3_vf610_mf mf = clk_pllv3_vf610_rate_to_mf(*prate, rate);
  300. return clk_pllv3_vf610_mf_to_rate(*prate, mf);
  301. }
  302. static int clk_pllv3_vf610_set_rate(struct clk_hw *hw, unsigned long rate,
  303. unsigned long parent_rate)
  304. {
  305. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  306. struct clk_pllv3_vf610_mf mf =
  307. clk_pllv3_vf610_rate_to_mf(parent_rate, rate);
  308. u32 val;
  309. val = readl_relaxed(pll->base);
  310. if (mf.mfi == 20)
  311. val &= ~pll->div_mask; /* clear bit for mfi=20 */
  312. else
  313. val |= pll->div_mask; /* set bit for mfi=22 */
  314. writel_relaxed(val, pll->base);
  315. writel_relaxed(mf.mfn, pll->base + PLL_VF610_NUM_OFFSET);
  316. writel_relaxed(mf.mfd, pll->base + PLL_VF610_DENOM_OFFSET);
  317. return clk_pllv3_wait_lock(pll);
  318. }
  319. static const struct clk_ops clk_pllv3_vf610_ops = {
  320. .prepare = clk_pllv3_prepare,
  321. .unprepare = clk_pllv3_unprepare,
  322. .is_prepared = clk_pllv3_is_prepared,
  323. .recalc_rate = clk_pllv3_vf610_recalc_rate,
  324. .round_rate = clk_pllv3_vf610_round_rate,
  325. .set_rate = clk_pllv3_vf610_set_rate,
  326. };
  327. static unsigned long clk_pllv3_enet_recalc_rate(struct clk_hw *hw,
  328. unsigned long parent_rate)
  329. {
  330. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  331. return pll->ref_clock;
  332. }
  333. static const struct clk_ops clk_pllv3_enet_ops = {
  334. .prepare = clk_pllv3_prepare,
  335. .unprepare = clk_pllv3_unprepare,
  336. .is_prepared = clk_pllv3_is_prepared,
  337. .recalc_rate = clk_pllv3_enet_recalc_rate,
  338. };
  339. struct clk *imx_clk_pllv3(enum imx_pllv3_type type, const char *name,
  340. const char *parent_name, void __iomem *base,
  341. u32 div_mask)
  342. {
  343. struct clk_pllv3 *pll;
  344. const struct clk_ops *ops;
  345. struct clk *clk;
  346. struct clk_init_data init;
  347. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  348. if (!pll)
  349. return ERR_PTR(-ENOMEM);
  350. pll->power_bit = BM_PLL_POWER;
  351. switch (type) {
  352. case IMX_PLLV3_SYS:
  353. ops = &clk_pllv3_sys_ops;
  354. break;
  355. case IMX_PLLV3_SYS_VF610:
  356. ops = &clk_pllv3_vf610_ops;
  357. break;
  358. case IMX_PLLV3_USB_VF610:
  359. pll->div_shift = 1;
  360. case IMX_PLLV3_USB:
  361. ops = &clk_pllv3_ops;
  362. pll->powerup_set = true;
  363. break;
  364. case IMX_PLLV3_AV:
  365. ops = &clk_pllv3_av_ops;
  366. break;
  367. case IMX_PLLV3_ENET_IMX7:
  368. pll->power_bit = IMX7_ENET_PLL_POWER;
  369. pll->ref_clock = 1000000000;
  370. ops = &clk_pllv3_enet_ops;
  371. break;
  372. case IMX_PLLV3_ENET:
  373. pll->ref_clock = 500000000;
  374. ops = &clk_pllv3_enet_ops;
  375. break;
  376. default:
  377. ops = &clk_pllv3_ops;
  378. }
  379. pll->base = base;
  380. pll->div_mask = div_mask;
  381. init.name = name;
  382. init.ops = ops;
  383. init.flags = 0;
  384. init.parent_names = &parent_name;
  385. init.num_parents = 1;
  386. pll->hw.init = &init;
  387. clk = clk_register(NULL, &pll->hw);
  388. if (IS_ERR(clk))
  389. kfree(pll);
  390. return clk;
  391. }