pll_clock.c 8.5 KB

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