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