sc520_freq.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/cpu_device_id.h>
  23. #include <asm/msr.h>
  24. #define MMCR_BASE 0xfffef000 /* The default base address */
  25. #define OFFS_CPUCTL 0x2 /* CPU Control Register */
  26. static __u8 __iomem *cpuctl;
  27. #define PFX "sc520_freq: "
  28. static struct cpufreq_frequency_table sc520_freq_table[] = {
  29. {0, 0x01, 100000},
  30. {0, 0x02, 133000},
  31. {0, 0, CPUFREQ_TABLE_END},
  32. };
  33. static unsigned int sc520_freq_get_cpu_frequency(unsigned int cpu)
  34. {
  35. u8 clockspeed_reg = *cpuctl;
  36. switch (clockspeed_reg & 0x03) {
  37. default:
  38. printk(KERN_ERR PFX "error: cpuctl register has unexpected "
  39. "value %02x\n", clockspeed_reg);
  40. case 0x01:
  41. return 100000;
  42. case 0x02:
  43. return 133000;
  44. }
  45. }
  46. static int sc520_freq_target(struct cpufreq_policy *policy, unsigned int state)
  47. {
  48. u8 clockspeed_reg;
  49. local_irq_disable();
  50. clockspeed_reg = *cpuctl & ~0x03;
  51. *cpuctl = clockspeed_reg | sc520_freq_table[state].driver_data;
  52. local_irq_enable();
  53. return 0;
  54. }
  55. /*
  56. * Module init and exit code
  57. */
  58. static int sc520_freq_cpu_init(struct cpufreq_policy *policy)
  59. {
  60. struct cpuinfo_x86 *c = &cpu_data(0);
  61. /* capability check */
  62. if (c->x86_vendor != X86_VENDOR_AMD ||
  63. c->x86 != 4 || c->x86_model != 9)
  64. return -ENODEV;
  65. /* cpuinfo and default policy values */
  66. policy->cpuinfo.transition_latency = 1000000; /* 1ms */
  67. return cpufreq_table_validate_and_show(policy, sc520_freq_table);
  68. }
  69. static struct cpufreq_driver sc520_freq_driver = {
  70. .get = sc520_freq_get_cpu_frequency,
  71. .verify = cpufreq_generic_frequency_table_verify,
  72. .target_index = sc520_freq_target,
  73. .init = sc520_freq_cpu_init,
  74. .name = "sc520_freq",
  75. .attr = cpufreq_generic_attr,
  76. };
  77. static const struct x86_cpu_id sc520_ids[] = {
  78. { X86_VENDOR_AMD, 4, 9 },
  79. {}
  80. };
  81. MODULE_DEVICE_TABLE(x86cpu, sc520_ids);
  82. static int __init sc520_freq_init(void)
  83. {
  84. int err;
  85. if (!x86_match_cpu(sc520_ids))
  86. return -ENODEV;
  87. cpuctl = ioremap((unsigned long)(MMCR_BASE + OFFS_CPUCTL), 1);
  88. if (!cpuctl) {
  89. printk(KERN_ERR "sc520_freq: error: failed to remap memory\n");
  90. return -ENOMEM;
  91. }
  92. err = cpufreq_register_driver(&sc520_freq_driver);
  93. if (err)
  94. iounmap(cpuctl);
  95. return err;
  96. }
  97. static void __exit sc520_freq_exit(void)
  98. {
  99. cpufreq_unregister_driver(&sc520_freq_driver);
  100. iounmap(cpuctl);
  101. }
  102. MODULE_LICENSE("GPL");
  103. MODULE_AUTHOR("Sean Young <sean@mess.org>");
  104. MODULE_DESCRIPTION("cpufreq driver for AMD's Elan sc520 CPU");
  105. module_init(sc520_freq_init);
  106. module_exit(sc520_freq_exit);