time.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * linux/arch/arm/mach-omap1/time.c
  3. *
  4. * OMAP Timers
  5. *
  6. * Copyright (C) 2004 Nokia Corporation
  7. * Partial timer rewrite and additional dynamic tick timer support by
  8. * Tony Lindgen <tony@atomide.com> and
  9. * Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
  10. *
  11. * MPU timer code based on the older MPU timer code for OMAP
  12. * Copyright (C) 2000 RidgeRun, Inc.
  13. * Author: Greg Lonnon <glonnon@ridgerun.com>
  14. *
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License as published by the
  17. * Free Software Foundation; either version 2 of the License, or (at your
  18. * option) any later version.
  19. *
  20. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  21. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  23. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  26. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  27. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  29. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. * You should have received a copy of the GNU General Public License along
  32. * with this program; if not, write to the Free Software Foundation, Inc.,
  33. * 675 Mass Ave, Cambridge, MA 02139, USA.
  34. */
  35. #include <linux/config.h>
  36. #include <linux/kernel.h>
  37. #include <linux/init.h>
  38. #include <linux/delay.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/sched.h>
  41. #include <linux/spinlock.h>
  42. #include <asm/system.h>
  43. #include <asm/hardware.h>
  44. #include <asm/io.h>
  45. #include <asm/leds.h>
  46. #include <asm/irq.h>
  47. #include <asm/mach/irq.h>
  48. #include <asm/mach/time.h>
  49. struct sys_timer omap_timer;
  50. /*
  51. * ---------------------------------------------------------------------------
  52. * MPU timer
  53. * ---------------------------------------------------------------------------
  54. */
  55. #define OMAP_MPU_TIMER_BASE OMAP_MPU_TIMER1_BASE
  56. #define OMAP_MPU_TIMER_OFFSET 0x100
  57. /* cycles to nsec conversions taken from arch/i386/kernel/timers/timer_tsc.c,
  58. * converted to use kHz by Kevin Hilman */
  59. /* convert from cycles(64bits) => nanoseconds (64bits)
  60. * basic equation:
  61. * ns = cycles / (freq / ns_per_sec)
  62. * ns = cycles * (ns_per_sec / freq)
  63. * ns = cycles * (10^9 / (cpu_khz * 10^3))
  64. * ns = cycles * (10^6 / cpu_khz)
  65. *
  66. * Then we use scaling math (suggested by george at mvista.com) to get:
  67. * ns = cycles * (10^6 * SC / cpu_khz / SC
  68. * ns = cycles * cyc2ns_scale / SC
  69. *
  70. * And since SC is a constant power of two, we can convert the div
  71. * into a shift.
  72. * -johnstul at us.ibm.com "math is hard, lets go shopping!"
  73. */
  74. static unsigned long cyc2ns_scale;
  75. #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
  76. static inline void set_cyc2ns_scale(unsigned long cpu_khz)
  77. {
  78. cyc2ns_scale = (1000000 << CYC2NS_SCALE_FACTOR)/cpu_khz;
  79. }
  80. static inline unsigned long long cycles_2_ns(unsigned long long cyc)
  81. {
  82. return (cyc * cyc2ns_scale) >> CYC2NS_SCALE_FACTOR;
  83. }
  84. /*
  85. * MPU_TICKS_PER_SEC must be an even number, otherwise machinecycles_to_usecs
  86. * will break. On P2, the timer count rate is 6.5 MHz after programming PTV
  87. * with 0. This divides the 13MHz input by 2, and is undocumented.
  88. */
  89. #ifdef CONFIG_MACH_OMAP_PERSEUS2
  90. /* REVISIT: This ifdef construct should be replaced by a query to clock
  91. * framework to see if timer base frequency is 12.0, 13.0 or 19.2 MHz.
  92. */
  93. #define MPU_TICKS_PER_SEC (13000000 / 2)
  94. #else
  95. #define MPU_TICKS_PER_SEC (12000000 / 2)
  96. #endif
  97. #define MPU_TIMER_TICK_PERIOD ((MPU_TICKS_PER_SEC / HZ) - 1)
  98. typedef struct {
  99. u32 cntl; /* CNTL_TIMER, R/W */
  100. u32 load_tim; /* LOAD_TIM, W */
  101. u32 read_tim; /* READ_TIM, R */
  102. } omap_mpu_timer_regs_t;
  103. #define omap_mpu_timer_base(n) \
  104. ((volatile omap_mpu_timer_regs_t*)IO_ADDRESS(OMAP_MPU_TIMER_BASE + \
  105. (n)*OMAP_MPU_TIMER_OFFSET))
  106. static inline unsigned long omap_mpu_timer_read(int nr)
  107. {
  108. volatile omap_mpu_timer_regs_t* timer = omap_mpu_timer_base(nr);
  109. return timer->read_tim;
  110. }
  111. static inline void omap_mpu_timer_start(int nr, unsigned long load_val)
  112. {
  113. volatile omap_mpu_timer_regs_t* timer = omap_mpu_timer_base(nr);
  114. timer->cntl = MPU_TIMER_CLOCK_ENABLE;
  115. udelay(1);
  116. timer->load_tim = load_val;
  117. udelay(1);
  118. timer->cntl = (MPU_TIMER_CLOCK_ENABLE | MPU_TIMER_AR | MPU_TIMER_ST);
  119. }
  120. unsigned long omap_mpu_timer_ticks_to_usecs(unsigned long nr_ticks)
  121. {
  122. unsigned long long nsec;
  123. nsec = cycles_2_ns((unsigned long long)nr_ticks);
  124. return (unsigned long)nsec / 1000;
  125. }
  126. /*
  127. * Last processed system timer interrupt
  128. */
  129. static unsigned long omap_mpu_timer_last = 0;
  130. /*
  131. * Returns elapsed usecs since last system timer interrupt
  132. */
  133. static unsigned long omap_mpu_timer_gettimeoffset(void)
  134. {
  135. unsigned long now = 0 - omap_mpu_timer_read(0);
  136. unsigned long elapsed = now - omap_mpu_timer_last;
  137. return omap_mpu_timer_ticks_to_usecs(elapsed);
  138. }
  139. /*
  140. * Elapsed time between interrupts is calculated using timer0.
  141. * Latency during the interrupt is calculated using timer1.
  142. * Both timer0 and timer1 are counting at 6MHz (P2 6.5MHz).
  143. */
  144. static irqreturn_t omap_mpu_timer_interrupt(int irq, void *dev_id,
  145. struct pt_regs *regs)
  146. {
  147. unsigned long now, latency;
  148. write_seqlock(&xtime_lock);
  149. now = 0 - omap_mpu_timer_read(0);
  150. latency = MPU_TICKS_PER_SEC / HZ - omap_mpu_timer_read(1);
  151. omap_mpu_timer_last = now - latency;
  152. timer_tick(regs);
  153. write_sequnlock(&xtime_lock);
  154. return IRQ_HANDLED;
  155. }
  156. static struct irqaction omap_mpu_timer_irq = {
  157. .name = "mpu timer",
  158. .flags = SA_INTERRUPT | SA_TIMER,
  159. .handler = omap_mpu_timer_interrupt,
  160. };
  161. static unsigned long omap_mpu_timer1_overflows;
  162. static irqreturn_t omap_mpu_timer1_interrupt(int irq, void *dev_id,
  163. struct pt_regs *regs)
  164. {
  165. omap_mpu_timer1_overflows++;
  166. return IRQ_HANDLED;
  167. }
  168. static struct irqaction omap_mpu_timer1_irq = {
  169. .name = "mpu timer1 overflow",
  170. .flags = SA_INTERRUPT,
  171. .handler = omap_mpu_timer1_interrupt,
  172. };
  173. static __init void omap_init_mpu_timer(void)
  174. {
  175. set_cyc2ns_scale(MPU_TICKS_PER_SEC / 1000);
  176. omap_timer.offset = omap_mpu_timer_gettimeoffset;
  177. setup_irq(INT_TIMER1, &omap_mpu_timer1_irq);
  178. setup_irq(INT_TIMER2, &omap_mpu_timer_irq);
  179. omap_mpu_timer_start(0, 0xffffffff);
  180. omap_mpu_timer_start(1, MPU_TIMER_TICK_PERIOD);
  181. }
  182. /*
  183. * Scheduler clock - returns current time in nanosec units.
  184. */
  185. unsigned long long sched_clock(void)
  186. {
  187. unsigned long ticks = 0 - omap_mpu_timer_read(0);
  188. unsigned long long ticks64;
  189. ticks64 = omap_mpu_timer1_overflows;
  190. ticks64 <<= 32;
  191. ticks64 |= ticks;
  192. return cycles_2_ns(ticks64);
  193. }
  194. /*
  195. * ---------------------------------------------------------------------------
  196. * Timer initialization
  197. * ---------------------------------------------------------------------------
  198. */
  199. static void __init omap_timer_init(void)
  200. {
  201. omap_init_mpu_timer();
  202. }
  203. struct sys_timer omap_timer = {
  204. .init = omap_timer_init,
  205. .offset = NULL, /* Initialized later */
  206. };