qoriq-cpufreq.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. * @pclk: the parent clock of cpu
  27. * @table: frequency table
  28. */
  29. struct cpu_data {
  30. struct clk **pclk;
  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, *pnode;
  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. pnode = of_parse_phandle(np, "clocks", 0);
  188. if (!pnode) {
  189. pr_err("%s: could not get clock information\n", __func__);
  190. goto err_nomem2;
  191. }
  192. count = of_property_count_strings(pnode, "clock-names");
  193. data->pclk = kcalloc(count, sizeof(struct clk *), GFP_KERNEL);
  194. if (!data->pclk) {
  195. pr_err("%s: no memory\n", __func__);
  196. goto err_node;
  197. }
  198. table = kcalloc(count + 1, sizeof(*table), GFP_KERNEL);
  199. if (!table) {
  200. pr_err("%s: no memory\n", __func__);
  201. goto err_pclk;
  202. }
  203. if (fmask)
  204. mask = fmask[get_cpu_physical_id(cpu)];
  205. else
  206. mask = 0x0;
  207. for (i = 0; i < count; i++) {
  208. clk = of_clk_get(pnode, i);
  209. data->pclk[i] = clk;
  210. freq = clk_get_rate(clk);
  211. /*
  212. * the clock is valid if its frequency is not masked
  213. * and large than minimum allowed frequency.
  214. */
  215. if (freq < min_cpufreq || (mask & (1 << i)))
  216. table[i].frequency = CPUFREQ_ENTRY_INVALID;
  217. else
  218. table[i].frequency = freq / 1000;
  219. table[i].driver_data = i;
  220. }
  221. freq_table_redup(table, count);
  222. freq_table_sort(table, count);
  223. table[i].frequency = CPUFREQ_TABLE_END;
  224. /* set the min and max frequency properly */
  225. ret = cpufreq_table_validate_and_show(policy, table);
  226. if (ret) {
  227. pr_err("invalid frequency table: %d\n", ret);
  228. goto err_nomem1;
  229. }
  230. data->table = table;
  231. /* update ->cpus if we have cluster, no harm if not */
  232. set_affected_cpus(policy);
  233. policy->driver_data = data;
  234. /* Minimum transition latency is 12 platform clocks */
  235. u64temp = 12ULL * NSEC_PER_SEC;
  236. do_div(u64temp, get_bus_freq());
  237. policy->cpuinfo.transition_latency = u64temp + 1;
  238. of_node_put(np);
  239. of_node_put(pnode);
  240. return 0;
  241. err_nomem1:
  242. kfree(table);
  243. err_pclk:
  244. kfree(data->pclk);
  245. err_node:
  246. of_node_put(pnode);
  247. err_nomem2:
  248. policy->driver_data = NULL;
  249. kfree(data);
  250. err_np:
  251. of_node_put(np);
  252. return -ENODEV;
  253. }
  254. static int __exit qoriq_cpufreq_cpu_exit(struct cpufreq_policy *policy)
  255. {
  256. struct cpu_data *data = policy->driver_data;
  257. kfree(data->pclk);
  258. kfree(data->table);
  259. kfree(data);
  260. policy->driver_data = NULL;
  261. return 0;
  262. }
  263. static int qoriq_cpufreq_target(struct cpufreq_policy *policy,
  264. unsigned int index)
  265. {
  266. struct clk *parent;
  267. struct cpu_data *data = policy->driver_data;
  268. parent = data->pclk[data->table[index].driver_data];
  269. return clk_set_parent(policy->clk, parent);
  270. }
  271. static struct cpufreq_driver qoriq_cpufreq_driver = {
  272. .name = "qoriq_cpufreq",
  273. .flags = CPUFREQ_CONST_LOOPS,
  274. .init = qoriq_cpufreq_cpu_init,
  275. .exit = __exit_p(qoriq_cpufreq_cpu_exit),
  276. .verify = cpufreq_generic_frequency_table_verify,
  277. .target_index = qoriq_cpufreq_target,
  278. .get = cpufreq_generic_get,
  279. .attr = cpufreq_generic_attr,
  280. };
  281. static const struct of_device_id node_matches[] __initconst = {
  282. { .compatible = "fsl,p2041-clockgen", .data = &sdata[0], },
  283. { .compatible = "fsl,p3041-clockgen", .data = &sdata[0], },
  284. { .compatible = "fsl,p5020-clockgen", .data = &sdata[1], },
  285. { .compatible = "fsl,p4080-clockgen", .data = &sdata[2], },
  286. { .compatible = "fsl,p5040-clockgen", .data = &sdata[2], },
  287. { .compatible = "fsl,qoriq-clockgen-2.0", },
  288. {}
  289. };
  290. static int __init qoriq_cpufreq_init(void)
  291. {
  292. int ret;
  293. struct device_node *np;
  294. const struct of_device_id *match;
  295. const struct soc_data *data;
  296. np = of_find_matching_node(NULL, node_matches);
  297. if (!np)
  298. return -ENODEV;
  299. match = of_match_node(node_matches, np);
  300. data = match->data;
  301. if (data) {
  302. if (data->flag)
  303. fmask = data->freq_mask;
  304. min_cpufreq = get_bus_freq();
  305. } else {
  306. min_cpufreq = get_bus_freq() / 2;
  307. }
  308. of_node_put(np);
  309. ret = cpufreq_register_driver(&qoriq_cpufreq_driver);
  310. if (!ret)
  311. pr_info("Freescale QorIQ CPU frequency scaling driver\n");
  312. return ret;
  313. }
  314. module_init(qoriq_cpufreq_init);
  315. static void __exit qoriq_cpufreq_exit(void)
  316. {
  317. cpufreq_unregister_driver(&qoriq_cpufreq_driver);
  318. }
  319. module_exit(qoriq_cpufreq_exit);
  320. MODULE_LICENSE("GPL");
  321. MODULE_AUTHOR("Tang Yuantian <Yuantian.Tang@freescale.com>");
  322. MODULE_DESCRIPTION("cpufreq driver for Freescale QorIQ series SoCs");