pll_clock.c 8.7 KB

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