1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/init.h>
22
23#include <linux/delay.h>
24#include <linux/cpufreq.h>
25
26#include <asm/cpu_device_id.h>
27#include <asm/msr.h>
28#include <linux/timex.h>
29#include <linux/io.h>
30
31#define REG_CSCIR 0x22
32#define REG_CSCDR 0x23
33
34
35static int max_freq;
36
37struct s_elan_multiplier {
38 int clock;
39 int val40h;
40 int val80h;
41};
42
43
44
45
46
47static struct s_elan_multiplier elan_multiplier[] = {
48 {1000, 0x02, 0x18},
49 {2000, 0x02, 0x10},
50 {4000, 0x02, 0x08},
51 {8000, 0x00, 0x00},
52 {16000, 0x00, 0x02},
53 {33000, 0x00, 0x04},
54 {66000, 0x01, 0x04},
55 {99000, 0x01, 0x05}
56};
57
58static struct cpufreq_frequency_table elanfreq_table[] = {
59 {0, 1000},
60 {1, 2000},
61 {2, 4000},
62 {3, 8000},
63 {4, 16000},
64 {5, 33000},
65 {6, 66000},
66 {7, 99000},
67 {0, CPUFREQ_TABLE_END},
68};
69
70
71
72
73
74
75
76
77
78
79
80static unsigned int elanfreq_get_cpu_frequency(unsigned int cpu)
81{
82 u8 clockspeed_reg;
83
84 local_irq_disable();
85 outb_p(0x80, REG_CSCIR);
86 clockspeed_reg = inb_p(REG_CSCDR);
87 local_irq_enable();
88
89 if ((clockspeed_reg & 0xE0) == 0xE0)
90 return 0;
91
92
93 if ((clockspeed_reg & 0xE0) == 0xC0) {
94 if ((clockspeed_reg & 0x01) == 0)
95 return 66000;
96 else
97 return 99000;
98 }
99
100
101 if ((clockspeed_reg & 0xE0) == 0xA0)
102 return 33000;
103
104 return (1<<((clockspeed_reg & 0xE0) >> 5)) * 1000;
105}
106
107
108static int elanfreq_target(struct cpufreq_policy *policy,
109 unsigned int state)
110{
111
112
113
114
115
116
117
118
119
120
121
122
123 local_irq_disable();
124 outb_p(0x40, REG_CSCIR);
125 outb_p(0x00, REG_CSCDR);
126 local_irq_enable();
127 udelay(1000);
128
129 local_irq_disable();
130
131
132 outb_p(0x80, REG_CSCIR);
133 outb_p(elan_multiplier[state].val80h, REG_CSCDR);
134
135
136 outb_p(0x40, REG_CSCIR);
137 outb_p(elan_multiplier[state].val40h, REG_CSCDR);
138 udelay(10000);
139 local_irq_enable();
140
141 return 0;
142}
143
144
145
146
147static int elanfreq_cpu_init(struct cpufreq_policy *policy)
148{
149 struct cpuinfo_x86 *c = &cpu_data(0);
150 unsigned int i;
151
152
153 if ((c->x86_vendor != X86_VENDOR_AMD) ||
154 (c->x86 != 4) || (c->x86_model != 10))
155 return -ENODEV;
156
157
158 if (!max_freq)
159 max_freq = elanfreq_get_cpu_frequency(0);
160
161
162 for (i = 0; (elanfreq_table[i].frequency != CPUFREQ_TABLE_END); i++) {
163 if (elanfreq_table[i].frequency > max_freq)
164 elanfreq_table[i].frequency = CPUFREQ_ENTRY_INVALID;
165 }
166
167
168 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
169
170 return cpufreq_table_validate_and_show(policy, elanfreq_table);
171}
172
173
174#ifndef MODULE
175
176
177
178
179
180
181
182
183
184
185
186static int __init elanfreq_setup(char *str)
187{
188 max_freq = simple_strtoul(str, &str, 0);
189 printk(KERN_WARNING "You're using the deprecated elanfreq command line option. Use elanfreq.max_freq instead, please!\n");
190 return 1;
191}
192__setup("elanfreq=", elanfreq_setup);
193#endif
194
195
196static struct cpufreq_driver elanfreq_driver = {
197 .get = elanfreq_get_cpu_frequency,
198 .verify = cpufreq_generic_frequency_table_verify,
199 .target_index = elanfreq_target,
200 .init = elanfreq_cpu_init,
201 .exit = cpufreq_generic_exit,
202 .name = "elanfreq",
203 .attr = cpufreq_generic_attr,
204};
205
206static const struct x86_cpu_id elan_id[] = {
207 { X86_VENDOR_AMD, 4, 10, },
208 {}
209};
210MODULE_DEVICE_TABLE(x86cpu, elan_id);
211
212static int __init elanfreq_init(void)
213{
214 if (!x86_match_cpu(elan_id))
215 return -ENODEV;
216 return cpufreq_register_driver(&elanfreq_driver);
217}
218
219
220static void __exit elanfreq_exit(void)
221{
222 cpufreq_unregister_driver(&elanfreq_driver);
223}
224
225
226module_param(max_freq, int, 0444);
227
228MODULE_LICENSE("GPL");
229MODULE_AUTHOR("Robert Schwebel <r.schwebel@pengutronix.de>, "
230 "Sven Geggus <sven@geggus.net>");
231MODULE_DESCRIPTION("cpufreq driver for AMD's Elan CPUs");
232
233module_init(elanfreq_init);
234module_exit(elanfreq_exit);
235