0045-cpufreq-Add-module-to-register-cpufreq-on-Krait-CPUs.patch 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. From patchwork Fri Dec 8 09:42:29 2017
  2. Content-Type: text/plain; charset="utf-8"
  3. MIME-Version: 1.0
  4. Content-Transfer-Encoding: 7bit
  5. Subject: [v4,11/12] cpufreq: Add module to register cpufreq on Krait CPUs
  6. From: Sricharan R <[email protected]>
  7. X-Patchwork-Id: 10102075
  8. Message-Id: <[email protected]>
  9. To: [email protected], [email protected],
  10. [email protected], [email protected],
  11. [email protected], [email protected],
  12. [email protected], [email protected]
  13. Cc: [email protected]
  14. Date: Fri, 8 Dec 2017 15:12:29 +0530
  15. From: Stephen Boyd <[email protected]>
  16. Register a cpufreq-generic device whenever we detect that a
  17. "qcom,krait" compatible CPU is present in DT.
  18. Cc: <[email protected]>
  19. Signed-off-by: Stephen Boyd <[email protected]>
  20. ---
  21. .../devicetree/bindings/arm/msm/qcom,pvs.txt | 38 ++++
  22. drivers/cpufreq/Kconfig.arm | 9 +
  23. drivers/cpufreq/Makefile | 1 +
  24. drivers/cpufreq/qcom-cpufreq.c | 204 +++++++++++++++++++++
  25. 4 files changed, 252 insertions(+)
  26. create mode 100644 Documentation/devicetree/bindings/arm/msm/qcom,pvs.txt
  27. create mode 100644 drivers/cpufreq/qcom-cpufreq.c
  28. --- /dev/null
  29. +++ b/Documentation/devicetree/bindings/arm/msm/qcom,pvs.txt
  30. @@ -0,0 +1,38 @@
  31. +Qualcomm Process Voltage Scaling Tables
  32. +
  33. +The node name is required to be "qcom,pvs". There shall only be one
  34. +such node present in the root of the tree.
  35. +
  36. +PROPERTIES
  37. +
  38. +- qcom,pvs-format-a or qcom,pvs-format-b:
  39. + Usage: required
  40. + Value type: <empty>
  41. + Definition: Indicates the format of qcom,speedX-pvsY-bin-vZ properties.
  42. + If qcom,pvs-format-a is used the table is two columns
  43. + (frequency and voltage in that order). If qcom,pvs-format-b is used the table is three columns (frequency, voltage,
  44. + and current in that order).
  45. +
  46. +- qcom,speedX-pvsY-bin-vZ:
  47. + Usage: required
  48. + Value type: <prop-encoded-array>
  49. + Definition: The PVS table corresponding to the speed bin X, pvs bin Y,
  50. + and version Z.
  51. +Example:
  52. +
  53. + qcom,pvs {
  54. + qcom,pvs-format-a;
  55. + qcom,speed0-pvs0-bin-v0 =
  56. + < 384000000 950000 >,
  57. + < 486000000 975000 >,
  58. + < 594000000 1000000 >,
  59. + < 702000000 1025000 >,
  60. + < 810000000 1075000 >,
  61. + < 918000000 1100000 >,
  62. + < 1026000000 1125000 >,
  63. + < 1134000000 1175000 >,
  64. + < 1242000000 1200000 >,
  65. + < 1350000000 1225000 >,
  66. + < 1458000000 1237500 >,
  67. + < 1512000000 1250000 >;
  68. + };
  69. --- a/drivers/cpufreq/Kconfig.arm
  70. +++ b/drivers/cpufreq/Kconfig.arm
  71. @@ -100,6 +100,15 @@ config ARM_OMAP2PLUS_CPUFREQ
  72. depends on ARCH_OMAP2PLUS
  73. default ARCH_OMAP2PLUS
  74. +config ARM_QCOM_CPUFREQ
  75. + tristate "Qualcomm based"
  76. + depends on ARCH_QCOM
  77. + select PM_OPP
  78. + help
  79. + This adds the CPUFreq driver for Qualcomm SoC based boards.
  80. +
  81. + If in doubt, say N.
  82. +
  83. config ARM_S3C_CPUFREQ
  84. bool
  85. help
  86. --- a/drivers/cpufreq/Makefile
  87. +++ b/drivers/cpufreq/Makefile
  88. @@ -62,6 +62,7 @@ obj-$(CONFIG_ARM_MEDIATEK_CPUFREQ) += me
  89. obj-$(CONFIG_ARM_OMAP2PLUS_CPUFREQ) += omap-cpufreq.o
  90. obj-$(CONFIG_ARM_PXA2xx_CPUFREQ) += pxa2xx-cpufreq.o
  91. obj-$(CONFIG_PXA3xx) += pxa3xx-cpufreq.o
  92. +obj-$(CONFIG_ARM_QCOM_CPUFREQ) += qcom-cpufreq.o
  93. obj-$(CONFIG_ARM_S3C24XX_CPUFREQ) += s3c24xx-cpufreq.o
  94. obj-$(CONFIG_ARM_S3C24XX_CPUFREQ_DEBUGFS) += s3c24xx-cpufreq-debugfs.o
  95. obj-$(CONFIG_ARM_S3C2410_CPUFREQ) += s3c2410-cpufreq.o
  96. --- /dev/null
  97. +++ b/drivers/cpufreq/qcom-cpufreq.c
  98. @@ -0,0 +1,204 @@
  99. +/* Copyright (c) 2014, The Linux Foundation. All rights reserved.
  100. + *
  101. + * This program is free software; you can redistribute it and/or modify
  102. + * it under the terms of the GNU General Public License version 2 and
  103. + * only version 2 as published by the Free Software Foundation.
  104. + *
  105. + * This program is distributed in the hope that it will be useful,
  106. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  107. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  108. + * GNU General Public License for more details.
  109. + */
  110. +
  111. +#include <linux/cpu.h>
  112. +#include <linux/err.h>
  113. +#include <linux/init.h>
  114. +#include <linux/io.h>
  115. +#include <linux/kernel.h>
  116. +#include <linux/module.h>
  117. +#include <linux/of.h>
  118. +#include <linux/platform_device.h>
  119. +#include <linux/pm_opp.h>
  120. +#include <linux/slab.h>
  121. +#include "cpufreq-dt.h"
  122. +
  123. +static void __init get_krait_bin_format_a(int *speed, int *pvs, int *pvs_ver)
  124. +{
  125. + void __iomem *base;
  126. + u32 pte_efuse;
  127. +
  128. + *speed = *pvs = *pvs_ver = 0;
  129. +
  130. + base = ioremap(0x007000c0, 4);
  131. + if (!base) {
  132. + pr_warn("Unable to read efuse data. Defaulting to 0!\n");
  133. + return;
  134. + }
  135. +
  136. + pte_efuse = readl_relaxed(base);
  137. + iounmap(base);
  138. +
  139. + *speed = pte_efuse & 0xf;
  140. + if (*speed == 0xf)
  141. + *speed = (pte_efuse >> 4) & 0xf;
  142. +
  143. + if (*speed == 0xf) {
  144. + *speed = 0;
  145. + pr_warn("Speed bin: Defaulting to %d\n", *speed);
  146. + } else {
  147. + pr_info("Speed bin: %d\n", *speed);
  148. + }
  149. +
  150. + *pvs = (pte_efuse >> 10) & 0x7;
  151. + if (*pvs == 0x7)
  152. + *pvs = (pte_efuse >> 13) & 0x7;
  153. +
  154. + if (*pvs == 0x7) {
  155. + *pvs = 0;
  156. + pr_warn("PVS bin: Defaulting to %d\n", *pvs);
  157. + } else {
  158. + pr_info("PVS bin: %d\n", *pvs);
  159. + }
  160. +}
  161. +
  162. +static void __init get_krait_bin_format_b(int *speed, int *pvs, int *pvs_ver)
  163. +{
  164. + u32 pte_efuse, redundant_sel;
  165. + void __iomem *base;
  166. +
  167. + *speed = 0;
  168. + *pvs = 0;
  169. + *pvs_ver = 0;
  170. +
  171. + base = ioremap(0xfc4b80b0, 8);
  172. + if (!base) {
  173. + pr_warn("Unable to read efuse data. Defaulting to 0!\n");
  174. + return;
  175. + }
  176. +
  177. + pte_efuse = readl_relaxed(base);
  178. + redundant_sel = (pte_efuse >> 24) & 0x7;
  179. + *speed = pte_efuse & 0x7;
  180. + /* 4 bits of PVS are in efuse register bits 31, 8-6. */
  181. + *pvs = ((pte_efuse >> 28) & 0x8) | ((pte_efuse >> 6) & 0x7);
  182. + *pvs_ver = (pte_efuse >> 4) & 0x3;
  183. +
  184. + switch (redundant_sel) {
  185. + case 1:
  186. + *speed = (pte_efuse >> 27) & 0xf;
  187. + break;
  188. + case 2:
  189. + *pvs = (pte_efuse >> 27) & 0xf;
  190. + break;
  191. + }
  192. +
  193. + /* Check SPEED_BIN_BLOW_STATUS */
  194. + if (pte_efuse & BIT(3)) {
  195. + pr_info("Speed bin: %d\n", *speed);
  196. + } else {
  197. + pr_warn("Speed bin not set. Defaulting to 0!\n");
  198. + *speed = 0;
  199. + }
  200. +
  201. + /* Check PVS_BLOW_STATUS */
  202. + pte_efuse = readl_relaxed(base + 0x4) & BIT(21);
  203. + if (pte_efuse) {
  204. + pr_info("PVS bin: %d\n", *pvs);
  205. + } else {
  206. + pr_warn("PVS bin not set. Defaulting to 0!\n");
  207. + *pvs = 0;
  208. + }
  209. +
  210. + pr_info("PVS version: %d\n", *pvs_ver);
  211. + iounmap(base);
  212. +}
  213. +
  214. +static int __init qcom_cpufreq_populate_opps(void)
  215. +{
  216. + int len, rows, cols, i, k, speed, pvs, pvs_ver;
  217. + char table_name[] = "qcom,speedXX-pvsXX-bin-vXX";
  218. + struct device_node *np;
  219. + struct device *dev;
  220. + int cpu = 0;
  221. +
  222. + np = of_find_node_by_name(NULL, "qcom,pvs");
  223. + if (!np)
  224. + return -ENODEV;
  225. +
  226. + if (of_property_read_bool(np, "qcom,pvs-format-a")) {
  227. + get_krait_bin_format_a(&speed, &pvs, &pvs_ver);
  228. + cols = 2;
  229. + } else if (of_property_read_bool(np, "qcom,pvs-format-b")) {
  230. + get_krait_bin_format_b(&speed, &pvs, &pvs_ver);
  231. + cols = 3;
  232. + } else {
  233. + return -ENODEV;
  234. + }
  235. +
  236. + snprintf(table_name, sizeof(table_name),
  237. + "qcom,speed%d-pvs%d-bin-v%d", speed, pvs, pvs_ver);
  238. +
  239. + if (!of_find_property(np, table_name, &len))
  240. + return -EINVAL;
  241. +
  242. + len /= sizeof(u32);
  243. + if (len % cols || len == 0)
  244. + return -EINVAL;
  245. +
  246. + rows = len / cols;
  247. +
  248. + for (i = 0, k = 0; i < rows; i++) {
  249. + u32 freq, volt;
  250. +
  251. + of_property_read_u32_index(np, table_name, k++, &freq);
  252. + of_property_read_u32_index(np, table_name, k++, &volt);
  253. + while (k % cols)
  254. + k++; /* Skip uA entries if present */
  255. + for (cpu = 0; cpu < num_possible_cpus(); cpu++) {
  256. + dev = get_cpu_device(cpu);
  257. + if (!dev)
  258. + return -ENODEV;
  259. + if (dev_pm_opp_add(dev, freq, volt))
  260. + pr_warn("failed to add OPP %u\n", freq);
  261. + }
  262. + }
  263. +
  264. + return 0;
  265. +}
  266. +
  267. +static int __init qcom_cpufreq_driver_init(void)
  268. +{
  269. + struct cpufreq_dt_platform_data pdata = { .independent_clocks = true };
  270. + struct platform_device_info devinfo = {
  271. + .name = "cpufreq-dt",
  272. + .data = &pdata,
  273. + .size_data = sizeof(pdata),
  274. + };
  275. + struct device *cpu_dev;
  276. + struct device_node *np;
  277. + int ret;
  278. +
  279. + cpu_dev = get_cpu_device(0);
  280. + if (!cpu_dev)
  281. + return -ENODEV;
  282. +
  283. + np = of_node_get(cpu_dev->of_node);
  284. + if (!np)
  285. + return -ENOENT;
  286. +
  287. + if (!of_device_is_compatible(np, "qcom,krait")) {
  288. + of_node_put(np);
  289. + return -ENODEV;
  290. + }
  291. + of_node_put(np);
  292. +
  293. + ret = qcom_cpufreq_populate_opps();
  294. + if (ret)
  295. + return ret;
  296. +
  297. + return PTR_ERR_OR_ZERO(platform_device_register_full(&devinfo));
  298. +}
  299. +module_init(qcom_cpufreq_driver_init);
  300. +
  301. +MODULE_DESCRIPTION("Qualcomm CPUfreq driver");
  302. +MODULE_LICENSE("GPL v2");