]>
Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
defa4c73 TY |
2 | /* |
3 | * Copyright 2013 Freescale Semiconductor, Inc. | |
4 | * | |
a4f20742 | 5 | * CPU Frequency Scaling driver for Freescale QorIQ SoCs. |
defa4c73 TY |
6 | */ |
7 | ||
8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
9 | ||
10 | #include <linux/clk.h> | |
b1e9a649 | 11 | #include <linux/clk-provider.h> |
defa4c73 TY |
12 | #include <linux/cpufreq.h> |
13 | #include <linux/errno.h> | |
defa4c73 TY |
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> | |
157f5276 | 21 | #include <linux/platform_device.h> |
defa4c73 TY |
22 | |
23 | /** | |
a4f20742 | 24 | * struct cpu_data |
8a95c144 | 25 | * @pclk: the parent clock of cpu |
defa4c73 TY |
26 | * @table: frequency table |
27 | */ | |
28 | struct cpu_data { | |
8a95c144 | 29 | struct clk **pclk; |
defa4c73 TY |
30 | struct cpufreq_frequency_table *table; |
31 | }; | |
32 | ||
33 | /** | |
34 | * struct soc_data - SoC specific data | |
b1e9a649 | 35 | * @flags: SOC_xxx |
defa4c73 TY |
36 | */ |
37 | struct soc_data { | |
b1e9a649 | 38 | u32 flags; |
defa4c73 TY |
39 | }; |
40 | ||
a4f20742 TY |
41 | static u32 get_bus_freq(void) |
42 | { | |
43 | struct device_node *soc; | |
44 | u32 sysfreq; | |
b51d3388 YT |
45 | struct clk *pltclk; |
46 | int ret; | |
a4f20742 | 47 | |
b51d3388 | 48 | /* get platform freq by searching bus-frequency property */ |
a4f20742 | 49 | soc = of_find_node_by_type(NULL, "soc"); |
b51d3388 YT |
50 | if (soc) { |
51 | ret = of_property_read_u32(soc, "bus-frequency", &sysfreq); | |
52 | of_node_put(soc); | |
53 | if (!ret) | |
54 | return sysfreq; | |
55 | } | |
a4f20742 | 56 | |
b51d3388 YT |
57 | /* get platform freq by its clock name */ |
58 | pltclk = clk_get(NULL, "cg-pll0-div1"); | |
59 | if (IS_ERR(pltclk)) { | |
60 | pr_err("%s: can't get bus frequency %ld\n", | |
61 | __func__, PTR_ERR(pltclk)); | |
62 | return PTR_ERR(pltclk); | |
63 | } | |
defa4c73 | 64 | |
b51d3388 | 65 | return clk_get_rate(pltclk); |
a4f20742 | 66 | } |
defa4c73 | 67 | |
b1e9a649 | 68 | static struct clk *cpu_to_clk(int cpu) |
defa4c73 | 69 | { |
b1e9a649 TY |
70 | struct device_node *np; |
71 | struct clk *clk; | |
a4f20742 TY |
72 | |
73 | if (!cpu_present(cpu)) | |
74 | return NULL; | |
75 | ||
76 | np = of_get_cpu_node(cpu, NULL); | |
77 | if (!np) | |
78 | return NULL; | |
79 | ||
b1e9a649 | 80 | clk = of_clk_get(np, 0); |
a4f20742 | 81 | of_node_put(np); |
b1e9a649 | 82 | return clk; |
a4f20742 TY |
83 | } |
84 | ||
85 | /* traverse cpu nodes to get cpu mask of sharing clock wire */ | |
86 | static void set_affected_cpus(struct cpufreq_policy *policy) | |
87 | { | |
a4f20742 | 88 | struct cpumask *dstp = policy->cpus; |
b1e9a649 | 89 | struct clk *clk; |
a4f20742 TY |
90 | int i; |
91 | ||
a4f20742 | 92 | for_each_present_cpu(i) { |
b1e9a649 TY |
93 | clk = cpu_to_clk(i); |
94 | if (IS_ERR(clk)) { | |
95 | pr_err("%s: no clock for cpu %d\n", __func__, i); | |
a4f20742 | 96 | continue; |
b1e9a649 | 97 | } |
a4f20742 | 98 | |
b1e9a649 | 99 | if (clk_is_match(policy->clk, clk)) |
a4f20742 | 100 | cpumask_set_cpu(i, dstp); |
a4f20742 | 101 | } |
defa4c73 | 102 | } |
defa4c73 | 103 | |
defa4c73 TY |
104 | /* reduce the duplicated frequencies in frequency table */ |
105 | static void freq_table_redup(struct cpufreq_frequency_table *freq_table, | |
106 | int count) | |
107 | { | |
108 | int i, j; | |
109 | ||
110 | for (i = 1; i < count; i++) { | |
111 | for (j = 0; j < i; j++) { | |
112 | if (freq_table[j].frequency == CPUFREQ_ENTRY_INVALID || | |
113 | freq_table[j].frequency != | |
114 | freq_table[i].frequency) | |
115 | continue; | |
116 | ||
117 | freq_table[i].frequency = CPUFREQ_ENTRY_INVALID; | |
118 | break; | |
119 | } | |
120 | } | |
121 | } | |
122 | ||
123 | /* sort the frequencies in frequency table in descenting order */ | |
124 | static void freq_table_sort(struct cpufreq_frequency_table *freq_table, | |
125 | int count) | |
126 | { | |
127 | int i, j, ind; | |
128 | unsigned int freq, max_freq; | |
129 | struct cpufreq_frequency_table table; | |
a4f20742 | 130 | |
defa4c73 TY |
131 | for (i = 0; i < count - 1; i++) { |
132 | max_freq = freq_table[i].frequency; | |
133 | ind = i; | |
134 | for (j = i + 1; j < count; j++) { | |
135 | freq = freq_table[j].frequency; | |
136 | if (freq == CPUFREQ_ENTRY_INVALID || | |
137 | freq <= max_freq) | |
138 | continue; | |
139 | ind = j; | |
140 | max_freq = freq; | |
141 | } | |
142 | ||
143 | if (ind != i) { | |
144 | /* exchange the frequencies */ | |
145 | table.driver_data = freq_table[i].driver_data; | |
146 | table.frequency = freq_table[i].frequency; | |
147 | freq_table[i].driver_data = freq_table[ind].driver_data; | |
148 | freq_table[i].frequency = freq_table[ind].frequency; | |
149 | freq_table[ind].driver_data = table.driver_data; | |
150 | freq_table[ind].frequency = table.frequency; | |
151 | } | |
152 | } | |
153 | } | |
154 | ||
a4f20742 | 155 | static int qoriq_cpufreq_cpu_init(struct cpufreq_policy *policy) |
defa4c73 | 156 | { |
b1e9a649 | 157 | struct device_node *np; |
5ab508be | 158 | int i, count; |
b1e9a649 | 159 | u32 freq; |
defa4c73 | 160 | struct clk *clk; |
b1e9a649 | 161 | const struct clk_hw *hwclk; |
defa4c73 TY |
162 | struct cpufreq_frequency_table *table; |
163 | struct cpu_data *data; | |
164 | unsigned int cpu = policy->cpu; | |
906fe033 | 165 | u64 u64temp; |
defa4c73 TY |
166 | |
167 | np = of_get_cpu_node(cpu, NULL); | |
168 | if (!np) | |
169 | return -ENODEV; | |
170 | ||
171 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
a4f20742 | 172 | if (!data) |
defa4c73 | 173 | goto err_np; |
defa4c73 | 174 | |
652ed95d VK |
175 | policy->clk = of_clk_get(np, 0); |
176 | if (IS_ERR(policy->clk)) { | |
defa4c73 TY |
177 | pr_err("%s: no clock information\n", __func__); |
178 | goto err_nomem2; | |
179 | } | |
180 | ||
b1e9a649 TY |
181 | hwclk = __clk_get_hw(policy->clk); |
182 | count = clk_hw_get_num_parents(hwclk); | |
defa4c73 | 183 | |
8a95c144 | 184 | data->pclk = kcalloc(count, sizeof(struct clk *), GFP_KERNEL); |
d6b96e47 | 185 | if (!data->pclk) |
b1e9a649 | 186 | goto err_nomem2; |
8a95c144 | 187 | |
defa4c73 | 188 | table = kcalloc(count + 1, sizeof(*table), GFP_KERNEL); |
d6b96e47 | 189 | if (!table) |
8a95c144 | 190 | goto err_pclk; |
defa4c73 | 191 | |
defa4c73 | 192 | for (i = 0; i < count; i++) { |
b1e9a649 | 193 | clk = clk_hw_get_parent_by_index(hwclk, i)->clk; |
8a95c144 | 194 | data->pclk[i] = clk; |
defa4c73 | 195 | freq = clk_get_rate(clk); |
b1e9a649 | 196 | table[i].frequency = freq / 1000; |
defa4c73 TY |
197 | table[i].driver_data = i; |
198 | } | |
199 | freq_table_redup(table, count); | |
200 | freq_table_sort(table, count); | |
201 | table[i].frequency = CPUFREQ_TABLE_END; | |
5ab508be | 202 | policy->freq_table = table; |
defa4c73 | 203 | data->table = table; |
defa4c73 TY |
204 | |
205 | /* update ->cpus if we have cluster, no harm if not */ | |
a4f20742 TY |
206 | set_affected_cpus(policy); |
207 | policy->driver_data = data; | |
defa4c73 | 208 | |
906fe033 ES |
209 | /* Minimum transition latency is 12 platform clocks */ |
210 | u64temp = 12ULL * NSEC_PER_SEC; | |
a4f20742 | 211 | do_div(u64temp, get_bus_freq()); |
906fe033 | 212 | policy->cpuinfo.transition_latency = u64temp + 1; |
6712d293 | 213 | |
defa4c73 TY |
214 | of_node_put(np); |
215 | ||
216 | return 0; | |
217 | ||
8a95c144 TY |
218 | err_pclk: |
219 | kfree(data->pclk); | |
defa4c73 | 220 | err_nomem2: |
defa4c73 TY |
221 | kfree(data); |
222 | err_np: | |
223 | of_node_put(np); | |
224 | ||
225 | return -ENODEV; | |
226 | } | |
227 | ||
495c716f | 228 | static int qoriq_cpufreq_cpu_exit(struct cpufreq_policy *policy) |
defa4c73 | 229 | { |
a4f20742 | 230 | struct cpu_data *data = policy->driver_data; |
defa4c73 | 231 | |
8a95c144 | 232 | kfree(data->pclk); |
defa4c73 TY |
233 | kfree(data->table); |
234 | kfree(data); | |
a4f20742 | 235 | policy->driver_data = NULL; |
defa4c73 TY |
236 | |
237 | return 0; | |
238 | } | |
239 | ||
a4f20742 | 240 | static int qoriq_cpufreq_target(struct cpufreq_policy *policy, |
9c0ebcf7 | 241 | unsigned int index) |
defa4c73 | 242 | { |
defa4c73 | 243 | struct clk *parent; |
a4f20742 | 244 | struct cpu_data *data = policy->driver_data; |
defa4c73 | 245 | |
8a95c144 | 246 | parent = data->pclk[data->table[index].driver_data]; |
652ed95d | 247 | return clk_set_parent(policy->clk, parent); |
defa4c73 TY |
248 | } |
249 | ||
a4f20742 TY |
250 | static struct cpufreq_driver qoriq_cpufreq_driver = { |
251 | .name = "qoriq_cpufreq", | |
17170ec1 AK |
252 | .flags = CPUFREQ_CONST_LOOPS | |
253 | CPUFREQ_IS_COOLING_DEV, | |
a4f20742 | 254 | .init = qoriq_cpufreq_cpu_init, |
495c716f | 255 | .exit = qoriq_cpufreq_cpu_exit, |
dc2398d7 | 256 | .verify = cpufreq_generic_frequency_table_verify, |
a4f20742 | 257 | .target_index = qoriq_cpufreq_target, |
652ed95d | 258 | .get = cpufreq_generic_get, |
dc2398d7 | 259 | .attr = cpufreq_generic_attr, |
defa4c73 TY |
260 | }; |
261 | ||
157f5276 | 262 | static const struct of_device_id qoriq_cpufreq_blacklist[] = { |
b1e9a649 | 263 | /* e6500 cannot use cpufreq due to erratum A-008083 */ |
157f5276 MYK |
264 | { .compatible = "fsl,b4420-clockgen", }, |
265 | { .compatible = "fsl,b4860-clockgen", }, | |
266 | { .compatible = "fsl,t2080-clockgen", }, | |
267 | { .compatible = "fsl,t4240-clockgen", }, | |
defa4c73 TY |
268 | {} |
269 | }; | |
270 | ||
157f5276 | 271 | static int qoriq_cpufreq_probe(struct platform_device *pdev) |
defa4c73 TY |
272 | { |
273 | int ret; | |
157f5276 | 274 | struct device_node *np; |
defa4c73 | 275 | |
157f5276 MYK |
276 | np = of_find_matching_node(NULL, qoriq_cpufreq_blacklist); |
277 | if (np) { | |
278 | dev_info(&pdev->dev, "Disabling due to erratum A-008083"); | |
b1e9a649 | 279 | return -ENODEV; |
157f5276 | 280 | } |
b1e9a649 | 281 | |
a4f20742 | 282 | ret = cpufreq_register_driver(&qoriq_cpufreq_driver); |
157f5276 MYK |
283 | if (ret) |
284 | return ret; | |
defa4c73 | 285 | |
157f5276 MYK |
286 | dev_info(&pdev->dev, "Freescale QorIQ CPU frequency scaling driver\n"); |
287 | return 0; | |
defa4c73 | 288 | } |
defa4c73 | 289 | |
157f5276 | 290 | static int qoriq_cpufreq_remove(struct platform_device *pdev) |
defa4c73 | 291 | { |
a4f20742 | 292 | cpufreq_unregister_driver(&qoriq_cpufreq_driver); |
157f5276 MYK |
293 | |
294 | return 0; | |
defa4c73 | 295 | } |
defa4c73 | 296 | |
157f5276 MYK |
297 | static struct platform_driver qoriq_cpufreq_platform_driver = { |
298 | .driver = { | |
299 | .name = "qoriq-cpufreq", | |
300 | }, | |
301 | .probe = qoriq_cpufreq_probe, | |
302 | .remove = qoriq_cpufreq_remove, | |
303 | }; | |
304 | module_platform_driver(qoriq_cpufreq_platform_driver); | |
305 | ||
306 | MODULE_ALIAS("platform:qoriq-cpufreq"); | |
defa4c73 TY |
307 | MODULE_LICENSE("GPL"); |
308 | MODULE_AUTHOR("Tang Yuantian <Yuantian.Tang@freescale.com>"); | |
a4f20742 | 309 | MODULE_DESCRIPTION("cpufreq driver for Freescale QorIQ series SoCs"); |