loongson2_cpufreq.c 5.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Cpufreq driver for the loongson-2 processors
 *
 * The 2E revision of loongson processor not support this feature.
 *
 * Copyright (C) 2006 - 2008 Lemote Inc. & Insititute of Computing Technology
 * Author: Yanhua, yanh@lemote.com
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 */
#include <linux/cpufreq.h>
#include <linux/module.h>
#include <linux/err.h>
#include <linux/sched.h>	/* set_cpus_allowed() */
#include <linux/delay.h>
#include <linux/platform_device.h>

#include <asm/clock.h>
21
#include <asm/idle.h>
22

23
#include <asm/mach-loongson/loongson.h>
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

static uint nowait;

static struct clk *cpuclk;

static void (*saved_cpu_wait) (void);

static int loongson2_cpu_freq_notifier(struct notifier_block *nb,
					unsigned long val, void *data);

static struct notifier_block loongson2_cpufreq_notifier_block = {
	.notifier_call = loongson2_cpu_freq_notifier
};

static int loongson2_cpu_freq_notifier(struct notifier_block *nb,
					unsigned long val, void *data)
{
	if (val == CPUFREQ_POSTCHANGE)
		current_cpu_data.udelay_val = loops_per_jiffy;

	return 0;
}

static unsigned int loongson2_cpufreq_get(unsigned int cpu)
{
	return clk_get_rate(cpuclk);
}

/*
 * Here we notify other drivers of the proposed change and the final change.
 */
static int loongson2_cpufreq_target(struct cpufreq_policy *policy,
56
				     unsigned int index)
57 58 59 60 61 62 63
{
	unsigned int cpu = policy->cpu;
	cpumask_t cpus_allowed;
	struct cpufreq_freqs freqs;
	unsigned int freq;

	cpus_allowed = current->cpus_allowed;
J
Julia Lawall 已提交
64
	set_cpus_allowed_ptr(current, cpumask_of(cpu));
65 66 67

	freq =
	    ((cpu_clock_freq / 1000) *
68
	     loongson2_clockmod_table[index].driver_data) / 8;
69

70 71
	pr_debug("cpufreq: requested frequency %u Hz\n",
			loongson2_clockmod_table[index].frequency * 1000);
72 73 74 75 76 77

	freqs.old = loongson2_cpufreq_get(cpu);
	freqs.new = freq;
	freqs.flags = 0;

	/* notifiers */
78
	cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
79

J
Julia Lawall 已提交
80
	set_cpus_allowed_ptr(current, &cpus_allowed);
81 82 83 84 85

	/* setting the cpu frequency */
	clk_set_rate(cpuclk, freq);

	/* notifiers */
86
	cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
87 88 89 90 91 92 93 94 95

	pr_debug("cpufreq: set frequency %u kHz\n", freq);

	return 0;
}

static int loongson2_cpufreq_cpu_init(struct cpufreq_policy *policy)
{
	int i;
96 97
	unsigned long rate;
	int ret;
98 99 100 101 102 103 104

	cpuclk = clk_get(NULL, "cpu_clk");
	if (IS_ERR(cpuclk)) {
		printk(KERN_ERR "cpufreq: couldn't get CPU clk\n");
		return PTR_ERR(cpuclk);
	}

105 106 107
	rate = cpu_clock_freq / 1000;
	if (!rate) {
		clk_put(cpuclk);
108
		return -EINVAL;
109
	}
110 111 112 113 114

	/* clock table init */
	for (i = 2;
	     (loongson2_clockmod_table[i].frequency != CPUFREQ_TABLE_END);
	     i++)
115
		loongson2_clockmod_table[i].frequency = (rate * i) / 8;
116

117 118 119 120 121 122
	ret = clk_set_rate(cpuclk, rate);
	if (ret) {
		clk_put(cpuclk);
		return ret;
	}

123
	return cpufreq_generic_init(policy, &loongson2_clockmod_table[0], 0);
124 125 126 127
}

static int loongson2_cpufreq_exit(struct cpufreq_policy *policy)
{
128
	cpufreq_frequency_table_put_attr(policy->cpu);
129 130 131 132 133 134 135
	clk_put(cpuclk);
	return 0;
}

static struct cpufreq_driver loongson2_cpufreq_driver = {
	.name = "loongson2",
	.init = loongson2_cpufreq_cpu_init,
136
	.verify = cpufreq_generic_frequency_table_verify,
137
	.target_index = loongson2_cpufreq_target,
138 139
	.get = loongson2_cpufreq_get,
	.exit = loongson2_cpufreq_exit,
140
	.attr = cpufreq_generic_attr,
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
};

static struct platform_device_id platform_device_ids[] = {
	{
		.name = "loongson2_cpufreq",
	},
	{}
};

MODULE_DEVICE_TABLE(platform, platform_device_ids);

static struct platform_driver platform_driver = {
	.driver = {
		.name = "loongson2_cpufreq",
		.owner = THIS_MODULE,
	},
	.id_table = platform_device_ids,
};

160 161 162 163 164 165 166 167 168 169 170 171 172 173
/*
 * This is the simple version of Loongson-2 wait, Maybe we need do this in
 * interrupt disabled context.
 */

static DEFINE_SPINLOCK(loongson2_wait_lock);

static void loongson2_cpu_wait(void)
{
	unsigned long flags;
	u32 cpu_freq;

	spin_lock_irqsave(&loongson2_wait_lock, flags);
	cpu_freq = LOONGSON_CHIPCFG0;
R
Ralf Baechle 已提交
174 175
	LOONGSON_CHIPCFG0 &= ~0x7;	/* Put CPU into wait mode */
	LOONGSON_CHIPCFG0 = cpu_freq;	/* Restore CPU state */
176
	spin_unlock_irqrestore(&loongson2_wait_lock, flags);
177
	local_irq_enable();
178 179
}

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
static int __init cpufreq_init(void)
{
	int ret;

	/* Register platform stuff */
	ret = platform_driver_register(&platform_driver);
	if (ret)
		return ret;

	pr_info("cpufreq: Loongson-2F CPU frequency driver.\n");

	cpufreq_register_notifier(&loongson2_cpufreq_notifier_block,
				  CPUFREQ_TRANSITION_NOTIFIER);

	ret = cpufreq_register_driver(&loongson2_cpufreq_driver);

	if (!ret && !nowait) {
		saved_cpu_wait = cpu_wait;
		cpu_wait = loongson2_cpu_wait;
	}

	return ret;
}

static void __exit cpufreq_exit(void)
{
	if (!nowait && saved_cpu_wait)
		cpu_wait = saved_cpu_wait;
	cpufreq_unregister_driver(&loongson2_cpufreq_driver);
	cpufreq_unregister_notifier(&loongson2_cpufreq_notifier_block,
				    CPUFREQ_TRANSITION_NOTIFIER);

	platform_driver_unregister(&platform_driver);
}

module_init(cpufreq_init);
module_exit(cpufreq_exit);

module_param(nowait, uint, 0644);
MODULE_PARM_DESC(nowait, "Disable Loongson-2F specific wait");

MODULE_AUTHOR("Yanhua <yanh@lemote.com>");
MODULE_DESCRIPTION("cpufreq driver for Loongson2F");
MODULE_LICENSE("GPL");