elanfreq.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. static int elanfreq_target(struct cpufreq_policy *policy,
  91. unsigned int state)
  92. {
  93. struct cpufreq_freqs freqs;
  94. freqs.old = elanfreq_get_cpu_frequency(0);
  95. freqs.new = elan_multiplier[state].clock;
  96. cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
  97. printk(KERN_INFO "elanfreq: attempting to set frequency to %i kHz\n",
  98. elan_multiplier[state].clock);
  99. /*
  100. * Access to the Elan's internal registers is indexed via
  101. * 0x22: Chip Setup & Control Register Index Register (CSCI)
  102. * 0x23: Chip Setup & Control Register Data Register (CSCD)
  103. *
  104. */
  105. /*
  106. * 0x40 is the Power Management Unit's Force Mode Register.
  107. * Bit 6 enables Hyperspeed Mode (66/100 MHz core frequency)
  108. */
  109. local_irq_disable();
  110. outb_p(0x40, REG_CSCIR); /* Disable hyperspeed mode */
  111. outb_p(0x00, REG_CSCDR);
  112. local_irq_enable(); /* wait till internal pipelines and */
  113. udelay(1000); /* buffers have cleaned up */
  114. local_irq_disable();
  115. /* now, set the CPU clock speed register (0x80) */
  116. outb_p(0x80, REG_CSCIR);
  117. outb_p(elan_multiplier[state].val80h, REG_CSCDR);
  118. /* now, the hyperspeed bit in PMU Force Mode Register (0x40) */
  119. outb_p(0x40, REG_CSCIR);
  120. outb_p(elan_multiplier[state].val40h, REG_CSCDR);
  121. udelay(10000);
  122. local_irq_enable();
  123. cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
  124. return 0;
  125. }
  126. /*
  127. * Module init and exit code
  128. */
  129. static int elanfreq_cpu_init(struct cpufreq_policy *policy)
  130. {
  131. struct cpuinfo_x86 *c = &cpu_data(0);
  132. unsigned int i;
  133. /* capability check */
  134. if ((c->x86_vendor != X86_VENDOR_AMD) ||
  135. (c->x86 != 4) || (c->x86_model != 10))
  136. return -ENODEV;
  137. /* max freq */
  138. if (!max_freq)
  139. max_freq = elanfreq_get_cpu_frequency(0);
  140. /* table init */
  141. for (i = 0; (elanfreq_table[i].frequency != CPUFREQ_TABLE_END); i++) {
  142. if (elanfreq_table[i].frequency > max_freq)
  143. elanfreq_table[i].frequency = CPUFREQ_ENTRY_INVALID;
  144. }
  145. /* cpuinfo and default policy values */
  146. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  147. return cpufreq_table_validate_and_show(policy, elanfreq_table);
  148. }
  149. #ifndef MODULE
  150. /**
  151. * elanfreq_setup - elanfreq command line parameter parsing
  152. *
  153. * elanfreq command line parameter. Use:
  154. * elanfreq=66000
  155. * to set the maximum CPU frequency to 66 MHz. Note that in
  156. * case you do not give this boot parameter, the maximum
  157. * frequency will fall back to _current_ CPU frequency which
  158. * might be lower. If you build this as a module, use the
  159. * max_freq module parameter instead.
  160. */
  161. static int __init elanfreq_setup(char *str)
  162. {
  163. max_freq = simple_strtoul(str, &str, 0);
  164. printk(KERN_WARNING "You're using the deprecated elanfreq command line option. Use elanfreq.max_freq instead, please!\n");
  165. return 1;
  166. }
  167. __setup("elanfreq=", elanfreq_setup);
  168. #endif
  169. static struct cpufreq_driver elanfreq_driver = {
  170. .get = elanfreq_get_cpu_frequency,
  171. .verify = cpufreq_generic_frequency_table_verify,
  172. .target_index = elanfreq_target,
  173. .init = elanfreq_cpu_init,
  174. .exit = cpufreq_generic_exit,
  175. .name = "elanfreq",
  176. .attr = cpufreq_generic_attr,
  177. };
  178. static const struct x86_cpu_id elan_id[] = {
  179. { X86_VENDOR_AMD, 4, 10, },
  180. {}
  181. };
  182. MODULE_DEVICE_TABLE(x86cpu, elan_id);
  183. static int __init elanfreq_init(void)
  184. {
  185. if (!x86_match_cpu(elan_id))
  186. return -ENODEV;
  187. return cpufreq_register_driver(&elanfreq_driver);
  188. }
  189. static void __exit elanfreq_exit(void)
  190. {
  191. cpufreq_unregister_driver(&elanfreq_driver);
  192. }
  193. module_param(max_freq, int, 0444);
  194. MODULE_LICENSE("GPL");
  195. MODULE_AUTHOR("Robert Schwebel <r.schwebel@pengutronix.de>, "
  196. "Sven Geggus <sven@geggus.net>");
  197. MODULE_DESCRIPTION("cpufreq driver for AMD's Elan CPUs");
  198. module_init(elanfreq_init);
  199. module_exit(elanfreq_exit);