blob: fc9afbb7c1fcd154a9cc8a827498e06a6d98c81c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * (C) 2001-2004 Dave Jones. <davej@codemonkey.org.uk>
3 * (C) 2002 Padraig Brady. <padraig@antefacto.com>
4 *
5 * Licensed under the terms of the GNU GPL License version 2.
6 * Based upon datasheets & sample CPUs kindly provided by VIA.
7 *
8 * VIA have currently 3 different versions of Longhaul.
9 * Version 1 (Longhaul) uses the BCR2 MSR at 0x1147.
10 * It is present only in Samuel 1 (C5A), Samuel 2 (C5B) stepping 0.
11 * Version 2 of longhaul is the same as v1, but adds voltage scaling.
12 * Present in Samuel 2 (steppings 1-7 only) (C5B), and Ezra (C5C)
13 * voltage scaling support has currently been disabled in this driver
14 * until we have code that gets it right.
15 * Version 3 of longhaul got renamed to Powersaver and redesigned
16 * to use the POWERSAVER MSR at 0x110a.
17 * It is present in Ezra-T (C5M), Nehemiah (C5X) and above.
18 * It's pretty much the same feature wise to longhaul v2, though
19 * there is provision for scaling FSB too, but this doesn't work
20 * too well in practice so we don't even try to use this.
21 *
22 * BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
23 */
24
25#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/init.h>
29#include <linux/cpufreq.h>
Rafa³ Bilski179da8e2006-08-08 19:12:20 +020030#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/slab.h>
32#include <linux/string.h>
33
34#include <asm/msr.h>
35#include <asm/timex.h>
36#include <asm/io.h>
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +020037#include <asm/acpi.h>
38#include <linux/acpi.h>
39#include <acpi/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include "longhaul.h"
42
43#define PFX "longhaul: "
44
45#define TYPE_LONGHAUL_V1 1
46#define TYPE_LONGHAUL_V2 2
47#define TYPE_POWERSAVER 3
48
49#define CPU_SAMUEL 1
50#define CPU_SAMUEL2 2
51#define CPU_EZRA 3
52#define CPU_EZRA_T 4
53#define CPU_NEHEMIAH 5
Rafa³ Bilski980342a2007-01-31 23:42:47 +010054#define CPU_NEHEMIAH_C 6
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Rafa³ Bilski264166e2006-12-24 14:04:23 +010056/* Flags */
57#define USE_ACPI_C3 (1 << 1)
58#define USE_NORTHBRIDGE (1 << 2)
Rafa³ Bilski786f46b2007-02-04 18:43:12 +010059#define USE_VT8235 (1 << 3)
Rafa³ Bilski264166e2006-12-24 14:04:23 +010060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static int cpu_model;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +020062static unsigned int numscales=16;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static unsigned int fsb;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +020064
65static struct mV_pos *vrm_mV_table;
66static unsigned char *mV_vrm_table;
67struct f_msr {
68 unsigned char vrm;
69};
70static struct f_msr f_msr_table[32];
71
72static unsigned int highest_speed, lowest_speed; /* kHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static unsigned int minmult, maxmult;
74static int can_scale_voltage;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +020075static struct acpi_processor *pr = NULL;
76static struct acpi_processor_cx *cx = NULL;
Rafa³ Bilski264166e2006-12-24 14:04:23 +010077static u8 longhaul_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/* Module parameters */
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +020080static int scale_voltage;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "longhaul", msg)
83
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/* Clock ratios multiplied by 10 */
86static int clock_ratio[32];
87static int eblcr_table[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static int longhaul_version;
89static struct cpufreq_frequency_table *longhaul_table;
90
91#ifdef CONFIG_CPU_FREQ_DEBUG
92static char speedbuffer[8];
93
94static char *print_speed(int speed)
95{
Dave Jonese2aa8732006-05-30 17:37:15 -040096 if (speed < 1000) {
97 snprintf(speedbuffer, sizeof(speedbuffer),"%dMHz", speed);
98 return speedbuffer;
99 }
100
101 if (speed%1000 == 0)
102 snprintf(speedbuffer, sizeof(speedbuffer),
103 "%dGHz", speed/1000);
104 else
105 snprintf(speedbuffer, sizeof(speedbuffer),
106 "%d.%dGHz", speed/1000, (speed%1000)/100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 return speedbuffer;
109}
110#endif
111
112
113static unsigned int calc_speed(int mult)
114{
115 int khz;
116 khz = (mult/10)*fsb;
117 if (mult%10)
118 khz += fsb/2;
119 khz *= 1000;
120 return khz;
121}
122
123
124static int longhaul_get_cpu_mult(void)
125{
126 unsigned long invalue=0,lo, hi;
127
128 rdmsr (MSR_IA32_EBL_CR_POWERON, lo, hi);
129 invalue = (lo & (1<<22|1<<23|1<<24|1<<25)) >>22;
130 if (longhaul_version==TYPE_LONGHAUL_V2 || longhaul_version==TYPE_POWERSAVER) {
131 if (lo & (1<<27))
132 invalue+=16;
133 }
134 return eblcr_table[invalue];
135}
136
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200137/* For processor with BCR2 MSR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200139static void do_longhaul1(unsigned int clock_ratio_index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200141 union msr_bcr2 bcr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200143 rdmsrl(MSR_VIA_BCR2, bcr2.val);
144 /* Enable software clock multiplier */
145 bcr2.bits.ESOFTBF = 1;
146 bcr2.bits.CLOCKMUL = clock_ratio_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200148 /* Sync to timer tick */
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -0700149 safe_halt();
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200150 /* Change frequency on next halt or sleep */
151 wrmsrl(MSR_VIA_BCR2, bcr2.val);
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200152 /* Invoke transition */
153 ACPI_FLUSH_CPU_CACHE();
154 halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200156 /* Disable software clock multiplier */
Dave Jones3be6a482005-05-31 19:03:51 -0700157 local_irq_disable();
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200158 rdmsrl(MSR_VIA_BCR2, bcr2.val);
159 bcr2.bits.ESOFTBF = 0;
160 wrmsrl(MSR_VIA_BCR2, bcr2.val);
161}
Dave Jones3be6a482005-05-31 19:03:51 -0700162
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200163/* For processor with Longhaul MSR */
Dave Jones11746312005-05-31 19:03:51 -0700164
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200165static void do_powersaver(int cx_address, unsigned int clock_ratio_index)
166{
167 union msr_longhaul longhaul;
168 u32 t;
Dave Jones3be6a482005-05-31 19:03:51 -0700169
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200170 rdmsrl(MSR_VIA_LONGHAUL, longhaul.val);
171 longhaul.bits.RevisionKey = longhaul.bits.RevisionID;
172 longhaul.bits.SoftBusRatio = clock_ratio_index & 0xf;
173 longhaul.bits.SoftBusRatio4 = (clock_ratio_index & 0x10) >> 4;
Rafa³ Bilskieb23c752006-07-09 21:47:04 +0200174 longhaul.bits.EnableSoftBusRatio = 1;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200175
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200176 if (can_scale_voltage) {
177 longhaul.bits.SoftVID = f_msr_table[clock_ratio_index].vrm;
178 longhaul.bits.EnableSoftVID = 1;
179 }
180
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200181 /* Sync to timer tick */
182 safe_halt();
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200183 /* Change frequency on next halt or sleep */
184 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100185 if (!cx_address) {
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200186 ACPI_FLUSH_CPU_CACHE();
187 /* Invoke C1 */
188 halt();
189 } else {
190 ACPI_FLUSH_CPU_CACHE();
191 /* Invoke C3 */
192 inb(cx_address);
193 /* Dummy op - must do something useless after P_LVL3 read */
194 t = inl(acpi_fadt.xpm_tmr_blk.address);
195 }
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200196 /* Disable bus ratio bit */
197 local_irq_disable();
198 longhaul.bits.RevisionKey = longhaul.bits.RevisionID;
199 longhaul.bits.EnableSoftBusRatio = 0;
200 longhaul.bits.EnableSoftBSEL = 0;
201 longhaul.bits.EnableSoftVID = 0;
202 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
205/**
206 * longhaul_set_cpu_frequency()
207 * @clock_ratio_index : bitpattern of the new multiplier.
208 *
209 * Sets a new clock ratio.
210 */
211
212static void longhaul_setstate(unsigned int clock_ratio_index)
213{
214 int speed, mult;
215 struct cpufreq_freqs freqs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 static unsigned int old_ratio=-1;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200217 unsigned long flags;
218 unsigned int pic1_mask, pic2_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 if (old_ratio == clock_ratio_index)
221 return;
222 old_ratio = clock_ratio_index;
223
224 mult = clock_ratio[clock_ratio_index];
225 if (mult == -1)
226 return;
227
228 speed = calc_speed(mult);
229 if ((speed > highest_speed) || (speed < lowest_speed))
230 return;
231
232 freqs.old = calc_speed(longhaul_get_cpu_mult());
233 freqs.new = speed;
234 freqs.cpu = 0; /* longhaul.c is UP only driver */
235
236 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
237
238 dprintk ("Setting to FSB:%dMHz Mult:%d.%dx (%s)\n",
239 fsb, mult/10, mult%10, print_speed(speed/1000));
240
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200241 preempt_disable();
242 local_irq_save(flags);
243
244 pic2_mask = inb(0xA1);
245 pic1_mask = inb(0x21); /* works on C3. save mask. */
246 outb(0xFF,0xA1); /* Overkill */
247 outb(0xFE,0x21); /* TMR0 only */
248
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100249 if (longhaul_flags & USE_NORTHBRIDGE) {
250 /* Disable AGP and PCI arbiters */
251 outb(3, 0x22);
252 } else if ((pr != NULL) && pr->flags.bm_control) {
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200253 /* Disable bus master arbitration */
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200254 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1,
255 ACPI_MTX_DO_NOT_LOCK);
256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 switch (longhaul_version) {
258
259 /*
260 * Longhaul v1. (Samuel[C5A] and Samuel2 stepping 0[C5B])
261 * Software controlled multipliers only.
262 *
263 * *NB* Until we get voltage scaling working v1 & v2 are the same code.
264 * Longhaul v2 appears in Samuel2 Steppings 1->7 [C5b] and Ezra [C5C]
265 */
266 case TYPE_LONGHAUL_V1:
267 case TYPE_LONGHAUL_V2:
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200268 do_longhaul1(clock_ratio_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 break;
270
271 /*
272 * Longhaul v3 (aka Powersaver). (Ezra-T [C5M] & Nehemiah [C5N])
273 * We can scale voltage with this too, but that's currently
274 * disabled until we come up with a decent 'match freq to voltage'
275 * algorithm.
276 * When we add voltage scaling, we will also need to do the
277 * voltage/freq setting in order depending on the direction
278 * of scaling (like we do in powernow-k7.c)
279 * Nehemiah can do FSB scaling too, but this has never been proven
280 * to work in practice.
281 */
282 case TYPE_POWERSAVER:
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100283 if (longhaul_flags & USE_ACPI_C3) {
284 /* Don't allow wakeup */
285 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0,
286 ACPI_MTX_DO_NOT_LOCK);
287 do_powersaver(cx->address, clock_ratio_index);
288 } else {
289 do_powersaver(0, clock_ratio_index);
290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 break;
292 }
293
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100294 if (longhaul_flags & USE_NORTHBRIDGE) {
295 /* Enable arbiters */
296 outb(0, 0x22);
297 } else if ((pr != NULL) && pr->flags.bm_control) {
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200298 /* Enable bus master arbitration */
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200299 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0,
300 ACPI_MTX_DO_NOT_LOCK);
301 }
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200302 outb(pic2_mask,0xA1); /* restore mask */
303 outb(pic1_mask,0x21);
304
305 local_irq_restore(flags);
306 preempt_enable();
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
309}
310
311/*
312 * Centaur decided to make life a little more tricky.
313 * Only longhaul v1 is allowed to read EBLCR BSEL[0:1].
314 * Samuel2 and above have to try and guess what the FSB is.
315 * We do this by assuming we booted at maximum multiplier, and interpolate
316 * between that value multiplied by possible FSBs and cpu_mhz which
317 * was calculated at boot time. Really ugly, but no other way to do this.
318 */
319
320#define ROUNDING 0xf
321
Rafa³ Bilski24ebead2007-01-01 23:49:34 +0100322static int guess_fsb(int mult)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Rafa³ Bilski46ef9552007-02-04 15:58:46 +0100324 int speed = cpu_khz / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 int i;
Rafa³ Bilski46ef9552007-02-04 15:58:46 +0100326 int speeds[] = { 666, 1000, 1333, 2000 };
327 int f_max, f_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Rafa³ Bilski46ef9552007-02-04 15:58:46 +0100329 for (i = 0; i < 4; i++) {
330 f_max = ((speeds[i] * mult) + 50) / 100;
331 f_max += (ROUNDING / 2);
332 f_min = f_max - ROUNDING;
333 if ((speed <= f_max) && (speed >= f_min))
334 return speeds[i] / 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336 return 0;
337}
338
339
340static int __init longhaul_get_ranges(void)
341{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 unsigned int j, k = 0;
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100343 int mult;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100345 /* Get current frequency */
346 mult = longhaul_get_cpu_mult();
347 if (mult == -1) {
348 printk(KERN_INFO PFX "Invalid (reserved) multiplier!\n");
349 return -EINVAL;
350 }
351 fsb = guess_fsb(mult);
352 if (fsb == 0) {
353 printk(KERN_INFO PFX "Invalid (reserved) FSB!\n");
354 return -EINVAL;
355 }
356 /* Get max multiplier - as we always did.
357 * Longhaul MSR is usefull only when voltage scaling is enabled.
358 * C3 is booting at max anyway. */
359 maxmult = mult;
360 /* Get min multiplier */
Rafa³ Bilski9addf3b2007-02-07 22:53:29 +0100361 switch (cpu_model) {
362 case CPU_NEHEMIAH:
363 minmult = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 break;
Rafa³ Bilski9addf3b2007-02-07 22:53:29 +0100365 case CPU_NEHEMIAH_C:
366 minmult = 40;
367 break;
368 default:
369 minmult = 30;
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100370 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372
373 dprintk ("MinMult:%d.%dx MaxMult:%d.%dx\n",
374 minmult/10, minmult%10, maxmult/10, maxmult%10);
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 highest_speed = calc_speed(maxmult);
377 lowest_speed = calc_speed(minmult);
378 dprintk ("FSB:%dMHz Lowest speed: %s Highest speed:%s\n", fsb,
379 print_speed(lowest_speed/1000),
380 print_speed(highest_speed/1000));
381
382 if (lowest_speed == highest_speed) {
383 printk (KERN_INFO PFX "highestspeed == lowest, aborting.\n");
384 return -EINVAL;
385 }
386 if (lowest_speed > highest_speed) {
387 printk (KERN_INFO PFX "nonsense! lowest (%d > %d) !\n",
388 lowest_speed, highest_speed);
389 return -EINVAL;
390 }
391
392 longhaul_table = kmalloc((numscales + 1) * sizeof(struct cpufreq_frequency_table), GFP_KERNEL);
393 if(!longhaul_table)
394 return -ENOMEM;
395
396 for (j=0; j < numscales; j++) {
397 unsigned int ratio;
398 ratio = clock_ratio[j];
399 if (ratio == -1)
400 continue;
401 if (ratio > maxmult || ratio < minmult)
402 continue;
403 longhaul_table[k].frequency = calc_speed(ratio);
404 longhaul_table[k].index = j;
405 k++;
406 }
407
408 longhaul_table[k].frequency = CPUFREQ_TABLE_END;
409 if (!k) {
410 kfree (longhaul_table);
411 return -EINVAL;
412 }
413
414 return 0;
415}
416
417
418static void __init longhaul_setup_voltagescaling(void)
419{
420 union msr_longhaul longhaul;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200421 struct mV_pos minvid, maxvid;
422 unsigned int j, speed, pos, kHz_step, numvscales;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200424 rdmsrl(MSR_VIA_LONGHAUL, longhaul.val);
425 if (!(longhaul.bits.RevisionID & 1)) {
426 printk(KERN_INFO PFX "Voltage scaling not supported by CPU.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200430 if (!longhaul.bits.VRMRev) {
431 printk (KERN_INFO PFX "VRM 8.5\n");
432 vrm_mV_table = &vrm85_mV[0];
433 mV_vrm_table = &mV_vrm85[0];
434 } else {
435 printk (KERN_INFO PFX "Mobile VRM\n");
436 vrm_mV_table = &mobilevrm_mV[0];
437 mV_vrm_table = &mV_mobilevrm[0];
438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200440 minvid = vrm_mV_table[longhaul.bits.MinimumVID];
441 maxvid = vrm_mV_table[longhaul.bits.MaximumVID];
442 numvscales = maxvid.pos - minvid.pos + 1;
443 kHz_step = (highest_speed - lowest_speed) / numvscales;
444
445 if (minvid.mV == 0 || maxvid.mV == 0 || minvid.mV > maxvid.mV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 printk (KERN_INFO PFX "Bogus values Min:%d.%03d Max:%d.%03d. "
447 "Voltage scaling disabled.\n",
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200448 minvid.mV/1000, minvid.mV%1000, maxvid.mV/1000, maxvid.mV%1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return;
450 }
451
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200452 if (minvid.mV == maxvid.mV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 printk (KERN_INFO PFX "Claims to support voltage scaling but min & max are "
454 "both %d.%03d. Voltage scaling disabled\n",
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200455 maxvid.mV/1000, maxvid.mV%1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return;
457 }
458
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200459 printk(KERN_INFO PFX "Max VID=%d.%03d Min VID=%d.%03d, %d possible voltage scales\n",
460 maxvid.mV/1000, maxvid.mV%1000,
461 minvid.mV/1000, minvid.mV%1000,
462 numvscales);
463
464 j = 0;
465 while (longhaul_table[j].frequency != CPUFREQ_TABLE_END) {
466 speed = longhaul_table[j].frequency;
467 pos = (speed - lowest_speed) / kHz_step + minvid.pos;
468 f_msr_table[longhaul_table[j].index].vrm = mV_vrm_table[pos];
469 j++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 can_scale_voltage = 1;
473}
474
475
476static int longhaul_verify(struct cpufreq_policy *policy)
477{
478 return cpufreq_frequency_table_verify(policy, longhaul_table);
479}
480
481
482static int longhaul_target(struct cpufreq_policy *policy,
483 unsigned int target_freq, unsigned int relation)
484{
485 unsigned int table_index = 0;
486 unsigned int new_clock_ratio = 0;
487
488 if (cpufreq_frequency_table_target(policy, longhaul_table, target_freq, relation, &table_index))
489 return -EINVAL;
490
491 new_clock_ratio = longhaul_table[table_index].index & 0xFF;
492
493 longhaul_setstate(new_clock_ratio);
494
495 return 0;
496}
497
498
499static unsigned int longhaul_get(unsigned int cpu)
500{
501 if (cpu)
502 return 0;
503 return calc_speed(longhaul_get_cpu_mult());
504}
505
Adrian Bunkc4a96c12006-07-09 19:53:08 +0200506static acpi_status longhaul_walk_callback(acpi_handle obj_handle,
507 u32 nesting_level,
508 void *context, void **return_value)
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200509{
510 struct acpi_device *d;
511
512 if ( acpi_bus_get_device(obj_handle, &d) ) {
513 return 0;
514 }
515 *return_value = (void *)acpi_driver_data(d);
516 return 1;
517}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200519/* VIA don't support PM2 reg, but have something similar */
520static int enable_arbiter_disable(void)
521{
522 struct pci_dev *dev;
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200523 int reg;
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200524 u8 pci_cmd;
525
526 /* Find PLE133 host bridge */
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200527 reg = 0x78;
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200528 dev = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8601_0, NULL);
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200529 /* Find CLE266 host bridge */
530 if (dev == NULL) {
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200531 reg = 0x76;
Rafa³ Bilskieed7d412006-09-27 08:25:27 +0200532 dev = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_862X_0, NULL);
Rafa³ Bilskidb2fb9d2006-11-30 03:47:41 +0100533 /* Find CN400 V-Link host bridge */
534 if (dev == NULL)
535 dev = pci_find_device(PCI_VENDOR_ID_VIA, 0x7259, NULL);
536
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200537 }
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200538 if (dev != NULL) {
539 /* Enable access to port 0x22 */
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200540 pci_read_config_byte(dev, reg, &pci_cmd);
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100541 if (!(pci_cmd & 1<<7)) {
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200542 pci_cmd |= 1<<7;
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200543 pci_write_config_byte(dev, reg, pci_cmd);
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100544 pci_read_config_byte(dev, reg, &pci_cmd);
545 if (!(pci_cmd & 1<<7)) {
546 printk(KERN_ERR PFX
547 "Can't enable access to port 0x22.\n");
548 return 0;
549 }
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200550 }
551 return 1;
552 }
553 return 0;
554}
555
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100556static int longhaul_setup_vt8235(void)
557{
558 struct pci_dev *dev;
559 u8 pci_cmd;
560
561 /* Find VT8235 southbridge */
562 dev = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8235, NULL);
563 if (dev != NULL) {
564 /* Set transition time to max */
565 pci_read_config_byte(dev, 0xec, &pci_cmd);
566 pci_cmd &= ~(1 << 2);
567 pci_write_config_byte(dev, 0xec, pci_cmd);
568 pci_read_config_byte(dev, 0xe4, &pci_cmd);
569 pci_cmd &= ~(1 << 7);
570 pci_write_config_byte(dev, 0xe4, pci_cmd);
571 pci_read_config_byte(dev, 0xe5, &pci_cmd);
572 pci_cmd |= 1 << 7;
573 pci_write_config_byte(dev, 0xe5, pci_cmd);
574 return 1;
575 }
576 return 0;
577}
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579static int __init longhaul_cpu_init(struct cpufreq_policy *policy)
580{
581 struct cpuinfo_x86 *c = cpu_data;
582 char *cpuname=NULL;
583 int ret;
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100584 int vt8235_present;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200586 /* Check what we have on this motherboard */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 switch (c->x86_model) {
588 case 6:
589 cpu_model = CPU_SAMUEL;
590 cpuname = "C3 'Samuel' [C5A]";
591 longhaul_version = TYPE_LONGHAUL_V1;
592 memcpy (clock_ratio, samuel1_clock_ratio, sizeof(samuel1_clock_ratio));
593 memcpy (eblcr_table, samuel1_eblcr, sizeof(samuel1_eblcr));
594 break;
595
596 case 7:
597 longhaul_version = TYPE_LONGHAUL_V1;
598 switch (c->x86_mask) {
599 case 0:
600 cpu_model = CPU_SAMUEL2;
601 cpuname = "C3 'Samuel 2' [C5B]";
602 /* Note, this is not a typo, early Samuel2's had Samuel1 ratios. */
603 memcpy (clock_ratio, samuel1_clock_ratio, sizeof(samuel1_clock_ratio));
604 memcpy (eblcr_table, samuel2_eblcr, sizeof(samuel2_eblcr));
605 break;
606 case 1 ... 15:
607 if (c->x86_mask < 8) {
608 cpu_model = CPU_SAMUEL2;
609 cpuname = "C3 'Samuel 2' [C5B]";
610 } else {
611 cpu_model = CPU_EZRA;
612 cpuname = "C3 'Ezra' [C5C]";
613 }
614 memcpy (clock_ratio, ezra_clock_ratio, sizeof(ezra_clock_ratio));
615 memcpy (eblcr_table, ezra_eblcr, sizeof(ezra_eblcr));
616 break;
617 }
618 break;
619
620 case 8:
621 cpu_model = CPU_EZRA_T;
622 cpuname = "C3 'Ezra-T' [C5M]";
623 longhaul_version = TYPE_POWERSAVER;
624 numscales=32;
625 memcpy (clock_ratio, ezrat_clock_ratio, sizeof(ezrat_clock_ratio));
626 memcpy (eblcr_table, ezrat_eblcr, sizeof(ezrat_eblcr));
627 break;
628
629 case 9:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 longhaul_version = TYPE_POWERSAVER;
Rafa³ Bilski0d44b2b2007-01-31 23:50:49 +0100631 numscales = 32;
632 memcpy(clock_ratio,
633 nehemiah_clock_ratio,
634 sizeof(nehemiah_clock_ratio));
635 memcpy(eblcr_table, nehemiah_eblcr, sizeof(nehemiah_eblcr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 switch (c->x86_mask) {
637 case 0 ... 1:
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100638 cpu_model = CPU_NEHEMIAH;
Rafa³ Bilskie57501c2007-02-08 23:12:02 +0100639 cpuname = "C3 'Nehemiah A' [C5XLOE]";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 break;
641 case 2 ... 4:
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100642 cpu_model = CPU_NEHEMIAH;
Rafa³ Bilskie57501c2007-02-08 23:12:02 +0100643 cpuname = "C3 'Nehemiah B' [C5XLOH]";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 break;
645 case 5 ... 15:
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100646 cpu_model = CPU_NEHEMIAH_C;
Rafa³ Bilskie57501c2007-02-08 23:12:02 +0100647 cpuname = "C3 'Nehemiah C' [C5P]";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 break;
649 }
650 break;
651
652 default:
653 cpuname = "Unknown";
654 break;
655 }
656
657 printk (KERN_INFO PFX "VIA %s CPU detected. ", cpuname);
658 switch (longhaul_version) {
659 case TYPE_LONGHAUL_V1:
660 case TYPE_LONGHAUL_V2:
661 printk ("Longhaul v%d supported.\n", longhaul_version);
662 break;
663 case TYPE_POWERSAVER:
664 printk ("Powersaver supported.\n");
665 break;
666 };
667
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100668 /* Doesn't hurt */
669 vt8235_present = longhaul_setup_vt8235();
670
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200671 /* Find ACPI data for processor */
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100672 acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
673 ACPI_UINT32_MAX, &longhaul_walk_callback,
674 NULL, (void *)&pr);
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200675
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100676 /* Check ACPI support for C3 state */
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100677 if (pr != NULL && longhaul_version == TYPE_POWERSAVER) {
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200678 cx = &pr->power.states[ACPI_STATE_C3];
Rafa³ Bilski14796722007-01-19 22:28:22 +0100679 if (cx->address > 0 && cx->latency <= 1000) {
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100680 longhaul_flags |= USE_ACPI_C3;
Rafa³ Bilskieed7d412006-09-27 08:25:27 +0200681 goto print_support_type;
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200682 }
683 }
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100684 /* Check if northbridge is friendly */
685 if (enable_arbiter_disable()) {
686 longhaul_flags |= USE_NORTHBRIDGE;
687 goto print_support_type;
Rafa³ Bilskieed7d412006-09-27 08:25:27 +0200688 }
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100689 /* Use VT8235 southbridge if present */
690 if (longhaul_version == TYPE_POWERSAVER && vt8235_present) {
691 longhaul_flags |= USE_VT8235;
692 goto print_support_type;
693 }
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100694 /* Check ACPI support for bus master arbiter disable */
695 if ((pr == NULL) || !(pr->flags.bm_control)) {
696 printk(KERN_ERR PFX
697 "No ACPI support. Unsupported northbridge.\n");
698 return -ENODEV;
699 }
700
Rafa³ Bilskieed7d412006-09-27 08:25:27 +0200701print_support_type:
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100702 if (longhaul_flags & USE_NORTHBRIDGE)
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200703 printk (KERN_INFO PFX "Using northbridge support.\n");
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100704 else if (longhaul_flags & USE_VT8235)
705 printk (KERN_INFO PFX "Using VT8235 support.\n");
706 else
707 printk (KERN_INFO PFX "Using ACPI support.\n");
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 ret = longhaul_get_ranges();
710 if (ret != 0)
711 return ret;
712
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100713 if ((longhaul_version != TYPE_LONGHAUL_V1) && (scale_voltage != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 longhaul_setup_voltagescaling();
715
716 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Dave Jones6778bae2005-05-31 19:03:51 -0700717 policy->cpuinfo.transition_latency = 200000; /* nsec */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 policy->cur = calc_speed(longhaul_get_cpu_mult());
719
720 ret = cpufreq_frequency_table_cpuinfo(policy, longhaul_table);
721 if (ret)
722 return ret;
723
724 cpufreq_frequency_table_get_attr(longhaul_table, policy->cpu);
725
726 return 0;
727}
728
729static int __devexit longhaul_cpu_exit(struct cpufreq_policy *policy)
730{
731 cpufreq_frequency_table_put_attr(policy->cpu);
732 return 0;
733}
734
735static struct freq_attr* longhaul_attr[] = {
736 &cpufreq_freq_attr_scaling_available_freqs,
737 NULL,
738};
739
740static struct cpufreq_driver longhaul_driver = {
741 .verify = longhaul_verify,
742 .target = longhaul_target,
743 .get = longhaul_get,
744 .init = longhaul_cpu_init,
745 .exit = __devexit_p(longhaul_cpu_exit),
746 .name = "longhaul",
747 .owner = THIS_MODULE,
748 .attr = longhaul_attr,
749};
750
751
752static int __init longhaul_init(void)
753{
754 struct cpuinfo_x86 *c = cpu_data;
755
756 if (c->x86_vendor != X86_VENDOR_CENTAUR || c->x86 != 6)
757 return -ENODEV;
758
Rafa³ Bilski48b7bde2006-07-04 17:50:57 +0200759#ifdef CONFIG_SMP
760 if (num_online_cpus() > 1) {
Rafa³ Bilski48b7bde2006-07-04 17:50:57 +0200761 printk(KERN_ERR PFX "More than 1 CPU detected, longhaul disabled.\n");
Dave Jones1cfe2012006-12-28 22:30:16 -0500762 return -ENODEV;
Rafa³ Bilski48b7bde2006-07-04 17:50:57 +0200763 }
764#endif
765#ifdef CONFIG_X86_IO_APIC
766 if (cpu_has_apic) {
767 printk(KERN_ERR PFX "APIC detected. Longhaul is currently broken in this configuration.\n");
768 return -ENODEV;
769 }
770#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 switch (c->x86_model) {
772 case 6 ... 9:
773 return cpufreq_register_driver(&longhaul_driver);
Dave Jones8ec98222006-12-17 19:07:35 -0500774 case 10:
775 printk(KERN_ERR PFX "Use acpi-cpufreq driver for VIA C7\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 default:
Dave Jones928ee512006-12-17 19:09:59 -0500777 ;;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
779
780 return -ENODEV;
781}
782
783
784static void __exit longhaul_exit(void)
785{
Dave Jones8eebf1a2006-05-30 17:40:16 -0400786 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 for (i=0; i < numscales; i++) {
789 if (clock_ratio[i] == maxmult) {
790 longhaul_setstate(i);
791 break;
792 }
793 }
794
795 cpufreq_unregister_driver(&longhaul_driver);
796 kfree(longhaul_table);
797}
798
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200799module_param (scale_voltage, int, 0644);
800MODULE_PARM_DESC(scale_voltage, "Scale voltage of processor");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802MODULE_AUTHOR ("Dave Jones <davej@codemonkey.org.uk>");
803MODULE_DESCRIPTION ("Longhaul driver for VIA Cyrix processors.");
804MODULE_LICENSE ("GPL");
805
Rafa³ Bilski0d6daba2006-07-07 08:48:26 +0200806late_initcall(longhaul_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807module_exit(longhaul_exit);