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