Merge tag 'pci-v3.15-changes' of git://git.kernel.org/pub/scm/linux/kernel/git/helgaa...
[deliverable/linux.git] / drivers / cpufreq / ppc-corenet-cpufreq.c
CommitLineData
defa4c73
TY
1/*
2 * Copyright 2013 Freescale Semiconductor, Inc.
3 *
4 * CPU Frequency Scaling driver for Freescale PowerPC corenet 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
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
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>
21#include <linux/of.h>
22#include <linux/slab.h>
23#include <linux/smp.h>
bfa709bc 24#include <sysdev/fsl_soc.h>
defa4c73
TY
25
26/**
27 * struct cpu_data - per CPU data struct
defa4c73
TY
28 * @parent: the parent node of cpu clock
29 * @table: frequency table
30 */
31struct cpu_data {
defa4c73
TY
32 struct device_node *parent;
33 struct cpufreq_frequency_table *table;
34};
35
36/**
37 * struct soc_data - SoC specific data
38 * @freq_mask: mask the disallowed frequencies
39 * @flag: unique flags
40 */
41struct soc_data {
42 u32 freq_mask[4];
43 u32 flag;
44};
45
46#define FREQ_MASK 1
47/* see hardware specification for the allowed frqeuencies */
48static const struct soc_data sdata[] = {
49 { /* used by p2041 and p3041 */
50 .freq_mask = {0x8, 0x8, 0x2, 0x2},
51 .flag = FREQ_MASK,
52 },
53 { /* used by p5020 */
54 .freq_mask = {0x8, 0x2},
55 .flag = FREQ_MASK,
56 },
57 { /* used by p4080, p5040 */
58 .freq_mask = {0},
59 .flag = 0,
60 },
61};
62
63/*
64 * the minimum allowed core frequency, in Hz
65 * for chassis v1.0, >= platform frequency
66 * for chassis v2.0, >= platform frequency / 2
67 */
68static u32 min_cpufreq;
69static const u32 *fmask;
70
defa4c73
TY
71static DEFINE_PER_CPU(struct cpu_data *, cpu_data);
72
73/* cpumask in a cluster */
74static DEFINE_PER_CPU(cpumask_var_t, cpu_mask);
75
76#ifndef CONFIG_SMP
77static inline const struct cpumask *cpu_core_mask(int cpu)
78{
79 return cpumask_of(0);
80}
81#endif
82
defa4c73
TY
83/* reduce the duplicated frequencies in frequency table */
84static void freq_table_redup(struct cpufreq_frequency_table *freq_table,
85 int count)
86{
87 int i, j;
88
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)
94 continue;
95
96 freq_table[i].frequency = CPUFREQ_ENTRY_INVALID;
97 break;
98 }
99 }
100}
101
102/* sort the frequencies in frequency table in descenting order */
103static void freq_table_sort(struct cpufreq_frequency_table *freq_table,
104 int count)
105{
106 int i, j, ind;
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;
111 ind = i;
112 for (j = i + 1; j < count; j++) {
113 freq = freq_table[j].frequency;
114 if (freq == CPUFREQ_ENTRY_INVALID ||
115 freq <= max_freq)
116 continue;
117 ind = j;
118 max_freq = freq;
119 }
120
121 if (ind != i) {
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;
129 }
130 }
131}
132
133static int corenet_cpufreq_cpu_init(struct cpufreq_policy *policy)
134{
135 struct device_node *np;
136 int i, count, ret;
137 u32 freq, mask;
138 struct clk *clk;
139 struct cpufreq_frequency_table *table;
140 struct cpu_data *data;
141 unsigned int cpu = policy->cpu;
142
143 np = of_get_cpu_node(cpu, NULL);
144 if (!np)
145 return -ENODEV;
146
147 data = kzalloc(sizeof(*data), GFP_KERNEL);
148 if (!data) {
149 pr_err("%s: no memory\n", __func__);
150 goto err_np;
151 }
152
652ed95d
VK
153 policy->clk = of_clk_get(np, 0);
154 if (IS_ERR(policy->clk)) {
defa4c73
TY
155 pr_err("%s: no clock information\n", __func__);
156 goto err_nomem2;
157 }
158
159 data->parent = of_parse_phandle(np, "clocks", 0);
160 if (!data->parent) {
161 pr_err("%s: could not get clock information\n", __func__);
162 goto err_nomem2;
163 }
164
165 count = of_property_count_strings(data->parent, "clock-names");
166 table = kcalloc(count + 1, sizeof(*table), GFP_KERNEL);
167 if (!table) {
168 pr_err("%s: no memory\n", __func__);
169 goto err_node;
170 }
171
172 if (fmask)
173 mask = fmask[get_hard_smp_processor_id(cpu)];
174 else
175 mask = 0x0;
176
177 for (i = 0; i < count; i++) {
178 clk = of_clk_get(data->parent, i);
179 freq = clk_get_rate(clk);
180 /*
181 * the clock is valid if its frequency is not masked
182 * and large than minimum allowed frequency.
183 */
184 if (freq < min_cpufreq || (mask & (1 << i)))
185 table[i].frequency = CPUFREQ_ENTRY_INVALID;
186 else
187 table[i].frequency = freq / 1000;
188 table[i].driver_data = i;
189 }
190 freq_table_redup(table, count);
191 freq_table_sort(table, count);
192 table[i].frequency = CPUFREQ_TABLE_END;
193
194 /* set the min and max frequency properly */
6b4147db 195 ret = cpufreq_table_validate_and_show(policy, table);
defa4c73
TY
196 if (ret) {
197 pr_err("invalid frequency table: %d\n", ret);
198 goto err_nomem1;
199 }
200
201 data->table = table;
202 per_cpu(cpu_data, cpu) = data;
203
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;
208
bfa709bc
ZZ
209 policy->cpuinfo.transition_latency =
210 (12 * NSEC_PER_SEC) / fsl_get_sys_freq();
defa4c73
TY
211 of_node_put(np);
212
213 return 0;
214
215err_nomem1:
216 kfree(table);
217err_node:
218 of_node_put(data->parent);
219err_nomem2:
220 per_cpu(cpu_data, cpu) = NULL;
221 kfree(data);
222err_np:
223 of_node_put(np);
224
225 return -ENODEV;
226}
227
228static int __exit corenet_cpufreq_cpu_exit(struct cpufreq_policy *policy)
229{
230 struct cpu_data *data = per_cpu(cpu_data, policy->cpu);
231 unsigned int cpu;
232
defa4c73
TY
233 of_node_put(data->parent);
234 kfree(data->table);
235 kfree(data);
236
237 for_each_cpu(cpu, per_cpu(cpu_mask, policy->cpu))
238 per_cpu(cpu_data, cpu) = NULL;
239
240 return 0;
241}
242
defa4c73 243static int corenet_cpufreq_target(struct cpufreq_policy *policy,
9c0ebcf7 244 unsigned int index)
defa4c73 245{
defa4c73 246 struct clk *parent;
defa4c73
TY
247 struct cpu_data *data = per_cpu(cpu_data, policy->cpu);
248
9c0ebcf7 249 parent = of_clk_get(data->parent, data->table[index].driver_data);
652ed95d 250 return clk_set_parent(policy->clk, parent);
defa4c73
TY
251}
252
defa4c73
TY
253static struct cpufreq_driver ppc_corenet_cpufreq_driver = {
254 .name = "ppc_cpufreq",
defa4c73
TY
255 .flags = CPUFREQ_CONST_LOOPS,
256 .init = corenet_cpufreq_cpu_init,
257 .exit = __exit_p(corenet_cpufreq_cpu_exit),
dc2398d7 258 .verify = cpufreq_generic_frequency_table_verify,
9c0ebcf7 259 .target_index = corenet_cpufreq_target,
652ed95d 260 .get = cpufreq_generic_get,
dc2398d7 261 .attr = cpufreq_generic_attr,
defa4c73
TY
262};
263
264static 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", },
271 {}
272};
273
274static int __init ppc_corenet_cpufreq_init(void)
275{
276 int ret;
277 struct device_node *np;
278 const struct of_device_id *match;
279 const struct soc_data *data;
280 unsigned int cpu;
281
282 np = of_find_matching_node(NULL, node_matches);
283 if (!np)
284 return -ENODEV;
285
286 for_each_possible_cpu(cpu) {
287 if (!alloc_cpumask_var(&per_cpu(cpu_mask, cpu), GFP_KERNEL))
288 goto err_mask;
289 cpumask_copy(per_cpu(cpu_mask, cpu), cpu_core_mask(cpu));
290 }
291
292 match = of_match_node(node_matches, np);
293 data = match->data;
294 if (data) {
295 if (data->flag)
296 fmask = data->freq_mask;
297 min_cpufreq = fsl_get_sys_freq();
298 } else {
299 min_cpufreq = fsl_get_sys_freq() / 2;
300 }
301
302 of_node_put(np);
303
304 ret = cpufreq_register_driver(&ppc_corenet_cpufreq_driver);
305 if (!ret)
306 pr_info("Freescale PowerPC corenet CPU frequency scaling driver\n");
307
308 return ret;
309
310err_mask:
311 for_each_possible_cpu(cpu)
312 free_cpumask_var(per_cpu(cpu_mask, cpu));
313
314 return -ENOMEM;
315}
316module_init(ppc_corenet_cpufreq_init);
317
318static void __exit ppc_corenet_cpufreq_exit(void)
319{
320 unsigned int cpu;
321
322 for_each_possible_cpu(cpu)
323 free_cpumask_var(per_cpu(cpu_mask, cpu));
324
325 cpufreq_unregister_driver(&ppc_corenet_cpufreq_driver);
326}
327module_exit(ppc_corenet_cpufreq_exit);
328
329MODULE_LICENSE("GPL");
330MODULE_AUTHOR("Tang Yuantian <Yuantian.Tang@freescale.com>");
331MODULE_DESCRIPTION("cpufreq driver for Freescale e500mc series SoCs");
This page took 0.130267 seconds and 5 git commands to generate.