qoriq-cpufreq.c 8.38 KB
Newer Older
1 2 3
/*
 * Copyright 2013 Freescale Semiconductor, Inc.
 *
4
 * CPU Frequency Scaling driver for Freescale QorIQ SoCs.
5 6 7 8 9 10 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt

#include <linux/clk.h>
14
#include <linux/clk-provider.h>
15
#include <linux/cpufreq.h>
16
#include <linux/cpu_cooling.h>
17 18 19 20 21 22 23 24 25 26
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/slab.h>
#include <linux/smp.h>

/**
27
 * struct cpu_data
28
 * @pclk: the parent clock of cpu
29 30 31
 * @table: frequency table
 */
struct cpu_data {
32
	struct clk **pclk;
33
	struct cpufreq_frequency_table *table;
34
	struct thermal_cooling_device *cdev;
35 36
};

37 38 39 40 41 42
/*
 * Don't use cpufreq on this SoC -- used when the SoC would have otherwise
 * matched a more generic compatible.
 */
#define SOC_BLACKLIST		1

43 44
/**
 * struct soc_data - SoC specific data
45
 * @flags: SOC_xxx
46 47
 */
struct soc_data {
48
	u32 flags;
49 50
};

51 52 53 54
static u32 get_bus_freq(void)
{
	struct device_node *soc;
	u32 sysfreq;
55 56
	struct clk *pltclk;
	int ret;
57

58
	/* get platform freq by searching bus-frequency property */
59
	soc = of_find_node_by_type(NULL, "soc");
60 61 62 63 64 65
	if (soc) {
		ret = of_property_read_u32(soc, "bus-frequency", &sysfreq);
		of_node_put(soc);
		if (!ret)
			return sysfreq;
	}
66

67 68 69 70 71 72 73
	/* get platform freq by its clock name */
	pltclk = clk_get(NULL, "cg-pll0-div1");
	if (IS_ERR(pltclk)) {
		pr_err("%s: can't get bus frequency %ld\n",
		       __func__, PTR_ERR(pltclk));
		return PTR_ERR(pltclk);
	}
74

75
	return clk_get_rate(pltclk);
76
}
77

78
static struct clk *cpu_to_clk(int cpu)
79
{
80 81
	struct device_node *np;
	struct clk *clk;
82 83 84 85 86 87 88 89

	if (!cpu_present(cpu))
		return NULL;

	np = of_get_cpu_node(cpu, NULL);
	if (!np)
		return NULL;

90
	clk = of_clk_get(np, 0);
91
	of_node_put(np);
92
	return clk;
93 94 95 96 97 98
}

/* traverse cpu nodes to get cpu mask of sharing clock wire */
static void set_affected_cpus(struct cpufreq_policy *policy)
{
	struct cpumask *dstp = policy->cpus;
99
	struct clk *clk;
100 101 102
	int i;

	for_each_present_cpu(i) {
103 104 105
		clk = cpu_to_clk(i);
		if (IS_ERR(clk)) {
			pr_err("%s: no clock for cpu %d\n", __func__, i);
106
			continue;
107
		}
108

109
		if (clk_is_match(policy->clk, clk))
110 111
			cpumask_set_cpu(i, dstp);
	}
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
}

/* reduce the duplicated frequencies in frequency table */
static void freq_table_redup(struct cpufreq_frequency_table *freq_table,
		int count)
{
	int i, j;

	for (i = 1; i < count; i++) {
		for (j = 0; j < i; j++) {
			if (freq_table[j].frequency == CPUFREQ_ENTRY_INVALID ||
					freq_table[j].frequency !=
					freq_table[i].frequency)
				continue;

			freq_table[i].frequency = CPUFREQ_ENTRY_INVALID;
			break;
		}
	}
}

/* sort the frequencies in frequency table in descenting order */
static void freq_table_sort(struct cpufreq_frequency_table *freq_table,
		int count)
{
	int i, j, ind;
	unsigned int freq, max_freq;
	struct cpufreq_frequency_table table;
140

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	for (i = 0; i < count - 1; i++) {
		max_freq = freq_table[i].frequency;
		ind = i;
		for (j = i + 1; j < count; j++) {
			freq = freq_table[j].frequency;
			if (freq == CPUFREQ_ENTRY_INVALID ||
					freq <= max_freq)
				continue;
			ind = j;
			max_freq = freq;
		}

		if (ind != i) {
			/* exchange the frequencies */
			table.driver_data = freq_table[i].driver_data;
			table.frequency = freq_table[i].frequency;
			freq_table[i].driver_data = freq_table[ind].driver_data;
			freq_table[i].frequency = freq_table[ind].frequency;
			freq_table[ind].driver_data = table.driver_data;
			freq_table[ind].frequency = table.frequency;
		}
	}
}

165
static int qoriq_cpufreq_cpu_init(struct cpufreq_policy *policy)
166
{
167
	struct device_node *np;
168
	int i, count, ret;
169
	u32 freq;
170
	struct clk *clk;
171
	const struct clk_hw *hwclk;
172 173 174
	struct cpufreq_frequency_table *table;
	struct cpu_data *data;
	unsigned int cpu = policy->cpu;
175
	u64 u64temp;
176 177 178 179 180 181

	np = of_get_cpu_node(cpu, NULL);
	if (!np)
		return -ENODEV;

	data = kzalloc(sizeof(*data), GFP_KERNEL);
182
	if (!data)
183 184
		goto err_np;

185 186
	policy->clk = of_clk_get(np, 0);
	if (IS_ERR(policy->clk)) {
187 188 189 190
		pr_err("%s: no clock information\n", __func__);
		goto err_nomem2;
	}

191 192
	hwclk = __clk_get_hw(policy->clk);
	count = clk_hw_get_num_parents(hwclk);
193

194 195 196
	data->pclk = kcalloc(count, sizeof(struct clk *), GFP_KERNEL);
	if (!data->pclk) {
		pr_err("%s: no memory\n", __func__);
197
		goto err_nomem2;
198 199
	}

200 201 202
	table = kcalloc(count + 1, sizeof(*table), GFP_KERNEL);
	if (!table) {
		pr_err("%s: no memory\n", __func__);
203
		goto err_pclk;
204 205 206
	}

	for (i = 0; i < count; i++) {
207
		clk = clk_hw_get_parent_by_index(hwclk, i)->clk;
208
		data->pclk[i] = clk;
209
		freq = clk_get_rate(clk);
210
		table[i].frequency = freq / 1000;
211 212 213 214 215 216 217
		table[i].driver_data = i;
	}
	freq_table_redup(table, count);
	freq_table_sort(table, count);
	table[i].frequency = CPUFREQ_TABLE_END;

	/* set the min and max frequency properly */
218
	ret = cpufreq_table_validate_and_show(policy, table);
219 220 221 222 223 224 225 226
	if (ret) {
		pr_err("invalid frequency table: %d\n", ret);
		goto err_nomem1;
	}

	data->table = table;

	/* update ->cpus if we have cluster, no harm if not */
227 228
	set_affected_cpus(policy);
	policy->driver_data = data;
229

230 231
	/* Minimum transition latency is 12 platform clocks */
	u64temp = 12ULL * NSEC_PER_SEC;
232
	do_div(u64temp, get_bus_freq());
233
	policy->cpuinfo.transition_latency = u64temp + 1;
234

235 236 237 238 239 240
	of_node_put(np);

	return 0;

err_nomem1:
	kfree(table);
241 242
err_pclk:
	kfree(data->pclk);
243 244 245 246 247 248 249 250
err_nomem2:
	kfree(data);
err_np:
	of_node_put(np);

	return -ENODEV;
}

251
static int qoriq_cpufreq_cpu_exit(struct cpufreq_policy *policy)
252
{
253
	struct cpu_data *data = policy->driver_data;
254

255
	cpufreq_cooling_unregister(data->cdev);
256
	kfree(data->pclk);
257 258
	kfree(data->table);
	kfree(data);
259
	policy->driver_data = NULL;
260 261 262 263

	return 0;
}

264
static int qoriq_cpufreq_target(struct cpufreq_policy *policy,
265
		unsigned int index)
266 267
{
	struct clk *parent;
268
	struct cpu_data *data = policy->driver_data;
269

270
	parent = data->pclk[data->table[index].driver_data];
271
	return clk_set_parent(policy->clk, parent);
272 273
}

274 275 276 277 278 279 280 281 282 283

static void qoriq_cpufreq_ready(struct cpufreq_policy *policy)
{
	struct cpu_data *cpud = policy->driver_data;
	struct device_node *np = of_get_cpu_node(policy->cpu, NULL);

	if (of_find_property(np, "#cooling-cells", NULL)) {
		cpud->cdev = of_cpufreq_cooling_register(np,
							 policy->related_cpus);

284 285
		if (IS_ERR(cpud->cdev) && PTR_ERR(cpud->cdev) != -ENOSYS) {
			pr_err("cpu%d is not running as cooling device: %ld\n",
286 287 288 289 290 291 292 293 294
					policy->cpu, PTR_ERR(cpud->cdev));

			cpud->cdev = NULL;
		}
	}

	of_node_put(np);
}

295 296
static struct cpufreq_driver qoriq_cpufreq_driver = {
	.name		= "qoriq_cpufreq",
297
	.flags		= CPUFREQ_CONST_LOOPS,
298
	.init		= qoriq_cpufreq_cpu_init,
299
	.exit		= qoriq_cpufreq_cpu_exit,
300
	.verify		= cpufreq_generic_frequency_table_verify,
301
	.target_index	= qoriq_cpufreq_target,
302
	.get		= cpufreq_generic_get,
303
	.ready		= qoriq_cpufreq_ready,
304
	.attr		= cpufreq_generic_attr,
305 306
};

307 308 309 310
static const struct soc_data blacklist = {
	.flags = SOC_BLACKLIST,
};

311
static const struct of_device_id node_matches[] __initconst = {
312 313 314 315 316 317 318 319 320 321 322 323 324 325
	/* e6500 cannot use cpufreq due to erratum A-008083 */
	{ .compatible = "fsl,b4420-clockgen", &blacklist },
	{ .compatible = "fsl,b4860-clockgen", &blacklist },
	{ .compatible = "fsl,t2080-clockgen", &blacklist },
	{ .compatible = "fsl,t4240-clockgen", &blacklist },

	{ .compatible = "fsl,ls1012a-clockgen", },
	{ .compatible = "fsl,ls1021a-clockgen", },
	{ .compatible = "fsl,ls1043a-clockgen", },
	{ .compatible = "fsl,ls1046a-clockgen", },
	{ .compatible = "fsl,ls1088a-clockgen", },
	{ .compatible = "fsl,ls2080a-clockgen", },
	{ .compatible = "fsl,p4080-clockgen", },
	{ .compatible = "fsl,qoriq-clockgen-1.0", },
326 327 328 329
	{ .compatible = "fsl,qoriq-clockgen-2.0", },
	{}
};

330
static int __init qoriq_cpufreq_init(void)
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
{
	int ret;
	struct device_node  *np;
	const struct of_device_id *match;
	const struct soc_data *data;

	np = of_find_matching_node(NULL, node_matches);
	if (!np)
		return -ENODEV;

	match = of_match_node(node_matches, np);
	data = match->data;

	of_node_put(np);

346 347 348
	if (data && data->flags & SOC_BLACKLIST)
		return -ENODEV;

349
	ret = cpufreq_register_driver(&qoriq_cpufreq_driver);
350
	if (!ret)
351
		pr_info("Freescale QorIQ CPU frequency scaling driver\n");
352 353 354

	return ret;
}
355
module_init(qoriq_cpufreq_init);
356

357
static void __exit qoriq_cpufreq_exit(void)
358
{
359
	cpufreq_unregister_driver(&qoriq_cpufreq_driver);
360
}
361
module_exit(qoriq_cpufreq_exit);
362 363 364

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Tang Yuantian <Yuantian.Tang@freescale.com>");
365
MODULE_DESCRIPTION("cpufreq driver for Freescale QorIQ series SoCs");