sc520_freq.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/delay.h>
  20. #include <linux/cpufreq.h>
  21. #include <linux/timex.h>
  22. #include <linux/io.h>
  23. #include <asm/cpu_device_id.h>
  24. #include <asm/msr.h>
  25. #define MMCR_BASE 0xfffef000 /* The default base address */
  26. #define OFFS_CPUCTL 0x2 /* CPU Control Register */
  27. static __u8 __iomem *cpuctl;
  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. pr_err("error: cpuctl register has unexpected value %02x\n",
  39. 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. policy->freq_table = sc520_freq_table;
  68. return 0;
  69. }
  70. static struct cpufreq_driver sc520_freq_driver = {
  71. .get = sc520_freq_get_cpu_frequency,
  72. .verify = cpufreq_generic_frequency_table_verify,
  73. .target_index = sc520_freq_target,
  74. .init = sc520_freq_cpu_init,
  75. .name = "sc520_freq",
  76. .attr = cpufreq_generic_attr,
  77. };
  78. static const struct x86_cpu_id sc520_ids[] = {
  79. { X86_VENDOR_AMD, 4, 9 },
  80. {}
  81. };
  82. MODULE_DEVICE_TABLE(x86cpu, sc520_ids);
  83. static int __init sc520_freq_init(void)
  84. {
  85. int err;
  86. if (!x86_match_cpu(sc520_ids))
  87. return -ENODEV;
  88. cpuctl = ioremap((unsigned long)(MMCR_BASE + OFFS_CPUCTL), 1);
  89. if (!cpuctl) {
  90. pr_err("sc520_freq: error: failed to remap memory\n");
  91. return -ENOMEM;
  92. }
  93. err = cpufreq_register_driver(&sc520_freq_driver);
  94. if (err)
  95. iounmap(cpuctl);
  96. return err;
  97. }
  98. static void __exit sc520_freq_exit(void)
  99. {
  100. cpufreq_unregister_driver(&sc520_freq_driver);
  101. iounmap(cpuctl);
  102. }
  103. MODULE_LICENSE("GPL");
  104. MODULE_AUTHOR("Sean Young <sean@mess.org>");
  105. MODULE_DESCRIPTION("cpufreq driver for AMD's Elan sc520 CPU");
  106. module_init(sc520_freq_init);
  107. module_exit(sc520_freq_exit);