sun50i-cpufreq-nvmem.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Allwinner CPUFreq nvmem based driver
  4. *
  5. * The sun50i-cpufreq-nvmem driver reads the efuse value from the SoC to
  6. * provide the OPP framework with required information.
  7. *
  8. * Copyright (C) 2019 Yangtao Li <tiny.windzz@gmail.com>
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/arm-smccc.h>
  12. #include <linux/cpu.h>
  13. #include <linux/module.h>
  14. #include <linux/nvmem-consumer.h>
  15. #include <linux/of.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm_opp.h>
  18. #include <linux/slab.h>
  19. #define NVMEM_MASK 0x7
  20. #define NVMEM_SHIFT 5
  21. static struct platform_device *cpufreq_dt_pdev, *sun50i_cpufreq_pdev;
  22. struct sunxi_cpufreq_data {
  23. u32 (*efuse_xlate)(u32 speedbin);
  24. };
  25. static u32 sun50i_h6_efuse_xlate(u32 speedbin)
  26. {
  27. u32 efuse_value;
  28. efuse_value = (speedbin >> NVMEM_SHIFT) & NVMEM_MASK;
  29. /*
  30. * We treat unexpected efuse values as if the SoC was from
  31. * the slowest bin. Expected efuse values are 1-3, slowest
  32. * to fastest.
  33. */
  34. if (efuse_value >= 1 && efuse_value <= 3)
  35. return efuse_value - 1;
  36. else
  37. return 0;
  38. }
  39. static int get_soc_id_revision(void)
  40. {
  41. #ifdef CONFIG_HAVE_ARM_SMCCC_DISCOVERY
  42. return arm_smccc_get_soc_id_revision();
  43. #else
  44. return SMCCC_RET_NOT_SUPPORTED;
  45. #endif
  46. }
  47. /*
  48. * Judging by the OPP tables in the vendor BSP, the quality order of the
  49. * returned speedbin index is 4 -> 0/2 -> 3 -> 1, from worst to best.
  50. * 0 and 2 seem identical from the OPP tables' point of view.
  51. */
  52. static u32 sun50i_h616_efuse_xlate(u32 speedbin)
  53. {
  54. int ver_bits = get_soc_id_revision();
  55. u32 value = 0;
  56. switch (speedbin & 0xffff) {
  57. case 0x2000:
  58. value = 0;
  59. break;
  60. case 0x2400:
  61. case 0x7400:
  62. case 0x2c00:
  63. case 0x7c00:
  64. if (ver_bits != SMCCC_RET_NOT_SUPPORTED && ver_bits <= 1) {
  65. /* ic version A/B */
  66. value = 1;
  67. } else {
  68. /* ic version C and later version */
  69. value = 2;
  70. }
  71. break;
  72. case 0x5000:
  73. case 0x5400:
  74. case 0x6000:
  75. value = 3;
  76. break;
  77. case 0x5c00:
  78. value = 4;
  79. break;
  80. case 0x5d00:
  81. value = 0;
  82. break;
  83. case 0x6c00:
  84. value = 5;
  85. break;
  86. default:
  87. pr_warn("sun50i-cpufreq-nvmem: unknown speed bin 0x%x, using default bin 0\n",
  88. speedbin & 0xffff);
  89. value = 0;
  90. break;
  91. }
  92. return value;
  93. }
  94. static struct sunxi_cpufreq_data sun50i_h6_cpufreq_data = {
  95. .efuse_xlate = sun50i_h6_efuse_xlate,
  96. };
  97. static struct sunxi_cpufreq_data sun50i_h616_cpufreq_data = {
  98. .efuse_xlate = sun50i_h616_efuse_xlate,
  99. };
  100. static const struct of_device_id cpu_opp_match_list[] = {
  101. { .compatible = "allwinner,sun50i-h6-operating-points",
  102. .data = &sun50i_h6_cpufreq_data,
  103. },
  104. { .compatible = "allwinner,sun50i-h616-operating-points",
  105. .data = &sun50i_h616_cpufreq_data,
  106. },
  107. {}
  108. };
  109. /**
  110. * dt_has_supported_hw() - Check if any OPPs use opp-supported-hw
  111. *
  112. * If we ask the cpufreq framework to use the opp-supported-hw feature, it
  113. * will ignore every OPP node without that DT property. If none of the OPPs
  114. * have it, the driver will fail probing, due to the lack of OPPs.
  115. *
  116. * Returns true if we have at least one OPP with the opp-supported-hw property.
  117. */
  118. static bool dt_has_supported_hw(void)
  119. {
  120. bool has_opp_supported_hw = false;
  121. struct device *cpu_dev;
  122. cpu_dev = get_cpu_device(0);
  123. if (!cpu_dev)
  124. return false;
  125. struct device_node *np __free(device_node) =
  126. dev_pm_opp_of_get_opp_desc_node(cpu_dev);
  127. if (!np)
  128. return false;
  129. for_each_child_of_node_scoped(np, opp) {
  130. if (of_property_present(opp, "opp-supported-hw")) {
  131. has_opp_supported_hw = true;
  132. break;
  133. }
  134. }
  135. return has_opp_supported_hw;
  136. }
  137. /**
  138. * sun50i_cpufreq_get_efuse() - Determine speed grade from efuse value
  139. *
  140. * Returns non-negative speed bin index on success, a negative error
  141. * value otherwise.
  142. */
  143. static int sun50i_cpufreq_get_efuse(void)
  144. {
  145. const struct sunxi_cpufreq_data *opp_data;
  146. struct nvmem_cell *speedbin_nvmem;
  147. const struct of_device_id *match;
  148. struct device *cpu_dev;
  149. u32 *speedbin;
  150. int ret;
  151. cpu_dev = get_cpu_device(0);
  152. if (!cpu_dev)
  153. return -ENODEV;
  154. struct device_node *np __free(device_node) =
  155. dev_pm_opp_of_get_opp_desc_node(cpu_dev);
  156. if (!np)
  157. return -ENOENT;
  158. match = of_match_node(cpu_opp_match_list, np);
  159. if (!match)
  160. return -ENOENT;
  161. opp_data = match->data;
  162. speedbin_nvmem = of_nvmem_cell_get(np, NULL);
  163. if (IS_ERR(speedbin_nvmem))
  164. return dev_err_probe(cpu_dev, PTR_ERR(speedbin_nvmem),
  165. "Could not get nvmem cell\n");
  166. speedbin = nvmem_cell_read(speedbin_nvmem, NULL);
  167. nvmem_cell_put(speedbin_nvmem);
  168. if (IS_ERR(speedbin))
  169. return PTR_ERR(speedbin);
  170. ret = opp_data->efuse_xlate(*speedbin);
  171. kfree(speedbin);
  172. return ret;
  173. };
  174. static int sun50i_cpufreq_nvmem_probe(struct platform_device *pdev)
  175. {
  176. int *opp_tokens;
  177. char name[] = "speedXXXXXXXXXXX"; /* Integers can take 11 chars max */
  178. unsigned int cpu, supported_hw;
  179. struct dev_pm_opp_config config = {};
  180. int speed;
  181. int ret;
  182. opp_tokens = kcalloc(num_possible_cpus(), sizeof(*opp_tokens),
  183. GFP_KERNEL);
  184. if (!opp_tokens)
  185. return -ENOMEM;
  186. speed = sun50i_cpufreq_get_efuse();
  187. if (speed < 0) {
  188. kfree(opp_tokens);
  189. return speed;
  190. }
  191. /*
  192. * We need at least one OPP with the "opp-supported-hw" property,
  193. * or else the upper layers will ignore every OPP and will bail out.
  194. */
  195. if (dt_has_supported_hw()) {
  196. supported_hw = 1U << speed;
  197. config.supported_hw = &supported_hw;
  198. config.supported_hw_count = 1;
  199. }
  200. snprintf(name, sizeof(name), "speed%d", speed);
  201. config.prop_name = name;
  202. for_each_possible_cpu(cpu) {
  203. struct device *cpu_dev = get_cpu_device(cpu);
  204. if (!cpu_dev) {
  205. ret = -ENODEV;
  206. goto free_opp;
  207. }
  208. ret = dev_pm_opp_set_config(cpu_dev, &config);
  209. if (ret < 0)
  210. goto free_opp;
  211. opp_tokens[cpu] = ret;
  212. }
  213. cpufreq_dt_pdev = platform_device_register_simple("cpufreq-dt", -1,
  214. NULL, 0);
  215. if (!IS_ERR(cpufreq_dt_pdev)) {
  216. platform_set_drvdata(pdev, opp_tokens);
  217. return 0;
  218. }
  219. ret = PTR_ERR(cpufreq_dt_pdev);
  220. pr_err("Failed to register platform device\n");
  221. free_opp:
  222. for_each_possible_cpu(cpu)
  223. dev_pm_opp_clear_config(opp_tokens[cpu]);
  224. kfree(opp_tokens);
  225. return ret;
  226. }
  227. static void sun50i_cpufreq_nvmem_remove(struct platform_device *pdev)
  228. {
  229. int *opp_tokens = platform_get_drvdata(pdev);
  230. unsigned int cpu;
  231. platform_device_unregister(cpufreq_dt_pdev);
  232. for_each_possible_cpu(cpu)
  233. dev_pm_opp_clear_config(opp_tokens[cpu]);
  234. kfree(opp_tokens);
  235. }
  236. static struct platform_driver sun50i_cpufreq_driver = {
  237. .probe = sun50i_cpufreq_nvmem_probe,
  238. .remove_new = sun50i_cpufreq_nvmem_remove,
  239. .driver = {
  240. .name = "sun50i-cpufreq-nvmem",
  241. },
  242. };
  243. static const struct of_device_id sun50i_cpufreq_match_list[] = {
  244. { .compatible = "allwinner,sun50i-h6" },
  245. { .compatible = "allwinner,sun50i-h616" },
  246. { .compatible = "allwinner,sun50i-h618" },
  247. { .compatible = "allwinner,sun50i-h700" },
  248. {}
  249. };
  250. MODULE_DEVICE_TABLE(of, sun50i_cpufreq_match_list);
  251. static const struct of_device_id *sun50i_cpufreq_match_node(void)
  252. {
  253. struct device_node *np __free(device_node) = of_find_node_by_path("/");
  254. return of_match_node(sun50i_cpufreq_match_list, np);
  255. }
  256. /*
  257. * Since the driver depends on nvmem drivers, which may return EPROBE_DEFER,
  258. * all the real activity is done in the probe, which may be defered as well.
  259. * The init here is only registering the driver and the platform device.
  260. */
  261. static int __init sun50i_cpufreq_init(void)
  262. {
  263. const struct of_device_id *match;
  264. int ret;
  265. match = sun50i_cpufreq_match_node();
  266. if (!match)
  267. return -ENODEV;
  268. ret = platform_driver_register(&sun50i_cpufreq_driver);
  269. if (unlikely(ret < 0))
  270. return ret;
  271. sun50i_cpufreq_pdev =
  272. platform_device_register_simple("sun50i-cpufreq-nvmem",
  273. -1, NULL, 0);
  274. ret = PTR_ERR_OR_ZERO(sun50i_cpufreq_pdev);
  275. if (ret == 0)
  276. return 0;
  277. platform_driver_unregister(&sun50i_cpufreq_driver);
  278. return ret;
  279. }
  280. module_init(sun50i_cpufreq_init);
  281. static void __exit sun50i_cpufreq_exit(void)
  282. {
  283. platform_device_unregister(sun50i_cpufreq_pdev);
  284. platform_driver_unregister(&sun50i_cpufreq_driver);
  285. }
  286. module_exit(sun50i_cpufreq_exit);
  287. MODULE_DESCRIPTION("Sun50i-h6 cpufreq driver");
  288. MODULE_LICENSE("GPL v2");