ppc-corenet-cpufreq.c 8.1 KB

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