sc520_freq.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * sc520_freq.c: cpufreq driver for the AMD Elan sc520
  3. *
  4. * Copyright (C) 2005 Sean Young <sean@mess.org>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * Based on elanfreq.c
  12. *
  13. * 2005-03-30: - initial revision
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/delay.h>
  19. #include <linux/cpufreq.h>
  20. #include <linux/timex.h>
  21. #include <linux/io.h>
  22. #include <asm/msr.h>
  23. #define MMCR_BASE 0xfffef000 /* The default base address */
  24. #define OFFS_CPUCTL 0x2 /* CPU Control Register */
  25. static __u8 __iomem *cpuctl;
  26. #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, \
  27. "sc520_freq", msg)
  28. #define PFX "sc520_freq: "
  29. static struct cpufreq_frequency_table sc520_freq_table[] = {
  30. {0x01, 100000},
  31. {0x02, 133000},
  32. {0, CPUFREQ_TABLE_END},
  33. };
  34. static unsigned int sc520_freq_get_cpu_frequency(unsigned int cpu)
  35. {
  36. u8 clockspeed_reg = *cpuctl;
  37. switch (clockspeed_reg & 0x03) {
  38. default:
  39. printk(KERN_ERR PFX "error: cpuctl register has unexpected "
  40. "value %02x\n", clockspeed_reg);
  41. case 0x01:
  42. return 100000;
  43. case 0x02:
  44. return 133000;
  45. }
  46. }
  47. static void sc520_freq_set_cpu_state(unsigned int state)
  48. {
  49. struct cpufreq_freqs freqs;
  50. u8 clockspeed_reg;
  51. freqs.old = sc520_freq_get_cpu_frequency(0);
  52. freqs.new = sc520_freq_table[state].frequency;
  53. freqs.cpu = 0; /* AMD Elan is UP */
  54. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  55. dprintk("attempting to set frequency to %i kHz\n",
  56. sc520_freq_table[state].frequency);
  57. local_irq_disable();
  58. clockspeed_reg = *cpuctl & ~0x03;
  59. *cpuctl = clockspeed_reg | sc520_freq_table[state].index;
  60. local_irq_enable();
  61. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  62. };
  63. static int sc520_freq_verify(struct cpufreq_policy *policy)
  64. {
  65. return cpufreq_frequency_table_verify(policy, &sc520_freq_table[0]);
  66. }
  67. static int sc520_freq_target(struct cpufreq_policy *policy,
  68. unsigned int target_freq,
  69. unsigned int relation)
  70. {
  71. unsigned int newstate = 0;
  72. if (cpufreq_frequency_table_target(policy, sc520_freq_table,
  73. target_freq, relation, &newstate))
  74. return -EINVAL;
  75. sc520_freq_set_cpu_state(newstate);
  76. return 0;
  77. }
  78. /*
  79. * Module init and exit code
  80. */
  81. static int sc520_freq_cpu_init(struct cpufreq_policy *policy)
  82. {
  83. struct cpuinfo_x86 *c = &cpu_data(0);
  84. int result;
  85. /* capability check */
  86. if (c->x86_vendor != X86_VENDOR_AMD ||
  87. c->x86 != 4 || c->x86_model != 9)
  88. return -ENODEV;
  89. /* cpuinfo and default policy values */
  90. policy->cpuinfo.transition_latency = 1000000; /* 1ms */
  91. policy->cur = sc520_freq_get_cpu_frequency(0);
  92. result = cpufreq_frequency_table_cpuinfo(policy, sc520_freq_table);
  93. if (result)
  94. return result;
  95. cpufreq_frequency_table_get_attr(sc520_freq_table, policy->cpu);
  96. return 0;
  97. }
  98. static int sc520_freq_cpu_exit(struct cpufreq_policy *policy)
  99. {
  100. cpufreq_frequency_table_put_attr(policy->cpu);
  101. return 0;
  102. }
  103. static struct freq_attr *sc520_freq_attr[] = {
  104. &cpufreq_freq_attr_scaling_available_freqs,
  105. NULL,
  106. };
  107. static struct cpufreq_driver sc520_freq_driver = {
  108. .get = sc520_freq_get_cpu_frequency,
  109. .verify = sc520_freq_verify,
  110. .target = sc520_freq_target,
  111. .init = sc520_freq_cpu_init,
  112. .exit = sc520_freq_cpu_exit,
  113. .name = "sc520_freq",
  114. .owner = THIS_MODULE,
  115. .attr = sc520_freq_attr,
  116. };
  117. static int __init sc520_freq_init(void)
  118. {
  119. struct cpuinfo_x86 *c = &cpu_data(0);
  120. int err;
  121. /* Test if we have the right hardware */
  122. if (c->x86_vendor != X86_VENDOR_AMD ||
  123. c->x86 != 4 || c->x86_model != 9) {
  124. dprintk("no Elan SC520 processor found!\n");
  125. return -ENODEV;
  126. }
  127. cpuctl = ioremap((unsigned long)(MMCR_BASE + OFFS_CPUCTL), 1);
  128. if (!cpuctl) {
  129. printk(KERN_ERR "sc520_freq: error: failed to remap memory\n");
  130. return -ENOMEM;
  131. }
  132. err = cpufreq_register_driver(&sc520_freq_driver);
  133. if (err)
  134. iounmap(cpuctl);
  135. return err;
  136. }
  137. static void __exit sc520_freq_exit(void)
  138. {
  139. cpufreq_unregister_driver(&sc520_freq_driver);
  140. iounmap(cpuctl);
  141. }
  142. MODULE_LICENSE("GPL");
  143. MODULE_AUTHOR("Sean Young <sean@mess.org>");
  144. MODULE_DESCRIPTION("cpufreq driver for AMD's Elan sc520 CPU");
  145. module_init(sc520_freq_init);
  146. module_exit(sc520_freq_exit);