krait-cc.c 10 KB

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