clk-pllv3.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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/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_ENABLE (0x1 << 13)
  23. #define BM_PLL_BYPASS (0x1 << 16)
  24. #define BM_PLL_LOCK (0x1 << 31)
  25. /**
  26. * struct clk_pllv3 - IMX PLL clock version 3
  27. * @clk_hw: clock source
  28. * @base: base address of PLL registers
  29. * @powerup_set: set POWER bit to power up the PLL
  30. * @gate_mask: mask of gate bits
  31. * @div_mask: mask 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 gate_mask;
  41. u32 div_mask;
  42. };
  43. #define to_clk_pllv3(_hw) container_of(_hw, struct clk_pllv3, hw)
  44. static int clk_pllv3_prepare(struct clk_hw *hw)
  45. {
  46. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  47. unsigned long timeout = jiffies + msecs_to_jiffies(10);
  48. u32 val;
  49. val = readl_relaxed(pll->base);
  50. val &= ~BM_PLL_BYPASS;
  51. if (pll->powerup_set)
  52. val |= BM_PLL_POWER;
  53. else
  54. val &= ~BM_PLL_POWER;
  55. writel_relaxed(val, pll->base);
  56. /* Wait for PLL to lock */
  57. while (!(readl_relaxed(pll->base) & BM_PLL_LOCK))
  58. if (time_after(jiffies, timeout))
  59. return -ETIMEDOUT;
  60. return 0;
  61. }
  62. static void clk_pllv3_unprepare(struct clk_hw *hw)
  63. {
  64. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  65. u32 val;
  66. val = readl_relaxed(pll->base);
  67. val |= BM_PLL_BYPASS;
  68. if (pll->powerup_set)
  69. val &= ~BM_PLL_POWER;
  70. else
  71. val |= BM_PLL_POWER;
  72. writel_relaxed(val, pll->base);
  73. }
  74. static int clk_pllv3_enable(struct clk_hw *hw)
  75. {
  76. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  77. u32 val;
  78. val = readl_relaxed(pll->base);
  79. val |= pll->gate_mask;
  80. writel_relaxed(val, pll->base);
  81. return 0;
  82. }
  83. static void clk_pllv3_disable(struct clk_hw *hw)
  84. {
  85. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  86. u32 val;
  87. val = readl_relaxed(pll->base);
  88. val &= ~pll->gate_mask;
  89. writel_relaxed(val, pll->base);
  90. }
  91. static unsigned long clk_pllv3_recalc_rate(struct clk_hw *hw,
  92. unsigned long parent_rate)
  93. {
  94. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  95. u32 div = readl_relaxed(pll->base) & pll->div_mask;
  96. return (div == 1) ? parent_rate * 22 : parent_rate * 20;
  97. }
  98. static long clk_pllv3_round_rate(struct clk_hw *hw, unsigned long rate,
  99. unsigned long *prate)
  100. {
  101. unsigned long parent_rate = *prate;
  102. return (rate >= parent_rate * 22) ? parent_rate * 22 :
  103. parent_rate * 20;
  104. }
  105. static int clk_pllv3_set_rate(struct clk_hw *hw, unsigned long rate,
  106. unsigned long parent_rate)
  107. {
  108. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  109. u32 val, div;
  110. if (rate == parent_rate * 22)
  111. div = 1;
  112. else if (rate == parent_rate * 20)
  113. div = 0;
  114. else
  115. return -EINVAL;
  116. val = readl_relaxed(pll->base);
  117. val &= ~pll->div_mask;
  118. val |= div;
  119. writel_relaxed(val, pll->base);
  120. return 0;
  121. }
  122. static const struct clk_ops clk_pllv3_ops = {
  123. .prepare = clk_pllv3_prepare,
  124. .unprepare = clk_pllv3_unprepare,
  125. .enable = clk_pllv3_enable,
  126. .disable = clk_pllv3_disable,
  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 0;
  167. }
  168. static const struct clk_ops clk_pllv3_sys_ops = {
  169. .prepare = clk_pllv3_prepare,
  170. .unprepare = clk_pllv3_unprepare,
  171. .enable = clk_pllv3_enable,
  172. .disable = clk_pllv3_disable,
  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. return (parent_rate * div) + ((parent_rate / mfd) * mfn);
  185. }
  186. static long clk_pllv3_av_round_rate(struct clk_hw *hw, unsigned long rate,
  187. unsigned long *prate)
  188. {
  189. unsigned long parent_rate = *prate;
  190. unsigned long min_rate = parent_rate * 27;
  191. unsigned long max_rate = parent_rate * 54;
  192. u32 div;
  193. u32 mfn, mfd = 1000000;
  194. s64 temp64;
  195. if (rate > max_rate)
  196. rate = max_rate;
  197. else if (rate < min_rate)
  198. rate = min_rate;
  199. div = rate / parent_rate;
  200. temp64 = (u64) (rate - div * parent_rate);
  201. temp64 *= mfd;
  202. do_div(temp64, parent_rate);
  203. mfn = temp64;
  204. return parent_rate * div + parent_rate / mfd * mfn;
  205. }
  206. static int clk_pllv3_av_set_rate(struct clk_hw *hw, unsigned long rate,
  207. unsigned long parent_rate)
  208. {
  209. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  210. unsigned long min_rate = parent_rate * 27;
  211. unsigned long max_rate = parent_rate * 54;
  212. u32 val, div;
  213. u32 mfn, mfd = 1000000;
  214. s64 temp64;
  215. if (rate < min_rate || rate > max_rate)
  216. return -EINVAL;
  217. div = rate / parent_rate;
  218. temp64 = (u64) (rate - div * parent_rate);
  219. temp64 *= mfd;
  220. do_div(temp64, parent_rate);
  221. mfn = temp64;
  222. val = readl_relaxed(pll->base);
  223. val &= ~pll->div_mask;
  224. val |= div;
  225. writel_relaxed(val, pll->base);
  226. writel_relaxed(mfn, pll->base + PLL_NUM_OFFSET);
  227. writel_relaxed(mfd, pll->base + PLL_DENOM_OFFSET);
  228. return 0;
  229. }
  230. static const struct clk_ops clk_pllv3_av_ops = {
  231. .prepare = clk_pllv3_prepare,
  232. .unprepare = clk_pllv3_unprepare,
  233. .enable = clk_pllv3_enable,
  234. .disable = clk_pllv3_disable,
  235. .recalc_rate = clk_pllv3_av_recalc_rate,
  236. .round_rate = clk_pllv3_av_round_rate,
  237. .set_rate = clk_pllv3_av_set_rate,
  238. };
  239. static unsigned long clk_pllv3_enet_recalc_rate(struct clk_hw *hw,
  240. unsigned long parent_rate)
  241. {
  242. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  243. u32 div = readl_relaxed(pll->base) & pll->div_mask;
  244. switch (div) {
  245. case 0:
  246. return 25000000;
  247. case 1:
  248. return 50000000;
  249. case 2:
  250. return 100000000;
  251. case 3:
  252. return 125000000;
  253. }
  254. return 0;
  255. }
  256. static long clk_pllv3_enet_round_rate(struct clk_hw *hw, unsigned long rate,
  257. unsigned long *prate)
  258. {
  259. if (rate >= 125000000)
  260. rate = 125000000;
  261. else if (rate >= 100000000)
  262. rate = 100000000;
  263. else if (rate >= 50000000)
  264. rate = 50000000;
  265. else
  266. rate = 25000000;
  267. return rate;
  268. }
  269. static int clk_pllv3_enet_set_rate(struct clk_hw *hw, unsigned long rate,
  270. unsigned long parent_rate)
  271. {
  272. struct clk_pllv3 *pll = to_clk_pllv3(hw);
  273. u32 val, div;
  274. switch (rate) {
  275. case 25000000:
  276. div = 0;
  277. break;
  278. case 50000000:
  279. div = 1;
  280. break;
  281. case 100000000:
  282. div = 2;
  283. break;
  284. case 125000000:
  285. div = 3;
  286. break;
  287. default:
  288. return -EINVAL;
  289. }
  290. val = readl_relaxed(pll->base);
  291. val &= ~pll->div_mask;
  292. val |= div;
  293. writel_relaxed(val, pll->base);
  294. return 0;
  295. }
  296. static const struct clk_ops clk_pllv3_enet_ops = {
  297. .prepare = clk_pllv3_prepare,
  298. .unprepare = clk_pllv3_unprepare,
  299. .enable = clk_pllv3_enable,
  300. .disable = clk_pllv3_disable,
  301. .recalc_rate = clk_pllv3_enet_recalc_rate,
  302. .round_rate = clk_pllv3_enet_round_rate,
  303. .set_rate = clk_pllv3_enet_set_rate,
  304. };
  305. static const struct clk_ops clk_pllv3_mlb_ops = {
  306. .prepare = clk_pllv3_prepare,
  307. .unprepare = clk_pllv3_unprepare,
  308. .enable = clk_pllv3_enable,
  309. .disable = clk_pllv3_disable,
  310. };
  311. struct clk *imx_clk_pllv3(enum imx_pllv3_type type, const char *name,
  312. const char *parent_name, void __iomem *base,
  313. u32 gate_mask, u32 div_mask)
  314. {
  315. struct clk_pllv3 *pll;
  316. const struct clk_ops *ops;
  317. struct clk *clk;
  318. struct clk_init_data init;
  319. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  320. if (!pll)
  321. return ERR_PTR(-ENOMEM);
  322. switch (type) {
  323. case IMX_PLLV3_SYS:
  324. ops = &clk_pllv3_sys_ops;
  325. break;
  326. case IMX_PLLV3_USB:
  327. ops = &clk_pllv3_ops;
  328. pll->powerup_set = true;
  329. break;
  330. case IMX_PLLV3_AV:
  331. ops = &clk_pllv3_av_ops;
  332. break;
  333. case IMX_PLLV3_ENET:
  334. ops = &clk_pllv3_enet_ops;
  335. break;
  336. case IMX_PLLV3_MLB:
  337. ops = &clk_pllv3_mlb_ops;
  338. break;
  339. default:
  340. ops = &clk_pllv3_ops;
  341. }
  342. pll->base = base;
  343. pll->gate_mask = gate_mask;
  344. pll->div_mask = div_mask;
  345. init.name = name;
  346. init.ops = ops;
  347. init.flags = 0;
  348. init.parent_names = &parent_name;
  349. init.num_parents = 1;
  350. pll->hw.init = &init;
  351. clk = clk_register(NULL, &pll->hw);
  352. if (IS_ERR(clk))
  353. kfree(pll);
  354. return clk;
  355. }