elanfreq.c 5.5 KB

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