acpi_pm.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * linux/drivers/clocksource/acpi_pm.c
  3. *
  4. * This file contains the ACPI PM based clocksource.
  5. *
  6. * This code was largely moved from the i386 timer_pm.c file
  7. * which was (C) Dominik Brodowski <linux@brodo.de> 2003
  8. * and contained the following comments:
  9. *
  10. * Driver to use the Power Management Timer (PMTMR) available in some
  11. * southbridges as primary timing source for the Linux kernel.
  12. *
  13. * Based on parts of linux/drivers/acpi/hardware/hwtimer.c, timer_pit.c,
  14. * timer_hpet.c, and on Arjan van de Ven's implementation for 2.4.
  15. *
  16. * This file is licensed under the GPL v2.
  17. */
  18. #include <linux/clocksource.h>
  19. #include <linux/errno.h>
  20. #include <linux/init.h>
  21. #include <linux/pci.h>
  22. #include <asm/io.h>
  23. /* Number of PMTMR ticks expected during calibration run */
  24. #define PMTMR_TICKS_PER_SEC 3579545
  25. /*
  26. * The I/O port the PMTMR resides at.
  27. * The location is detected during setup_arch(),
  28. * in arch/i386/acpi/boot.c
  29. */
  30. u32 pmtmr_ioport __read_mostly;
  31. #define ACPI_PM_MASK CLOCKSOURCE_MASK(24) /* limit it to 24 bits */
  32. static inline u32 read_pmtmr(void)
  33. {
  34. /* mask the output to 24 bits */
  35. return inl(pmtmr_ioport) & ACPI_PM_MASK;
  36. }
  37. static cycle_t acpi_pm_read_verified(void)
  38. {
  39. u32 v1 = 0, v2 = 0, v3 = 0;
  40. /*
  41. * It has been reported that because of various broken
  42. * chipsets (ICH4, PIIX4 and PIIX4E) where the ACPI PM clock
  43. * source is not latched, you must read it multiple
  44. * times to ensure a safe value is read:
  45. */
  46. do {
  47. v1 = read_pmtmr();
  48. v2 = read_pmtmr();
  49. v3 = read_pmtmr();
  50. } while (unlikely((v1 > v2 && v1 < v3) || (v2 > v3 && v2 < v1)
  51. || (v3 > v1 && v3 < v2)));
  52. return (cycle_t)v2;
  53. }
  54. static cycle_t acpi_pm_read(void)
  55. {
  56. return (cycle_t)read_pmtmr();
  57. }
  58. static struct clocksource clocksource_acpi_pm = {
  59. .name = "acpi_pm",
  60. .rating = 200,
  61. .read = acpi_pm_read,
  62. .mask = (cycle_t)ACPI_PM_MASK,
  63. .mult = 0, /*to be caluclated*/
  64. .shift = 22,
  65. .is_continuous = 1,
  66. };
  67. #ifdef CONFIG_PCI
  68. static int acpi_pm_good;
  69. static int __init acpi_pm_good_setup(char *__str)
  70. {
  71. acpi_pm_good = 1;
  72. return 1;
  73. }
  74. __setup("acpi_pm_good", acpi_pm_good_setup);
  75. static inline void acpi_pm_need_workaround(void)
  76. {
  77. clocksource_acpi_pm.read = acpi_pm_read_verified;
  78. clocksource_acpi_pm.rating = 110;
  79. }
  80. /*
  81. * PIIX4 Errata:
  82. *
  83. * The power management timer may return improper results when read.
  84. * Although the timer value settles properly after incrementing,
  85. * while incrementing there is a 3 ns window every 69.8 ns where the
  86. * timer value is indeterminate (a 4.2% chance that the data will be
  87. * incorrect when read). As a result, the ACPI free running count up
  88. * timer specification is violated due to erroneous reads.
  89. */
  90. static void __devinit acpi_pm_check_blacklist(struct pci_dev *dev)
  91. {
  92. u8 rev;
  93. if (acpi_pm_good)
  94. return;
  95. pci_read_config_byte(dev, PCI_REVISION_ID, &rev);
  96. /* the bug has been fixed in PIIX4M */
  97. if (rev < 3) {
  98. printk(KERN_WARNING "* Found PM-Timer Bug on the chipset."
  99. " Due to workarounds for a bug,\n"
  100. "* this clock source is slow. Consider trying"
  101. " other clock sources\n");
  102. acpi_pm_need_workaround();
  103. }
  104. }
  105. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3,
  106. acpi_pm_check_blacklist);
  107. static void __devinit acpi_pm_check_graylist(struct pci_dev *dev)
  108. {
  109. if (acpi_pm_good)
  110. return;
  111. printk(KERN_WARNING "* The chipset may have PM-Timer Bug. Due to"
  112. " workarounds for a bug,\n"
  113. "* this clock source is slow. If you are sure your timer"
  114. " does not have\n"
  115. "* this bug, please use \"acpi_pm_good\" to disable the"
  116. " workaround\n");
  117. acpi_pm_need_workaround();
  118. }
  119. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_0,
  120. acpi_pm_check_graylist);
  121. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_LE,
  122. acpi_pm_check_graylist);
  123. #endif
  124. static int __init init_acpi_pm_clocksource(void)
  125. {
  126. u32 value1, value2;
  127. unsigned int i;
  128. if (!pmtmr_ioport)
  129. return -ENODEV;
  130. clocksource_acpi_pm.mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC,
  131. clocksource_acpi_pm.shift);
  132. /* "verify" this timing source: */
  133. value1 = read_pmtmr();
  134. for (i = 0; i < 10000; i++) {
  135. value2 = read_pmtmr();
  136. if (value2 == value1)
  137. continue;
  138. if (value2 > value1)
  139. goto pm_good;
  140. if ((value2 < value1) && ((value2) < 0xFFF))
  141. goto pm_good;
  142. printk(KERN_INFO "PM-Timer had inconsistent results:"
  143. " 0x%#x, 0x%#x - aborting.\n", value1, value2);
  144. return -EINVAL;
  145. }
  146. printk(KERN_INFO "PM-Timer had no reasonable result:"
  147. " 0x%#x - aborting.\n", value1);
  148. return -ENODEV;
  149. pm_good:
  150. return clocksource_register(&clocksource_acpi_pm);
  151. }
  152. module_init(init_acpi_pm_clocksource);