elanfreq.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * elanfreq: cpufreq driver for the AMD ELAN family
  3. *
  4. * (c) Copyright 2002 Robert Schwebel <r.schwebel@pengutronix.de>
  5. *
  6. * Parts of this code are (c) Sven Geggus <sven@geggus.net>
  7. *
  8. * All Rights Reserved.
  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
  13. * 2 of the License, or (at your option) any later version.
  14. *
  15. * 2002-02-13: - initial revision for 2.4.18-pre9 by Robert Schwebel
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/delay.h>
  22. #include <linux/cpufreq.h>
  23. #include <asm/cpu_device_id.h>
  24. #include <asm/msr.h>
  25. #include <linux/timex.h>
  26. #include <linux/io.h>
  27. #define REG_CSCIR 0x22 /* Chip Setup and Control Index Register */
  28. #define REG_CSCDR 0x23 /* Chip Setup and Control Data Register */
  29. /* Module parameter */
  30. static int max_freq;
  31. struct s_elan_multiplier {
  32. int clock; /* frequency in kHz */
  33. int val40h; /* PMU Force Mode register */
  34. int val80h; /* CPU Clock Speed Register */
  35. };
  36. /*
  37. * It is important that the frequencies
  38. * are listed in ascending order here!
  39. */
  40. static struct s_elan_multiplier elan_multiplier[] = {
  41. {1000, 0x02, 0x18},
  42. {2000, 0x02, 0x10},
  43. {4000, 0x02, 0x08},
  44. {8000, 0x00, 0x00},
  45. {16000, 0x00, 0x02},
  46. {33000, 0x00, 0x04},
  47. {66000, 0x01, 0x04},
  48. {99000, 0x01, 0x05}
  49. };
  50. static struct cpufreq_frequency_table elanfreq_table[] = {
  51. {0, 1000},
  52. {1, 2000},
  53. {2, 4000},
  54. {3, 8000},
  55. {4, 16000},
  56. {5, 33000},
  57. {6, 66000},
  58. {7, 99000},
  59. {0, CPUFREQ_TABLE_END},
  60. };
  61. /**
  62. * elanfreq_get_cpu_frequency: determine current cpu speed
  63. *
  64. * Finds out at which frequency the CPU of the Elan SOC runs
  65. * at the moment. Frequencies from 1 to 33 MHz are generated
  66. * the normal way, 66 and 99 MHz are called "Hyperspeed Mode"
  67. * and have the rest of the chip running with 33 MHz.
  68. */
  69. static unsigned int elanfreq_get_cpu_frequency(unsigned int cpu)
  70. {
  71. u8 clockspeed_reg; /* Clock Speed Register */
  72. local_irq_disable();
  73. outb_p(0x80, REG_CSCIR);
  74. clockspeed_reg = inb_p(REG_CSCDR);
  75. local_irq_enable();
  76. if ((clockspeed_reg & 0xE0) == 0xE0)
  77. return 0;
  78. /* Are we in CPU clock multiplied mode (66/99 MHz)? */
  79. if ((clockspeed_reg & 0xE0) == 0xC0) {
  80. if ((clockspeed_reg & 0x01) == 0)
  81. return 66000;
  82. else
  83. return 99000;
  84. }
  85. /* 33 MHz is not 32 MHz... */
  86. if ((clockspeed_reg & 0xE0) == 0xA0)
  87. return 33000;
  88. return (1<<((clockspeed_reg & 0xE0) >> 5)) * 1000;
  89. }
  90. /**
  91. * elanfreq_set_cpu_frequency: Change the CPU core frequency
  92. * @cpu: cpu number
  93. * @freq: frequency in kHz
  94. *
  95. * This function takes a frequency value and changes the CPU frequency
  96. * according to this. Note that the frequency has to be checked by
  97. * elanfreq_validatespeed() for correctness!
  98. *
  99. * There is no return value.
  100. */
  101. static void elanfreq_set_cpu_state(unsigned int state)
  102. {
  103. struct cpufreq_freqs freqs;
  104. freqs.old = elanfreq_get_cpu_frequency(0);
  105. freqs.new = elan_multiplier[state].clock;
  106. freqs.cpu = 0; /* elanfreq.c is UP only driver */
  107. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  108. printk(KERN_INFO "elanfreq: attempting to set frequency to %i kHz\n",
  109. elan_multiplier[state].clock);
  110. /*
  111. * Access to the Elan's internal registers is indexed via
  112. * 0x22: Chip Setup & Control Register Index Register (CSCI)
  113. * 0x23: Chip Setup & Control Register Data Register (CSCD)
  114. *
  115. */
  116. /*
  117. * 0x40 is the Power Management Unit's Force Mode Register.
  118. * Bit 6 enables Hyperspeed Mode (66/100 MHz core frequency)
  119. */
  120. local_irq_disable();
  121. outb_p(0x40, REG_CSCIR); /* Disable hyperspeed mode */
  122. outb_p(0x00, REG_CSCDR);
  123. local_irq_enable(); /* wait till internal pipelines and */
  124. udelay(1000); /* buffers have cleaned up */
  125. local_irq_disable();
  126. /* now, set the CPU clock speed register (0x80) */
  127. outb_p(0x80, REG_CSCIR);
  128. outb_p(elan_multiplier[state].val80h, REG_CSCDR);
  129. /* now, the hyperspeed bit in PMU Force Mode Register (0x40) */
  130. outb_p(0x40, REG_CSCIR);
  131. outb_p(elan_multiplier[state].val40h, REG_CSCDR);
  132. udelay(10000);
  133. local_irq_enable();
  134. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  135. };
  136. /**
  137. * elanfreq_validatespeed: test if frequency range is valid
  138. * @policy: the policy to validate
  139. *
  140. * This function checks if a given frequency range in kHz is valid
  141. * for the hardware supported by the driver.
  142. */
  143. static int elanfreq_verify(struct cpufreq_policy *policy)
  144. {
  145. return cpufreq_frequency_table_verify(policy, &elanfreq_table[0]);
  146. }
  147. static int elanfreq_target(struct cpufreq_policy *policy,
  148. unsigned int target_freq,
  149. unsigned int relation)
  150. {
  151. unsigned int newstate = 0;
  152. if (cpufreq_frequency_table_target(policy, &elanfreq_table[0],
  153. target_freq, relation, &newstate))
  154. return -EINVAL;
  155. elanfreq_set_cpu_state(newstate);
  156. return 0;
  157. }
  158. /*
  159. * Module init and exit code
  160. */
  161. static int elanfreq_cpu_init(struct cpufreq_policy *policy)
  162. {
  163. struct cpuinfo_x86 *c = &cpu_data(0);
  164. unsigned int i;
  165. int result;
  166. /* capability check */
  167. if ((c->x86_vendor != X86_VENDOR_AMD) ||
  168. (c->x86 != 4) || (c->x86_model != 10))
  169. return -ENODEV;
  170. /* max freq */
  171. if (!max_freq)
  172. max_freq = elanfreq_get_cpu_frequency(0);
  173. /* table init */
  174. for (i = 0; (elanfreq_table[i].frequency != CPUFREQ_TABLE_END); i++) {
  175. if (elanfreq_table[i].frequency > max_freq)
  176. elanfreq_table[i].frequency = CPUFREQ_ENTRY_INVALID;
  177. }
  178. /* cpuinfo and default policy values */
  179. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  180. policy->cur = elanfreq_get_cpu_frequency(0);
  181. result = cpufreq_frequency_table_cpuinfo(policy, elanfreq_table);
  182. if (result)
  183. return result;
  184. cpufreq_frequency_table_get_attr(elanfreq_table, policy->cpu);
  185. return 0;
  186. }
  187. static int elanfreq_cpu_exit(struct cpufreq_policy *policy)
  188. {
  189. cpufreq_frequency_table_put_attr(policy->cpu);
  190. return 0;
  191. }
  192. #ifndef MODULE
  193. /**
  194. * elanfreq_setup - elanfreq command line parameter parsing
  195. *
  196. * elanfreq command line parameter. Use:
  197. * elanfreq=66000
  198. * to set the maximum CPU frequency to 66 MHz. Note that in
  199. * case you do not give this boot parameter, the maximum
  200. * frequency will fall back to _current_ CPU frequency which
  201. * might be lower. If you build this as a module, use the
  202. * max_freq module parameter instead.
  203. */
  204. static int __init elanfreq_setup(char *str)
  205. {
  206. max_freq = simple_strtoul(str, &str, 0);
  207. printk(KERN_WARNING "You're using the deprecated elanfreq command line option. Use elanfreq.max_freq instead, please!\n");
  208. return 1;
  209. }
  210. __setup("elanfreq=", elanfreq_setup);
  211. #endif
  212. static struct freq_attr *elanfreq_attr[] = {
  213. &cpufreq_freq_attr_scaling_available_freqs,
  214. NULL,
  215. };
  216. static struct cpufreq_driver elanfreq_driver = {
  217. .get = elanfreq_get_cpu_frequency,
  218. .verify = elanfreq_verify,
  219. .target = elanfreq_target,
  220. .init = elanfreq_cpu_init,
  221. .exit = elanfreq_cpu_exit,
  222. .name = "elanfreq",
  223. .owner = THIS_MODULE,
  224. .attr = elanfreq_attr,
  225. };
  226. static const struct x86_cpu_id elan_id[] = {
  227. { X86_VENDOR_AMD, 4, 10, },
  228. {}
  229. };
  230. MODULE_DEVICE_TABLE(x86cpu, elan_id);
  231. static int __init elanfreq_init(void)
  232. {
  233. if (!x86_match_cpu(elan_id))
  234. return -ENODEV;
  235. return cpufreq_register_driver(&elanfreq_driver);
  236. }
  237. static void __exit elanfreq_exit(void)
  238. {
  239. cpufreq_unregister_driver(&elanfreq_driver);
  240. }
  241. module_param(max_freq, int, 0444);
  242. MODULE_LICENSE("GPL");
  243. MODULE_AUTHOR("Robert Schwebel <r.schwebel@pengutronix.de>, "
  244. "Sven Geggus <sven@geggus.net>");
  245. MODULE_DESCRIPTION("cpufreq driver for AMD's Elan CPUs");
  246. module_init(elanfreq_init);
  247. module_exit(elanfreq_exit);