elanfreq.c 5.5 KB

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