blob: c587db472a75a21eeabb0c52a1df3b7f48b549f4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Dave Jones32ee8c32006-02-28 00:43:23 -05002 * elanfreq: cpufreq driver for the AMD ELAN family
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * (c) Copyright 2002 Robert Schwebel <r.schwebel@pengutronix.de>
5 *
Dave Jones32ee8c32006-02-28 00:43:23 -05006 * Parts of this code are (c) Sven Geggus <sven@geggus.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Dave Jones32ee8c32006-02-28 00:43:23 -05008 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
Dave Jones32ee8c32006-02-28 00:43:23 -050013 * 2 of the License, or (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 * 2002-02-13: - initial revision for 2.4.18-pre9 by Robert Schwebel
16 *
17 */
18
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/init.h>
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/delay.h>
24#include <linux/cpufreq.h>
25
26#include <asm/msr.h>
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +020027#include <linux/timex.h>
28#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Dave Jones32ee8c32006-02-28 00:43:23 -050030#define REG_CSCIR 0x22 /* Chip Setup and Control Index Register */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#define REG_CSCDR 0x23 /* Chip Setup and Control Data Register */
32
33/* Module parameter */
34static int max_freq;
35
36struct s_elan_multiplier {
37 int clock; /* frequency in kHz */
38 int val40h; /* PMU Force Mode register */
39 int val80h; /* CPU Clock Speed Register */
40};
41
42/*
Dave Jones32ee8c32006-02-28 00:43:23 -050043 * It is important that the frequencies
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * are listed in ascending order here!
45 */
Dave Jones460f5ef2008-07-30 13:01:42 -040046static struct s_elan_multiplier elan_multiplier[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 {1000, 0x02, 0x18},
48 {2000, 0x02, 0x10},
49 {4000, 0x02, 0x08},
50 {8000, 0x00, 0x00},
51 {16000, 0x00, 0x02},
52 {33000, 0x00, 0x04},
53 {66000, 0x01, 0x04},
54 {99000, 0x01, 0x05}
55};
56
57static struct cpufreq_frequency_table elanfreq_table[] = {
58 {0, 1000},
59 {1, 2000},
60 {2, 4000},
61 {3, 8000},
62 {4, 16000},
63 {5, 33000},
64 {6, 66000},
65 {7, 99000},
66 {0, CPUFREQ_TABLE_END},
67};
68
69
70/**
71 * elanfreq_get_cpu_frequency: determine current cpu speed
72 *
73 * Finds out at which frequency the CPU of the Elan SOC runs
Dave Jones32ee8c32006-02-28 00:43:23 -050074 * at the moment. Frequencies from 1 to 33 MHz are generated
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * the normal way, 66 and 99 MHz are called "Hyperspeed Mode"
Dave Jones32ee8c32006-02-28 00:43:23 -050076 * and have the rest of the chip running with 33 MHz.
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 */
78
79static unsigned int elanfreq_get_cpu_frequency(unsigned int cpu)
80{
Dave Jones32ee8c32006-02-28 00:43:23 -050081 u8 clockspeed_reg; /* Clock Speed Register */
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 local_irq_disable();
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +020084 outb_p(0x80, REG_CSCIR);
Dave Jones32ee8c32006-02-28 00:43:23 -050085 clockspeed_reg = inb_p(REG_CSCDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 local_irq_enable();
87
Dave Jones32ee8c32006-02-28 00:43:23 -050088 if ((clockspeed_reg & 0xE0) == 0xE0)
89 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Dave Jones32ee8c32006-02-28 00:43:23 -050091 /* Are we in CPU clock multiplied mode (66/99 MHz)? */
92 if ((clockspeed_reg & 0xE0) == 0xC0) {
93 if ((clockspeed_reg & 0x01) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return 66000;
Dave Jones32ee8c32006-02-28 00:43:23 -050095 else
96 return 99000;
97 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 /* 33 MHz is not 32 MHz... */
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +0200100 if ((clockspeed_reg & 0xE0) == 0xA0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return 33000;
102
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +0200103 return (1<<((clockspeed_reg & 0xE0) >> 5)) * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
106
107/**
Dave Jones32ee8c32006-02-28 00:43:23 -0500108 * elanfreq_set_cpu_frequency: Change the CPU core frequency
109 * @cpu: cpu number
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * @freq: frequency in kHz
111 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500112 * This function takes a frequency value and changes the CPU frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * according to this. Note that the frequency has to be checked by
114 * elanfreq_validatespeed() for correctness!
Dave Jones32ee8c32006-02-28 00:43:23 -0500115 *
116 * There is no return value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
118
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +0200119static void elanfreq_set_cpu_state(unsigned int state)
Dave Jones32ee8c32006-02-28 00:43:23 -0500120{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 struct cpufreq_freqs freqs;
122
123 freqs.old = elanfreq_get_cpu_frequency(0);
124 freqs.new = elan_multiplier[state].clock;
125 freqs.cpu = 0; /* elanfreq.c is UP only driver */
Dave Jones32ee8c32006-02-28 00:43:23 -0500126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
128
Dave Jones32ee8c32006-02-28 00:43:23 -0500129 printk(KERN_INFO "elanfreq: attempting to set frequency to %i kHz\n",
130 elan_multiplier[state].clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132
Dave Jones32ee8c32006-02-28 00:43:23 -0500133 /*
134 * Access to the Elan's internal registers is indexed via
135 * 0x22: Chip Setup & Control Register Index Register (CSCI)
136 * 0x23: Chip Setup & Control Register Data Register (CSCD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 *
138 */
139
Dave Jones32ee8c32006-02-28 00:43:23 -0500140 /*
141 * 0x40 is the Power Management Unit's Force Mode Register.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 * Bit 6 enables Hyperspeed Mode (66/100 MHz core frequency)
143 */
144
145 local_irq_disable();
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +0200146 outb_p(0x40, REG_CSCIR); /* Disable hyperspeed mode */
147 outb_p(0x00, REG_CSCDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 local_irq_enable(); /* wait till internal pipelines and */
149 udelay(1000); /* buffers have cleaned up */
150
151 local_irq_disable();
152
153 /* now, set the CPU clock speed register (0x80) */
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +0200154 outb_p(0x80, REG_CSCIR);
155 outb_p(elan_multiplier[state].val80h, REG_CSCDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 /* now, the hyperspeed bit in PMU Force Mode Register (0x40) */
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +0200158 outb_p(0x40, REG_CSCIR);
159 outb_p(elan_multiplier[state].val40h, REG_CSCDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 udelay(10000);
161 local_irq_enable();
162
163 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
164};
165
166
167/**
168 * elanfreq_validatespeed: test if frequency range is valid
Dave Jones32ee8c32006-02-28 00:43:23 -0500169 * @policy: the policy to validate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500171 * This function checks if a given frequency range in kHz is valid
172 * for the hardware supported by the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 */
174
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +0200175static int elanfreq_verify(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 return cpufreq_frequency_table_verify(policy, &elanfreq_table[0]);
178}
179
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +0200180static int elanfreq_target(struct cpufreq_policy *policy,
Dave Jones32ee8c32006-02-28 00:43:23 -0500181 unsigned int target_freq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 unsigned int relation)
183{
Dave Jones32ee8c32006-02-28 00:43:23 -0500184 unsigned int newstate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Dave Jones04cd1a92009-01-17 22:50:33 -0500186 if (cpufreq_frequency_table_target(policy, &elanfreq_table[0],
187 target_freq, relation, &newstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return -EINVAL;
189
190 elanfreq_set_cpu_state(newstate);
191
192 return 0;
193}
194
195
196/*
197 * Module init and exit code
198 */
199
200static int elanfreq_cpu_init(struct cpufreq_policy *policy)
201{
Mike Travis92cb7612007-10-19 20:35:04 +0200202 struct cpuinfo_x86 *c = &cpu_data(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 unsigned int i;
204 int result;
205
206 /* capability check */
207 if ((c->x86_vendor != X86_VENDOR_AMD) ||
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +0200208 (c->x86 != 4) || (c->x86_model != 10))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return -ENODEV;
210
211 /* max freq */
212 if (!max_freq)
213 max_freq = elanfreq_get_cpu_frequency(0);
214
215 /* table init */
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +0200216 for (i = 0; (elanfreq_table[i].frequency != CPUFREQ_TABLE_END); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (elanfreq_table[i].frequency > max_freq)
218 elanfreq_table[i].frequency = CPUFREQ_ENTRY_INVALID;
219 }
220
221 /* cpuinfo and default policy values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
223 policy->cur = elanfreq_get_cpu_frequency(0);
224
225 result = cpufreq_frequency_table_cpuinfo(policy, elanfreq_table);
226 if (result)
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +0200227 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Dave Jones32ee8c32006-02-28 00:43:23 -0500229 cpufreq_frequency_table_get_attr(elanfreq_table, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return 0;
231}
232
233
234static int elanfreq_cpu_exit(struct cpufreq_policy *policy)
235{
236 cpufreq_frequency_table_put_attr(policy->cpu);
237 return 0;
238}
239
240
241#ifndef MODULE
242/**
243 * elanfreq_setup - elanfreq command line parameter parsing
244 *
245 * elanfreq command line parameter. Use:
246 * elanfreq=66000
247 * to set the maximum CPU frequency to 66 MHz. Note that in
248 * case you do not give this boot parameter, the maximum
249 * frequency will fall back to _current_ CPU frequency which
250 * might be lower. If you build this as a module, use the
251 * max_freq module parameter instead.
252 */
253static int __init elanfreq_setup(char *str)
254{
255 max_freq = simple_strtoul(str, &str, 0);
256 printk(KERN_WARNING "You're using the deprecated elanfreq command line option. Use elanfreq.max_freq instead, please!\n");
257 return 1;
258}
259__setup("elanfreq=", elanfreq_setup);
260#endif
261
262
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +0200263static struct freq_attr *elanfreq_attr[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 &cpufreq_freq_attr_scaling_available_freqs,
265 NULL,
266};
267
268
Linus Torvalds221dee22007-02-26 14:55:48 -0800269static struct cpufreq_driver elanfreq_driver = {
Dave Jones32ee8c32006-02-28 00:43:23 -0500270 .get = elanfreq_get_cpu_frequency,
271 .verify = elanfreq_verify,
272 .target = elanfreq_target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 .init = elanfreq_cpu_init,
274 .exit = elanfreq_cpu_exit,
275 .name = "elanfreq",
276 .owner = THIS_MODULE,
277 .attr = elanfreq_attr,
278};
279
280
Dave Jones32ee8c32006-02-28 00:43:23 -0500281static int __init elanfreq_init(void)
282{
Mike Travis92cb7612007-10-19 20:35:04 +0200283 struct cpuinfo_x86 *c = &cpu_data(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 /* Test if we have the right hardware */
286 if ((c->x86_vendor != X86_VENDOR_AMD) ||
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +0200287 (c->x86 != 4) || (c->x86_model != 10)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 printk(KERN_INFO "elanfreq: error: no Elan processor found!\n");
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +0200289 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return cpufreq_register_driver(&elanfreq_driver);
292}
293
294
Dave Jones32ee8c32006-02-28 00:43:23 -0500295static void __exit elanfreq_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 cpufreq_unregister_driver(&elanfreq_driver);
298}
299
300
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +0200301module_param(max_freq, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303MODULE_LICENSE("GPL");
Dave Jones04cd1a92009-01-17 22:50:33 -0500304MODULE_AUTHOR("Robert Schwebel <r.schwebel@pengutronix.de>, "
305 "Sven Geggus <sven@geggus.net>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306MODULE_DESCRIPTION("cpufreq driver for AMD's Elan CPUs");
307
308module_init(elanfreq_init);
309module_exit(elanfreq_exit);