qcom-cpufreq-nvmem.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2018, The Linux Foundation. All rights reserved.
  4. */
  5. /*
  6. * In Certain QCOM SoCs like apq8096 and msm8996 that have KRYO processors,
  7. * the CPU frequency subset and voltage value of each OPP varies
  8. * based on the silicon variant in use. Qualcomm Process Voltage Scaling Tables
  9. * defines the voltage and frequency value based on the msm-id in SMEM
  10. * and speedbin blown in the efuse combination.
  11. * The qcom-cpufreq-nvmem driver reads the msm-id and efuse value from the SoC
  12. * to provide the OPP framework with required information.
  13. * This is used to determine the voltage and frequency value for each OPP of
  14. * operating-points-v2 table when it is parsed by the OPP framework.
  15. */
  16. #include <linux/cpu.h>
  17. #include <linux/err.h>
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/nvmem-consumer.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/pm_domain.h>
  26. #include <linux/pm_opp.h>
  27. #include <linux/slab.h>
  28. #include <linux/soc/qcom/smem.h>
  29. #define MSM_ID_SMEM 137
  30. enum _msm_id {
  31. MSM8996V3 = 0xF6ul,
  32. APQ8096V3 = 0x123ul,
  33. MSM8996SG = 0x131ul,
  34. APQ8096SG = 0x138ul,
  35. };
  36. enum _msm8996_version {
  37. MSM8996_V3,
  38. MSM8996_SG,
  39. NUM_OF_MSM8996_VERSIONS,
  40. };
  41. struct qcom_cpufreq_drv;
  42. struct qcom_cpufreq_match_data {
  43. int (*get_version)(struct device *cpu_dev,
  44. struct nvmem_cell *speedbin_nvmem,
  45. char **pvs_name,
  46. struct qcom_cpufreq_drv *drv);
  47. const char **genpd_names;
  48. };
  49. struct qcom_cpufreq_drv {
  50. int *opp_tokens;
  51. u32 versions;
  52. const struct qcom_cpufreq_match_data *data;
  53. };
  54. static struct platform_device *cpufreq_dt_pdev, *cpufreq_pdev;
  55. static void get_krait_bin_format_a(struct device *cpu_dev,
  56. int *speed, int *pvs, int *pvs_ver,
  57. u8 *buf)
  58. {
  59. u32 pte_efuse;
  60. pte_efuse = *((u32 *)buf);
  61. *speed = pte_efuse & 0xf;
  62. if (*speed == 0xf)
  63. *speed = (pte_efuse >> 4) & 0xf;
  64. if (*speed == 0xf) {
  65. *speed = 0;
  66. dev_warn(cpu_dev, "Speed bin: Defaulting to %d\n", *speed);
  67. } else {
  68. dev_dbg(cpu_dev, "Speed bin: %d\n", *speed);
  69. }
  70. *pvs = (pte_efuse >> 10) & 0x7;
  71. if (*pvs == 0x7)
  72. *pvs = (pte_efuse >> 13) & 0x7;
  73. if (*pvs == 0x7) {
  74. *pvs = 0;
  75. dev_warn(cpu_dev, "PVS bin: Defaulting to %d\n", *pvs);
  76. } else {
  77. dev_dbg(cpu_dev, "PVS bin: %d\n", *pvs);
  78. }
  79. }
  80. static void get_krait_bin_format_b(struct device *cpu_dev,
  81. int *speed, int *pvs, int *pvs_ver,
  82. u8 *buf)
  83. {
  84. u32 pte_efuse, redundant_sel;
  85. pte_efuse = *((u32 *)buf);
  86. redundant_sel = (pte_efuse >> 24) & 0x7;
  87. *pvs_ver = (pte_efuse >> 4) & 0x3;
  88. switch (redundant_sel) {
  89. case 1:
  90. *pvs = ((pte_efuse >> 28) & 0x8) | ((pte_efuse >> 6) & 0x7);
  91. *speed = (pte_efuse >> 27) & 0xf;
  92. break;
  93. case 2:
  94. *pvs = (pte_efuse >> 27) & 0xf;
  95. *speed = pte_efuse & 0x7;
  96. break;
  97. default:
  98. /* 4 bits of PVS are in efuse register bits 31, 8-6. */
  99. *pvs = ((pte_efuse >> 28) & 0x8) | ((pte_efuse >> 6) & 0x7);
  100. *speed = pte_efuse & 0x7;
  101. }
  102. /* Check SPEED_BIN_BLOW_STATUS */
  103. if (pte_efuse & BIT(3)) {
  104. dev_dbg(cpu_dev, "Speed bin: %d\n", *speed);
  105. } else {
  106. dev_warn(cpu_dev, "Speed bin not set. Defaulting to 0!\n");
  107. *speed = 0;
  108. }
  109. /* Check PVS_BLOW_STATUS */
  110. pte_efuse = *(((u32 *)buf) + 1);
  111. pte_efuse &= BIT(21);
  112. if (pte_efuse) {
  113. dev_dbg(cpu_dev, "PVS bin: %d\n", *pvs);
  114. } else {
  115. dev_warn(cpu_dev, "PVS bin not set. Defaulting to 0!\n");
  116. *pvs = 0;
  117. }
  118. dev_dbg(cpu_dev, "PVS version: %d\n", *pvs_ver);
  119. }
  120. static enum _msm8996_version qcom_cpufreq_get_msm_id(void)
  121. {
  122. size_t len;
  123. u32 *msm_id;
  124. enum _msm8996_version version;
  125. msm_id = qcom_smem_get(QCOM_SMEM_HOST_ANY, MSM_ID_SMEM, &len);
  126. if (IS_ERR(msm_id))
  127. return NUM_OF_MSM8996_VERSIONS;
  128. /* The first 4 bytes are format, next to them is the actual msm-id */
  129. msm_id++;
  130. switch ((enum _msm_id)*msm_id) {
  131. case MSM8996V3:
  132. case APQ8096V3:
  133. version = MSM8996_V3;
  134. break;
  135. case MSM8996SG:
  136. case APQ8096SG:
  137. version = MSM8996_SG;
  138. break;
  139. default:
  140. version = NUM_OF_MSM8996_VERSIONS;
  141. }
  142. return version;
  143. }
  144. static int qcom_cpufreq_kryo_name_version(struct device *cpu_dev,
  145. struct nvmem_cell *speedbin_nvmem,
  146. char **pvs_name,
  147. struct qcom_cpufreq_drv *drv)
  148. {
  149. size_t len;
  150. u8 *speedbin;
  151. enum _msm8996_version msm8996_version;
  152. *pvs_name = NULL;
  153. msm8996_version = qcom_cpufreq_get_msm_id();
  154. if (NUM_OF_MSM8996_VERSIONS == msm8996_version) {
  155. dev_err(cpu_dev, "Not Snapdragon 820/821!");
  156. return -ENODEV;
  157. }
  158. speedbin = nvmem_cell_read(speedbin_nvmem, &len);
  159. if (IS_ERR(speedbin))
  160. return PTR_ERR(speedbin);
  161. switch (msm8996_version) {
  162. case MSM8996_V3:
  163. drv->versions = 1 << (unsigned int)(*speedbin);
  164. break;
  165. case MSM8996_SG:
  166. drv->versions = 1 << ((unsigned int)(*speedbin) + 4);
  167. break;
  168. default:
  169. BUG();
  170. break;
  171. }
  172. kfree(speedbin);
  173. return 0;
  174. }
  175. static int qcom_cpufreq_krait_name_version(struct device *cpu_dev,
  176. struct nvmem_cell *speedbin_nvmem,
  177. char **pvs_name,
  178. struct qcom_cpufreq_drv *drv)
  179. {
  180. int speed = 0, pvs = 0, pvs_ver = 0;
  181. u8 *speedbin;
  182. size_t len;
  183. int ret = 0;
  184. speedbin = nvmem_cell_read(speedbin_nvmem, &len);
  185. if (IS_ERR(speedbin))
  186. return PTR_ERR(speedbin);
  187. switch (len) {
  188. case 4:
  189. get_krait_bin_format_a(cpu_dev, &speed, &pvs, &pvs_ver,
  190. speedbin);
  191. break;
  192. case 8:
  193. get_krait_bin_format_b(cpu_dev, &speed, &pvs, &pvs_ver,
  194. speedbin);
  195. break;
  196. default:
  197. dev_err(cpu_dev, "Unable to read nvmem data. Defaulting to 0!\n");
  198. ret = -ENODEV;
  199. goto len_error;
  200. }
  201. snprintf(*pvs_name, sizeof("speedXX-pvsXX-vXX"), "speed%d-pvs%d-v%d",
  202. speed, pvs, pvs_ver);
  203. drv->versions = (1 << speed);
  204. len_error:
  205. kfree(speedbin);
  206. return ret;
  207. }
  208. static const struct qcom_cpufreq_match_data match_data_kryo = {
  209. .get_version = qcom_cpufreq_kryo_name_version,
  210. };
  211. static const struct qcom_cpufreq_match_data match_data_krait = {
  212. .get_version = qcom_cpufreq_krait_name_version,
  213. };
  214. static const char *qcs404_genpd_names[] = { "cpr", NULL };
  215. static const struct qcom_cpufreq_match_data match_data_qcs404 = {
  216. .genpd_names = qcs404_genpd_names,
  217. };
  218. static int qcom_cpufreq_probe(struct platform_device *pdev)
  219. {
  220. struct qcom_cpufreq_drv *drv;
  221. struct nvmem_cell *speedbin_nvmem;
  222. struct device_node *np;
  223. struct device *cpu_dev;
  224. char pvs_name_buffer[] = "speedXX-pvsXX-vXX";
  225. char *pvs_name = pvs_name_buffer;
  226. unsigned cpu;
  227. const struct of_device_id *match;
  228. int ret;
  229. cpu_dev = get_cpu_device(0);
  230. if (!cpu_dev)
  231. return -ENODEV;
  232. np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
  233. if (!np)
  234. return -ENOENT;
  235. ret = of_device_is_compatible(np, "operating-points-v2-kryo-cpu");
  236. if (!ret) {
  237. of_node_put(np);
  238. return -ENOENT;
  239. }
  240. drv = kzalloc(sizeof(*drv), GFP_KERNEL);
  241. if (!drv)
  242. return -ENOMEM;
  243. match = pdev->dev.platform_data;
  244. drv->data = match->data;
  245. if (!drv->data) {
  246. ret = -ENODEV;
  247. goto free_drv;
  248. }
  249. if (drv->data->get_version) {
  250. speedbin_nvmem = of_nvmem_cell_get(np, NULL);
  251. if (IS_ERR(speedbin_nvmem)) {
  252. ret = dev_err_probe(cpu_dev, PTR_ERR(speedbin_nvmem),
  253. "Could not get nvmem cell\n");
  254. goto free_drv;
  255. }
  256. ret = drv->data->get_version(cpu_dev,
  257. speedbin_nvmem, &pvs_name, drv);
  258. if (ret) {
  259. nvmem_cell_put(speedbin_nvmem);
  260. goto free_drv;
  261. }
  262. nvmem_cell_put(speedbin_nvmem);
  263. }
  264. of_node_put(np);
  265. drv->opp_tokens = kcalloc(num_possible_cpus(), sizeof(*drv->opp_tokens),
  266. GFP_KERNEL);
  267. if (!drv->opp_tokens) {
  268. ret = -ENOMEM;
  269. goto free_drv;
  270. }
  271. for_each_possible_cpu(cpu) {
  272. struct dev_pm_opp_config config = {
  273. .supported_hw = NULL,
  274. };
  275. cpu_dev = get_cpu_device(cpu);
  276. if (NULL == cpu_dev) {
  277. ret = -ENODEV;
  278. goto free_opp;
  279. }
  280. if (drv->data->get_version) {
  281. config.supported_hw = &drv->versions;
  282. config.supported_hw_count = 1;
  283. if (pvs_name)
  284. config.prop_name = pvs_name;
  285. }
  286. if (drv->data->genpd_names) {
  287. config.genpd_names = drv->data->genpd_names;
  288. config.virt_devs = NULL;
  289. }
  290. if (config.supported_hw || config.genpd_names) {
  291. drv->opp_tokens[cpu] = dev_pm_opp_set_config(cpu_dev, &config);
  292. if (drv->opp_tokens[cpu] < 0) {
  293. ret = drv->opp_tokens[cpu];
  294. dev_err(cpu_dev, "Failed to set OPP config\n");
  295. goto free_opp;
  296. }
  297. }
  298. }
  299. cpufreq_dt_pdev = platform_device_register_simple("cpufreq-dt", -1,
  300. NULL, 0);
  301. if (!IS_ERR(cpufreq_dt_pdev)) {
  302. platform_set_drvdata(pdev, drv);
  303. return 0;
  304. }
  305. ret = PTR_ERR(cpufreq_dt_pdev);
  306. dev_err(cpu_dev, "Failed to register platform device\n");
  307. free_opp:
  308. for_each_possible_cpu(cpu)
  309. dev_pm_opp_clear_config(drv->opp_tokens[cpu]);
  310. kfree(drv->opp_tokens);
  311. free_drv:
  312. kfree(drv);
  313. return ret;
  314. }
  315. static int qcom_cpufreq_remove(struct platform_device *pdev)
  316. {
  317. struct qcom_cpufreq_drv *drv = platform_get_drvdata(pdev);
  318. unsigned int cpu;
  319. platform_device_unregister(cpufreq_dt_pdev);
  320. for_each_possible_cpu(cpu)
  321. dev_pm_opp_clear_config(drv->opp_tokens[cpu]);
  322. kfree(drv->opp_tokens);
  323. kfree(drv);
  324. return 0;
  325. }
  326. static struct platform_driver qcom_cpufreq_driver = {
  327. .probe = qcom_cpufreq_probe,
  328. .remove = qcom_cpufreq_remove,
  329. .driver = {
  330. .name = "qcom-cpufreq-nvmem",
  331. },
  332. };
  333. static const struct of_device_id qcom_cpufreq_match_list[] __initconst = {
  334. { .compatible = "qcom,apq8096", .data = &match_data_kryo },
  335. { .compatible = "qcom,msm8996", .data = &match_data_kryo },
  336. { .compatible = "qcom,qcs404", .data = &match_data_qcs404 },
  337. { .compatible = "qcom,ipq8064", .data = &match_data_krait },
  338. { .compatible = "qcom,apq8064", .data = &match_data_krait },
  339. { .compatible = "qcom,msm8974", .data = &match_data_krait },
  340. { .compatible = "qcom,msm8960", .data = &match_data_krait },
  341. {},
  342. };
  343. MODULE_DEVICE_TABLE(of, qcom_cpufreq_match_list);
  344. /*
  345. * Since the driver depends on smem and nvmem drivers, which may
  346. * return EPROBE_DEFER, all the real activity is done in the probe,
  347. * which may be defered as well. The init here is only registering
  348. * the driver and the platform device.
  349. */
  350. static int __init qcom_cpufreq_init(void)
  351. {
  352. struct device_node *np = of_find_node_by_path("/");
  353. const struct of_device_id *match;
  354. int ret;
  355. if (!np)
  356. return -ENODEV;
  357. match = of_match_node(qcom_cpufreq_match_list, np);
  358. of_node_put(np);
  359. if (!match)
  360. return -ENODEV;
  361. ret = platform_driver_register(&qcom_cpufreq_driver);
  362. if (unlikely(ret < 0))
  363. return ret;
  364. cpufreq_pdev = platform_device_register_data(NULL, "qcom-cpufreq-nvmem",
  365. -1, match, sizeof(*match));
  366. ret = PTR_ERR_OR_ZERO(cpufreq_pdev);
  367. if (0 == ret)
  368. return 0;
  369. platform_driver_unregister(&qcom_cpufreq_driver);
  370. return ret;
  371. }
  372. module_init(qcom_cpufreq_init);
  373. static void __exit qcom_cpufreq_exit(void)
  374. {
  375. platform_device_unregister(cpufreq_pdev);
  376. platform_driver_unregister(&qcom_cpufreq_driver);
  377. }
  378. module_exit(qcom_cpufreq_exit);
  379. MODULE_DESCRIPTION("Qualcomm Technologies, Inc. CPUfreq driver");
  380. MODULE_LICENSE("GPL v2");