common.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Common functions used across the timers go here
  3. */
  4. #include <linux/init.h>
  5. #include <linux/timex.h>
  6. #include <linux/errno.h>
  7. #include <linux/jiffies.h>
  8. #include <linux/module.h>
  9. #include <asm/io.h>
  10. #include <asm/timer.h>
  11. #include <asm/hpet.h>
  12. #include "mach_timer.h"
  13. /* ------ Calibrate the TSC -------
  14. * Return 2^32 * (1 / (TSC clocks per usec)) for do_fast_gettimeoffset().
  15. * Too much 64-bit arithmetic here to do this cleanly in C, and for
  16. * accuracy's sake we want to keep the overhead on the CTC speaker (channel 2)
  17. * output busy loop as low as possible. We avoid reading the CTC registers
  18. * directly because of the awkward 8-bit access mechanism of the 82C54
  19. * device.
  20. */
  21. #define CALIBRATE_TIME (5 * 1000020/HZ)
  22. unsigned long calibrate_tsc(void)
  23. {
  24. mach_prepare_counter();
  25. {
  26. unsigned long startlow, starthigh;
  27. unsigned long endlow, endhigh;
  28. unsigned long count;
  29. rdtsc(startlow,starthigh);
  30. mach_countup(&count);
  31. rdtsc(endlow,endhigh);
  32. /* Error: ECTCNEVERSET */
  33. if (count <= 1)
  34. goto bad_ctc;
  35. /* 64-bit subtract - gcc just messes up with long longs */
  36. __asm__("subl %2,%0\n\t"
  37. "sbbl %3,%1"
  38. :"=a" (endlow), "=d" (endhigh)
  39. :"g" (startlow), "g" (starthigh),
  40. "0" (endlow), "1" (endhigh));
  41. /* Error: ECPUTOOFAST */
  42. if (endhigh)
  43. goto bad_ctc;
  44. /* Error: ECPUTOOSLOW */
  45. if (endlow <= CALIBRATE_TIME)
  46. goto bad_ctc;
  47. __asm__("divl %2"
  48. :"=a" (endlow), "=d" (endhigh)
  49. :"r" (endlow), "0" (0), "1" (CALIBRATE_TIME));
  50. return endlow;
  51. }
  52. /*
  53. * The CTC wasn't reliable: we got a hit on the very first read,
  54. * or the CPU was so fast/slow that the quotient wouldn't fit in
  55. * 32 bits..
  56. */
  57. bad_ctc:
  58. return 0;
  59. }
  60. #ifdef CONFIG_HPET_TIMER
  61. /* ------ Calibrate the TSC using HPET -------
  62. * Return 2^32 * (1 / (TSC clocks per usec)) for getting the CPU freq.
  63. * Second output is parameter 1 (when non NULL)
  64. * Set 2^32 * (1 / (tsc per HPET clk)) for delay_hpet().
  65. * calibrate_tsc() calibrates the processor TSC by comparing
  66. * it to the HPET timer of known frequency.
  67. * Too much 64-bit arithmetic here to do this cleanly in C
  68. */
  69. #define CALIBRATE_CNT_HPET (5 * hpet_tick)
  70. #define CALIBRATE_TIME_HPET (5 * KERNEL_TICK_USEC)
  71. unsigned long __devinit calibrate_tsc_hpet(unsigned long *tsc_hpet_quotient_ptr)
  72. {
  73. unsigned long tsc_startlow, tsc_starthigh;
  74. unsigned long tsc_endlow, tsc_endhigh;
  75. unsigned long hpet_start, hpet_end;
  76. unsigned long result, remain;
  77. hpet_start = hpet_readl(HPET_COUNTER);
  78. rdtsc(tsc_startlow, tsc_starthigh);
  79. do {
  80. hpet_end = hpet_readl(HPET_COUNTER);
  81. } while ((hpet_end - hpet_start) < CALIBRATE_CNT_HPET);
  82. rdtsc(tsc_endlow, tsc_endhigh);
  83. /* 64-bit subtract - gcc just messes up with long longs */
  84. __asm__("subl %2,%0\n\t"
  85. "sbbl %3,%1"
  86. :"=a" (tsc_endlow), "=d" (tsc_endhigh)
  87. :"g" (tsc_startlow), "g" (tsc_starthigh),
  88. "0" (tsc_endlow), "1" (tsc_endhigh));
  89. /* Error: ECPUTOOFAST */
  90. if (tsc_endhigh)
  91. goto bad_calibration;
  92. /* Error: ECPUTOOSLOW */
  93. if (tsc_endlow <= CALIBRATE_TIME_HPET)
  94. goto bad_calibration;
  95. ASM_DIV64_REG(result, remain, tsc_endlow, 0, CALIBRATE_TIME_HPET);
  96. if (remain > (tsc_endlow >> 1))
  97. result++; /* rounding the result */
  98. if (tsc_hpet_quotient_ptr) {
  99. unsigned long tsc_hpet_quotient;
  100. ASM_DIV64_REG(tsc_hpet_quotient, remain, tsc_endlow, 0,
  101. CALIBRATE_CNT_HPET);
  102. if (remain > (tsc_endlow >> 1))
  103. tsc_hpet_quotient++; /* rounding the result */
  104. *tsc_hpet_quotient_ptr = tsc_hpet_quotient;
  105. }
  106. return result;
  107. bad_calibration:
  108. /*
  109. * the CPU was so fast/slow that the quotient wouldn't fit in
  110. * 32 bits..
  111. */
  112. return 0;
  113. }
  114. #endif
  115. unsigned long read_timer_tsc(void)
  116. {
  117. unsigned long retval;
  118. rdtscl(retval);
  119. return retval;
  120. }
  121. /* calculate cpu_khz */
  122. void init_cpu_khz(void)
  123. {
  124. if (cpu_has_tsc) {
  125. unsigned long tsc_quotient = calibrate_tsc();
  126. if (tsc_quotient) {
  127. /* report CPU clock rate in Hz.
  128. * The formula is (10^6 * 2^32) / (2^32 * 1 / (clocks/us)) =
  129. * clock/second. Our precision is about 100 ppm.
  130. */
  131. { unsigned long eax=0, edx=1000;
  132. __asm__("divl %2"
  133. :"=a" (cpu_khz), "=d" (edx)
  134. :"r" (tsc_quotient),
  135. "0" (eax), "1" (edx));
  136. printk("Detected %u.%03u MHz processor.\n",
  137. cpu_khz / 1000, cpu_khz % 1000);
  138. }
  139. }
  140. }
  141. }