blob: 83a8793f1db88e50d64e84f7fd0cf436ee74d9b3 [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;
56static unsigned int numscales=16, numvscales;
57static unsigned int fsb;
58static int minvid, maxvid;
59static unsigned int minmult, maxmult;
60static int can_scale_voltage;
61static int vrmrev;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +020062static struct acpi_processor *pr = NULL;
63static struct acpi_processor_cx *cx = NULL;
Rafa³ Bilski179da8e2006-08-08 19:12:20 +020064static int port22_en = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/* Module parameters */
67static int dont_scale_voltage;
68
69
70#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "longhaul", msg)
71
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* Clock ratios multiplied by 10 */
74static int clock_ratio[32];
75static int eblcr_table[32];
76static int voltage_table[32];
77static unsigned int highest_speed, lowest_speed; /* kHz */
78static int longhaul_version;
79static struct cpufreq_frequency_table *longhaul_table;
80
81#ifdef CONFIG_CPU_FREQ_DEBUG
82static char speedbuffer[8];
83
84static char *print_speed(int speed)
85{
Dave Jonese2aa8732006-05-30 17:37:15 -040086 if (speed < 1000) {
87 snprintf(speedbuffer, sizeof(speedbuffer),"%dMHz", speed);
88 return speedbuffer;
89 }
90
91 if (speed%1000 == 0)
92 snprintf(speedbuffer, sizeof(speedbuffer),
93 "%dGHz", speed/1000);
94 else
95 snprintf(speedbuffer, sizeof(speedbuffer),
96 "%d.%dGHz", speed/1000, (speed%1000)/100);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 return speedbuffer;
99}
100#endif
101
102
103static unsigned int calc_speed(int mult)
104{
105 int khz;
106 khz = (mult/10)*fsb;
107 if (mult%10)
108 khz += fsb/2;
109 khz *= 1000;
110 return khz;
111}
112
113
114static int longhaul_get_cpu_mult(void)
115{
116 unsigned long invalue=0,lo, hi;
117
118 rdmsr (MSR_IA32_EBL_CR_POWERON, lo, hi);
119 invalue = (lo & (1<<22|1<<23|1<<24|1<<25)) >>22;
120 if (longhaul_version==TYPE_LONGHAUL_V2 || longhaul_version==TYPE_POWERSAVER) {
121 if (lo & (1<<27))
122 invalue+=16;
123 }
124 return eblcr_table[invalue];
125}
126
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200127/* For processor with BCR2 MSR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200129static void do_longhaul1(unsigned int clock_ratio_index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200131 union msr_bcr2 bcr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200133 rdmsrl(MSR_VIA_BCR2, bcr2.val);
134 /* Enable software clock multiplier */
135 bcr2.bits.ESOFTBF = 1;
136 bcr2.bits.CLOCKMUL = clock_ratio_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200138 /* Sync to timer tick */
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -0700139 safe_halt();
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200140 /* Change frequency on next halt or sleep */
141 wrmsrl(MSR_VIA_BCR2, bcr2.val);
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200142 /* Invoke transition */
143 ACPI_FLUSH_CPU_CACHE();
144 halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200146 /* Disable software clock multiplier */
Dave Jones3be6a482005-05-31 19:03:51 -0700147 local_irq_disable();
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200148 rdmsrl(MSR_VIA_BCR2, bcr2.val);
149 bcr2.bits.ESOFTBF = 0;
150 wrmsrl(MSR_VIA_BCR2, bcr2.val);
151}
Dave Jones3be6a482005-05-31 19:03:51 -0700152
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200153/* For processor with Longhaul MSR */
Dave Jones11746312005-05-31 19:03:51 -0700154
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200155static void do_powersaver(int cx_address, unsigned int clock_ratio_index)
156{
157 union msr_longhaul longhaul;
158 u32 t;
Dave Jones3be6a482005-05-31 19:03:51 -0700159
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200160 rdmsrl(MSR_VIA_LONGHAUL, longhaul.val);
161 longhaul.bits.RevisionKey = longhaul.bits.RevisionID;
162 longhaul.bits.SoftBusRatio = clock_ratio_index & 0xf;
163 longhaul.bits.SoftBusRatio4 = (clock_ratio_index & 0x10) >> 4;
Rafa³ Bilskieb23c752006-07-09 21:47:04 +0200164 longhaul.bits.EnableSoftBusRatio = 1;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200165
166 /* Sync to timer tick */
167 safe_halt();
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200168 /* Change frequency on next halt or sleep */
169 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200170 ACPI_FLUSH_CPU_CACHE();
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200171 /* Invoke C3 */
172 inb(cx_address);
173 /* Dummy op - must do something useless after P_LVL3 read */
174 t = inl(acpi_fadt.xpm_tmr_blk.address);
175
176 /* Disable bus ratio bit */
177 local_irq_disable();
178 longhaul.bits.RevisionKey = longhaul.bits.RevisionID;
179 longhaul.bits.EnableSoftBusRatio = 0;
180 longhaul.bits.EnableSoftBSEL = 0;
181 longhaul.bits.EnableSoftVID = 0;
182 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
185/**
186 * longhaul_set_cpu_frequency()
187 * @clock_ratio_index : bitpattern of the new multiplier.
188 *
189 * Sets a new clock ratio.
190 */
191
192static void longhaul_setstate(unsigned int clock_ratio_index)
193{
194 int speed, mult;
195 struct cpufreq_freqs freqs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 static unsigned int old_ratio=-1;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200197 unsigned long flags;
198 unsigned int pic1_mask, pic2_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 if (old_ratio == clock_ratio_index)
201 return;
202 old_ratio = clock_ratio_index;
203
204 mult = clock_ratio[clock_ratio_index];
205 if (mult == -1)
206 return;
207
208 speed = calc_speed(mult);
209 if ((speed > highest_speed) || (speed < lowest_speed))
210 return;
211
212 freqs.old = calc_speed(longhaul_get_cpu_mult());
213 freqs.new = speed;
214 freqs.cpu = 0; /* longhaul.c is UP only driver */
215
216 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
217
218 dprintk ("Setting to FSB:%dMHz Mult:%d.%dx (%s)\n",
219 fsb, mult/10, mult%10, print_speed(speed/1000));
220
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200221 preempt_disable();
222 local_irq_save(flags);
223
224 pic2_mask = inb(0xA1);
225 pic1_mask = inb(0x21); /* works on C3. save mask. */
226 outb(0xFF,0xA1); /* Overkill */
227 outb(0xFE,0x21); /* TMR0 only */
228
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200229 if (pr->flags.bm_control) {
230 /* Disable bus master arbitration */
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200231 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1,
232 ACPI_MTX_DO_NOT_LOCK);
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200233 } else if (port22_en) {
234 /* Disable AGP and PCI arbiters */
235 outb(3, 0x22);
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200236 }
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 switch (longhaul_version) {
239
240 /*
241 * Longhaul v1. (Samuel[C5A] and Samuel2 stepping 0[C5B])
242 * Software controlled multipliers only.
243 *
244 * *NB* Until we get voltage scaling working v1 & v2 are the same code.
245 * Longhaul v2 appears in Samuel2 Steppings 1->7 [C5b] and Ezra [C5C]
246 */
247 case TYPE_LONGHAUL_V1:
248 case TYPE_LONGHAUL_V2:
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200249 do_longhaul1(clock_ratio_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 break;
251
252 /*
253 * Longhaul v3 (aka Powersaver). (Ezra-T [C5M] & Nehemiah [C5N])
254 * We can scale voltage with this too, but that's currently
255 * disabled until we come up with a decent 'match freq to voltage'
256 * algorithm.
257 * When we add voltage scaling, we will also need to do the
258 * voltage/freq setting in order depending on the direction
259 * of scaling (like we do in powernow-k7.c)
260 * Nehemiah can do FSB scaling too, but this has never been proven
261 * to work in practice.
262 */
263 case TYPE_POWERSAVER:
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200264 /* Don't allow wakeup */
265 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0,
266 ACPI_MTX_DO_NOT_LOCK);
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200267 do_powersaver(cx->address, clock_ratio_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 break;
269 }
270
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200271 if (pr->flags.bm_control) {
272 /* Enable bus master arbitration */
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200273 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0,
274 ACPI_MTX_DO_NOT_LOCK);
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200275 } else if (port22_en) {
276 /* Enable arbiters */
277 outb(0, 0x22);
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200278 }
279
280 outb(pic2_mask,0xA1); /* restore mask */
281 outb(pic1_mask,0x21);
282
283 local_irq_restore(flags);
284 preempt_enable();
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
287}
288
289/*
290 * Centaur decided to make life a little more tricky.
291 * Only longhaul v1 is allowed to read EBLCR BSEL[0:1].
292 * Samuel2 and above have to try and guess what the FSB is.
293 * We do this by assuming we booted at maximum multiplier, and interpolate
294 * between that value multiplied by possible FSBs and cpu_mhz which
295 * was calculated at boot time. Really ugly, but no other way to do this.
296 */
297
298#define ROUNDING 0xf
299
300static int _guess(int guess)
301{
302 int target;
303
304 target = ((maxmult/10)*guess);
305 if (maxmult%10 != 0)
306 target += (guess/2);
307 target += ROUNDING/2;
308 target &= ~ROUNDING;
309 return target;
310}
311
312
313static int guess_fsb(void)
314{
315 int speed = (cpu_khz/1000);
316 int i;
317 int speeds[3] = { 66, 100, 133 };
318
319 speed += ROUNDING/2;
320 speed &= ~ROUNDING;
321
322 for (i=0; i<3; i++) {
323 if (_guess(speeds[i]) == speed)
324 return speeds[i];
325 }
326 return 0;
327}
328
329
330static int __init longhaul_get_ranges(void)
331{
332 unsigned long invalue;
Rafa³ Bilski32deb2d2006-07-15 19:31:30 +0200333 unsigned int ezra_t_multipliers[32]= {
334 90, 30, 40, 100, 55, 35, 45, 95,
335 50, 70, 80, 60, 120, 75, 85, 65,
336 -1, 110, 120, -1, 135, 115, 125, 105,
337 130, 150, 160, 140, -1, 155, -1, 145 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 unsigned int j, k = 0;
339 union msr_longhaul longhaul;
340 unsigned long lo, hi;
341 unsigned int eblcr_fsb_table_v1[] = { 66, 133, 100, -1 };
342 unsigned int eblcr_fsb_table_v2[] = { 133, 100, -1, 66 };
343
344 switch (longhaul_version) {
345 case TYPE_LONGHAUL_V1:
346 case TYPE_LONGHAUL_V2:
347 /* Ugh, Longhaul v1 didn't have the min/max MSRs.
348 Assume min=3.0x & max = whatever we booted at. */
349 minmult = 30;
350 maxmult = longhaul_get_cpu_mult();
351 rdmsr (MSR_IA32_EBL_CR_POWERON, lo, hi);
352 invalue = (lo & (1<<18|1<<19)) >>18;
353 if (cpu_model==CPU_SAMUEL || cpu_model==CPU_SAMUEL2)
354 fsb = eblcr_fsb_table_v1[invalue];
355 else
356 fsb = guess_fsb();
357 break;
358
359 case TYPE_POWERSAVER:
360 /* Ezra-T */
361 if (cpu_model==CPU_EZRA_T) {
362 rdmsrl (MSR_VIA_LONGHAUL, longhaul.val);
363 invalue = longhaul.bits.MaxMHzBR;
364 if (longhaul.bits.MaxMHzBR4)
365 invalue += 16;
Rafa³ Bilski32deb2d2006-07-15 19:31:30 +0200366 maxmult=ezra_t_multipliers[invalue];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 invalue = longhaul.bits.MinMHzBR;
369 if (longhaul.bits.MinMHzBR4 == 1)
370 minmult = 30;
371 else
Rafa³ Bilski32deb2d2006-07-15 19:31:30 +0200372 minmult = ezra_t_multipliers[invalue];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 fsb = eblcr_fsb_table_v2[longhaul.bits.MaxMHzFSB];
374 break;
375 }
376
377 /* Nehemiah */
378 if (cpu_model==CPU_NEHEMIAH) {
379 rdmsrl (MSR_VIA_LONGHAUL, longhaul.val);
380
381 /*
382 * TODO: This code works, but raises a lot of questions.
383 * - Some Nehemiah's seem to have broken Min/MaxMHzBR's.
384 * We get around this by using a hardcoded multiplier of 4.0x
385 * for the minimimum speed, and the speed we booted up at for the max.
386 * This is done in longhaul_get_cpu_mult() by reading the EBLCR register.
387 * - According to some VIA documentation EBLCR is only
388 * in pre-Nehemiah C3s. How this still works is a mystery.
389 * We're possibly using something undocumented and unsupported,
390 * But it works, so we don't grumble.
391 */
392 minmult=40;
393 maxmult=longhaul_get_cpu_mult();
394
395 /* Starting with the 1.2GHz parts, theres a 200MHz bus. */
396 if ((cpu_khz/1000) > 1200)
397 fsb = 200;
398 else
399 fsb = eblcr_fsb_table_v2[longhaul.bits.MaxMHzFSB];
400 break;
401 }
402 }
403
404 dprintk ("MinMult:%d.%dx MaxMult:%d.%dx\n",
405 minmult/10, minmult%10, maxmult/10, maxmult%10);
406
407 if (fsb == -1) {
408 printk (KERN_INFO PFX "Invalid (reserved) FSB!\n");
409 return -EINVAL;
410 }
411
412 highest_speed = calc_speed(maxmult);
413 lowest_speed = calc_speed(minmult);
414 dprintk ("FSB:%dMHz Lowest speed: %s Highest speed:%s\n", fsb,
415 print_speed(lowest_speed/1000),
416 print_speed(highest_speed/1000));
417
418 if (lowest_speed == highest_speed) {
419 printk (KERN_INFO PFX "highestspeed == lowest, aborting.\n");
420 return -EINVAL;
421 }
422 if (lowest_speed > highest_speed) {
423 printk (KERN_INFO PFX "nonsense! lowest (%d > %d) !\n",
424 lowest_speed, highest_speed);
425 return -EINVAL;
426 }
427
428 longhaul_table = kmalloc((numscales + 1) * sizeof(struct cpufreq_frequency_table), GFP_KERNEL);
429 if(!longhaul_table)
430 return -ENOMEM;
431
432 for (j=0; j < numscales; j++) {
433 unsigned int ratio;
434 ratio = clock_ratio[j];
435 if (ratio == -1)
436 continue;
437 if (ratio > maxmult || ratio < minmult)
438 continue;
439 longhaul_table[k].frequency = calc_speed(ratio);
440 longhaul_table[k].index = j;
441 k++;
442 }
443
444 longhaul_table[k].frequency = CPUFREQ_TABLE_END;
445 if (!k) {
446 kfree (longhaul_table);
447 return -EINVAL;
448 }
449
450 return 0;
451}
452
453
454static void __init longhaul_setup_voltagescaling(void)
455{
456 union msr_longhaul longhaul;
457
458 rdmsrl (MSR_VIA_LONGHAUL, longhaul.val);
459
460 if (!(longhaul.bits.RevisionID & 1))
461 return;
462
463 minvid = longhaul.bits.MinimumVID;
464 maxvid = longhaul.bits.MaximumVID;
465 vrmrev = longhaul.bits.VRMRev;
466
467 if (minvid == 0 || maxvid == 0) {
468 printk (KERN_INFO PFX "Bogus values Min:%d.%03d Max:%d.%03d. "
469 "Voltage scaling disabled.\n",
470 minvid/1000, minvid%1000, maxvid/1000, maxvid%1000);
471 return;
472 }
473
474 if (minvid == maxvid) {
475 printk (KERN_INFO PFX "Claims to support voltage scaling but min & max are "
476 "both %d.%03d. Voltage scaling disabled\n",
477 maxvid/1000, maxvid%1000);
478 return;
479 }
480
481 if (vrmrev==0) {
Dave Jones52c18fd2005-09-01 11:01:02 -0700482 dprintk ("VRM 8.5\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 memcpy (voltage_table, vrm85scales, sizeof(voltage_table));
484 numvscales = (voltage_table[maxvid]-voltage_table[minvid])/25;
485 } else {
Dave Jones52c18fd2005-09-01 11:01:02 -0700486 dprintk ("Mobile VRM\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 memcpy (voltage_table, mobilevrmscales, sizeof(voltage_table));
488 numvscales = (voltage_table[maxvid]-voltage_table[minvid])/5;
489 }
490
491 /* Current voltage isn't readable at first, so we need to
492 set it to a known value. The spec says to use maxvid */
493 longhaul.bits.RevisionKey = longhaul.bits.RevisionID; /* FIXME: This is bad. */
494 longhaul.bits.EnableSoftVID = 1;
495 longhaul.bits.SoftVID = maxvid;
496 wrmsrl (MSR_VIA_LONGHAUL, longhaul.val);
497
498 minvid = voltage_table[minvid];
499 maxvid = voltage_table[maxvid];
500
501 dprintk ("Min VID=%d.%03d Max VID=%d.%03d, %d possible voltage scales\n",
502 maxvid/1000, maxvid%1000, minvid/1000, minvid%1000, numvscales);
503
504 can_scale_voltage = 1;
505}
506
507
508static int longhaul_verify(struct cpufreq_policy *policy)
509{
510 return cpufreq_frequency_table_verify(policy, longhaul_table);
511}
512
513
514static int longhaul_target(struct cpufreq_policy *policy,
515 unsigned int target_freq, unsigned int relation)
516{
517 unsigned int table_index = 0;
518 unsigned int new_clock_ratio = 0;
519
520 if (cpufreq_frequency_table_target(policy, longhaul_table, target_freq, relation, &table_index))
521 return -EINVAL;
522
523 new_clock_ratio = longhaul_table[table_index].index & 0xFF;
524
525 longhaul_setstate(new_clock_ratio);
526
527 return 0;
528}
529
530
531static unsigned int longhaul_get(unsigned int cpu)
532{
533 if (cpu)
534 return 0;
535 return calc_speed(longhaul_get_cpu_mult());
536}
537
Adrian Bunkc4a96c12006-07-09 19:53:08 +0200538static acpi_status longhaul_walk_callback(acpi_handle obj_handle,
539 u32 nesting_level,
540 void *context, void **return_value)
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200541{
542 struct acpi_device *d;
543
544 if ( acpi_bus_get_device(obj_handle, &d) ) {
545 return 0;
546 }
547 *return_value = (void *)acpi_driver_data(d);
548 return 1;
549}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200551/* VIA don't support PM2 reg, but have something similar */
552static int enable_arbiter_disable(void)
553{
554 struct pci_dev *dev;
555 u8 pci_cmd;
556
557 /* Find PLE133 host bridge */
558 dev = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8601_0, NULL);
559 if (dev != NULL) {
560 /* Enable access to port 0x22 */
561 pci_read_config_byte(dev, 0x78, &pci_cmd);
562 if ( !(pci_cmd & 1<<7) ) {
563 pci_cmd |= 1<<7;
564 pci_write_config_byte(dev, 0x78, pci_cmd);
565 }
566 return 1;
567 }
568 return 0;
569}
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571static int __init longhaul_cpu_init(struct cpufreq_policy *policy)
572{
573 struct cpuinfo_x86 *c = cpu_data;
574 char *cpuname=NULL;
575 int ret;
576
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200577 /* Check what we have on this motherboard */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 switch (c->x86_model) {
579 case 6:
580 cpu_model = CPU_SAMUEL;
581 cpuname = "C3 'Samuel' [C5A]";
582 longhaul_version = TYPE_LONGHAUL_V1;
583 memcpy (clock_ratio, samuel1_clock_ratio, sizeof(samuel1_clock_ratio));
584 memcpy (eblcr_table, samuel1_eblcr, sizeof(samuel1_eblcr));
585 break;
586
587 case 7:
588 longhaul_version = TYPE_LONGHAUL_V1;
589 switch (c->x86_mask) {
590 case 0:
591 cpu_model = CPU_SAMUEL2;
592 cpuname = "C3 'Samuel 2' [C5B]";
593 /* Note, this is not a typo, early Samuel2's had Samuel1 ratios. */
594 memcpy (clock_ratio, samuel1_clock_ratio, sizeof(samuel1_clock_ratio));
595 memcpy (eblcr_table, samuel2_eblcr, sizeof(samuel2_eblcr));
596 break;
597 case 1 ... 15:
598 if (c->x86_mask < 8) {
599 cpu_model = CPU_SAMUEL2;
600 cpuname = "C3 'Samuel 2' [C5B]";
601 } else {
602 cpu_model = CPU_EZRA;
603 cpuname = "C3 'Ezra' [C5C]";
604 }
605 memcpy (clock_ratio, ezra_clock_ratio, sizeof(ezra_clock_ratio));
606 memcpy (eblcr_table, ezra_eblcr, sizeof(ezra_eblcr));
607 break;
608 }
609 break;
610
611 case 8:
612 cpu_model = CPU_EZRA_T;
613 cpuname = "C3 'Ezra-T' [C5M]";
614 longhaul_version = TYPE_POWERSAVER;
615 numscales=32;
616 memcpy (clock_ratio, ezrat_clock_ratio, sizeof(ezrat_clock_ratio));
617 memcpy (eblcr_table, ezrat_eblcr, sizeof(ezrat_eblcr));
618 break;
619
620 case 9:
621 cpu_model = CPU_NEHEMIAH;
622 longhaul_version = TYPE_POWERSAVER;
623 numscales=32;
624 switch (c->x86_mask) {
625 case 0 ... 1:
626 cpuname = "C3 'Nehemiah A' [C5N]";
627 memcpy (clock_ratio, nehemiah_a_clock_ratio, sizeof(nehemiah_a_clock_ratio));
628 memcpy (eblcr_table, nehemiah_a_eblcr, sizeof(nehemiah_a_eblcr));
629 break;
630 case 2 ... 4:
631 cpuname = "C3 'Nehemiah B' [C5N]";
632 memcpy (clock_ratio, nehemiah_b_clock_ratio, sizeof(nehemiah_b_clock_ratio));
633 memcpy (eblcr_table, nehemiah_b_eblcr, sizeof(nehemiah_b_eblcr));
634 break;
635 case 5 ... 15:
636 cpuname = "C3 'Nehemiah C' [C5N]";
637 memcpy (clock_ratio, nehemiah_c_clock_ratio, sizeof(nehemiah_c_clock_ratio));
638 memcpy (eblcr_table, nehemiah_c_eblcr, sizeof(nehemiah_c_eblcr));
639 break;
640 }
641 break;
642
643 default:
644 cpuname = "Unknown";
645 break;
646 }
647
648 printk (KERN_INFO PFX "VIA %s CPU detected. ", cpuname);
649 switch (longhaul_version) {
650 case TYPE_LONGHAUL_V1:
651 case TYPE_LONGHAUL_V2:
652 printk ("Longhaul v%d supported.\n", longhaul_version);
653 break;
654 case TYPE_POWERSAVER:
655 printk ("Powersaver supported.\n");
656 break;
657 };
658
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200659 /* Find ACPI data for processor */
660 acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT, ACPI_UINT32_MAX,
661 &longhaul_walk_callback, NULL, (void *)&pr);
662 if (pr == NULL)
663 goto err_acpi;
664
665 if (longhaul_version == TYPE_POWERSAVER) {
666 /* Check ACPI support for C3 state */
667 cx = &pr->power.states[ACPI_STATE_C3];
668 if (cx->address == 0 || cx->latency > 1000)
669 goto err_acpi;
670 } else {
671 /* Check ACPI support for bus master arbiter disable */
672 if (!pr->flags.bm_control) {
673 if (!enable_arbiter_disable()) {
674 printk(KERN_ERR PFX "No ACPI support. No VT8601 host bridge. Aborting.\n");
675 return -ENODEV;
676 } else
677 port22_en = 1;
678 }
679 }
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 ret = longhaul_get_ranges();
682 if (ret != 0)
683 return ret;
684
685 if ((longhaul_version==TYPE_LONGHAUL_V2 || longhaul_version==TYPE_POWERSAVER) &&
686 (dont_scale_voltage==0))
687 longhaul_setup_voltagescaling();
688
689 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Dave Jones6778bae2005-05-31 19:03:51 -0700690 policy->cpuinfo.transition_latency = 200000; /* nsec */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 policy->cur = calc_speed(longhaul_get_cpu_mult());
692
693 ret = cpufreq_frequency_table_cpuinfo(policy, longhaul_table);
694 if (ret)
695 return ret;
696
697 cpufreq_frequency_table_get_attr(longhaul_table, policy->cpu);
698
699 return 0;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200700
701err_acpi:
702 printk(KERN_ERR PFX "No ACPI support for CPU frequency changes.\n");
703 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
706static int __devexit longhaul_cpu_exit(struct cpufreq_policy *policy)
707{
708 cpufreq_frequency_table_put_attr(policy->cpu);
709 return 0;
710}
711
712static struct freq_attr* longhaul_attr[] = {
713 &cpufreq_freq_attr_scaling_available_freqs,
714 NULL,
715};
716
717static struct cpufreq_driver longhaul_driver = {
718 .verify = longhaul_verify,
719 .target = longhaul_target,
720 .get = longhaul_get,
721 .init = longhaul_cpu_init,
722 .exit = __devexit_p(longhaul_cpu_exit),
723 .name = "longhaul",
724 .owner = THIS_MODULE,
725 .attr = longhaul_attr,
726};
727
728
729static int __init longhaul_init(void)
730{
731 struct cpuinfo_x86 *c = cpu_data;
732
733 if (c->x86_vendor != X86_VENDOR_CENTAUR || c->x86 != 6)
734 return -ENODEV;
735
Rafa³ Bilski48b7bde2006-07-04 17:50:57 +0200736#ifdef CONFIG_SMP
737 if (num_online_cpus() > 1) {
738 return -ENODEV;
739 printk(KERN_ERR PFX "More than 1 CPU detected, longhaul disabled.\n");
740 }
741#endif
742#ifdef CONFIG_X86_IO_APIC
743 if (cpu_has_apic) {
744 printk(KERN_ERR PFX "APIC detected. Longhaul is currently broken in this configuration.\n");
745 return -ENODEV;
746 }
747#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 switch (c->x86_model) {
749 case 6 ... 9:
750 return cpufreq_register_driver(&longhaul_driver);
751 default:
752 printk (KERN_INFO PFX "Unknown VIA CPU. Contact davej@codemonkey.org.uk\n");
753 }
754
755 return -ENODEV;
756}
757
758
759static void __exit longhaul_exit(void)
760{
Dave Jones8eebf1a2006-05-30 17:40:16 -0400761 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 for (i=0; i < numscales; i++) {
764 if (clock_ratio[i] == maxmult) {
765 longhaul_setstate(i);
766 break;
767 }
768 }
769
770 cpufreq_unregister_driver(&longhaul_driver);
771 kfree(longhaul_table);
772}
773
774module_param (dont_scale_voltage, int, 0644);
775MODULE_PARM_DESC(dont_scale_voltage, "Don't scale voltage of processor");
776
777MODULE_AUTHOR ("Dave Jones <davej@codemonkey.org.uk>");
778MODULE_DESCRIPTION ("Longhaul driver for VIA Cyrix processors.");
779MODULE_LICENSE ("GPL");
780
Rafa³ Bilski0d6daba2006-07-07 08:48:26 +0200781late_initcall(longhaul_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782module_exit(longhaul_exit);
783