blob: fa5cac255c16e32f0e2e19562b92ef8874fa740b [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 {
Rafa³ Bilski348f31e2007-02-08 18:56:04 +010068 u8 vrm;
69 u8 pos;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +020070};
71static struct f_msr f_msr_table[32];
72
73static unsigned int highest_speed, lowest_speed; /* kHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static unsigned int minmult, maxmult;
75static int can_scale_voltage;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +020076static struct acpi_processor *pr = NULL;
77static struct acpi_processor_cx *cx = NULL;
Rafa³ Bilski264166e2006-12-24 14:04:23 +010078static u8 longhaul_flags;
Rafa³ Bilski348f31e2007-02-08 18:56:04 +010079static u8 longhaul_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/* Module parameters */
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +020082static int scale_voltage;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "longhaul", msg)
85
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/* Clock ratios multiplied by 10 */
88static int clock_ratio[32];
89static int eblcr_table[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int longhaul_version;
91static struct cpufreq_frequency_table *longhaul_table;
92
93#ifdef CONFIG_CPU_FREQ_DEBUG
94static char speedbuffer[8];
95
96static char *print_speed(int speed)
97{
Dave Jonese2aa8732006-05-30 17:37:15 -040098 if (speed < 1000) {
99 snprintf(speedbuffer, sizeof(speedbuffer),"%dMHz", speed);
100 return speedbuffer;
101 }
102
103 if (speed%1000 == 0)
104 snprintf(speedbuffer, sizeof(speedbuffer),
105 "%dGHz", speed/1000);
106 else
107 snprintf(speedbuffer, sizeof(speedbuffer),
108 "%d.%dGHz", speed/1000, (speed%1000)/100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 return speedbuffer;
111}
112#endif
113
114
115static unsigned int calc_speed(int mult)
116{
117 int khz;
118 khz = (mult/10)*fsb;
119 if (mult%10)
120 khz += fsb/2;
121 khz *= 1000;
122 return khz;
123}
124
125
126static int longhaul_get_cpu_mult(void)
127{
128 unsigned long invalue=0,lo, hi;
129
130 rdmsr (MSR_IA32_EBL_CR_POWERON, lo, hi);
131 invalue = (lo & (1<<22|1<<23|1<<24|1<<25)) >>22;
132 if (longhaul_version==TYPE_LONGHAUL_V2 || longhaul_version==TYPE_POWERSAVER) {
133 if (lo & (1<<27))
134 invalue+=16;
135 }
136 return eblcr_table[invalue];
137}
138
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200139/* For processor with BCR2 MSR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200141static void do_longhaul1(unsigned int clock_ratio_index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200143 union msr_bcr2 bcr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200145 rdmsrl(MSR_VIA_BCR2, bcr2.val);
146 /* Enable software clock multiplier */
147 bcr2.bits.ESOFTBF = 1;
148 bcr2.bits.CLOCKMUL = clock_ratio_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200150 /* Sync to timer tick */
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -0700151 safe_halt();
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200152 /* Change frequency on next halt or sleep */
153 wrmsrl(MSR_VIA_BCR2, bcr2.val);
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200154 /* Invoke transition */
155 ACPI_FLUSH_CPU_CACHE();
156 halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200158 /* Disable software clock multiplier */
Dave Jones3be6a482005-05-31 19:03:51 -0700159 local_irq_disable();
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200160 rdmsrl(MSR_VIA_BCR2, bcr2.val);
161 bcr2.bits.ESOFTBF = 0;
162 wrmsrl(MSR_VIA_BCR2, bcr2.val);
163}
Dave Jones3be6a482005-05-31 19:03:51 -0700164
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200165/* For processor with Longhaul MSR */
Dave Jones11746312005-05-31 19:03:51 -0700166
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200167static void do_powersaver(int cx_address, unsigned int clock_ratio_index)
168{
169 union msr_longhaul longhaul;
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100170 u8 dest_pos;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200171 u32 t;
Dave Jones3be6a482005-05-31 19:03:51 -0700172
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100173 dest_pos = f_msr_table[clock_ratio_index].pos;
174
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200175 rdmsrl(MSR_VIA_LONGHAUL, longhaul.val);
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100176 /* Setup new frequency */
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200177 longhaul.bits.RevisionKey = longhaul.bits.RevisionID;
178 longhaul.bits.SoftBusRatio = clock_ratio_index & 0xf;
179 longhaul.bits.SoftBusRatio4 = (clock_ratio_index & 0x10) >> 4;
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100180 /* Setup new voltage */
181 if (can_scale_voltage)
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200182 longhaul.bits.SoftVID = f_msr_table[clock_ratio_index].vrm;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200183 /* Sync to timer tick */
184 safe_halt();
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100185 /* Raise voltage if necessary */
186 if (can_scale_voltage && longhaul_pos < dest_pos) {
187 longhaul.bits.EnableSoftVID = 1;
188 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
189 /* Change voltage */
190 if (!cx_address) {
191 ACPI_FLUSH_CPU_CACHE();
192 halt();
193 } else {
194 ACPI_FLUSH_CPU_CACHE();
195 /* Invoke C3 */
196 inb(cx_address);
197 /* Dummy op - must do something useless after P_LVL3
198 * read */
Dave Jonesbd0561c2007-02-10 20:36:29 -0500199 t = inl(acpi_gbl_FADT.xpm_timer_block.address);
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100200 }
201 longhaul.bits.EnableSoftVID = 0;
202 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
203 longhaul_pos = dest_pos;
204 }
205
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200206 /* Change frequency on next halt or sleep */
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100207 longhaul.bits.EnableSoftBusRatio = 1;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200208 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100209 if (!cx_address) {
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200210 ACPI_FLUSH_CPU_CACHE();
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200211 halt();
212 } else {
213 ACPI_FLUSH_CPU_CACHE();
214 /* Invoke C3 */
215 inb(cx_address);
216 /* Dummy op - must do something useless after P_LVL3 read */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300217 t = inl(acpi_gbl_FADT.xpm_timer_block.address);
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200218 }
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200219 /* Disable bus ratio bit */
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200220 longhaul.bits.EnableSoftBusRatio = 0;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200221 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100222
223 /* Reduce voltage if necessary */
224 if (can_scale_voltage && longhaul_pos > dest_pos) {
225 longhaul.bits.EnableSoftVID = 1;
226 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
227 /* Change voltage */
228 if (!cx_address) {
229 ACPI_FLUSH_CPU_CACHE();
230 halt();
231 } else {
232 ACPI_FLUSH_CPU_CACHE();
233 /* Invoke C3 */
234 inb(cx_address);
235 /* Dummy op - must do something useless after P_LVL3
236 * read */
Dave Jonesbd0561c2007-02-10 20:36:29 -0500237 t = inl(acpi_gbl_FADT.xpm_timer_block.address);
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100238 }
239 longhaul.bits.EnableSoftVID = 0;
240 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
241 longhaul_pos = dest_pos;
242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
245/**
246 * longhaul_set_cpu_frequency()
247 * @clock_ratio_index : bitpattern of the new multiplier.
248 *
249 * Sets a new clock ratio.
250 */
251
252static void longhaul_setstate(unsigned int clock_ratio_index)
253{
254 int speed, mult;
255 struct cpufreq_freqs freqs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 static unsigned int old_ratio=-1;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200257 unsigned long flags;
258 unsigned int pic1_mask, pic2_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 if (old_ratio == clock_ratio_index)
261 return;
262 old_ratio = clock_ratio_index;
263
264 mult = clock_ratio[clock_ratio_index];
265 if (mult == -1)
266 return;
267
268 speed = calc_speed(mult);
269 if ((speed > highest_speed) || (speed < lowest_speed))
270 return;
271
272 freqs.old = calc_speed(longhaul_get_cpu_mult());
273 freqs.new = speed;
274 freqs.cpu = 0; /* longhaul.c is UP only driver */
275
276 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
277
278 dprintk ("Setting to FSB:%dMHz Mult:%d.%dx (%s)\n",
279 fsb, mult/10, mult%10, print_speed(speed/1000));
280
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200281 preempt_disable();
282 local_irq_save(flags);
283
284 pic2_mask = inb(0xA1);
285 pic1_mask = inb(0x21); /* works on C3. save mask. */
286 outb(0xFF,0xA1); /* Overkill */
287 outb(0xFE,0x21); /* TMR0 only */
288
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100289 if (longhaul_flags & USE_NORTHBRIDGE) {
290 /* Disable AGP and PCI arbiters */
291 outb(3, 0x22);
292 } else if ((pr != NULL) && pr->flags.bm_control) {
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200293 /* Disable bus master arbitration */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300294 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1);
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 switch (longhaul_version) {
297
298 /*
299 * Longhaul v1. (Samuel[C5A] and Samuel2 stepping 0[C5B])
300 * Software controlled multipliers only.
301 *
302 * *NB* Until we get voltage scaling working v1 & v2 are the same code.
303 * Longhaul v2 appears in Samuel2 Steppings 1->7 [C5b] and Ezra [C5C]
304 */
305 case TYPE_LONGHAUL_V1:
306 case TYPE_LONGHAUL_V2:
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200307 do_longhaul1(clock_ratio_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 break;
309
310 /*
311 * Longhaul v3 (aka Powersaver). (Ezra-T [C5M] & Nehemiah [C5N])
312 * We can scale voltage with this too, but that's currently
313 * disabled until we come up with a decent 'match freq to voltage'
314 * algorithm.
315 * When we add voltage scaling, we will also need to do the
316 * voltage/freq setting in order depending on the direction
317 * of scaling (like we do in powernow-k7.c)
318 * Nehemiah can do FSB scaling too, but this has never been proven
319 * to work in practice.
320 */
321 case TYPE_POWERSAVER:
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100322 if (longhaul_flags & USE_ACPI_C3) {
323 /* Don't allow wakeup */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300324 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100325 do_powersaver(cx->address, clock_ratio_index);
326 } else {
327 do_powersaver(0, clock_ratio_index);
328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 break;
330 }
331
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100332 if (longhaul_flags & USE_NORTHBRIDGE) {
333 /* Enable arbiters */
334 outb(0, 0x22);
335 } else if ((pr != NULL) && pr->flags.bm_control) {
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200336 /* Enable bus master arbitration */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300337 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0);
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200338 }
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200339 outb(pic2_mask,0xA1); /* restore mask */
340 outb(pic1_mask,0x21);
341
342 local_irq_restore(flags);
343 preempt_enable();
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
346}
347
348/*
349 * Centaur decided to make life a little more tricky.
350 * Only longhaul v1 is allowed to read EBLCR BSEL[0:1].
351 * Samuel2 and above have to try and guess what the FSB is.
352 * We do this by assuming we booted at maximum multiplier, and interpolate
353 * between that value multiplied by possible FSBs and cpu_mhz which
354 * was calculated at boot time. Really ugly, but no other way to do this.
355 */
356
357#define ROUNDING 0xf
358
Rafa³ Bilski24ebead2007-01-01 23:49:34 +0100359static int guess_fsb(int mult)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Rafa³ Bilski46ef9552007-02-04 15:58:46 +0100361 int speed = cpu_khz / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 int i;
Rafa³ Bilski46ef9552007-02-04 15:58:46 +0100363 int speeds[] = { 666, 1000, 1333, 2000 };
364 int f_max, f_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Rafa³ Bilski46ef9552007-02-04 15:58:46 +0100366 for (i = 0; i < 4; i++) {
367 f_max = ((speeds[i] * mult) + 50) / 100;
368 f_max += (ROUNDING / 2);
369 f_min = f_max - ROUNDING;
370 if ((speed <= f_max) && (speed >= f_min))
371 return speeds[i] / 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373 return 0;
374}
375
376
377static int __init longhaul_get_ranges(void)
378{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 unsigned int j, k = 0;
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100380 int mult;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100382 /* Get current frequency */
383 mult = longhaul_get_cpu_mult();
384 if (mult == -1) {
385 printk(KERN_INFO PFX "Invalid (reserved) multiplier!\n");
386 return -EINVAL;
387 }
388 fsb = guess_fsb(mult);
389 if (fsb == 0) {
390 printk(KERN_INFO PFX "Invalid (reserved) FSB!\n");
391 return -EINVAL;
392 }
393 /* Get max multiplier - as we always did.
394 * Longhaul MSR is usefull only when voltage scaling is enabled.
395 * C3 is booting at max anyway. */
396 maxmult = mult;
397 /* Get min multiplier */
Rafa³ Bilski9addf3b2007-02-07 22:53:29 +0100398 switch (cpu_model) {
399 case CPU_NEHEMIAH:
400 minmult = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 break;
Rafa³ Bilski9addf3b2007-02-07 22:53:29 +0100402 case CPU_NEHEMIAH_C:
403 minmult = 40;
404 break;
405 default:
406 minmult = 30;
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100407 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409
410 dprintk ("MinMult:%d.%dx MaxMult:%d.%dx\n",
411 minmult/10, minmult%10, maxmult/10, maxmult%10);
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 highest_speed = calc_speed(maxmult);
414 lowest_speed = calc_speed(minmult);
415 dprintk ("FSB:%dMHz Lowest speed: %s Highest speed:%s\n", fsb,
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300416 print_speed(lowest_speed/1000),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 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;
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100460 int min_vid_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200462 rdmsrl(MSR_VIA_LONGHAUL, longhaul.val);
463 if (!(longhaul.bits.RevisionID & 1)) {
464 printk(KERN_INFO PFX "Voltage scaling not supported by CPU.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200468 if (!longhaul.bits.VRMRev) {
469 printk (KERN_INFO PFX "VRM 8.5\n");
470 vrm_mV_table = &vrm85_mV[0];
471 mV_vrm_table = &mV_vrm85[0];
472 } else {
473 printk (KERN_INFO PFX "Mobile VRM\n");
474 vrm_mV_table = &mobilevrm_mV[0];
475 mV_vrm_table = &mV_mobilevrm[0];
476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200478 minvid = vrm_mV_table[longhaul.bits.MinimumVID];
479 maxvid = vrm_mV_table[longhaul.bits.MaximumVID];
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200480
481 if (minvid.mV == 0 || maxvid.mV == 0 || minvid.mV > maxvid.mV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 printk (KERN_INFO PFX "Bogus values Min:%d.%03d Max:%d.%03d. "
483 "Voltage scaling disabled.\n",
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200484 minvid.mV/1000, minvid.mV%1000, maxvid.mV/1000, maxvid.mV%1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return;
486 }
487
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200488 if (minvid.mV == maxvid.mV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 printk (KERN_INFO PFX "Claims to support voltage scaling but min & max are "
490 "both %d.%03d. Voltage scaling disabled\n",
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200491 maxvid.mV/1000, maxvid.mV%1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return;
493 }
494
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100495 /* How many voltage steps */
496 numvscales = maxvid.pos - minvid.pos + 1;
497 printk(KERN_INFO PFX
498 "Max VID=%d.%03d "
499 "Min VID=%d.%03d, "
500 "%d possible voltage scales\n",
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200501 maxvid.mV/1000, maxvid.mV%1000,
502 minvid.mV/1000, minvid.mV%1000,
503 numvscales);
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100504
505 /* Calculate max frequency at min voltage */
506 j = longhaul.bits.MinMHzBR;
507 if (longhaul.bits.MinMHzBR4)
508 j += 16;
509 min_vid_speed = eblcr_table[j];
510 if (min_vid_speed == -1)
511 return;
512 switch (longhaul.bits.MinMHzFSB) {
513 case 0:
514 min_vid_speed *= 13333;
515 break;
516 case 1:
517 min_vid_speed *= 10000;
518 break;
519 case 3:
520 min_vid_speed *= 6666;
521 break;
522 default:
523 return;
524 break;
525 }
526 if (min_vid_speed >= highest_speed)
527 return;
528 /* Calculate kHz for one voltage step */
529 kHz_step = (highest_speed - min_vid_speed) / numvscales;
530
Dave Jonesbd0561c2007-02-10 20:36:29 -0500531
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200532 j = 0;
533 while (longhaul_table[j].frequency != CPUFREQ_TABLE_END) {
534 speed = longhaul_table[j].frequency;
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100535 if (speed > min_vid_speed)
536 pos = (speed - min_vid_speed) / kHz_step + minvid.pos;
537 else
538 pos = minvid.pos;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200539 f_msr_table[longhaul_table[j].index].vrm = mV_vrm_table[pos];
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100540 f_msr_table[longhaul_table[j].index].pos = pos;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200541 j++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100544 longhaul_pos = maxvid.pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 can_scale_voltage = 1;
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100546 printk(KERN_INFO PFX "Voltage scaling enabled. "
547 "Use of \"conservative\" governor is highly recommended.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
550
551static int longhaul_verify(struct cpufreq_policy *policy)
552{
553 return cpufreq_frequency_table_verify(policy, longhaul_table);
554}
555
556
557static int longhaul_target(struct cpufreq_policy *policy,
558 unsigned int target_freq, unsigned int relation)
559{
560 unsigned int table_index = 0;
561 unsigned int new_clock_ratio = 0;
562
563 if (cpufreq_frequency_table_target(policy, longhaul_table, target_freq, relation, &table_index))
564 return -EINVAL;
565
566 new_clock_ratio = longhaul_table[table_index].index & 0xFF;
567
568 longhaul_setstate(new_clock_ratio);
569
570 return 0;
571}
572
573
574static unsigned int longhaul_get(unsigned int cpu)
575{
576 if (cpu)
577 return 0;
578 return calc_speed(longhaul_get_cpu_mult());
579}
580
Adrian Bunkc4a96c12006-07-09 19:53:08 +0200581static acpi_status longhaul_walk_callback(acpi_handle obj_handle,
582 u32 nesting_level,
583 void *context, void **return_value)
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200584{
585 struct acpi_device *d;
586
587 if ( acpi_bus_get_device(obj_handle, &d) ) {
588 return 0;
589 }
590 *return_value = (void *)acpi_driver_data(d);
591 return 1;
592}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200594/* VIA don't support PM2 reg, but have something similar */
595static int enable_arbiter_disable(void)
596{
597 struct pci_dev *dev;
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200598 int reg;
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200599 u8 pci_cmd;
600
601 /* Find PLE133 host bridge */
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200602 reg = 0x78;
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200603 dev = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8601_0, NULL);
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200604 /* Find CLE266 host bridge */
605 if (dev == NULL) {
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200606 reg = 0x76;
Rafa³ Bilskieed7d412006-09-27 08:25:27 +0200607 dev = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_862X_0, NULL);
Rafa³ Bilskidb2fb9d2006-11-30 03:47:41 +0100608 /* Find CN400 V-Link host bridge */
609 if (dev == NULL)
610 dev = pci_find_device(PCI_VENDOR_ID_VIA, 0x7259, NULL);
611
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200612 }
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200613 if (dev != NULL) {
614 /* Enable access to port 0x22 */
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200615 pci_read_config_byte(dev, reg, &pci_cmd);
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100616 if (!(pci_cmd & 1<<7)) {
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200617 pci_cmd |= 1<<7;
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200618 pci_write_config_byte(dev, reg, pci_cmd);
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100619 pci_read_config_byte(dev, reg, &pci_cmd);
620 if (!(pci_cmd & 1<<7)) {
621 printk(KERN_ERR PFX
622 "Can't enable access to port 0x22.\n");
623 return 0;
624 }
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200625 }
626 return 1;
627 }
628 return 0;
629}
630
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100631static int longhaul_setup_vt8235(void)
632{
633 struct pci_dev *dev;
634 u8 pci_cmd;
635
636 /* Find VT8235 southbridge */
637 dev = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8235, NULL);
638 if (dev != NULL) {
639 /* Set transition time to max */
640 pci_read_config_byte(dev, 0xec, &pci_cmd);
641 pci_cmd &= ~(1 << 2);
642 pci_write_config_byte(dev, 0xec, pci_cmd);
643 pci_read_config_byte(dev, 0xe4, &pci_cmd);
644 pci_cmd &= ~(1 << 7);
645 pci_write_config_byte(dev, 0xe4, pci_cmd);
646 pci_read_config_byte(dev, 0xe5, &pci_cmd);
647 pci_cmd |= 1 << 7;
648 pci_write_config_byte(dev, 0xe5, pci_cmd);
649 return 1;
650 }
651 return 0;
652}
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654static int __init longhaul_cpu_init(struct cpufreq_policy *policy)
655{
656 struct cpuinfo_x86 *c = cpu_data;
657 char *cpuname=NULL;
658 int ret;
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100659 int vt8235_present;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200661 /* Check what we have on this motherboard */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 switch (c->x86_model) {
663 case 6:
664 cpu_model = CPU_SAMUEL;
665 cpuname = "C3 'Samuel' [C5A]";
666 longhaul_version = TYPE_LONGHAUL_V1;
667 memcpy (clock_ratio, samuel1_clock_ratio, sizeof(samuel1_clock_ratio));
668 memcpy (eblcr_table, samuel1_eblcr, sizeof(samuel1_eblcr));
669 break;
670
671 case 7:
672 longhaul_version = TYPE_LONGHAUL_V1;
673 switch (c->x86_mask) {
674 case 0:
675 cpu_model = CPU_SAMUEL2;
676 cpuname = "C3 'Samuel 2' [C5B]";
677 /* Note, this is not a typo, early Samuel2's had Samuel1 ratios. */
678 memcpy (clock_ratio, samuel1_clock_ratio, sizeof(samuel1_clock_ratio));
679 memcpy (eblcr_table, samuel2_eblcr, sizeof(samuel2_eblcr));
680 break;
681 case 1 ... 15:
682 if (c->x86_mask < 8) {
683 cpu_model = CPU_SAMUEL2;
684 cpuname = "C3 'Samuel 2' [C5B]";
685 } else {
686 cpu_model = CPU_EZRA;
687 cpuname = "C3 'Ezra' [C5C]";
688 }
689 memcpy (clock_ratio, ezra_clock_ratio, sizeof(ezra_clock_ratio));
690 memcpy (eblcr_table, ezra_eblcr, sizeof(ezra_eblcr));
691 break;
692 }
693 break;
694
695 case 8:
696 cpu_model = CPU_EZRA_T;
697 cpuname = "C3 'Ezra-T' [C5M]";
698 longhaul_version = TYPE_POWERSAVER;
699 numscales=32;
700 memcpy (clock_ratio, ezrat_clock_ratio, sizeof(ezrat_clock_ratio));
701 memcpy (eblcr_table, ezrat_eblcr, sizeof(ezrat_eblcr));
702 break;
703
704 case 9:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 longhaul_version = TYPE_POWERSAVER;
Rafa³ Bilski0d44b2b2007-01-31 23:50:49 +0100706 numscales = 32;
707 memcpy(clock_ratio,
708 nehemiah_clock_ratio,
709 sizeof(nehemiah_clock_ratio));
710 memcpy(eblcr_table, nehemiah_eblcr, sizeof(nehemiah_eblcr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 switch (c->x86_mask) {
712 case 0 ... 1:
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100713 cpu_model = CPU_NEHEMIAH;
Rafa³ Bilskie57501c2007-02-08 23:12:02 +0100714 cpuname = "C3 'Nehemiah A' [C5XLOE]";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 break;
716 case 2 ... 4:
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100717 cpu_model = CPU_NEHEMIAH;
Rafa³ Bilskie57501c2007-02-08 23:12:02 +0100718 cpuname = "C3 'Nehemiah B' [C5XLOH]";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 break;
720 case 5 ... 15:
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100721 cpu_model = CPU_NEHEMIAH_C;
Rafa³ Bilskie57501c2007-02-08 23:12:02 +0100722 cpuname = "C3 'Nehemiah C' [C5P]";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 break;
724 }
725 break;
726
727 default:
728 cpuname = "Unknown";
729 break;
730 }
731
732 printk (KERN_INFO PFX "VIA %s CPU detected. ", cpuname);
733 switch (longhaul_version) {
734 case TYPE_LONGHAUL_V1:
735 case TYPE_LONGHAUL_V2:
736 printk ("Longhaul v%d supported.\n", longhaul_version);
737 break;
738 case TYPE_POWERSAVER:
739 printk ("Powersaver supported.\n");
740 break;
741 };
742
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100743 /* Doesn't hurt */
744 vt8235_present = longhaul_setup_vt8235();
745
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200746 /* Find ACPI data for processor */
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100747 acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
748 ACPI_UINT32_MAX, &longhaul_walk_callback,
749 NULL, (void *)&pr);
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200750
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100751 /* Check ACPI support for C3 state */
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100752 if (pr != NULL && longhaul_version == TYPE_POWERSAVER) {
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200753 cx = &pr->power.states[ACPI_STATE_C3];
Rafa³ Bilski14796722007-01-19 22:28:22 +0100754 if (cx->address > 0 && cx->latency <= 1000) {
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100755 longhaul_flags |= USE_ACPI_C3;
Rafa³ Bilskieed7d412006-09-27 08:25:27 +0200756 goto print_support_type;
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200757 }
758 }
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100759 /* Check if northbridge is friendly */
760 if (enable_arbiter_disable()) {
761 longhaul_flags |= USE_NORTHBRIDGE;
762 goto print_support_type;
Rafa³ Bilskieed7d412006-09-27 08:25:27 +0200763 }
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100764 /* Use VT8235 southbridge if present */
765 if (longhaul_version == TYPE_POWERSAVER && vt8235_present) {
766 longhaul_flags |= USE_VT8235;
767 goto print_support_type;
768 }
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100769 /* Check ACPI support for bus master arbiter disable */
770 if ((pr == NULL) || !(pr->flags.bm_control)) {
771 printk(KERN_ERR PFX
772 "No ACPI support. Unsupported northbridge.\n");
773 return -ENODEV;
774 }
775
Rafa³ Bilskieed7d412006-09-27 08:25:27 +0200776print_support_type:
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100777 if (longhaul_flags & USE_NORTHBRIDGE)
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200778 printk (KERN_INFO PFX "Using northbridge support.\n");
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100779 else if (longhaul_flags & USE_VT8235)
780 printk (KERN_INFO PFX "Using VT8235 support.\n");
781 else
782 printk (KERN_INFO PFX "Using ACPI support.\n");
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 ret = longhaul_get_ranges();
785 if (ret != 0)
786 return ret;
787
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100788 if ((longhaul_version != TYPE_LONGHAUL_V1) && (scale_voltage != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 longhaul_setup_voltagescaling();
790
791 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Dave Jones6778bae2005-05-31 19:03:51 -0700792 policy->cpuinfo.transition_latency = 200000; /* nsec */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 policy->cur = calc_speed(longhaul_get_cpu_mult());
794
795 ret = cpufreq_frequency_table_cpuinfo(policy, longhaul_table);
796 if (ret)
797 return ret;
798
799 cpufreq_frequency_table_get_attr(longhaul_table, policy->cpu);
800
801 return 0;
802}
803
804static int __devexit longhaul_cpu_exit(struct cpufreq_policy *policy)
805{
806 cpufreq_frequency_table_put_attr(policy->cpu);
807 return 0;
808}
809
810static struct freq_attr* longhaul_attr[] = {
811 &cpufreq_freq_attr_scaling_available_freqs,
812 NULL,
813};
814
815static struct cpufreq_driver longhaul_driver = {
816 .verify = longhaul_verify,
817 .target = longhaul_target,
818 .get = longhaul_get,
819 .init = longhaul_cpu_init,
820 .exit = __devexit_p(longhaul_cpu_exit),
821 .name = "longhaul",
822 .owner = THIS_MODULE,
823 .attr = longhaul_attr,
824};
825
826
827static int __init longhaul_init(void)
828{
829 struct cpuinfo_x86 *c = cpu_data;
830
831 if (c->x86_vendor != X86_VENDOR_CENTAUR || c->x86 != 6)
832 return -ENODEV;
833
Rafa³ Bilski48b7bde2006-07-04 17:50:57 +0200834#ifdef CONFIG_SMP
835 if (num_online_cpus() > 1) {
Rafa³ Bilski48b7bde2006-07-04 17:50:57 +0200836 printk(KERN_ERR PFX "More than 1 CPU detected, longhaul disabled.\n");
Dave Jones1cfe2012006-12-28 22:30:16 -0500837 return -ENODEV;
Rafa³ Bilski48b7bde2006-07-04 17:50:57 +0200838 }
839#endif
840#ifdef CONFIG_X86_IO_APIC
841 if (cpu_has_apic) {
842 printk(KERN_ERR PFX "APIC detected. Longhaul is currently broken in this configuration.\n");
843 return -ENODEV;
844 }
845#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 switch (c->x86_model) {
847 case 6 ... 9:
848 return cpufreq_register_driver(&longhaul_driver);
Dave Jones8ec98222006-12-17 19:07:35 -0500849 case 10:
850 printk(KERN_ERR PFX "Use acpi-cpufreq driver for VIA C7\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 default:
Dave Jones928ee512006-12-17 19:09:59 -0500852 ;;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
854
855 return -ENODEV;
856}
857
858
859static void __exit longhaul_exit(void)
860{
Dave Jones8eebf1a2006-05-30 17:40:16 -0400861 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 for (i=0; i < numscales; i++) {
864 if (clock_ratio[i] == maxmult) {
865 longhaul_setstate(i);
866 break;
867 }
868 }
869
870 cpufreq_unregister_driver(&longhaul_driver);
871 kfree(longhaul_table);
872}
873
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200874module_param (scale_voltage, int, 0644);
875MODULE_PARM_DESC(scale_voltage, "Scale voltage of processor");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877MODULE_AUTHOR ("Dave Jones <davej@codemonkey.org.uk>");
878MODULE_DESCRIPTION ("Longhaul driver for VIA Cyrix processors.");
879MODULE_LICENSE ("GPL");
880
Rafa³ Bilski0d6daba2006-07-07 08:48:26 +0200881late_initcall(longhaul_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882module_exit(longhaul_exit);