blob: dc03622a83a043831d71e4e5cb3a751a7786b8af [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>
Dave Jonesac617bd2009-01-17 23:29:53 -050033#include <linux/timex.h>
34#include <linux/io.h>
35#include <linux/acpi.h>
36#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <asm/msr.h>
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +020039#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;
Dave Jonesac617bd2009-01-17 23:29:53 -050061static 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;
Dave Jonesac617bd2009-01-17 23:29:53 -050070static struct acpi_processor *pr;
71static struct acpi_processor_cx *cx;
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
Dave Jonesac617bd2009-01-17 23:29:53 -050081#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, \
82 "longhaul", msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/* Clock ratios multiplied by 10 */
Dave Jonesac617bd2009-01-17 23:29:53 -050086static int mults[32];
87static int eblcr[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static int longhaul_version;
89static struct cpufreq_frequency_table *longhaul_table;
90
91#ifdef CONFIG_CPU_FREQ_DEBUG
92static char speedbuffer[8];
93
94static char *print_speed(int speed)
95{
Dave Jonese2aa8732006-05-30 17:37:15 -040096 if (speed < 1000) {
Dave Jonesac617bd2009-01-17 23:29:53 -050097 snprintf(speedbuffer, sizeof(speedbuffer), "%dMHz", speed);
Dave Jonese2aa8732006-05-30 17:37:15 -040098 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 return speedbuffer;
109}
110#endif
111
112
113static 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
124static int longhaul_get_cpu_mult(void)
125{
Dave Jonesac617bd2009-01-17 23:29:53 -0500126 unsigned long invalue = 0, lo, hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Dave Jonesac617bd2009-01-17 23:29:53 -0500128 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 ||
131 longhaul_version == TYPE_POWERSAVER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (lo & (1<<27))
Dave Jonesac617bd2009-01-17 23:29:53 -0500133 invalue += 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
Dave Jonesac617bd2009-01-17 23:29:53 -0500135 return eblcr[invalue];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200138/* For processor with BCR2 MSR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Dave Jonesac617bd2009-01-17 23:29:53 -0500140static void do_longhaul1(unsigned int mults_index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200142 union msr_bcr2 bcr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200144 rdmsrl(MSR_VIA_BCR2, bcr2.val);
145 /* Enable software clock multiplier */
146 bcr2.bits.ESOFTBF = 1;
Dave Jonesac617bd2009-01-17 23:29:53 -0500147 bcr2.bits.CLOCKMUL = mults_index & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200149 /* Sync to timer tick */
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -0700150 safe_halt();
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200151 /* Change frequency on next halt or sleep */
152 wrmsrl(MSR_VIA_BCR2, bcr2.val);
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200153 /* Invoke transition */
154 ACPI_FLUSH_CPU_CACHE();
155 halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200157 /* Disable software clock multiplier */
Dave Jones3be6a482005-05-31 19:03:51 -0700158 local_irq_disable();
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200159 rdmsrl(MSR_VIA_BCR2, bcr2.val);
160 bcr2.bits.ESOFTBF = 0;
161 wrmsrl(MSR_VIA_BCR2, bcr2.val);
162}
Dave Jones3be6a482005-05-31 19:03:51 -0700163
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200164/* For processor with Longhaul MSR */
Dave Jones11746312005-05-31 19:03:51 -0700165
Dave Jonesac617bd2009-01-17 23:29:53 -0500166static void do_powersaver(int cx_address, unsigned int mults_index,
Rafał Bilski73e107d2007-05-28 21:56:19 +0200167 unsigned int dir)
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200168{
169 union msr_longhaul longhaul;
170 u32 t;
Dave Jones3be6a482005-05-31 19:03:51 -0700171
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200172 rdmsrl(MSR_VIA_LONGHAUL, longhaul.val);
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100173 /* Setup new frequency */
Rafal Bilski52a26382007-10-07 00:24:32 -0700174 if (!revid_errata)
175 longhaul.bits.RevisionKey = longhaul.bits.RevisionID;
176 else
177 longhaul.bits.RevisionKey = 0;
Dave Jonesac617bd2009-01-17 23:29:53 -0500178 longhaul.bits.SoftBusRatio = mults_index & 0xf;
179 longhaul.bits.SoftBusRatio4 = (mults_index & 0x10) >> 4;
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100180 /* Setup new voltage */
181 if (can_scale_voltage)
Dave Jonesac617bd2009-01-17 23:29:53 -0500182 longhaul.bits.SoftVID = (mults_index >> 8) & 0x1f;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200183 /* Sync to timer tick */
184 safe_halt();
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100185 /* Raise voltage if necessary */
Rafał Bilski73e107d2007-05-28 21:56:19 +0200186 if (can_scale_voltage && dir) {
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100187 longhaul.bits.EnableSoftVID = 1;
188 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
189 /* Change voltage */
190 if (!cx_address) {
191 ACPI_FLUSH_CPU_CACHE();
192 halt();
193 } else {
194 ACPI_FLUSH_CPU_CACHE();
195 /* Invoke C3 */
196 inb(cx_address);
197 /* Dummy op - must do something useless after P_LVL3
198 * read */
Dave Jonesbd0561c2007-02-10 20:36:29 -0500199 t = inl(acpi_gbl_FADT.xpm_timer_block.address);
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100200 }
201 longhaul.bits.EnableSoftVID = 0;
202 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100203 }
204
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200205 /* Change frequency on next halt or sleep */
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100206 longhaul.bits.EnableSoftBusRatio = 1;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200207 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100208 if (!cx_address) {
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200209 ACPI_FLUSH_CPU_CACHE();
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200210 halt();
211 } else {
212 ACPI_FLUSH_CPU_CACHE();
213 /* Invoke C3 */
214 inb(cx_address);
215 /* Dummy op - must do something useless after P_LVL3 read */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300216 t = inl(acpi_gbl_FADT.xpm_timer_block.address);
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200217 }
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200218 /* Disable bus ratio bit */
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200219 longhaul.bits.EnableSoftBusRatio = 0;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200220 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100221
222 /* Reduce voltage if necessary */
Rafał Bilski73e107d2007-05-28 21:56:19 +0200223 if (can_scale_voltage && !dir) {
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100224 longhaul.bits.EnableSoftVID = 1;
225 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
226 /* Change voltage */
227 if (!cx_address) {
228 ACPI_FLUSH_CPU_CACHE();
229 halt();
230 } else {
231 ACPI_FLUSH_CPU_CACHE();
232 /* Invoke C3 */
233 inb(cx_address);
234 /* Dummy op - must do something useless after P_LVL3
235 * read */
Dave Jonesbd0561c2007-02-10 20:36:29 -0500236 t = inl(acpi_gbl_FADT.xpm_timer_block.address);
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100237 }
238 longhaul.bits.EnableSoftVID = 0;
239 wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243/**
244 * longhaul_set_cpu_frequency()
Dave Jonesac617bd2009-01-17 23:29:53 -0500245 * @mults_index : bitpattern of the new multiplier.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 *
247 * Sets a new clock ratio.
248 */
249
Rafał Bilski73e107d2007-05-28 21:56:19 +0200250static void longhaul_setstate(unsigned int table_index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Dave Jonesac617bd2009-01-17 23:29:53 -0500252 unsigned int mults_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 int speed, mult;
254 struct cpufreq_freqs freqs;
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200255 unsigned long flags;
256 unsigned int pic1_mask, pic2_mask;
Rafał Bilski689eba72007-06-07 22:31:24 +0200257 u16 bm_status = 0;
Rafał Bilski275bc6b2007-06-05 22:08:50 +0200258 u32 bm_timeout = 1000;
Rafał Bilski73e107d2007-05-28 21:56:19 +0200259 unsigned int dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Dave Jonesac617bd2009-01-17 23:29:53 -0500261 mults_index = longhaul_table[table_index].index;
Rafał Bilski73e107d2007-05-28 21:56:19 +0200262 /* Safety precautions */
Dave Jonesac617bd2009-01-17 23:29:53 -0500263 mult = mults[mults_index & 0x1f];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (mult == -1)
265 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 speed = calc_speed(mult);
267 if ((speed > highest_speed) || (speed < lowest_speed))
268 return;
Rafał Bilski73e107d2007-05-28 21:56:19 +0200269 /* Voltage transition before frequency transition? */
270 if (can_scale_voltage && longhaul_index < table_index)
271 dir = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 freqs.old = calc_speed(longhaul_get_cpu_mult());
274 freqs.new = speed;
275 freqs.cpu = 0; /* longhaul.c is UP only driver */
276
277 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
278
Dave Jonesac617bd2009-01-17 23:29:53 -0500279 dprintk("Setting to FSB:%dMHz Mult:%d.%dx (%s)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 fsb, mult/10, mult%10, print_speed(speed/1000));
Rafal Bilski52a26382007-10-07 00:24:32 -0700281retry_loop:
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200282 preempt_disable();
283 local_irq_save(flags);
284
285 pic2_mask = inb(0xA1);
286 pic1_mask = inb(0x21); /* works on C3. save mask. */
Dave Jonesac617bd2009-01-17 23:29:53 -0500287 outb(0xFF, 0xA1); /* Overkill */
288 outb(0xFE, 0x21); /* TMR0 only */
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200289
Rafał Bilski489dc5c2007-05-17 22:39:02 +0200290 /* Wait while PCI bus is busy. */
Rafał Bilski689eba72007-06-07 22:31:24 +0200291 if (acpi_regs_addr && (longhaul_flags & USE_NORTHBRIDGE
292 || ((pr != NULL) && pr->flags.bm_control))) {
293 bm_status = inw(acpi_regs_addr);
Rafał Bilski275bc6b2007-06-05 22:08:50 +0200294 bm_status &= 1 << 4;
Rafał Bilski489dc5c2007-05-17 22:39:02 +0200295 while (bm_status && bm_timeout) {
Rafał Bilski689eba72007-06-07 22:31:24 +0200296 outw(1 << 4, acpi_regs_addr);
Rafał Bilski489dc5c2007-05-17 22:39:02 +0200297 bm_timeout--;
Rafał Bilski689eba72007-06-07 22:31:24 +0200298 bm_status = inw(acpi_regs_addr);
Rafał Bilski275bc6b2007-06-05 22:08:50 +0200299 bm_status &= 1 << 4;
Rafał Bilski489dc5c2007-05-17 22:39:02 +0200300 }
301 }
302
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100303 if (longhaul_flags & USE_NORTHBRIDGE) {
304 /* Disable AGP and PCI arbiters */
305 outb(3, 0x22);
306 } else if ((pr != NULL) && pr->flags.bm_control) {
Rafał Bilski73e107d2007-05-28 21:56:19 +0200307 /* Disable bus master arbitration */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300308 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1);
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 switch (longhaul_version) {
311
312 /*
313 * Longhaul v1. (Samuel[C5A] and Samuel2 stepping 0[C5B])
314 * Software controlled multipliers only.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 */
316 case TYPE_LONGHAUL_V1:
Dave Jonesac617bd2009-01-17 23:29:53 -0500317 do_longhaul1(mults_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 break;
319
320 /*
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +0100321 * Longhaul v2 appears in Samuel2 Steppings 1->7 [C5B] and Ezra [C5C]
322 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 * Longhaul v3 (aka Powersaver). (Ezra-T [C5M] & Nehemiah [C5N])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 * Nehemiah can do FSB scaling too, but this has never been proven
325 * to work in practice.
326 */
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +0100327 case TYPE_LONGHAUL_V2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 case TYPE_POWERSAVER:
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100329 if (longhaul_flags & USE_ACPI_C3) {
330 /* Don't allow wakeup */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300331 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
Dave Jonesac617bd2009-01-17 23:29:53 -0500332 do_powersaver(cx->address, mults_index, dir);
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100333 } else {
Dave Jonesac617bd2009-01-17 23:29:53 -0500334 do_powersaver(0, mults_index, dir);
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 break;
337 }
338
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100339 if (longhaul_flags & USE_NORTHBRIDGE) {
340 /* Enable arbiters */
341 outb(0, 0x22);
342 } else if ((pr != NULL) && pr->flags.bm_control) {
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200343 /* Enable bus master arbitration */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300344 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0);
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200345 }
Dave Jonesac617bd2009-01-17 23:29:53 -0500346 outb(pic2_mask, 0xA1); /* restore mask */
347 outb(pic1_mask, 0x21);
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200348
349 local_irq_restore(flags);
350 preempt_enable();
351
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +0100352 freqs.new = calc_speed(longhaul_get_cpu_mult());
Rafal Bilski52a26382007-10-07 00:24:32 -0700353 /* Check if requested frequency is set. */
354 if (unlikely(freqs.new != speed)) {
355 printk(KERN_INFO PFX "Failed to set requested frequency!\n");
356 /* Revision ID = 1 but processor is expecting revision key
357 * equal to 0. Jumpers at the bottom of processor will change
358 * multiplier and FSB, but will not change bits in Longhaul
359 * MSR nor enable voltage scaling. */
360 if (!revid_errata) {
361 printk(KERN_INFO PFX "Enabling \"Ignore Revision ID\" "
362 "option.\n");
363 revid_errata = 1;
364 msleep(200);
365 goto retry_loop;
366 }
367 /* Why ACPI C3 sometimes doesn't work is a mystery for me.
368 * But it does happen. Processor is entering ACPI C3 state,
369 * but it doesn't change frequency. I tried poking various
370 * bits in northbridge registers, but without success. */
371 if (longhaul_flags & USE_ACPI_C3) {
372 printk(KERN_INFO PFX "Disabling ACPI C3 support.\n");
373 longhaul_flags &= ~USE_ACPI_C3;
374 if (revid_errata) {
375 printk(KERN_INFO PFX "Disabling \"Ignore "
376 "Revision ID\" option.\n");
377 revid_errata = 0;
378 }
379 msleep(200);
380 goto retry_loop;
381 }
382 /* This shouldn't happen. Longhaul ver. 2 was reported not
383 * working on processors without voltage scaling, but with
384 * RevID = 1. RevID errata will make things right. Just
385 * to be 100% sure. */
386 if (longhaul_version == TYPE_LONGHAUL_V2) {
387 printk(KERN_INFO PFX "Switching to Longhaul ver. 1\n");
388 longhaul_version = TYPE_LONGHAUL_V1;
389 msleep(200);
390 goto retry_loop;
391 }
392 }
393 /* Report true CPU frequency */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
Rafał Bilski489dc5c2007-05-17 22:39:02 +0200395
396 if (!bm_timeout)
Dave Jonesac617bd2009-01-17 23:29:53 -0500397 printk(KERN_INFO PFX "Warning: Timeout while waiting for "
398 "idle PCI bus.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
401/*
402 * Centaur decided to make life a little more tricky.
403 * Only longhaul v1 is allowed to read EBLCR BSEL[0:1].
404 * Samuel2 and above have to try and guess what the FSB is.
405 * We do this by assuming we booted at maximum multiplier, and interpolate
406 * between that value multiplied by possible FSBs and cpu_mhz which
407 * was calculated at boot time. Really ugly, but no other way to do this.
408 */
409
410#define ROUNDING 0xf
411
Rafa³ Bilski24ebead2007-01-01 23:49:34 +0100412static int guess_fsb(int mult)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Rafa³ Bilski46ef9552007-02-04 15:58:46 +0100414 int speed = cpu_khz / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 int i;
Rafa³ Bilski46ef9552007-02-04 15:58:46 +0100416 int speeds[] = { 666, 1000, 1333, 2000 };
417 int f_max, f_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Rafa³ Bilski46ef9552007-02-04 15:58:46 +0100419 for (i = 0; i < 4; i++) {
420 f_max = ((speeds[i] * mult) + 50) / 100;
421 f_max += (ROUNDING / 2);
422 f_min = f_max - ROUNDING;
423 if ((speed <= f_max) && (speed >= f_min))
424 return speeds[i] / 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426 return 0;
427}
428
429
430static int __init longhaul_get_ranges(void)
431{
Rafał Bilski73e107d2007-05-28 21:56:19 +0200432 unsigned int i, j, k = 0;
433 unsigned int ratio;
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100434 int mult;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100436 /* Get current frequency */
437 mult = longhaul_get_cpu_mult();
438 if (mult == -1) {
439 printk(KERN_INFO PFX "Invalid (reserved) multiplier!\n");
440 return -EINVAL;
441 }
442 fsb = guess_fsb(mult);
443 if (fsb == 0) {
444 printk(KERN_INFO PFX "Invalid (reserved) FSB!\n");
445 return -EINVAL;
446 }
447 /* Get max multiplier - as we always did.
448 * Longhaul MSR is usefull only when voltage scaling is enabled.
449 * C3 is booting at max anyway. */
450 maxmult = mult;
451 /* Get min multiplier */
Rafa³ Bilski9addf3b2007-02-07 22:53:29 +0100452 switch (cpu_model) {
453 case CPU_NEHEMIAH:
454 minmult = 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 break;
Rafa³ Bilski9addf3b2007-02-07 22:53:29 +0100456 case CPU_NEHEMIAH_C:
457 minmult = 40;
458 break;
459 default:
460 minmult = 30;
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100461 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463
Dave Jonesac617bd2009-01-17 23:29:53 -0500464 dprintk("MinMult:%d.%dx MaxMult:%d.%dx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 minmult/10, minmult%10, maxmult/10, maxmult%10);
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 highest_speed = calc_speed(maxmult);
468 lowest_speed = calc_speed(minmult);
Dave Jonesac617bd2009-01-17 23:29:53 -0500469 dprintk("FSB:%dMHz Lowest speed: %s Highest speed:%s\n", fsb,
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300470 print_speed(lowest_speed/1000),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 print_speed(highest_speed/1000));
472
473 if (lowest_speed == highest_speed) {
Dave Jonesac617bd2009-01-17 23:29:53 -0500474 printk(KERN_INFO PFX "highestspeed == lowest, aborting.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return -EINVAL;
476 }
477 if (lowest_speed > highest_speed) {
Dave Jonesac617bd2009-01-17 23:29:53 -0500478 printk(KERN_INFO PFX "nonsense! lowest (%d > %d) !\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 lowest_speed, highest_speed);
480 return -EINVAL;
481 }
482
Dave Jonesac617bd2009-01-17 23:29:53 -0500483 longhaul_table = kmalloc((numscales + 1) * sizeof(*longhaul_table),
484 GFP_KERNEL);
485 if (!longhaul_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return -ENOMEM;
487
Rafał Bilski73e107d2007-05-28 21:56:19 +0200488 for (j = 0; j < numscales; j++) {
Dave Jonesac617bd2009-01-17 23:29:53 -0500489 ratio = mults[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (ratio == -1)
491 continue;
492 if (ratio > maxmult || ratio < minmult)
493 continue;
494 longhaul_table[k].frequency = calc_speed(ratio);
495 longhaul_table[k].index = j;
496 k++;
497 }
Rafał Bilski73e107d2007-05-28 21:56:19 +0200498 if (k <= 1) {
499 kfree(longhaul_table);
500 return -ENODEV;
501 }
502 /* Sort */
503 for (j = 0; j < k - 1; j++) {
504 unsigned int min_f, min_i;
505 min_f = longhaul_table[j].frequency;
506 min_i = j;
507 for (i = j + 1; i < k; i++) {
508 if (longhaul_table[i].frequency < min_f) {
509 min_f = longhaul_table[i].frequency;
510 min_i = i;
511 }
512 }
513 if (min_i != j) {
514 unsigned int temp;
515 temp = longhaul_table[j].frequency;
516 longhaul_table[j].frequency = longhaul_table[min_i].frequency;
517 longhaul_table[min_i].frequency = temp;
518 temp = longhaul_table[j].index;
519 longhaul_table[j].index = longhaul_table[min_i].index;
520 longhaul_table[min_i].index = temp;
521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523
Rafał Bilski73e107d2007-05-28 21:56:19 +0200524 longhaul_table[k].frequency = CPUFREQ_TABLE_END;
525
526 /* Find index we are running on */
527 for (j = 0; j < k; j++) {
Dave Jonesac617bd2009-01-17 23:29:53 -0500528 if (mults[longhaul_table[j].index & 0x1f] == mult) {
Rafał Bilski73e107d2007-05-28 21:56:19 +0200529 longhaul_index = j;
530 break;
531 }
532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return 0;
534}
535
536
537static void __init longhaul_setup_voltagescaling(void)
538{
539 union msr_longhaul longhaul;
Rafał Bilski73e107d2007-05-28 21:56:19 +0200540 struct mV_pos minvid, maxvid, vid;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200541 unsigned int j, speed, pos, kHz_step, numvscales;
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100542 int min_vid_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200544 rdmsrl(MSR_VIA_LONGHAUL, longhaul.val);
545 if (!(longhaul.bits.RevisionID & 1)) {
546 printk(KERN_INFO PFX "Voltage scaling not supported by CPU.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200550 if (!longhaul.bits.VRMRev) {
Rafał Bilski73e107d2007-05-28 21:56:19 +0200551 printk(KERN_INFO PFX "VRM 8.5\n");
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200552 vrm_mV_table = &vrm85_mV[0];
553 mV_vrm_table = &mV_vrm85[0];
554 } else {
Rafał Bilski73e107d2007-05-28 21:56:19 +0200555 printk(KERN_INFO PFX "Mobile VRM\n");
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +0100556 if (cpu_model < CPU_NEHEMIAH)
557 return;
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200558 vrm_mV_table = &mobilevrm_mV[0];
559 mV_vrm_table = &mV_mobilevrm[0];
560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200562 minvid = vrm_mV_table[longhaul.bits.MinimumVID];
563 maxvid = vrm_mV_table[longhaul.bits.MaximumVID];
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200564
565 if (minvid.mV == 0 || maxvid.mV == 0 || minvid.mV > maxvid.mV) {
Dave Jonesac617bd2009-01-17 23:29:53 -0500566 printk(KERN_INFO PFX "Bogus values Min:%d.%03d Max:%d.%03d. "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 "Voltage scaling disabled.\n",
Dave Jonesac617bd2009-01-17 23:29:53 -0500568 minvid.mV/1000, minvid.mV%1000,
569 maxvid.mV/1000, maxvid.mV%1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return;
571 }
572
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200573 if (minvid.mV == maxvid.mV) {
Dave Jonesac617bd2009-01-17 23:29:53 -0500574 printk(KERN_INFO PFX "Claims to support voltage scaling but "
575 "min & max are both %d.%03d. "
576 "Voltage scaling disabled\n",
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200577 maxvid.mV/1000, maxvid.mV%1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return;
579 }
580
Dave Jonesac617bd2009-01-17 23:29:53 -0500581 /* How many voltage steps*/
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100582 numvscales = maxvid.pos - minvid.pos + 1;
583 printk(KERN_INFO PFX
584 "Max VID=%d.%03d "
585 "Min VID=%d.%03d, "
586 "%d possible voltage scales\n",
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200587 maxvid.mV/1000, maxvid.mV%1000,
588 minvid.mV/1000, minvid.mV%1000,
589 numvscales);
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100590
591 /* Calculate max frequency at min voltage */
592 j = longhaul.bits.MinMHzBR;
593 if (longhaul.bits.MinMHzBR4)
594 j += 16;
Dave Jonesac617bd2009-01-17 23:29:53 -0500595 min_vid_speed = eblcr[j];
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100596 if (min_vid_speed == -1)
597 return;
598 switch (longhaul.bits.MinMHzFSB) {
599 case 0:
600 min_vid_speed *= 13333;
601 break;
602 case 1:
603 min_vid_speed *= 10000;
604 break;
605 case 3:
606 min_vid_speed *= 6666;
607 break;
608 default:
609 return;
610 break;
611 }
612 if (min_vid_speed >= highest_speed)
613 return;
614 /* Calculate kHz for one voltage step */
615 kHz_step = (highest_speed - min_vid_speed) / numvscales;
616
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200617 j = 0;
618 while (longhaul_table[j].frequency != CPUFREQ_TABLE_END) {
619 speed = longhaul_table[j].frequency;
Rafa³ Bilski348f31ed2007-02-08 18:56:04 +0100620 if (speed > min_vid_speed)
621 pos = (speed - min_vid_speed) / kHz_step + minvid.pos;
622 else
623 pos = minvid.pos;
Rafał Bilski73e107d2007-05-28 21:56:19 +0200624 longhaul_table[j].index |= mV_vrm_table[pos] << 8;
625 vid = vrm_mV_table[mV_vrm_table[pos]];
Dave Jonesac617bd2009-01-17 23:29:53 -0500626 printk(KERN_INFO PFX "f: %d kHz, index: %d, vid: %d mV\n",
627 speed, j, vid.mV);
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +0200628 j++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 can_scale_voltage = 1;
Rafał Bilski73e107d2007-05-28 21:56:19 +0200632 printk(KERN_INFO PFX "Voltage scaling enabled.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
635
636static int longhaul_verify(struct cpufreq_policy *policy)
637{
638 return cpufreq_frequency_table_verify(policy, longhaul_table);
639}
640
641
642static int longhaul_target(struct cpufreq_policy *policy,
643 unsigned int target_freq, unsigned int relation)
644{
645 unsigned int table_index = 0;
Rafał Bilski73e107d2007-05-28 21:56:19 +0200646 unsigned int i;
647 unsigned int dir = 0;
648 u8 vid, current_vid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Dave Jonesac617bd2009-01-17 23:29:53 -0500650 if (cpufreq_frequency_table_target(policy, longhaul_table, target_freq,
651 relation, &table_index))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return -EINVAL;
653
Rafał Bilski73e107d2007-05-28 21:56:19 +0200654 /* Don't set same frequency again */
655 if (longhaul_index == table_index)
656 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Rafał Bilski73e107d2007-05-28 21:56:19 +0200658 if (!can_scale_voltage)
659 longhaul_setstate(table_index);
660 else {
661 /* On test system voltage transitions exceeding single
662 * step up or down were turning motherboard off. Both
663 * "ondemand" and "userspace" are unsafe. C7 is doing
664 * this in hardware, C3 is old and we need to do this
665 * in software. */
666 i = longhaul_index;
Dave Jonesac617bd2009-01-17 23:29:53 -0500667 current_vid = (longhaul_table[longhaul_index].index >> 8);
668 current_vid &= 0x1f;
Rafał Bilski73e107d2007-05-28 21:56:19 +0200669 if (table_index > longhaul_index)
670 dir = 1;
671 while (i != table_index) {
672 vid = (longhaul_table[i].index >> 8) & 0x1f;
673 if (vid != current_vid) {
674 longhaul_setstate(i);
675 current_vid = vid;
676 msleep(200);
677 }
678 if (dir)
679 i++;
680 else
681 i--;
682 }
683 longhaul_setstate(table_index);
684 }
685 longhaul_index = table_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return 0;
687}
688
689
690static unsigned int longhaul_get(unsigned int cpu)
691{
692 if (cpu)
693 return 0;
694 return calc_speed(longhaul_get_cpu_mult());
695}
696
Adrian Bunkc4a96c12006-07-09 19:53:08 +0200697static acpi_status longhaul_walk_callback(acpi_handle obj_handle,
698 u32 nesting_level,
699 void *context, void **return_value)
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200700{
701 struct acpi_device *d;
702
Dave Jonesac617bd2009-01-17 23:29:53 -0500703 if (acpi_bus_get_device(obj_handle, &d))
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200704 return 0;
Dave Jonesac617bd2009-01-17 23:29:53 -0500705
Jan Engelhardtade1af72008-01-30 13:33:23 +0100706 *return_value = acpi_driver_data(d);
Rafa³ Bilskidadb49d2006-07-03 07:19:05 +0200707 return 1;
708}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200710/* VIA don't support PM2 reg, but have something similar */
711static int enable_arbiter_disable(void)
712{
713 struct pci_dev *dev;
Rafał Bilski73e107d2007-05-28 21:56:19 +0200714 int status = 1;
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200715 int reg;
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200716 u8 pci_cmd;
717
718 /* Find PLE133 host bridge */
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200719 reg = 0x78;
Rafał Bilskifb48e152007-03-02 20:12:27 +0100720 dev = pci_get_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8601_0,
721 NULL);
Linus Torvalds4d5709a2007-10-12 15:42:01 -0700722 /* Find PM133/VT8605 host bridge */
723 if (dev == NULL)
724 dev = pci_get_device(PCI_VENDOR_ID_VIA,
725 PCI_DEVICE_ID_VIA_8605_0, NULL);
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200726 /* Find CLE266 host bridge */
727 if (dev == NULL) {
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200728 reg = 0x76;
Rafał Bilskifb48e152007-03-02 20:12:27 +0100729 dev = pci_get_device(PCI_VENDOR_ID_VIA,
730 PCI_DEVICE_ID_VIA_862X_0, NULL);
Rafa³ Bilskidb2fb9d2006-11-30 03:47:41 +0100731 /* Find CN400 V-Link host bridge */
732 if (dev == NULL)
Rafał Bilskifb48e152007-03-02 20:12:27 +0100733 dev = pci_get_device(PCI_VENDOR_ID_VIA, 0x7259, NULL);
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200734 }
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200735 if (dev != NULL) {
736 /* Enable access to port 0x22 */
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200737 pci_read_config_byte(dev, reg, &pci_cmd);
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100738 if (!(pci_cmd & 1<<7)) {
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200739 pci_cmd |= 1<<7;
rafalbilski@interia.pl7f1be892006-09-24 20:28:13 +0200740 pci_write_config_byte(dev, reg, pci_cmd);
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100741 pci_read_config_byte(dev, reg, &pci_cmd);
742 if (!(pci_cmd & 1<<7)) {
743 printk(KERN_ERR PFX
744 "Can't enable access to port 0x22.\n");
Rafał Bilskifb48e152007-03-02 20:12:27 +0100745 status = 0;
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100746 }
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200747 }
Rafał Bilskifb48e152007-03-02 20:12:27 +0100748 pci_dev_put(dev);
749 return status;
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200750 }
751 return 0;
752}
753
Rafał Bilski7d5edcc2007-05-17 22:33:46 +0200754static int longhaul_setup_southbridge(void)
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100755{
756 struct pci_dev *dev;
757 u8 pci_cmd;
758
759 /* Find VT8235 southbridge */
Rafał Bilskifb48e152007-03-02 20:12:27 +0100760 dev = pci_get_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8235, NULL);
Rafał Bilski920dd0f2007-05-17 22:35:29 +0200761 if (dev == NULL)
Dave Jonesac617bd2009-01-17 23:29:53 -0500762 /* Find VT8237 southbridge */
Rafał Bilski920dd0f2007-05-17 22:35:29 +0200763 dev = pci_get_device(PCI_VENDOR_ID_VIA,
764 PCI_DEVICE_ID_VIA_8237, NULL);
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100765 if (dev != NULL) {
766 /* Set transition time to max */
767 pci_read_config_byte(dev, 0xec, &pci_cmd);
768 pci_cmd &= ~(1 << 2);
769 pci_write_config_byte(dev, 0xec, pci_cmd);
770 pci_read_config_byte(dev, 0xe4, &pci_cmd);
771 pci_cmd &= ~(1 << 7);
772 pci_write_config_byte(dev, 0xe4, pci_cmd);
773 pci_read_config_byte(dev, 0xe5, &pci_cmd);
774 pci_cmd |= 1 << 7;
775 pci_write_config_byte(dev, 0xe5, pci_cmd);
Rafał Bilski275bc6b2007-06-05 22:08:50 +0200776 /* Get address of ACPI registers block*/
777 pci_read_config_byte(dev, 0x81, &pci_cmd);
778 if (pci_cmd & 1 << 7) {
779 pci_read_config_dword(dev, 0x88, &acpi_regs_addr);
780 acpi_regs_addr &= 0xff00;
Dave Jonesac617bd2009-01-17 23:29:53 -0500781 printk(KERN_INFO PFX "ACPI I/O at 0x%x\n",
782 acpi_regs_addr);
Rafał Bilski275bc6b2007-06-05 22:08:50 +0200783 }
784
Rafał Bilskifb48e152007-03-02 20:12:27 +0100785 pci_dev_put(dev);
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100786 return 1;
787 }
788 return 0;
789}
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791static int __init longhaul_cpu_init(struct cpufreq_policy *policy)
792{
Mike Travis92cb7612007-10-19 20:35:04 +0200793 struct cpuinfo_x86 *c = &cpu_data(0);
Dave Jonesac617bd2009-01-17 23:29:53 -0500794 char *cpuname = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 int ret;
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +0100796 u32 lo, hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200798 /* Check what we have on this motherboard */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 switch (c->x86_model) {
800 case 6:
801 cpu_model = CPU_SAMUEL;
802 cpuname = "C3 'Samuel' [C5A]";
803 longhaul_version = TYPE_LONGHAUL_V1;
Dave Jonesac617bd2009-01-17 23:29:53 -0500804 memcpy(mults, samuel1_mults, sizeof(samuel1_mults));
805 memcpy(eblcr, samuel1_eblcr, sizeof(samuel1_eblcr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 break;
807
808 case 7:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 switch (c->x86_mask) {
810 case 0:
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +0100811 longhaul_version = TYPE_LONGHAUL_V1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 cpu_model = CPU_SAMUEL2;
813 cpuname = "C3 'Samuel 2' [C5B]";
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +0100814 /* Note, this is not a typo, early Samuel2's had
815 * Samuel1 ratios. */
Dave Jonesac617bd2009-01-17 23:29:53 -0500816 memcpy(mults, samuel1_mults, sizeof(samuel1_mults));
817 memcpy(eblcr, samuel2_eblcr, sizeof(samuel2_eblcr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 break;
819 case 1 ... 15:
Rafal Bilski07844252007-04-22 12:26:04 +0200820 longhaul_version = TYPE_LONGHAUL_V1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (c->x86_mask < 8) {
822 cpu_model = CPU_SAMUEL2;
823 cpuname = "C3 'Samuel 2' [C5B]";
824 } else {
825 cpu_model = CPU_EZRA;
826 cpuname = "C3 'Ezra' [C5C]";
827 }
Dave Jonesac617bd2009-01-17 23:29:53 -0500828 memcpy(mults, ezra_mults, sizeof(ezra_mults));
829 memcpy(eblcr, ezra_eblcr, sizeof(ezra_eblcr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 break;
831 }
832 break;
833
834 case 8:
835 cpu_model = CPU_EZRA_T;
836 cpuname = "C3 'Ezra-T' [C5M]";
837 longhaul_version = TYPE_POWERSAVER;
Dave Jonesac617bd2009-01-17 23:29:53 -0500838 numscales = 32;
839 memcpy(mults, ezrat_mults, sizeof(ezrat_mults));
840 memcpy(eblcr, ezrat_eblcr, sizeof(ezrat_eblcr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 break;
842
843 case 9:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 longhaul_version = TYPE_POWERSAVER;
Rafa³ Bilski0d44b2b2007-01-31 23:50:49 +0100845 numscales = 32;
Dave Jonesac617bd2009-01-17 23:29:53 -0500846 memcpy(mults, nehemiah_mults, sizeof(nehemiah_mults));
847 memcpy(eblcr, nehemiah_eblcr, sizeof(nehemiah_eblcr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 switch (c->x86_mask) {
849 case 0 ... 1:
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100850 cpu_model = CPU_NEHEMIAH;
Rafa³ Bilskie57501c2007-02-08 23:12:02 +0100851 cpuname = "C3 'Nehemiah A' [C5XLOE]";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 break;
853 case 2 ... 4:
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100854 cpu_model = CPU_NEHEMIAH;
Rafa³ Bilskie57501c2007-02-08 23:12:02 +0100855 cpuname = "C3 'Nehemiah B' [C5XLOH]";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 break;
857 case 5 ... 15:
Rafa³ Bilski980342a2007-01-31 23:42:47 +0100858 cpu_model = CPU_NEHEMIAH_C;
Rafa³ Bilskie57501c2007-02-08 23:12:02 +0100859 cpuname = "C3 'Nehemiah C' [C5P]";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 break;
861 }
862 break;
863
864 default:
865 cpuname = "Unknown";
866 break;
867 }
Rafa³ Bilski2b8c0e12007-02-14 22:00:37 +0100868 /* Check Longhaul ver. 2 */
869 if (longhaul_version == TYPE_LONGHAUL_V2) {
870 rdmsr(MSR_VIA_LONGHAUL, lo, hi);
871 if (lo == 0 && hi == 0)
872 /* Looks like MSR isn't present */
873 longhaul_version = TYPE_LONGHAUL_V1;
874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Dave Jonesac617bd2009-01-17 23:29:53 -0500876 printk(KERN_INFO PFX "VIA %s CPU detected. ", cpuname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 switch (longhaul_version) {
878 case TYPE_LONGHAUL_V1:
879 case TYPE_LONGHAUL_V2:
Dave Jonesac617bd2009-01-17 23:29:53 -0500880 printk(KERN_CONT "Longhaul v%d supported.\n", longhaul_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 break;
882 case TYPE_POWERSAVER:
Dave Jonesac617bd2009-01-17 23:29:53 -0500883 printk(KERN_CONT "Powersaver supported.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 break;
885 };
886
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100887 /* Doesn't hurt */
Rafał Bilski7d5edcc2007-05-17 22:33:46 +0200888 longhaul_setup_southbridge();
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100889
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200890 /* Find ACPI data for processor */
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100891 acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
892 ACPI_UINT32_MAX, &longhaul_walk_callback,
893 NULL, (void *)&pr);
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200894
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100895 /* Check ACPI support for C3 state */
Dave Jones7ab77e02007-04-20 15:58:00 -0400896 if (pr != NULL && longhaul_version == TYPE_POWERSAVER) {
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200897 cx = &pr->power.states[ACPI_STATE_C3];
Rafał Bilski7d5edcc2007-05-17 22:33:46 +0200898 if (cx->address > 0 && cx->latency <= 1000)
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100899 longhaul_flags |= USE_ACPI_C3;
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200900 }
Rafał Bilski905497c2007-07-08 21:51:26 +0200901 /* Disable if it isn't working */
902 if (disable_acpi_c3)
903 longhaul_flags &= ~USE_ACPI_C3;
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100904 /* Check if northbridge is friendly */
Rafał Bilski7d5edcc2007-05-17 22:33:46 +0200905 if (enable_arbiter_disable())
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100906 longhaul_flags |= USE_NORTHBRIDGE;
Rafał Bilski7d5edcc2007-05-17 22:33:46 +0200907
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100908 /* Check ACPI support for bus master arbiter disable */
Rafał Bilski7d5edcc2007-05-17 22:33:46 +0200909 if (!(longhaul_flags & USE_ACPI_C3
910 || longhaul_flags & USE_NORTHBRIDGE)
911 && ((pr == NULL) || !(pr->flags.bm_control))) {
Rafa³ Bilski264166e2006-12-24 14:04:23 +0100912 printk(KERN_ERR PFX
913 "No ACPI support. Unsupported northbridge.\n");
914 return -ENODEV;
915 }
916
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100917 if (longhaul_flags & USE_NORTHBRIDGE)
Rafał Bilski7d5edcc2007-05-17 22:33:46 +0200918 printk(KERN_INFO PFX "Using northbridge support.\n");
919 if (longhaul_flags & USE_ACPI_C3)
920 printk(KERN_INFO PFX "Using ACPI support.\n");
Rafa³ Bilski179da8e2006-08-08 19:12:20 +0200921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 ret = longhaul_get_ranges();
923 if (ret != 0)
924 return ret;
925
Rafa³ Bilski786f46b2007-02-04 18:43:12 +0100926 if ((longhaul_version != TYPE_LONGHAUL_V1) && (scale_voltage != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 longhaul_setup_voltagescaling();
928
Dave Jones6778bae2005-05-31 19:03:51 -0700929 policy->cpuinfo.transition_latency = 200000; /* nsec */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 policy->cur = calc_speed(longhaul_get_cpu_mult());
931
932 ret = cpufreq_frequency_table_cpuinfo(policy, longhaul_table);
933 if (ret)
934 return ret;
935
936 cpufreq_frequency_table_get_attr(longhaul_table, policy->cpu);
937
938 return 0;
939}
940
941static int __devexit longhaul_cpu_exit(struct cpufreq_policy *policy)
942{
943 cpufreq_frequency_table_put_attr(policy->cpu);
944 return 0;
945}
946
Dave Jonesac617bd2009-01-17 23:29:53 -0500947static struct freq_attr *longhaul_attr[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 &cpufreq_freq_attr_scaling_available_freqs,
949 NULL,
950};
951
Linus Torvalds221dee22007-02-26 14:55:48 -0800952static struct cpufreq_driver longhaul_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 .verify = longhaul_verify,
954 .target = longhaul_target,
955 .get = longhaul_get,
956 .init = longhaul_cpu_init,
957 .exit = __devexit_p(longhaul_cpu_exit),
958 .name = "longhaul",
959 .owner = THIS_MODULE,
960 .attr = longhaul_attr,
961};
962
963
964static int __init longhaul_init(void)
965{
Mike Travis92cb7612007-10-19 20:35:04 +0200966 struct cpuinfo_x86 *c = &cpu_data(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 if (c->x86_vendor != X86_VENDOR_CENTAUR || c->x86 != 6)
969 return -ENODEV;
970
Rafa³ Bilski48b7bde2006-07-04 17:50:57 +0200971#ifdef CONFIG_SMP
972 if (num_online_cpus() > 1) {
Dave Jonesac617bd2009-01-17 23:29:53 -0500973 printk(KERN_ERR PFX "More than 1 CPU detected, "
974 "longhaul disabled.\n");
Dave Jones1cfe2012006-12-28 22:30:16 -0500975 return -ENODEV;
Rafa³ Bilski48b7bde2006-07-04 17:50:57 +0200976 }
977#endif
978#ifdef CONFIG_X86_IO_APIC
979 if (cpu_has_apic) {
Dave Jonesac617bd2009-01-17 23:29:53 -0500980 printk(KERN_ERR PFX "APIC detected. Longhaul is currently "
981 "broken in this configuration.\n");
Rafa³ Bilski48b7bde2006-07-04 17:50:57 +0200982 return -ENODEV;
983 }
984#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 switch (c->x86_model) {
986 case 6 ... 9:
987 return cpufreq_register_driver(&longhaul_driver);
Dave Jones8ec98222006-12-17 19:07:35 -0500988 case 10:
989 printk(KERN_ERR PFX "Use acpi-cpufreq driver for VIA C7\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 default:
Fernando Carrijoc19a28e2009-01-07 18:09:08 -0800991 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
993
994 return -ENODEV;
995}
996
997
998static void __exit longhaul_exit(void)
999{
Dave Jones8eebf1a2006-05-30 17:40:16 -04001000 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Dave Jonesac617bd2009-01-17 23:29:53 -05001002 for (i = 0; i < numscales; i++) {
1003 if (mults[i] == maxmult) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 longhaul_setstate(i);
1005 break;
1006 }
1007 }
1008
1009 cpufreq_unregister_driver(&longhaul_driver);
1010 kfree(longhaul_table);
1011}
1012
Rafal Bilski52a26382007-10-07 00:24:32 -07001013/* Even if BIOS is exporting ACPI C3 state, and it is used
1014 * with success when CPU is idle, this state doesn't
1015 * trigger frequency transition in some cases. */
Dave Jonesac617bd2009-01-17 23:29:53 -05001016module_param(disable_acpi_c3, int, 0644);
Rafał Bilski905497c2007-07-08 21:51:26 +02001017MODULE_PARM_DESC(disable_acpi_c3, "Don't use ACPI C3 support");
Rafal Bilski52a26382007-10-07 00:24:32 -07001018/* Change CPU voltage with frequency. Very usefull to save
1019 * power, but most VIA C3 processors aren't supporting it. */
Dave Jonesac617bd2009-01-17 23:29:53 -05001020module_param(scale_voltage, int, 0644);
Rafa³ Bilskidb44aaf2006-08-16 01:07:33 +02001021MODULE_PARM_DESC(scale_voltage, "Scale voltage of processor");
Rafal Bilski52a26382007-10-07 00:24:32 -07001022/* Force revision key to 0 for processors which doesn't
1023 * support voltage scaling, but are introducing itself as
1024 * such. */
1025module_param(revid_errata, int, 0644);
1026MODULE_PARM_DESC(revid_errata, "Ignore CPU Revision ID");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Dave Jonesac617bd2009-01-17 23:29:53 -05001028MODULE_AUTHOR("Dave Jones <davej@redhat.com>");
1029MODULE_DESCRIPTION("Longhaul driver for VIA Cyrix processors.");
1030MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Rafa³ Bilski0d6daba2006-07-07 08:48:26 +02001032late_initcall(longhaul_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033module_exit(longhaul_exit);