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;
29 /* Clock ratio multiplied by 10 - see table 27 in AMD#23446 */
30 static struct cpufreq_frequency_table clock_ratio[] = {
31 {45, /* 000 -> 4.5x */ 0},
32 {50, /* 001 -> 5.0x */ 0},
33 {40, /* 010 -> 4.0x */ 0},
34 {55, /* 011 -> 5.5x */ 0},
35 {20, /* 100 -> 2.0x */ 0},
36 {30, /* 101 -> 3.0x */ 0},
37 {60, /* 110 -> 6.0x */ 0},
38 {35, /* 111 -> 3.5x */ 0},
39 {0, CPUFREQ_TABLE_END}
44 * powernow_k6_get_cpu_multiplier - returns the current FSB multiplier
46 * Returns the current setting of the frequency multiplier. Core clock
47 * speed is frequency of the Front-Side Bus multiplied with this value.
49 static int powernow_k6_get_cpu_multiplier(void)
51 unsigned long invalue = 0;
56 msrval = POWERNOW_IOPORT + 0x1;
57 wrmsr(MSR_K6_EPMR, msrval, 0); /* enable the PowerNow port */
58 invalue = inl(POWERNOW_IOPORT + 0x8);
59 msrval = POWERNOW_IOPORT + 0x0;
60 wrmsr(MSR_K6_EPMR, msrval, 0); /* disable it again */
64 return clock_ratio[(invalue >> 5)&7].index;
67 static void powernow_k6_set_cpu_multiplier(unsigned int best_i)
69 unsigned long outvalue, invalue;
73 /* we now need to transform best_i to the BVC format, see AMD#23446 */
76 * The processor doesn't respond to inquiry cycles while changing the
77 * frequency, so we must disable cache.
81 write_cr0(cr0 | X86_CR0_CD);
84 outvalue = (1<<12) | (1<<10) | (1<<9) | (best_i<<5);
86 msrval = POWERNOW_IOPORT + 0x1;
87 wrmsr(MSR_K6_EPMR, msrval, 0); /* enable the PowerNow port */
88 invalue = inl(POWERNOW_IOPORT + 0x8);
89 invalue = invalue & 0x1f;
90 outvalue = outvalue | invalue;
91 outl(outvalue, (POWERNOW_IOPORT + 0x8));
92 msrval = POWERNOW_IOPORT + 0x0;
93 wrmsr(MSR_K6_EPMR, msrval, 0); /* disable it again */
100 * powernow_k6_set_state - set the PowerNow! multiplier
101 * @best_i: clock_ratio[best_i] is the target multiplier
103 * Tries to change the PowerNow! multiplier
105 static void powernow_k6_set_state(unsigned int best_i)
107 struct cpufreq_freqs freqs;
109 if (clock_ratio[best_i].index > max_multiplier) {
110 printk(KERN_ERR PFX "invalid target frequency\n");
114 freqs.old = busfreq * powernow_k6_get_cpu_multiplier();
115 freqs.new = busfreq * clock_ratio[best_i].index;
116 freqs.cpu = 0; /* powernow-k6.c is UP only driver */
118 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
120 powernow_k6_set_cpu_multiplier(best_i);
122 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
129 * powernow_k6_verify - verifies a new CPUfreq policy
130 * @policy: new policy
132 * Policy must be within lowest and highest possible CPU Frequency,
133 * and at least one possible state must be within min and max.
135 static int powernow_k6_verify(struct cpufreq_policy *policy)
137 return cpufreq_frequency_table_verify(policy, &clock_ratio[0]);
142 * powernow_k6_setpolicy - sets a new CPUFreq policy
143 * @policy: new policy
144 * @target_freq: the target frequency
145 * @relation: how that frequency relates to achieved frequency
146 * (CPUFREQ_RELATION_L or CPUFREQ_RELATION_H)
148 * sets a new CPUFreq policy
150 static int powernow_k6_target(struct cpufreq_policy *policy,
151 unsigned int target_freq,
152 unsigned int relation)
154 unsigned int newstate = 0;
156 if (cpufreq_frequency_table_target(policy, &clock_ratio[0],
157 target_freq, relation, &newstate))
160 powernow_k6_set_state(newstate);
166 static int powernow_k6_cpu_init(struct cpufreq_policy *policy)
171 if (policy->cpu != 0)
174 /* get frequencies */
175 max_multiplier = powernow_k6_get_cpu_multiplier();
176 busfreq = cpu_khz / max_multiplier;
179 for (i = 0; (clock_ratio[i].frequency != CPUFREQ_TABLE_END); i++) {
180 f = clock_ratio[i].index;
181 if (f > max_multiplier)
182 clock_ratio[i].frequency = CPUFREQ_ENTRY_INVALID;
184 clock_ratio[i].frequency = busfreq * f;
187 /* cpuinfo and default policy values */
188 policy->cpuinfo.transition_latency = 500000;
189 policy->cur = busfreq * max_multiplier;
191 result = cpufreq_frequency_table_cpuinfo(policy, clock_ratio);
195 cpufreq_frequency_table_get_attr(clock_ratio, policy->cpu);
201 static int powernow_k6_cpu_exit(struct cpufreq_policy *policy)
204 for (i = 0; i < 8; i++) {
205 if (i == max_multiplier)
206 powernow_k6_set_state(i);
208 cpufreq_frequency_table_put_attr(policy->cpu);
212 static unsigned int powernow_k6_get(unsigned int cpu)
215 ret = (busfreq * powernow_k6_get_cpu_multiplier());
219 static struct freq_attr *powernow_k6_attr[] = {
220 &cpufreq_freq_attr_scaling_available_freqs,
224 static struct cpufreq_driver powernow_k6_driver = {
225 .verify = powernow_k6_verify,
226 .target = powernow_k6_target,
227 .init = powernow_k6_cpu_init,
228 .exit = powernow_k6_cpu_exit,
229 .get = powernow_k6_get,
230 .name = "powernow-k6",
231 .owner = THIS_MODULE,
232 .attr = powernow_k6_attr,
237 * powernow_k6_init - initializes the k6 PowerNow! CPUFreq driver
239 * Initializes the K6 PowerNow! support. Returns -ENODEV on unsupported
240 * devices, -EINVAL or -ENOMEM on problems during initiatization, and zero
243 static int __init powernow_k6_init(void)
245 struct cpuinfo_x86 *c = &cpu_data(0);
247 if ((c->x86_vendor != X86_VENDOR_AMD) || (c->x86 != 5) ||
248 ((c->x86_model != 12) && (c->x86_model != 13)))
251 if (!request_region(POWERNOW_IOPORT, 16, "PowerNow!")) {
252 printk(KERN_INFO PFX "PowerNow IOPORT region already used.\n");
256 if (cpufreq_register_driver(&powernow_k6_driver)) {
257 release_region(POWERNOW_IOPORT, 16);
266 * powernow_k6_exit - unregisters AMD K6-2+/3+ PowerNow! support
268 * Unregisters AMD K6-2+ / K6-3+ PowerNow! support.
270 static void __exit powernow_k6_exit(void)
272 cpufreq_unregister_driver(&powernow_k6_driver);
273 release_region(POWERNOW_IOPORT, 16);
277 MODULE_AUTHOR("Arjan van de Ven, Dave Jones <davej@redhat.com>, "
278 "Dominik Brodowski <linux@brodo.de>");
279 MODULE_DESCRIPTION("PowerNow! driver for AMD K6-2+ / K6-3+ processors.");
280 MODULE_LICENSE("GPL");
282 module_init(powernow_k6_init);
283 module_exit(powernow_k6_exit);