blob: f152c291e7c8590628df02f88eb9d49adc3ff11f [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
54
Rafa³ Bilski264166e2006-12-24 14:04:23 +010055/* Flags */
56#define USE_ACPI_C3 (1 << 1)
57#define USE_NORTHBRIDGE (1 << 2)
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static int cpu_model;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +020060static unsigned int numscales=16;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static unsigned int fsb;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +020062
63static struct mV_pos *vrm_mV_table;
64static unsigned char *mV_vrm_table;
65struct f_msr {
66 unsigned char vrm;
67};
68static struct f_msr f_msr_table[32];
69
70static unsigned int highest_speed, lowest_speed; /* kHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static unsigned int minmult, maxmult;
72static int can_scale_voltage;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +020073static struct acpi_processor *pr = NULL;
74static struct acpi_processor_cx *cx = NULL;
Rafa³ Bilski264166e2006-12-24 14:04:23 +010075static u8 longhaul_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/* Module parameters */
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +020078static int scale_voltage;
79static int ignore_latency;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "longhaul", msg)
82
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/* Clock ratios multiplied by 10 */
85static int clock_ratio[32];
86static int eblcr_table[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static int longhaul_version;
88static struct cpufreq_frequency_table *longhaul_table;
89
90#ifdef CONFIG_CPU_FREQ_DEBUG
91static char speedbuffer[8];
92
93static char *print_speed(int speed)
94{
Dave Jonese2aa8732006-05-30 17:37:15 -040095 if (speed < 1000) {
96 snprintf(speedbuffer, sizeof(speedbuffer),"%dMHz", speed);
97 return speedbuffer;
98 }
99
100 if (speed%1000 == 0)
101 snprintf(speedbuffer, sizeof(speedbuffer),
102 "%dGHz", speed/1000);
103 else
104 snprintf(speedbuffer, sizeof(speedbuffer),
105 "%d.%dGHz", speed/1000, (speed%1000)/100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 return speedbuffer;
108}
109#endif
110
111
112static unsigned int calc_speed(int mult)
113{
114 int khz;
115 khz = (mult/10)*fsb;
116 if (mult%10)
117 khz += fsb/2;
118 khz *= 1000;
119 return khz;
120}
121
122
123static int longhaul_get_cpu_mult(void)
124{
125 unsigned long invalue=0,lo, hi;
126
127 rdmsr (MSR_IA32_EBL_CR_POWERON, lo, hi);
128 invalue = (lo & (1<<22|1<<23|1<<24|1<<25)) >>22;
129 if (longhaul_version==TYPE_LONGHAUL_V2 || longhaul_version==TYPE_POWERSAVER) {
130 if (lo & (1<<27))
131 invalue+=16;
132 }
133 return eblcr_table[invalue];
134}
135
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200136/* For processor with BCR2 MSR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200138static void do_longhaul1(unsigned int clock_ratio_index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200140 union msr_bcr2 bcr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200142 rdmsrl(MSR_VIA_BCR2, bcr2.val);
143 /* Enable software clock multiplier */
144 bcr2.bits.ESOFTBF = 1;
145 bcr2.bits.CLOCKMUL = clock_ratio_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200147 /* Sync to timer tick */
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -0700148 safe_halt();
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200149 /* Change frequency on next halt or sleep */
150 wrmsrl(MSR_VIA_BCR2, bcr2.val);
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200151 /* Invoke transition */
152 ACPI_FLUSH_CPU_CACHE();
153 halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200155 /* Disable software clock multiplier */
Dave Jones3be6a482005-05-31 19:03:51 -0700156 local_irq_disable();
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200157 rdmsrl(MSR_VIA_BCR2, bcr2.val);
158 bcr2.bits.ESOFTBF = 0;
159 wrmsrl(MSR_VIA_BCR2, bcr2.val);
160}
Dave Jones3be6a482005-05-31 19:03:51 -0700161
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200162/* For processor with Longhaul MSR */
Dave Jones11746312005-05-31 19:03:51 -0700163
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200164static void do_powersaver(int cx_address, unsigned int clock_ratio_index)
165{
166 union msr_longhaul longhaul;
167 u32 t;
Dave Jones3be6a482005-05-31 19:03:51 -0700168
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200169 rdmsrl(MSR_VIA_LONGHAUL, longhaul.val);
170 longhaul.bits.RevisionKey = longhaul.bits.RevisionID;
171 longhaul.bits.SoftBusRatio = clock_ratio_index & 0xf;
172 longhaul.bits.SoftBusRatio4 = (clock_ratio_index & 0x10) >> 4;
Rafa³ Bilskieb23c752006-07-09 21:47:04 +0200173 longhaul.bits.EnableSoftBusRatio = 1;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200174
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200175 if (can_scale_voltage) {
176 longhaul.bits.SoftVID = f_msr_table[clock_ratio_index].vrm;
177 longhaul.bits.EnableSoftVID = 1;
178 }
179
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200180 /* Sync to timer tick */
181 safe_halt();
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200182 /* Change frequency on next halt or sleep */
183 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100184 if (!cx_address) {
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200185 ACPI_FLUSH_CPU_CACHE();
186 /* Invoke C1 */
187 halt();
188 } else {
189 ACPI_FLUSH_CPU_CACHE();
190 /* Invoke C3 */
191 inb(cx_address);
192 /* Dummy op - must do something useless after P_LVL3 read */
193 t = inl(acpi_fadt.xpm_tmr_blk.address);
194 }
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200195 /* Disable bus ratio bit */
196 local_irq_disable();
197 longhaul.bits.RevisionKey = longhaul.bits.RevisionID;
198 longhaul.bits.EnableSoftBusRatio = 0;
199 longhaul.bits.EnableSoftBSEL = 0;
200 longhaul.bits.EnableSoftVID = 0;
201 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
204/**
205 * longhaul_set_cpu_frequency()
206 * @clock_ratio_index : bitpattern of the new multiplier.
207 *
208 * Sets a new clock ratio.
209 */
210
211static void longhaul_setstate(unsigned int clock_ratio_index)
212{
213 int speed, mult;
214 struct cpufreq_freqs freqs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 static unsigned int old_ratio=-1;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200216 unsigned long flags;
217 unsigned int pic1_mask, pic2_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 if (old_ratio == clock_ratio_index)
220 return;
221 old_ratio = clock_ratio_index;
222
223 mult = clock_ratio[clock_ratio_index];
224 if (mult == -1)
225 return;
226
227 speed = calc_speed(mult);
228 if ((speed > highest_speed) || (speed < lowest_speed))
229 return;
230
231 freqs.old = calc_speed(longhaul_get_cpu_mult());
232 freqs.new = speed;
233 freqs.cpu = 0; /* longhaul.c is UP only driver */
234
235 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
236
237 dprintk ("Setting to FSB:%dMHz Mult:%d.%dx (%s)\n",
238 fsb, mult/10, mult%10, print_speed(speed/1000));
239
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200240 preempt_disable();
241 local_irq_save(flags);
242
243 pic2_mask = inb(0xA1);
244 pic1_mask = inb(0x21); /* works on C3. save mask. */
245 outb(0xFF,0xA1); /* Overkill */
246 outb(0xFE,0x21); /* TMR0 only */
247
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100248 if (longhaul_flags & USE_NORTHBRIDGE) {
249 /* Disable AGP and PCI arbiters */
250 outb(3, 0x22);
251 } else if ((pr != NULL) && pr->flags.bm_control) {
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200252 /* Disable bus master arbitration */
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200253 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1,
254 ACPI_MTX_DO_NOT_LOCK);
255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 switch (longhaul_version) {
257
258 /*
259 * Longhaul v1. (Samuel[C5A] and Samuel2 stepping 0[C5B])
260 * Software controlled multipliers only.
261 *
262 * *NB* Until we get voltage scaling working v1 & v2 are the same code.
263 * Longhaul v2 appears in Samuel2 Steppings 1->7 [C5b] and Ezra [C5C]
264 */
265 case TYPE_LONGHAUL_V1:
266 case TYPE_LONGHAUL_V2:
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200267 do_longhaul1(clock_ratio_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 break;
269
270 /*
271 * Longhaul v3 (aka Powersaver). (Ezra-T [C5M] & Nehemiah [C5N])
272 * We can scale voltage with this too, but that's currently
273 * disabled until we come up with a decent 'match freq to voltage'
274 * algorithm.
275 * When we add voltage scaling, we will also need to do the
276 * voltage/freq setting in order depending on the direction
277 * of scaling (like we do in powernow-k7.c)
278 * Nehemiah can do FSB scaling too, but this has never been proven
279 * to work in practice.
280 */
281 case TYPE_POWERSAVER:
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100282 if (longhaul_flags & USE_ACPI_C3) {
283 /* Don't allow wakeup */
284 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0,
285 ACPI_MTX_DO_NOT_LOCK);
286 do_powersaver(cx->address, clock_ratio_index);
287 } else {
288 do_powersaver(0, clock_ratio_index);
289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 break;
291 }
292
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100293 if (longhaul_flags & USE_NORTHBRIDGE) {
294 /* Enable arbiters */
295 outb(0, 0x22);
296 } else if ((pr != NULL) && pr->flags.bm_control) {
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200297 /* Enable bus master arbitration */
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200298 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0,
299 ACPI_MTX_DO_NOT_LOCK);
300 }
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200301 outb(pic2_mask,0xA1); /* restore mask */
302 outb(pic1_mask,0x21);
303
304 local_irq_restore(flags);
305 preempt_enable();
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
308}
309
310/*
311 * Centaur decided to make life a little more tricky.
312 * Only longhaul v1 is allowed to read EBLCR BSEL[0:1].
313 * Samuel2 and above have to try and guess what the FSB is.
314 * We do this by assuming we booted at maximum multiplier, and interpolate
315 * between that value multiplied by possible FSBs and cpu_mhz which
316 * was calculated at boot time. Really ugly, but no other way to do this.
317 */
318
319#define ROUNDING 0xf
320
Rafa³ Bilski24ebead2007-01-01 23:49:34 +0100321static int _guess(int guess, int mult)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 int target;
324
Rafa³ Bilski24ebead2007-01-01 23:49:34 +0100325 target = ((mult/10)*guess);
326 if (mult%10 != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 target += (guess/2);
328 target += ROUNDING/2;
329 target &= ~ROUNDING;
330 return target;
331}
332
333
Rafa³ Bilski24ebead2007-01-01 23:49:34 +0100334static int guess_fsb(int mult)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 int speed = (cpu_khz/1000);
337 int i;
Rafa³ Bilski24ebead2007-01-01 23:49:34 +0100338 int speeds[] = { 66, 100, 133, 200 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 speed += ROUNDING/2;
341 speed &= ~ROUNDING;
342
Rafa³ Bilski24ebead2007-01-01 23:49:34 +0100343 for (i=0; i<4; i++) {
344 if (_guess(speeds[i], mult) == speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return speeds[i];
346 }
347 return 0;
348}
349
350
351static int __init longhaul_get_ranges(void)
352{
353 unsigned long invalue;
Rafa³ Bilski32deb2d2006-07-15 19:31:30 +0200354 unsigned int ezra_t_multipliers[32]= {
355 90, 30, 40, 100, 55, 35, 45, 95,
356 50, 70, 80, 60, 120, 75, 85, 65,
357 -1, 110, 120, -1, 135, 115, 125, 105,
358 130, 150, 160, 140, -1, 155, -1, 145 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 unsigned int j, k = 0;
360 union msr_longhaul longhaul;
361 unsigned long lo, hi;
362 unsigned int eblcr_fsb_table_v1[] = { 66, 133, 100, -1 };
363 unsigned int eblcr_fsb_table_v2[] = { 133, 100, -1, 66 };
Rafa³ Bilski24ebead2007-01-01 23:49:34 +0100364 int mult;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 switch (longhaul_version) {
367 case TYPE_LONGHAUL_V1:
368 case TYPE_LONGHAUL_V2:
369 /* Ugh, Longhaul v1 didn't have the min/max MSRs.
370 Assume min=3.0x & max = whatever we booted at. */
371 minmult = 30;
Rafa³ Bilski24ebead2007-01-01 23:49:34 +0100372 maxmult = mult = longhaul_get_cpu_mult();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 break;
374
375 case TYPE_POWERSAVER:
376 /* Ezra-T */
377 if (cpu_model==CPU_EZRA_T) {
Rafa³ Bilski24ebead2007-01-01 23:49:34 +0100378 minmult = 30;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 rdmsrl (MSR_VIA_LONGHAUL, longhaul.val);
380 invalue = longhaul.bits.MaxMHzBR;
381 if (longhaul.bits.MaxMHzBR4)
382 invalue += 16;
Rafa³ Bilski24ebead2007-01-01 23:49:34 +0100383 maxmult = mult = ezra_t_multipliers[invalue];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 break;
385 }
386
387 /* Nehemiah */
388 if (cpu_model==CPU_NEHEMIAH) {
389 rdmsrl (MSR_VIA_LONGHAUL, longhaul.val);
390
391 /*
392 * TODO: This code works, but raises a lot of questions.
393 * - Some Nehemiah's seem to have broken Min/MaxMHzBR's.
394 * We get around this by using a hardcoded multiplier of 4.0x
395 * for the minimimum speed, and the speed we booted up at for the max.
396 * This is done in longhaul_get_cpu_mult() by reading the EBLCR register.
397 * - According to some VIA documentation EBLCR is only
398 * in pre-Nehemiah C3s. How this still works is a mystery.
399 * We're possibly using something undocumented and unsupported,
400 * But it works, so we don't grumble.
401 */
402 minmult=40;
Rafa³ Bilski24ebead2007-01-01 23:49:34 +0100403 maxmult = mult = longhaul_get_cpu_mult();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 break;
405 }
406 }
Rafa³ Bilski24ebead2007-01-01 23:49:34 +0100407 fsb = guess_fsb(mult);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 dprintk ("MinMult:%d.%dx MaxMult:%d.%dx\n",
410 minmult/10, minmult%10, maxmult/10, maxmult%10);
411
Rafa³ Bilski24ebead2007-01-01 23:49:34 +0100412 if (fsb == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 printk (KERN_INFO PFX "Invalid (reserved) FSB!\n");
414 return -EINVAL;
415 }
416
417 highest_speed = calc_speed(maxmult);
418 lowest_speed = calc_speed(minmult);
419 dprintk ("FSB:%dMHz Lowest speed: %s Highest speed:%s\n", fsb,
420 print_speed(lowest_speed/1000),
421 print_speed(highest_speed/1000));
422
423 if (lowest_speed == highest_speed) {
424 printk (KERN_INFO PFX "highestspeed == lowest, aborting.\n");
425 return -EINVAL;
426 }
427 if (lowest_speed > highest_speed) {
428 printk (KERN_INFO PFX "nonsense! lowest (%d > %d) !\n",
429 lowest_speed, highest_speed);
430 return -EINVAL;
431 }
432
433 longhaul_table = kmalloc((numscales + 1) * sizeof(struct cpufreq_frequency_table), GFP_KERNEL);
434 if(!longhaul_table)
435 return -ENOMEM;
436
437 for (j=0; j < numscales; j++) {
438 unsigned int ratio;
439 ratio = clock_ratio[j];
440 if (ratio == -1)
441 continue;
442 if (ratio > maxmult || ratio < minmult)
443 continue;
444 longhaul_table[k].frequency = calc_speed(ratio);
445 longhaul_table[k].index = j;
446 k++;
447 }
448
449 longhaul_table[k].frequency = CPUFREQ_TABLE_END;
450 if (!k) {
451 kfree (longhaul_table);
452 return -EINVAL;
453 }
454
455 return 0;
456}
457
458
459static void __init longhaul_setup_voltagescaling(void)
460{
461 union msr_longhaul longhaul;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200462 struct mV_pos minvid, maxvid;
463 unsigned int j, speed, pos, kHz_step, numvscales;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200465 rdmsrl(MSR_VIA_LONGHAUL, longhaul.val);
466 if (!(longhaul.bits.RevisionID & 1)) {
467 printk(KERN_INFO PFX "Voltage scaling not supported by CPU.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200471 if (!longhaul.bits.VRMRev) {
472 printk (KERN_INFO PFX "VRM 8.5\n");
473 vrm_mV_table = &vrm85_mV[0];
474 mV_vrm_table = &mV_vrm85[0];
475 } else {
476 printk (KERN_INFO PFX "Mobile VRM\n");
477 vrm_mV_table = &mobilevrm_mV[0];
478 mV_vrm_table = &mV_mobilevrm[0];
479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200481 minvid = vrm_mV_table[longhaul.bits.MinimumVID];
482 maxvid = vrm_mV_table[longhaul.bits.MaximumVID];
483 numvscales = maxvid.pos - minvid.pos + 1;
484 kHz_step = (highest_speed - lowest_speed) / numvscales;
485
486 if (minvid.mV == 0 || maxvid.mV == 0 || minvid.mV > maxvid.mV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 printk (KERN_INFO PFX "Bogus values Min:%d.%03d Max:%d.%03d. "
488 "Voltage scaling disabled.\n",
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200489 minvid.mV/1000, minvid.mV%1000, maxvid.mV/1000, maxvid.mV%1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return;
491 }
492
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200493 if (minvid.mV == maxvid.mV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 printk (KERN_INFO PFX "Claims to support voltage scaling but min & max are "
495 "both %d.%03d. Voltage scaling disabled\n",
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200496 maxvid.mV/1000, maxvid.mV%1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return;
498 }
499
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200500 printk(KERN_INFO PFX "Max VID=%d.%03d Min VID=%d.%03d, %d possible voltage scales\n",
501 maxvid.mV/1000, maxvid.mV%1000,
502 minvid.mV/1000, minvid.mV%1000,
503 numvscales);
504
505 j = 0;
506 while (longhaul_table[j].frequency != CPUFREQ_TABLE_END) {
507 speed = longhaul_table[j].frequency;
508 pos = (speed - lowest_speed) / kHz_step + minvid.pos;
509 f_msr_table[longhaul_table[j].index].vrm = mV_vrm_table[pos];
510 j++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 can_scale_voltage = 1;
514}
515
516
517static int longhaul_verify(struct cpufreq_policy *policy)
518{
519 return cpufreq_frequency_table_verify(policy, longhaul_table);
520}
521
522
523static int longhaul_target(struct cpufreq_policy *policy,
524 unsigned int target_freq, unsigned int relation)
525{
526 unsigned int table_index = 0;
527 unsigned int new_clock_ratio = 0;
528
529 if (cpufreq_frequency_table_target(policy, longhaul_table, target_freq, relation, &table_index))
530 return -EINVAL;
531
532 new_clock_ratio = longhaul_table[table_index].index & 0xFF;
533
534 longhaul_setstate(new_clock_ratio);
535
536 return 0;
537}
538
539
540static unsigned int longhaul_get(unsigned int cpu)
541{
542 if (cpu)
543 return 0;
544 return calc_speed(longhaul_get_cpu_mult());
545}
546
Adrian Bunkc4a96c12006-07-09 19:53:08 +0200547static acpi_status longhaul_walk_callback(acpi_handle obj_handle,
548 u32 nesting_level,
549 void *context, void **return_value)
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200550{
551 struct acpi_device *d;
552
553 if ( acpi_bus_get_device(obj_handle, &d) ) {
554 return 0;
555 }
556 *return_value = (void *)acpi_driver_data(d);
557 return 1;
558}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200560/* VIA don't support PM2 reg, but have something similar */
561static int enable_arbiter_disable(void)
562{
563 struct pci_dev *dev;
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200564 int reg;
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200565 u8 pci_cmd;
566
567 /* Find PLE133 host bridge */
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200568 reg = 0x78;
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200569 dev = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8601_0, NULL);
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200570 /* Find CLE266 host bridge */
571 if (dev == NULL) {
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200572 reg = 0x76;
Rafa³ Bilskieed7d412006-09-27 08:25:27 +0200573 dev = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_862X_0, NULL);
Rafa³ Bilskidb2fb9d2006-11-30 03:47:41 +0100574 /* Find CN400 V-Link host bridge */
575 if (dev == NULL)
576 dev = pci_find_device(PCI_VENDOR_ID_VIA, 0x7259, NULL);
577
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200578 }
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200579 if (dev != NULL) {
580 /* Enable access to port 0x22 */
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200581 pci_read_config_byte(dev, reg, &pci_cmd);
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200582 if ( !(pci_cmd & 1<<7) ) {
583 pci_cmd |= 1<<7;
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200584 pci_write_config_byte(dev, reg, pci_cmd);
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200585 }
586 return 1;
587 }
588 return 0;
589}
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591static int __init longhaul_cpu_init(struct cpufreq_policy *policy)
592{
593 struct cpuinfo_x86 *c = cpu_data;
594 char *cpuname=NULL;
595 int ret;
596
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200597 /* Check what we have on this motherboard */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 switch (c->x86_model) {
599 case 6:
600 cpu_model = CPU_SAMUEL;
601 cpuname = "C3 'Samuel' [C5A]";
602 longhaul_version = TYPE_LONGHAUL_V1;
603 memcpy (clock_ratio, samuel1_clock_ratio, sizeof(samuel1_clock_ratio));
604 memcpy (eblcr_table, samuel1_eblcr, sizeof(samuel1_eblcr));
605 break;
606
607 case 7:
608 longhaul_version = TYPE_LONGHAUL_V1;
609 switch (c->x86_mask) {
610 case 0:
611 cpu_model = CPU_SAMUEL2;
612 cpuname = "C3 'Samuel 2' [C5B]";
613 /* Note, this is not a typo, early Samuel2's had Samuel1 ratios. */
614 memcpy (clock_ratio, samuel1_clock_ratio, sizeof(samuel1_clock_ratio));
615 memcpy (eblcr_table, samuel2_eblcr, sizeof(samuel2_eblcr));
616 break;
617 case 1 ... 15:
618 if (c->x86_mask < 8) {
619 cpu_model = CPU_SAMUEL2;
620 cpuname = "C3 'Samuel 2' [C5B]";
621 } else {
622 cpu_model = CPU_EZRA;
623 cpuname = "C3 'Ezra' [C5C]";
624 }
625 memcpy (clock_ratio, ezra_clock_ratio, sizeof(ezra_clock_ratio));
626 memcpy (eblcr_table, ezra_eblcr, sizeof(ezra_eblcr));
627 break;
628 }
629 break;
630
631 case 8:
632 cpu_model = CPU_EZRA_T;
633 cpuname = "C3 'Ezra-T' [C5M]";
634 longhaul_version = TYPE_POWERSAVER;
635 numscales=32;
636 memcpy (clock_ratio, ezrat_clock_ratio, sizeof(ezrat_clock_ratio));
637 memcpy (eblcr_table, ezrat_eblcr, sizeof(ezrat_eblcr));
638 break;
639
640 case 9:
641 cpu_model = CPU_NEHEMIAH;
642 longhaul_version = TYPE_POWERSAVER;
643 numscales=32;
644 switch (c->x86_mask) {
645 case 0 ... 1:
646 cpuname = "C3 'Nehemiah A' [C5N]";
647 memcpy (clock_ratio, nehemiah_a_clock_ratio, sizeof(nehemiah_a_clock_ratio));
648 memcpy (eblcr_table, nehemiah_a_eblcr, sizeof(nehemiah_a_eblcr));
649 break;
650 case 2 ... 4:
651 cpuname = "C3 'Nehemiah B' [C5N]";
652 memcpy (clock_ratio, nehemiah_b_clock_ratio, sizeof(nehemiah_b_clock_ratio));
653 memcpy (eblcr_table, nehemiah_b_eblcr, sizeof(nehemiah_b_eblcr));
654 break;
655 case 5 ... 15:
656 cpuname = "C3 'Nehemiah C' [C5N]";
657 memcpy (clock_ratio, nehemiah_c_clock_ratio, sizeof(nehemiah_c_clock_ratio));
658 memcpy (eblcr_table, nehemiah_c_eblcr, sizeof(nehemiah_c_eblcr));
659 break;
660 }
661 break;
662
663 default:
664 cpuname = "Unknown";
665 break;
666 }
667
668 printk (KERN_INFO PFX "VIA %s CPU detected. ", cpuname);
669 switch (longhaul_version) {
670 case TYPE_LONGHAUL_V1:
671 case TYPE_LONGHAUL_V2:
672 printk ("Longhaul v%d supported.\n", longhaul_version);
673 break;
674 case TYPE_POWERSAVER:
675 printk ("Powersaver supported.\n");
676 break;
677 };
678
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200679 /* Find ACPI data for processor */
680 acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT, ACPI_UINT32_MAX,
681 &longhaul_walk_callback, NULL, (void *)&pr);
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200682
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100683 /* Check ACPI support for C3 state */
684 if ((pr != NULL) && (longhaul_version == TYPE_POWERSAVER)) {
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200685 cx = &pr->power.states[ACPI_STATE_C3];
Rafa³ Bilskieed7d412006-09-27 08:25:27 +0200686 if (cx->address > 0 &&
687 (cx->latency <= 1000 || ignore_latency != 0) ) {
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100688 longhaul_flags |= USE_ACPI_C3;
Rafa³ Bilskieed7d412006-09-27 08:25:27 +0200689 goto print_support_type;
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200690 }
691 }
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100692 /* Check if northbridge is friendly */
693 if (enable_arbiter_disable()) {
694 longhaul_flags |= USE_NORTHBRIDGE;
695 goto print_support_type;
Rafa³ Bilskieed7d412006-09-27 08:25:27 +0200696 }
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100697
698 /* No ACPI C3 or we can't use it */
699 /* Check ACPI support for bus master arbiter disable */
700 if ((pr == NULL) || !(pr->flags.bm_control)) {
701 printk(KERN_ERR PFX
702 "No ACPI support. Unsupported northbridge.\n");
703 return -ENODEV;
704 }
705
Rafa³ Bilskieed7d412006-09-27 08:25:27 +0200706print_support_type:
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100707 if (!(longhaul_flags & USE_NORTHBRIDGE)) {
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200708 printk (KERN_INFO PFX "Using ACPI support.\n");
709 } else {
710 printk (KERN_INFO PFX "Using northbridge support.\n");
711 }
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 ret = longhaul_get_ranges();
714 if (ret != 0)
715 return ret;
716
717 if ((longhaul_version==TYPE_LONGHAUL_V2 || longhaul_version==TYPE_POWERSAVER) &&
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200718 (scale_voltage != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 longhaul_setup_voltagescaling();
720
721 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Dave Jones6778bae2005-05-31 19:03:51 -0700722 policy->cpuinfo.transition_latency = 200000; /* nsec */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 policy->cur = calc_speed(longhaul_get_cpu_mult());
724
725 ret = cpufreq_frequency_table_cpuinfo(policy, longhaul_table);
726 if (ret)
727 return ret;
728
729 cpufreq_frequency_table_get_attr(longhaul_table, policy->cpu);
730
731 return 0;
732}
733
734static int __devexit longhaul_cpu_exit(struct cpufreq_policy *policy)
735{
736 cpufreq_frequency_table_put_attr(policy->cpu);
737 return 0;
738}
739
740static struct freq_attr* longhaul_attr[] = {
741 &cpufreq_freq_attr_scaling_available_freqs,
742 NULL,
743};
744
745static struct cpufreq_driver longhaul_driver = {
746 .verify = longhaul_verify,
747 .target = longhaul_target,
748 .get = longhaul_get,
749 .init = longhaul_cpu_init,
750 .exit = __devexit_p(longhaul_cpu_exit),
751 .name = "longhaul",
752 .owner = THIS_MODULE,
753 .attr = longhaul_attr,
754};
755
756
757static int __init longhaul_init(void)
758{
759 struct cpuinfo_x86 *c = cpu_data;
760
761 if (c->x86_vendor != X86_VENDOR_CENTAUR || c->x86 != 6)
762 return -ENODEV;
763
Rafa³ Bilski48b7bde2006-07-04 17:50:57 +0200764#ifdef CONFIG_SMP
765 if (num_online_cpus() > 1) {
Rafa³ Bilski48b7bde2006-07-04 17:50:57 +0200766 printk(KERN_ERR PFX "More than 1 CPU detected, longhaul disabled.\n");
Dave Jones1cfe2012006-12-28 22:30:16 -0500767 return -ENODEV;
Rafa³ Bilski48b7bde2006-07-04 17:50:57 +0200768 }
769#endif
770#ifdef CONFIG_X86_IO_APIC
771 if (cpu_has_apic) {
772 printk(KERN_ERR PFX "APIC detected. Longhaul is currently broken in this configuration.\n");
773 return -ENODEV;
774 }
775#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 switch (c->x86_model) {
777 case 6 ... 9:
778 return cpufreq_register_driver(&longhaul_driver);
Dave Jones8ec98222006-12-17 19:07:35 -0500779 case 10:
780 printk(KERN_ERR PFX "Use acpi-cpufreq driver for VIA C7\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 default:
Dave Jones928ee512006-12-17 19:09:59 -0500782 ;;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784
785 return -ENODEV;
786}
787
788
789static void __exit longhaul_exit(void)
790{
Dave Jones8eebf1a2006-05-30 17:40:16 -0400791 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 for (i=0; i < numscales; i++) {
794 if (clock_ratio[i] == maxmult) {
795 longhaul_setstate(i);
796 break;
797 }
798 }
799
800 cpufreq_unregister_driver(&longhaul_driver);
801 kfree(longhaul_table);
802}
803
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200804module_param (scale_voltage, int, 0644);
805MODULE_PARM_DESC(scale_voltage, "Scale voltage of processor");
Rafa³ Bilski65954132006-08-13 09:16:20 +0200806module_param(ignore_latency, int, 0644);
807MODULE_PARM_DESC(ignore_latency, "Skip ACPI C3 latency test");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809MODULE_AUTHOR ("Dave Jones <davej@codemonkey.org.uk>");
810MODULE_DESCRIPTION ("Longhaul driver for VIA Cyrix processors.");
811MODULE_LICENSE ("GPL");
812
Rafa³ Bilski0d6daba2006-07-07 08:48:26 +0200813late_initcall(longhaul_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814module_exit(longhaul_exit);