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