blob: 4ada55b8856e1e290e043ce43f7f3b5ebfc484f2 [file] [log] [blame]
Tang Yuantiandefa4c72013-06-05 09:30:48 +00001/*
2 * Copyright 2013 Freescale Semiconductor, Inc.
3 *
Tang Yuantiana4f20742015-03-13 12:39:01 +08004 * CPU Frequency Scaling driver for Freescale QorIQ SoCs.
Tang Yuantiandefa4c72013-06-05 09:30:48 +00005 *
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>
Tang Yuantianb1e9a642017-02-09 10:33:02 +080014#include <linux/clk-provider.h>
Tang Yuantiandefa4c72013-06-05 09:30:48 +000015#include <linux/cpufreq.h>
Hongtao Jia8ae17022015-11-26 17:21:11 +080016#include <linux/cpu_cooling.h>
Tang Yuantiandefa4c72013-06-05 09:30:48 +000017#include <linux/errno.h>
Tang Yuantiandefa4c72013-06-05 09:30:48 +000018#include <linux/init.h>
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/mutex.h>
22#include <linux/of.h>
23#include <linux/slab.h>
24#include <linux/smp.h>
25
26/**
Tang Yuantiana4f20742015-03-13 12:39:01 +080027 * struct cpu_data
Tang Yuantian8a95c142015-06-04 14:25:42 +080028 * @pclk: the parent clock of cpu
Tang Yuantiandefa4c72013-06-05 09:30:48 +000029 * @table: frequency table
30 */
31struct cpu_data {
Tang Yuantian8a95c142015-06-04 14:25:42 +080032 struct clk **pclk;
Tang Yuantiandefa4c72013-06-05 09:30:48 +000033 struct cpufreq_frequency_table *table;
Hongtao Jia8ae17022015-11-26 17:21:11 +080034 struct thermal_cooling_device *cdev;
Tang Yuantiandefa4c72013-06-05 09:30:48 +000035};
36
Tang Yuantianb1e9a642017-02-09 10:33:02 +080037/*
38 * Don't use cpufreq on this SoC -- used when the SoC would have otherwise
39 * matched a more generic compatible.
40 */
41#define SOC_BLACKLIST 1
42
Tang Yuantiandefa4c72013-06-05 09:30:48 +000043/**
44 * struct soc_data - SoC specific data
Tang Yuantianb1e9a642017-02-09 10:33:02 +080045 * @flags: SOC_xxx
Tang Yuantiandefa4c72013-06-05 09:30:48 +000046 */
47struct soc_data {
Tang Yuantianb1e9a642017-02-09 10:33:02 +080048 u32 flags;
Tang Yuantiandefa4c72013-06-05 09:30:48 +000049};
50
Tang Yuantiana4f20742015-03-13 12:39:01 +080051static u32 get_bus_freq(void)
52{
53 struct device_node *soc;
54 u32 sysfreq;
YuanTian Tangb51d3382017-03-10 09:28:43 +080055 struct clk *pltclk;
56 int ret;
Tang Yuantiana4f20742015-03-13 12:39:01 +080057
YuanTian Tangb51d3382017-03-10 09:28:43 +080058 /* get platform freq by searching bus-frequency property */
Tang Yuantiana4f20742015-03-13 12:39:01 +080059 soc = of_find_node_by_type(NULL, "soc");
YuanTian Tangb51d3382017-03-10 09:28:43 +080060 if (soc) {
61 ret = of_property_read_u32(soc, "bus-frequency", &sysfreq);
62 of_node_put(soc);
63 if (!ret)
64 return sysfreq;
65 }
Tang Yuantiana4f20742015-03-13 12:39:01 +080066
YuanTian Tangb51d3382017-03-10 09:28:43 +080067 /* get platform freq by its clock name */
68 pltclk = clk_get(NULL, "cg-pll0-div1");
69 if (IS_ERR(pltclk)) {
70 pr_err("%s: can't get bus frequency %ld\n",
71 __func__, PTR_ERR(pltclk));
72 return PTR_ERR(pltclk);
73 }
Tang Yuantiana4f20742015-03-13 12:39:01 +080074
YuanTian Tangb51d3382017-03-10 09:28:43 +080075 return clk_get_rate(pltclk);
Tang Yuantiana4f20742015-03-13 12:39:01 +080076}
77
Tang Yuantianb1e9a642017-02-09 10:33:02 +080078static struct clk *cpu_to_clk(int cpu)
Tang Yuantiana4f20742015-03-13 12:39:01 +080079{
Tang Yuantianb1e9a642017-02-09 10:33:02 +080080 struct device_node *np;
81 struct clk *clk;
Tang Yuantiana4f20742015-03-13 12:39:01 +080082
83 if (!cpu_present(cpu))
84 return NULL;
85
86 np = of_get_cpu_node(cpu, NULL);
87 if (!np)
88 return NULL;
89
Tang Yuantianb1e9a642017-02-09 10:33:02 +080090 clk = of_clk_get(np, 0);
Tang Yuantiana4f20742015-03-13 12:39:01 +080091 of_node_put(np);
Tang Yuantianb1e9a642017-02-09 10:33:02 +080092 return clk;
Tang Yuantiana4f20742015-03-13 12:39:01 +080093}
94
95/* traverse cpu nodes to get cpu mask of sharing clock wire */
96static void set_affected_cpus(struct cpufreq_policy *policy)
97{
Tang Yuantiana4f20742015-03-13 12:39:01 +080098 struct cpumask *dstp = policy->cpus;
Tang Yuantianb1e9a642017-02-09 10:33:02 +080099 struct clk *clk;
Tang Yuantiana4f20742015-03-13 12:39:01 +0800100 int i;
101
Tang Yuantiana4f20742015-03-13 12:39:01 +0800102 for_each_present_cpu(i) {
Tang Yuantianb1e9a642017-02-09 10:33:02 +0800103 clk = cpu_to_clk(i);
104 if (IS_ERR(clk)) {
105 pr_err("%s: no clock for cpu %d\n", __func__, i);
Tang Yuantiana4f20742015-03-13 12:39:01 +0800106 continue;
Tang Yuantianb1e9a642017-02-09 10:33:02 +0800107 }
Tang Yuantiana4f20742015-03-13 12:39:01 +0800108
Tang Yuantianb1e9a642017-02-09 10:33:02 +0800109 if (clk_is_match(policy->clk, clk))
Tang Yuantiana4f20742015-03-13 12:39:01 +0800110 cpumask_set_cpu(i, dstp);
Tang Yuantiana4f20742015-03-13 12:39:01 +0800111 }
Tang Yuantiana4f20742015-03-13 12:39:01 +0800112}
113
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000114/* reduce the duplicated frequencies in frequency table */
115static void freq_table_redup(struct cpufreq_frequency_table *freq_table,
116 int count)
117{
118 int i, j;
119
120 for (i = 1; i < count; i++) {
121 for (j = 0; j < i; j++) {
122 if (freq_table[j].frequency == CPUFREQ_ENTRY_INVALID ||
123 freq_table[j].frequency !=
124 freq_table[i].frequency)
125 continue;
126
127 freq_table[i].frequency = CPUFREQ_ENTRY_INVALID;
128 break;
129 }
130 }
131}
132
133/* sort the frequencies in frequency table in descenting order */
134static void freq_table_sort(struct cpufreq_frequency_table *freq_table,
135 int count)
136{
137 int i, j, ind;
138 unsigned int freq, max_freq;
139 struct cpufreq_frequency_table table;
Tang Yuantiana4f20742015-03-13 12:39:01 +0800140
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000141 for (i = 0; i < count - 1; i++) {
142 max_freq = freq_table[i].frequency;
143 ind = i;
144 for (j = i + 1; j < count; j++) {
145 freq = freq_table[j].frequency;
146 if (freq == CPUFREQ_ENTRY_INVALID ||
147 freq <= max_freq)
148 continue;
149 ind = j;
150 max_freq = freq;
151 }
152
153 if (ind != i) {
154 /* exchange the frequencies */
155 table.driver_data = freq_table[i].driver_data;
156 table.frequency = freq_table[i].frequency;
157 freq_table[i].driver_data = freq_table[ind].driver_data;
158 freq_table[i].frequency = freq_table[ind].frequency;
159 freq_table[ind].driver_data = table.driver_data;
160 freq_table[ind].frequency = table.frequency;
161 }
162 }
163}
164
Tang Yuantiana4f20742015-03-13 12:39:01 +0800165static int qoriq_cpufreq_cpu_init(struct cpufreq_policy *policy)
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000166{
Tang Yuantianb1e9a642017-02-09 10:33:02 +0800167 struct device_node *np;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000168 int i, count, ret;
Tang Yuantianb1e9a642017-02-09 10:33:02 +0800169 u32 freq;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000170 struct clk *clk;
Tang Yuantianb1e9a642017-02-09 10:33:02 +0800171 const struct clk_hw *hwclk;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000172 struct cpufreq_frequency_table *table;
173 struct cpu_data *data;
174 unsigned int cpu = policy->cpu;
Ed Swarthout906fe032014-06-05 18:56:17 -0500175 u64 u64temp;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000176
177 np = of_get_cpu_node(cpu, NULL);
178 if (!np)
179 return -ENODEV;
180
181 data = kzalloc(sizeof(*data), GFP_KERNEL);
Tang Yuantiana4f20742015-03-13 12:39:01 +0800182 if (!data)
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000183 goto err_np;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000184
Viresh Kumar652ed952014-01-09 20:38:43 +0530185 policy->clk = of_clk_get(np, 0);
186 if (IS_ERR(policy->clk)) {
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000187 pr_err("%s: no clock information\n", __func__);
188 goto err_nomem2;
189 }
190
Tang Yuantianb1e9a642017-02-09 10:33:02 +0800191 hwclk = __clk_get_hw(policy->clk);
192 count = clk_hw_get_num_parents(hwclk);
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000193
Tang Yuantian8a95c142015-06-04 14:25:42 +0800194 data->pclk = kcalloc(count, sizeof(struct clk *), GFP_KERNEL);
195 if (!data->pclk) {
196 pr_err("%s: no memory\n", __func__);
Tang Yuantianb1e9a642017-02-09 10:33:02 +0800197 goto err_nomem2;
Tang Yuantian8a95c142015-06-04 14:25:42 +0800198 }
199
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000200 table = kcalloc(count + 1, sizeof(*table), GFP_KERNEL);
201 if (!table) {
202 pr_err("%s: no memory\n", __func__);
Tang Yuantian8a95c142015-06-04 14:25:42 +0800203 goto err_pclk;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000204 }
205
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000206 for (i = 0; i < count; i++) {
Tang Yuantianb1e9a642017-02-09 10:33:02 +0800207 clk = clk_hw_get_parent_by_index(hwclk, i)->clk;
Tang Yuantian8a95c142015-06-04 14:25:42 +0800208 data->pclk[i] = clk;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000209 freq = clk_get_rate(clk);
Tang Yuantianb1e9a642017-02-09 10:33:02 +0800210 table[i].frequency = freq / 1000;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000211 table[i].driver_data = i;
212 }
213 freq_table_redup(table, count);
214 freq_table_sort(table, count);
215 table[i].frequency = CPUFREQ_TABLE_END;
216
217 /* set the min and max frequency properly */
Viresh Kumar6b4147d2013-09-16 18:56:28 +0530218 ret = cpufreq_table_validate_and_show(policy, table);
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000219 if (ret) {
220 pr_err("invalid frequency table: %d\n", ret);
221 goto err_nomem1;
222 }
223
224 data->table = table;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000225
226 /* update ->cpus if we have cluster, no harm if not */
Tang Yuantiana4f20742015-03-13 12:39:01 +0800227 set_affected_cpus(policy);
228 policy->driver_data = data;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000229
Ed Swarthout906fe032014-06-05 18:56:17 -0500230 /* Minimum transition latency is 12 platform clocks */
231 u64temp = 12ULL * NSEC_PER_SEC;
Tang Yuantiana4f20742015-03-13 12:39:01 +0800232 do_div(u64temp, get_bus_freq());
Ed Swarthout906fe032014-06-05 18:56:17 -0500233 policy->cpuinfo.transition_latency = u64temp + 1;
Tim Gardner6712d292014-04-28 10:18:18 -0600234
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000235 of_node_put(np);
236
237 return 0;
238
239err_nomem1:
240 kfree(table);
Tang Yuantian8a95c142015-06-04 14:25:42 +0800241err_pclk:
242 kfree(data->pclk);
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000243err_nomem2:
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000244 kfree(data);
245err_np:
246 of_node_put(np);
247
248 return -ENODEV;
249}
250
Jia Hongtao495c7162016-04-19 17:00:06 +0800251static int qoriq_cpufreq_cpu_exit(struct cpufreq_policy *policy)
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000252{
Tang Yuantiana4f20742015-03-13 12:39:01 +0800253 struct cpu_data *data = policy->driver_data;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000254
Jia Hongtao394cb832016-04-19 17:00:07 +0800255 cpufreq_cooling_unregister(data->cdev);
Tang Yuantian8a95c142015-06-04 14:25:42 +0800256 kfree(data->pclk);
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000257 kfree(data->table);
258 kfree(data);
Tang Yuantiana4f20742015-03-13 12:39:01 +0800259 policy->driver_data = NULL;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000260
261 return 0;
262}
263
Tang Yuantiana4f20742015-03-13 12:39:01 +0800264static int qoriq_cpufreq_target(struct cpufreq_policy *policy,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530265 unsigned int index)
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000266{
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000267 struct clk *parent;
Tang Yuantiana4f20742015-03-13 12:39:01 +0800268 struct cpu_data *data = policy->driver_data;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000269
Tang Yuantian8a95c142015-06-04 14:25:42 +0800270 parent = data->pclk[data->table[index].driver_data];
Viresh Kumar652ed952014-01-09 20:38:43 +0530271 return clk_set_parent(policy->clk, parent);
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000272}
273
Hongtao Jia8ae17022015-11-26 17:21:11 +0800274
275static void qoriq_cpufreq_ready(struct cpufreq_policy *policy)
276{
277 struct cpu_data *cpud = policy->driver_data;
278 struct device_node *np = of_get_cpu_node(policy->cpu, NULL);
279
280 if (of_find_property(np, "#cooling-cells", NULL)) {
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530281 cpud->cdev = of_cpufreq_cooling_register(np, policy);
Hongtao Jia8ae17022015-11-26 17:21:11 +0800282
Jia Hongtao27b8fe82016-04-18 15:59:32 +0800283 if (IS_ERR(cpud->cdev) && PTR_ERR(cpud->cdev) != -ENOSYS) {
284 pr_err("cpu%d is not running as cooling device: %ld\n",
Hongtao Jia8ae17022015-11-26 17:21:11 +0800285 policy->cpu, PTR_ERR(cpud->cdev));
286
287 cpud->cdev = NULL;
288 }
289 }
290
291 of_node_put(np);
292}
293
Tang Yuantiana4f20742015-03-13 12:39:01 +0800294static struct cpufreq_driver qoriq_cpufreq_driver = {
295 .name = "qoriq_cpufreq",
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000296 .flags = CPUFREQ_CONST_LOOPS,
Tang Yuantiana4f20742015-03-13 12:39:01 +0800297 .init = qoriq_cpufreq_cpu_init,
Jia Hongtao495c7162016-04-19 17:00:06 +0800298 .exit = qoriq_cpufreq_cpu_exit,
Viresh Kumardc2398d2013-10-03 20:28:18 +0530299 .verify = cpufreq_generic_frequency_table_verify,
Tang Yuantiana4f20742015-03-13 12:39:01 +0800300 .target_index = qoriq_cpufreq_target,
Viresh Kumar652ed952014-01-09 20:38:43 +0530301 .get = cpufreq_generic_get,
Hongtao Jia8ae17022015-11-26 17:21:11 +0800302 .ready = qoriq_cpufreq_ready,
Viresh Kumardc2398d2013-10-03 20:28:18 +0530303 .attr = cpufreq_generic_attr,
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000304};
305
Tang Yuantianb1e9a642017-02-09 10:33:02 +0800306static const struct soc_data blacklist = {
307 .flags = SOC_BLACKLIST,
308};
309
Tang Yuantiana4f20742015-03-13 12:39:01 +0800310static const struct of_device_id node_matches[] __initconst = {
Tang Yuantianb1e9a642017-02-09 10:33:02 +0800311 /* e6500 cannot use cpufreq due to erratum A-008083 */
312 { .compatible = "fsl,b4420-clockgen", &blacklist },
313 { .compatible = "fsl,b4860-clockgen", &blacklist },
314 { .compatible = "fsl,t2080-clockgen", &blacklist },
315 { .compatible = "fsl,t4240-clockgen", &blacklist },
316
317 { .compatible = "fsl,ls1012a-clockgen", },
318 { .compatible = "fsl,ls1021a-clockgen", },
319 { .compatible = "fsl,ls1043a-clockgen", },
320 { .compatible = "fsl,ls1046a-clockgen", },
321 { .compatible = "fsl,ls1088a-clockgen", },
322 { .compatible = "fsl,ls2080a-clockgen", },
323 { .compatible = "fsl,p4080-clockgen", },
324 { .compatible = "fsl,qoriq-clockgen-1.0", },
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000325 { .compatible = "fsl,qoriq-clockgen-2.0", },
326 {}
327};
328
Tang Yuantiana4f20742015-03-13 12:39:01 +0800329static int __init qoriq_cpufreq_init(void)
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000330{
331 int ret;
332 struct device_node *np;
333 const struct of_device_id *match;
334 const struct soc_data *data;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000335
336 np = of_find_matching_node(NULL, node_matches);
337 if (!np)
338 return -ENODEV;
339
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000340 match = of_match_node(node_matches, np);
341 data = match->data;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000342
343 of_node_put(np);
344
Tang Yuantianb1e9a642017-02-09 10:33:02 +0800345 if (data && data->flags & SOC_BLACKLIST)
346 return -ENODEV;
347
Tang Yuantiana4f20742015-03-13 12:39:01 +0800348 ret = cpufreq_register_driver(&qoriq_cpufreq_driver);
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000349 if (!ret)
Tang Yuantiana4f20742015-03-13 12:39:01 +0800350 pr_info("Freescale QorIQ CPU frequency scaling driver\n");
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000351
352 return ret;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000353}
Tang Yuantiana4f20742015-03-13 12:39:01 +0800354module_init(qoriq_cpufreq_init);
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000355
Tang Yuantiana4f20742015-03-13 12:39:01 +0800356static void __exit qoriq_cpufreq_exit(void)
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000357{
Tang Yuantiana4f20742015-03-13 12:39:01 +0800358 cpufreq_unregister_driver(&qoriq_cpufreq_driver);
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000359}
Tang Yuantiana4f20742015-03-13 12:39:01 +0800360module_exit(qoriq_cpufreq_exit);
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000361
362MODULE_LICENSE("GPL");
363MODULE_AUTHOR("Tang Yuantian <Yuantian.Tang@freescale.com>");
Tang Yuantiana4f20742015-03-13 12:39:01 +0800364MODULE_DESCRIPTION("cpufreq driver for Freescale QorIQ series SoCs");