elanfreq.c 5.7 KB

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