ppc-corenet-cpufreq.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/mutex.h>
  18. #include <linux/of.h>
  19. #include <linux/slab.h>
  20. #include <linux/smp.h>
  21. #include <sysdev/fsl_soc.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. u64 u64temp;
  127. np = of_get_cpu_node(cpu, NULL);
  128. if (!np)
  129. return -ENODEV;
  130. data = kzalloc(sizeof(*data), GFP_KERNEL);
  131. if (!data) {
  132. pr_err("%s: no memory\n", __func__);
  133. goto err_np;
  134. }
  135. policy->clk = of_clk_get(np, 0);
  136. if (IS_ERR(policy->clk)) {
  137. pr_err("%s: no clock information\n", __func__);
  138. goto err_nomem2;
  139. }
  140. data->parent = of_parse_phandle(np, "clocks", 0);
  141. if (!data->parent) {
  142. pr_err("%s: could not get clock information\n", __func__);
  143. goto err_nomem2;
  144. }
  145. count = of_property_count_strings(data->parent, "clock-names");
  146. table = kcalloc(count + 1, sizeof(*table), GFP_KERNEL);
  147. if (!table) {
  148. pr_err("%s: no memory\n", __func__);
  149. goto err_node;
  150. }
  151. if (fmask)
  152. mask = fmask[get_hard_smp_processor_id(cpu)];
  153. else
  154. mask = 0x0;
  155. for (i = 0; i < count; i++) {
  156. clk = of_clk_get(data->parent, i);
  157. freq = clk_get_rate(clk);
  158. /*
  159. * the clock is valid if its frequency is not masked
  160. * and large than minimum allowed frequency.
  161. */
  162. if (freq < min_cpufreq || (mask & (1 << i)))
  163. table[i].frequency = CPUFREQ_ENTRY_INVALID;
  164. else
  165. table[i].frequency = freq / 1000;
  166. table[i].driver_data = i;
  167. }
  168. freq_table_redup(table, count);
  169. freq_table_sort(table, count);
  170. table[i].frequency = CPUFREQ_TABLE_END;
  171. /* set the min and max frequency properly */
  172. ret = cpufreq_table_validate_and_show(policy, table);
  173. if (ret) {
  174. pr_err("invalid frequency table: %d\n", ret);
  175. goto err_nomem1;
  176. }
  177. data->table = table;
  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. /* Minimum transition latency is 12 platform clocks */
  183. u64temp = 12ULL * NSEC_PER_SEC;
  184. do_div(u64temp, fsl_get_sys_freq());
  185. policy->cpuinfo.transition_latency = u64temp + 1;
  186. of_node_put(np);
  187. return 0;
  188. err_nomem1:
  189. kfree(table);
  190. err_node:
  191. of_node_put(data->parent);
  192. err_nomem2:
  193. per_cpu(cpu_data, cpu) = NULL;
  194. kfree(data);
  195. err_np:
  196. of_node_put(np);
  197. return -ENODEV;
  198. }
  199. static int __exit corenet_cpufreq_cpu_exit(struct cpufreq_policy *policy)
  200. {
  201. struct cpu_data *data = per_cpu(cpu_data, policy->cpu);
  202. unsigned int cpu;
  203. of_node_put(data->parent);
  204. kfree(data->table);
  205. kfree(data);
  206. for_each_cpu(cpu, per_cpu(cpu_mask, policy->cpu))
  207. per_cpu(cpu_data, cpu) = NULL;
  208. return 0;
  209. }
  210. static int corenet_cpufreq_target(struct cpufreq_policy *policy,
  211. unsigned int index)
  212. {
  213. struct clk *parent;
  214. struct cpu_data *data = per_cpu(cpu_data, policy->cpu);
  215. parent = of_clk_get(data->parent, data->table[index].driver_data);
  216. return clk_set_parent(policy->clk, parent);
  217. }
  218. static struct cpufreq_driver ppc_corenet_cpufreq_driver = {
  219. .name = "ppc_cpufreq",
  220. .flags = CPUFREQ_CONST_LOOPS,
  221. .init = corenet_cpufreq_cpu_init,
  222. .exit = __exit_p(corenet_cpufreq_cpu_exit),
  223. .verify = cpufreq_generic_frequency_table_verify,
  224. .target_index = corenet_cpufreq_target,
  225. .get = cpufreq_generic_get,
  226. .attr = cpufreq_generic_attr,
  227. };
  228. static const struct of_device_id node_matches[] __initdata = {
  229. { .compatible = "fsl,p2041-clockgen", .data = &sdata[0], },
  230. { .compatible = "fsl,p3041-clockgen", .data = &sdata[0], },
  231. { .compatible = "fsl,p5020-clockgen", .data = &sdata[1], },
  232. { .compatible = "fsl,p4080-clockgen", .data = &sdata[2], },
  233. { .compatible = "fsl,p5040-clockgen", .data = &sdata[2], },
  234. { .compatible = "fsl,qoriq-clockgen-2.0", },
  235. {}
  236. };
  237. static int __init ppc_corenet_cpufreq_init(void)
  238. {
  239. int ret;
  240. struct device_node *np;
  241. const struct of_device_id *match;
  242. const struct soc_data *data;
  243. unsigned int cpu;
  244. np = of_find_matching_node(NULL, node_matches);
  245. if (!np)
  246. return -ENODEV;
  247. for_each_possible_cpu(cpu) {
  248. if (!alloc_cpumask_var(&per_cpu(cpu_mask, cpu), GFP_KERNEL))
  249. goto err_mask;
  250. cpumask_copy(per_cpu(cpu_mask, cpu), cpu_core_mask(cpu));
  251. }
  252. match = of_match_node(node_matches, np);
  253. data = match->data;
  254. if (data) {
  255. if (data->flag)
  256. fmask = data->freq_mask;
  257. min_cpufreq = fsl_get_sys_freq();
  258. } else {
  259. min_cpufreq = fsl_get_sys_freq() / 2;
  260. }
  261. of_node_put(np);
  262. ret = cpufreq_register_driver(&ppc_corenet_cpufreq_driver);
  263. if (!ret)
  264. pr_info("Freescale PowerPC corenet CPU frequency scaling driver\n");
  265. return ret;
  266. err_mask:
  267. for_each_possible_cpu(cpu)
  268. free_cpumask_var(per_cpu(cpu_mask, cpu));
  269. return -ENOMEM;
  270. }
  271. module_init(ppc_corenet_cpufreq_init);
  272. static void __exit ppc_corenet_cpufreq_exit(void)
  273. {
  274. unsigned int cpu;
  275. for_each_possible_cpu(cpu)
  276. free_cpumask_var(per_cpu(cpu_mask, cpu));
  277. cpufreq_unregister_driver(&ppc_corenet_cpufreq_driver);
  278. }
  279. module_exit(ppc_corenet_cpufreq_exit);
  280. MODULE_LICENSE("GPL");
  281. MODULE_AUTHOR("Tang Yuantian <Yuantian.Tang@freescale.com>");
  282. MODULE_DESCRIPTION("cpufreq driver for Freescale e500mc series SoCs");