elanfreq.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/cpufreq.h>
  24. #include <asm/cpu_device_id.h>
  25. #include <asm/msr.h>
  26. #include <linux/timex.h>
  27. #include <linux/io.h>
  28. #define REG_CSCIR 0x22 /* Chip Setup and Control Index Register */
  29. #define REG_CSCDR 0x23 /* Chip Setup and Control Data Register */
  30. /* Module parameter */
  31. static int max_freq;
  32. struct s_elan_multiplier {
  33. int clock; /* frequency in kHz */
  34. int val40h; /* PMU Force Mode register */
  35. int val80h; /* CPU Clock Speed Register */
  36. };
  37. /*
  38. * It is important that the frequencies
  39. * are listed in ascending order here!
  40. */
  41. static struct s_elan_multiplier elan_multiplier[] = {
  42. {1000, 0x02, 0x18},
  43. {2000, 0x02, 0x10},
  44. {4000, 0x02, 0x08},
  45. {8000, 0x00, 0x00},
  46. {16000, 0x00, 0x02},
  47. {33000, 0x00, 0x04},
  48. {66000, 0x01, 0x04},
  49. {99000, 0x01, 0x05}
  50. };
  51. static struct cpufreq_frequency_table elanfreq_table[] = {
  52. {0, 0, 1000},
  53. {0, 1, 2000},
  54. {0, 2, 4000},
  55. {0, 3, 8000},
  56. {0, 4, 16000},
  57. {0, 5, 33000},
  58. {0, 6, 66000},
  59. {0, 7, 99000},
  60. {0, 0, CPUFREQ_TABLE_END},
  61. };
  62. /**
  63. * elanfreq_get_cpu_frequency: determine current cpu speed
  64. *
  65. * Finds out at which frequency the CPU of the Elan SOC runs
  66. * at the moment. Frequencies from 1 to 33 MHz are generated
  67. * the normal way, 66 and 99 MHz are called "Hyperspeed Mode"
  68. * and have the rest of the chip running with 33 MHz.
  69. */
  70. static unsigned int elanfreq_get_cpu_frequency(unsigned int cpu)
  71. {
  72. u8 clockspeed_reg; /* Clock Speed Register */
  73. local_irq_disable();
  74. outb_p(0x80, REG_CSCIR);
  75. clockspeed_reg = inb_p(REG_CSCDR);
  76. local_irq_enable();
  77. if ((clockspeed_reg & 0xE0) == 0xE0)
  78. return 0;
  79. /* Are we in CPU clock multiplied mode (66/99 MHz)? */
  80. if ((clockspeed_reg & 0xE0) == 0xC0) {
  81. if ((clockspeed_reg & 0x01) == 0)
  82. return 66000;
  83. else
  84. return 99000;
  85. }
  86. /* 33 MHz is not 32 MHz... */
  87. if ((clockspeed_reg & 0xE0) == 0xA0)
  88. return 33000;
  89. return (1<<((clockspeed_reg & 0xE0) >> 5)) * 1000;
  90. }
  91. static int elanfreq_target(struct cpufreq_policy *policy,
  92. unsigned int state)
  93. {
  94. /*
  95. * Access to the Elan's internal registers is indexed via
  96. * 0x22: Chip Setup & Control Register Index Register (CSCI)
  97. * 0x23: Chip Setup & Control Register Data Register (CSCD)
  98. *
  99. */
  100. /*
  101. * 0x40 is the Power Management Unit's Force Mode Register.
  102. * Bit 6 enables Hyperspeed Mode (66/100 MHz core frequency)
  103. */
  104. local_irq_disable();
  105. outb_p(0x40, REG_CSCIR); /* Disable hyperspeed mode */
  106. outb_p(0x00, REG_CSCDR);
  107. local_irq_enable(); /* wait till internal pipelines and */
  108. udelay(1000); /* buffers have cleaned up */
  109. local_irq_disable();
  110. /* now, set the CPU clock speed register (0x80) */
  111. outb_p(0x80, REG_CSCIR);
  112. outb_p(elan_multiplier[state].val80h, REG_CSCDR);
  113. /* now, the hyperspeed bit in PMU Force Mode Register (0x40) */
  114. outb_p(0x40, REG_CSCIR);
  115. outb_p(elan_multiplier[state].val40h, REG_CSCDR);
  116. udelay(10000);
  117. local_irq_enable();
  118. return 0;
  119. }
  120. /*
  121. * Module init and exit code
  122. */
  123. static int elanfreq_cpu_init(struct cpufreq_policy *policy)
  124. {
  125. struct cpuinfo_x86 *c = &cpu_data(0);
  126. struct cpufreq_frequency_table *pos;
  127. /* capability check */
  128. if ((c->x86_vendor != X86_VENDOR_AMD) ||
  129. (c->x86 != 4) || (c->x86_model != 10))
  130. return -ENODEV;
  131. /* max freq */
  132. if (!max_freq)
  133. max_freq = elanfreq_get_cpu_frequency(0);
  134. /* table init */
  135. cpufreq_for_each_entry(pos, elanfreq_table)
  136. if (pos->frequency > max_freq)
  137. pos->frequency = CPUFREQ_ENTRY_INVALID;
  138. /* cpuinfo and default policy values */
  139. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  140. return cpufreq_table_validate_and_show(policy, elanfreq_table);
  141. }
  142. #ifndef MODULE
  143. /**
  144. * elanfreq_setup - elanfreq command line parameter parsing
  145. *
  146. * elanfreq command line parameter. Use:
  147. * elanfreq=66000
  148. * to set the maximum CPU frequency to 66 MHz. Note that in
  149. * case you do not give this boot parameter, the maximum
  150. * frequency will fall back to _current_ CPU frequency which
  151. * might be lower. If you build this as a module, use the
  152. * max_freq module parameter instead.
  153. */
  154. static int __init elanfreq_setup(char *str)
  155. {
  156. max_freq = simple_strtoul(str, &str, 0);
  157. pr_warn("You're using the deprecated elanfreq command line option. Use elanfreq.max_freq instead, please!\n");
  158. return 1;
  159. }
  160. __setup("elanfreq=", elanfreq_setup);
  161. #endif
  162. static struct cpufreq_driver elanfreq_driver = {
  163. .get = elanfreq_get_cpu_frequency,
  164. .verify = cpufreq_generic_frequency_table_verify,
  165. .target_index = elanfreq_target,
  166. .init = elanfreq_cpu_init,
  167. .name = "elanfreq",
  168. .attr = cpufreq_generic_attr,
  169. };
  170. static const struct x86_cpu_id elan_id[] = {
  171. { X86_VENDOR_AMD, 4, 10, },
  172. {}
  173. };
  174. MODULE_DEVICE_TABLE(x86cpu, elan_id);
  175. static int __init elanfreq_init(void)
  176. {
  177. if (!x86_match_cpu(elan_id))
  178. return -ENODEV;
  179. return cpufreq_register_driver(&elanfreq_driver);
  180. }
  181. static void __exit elanfreq_exit(void)
  182. {
  183. cpufreq_unregister_driver(&elanfreq_driver);
  184. }
  185. module_param(max_freq, int, 0444);
  186. MODULE_LICENSE("GPL");
  187. MODULE_AUTHOR("Robert Schwebel <r.schwebel@pengutronix.de>, "
  188. "Sven Geggus <sven@geggus.net>");
  189. MODULE_DESCRIPTION("cpufreq driver for AMD's Elan CPUs");
  190. module_init(elanfreq_init);
  191. module_exit(elanfreq_exit);