Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * elanfreq: cpufreq driver for the AMD ELAN family | |
3 | * | |
4 | * (c) Copyright 2002 Robert Schwebel <r.schwebel@pengutronix.de> | |
5 | * | |
6 | * Parts of this code are (c) Sven Geggus <sven@geggus.net> | |
7 | * | |
8 | * All Rights Reserved. | |
9 | * | |
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 | |
13 | * 2 of the License, or (at your option) any later version. | |
14 | * | |
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 | ||
23 | #include <linux/slab.h> | |
24 | #include <linux/delay.h> | |
25 | #include <linux/cpufreq.h> | |
26 | ||
27 | #include <asm/msr.h> | |
28 | #include <asm/timex.h> | |
29 | #include <asm/io.h> | |
30 | ||
31 | #define REG_CSCIR 0x22 /* Chip Setup and Control Index Register */ | |
32 | #define REG_CSCDR 0x23 /* Chip Setup and Control Data Register */ | |
33 | ||
34 | /* Module parameter */ | |
35 | static int max_freq; | |
36 | ||
37 | struct s_elan_multiplier { | |
38 | int clock; /* frequency in kHz */ | |
39 | int val40h; /* PMU Force Mode register */ | |
40 | int val80h; /* CPU Clock Speed Register */ | |
41 | }; | |
42 | ||
43 | /* | |
44 | * It is important that the frequencies | |
45 | * are listed in ascending order here! | |
46 | */ | |
47 | struct s_elan_multiplier elan_multiplier[] = { | |
48 | {1000, 0x02, 0x18}, | |
49 | {2000, 0x02, 0x10}, | |
50 | {4000, 0x02, 0x08}, | |
51 | {8000, 0x00, 0x00}, | |
52 | {16000, 0x00, 0x02}, | |
53 | {33000, 0x00, 0x04}, | |
54 | {66000, 0x01, 0x04}, | |
55 | {99000, 0x01, 0x05} | |
56 | }; | |
57 | ||
58 | static struct cpufreq_frequency_table elanfreq_table[] = { | |
59 | {0, 1000}, | |
60 | {1, 2000}, | |
61 | {2, 4000}, | |
62 | {3, 8000}, | |
63 | {4, 16000}, | |
64 | {5, 33000}, | |
65 | {6, 66000}, | |
66 | {7, 99000}, | |
67 | {0, CPUFREQ_TABLE_END}, | |
68 | }; | |
69 | ||
70 | ||
71 | /** | |
72 | * elanfreq_get_cpu_frequency: determine current cpu speed | |
73 | * | |
74 | * Finds out at which frequency the CPU of the Elan SOC runs | |
75 | * at the moment. Frequencies from 1 to 33 MHz are generated | |
76 | * the normal way, 66 and 99 MHz are called "Hyperspeed Mode" | |
77 | * and have the rest of the chip running with 33 MHz. | |
78 | */ | |
79 | ||
80 | static unsigned int elanfreq_get_cpu_frequency(unsigned int cpu) | |
81 | { | |
82 | u8 clockspeed_reg; /* Clock Speed Register */ | |
83 | ||
84 | local_irq_disable(); | |
85 | outb_p(0x80,REG_CSCIR); | |
86 | clockspeed_reg = inb_p(REG_CSCDR); | |
87 | local_irq_enable(); | |
88 | ||
89 | if ((clockspeed_reg & 0xE0) == 0xE0) { return 0; } | |
90 | ||
91 | /* Are we in CPU clock multiplied mode (66/99 MHz)? */ | |
92 | if ((clockspeed_reg & 0xE0) == 0xC0) { | |
93 | if ((clockspeed_reg & 0x01) == 0) { | |
94 | return 66000; | |
95 | } else { | |
96 | return 99000; | |
97 | } | |
98 | } | |
99 | ||
100 | /* 33 MHz is not 32 MHz... */ | |
101 | if ((clockspeed_reg & 0xE0)==0xA0) | |
102 | return 33000; | |
103 | ||
104 | return ((1<<((clockspeed_reg & 0xE0) >> 5)) * 1000); | |
105 | } | |
106 | ||
107 | ||
108 | /** | |
109 | * elanfreq_set_cpu_frequency: Change the CPU core frequency | |
110 | * @cpu: cpu number | |
111 | * @freq: frequency in kHz | |
112 | * | |
113 | * This function takes a frequency value and changes the CPU frequency | |
114 | * according to this. Note that the frequency has to be checked by | |
115 | * elanfreq_validatespeed() for correctness! | |
116 | * | |
117 | * There is no return value. | |
118 | */ | |
119 | ||
120 | static void elanfreq_set_cpu_state (unsigned int state) { | |
121 | ||
122 | struct cpufreq_freqs freqs; | |
123 | ||
124 | freqs.old = elanfreq_get_cpu_frequency(0); | |
125 | freqs.new = elan_multiplier[state].clock; | |
126 | freqs.cpu = 0; /* elanfreq.c is UP only driver */ | |
127 | ||
128 | cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE); | |
129 | ||
130 | printk(KERN_INFO "elanfreq: attempting to set frequency to %i kHz\n",elan_multiplier[state].clock); | |
131 | ||
132 | ||
133 | /* | |
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) | |
137 | * | |
138 | */ | |
139 | ||
140 | /* | |
141 | * 0x40 is the Power Management Unit's Force Mode Register. | |
142 | * Bit 6 enables Hyperspeed Mode (66/100 MHz core frequency) | |
143 | */ | |
144 | ||
145 | local_irq_disable(); | |
146 | outb_p(0x40,REG_CSCIR); /* Disable hyperspeed mode */ | |
147 | outb_p(0x00,REG_CSCDR); | |
148 | 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) */ | |
154 | outb_p(0x80,REG_CSCIR); | |
155 | outb_p(elan_multiplier[state].val80h,REG_CSCDR); | |
156 | ||
157 | /* now, the hyperspeed bit in PMU Force Mode Register (0x40) */ | |
158 | outb_p(0x40,REG_CSCIR); | |
159 | outb_p(elan_multiplier[state].val40h,REG_CSCDR); | |
160 | 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 | |
169 | * @policy: the policy to validate | |
170 | * | |
171 | * This function checks if a given frequency range in kHz is valid | |
172 | * for the hardware supported by the driver. | |
173 | */ | |
174 | ||
175 | static int elanfreq_verify (struct cpufreq_policy *policy) | |
176 | { | |
177 | return cpufreq_frequency_table_verify(policy, &elanfreq_table[0]); | |
178 | } | |
179 | ||
180 | static int elanfreq_target (struct cpufreq_policy *policy, | |
181 | unsigned int target_freq, | |
182 | unsigned int relation) | |
183 | { | |
184 | unsigned int newstate = 0; | |
185 | ||
186 | if (cpufreq_frequency_table_target(policy, &elanfreq_table[0], target_freq, relation, &newstate)) | |
187 | return -EINVAL; | |
188 | ||
189 | elanfreq_set_cpu_state(newstate); | |
190 | ||
191 | return 0; | |
192 | } | |
193 | ||
194 | ||
195 | /* | |
196 | * Module init and exit code | |
197 | */ | |
198 | ||
199 | static int elanfreq_cpu_init(struct cpufreq_policy *policy) | |
200 | { | |
201 | struct cpuinfo_x86 *c = cpu_data; | |
202 | unsigned int i; | |
203 | int result; | |
204 | ||
205 | /* capability check */ | |
206 | if ((c->x86_vendor != X86_VENDOR_AMD) || | |
207 | (c->x86 != 4) || (c->x86_model!=10)) | |
208 | return -ENODEV; | |
209 | ||
210 | /* max freq */ | |
211 | if (!max_freq) | |
212 | max_freq = elanfreq_get_cpu_frequency(0); | |
213 | ||
214 | /* table init */ | |
215 | for (i=0; (elanfreq_table[i].frequency != CPUFREQ_TABLE_END); i++) { | |
216 | if (elanfreq_table[i].frequency > max_freq) | |
217 | elanfreq_table[i].frequency = CPUFREQ_ENTRY_INVALID; | |
218 | } | |
219 | ||
220 | /* cpuinfo and default policy values */ | |
221 | policy->governor = CPUFREQ_DEFAULT_GOVERNOR; | |
222 | 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) | |
227 | return (result); | |
228 | ||
229 | cpufreq_frequency_table_get_attr(elanfreq_table, policy->cpu); | |
230 | ||
231 | return 0; | |
232 | } | |
233 | ||
234 | ||
235 | static int elanfreq_cpu_exit(struct cpufreq_policy *policy) | |
236 | { | |
237 | cpufreq_frequency_table_put_attr(policy->cpu); | |
238 | return 0; | |
239 | } | |
240 | ||
241 | ||
242 | #ifndef MODULE | |
243 | /** | |
244 | * elanfreq_setup - elanfreq command line parameter parsing | |
245 | * | |
246 | * elanfreq command line parameter. Use: | |
247 | * elanfreq=66000 | |
248 | * to set the maximum CPU frequency to 66 MHz. Note that in | |
249 | * case you do not give this boot parameter, the maximum | |
250 | * frequency will fall back to _current_ CPU frequency which | |
251 | * might be lower. If you build this as a module, use the | |
252 | * max_freq module parameter instead. | |
253 | */ | |
254 | static int __init elanfreq_setup(char *str) | |
255 | { | |
256 | max_freq = simple_strtoul(str, &str, 0); | |
257 | printk(KERN_WARNING "You're using the deprecated elanfreq command line option. Use elanfreq.max_freq instead, please!\n"); | |
258 | return 1; | |
259 | } | |
260 | __setup("elanfreq=", elanfreq_setup); | |
261 | #endif | |
262 | ||
263 | ||
264 | static struct freq_attr* elanfreq_attr[] = { | |
265 | &cpufreq_freq_attr_scaling_available_freqs, | |
266 | NULL, | |
267 | }; | |
268 | ||
269 | ||
270 | static struct cpufreq_driver elanfreq_driver = { | |
271 | .get = elanfreq_get_cpu_frequency, | |
272 | .verify = elanfreq_verify, | |
273 | .target = elanfreq_target, | |
274 | .init = elanfreq_cpu_init, | |
275 | .exit = elanfreq_cpu_exit, | |
276 | .name = "elanfreq", | |
277 | .owner = THIS_MODULE, | |
278 | .attr = elanfreq_attr, | |
279 | }; | |
280 | ||
281 | ||
282 | static int __init elanfreq_init(void) | |
283 | { | |
284 | struct cpuinfo_x86 *c = cpu_data; | |
285 | ||
286 | /* Test if we have the right hardware */ | |
287 | if ((c->x86_vendor != X86_VENDOR_AMD) || | |
288 | (c->x86 != 4) || (c->x86_model!=10)) | |
289 | { | |
290 | printk(KERN_INFO "elanfreq: error: no Elan processor found!\n"); | |
291 | return -ENODEV; | |
292 | } | |
293 | ||
294 | return cpufreq_register_driver(&elanfreq_driver); | |
295 | } | |
296 | ||
297 | ||
298 | static void __exit elanfreq_exit(void) | |
299 | { | |
300 | cpufreq_unregister_driver(&elanfreq_driver); | |
301 | } | |
302 | ||
303 | ||
304 | module_param (max_freq, int, 0444); | |
305 | ||
306 | MODULE_LICENSE("GPL"); | |
307 | MODULE_AUTHOR("Robert Schwebel <r.schwebel@pengutronix.de>, Sven Geggus <sven@geggus.net>"); | |
308 | MODULE_DESCRIPTION("cpufreq driver for AMD's Elan CPUs"); | |
309 | ||
310 | module_init(elanfreq_init); | |
311 | module_exit(elanfreq_exit); | |
312 |