blob: 4e18d5119828767d88f2dddb5bda343b971aeb82 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Dave Jonesf4432c52008-10-20 13:31:45 -04002 * (C) 2001-2004 Dave Jones. <davej@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * (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.
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +010011 * Version 2 of longhaul is backward compatible with v1, but adds
12 * LONGHAUL MSR for purpose of both frequency and voltage scaling.
13 * Present in Samuel 2 (steppings 1-7 only) (C5B), and Ezra (C5C).
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * Version 3 of longhaul got renamed to Powersaver and redesigned
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +010015 * to use only the POWERSAVER MSR at 0x110a.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * It is present in Ezra-T (C5M), Nehemiah (C5X) and above.
17 * It's pretty much the same feature wise to longhaul v2, though
18 * there is provision for scaling FSB too, but this doesn't work
19 * too well in practice so we don't even try to use this.
20 *
21 * BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
22 */
23
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/init.h>
28#include <linux/cpufreq.h>
Rafa³ Bilski179da8e2006-08-08 19:12:20 +020029#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/slab.h>
31#include <linux/string.h>
Rafał Bilski73e107d2007-05-28 21:56:19 +020032#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include <asm/msr.h>
35#include <asm/timex.h>
36#include <asm/io.h>
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +020037#include <asm/acpi.h>
38#include <linux/acpi.h>
39#include <acpi/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include "longhaul.h"
42
43#define PFX "longhaul: "
44
45#define TYPE_LONGHAUL_V1 1
46#define TYPE_LONGHAUL_V2 2
47#define TYPE_POWERSAVER 3
48
49#define CPU_SAMUEL 1
50#define CPU_SAMUEL2 2
51#define CPU_EZRA 3
52#define CPU_EZRA_T 4
53#define CPU_NEHEMIAH 5
Rafa³ Bilski980342a2007-01-31 23:42:47 +010054#define CPU_NEHEMIAH_C 6
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Rafa³ Bilski264166e2006-12-24 14:04:23 +010056/* Flags */
57#define USE_ACPI_C3 (1 << 1)
58#define USE_NORTHBRIDGE (1 << 2)
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static int cpu_model;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +020061static unsigned int numscales=16;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static unsigned int fsb;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +020063
Dave Jonesbd5ab262007-02-22 19:11:16 -050064static const struct mV_pos *vrm_mV_table;
65static const unsigned char *mV_vrm_table;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +020066
67static unsigned int highest_speed, lowest_speed; /* kHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static unsigned int minmult, maxmult;
69static int can_scale_voltage;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +020070static struct acpi_processor *pr = NULL;
71static struct acpi_processor_cx *cx = NULL;
Rafał Bilski275bc6b2007-06-05 22:08:50 +020072static u32 acpi_regs_addr;
Rafa³ Bilski264166e2006-12-24 14:04:23 +010073static u8 longhaul_flags;
Rafał Bilski73e107d2007-05-28 21:56:19 +020074static unsigned int longhaul_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76/* Module parameters */
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +020077static int scale_voltage;
Rafał Bilski905497c2007-07-08 21:51:26 +020078static int disable_acpi_c3;
Rafal Bilski52a26382007-10-07 00:24:32 -070079static int revid_errata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "longhaul", msg)
82
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/* Clock ratios multiplied by 10 */
85static int clock_ratio[32];
86static int eblcr_table[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static int longhaul_version;
88static struct cpufreq_frequency_table *longhaul_table;
89
90#ifdef CONFIG_CPU_FREQ_DEBUG
91static char speedbuffer[8];
92
93static char *print_speed(int speed)
94{
Dave Jonese2aa8732006-05-30 17:37:15 -040095 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 return speedbuffer;
108}
109#endif
110
111
112static 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
123static 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
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200136/* For processor with BCR2 MSR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200138static void do_longhaul1(unsigned int clock_ratio_index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200140 union msr_bcr2 bcr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200142 rdmsrl(MSR_VIA_BCR2, bcr2.val);
143 /* Enable software clock multiplier */
144 bcr2.bits.ESOFTBF = 1;
Rafał Bilski73e107d2007-05-28 21:56:19 +0200145 bcr2.bits.CLOCKMUL = clock_ratio_index & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200147 /* Sync to timer tick */
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -0700148 safe_halt();
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200149 /* Change frequency on next halt or sleep */
150 wrmsrl(MSR_VIA_BCR2, bcr2.val);
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200151 /* Invoke transition */
152 ACPI_FLUSH_CPU_CACHE();
153 halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200155 /* Disable software clock multiplier */
Dave Jones3be6a482005-05-31 19:03:51 -0700156 local_irq_disable();
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200157 rdmsrl(MSR_VIA_BCR2, bcr2.val);
158 bcr2.bits.ESOFTBF = 0;
159 wrmsrl(MSR_VIA_BCR2, bcr2.val);
160}
Dave Jones3be6a482005-05-31 19:03:51 -0700161
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200162/* For processor with Longhaul MSR */
Dave Jones11746312005-05-31 19:03:51 -0700163
Rafał Bilski73e107d2007-05-28 21:56:19 +0200164static void do_powersaver(int cx_address, unsigned int clock_ratio_index,
165 unsigned int dir)
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200166{
167 union msr_longhaul longhaul;
168 u32 t;
Dave Jones3be6a482005-05-31 19:03:51 -0700169
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200170 rdmsrl(MSR_VIA_LONGHAUL, longhaul.val);
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100171 /* Setup new frequency */
Rafal Bilski52a26382007-10-07 00:24:32 -0700172 if (!revid_errata)
173 longhaul.bits.RevisionKey = longhaul.bits.RevisionID;
174 else
175 longhaul.bits.RevisionKey = 0;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200176 longhaul.bits.SoftBusRatio = clock_ratio_index & 0xf;
177 longhaul.bits.SoftBusRatio4 = (clock_ratio_index & 0x10) >> 4;
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100178 /* Setup new voltage */
179 if (can_scale_voltage)
Rafał Bilski73e107d2007-05-28 21:56:19 +0200180 longhaul.bits.SoftVID = (clock_ratio_index >> 8) & 0x1f;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200181 /* Sync to timer tick */
182 safe_halt();
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100183 /* Raise voltage if necessary */
Rafał Bilski73e107d2007-05-28 21:56:19 +0200184 if (can_scale_voltage && dir) {
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100185 longhaul.bits.EnableSoftVID = 1;
186 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
187 /* Change voltage */
188 if (!cx_address) {
189 ACPI_FLUSH_CPU_CACHE();
190 halt();
191 } else {
192 ACPI_FLUSH_CPU_CACHE();
193 /* Invoke C3 */
194 inb(cx_address);
195 /* Dummy op - must do something useless after P_LVL3
196 * read */
Dave Jonesbd0561c2007-02-10 20:36:29 -0500197 t = inl(acpi_gbl_FADT.xpm_timer_block.address);
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100198 }
199 longhaul.bits.EnableSoftVID = 0;
200 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100201 }
202
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200203 /* Change frequency on next halt or sleep */
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100204 longhaul.bits.EnableSoftBusRatio = 1;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200205 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100206 if (!cx_address) {
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200207 ACPI_FLUSH_CPU_CACHE();
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200208 halt();
209 } else {
210 ACPI_FLUSH_CPU_CACHE();
211 /* Invoke C3 */
212 inb(cx_address);
213 /* Dummy op - must do something useless after P_LVL3 read */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300214 t = inl(acpi_gbl_FADT.xpm_timer_block.address);
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200215 }
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200216 /* Disable bus ratio bit */
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200217 longhaul.bits.EnableSoftBusRatio = 0;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200218 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100219
220 /* Reduce voltage if necessary */
Rafał Bilski73e107d2007-05-28 21:56:19 +0200221 if (can_scale_voltage && !dir) {
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100222 longhaul.bits.EnableSoftVID = 1;
223 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
224 /* Change voltage */
225 if (!cx_address) {
226 ACPI_FLUSH_CPU_CACHE();
227 halt();
228 } else {
229 ACPI_FLUSH_CPU_CACHE();
230 /* Invoke C3 */
231 inb(cx_address);
232 /* Dummy op - must do something useless after P_LVL3
233 * read */
Dave Jonesbd0561c2007-02-10 20:36:29 -0500234 t = inl(acpi_gbl_FADT.xpm_timer_block.address);
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100235 }
236 longhaul.bits.EnableSoftVID = 0;
237 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
241/**
242 * longhaul_set_cpu_frequency()
243 * @clock_ratio_index : bitpattern of the new multiplier.
244 *
245 * Sets a new clock ratio.
246 */
247
Rafał Bilski73e107d2007-05-28 21:56:19 +0200248static void longhaul_setstate(unsigned int table_index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Rafał Bilski73e107d2007-05-28 21:56:19 +0200250 unsigned int clock_ratio_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 int speed, mult;
252 struct cpufreq_freqs freqs;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200253 unsigned long flags;
254 unsigned int pic1_mask, pic2_mask;
Rafał Bilski689eba72007-06-07 22:31:24 +0200255 u16 bm_status = 0;
Rafał Bilski275bc6b2007-06-05 22:08:50 +0200256 u32 bm_timeout = 1000;
Rafał Bilski73e107d2007-05-28 21:56:19 +0200257 unsigned int dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Rafał Bilski73e107d2007-05-28 21:56:19 +0200259 clock_ratio_index = longhaul_table[table_index].index;
260 /* Safety precautions */
261 mult = clock_ratio[clock_ratio_index & 0x1f];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (mult == -1)
263 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 speed = calc_speed(mult);
265 if ((speed > highest_speed) || (speed < lowest_speed))
266 return;
Rafał Bilski73e107d2007-05-28 21:56:19 +0200267 /* Voltage transition before frequency transition? */
268 if (can_scale_voltage && longhaul_index < table_index)
269 dir = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 freqs.old = calc_speed(longhaul_get_cpu_mult());
272 freqs.new = speed;
273 freqs.cpu = 0; /* longhaul.c is UP only driver */
274
275 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
276
277 dprintk ("Setting to FSB:%dMHz Mult:%d.%dx (%s)\n",
278 fsb, mult/10, mult%10, print_speed(speed/1000));
Rafal Bilski52a26382007-10-07 00:24:32 -0700279retry_loop:
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200280 preempt_disable();
281 local_irq_save(flags);
282
283 pic2_mask = inb(0xA1);
284 pic1_mask = inb(0x21); /* works on C3. save mask. */
285 outb(0xFF,0xA1); /* Overkill */
286 outb(0xFE,0x21); /* TMR0 only */
287
Rafał Bilski489dc5c2007-05-17 22:39:02 +0200288 /* Wait while PCI bus is busy. */
Rafał Bilski689eba72007-06-07 22:31:24 +0200289 if (acpi_regs_addr && (longhaul_flags & USE_NORTHBRIDGE
290 || ((pr != NULL) && pr->flags.bm_control))) {
291 bm_status = inw(acpi_regs_addr);
Rafał Bilski275bc6b2007-06-05 22:08:50 +0200292 bm_status &= 1 << 4;
Rafał Bilski489dc5c2007-05-17 22:39:02 +0200293 while (bm_status && bm_timeout) {
Rafał Bilski689eba72007-06-07 22:31:24 +0200294 outw(1 << 4, acpi_regs_addr);
Rafał Bilski489dc5c2007-05-17 22:39:02 +0200295 bm_timeout--;
Rafał Bilski689eba72007-06-07 22:31:24 +0200296 bm_status = inw(acpi_regs_addr);
Rafał Bilski275bc6b2007-06-05 22:08:50 +0200297 bm_status &= 1 << 4;
Rafał Bilski489dc5c2007-05-17 22:39:02 +0200298 }
299 }
300
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100301 if (longhaul_flags & USE_NORTHBRIDGE) {
302 /* Disable AGP and PCI arbiters */
303 outb(3, 0x22);
304 } else if ((pr != NULL) && pr->flags.bm_control) {
Rafał Bilski73e107d2007-05-28 21:56:19 +0200305 /* Disable bus master arbitration */
Lin Mingfb318cb2009-03-18 09:09:01 +0800306 acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 1);
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 switch (longhaul_version) {
309
310 /*
311 * Longhaul v1. (Samuel[C5A] and Samuel2 stepping 0[C5B])
312 * Software controlled multipliers only.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 */
314 case TYPE_LONGHAUL_V1:
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200315 do_longhaul1(clock_ratio_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 break;
317
318 /*
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +0100319 * Longhaul v2 appears in Samuel2 Steppings 1->7 [C5B] and Ezra [C5C]
320 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 * Longhaul v3 (aka Powersaver). (Ezra-T [C5M] & Nehemiah [C5N])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 * Nehemiah can do FSB scaling too, but this has never been proven
323 * to work in practice.
324 */
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +0100325 case TYPE_LONGHAUL_V2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 case TYPE_POWERSAVER:
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100327 if (longhaul_flags & USE_ACPI_C3) {
328 /* Don't allow wakeup */
Lin Mingfb318cb2009-03-18 09:09:01 +0800329 acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
Rafał Bilski73e107d2007-05-28 21:56:19 +0200330 do_powersaver(cx->address, clock_ratio_index, dir);
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100331 } else {
Rafał Bilski73e107d2007-05-28 21:56:19 +0200332 do_powersaver(0, clock_ratio_index, dir);
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 break;
335 }
336
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100337 if (longhaul_flags & USE_NORTHBRIDGE) {
338 /* Enable arbiters */
339 outb(0, 0x22);
340 } else if ((pr != NULL) && pr->flags.bm_control) {
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200341 /* Enable bus master arbitration */
Lin Mingfb318cb2009-03-18 09:09:01 +0800342 acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 0);
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200343 }
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200344 outb(pic2_mask,0xA1); /* restore mask */
345 outb(pic1_mask,0x21);
346
347 local_irq_restore(flags);
348 preempt_enable();
349
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +0100350 freqs.new = calc_speed(longhaul_get_cpu_mult());
Rafal Bilski52a26382007-10-07 00:24:32 -0700351 /* Check if requested frequency is set. */
352 if (unlikely(freqs.new != speed)) {
353 printk(KERN_INFO PFX "Failed to set requested frequency!\n");
354 /* Revision ID = 1 but processor is expecting revision key
355 * equal to 0. Jumpers at the bottom of processor will change
356 * multiplier and FSB, but will not change bits in Longhaul
357 * MSR nor enable voltage scaling. */
358 if (!revid_errata) {
359 printk(KERN_INFO PFX "Enabling \"Ignore Revision ID\" "
360 "option.\n");
361 revid_errata = 1;
362 msleep(200);
363 goto retry_loop;
364 }
365 /* Why ACPI C3 sometimes doesn't work is a mystery for me.
366 * But it does happen. Processor is entering ACPI C3 state,
367 * but it doesn't change frequency. I tried poking various
368 * bits in northbridge registers, but without success. */
369 if (longhaul_flags & USE_ACPI_C3) {
370 printk(KERN_INFO PFX "Disabling ACPI C3 support.\n");
371 longhaul_flags &= ~USE_ACPI_C3;
372 if (revid_errata) {
373 printk(KERN_INFO PFX "Disabling \"Ignore "
374 "Revision ID\" option.\n");
375 revid_errata = 0;
376 }
377 msleep(200);
378 goto retry_loop;
379 }
380 /* This shouldn't happen. Longhaul ver. 2 was reported not
381 * working on processors without voltage scaling, but with
382 * RevID = 1. RevID errata will make things right. Just
383 * to be 100% sure. */
384 if (longhaul_version == TYPE_LONGHAUL_V2) {
385 printk(KERN_INFO PFX "Switching to Longhaul ver. 1\n");
386 longhaul_version = TYPE_LONGHAUL_V1;
387 msleep(200);
388 goto retry_loop;
389 }
390 }
391 /* Report true CPU frequency */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
Rafał Bilski489dc5c2007-05-17 22:39:02 +0200393
394 if (!bm_timeout)
Rafał Bilski275bc6b2007-06-05 22:08:50 +0200395 printk(KERN_INFO PFX "Warning: Timeout while waiting for idle PCI bus.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
398/*
399 * Centaur decided to make life a little more tricky.
400 * Only longhaul v1 is allowed to read EBLCR BSEL[0:1].
401 * Samuel2 and above have to try and guess what the FSB is.
402 * We do this by assuming we booted at maximum multiplier, and interpolate
403 * between that value multiplied by possible FSBs and cpu_mhz which
404 * was calculated at boot time. Really ugly, but no other way to do this.
405 */
406
407#define ROUNDING 0xf
408
Rafa³ Bilski24ebead2007-01-01 23:49:34 +0100409static int guess_fsb(int mult)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Rafa³ Bilski46ef9552007-02-04 15:58:46 +0100411 int speed = cpu_khz / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 int i;
Rafa³ Bilski46ef9552007-02-04 15:58:46 +0100413 int speeds[] = { 666, 1000, 1333, 2000 };
414 int f_max, f_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Rafa³ Bilski46ef9552007-02-04 15:58:46 +0100416 for (i = 0; i < 4; i++) {
417 f_max = ((speeds[i] * mult) + 50) / 100;
418 f_max += (ROUNDING / 2);
419 f_min = f_max - ROUNDING;
420 if ((speed <= f_max) && (speed >= f_min))
421 return speeds[i] / 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423 return 0;
424}
425
426
427static int __init longhaul_get_ranges(void)
428{
Rafał Bilski73e107d2007-05-28 21:56:19 +0200429 unsigned int i, j, k = 0;
430 unsigned int ratio;
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100431 int mult;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100433 /* Get current frequency */
434 mult = longhaul_get_cpu_mult();
435 if (mult == -1) {
436 printk(KERN_INFO PFX "Invalid (reserved) multiplier!\n");
437 return -EINVAL;
438 }
439 fsb = guess_fsb(mult);
440 if (fsb == 0) {
441 printk(KERN_INFO PFX "Invalid (reserved) FSB!\n");
442 return -EINVAL;
443 }
444 /* Get max multiplier - as we always did.
445 * Longhaul MSR is usefull only when voltage scaling is enabled.
446 * C3 is booting at max anyway. */
447 maxmult = mult;
448 /* Get min multiplier */
Rafa³ Bilski9addf3b2007-02-07 22:53:29 +0100449 switch (cpu_model) {
450 case CPU_NEHEMIAH:
451 minmult = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 break;
Rafa³ Bilski9addf3b2007-02-07 22:53:29 +0100453 case CPU_NEHEMIAH_C:
454 minmult = 40;
455 break;
456 default:
457 minmult = 30;
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100458 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460
461 dprintk ("MinMult:%d.%dx MaxMult:%d.%dx\n",
462 minmult/10, minmult%10, maxmult/10, maxmult%10);
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 highest_speed = calc_speed(maxmult);
465 lowest_speed = calc_speed(minmult);
466 dprintk ("FSB:%dMHz Lowest speed: %s Highest speed:%s\n", fsb,
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300467 print_speed(lowest_speed/1000),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 print_speed(highest_speed/1000));
469
470 if (lowest_speed == highest_speed) {
471 printk (KERN_INFO PFX "highestspeed == lowest, aborting.\n");
472 return -EINVAL;
473 }
474 if (lowest_speed > highest_speed) {
475 printk (KERN_INFO PFX "nonsense! lowest (%d > %d) !\n",
476 lowest_speed, highest_speed);
477 return -EINVAL;
478 }
479
480 longhaul_table = kmalloc((numscales + 1) * sizeof(struct cpufreq_frequency_table), GFP_KERNEL);
481 if(!longhaul_table)
482 return -ENOMEM;
483
Rafał Bilski73e107d2007-05-28 21:56:19 +0200484 for (j = 0; j < numscales; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 ratio = clock_ratio[j];
486 if (ratio == -1)
487 continue;
488 if (ratio > maxmult || ratio < minmult)
489 continue;
490 longhaul_table[k].frequency = calc_speed(ratio);
491 longhaul_table[k].index = j;
492 k++;
493 }
Rafał Bilski73e107d2007-05-28 21:56:19 +0200494 if (k <= 1) {
495 kfree(longhaul_table);
496 return -ENODEV;
497 }
498 /* Sort */
499 for (j = 0; j < k - 1; j++) {
500 unsigned int min_f, min_i;
501 min_f = longhaul_table[j].frequency;
502 min_i = j;
503 for (i = j + 1; i < k; i++) {
504 if (longhaul_table[i].frequency < min_f) {
505 min_f = longhaul_table[i].frequency;
506 min_i = i;
507 }
508 }
509 if (min_i != j) {
510 unsigned int temp;
511 temp = longhaul_table[j].frequency;
512 longhaul_table[j].frequency = longhaul_table[min_i].frequency;
513 longhaul_table[min_i].frequency = temp;
514 temp = longhaul_table[j].index;
515 longhaul_table[j].index = longhaul_table[min_i].index;
516 longhaul_table[min_i].index = temp;
517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519
Rafał Bilski73e107d2007-05-28 21:56:19 +0200520 longhaul_table[k].frequency = CPUFREQ_TABLE_END;
521
522 /* Find index we are running on */
523 for (j = 0; j < k; j++) {
524 if (clock_ratio[longhaul_table[j].index & 0x1f] == mult) {
525 longhaul_index = j;
526 break;
527 }
528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return 0;
530}
531
532
533static void __init longhaul_setup_voltagescaling(void)
534{
535 union msr_longhaul longhaul;
Rafał Bilski73e107d2007-05-28 21:56:19 +0200536 struct mV_pos minvid, maxvid, vid;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200537 unsigned int j, speed, pos, kHz_step, numvscales;
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100538 int min_vid_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200540 rdmsrl(MSR_VIA_LONGHAUL, longhaul.val);
541 if (!(longhaul.bits.RevisionID & 1)) {
542 printk(KERN_INFO PFX "Voltage scaling not supported by CPU.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200546 if (!longhaul.bits.VRMRev) {
Rafał Bilski73e107d2007-05-28 21:56:19 +0200547 printk(KERN_INFO PFX "VRM 8.5\n");
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200548 vrm_mV_table = &vrm85_mV[0];
549 mV_vrm_table = &mV_vrm85[0];
550 } else {
Rafał Bilski73e107d2007-05-28 21:56:19 +0200551 printk(KERN_INFO PFX "Mobile VRM\n");
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +0100552 if (cpu_model < CPU_NEHEMIAH)
553 return;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200554 vrm_mV_table = &mobilevrm_mV[0];
555 mV_vrm_table = &mV_mobilevrm[0];
556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200558 minvid = vrm_mV_table[longhaul.bits.MinimumVID];
559 maxvid = vrm_mV_table[longhaul.bits.MaximumVID];
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200560
561 if (minvid.mV == 0 || maxvid.mV == 0 || minvid.mV > maxvid.mV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 printk (KERN_INFO PFX "Bogus values Min:%d.%03d Max:%d.%03d. "
563 "Voltage scaling disabled.\n",
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200564 minvid.mV/1000, minvid.mV%1000, maxvid.mV/1000, maxvid.mV%1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return;
566 }
567
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200568 if (minvid.mV == maxvid.mV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 printk (KERN_INFO PFX "Claims to support voltage scaling but min & max are "
570 "both %d.%03d. Voltage scaling disabled\n",
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200571 maxvid.mV/1000, maxvid.mV%1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return;
573 }
574
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100575 /* How many voltage steps */
576 numvscales = maxvid.pos - minvid.pos + 1;
577 printk(KERN_INFO PFX
578 "Max VID=%d.%03d "
579 "Min VID=%d.%03d, "
580 "%d possible voltage scales\n",
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200581 maxvid.mV/1000, maxvid.mV%1000,
582 minvid.mV/1000, minvid.mV%1000,
583 numvscales);
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100584
585 /* Calculate max frequency at min voltage */
586 j = longhaul.bits.MinMHzBR;
587 if (longhaul.bits.MinMHzBR4)
588 j += 16;
589 min_vid_speed = eblcr_table[j];
590 if (min_vid_speed == -1)
591 return;
592 switch (longhaul.bits.MinMHzFSB) {
593 case 0:
594 min_vid_speed *= 13333;
595 break;
596 case 1:
597 min_vid_speed *= 10000;
598 break;
599 case 3:
600 min_vid_speed *= 6666;
601 break;
602 default:
603 return;
604 break;
605 }
606 if (min_vid_speed >= highest_speed)
607 return;
608 /* Calculate kHz for one voltage step */
609 kHz_step = (highest_speed - min_vid_speed) / numvscales;
610
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200611 j = 0;
612 while (longhaul_table[j].frequency != CPUFREQ_TABLE_END) {
613 speed = longhaul_table[j].frequency;
Rafa³ Bilski348f31e2007-02-08 18:56:04 +0100614 if (speed > min_vid_speed)
615 pos = (speed - min_vid_speed) / kHz_step + minvid.pos;
616 else
617 pos = minvid.pos;
Rafał Bilski73e107d2007-05-28 21:56:19 +0200618 longhaul_table[j].index |= mV_vrm_table[pos] << 8;
619 vid = vrm_mV_table[mV_vrm_table[pos]];
620 printk(KERN_INFO PFX "f: %d kHz, index: %d, vid: %d mV\n", speed, j, vid.mV);
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200621 j++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 can_scale_voltage = 1;
Rafał Bilski73e107d2007-05-28 21:56:19 +0200625 printk(KERN_INFO PFX "Voltage scaling enabled.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
628
629static int longhaul_verify(struct cpufreq_policy *policy)
630{
631 return cpufreq_frequency_table_verify(policy, longhaul_table);
632}
633
634
635static int longhaul_target(struct cpufreq_policy *policy,
636 unsigned int target_freq, unsigned int relation)
637{
638 unsigned int table_index = 0;
Rafał Bilski73e107d2007-05-28 21:56:19 +0200639 unsigned int i;
640 unsigned int dir = 0;
641 u8 vid, current_vid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 if (cpufreq_frequency_table_target(policy, longhaul_table, target_freq, relation, &table_index))
644 return -EINVAL;
645
Rafał Bilski73e107d2007-05-28 21:56:19 +0200646 /* Don't set same frequency again */
647 if (longhaul_index == table_index)
648 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Rafał Bilski73e107d2007-05-28 21:56:19 +0200650 if (!can_scale_voltage)
651 longhaul_setstate(table_index);
652 else {
653 /* On test system voltage transitions exceeding single
654 * step up or down were turning motherboard off. Both
655 * "ondemand" and "userspace" are unsafe. C7 is doing
656 * this in hardware, C3 is old and we need to do this
657 * in software. */
658 i = longhaul_index;
659 current_vid = (longhaul_table[longhaul_index].index >> 8) & 0x1f;
660 if (table_index > longhaul_index)
661 dir = 1;
662 while (i != table_index) {
663 vid = (longhaul_table[i].index >> 8) & 0x1f;
664 if (vid != current_vid) {
665 longhaul_setstate(i);
666 current_vid = vid;
667 msleep(200);
668 }
669 if (dir)
670 i++;
671 else
672 i--;
673 }
674 longhaul_setstate(table_index);
675 }
676 longhaul_index = table_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return 0;
678}
679
680
681static unsigned int longhaul_get(unsigned int cpu)
682{
683 if (cpu)
684 return 0;
685 return calc_speed(longhaul_get_cpu_mult());
686}
687
Adrian Bunkc4a96c12006-07-09 19:53:08 +0200688static acpi_status longhaul_walk_callback(acpi_handle obj_handle,
689 u32 nesting_level,
690 void *context, void **return_value)
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200691{
692 struct acpi_device *d;
693
694 if ( acpi_bus_get_device(obj_handle, &d) ) {
695 return 0;
696 }
Jan Engelhardtade1af72008-01-30 13:33:23 +0100697 *return_value = acpi_driver_data(d);
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200698 return 1;
699}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200701/* VIA don't support PM2 reg, but have something similar */
702static int enable_arbiter_disable(void)
703{
704 struct pci_dev *dev;
Rafał Bilski73e107d2007-05-28 21:56:19 +0200705 int status = 1;
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200706 int reg;
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200707 u8 pci_cmd;
708
709 /* Find PLE133 host bridge */
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200710 reg = 0x78;
Rafał Bilskifb48e152007-03-02 20:12:27 +0100711 dev = pci_get_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8601_0,
712 NULL);
Linus Torvalds4d5709a2007-10-12 15:42:01 -0700713 /* Find PM133/VT8605 host bridge */
714 if (dev == NULL)
715 dev = pci_get_device(PCI_VENDOR_ID_VIA,
716 PCI_DEVICE_ID_VIA_8605_0, NULL);
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200717 /* Find CLE266 host bridge */
718 if (dev == NULL) {
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200719 reg = 0x76;
Rafał Bilskifb48e152007-03-02 20:12:27 +0100720 dev = pci_get_device(PCI_VENDOR_ID_VIA,
721 PCI_DEVICE_ID_VIA_862X_0, NULL);
Rafa³ Bilskidb2fb9d2006-11-30 03:47:41 +0100722 /* Find CN400 V-Link host bridge */
723 if (dev == NULL)
Rafał Bilskifb48e152007-03-02 20:12:27 +0100724 dev = pci_get_device(PCI_VENDOR_ID_VIA, 0x7259, NULL);
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200725 }
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200726 if (dev != NULL) {
727 /* Enable access to port 0x22 */
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200728 pci_read_config_byte(dev, reg, &pci_cmd);
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100729 if (!(pci_cmd & 1<<7)) {
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200730 pci_cmd |= 1<<7;
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200731 pci_write_config_byte(dev, reg, pci_cmd);
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100732 pci_read_config_byte(dev, reg, &pci_cmd);
733 if (!(pci_cmd & 1<<7)) {
734 printk(KERN_ERR PFX
735 "Can't enable access to port 0x22.\n");
Rafał Bilskifb48e152007-03-02 20:12:27 +0100736 status = 0;
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100737 }
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200738 }
Rafał Bilskifb48e152007-03-02 20:12:27 +0100739 pci_dev_put(dev);
740 return status;
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200741 }
742 return 0;
743}
744
Rafał Bilski7d5edcc2007-05-17 22:33:46 +0200745static int longhaul_setup_southbridge(void)
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100746{
747 struct pci_dev *dev;
748 u8 pci_cmd;
749
750 /* Find VT8235 southbridge */
Rafał Bilskifb48e152007-03-02 20:12:27 +0100751 dev = pci_get_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8235, NULL);
Rafał Bilski920dd0f2007-05-17 22:35:29 +0200752 if (dev == NULL)
753 /* Find VT8237 southbridge */
754 dev = pci_get_device(PCI_VENDOR_ID_VIA,
755 PCI_DEVICE_ID_VIA_8237, NULL);
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100756 if (dev != NULL) {
757 /* Set transition time to max */
758 pci_read_config_byte(dev, 0xec, &pci_cmd);
759 pci_cmd &= ~(1 << 2);
760 pci_write_config_byte(dev, 0xec, pci_cmd);
761 pci_read_config_byte(dev, 0xe4, &pci_cmd);
762 pci_cmd &= ~(1 << 7);
763 pci_write_config_byte(dev, 0xe4, pci_cmd);
764 pci_read_config_byte(dev, 0xe5, &pci_cmd);
765 pci_cmd |= 1 << 7;
766 pci_write_config_byte(dev, 0xe5, pci_cmd);
Rafał Bilski275bc6b2007-06-05 22:08:50 +0200767 /* Get address of ACPI registers block*/
768 pci_read_config_byte(dev, 0x81, &pci_cmd);
769 if (pci_cmd & 1 << 7) {
770 pci_read_config_dword(dev, 0x88, &acpi_regs_addr);
771 acpi_regs_addr &= 0xff00;
772 printk(KERN_INFO PFX "ACPI I/O at 0x%x\n", acpi_regs_addr);
773 }
774
Rafał Bilskifb48e152007-03-02 20:12:27 +0100775 pci_dev_put(dev);
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100776 return 1;
777 }
778 return 0;
779}
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781static int __init longhaul_cpu_init(struct cpufreq_policy *policy)
782{
Mike Travis92cb7612007-10-19 20:35:04 +0200783 struct cpuinfo_x86 *c = &cpu_data(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 char *cpuname=NULL;
785 int ret;
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +0100786 u32 lo, hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200788 /* Check what we have on this motherboard */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 switch (c->x86_model) {
790 case 6:
791 cpu_model = CPU_SAMUEL;
792 cpuname = "C3 'Samuel' [C5A]";
793 longhaul_version = TYPE_LONGHAUL_V1;
794 memcpy (clock_ratio, samuel1_clock_ratio, sizeof(samuel1_clock_ratio));
795 memcpy (eblcr_table, samuel1_eblcr, sizeof(samuel1_eblcr));
796 break;
797
798 case 7:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 switch (c->x86_mask) {
800 case 0:
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +0100801 longhaul_version = TYPE_LONGHAUL_V1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 cpu_model = CPU_SAMUEL2;
803 cpuname = "C3 'Samuel 2' [C5B]";
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +0100804 /* Note, this is not a typo, early Samuel2's had
805 * Samuel1 ratios. */
806 memcpy(clock_ratio, samuel1_clock_ratio,
807 sizeof(samuel1_clock_ratio));
808 memcpy(eblcr_table, samuel2_eblcr,
809 sizeof(samuel2_eblcr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 break;
811 case 1 ... 15:
Rafal Bilski07844252007-04-22 12:26:04 +0200812 longhaul_version = TYPE_LONGHAUL_V1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (c->x86_mask < 8) {
814 cpu_model = CPU_SAMUEL2;
815 cpuname = "C3 'Samuel 2' [C5B]";
816 } else {
817 cpu_model = CPU_EZRA;
818 cpuname = "C3 'Ezra' [C5C]";
819 }
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +0100820 memcpy(clock_ratio, ezra_clock_ratio,
821 sizeof(ezra_clock_ratio));
822 memcpy(eblcr_table, ezra_eblcr,
823 sizeof(ezra_eblcr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 break;
825 }
826 break;
827
828 case 8:
829 cpu_model = CPU_EZRA_T;
830 cpuname = "C3 'Ezra-T' [C5M]";
831 longhaul_version = TYPE_POWERSAVER;
832 numscales=32;
833 memcpy (clock_ratio, ezrat_clock_ratio, sizeof(ezrat_clock_ratio));
834 memcpy (eblcr_table, ezrat_eblcr, sizeof(ezrat_eblcr));
835 break;
836
837 case 9:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 longhaul_version = TYPE_POWERSAVER;
Rafa³ Bilski0d44b2b2007-01-31 23:50:49 +0100839 numscales = 32;
840 memcpy(clock_ratio,
841 nehemiah_clock_ratio,
842 sizeof(nehemiah_clock_ratio));
843 memcpy(eblcr_table, nehemiah_eblcr, sizeof(nehemiah_eblcr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 switch (c->x86_mask) {
845 case 0 ... 1:
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100846 cpu_model = CPU_NEHEMIAH;
Rafa³ Bilskie57501c2007-02-08 23:12:02 +0100847 cpuname = "C3 'Nehemiah A' [C5XLOE]";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 break;
849 case 2 ... 4:
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100850 cpu_model = CPU_NEHEMIAH;
Rafa³ Bilskie57501c2007-02-08 23:12:02 +0100851 cpuname = "C3 'Nehemiah B' [C5XLOH]";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 break;
853 case 5 ... 15:
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100854 cpu_model = CPU_NEHEMIAH_C;
Rafa³ Bilskie57501c2007-02-08 23:12:02 +0100855 cpuname = "C3 'Nehemiah C' [C5P]";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 break;
857 }
858 break;
859
860 default:
861 cpuname = "Unknown";
862 break;
863 }
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +0100864 /* Check Longhaul ver. 2 */
865 if (longhaul_version == TYPE_LONGHAUL_V2) {
866 rdmsr(MSR_VIA_LONGHAUL, lo, hi);
867 if (lo == 0 && hi == 0)
868 /* Looks like MSR isn't present */
869 longhaul_version = TYPE_LONGHAUL_V1;
870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 printk (KERN_INFO PFX "VIA %s CPU detected. ", cpuname);
873 switch (longhaul_version) {
874 case TYPE_LONGHAUL_V1:
875 case TYPE_LONGHAUL_V2:
876 printk ("Longhaul v%d supported.\n", longhaul_version);
877 break;
878 case TYPE_POWERSAVER:
879 printk ("Powersaver supported.\n");
880 break;
881 };
882
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100883 /* Doesn't hurt */
Rafał Bilski7d5edcc2007-05-17 22:33:46 +0200884 longhaul_setup_southbridge();
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100885
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200886 /* Find ACPI data for processor */
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100887 acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
888 ACPI_UINT32_MAX, &longhaul_walk_callback,
889 NULL, (void *)&pr);
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200890
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100891 /* Check ACPI support for C3 state */
Dave Jones7ab77e02007-04-20 15:58:00 -0400892 if (pr != NULL && longhaul_version == TYPE_POWERSAVER) {
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200893 cx = &pr->power.states[ACPI_STATE_C3];
Rafał Bilski7d5edcc2007-05-17 22:33:46 +0200894 if (cx->address > 0 && cx->latency <= 1000)
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100895 longhaul_flags |= USE_ACPI_C3;
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200896 }
Rafał Bilski905497c2007-07-08 21:51:26 +0200897 /* Disable if it isn't working */
898 if (disable_acpi_c3)
899 longhaul_flags &= ~USE_ACPI_C3;
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100900 /* Check if northbridge is friendly */
Rafał Bilski7d5edcc2007-05-17 22:33:46 +0200901 if (enable_arbiter_disable())
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100902 longhaul_flags |= USE_NORTHBRIDGE;
Rafał Bilski7d5edcc2007-05-17 22:33:46 +0200903
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100904 /* Check ACPI support for bus master arbiter disable */
Rafał Bilski7d5edcc2007-05-17 22:33:46 +0200905 if (!(longhaul_flags & USE_ACPI_C3
906 || longhaul_flags & USE_NORTHBRIDGE)
907 && ((pr == NULL) || !(pr->flags.bm_control))) {
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100908 printk(KERN_ERR PFX
909 "No ACPI support. Unsupported northbridge.\n");
910 return -ENODEV;
911 }
912
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100913 if (longhaul_flags & USE_NORTHBRIDGE)
Rafał Bilski7d5edcc2007-05-17 22:33:46 +0200914 printk(KERN_INFO PFX "Using northbridge support.\n");
915 if (longhaul_flags & USE_ACPI_C3)
916 printk(KERN_INFO PFX "Using ACPI support.\n");
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 ret = longhaul_get_ranges();
919 if (ret != 0)
920 return ret;
921
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100922 if ((longhaul_version != TYPE_LONGHAUL_V1) && (scale_voltage != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 longhaul_setup_voltagescaling();
924
Dave Jones6778bae2005-05-31 19:03:51 -0700925 policy->cpuinfo.transition_latency = 200000; /* nsec */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 policy->cur = calc_speed(longhaul_get_cpu_mult());
927
928 ret = cpufreq_frequency_table_cpuinfo(policy, longhaul_table);
929 if (ret)
930 return ret;
931
932 cpufreq_frequency_table_get_attr(longhaul_table, policy->cpu);
933
934 return 0;
935}
936
937static int __devexit longhaul_cpu_exit(struct cpufreq_policy *policy)
938{
939 cpufreq_frequency_table_put_attr(policy->cpu);
940 return 0;
941}
942
943static struct freq_attr* longhaul_attr[] = {
944 &cpufreq_freq_attr_scaling_available_freqs,
945 NULL,
946};
947
Linus Torvalds221dee22007-02-26 14:55:48 -0800948static struct cpufreq_driver longhaul_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 .verify = longhaul_verify,
950 .target = longhaul_target,
951 .get = longhaul_get,
952 .init = longhaul_cpu_init,
953 .exit = __devexit_p(longhaul_cpu_exit),
954 .name = "longhaul",
955 .owner = THIS_MODULE,
956 .attr = longhaul_attr,
957};
958
959
960static int __init longhaul_init(void)
961{
Mike Travis92cb7612007-10-19 20:35:04 +0200962 struct cpuinfo_x86 *c = &cpu_data(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 if (c->x86_vendor != X86_VENDOR_CENTAUR || c->x86 != 6)
965 return -ENODEV;
966
Rafa³ Bilski48b7bde2006-07-04 17:50:57 +0200967#ifdef CONFIG_SMP
968 if (num_online_cpus() > 1) {
Rafa³ Bilski48b7bde2006-07-04 17:50:57 +0200969 printk(KERN_ERR PFX "More than 1 CPU detected, longhaul disabled.\n");
Dave Jones1cfe2012006-12-28 22:30:16 -0500970 return -ENODEV;
Rafa³ Bilski48b7bde2006-07-04 17:50:57 +0200971 }
972#endif
973#ifdef CONFIG_X86_IO_APIC
974 if (cpu_has_apic) {
975 printk(KERN_ERR PFX "APIC detected. Longhaul is currently broken in this configuration.\n");
976 return -ENODEV;
977 }
978#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 switch (c->x86_model) {
980 case 6 ... 9:
981 return cpufreq_register_driver(&longhaul_driver);
Dave Jones8ec98222006-12-17 19:07:35 -0500982 case 10:
983 printk(KERN_ERR PFX "Use acpi-cpufreq driver for VIA C7\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 default:
Fernando Carrijoc19a28e2009-01-07 18:09:08 -0800985 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
987
988 return -ENODEV;
989}
990
991
992static void __exit longhaul_exit(void)
993{
Dave Jones8eebf1a2006-05-30 17:40:16 -0400994 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 for (i=0; i < numscales; i++) {
997 if (clock_ratio[i] == maxmult) {
998 longhaul_setstate(i);
999 break;
1000 }
1001 }
1002
1003 cpufreq_unregister_driver(&longhaul_driver);
1004 kfree(longhaul_table);
1005}
1006
Rafal Bilski52a26382007-10-07 00:24:32 -07001007/* Even if BIOS is exporting ACPI C3 state, and it is used
1008 * with success when CPU is idle, this state doesn't
1009 * trigger frequency transition in some cases. */
Rafał Bilski905497c2007-07-08 21:51:26 +02001010module_param (disable_acpi_c3, int, 0644);
1011MODULE_PARM_DESC(disable_acpi_c3, "Don't use ACPI C3 support");
Rafal Bilski52a26382007-10-07 00:24:32 -07001012/* Change CPU voltage with frequency. Very usefull to save
1013 * power, but most VIA C3 processors aren't supporting it. */
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +02001014module_param (scale_voltage, int, 0644);
1015MODULE_PARM_DESC(scale_voltage, "Scale voltage of processor");
Rafal Bilski52a26382007-10-07 00:24:32 -07001016/* Force revision key to 0 for processors which doesn't
1017 * support voltage scaling, but are introducing itself as
1018 * such. */
1019module_param(revid_errata, int, 0644);
1020MODULE_PARM_DESC(revid_errata, "Ignore CPU Revision ID");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Dave Jonesf4432c52008-10-20 13:31:45 -04001022MODULE_AUTHOR ("Dave Jones <davej@redhat.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023MODULE_DESCRIPTION ("Longhaul driver for VIA Cyrix processors.");
1024MODULE_LICENSE ("GPL");
1025
Rafa³ Bilski0d6daba2006-07-07 08:48:26 +02001026late_initcall(longhaul_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027module_exit(longhaul_exit);