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ä,
6 * Licensed under the terms of the GNU GPL License version 2.
8 * BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
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/timex.h>
21 #define POWERNOW_IOPORT 0xfff0 /* it doesn't matter where, as long
24 #define PFX "powernow-k6: "
25 static unsigned int busfreq; /* FSB, in 10 kHz */
26 static unsigned int max_multiplier;
28 static unsigned int param_busfreq = 0;
29 static unsigned int param_max_multiplier = 0;
31 module_param_named(max_multiplier, param_max_multiplier, uint, S_IRUGO);
32 MODULE_PARM_DESC(max_multiplier, "Maximum multiplier (allowed values: 20 30 35 40 45 50 55 60)");
34 module_param_named(bus_frequency, param_busfreq, uint, S_IRUGO);
35 MODULE_PARM_DESC(bus_frequency, "Bus frequency in kHz");
37 /* Clock ratio multiplied by 10 - see table 27 in AMD#23446 */
38 static struct cpufreq_frequency_table clock_ratio[] = {
39 {45, /* 000 -> 4.5x */ 0},
40 {50, /* 001 -> 5.0x */ 0},
41 {40, /* 010 -> 4.0x */ 0},
42 {55, /* 011 -> 5.5x */ 0},
43 {20, /* 100 -> 2.0x */ 0},
44 {30, /* 101 -> 3.0x */ 0},
45 {60, /* 110 -> 6.0x */ 0},
46 {35, /* 111 -> 3.5x */ 0},
47 {0, CPUFREQ_TABLE_END}
53 } usual_frequency_table[] = {
54 { 400000, 40 }, // 100 * 4
55 { 450000, 45 }, // 100 * 4.5
56 { 475000, 50 }, // 95 * 5
57 { 500000, 50 }, // 100 * 5
58 { 506250, 45 }, // 112.5 * 4.5
59 { 533500, 55 }, // 97 * 5.5
60 { 550000, 55 }, // 100 * 5.5
61 { 562500, 50 }, // 112.5 * 5
62 { 570000, 60 }, // 95 * 6
63 { 600000, 60 }, // 100 * 6
64 { 618750, 55 }, // 112.5 * 5.5
65 { 660000, 55 }, // 120 * 5.5
66 { 675000, 60 }, // 112.5 * 6
67 { 720000, 60 }, // 120 * 6
70 #define FREQ_RANGE 3000
73 * powernow_k6_get_cpu_multiplier - returns the current FSB multiplier
75 * Returns the current setting of the frequency multiplier. Core clock
76 * speed is frequency of the Front-Side Bus multiplied with this value.
78 static int powernow_k6_get_cpu_multiplier(void)
80 unsigned long invalue = 0;
85 msrval = POWERNOW_IOPORT + 0x1;
86 wrmsr(MSR_K6_EPMR, msrval, 0); /* enable the PowerNow port */
87 invalue = inl(POWERNOW_IOPORT + 0x8);
88 msrval = POWERNOW_IOPORT + 0x0;
89 wrmsr(MSR_K6_EPMR, msrval, 0); /* disable it again */
93 return clock_ratio[(invalue >> 5)&7].index;
96 static void powernow_k6_set_cpu_multiplier(unsigned int best_i)
98 unsigned long outvalue, invalue;
102 /* we now need to transform best_i to the BVC format, see AMD#23446 */
105 * The processor doesn't respond to inquiry cycles while changing the
106 * frequency, so we must disable cache.
110 write_cr0(cr0 | X86_CR0_CD);
113 outvalue = (1<<12) | (1<<10) | (1<<9) | (best_i<<5);
115 msrval = POWERNOW_IOPORT + 0x1;
116 wrmsr(MSR_K6_EPMR, msrval, 0); /* enable the PowerNow port */
117 invalue = inl(POWERNOW_IOPORT + 0x8);
118 invalue = invalue & 0x1f;
119 outvalue = outvalue | invalue;
120 outl(outvalue, (POWERNOW_IOPORT + 0x8));
121 msrval = POWERNOW_IOPORT + 0x0;
122 wrmsr(MSR_K6_EPMR, msrval, 0); /* disable it again */
129 * powernow_k6_set_state - set the PowerNow! multiplier
130 * @best_i: clock_ratio[best_i] is the target multiplier
132 * Tries to change the PowerNow! multiplier
134 static void powernow_k6_set_state(unsigned int best_i)
136 struct cpufreq_freqs freqs;
138 if (clock_ratio[best_i].index > max_multiplier) {
139 printk(KERN_ERR PFX "invalid target frequency\n");
143 freqs.old = busfreq * powernow_k6_get_cpu_multiplier();
144 freqs.new = busfreq * clock_ratio[best_i].index;
145 freqs.cpu = 0; /* powernow-k6.c is UP only driver */
147 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
149 powernow_k6_set_cpu_multiplier(best_i);
151 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
158 * powernow_k6_verify - verifies a new CPUfreq policy
159 * @policy: new policy
161 * Policy must be within lowest and highest possible CPU Frequency,
162 * and at least one possible state must be within min and max.
164 static int powernow_k6_verify(struct cpufreq_policy *policy)
166 return cpufreq_frequency_table_verify(policy, &clock_ratio[0]);
171 * powernow_k6_setpolicy - sets a new CPUFreq policy
172 * @policy: new policy
173 * @target_freq: the target frequency
174 * @relation: how that frequency relates to achieved frequency
175 * (CPUFREQ_RELATION_L or CPUFREQ_RELATION_H)
177 * sets a new CPUFreq policy
179 static int powernow_k6_target(struct cpufreq_policy *policy,
180 unsigned int target_freq,
181 unsigned int relation)
183 unsigned int newstate = 0;
185 if (cpufreq_frequency_table_target(policy, &clock_ratio[0],
186 target_freq, relation, &newstate))
189 powernow_k6_set_state(newstate);
194 static int powernow_k6_cpu_init(struct cpufreq_policy *policy)
200 if (policy->cpu != 0)
205 for (i = 0; i < ARRAY_SIZE(usual_frequency_table); i++) {
206 if (khz >= usual_frequency_table[i].freq - FREQ_RANGE &&
207 khz <= usual_frequency_table[i].freq + FREQ_RANGE) {
208 khz = usual_frequency_table[i].freq;
209 max_multiplier = usual_frequency_table[i].mult;
213 if (param_max_multiplier) {
214 for (i = 0; (clock_ratio[i].frequency != CPUFREQ_TABLE_END); i++) {
215 if (clock_ratio[i].index == param_max_multiplier) {
216 max_multiplier = param_max_multiplier;
217 goto have_max_multiplier;
220 printk(KERN_ERR "powernow-k6: invalid max_multiplier parameter, valid parameters 20, 30, 35, 40, 45, 50, 55, 60\n");
224 if (!max_multiplier) {
225 printk(KERN_WARNING "powernow-k6: unknown frequency %u, cannot determine current multiplier\n", khz);
226 printk(KERN_WARNING "powernow-k6: use module parameters max_multiplier and bus_frequency\n");
231 param_max_multiplier = max_multiplier;
234 if (param_busfreq >= 50000 && param_busfreq <= 150000) {
235 busfreq = param_busfreq / 10;
238 printk(KERN_ERR "powernow-k6: invalid bus_frequency parameter, allowed range 50000 - 150000 kHz\n");
242 busfreq = khz / max_multiplier;
244 param_busfreq = busfreq * 10;
247 for (i = 0; (clock_ratio[i].frequency != CPUFREQ_TABLE_END); i++) {
248 f = clock_ratio[i].index;
249 if (f > max_multiplier)
250 clock_ratio[i].frequency = CPUFREQ_ENTRY_INVALID;
252 clock_ratio[i].frequency = busfreq * f;
255 /* cpuinfo and default policy values */
256 policy->cpuinfo.transition_latency = 500000;
257 policy->cur = busfreq * max_multiplier;
259 result = cpufreq_frequency_table_cpuinfo(policy, clock_ratio);
263 cpufreq_frequency_table_get_attr(clock_ratio, policy->cpu);
269 static int powernow_k6_cpu_exit(struct cpufreq_policy *policy)
272 for (i = 0; i < 8; i++) {
273 if (i == max_multiplier)
274 powernow_k6_set_state(i);
276 cpufreq_frequency_table_put_attr(policy->cpu);
280 static unsigned int powernow_k6_get(unsigned int cpu)
283 ret = (busfreq * powernow_k6_get_cpu_multiplier());
287 static struct freq_attr *powernow_k6_attr[] = {
288 &cpufreq_freq_attr_scaling_available_freqs,
292 static struct cpufreq_driver powernow_k6_driver = {
293 .verify = powernow_k6_verify,
294 .target = powernow_k6_target,
295 .init = powernow_k6_cpu_init,
296 .exit = powernow_k6_cpu_exit,
297 .get = powernow_k6_get,
298 .name = "powernow-k6",
299 .owner = THIS_MODULE,
300 .attr = powernow_k6_attr,
305 * powernow_k6_init - initializes the k6 PowerNow! CPUFreq driver
307 * Initializes the K6 PowerNow! support. Returns -ENODEV on unsupported
308 * devices, -EINVAL or -ENOMEM on problems during initiatization, and zero
311 static int __init powernow_k6_init(void)
313 struct cpuinfo_x86 *c = &cpu_data(0);
315 if ((c->x86_vendor != X86_VENDOR_AMD) || (c->x86 != 5) ||
316 ((c->x86_model != 12) && (c->x86_model != 13)))
319 if (!request_region(POWERNOW_IOPORT, 16, "PowerNow!")) {
320 printk(KERN_INFO PFX "PowerNow IOPORT region already used.\n");
324 if (cpufreq_register_driver(&powernow_k6_driver)) {
325 release_region(POWERNOW_IOPORT, 16);
334 * powernow_k6_exit - unregisters AMD K6-2+/3+ PowerNow! support
336 * Unregisters AMD K6-2+ / K6-3+ PowerNow! support.
338 static void __exit powernow_k6_exit(void)
340 cpufreq_unregister_driver(&powernow_k6_driver);
341 release_region(POWERNOW_IOPORT, 16);
345 MODULE_AUTHOR("Arjan van de Ven, Dave Jones <davej@redhat.com>, "
346 "Dominik Brodowski <linux@brodo.de>");
347 MODULE_DESCRIPTION("PowerNow! driver for AMD K6-2+ / K6-3+ processors.");
348 MODULE_LICENSE("GPL");
350 module_init(powernow_k6_init);
351 module_exit(powernow_k6_exit);