ppc-corenet-cpufreq.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright 2013 Freescale Semiconductor, Inc.
  3. *
  4. * CPU Frequency Scaling driver for Freescale PowerPC corenet 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/cpufreq.h>
  13. #include <linux/errno.h>
  14. #include <sysdev/fsl_soc.h>
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/mutex.h>
  19. #include <linux/of.h>
  20. #include <linux/slab.h>
  21. #include <linux/smp.h>
  22. /**
  23. * struct cpu_data - per CPU data struct
  24. * @parent: the parent node of cpu clock
  25. * @table: frequency table
  26. */
  27. struct cpu_data {
  28. struct device_node *parent;
  29. struct cpufreq_frequency_table *table;
  30. };
  31. /**
  32. * struct soc_data - SoC specific data
  33. * @freq_mask: mask the disallowed frequencies
  34. * @flag: unique flags
  35. */
  36. struct soc_data {
  37. u32 freq_mask[4];
  38. u32 flag;
  39. };
  40. #define FREQ_MASK 1
  41. /* see hardware specification for the allowed frqeuencies */
  42. static const struct soc_data sdata[] = {
  43. { /* used by p2041 and p3041 */
  44. .freq_mask = {0x8, 0x8, 0x2, 0x2},
  45. .flag = FREQ_MASK,
  46. },
  47. { /* used by p5020 */
  48. .freq_mask = {0x8, 0x2},
  49. .flag = FREQ_MASK,
  50. },
  51. { /* used by p4080, p5040 */
  52. .freq_mask = {0},
  53. .flag = 0,
  54. },
  55. };
  56. /*
  57. * the minimum allowed core frequency, in Hz
  58. * for chassis v1.0, >= platform frequency
  59. * for chassis v2.0, >= platform frequency / 2
  60. */
  61. static u32 min_cpufreq;
  62. static const u32 *fmask;
  63. static DEFINE_PER_CPU(struct cpu_data *, cpu_data);
  64. /* cpumask in a cluster */
  65. static DEFINE_PER_CPU(cpumask_var_t, cpu_mask);
  66. #ifndef CONFIG_SMP
  67. static inline const struct cpumask *cpu_core_mask(int cpu)
  68. {
  69. return cpumask_of(0);
  70. }
  71. #endif
  72. /* reduce the duplicated frequencies in frequency table */
  73. static void freq_table_redup(struct cpufreq_frequency_table *freq_table,
  74. int count)
  75. {
  76. int i, j;
  77. for (i = 1; i < count; i++) {
  78. for (j = 0; j < i; j++) {
  79. if (freq_table[j].frequency == CPUFREQ_ENTRY_INVALID ||
  80. freq_table[j].frequency !=
  81. freq_table[i].frequency)
  82. continue;
  83. freq_table[i].frequency = CPUFREQ_ENTRY_INVALID;
  84. break;
  85. }
  86. }
  87. }
  88. /* sort the frequencies in frequency table in descenting order */
  89. static void freq_table_sort(struct cpufreq_frequency_table *freq_table,
  90. int count)
  91. {
  92. int i, j, ind;
  93. unsigned int freq, max_freq;
  94. struct cpufreq_frequency_table table;
  95. for (i = 0; i < count - 1; i++) {
  96. max_freq = freq_table[i].frequency;
  97. ind = i;
  98. for (j = i + 1; j < count; j++) {
  99. freq = freq_table[j].frequency;
  100. if (freq == CPUFREQ_ENTRY_INVALID ||
  101. freq <= max_freq)
  102. continue;
  103. ind = j;
  104. max_freq = freq;
  105. }
  106. if (ind != i) {
  107. /* exchange the frequencies */
  108. table.driver_data = freq_table[i].driver_data;
  109. table.frequency = freq_table[i].frequency;
  110. freq_table[i].driver_data = freq_table[ind].driver_data;
  111. freq_table[i].frequency = freq_table[ind].frequency;
  112. freq_table[ind].driver_data = table.driver_data;
  113. freq_table[ind].frequency = table.frequency;
  114. }
  115. }
  116. }
  117. static int corenet_cpufreq_cpu_init(struct cpufreq_policy *policy)
  118. {
  119. struct device_node *np;
  120. int i, count, ret;
  121. u32 freq, mask;
  122. struct clk *clk;
  123. struct cpufreq_frequency_table *table;
  124. struct cpu_data *data;
  125. unsigned int cpu = policy->cpu;
  126. np = of_get_cpu_node(cpu, NULL);
  127. if (!np)
  128. return -ENODEV;
  129. data = kzalloc(sizeof(*data), GFP_KERNEL);
  130. if (!data) {
  131. pr_err("%s: no memory\n", __func__);
  132. goto err_np;
  133. }
  134. policy->clk = of_clk_get(np, 0);
  135. if (IS_ERR(policy->clk)) {
  136. pr_err("%s: no clock information\n", __func__);
  137. goto err_nomem2;
  138. }
  139. data->parent = of_parse_phandle(np, "clocks", 0);
  140. if (!data->parent) {
  141. pr_err("%s: could not get clock information\n", __func__);
  142. goto err_nomem2;
  143. }
  144. count = of_property_count_strings(data->parent, "clock-names");
  145. table = kcalloc(count + 1, sizeof(*table), GFP_KERNEL);
  146. if (!table) {
  147. pr_err("%s: no memory\n", __func__);
  148. goto err_node;
  149. }
  150. if (fmask)
  151. mask = fmask[get_hard_smp_processor_id(cpu)];
  152. else
  153. mask = 0x0;
  154. for (i = 0; i < count; i++) {
  155. clk = of_clk_get(data->parent, i);
  156. freq = clk_get_rate(clk);
  157. /*
  158. * the clock is valid if its frequency is not masked
  159. * and large than minimum allowed frequency.
  160. */
  161. if (freq < min_cpufreq || (mask & (1 << i)))
  162. table[i].frequency = CPUFREQ_ENTRY_INVALID;
  163. else
  164. table[i].frequency = freq / 1000;
  165. table[i].driver_data = i;
  166. }
  167. freq_table_redup(table, count);
  168. freq_table_sort(table, count);
  169. table[i].frequency = CPUFREQ_TABLE_END;
  170. /* set the min and max frequency properly */
  171. ret = cpufreq_table_validate_and_show(policy, table);
  172. if (ret) {
  173. pr_err("invalid frequency table: %d\n", ret);
  174. goto err_nomem1;
  175. }
  176. data->table = table;
  177. per_cpu(cpu_data, cpu) = data;
  178. /* update ->cpus if we have cluster, no harm if not */
  179. cpumask_copy(policy->cpus, per_cpu(cpu_mask, cpu));
  180. for_each_cpu(i, per_cpu(cpu_mask, cpu))
  181. per_cpu(cpu_data, i) = data;
  182. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  183. of_node_put(np);
  184. return 0;
  185. err_nomem1:
  186. kfree(table);
  187. err_node:
  188. of_node_put(data->parent);
  189. err_nomem2:
  190. per_cpu(cpu_data, cpu) = NULL;
  191. kfree(data);
  192. err_np:
  193. of_node_put(np);
  194. return -ENODEV;
  195. }
  196. static int __exit corenet_cpufreq_cpu_exit(struct cpufreq_policy *policy)
  197. {
  198. struct cpu_data *data = per_cpu(cpu_data, policy->cpu);
  199. unsigned int cpu;
  200. cpufreq_frequency_table_put_attr(policy->cpu);
  201. of_node_put(data->parent);
  202. kfree(data->table);
  203. kfree(data);
  204. for_each_cpu(cpu, per_cpu(cpu_mask, policy->cpu))
  205. per_cpu(cpu_data, cpu) = NULL;
  206. return 0;
  207. }
  208. static int corenet_cpufreq_target(struct cpufreq_policy *policy,
  209. unsigned int index)
  210. {
  211. struct clk *parent;
  212. struct cpu_data *data = per_cpu(cpu_data, policy->cpu);
  213. parent = of_clk_get(data->parent, data->table[index].driver_data);
  214. return clk_set_parent(policy->clk, parent);
  215. }
  216. static struct cpufreq_driver ppc_corenet_cpufreq_driver = {
  217. .name = "ppc_cpufreq",
  218. .flags = CPUFREQ_CONST_LOOPS,
  219. .init = corenet_cpufreq_cpu_init,
  220. .exit = __exit_p(corenet_cpufreq_cpu_exit),
  221. .verify = cpufreq_generic_frequency_table_verify,
  222. .target_index = corenet_cpufreq_target,
  223. .get = cpufreq_generic_get,
  224. .attr = cpufreq_generic_attr,
  225. };
  226. static const struct of_device_id node_matches[] __initdata = {
  227. { .compatible = "fsl,p2041-clockgen", .data = &sdata[0], },
  228. { .compatible = "fsl,p3041-clockgen", .data = &sdata[0], },
  229. { .compatible = "fsl,p5020-clockgen", .data = &sdata[1], },
  230. { .compatible = "fsl,p4080-clockgen", .data = &sdata[2], },
  231. { .compatible = "fsl,p5040-clockgen", .data = &sdata[2], },
  232. { .compatible = "fsl,qoriq-clockgen-2.0", },
  233. {}
  234. };
  235. static int __init ppc_corenet_cpufreq_init(void)
  236. {
  237. int ret;
  238. struct device_node *np;
  239. const struct of_device_id *match;
  240. const struct soc_data *data;
  241. unsigned int cpu;
  242. np = of_find_matching_node(NULL, node_matches);
  243. if (!np)
  244. return -ENODEV;
  245. for_each_possible_cpu(cpu) {
  246. if (!alloc_cpumask_var(&per_cpu(cpu_mask, cpu), GFP_KERNEL))
  247. goto err_mask;
  248. cpumask_copy(per_cpu(cpu_mask, cpu), cpu_core_mask(cpu));
  249. }
  250. match = of_match_node(node_matches, np);
  251. data = match->data;
  252. if (data) {
  253. if (data->flag)
  254. fmask = data->freq_mask;
  255. min_cpufreq = fsl_get_sys_freq();
  256. } else {
  257. min_cpufreq = fsl_get_sys_freq() / 2;
  258. }
  259. of_node_put(np);
  260. ret = cpufreq_register_driver(&ppc_corenet_cpufreq_driver);
  261. if (!ret)
  262. pr_info("Freescale PowerPC corenet CPU frequency scaling driver\n");
  263. return ret;
  264. err_mask:
  265. for_each_possible_cpu(cpu)
  266. free_cpumask_var(per_cpu(cpu_mask, cpu));
  267. return -ENOMEM;
  268. }
  269. module_init(ppc_corenet_cpufreq_init);
  270. static void __exit ppc_corenet_cpufreq_exit(void)
  271. {
  272. unsigned int cpu;
  273. for_each_possible_cpu(cpu)
  274. free_cpumask_var(per_cpu(cpu_mask, cpu));
  275. cpufreq_unregister_driver(&ppc_corenet_cpufreq_driver);
  276. }
  277. module_exit(ppc_corenet_cpufreq_exit);
  278. MODULE_LICENSE("GPL");
  279. MODULE_AUTHOR("Tang Yuantian <Yuantian.Tang@freescale.com>");
  280. MODULE_DESCRIPTION("cpufreq driver for Freescale e500mc series SoCs");