ppc-corenet-cpufreq.c 8.0 KB

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