sc520_freq.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 <asm/msr.h>
  21. #include <asm/timex.h>
  22. #include <asm/io.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, "sc520_freq", msg)
  27. static struct cpufreq_frequency_table sc520_freq_table[] = {
  28. {0x01, 100000},
  29. {0x02, 133000},
  30. {0, CPUFREQ_TABLE_END},
  31. };
  32. static unsigned int sc520_freq_get_cpu_frequency(unsigned int cpu)
  33. {
  34. u8 clockspeed_reg = *cpuctl;
  35. switch (clockspeed_reg & 0x03) {
  36. default:
  37. printk(KERN_ERR "sc520_freq: error: cpuctl register has unexpected value %02x\n", clockspeed_reg);
  38. case 0x01:
  39. return 100000;
  40. case 0x02:
  41. return 133000;
  42. }
  43. }
  44. static void sc520_freq_set_cpu_state (unsigned int state)
  45. {
  46. struct cpufreq_freqs freqs;
  47. u8 clockspeed_reg;
  48. freqs.old = sc520_freq_get_cpu_frequency(0);
  49. freqs.new = sc520_freq_table[state].frequency;
  50. freqs.cpu = 0; /* AMD Elan is UP */
  51. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  52. dprintk("attempting to set frequency to %i kHz\n",
  53. sc520_freq_table[state].frequency);
  54. local_irq_disable();
  55. clockspeed_reg = *cpuctl & ~0x03;
  56. *cpuctl = clockspeed_reg | sc520_freq_table[state].index;
  57. local_irq_enable();
  58. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  59. };
  60. static int sc520_freq_verify (struct cpufreq_policy *policy)
  61. {
  62. return cpufreq_frequency_table_verify(policy, &sc520_freq_table[0]);
  63. }
  64. static int sc520_freq_target (struct cpufreq_policy *policy,
  65. unsigned int target_freq,
  66. unsigned int relation)
  67. {
  68. unsigned int newstate = 0;
  69. if (cpufreq_frequency_table_target(policy, sc520_freq_table, target_freq, relation, &newstate))
  70. return -EINVAL;
  71. sc520_freq_set_cpu_state(newstate);
  72. return 0;
  73. }
  74. /*
  75. * Module init and exit code
  76. */
  77. static int sc520_freq_cpu_init(struct cpufreq_policy *policy)
  78. {
  79. struct cpuinfo_x86 *c = cpu_data;
  80. int result;
  81. /* capability check */
  82. if (c->x86_vendor != X86_VENDOR_AMD ||
  83. c->x86 != 4 || c->x86_model != 9)
  84. return -ENODEV;
  85. /* cpuinfo and default policy values */
  86. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  87. policy->cpuinfo.transition_latency = 1000000; /* 1ms */
  88. policy->cur = sc520_freq_get_cpu_frequency(0);
  89. result = cpufreq_frequency_table_cpuinfo(policy, sc520_freq_table);
  90. if (result)
  91. return (result);
  92. cpufreq_frequency_table_get_attr(sc520_freq_table, policy->cpu);
  93. return 0;
  94. }
  95. static int sc520_freq_cpu_exit(struct cpufreq_policy *policy)
  96. {
  97. cpufreq_frequency_table_put_attr(policy->cpu);
  98. return 0;
  99. }
  100. static struct freq_attr* sc520_freq_attr[] = {
  101. &cpufreq_freq_attr_scaling_available_freqs,
  102. NULL,
  103. };
  104. static struct cpufreq_driver sc520_freq_driver = {
  105. .get = sc520_freq_get_cpu_frequency,
  106. .verify = sc520_freq_verify,
  107. .target = sc520_freq_target,
  108. .init = sc520_freq_cpu_init,
  109. .exit = sc520_freq_cpu_exit,
  110. .name = "sc520_freq",
  111. .owner = THIS_MODULE,
  112. .attr = sc520_freq_attr,
  113. };
  114. static int __init sc520_freq_init(void)
  115. {
  116. struct cpuinfo_x86 *c = cpu_data;
  117. /* Test if we have the right hardware */
  118. if(c->x86_vendor != X86_VENDOR_AMD ||
  119. c->x86 != 4 || c->x86_model != 9) {
  120. dprintk("no Elan SC520 processor found!\n");
  121. return -ENODEV;
  122. }
  123. cpuctl = ioremap((unsigned long)(MMCR_BASE + OFFS_CPUCTL), 1);
  124. if(!cpuctl) {
  125. printk(KERN_ERR "sc520_freq: error: failed to remap memory\n");
  126. return -ENOMEM;
  127. }
  128. return cpufreq_register_driver(&sc520_freq_driver);
  129. }
  130. static void __exit sc520_freq_exit(void)
  131. {
  132. cpufreq_unregister_driver(&sc520_freq_driver);
  133. iounmap(cpuctl);
  134. }
  135. MODULE_LICENSE("GPL");
  136. MODULE_AUTHOR("Sean Young <sean@mess.org>");
  137. MODULE_DESCRIPTION("cpufreq driver for AMD's Elan sc520 CPU");
  138. module_init(sc520_freq_init);
  139. module_exit(sc520_freq_exit);