blob: fe7053cd68aff832267f3f43f727e8ed0ac2076d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Dave Jones32ee8c32006-02-28 00:43:23 -05002 * elanfreq: cpufreq driver for the AMD ELAN family
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * (c) Copyright 2002 Robert Schwebel <r.schwebel@pengutronix.de>
5 *
Dave Jones32ee8c32006-02-28 00:43:23 -05006 * Parts of this code are (c) Sven Geggus <sven@geggus.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Dave Jones32ee8c32006-02-28 00:43:23 -05008 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
Dave Jones32ee8c32006-02-28 00:43:23 -050013 * 2 of the License, or (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 * 2002-02-13: - initial revision for 2.4.18-pre9 by Robert Schwebel
16 *
17 */
18
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/init.h>
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/delay.h>
24#include <linux/cpufreq.h>
25
Andi Kleenfa8031a2012-01-26 00:09:12 +010026#include <asm/cpu_device_id.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/msr.h>
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +020028#include <linux/timex.h>
29#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Dave Jones32ee8c32006-02-28 00:43:23 -050031#define REG_CSCIR 0x22 /* Chip Setup and Control Index Register */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#define REG_CSCDR 0x23 /* Chip Setup and Control Data Register */
33
34/* Module parameter */
35static int max_freq;
36
37struct s_elan_multiplier {
38 int clock; /* frequency in kHz */
39 int val40h; /* PMU Force Mode register */
40 int val80h; /* CPU Clock Speed Register */
41};
42
43/*
Dave Jones32ee8c32006-02-28 00:43:23 -050044 * It is important that the frequencies
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * are listed in ascending order here!
46 */
Dave Jones460f5ef2008-07-30 13:01:42 -040047static struct s_elan_multiplier elan_multiplier[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 {1000, 0x02, 0x18},
49 {2000, 0x02, 0x10},
50 {4000, 0x02, 0x08},
51 {8000, 0x00, 0x00},
52 {16000, 0x00, 0x02},
53 {33000, 0x00, 0x04},
54 {66000, 0x01, 0x04},
55 {99000, 0x01, 0x05}
56};
57
58static struct cpufreq_frequency_table elanfreq_table[] = {
59 {0, 1000},
60 {1, 2000},
61 {2, 4000},
62 {3, 8000},
63 {4, 16000},
64 {5, 33000},
65 {6, 66000},
66 {7, 99000},
67 {0, CPUFREQ_TABLE_END},
68};
69
70
71/**
72 * elanfreq_get_cpu_frequency: determine current cpu speed
73 *
74 * Finds out at which frequency the CPU of the Elan SOC runs
Dave Jones32ee8c32006-02-28 00:43:23 -050075 * at the moment. Frequencies from 1 to 33 MHz are generated
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 * the normal way, 66 and 99 MHz are called "Hyperspeed Mode"
Dave Jones32ee8c32006-02-28 00:43:23 -050077 * and have the rest of the chip running with 33 MHz.
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 */
79
80static unsigned int elanfreq_get_cpu_frequency(unsigned int cpu)
81{
Dave Jones32ee8c32006-02-28 00:43:23 -050082 u8 clockspeed_reg; /* Clock Speed Register */
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 local_irq_disable();
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +020085 outb_p(0x80, REG_CSCIR);
Dave Jones32ee8c32006-02-28 00:43:23 -050086 clockspeed_reg = inb_p(REG_CSCDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 local_irq_enable();
88
Dave Jones32ee8c32006-02-28 00:43:23 -050089 if ((clockspeed_reg & 0xE0) == 0xE0)
90 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Dave Jones32ee8c32006-02-28 00:43:23 -050092 /* Are we in CPU clock multiplied mode (66/99 MHz)? */
93 if ((clockspeed_reg & 0xE0) == 0xC0) {
94 if ((clockspeed_reg & 0x01) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return 66000;
Dave Jones32ee8c32006-02-28 00:43:23 -050096 else
97 return 99000;
98 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 /* 33 MHz is not 32 MHz... */
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +0200101 if ((clockspeed_reg & 0xE0) == 0xA0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return 33000;
103
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +0200104 return (1<<((clockspeed_reg & 0xE0) >> 5)) * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
107
108/**
Dave Jones32ee8c32006-02-28 00:43:23 -0500109 * elanfreq_set_cpu_frequency: Change the CPU core frequency
110 * @cpu: cpu number
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * @freq: frequency in kHz
112 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500113 * This function takes a frequency value and changes the CPU frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * according to this. Note that the frequency has to be checked by
115 * elanfreq_validatespeed() for correctness!
Dave Jones32ee8c32006-02-28 00:43:23 -0500116 *
117 * There is no return value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 */
119
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530120static void elanfreq_set_cpu_state(struct cpufreq_policy *policy,
121 unsigned int state)
Dave Jones32ee8c32006-02-28 00:43:23 -0500122{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 struct cpufreq_freqs freqs;
124
125 freqs.old = elanfreq_get_cpu_frequency(0);
126 freqs.new = elan_multiplier[state].clock;
Dave Jones32ee8c32006-02-28 00:43:23 -0500127
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530128 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Dave Jones32ee8c32006-02-28 00:43:23 -0500130 printk(KERN_INFO "elanfreq: attempting to set frequency to %i kHz\n",
131 elan_multiplier[state].clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133
Dave Jones32ee8c32006-02-28 00:43:23 -0500134 /*
135 * Access to the Elan's internal registers is indexed via
136 * 0x22: Chip Setup & Control Register Index Register (CSCI)
137 * 0x23: Chip Setup & Control Register Data Register (CSCD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 *
139 */
140
Dave Jones32ee8c32006-02-28 00:43:23 -0500141 /*
142 * 0x40 is the Power Management Unit's Force Mode Register.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 * Bit 6 enables Hyperspeed Mode (66/100 MHz core frequency)
144 */
145
146 local_irq_disable();
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +0200147 outb_p(0x40, REG_CSCIR); /* Disable hyperspeed mode */
148 outb_p(0x00, REG_CSCDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 local_irq_enable(); /* wait till internal pipelines and */
150 udelay(1000); /* buffers have cleaned up */
151
152 local_irq_disable();
153
154 /* now, set the CPU clock speed register (0x80) */
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +0200155 outb_p(0x80, REG_CSCIR);
156 outb_p(elan_multiplier[state].val80h, REG_CSCDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 /* now, the hyperspeed bit in PMU Force Mode Register (0x40) */
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +0200159 outb_p(0x40, REG_CSCIR);
160 outb_p(elan_multiplier[state].val40h, REG_CSCDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 udelay(10000);
162 local_irq_enable();
163
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530164 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165};
166
167
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +0200168static int elanfreq_target(struct cpufreq_policy *policy,
Dave Jones32ee8c32006-02-28 00:43:23 -0500169 unsigned int target_freq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 unsigned int relation)
171{
Dave Jones32ee8c32006-02-28 00:43:23 -0500172 unsigned int newstate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Dave Jones04cd1a92009-01-17 22:50:33 -0500174 if (cpufreq_frequency_table_target(policy, &elanfreq_table[0],
175 target_freq, relation, &newstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return -EINVAL;
177
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530178 elanfreq_set_cpu_state(policy, newstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 return 0;
181}
182
183
184/*
185 * Module init and exit code
186 */
187
188static int elanfreq_cpu_init(struct cpufreq_policy *policy)
189{
Mike Travis92cb7612007-10-19 20:35:04 +0200190 struct cpuinfo_x86 *c = &cpu_data(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 /* capability check */
194 if ((c->x86_vendor != X86_VENDOR_AMD) ||
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +0200195 (c->x86 != 4) || (c->x86_model != 10))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return -ENODEV;
197
198 /* max freq */
199 if (!max_freq)
200 max_freq = elanfreq_get_cpu_frequency(0);
201
202 /* table init */
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +0200203 for (i = 0; (elanfreq_table[i].frequency != CPUFREQ_TABLE_END); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if (elanfreq_table[i].frequency > max_freq)
205 elanfreq_table[i].frequency = CPUFREQ_ENTRY_INVALID;
206 }
207
208 /* cpuinfo and default policy values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
210 policy->cur = elanfreq_get_cpu_frequency(0);
211
Viresh Kumar55bb85b2013-09-16 18:56:15 +0530212 return cpufreq_table_validate_and_show(policy, elanfreq_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216#ifndef MODULE
217/**
218 * elanfreq_setup - elanfreq command line parameter parsing
219 *
220 * elanfreq command line parameter. Use:
221 * elanfreq=66000
222 * to set the maximum CPU frequency to 66 MHz. Note that in
223 * case you do not give this boot parameter, the maximum
224 * frequency will fall back to _current_ CPU frequency which
225 * might be lower. If you build this as a module, use the
226 * max_freq module parameter instead.
227 */
228static int __init elanfreq_setup(char *str)
229{
230 max_freq = simple_strtoul(str, &str, 0);
231 printk(KERN_WARNING "You're using the deprecated elanfreq command line option. Use elanfreq.max_freq instead, please!\n");
232 return 1;
233}
234__setup("elanfreq=", elanfreq_setup);
235#endif
236
237
Linus Torvalds221dee22007-02-26 14:55:48 -0800238static struct cpufreq_driver elanfreq_driver = {
Dave Jones32ee8c32006-02-28 00:43:23 -0500239 .get = elanfreq_get_cpu_frequency,
Viresh Kumar06494eb2013-10-03 20:28:05 +0530240 .verify = cpufreq_generic_frequency_table_verify,
Dave Jones32ee8c32006-02-28 00:43:23 -0500241 .target = elanfreq_target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 .init = elanfreq_cpu_init,
Viresh Kumar06494eb2013-10-03 20:28:05 +0530243 .exit = cpufreq_generic_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 .name = "elanfreq",
Viresh Kumar06494eb2013-10-03 20:28:05 +0530245 .attr = cpufreq_generic_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246};
247
Andi Kleenfa8031a2012-01-26 00:09:12 +0100248static const struct x86_cpu_id elan_id[] = {
249 { X86_VENDOR_AMD, 4, 10, },
250 {}
251};
252MODULE_DEVICE_TABLE(x86cpu, elan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Dave Jones32ee8c32006-02-28 00:43:23 -0500254static int __init elanfreq_init(void)
255{
Andi Kleenfa8031a2012-01-26 00:09:12 +0100256 if (!x86_match_cpu(elan_id))
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +0200257 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return cpufreq_register_driver(&elanfreq_driver);
259}
260
261
Dave Jones32ee8c32006-02-28 00:43:23 -0500262static void __exit elanfreq_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 cpufreq_unregister_driver(&elanfreq_driver);
265}
266
267
Paolo Ciarrocchi18c6faa2008-07-28 13:00:49 +0200268module_param(max_freq, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270MODULE_LICENSE("GPL");
Dave Jones04cd1a92009-01-17 22:50:33 -0500271MODULE_AUTHOR("Robert Schwebel <r.schwebel@pengutronix.de>, "
272 "Sven Geggus <sven@geggus.net>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273MODULE_DESCRIPTION("cpufreq driver for AMD's Elan CPUs");
274
275module_init(elanfreq_init);
276module_exit(elanfreq_exit);