powernow-k6: correctly initialize default parameters
[pandora-kernel.git] / drivers / cpufreq / powernow-k6.c
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.
5  *
6  *  Licensed under the terms of the GNU GPL License version 2.
7  *
8  *  BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
9  */
10
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>
17 #include <linux/io.h>
18
19 #include <asm/msr.h>
20
21 #define POWERNOW_IOPORT 0xfff0          /* it doesn't matter where, as long
22                                            as it is unused */
23
24 #define PFX "powernow-k6: "
25 static unsigned int                     busfreq;   /* FSB, in 10 kHz */
26 static unsigned int                     max_multiplier;
27
28 static unsigned int                     param_busfreq = 0;
29 static unsigned int                     param_max_multiplier = 0;
30
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)");
33
34 module_param_named(bus_frequency, param_busfreq, uint, S_IRUGO);
35 MODULE_PARM_DESC(bus_frequency, "Bus frequency in kHz");
36
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}
48 };
49
50 static const struct {
51         unsigned freq;
52         unsigned mult;
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
68 };
69
70 #define FREQ_RANGE              3000
71
72 /**
73  * powernow_k6_get_cpu_multiplier - returns the current FSB multiplier
74  *
75  * Returns the current setting of the frequency multiplier. Core clock
76  * speed is frequency of the Front-Side Bus multiplied with this value.
77  */
78 static int powernow_k6_get_cpu_multiplier(void)
79 {
80         unsigned long invalue = 0;
81         u32 msrval;
82
83         local_irq_disable();
84
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 */
90
91         local_irq_enable();
92
93         return clock_ratio[(invalue >> 5)&7].index;
94 }
95
96 static void powernow_k6_set_cpu_multiplier(unsigned int best_i)
97 {
98         unsigned long outvalue, invalue;
99         unsigned long msrval;
100         unsigned long cr0;
101
102         /* we now need to transform best_i to the BVC format, see AMD#23446 */
103
104         /*
105          * The processor doesn't respond to inquiry cycles while changing the
106          * frequency, so we must disable cache.
107          */
108         local_irq_disable();
109         cr0 = read_cr0();
110         write_cr0(cr0 | X86_CR0_CD);
111         wbinvd();
112
113         outvalue = (1<<12) | (1<<10) | (1<<9) | (best_i<<5);
114
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 */
123
124         write_cr0(cr0);
125         local_irq_enable();
126 }
127
128 /**
129  * powernow_k6_set_state - set the PowerNow! multiplier
130  * @best_i: clock_ratio[best_i] is the target multiplier
131  *
132  *   Tries to change the PowerNow! multiplier
133  */
134 static void powernow_k6_set_state(unsigned int best_i)
135 {
136         struct cpufreq_freqs freqs;
137
138         if (clock_ratio[best_i].index > max_multiplier) {
139                 printk(KERN_ERR PFX "invalid target frequency\n");
140                 return;
141         }
142
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 */
146
147         cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
148
149         powernow_k6_set_cpu_multiplier(best_i);
150
151         cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
152
153         return;
154 }
155
156
157 /**
158  * powernow_k6_verify - verifies a new CPUfreq policy
159  * @policy: new policy
160  *
161  * Policy must be within lowest and highest possible CPU Frequency,
162  * and at least one possible state must be within min and max.
163  */
164 static int powernow_k6_verify(struct cpufreq_policy *policy)
165 {
166         return cpufreq_frequency_table_verify(policy, &clock_ratio[0]);
167 }
168
169
170 /**
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)
176  *
177  * sets a new CPUFreq policy
178  */
179 static int powernow_k6_target(struct cpufreq_policy *policy,
180                                unsigned int target_freq,
181                                unsigned int relation)
182 {
183         unsigned int newstate = 0;
184
185         if (cpufreq_frequency_table_target(policy, &clock_ratio[0],
186                                 target_freq, relation, &newstate))
187                 return -EINVAL;
188
189         powernow_k6_set_state(newstate);
190
191         return 0;
192 }
193
194 static int powernow_k6_cpu_init(struct cpufreq_policy *policy)
195 {
196         unsigned int i, f;
197         int result;
198         unsigned khz;
199
200         if (policy->cpu != 0)
201                 return -ENODEV;
202
203         max_multiplier = 0;
204         khz = cpu_khz;
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;
210                         break;
211                 }
212         }
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;
218                         }
219                 }
220                 printk(KERN_ERR "powernow-k6: invalid max_multiplier parameter, valid parameters 20, 30, 35, 40, 45, 50, 55, 60\n");
221                 return -EINVAL;
222         }
223
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");
227                 return -EOPNOTSUPP;
228         }
229
230 have_max_multiplier:
231         param_max_multiplier = max_multiplier;
232
233         if (param_busfreq) {
234                 if (param_busfreq >= 50000 && param_busfreq <= 150000) {
235                         busfreq = param_busfreq / 10;
236                         goto have_busfreq;
237                 }
238                 printk(KERN_ERR "powernow-k6: invalid bus_frequency parameter, allowed range 50000 - 150000 kHz\n");
239                 return -EINVAL;
240         }
241
242         busfreq = khz / max_multiplier;
243 have_busfreq:
244         param_busfreq = busfreq * 10;
245
246         /* table init */
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;
251                 else
252                         clock_ratio[i].frequency = busfreq * f;
253         }
254
255         /* cpuinfo and default policy values */
256         policy->cpuinfo.transition_latency = 500000;
257         policy->cur = busfreq * max_multiplier;
258
259         result = cpufreq_frequency_table_cpuinfo(policy, clock_ratio);
260         if (result)
261                 return result;
262
263         cpufreq_frequency_table_get_attr(clock_ratio, policy->cpu);
264
265         return 0;
266 }
267
268
269 static int powernow_k6_cpu_exit(struct cpufreq_policy *policy)
270 {
271         unsigned int i;
272         for (i = 0; i < 8; i++) {
273                 if (i == max_multiplier)
274                         powernow_k6_set_state(i);
275         }
276         cpufreq_frequency_table_put_attr(policy->cpu);
277         return 0;
278 }
279
280 static unsigned int powernow_k6_get(unsigned int cpu)
281 {
282         unsigned int ret;
283         ret = (busfreq * powernow_k6_get_cpu_multiplier());
284         return ret;
285 }
286
287 static struct freq_attr *powernow_k6_attr[] = {
288         &cpufreq_freq_attr_scaling_available_freqs,
289         NULL,
290 };
291
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,
301 };
302
303
304 /**
305  * powernow_k6_init - initializes the k6 PowerNow! CPUFreq driver
306  *
307  *   Initializes the K6 PowerNow! support. Returns -ENODEV on unsupported
308  * devices, -EINVAL or -ENOMEM on problems during initiatization, and zero
309  * on success.
310  */
311 static int __init powernow_k6_init(void)
312 {
313         struct cpuinfo_x86 *c = &cpu_data(0);
314
315         if ((c->x86_vendor != X86_VENDOR_AMD) || (c->x86 != 5) ||
316                 ((c->x86_model != 12) && (c->x86_model != 13)))
317                 return -ENODEV;
318
319         if (!request_region(POWERNOW_IOPORT, 16, "PowerNow!")) {
320                 printk(KERN_INFO PFX "PowerNow IOPORT region already used.\n");
321                 return -EIO;
322         }
323
324         if (cpufreq_register_driver(&powernow_k6_driver)) {
325                 release_region(POWERNOW_IOPORT, 16);
326                 return -EINVAL;
327         }
328
329         return 0;
330 }
331
332
333 /**
334  * powernow_k6_exit - unregisters AMD K6-2+/3+ PowerNow! support
335  *
336  *   Unregisters AMD K6-2+ / K6-3+ PowerNow! support.
337  */
338 static void __exit powernow_k6_exit(void)
339 {
340         cpufreq_unregister_driver(&powernow_k6_driver);
341         release_region(POWERNOW_IOPORT, 16);
342 }
343
344
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");
349
350 module_init(powernow_k6_init);
351 module_exit(powernow_k6_exit);