[CPUFREQ] Longhaul - Simplier minmult
[safe/jmp/linux-2.6] / arch / i386 / kernel / cpu / cpufreq / longhaul.c
1 /*
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>
30 #include <linux/pci.h>
31 #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>
37 #include <asm/acpi.h>
38 #include <linux/acpi.h>
39 #include <acpi/processor.h>
40
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 #define CPU_NEHEMIAH_C  6
55
56 /* Flags */
57 #define USE_ACPI_C3             (1 << 1)
58 #define USE_NORTHBRIDGE         (1 << 2)
59 #define USE_VT8235              (1 << 3)
60
61 static int cpu_model;
62 static unsigned int numscales=16;
63 static unsigned int fsb;
64
65 static struct mV_pos *vrm_mV_table;
66 static unsigned char *mV_vrm_table;
67 struct f_msr {
68         unsigned char vrm;
69 };
70 static struct f_msr f_msr_table[32];
71
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;
78
79 /* Module parameters */
80 static int scale_voltage;
81
82 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "longhaul", msg)
83
84
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;
90
91 #ifdef CONFIG_CPU_FREQ_DEBUG
92 static char speedbuffer[8];
93
94 static char *print_speed(int speed)
95 {
96         if (speed < 1000) {
97                 snprintf(speedbuffer, sizeof(speedbuffer),"%dMHz", speed);
98                 return speedbuffer;
99         }
100
101         if (speed%1000 == 0)
102                 snprintf(speedbuffer, sizeof(speedbuffer),
103                         "%dGHz", speed/1000);
104         else
105                 snprintf(speedbuffer, sizeof(speedbuffer),
106                         "%d.%dGHz", speed/1000, (speed%1000)/100);
107
108         return speedbuffer;
109 }
110 #endif
111
112
113 static unsigned int calc_speed(int mult)
114 {
115         int khz;
116         khz = (mult/10)*fsb;
117         if (mult%10)
118                 khz += fsb/2;
119         khz *= 1000;
120         return khz;
121 }
122
123
124 static int longhaul_get_cpu_mult(void)
125 {
126         unsigned long invalue=0,lo, hi;
127
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) {
131                 if (lo & (1<<27))
132                         invalue+=16;
133         }
134         return eblcr_table[invalue];
135 }
136
137 /* For processor with BCR2 MSR */
138
139 static void do_longhaul1(unsigned int clock_ratio_index)
140 {
141         union msr_bcr2 bcr2;
142
143         rdmsrl(MSR_VIA_BCR2, bcr2.val);
144         /* Enable software clock multiplier */
145         bcr2.bits.ESOFTBF = 1;
146         bcr2.bits.CLOCKMUL = clock_ratio_index;
147
148         /* Sync to timer tick */
149         safe_halt();
150         /* Change frequency on next halt or sleep */
151         wrmsrl(MSR_VIA_BCR2, bcr2.val);
152         /* Invoke transition */
153         ACPI_FLUSH_CPU_CACHE();
154         halt();
155
156         /* Disable software clock multiplier */
157         local_irq_disable();
158         rdmsrl(MSR_VIA_BCR2, bcr2.val);
159         bcr2.bits.ESOFTBF = 0;
160         wrmsrl(MSR_VIA_BCR2, bcr2.val);
161 }
162
163 /* For processor with Longhaul MSR */
164
165 static void do_powersaver(int cx_address, unsigned int clock_ratio_index)
166 {
167         union msr_longhaul longhaul;
168         u32 t;
169
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;
175
176         if (can_scale_voltage) {
177                 longhaul.bits.SoftVID = f_msr_table[clock_ratio_index].vrm;
178                 longhaul.bits.EnableSoftVID = 1;
179         }
180
181         /* Sync to timer tick */
182         safe_halt();
183         /* Change frequency on next halt or sleep */
184         wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
185         if (!cx_address) {
186                 ACPI_FLUSH_CPU_CACHE();
187                 /* Invoke C1 */
188                 halt();
189         } else {
190                 ACPI_FLUSH_CPU_CACHE();
191                 /* Invoke C3 */
192                 inb(cx_address);
193                 /* Dummy op - must do something useless after P_LVL3 read */
194                 t = inl(acpi_fadt.xpm_tmr_blk.address);
195         }
196         /* Disable bus ratio bit */
197         local_irq_disable();
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);
203 }
204
205 /**
206  * longhaul_set_cpu_frequency()
207  * @clock_ratio_index : bitpattern of the new multiplier.
208  *
209  * Sets a new clock ratio.
210  */
211
212 static void longhaul_setstate(unsigned int clock_ratio_index)
213 {
214         int speed, mult;
215         struct cpufreq_freqs freqs;
216         static unsigned int old_ratio=-1;
217         unsigned long flags;
218         unsigned int pic1_mask, pic2_mask;
219
220         if (old_ratio == clock_ratio_index)
221                 return;
222         old_ratio = clock_ratio_index;
223
224         mult = clock_ratio[clock_ratio_index];
225         if (mult == -1)
226                 return;
227
228         speed = calc_speed(mult);
229         if ((speed > highest_speed) || (speed < lowest_speed))
230                 return;
231
232         freqs.old = calc_speed(longhaul_get_cpu_mult());
233         freqs.new = speed;
234         freqs.cpu = 0; /* longhaul.c is UP only driver */
235
236         cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
237
238         dprintk ("Setting to FSB:%dMHz Mult:%d.%dx (%s)\n",
239                         fsb, mult/10, mult%10, print_speed(speed/1000));
240
241         preempt_disable();
242         local_irq_save(flags);
243
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 */
248
249         if (longhaul_flags & USE_NORTHBRIDGE) {
250                 /* Disable AGP and PCI arbiters */
251                 outb(3, 0x22);
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);
256         }
257         switch (longhaul_version) {
258
259         /*
260          * Longhaul v1. (Samuel[C5A] and Samuel2 stepping 0[C5B])
261          * Software controlled multipliers only.
262          *
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]
265          */
266         case TYPE_LONGHAUL_V1:
267         case TYPE_LONGHAUL_V2:
268                 do_longhaul1(clock_ratio_index);
269                 break;
270
271         /*
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'
275          * algorithm.
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.
281          */
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);
288                 } else {
289                         do_powersaver(0, clock_ratio_index);
290                 }
291                 break;
292         }
293
294         if (longhaul_flags & USE_NORTHBRIDGE) {
295                 /* Enable arbiters */
296                 outb(0, 0x22);
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);
301         }
302         outb(pic2_mask,0xA1);   /* restore mask */
303         outb(pic1_mask,0x21);
304
305         local_irq_restore(flags);
306         preempt_enable();
307
308         cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
309 }
310
311 /*
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.
318  */
319
320 #define ROUNDING        0xf
321
322 static int guess_fsb(int mult)
323 {
324         int speed = cpu_khz / 1000;
325         int i;
326         int speeds[] = { 666, 1000, 1333, 2000 };
327         int f_max, f_min;
328
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;
335         }
336         return 0;
337 }
338
339
340 static int __init longhaul_get_ranges(void)
341 {
342         unsigned int j, k = 0;
343         int mult;
344
345         /* Get current frequency */
346         mult = longhaul_get_cpu_mult();
347         if (mult == -1) {
348                 printk(KERN_INFO PFX "Invalid (reserved) multiplier!\n");
349                 return -EINVAL;
350         }
351         fsb = guess_fsb(mult);
352         if (fsb == 0) {
353                 printk(KERN_INFO PFX "Invalid (reserved) FSB!\n");
354                 return -EINVAL;
355         }
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. */
359         maxmult = mult;
360         /* Get min multiplier */
361         switch (cpu_model) {
362         case CPU_NEHEMIAH:
363                 minmult = 50;
364                 break;
365         case CPU_NEHEMIAH_C:
366                 minmult = 40;
367                 break;
368         default:
369                 minmult = 30;
370                 break;
371         }
372
373         dprintk ("MinMult:%d.%dx MaxMult:%d.%dx\n",
374                  minmult/10, minmult%10, maxmult/10, maxmult%10);
375
376         highest_speed = calc_speed(maxmult);
377         lowest_speed = calc_speed(minmult);
378         dprintk ("FSB:%dMHz  Lowest speed: %s   Highest speed:%s\n", fsb,
379                  print_speed(lowest_speed/1000), 
380                  print_speed(highest_speed/1000));
381
382         if (lowest_speed == highest_speed) {
383                 printk (KERN_INFO PFX "highestspeed == lowest, aborting.\n");
384                 return -EINVAL;
385         }
386         if (lowest_speed > highest_speed) {
387                 printk (KERN_INFO PFX "nonsense! lowest (%d > %d) !\n",
388                         lowest_speed, highest_speed);
389                 return -EINVAL;
390         }
391
392         longhaul_table = kmalloc((numscales + 1) * sizeof(struct cpufreq_frequency_table), GFP_KERNEL);
393         if(!longhaul_table)
394                 return -ENOMEM;
395
396         for (j=0; j < numscales; j++) {
397                 unsigned int ratio;
398                 ratio = clock_ratio[j];
399                 if (ratio == -1)
400                         continue;
401                 if (ratio > maxmult || ratio < minmult)
402                         continue;
403                 longhaul_table[k].frequency = calc_speed(ratio);
404                 longhaul_table[k].index = j;
405                 k++;
406         }
407
408         longhaul_table[k].frequency = CPUFREQ_TABLE_END;
409         if (!k) {
410                 kfree (longhaul_table);
411                 return -EINVAL;
412         }
413
414         return 0;
415 }
416
417
418 static void __init longhaul_setup_voltagescaling(void)
419 {
420         union msr_longhaul longhaul;
421         struct mV_pos minvid, maxvid;
422         unsigned int j, speed, pos, kHz_step, numvscales;
423
424         rdmsrl(MSR_VIA_LONGHAUL, longhaul.val);
425         if (!(longhaul.bits.RevisionID & 1)) {
426                 printk(KERN_INFO PFX "Voltage scaling not supported by CPU.\n");
427                 return;
428         }
429
430         if (!longhaul.bits.VRMRev) {
431                 printk (KERN_INFO PFX "VRM 8.5\n");
432                 vrm_mV_table = &vrm85_mV[0];
433                 mV_vrm_table = &mV_vrm85[0];
434         } else {
435                 printk (KERN_INFO PFX "Mobile VRM\n");
436                 vrm_mV_table = &mobilevrm_mV[0];
437                 mV_vrm_table = &mV_mobilevrm[0];
438         }
439
440         minvid = vrm_mV_table[longhaul.bits.MinimumVID];
441         maxvid = vrm_mV_table[longhaul.bits.MaximumVID];
442         numvscales = maxvid.pos - minvid.pos + 1;
443         kHz_step = (highest_speed - lowest_speed) / numvscales;
444
445         if (minvid.mV == 0 || maxvid.mV == 0 || minvid.mV > maxvid.mV) {
446                 printk (KERN_INFO PFX "Bogus values Min:%d.%03d Max:%d.%03d. "
447                                         "Voltage scaling disabled.\n",
448                                         minvid.mV/1000, minvid.mV%1000, maxvid.mV/1000, maxvid.mV%1000);
449                 return;
450         }
451
452         if (minvid.mV == maxvid.mV) {
453                 printk (KERN_INFO PFX "Claims to support voltage scaling but min & max are "
454                                 "both %d.%03d. Voltage scaling disabled\n",
455                                 maxvid.mV/1000, maxvid.mV%1000);
456                 return;
457         }
458
459         printk(KERN_INFO PFX "Max VID=%d.%03d  Min VID=%d.%03d, %d possible voltage scales\n",
460                 maxvid.mV/1000, maxvid.mV%1000,
461                 minvid.mV/1000, minvid.mV%1000,
462                 numvscales);
463         
464         j = 0;
465         while (longhaul_table[j].frequency != CPUFREQ_TABLE_END) {
466                 speed = longhaul_table[j].frequency;
467                 pos = (speed - lowest_speed) / kHz_step + minvid.pos;
468                 f_msr_table[longhaul_table[j].index].vrm = mV_vrm_table[pos];
469                 j++;
470         }
471
472         can_scale_voltage = 1;
473 }
474
475
476 static int longhaul_verify(struct cpufreq_policy *policy)
477 {
478         return cpufreq_frequency_table_verify(policy, longhaul_table);
479 }
480
481
482 static int longhaul_target(struct cpufreq_policy *policy,
483                             unsigned int target_freq, unsigned int relation)
484 {
485         unsigned int table_index = 0;
486         unsigned int new_clock_ratio = 0;
487
488         if (cpufreq_frequency_table_target(policy, longhaul_table, target_freq, relation, &table_index))
489                 return -EINVAL;
490
491         new_clock_ratio = longhaul_table[table_index].index & 0xFF;
492
493         longhaul_setstate(new_clock_ratio);
494
495         return 0;
496 }
497
498
499 static unsigned int longhaul_get(unsigned int cpu)
500 {
501         if (cpu)
502                 return 0;
503         return calc_speed(longhaul_get_cpu_mult());
504 }
505
506 static acpi_status longhaul_walk_callback(acpi_handle obj_handle,
507                                           u32 nesting_level,
508                                           void *context, void **return_value)
509 {
510         struct acpi_device *d;
511
512         if ( acpi_bus_get_device(obj_handle, &d) ) {
513                 return 0;
514         }
515         *return_value = (void *)acpi_driver_data(d);
516         return 1;
517 }
518
519 /* VIA don't support PM2 reg, but have something similar */
520 static int enable_arbiter_disable(void)
521 {
522         struct pci_dev *dev;
523         int reg;
524         u8 pci_cmd;
525
526         /* Find PLE133 host bridge */
527         reg = 0x78;
528         dev = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8601_0, NULL);
529         /* Find CLE266 host bridge */
530         if (dev == NULL) {
531                 reg = 0x76;
532                 dev = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_862X_0, NULL);
533                 /* Find CN400 V-Link host bridge */
534                 if (dev == NULL)
535                         dev = pci_find_device(PCI_VENDOR_ID_VIA, 0x7259, NULL);
536
537         }
538         if (dev != NULL) {
539                 /* Enable access to port 0x22 */
540                 pci_read_config_byte(dev, reg, &pci_cmd);
541                 if (!(pci_cmd & 1<<7)) {
542                         pci_cmd |= 1<<7;
543                         pci_write_config_byte(dev, reg, pci_cmd);
544                         pci_read_config_byte(dev, reg, &pci_cmd);
545                         if (!(pci_cmd & 1<<7)) {
546                                 printk(KERN_ERR PFX
547                                         "Can't enable access to port 0x22.\n");
548                                 return 0;
549                         }
550                 }
551                 return 1;
552         }
553         return 0;
554 }
555
556 static int longhaul_setup_vt8235(void)
557 {
558         struct pci_dev *dev;
559         u8 pci_cmd;
560
561         /* Find VT8235 southbridge */
562         dev = pci_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8235, NULL);
563         if (dev != NULL) {
564                 /* Set transition time to max */
565                 pci_read_config_byte(dev, 0xec, &pci_cmd);
566                 pci_cmd &= ~(1 << 2);
567                 pci_write_config_byte(dev, 0xec, pci_cmd);
568                 pci_read_config_byte(dev, 0xe4, &pci_cmd);
569                 pci_cmd &= ~(1 << 7);
570                 pci_write_config_byte(dev, 0xe4, pci_cmd);
571                 pci_read_config_byte(dev, 0xe5, &pci_cmd);
572                 pci_cmd |= 1 << 7;
573                 pci_write_config_byte(dev, 0xe5, pci_cmd);
574                 return 1;
575         }
576         return 0;
577 }
578
579 static int __init longhaul_cpu_init(struct cpufreq_policy *policy)
580 {
581         struct cpuinfo_x86 *c = cpu_data;
582         char *cpuname=NULL;
583         int ret;
584         int vt8235_present;
585
586         /* Check what we have on this motherboard */
587         switch (c->x86_model) {
588         case 6:
589                 cpu_model = CPU_SAMUEL;
590                 cpuname = "C3 'Samuel' [C5A]";
591                 longhaul_version = TYPE_LONGHAUL_V1;
592                 memcpy (clock_ratio, samuel1_clock_ratio, sizeof(samuel1_clock_ratio));
593                 memcpy (eblcr_table, samuel1_eblcr, sizeof(samuel1_eblcr));
594                 break;
595
596         case 7:
597                 longhaul_version = TYPE_LONGHAUL_V1;
598                 switch (c->x86_mask) {
599                 case 0:
600                         cpu_model = CPU_SAMUEL2;
601                         cpuname = "C3 'Samuel 2' [C5B]";
602                         /* Note, this is not a typo, early Samuel2's had Samuel1 ratios. */
603                         memcpy (clock_ratio, samuel1_clock_ratio, sizeof(samuel1_clock_ratio));
604                         memcpy (eblcr_table, samuel2_eblcr, sizeof(samuel2_eblcr));
605                         break;
606                 case 1 ... 15:
607                         if (c->x86_mask < 8) {
608                                 cpu_model = CPU_SAMUEL2;
609                                 cpuname = "C3 'Samuel 2' [C5B]";
610                         } else {
611                                 cpu_model = CPU_EZRA;
612                                 cpuname = "C3 'Ezra' [C5C]";
613                         }
614                         memcpy (clock_ratio, ezra_clock_ratio, sizeof(ezra_clock_ratio));
615                         memcpy (eblcr_table, ezra_eblcr, sizeof(ezra_eblcr));
616                         break;
617                 }
618                 break;
619
620         case 8:
621                 cpu_model = CPU_EZRA_T;
622                 cpuname = "C3 'Ezra-T' [C5M]";
623                 longhaul_version = TYPE_POWERSAVER;
624                 numscales=32;
625                 memcpy (clock_ratio, ezrat_clock_ratio, sizeof(ezrat_clock_ratio));
626                 memcpy (eblcr_table, ezrat_eblcr, sizeof(ezrat_eblcr));
627                 break;
628
629         case 9:
630                 longhaul_version = TYPE_POWERSAVER;
631                 numscales = 32;
632                 memcpy(clock_ratio,
633                        nehemiah_clock_ratio,
634                        sizeof(nehemiah_clock_ratio));
635                 memcpy(eblcr_table, nehemiah_eblcr, sizeof(nehemiah_eblcr));
636                 switch (c->x86_mask) {
637                 case 0 ... 1:
638                         cpu_model = CPU_NEHEMIAH;
639                         cpuname = "C3 'Nehemiah A' [C5N]";
640                         break;
641                 case 2 ... 4:
642                         cpu_model = CPU_NEHEMIAH;
643                         cpuname = "C3 'Nehemiah B' [C5N]";
644                         break;
645                 case 5 ... 15:
646                         cpu_model = CPU_NEHEMIAH_C;
647                         cpuname = "C3 'Nehemiah C' [C5N]";
648                         break;
649                 }
650                 break;
651
652         default:
653                 cpuname = "Unknown";
654                 break;
655         }
656
657         printk (KERN_INFO PFX "VIA %s CPU detected.  ", cpuname);
658         switch (longhaul_version) {
659         case TYPE_LONGHAUL_V1:
660         case TYPE_LONGHAUL_V2:
661                 printk ("Longhaul v%d supported.\n", longhaul_version);
662                 break;
663         case TYPE_POWERSAVER:
664                 printk ("Powersaver supported.\n");
665                 break;
666         };
667
668         /* Doesn't hurt */
669         vt8235_present = longhaul_setup_vt8235();
670
671         /* Find ACPI data for processor */
672         acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
673                                 ACPI_UINT32_MAX, &longhaul_walk_callback,
674                                 NULL, (void *)&pr);
675
676         /* Check ACPI support for C3 state */
677         if (pr != NULL && longhaul_version == TYPE_POWERSAVER) {
678                 cx = &pr->power.states[ACPI_STATE_C3];
679                 if (cx->address > 0 && cx->latency <= 1000) {
680                         longhaul_flags |= USE_ACPI_C3;
681                         goto print_support_type;
682                 }
683         }
684         /* Check if northbridge is friendly */
685         if (enable_arbiter_disable()) {
686                 longhaul_flags |= USE_NORTHBRIDGE;
687                 goto print_support_type;
688         }
689         /* Use VT8235 southbridge if present */
690         if (longhaul_version == TYPE_POWERSAVER && vt8235_present) {
691                 longhaul_flags |= USE_VT8235;
692                 goto print_support_type;
693         }
694         /* Check ACPI support for bus master arbiter disable */
695         if ((pr == NULL) || !(pr->flags.bm_control)) {
696                 printk(KERN_ERR PFX
697                         "No ACPI support. Unsupported northbridge.\n");
698                 return -ENODEV;
699         }
700
701 print_support_type:
702         if (longhaul_flags & USE_NORTHBRIDGE)
703                 printk (KERN_INFO PFX "Using northbridge support.\n");
704         else if (longhaul_flags & USE_VT8235)
705                 printk (KERN_INFO PFX "Using VT8235 support.\n");
706         else
707                 printk (KERN_INFO PFX "Using ACPI support.\n");
708
709         ret = longhaul_get_ranges();
710         if (ret != 0)
711                 return ret;
712
713         if ((longhaul_version != TYPE_LONGHAUL_V1) && (scale_voltage != 0))
714                 longhaul_setup_voltagescaling();
715
716         policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
717         policy->cpuinfo.transition_latency = 200000;    /* nsec */
718         policy->cur = calc_speed(longhaul_get_cpu_mult());
719
720         ret = cpufreq_frequency_table_cpuinfo(policy, longhaul_table);
721         if (ret)
722                 return ret;
723
724         cpufreq_frequency_table_get_attr(longhaul_table, policy->cpu);
725
726         return 0;
727 }
728
729 static int __devexit longhaul_cpu_exit(struct cpufreq_policy *policy)
730 {
731         cpufreq_frequency_table_put_attr(policy->cpu);
732         return 0;
733 }
734
735 static struct freq_attr* longhaul_attr[] = {
736         &cpufreq_freq_attr_scaling_available_freqs,
737         NULL,
738 };
739
740 static struct cpufreq_driver longhaul_driver = {
741         .verify = longhaul_verify,
742         .target = longhaul_target,
743         .get    = longhaul_get,
744         .init   = longhaul_cpu_init,
745         .exit   = __devexit_p(longhaul_cpu_exit),
746         .name   = "longhaul",
747         .owner  = THIS_MODULE,
748         .attr   = longhaul_attr,
749 };
750
751
752 static int __init longhaul_init(void)
753 {
754         struct cpuinfo_x86 *c = cpu_data;
755
756         if (c->x86_vendor != X86_VENDOR_CENTAUR || c->x86 != 6)
757                 return -ENODEV;
758
759 #ifdef CONFIG_SMP
760         if (num_online_cpus() > 1) {
761                 printk(KERN_ERR PFX "More than 1 CPU detected, longhaul disabled.\n");
762                 return -ENODEV;
763         }
764 #endif
765 #ifdef CONFIG_X86_IO_APIC
766         if (cpu_has_apic) {
767                 printk(KERN_ERR PFX "APIC detected. Longhaul is currently broken in this configuration.\n");
768                 return -ENODEV;
769         }
770 #endif
771         switch (c->x86_model) {
772         case 6 ... 9:
773                 return cpufreq_register_driver(&longhaul_driver);
774         case 10:
775                 printk(KERN_ERR PFX "Use acpi-cpufreq driver for VIA C7\n");
776         default:
777                 ;;
778         }
779
780         return -ENODEV;
781 }
782
783
784 static void __exit longhaul_exit(void)
785 {
786         int i;
787
788         for (i=0; i < numscales; i++) {
789                 if (clock_ratio[i] == maxmult) {
790                         longhaul_setstate(i);
791                         break;
792                 }
793         }
794
795         cpufreq_unregister_driver(&longhaul_driver);
796         kfree(longhaul_table);
797 }
798
799 module_param (scale_voltage, int, 0644);
800 MODULE_PARM_DESC(scale_voltage, "Scale voltage of processor");
801
802 MODULE_AUTHOR ("Dave Jones <davej@codemonkey.org.uk>");
803 MODULE_DESCRIPTION ("Longhaul driver for VIA Cyrix processors.");
804 MODULE_LICENSE ("GPL");
805
806 late_initcall(longhaul_init);
807 module_exit(longhaul_exit);