powernow-k6: correctly initialize default parameters
[linux/fpc-iii.git] / arch / x86 / kernel / cpu / cpufreq / powernow-k6.c
blob2d9161eda69cc7590ec6fad75685b1079a1fb5a5
1 /*
2 * This file was based upon code in Powertweak Linux (http://powertweak.sf.net)
3 * (C) 2000-2003 Dave Jones, Arjan van de Ven, Janne Pänkälä,
4 * Dominik Brodowski.
6 * Licensed under the terms of the GNU GPL License version 2.
8 * BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
9 */
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/cpufreq.h>
15 #include <linux/ioport.h>
16 #include <linux/slab.h>
17 #include <linux/timex.h>
18 #include <linux/io.h>
20 #include <asm/msr.h>
22 #define POWERNOW_IOPORT 0xfff0 /* it doesn't matter where, as long
23 as it is unused */
25 #define PFX "powernow-k6: "
26 static unsigned int busfreq; /* FSB, in 10 kHz */
27 static unsigned int max_multiplier;
29 static unsigned int param_busfreq = 0;
30 static unsigned int param_max_multiplier = 0;
32 module_param_named(max_multiplier, param_max_multiplier, uint, S_IRUGO);
33 MODULE_PARM_DESC(max_multiplier, "Maximum multiplier (allowed values: 20 30 35 40 45 50 55 60)");
35 module_param_named(bus_frequency, param_busfreq, uint, S_IRUGO);
36 MODULE_PARM_DESC(bus_frequency, "Bus frequency in kHz");
38 /* Clock ratio multiplied by 10 - see table 27 in AMD#23446 */
39 static struct cpufreq_frequency_table clock_ratio[] = {
40 {45, /* 000 -> 4.5x */ 0},
41 {50, /* 001 -> 5.0x */ 0},
42 {40, /* 010 -> 4.0x */ 0},
43 {55, /* 011 -> 5.5x */ 0},
44 {20, /* 100 -> 2.0x */ 0},
45 {30, /* 101 -> 3.0x */ 0},
46 {60, /* 110 -> 6.0x */ 0},
47 {35, /* 111 -> 3.5x */ 0},
48 {0, CPUFREQ_TABLE_END}
51 static const struct {
52 unsigned freq;
53 unsigned mult;
54 } usual_frequency_table[] = {
55 { 400000, 40 }, // 100 * 4
56 { 450000, 45 }, // 100 * 4.5
57 { 475000, 50 }, // 95 * 5
58 { 500000, 50 }, // 100 * 5
59 { 506250, 45 }, // 112.5 * 4.5
60 { 533500, 55 }, // 97 * 5.5
61 { 550000, 55 }, // 100 * 5.5
62 { 562500, 50 }, // 112.5 * 5
63 { 570000, 60 }, // 95 * 6
64 { 600000, 60 }, // 100 * 6
65 { 618750, 55 }, // 112.5 * 5.5
66 { 660000, 55 }, // 120 * 5.5
67 { 675000, 60 }, // 112.5 * 6
68 { 720000, 60 }, // 120 * 6
71 #define FREQ_RANGE 3000
73 /**
74 * powernow_k6_get_cpu_multiplier - returns the current FSB multiplier
76 * Returns the current setting of the frequency multiplier. Core clock
77 * speed is frequency of the Front-Side Bus multiplied with this value.
79 static int powernow_k6_get_cpu_multiplier(void)
81 unsigned long invalue = 0;
82 u32 msrval;
84 local_irq_disable();
86 msrval = POWERNOW_IOPORT + 0x1;
87 wrmsr(MSR_K6_EPMR, msrval, 0); /* enable the PowerNow port */
88 invalue = inl(POWERNOW_IOPORT + 0x8);
89 msrval = POWERNOW_IOPORT + 0x0;
90 wrmsr(MSR_K6_EPMR, msrval, 0); /* disable it again */
92 local_irq_enable();
94 return clock_ratio[(invalue >> 5)&7].index;
97 static void powernow_k6_set_cpu_multiplier(unsigned int best_i)
99 unsigned long outvalue, invalue;
100 unsigned long msrval;
101 unsigned long cr0;
103 /* we now need to transform best_i to the BVC format, see AMD#23446 */
106 * The processor doesn't respond to inquiry cycles while changing the
107 * frequency, so we must disable cache.
109 local_irq_disable();
110 cr0 = read_cr0();
111 write_cr0(cr0 | X86_CR0_CD);
112 wbinvd();
114 outvalue = (1<<12) | (1<<10) | (1<<9) | (best_i<<5);
116 msrval = POWERNOW_IOPORT + 0x1;
117 wrmsr(MSR_K6_EPMR, msrval, 0); /* enable the PowerNow port */
118 invalue = inl(POWERNOW_IOPORT + 0x8);
119 invalue = invalue & 0x1f;
120 outvalue = outvalue | invalue;
121 outl(outvalue, (POWERNOW_IOPORT + 0x8));
122 msrval = POWERNOW_IOPORT + 0x0;
123 wrmsr(MSR_K6_EPMR, msrval, 0); /* disable it again */
125 write_cr0(cr0);
126 local_irq_enable();
130 * powernow_k6_set_state - set the PowerNow! multiplier
131 * @best_i: clock_ratio[best_i] is the target multiplier
133 * Tries to change the PowerNow! multiplier
135 static void powernow_k6_set_state(unsigned int best_i)
137 struct cpufreq_freqs freqs;
139 if (clock_ratio[best_i].index > max_multiplier) {
140 printk(KERN_ERR PFX "invalid target frequency\n");
141 return;
144 freqs.old = busfreq * powernow_k6_get_cpu_multiplier();
145 freqs.new = busfreq * clock_ratio[best_i].index;
146 freqs.cpu = 0; /* powernow-k6.c is UP only driver */
148 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
150 powernow_k6_set_cpu_multiplier(best_i);
152 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
154 return;
159 * powernow_k6_verify - verifies a new CPUfreq policy
160 * @policy: new policy
162 * Policy must be within lowest and highest possible CPU Frequency,
163 * and at least one possible state must be within min and max.
165 static int powernow_k6_verify(struct cpufreq_policy *policy)
167 return cpufreq_frequency_table_verify(policy, &clock_ratio[0]);
172 * powernow_k6_setpolicy - sets a new CPUFreq policy
173 * @policy: new policy
174 * @target_freq: the target frequency
175 * @relation: how that frequency relates to achieved frequency
176 * (CPUFREQ_RELATION_L or CPUFREQ_RELATION_H)
178 * sets a new CPUFreq policy
180 static int powernow_k6_target(struct cpufreq_policy *policy,
181 unsigned int target_freq,
182 unsigned int relation)
184 unsigned int newstate = 0;
186 if (cpufreq_frequency_table_target(policy, &clock_ratio[0],
187 target_freq, relation, &newstate))
188 return -EINVAL;
190 powernow_k6_set_state(newstate);
192 return 0;
195 static int powernow_k6_cpu_init(struct cpufreq_policy *policy)
197 unsigned int i, f;
198 int result;
199 unsigned khz;
201 if (policy->cpu != 0)
202 return -ENODEV;
204 max_multiplier = 0;
205 khz = cpu_khz;
206 for (i = 0; i < ARRAY_SIZE(usual_frequency_table); i++) {
207 if (khz >= usual_frequency_table[i].freq - FREQ_RANGE &&
208 khz <= usual_frequency_table[i].freq + FREQ_RANGE) {
209 khz = usual_frequency_table[i].freq;
210 max_multiplier = usual_frequency_table[i].mult;
211 break;
214 if (param_max_multiplier) {
215 for (i = 0; (clock_ratio[i].frequency != CPUFREQ_TABLE_END); i++) {
216 if (clock_ratio[i].index == param_max_multiplier) {
217 max_multiplier = param_max_multiplier;
218 goto have_max_multiplier;
221 printk(KERN_ERR "powernow-k6: invalid max_multiplier parameter, valid parameters 20, 30, 35, 40, 45, 50, 55, 60\n");
222 return -EINVAL;
225 if (!max_multiplier) {
226 printk(KERN_WARNING "powernow-k6: unknown frequency %u, cannot determine current multiplier\n", khz);
227 printk(KERN_WARNING "powernow-k6: use module parameters max_multiplier and bus_frequency\n");
228 return -EOPNOTSUPP;
231 have_max_multiplier:
232 param_max_multiplier = max_multiplier;
234 if (param_busfreq) {
235 if (param_busfreq >= 50000 && param_busfreq <= 150000) {
236 busfreq = param_busfreq / 10;
237 goto have_busfreq;
239 printk(KERN_ERR "powernow-k6: invalid bus_frequency parameter, allowed range 50000 - 150000 kHz\n");
240 return -EINVAL;
243 busfreq = khz / max_multiplier;
244 have_busfreq:
245 param_busfreq = busfreq * 10;
247 /* table init */
248 for (i = 0; (clock_ratio[i].frequency != CPUFREQ_TABLE_END); i++) {
249 f = clock_ratio[i].index;
250 if (f > max_multiplier)
251 clock_ratio[i].frequency = CPUFREQ_ENTRY_INVALID;
252 else
253 clock_ratio[i].frequency = busfreq * f;
256 /* cpuinfo and default policy values */
257 policy->cpuinfo.transition_latency = 500000;
258 policy->cur = busfreq * max_multiplier;
260 result = cpufreq_frequency_table_cpuinfo(policy, clock_ratio);
261 if (result)
262 return result;
264 cpufreq_frequency_table_get_attr(clock_ratio, policy->cpu);
266 return 0;
270 static int powernow_k6_cpu_exit(struct cpufreq_policy *policy)
272 unsigned int i;
273 for (i = 0; i < 8; i++) {
274 if (i == max_multiplier)
275 powernow_k6_set_state(i);
277 cpufreq_frequency_table_put_attr(policy->cpu);
278 return 0;
281 static unsigned int powernow_k6_get(unsigned int cpu)
283 unsigned int ret;
284 ret = (busfreq * powernow_k6_get_cpu_multiplier());
285 return ret;
288 static struct freq_attr *powernow_k6_attr[] = {
289 &cpufreq_freq_attr_scaling_available_freqs,
290 NULL,
293 static struct cpufreq_driver powernow_k6_driver = {
294 .verify = powernow_k6_verify,
295 .target = powernow_k6_target,
296 .init = powernow_k6_cpu_init,
297 .exit = powernow_k6_cpu_exit,
298 .get = powernow_k6_get,
299 .name = "powernow-k6",
300 .owner = THIS_MODULE,
301 .attr = powernow_k6_attr,
306 * powernow_k6_init - initializes the k6 PowerNow! CPUFreq driver
308 * Initializes the K6 PowerNow! support. Returns -ENODEV on unsupported
309 * devices, -EINVAL or -ENOMEM on problems during initiatization, and zero
310 * on success.
312 static int __init powernow_k6_init(void)
314 struct cpuinfo_x86 *c = &cpu_data(0);
316 if ((c->x86_vendor != X86_VENDOR_AMD) || (c->x86 != 5) ||
317 ((c->x86_model != 12) && (c->x86_model != 13)))
318 return -ENODEV;
320 if (!request_region(POWERNOW_IOPORT, 16, "PowerNow!")) {
321 printk(KERN_INFO PFX "PowerNow IOPORT region already used.\n");
322 return -EIO;
325 if (cpufreq_register_driver(&powernow_k6_driver)) {
326 release_region(POWERNOW_IOPORT, 16);
327 return -EINVAL;
330 return 0;
335 * powernow_k6_exit - unregisters AMD K6-2+/3+ PowerNow! support
337 * Unregisters AMD K6-2+ / K6-3+ PowerNow! support.
339 static void __exit powernow_k6_exit(void)
341 cpufreq_unregister_driver(&powernow_k6_driver);
342 release_region(POWERNOW_IOPORT, 16);
346 MODULE_AUTHOR("Arjan van de Ven, Dave Jones <davej@redhat.com>, "
347 "Dominik Brodowski <linux@brodo.de>");
348 MODULE_DESCRIPTION("PowerNow! driver for AMD K6-2+ / K6-3+ processors.");
349 MODULE_LICENSE("GPL");
351 module_init(powernow_k6_init);
352 module_exit(powernow_k6_exit);