2 * Copyright 2013 Freescale Semiconductor, Inc.
4 * CPU Frequency Scaling driver for Freescale QorIQ 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/cpu_cooling.h>
16 #include <linux/errno.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>
25 #if !defined(CONFIG_ARM)
26 #include <asm/smp.h> /* for get_hard_smp_processor_id() in UP configs */
31 * @pclk: the parent clock of cpu
32 * @table: frequency table
36 struct cpufreq_frequency_table
*table
;
37 struct thermal_cooling_device
*cdev
;
41 * struct soc_data - SoC specific data
42 * @freq_mask: mask the disallowed frequencies
51 /* see hardware specification for the allowed frqeuencies */
52 static const struct soc_data sdata
[] = {
53 { /* used by p2041 and p3041 */
54 .freq_mask
= {0x8, 0x8, 0x2, 0x2},
58 .freq_mask
= {0x8, 0x2},
61 { /* used by p4080, p5040 */
68 * the minimum allowed core frequency, in Hz
69 * for chassis v1.0, >= platform frequency
70 * for chassis v2.0, >= platform frequency / 2
72 static u32 min_cpufreq
;
73 static const u32
*fmask
;
75 #if defined(CONFIG_ARM)
76 static int get_cpu_physical_id(int cpu
)
78 return topology_core_id(cpu
);
81 static int get_cpu_physical_id(int cpu
)
83 return get_hard_smp_processor_id(cpu
);
87 static u32
get_bus_freq(void)
89 struct device_node
*soc
;
92 soc
= of_find_node_by_type(NULL
, "soc");
96 if (of_property_read_u32(soc
, "bus-frequency", &sysfreq
))
104 static struct device_node
*cpu_to_clk_node(int cpu
)
106 struct device_node
*np
, *clk_np
;
108 if (!cpu_present(cpu
))
111 np
= of_get_cpu_node(cpu
, NULL
);
115 clk_np
= of_parse_phandle(np
, "clocks", 0);
124 /* traverse cpu nodes to get cpu mask of sharing clock wire */
125 static void set_affected_cpus(struct cpufreq_policy
*policy
)
127 struct device_node
*np
, *clk_np
;
128 struct cpumask
*dstp
= policy
->cpus
;
131 np
= cpu_to_clk_node(policy
->cpu
);
135 for_each_present_cpu(i
) {
136 clk_np
= cpu_to_clk_node(i
);
141 cpumask_set_cpu(i
, dstp
);
148 /* reduce the duplicated frequencies in frequency table */
149 static void freq_table_redup(struct cpufreq_frequency_table
*freq_table
,
154 for (i
= 1; i
< count
; i
++) {
155 for (j
= 0; j
< i
; j
++) {
156 if (freq_table
[j
].frequency
== CPUFREQ_ENTRY_INVALID
||
157 freq_table
[j
].frequency
!=
158 freq_table
[i
].frequency
)
161 freq_table
[i
].frequency
= CPUFREQ_ENTRY_INVALID
;
167 /* sort the frequencies in frequency table in descenting order */
168 static void freq_table_sort(struct cpufreq_frequency_table
*freq_table
,
172 unsigned int freq
, max_freq
;
173 struct cpufreq_frequency_table table
;
175 for (i
= 0; i
< count
- 1; i
++) {
176 max_freq
= freq_table
[i
].frequency
;
178 for (j
= i
+ 1; j
< count
; j
++) {
179 freq
= freq_table
[j
].frequency
;
180 if (freq
== CPUFREQ_ENTRY_INVALID
||
188 /* exchange the frequencies */
189 table
.driver_data
= freq_table
[i
].driver_data
;
190 table
.frequency
= freq_table
[i
].frequency
;
191 freq_table
[i
].driver_data
= freq_table
[ind
].driver_data
;
192 freq_table
[i
].frequency
= freq_table
[ind
].frequency
;
193 freq_table
[ind
].driver_data
= table
.driver_data
;
194 freq_table
[ind
].frequency
= table
.frequency
;
199 static int qoriq_cpufreq_cpu_init(struct cpufreq_policy
*policy
)
201 struct device_node
*np
, *pnode
;
205 struct cpufreq_frequency_table
*table
;
206 struct cpu_data
*data
;
207 unsigned int cpu
= policy
->cpu
;
210 np
= of_get_cpu_node(cpu
, NULL
);
214 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
218 policy
->clk
= of_clk_get(np
, 0);
219 if (IS_ERR(policy
->clk
)) {
220 pr_err("%s: no clock information\n", __func__
);
224 pnode
= of_parse_phandle(np
, "clocks", 0);
226 pr_err("%s: could not get clock information\n", __func__
);
230 count
= of_property_count_strings(pnode
, "clock-names");
231 data
->pclk
= kcalloc(count
, sizeof(struct clk
*), GFP_KERNEL
);
233 pr_err("%s: no memory\n", __func__
);
237 table
= kcalloc(count
+ 1, sizeof(*table
), GFP_KERNEL
);
239 pr_err("%s: no memory\n", __func__
);
244 mask
= fmask
[get_cpu_physical_id(cpu
)];
248 for (i
= 0; i
< count
; i
++) {
249 clk
= of_clk_get(pnode
, i
);
251 freq
= clk_get_rate(clk
);
253 * the clock is valid if its frequency is not masked
254 * and large than minimum allowed frequency.
256 if (freq
< min_cpufreq
|| (mask
& (1 << i
)))
257 table
[i
].frequency
= CPUFREQ_ENTRY_INVALID
;
259 table
[i
].frequency
= freq
/ 1000;
260 table
[i
].driver_data
= i
;
262 freq_table_redup(table
, count
);
263 freq_table_sort(table
, count
);
264 table
[i
].frequency
= CPUFREQ_TABLE_END
;
266 /* set the min and max frequency properly */
267 ret
= cpufreq_table_validate_and_show(policy
, table
);
269 pr_err("invalid frequency table: %d\n", ret
);
275 /* update ->cpus if we have cluster, no harm if not */
276 set_affected_cpus(policy
);
277 policy
->driver_data
= data
;
279 /* Minimum transition latency is 12 platform clocks */
280 u64temp
= 12ULL * NSEC_PER_SEC
;
281 do_div(u64temp
, get_bus_freq());
282 policy
->cpuinfo
.transition_latency
= u64temp
+ 1;
296 policy
->driver_data
= NULL
;
304 static int qoriq_cpufreq_cpu_exit(struct cpufreq_policy
*policy
)
306 struct cpu_data
*data
= policy
->driver_data
;
308 cpufreq_cooling_unregister(data
->cdev
);
312 policy
->driver_data
= NULL
;
317 static int qoriq_cpufreq_target(struct cpufreq_policy
*policy
,
321 struct cpu_data
*data
= policy
->driver_data
;
323 parent
= data
->pclk
[data
->table
[index
].driver_data
];
324 return clk_set_parent(policy
->clk
, parent
);
328 static void qoriq_cpufreq_ready(struct cpufreq_policy
*policy
)
330 struct cpu_data
*cpud
= policy
->driver_data
;
331 struct device_node
*np
= of_get_cpu_node(policy
->cpu
, NULL
);
333 if (of_find_property(np
, "#cooling-cells", NULL
)) {
334 cpud
->cdev
= of_cpufreq_cooling_register(np
,
335 policy
->related_cpus
);
337 if (IS_ERR(cpud
->cdev
) && PTR_ERR(cpud
->cdev
) != -ENOSYS
) {
338 pr_err("cpu%d is not running as cooling device: %ld\n",
339 policy
->cpu
, PTR_ERR(cpud
->cdev
));
348 static struct cpufreq_driver qoriq_cpufreq_driver
= {
349 .name
= "qoriq_cpufreq",
350 .flags
= CPUFREQ_CONST_LOOPS
,
351 .init
= qoriq_cpufreq_cpu_init
,
352 .exit
= qoriq_cpufreq_cpu_exit
,
353 .verify
= cpufreq_generic_frequency_table_verify
,
354 .target_index
= qoriq_cpufreq_target
,
355 .get
= cpufreq_generic_get
,
356 .ready
= qoriq_cpufreq_ready
,
357 .attr
= cpufreq_generic_attr
,
360 static const struct of_device_id node_matches
[] __initconst
= {
361 { .compatible
= "fsl,p2041-clockgen", .data
= &sdata
[0], },
362 { .compatible
= "fsl,p3041-clockgen", .data
= &sdata
[0], },
363 { .compatible
= "fsl,p5020-clockgen", .data
= &sdata
[1], },
364 { .compatible
= "fsl,p4080-clockgen", .data
= &sdata
[2], },
365 { .compatible
= "fsl,p5040-clockgen", .data
= &sdata
[2], },
366 { .compatible
= "fsl,qoriq-clockgen-2.0", },
370 static int __init
qoriq_cpufreq_init(void)
373 struct device_node
*np
;
374 const struct of_device_id
*match
;
375 const struct soc_data
*data
;
377 np
= of_find_matching_node(NULL
, node_matches
);
381 match
= of_match_node(node_matches
, np
);
385 fmask
= data
->freq_mask
;
386 min_cpufreq
= get_bus_freq();
388 min_cpufreq
= get_bus_freq() / 2;
393 ret
= cpufreq_register_driver(&qoriq_cpufreq_driver
);
395 pr_info("Freescale QorIQ CPU frequency scaling driver\n");
399 module_init(qoriq_cpufreq_init
);
401 static void __exit
qoriq_cpufreq_exit(void)
403 cpufreq_unregister_driver(&qoriq_cpufreq_driver
);
405 module_exit(qoriq_cpufreq_exit
);
407 MODULE_LICENSE("GPL");
408 MODULE_AUTHOR("Tang Yuantian <Yuantian.Tang@freescale.com>");
409 MODULE_DESCRIPTION("cpufreq driver for Freescale QorIQ series SoCs");