qoriq-cpufreq.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * Copyright 2013 Freescale Semiconductor, Inc.
  3. *
  4. * CPU Frequency Scaling driver for Freescale QorIQ 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. #if !defined(CONFIG_ARM)
  22. #include <asm/smp.h> /* for get_hard_smp_processor_id() in UP configs */
  23. #endif
  24. /**
  25. * struct cpu_data
  26. * @parent: the parent node of cpu clock
  27. * @table: frequency table
  28. */
  29. struct cpu_data {
  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. #if defined(CONFIG_ARM)
  66. static int get_cpu_physical_id(int cpu)
  67. {
  68. return topology_core_id(cpu);
  69. }
  70. #else
  71. static int get_cpu_physical_id(int cpu)
  72. {
  73. return get_hard_smp_processor_id(cpu);
  74. }
  75. #endif
  76. static u32 get_bus_freq(void)
  77. {
  78. struct device_node *soc;
  79. u32 sysfreq;
  80. soc = of_find_node_by_type(NULL, "soc");
  81. if (!soc)
  82. return 0;
  83. if (of_property_read_u32(soc, "bus-frequency", &sysfreq))
  84. sysfreq = 0;
  85. of_node_put(soc);
  86. return sysfreq;
  87. }
  88. static struct device_node *cpu_to_clk_node(int cpu)
  89. {
  90. struct device_node *np, *clk_np;
  91. if (!cpu_present(cpu))
  92. return NULL;
  93. np = of_get_cpu_node(cpu, NULL);
  94. if (!np)
  95. return NULL;
  96. clk_np = of_parse_phandle(np, "clocks", 0);
  97. if (!clk_np)
  98. return NULL;
  99. of_node_put(np);
  100. return clk_np;
  101. }
  102. /* traverse cpu nodes to get cpu mask of sharing clock wire */
  103. static void set_affected_cpus(struct cpufreq_policy *policy)
  104. {
  105. struct device_node *np, *clk_np;
  106. struct cpumask *dstp = policy->cpus;
  107. int i;
  108. np = cpu_to_clk_node(policy->cpu);
  109. if (!np)
  110. return;
  111. for_each_present_cpu(i) {
  112. clk_np = cpu_to_clk_node(i);
  113. if (!clk_np)
  114. continue;
  115. if (clk_np == np)
  116. cpumask_set_cpu(i, dstp);
  117. of_node_put(clk_np);
  118. }
  119. of_node_put(np);
  120. }
  121. /* reduce the duplicated frequencies in frequency table */
  122. static void freq_table_redup(struct cpufreq_frequency_table *freq_table,
  123. int count)
  124. {
  125. int i, j;
  126. for (i = 1; i < count; i++) {
  127. for (j = 0; j < i; j++) {
  128. if (freq_table[j].frequency == CPUFREQ_ENTRY_INVALID ||
  129. freq_table[j].frequency !=
  130. freq_table[i].frequency)
  131. continue;
  132. freq_table[i].frequency = CPUFREQ_ENTRY_INVALID;
  133. break;
  134. }
  135. }
  136. }
  137. /* sort the frequencies in frequency table in descenting order */
  138. static void freq_table_sort(struct cpufreq_frequency_table *freq_table,
  139. int count)
  140. {
  141. int i, j, ind;
  142. unsigned int freq, max_freq;
  143. struct cpufreq_frequency_table table;
  144. for (i = 0; i < count - 1; i++) {
  145. max_freq = freq_table[i].frequency;
  146. ind = i;
  147. for (j = i + 1; j < count; j++) {
  148. freq = freq_table[j].frequency;
  149. if (freq == CPUFREQ_ENTRY_INVALID ||
  150. freq <= max_freq)
  151. continue;
  152. ind = j;
  153. max_freq = freq;
  154. }
  155. if (ind != i) {
  156. /* exchange the frequencies */
  157. table.driver_data = freq_table[i].driver_data;
  158. table.frequency = freq_table[i].frequency;
  159. freq_table[i].driver_data = freq_table[ind].driver_data;
  160. freq_table[i].frequency = freq_table[ind].frequency;
  161. freq_table[ind].driver_data = table.driver_data;
  162. freq_table[ind].frequency = table.frequency;
  163. }
  164. }
  165. }
  166. static int qoriq_cpufreq_cpu_init(struct cpufreq_policy *policy)
  167. {
  168. struct device_node *np;
  169. int i, count, ret;
  170. u32 freq, mask;
  171. struct clk *clk;
  172. struct cpufreq_frequency_table *table;
  173. struct cpu_data *data;
  174. unsigned int cpu = policy->cpu;
  175. u64 u64temp;
  176. np = of_get_cpu_node(cpu, NULL);
  177. if (!np)
  178. return -ENODEV;
  179. data = kzalloc(sizeof(*data), GFP_KERNEL);
  180. if (!data)
  181. goto err_np;
  182. policy->clk = of_clk_get(np, 0);
  183. if (IS_ERR(policy->clk)) {
  184. pr_err("%s: no clock information\n", __func__);
  185. goto err_nomem2;
  186. }
  187. data->parent = of_parse_phandle(np, "clocks", 0);
  188. if (!data->parent) {
  189. pr_err("%s: could not get clock information\n", __func__);
  190. goto err_nomem2;
  191. }
  192. count = of_property_count_strings(data->parent, "clock-names");
  193. table = kcalloc(count + 1, sizeof(*table), GFP_KERNEL);
  194. if (!table) {
  195. pr_err("%s: no memory\n", __func__);
  196. goto err_node;
  197. }
  198. if (fmask)
  199. mask = fmask[get_cpu_physical_id(cpu)];
  200. else
  201. mask = 0x0;
  202. for (i = 0; i < count; i++) {
  203. clk = of_clk_get(data->parent, i);
  204. freq = clk_get_rate(clk);
  205. /*
  206. * the clock is valid if its frequency is not masked
  207. * and large than minimum allowed frequency.
  208. */
  209. if (freq < min_cpufreq || (mask & (1 << i)))
  210. table[i].frequency = CPUFREQ_ENTRY_INVALID;
  211. else
  212. table[i].frequency = freq / 1000;
  213. table[i].driver_data = i;
  214. }
  215. freq_table_redup(table, count);
  216. freq_table_sort(table, count);
  217. table[i].frequency = CPUFREQ_TABLE_END;
  218. /* set the min and max frequency properly */
  219. ret = cpufreq_table_validate_and_show(policy, table);
  220. if (ret) {
  221. pr_err("invalid frequency table: %d\n", ret);
  222. goto err_nomem1;
  223. }
  224. data->table = table;
  225. /* update ->cpus if we have cluster, no harm if not */
  226. set_affected_cpus(policy);
  227. policy->driver_data = data;
  228. /* Minimum transition latency is 12 platform clocks */
  229. u64temp = 12ULL * NSEC_PER_SEC;
  230. do_div(u64temp, get_bus_freq());
  231. policy->cpuinfo.transition_latency = u64temp + 1;
  232. of_node_put(np);
  233. return 0;
  234. err_nomem1:
  235. kfree(table);
  236. err_node:
  237. of_node_put(data->parent);
  238. err_nomem2:
  239. policy->driver_data = NULL;
  240. kfree(data);
  241. err_np:
  242. of_node_put(np);
  243. return -ENODEV;
  244. }
  245. static int __exit qoriq_cpufreq_cpu_exit(struct cpufreq_policy *policy)
  246. {
  247. struct cpu_data *data = policy->driver_data;
  248. of_node_put(data->parent);
  249. kfree(data->table);
  250. kfree(data);
  251. policy->driver_data = NULL;
  252. return 0;
  253. }
  254. static int qoriq_cpufreq_target(struct cpufreq_policy *policy,
  255. unsigned int index)
  256. {
  257. struct clk *parent;
  258. struct cpu_data *data = policy->driver_data;
  259. parent = of_clk_get(data->parent, data->table[index].driver_data);
  260. return clk_set_parent(policy->clk, parent);
  261. }
  262. static struct cpufreq_driver qoriq_cpufreq_driver = {
  263. .name = "qoriq_cpufreq",
  264. .flags = CPUFREQ_CONST_LOOPS,
  265. .init = qoriq_cpufreq_cpu_init,
  266. .exit = __exit_p(qoriq_cpufreq_cpu_exit),
  267. .verify = cpufreq_generic_frequency_table_verify,
  268. .target_index = qoriq_cpufreq_target,
  269. .get = cpufreq_generic_get,
  270. .attr = cpufreq_generic_attr,
  271. };
  272. static const struct of_device_id node_matches[] __initconst = {
  273. { .compatible = "fsl,p2041-clockgen", .data = &sdata[0], },
  274. { .compatible = "fsl,p3041-clockgen", .data = &sdata[0], },
  275. { .compatible = "fsl,p5020-clockgen", .data = &sdata[1], },
  276. { .compatible = "fsl,p4080-clockgen", .data = &sdata[2], },
  277. { .compatible = "fsl,p5040-clockgen", .data = &sdata[2], },
  278. { .compatible = "fsl,qoriq-clockgen-2.0", },
  279. {}
  280. };
  281. static int __init qoriq_cpufreq_init(void)
  282. {
  283. int ret;
  284. struct device_node *np;
  285. const struct of_device_id *match;
  286. const struct soc_data *data;
  287. np = of_find_matching_node(NULL, node_matches);
  288. if (!np)
  289. return -ENODEV;
  290. match = of_match_node(node_matches, np);
  291. data = match->data;
  292. if (data) {
  293. if (data->flag)
  294. fmask = data->freq_mask;
  295. min_cpufreq = get_bus_freq();
  296. } else {
  297. min_cpufreq = get_bus_freq() / 2;
  298. }
  299. of_node_put(np);
  300. ret = cpufreq_register_driver(&qoriq_cpufreq_driver);
  301. if (!ret)
  302. pr_info("Freescale QorIQ CPU frequency scaling driver\n");
  303. return ret;
  304. }
  305. module_init(qoriq_cpufreq_init);
  306. static void __exit qoriq_cpufreq_exit(void)
  307. {
  308. cpufreq_unregister_driver(&qoriq_cpufreq_driver);
  309. }
  310. module_exit(qoriq_cpufreq_exit);
  311. MODULE_LICENSE("GPL");
  312. MODULE_AUTHOR("Tang Yuantian <Yuantian.Tang@freescale.com>");
  313. MODULE_DESCRIPTION("cpufreq driver for Freescale QorIQ series SoCs");