ppc-corenet-cpufreq.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. per_cpu(cpu_data, cpu) = data;
  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");