clk-ccu-pll.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
  4. *
  5. * Authors:
  6. * Serge Semin <[email protected]>
  7. * Dmitry Dunaev <[email protected]>
  8. *
  9. * Baikal-T1 CCU PLL clocks driver
  10. */
  11. #define pr_fmt(fmt) "bt1-ccu-pll: " fmt
  12. #include <linux/kernel.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/printk.h>
  15. #include <linux/slab.h>
  16. #include <linux/clk-provider.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/ioport.h>
  21. #include <linux/regmap.h>
  22. #include <dt-bindings/clock/bt1-ccu.h>
  23. #include "ccu-pll.h"
  24. #define CCU_CPU_PLL_BASE 0x000
  25. #define CCU_SATA_PLL_BASE 0x008
  26. #define CCU_DDR_PLL_BASE 0x010
  27. #define CCU_PCIE_PLL_BASE 0x018
  28. #define CCU_ETH_PLL_BASE 0x020
  29. #define CCU_PLL_INFO(_id, _name, _pname, _base, _flags, _features) \
  30. { \
  31. .id = _id, \
  32. .name = _name, \
  33. .parent_name = _pname, \
  34. .base = _base, \
  35. .flags = _flags, \
  36. .features = _features, \
  37. }
  38. #define CCU_PLL_NUM ARRAY_SIZE(pll_info)
  39. struct ccu_pll_info {
  40. unsigned int id;
  41. const char *name;
  42. const char *parent_name;
  43. unsigned int base;
  44. unsigned long flags;
  45. unsigned long features;
  46. };
  47. /*
  48. * Alas we have to mark all PLLs as critical. CPU and DDR PLLs are sources of
  49. * CPU cores and DDR controller reference clocks, due to which they obviously
  50. * shouldn't be ever gated. SATA and PCIe PLLs are the parents of APB-bus and
  51. * DDR controller AXI-bus clocks. If they are gated the system will be
  52. * unusable. Moreover disabling SATA and Ethernet PLLs causes automatic reset
  53. * of the corresponding subsystems. So until we aren't ready to re-initialize
  54. * all the devices consuming those PLLs, they will be marked as critical too.
  55. */
  56. static const struct ccu_pll_info pll_info[] = {
  57. CCU_PLL_INFO(CCU_CPU_PLL, "cpu_pll", "ref_clk", CCU_CPU_PLL_BASE,
  58. CLK_IS_CRITICAL, CCU_PLL_BASIC),
  59. CCU_PLL_INFO(CCU_SATA_PLL, "sata_pll", "ref_clk", CCU_SATA_PLL_BASE,
  60. CLK_IS_CRITICAL | CLK_SET_RATE_GATE, 0),
  61. CCU_PLL_INFO(CCU_DDR_PLL, "ddr_pll", "ref_clk", CCU_DDR_PLL_BASE,
  62. CLK_IS_CRITICAL | CLK_SET_RATE_GATE, 0),
  63. CCU_PLL_INFO(CCU_PCIE_PLL, "pcie_pll", "ref_clk", CCU_PCIE_PLL_BASE,
  64. CLK_IS_CRITICAL, CCU_PLL_BASIC),
  65. CCU_PLL_INFO(CCU_ETH_PLL, "eth_pll", "ref_clk", CCU_ETH_PLL_BASE,
  66. CLK_IS_CRITICAL | CLK_SET_RATE_GATE, 0)
  67. };
  68. struct ccu_pll_data {
  69. struct device_node *np;
  70. struct regmap *sys_regs;
  71. struct ccu_pll *plls[CCU_PLL_NUM];
  72. };
  73. static struct ccu_pll_data *pll_data;
  74. static struct ccu_pll *ccu_pll_find_desc(struct ccu_pll_data *data,
  75. unsigned int clk_id)
  76. {
  77. int idx;
  78. for (idx = 0; idx < CCU_PLL_NUM; ++idx) {
  79. if (pll_info[idx].id == clk_id)
  80. return data->plls[idx];
  81. }
  82. return ERR_PTR(-EINVAL);
  83. }
  84. static struct ccu_pll_data *ccu_pll_create_data(struct device_node *np)
  85. {
  86. struct ccu_pll_data *data;
  87. data = kzalloc(sizeof(*data), GFP_KERNEL);
  88. if (!data)
  89. return ERR_PTR(-ENOMEM);
  90. data->np = np;
  91. return data;
  92. }
  93. static void ccu_pll_free_data(struct ccu_pll_data *data)
  94. {
  95. kfree(data);
  96. }
  97. static int ccu_pll_find_sys_regs(struct ccu_pll_data *data)
  98. {
  99. data->sys_regs = syscon_node_to_regmap(data->np->parent);
  100. if (IS_ERR(data->sys_regs)) {
  101. pr_err("Failed to find syscon regs for '%s'\n",
  102. of_node_full_name(data->np));
  103. return PTR_ERR(data->sys_regs);
  104. }
  105. return 0;
  106. }
  107. static struct clk_hw *ccu_pll_of_clk_hw_get(struct of_phandle_args *clkspec,
  108. void *priv)
  109. {
  110. struct ccu_pll_data *data = priv;
  111. struct ccu_pll *pll;
  112. unsigned int clk_id;
  113. clk_id = clkspec->args[0];
  114. pll = ccu_pll_find_desc(data, clk_id);
  115. if (IS_ERR(pll)) {
  116. if (pll != ERR_PTR(-EPROBE_DEFER))
  117. pr_info("Invalid PLL clock ID %d specified\n", clk_id);
  118. return ERR_CAST(pll);
  119. }
  120. return ccu_pll_get_clk_hw(pll);
  121. }
  122. static int ccu_pll_clk_register(struct ccu_pll_data *data, bool defer)
  123. {
  124. int idx, ret;
  125. for (idx = 0; idx < CCU_PLL_NUM; ++idx) {
  126. const struct ccu_pll_info *info = &pll_info[idx];
  127. struct ccu_pll_init_data init = {0};
  128. /* Defer non-basic PLLs allocation for the probe stage */
  129. if (!!(info->features & CCU_PLL_BASIC) ^ defer) {
  130. if (!data->plls[idx])
  131. data->plls[idx] = ERR_PTR(-EPROBE_DEFER);
  132. continue;
  133. }
  134. init.id = info->id;
  135. init.name = info->name;
  136. init.parent_name = info->parent_name;
  137. init.base = info->base;
  138. init.sys_regs = data->sys_regs;
  139. init.np = data->np;
  140. init.flags = info->flags;
  141. init.features = info->features;
  142. data->plls[idx] = ccu_pll_hw_register(&init);
  143. if (IS_ERR(data->plls[idx])) {
  144. ret = PTR_ERR(data->plls[idx]);
  145. pr_err("Couldn't register PLL hw '%s'\n",
  146. init.name);
  147. goto err_hw_unregister;
  148. }
  149. }
  150. return 0;
  151. err_hw_unregister:
  152. for (--idx; idx >= 0; --idx) {
  153. if (!!(pll_info[idx].features & CCU_PLL_BASIC) ^ defer)
  154. continue;
  155. ccu_pll_hw_unregister(data->plls[idx]);
  156. }
  157. return ret;
  158. }
  159. static void ccu_pll_clk_unregister(struct ccu_pll_data *data, bool defer)
  160. {
  161. int idx;
  162. /* Uninstall only the clocks registered on the specfied stage */
  163. for (idx = 0; idx < CCU_PLL_NUM; ++idx) {
  164. if (!!(pll_info[idx].features & CCU_PLL_BASIC) ^ defer)
  165. continue;
  166. ccu_pll_hw_unregister(data->plls[idx]);
  167. }
  168. }
  169. static int ccu_pll_of_register(struct ccu_pll_data *data)
  170. {
  171. int ret;
  172. ret = of_clk_add_hw_provider(data->np, ccu_pll_of_clk_hw_get, data);
  173. if (ret) {
  174. pr_err("Couldn't register PLL provider of '%s'\n",
  175. of_node_full_name(data->np));
  176. }
  177. return ret;
  178. }
  179. static int ccu_pll_probe(struct platform_device *pdev)
  180. {
  181. struct ccu_pll_data *data = pll_data;
  182. if (!data)
  183. return -EINVAL;
  184. return ccu_pll_clk_register(data, false);
  185. }
  186. static const struct of_device_id ccu_pll_of_match[] = {
  187. { .compatible = "baikal,bt1-ccu-pll" },
  188. { }
  189. };
  190. static struct platform_driver ccu_pll_driver = {
  191. .probe = ccu_pll_probe,
  192. .driver = {
  193. .name = "clk-ccu-pll",
  194. .of_match_table = ccu_pll_of_match,
  195. .suppress_bind_attrs = true,
  196. },
  197. };
  198. builtin_platform_driver(ccu_pll_driver);
  199. static __init void ccu_pll_init(struct device_node *np)
  200. {
  201. struct ccu_pll_data *data;
  202. int ret;
  203. data = ccu_pll_create_data(np);
  204. if (IS_ERR(data))
  205. return;
  206. ret = ccu_pll_find_sys_regs(data);
  207. if (ret)
  208. goto err_free_data;
  209. ret = ccu_pll_clk_register(data, true);
  210. if (ret)
  211. goto err_free_data;
  212. ret = ccu_pll_of_register(data);
  213. if (ret)
  214. goto err_clk_unregister;
  215. pll_data = data;
  216. return;
  217. err_clk_unregister:
  218. ccu_pll_clk_unregister(data, true);
  219. err_free_data:
  220. ccu_pll_free_data(data);
  221. }
  222. CLK_OF_DECLARE_DRIVER(ccu_pll, "baikal,bt1-ccu-pll", ccu_pll_init);