qoriq-cpufreq.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. struct clk *pltclk;
  50. int ret;
  51. /* get platform freq by searching bus-frequency property */
  52. soc = of_find_node_by_type(NULL, "soc");
  53. if (soc) {
  54. ret = of_property_read_u32(soc, "bus-frequency", &sysfreq);
  55. of_node_put(soc);
  56. if (!ret)
  57. return sysfreq;
  58. }
  59. /* get platform freq by its clock name */
  60. pltclk = clk_get(NULL, "cg-pll0-div1");
  61. if (IS_ERR(pltclk)) {
  62. pr_err("%s: can't get bus frequency %ld\n",
  63. __func__, PTR_ERR(pltclk));
  64. return PTR_ERR(pltclk);
  65. }
  66. return clk_get_rate(pltclk);
  67. }
  68. static struct clk *cpu_to_clk(int cpu)
  69. {
  70. struct device_node *np;
  71. struct clk *clk;
  72. if (!cpu_present(cpu))
  73. return NULL;
  74. np = of_get_cpu_node(cpu, NULL);
  75. if (!np)
  76. return NULL;
  77. clk = of_clk_get(np, 0);
  78. of_node_put(np);
  79. return clk;
  80. }
  81. /* traverse cpu nodes to get cpu mask of sharing clock wire */
  82. static void set_affected_cpus(struct cpufreq_policy *policy)
  83. {
  84. struct cpumask *dstp = policy->cpus;
  85. struct clk *clk;
  86. int i;
  87. for_each_present_cpu(i) {
  88. clk = cpu_to_clk(i);
  89. if (IS_ERR(clk)) {
  90. pr_err("%s: no clock for cpu %d\n", __func__, i);
  91. continue;
  92. }
  93. if (clk_is_match(policy->clk, clk))
  94. cpumask_set_cpu(i, dstp);
  95. }
  96. }
  97. /* reduce the duplicated frequencies in frequency table */
  98. static void freq_table_redup(struct cpufreq_frequency_table *freq_table,
  99. int count)
  100. {
  101. int i, j;
  102. for (i = 1; i < count; i++) {
  103. for (j = 0; j < i; j++) {
  104. if (freq_table[j].frequency == CPUFREQ_ENTRY_INVALID ||
  105. freq_table[j].frequency !=
  106. freq_table[i].frequency)
  107. continue;
  108. freq_table[i].frequency = CPUFREQ_ENTRY_INVALID;
  109. break;
  110. }
  111. }
  112. }
  113. /* sort the frequencies in frequency table in descenting order */
  114. static void freq_table_sort(struct cpufreq_frequency_table *freq_table,
  115. int count)
  116. {
  117. int i, j, ind;
  118. unsigned int freq, max_freq;
  119. struct cpufreq_frequency_table table;
  120. for (i = 0; i < count - 1; i++) {
  121. max_freq = freq_table[i].frequency;
  122. ind = i;
  123. for (j = i + 1; j < count; j++) {
  124. freq = freq_table[j].frequency;
  125. if (freq == CPUFREQ_ENTRY_INVALID ||
  126. freq <= max_freq)
  127. continue;
  128. ind = j;
  129. max_freq = freq;
  130. }
  131. if (ind != i) {
  132. /* exchange the frequencies */
  133. table.driver_data = freq_table[i].driver_data;
  134. table.frequency = freq_table[i].frequency;
  135. freq_table[i].driver_data = freq_table[ind].driver_data;
  136. freq_table[i].frequency = freq_table[ind].frequency;
  137. freq_table[ind].driver_data = table.driver_data;
  138. freq_table[ind].frequency = table.frequency;
  139. }
  140. }
  141. }
  142. static int qoriq_cpufreq_cpu_init(struct cpufreq_policy *policy)
  143. {
  144. struct device_node *np;
  145. int i, count;
  146. u32 freq;
  147. struct clk *clk;
  148. const struct clk_hw *hwclk;
  149. struct cpufreq_frequency_table *table;
  150. struct cpu_data *data;
  151. unsigned int cpu = policy->cpu;
  152. u64 u64temp;
  153. np = of_get_cpu_node(cpu, NULL);
  154. if (!np)
  155. return -ENODEV;
  156. data = kzalloc(sizeof(*data), GFP_KERNEL);
  157. if (!data)
  158. goto err_np;
  159. policy->clk = of_clk_get(np, 0);
  160. if (IS_ERR(policy->clk)) {
  161. pr_err("%s: no clock information\n", __func__);
  162. goto err_nomem2;
  163. }
  164. hwclk = __clk_get_hw(policy->clk);
  165. count = clk_hw_get_num_parents(hwclk);
  166. data->pclk = kcalloc(count, sizeof(struct clk *), GFP_KERNEL);
  167. if (!data->pclk)
  168. goto err_nomem2;
  169. table = kcalloc(count + 1, sizeof(*table), GFP_KERNEL);
  170. if (!table)
  171. goto err_pclk;
  172. for (i = 0; i < count; i++) {
  173. clk = clk_hw_get_parent_by_index(hwclk, i)->clk;
  174. data->pclk[i] = clk;
  175. freq = clk_get_rate(clk);
  176. table[i].frequency = freq / 1000;
  177. table[i].driver_data = i;
  178. }
  179. freq_table_redup(table, count);
  180. freq_table_sort(table, count);
  181. table[i].frequency = CPUFREQ_TABLE_END;
  182. policy->freq_table = table;
  183. data->table = table;
  184. /* update ->cpus if we have cluster, no harm if not */
  185. set_affected_cpus(policy);
  186. policy->driver_data = data;
  187. /* Minimum transition latency is 12 platform clocks */
  188. u64temp = 12ULL * NSEC_PER_SEC;
  189. do_div(u64temp, get_bus_freq());
  190. policy->cpuinfo.transition_latency = u64temp + 1;
  191. of_node_put(np);
  192. return 0;
  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. cpud->cdev = of_cpufreq_cooling_register(policy);
  223. }
  224. static struct cpufreq_driver qoriq_cpufreq_driver = {
  225. .name = "qoriq_cpufreq",
  226. .flags = CPUFREQ_CONST_LOOPS,
  227. .init = qoriq_cpufreq_cpu_init,
  228. .exit = qoriq_cpufreq_cpu_exit,
  229. .verify = cpufreq_generic_frequency_table_verify,
  230. .target_index = qoriq_cpufreq_target,
  231. .get = cpufreq_generic_get,
  232. .ready = qoriq_cpufreq_ready,
  233. .attr = cpufreq_generic_attr,
  234. };
  235. static const struct soc_data blacklist = {
  236. .flags = SOC_BLACKLIST,
  237. };
  238. static const struct of_device_id node_matches[] __initconst = {
  239. /* e6500 cannot use cpufreq due to erratum A-008083 */
  240. { .compatible = "fsl,b4420-clockgen", &blacklist },
  241. { .compatible = "fsl,b4860-clockgen", &blacklist },
  242. { .compatible = "fsl,t2080-clockgen", &blacklist },
  243. { .compatible = "fsl,t4240-clockgen", &blacklist },
  244. { .compatible = "fsl,ls1012a-clockgen", },
  245. { .compatible = "fsl,ls1021a-clockgen", },
  246. { .compatible = "fsl,ls1043a-clockgen", },
  247. { .compatible = "fsl,ls1046a-clockgen", },
  248. { .compatible = "fsl,ls1088a-clockgen", },
  249. { .compatible = "fsl,ls2080a-clockgen", },
  250. { .compatible = "fsl,p4080-clockgen", },
  251. { .compatible = "fsl,qoriq-clockgen-1.0", },
  252. { .compatible = "fsl,qoriq-clockgen-2.0", },
  253. {}
  254. };
  255. static int __init qoriq_cpufreq_init(void)
  256. {
  257. int ret;
  258. struct device_node *np;
  259. const struct of_device_id *match;
  260. const struct soc_data *data;
  261. np = of_find_matching_node(NULL, node_matches);
  262. if (!np)
  263. return -ENODEV;
  264. match = of_match_node(node_matches, np);
  265. data = match->data;
  266. of_node_put(np);
  267. if (data && data->flags & SOC_BLACKLIST)
  268. return -ENODEV;
  269. ret = cpufreq_register_driver(&qoriq_cpufreq_driver);
  270. if (!ret)
  271. pr_info("Freescale QorIQ CPU frequency scaling driver\n");
  272. return ret;
  273. }
  274. module_init(qoriq_cpufreq_init);
  275. static void __exit qoriq_cpufreq_exit(void)
  276. {
  277. cpufreq_unregister_driver(&qoriq_cpufreq_driver);
  278. }
  279. module_exit(qoriq_cpufreq_exit);
  280. MODULE_LICENSE("GPL");
  281. MODULE_AUTHOR("Tang Yuantian <Yuantian.Tang@freescale.com>");
  282. MODULE_DESCRIPTION("cpufreq driver for Freescale QorIQ series SoCs");