2 * (C) 2001-2004 Dave Jones. <davej@codemonkey.org.uk>
3 * (C) 2002 Padraig Brady. <padraig@antefacto.com>
5 * Licensed under the terms of the GNU GPL License version 2.
6 * Based upon datasheets & sample CPUs kindly provided by VIA.
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.
22 * BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
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>
30 #include <linux/slab.h>
31 #include <linux/string.h>
32 #include <linux/pci.h>
35 #include <asm/timex.h>
40 #define PFX "longhaul: "
42 #define TYPE_LONGHAUL_V1 1
43 #define TYPE_LONGHAUL_V2 2
44 #define TYPE_POWERSAVER 3
50 #define CPU_NEHEMIAH 5
53 static unsigned int numscales=16, numvscales;
54 static unsigned int fsb;
55 static int minvid, maxvid;
56 static unsigned int minmult, maxmult;
57 static int can_scale_voltage;
60 /* Module parameters */
61 static int dont_scale_voltage;
64 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "longhaul", msg)
67 /* Clock ratios multiplied by 10 */
68 static int clock_ratio[32];
69 static int eblcr_table[32];
70 static int voltage_table[32];
71 static unsigned int highest_speed, lowest_speed; /* kHz */
72 static int longhaul_version;
73 static struct cpufreq_frequency_table *longhaul_table;
75 #ifdef CONFIG_CPU_FREQ_DEBUG
76 static char speedbuffer[8];
78 static char *print_speed(int speed)
82 sprintf (speedbuffer, "%dGHz", speed/1000);
84 sprintf (speedbuffer, "%d.%dGHz", speed/1000, (speed%1000)/100);
86 sprintf (speedbuffer, "%dMHz", speed);
93 static unsigned int calc_speed(int mult)
104 static int longhaul_get_cpu_mult(void)
106 unsigned long invalue=0,lo, hi;
108 rdmsr (MSR_IA32_EBL_CR_POWERON, lo, hi);
109 invalue = (lo & (1<<22|1<<23|1<<24|1<<25)) >>22;
110 if (longhaul_version==TYPE_LONGHAUL_V2 || longhaul_version==TYPE_POWERSAVER) {
114 return eblcr_table[invalue];
118 static void do_powersaver(union msr_longhaul *longhaul,
119 unsigned int clock_ratio_index)
123 unsigned int tmp_mask;
140 rdmsrl(MSR_VIA_LONGHAUL, longhaul->val);
141 longhaul->bits.SoftBusRatio = clock_ratio_index & 0xf;
142 longhaul->bits.SoftBusRatio4 = (clock_ratio_index & 0x10) >> 4;
143 longhaul->bits.EnableSoftBusRatio = 1;
144 longhaul->bits.RevisionKey = 0;
147 local_irq_save(flags);
150 * get current pci bus master state for all devices
151 * and clear bus master bit
156 dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev);
158 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
159 cmd_state[i++] = pci_cmd;
160 pci_cmd &= ~PCI_COMMAND_MASTER;
161 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
163 } while (dev != NULL);
165 tmp_mask=inb(0x21); /* works on C3. save mask. */
166 outb(0xFE,0x21); /* TMR0 only */
167 outb(0xFF,0x80); /* delay */
170 wrmsrl(MSR_VIA_LONGHAUL, longhaul->val);
175 outb(tmp_mask,0x21); /* restore mask */
177 /* restore pci bus master state for all devices */
181 dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev);
183 pci_cmd = cmd_state[i++];
184 pci_write_config_byte(dev, PCI_COMMAND, pci_cmd);
186 } while (dev != NULL);
187 local_irq_restore(flags);
190 /* disable bus ratio bit */
191 rdmsrl(MSR_VIA_LONGHAUL, longhaul->val);
192 longhaul->bits.EnableSoftBusRatio = 0;
193 longhaul->bits.RevisionKey = version;
194 wrmsrl(MSR_VIA_LONGHAUL, longhaul->val);
198 * longhaul_set_cpu_frequency()
199 * @clock_ratio_index : bitpattern of the new multiplier.
201 * Sets a new clock ratio.
204 static void longhaul_setstate(unsigned int clock_ratio_index)
207 struct cpufreq_freqs freqs;
208 union msr_longhaul longhaul;
210 static unsigned int old_ratio=-1;
212 if (old_ratio == clock_ratio_index)
214 old_ratio = clock_ratio_index;
216 mult = clock_ratio[clock_ratio_index];
220 speed = calc_speed(mult);
221 if ((speed > highest_speed) || (speed < lowest_speed))
224 freqs.old = calc_speed(longhaul_get_cpu_mult());
226 freqs.cpu = 0; /* longhaul.c is UP only driver */
228 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
230 dprintk ("Setting to FSB:%dMHz Mult:%d.%dx (%s)\n",
231 fsb, mult/10, mult%10, print_speed(speed/1000));
233 switch (longhaul_version) {
236 * Longhaul v1. (Samuel[C5A] and Samuel2 stepping 0[C5B])
237 * Software controlled multipliers only.
239 * *NB* Until we get voltage scaling working v1 & v2 are the same code.
240 * Longhaul v2 appears in Samuel2 Steppings 1->7 [C5b] and Ezra [C5C]
242 case TYPE_LONGHAUL_V1:
243 case TYPE_LONGHAUL_V2:
244 rdmsrl (MSR_VIA_BCR2, bcr2.val);
245 /* Enable software clock multiplier */
246 bcr2.bits.ESOFTBF = 1;
247 bcr2.bits.CLOCKMUL = clock_ratio_index;
249 wrmsrl (MSR_VIA_BCR2, bcr2.val);
252 /* Disable software clock multiplier */
253 rdmsrl (MSR_VIA_BCR2, bcr2.val);
254 bcr2.bits.ESOFTBF = 0;
256 wrmsrl (MSR_VIA_BCR2, bcr2.val);
261 * Longhaul v3 (aka Powersaver). (Ezra-T [C5M] & Nehemiah [C5N])
262 * We can scale voltage with this too, but that's currently
263 * disabled until we come up with a decent 'match freq to voltage'
265 * When we add voltage scaling, we will also need to do the
266 * voltage/freq setting in order depending on the direction
267 * of scaling (like we do in powernow-k7.c)
268 * Nehemiah can do FSB scaling too, but this has never been proven
269 * to work in practice.
271 case TYPE_POWERSAVER:
272 do_powersaver(&longhaul, clock_ratio_index);
276 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
280 * Centaur decided to make life a little more tricky.
281 * Only longhaul v1 is allowed to read EBLCR BSEL[0:1].
282 * Samuel2 and above have to try and guess what the FSB is.
283 * We do this by assuming we booted at maximum multiplier, and interpolate
284 * between that value multiplied by possible FSBs and cpu_mhz which
285 * was calculated at boot time. Really ugly, but no other way to do this.
290 static int _guess(int guess)
294 target = ((maxmult/10)*guess);
297 target += ROUNDING/2;
303 static int guess_fsb(void)
305 int speed = (cpu_khz/1000);
307 int speeds[3] = { 66, 100, 133 };
312 for (i=0; i<3; i++) {
313 if (_guess(speeds[i]) == speed)
320 static int __init longhaul_get_ranges(void)
322 unsigned long invalue;
323 unsigned int multipliers[32]= {
324 50,30,40,100,55,35,45,95,90,70,80,60,120,75,85,65,
325 -1,110,120,-1,135,115,125,105,130,150,160,140,-1,155,-1,145 };
326 unsigned int j, k = 0;
327 union msr_longhaul longhaul;
328 unsigned long lo, hi;
329 unsigned int eblcr_fsb_table_v1[] = { 66, 133, 100, -1 };
330 unsigned int eblcr_fsb_table_v2[] = { 133, 100, -1, 66 };
332 switch (longhaul_version) {
333 case TYPE_LONGHAUL_V1:
334 case TYPE_LONGHAUL_V2:
335 /* Ugh, Longhaul v1 didn't have the min/max MSRs.
336 Assume min=3.0x & max = whatever we booted at. */
338 maxmult = longhaul_get_cpu_mult();
339 rdmsr (MSR_IA32_EBL_CR_POWERON, lo, hi);
340 invalue = (lo & (1<<18|1<<19)) >>18;
341 if (cpu_model==CPU_SAMUEL || cpu_model==CPU_SAMUEL2)
342 fsb = eblcr_fsb_table_v1[invalue];
347 case TYPE_POWERSAVER:
349 if (cpu_model==CPU_EZRA_T) {
350 rdmsrl (MSR_VIA_LONGHAUL, longhaul.val);
351 invalue = longhaul.bits.MaxMHzBR;
352 if (longhaul.bits.MaxMHzBR4)
354 maxmult=multipliers[invalue];
356 invalue = longhaul.bits.MinMHzBR;
357 if (longhaul.bits.MinMHzBR4 == 1)
360 minmult = multipliers[invalue];
361 fsb = eblcr_fsb_table_v2[longhaul.bits.MaxMHzFSB];
366 if (cpu_model==CPU_NEHEMIAH) {
367 rdmsrl (MSR_VIA_LONGHAUL, longhaul.val);
370 * TODO: This code works, but raises a lot of questions.
371 * - Some Nehemiah's seem to have broken Min/MaxMHzBR's.
372 * We get around this by using a hardcoded multiplier of 4.0x
373 * for the minimimum speed, and the speed we booted up at for the max.
374 * This is done in longhaul_get_cpu_mult() by reading the EBLCR register.
375 * - According to some VIA documentation EBLCR is only
376 * in pre-Nehemiah C3s. How this still works is a mystery.
377 * We're possibly using something undocumented and unsupported,
378 * But it works, so we don't grumble.
381 maxmult=longhaul_get_cpu_mult();
383 /* Starting with the 1.2GHz parts, theres a 200MHz bus. */
384 if ((cpu_khz/1000) > 1200)
387 fsb = eblcr_fsb_table_v2[longhaul.bits.MaxMHzFSB];
392 dprintk ("MinMult:%d.%dx MaxMult:%d.%dx\n",
393 minmult/10, minmult%10, maxmult/10, maxmult%10);
396 printk (KERN_INFO PFX "Invalid (reserved) FSB!\n");
400 highest_speed = calc_speed(maxmult);
401 lowest_speed = calc_speed(minmult);
402 dprintk ("FSB:%dMHz Lowest speed: %s Highest speed:%s\n", fsb,
403 print_speed(lowest_speed/1000),
404 print_speed(highest_speed/1000));
406 if (lowest_speed == highest_speed) {
407 printk (KERN_INFO PFX "highestspeed == lowest, aborting.\n");
410 if (lowest_speed > highest_speed) {
411 printk (KERN_INFO PFX "nonsense! lowest (%d > %d) !\n",
412 lowest_speed, highest_speed);
416 longhaul_table = kmalloc((numscales + 1) * sizeof(struct cpufreq_frequency_table), GFP_KERNEL);
420 for (j=0; j < numscales; j++) {
422 ratio = clock_ratio[j];
425 if (ratio > maxmult || ratio < minmult)
427 longhaul_table[k].frequency = calc_speed(ratio);
428 longhaul_table[k].index = j;
432 longhaul_table[k].frequency = CPUFREQ_TABLE_END;
434 kfree (longhaul_table);
442 static void __init longhaul_setup_voltagescaling(void)
444 union msr_longhaul longhaul;
446 rdmsrl (MSR_VIA_LONGHAUL, longhaul.val);
448 if (!(longhaul.bits.RevisionID & 1))
451 minvid = longhaul.bits.MinimumVID;
452 maxvid = longhaul.bits.MaximumVID;
453 vrmrev = longhaul.bits.VRMRev;
455 if (minvid == 0 || maxvid == 0) {
456 printk (KERN_INFO PFX "Bogus values Min:%d.%03d Max:%d.%03d. "
457 "Voltage scaling disabled.\n",
458 minvid/1000, minvid%1000, maxvid/1000, maxvid%1000);
462 if (minvid == maxvid) {
463 printk (KERN_INFO PFX "Claims to support voltage scaling but min & max are "
464 "both %d.%03d. Voltage scaling disabled\n",
465 maxvid/1000, maxvid%1000);
470 dprintk ("VRM 8.5 \n");
471 memcpy (voltage_table, vrm85scales, sizeof(voltage_table));
472 numvscales = (voltage_table[maxvid]-voltage_table[minvid])/25;
474 dprintk ("Mobile VRM \n");
475 memcpy (voltage_table, mobilevrmscales, sizeof(voltage_table));
476 numvscales = (voltage_table[maxvid]-voltage_table[minvid])/5;
479 /* Current voltage isn't readable at first, so we need to
480 set it to a known value. The spec says to use maxvid */
481 longhaul.bits.RevisionKey = longhaul.bits.RevisionID; /* FIXME: This is bad. */
482 longhaul.bits.EnableSoftVID = 1;
483 longhaul.bits.SoftVID = maxvid;
484 wrmsrl (MSR_VIA_LONGHAUL, longhaul.val);
486 minvid = voltage_table[minvid];
487 maxvid = voltage_table[maxvid];
489 dprintk ("Min VID=%d.%03d Max VID=%d.%03d, %d possible voltage scales\n",
490 maxvid/1000, maxvid%1000, minvid/1000, minvid%1000, numvscales);
492 can_scale_voltage = 1;
496 static int longhaul_verify(struct cpufreq_policy *policy)
498 return cpufreq_frequency_table_verify(policy, longhaul_table);
502 static int longhaul_target(struct cpufreq_policy *policy,
503 unsigned int target_freq, unsigned int relation)
505 unsigned int table_index = 0;
506 unsigned int new_clock_ratio = 0;
508 if (cpufreq_frequency_table_target(policy, longhaul_table, target_freq, relation, &table_index))
511 new_clock_ratio = longhaul_table[table_index].index & 0xFF;
513 longhaul_setstate(new_clock_ratio);
519 static unsigned int longhaul_get(unsigned int cpu)
523 return calc_speed(longhaul_get_cpu_mult());
527 static int __init longhaul_cpu_init(struct cpufreq_policy *policy)
529 struct cpuinfo_x86 *c = cpu_data;
533 switch (c->x86_model) {
535 cpu_model = CPU_SAMUEL;
536 cpuname = "C3 'Samuel' [C5A]";
537 longhaul_version = TYPE_LONGHAUL_V1;
538 memcpy (clock_ratio, samuel1_clock_ratio, sizeof(samuel1_clock_ratio));
539 memcpy (eblcr_table, samuel1_eblcr, sizeof(samuel1_eblcr));
543 longhaul_version = TYPE_LONGHAUL_V1;
544 switch (c->x86_mask) {
546 cpu_model = CPU_SAMUEL2;
547 cpuname = "C3 'Samuel 2' [C5B]";
548 /* Note, this is not a typo, early Samuel2's had Samuel1 ratios. */
549 memcpy (clock_ratio, samuel1_clock_ratio, sizeof(samuel1_clock_ratio));
550 memcpy (eblcr_table, samuel2_eblcr, sizeof(samuel2_eblcr));
553 if (c->x86_mask < 8) {
554 cpu_model = CPU_SAMUEL2;
555 cpuname = "C3 'Samuel 2' [C5B]";
557 cpu_model = CPU_EZRA;
558 cpuname = "C3 'Ezra' [C5C]";
560 memcpy (clock_ratio, ezra_clock_ratio, sizeof(ezra_clock_ratio));
561 memcpy (eblcr_table, ezra_eblcr, sizeof(ezra_eblcr));
567 cpu_model = CPU_EZRA_T;
568 cpuname = "C3 'Ezra-T' [C5M]";
569 longhaul_version = TYPE_POWERSAVER;
571 memcpy (clock_ratio, ezrat_clock_ratio, sizeof(ezrat_clock_ratio));
572 memcpy (eblcr_table, ezrat_eblcr, sizeof(ezrat_eblcr));
576 cpu_model = CPU_NEHEMIAH;
577 longhaul_version = TYPE_POWERSAVER;
579 switch (c->x86_mask) {
581 cpuname = "C3 'Nehemiah A' [C5N]";
582 memcpy (clock_ratio, nehemiah_a_clock_ratio, sizeof(nehemiah_a_clock_ratio));
583 memcpy (eblcr_table, nehemiah_a_eblcr, sizeof(nehemiah_a_eblcr));
586 cpuname = "C3 'Nehemiah B' [C5N]";
587 memcpy (clock_ratio, nehemiah_b_clock_ratio, sizeof(nehemiah_b_clock_ratio));
588 memcpy (eblcr_table, nehemiah_b_eblcr, sizeof(nehemiah_b_eblcr));
591 cpuname = "C3 'Nehemiah C' [C5N]";
592 memcpy (clock_ratio, nehemiah_c_clock_ratio, sizeof(nehemiah_c_clock_ratio));
593 memcpy (eblcr_table, nehemiah_c_eblcr, sizeof(nehemiah_c_eblcr));
603 printk (KERN_INFO PFX "VIA %s CPU detected. ", cpuname);
604 switch (longhaul_version) {
605 case TYPE_LONGHAUL_V1:
606 case TYPE_LONGHAUL_V2:
607 printk ("Longhaul v%d supported.\n", longhaul_version);
609 case TYPE_POWERSAVER:
610 printk ("Powersaver supported.\n");
614 ret = longhaul_get_ranges();
618 if ((longhaul_version==TYPE_LONGHAUL_V2 || longhaul_version==TYPE_POWERSAVER) &&
619 (dont_scale_voltage==0))
620 longhaul_setup_voltagescaling();
622 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
623 policy->cpuinfo.transition_latency = 200000; /* nsec */
624 policy->cur = calc_speed(longhaul_get_cpu_mult());
626 ret = cpufreq_frequency_table_cpuinfo(policy, longhaul_table);
630 cpufreq_frequency_table_get_attr(longhaul_table, policy->cpu);
635 static int __devexit longhaul_cpu_exit(struct cpufreq_policy *policy)
637 cpufreq_frequency_table_put_attr(policy->cpu);
641 static struct freq_attr* longhaul_attr[] = {
642 &cpufreq_freq_attr_scaling_available_freqs,
646 static struct cpufreq_driver longhaul_driver = {
647 .verify = longhaul_verify,
648 .target = longhaul_target,
650 .init = longhaul_cpu_init,
651 .exit = __devexit_p(longhaul_cpu_exit),
653 .owner = THIS_MODULE,
654 .attr = longhaul_attr,
658 static int __init longhaul_init(void)
660 struct cpuinfo_x86 *c = cpu_data;
662 if (c->x86_vendor != X86_VENDOR_CENTAUR || c->x86 != 6)
665 switch (c->x86_model) {
667 return cpufreq_register_driver(&longhaul_driver);
669 printk (KERN_INFO PFX "Unknown VIA CPU. Contact davej@codemonkey.org.uk\n");
676 static void __exit longhaul_exit(void)
680 for (i=0; i < numscales; i++) {
681 if (clock_ratio[i] == maxmult) {
682 longhaul_setstate(i);
687 cpufreq_unregister_driver(&longhaul_driver);
688 kfree(longhaul_table);
691 module_param (dont_scale_voltage, int, 0644);
692 MODULE_PARM_DESC(dont_scale_voltage, "Don't scale voltage of processor");
694 MODULE_AUTHOR ("Dave Jones <davej@codemonkey.org.uk>");
695 MODULE_DESCRIPTION ("Longhaul driver for VIA Cyrix processors.");
696 MODULE_LICENSE ("GPL");
698 module_init(longhaul_init);
699 module_exit(longhaul_exit);