blob: 6b6b20da2bcfc83846d37a3524de5c4d3f7598f2 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Tang Yuantiandefa4c72013-06-05 09:30:48 +00002/*
3 * Copyright 2013 Freescale Semiconductor, Inc.
4 *
Tang Yuantiana4f20742015-03-13 12:39:01 +08005 * CPU Frequency Scaling driver for Freescale QorIQ SoCs.
Tang Yuantiandefa4c72013-06-05 09:30:48 +00006 */
7
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10#include <linux/clk.h>
Tang Yuantianb1e9a642017-02-09 10:33:02 +080011#include <linux/clk-provider.h>
Tang Yuantiandefa4c72013-06-05 09:30:48 +000012#include <linux/cpufreq.h>
13#include <linux/errno.h>
Tang Yuantiandefa4c72013-06-05 09:30:48 +000014#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>
Mian Yousaf Kaukab157f5272020-04-21 10:29:59 +020021#include <linux/platform_device.h>
Tang Yuantiandefa4c72013-06-05 09:30:48 +000022
23/**
Tang Yuantiana4f20742015-03-13 12:39:01 +080024 * struct cpu_data
Tang Yuantian8a95c142015-06-04 14:25:42 +080025 * @pclk: the parent clock of cpu
Tang Yuantiandefa4c72013-06-05 09:30:48 +000026 * @table: frequency table
27 */
28struct cpu_data {
Tang Yuantian8a95c142015-06-04 14:25:42 +080029 struct clk **pclk;
Tang Yuantiandefa4c72013-06-05 09:30:48 +000030 struct cpufreq_frequency_table *table;
31};
32
33/**
34 * struct soc_data - SoC specific data
Tang Yuantianb1e9a642017-02-09 10:33:02 +080035 * @flags: SOC_xxx
Tang Yuantiandefa4c72013-06-05 09:30:48 +000036 */
37struct soc_data {
Tang Yuantianb1e9a642017-02-09 10:33:02 +080038 u32 flags;
Tang Yuantiandefa4c72013-06-05 09:30:48 +000039};
40
Tang Yuantiana4f20742015-03-13 12:39:01 +080041static u32 get_bus_freq(void)
42{
43 struct device_node *soc;
44 u32 sysfreq;
YuanTian Tangb51d3382017-03-10 09:28:43 +080045 struct clk *pltclk;
46 int ret;
Tang Yuantiana4f20742015-03-13 12:39:01 +080047
YuanTian Tangb51d3382017-03-10 09:28:43 +080048 /* get platform freq by searching bus-frequency property */
Tang Yuantiana4f20742015-03-13 12:39:01 +080049 soc = of_find_node_by_type(NULL, "soc");
YuanTian Tangb51d3382017-03-10 09:28:43 +080050 if (soc) {
51 ret = of_property_read_u32(soc, "bus-frequency", &sysfreq);
52 of_node_put(soc);
53 if (!ret)
54 return sysfreq;
55 }
Tang Yuantiana4f20742015-03-13 12:39:01 +080056
YuanTian Tangb51d3382017-03-10 09:28:43 +080057 /* 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 }
Tang Yuantiana4f20742015-03-13 12:39:01 +080064
YuanTian Tangb51d3382017-03-10 09:28:43 +080065 return clk_get_rate(pltclk);
Tang Yuantiana4f20742015-03-13 12:39:01 +080066}
67
Tang Yuantianb1e9a642017-02-09 10:33:02 +080068static struct clk *cpu_to_clk(int cpu)
Tang Yuantiana4f20742015-03-13 12:39:01 +080069{
Tang Yuantianb1e9a642017-02-09 10:33:02 +080070 struct device_node *np;
71 struct clk *clk;
Tang Yuantiana4f20742015-03-13 12:39:01 +080072
73 if (!cpu_present(cpu))
74 return NULL;
75
76 np = of_get_cpu_node(cpu, NULL);
77 if (!np)
78 return NULL;
79
Tang Yuantianb1e9a642017-02-09 10:33:02 +080080 clk = of_clk_get(np, 0);
Tang Yuantiana4f20742015-03-13 12:39:01 +080081 of_node_put(np);
Tang Yuantianb1e9a642017-02-09 10:33:02 +080082 return clk;
Tang Yuantiana4f20742015-03-13 12:39:01 +080083}
84
85/* traverse cpu nodes to get cpu mask of sharing clock wire */
86static void set_affected_cpus(struct cpufreq_policy *policy)
87{
Tang Yuantiana4f20742015-03-13 12:39:01 +080088 struct cpumask *dstp = policy->cpus;
Tang Yuantianb1e9a642017-02-09 10:33:02 +080089 struct clk *clk;
Tang Yuantiana4f20742015-03-13 12:39:01 +080090 int i;
91
Tang Yuantiana4f20742015-03-13 12:39:01 +080092 for_each_present_cpu(i) {
Tang Yuantianb1e9a642017-02-09 10:33:02 +080093 clk = cpu_to_clk(i);
94 if (IS_ERR(clk)) {
95 pr_err("%s: no clock for cpu %d\n", __func__, i);
Tang Yuantiana4f20742015-03-13 12:39:01 +080096 continue;
Tang Yuantianb1e9a642017-02-09 10:33:02 +080097 }
Tang Yuantiana4f20742015-03-13 12:39:01 +080098
Tang Yuantianb1e9a642017-02-09 10:33:02 +080099 if (clk_is_match(policy->clk, clk))
Tang Yuantiana4f20742015-03-13 12:39:01 +0800100 cpumask_set_cpu(i, dstp);
Tang Yuantiana4f20742015-03-13 12:39:01 +0800101 }
Tang Yuantiana4f20742015-03-13 12:39:01 +0800102}
103
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000104/* reduce the duplicated frequencies in frequency table */
105static 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 */
124static 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;
Tang Yuantiana4f20742015-03-13 12:39:01 +0800130
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000131 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
Tang Yuantiana4f20742015-03-13 12:39:01 +0800155static int qoriq_cpufreq_cpu_init(struct cpufreq_policy *policy)
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000156{
Tang Yuantianb1e9a642017-02-09 10:33:02 +0800157 struct device_node *np;
Viresh Kumar5ab508be2018-02-26 10:39:01 +0530158 int i, count;
Tang Yuantianb1e9a642017-02-09 10:33:02 +0800159 u32 freq;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000160 struct clk *clk;
Tang Yuantianb1e9a642017-02-09 10:33:02 +0800161 const struct clk_hw *hwclk;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000162 struct cpufreq_frequency_table *table;
163 struct cpu_data *data;
164 unsigned int cpu = policy->cpu;
Ed Swarthout906fe032014-06-05 18:56:17 -0500165 u64 u64temp;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000166
167 np = of_get_cpu_node(cpu, NULL);
168 if (!np)
169 return -ENODEV;
170
171 data = kzalloc(sizeof(*data), GFP_KERNEL);
Tang Yuantiana4f20742015-03-13 12:39:01 +0800172 if (!data)
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000173 goto err_np;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000174
Viresh Kumar652ed952014-01-09 20:38:43 +0530175 policy->clk = of_clk_get(np, 0);
176 if (IS_ERR(policy->clk)) {
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000177 pr_err("%s: no clock information\n", __func__);
178 goto err_nomem2;
179 }
180
Tang Yuantianb1e9a642017-02-09 10:33:02 +0800181 hwclk = __clk_get_hw(policy->clk);
182 count = clk_hw_get_num_parents(hwclk);
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000183
Tang Yuantian8a95c142015-06-04 14:25:42 +0800184 data->pclk = kcalloc(count, sizeof(struct clk *), GFP_KERNEL);
Markus Elfringd6b96e42018-02-15 18:00:37 +0100185 if (!data->pclk)
Tang Yuantianb1e9a642017-02-09 10:33:02 +0800186 goto err_nomem2;
Tang Yuantian8a95c142015-06-04 14:25:42 +0800187
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000188 table = kcalloc(count + 1, sizeof(*table), GFP_KERNEL);
Markus Elfringd6b96e42018-02-15 18:00:37 +0100189 if (!table)
Tang Yuantian8a95c142015-06-04 14:25:42 +0800190 goto err_pclk;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000191
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000192 for (i = 0; i < count; i++) {
Tang Yuantianb1e9a642017-02-09 10:33:02 +0800193 clk = clk_hw_get_parent_by_index(hwclk, i)->clk;
Tang Yuantian8a95c142015-06-04 14:25:42 +0800194 data->pclk[i] = clk;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000195 freq = clk_get_rate(clk);
Tang Yuantianb1e9a642017-02-09 10:33:02 +0800196 table[i].frequency = freq / 1000;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000197 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;
Viresh Kumar5ab508be2018-02-26 10:39:01 +0530202 policy->freq_table = table;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000203 data->table = table;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000204
205 /* update ->cpus if we have cluster, no harm if not */
Tang Yuantiana4f20742015-03-13 12:39:01 +0800206 set_affected_cpus(policy);
207 policy->driver_data = data;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000208
Ed Swarthout906fe032014-06-05 18:56:17 -0500209 /* Minimum transition latency is 12 platform clocks */
210 u64temp = 12ULL * NSEC_PER_SEC;
Tang Yuantiana4f20742015-03-13 12:39:01 +0800211 do_div(u64temp, get_bus_freq());
Ed Swarthout906fe032014-06-05 18:56:17 -0500212 policy->cpuinfo.transition_latency = u64temp + 1;
Tim Gardner6712d292014-04-28 10:18:18 -0600213
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000214 of_node_put(np);
215
216 return 0;
217
Tang Yuantian8a95c142015-06-04 14:25:42 +0800218err_pclk:
219 kfree(data->pclk);
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000220err_nomem2:
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000221 kfree(data);
222err_np:
223 of_node_put(np);
224
225 return -ENODEV;
226}
227
Jia Hongtao495c7162016-04-19 17:00:06 +0800228static int qoriq_cpufreq_cpu_exit(struct cpufreq_policy *policy)
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000229{
Tang Yuantiana4f20742015-03-13 12:39:01 +0800230 struct cpu_data *data = policy->driver_data;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000231
Tang Yuantian8a95c142015-06-04 14:25:42 +0800232 kfree(data->pclk);
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000233 kfree(data->table);
234 kfree(data);
Tang Yuantiana4f20742015-03-13 12:39:01 +0800235 policy->driver_data = NULL;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000236
237 return 0;
238}
239
Tang Yuantiana4f20742015-03-13 12:39:01 +0800240static int qoriq_cpufreq_target(struct cpufreq_policy *policy,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530241 unsigned int index)
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000242{
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000243 struct clk *parent;
Tang Yuantiana4f20742015-03-13 12:39:01 +0800244 struct cpu_data *data = policy->driver_data;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000245
Tang Yuantian8a95c142015-06-04 14:25:42 +0800246 parent = data->pclk[data->table[index].driver_data];
Viresh Kumar652ed952014-01-09 20:38:43 +0530247 return clk_set_parent(policy->clk, parent);
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000248}
249
Tang Yuantiana4f20742015-03-13 12:39:01 +0800250static struct cpufreq_driver qoriq_cpufreq_driver = {
251 .name = "qoriq_cpufreq",
Amit Kucheria17170ec2019-01-29 10:25:13 +0530252 .flags = CPUFREQ_CONST_LOOPS |
253 CPUFREQ_IS_COOLING_DEV,
Tang Yuantiana4f20742015-03-13 12:39:01 +0800254 .init = qoriq_cpufreq_cpu_init,
Jia Hongtao495c7162016-04-19 17:00:06 +0800255 .exit = qoriq_cpufreq_cpu_exit,
Viresh Kumardc2398d2013-10-03 20:28:18 +0530256 .verify = cpufreq_generic_frequency_table_verify,
Tang Yuantiana4f20742015-03-13 12:39:01 +0800257 .target_index = qoriq_cpufreq_target,
Viresh Kumar652ed952014-01-09 20:38:43 +0530258 .get = cpufreq_generic_get,
Viresh Kumardc2398d2013-10-03 20:28:18 +0530259 .attr = cpufreq_generic_attr,
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000260};
261
Mian Yousaf Kaukab157f5272020-04-21 10:29:59 +0200262static const struct of_device_id qoriq_cpufreq_blacklist[] = {
Tang Yuantianb1e9a642017-02-09 10:33:02 +0800263 /* e6500 cannot use cpufreq due to erratum A-008083 */
Mian Yousaf Kaukab157f5272020-04-21 10:29:59 +0200264 { .compatible = "fsl,b4420-clockgen", },
265 { .compatible = "fsl,b4860-clockgen", },
266 { .compatible = "fsl,t2080-clockgen", },
267 { .compatible = "fsl,t4240-clockgen", },
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000268 {}
269};
270
Mian Yousaf Kaukab157f5272020-04-21 10:29:59 +0200271static int qoriq_cpufreq_probe(struct platform_device *pdev)
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000272{
273 int ret;
Mian Yousaf Kaukab157f5272020-04-21 10:29:59 +0200274 struct device_node *np;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000275
Mian Yousaf Kaukab157f5272020-04-21 10:29:59 +0200276 np = of_find_matching_node(NULL, qoriq_cpufreq_blacklist);
277 if (np) {
278 dev_info(&pdev->dev, "Disabling due to erratum A-008083");
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000279 return -ENODEV;
Mian Yousaf Kaukab157f5272020-04-21 10:29:59 +0200280 }
Tang Yuantianb1e9a642017-02-09 10:33:02 +0800281
Tang Yuantiana4f20742015-03-13 12:39:01 +0800282 ret = cpufreq_register_driver(&qoriq_cpufreq_driver);
Mian Yousaf Kaukab157f5272020-04-21 10:29:59 +0200283 if (ret)
284 return ret;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000285
Mian Yousaf Kaukab157f5272020-04-21 10:29:59 +0200286 dev_info(&pdev->dev, "Freescale QorIQ CPU frequency scaling driver\n");
287 return 0;
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000288}
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000289
Mian Yousaf Kaukab157f5272020-04-21 10:29:59 +0200290static int qoriq_cpufreq_remove(struct platform_device *pdev)
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000291{
Tang Yuantiana4f20742015-03-13 12:39:01 +0800292 cpufreq_unregister_driver(&qoriq_cpufreq_driver);
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000293
Mian Yousaf Kaukab157f5272020-04-21 10:29:59 +0200294 return 0;
295}
296
297static 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};
304module_platform_driver(qoriq_cpufreq_platform_driver);
305
306MODULE_ALIAS("platform:qoriq-cpufreq");
Tang Yuantiandefa4c72013-06-05 09:30:48 +0000307MODULE_LICENSE("GPL");
308MODULE_AUTHOR("Tang Yuantian <Yuantian.Tang@freescale.com>");
Tang Yuantiana4f20742015-03-13 12:39:01 +0800309MODULE_DESCRIPTION("cpufreq driver for Freescale QorIQ series SoCs");