qoriq-cpufreq.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * Copyright 2013 Freescale Semiconductor, Inc.
  3. *
  4. * CPU Frequency Scaling driver for Freescale QorIQ SoCs.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/clk.h>
  12. #include <linux/clk-provider.h>
  13. #include <linux/cpufreq.h>
  14. #include <linux/cpu_cooling.h>
  15. #include <linux/errno.h>
  16. #include <linux/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include <linux/of.h>
  21. #include <linux/slab.h>
  22. #include <linux/smp.h>
  23. /**
  24. * struct cpu_data
  25. * @pclk: the parent clock of cpu
  26. * @table: frequency table
  27. */
  28. struct cpu_data {
  29. struct clk **pclk;
  30. struct cpufreq_frequency_table *table;
  31. struct thermal_cooling_device *cdev;
  32. };
  33. /*
  34. * Don't use cpufreq on this SoC -- used when the SoC would have otherwise
  35. * matched a more generic compatible.
  36. */
  37. #define SOC_BLACKLIST 1
  38. /**
  39. * struct soc_data - SoC specific data
  40. * @flags: SOC_xxx
  41. */
  42. struct soc_data {
  43. u32 flags;
  44. };
  45. static u32 get_bus_freq(void)
  46. {
  47. struct device_node *soc;
  48. u32 sysfreq;
  49. soc = of_find_node_by_type(NULL, "soc");
  50. if (!soc)
  51. return 0;
  52. if (of_property_read_u32(soc, "bus-frequency", &sysfreq))
  53. sysfreq = 0;
  54. of_node_put(soc);
  55. return sysfreq;
  56. }
  57. static struct clk *cpu_to_clk(int cpu)
  58. {
  59. struct device_node *np;
  60. struct clk *clk;
  61. if (!cpu_present(cpu))
  62. return NULL;
  63. np = of_get_cpu_node(cpu, NULL);
  64. if (!np)
  65. return NULL;
  66. clk = of_clk_get(np, 0);
  67. of_node_put(np);
  68. return clk;
  69. }
  70. /* traverse cpu nodes to get cpu mask of sharing clock wire */
  71. static void set_affected_cpus(struct cpufreq_policy *policy)
  72. {
  73. struct cpumask *dstp = policy->cpus;
  74. struct clk *clk;
  75. int i;
  76. for_each_present_cpu(i) {
  77. clk = cpu_to_clk(i);
  78. if (IS_ERR(clk)) {
  79. pr_err("%s: no clock for cpu %d\n", __func__, i);
  80. continue;
  81. }
  82. if (clk_is_match(policy->clk, clk))
  83. cpumask_set_cpu(i, dstp);
  84. }
  85. }
  86. /* reduce the duplicated frequencies in frequency table */
  87. static void freq_table_redup(struct cpufreq_frequency_table *freq_table,
  88. int count)
  89. {
  90. int i, j;
  91. for (i = 1; i < count; i++) {
  92. for (j = 0; j < i; j++) {
  93. if (freq_table[j].frequency == CPUFREQ_ENTRY_INVALID ||
  94. freq_table[j].frequency !=
  95. freq_table[i].frequency)
  96. continue;
  97. freq_table[i].frequency = CPUFREQ_ENTRY_INVALID;
  98. break;
  99. }
  100. }
  101. }
  102. /* sort the frequencies in frequency table in descenting order */
  103. static void freq_table_sort(struct cpufreq_frequency_table *freq_table,
  104. int count)
  105. {
  106. int i, j, ind;
  107. unsigned int freq, max_freq;
  108. struct cpufreq_frequency_table table;
  109. for (i = 0; i < count - 1; i++) {
  110. max_freq = freq_table[i].frequency;
  111. ind = i;
  112. for (j = i + 1; j < count; j++) {
  113. freq = freq_table[j].frequency;
  114. if (freq == CPUFREQ_ENTRY_INVALID ||
  115. freq <= max_freq)
  116. continue;
  117. ind = j;
  118. max_freq = freq;
  119. }
  120. if (ind != i) {
  121. /* exchange the frequencies */
  122. table.driver_data = freq_table[i].driver_data;
  123. table.frequency = freq_table[i].frequency;
  124. freq_table[i].driver_data = freq_table[ind].driver_data;
  125. freq_table[i].frequency = freq_table[ind].frequency;
  126. freq_table[ind].driver_data = table.driver_data;
  127. freq_table[ind].frequency = table.frequency;
  128. }
  129. }
  130. }
  131. static int qoriq_cpufreq_cpu_init(struct cpufreq_policy *policy)
  132. {
  133. struct device_node *np;
  134. int i, count, ret;
  135. u32 freq;
  136. struct clk *clk;
  137. const struct clk_hw *hwclk;
  138. struct cpufreq_frequency_table *table;
  139. struct cpu_data *data;
  140. unsigned int cpu = policy->cpu;
  141. u64 u64temp;
  142. np = of_get_cpu_node(cpu, NULL);
  143. if (!np)
  144. return -ENODEV;
  145. data = kzalloc(sizeof(*data), GFP_KERNEL);
  146. if (!data)
  147. goto err_np;
  148. policy->clk = of_clk_get(np, 0);
  149. if (IS_ERR(policy->clk)) {
  150. pr_err("%s: no clock information\n", __func__);
  151. goto err_nomem2;
  152. }
  153. hwclk = __clk_get_hw(policy->clk);
  154. count = clk_hw_get_num_parents(hwclk);
  155. data->pclk = kcalloc(count, sizeof(struct clk *), GFP_KERNEL);
  156. if (!data->pclk) {
  157. pr_err("%s: no memory\n", __func__);
  158. goto err_nomem2;
  159. }
  160. table = kcalloc(count + 1, sizeof(*table), GFP_KERNEL);
  161. if (!table) {
  162. pr_err("%s: no memory\n", __func__);
  163. goto err_pclk;
  164. }
  165. for (i = 0; i < count; i++) {
  166. clk = clk_hw_get_parent_by_index(hwclk, i)->clk;
  167. data->pclk[i] = clk;
  168. freq = clk_get_rate(clk);
  169. table[i].frequency = freq / 1000;
  170. table[i].driver_data = i;
  171. }
  172. freq_table_redup(table, count);
  173. freq_table_sort(table, count);
  174. table[i].frequency = CPUFREQ_TABLE_END;
  175. /* set the min and max frequency properly */
  176. ret = cpufreq_table_validate_and_show(policy, table);
  177. if (ret) {
  178. pr_err("invalid frequency table: %d\n", ret);
  179. goto err_nomem1;
  180. }
  181. data->table = table;
  182. /* update ->cpus if we have cluster, no harm if not */
  183. set_affected_cpus(policy);
  184. policy->driver_data = data;
  185. /* Minimum transition latency is 12 platform clocks */
  186. u64temp = 12ULL * NSEC_PER_SEC;
  187. do_div(u64temp, get_bus_freq());
  188. policy->cpuinfo.transition_latency = u64temp + 1;
  189. of_node_put(np);
  190. return 0;
  191. err_nomem1:
  192. kfree(table);
  193. err_pclk:
  194. kfree(data->pclk);
  195. err_nomem2:
  196. kfree(data);
  197. err_np:
  198. of_node_put(np);
  199. return -ENODEV;
  200. }
  201. static int qoriq_cpufreq_cpu_exit(struct cpufreq_policy *policy)
  202. {
  203. struct cpu_data *data = policy->driver_data;
  204. cpufreq_cooling_unregister(data->cdev);
  205. kfree(data->pclk);
  206. kfree(data->table);
  207. kfree(data);
  208. policy->driver_data = NULL;
  209. return 0;
  210. }
  211. static int qoriq_cpufreq_target(struct cpufreq_policy *policy,
  212. unsigned int index)
  213. {
  214. struct clk *parent;
  215. struct cpu_data *data = policy->driver_data;
  216. parent = data->pclk[data->table[index].driver_data];
  217. return clk_set_parent(policy->clk, parent);
  218. }
  219. static void qoriq_cpufreq_ready(struct cpufreq_policy *policy)
  220. {
  221. struct cpu_data *cpud = policy->driver_data;
  222. struct device_node *np = of_get_cpu_node(policy->cpu, NULL);
  223. if (of_find_property(np, "#cooling-cells", NULL)) {
  224. cpud->cdev = of_cpufreq_cooling_register(np,
  225. policy->related_cpus);
  226. if (IS_ERR(cpud->cdev) && PTR_ERR(cpud->cdev) != -ENOSYS) {
  227. pr_err("cpu%d is not running as cooling device: %ld\n",
  228. policy->cpu, PTR_ERR(cpud->cdev));
  229. cpud->cdev = NULL;
  230. }
  231. }
  232. of_node_put(np);
  233. }
  234. static struct cpufreq_driver qoriq_cpufreq_driver = {
  235. .name = "qoriq_cpufreq",
  236. .flags = CPUFREQ_CONST_LOOPS,
  237. .init = qoriq_cpufreq_cpu_init,
  238. .exit = qoriq_cpufreq_cpu_exit,
  239. .verify = cpufreq_generic_frequency_table_verify,
  240. .target_index = qoriq_cpufreq_target,
  241. .get = cpufreq_generic_get,
  242. .ready = qoriq_cpufreq_ready,
  243. .attr = cpufreq_generic_attr,
  244. };
  245. static const struct soc_data blacklist = {
  246. .flags = SOC_BLACKLIST,
  247. };
  248. static const struct of_device_id node_matches[] __initconst = {
  249. /* e6500 cannot use cpufreq due to erratum A-008083 */
  250. { .compatible = "fsl,b4420-clockgen", &blacklist },
  251. { .compatible = "fsl,b4860-clockgen", &blacklist },
  252. { .compatible = "fsl,t2080-clockgen", &blacklist },
  253. { .compatible = "fsl,t4240-clockgen", &blacklist },
  254. { .compatible = "fsl,ls1012a-clockgen", },
  255. { .compatible = "fsl,ls1021a-clockgen", },
  256. { .compatible = "fsl,ls1043a-clockgen", },
  257. { .compatible = "fsl,ls1046a-clockgen", },
  258. { .compatible = "fsl,ls1088a-clockgen", },
  259. { .compatible = "fsl,ls2080a-clockgen", },
  260. { .compatible = "fsl,p4080-clockgen", },
  261. { .compatible = "fsl,qoriq-clockgen-1.0", },
  262. { .compatible = "fsl,qoriq-clockgen-2.0", },
  263. {}
  264. };
  265. static int __init qoriq_cpufreq_init(void)
  266. {
  267. int ret;
  268. struct device_node *np;
  269. const struct of_device_id *match;
  270. const struct soc_data *data;
  271. np = of_find_matching_node(NULL, node_matches);
  272. if (!np)
  273. return -ENODEV;
  274. match = of_match_node(node_matches, np);
  275. data = match->data;
  276. of_node_put(np);
  277. if (data && data->flags & SOC_BLACKLIST)
  278. return -ENODEV;
  279. ret = cpufreq_register_driver(&qoriq_cpufreq_driver);
  280. if (!ret)
  281. pr_info("Freescale QorIQ CPU frequency scaling driver\n");
  282. return ret;
  283. }
  284. module_init(qoriq_cpufreq_init);
  285. static void __exit qoriq_cpufreq_exit(void)
  286. {
  287. cpufreq_unregister_driver(&qoriq_cpufreq_driver);
  288. }
  289. module_exit(qoriq_cpufreq_exit);
  290. MODULE_LICENSE("GPL");
  291. MODULE_AUTHOR("Tang Yuantian <Yuantian.Tang@freescale.com>");
  292. MODULE_DESCRIPTION("cpufreq driver for Freescale QorIQ series SoCs");