qoriq-cpufreq.c 8.05 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 55 56 57 58 59 60 61 62 63
static u32 get_bus_freq(void)
{
	struct device_node *soc;
	u32 sysfreq;

	soc = of_find_node_by_type(NULL, "soc");
	if (!soc)
		return 0;

	if (of_property_read_u32(soc, "bus-frequency", &sysfreq))
		sysfreq = 0;

	of_node_put(soc);
64

65 66
	return sysfreq;
}
67

68
static struct clk *cpu_to_clk(int cpu)
69
{
70 71
	struct device_node *np;
	struct clk *clk;
72 73 74 75 76 77 78 79

	if (!cpu_present(cpu))
		return NULL;

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

80
	clk = of_clk_get(np, 0);
81
	of_node_put(np);
82
	return clk;
83 84 85 86 87 88
}

/* 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;
89
	struct clk *clk;
90 91 92
	int i;

	for_each_present_cpu(i) {
93 94 95
		clk = cpu_to_clk(i);
		if (IS_ERR(clk)) {
			pr_err("%s: no clock for cpu %d\n", __func__, i);
96
			continue;
97
		}
98

99
		if (clk_is_match(policy->clk, clk))
100 101
			cpumask_set_cpu(i, dstp);
	}
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
}

/* 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;
130

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
	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;
		}
	}
}

155
static int qoriq_cpufreq_cpu_init(struct cpufreq_policy *policy)
156
{
157
	struct device_node *np;
158
	int i, count, ret;
159
	u32 freq;
160
	struct clk *clk;
161
	const struct clk_hw *hwclk;
162 163 164
	struct cpufreq_frequency_table *table;
	struct cpu_data *data;
	unsigned int cpu = policy->cpu;
165
	u64 u64temp;
166 167 168 169 170 171

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

	data = kzalloc(sizeof(*data), GFP_KERNEL);
172
	if (!data)
173 174
		goto err_np;

175 176
	policy->clk = of_clk_get(np, 0);
	if (IS_ERR(policy->clk)) {
177 178 179 180
		pr_err("%s: no clock information\n", __func__);
		goto err_nomem2;
	}

181 182
	hwclk = __clk_get_hw(policy->clk);
	count = clk_hw_get_num_parents(hwclk);
183

184 185 186
	data->pclk = kcalloc(count, sizeof(struct clk *), GFP_KERNEL);
	if (!data->pclk) {
		pr_err("%s: no memory\n", __func__);
187
		goto err_nomem2;
188 189
	}

190 191 192
	table = kcalloc(count + 1, sizeof(*table), GFP_KERNEL);
	if (!table) {
		pr_err("%s: no memory\n", __func__);
193
		goto err_pclk;
194 195 196
	}

	for (i = 0; i < count; i++) {
197
		clk = clk_hw_get_parent_by_index(hwclk, i)->clk;
198
		data->pclk[i] = clk;
199
		freq = clk_get_rate(clk);
200
		table[i].frequency = freq / 1000;
201 202 203 204 205 206 207
		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 */
208
	ret = cpufreq_table_validate_and_show(policy, table);
209 210 211 212 213 214 215 216
	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 */
217 218
	set_affected_cpus(policy);
	policy->driver_data = data;
219

220 221
	/* Minimum transition latency is 12 platform clocks */
	u64temp = 12ULL * NSEC_PER_SEC;
222
	do_div(u64temp, get_bus_freq());
223
	policy->cpuinfo.transition_latency = u64temp + 1;
224

225 226 227 228 229 230
	of_node_put(np);

	return 0;

err_nomem1:
	kfree(table);
231 232
err_pclk:
	kfree(data->pclk);
233 234 235 236 237 238 239 240
err_nomem2:
	kfree(data);
err_np:
	of_node_put(np);

	return -ENODEV;
}

241
static int qoriq_cpufreq_cpu_exit(struct cpufreq_policy *policy)
242
{
243
	struct cpu_data *data = policy->driver_data;
244

245
	cpufreq_cooling_unregister(data->cdev);
246
	kfree(data->pclk);
247 248
	kfree(data->table);
	kfree(data);
249
	policy->driver_data = NULL;
250 251 252 253

	return 0;
}

254
static int qoriq_cpufreq_target(struct cpufreq_policy *policy,
255
		unsigned int index)
256 257
{
	struct clk *parent;
258
	struct cpu_data *data = policy->driver_data;
259

260
	parent = data->pclk[data->table[index].driver_data];
261
	return clk_set_parent(policy->clk, parent);
262 263
}

264 265 266 267 268 269 270 271 272 273

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);

274 275
		if (IS_ERR(cpud->cdev) && PTR_ERR(cpud->cdev) != -ENOSYS) {
			pr_err("cpu%d is not running as cooling device: %ld\n",
276 277 278 279 280 281 282 283 284
					policy->cpu, PTR_ERR(cpud->cdev));

			cpud->cdev = NULL;
		}
	}

	of_node_put(np);
}

285 286
static struct cpufreq_driver qoriq_cpufreq_driver = {
	.name		= "qoriq_cpufreq",
287
	.flags		= CPUFREQ_CONST_LOOPS,
288
	.init		= qoriq_cpufreq_cpu_init,
289
	.exit		= qoriq_cpufreq_cpu_exit,
290
	.verify		= cpufreq_generic_frequency_table_verify,
291
	.target_index	= qoriq_cpufreq_target,
292
	.get		= cpufreq_generic_get,
293
	.ready		= qoriq_cpufreq_ready,
294
	.attr		= cpufreq_generic_attr,
295 296
};

297 298 299 300
static const struct soc_data blacklist = {
	.flags = SOC_BLACKLIST,
};

301
static const struct of_device_id node_matches[] __initconst = {
302 303 304 305 306 307 308 309 310 311 312 313 314 315
	/* 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", },
316 317 318 319
	{ .compatible = "fsl,qoriq-clockgen-2.0", },
	{}
};

320
static int __init qoriq_cpufreq_init(void)
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
{
	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);

336 337 338
	if (data && data->flags & SOC_BLACKLIST)
		return -ENODEV;

339
	ret = cpufreq_register_driver(&qoriq_cpufreq_driver);
340
	if (!ret)
341
		pr_info("Freescale QorIQ CPU frequency scaling driver\n");
342 343 344

	return ret;
}
345
module_init(qoriq_cpufreq_init);
346

347
static void __exit qoriq_cpufreq_exit(void)
348
{
349
	cpufreq_unregister_driver(&qoriq_cpufreq_driver);
350
}
351
module_exit(qoriq_cpufreq_exit);
352 353 354

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