time.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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/kernel.h>
  36. #include <linux/init.h>
  37. #include <linux/delay.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/sched.h>
  40. #include <linux/spinlock.h>
  41. #include <asm/system.h>
  42. #include <asm/hardware.h>
  43. #include <asm/io.h>
  44. #include <asm/leds.h>
  45. #include <asm/irq.h>
  46. #include <asm/mach/irq.h>
  47. #include <asm/mach/time.h>
  48. struct sys_timer omap_timer;
  49. /*
  50. * ---------------------------------------------------------------------------
  51. * MPU timer
  52. * ---------------------------------------------------------------------------
  53. */
  54. #define OMAP_MPU_TIMER_BASE OMAP_MPU_TIMER1_BASE
  55. #define OMAP_MPU_TIMER_OFFSET 0x100
  56. /* cycles to nsec conversions taken from arch/i386/kernel/timers/timer_tsc.c,
  57. * converted to use kHz by Kevin Hilman */
  58. /* convert from cycles(64bits) => nanoseconds (64bits)
  59. * basic equation:
  60. * ns = cycles / (freq / ns_per_sec)
  61. * ns = cycles * (ns_per_sec / freq)
  62. * ns = cycles * (10^9 / (cpu_khz * 10^3))
  63. * ns = cycles * (10^6 / cpu_khz)
  64. *
  65. * Then we use scaling math (suggested by george at mvista.com) to get:
  66. * ns = cycles * (10^6 * SC / cpu_khz / SC
  67. * ns = cycles * cyc2ns_scale / SC
  68. *
  69. * And since SC is a constant power of two, we can convert the div
  70. * into a shift.
  71. * -johnstul at us.ibm.com "math is hard, lets go shopping!"
  72. */
  73. static unsigned long cyc2ns_scale;
  74. #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
  75. static inline void set_cyc2ns_scale(unsigned long cpu_khz)
  76. {
  77. cyc2ns_scale = (1000000 << CYC2NS_SCALE_FACTOR)/cpu_khz;
  78. }
  79. static inline unsigned long long cycles_2_ns(unsigned long long cyc)
  80. {
  81. return (cyc * cyc2ns_scale) >> CYC2NS_SCALE_FACTOR;
  82. }
  83. /*
  84. * MPU_TICKS_PER_SEC must be an even number, otherwise machinecycles_to_usecs
  85. * will break. On P2, the timer count rate is 6.5 MHz after programming PTV
  86. * with 0. This divides the 13MHz input by 2, and is undocumented.
  87. */
  88. #if defined(CONFIG_MACH_OMAP_PERSEUS2) || defined(CONFIG_MACH_OMAP_FSAMPLE)
  89. /* REVISIT: This ifdef construct should be replaced by a query to clock
  90. * framework to see if timer base frequency is 12.0, 13.0 or 19.2 MHz.
  91. */
  92. #define MPU_TICKS_PER_SEC (13000000 / 2)
  93. #else
  94. #define MPU_TICKS_PER_SEC (12000000 / 2)
  95. #endif
  96. #define MPU_TIMER_TICK_PERIOD ((MPU_TICKS_PER_SEC / HZ) - 1)
  97. typedef struct {
  98. u32 cntl; /* CNTL_TIMER, R/W */
  99. u32 load_tim; /* LOAD_TIM, W */
  100. u32 read_tim; /* READ_TIM, R */
  101. } omap_mpu_timer_regs_t;
  102. #define omap_mpu_timer_base(n) \
  103. ((volatile omap_mpu_timer_regs_t*)IO_ADDRESS(OMAP_MPU_TIMER_BASE + \
  104. (n)*OMAP_MPU_TIMER_OFFSET))
  105. static inline unsigned long omap_mpu_timer_read(int nr)
  106. {
  107. volatile omap_mpu_timer_regs_t* timer = omap_mpu_timer_base(nr);
  108. return timer->read_tim;
  109. }
  110. static inline void omap_mpu_timer_start(int nr, unsigned long load_val)
  111. {
  112. volatile omap_mpu_timer_regs_t* timer = omap_mpu_timer_base(nr);
  113. timer->cntl = MPU_TIMER_CLOCK_ENABLE;
  114. udelay(1);
  115. timer->load_tim = load_val;
  116. udelay(1);
  117. timer->cntl = (MPU_TIMER_CLOCK_ENABLE | MPU_TIMER_AR | MPU_TIMER_ST);
  118. }
  119. unsigned long omap_mpu_timer_ticks_to_usecs(unsigned long nr_ticks)
  120. {
  121. unsigned long long nsec;
  122. nsec = cycles_2_ns((unsigned long long)nr_ticks);
  123. return (unsigned long)nsec / 1000;
  124. }
  125. /*
  126. * Last processed system timer interrupt
  127. */
  128. static unsigned long omap_mpu_timer_last = 0;
  129. /*
  130. * Returns elapsed usecs since last system timer interrupt
  131. */
  132. static unsigned long omap_mpu_timer_gettimeoffset(void)
  133. {
  134. unsigned long now = 0 - omap_mpu_timer_read(0);
  135. unsigned long elapsed = now - omap_mpu_timer_last;
  136. return omap_mpu_timer_ticks_to_usecs(elapsed);
  137. }
  138. /*
  139. * Elapsed time between interrupts is calculated using timer0.
  140. * Latency during the interrupt is calculated using timer1.
  141. * Both timer0 and timer1 are counting at 6MHz (P2 6.5MHz).
  142. */
  143. static irqreturn_t omap_mpu_timer_interrupt(int irq, void *dev_id,
  144. struct pt_regs *regs)
  145. {
  146. unsigned long now, latency;
  147. write_seqlock(&xtime_lock);
  148. now = 0 - omap_mpu_timer_read(0);
  149. latency = MPU_TICKS_PER_SEC / HZ - omap_mpu_timer_read(1);
  150. omap_mpu_timer_last = now - latency;
  151. timer_tick(regs);
  152. write_sequnlock(&xtime_lock);
  153. return IRQ_HANDLED;
  154. }
  155. static struct irqaction omap_mpu_timer_irq = {
  156. .name = "mpu timer",
  157. .flags = IRQF_DISABLED | IRQF_TIMER,
  158. .handler = omap_mpu_timer_interrupt,
  159. };
  160. static unsigned long omap_mpu_timer1_overflows;
  161. static irqreturn_t omap_mpu_timer1_interrupt(int irq, void *dev_id,
  162. struct pt_regs *regs)
  163. {
  164. omap_mpu_timer1_overflows++;
  165. return IRQ_HANDLED;
  166. }
  167. static struct irqaction omap_mpu_timer1_irq = {
  168. .name = "mpu timer1 overflow",
  169. .flags = IRQF_DISABLED,
  170. .handler = omap_mpu_timer1_interrupt,
  171. };
  172. static __init void omap_init_mpu_timer(void)
  173. {
  174. set_cyc2ns_scale(MPU_TICKS_PER_SEC / 1000);
  175. omap_timer.offset = omap_mpu_timer_gettimeoffset;
  176. setup_irq(INT_TIMER1, &omap_mpu_timer1_irq);
  177. setup_irq(INT_TIMER2, &omap_mpu_timer_irq);
  178. omap_mpu_timer_start(0, 0xffffffff);
  179. omap_mpu_timer_start(1, MPU_TIMER_TICK_PERIOD);
  180. }
  181. /*
  182. * Scheduler clock - returns current time in nanosec units.
  183. */
  184. unsigned long long sched_clock(void)
  185. {
  186. unsigned long ticks = 0 - omap_mpu_timer_read(0);
  187. unsigned long long ticks64;
  188. ticks64 = omap_mpu_timer1_overflows;
  189. ticks64 <<= 32;
  190. ticks64 |= ticks;
  191. return cycles_2_ns(ticks64);
  192. }
  193. /*
  194. * ---------------------------------------------------------------------------
  195. * Timer initialization
  196. * ---------------------------------------------------------------------------
  197. */
  198. static void __init omap_timer_init(void)
  199. {
  200. omap_init_mpu_timer();
  201. }
  202. struct sys_timer omap_timer = {
  203. .init = omap_timer_init,
  204. .offset = NULL, /* Initialized later */
  205. };