2 * Copyright 2013 Freescale Semiconductor, Inc.
4 * CPU Frequency Scaling driver for Freescale PowerPC corenet SoCs.
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.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/clk.h>
14 #include <linux/cpufreq.h>
15 #include <linux/errno.h>
16 #include <sysdev/fsl_soc.h>
17 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
22 #include <linux/slab.h>
23 #include <linux/smp.h>
24 #include <sysdev/fsl_soc.h>
27 * struct cpu_data - per CPU data struct
28 * @parent: the parent node of cpu clock
29 * @table: frequency table
32 struct device_node *parent;
33 struct cpufreq_frequency_table *table;
37 * struct soc_data - SoC specific data
38 * @freq_mask: mask the disallowed frequencies
47 /* see hardware specification for the allowed frqeuencies */
48 static const struct soc_data sdata[] = {
49 { /* used by p2041 and p3041 */
50 .freq_mask = {0x8, 0x8, 0x2, 0x2},
54 .freq_mask = {0x8, 0x2},
57 { /* used by p4080, p5040 */
64 * the minimum allowed core frequency, in Hz
65 * for chassis v1.0, >= platform frequency
66 * for chassis v2.0, >= platform frequency / 2
68 static u32 min_cpufreq;
69 static const u32 *fmask;
71 static DEFINE_PER_CPU(struct cpu_data *, cpu_data);
73 /* cpumask in a cluster */
74 static DEFINE_PER_CPU(cpumask_var_t, cpu_mask);
77 static inline const struct cpumask *cpu_core_mask(int cpu)
83 /* reduce the duplicated frequencies in frequency table */
84 static void freq_table_redup(struct cpufreq_frequency_table *freq_table,
89 for (i = 1; i < count; i++) {
90 for (j = 0; j < i; j++) {
91 if (freq_table[j].frequency == CPUFREQ_ENTRY_INVALID ||
92 freq_table[j].frequency !=
93 freq_table[i].frequency)
96 freq_table[i].frequency = CPUFREQ_ENTRY_INVALID;
102 /* sort the frequencies in frequency table in descenting order */
103 static void freq_table_sort(struct cpufreq_frequency_table *freq_table,
107 unsigned int freq, max_freq;
108 struct cpufreq_frequency_table table;
109 for (i = 0; i < count - 1; i++) {
110 max_freq = freq_table[i].frequency;
112 for (j = i + 1; j < count; j++) {
113 freq = freq_table[j].frequency;
114 if (freq == CPUFREQ_ENTRY_INVALID ||
122 /* exchange the frequencies */
123 table.driver_data = freq_table[i].driver_data;
124 table.frequency = freq_table[i].frequency;
125 freq_table[i].driver_data = freq_table[ind].driver_data;
126 freq_table[i].frequency = freq_table[ind].frequency;
127 freq_table[ind].driver_data = table.driver_data;
128 freq_table[ind].frequency = table.frequency;
133 static int corenet_cpufreq_cpu_init(struct cpufreq_policy *policy)
135 struct device_node *np;
139 struct cpufreq_frequency_table *table;
140 struct cpu_data *data;
141 unsigned int cpu = policy->cpu;
143 np = of_get_cpu_node(cpu, NULL);
147 data = kzalloc(sizeof(*data), GFP_KERNEL);
149 pr_err("%s: no memory\n", __func__);
153 policy->clk = of_clk_get(np, 0);
154 if (IS_ERR(policy->clk)) {
155 pr_err("%s: no clock information\n", __func__);
159 data->parent = of_parse_phandle(np, "clocks", 0);
161 pr_err("%s: could not get clock information\n", __func__);
165 count = of_property_count_strings(data->parent, "clock-names");
166 table = kcalloc(count + 1, sizeof(*table), GFP_KERNEL);
168 pr_err("%s: no memory\n", __func__);
173 mask = fmask[get_hard_smp_processor_id(cpu)];
177 for (i = 0; i < count; i++) {
178 clk = of_clk_get(data->parent, i);
179 freq = clk_get_rate(clk);
181 * the clock is valid if its frequency is not masked
182 * and large than minimum allowed frequency.
184 if (freq < min_cpufreq || (mask & (1 << i)))
185 table[i].frequency = CPUFREQ_ENTRY_INVALID;
187 table[i].frequency = freq / 1000;
188 table[i].driver_data = i;
190 freq_table_redup(table, count);
191 freq_table_sort(table, count);
192 table[i].frequency = CPUFREQ_TABLE_END;
194 /* set the min and max frequency properly */
195 ret = cpufreq_table_validate_and_show(policy, table);
197 pr_err("invalid frequency table: %d\n", ret);
202 per_cpu(cpu_data, cpu) = data;
204 /* update ->cpus if we have cluster, no harm if not */
205 cpumask_copy(policy->cpus, per_cpu(cpu_mask, cpu));
206 for_each_cpu(i, per_cpu(cpu_mask, cpu))
207 per_cpu(cpu_data, i) = data;
209 policy->cpuinfo.transition_latency =
210 (12 * NSEC_PER_SEC) / fsl_get_sys_freq();
218 of_node_put(data->parent);
220 per_cpu(cpu_data, cpu) = NULL;
228 static int __exit corenet_cpufreq_cpu_exit(struct cpufreq_policy *policy)
230 struct cpu_data *data = per_cpu(cpu_data, policy->cpu);
233 of_node_put(data->parent);
237 for_each_cpu(cpu, per_cpu(cpu_mask, policy->cpu))
238 per_cpu(cpu_data, cpu) = NULL;
243 static int corenet_cpufreq_target(struct cpufreq_policy *policy,
247 struct cpu_data *data = per_cpu(cpu_data, policy->cpu);
249 parent = of_clk_get(data->parent, data->table[index].driver_data);
250 return clk_set_parent(policy->clk, parent);
253 static struct cpufreq_driver ppc_corenet_cpufreq_driver = {
254 .name = "ppc_cpufreq",
255 .flags = CPUFREQ_CONST_LOOPS,
256 .init = corenet_cpufreq_cpu_init,
257 .exit = __exit_p(corenet_cpufreq_cpu_exit),
258 .verify = cpufreq_generic_frequency_table_verify,
259 .target_index = corenet_cpufreq_target,
260 .get = cpufreq_generic_get,
261 .attr = cpufreq_generic_attr,
264 static const struct of_device_id node_matches[] __initdata = {
265 { .compatible = "fsl,p2041-clockgen", .data = &sdata[0], },
266 { .compatible = "fsl,p3041-clockgen", .data = &sdata[0], },
267 { .compatible = "fsl,p5020-clockgen", .data = &sdata[1], },
268 { .compatible = "fsl,p4080-clockgen", .data = &sdata[2], },
269 { .compatible = "fsl,p5040-clockgen", .data = &sdata[2], },
270 { .compatible = "fsl,qoriq-clockgen-2.0", },
274 static int __init ppc_corenet_cpufreq_init(void)
277 struct device_node *np;
278 const struct of_device_id *match;
279 const struct soc_data *data;
282 np = of_find_matching_node(NULL, node_matches);
286 for_each_possible_cpu(cpu) {
287 if (!alloc_cpumask_var(&per_cpu(cpu_mask, cpu), GFP_KERNEL))
289 cpumask_copy(per_cpu(cpu_mask, cpu), cpu_core_mask(cpu));
292 match = of_match_node(node_matches, np);
296 fmask = data->freq_mask;
297 min_cpufreq = fsl_get_sys_freq();
299 min_cpufreq = fsl_get_sys_freq() / 2;
304 ret = cpufreq_register_driver(&ppc_corenet_cpufreq_driver);
306 pr_info("Freescale PowerPC corenet CPU frequency scaling driver\n");
311 for_each_possible_cpu(cpu)
312 free_cpumask_var(per_cpu(cpu_mask, cpu));
316 module_init(ppc_corenet_cpufreq_init);
318 static void __exit ppc_corenet_cpufreq_exit(void)
322 for_each_possible_cpu(cpu)
323 free_cpumask_var(per_cpu(cpu_mask, cpu));
325 cpufreq_unregister_driver(&ppc_corenet_cpufreq_driver);
327 module_exit(ppc_corenet_cpufreq_exit);
329 MODULE_LICENSE("GPL");
330 MODULE_AUTHOR("Tang Yuantian <Yuantian.Tang@freescale.com>");
331 MODULE_DESCRIPTION("cpufreq driver for Freescale e500mc series SoCs");