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/pci.h>
31 #include <linux/slab.h>
32 #include <linux/string.h>
35 #include <asm/timex.h>
38 #include <linux/acpi.h>
39 #include <acpi/processor.h>
43 #define PFX "longhaul: "
45 #define TYPE_LONGHAUL_V1 1
46 #define TYPE_LONGHAUL_V2 2
47 #define TYPE_POWERSAVER 3
53 #define CPU_NEHEMIAH 5
54 #define CPU_NEHEMIAH_C 6
57 #define USE_ACPI_C3 (1 << 1)
58 #define USE_NORTHBRIDGE (1 << 2)
59 #define USE_VT8235 (1 << 3)
62 static unsigned int numscales=16;
63 static unsigned int fsb;
65 static struct mV_pos *vrm_mV_table;
66 static unsigned char *mV_vrm_table;
70 static struct f_msr f_msr_table[32];
72 static unsigned int highest_speed, lowest_speed; /* kHz */
73 static unsigned int minmult, maxmult;
74 static int can_scale_voltage;
75 static struct acpi_processor *pr = NULL;
76 static struct acpi_processor_cx *cx = NULL;
77 static u8 longhaul_flags;
79 /* Module parameters */
80 static int scale_voltage;
82 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "longhaul", msg)
85 /* Clock ratios multiplied by 10 */
86 static int clock_ratio[32];
87 static int eblcr_table[32];
88 static int longhaul_version;
89 static struct cpufreq_frequency_table *longhaul_table;
91 #ifdef CONFIG_CPU_FREQ_DEBUG
92 static char speedbuffer[8];
94 static char *print_speed(int speed)
97 snprintf(speedbuffer, sizeof(speedbuffer),"%dMHz", speed);
102 snprintf(speedbuffer, sizeof(speedbuffer),
103 "%dGHz", speed/1000);
105 snprintf(speedbuffer, sizeof(speedbuffer),
106 "%d.%dGHz", speed/1000, (speed%1000)/100);
113 static unsigned int calc_speed(int mult)
124 static int longhaul_get_cpu_mult(void)
126 unsigned long invalue=0,lo, hi;
128 rdmsr (MSR_IA32_EBL_CR_POWERON, lo, hi);
129 invalue = (lo & (1<<22|1<<23|1<<24|1<<25)) >>22;
130 if (longhaul_version==TYPE_LONGHAUL_V2 || longhaul_version==TYPE_POWERSAVER) {
134 return eblcr_table[invalue];
137 /* For processor with BCR2 MSR */
139 static void do_longhaul1(unsigned int clock_ratio_index)
143 rdmsrl(MSR_VIA_BCR2, bcr2.val);
144 /* Enable software clock multiplier */
145 bcr2.bits.ESOFTBF = 1;
146 bcr2.bits.CLOCKMUL = clock_ratio_index;
148 /* Sync to timer tick */
150 /* Change frequency on next halt or sleep */
151 wrmsrl(MSR_VIA_BCR2, bcr2.val);
152 /* Invoke transition */
153 ACPI_FLUSH_CPU_CACHE();
156 /* Disable software clock multiplier */
158 rdmsrl(MSR_VIA_BCR2, bcr2.val);
159 bcr2.bits.ESOFTBF = 0;
160 wrmsrl(MSR_VIA_BCR2, bcr2.val);
163 /* For processor with Longhaul MSR */
165 static void do_powersaver(int cx_address, unsigned int clock_ratio_index)
167 union msr_longhaul longhaul;
170 rdmsrl(MSR_VIA_LONGHAUL, longhaul.val);
171 longhaul.bits.RevisionKey = longhaul.bits.RevisionID;
172 longhaul.bits.SoftBusRatio = clock_ratio_index & 0xf;
173 longhaul.bits.SoftBusRatio4 = (clock_ratio_index & 0x10) >> 4;
174 longhaul.bits.EnableSoftBusRatio = 1;
176 if (can_scale_voltage) {
177 longhaul.bits.SoftVID = f_msr_table[clock_ratio_index].vrm;
178 longhaul.bits.EnableSoftVID = 1;
181 /* Sync to timer tick */
183 /* Change frequency on next halt or sleep */
184 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
186 ACPI_FLUSH_CPU_CACHE();
190 ACPI_FLUSH_CPU_CACHE();
193 /* Dummy op - must do something useless after P_LVL3 read */
194 t = inl(acpi_fadt.xpm_tmr_blk.address);
196 /* Disable bus ratio bit */
198 longhaul.bits.RevisionKey = longhaul.bits.RevisionID;
199 longhaul.bits.EnableSoftBusRatio = 0;
200 longhaul.bits.EnableSoftBSEL = 0;
201 longhaul.bits.EnableSoftVID = 0;
202 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
206 * longhaul_set_cpu_frequency()
207 * @clock_ratio_index : bitpattern of the new multiplier.
209 * Sets a new clock ratio.
212 static void longhaul_setstate(unsigned int clock_ratio_index)
215 struct cpufreq_freqs freqs;
216 static unsigned int old_ratio=-1;
218 unsigned int pic1_mask, pic2_mask;
220 if (old_ratio == clock_ratio_index)
222 old_ratio = clock_ratio_index;
224 mult = clock_ratio[clock_ratio_index];
228 speed = calc_speed(mult);
229 if ((speed > highest_speed) || (speed < lowest_speed))
232 freqs.old = calc_speed(longhaul_get_cpu_mult());
234 freqs.cpu = 0; /* longhaul.c is UP only driver */
236 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
238 dprintk ("Setting to FSB:%dMHz Mult:%d.%dx (%s)\n",
239 fsb, mult/10, mult%10, print_speed(speed/1000));
242 local_irq_save(flags);
244 pic2_mask = inb(0xA1);
245 pic1_mask = inb(0x21); /* works on C3. save mask. */
246 outb(0xFF,0xA1); /* Overkill */
247 outb(0xFE,0x21); /* TMR0 only */
249 if (longhaul_flags & USE_NORTHBRIDGE) {
250 /* Disable AGP and PCI arbiters */
252 } else if ((pr != NULL) && pr->flags.bm_control) {
253 /* Disable bus master arbitration */
254 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1,
255 ACPI_MTX_DO_NOT_LOCK);
257 switch (longhaul_version) {
260 * Longhaul v1. (Samuel[C5A] and Samuel2 stepping 0[C5B])
261 * Software controlled multipliers only.
263 * *NB* Until we get voltage scaling working v1 & v2 are the same code.
264 * Longhaul v2 appears in Samuel2 Steppings 1->7 [C5b] and Ezra [C5C]
266 case TYPE_LONGHAUL_V1:
267 case TYPE_LONGHAUL_V2:
268 do_longhaul1(clock_ratio_index);
272 * Longhaul v3 (aka Powersaver). (Ezra-T [C5M] & Nehemiah [C5N])
273 * We can scale voltage with this too, but that's currently
274 * disabled until we come up with a decent 'match freq to voltage'
276 * When we add voltage scaling, we will also need to do the
277 * voltage/freq setting in order depending on the direction
278 * of scaling (like we do in powernow-k7.c)
279 * Nehemiah can do FSB scaling too, but this has never been proven
280 * to work in practice.
282 case TYPE_POWERSAVER:
283 if (longhaul_flags & USE_ACPI_C3) {
284 /* Don't allow wakeup */
285 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0,
286 ACPI_MTX_DO_NOT_LOCK);
287 do_powersaver(cx->address, clock_ratio_index);
289 do_powersaver(0, clock_ratio_index);
294 if (longhaul_flags & USE_NORTHBRIDGE) {
295 /* Enable arbiters */
297 } else if ((pr != NULL) && pr->flags.bm_control) {
298 /* Enable bus master arbitration */
299 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0,
300 ACPI_MTX_DO_NOT_LOCK);
302 outb(pic2_mask,0xA1); /* restore mask */
303 outb(pic1_mask,0x21);
305 local_irq_restore(flags);
308 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
312 * Centaur decided to make life a little more tricky.
313 * Only longhaul v1 is allowed to read EBLCR BSEL[0:1].
314 * Samuel2 and above have to try and guess what the FSB is.
315 * We do this by assuming we booted at maximum multiplier, and interpolate
316 * between that value multiplied by possible FSBs and cpu_mhz which
317 * was calculated at boot time. Really ugly, but no other way to do this.
322 static int guess_fsb(int mult)
324 int speed = cpu_khz / 1000;
326 int speeds[] = { 666, 1000, 1333, 2000 };
329 for (i = 0; i < 4; i++) {
330 f_max = ((speeds[i] * mult) + 50) / 100;
331 f_max += (ROUNDING / 2);
332 f_min = f_max - ROUNDING;
333 if ((speed <= f_max) && (speed >= f_min))
334 return speeds[i] / 10;
340 static int __init longhaul_get_ranges(void)
342 unsigned int j, k = 0;
345 /* Get current frequency */
346 mult = longhaul_get_cpu_mult();
348 printk(KERN_INFO PFX "Invalid (reserved) multiplier!\n");
351 fsb = guess_fsb(mult);
353 printk(KERN_INFO PFX "Invalid (reserved) FSB!\n");
356 /* Get max multiplier - as we always did.
357 * Longhaul MSR is usefull only when voltage scaling is enabled.
358 * C3 is booting at max anyway. */
360 /* Get min multiplier */
361 switch (longhaul_version) {
362 case TYPE_LONGHAUL_V1:
363 case TYPE_LONGHAUL_V2:
367 case TYPE_POWERSAVER:
369 if (cpu_model == CPU_EZRA_T)
372 else if (cpu_model == CPU_NEHEMIAH)
375 else if (cpu_model == CPU_NEHEMIAH_C)
380 dprintk ("MinMult:%d.%dx MaxMult:%d.%dx\n",
381 minmult/10, minmult%10, maxmult/10, maxmult%10);
383 highest_speed = calc_speed(maxmult);
384 lowest_speed = calc_speed(minmult);
385 dprintk ("FSB:%dMHz Lowest speed: %s Highest speed:%s\n", fsb,
386 print_speed(lowest_speed/1000),
387 print_speed(highest_speed/1000));
389 if (lowest_speed == highest_speed) {
390 printk (KERN_INFO PFX "highestspeed == lowest, aborting.\n");
393 if (lowest_speed > highest_speed) {
394 printk (KERN_INFO PFX "nonsense! lowest (%d > %d) !\n",
395 lowest_speed, highest_speed);
399 longhaul_table = kmalloc((numscales + 1) * sizeof(struct cpufreq_frequency_table), GFP_KERNEL);
403 for (j=0; j < numscales; j++) {
405 ratio = clock_ratio[j];
408 if (ratio > maxmult || ratio < minmult)
410 longhaul_table[k].frequency = calc_speed(ratio);
411 longhaul_table[k].index = j;
415 longhaul_table[k].frequency = CPUFREQ_TABLE_END;
417 kfree (longhaul_table);
425 static void __init longhaul_setup_voltagescaling(void)
427 union msr_longhaul longhaul;
428 struct mV_pos minvid, maxvid;
429 unsigned int j, speed, pos, kHz_step, numvscales;
431 rdmsrl(MSR_VIA_LONGHAUL, longhaul.val);
432 if (!(longhaul.bits.RevisionID & 1)) {
433 printk(KERN_INFO PFX "Voltage scaling not supported by CPU.\n");
437 if (!longhaul.bits.VRMRev) {
438 printk (KERN_INFO PFX "VRM 8.5\n");
439 vrm_mV_table = &vrm85_mV[0];
440 mV_vrm_table = &mV_vrm85[0];
442 printk (KERN_INFO PFX "Mobile VRM\n");
443 vrm_mV_table = &mobilevrm_mV[0];
444 mV_vrm_table = &mV_mobilevrm[0];
447 minvid = vrm_mV_table[longhaul.bits.MinimumVID];
448 maxvid = vrm_mV_table[longhaul.bits.MaximumVID];
449 numvscales = maxvid.pos - minvid.pos + 1;
450 kHz_step = (highest_speed - lowest_speed) / numvscales;
452 if (minvid.mV == 0 || maxvid.mV == 0 || minvid.mV > maxvid.mV) {
453 printk (KERN_INFO PFX "Bogus values Min:%d.%03d Max:%d.%03d. "
454 "Voltage scaling disabled.\n",
455 minvid.mV/1000, minvid.mV%1000, maxvid.mV/1000, maxvid.mV%1000);
459 if (minvid.mV == maxvid.mV) {
460 printk (KERN_INFO PFX "Claims to support voltage scaling but min & max are "
461 "both %d.%03d. Voltage scaling disabled\n",
462 maxvid.mV/1000, maxvid.mV%1000);
466 printk(KERN_INFO PFX "Max VID=%d.%03d Min VID=%d.%03d, %d possible voltage scales\n",
467 maxvid.mV/1000, maxvid.mV%1000,
468 minvid.mV/1000, minvid.mV%1000,
472 while (longhaul_table[j].frequency != CPUFREQ_TABLE_END) {
473 speed = longhaul_table[j].frequency;
474 pos = (speed - lowest_speed) / kHz_step + minvid.pos;
475 f_msr_table[longhaul_table[j].index].vrm = mV_vrm_table[pos];
479 can_scale_voltage = 1;
483 static int longhaul_verify(struct cpufreq_policy *policy)
485 return cpufreq_frequency_table_verify(policy, longhaul_table);
489 static int longhaul_target(struct cpufreq_policy *policy,
490 unsigned int target_freq, unsigned int relation)
492 unsigned int table_index = 0;
493 unsigned int new_clock_ratio = 0;
495 if (cpufreq_frequency_table_target(policy, longhaul_table, target_freq, relation, &table_index))
498 new_clock_ratio = longhaul_table[table_index].index & 0xFF;
500 longhaul_setstate(new_clock_ratio);
506 static unsigned int longhaul_get(unsigned int cpu)
510 return calc_speed(longhaul_get_cpu_mult());
513 static acpi_status longhaul_walk_callback(acpi_handle obj_handle,
515 void *context, void **return_value)
517 struct acpi_device *d;
519 if ( acpi_bus_get_device(obj_handle, &d) ) {
522 *return_value = (void *)acpi_driver_data(d);
526 /* VIA don't support PM2 reg, but have something similar */
527 static int enable_arbiter_disable(void)
533 /* Find PLE133 host bridge */
535 dev = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8601_0, NULL);
536 /* Find CLE266 host bridge */
539 dev = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_862X_0, NULL);
540 /* Find CN400 V-Link host bridge */
542 dev = pci_find_device(PCI_VENDOR_ID_VIA, 0x7259, NULL);
546 /* Enable access to port 0x22 */
547 pci_read_config_byte(dev, reg, &pci_cmd);
548 if (!(pci_cmd & 1<<7)) {
550 pci_write_config_byte(dev, reg, pci_cmd);
551 pci_read_config_byte(dev, reg, &pci_cmd);
552 if (!(pci_cmd & 1<<7)) {
554 "Can't enable access to port 0x22.\n");
563 static int longhaul_setup_vt8235(void)
568 /* Find VT8235 southbridge */
569 dev = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8235, NULL);
571 /* Set transition time to max */
572 pci_read_config_byte(dev, 0xec, &pci_cmd);
573 pci_cmd &= ~(1 << 2);
574 pci_write_config_byte(dev, 0xec, pci_cmd);
575 pci_read_config_byte(dev, 0xe4, &pci_cmd);
576 pci_cmd &= ~(1 << 7);
577 pci_write_config_byte(dev, 0xe4, pci_cmd);
578 pci_read_config_byte(dev, 0xe5, &pci_cmd);
580 pci_write_config_byte(dev, 0xe5, pci_cmd);
586 static int __init longhaul_cpu_init(struct cpufreq_policy *policy)
588 struct cpuinfo_x86 *c = cpu_data;
593 /* Check what we have on this motherboard */
594 switch (c->x86_model) {
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));
604 longhaul_version = TYPE_LONGHAUL_V1;
605 switch (c->x86_mask) {
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));
614 if (c->x86_mask < 8) {
615 cpu_model = CPU_SAMUEL2;
616 cpuname = "C3 'Samuel 2' [C5B]";
618 cpu_model = CPU_EZRA;
619 cpuname = "C3 'Ezra' [C5C]";
621 memcpy (clock_ratio, ezra_clock_ratio, sizeof(ezra_clock_ratio));
622 memcpy (eblcr_table, ezra_eblcr, sizeof(ezra_eblcr));
628 cpu_model = CPU_EZRA_T;
629 cpuname = "C3 'Ezra-T' [C5M]";
630 longhaul_version = TYPE_POWERSAVER;
632 memcpy (clock_ratio, ezrat_clock_ratio, sizeof(ezrat_clock_ratio));
633 memcpy (eblcr_table, ezrat_eblcr, sizeof(ezrat_eblcr));
637 longhaul_version = TYPE_POWERSAVER;
640 nehemiah_clock_ratio,
641 sizeof(nehemiah_clock_ratio));
642 memcpy(eblcr_table, nehemiah_eblcr, sizeof(nehemiah_eblcr));
643 switch (c->x86_mask) {
645 cpu_model = CPU_NEHEMIAH;
646 cpuname = "C3 'Nehemiah A' [C5N]";
649 cpu_model = CPU_NEHEMIAH;
650 cpuname = "C3 'Nehemiah B' [C5N]";
653 cpu_model = CPU_NEHEMIAH_C;
654 cpuname = "C3 'Nehemiah C' [C5N]";
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);
670 case TYPE_POWERSAVER:
671 printk ("Powersaver supported.\n");
676 vt8235_present = longhaul_setup_vt8235();
678 /* Find ACPI data for processor */
679 acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
680 ACPI_UINT32_MAX, &longhaul_walk_callback,
683 /* Check ACPI support for C3 state */
684 if (pr != NULL && longhaul_version == TYPE_POWERSAVER) {
685 cx = &pr->power.states[ACPI_STATE_C3];
686 if (cx->address > 0 && cx->latency <= 1000) {
687 longhaul_flags |= USE_ACPI_C3;
688 goto print_support_type;
691 /* Check if northbridge is friendly */
692 if (enable_arbiter_disable()) {
693 longhaul_flags |= USE_NORTHBRIDGE;
694 goto print_support_type;
696 /* Use VT8235 southbridge if present */
697 if (longhaul_version == TYPE_POWERSAVER && vt8235_present) {
698 longhaul_flags |= USE_VT8235;
699 goto print_support_type;
701 /* Check ACPI support for bus master arbiter disable */
702 if ((pr == NULL) || !(pr->flags.bm_control)) {
704 "No ACPI support. Unsupported northbridge.\n");
709 if (longhaul_flags & USE_NORTHBRIDGE)
710 printk (KERN_INFO PFX "Using northbridge support.\n");
711 else if (longhaul_flags & USE_VT8235)
712 printk (KERN_INFO PFX "Using VT8235 support.\n");
714 printk (KERN_INFO PFX "Using ACPI support.\n");
716 ret = longhaul_get_ranges();
720 if ((longhaul_version != TYPE_LONGHAUL_V1) && (scale_voltage != 0))
721 longhaul_setup_voltagescaling();
723 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
724 policy->cpuinfo.transition_latency = 200000; /* nsec */
725 policy->cur = calc_speed(longhaul_get_cpu_mult());
727 ret = cpufreq_frequency_table_cpuinfo(policy, longhaul_table);
731 cpufreq_frequency_table_get_attr(longhaul_table, policy->cpu);
736 static int __devexit longhaul_cpu_exit(struct cpufreq_policy *policy)
738 cpufreq_frequency_table_put_attr(policy->cpu);
742 static struct freq_attr* longhaul_attr[] = {
743 &cpufreq_freq_attr_scaling_available_freqs,
747 static struct cpufreq_driver longhaul_driver = {
748 .verify = longhaul_verify,
749 .target = longhaul_target,
751 .init = longhaul_cpu_init,
752 .exit = __devexit_p(longhaul_cpu_exit),
754 .owner = THIS_MODULE,
755 .attr = longhaul_attr,
759 static int __init longhaul_init(void)
761 struct cpuinfo_x86 *c = cpu_data;
763 if (c->x86_vendor != X86_VENDOR_CENTAUR || c->x86 != 6)
767 if (num_online_cpus() > 1) {
768 printk(KERN_ERR PFX "More than 1 CPU detected, longhaul disabled.\n");
772 #ifdef CONFIG_X86_IO_APIC
774 printk(KERN_ERR PFX "APIC detected. Longhaul is currently broken in this configuration.\n");
778 switch (c->x86_model) {
780 return cpufreq_register_driver(&longhaul_driver);
782 printk(KERN_ERR PFX "Use acpi-cpufreq driver for VIA C7\n");
791 static void __exit longhaul_exit(void)
795 for (i=0; i < numscales; i++) {
796 if (clock_ratio[i] == maxmult) {
797 longhaul_setstate(i);
802 cpufreq_unregister_driver(&longhaul_driver);
803 kfree(longhaul_table);
806 module_param (scale_voltage, int, 0644);
807 MODULE_PARM_DESC(scale_voltage, "Scale voltage of processor");
809 MODULE_AUTHOR ("Dave Jones <davej@codemonkey.org.uk>");
810 MODULE_DESCRIPTION ("Longhaul driver for VIA Cyrix processors.");
811 MODULE_LICENSE ("GPL");
813 late_initcall(longhaul_init);
814 module_exit(longhaul_exit);