qoriq-cpufreq.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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/cpu_cooling.h>
  14. #include <linux/errno.h>
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/mutex.h>
  19. #include <linux/of.h>
  20. #include <linux/slab.h>
  21. #include <linux/smp.h>
  22. #if !defined(CONFIG_ARM)
  23. #include <asm/smp.h> /* for get_hard_smp_processor_id() in UP configs */
  24. #endif
  25. /**
  26. * struct cpu_data
  27. * @pclk: the parent clock of cpu
  28. * @table: frequency table
  29. */
  30. struct cpu_data {
  31. struct clk **pclk;
  32. struct cpufreq_frequency_table *table;
  33. struct thermal_cooling_device *cdev;
  34. };
  35. /**
  36. * struct soc_data - SoC specific data
  37. * @freq_mask: mask the disallowed frequencies
  38. * @flag: unique flags
  39. */
  40. struct soc_data {
  41. u32 freq_mask[4];
  42. u32 flag;
  43. };
  44. #define FREQ_MASK 1
  45. /* see hardware specification for the allowed frqeuencies */
  46. static const struct soc_data sdata[] = {
  47. { /* used by p2041 and p3041 */
  48. .freq_mask = {0x8, 0x8, 0x2, 0x2},
  49. .flag = FREQ_MASK,
  50. },
  51. { /* used by p5020 */
  52. .freq_mask = {0x8, 0x2},
  53. .flag = FREQ_MASK,
  54. },
  55. { /* used by p4080, p5040 */
  56. .freq_mask = {0},
  57. .flag = 0,
  58. },
  59. };
  60. /*
  61. * the minimum allowed core frequency, in Hz
  62. * for chassis v1.0, >= platform frequency
  63. * for chassis v2.0, >= platform frequency / 2
  64. */
  65. static u32 min_cpufreq;
  66. static const u32 *fmask;
  67. #if defined(CONFIG_ARM)
  68. static int get_cpu_physical_id(int cpu)
  69. {
  70. return topology_core_id(cpu);
  71. }
  72. #else
  73. static int get_cpu_physical_id(int cpu)
  74. {
  75. return get_hard_smp_processor_id(cpu);
  76. }
  77. #endif
  78. static u32 get_bus_freq(void)
  79. {
  80. struct device_node *soc;
  81. u32 sysfreq;
  82. soc = of_find_node_by_type(NULL, "soc");
  83. if (!soc)
  84. return 0;
  85. if (of_property_read_u32(soc, "bus-frequency", &sysfreq))
  86. sysfreq = 0;
  87. of_node_put(soc);
  88. return sysfreq;
  89. }
  90. static struct device_node *cpu_to_clk_node(int cpu)
  91. {
  92. struct device_node *np, *clk_np;
  93. if (!cpu_present(cpu))
  94. return NULL;
  95. np = of_get_cpu_node(cpu, NULL);
  96. if (!np)
  97. return NULL;
  98. clk_np = of_parse_phandle(np, "clocks", 0);
  99. if (!clk_np)
  100. return NULL;
  101. of_node_put(np);
  102. return clk_np;
  103. }
  104. /* traverse cpu nodes to get cpu mask of sharing clock wire */
  105. static void set_affected_cpus(struct cpufreq_policy *policy)
  106. {
  107. struct device_node *np, *clk_np;
  108. struct cpumask *dstp = policy->cpus;
  109. int i;
  110. np = cpu_to_clk_node(policy->cpu);
  111. if (!np)
  112. return;
  113. for_each_present_cpu(i) {
  114. clk_np = cpu_to_clk_node(i);
  115. if (!clk_np)
  116. continue;
  117. if (clk_np == np)
  118. cpumask_set_cpu(i, dstp);
  119. of_node_put(clk_np);
  120. }
  121. of_node_put(np);
  122. }
  123. /* reduce the duplicated frequencies in frequency table */
  124. static void freq_table_redup(struct cpufreq_frequency_table *freq_table,
  125. int count)
  126. {
  127. int i, j;
  128. for (i = 1; i < count; i++) {
  129. for (j = 0; j < i; j++) {
  130. if (freq_table[j].frequency == CPUFREQ_ENTRY_INVALID ||
  131. freq_table[j].frequency !=
  132. freq_table[i].frequency)
  133. continue;
  134. freq_table[i].frequency = CPUFREQ_ENTRY_INVALID;
  135. break;
  136. }
  137. }
  138. }
  139. /* sort the frequencies in frequency table in descenting order */
  140. static void freq_table_sort(struct cpufreq_frequency_table *freq_table,
  141. int count)
  142. {
  143. int i, j, ind;
  144. unsigned int freq, max_freq;
  145. struct cpufreq_frequency_table table;
  146. for (i = 0; i < count - 1; i++) {
  147. max_freq = freq_table[i].frequency;
  148. ind = i;
  149. for (j = i + 1; j < count; j++) {
  150. freq = freq_table[j].frequency;
  151. if (freq == CPUFREQ_ENTRY_INVALID ||
  152. freq <= max_freq)
  153. continue;
  154. ind = j;
  155. max_freq = freq;
  156. }
  157. if (ind != i) {
  158. /* exchange the frequencies */
  159. table.driver_data = freq_table[i].driver_data;
  160. table.frequency = freq_table[i].frequency;
  161. freq_table[i].driver_data = freq_table[ind].driver_data;
  162. freq_table[i].frequency = freq_table[ind].frequency;
  163. freq_table[ind].driver_data = table.driver_data;
  164. freq_table[ind].frequency = table.frequency;
  165. }
  166. }
  167. }
  168. static int qoriq_cpufreq_cpu_init(struct cpufreq_policy *policy)
  169. {
  170. struct device_node *np, *pnode;
  171. int i, count, ret;
  172. u32 freq, mask;
  173. struct clk *clk;
  174. struct cpufreq_frequency_table *table;
  175. struct cpu_data *data;
  176. unsigned int cpu = policy->cpu;
  177. u64 u64temp;
  178. np = of_get_cpu_node(cpu, NULL);
  179. if (!np)
  180. return -ENODEV;
  181. data = kzalloc(sizeof(*data), GFP_KERNEL);
  182. if (!data)
  183. goto err_np;
  184. policy->clk = of_clk_get(np, 0);
  185. if (IS_ERR(policy->clk)) {
  186. pr_err("%s: no clock information\n", __func__);
  187. goto err_nomem2;
  188. }
  189. pnode = of_parse_phandle(np, "clocks", 0);
  190. if (!pnode) {
  191. pr_err("%s: could not get clock information\n", __func__);
  192. goto err_nomem2;
  193. }
  194. count = of_property_count_strings(pnode, "clock-names");
  195. data->pclk = kcalloc(count, sizeof(struct clk *), GFP_KERNEL);
  196. if (!data->pclk) {
  197. pr_err("%s: no memory\n", __func__);
  198. goto err_node;
  199. }
  200. table = kcalloc(count + 1, sizeof(*table), GFP_KERNEL);
  201. if (!table) {
  202. pr_err("%s: no memory\n", __func__);
  203. goto err_pclk;
  204. }
  205. if (fmask)
  206. mask = fmask[get_cpu_physical_id(cpu)];
  207. else
  208. mask = 0x0;
  209. for (i = 0; i < count; i++) {
  210. clk = of_clk_get(pnode, i);
  211. data->pclk[i] = clk;
  212. freq = clk_get_rate(clk);
  213. /*
  214. * the clock is valid if its frequency is not masked
  215. * and large than minimum allowed frequency.
  216. */
  217. if (freq < min_cpufreq || (mask & (1 << i)))
  218. table[i].frequency = CPUFREQ_ENTRY_INVALID;
  219. else
  220. table[i].frequency = freq / 1000;
  221. table[i].driver_data = i;
  222. }
  223. freq_table_redup(table, count);
  224. freq_table_sort(table, count);
  225. table[i].frequency = CPUFREQ_TABLE_END;
  226. /* set the min and max frequency properly */
  227. ret = cpufreq_table_validate_and_show(policy, table);
  228. if (ret) {
  229. pr_err("invalid frequency table: %d\n", ret);
  230. goto err_nomem1;
  231. }
  232. data->table = table;
  233. /* update ->cpus if we have cluster, no harm if not */
  234. set_affected_cpus(policy);
  235. policy->driver_data = data;
  236. /* Minimum transition latency is 12 platform clocks */
  237. u64temp = 12ULL * NSEC_PER_SEC;
  238. do_div(u64temp, get_bus_freq());
  239. policy->cpuinfo.transition_latency = u64temp + 1;
  240. of_node_put(np);
  241. of_node_put(pnode);
  242. return 0;
  243. err_nomem1:
  244. kfree(table);
  245. err_pclk:
  246. kfree(data->pclk);
  247. err_node:
  248. of_node_put(pnode);
  249. err_nomem2:
  250. policy->driver_data = NULL;
  251. kfree(data);
  252. err_np:
  253. of_node_put(np);
  254. return -ENODEV;
  255. }
  256. static int qoriq_cpufreq_cpu_exit(struct cpufreq_policy *policy)
  257. {
  258. struct cpu_data *data = policy->driver_data;
  259. cpufreq_cooling_unregister(data->cdev);
  260. kfree(data->pclk);
  261. kfree(data->table);
  262. kfree(data);
  263. policy->driver_data = NULL;
  264. return 0;
  265. }
  266. static int qoriq_cpufreq_target(struct cpufreq_policy *policy,
  267. unsigned int index)
  268. {
  269. struct clk *parent;
  270. struct cpu_data *data = policy->driver_data;
  271. parent = data->pclk[data->table[index].driver_data];
  272. return clk_set_parent(policy->clk, parent);
  273. }
  274. static void qoriq_cpufreq_ready(struct cpufreq_policy *policy)
  275. {
  276. struct cpu_data *cpud = policy->driver_data;
  277. struct device_node *np = of_get_cpu_node(policy->cpu, NULL);
  278. if (of_find_property(np, "#cooling-cells", NULL)) {
  279. cpud->cdev = of_cpufreq_cooling_register(np,
  280. policy->related_cpus);
  281. if (IS_ERR(cpud->cdev) && PTR_ERR(cpud->cdev) != -ENOSYS) {
  282. pr_err("cpu%d is not running as cooling device: %ld\n",
  283. policy->cpu, PTR_ERR(cpud->cdev));
  284. cpud->cdev = NULL;
  285. }
  286. }
  287. of_node_put(np);
  288. }
  289. static struct cpufreq_driver qoriq_cpufreq_driver = {
  290. .name = "qoriq_cpufreq",
  291. .flags = CPUFREQ_CONST_LOOPS,
  292. .init = qoriq_cpufreq_cpu_init,
  293. .exit = qoriq_cpufreq_cpu_exit,
  294. .verify = cpufreq_generic_frequency_table_verify,
  295. .target_index = qoriq_cpufreq_target,
  296. .get = cpufreq_generic_get,
  297. .ready = qoriq_cpufreq_ready,
  298. .attr = cpufreq_generic_attr,
  299. };
  300. static const struct of_device_id node_matches[] __initconst = {
  301. { .compatible = "fsl,p2041-clockgen", .data = &sdata[0], },
  302. { .compatible = "fsl,p3041-clockgen", .data = &sdata[0], },
  303. { .compatible = "fsl,p5020-clockgen", .data = &sdata[1], },
  304. { .compatible = "fsl,p4080-clockgen", .data = &sdata[2], },
  305. { .compatible = "fsl,p5040-clockgen", .data = &sdata[2], },
  306. { .compatible = "fsl,qoriq-clockgen-2.0", },
  307. {}
  308. };
  309. static int __init qoriq_cpufreq_init(void)
  310. {
  311. int ret;
  312. struct device_node *np;
  313. const struct of_device_id *match;
  314. const struct soc_data *data;
  315. np = of_find_matching_node(NULL, node_matches);
  316. if (!np)
  317. return -ENODEV;
  318. match = of_match_node(node_matches, np);
  319. data = match->data;
  320. if (data) {
  321. if (data->flag)
  322. fmask = data->freq_mask;
  323. min_cpufreq = get_bus_freq();
  324. } else {
  325. min_cpufreq = get_bus_freq() / 2;
  326. }
  327. of_node_put(np);
  328. ret = cpufreq_register_driver(&qoriq_cpufreq_driver);
  329. if (!ret)
  330. pr_info("Freescale QorIQ CPU frequency scaling driver\n");
  331. return ret;
  332. }
  333. module_init(qoriq_cpufreq_init);
  334. static void __exit qoriq_cpufreq_exit(void)
  335. {
  336. cpufreq_unregister_driver(&qoriq_cpufreq_driver);
  337. }
  338. module_exit(qoriq_cpufreq_exit);
  339. MODULE_LICENSE("GPL");
  340. MODULE_AUTHOR("Tang Yuantian <Yuantian.Tang@freescale.com>");
  341. MODULE_DESCRIPTION("cpufreq driver for Freescale QorIQ series SoCs");