blob: b679aaf0c6b4048bd08dc468b61c3952cb0dbffd [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "longhaul", msg)
81
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/* Clock ratios multiplied by 10 */
84static int clock_ratio[32];
85static int eblcr_table[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static int longhaul_version;
87static struct cpufreq_frequency_table *longhaul_table;
88
89#ifdef CONFIG_CPU_FREQ_DEBUG
90static char speedbuffer[8];
91
92static char *print_speed(int speed)
93{
Dave Jonese2aa8732006-05-30 17:37:15 -040094 if (speed < 1000) {
95 snprintf(speedbuffer, sizeof(speedbuffer),"%dMHz", speed);
96 return speedbuffer;
97 }
98
99 if (speed%1000 == 0)
100 snprintf(speedbuffer, sizeof(speedbuffer),
101 "%dGHz", speed/1000);
102 else
103 snprintf(speedbuffer, sizeof(speedbuffer),
104 "%d.%dGHz", speed/1000, (speed%1000)/100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 return speedbuffer;
107}
108#endif
109
110
111static unsigned int calc_speed(int mult)
112{
113 int khz;
114 khz = (mult/10)*fsb;
115 if (mult%10)
116 khz += fsb/2;
117 khz *= 1000;
118 return khz;
119}
120
121
122static int longhaul_get_cpu_mult(void)
123{
124 unsigned long invalue=0,lo, hi;
125
126 rdmsr (MSR_IA32_EBL_CR_POWERON, lo, hi);
127 invalue = (lo & (1<<22|1<<23|1<<24|1<<25)) >>22;
128 if (longhaul_version==TYPE_LONGHAUL_V2 || longhaul_version==TYPE_POWERSAVER) {
129 if (lo & (1<<27))
130 invalue+=16;
131 }
132 return eblcr_table[invalue];
133}
134
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200135/* For processor with BCR2 MSR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200137static void do_longhaul1(unsigned int clock_ratio_index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200139 union msr_bcr2 bcr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200141 rdmsrl(MSR_VIA_BCR2, bcr2.val);
142 /* Enable software clock multiplier */
143 bcr2.bits.ESOFTBF = 1;
144 bcr2.bits.CLOCKMUL = clock_ratio_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200146 /* Sync to timer tick */
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -0700147 safe_halt();
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200148 /* Change frequency on next halt or sleep */
149 wrmsrl(MSR_VIA_BCR2, bcr2.val);
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200150 /* Invoke transition */
151 ACPI_FLUSH_CPU_CACHE();
152 halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200154 /* Disable software clock multiplier */
Dave Jones3be6a482005-05-31 19:03:51 -0700155 local_irq_disable();
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200156 rdmsrl(MSR_VIA_BCR2, bcr2.val);
157 bcr2.bits.ESOFTBF = 0;
158 wrmsrl(MSR_VIA_BCR2, bcr2.val);
159}
Dave Jones3be6a482005-05-31 19:03:51 -0700160
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200161/* For processor with Longhaul MSR */
Dave Jones11746312005-05-31 19:03:51 -0700162
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200163static void do_powersaver(int cx_address, unsigned int clock_ratio_index)
164{
165 union msr_longhaul longhaul;
166 u32 t;
Dave Jones3be6a482005-05-31 19:03:51 -0700167
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200168 rdmsrl(MSR_VIA_LONGHAUL, longhaul.val);
169 longhaul.bits.RevisionKey = longhaul.bits.RevisionID;
170 longhaul.bits.SoftBusRatio = clock_ratio_index & 0xf;
171 longhaul.bits.SoftBusRatio4 = (clock_ratio_index & 0x10) >> 4;
Rafa³ Bilskieb23c752006-07-09 21:47:04 +0200172 longhaul.bits.EnableSoftBusRatio = 1;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200173
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200174 if (can_scale_voltage) {
175 longhaul.bits.SoftVID = f_msr_table[clock_ratio_index].vrm;
176 longhaul.bits.EnableSoftVID = 1;
177 }
178
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200179 /* Sync to timer tick */
180 safe_halt();
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200181 /* Change frequency on next halt or sleep */
182 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100183 if (!cx_address) {
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200184 ACPI_FLUSH_CPU_CACHE();
185 /* Invoke C1 */
186 halt();
187 } else {
188 ACPI_FLUSH_CPU_CACHE();
189 /* Invoke C3 */
190 inb(cx_address);
191 /* Dummy op - must do something useless after P_LVL3 read */
192 t = inl(acpi_fadt.xpm_tmr_blk.address);
193 }
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200194 /* Disable bus ratio bit */
195 local_irq_disable();
196 longhaul.bits.RevisionKey = longhaul.bits.RevisionID;
197 longhaul.bits.EnableSoftBusRatio = 0;
198 longhaul.bits.EnableSoftBSEL = 0;
199 longhaul.bits.EnableSoftVID = 0;
200 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
203/**
204 * longhaul_set_cpu_frequency()
205 * @clock_ratio_index : bitpattern of the new multiplier.
206 *
207 * Sets a new clock ratio.
208 */
209
210static void longhaul_setstate(unsigned int clock_ratio_index)
211{
212 int speed, mult;
213 struct cpufreq_freqs freqs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 static unsigned int old_ratio=-1;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200215 unsigned long flags;
216 unsigned int pic1_mask, pic2_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 if (old_ratio == clock_ratio_index)
219 return;
220 old_ratio = clock_ratio_index;
221
222 mult = clock_ratio[clock_ratio_index];
223 if (mult == -1)
224 return;
225
226 speed = calc_speed(mult);
227 if ((speed > highest_speed) || (speed < lowest_speed))
228 return;
229
230 freqs.old = calc_speed(longhaul_get_cpu_mult());
231 freqs.new = speed;
232 freqs.cpu = 0; /* longhaul.c is UP only driver */
233
234 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
235
236 dprintk ("Setting to FSB:%dMHz Mult:%d.%dx (%s)\n",
237 fsb, mult/10, mult%10, print_speed(speed/1000));
238
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200239 preempt_disable();
240 local_irq_save(flags);
241
242 pic2_mask = inb(0xA1);
243 pic1_mask = inb(0x21); /* works on C3. save mask. */
244 outb(0xFF,0xA1); /* Overkill */
245 outb(0xFE,0x21); /* TMR0 only */
246
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100247 if (longhaul_flags & USE_NORTHBRIDGE) {
248 /* Disable AGP and PCI arbiters */
249 outb(3, 0x22);
250 } else if ((pr != NULL) && pr->flags.bm_control) {
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200251 /* Disable bus master arbitration */
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200252 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1,
253 ACPI_MTX_DO_NOT_LOCK);
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³ Bilski264166e2006-12-24 14:04:23 +0100281 if (longhaul_flags & USE_ACPI_C3) {
282 /* Don't allow wakeup */
283 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0,
284 ACPI_MTX_DO_NOT_LOCK);
285 do_powersaver(cx->address, clock_ratio_index);
286 } else {
287 do_powersaver(0, clock_ratio_index);
288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 break;
290 }
291
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100292 if (longhaul_flags & USE_NORTHBRIDGE) {
293 /* Enable arbiters */
294 outb(0, 0x22);
295 } else if ((pr != NULL) && pr->flags.bm_control) {
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200296 /* Enable bus master arbitration */
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200297 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0,
298 ACPI_MTX_DO_NOT_LOCK);
299 }
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200300 outb(pic2_mask,0xA1); /* restore mask */
301 outb(pic1_mask,0x21);
302
303 local_irq_restore(flags);
304 preempt_enable();
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
307}
308
309/*
310 * Centaur decided to make life a little more tricky.
311 * Only longhaul v1 is allowed to read EBLCR BSEL[0:1].
312 * Samuel2 and above have to try and guess what the FSB is.
313 * We do this by assuming we booted at maximum multiplier, and interpolate
314 * between that value multiplied by possible FSBs and cpu_mhz which
315 * was calculated at boot time. Really ugly, but no other way to do this.
316 */
317
318#define ROUNDING 0xf
319
Rafa³ Bilski24ebead2007-01-01 23:49:34 +0100320static int _guess(int guess, int mult)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 int target;
323
Rafa³ Bilski24ebead2007-01-01 23:49:34 +0100324 target = ((mult/10)*guess);
325 if (mult%10 != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 target += (guess/2);
327 target += ROUNDING/2;
328 target &= ~ROUNDING;
329 return target;
330}
331
332
Rafa³ Bilski24ebead2007-01-01 23:49:34 +0100333static int guess_fsb(int mult)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 int speed = (cpu_khz/1000);
336 int i;
Rafa³ Bilski24ebead2007-01-01 23:49:34 +0100337 int speeds[] = { 66, 100, 133, 200 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 speed += ROUNDING/2;
340 speed &= ~ROUNDING;
341
Rafa³ Bilski24ebead2007-01-01 23:49:34 +0100342 for (i=0; i<4; i++) {
343 if (_guess(speeds[i], mult) == speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return speeds[i];
345 }
346 return 0;
347}
348
349
350static int __init longhaul_get_ranges(void)
351{
352 unsigned long invalue;
Rafa³ Bilski32deb2d2006-07-15 19:31:30 +0200353 unsigned int ezra_t_multipliers[32]= {
354 90, 30, 40, 100, 55, 35, 45, 95,
355 50, 70, 80, 60, 120, 75, 85, 65,
356 -1, 110, 120, -1, 135, 115, 125, 105,
357 130, 150, 160, 140, -1, 155, -1, 145 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 unsigned int j, k = 0;
359 union msr_longhaul longhaul;
Dave Jones43c8f122007-01-02 23:42:16 -0500360 int mult = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 switch (longhaul_version) {
363 case TYPE_LONGHAUL_V1:
364 case TYPE_LONGHAUL_V2:
365 /* Ugh, Longhaul v1 didn't have the min/max MSRs.
366 Assume min=3.0x & max = whatever we booted at. */
367 minmult = 30;
Rafa³ Bilski24ebead2007-01-01 23:49:34 +0100368 maxmult = mult = longhaul_get_cpu_mult();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 break;
370
371 case TYPE_POWERSAVER:
372 /* Ezra-T */
373 if (cpu_model==CPU_EZRA_T) {
Rafa³ Bilski24ebead2007-01-01 23:49:34 +0100374 minmult = 30;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 rdmsrl (MSR_VIA_LONGHAUL, longhaul.val);
376 invalue = longhaul.bits.MaxMHzBR;
377 if (longhaul.bits.MaxMHzBR4)
378 invalue += 16;
Rafa³ Bilski24ebead2007-01-01 23:49:34 +0100379 maxmult = mult = ezra_t_multipliers[invalue];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 break;
381 }
382
383 /* Nehemiah */
384 if (cpu_model==CPU_NEHEMIAH) {
385 rdmsrl (MSR_VIA_LONGHAUL, longhaul.val);
386
387 /*
388 * TODO: This code works, but raises a lot of questions.
389 * - Some Nehemiah's seem to have broken Min/MaxMHzBR's.
390 * We get around this by using a hardcoded multiplier of 4.0x
391 * for the minimimum speed, and the speed we booted up at for the max.
392 * This is done in longhaul_get_cpu_mult() by reading the EBLCR register.
393 * - According to some VIA documentation EBLCR is only
394 * in pre-Nehemiah C3s. How this still works is a mystery.
395 * We're possibly using something undocumented and unsupported,
396 * But it works, so we don't grumble.
397 */
398 minmult=40;
Rafa³ Bilski24ebead2007-01-01 23:49:34 +0100399 maxmult = mult = longhaul_get_cpu_mult();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 break;
401 }
402 }
Rafa³ Bilski24ebead2007-01-01 23:49:34 +0100403 fsb = guess_fsb(mult);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 dprintk ("MinMult:%d.%dx MaxMult:%d.%dx\n",
406 minmult/10, minmult%10, maxmult/10, maxmult%10);
407
Rafa³ Bilski24ebead2007-01-01 23:49:34 +0100408 if (fsb == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 printk (KERN_INFO PFX "Invalid (reserved) FSB!\n");
410 return -EINVAL;
411 }
412
413 highest_speed = calc_speed(maxmult);
414 lowest_speed = calc_speed(minmult);
415 dprintk ("FSB:%dMHz Lowest speed: %s Highest speed:%s\n", fsb,
416 print_speed(lowest_speed/1000),
417 print_speed(highest_speed/1000));
418
419 if (lowest_speed == highest_speed) {
420 printk (KERN_INFO PFX "highestspeed == lowest, aborting.\n");
421 return -EINVAL;
422 }
423 if (lowest_speed > highest_speed) {
424 printk (KERN_INFO PFX "nonsense! lowest (%d > %d) !\n",
425 lowest_speed, highest_speed);
426 return -EINVAL;
427 }
428
429 longhaul_table = kmalloc((numscales + 1) * sizeof(struct cpufreq_frequency_table), GFP_KERNEL);
430 if(!longhaul_table)
431 return -ENOMEM;
432
433 for (j=0; j < numscales; j++) {
434 unsigned int ratio;
435 ratio = clock_ratio[j];
436 if (ratio == -1)
437 continue;
438 if (ratio > maxmult || ratio < minmult)
439 continue;
440 longhaul_table[k].frequency = calc_speed(ratio);
441 longhaul_table[k].index = j;
442 k++;
443 }
444
445 longhaul_table[k].frequency = CPUFREQ_TABLE_END;
446 if (!k) {
447 kfree (longhaul_table);
448 return -EINVAL;
449 }
450
451 return 0;
452}
453
454
455static void __init longhaul_setup_voltagescaling(void)
456{
457 union msr_longhaul longhaul;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200458 struct mV_pos minvid, maxvid;
459 unsigned int j, speed, pos, kHz_step, numvscales;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200461 rdmsrl(MSR_VIA_LONGHAUL, longhaul.val);
462 if (!(longhaul.bits.RevisionID & 1)) {
463 printk(KERN_INFO PFX "Voltage scaling not supported by CPU.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200467 if (!longhaul.bits.VRMRev) {
468 printk (KERN_INFO PFX "VRM 8.5\n");
469 vrm_mV_table = &vrm85_mV[0];
470 mV_vrm_table = &mV_vrm85[0];
471 } else {
472 printk (KERN_INFO PFX "Mobile VRM\n");
473 vrm_mV_table = &mobilevrm_mV[0];
474 mV_vrm_table = &mV_mobilevrm[0];
475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200477 minvid = vrm_mV_table[longhaul.bits.MinimumVID];
478 maxvid = vrm_mV_table[longhaul.bits.MaximumVID];
479 numvscales = maxvid.pos - minvid.pos + 1;
480 kHz_step = (highest_speed - lowest_speed) / numvscales;
481
482 if (minvid.mV == 0 || maxvid.mV == 0 || minvid.mV > maxvid.mV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 printk (KERN_INFO PFX "Bogus values Min:%d.%03d Max:%d.%03d. "
484 "Voltage scaling disabled.\n",
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200485 minvid.mV/1000, minvid.mV%1000, maxvid.mV/1000, maxvid.mV%1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return;
487 }
488
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200489 if (minvid.mV == maxvid.mV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 printk (KERN_INFO PFX "Claims to support voltage scaling but min & max are "
491 "both %d.%03d. Voltage scaling disabled\n",
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200492 maxvid.mV/1000, maxvid.mV%1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return;
494 }
495
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200496 printk(KERN_INFO PFX "Max VID=%d.%03d Min VID=%d.%03d, %d possible voltage scales\n",
497 maxvid.mV/1000, maxvid.mV%1000,
498 minvid.mV/1000, minvid.mV%1000,
499 numvscales);
500
501 j = 0;
502 while (longhaul_table[j].frequency != CPUFREQ_TABLE_END) {
503 speed = longhaul_table[j].frequency;
504 pos = (speed - lowest_speed) / kHz_step + minvid.pos;
505 f_msr_table[longhaul_table[j].index].vrm = mV_vrm_table[pos];
506 j++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 can_scale_voltage = 1;
510}
511
512
513static int longhaul_verify(struct cpufreq_policy *policy)
514{
515 return cpufreq_frequency_table_verify(policy, longhaul_table);
516}
517
518
519static int longhaul_target(struct cpufreq_policy *policy,
520 unsigned int target_freq, unsigned int relation)
521{
522 unsigned int table_index = 0;
523 unsigned int new_clock_ratio = 0;
524
525 if (cpufreq_frequency_table_target(policy, longhaul_table, target_freq, relation, &table_index))
526 return -EINVAL;
527
528 new_clock_ratio = longhaul_table[table_index].index & 0xFF;
529
530 longhaul_setstate(new_clock_ratio);
531
532 return 0;
533}
534
535
536static unsigned int longhaul_get(unsigned int cpu)
537{
538 if (cpu)
539 return 0;
540 return calc_speed(longhaul_get_cpu_mult());
541}
542
Adrian Bunkc4a96c12006-07-09 19:53:08 +0200543static acpi_status longhaul_walk_callback(acpi_handle obj_handle,
544 u32 nesting_level,
545 void *context, void **return_value)
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200546{
547 struct acpi_device *d;
548
549 if ( acpi_bus_get_device(obj_handle, &d) ) {
550 return 0;
551 }
552 *return_value = (void *)acpi_driver_data(d);
553 return 1;
554}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200556/* VIA don't support PM2 reg, but have something similar */
557static int enable_arbiter_disable(void)
558{
559 struct pci_dev *dev;
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200560 int reg;
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200561 u8 pci_cmd;
562
563 /* Find PLE133 host bridge */
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200564 reg = 0x78;
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200565 dev = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8601_0, NULL);
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200566 /* Find CLE266 host bridge */
567 if (dev == NULL) {
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200568 reg = 0x76;
Rafa³ Bilskieed7d412006-09-27 08:25:27 +0200569 dev = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_862X_0, NULL);
Rafa³ Bilskidb2fb9d2006-11-30 03:47:41 +0100570 /* Find CN400 V-Link host bridge */
571 if (dev == NULL)
572 dev = pci_find_device(PCI_VENDOR_ID_VIA, 0x7259, NULL);
573
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200574 }
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200575 if (dev != NULL) {
576 /* Enable access to port 0x22 */
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200577 pci_read_config_byte(dev, reg, &pci_cmd);
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200578 if ( !(pci_cmd & 1<<7) ) {
579 pci_cmd |= 1<<7;
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200580 pci_write_config_byte(dev, reg, pci_cmd);
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200581 }
582 return 1;
583 }
584 return 0;
585}
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587static int __init longhaul_cpu_init(struct cpufreq_policy *policy)
588{
589 struct cpuinfo_x86 *c = cpu_data;
590 char *cpuname=NULL;
591 int ret;
592
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200593 /* Check what we have on this motherboard */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 switch (c->x86_model) {
595 case 6:
596 cpu_model = CPU_SAMUEL;
597 cpuname = "C3 'Samuel' [C5A]";
598 longhaul_version = TYPE_LONGHAUL_V1;
599 memcpy (clock_ratio, samuel1_clock_ratio, sizeof(samuel1_clock_ratio));
600 memcpy (eblcr_table, samuel1_eblcr, sizeof(samuel1_eblcr));
601 break;
602
603 case 7:
604 longhaul_version = TYPE_LONGHAUL_V1;
605 switch (c->x86_mask) {
606 case 0:
607 cpu_model = CPU_SAMUEL2;
608 cpuname = "C3 'Samuel 2' [C5B]";
609 /* Note, this is not a typo, early Samuel2's had Samuel1 ratios. */
610 memcpy (clock_ratio, samuel1_clock_ratio, sizeof(samuel1_clock_ratio));
611 memcpy (eblcr_table, samuel2_eblcr, sizeof(samuel2_eblcr));
612 break;
613 case 1 ... 15:
614 if (c->x86_mask < 8) {
615 cpu_model = CPU_SAMUEL2;
616 cpuname = "C3 'Samuel 2' [C5B]";
617 } else {
618 cpu_model = CPU_EZRA;
619 cpuname = "C3 'Ezra' [C5C]";
620 }
621 memcpy (clock_ratio, ezra_clock_ratio, sizeof(ezra_clock_ratio));
622 memcpy (eblcr_table, ezra_eblcr, sizeof(ezra_eblcr));
623 break;
624 }
625 break;
626
627 case 8:
628 cpu_model = CPU_EZRA_T;
629 cpuname = "C3 'Ezra-T' [C5M]";
630 longhaul_version = TYPE_POWERSAVER;
631 numscales=32;
632 memcpy (clock_ratio, ezrat_clock_ratio, sizeof(ezrat_clock_ratio));
633 memcpy (eblcr_table, ezrat_eblcr, sizeof(ezrat_eblcr));
634 break;
635
636 case 9:
637 cpu_model = CPU_NEHEMIAH;
638 longhaul_version = TYPE_POWERSAVER;
639 numscales=32;
640 switch (c->x86_mask) {
641 case 0 ... 1:
642 cpuname = "C3 'Nehemiah A' [C5N]";
643 memcpy (clock_ratio, nehemiah_a_clock_ratio, sizeof(nehemiah_a_clock_ratio));
644 memcpy (eblcr_table, nehemiah_a_eblcr, sizeof(nehemiah_a_eblcr));
645 break;
646 case 2 ... 4:
647 cpuname = "C3 'Nehemiah B' [C5N]";
648 memcpy (clock_ratio, nehemiah_b_clock_ratio, sizeof(nehemiah_b_clock_ratio));
649 memcpy (eblcr_table, nehemiah_b_eblcr, sizeof(nehemiah_b_eblcr));
650 break;
651 case 5 ... 15:
652 cpuname = "C3 'Nehemiah C' [C5N]";
653 memcpy (clock_ratio, nehemiah_c_clock_ratio, sizeof(nehemiah_c_clock_ratio));
654 memcpy (eblcr_table, nehemiah_c_eblcr, sizeof(nehemiah_c_eblcr));
655 break;
656 }
657 break;
658
659 default:
660 cpuname = "Unknown";
661 break;
662 }
663
664 printk (KERN_INFO PFX "VIA %s CPU detected. ", cpuname);
665 switch (longhaul_version) {
666 case TYPE_LONGHAUL_V1:
667 case TYPE_LONGHAUL_V2:
668 printk ("Longhaul v%d supported.\n", longhaul_version);
669 break;
670 case TYPE_POWERSAVER:
671 printk ("Powersaver supported.\n");
672 break;
673 };
674
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200675 /* Find ACPI data for processor */
676 acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT, ACPI_UINT32_MAX,
677 &longhaul_walk_callback, NULL, (void *)&pr);
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200678
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100679 /* Check ACPI support for C3 state */
680 if ((pr != NULL) && (longhaul_version == TYPE_POWERSAVER)) {
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200681 cx = &pr->power.states[ACPI_STATE_C3];
Rafa³ Bilski14796722007-01-19 22:28:22 +0100682 if (cx->address > 0 && cx->latency <= 1000) {
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100683 longhaul_flags |= USE_ACPI_C3;
Rafa³ Bilskieed7d412006-09-27 08:25:27 +0200684 goto print_support_type;
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200685 }
686 }
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100687 /* Check if northbridge is friendly */
688 if (enable_arbiter_disable()) {
689 longhaul_flags |= USE_NORTHBRIDGE;
690 goto print_support_type;
Rafa³ Bilskieed7d412006-09-27 08:25:27 +0200691 }
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100692
693 /* No ACPI C3 or we can't use it */
694 /* 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³ Bilski264166e2006-12-24 14:04:23 +0100702 if (!(longhaul_flags & USE_NORTHBRIDGE)) {
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200703 printk (KERN_INFO PFX "Using ACPI support.\n");
704 } else {
705 printk (KERN_INFO PFX "Using northbridge support.\n");
706 }
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 ret = longhaul_get_ranges();
709 if (ret != 0)
710 return ret;
711
712 if ((longhaul_version==TYPE_LONGHAUL_V2 || longhaul_version==TYPE_POWERSAVER) &&
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200713 (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);