krait-cc.c 10 KB

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