Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 2 | * elanfreq: cpufreq driver for the AMD ELAN family |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * (c) Copyright 2002 Robert Schwebel <r.schwebel@pengutronix.de> |
| 5 | * |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 6 | * Parts of this code are (c) Sven Geggus <sven@geggus.net> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 8 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * |
| 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 Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 13 | * 2 of the License, or (at your option) any later version. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * |
| 15 | * 2002-02-13: - initial revision for 2.4.18-pre9 by Robert Schwebel |
| 16 | * |
| 17 | */ |
| 18 | |
Joe Perches | 1c5864e | 2016-04-05 13:28:25 -0700 | [diff] [blame] | 19 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/kernel.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/init.h> |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/delay.h> |
| 26 | #include <linux/cpufreq.h> |
| 27 | |
Andi Kleen | fa8031a | 2012-01-26 00:09:12 +0100 | [diff] [blame] | 28 | #include <asm/cpu_device_id.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <asm/msr.h> |
Paolo Ciarrocchi | 18c6faa | 2008-07-28 13:00:49 +0200 | [diff] [blame] | 30 | #include <linux/timex.h> |
| 31 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 33 | #define REG_CSCIR 0x22 /* Chip Setup and Control Index Register */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #define REG_CSCDR 0x23 /* Chip Setup and Control Data Register */ |
| 35 | |
| 36 | /* Module parameter */ |
| 37 | static int max_freq; |
| 38 | |
| 39 | struct s_elan_multiplier { |
| 40 | int clock; /* frequency in kHz */ |
| 41 | int val40h; /* PMU Force Mode register */ |
| 42 | int val80h; /* CPU Clock Speed Register */ |
| 43 | }; |
| 44 | |
| 45 | /* |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 46 | * It is important that the frequencies |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | * are listed in ascending order here! |
| 48 | */ |
Dave Jones | 460f5ef | 2008-07-30 13:01:42 -0400 | [diff] [blame] | 49 | static struct s_elan_multiplier elan_multiplier[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | {1000, 0x02, 0x18}, |
| 51 | {2000, 0x02, 0x10}, |
| 52 | {4000, 0x02, 0x08}, |
| 53 | {8000, 0x00, 0x00}, |
| 54 | {16000, 0x00, 0x02}, |
| 55 | {33000, 0x00, 0x04}, |
| 56 | {66000, 0x01, 0x04}, |
| 57 | {99000, 0x01, 0x05} |
| 58 | }; |
| 59 | |
| 60 | static struct cpufreq_frequency_table elanfreq_table[] = { |
Viresh Kumar | 7f4b046 | 2014-03-28 19:11:47 +0530 | [diff] [blame] | 61 | {0, 0, 1000}, |
| 62 | {0, 1, 2000}, |
| 63 | {0, 2, 4000}, |
| 64 | {0, 3, 8000}, |
| 65 | {0, 4, 16000}, |
| 66 | {0, 5, 33000}, |
| 67 | {0, 6, 66000}, |
| 68 | {0, 7, 99000}, |
| 69 | {0, 0, CPUFREQ_TABLE_END}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | |
| 73 | /** |
| 74 | * elanfreq_get_cpu_frequency: determine current cpu speed |
| 75 | * |
| 76 | * Finds out at which frequency the CPU of the Elan SOC runs |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 77 | * at the moment. Frequencies from 1 to 33 MHz are generated |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | * the normal way, 66 and 99 MHz are called "Hyperspeed Mode" |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 79 | * and have the rest of the chip running with 33 MHz. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | */ |
| 81 | |
| 82 | static unsigned int elanfreq_get_cpu_frequency(unsigned int cpu) |
| 83 | { |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 84 | u8 clockspeed_reg; /* Clock Speed Register */ |
| 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | local_irq_disable(); |
Paolo Ciarrocchi | 18c6faa | 2008-07-28 13:00:49 +0200 | [diff] [blame] | 87 | outb_p(0x80, REG_CSCIR); |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 88 | clockspeed_reg = inb_p(REG_CSCDR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | local_irq_enable(); |
| 90 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 91 | if ((clockspeed_reg & 0xE0) == 0xE0) |
| 92 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 94 | /* Are we in CPU clock multiplied mode (66/99 MHz)? */ |
| 95 | if ((clockspeed_reg & 0xE0) == 0xC0) { |
| 96 | if ((clockspeed_reg & 0x01) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | return 66000; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 98 | else |
| 99 | return 99000; |
| 100 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | /* 33 MHz is not 32 MHz... */ |
Paolo Ciarrocchi | 18c6faa | 2008-07-28 13:00:49 +0200 | [diff] [blame] | 103 | if ((clockspeed_reg & 0xE0) == 0xA0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | return 33000; |
| 105 | |
Paolo Ciarrocchi | 18c6faa | 2008-07-28 13:00:49 +0200 | [diff] [blame] | 106 | return (1<<((clockspeed_reg & 0xE0) >> 5)) * 1000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 110 | static int elanfreq_target(struct cpufreq_policy *policy, |
| 111 | unsigned int state) |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 112 | { |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 113 | /* |
| 114 | * Access to the Elan's internal registers is indexed via |
| 115 | * 0x22: Chip Setup & Control Register Index Register (CSCI) |
| 116 | * 0x23: Chip Setup & Control Register Data Register (CSCD) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | * |
| 118 | */ |
| 119 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 120 | /* |
| 121 | * 0x40 is the Power Management Unit's Force Mode Register. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | * Bit 6 enables Hyperspeed Mode (66/100 MHz core frequency) |
| 123 | */ |
| 124 | |
| 125 | local_irq_disable(); |
Paolo Ciarrocchi | 18c6faa | 2008-07-28 13:00:49 +0200 | [diff] [blame] | 126 | outb_p(0x40, REG_CSCIR); /* Disable hyperspeed mode */ |
| 127 | outb_p(0x00, REG_CSCDR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | local_irq_enable(); /* wait till internal pipelines and */ |
| 129 | udelay(1000); /* buffers have cleaned up */ |
| 130 | |
| 131 | local_irq_disable(); |
| 132 | |
| 133 | /* now, set the CPU clock speed register (0x80) */ |
Paolo Ciarrocchi | 18c6faa | 2008-07-28 13:00:49 +0200 | [diff] [blame] | 134 | outb_p(0x80, REG_CSCIR); |
| 135 | outb_p(elan_multiplier[state].val80h, REG_CSCDR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
| 137 | /* now, the hyperspeed bit in PMU Force Mode Register (0x40) */ |
Paolo Ciarrocchi | 18c6faa | 2008-07-28 13:00:49 +0200 | [diff] [blame] | 138 | outb_p(0x40, REG_CSCIR); |
| 139 | outb_p(elan_multiplier[state].val40h, REG_CSCDR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | udelay(10000); |
| 141 | local_irq_enable(); |
| 142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | return 0; |
| 144 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | /* |
| 146 | * Module init and exit code |
| 147 | */ |
| 148 | |
| 149 | static int elanfreq_cpu_init(struct cpufreq_policy *policy) |
| 150 | { |
Mike Travis | 92cb761 | 2007-10-19 20:35:04 +0200 | [diff] [blame] | 151 | struct cpuinfo_x86 *c = &cpu_data(0); |
Stratos Karafotis | 041526f | 2014-04-25 23:15:38 +0300 | [diff] [blame] | 152 | struct cpufreq_frequency_table *pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
| 154 | /* capability check */ |
| 155 | if ((c->x86_vendor != X86_VENDOR_AMD) || |
Paolo Ciarrocchi | 18c6faa | 2008-07-28 13:00:49 +0200 | [diff] [blame] | 156 | (c->x86 != 4) || (c->x86_model != 10)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | return -ENODEV; |
| 158 | |
| 159 | /* max freq */ |
| 160 | if (!max_freq) |
| 161 | max_freq = elanfreq_get_cpu_frequency(0); |
| 162 | |
| 163 | /* table init */ |
Stratos Karafotis | 041526f | 2014-04-25 23:15:38 +0300 | [diff] [blame] | 164 | cpufreq_for_each_entry(pos, elanfreq_table) |
| 165 | if (pos->frequency > max_freq) |
| 166 | pos->frequency = CPUFREQ_ENTRY_INVALID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
Viresh Kumar | c3e3cc8 | 2018-02-26 10:38:52 +0530 | [diff] [blame] | 168 | policy->freq_table = elanfreq_table; |
| 169 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | #ifndef MODULE |
| 174 | /** |
| 175 | * elanfreq_setup - elanfreq command line parameter parsing |
| 176 | * |
| 177 | * elanfreq command line parameter. Use: |
| 178 | * elanfreq=66000 |
| 179 | * to set the maximum CPU frequency to 66 MHz. Note that in |
| 180 | * case you do not give this boot parameter, the maximum |
| 181 | * frequency will fall back to _current_ CPU frequency which |
| 182 | * might be lower. If you build this as a module, use the |
| 183 | * max_freq module parameter instead. |
| 184 | */ |
| 185 | static int __init elanfreq_setup(char *str) |
| 186 | { |
| 187 | max_freq = simple_strtoul(str, &str, 0); |
Joe Perches | b49c22a | 2016-04-05 13:28:24 -0700 | [diff] [blame] | 188 | pr_warn("You're using the deprecated elanfreq command line option. Use elanfreq.max_freq instead, please!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | return 1; |
| 190 | } |
| 191 | __setup("elanfreq=", elanfreq_setup); |
| 192 | #endif |
| 193 | |
| 194 | |
Linus Torvalds | 221dee2 | 2007-02-26 14:55:48 -0800 | [diff] [blame] | 195 | static struct cpufreq_driver elanfreq_driver = { |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 196 | .get = elanfreq_get_cpu_frequency, |
Viresh Kumar | fe829ed | 2017-07-19 15:42:48 +0530 | [diff] [blame] | 197 | .flags = CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING, |
Viresh Kumar | 06494eb | 2013-10-03 20:28:05 +0530 | [diff] [blame] | 198 | .verify = cpufreq_generic_frequency_table_verify, |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 199 | .target_index = elanfreq_target, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | .init = elanfreq_cpu_init, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | .name = "elanfreq", |
Viresh Kumar | 06494eb | 2013-10-03 20:28:05 +0530 | [diff] [blame] | 202 | .attr = cpufreq_generic_attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | }; |
| 204 | |
Andi Kleen | fa8031a | 2012-01-26 00:09:12 +0100 | [diff] [blame] | 205 | static const struct x86_cpu_id elan_id[] = { |
| 206 | { X86_VENDOR_AMD, 4, 10, }, |
| 207 | {} |
| 208 | }; |
| 209 | MODULE_DEVICE_TABLE(x86cpu, elan_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 211 | static int __init elanfreq_init(void) |
| 212 | { |
Andi Kleen | fa8031a | 2012-01-26 00:09:12 +0100 | [diff] [blame] | 213 | if (!x86_match_cpu(elan_id)) |
Paolo Ciarrocchi | 18c6faa | 2008-07-28 13:00:49 +0200 | [diff] [blame] | 214 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | return cpufreq_register_driver(&elanfreq_driver); |
| 216 | } |
| 217 | |
| 218 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 219 | static void __exit elanfreq_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | { |
| 221 | cpufreq_unregister_driver(&elanfreq_driver); |
| 222 | } |
| 223 | |
| 224 | |
Paolo Ciarrocchi | 18c6faa | 2008-07-28 13:00:49 +0200 | [diff] [blame] | 225 | module_param(max_freq, int, 0444); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
| 227 | MODULE_LICENSE("GPL"); |
Dave Jones | 04cd1a9 | 2009-01-17 22:50:33 -0500 | [diff] [blame] | 228 | MODULE_AUTHOR("Robert Schwebel <r.schwebel@pengutronix.de>, " |
| 229 | "Sven Geggus <sven@geggus.net>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | MODULE_DESCRIPTION("cpufreq driver for AMD's Elan CPUs"); |
| 231 | |
| 232 | module_init(elanfreq_init); |
| 233 | module_exit(elanfreq_exit); |