krait-cc.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2018, The Linux Foundation. All rights reserved.
  3. #include <linux/kernel.h>
  4. #include <linux/init.h>
  5. #include <linux/module.h>
  6. #include <linux/platform_device.h>
  7. #include <linux/err.h>
  8. #include <linux/io.h>
  9. #include <linux/of.h>
  10. #include <linux/of_device.h>
  11. #include <linux/clk.h>
  12. #include <linux/clk-provider.h>
  13. #include <linux/slab.h>
  14. #include "clk-krait.h"
  15. static unsigned int sec_mux_map[] = {
  16. 2,
  17. 0,
  18. };
  19. static unsigned int pri_mux_map[] = {
  20. 1,
  21. 2,
  22. 0,
  23. };
  24. /*
  25. * Notifier function for switching the muxes to safe parent
  26. * while the hfpll is getting reprogrammed.
  27. */
  28. static int krait_notifier_cb(struct notifier_block *nb,
  29. unsigned long event,
  30. void *data)
  31. {
  32. int ret = 0;
  33. struct krait_mux_clk *mux = container_of(nb, struct krait_mux_clk,
  34. clk_nb);
  35. /* Switch to safe parent */
  36. if (event == PRE_RATE_CHANGE) {
  37. mux->old_index = krait_mux_clk_ops.get_parent(&mux->hw);
  38. ret = krait_mux_clk_ops.set_parent(&mux->hw, mux->safe_sel);
  39. mux->reparent = false;
  40. /*
  41. * By the time POST_RATE_CHANGE notifier is called,
  42. * clk framework itself would have changed the parent for the new rate.
  43. * Only otherwise, put back to the old parent.
  44. */
  45. } else if (event == POST_RATE_CHANGE) {
  46. if (!mux->reparent)
  47. ret = krait_mux_clk_ops.set_parent(&mux->hw,
  48. mux->old_index);
  49. }
  50. return notifier_from_errno(ret);
  51. }
  52. static int krait_notifier_register(struct device *dev, struct clk *clk,
  53. struct krait_mux_clk *mux)
  54. {
  55. int ret = 0;
  56. mux->clk_nb.notifier_call = krait_notifier_cb;
  57. ret = clk_notifier_register(clk, &mux->clk_nb);
  58. if (ret)
  59. dev_err(dev, "failed to register clock notifier: %d\n", ret);
  60. return ret;
  61. }
  62. static int
  63. krait_add_div(struct device *dev, int id, const char *s, unsigned int offset)
  64. {
  65. struct krait_div2_clk *div;
  66. struct clk_init_data init = {
  67. .num_parents = 1,
  68. .ops = &krait_div2_clk_ops,
  69. .flags = CLK_SET_RATE_PARENT,
  70. };
  71. const char *p_names[1];
  72. struct clk *clk;
  73. div = devm_kzalloc(dev, sizeof(*div), GFP_KERNEL);
  74. if (!div)
  75. return -ENOMEM;
  76. div->width = 2;
  77. div->shift = 6;
  78. div->lpl = id >= 0;
  79. div->offset = offset;
  80. div->hw.init = &init;
  81. init.name = kasprintf(GFP_KERNEL, "hfpll%s_div", s);
  82. if (!init.name)
  83. return -ENOMEM;
  84. init.parent_names = p_names;
  85. p_names[0] = kasprintf(GFP_KERNEL, "hfpll%s", s);
  86. if (!p_names[0]) {
  87. kfree(init.name);
  88. return -ENOMEM;
  89. }
  90. clk = devm_clk_register(dev, &div->hw);
  91. kfree(p_names[0]);
  92. kfree(init.name);
  93. return PTR_ERR_OR_ZERO(clk);
  94. }
  95. static int
  96. krait_add_sec_mux(struct device *dev, int id, const char *s,
  97. unsigned int offset, bool unique_aux)
  98. {
  99. int ret;
  100. struct krait_mux_clk *mux;
  101. static const char *sec_mux_list[] = {
  102. "acpu_aux",
  103. "qsb",
  104. };
  105. struct clk_init_data init = {
  106. .parent_names = sec_mux_list,
  107. .num_parents = ARRAY_SIZE(sec_mux_list),
  108. .ops = &krait_mux_clk_ops,
  109. .flags = CLK_SET_RATE_PARENT,
  110. };
  111. struct clk *clk;
  112. mux = devm_kzalloc(dev, sizeof(*mux), GFP_KERNEL);
  113. if (!mux)
  114. return -ENOMEM;
  115. mux->offset = offset;
  116. mux->lpl = id >= 0;
  117. mux->mask = 0x3;
  118. mux->shift = 2;
  119. mux->parent_map = sec_mux_map;
  120. mux->hw.init = &init;
  121. mux->safe_sel = 0;
  122. /* Checking for qcom,krait-cc-v1 or qcom,krait-cc-v2 is not
  123. * enough to limit this to apq/ipq8064. Directly check machine
  124. * compatible to correctly handle this errata.
  125. */
  126. if (of_machine_is_compatible("qcom,ipq8064") ||
  127. of_machine_is_compatible("qcom,apq8064"))
  128. mux->disable_sec_src_gating = true;
  129. init.name = kasprintf(GFP_KERNEL, "krait%s_sec_mux", s);
  130. if (!init.name)
  131. return -ENOMEM;
  132. if (unique_aux) {
  133. sec_mux_list[0] = kasprintf(GFP_KERNEL, "acpu%s_aux", s);
  134. if (!sec_mux_list[0]) {
  135. clk = ERR_PTR(-ENOMEM);
  136. goto err_aux;
  137. }
  138. }
  139. clk = devm_clk_register(dev, &mux->hw);
  140. ret = krait_notifier_register(dev, clk, mux);
  141. if (ret)
  142. goto unique_aux;
  143. unique_aux:
  144. if (unique_aux)
  145. kfree(sec_mux_list[0]);
  146. err_aux:
  147. kfree(init.name);
  148. return PTR_ERR_OR_ZERO(clk);
  149. }
  150. static struct clk *
  151. krait_add_pri_mux(struct device *dev, int id, const char *s,
  152. unsigned int offset)
  153. {
  154. int ret;
  155. struct krait_mux_clk *mux;
  156. const char *p_names[3];
  157. struct clk_init_data init = {
  158. .parent_names = p_names,
  159. .num_parents = ARRAY_SIZE(p_names),
  160. .ops = &krait_mux_clk_ops,
  161. .flags = CLK_SET_RATE_PARENT,
  162. };
  163. struct clk *clk;
  164. mux = devm_kzalloc(dev, sizeof(*mux), GFP_KERNEL);
  165. if (!mux)
  166. return ERR_PTR(-ENOMEM);
  167. mux->mask = 0x3;
  168. mux->shift = 0;
  169. mux->offset = offset;
  170. mux->lpl = id >= 0;
  171. mux->parent_map = pri_mux_map;
  172. mux->hw.init = &init;
  173. mux->safe_sel = 2;
  174. init.name = kasprintf(GFP_KERNEL, "krait%s_pri_mux", s);
  175. if (!init.name)
  176. return ERR_PTR(-ENOMEM);
  177. p_names[0] = kasprintf(GFP_KERNEL, "hfpll%s", s);
  178. if (!p_names[0]) {
  179. clk = ERR_PTR(-ENOMEM);
  180. goto err_p0;
  181. }
  182. p_names[1] = kasprintf(GFP_KERNEL, "hfpll%s_div", s);
  183. if (!p_names[1]) {
  184. clk = ERR_PTR(-ENOMEM);
  185. goto err_p1;
  186. }
  187. p_names[2] = kasprintf(GFP_KERNEL, "krait%s_sec_mux", s);
  188. if (!p_names[2]) {
  189. clk = ERR_PTR(-ENOMEM);
  190. goto err_p2;
  191. }
  192. clk = devm_clk_register(dev, &mux->hw);
  193. ret = krait_notifier_register(dev, clk, mux);
  194. if (ret)
  195. goto err_p3;
  196. err_p3:
  197. kfree(p_names[2]);
  198. err_p2:
  199. kfree(p_names[1]);
  200. err_p1:
  201. kfree(p_names[0]);
  202. err_p0:
  203. kfree(init.name);
  204. return clk;
  205. }
  206. /* id < 0 for L2, otherwise id == physical CPU number */
  207. static struct clk *krait_add_clks(struct device *dev, int id, bool unique_aux)
  208. {
  209. int ret;
  210. unsigned int offset;
  211. void *p = NULL;
  212. const char *s;
  213. struct clk *clk;
  214. if (id >= 0) {
  215. offset = 0x4501 + (0x1000 * id);
  216. s = p = kasprintf(GFP_KERNEL, "%d", id);
  217. if (!s)
  218. return ERR_PTR(-ENOMEM);
  219. } else {
  220. offset = 0x500;
  221. s = "_l2";
  222. }
  223. ret = krait_add_div(dev, id, s, offset);
  224. if (ret) {
  225. clk = ERR_PTR(ret);
  226. goto err;
  227. }
  228. ret = krait_add_sec_mux(dev, id, s, offset, unique_aux);
  229. if (ret) {
  230. clk = ERR_PTR(ret);
  231. goto err;
  232. }
  233. clk = krait_add_pri_mux(dev, id, s, offset);
  234. err:
  235. kfree(p);
  236. return clk;
  237. }
  238. static struct clk *krait_of_get(struct of_phandle_args *clkspec, void *data)
  239. {
  240. unsigned int idx = clkspec->args[0];
  241. struct clk **clks = data;
  242. if (idx >= 5) {
  243. pr_err("%s: invalid clock index %d\n", __func__, idx);
  244. return ERR_PTR(-EINVAL);
  245. }
  246. return clks[idx] ? : ERR_PTR(-ENODEV);
  247. }
  248. static const struct of_device_id krait_cc_match_table[] = {
  249. { .compatible = "qcom,krait-cc-v1", (void *)1UL },
  250. { .compatible = "qcom,krait-cc-v2" },
  251. {}
  252. };
  253. MODULE_DEVICE_TABLE(of, krait_cc_match_table);
  254. static int krait_cc_probe(struct platform_device *pdev)
  255. {
  256. struct device *dev = &pdev->dev;
  257. const struct of_device_id *id;
  258. unsigned long cur_rate, aux_rate;
  259. int cpu;
  260. struct clk *clk;
  261. struct clk **clks;
  262. struct clk *l2_pri_mux_clk;
  263. id = of_match_device(krait_cc_match_table, dev);
  264. if (!id)
  265. return -ENODEV;
  266. /* Rate is 1 because 0 causes problems for __clk_mux_determine_rate */
  267. clk = clk_register_fixed_rate(dev, "qsb", NULL, 0, 1);
  268. if (IS_ERR(clk))
  269. return PTR_ERR(clk);
  270. if (!id->data) {
  271. clk = clk_register_fixed_factor(dev, "acpu_aux",
  272. "gpll0_vote", 0, 1, 2);
  273. if (IS_ERR(clk))
  274. return PTR_ERR(clk);
  275. }
  276. /* Krait configurations have at most 4 CPUs and one L2 */
  277. clks = devm_kcalloc(dev, 5, sizeof(*clks), GFP_KERNEL);
  278. if (!clks)
  279. return -ENOMEM;
  280. for_each_possible_cpu(cpu) {
  281. clk = krait_add_clks(dev, cpu, id->data);
  282. if (IS_ERR(clk))
  283. return PTR_ERR(clk);
  284. clks[cpu] = clk;
  285. }
  286. l2_pri_mux_clk = krait_add_clks(dev, -1, id->data);
  287. if (IS_ERR(l2_pri_mux_clk))
  288. return PTR_ERR(l2_pri_mux_clk);
  289. clks[4] = l2_pri_mux_clk;
  290. /*
  291. * We don't want the CPU or L2 clocks to be turned off at late init
  292. * if CPUFREQ or HOTPLUG configs are disabled. So, bump up the
  293. * refcount of these clocks. Any cpufreq/hotplug manager can assume
  294. * that the clocks have already been prepared and enabled by the time
  295. * they take over.
  296. */
  297. for_each_online_cpu(cpu) {
  298. clk_prepare_enable(l2_pri_mux_clk);
  299. WARN(clk_prepare_enable(clks[cpu]),
  300. "Unable to turn on CPU%d clock", cpu);
  301. }
  302. /*
  303. * Force reinit of HFPLLs and muxes to overwrite any potential
  304. * incorrect configuration of HFPLLs and muxes by the bootloader.
  305. * While at it, also make sure the cores are running at known rates
  306. * and print the current rate.
  307. *
  308. * The clocks are set to aux clock rate first to make sure the
  309. * secondary mux is not sourcing off of QSB. The rate is then set to
  310. * two different rates to force a HFPLL reinit under all
  311. * circumstances.
  312. */
  313. cur_rate = clk_get_rate(l2_pri_mux_clk);
  314. aux_rate = 384000000;
  315. if (cur_rate == 1) {
  316. pr_info("L2 @ QSB rate. Forcing new rate.\n");
  317. cur_rate = aux_rate;
  318. }
  319. clk_set_rate(l2_pri_mux_clk, aux_rate);
  320. clk_set_rate(l2_pri_mux_clk, 2);
  321. clk_set_rate(l2_pri_mux_clk, cur_rate);
  322. pr_info("L2 @ %lu KHz\n", clk_get_rate(l2_pri_mux_clk) / 1000);
  323. for_each_possible_cpu(cpu) {
  324. clk = clks[cpu];
  325. cur_rate = clk_get_rate(clk);
  326. if (cur_rate == 1) {
  327. pr_info("CPU%d @ QSB rate. Forcing new rate.\n", cpu);
  328. cur_rate = aux_rate;
  329. }
  330. clk_set_rate(clk, aux_rate);
  331. clk_set_rate(clk, 2);
  332. clk_set_rate(clk, cur_rate);
  333. pr_info("CPU%d @ %lu KHz\n", cpu, clk_get_rate(clk) / 1000);
  334. }
  335. of_clk_add_provider(dev->of_node, krait_of_get, clks);
  336. return 0;
  337. }
  338. static struct platform_driver krait_cc_driver = {
  339. .probe = krait_cc_probe,
  340. .driver = {
  341. .name = "krait-cc",
  342. .of_match_table = krait_cc_match_table,
  343. },
  344. };
  345. module_platform_driver(krait_cc_driver);
  346. MODULE_DESCRIPTION("Krait CPU Clock Driver");
  347. MODULE_LICENSE("GPL v2");
  348. MODULE_ALIAS("platform:krait-cc");