blob: c548daad347639c8451a9be8636e121d5a9c86af [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
55static int cpu_model;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +020056static unsigned int numscales=16;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static unsigned int fsb;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +020058
59static struct mV_pos *vrm_mV_table;
60static unsigned char *mV_vrm_table;
61struct f_msr {
62 unsigned char vrm;
63};
64static struct f_msr f_msr_table[32];
65
66static unsigned int highest_speed, lowest_speed; /* kHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static unsigned int minmult, maxmult;
68static int can_scale_voltage;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +020069static struct acpi_processor *pr = NULL;
70static struct acpi_processor_cx *cx = NULL;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +020071static int port22_en;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/* Module parameters */
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +020074static int scale_voltage;
75static int ignore_latency;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "longhaul", msg)
78
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/* Clock ratios multiplied by 10 */
81static int clock_ratio[32];
82static int eblcr_table[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static unsigned int highest_speed, lowest_speed; /* kHz */
84static int longhaul_version;
85static struct cpufreq_frequency_table *longhaul_table;
86
87#ifdef CONFIG_CPU_FREQ_DEBUG
88static char speedbuffer[8];
89
90static char *print_speed(int speed)
91{
Dave Jonese2aa8732006-05-30 17:37:15 -040092 if (speed < 1000) {
93 snprintf(speedbuffer, sizeof(speedbuffer),"%dMHz", speed);
94 return speedbuffer;
95 }
96
97 if (speed%1000 == 0)
98 snprintf(speedbuffer, sizeof(speedbuffer),
99 "%dGHz", speed/1000);
100 else
101 snprintf(speedbuffer, sizeof(speedbuffer),
102 "%d.%dGHz", speed/1000, (speed%1000)/100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 return speedbuffer;
105}
106#endif
107
108
109static unsigned int calc_speed(int mult)
110{
111 int khz;
112 khz = (mult/10)*fsb;
113 if (mult%10)
114 khz += fsb/2;
115 khz *= 1000;
116 return khz;
117}
118
119
120static int longhaul_get_cpu_mult(void)
121{
122 unsigned long invalue=0,lo, hi;
123
124 rdmsr (MSR_IA32_EBL_CR_POWERON, lo, hi);
125 invalue = (lo & (1<<22|1<<23|1<<24|1<<25)) >>22;
126 if (longhaul_version==TYPE_LONGHAUL_V2 || longhaul_version==TYPE_POWERSAVER) {
127 if (lo & (1<<27))
128 invalue+=16;
129 }
130 return eblcr_table[invalue];
131}
132
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200133/* For processor with BCR2 MSR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200135static void do_longhaul1(unsigned int clock_ratio_index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200137 union msr_bcr2 bcr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200139 rdmsrl(MSR_VIA_BCR2, bcr2.val);
140 /* Enable software clock multiplier */
141 bcr2.bits.ESOFTBF = 1;
142 bcr2.bits.CLOCKMUL = clock_ratio_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200144 /* Sync to timer tick */
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -0700145 safe_halt();
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200146 /* Change frequency on next halt or sleep */
147 wrmsrl(MSR_VIA_BCR2, bcr2.val);
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200148 /* Invoke transition */
149 ACPI_FLUSH_CPU_CACHE();
150 halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200152 /* Disable software clock multiplier */
Dave Jones3be6a482005-05-31 19:03:51 -0700153 local_irq_disable();
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200154 rdmsrl(MSR_VIA_BCR2, bcr2.val);
155 bcr2.bits.ESOFTBF = 0;
156 wrmsrl(MSR_VIA_BCR2, bcr2.val);
157}
Dave Jones3be6a482005-05-31 19:03:51 -0700158
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200159/* For processor with Longhaul MSR */
Dave Jones11746312005-05-31 19:03:51 -0700160
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200161static void do_powersaver(int cx_address, unsigned int clock_ratio_index)
162{
163 union msr_longhaul longhaul;
164 u32 t;
Dave Jones3be6a482005-05-31 19:03:51 -0700165
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200166 rdmsrl(MSR_VIA_LONGHAUL, longhaul.val);
167 longhaul.bits.RevisionKey = longhaul.bits.RevisionID;
168 longhaul.bits.SoftBusRatio = clock_ratio_index & 0xf;
169 longhaul.bits.SoftBusRatio4 = (clock_ratio_index & 0x10) >> 4;
Rafa³ Bilskieb23c752006-07-09 21:47:04 +0200170 longhaul.bits.EnableSoftBusRatio = 1;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200171
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200172 if (can_scale_voltage) {
173 longhaul.bits.SoftVID = f_msr_table[clock_ratio_index].vrm;
174 longhaul.bits.EnableSoftVID = 1;
175 }
176
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200177 /* Sync to timer tick */
178 safe_halt();
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200179 /* Change frequency on next halt or sleep */
180 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200181 if (port22_en) {
182 ACPI_FLUSH_CPU_CACHE();
183 /* Invoke C1 */
184 halt();
185 } else {
186 ACPI_FLUSH_CPU_CACHE();
187 /* Invoke C3 */
188 inb(cx_address);
189 /* Dummy op - must do something useless after P_LVL3 read */
190 t = inl(acpi_fadt.xpm_tmr_blk.address);
191 }
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200192
193 /* Disable bus ratio bit */
194 local_irq_disable();
195 longhaul.bits.RevisionKey = longhaul.bits.RevisionID;
196 longhaul.bits.EnableSoftBusRatio = 0;
197 longhaul.bits.EnableSoftBSEL = 0;
198 longhaul.bits.EnableSoftVID = 0;
199 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202/**
203 * longhaul_set_cpu_frequency()
204 * @clock_ratio_index : bitpattern of the new multiplier.
205 *
206 * Sets a new clock ratio.
207 */
208
209static void longhaul_setstate(unsigned int clock_ratio_index)
210{
211 int speed, mult;
212 struct cpufreq_freqs freqs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 static unsigned int old_ratio=-1;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200214 unsigned long flags;
215 unsigned int pic1_mask, pic2_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 if (old_ratio == clock_ratio_index)
218 return;
219 old_ratio = clock_ratio_index;
220
221 mult = clock_ratio[clock_ratio_index];
222 if (mult == -1)
223 return;
224
225 speed = calc_speed(mult);
226 if ((speed > highest_speed) || (speed < lowest_speed))
227 return;
228
229 freqs.old = calc_speed(longhaul_get_cpu_mult());
230 freqs.new = speed;
231 freqs.cpu = 0; /* longhaul.c is UP only driver */
232
233 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
234
235 dprintk ("Setting to FSB:%dMHz Mult:%d.%dx (%s)\n",
236 fsb, mult/10, mult%10, print_speed(speed/1000));
237
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200238 preempt_disable();
239 local_irq_save(flags);
240
241 pic2_mask = inb(0xA1);
242 pic1_mask = inb(0x21); /* works on C3. save mask. */
243 outb(0xFF,0xA1); /* Overkill */
244 outb(0xFE,0x21); /* TMR0 only */
245
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200246 if (pr->flags.bm_control) {
247 /* Disable bus master arbitration */
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200248 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1,
249 ACPI_MTX_DO_NOT_LOCK);
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200250 } else if (port22_en) {
251 /* Disable AGP and PCI arbiters */
252 outb(3, 0x22);
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200253 }
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 switch (longhaul_version) {
256
257 /*
258 * Longhaul v1. (Samuel[C5A] and Samuel2 stepping 0[C5B])
259 * Software controlled multipliers only.
260 *
261 * *NB* Until we get voltage scaling working v1 & v2 are the same code.
262 * Longhaul v2 appears in Samuel2 Steppings 1->7 [C5b] and Ezra [C5C]
263 */
264 case TYPE_LONGHAUL_V1:
265 case TYPE_LONGHAUL_V2:
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200266 do_longhaul1(clock_ratio_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 break;
268
269 /*
270 * Longhaul v3 (aka Powersaver). (Ezra-T [C5M] & Nehemiah [C5N])
271 * We can scale voltage with this too, but that's currently
272 * disabled until we come up with a decent 'match freq to voltage'
273 * algorithm.
274 * When we add voltage scaling, we will also need to do the
275 * voltage/freq setting in order depending on the direction
276 * of scaling (like we do in powernow-k7.c)
277 * Nehemiah can do FSB scaling too, but this has never been proven
278 * to work in practice.
279 */
280 case TYPE_POWERSAVER:
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200281 /* Don't allow wakeup */
282 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0,
283 ACPI_MTX_DO_NOT_LOCK);
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200284 do_powersaver(cx->address, clock_ratio_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 break;
286 }
287
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200288 if (pr->flags.bm_control) {
289 /* Enable bus master arbitration */
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200290 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0,
291 ACPI_MTX_DO_NOT_LOCK);
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200292 } else if (port22_en) {
293 /* Enable arbiters */
294 outb(0, 0x22);
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200295 }
296
297 outb(pic2_mask,0xA1); /* restore mask */
298 outb(pic1_mask,0x21);
299
300 local_irq_restore(flags);
301 preempt_enable();
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
304}
305
306/*
307 * Centaur decided to make life a little more tricky.
308 * Only longhaul v1 is allowed to read EBLCR BSEL[0:1].
309 * Samuel2 and above have to try and guess what the FSB is.
310 * We do this by assuming we booted at maximum multiplier, and interpolate
311 * between that value multiplied by possible FSBs and cpu_mhz which
312 * was calculated at boot time. Really ugly, but no other way to do this.
313 */
314
315#define ROUNDING 0xf
316
317static int _guess(int guess)
318{
319 int target;
320
321 target = ((maxmult/10)*guess);
322 if (maxmult%10 != 0)
323 target += (guess/2);
324 target += ROUNDING/2;
325 target &= ~ROUNDING;
326 return target;
327}
328
329
330static int guess_fsb(void)
331{
332 int speed = (cpu_khz/1000);
333 int i;
334 int speeds[3] = { 66, 100, 133 };
335
336 speed += ROUNDING/2;
337 speed &= ~ROUNDING;
338
339 for (i=0; i<3; i++) {
340 if (_guess(speeds[i]) == speed)
341 return speeds[i];
342 }
343 return 0;
344}
345
346
347static int __init longhaul_get_ranges(void)
348{
349 unsigned long invalue;
Rafa³ Bilski32deb2d2006-07-15 19:31:30 +0200350 unsigned int ezra_t_multipliers[32]= {
351 90, 30, 40, 100, 55, 35, 45, 95,
352 50, 70, 80, 60, 120, 75, 85, 65,
353 -1, 110, 120, -1, 135, 115, 125, 105,
354 130, 150, 160, 140, -1, 155, -1, 145 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 unsigned int j, k = 0;
356 union msr_longhaul longhaul;
357 unsigned long lo, hi;
358 unsigned int eblcr_fsb_table_v1[] = { 66, 133, 100, -1 };
359 unsigned int eblcr_fsb_table_v2[] = { 133, 100, -1, 66 };
360
361 switch (longhaul_version) {
362 case TYPE_LONGHAUL_V1:
363 case TYPE_LONGHAUL_V2:
364 /* Ugh, Longhaul v1 didn't have the min/max MSRs.
365 Assume min=3.0x & max = whatever we booted at. */
366 minmult = 30;
367 maxmult = longhaul_get_cpu_mult();
368 rdmsr (MSR_IA32_EBL_CR_POWERON, lo, hi);
369 invalue = (lo & (1<<18|1<<19)) >>18;
370 if (cpu_model==CPU_SAMUEL || cpu_model==CPU_SAMUEL2)
371 fsb = eblcr_fsb_table_v1[invalue];
372 else
373 fsb = guess_fsb();
374 break;
375
376 case TYPE_POWERSAVER:
377 /* Ezra-T */
378 if (cpu_model==CPU_EZRA_T) {
379 rdmsrl (MSR_VIA_LONGHAUL, longhaul.val);
380 invalue = longhaul.bits.MaxMHzBR;
381 if (longhaul.bits.MaxMHzBR4)
382 invalue += 16;
Rafa³ Bilski32deb2d2006-07-15 19:31:30 +0200383 maxmult=ezra_t_multipliers[invalue];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 invalue = longhaul.bits.MinMHzBR;
386 if (longhaul.bits.MinMHzBR4 == 1)
387 minmult = 30;
388 else
Rafa³ Bilski32deb2d2006-07-15 19:31:30 +0200389 minmult = ezra_t_multipliers[invalue];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 fsb = eblcr_fsb_table_v2[longhaul.bits.MaxMHzFSB];
391 break;
392 }
393
394 /* Nehemiah */
395 if (cpu_model==CPU_NEHEMIAH) {
396 rdmsrl (MSR_VIA_LONGHAUL, longhaul.val);
397
398 /*
399 * TODO: This code works, but raises a lot of questions.
400 * - Some Nehemiah's seem to have broken Min/MaxMHzBR's.
401 * We get around this by using a hardcoded multiplier of 4.0x
402 * for the minimimum speed, and the speed we booted up at for the max.
403 * This is done in longhaul_get_cpu_mult() by reading the EBLCR register.
404 * - According to some VIA documentation EBLCR is only
405 * in pre-Nehemiah C3s. How this still works is a mystery.
406 * We're possibly using something undocumented and unsupported,
407 * But it works, so we don't grumble.
408 */
409 minmult=40;
410 maxmult=longhaul_get_cpu_mult();
411
412 /* Starting with the 1.2GHz parts, theres a 200MHz bus. */
Rafa³ Bilski3f4a25f2006-11-30 03:36:44 +0100413 if ((cpu_khz/maxmult) > 13400)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 fsb = 200;
415 else
416 fsb = eblcr_fsb_table_v2[longhaul.bits.MaxMHzFSB];
417 break;
418 }
419 }
420
421 dprintk ("MinMult:%d.%dx MaxMult:%d.%dx\n",
422 minmult/10, minmult%10, maxmult/10, maxmult%10);
423
424 if (fsb == -1) {
425 printk (KERN_INFO PFX "Invalid (reserved) FSB!\n");
426 return -EINVAL;
427 }
428
429 highest_speed = calc_speed(maxmult);
430 lowest_speed = calc_speed(minmult);
431 dprintk ("FSB:%dMHz Lowest speed: %s Highest speed:%s\n", fsb,
432 print_speed(lowest_speed/1000),
433 print_speed(highest_speed/1000));
434
435 if (lowest_speed == highest_speed) {
436 printk (KERN_INFO PFX "highestspeed == lowest, aborting.\n");
437 return -EINVAL;
438 }
439 if (lowest_speed > highest_speed) {
440 printk (KERN_INFO PFX "nonsense! lowest (%d > %d) !\n",
441 lowest_speed, highest_speed);
442 return -EINVAL;
443 }
444
445 longhaul_table = kmalloc((numscales + 1) * sizeof(struct cpufreq_frequency_table), GFP_KERNEL);
446 if(!longhaul_table)
447 return -ENOMEM;
448
449 for (j=0; j < numscales; j++) {
450 unsigned int ratio;
451 ratio = clock_ratio[j];
452 if (ratio == -1)
453 continue;
454 if (ratio > maxmult || ratio < minmult)
455 continue;
456 longhaul_table[k].frequency = calc_speed(ratio);
457 longhaul_table[k].index = j;
458 k++;
459 }
460
461 longhaul_table[k].frequency = CPUFREQ_TABLE_END;
462 if (!k) {
463 kfree (longhaul_table);
464 return -EINVAL;
465 }
466
467 return 0;
468}
469
470
471static void __init longhaul_setup_voltagescaling(void)
472{
473 union msr_longhaul longhaul;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200474 struct mV_pos minvid, maxvid;
475 unsigned int j, speed, pos, kHz_step, numvscales;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200477 rdmsrl(MSR_VIA_LONGHAUL, longhaul.val);
478 if (!(longhaul.bits.RevisionID & 1)) {
479 printk(KERN_INFO PFX "Voltage scaling not supported by CPU.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200483 if (!longhaul.bits.VRMRev) {
484 printk (KERN_INFO PFX "VRM 8.5\n");
485 vrm_mV_table = &vrm85_mV[0];
486 mV_vrm_table = &mV_vrm85[0];
487 } else {
488 printk (KERN_INFO PFX "Mobile VRM\n");
489 vrm_mV_table = &mobilevrm_mV[0];
490 mV_vrm_table = &mV_mobilevrm[0];
491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200493 minvid = vrm_mV_table[longhaul.bits.MinimumVID];
494 maxvid = vrm_mV_table[longhaul.bits.MaximumVID];
495 numvscales = maxvid.pos - minvid.pos + 1;
496 kHz_step = (highest_speed - lowest_speed) / numvscales;
497
498 if (minvid.mV == 0 || maxvid.mV == 0 || minvid.mV > maxvid.mV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 printk (KERN_INFO PFX "Bogus values Min:%d.%03d Max:%d.%03d. "
500 "Voltage scaling disabled.\n",
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200501 minvid.mV/1000, minvid.mV%1000, maxvid.mV/1000, maxvid.mV%1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return;
503 }
504
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200505 if (minvid.mV == maxvid.mV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 printk (KERN_INFO PFX "Claims to support voltage scaling but min & max are "
507 "both %d.%03d. Voltage scaling disabled\n",
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200508 maxvid.mV/1000, maxvid.mV%1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return;
510 }
511
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200512 printk(KERN_INFO PFX "Max VID=%d.%03d Min VID=%d.%03d, %d possible voltage scales\n",
513 maxvid.mV/1000, maxvid.mV%1000,
514 minvid.mV/1000, minvid.mV%1000,
515 numvscales);
516
517 j = 0;
518 while (longhaul_table[j].frequency != CPUFREQ_TABLE_END) {
519 speed = longhaul_table[j].frequency;
520 pos = (speed - lowest_speed) / kHz_step + minvid.pos;
521 f_msr_table[longhaul_table[j].index].vrm = mV_vrm_table[pos];
522 j++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 can_scale_voltage = 1;
526}
527
528
529static int longhaul_verify(struct cpufreq_policy *policy)
530{
531 return cpufreq_frequency_table_verify(policy, longhaul_table);
532}
533
534
535static int longhaul_target(struct cpufreq_policy *policy,
536 unsigned int target_freq, unsigned int relation)
537{
538 unsigned int table_index = 0;
539 unsigned int new_clock_ratio = 0;
540
541 if (cpufreq_frequency_table_target(policy, longhaul_table, target_freq, relation, &table_index))
542 return -EINVAL;
543
544 new_clock_ratio = longhaul_table[table_index].index & 0xFF;
545
546 longhaul_setstate(new_clock_ratio);
547
548 return 0;
549}
550
551
552static unsigned int longhaul_get(unsigned int cpu)
553{
554 if (cpu)
555 return 0;
556 return calc_speed(longhaul_get_cpu_mult());
557}
558
Adrian Bunkc4a96c12006-07-09 19:53:08 +0200559static acpi_status longhaul_walk_callback(acpi_handle obj_handle,
560 u32 nesting_level,
561 void *context, void **return_value)
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200562{
563 struct acpi_device *d;
564
565 if ( acpi_bus_get_device(obj_handle, &d) ) {
566 return 0;
567 }
568 *return_value = (void *)acpi_driver_data(d);
569 return 1;
570}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200572/* VIA don't support PM2 reg, but have something similar */
573static int enable_arbiter_disable(void)
574{
575 struct pci_dev *dev;
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200576 int reg;
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200577 u8 pci_cmd;
578
579 /* Find PLE133 host bridge */
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200580 reg = 0x78;
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200581 dev = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8601_0, NULL);
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200582 /* Find CLE266 host bridge */
583 if (dev == NULL) {
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200584 reg = 0x76;
Rafa³ Bilskieed7d412006-09-27 08:25:27 +0200585 dev = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_862X_0, NULL);
Rafa³ Bilskidb2fb9d2006-11-30 03:47:41 +0100586 /* Find CN400 V-Link host bridge */
587 if (dev == NULL)
588 dev = pci_find_device(PCI_VENDOR_ID_VIA, 0x7259, NULL);
589
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200590 }
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200591 if (dev != NULL) {
592 /* Enable access to port 0x22 */
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200593 pci_read_config_byte(dev, reg, &pci_cmd);
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200594 if ( !(pci_cmd & 1<<7) ) {
595 pci_cmd |= 1<<7;
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200596 pci_write_config_byte(dev, reg, pci_cmd);
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200597 }
598 return 1;
599 }
600 return 0;
601}
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603static int __init longhaul_cpu_init(struct cpufreq_policy *policy)
604{
605 struct cpuinfo_x86 *c = cpu_data;
606 char *cpuname=NULL;
607 int ret;
608
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200609 /* Check what we have on this motherboard */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 switch (c->x86_model) {
611 case 6:
612 cpu_model = CPU_SAMUEL;
613 cpuname = "C3 'Samuel' [C5A]";
614 longhaul_version = TYPE_LONGHAUL_V1;
615 memcpy (clock_ratio, samuel1_clock_ratio, sizeof(samuel1_clock_ratio));
616 memcpy (eblcr_table, samuel1_eblcr, sizeof(samuel1_eblcr));
617 break;
618
619 case 7:
620 longhaul_version = TYPE_LONGHAUL_V1;
621 switch (c->x86_mask) {
622 case 0:
623 cpu_model = CPU_SAMUEL2;
624 cpuname = "C3 'Samuel 2' [C5B]";
625 /* Note, this is not a typo, early Samuel2's had Samuel1 ratios. */
626 memcpy (clock_ratio, samuel1_clock_ratio, sizeof(samuel1_clock_ratio));
627 memcpy (eblcr_table, samuel2_eblcr, sizeof(samuel2_eblcr));
628 break;
629 case 1 ... 15:
630 if (c->x86_mask < 8) {
631 cpu_model = CPU_SAMUEL2;
632 cpuname = "C3 'Samuel 2' [C5B]";
633 } else {
634 cpu_model = CPU_EZRA;
635 cpuname = "C3 'Ezra' [C5C]";
636 }
637 memcpy (clock_ratio, ezra_clock_ratio, sizeof(ezra_clock_ratio));
638 memcpy (eblcr_table, ezra_eblcr, sizeof(ezra_eblcr));
639 break;
640 }
641 break;
642
643 case 8:
644 cpu_model = CPU_EZRA_T;
645 cpuname = "C3 'Ezra-T' [C5M]";
646 longhaul_version = TYPE_POWERSAVER;
647 numscales=32;
648 memcpy (clock_ratio, ezrat_clock_ratio, sizeof(ezrat_clock_ratio));
649 memcpy (eblcr_table, ezrat_eblcr, sizeof(ezrat_eblcr));
650 break;
651
652 case 9:
653 cpu_model = CPU_NEHEMIAH;
654 longhaul_version = TYPE_POWERSAVER;
655 numscales=32;
656 switch (c->x86_mask) {
657 case 0 ... 1:
658 cpuname = "C3 'Nehemiah A' [C5N]";
659 memcpy (clock_ratio, nehemiah_a_clock_ratio, sizeof(nehemiah_a_clock_ratio));
660 memcpy (eblcr_table, nehemiah_a_eblcr, sizeof(nehemiah_a_eblcr));
661 break;
662 case 2 ... 4:
663 cpuname = "C3 'Nehemiah B' [C5N]";
664 memcpy (clock_ratio, nehemiah_b_clock_ratio, sizeof(nehemiah_b_clock_ratio));
665 memcpy (eblcr_table, nehemiah_b_eblcr, sizeof(nehemiah_b_eblcr));
666 break;
667 case 5 ... 15:
668 cpuname = "C3 'Nehemiah C' [C5N]";
669 memcpy (clock_ratio, nehemiah_c_clock_ratio, sizeof(nehemiah_c_clock_ratio));
670 memcpy (eblcr_table, nehemiah_c_eblcr, sizeof(nehemiah_c_eblcr));
671 break;
672 }
673 break;
674
675 default:
676 cpuname = "Unknown";
677 break;
678 }
679
680 printk (KERN_INFO PFX "VIA %s CPU detected. ", cpuname);
681 switch (longhaul_version) {
682 case TYPE_LONGHAUL_V1:
683 case TYPE_LONGHAUL_V2:
684 printk ("Longhaul v%d supported.\n", longhaul_version);
685 break;
686 case TYPE_POWERSAVER:
687 printk ("Powersaver supported.\n");
688 break;
689 };
690
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200691 /* Find ACPI data for processor */
692 acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT, ACPI_UINT32_MAX,
693 &longhaul_walk_callback, NULL, (void *)&pr);
694 if (pr == NULL)
695 goto err_acpi;
696
697 if (longhaul_version == TYPE_POWERSAVER) {
698 /* Check ACPI support for C3 state */
699 cx = &pr->power.states[ACPI_STATE_C3];
Rafa³ Bilskieed7d412006-09-27 08:25:27 +0200700 if (cx->address > 0 &&
701 (cx->latency <= 1000 || ignore_latency != 0) ) {
702 goto print_support_type;
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200703 }
704 }
Rafa³ Bilskieed7d412006-09-27 08:25:27 +0200705 /* Check ACPI support for bus master arbiter disable */
706 if (!pr->flags.bm_control) {
707 if (enable_arbiter_disable()) {
708 port22_en = 1;
709 } else {
710 goto err_acpi;
711 }
712 }
713print_support_type:
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200714 if (!port22_en) {
715 printk (KERN_INFO PFX "Using ACPI support.\n");
716 } else {
717 printk (KERN_INFO PFX "Using northbridge support.\n");
718 }
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 ret = longhaul_get_ranges();
721 if (ret != 0)
722 return ret;
723
724 if ((longhaul_version==TYPE_LONGHAUL_V2 || longhaul_version==TYPE_POWERSAVER) &&
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200725 (scale_voltage != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 longhaul_setup_voltagescaling();
727
728 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Dave Jones6778bae2005-05-31 19:03:51 -0700729 policy->cpuinfo.transition_latency = 200000; /* nsec */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 policy->cur = calc_speed(longhaul_get_cpu_mult());
731
732 ret = cpufreq_frequency_table_cpuinfo(policy, longhaul_table);
733 if (ret)
734 return ret;
735
736 cpufreq_frequency_table_get_attr(longhaul_table, policy->cpu);
737
738 return 0;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200739
740err_acpi:
Rafa³ Bilskidb2fb9d2006-11-30 03:47:41 +0100741 printk(KERN_ERR PFX "No ACPI support. Unsupported northbridge. Aborting.\n");
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200742 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
745static int __devexit longhaul_cpu_exit(struct cpufreq_policy *policy)
746{
747 cpufreq_frequency_table_put_attr(policy->cpu);
748 return 0;
749}
750
751static struct freq_attr* longhaul_attr[] = {
752 &cpufreq_freq_attr_scaling_available_freqs,
753 NULL,
754};
755
756static struct cpufreq_driver longhaul_driver = {
757 .verify = longhaul_verify,
758 .target = longhaul_target,
759 .get = longhaul_get,
760 .init = longhaul_cpu_init,
761 .exit = __devexit_p(longhaul_cpu_exit),
762 .name = "longhaul",
763 .owner = THIS_MODULE,
764 .attr = longhaul_attr,
765};
766
767
768static int __init longhaul_init(void)
769{
770 struct cpuinfo_x86 *c = cpu_data;
771
772 if (c->x86_vendor != X86_VENDOR_CENTAUR || c->x86 != 6)
773 return -ENODEV;
774
Rafa³ Bilski48b7bde2006-07-04 17:50:57 +0200775#ifdef CONFIG_SMP
776 if (num_online_cpus() > 1) {
777 return -ENODEV;
778 printk(KERN_ERR PFX "More than 1 CPU detected, longhaul disabled.\n");
779 }
780#endif
781#ifdef CONFIG_X86_IO_APIC
782 if (cpu_has_apic) {
783 printk(KERN_ERR PFX "APIC detected. Longhaul is currently broken in this configuration.\n");
784 return -ENODEV;
785 }
786#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 switch (c->x86_model) {
788 case 6 ... 9:
789 return cpufreq_register_driver(&longhaul_driver);
790 default:
791 printk (KERN_INFO PFX "Unknown VIA CPU. Contact davej@codemonkey.org.uk\n");
792 }
793
794 return -ENODEV;
795}
796
797
798static void __exit longhaul_exit(void)
799{
Dave Jones8eebf1a2006-05-30 17:40:16 -0400800 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 for (i=0; i < numscales; i++) {
803 if (clock_ratio[i] == maxmult) {
804 longhaul_setstate(i);
805 break;
806 }
807 }
808
809 cpufreq_unregister_driver(&longhaul_driver);
810 kfree(longhaul_table);
811}
812
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200813module_param (scale_voltage, int, 0644);
814MODULE_PARM_DESC(scale_voltage, "Scale voltage of processor");
Rafa³ Bilski65954132006-08-13 09:16:20 +0200815module_param(ignore_latency, int, 0644);
816MODULE_PARM_DESC(ignore_latency, "Skip ACPI C3 latency test");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818MODULE_AUTHOR ("Dave Jones <davej@codemonkey.org.uk>");
819MODULE_DESCRIPTION ("Longhaul driver for VIA Cyrix processors.");
820MODULE_LICENSE ("GPL");
821
Rafa³ Bilski0d6daba2006-07-07 08:48:26 +0200822late_initcall(longhaul_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823module_exit(longhaul_exit);