pll_clock.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Synopsys AXS10X SDP Generic PLL clock driver
  4. *
  5. * Copyright (C) 2017 Synopsys
  6. */
  7. #include <linux/platform_device.h>
  8. #include <linux/module.h>
  9. #include <linux/clk-provider.h>
  10. #include <linux/delay.h>
  11. #include <linux/err.h>
  12. #include <linux/device.h>
  13. #include <linux/io.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/slab.h>
  17. /* PLL registers addresses */
  18. #define PLL_REG_IDIV 0x0
  19. #define PLL_REG_FBDIV 0x4
  20. #define PLL_REG_ODIV 0x8
  21. /*
  22. * Bit fields of the PLL IDIV/FBDIV/ODIV registers:
  23. * ________________________________________________________________________
  24. * |31 15| 14 | 13 | 12 |11 6|5 0|
  25. * |-------RESRVED------|-NOUPDATE-|-BYPASS-|-EDGE-|--HIGHTIME--|--LOWTIME--|
  26. * |____________________|__________|________|______|____________|___________|
  27. *
  28. * Following macros determine the way of access to these registers
  29. * They should be set up only using the macros.
  30. * reg should be an u32 variable.
  31. */
  32. #define PLL_REG_GET_LOW(reg) \
  33. (((reg) & (0x3F << 0)) >> 0)
  34. #define PLL_REG_GET_HIGH(reg) \
  35. (((reg) & (0x3F << 6)) >> 6)
  36. #define PLL_REG_GET_EDGE(reg) \
  37. (((reg) & (BIT(12))) ? 1 : 0)
  38. #define PLL_REG_GET_BYPASS(reg) \
  39. (((reg) & (BIT(13))) ? 1 : 0)
  40. #define PLL_REG_GET_NOUPD(reg) \
  41. (((reg) & (BIT(14))) ? 1 : 0)
  42. #define PLL_REG_GET_PAD(reg) \
  43. (((reg) & (0x1FFFF << 15)) >> 15)
  44. #define PLL_REG_SET_LOW(reg, value) \
  45. { reg |= (((value) & 0x3F) << 0); }
  46. #define PLL_REG_SET_HIGH(reg, value) \
  47. { reg |= (((value) & 0x3F) << 6); }
  48. #define PLL_REG_SET_EDGE(reg, value) \
  49. { reg |= (((value) & 0x01) << 12); }
  50. #define PLL_REG_SET_BYPASS(reg, value) \
  51. { reg |= (((value) & 0x01) << 13); }
  52. #define PLL_REG_SET_NOUPD(reg, value) \
  53. { reg |= (((value) & 0x01) << 14); }
  54. #define PLL_REG_SET_PAD(reg, value) \
  55. { reg |= (((value) & 0x1FFFF) << 15); }
  56. #define PLL_LOCK BIT(0)
  57. #define PLL_ERROR BIT(1)
  58. #define PLL_MAX_LOCK_TIME 100 /* 100 us */
  59. struct axs10x_pll_cfg {
  60. u32 rate;
  61. u32 idiv;
  62. u32 fbdiv;
  63. u32 odiv;
  64. };
  65. static const struct axs10x_pll_cfg arc_pll_cfg[] = {
  66. { 33333333, 1, 1, 1 },
  67. { 50000000, 1, 30, 20 },
  68. { 75000000, 2, 45, 10 },
  69. { 90000000, 2, 54, 10 },
  70. { 100000000, 1, 30, 10 },
  71. { 125000000, 2, 45, 6 },
  72. {}
  73. };
  74. static const struct axs10x_pll_cfg pgu_pll_cfg[] = {
  75. { 25200000, 1, 84, 90 },
  76. { 50000000, 1, 100, 54 },
  77. { 74250000, 1, 44, 16 },
  78. {}
  79. };
  80. struct axs10x_pll_clk {
  81. struct clk_hw hw;
  82. void __iomem *base;
  83. void __iomem *lock;
  84. const struct axs10x_pll_cfg *pll_cfg;
  85. struct device *dev;
  86. };
  87. static inline void axs10x_pll_write(struct axs10x_pll_clk *clk, u32 reg,
  88. u32 val)
  89. {
  90. iowrite32(val, clk->base + reg);
  91. }
  92. static inline u32 axs10x_pll_read(struct axs10x_pll_clk *clk, u32 reg)
  93. {
  94. return ioread32(clk->base + reg);
  95. }
  96. static inline struct axs10x_pll_clk *to_axs10x_pll_clk(struct clk_hw *hw)
  97. {
  98. return container_of(hw, struct axs10x_pll_clk, hw);
  99. }
  100. static inline u32 axs10x_div_get_value(u32 reg)
  101. {
  102. if (PLL_REG_GET_BYPASS(reg))
  103. return 1;
  104. return PLL_REG_GET_HIGH(reg) + PLL_REG_GET_LOW(reg);
  105. }
  106. static inline u32 axs10x_encode_div(unsigned int id, int upd)
  107. {
  108. u32 div = 0;
  109. PLL_REG_SET_LOW(div, (id % 2 == 0) ? id >> 1 : (id >> 1) + 1);
  110. PLL_REG_SET_HIGH(div, id >> 1);
  111. PLL_REG_SET_EDGE(div, id % 2);
  112. PLL_REG_SET_BYPASS(div, id == 1 ? 1 : 0);
  113. PLL_REG_SET_NOUPD(div, upd == 0 ? 1 : 0);
  114. return div;
  115. }
  116. static unsigned long axs10x_pll_recalc_rate(struct clk_hw *hw,
  117. unsigned long parent_rate)
  118. {
  119. u64 rate;
  120. u32 idiv, fbdiv, odiv;
  121. struct axs10x_pll_clk *clk = to_axs10x_pll_clk(hw);
  122. idiv = axs10x_div_get_value(axs10x_pll_read(clk, PLL_REG_IDIV));
  123. fbdiv = axs10x_div_get_value(axs10x_pll_read(clk, PLL_REG_FBDIV));
  124. odiv = axs10x_div_get_value(axs10x_pll_read(clk, PLL_REG_ODIV));
  125. rate = (u64)parent_rate * fbdiv;
  126. do_div(rate, idiv * odiv);
  127. return rate;
  128. }
  129. static long axs10x_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  130. unsigned long *prate)
  131. {
  132. int i;
  133. long best_rate;
  134. struct axs10x_pll_clk *clk = to_axs10x_pll_clk(hw);
  135. const struct axs10x_pll_cfg *pll_cfg = clk->pll_cfg;
  136. if (pll_cfg[0].rate == 0)
  137. return -EINVAL;
  138. best_rate = pll_cfg[0].rate;
  139. for (i = 1; pll_cfg[i].rate != 0; i++) {
  140. if (abs(rate - pll_cfg[i].rate) < abs(rate - best_rate))
  141. best_rate = pll_cfg[i].rate;
  142. }
  143. return best_rate;
  144. }
  145. static int axs10x_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  146. unsigned long parent_rate)
  147. {
  148. int i;
  149. struct axs10x_pll_clk *clk = to_axs10x_pll_clk(hw);
  150. const struct axs10x_pll_cfg *pll_cfg = clk->pll_cfg;
  151. for (i = 0; pll_cfg[i].rate != 0; i++) {
  152. if (pll_cfg[i].rate == rate) {
  153. axs10x_pll_write(clk, PLL_REG_IDIV,
  154. axs10x_encode_div(pll_cfg[i].idiv, 0));
  155. axs10x_pll_write(clk, PLL_REG_FBDIV,
  156. axs10x_encode_div(pll_cfg[i].fbdiv, 0));
  157. axs10x_pll_write(clk, PLL_REG_ODIV,
  158. axs10x_encode_div(pll_cfg[i].odiv, 1));
  159. /*
  160. * Wait until CGU relocks and check error status.
  161. * If after timeout CGU is unlocked yet return error
  162. */
  163. udelay(PLL_MAX_LOCK_TIME);
  164. if (!(ioread32(clk->lock) & PLL_LOCK))
  165. return -ETIMEDOUT;
  166. if (ioread32(clk->lock) & PLL_ERROR)
  167. return -EINVAL;
  168. return 0;
  169. }
  170. }
  171. dev_err(clk->dev, "invalid rate=%ld, parent_rate=%ld\n", rate,
  172. parent_rate);
  173. return -EINVAL;
  174. }
  175. static const struct clk_ops axs10x_pll_ops = {
  176. .recalc_rate = axs10x_pll_recalc_rate,
  177. .round_rate = axs10x_pll_round_rate,
  178. .set_rate = axs10x_pll_set_rate,
  179. };
  180. static int axs10x_pll_clk_probe(struct platform_device *pdev)
  181. {
  182. struct device *dev = &pdev->dev;
  183. const char *parent_name;
  184. struct axs10x_pll_clk *pll_clk;
  185. struct clk_init_data init = { };
  186. int ret;
  187. pll_clk = devm_kzalloc(dev, sizeof(*pll_clk), GFP_KERNEL);
  188. if (!pll_clk)
  189. return -ENOMEM;
  190. pll_clk->base = devm_platform_ioremap_resource(pdev, 0);
  191. if (IS_ERR(pll_clk->base))
  192. return PTR_ERR(pll_clk->base);
  193. pll_clk->lock = devm_platform_ioremap_resource(pdev, 1);
  194. if (IS_ERR(pll_clk->lock))
  195. return PTR_ERR(pll_clk->lock);
  196. init.name = dev->of_node->name;
  197. init.ops = &axs10x_pll_ops;
  198. parent_name = of_clk_get_parent_name(dev->of_node, 0);
  199. init.parent_names = &parent_name;
  200. init.num_parents = 1;
  201. pll_clk->hw.init = &init;
  202. pll_clk->dev = dev;
  203. pll_clk->pll_cfg = of_device_get_match_data(dev);
  204. if (!pll_clk->pll_cfg) {
  205. dev_err(dev, "No OF match data provided\n");
  206. return -EINVAL;
  207. }
  208. ret = devm_clk_hw_register(dev, &pll_clk->hw);
  209. if (ret) {
  210. dev_err(dev, "failed to register %s clock\n", init.name);
  211. return ret;
  212. }
  213. return devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get,
  214. &pll_clk->hw);
  215. }
  216. static void __init of_axs10x_pll_clk_setup(struct device_node *node)
  217. {
  218. const char *parent_name;
  219. struct axs10x_pll_clk *pll_clk;
  220. struct clk_init_data init = { };
  221. int ret;
  222. pll_clk = kzalloc(sizeof(*pll_clk), GFP_KERNEL);
  223. if (!pll_clk)
  224. return;
  225. pll_clk->base = of_iomap(node, 0);
  226. if (!pll_clk->base) {
  227. pr_err("failed to map pll div registers\n");
  228. goto err_free_pll_clk;
  229. }
  230. pll_clk->lock = of_iomap(node, 1);
  231. if (!pll_clk->lock) {
  232. pr_err("failed to map pll lock register\n");
  233. goto err_unmap_base;
  234. }
  235. init.name = node->name;
  236. init.ops = &axs10x_pll_ops;
  237. parent_name = of_clk_get_parent_name(node, 0);
  238. init.parent_names = &parent_name;
  239. init.num_parents = parent_name ? 1 : 0;
  240. pll_clk->hw.init = &init;
  241. pll_clk->pll_cfg = arc_pll_cfg;
  242. ret = clk_hw_register(NULL, &pll_clk->hw);
  243. if (ret) {
  244. pr_err("failed to register %pOFn clock\n", node);
  245. goto err_unmap_lock;
  246. }
  247. ret = of_clk_add_hw_provider(node, of_clk_hw_simple_get, &pll_clk->hw);
  248. if (ret) {
  249. pr_err("failed to add hw provider for %pOFn clock\n", node);
  250. goto err_unregister_clk;
  251. }
  252. return;
  253. err_unregister_clk:
  254. clk_hw_unregister(&pll_clk->hw);
  255. err_unmap_lock:
  256. iounmap(pll_clk->lock);
  257. err_unmap_base:
  258. iounmap(pll_clk->base);
  259. err_free_pll_clk:
  260. kfree(pll_clk);
  261. }
  262. CLK_OF_DECLARE(axs10x_pll_clock, "snps,axs10x-arc-pll-clock",
  263. of_axs10x_pll_clk_setup);
  264. static const struct of_device_id axs10x_pll_clk_id[] = {
  265. { .compatible = "snps,axs10x-pgu-pll-clock", .data = &pgu_pll_cfg},
  266. { }
  267. };
  268. MODULE_DEVICE_TABLE(of, axs10x_pll_clk_id);
  269. static struct platform_driver axs10x_pll_clk_driver = {
  270. .driver = {
  271. .name = "axs10x-pll-clock",
  272. .of_match_table = axs10x_pll_clk_id,
  273. },
  274. .probe = axs10x_pll_clk_probe,
  275. };
  276. builtin_platform_driver(axs10x_pll_clk_driver);
  277. MODULE_AUTHOR("Vlad Zakharov <vzakhar@synopsys.com>");
  278. MODULE_DESCRIPTION("Synopsys AXS10X SDP Generic PLL Clock Driver");
  279. MODULE_LICENSE("GPL v2");