8ee85683fe321a5d898f0970d148f9885b7c2ded
[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
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}
40 };
41
42
43 /**
44  * powernow_k6_get_cpu_multiplier - returns the current FSB multiplier
45  *
46  * Returns the current setting of the frequency multiplier. Core clock
47  * speed is frequency of the Front-Side Bus multiplied with this value.
48  */
49 static int powernow_k6_get_cpu_multiplier(void)
50 {
51         unsigned long invalue = 0;
52         u32 msrval;
53
54         local_irq_disable();
55
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 */
61
62         local_irq_enable();
63
64         return clock_ratio[(invalue >> 5)&7].index;
65 }
66
67 static void powernow_k6_set_cpu_multiplier(unsigned int best_i)
68 {
69         unsigned long outvalue, invalue;
70         unsigned long msrval;
71         unsigned long cr0;
72
73         /* we now need to transform best_i to the BVC format, see AMD#23446 */
74
75         /*
76          * The processor doesn't respond to inquiry cycles while changing the
77          * frequency, so we must disable cache.
78          */
79         local_irq_disable();
80         cr0 = read_cr0();
81         write_cr0(cr0 | X86_CR0_CD);
82         wbinvd();
83
84         outvalue = (1<<12) | (1<<10) | (1<<9) | (best_i<<5);
85
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 */
94
95         write_cr0(cr0);
96         local_irq_enable();
97 }
98
99 /**
100  * powernow_k6_set_state - set the PowerNow! multiplier
101  * @best_i: clock_ratio[best_i] is the target multiplier
102  *
103  *   Tries to change the PowerNow! multiplier
104  */
105 static void powernow_k6_set_state(unsigned int best_i)
106 {
107         struct cpufreq_freqs freqs;
108
109         if (clock_ratio[best_i].index > max_multiplier) {
110                 printk(KERN_ERR PFX "invalid target frequency\n");
111                 return;
112         }
113
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 */
117
118         cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
119
120         powernow_k6_set_cpu_multiplier(best_i);
121
122         cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
123
124         return;
125 }
126
127
128 /**
129  * powernow_k6_verify - verifies a new CPUfreq policy
130  * @policy: new policy
131  *
132  * Policy must be within lowest and highest possible CPU Frequency,
133  * and at least one possible state must be within min and max.
134  */
135 static int powernow_k6_verify(struct cpufreq_policy *policy)
136 {
137         return cpufreq_frequency_table_verify(policy, &clock_ratio[0]);
138 }
139
140
141 /**
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)
147  *
148  * sets a new CPUFreq policy
149  */
150 static int powernow_k6_target(struct cpufreq_policy *policy,
151                                unsigned int target_freq,
152                                unsigned int relation)
153 {
154         unsigned int newstate = 0;
155
156         if (cpufreq_frequency_table_target(policy, &clock_ratio[0],
157                                 target_freq, relation, &newstate))
158                 return -EINVAL;
159
160         powernow_k6_set_state(newstate);
161
162         return 0;
163 }
164
165
166 static int powernow_k6_cpu_init(struct cpufreq_policy *policy)
167 {
168         unsigned int i, f;
169         int result;
170
171         if (policy->cpu != 0)
172                 return -ENODEV;
173
174         /* get frequencies */
175         max_multiplier = powernow_k6_get_cpu_multiplier();
176         busfreq = cpu_khz / max_multiplier;
177
178         /* table init */
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;
183                 else
184                         clock_ratio[i].frequency = busfreq * f;
185         }
186
187         /* cpuinfo and default policy values */
188         policy->cpuinfo.transition_latency = 500000;
189         policy->cur = busfreq * max_multiplier;
190
191         result = cpufreq_frequency_table_cpuinfo(policy, clock_ratio);
192         if (result)
193                 return result;
194
195         cpufreq_frequency_table_get_attr(clock_ratio, policy->cpu);
196
197         return 0;
198 }
199
200
201 static int powernow_k6_cpu_exit(struct cpufreq_policy *policy)
202 {
203         unsigned int i;
204         for (i = 0; i < 8; i++) {
205                 if (i == max_multiplier)
206                         powernow_k6_set_state(i);
207         }
208         cpufreq_frequency_table_put_attr(policy->cpu);
209         return 0;
210 }
211
212 static unsigned int powernow_k6_get(unsigned int cpu)
213 {
214         unsigned int ret;
215         ret = (busfreq * powernow_k6_get_cpu_multiplier());
216         return ret;
217 }
218
219 static struct freq_attr *powernow_k6_attr[] = {
220         &cpufreq_freq_attr_scaling_available_freqs,
221         NULL,
222 };
223
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,
233 };
234
235
236 /**
237  * powernow_k6_init - initializes the k6 PowerNow! CPUFreq driver
238  *
239  *   Initializes the K6 PowerNow! support. Returns -ENODEV on unsupported
240  * devices, -EINVAL or -ENOMEM on problems during initiatization, and zero
241  * on success.
242  */
243 static int __init powernow_k6_init(void)
244 {
245         struct cpuinfo_x86 *c = &cpu_data(0);
246
247         if ((c->x86_vendor != X86_VENDOR_AMD) || (c->x86 != 5) ||
248                 ((c->x86_model != 12) && (c->x86_model != 13)))
249                 return -ENODEV;
250
251         if (!request_region(POWERNOW_IOPORT, 16, "PowerNow!")) {
252                 printk(KERN_INFO PFX "PowerNow IOPORT region already used.\n");
253                 return -EIO;
254         }
255
256         if (cpufreq_register_driver(&powernow_k6_driver)) {
257                 release_region(POWERNOW_IOPORT, 16);
258                 return -EINVAL;
259         }
260
261         return 0;
262 }
263
264
265 /**
266  * powernow_k6_exit - unregisters AMD K6-2+/3+ PowerNow! support
267  *
268  *   Unregisters AMD K6-2+ / K6-3+ PowerNow! support.
269  */
270 static void __exit powernow_k6_exit(void)
271 {
272         cpufreq_unregister_driver(&powernow_k6_driver);
273         release_region(POWERNOW_IOPORT, 16);
274 }
275
276
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");
281
282 module_init(powernow_k6_init);
283 module_exit(powernow_k6_exit);