time.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 <linux/clk.h>
  42. #include <linux/err.h>
  43. #include <linux/clocksource.h>
  44. #include <linux/clockchips.h>
  45. #include <asm/system.h>
  46. #include <asm/hardware.h>
  47. #include <asm/io.h>
  48. #include <asm/leds.h>
  49. #include <asm/irq.h>
  50. #include <asm/mach/irq.h>
  51. #include <asm/mach/time.h>
  52. #define OMAP_MPU_TIMER_BASE OMAP_MPU_TIMER1_BASE
  53. #define OMAP_MPU_TIMER_OFFSET 0x100
  54. /* cycles to nsec conversions taken from arch/i386/kernel/timers/timer_tsc.c,
  55. * converted to use kHz by Kevin Hilman */
  56. /* convert from cycles(64bits) => nanoseconds (64bits)
  57. * basic equation:
  58. * ns = cycles / (freq / ns_per_sec)
  59. * ns = cycles * (ns_per_sec / freq)
  60. * ns = cycles * (10^9 / (cpu_khz * 10^3))
  61. * ns = cycles * (10^6 / cpu_khz)
  62. *
  63. * Then we use scaling math (suggested by george at mvista.com) to get:
  64. * ns = cycles * (10^6 * SC / cpu_khz / SC
  65. * ns = cycles * cyc2ns_scale / SC
  66. *
  67. * And since SC is a constant power of two, we can convert the div
  68. * into a shift.
  69. * -johnstul at us.ibm.com "math is hard, lets go shopping!"
  70. */
  71. static unsigned long cyc2ns_scale;
  72. #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
  73. static inline void set_cyc2ns_scale(unsigned long cpu_khz)
  74. {
  75. cyc2ns_scale = (1000000 << CYC2NS_SCALE_FACTOR)/cpu_khz;
  76. }
  77. static inline unsigned long long cycles_2_ns(unsigned long long cyc)
  78. {
  79. return (cyc * cyc2ns_scale) >> CYC2NS_SCALE_FACTOR;
  80. }
  81. typedef struct {
  82. u32 cntl; /* CNTL_TIMER, R/W */
  83. u32 load_tim; /* LOAD_TIM, W */
  84. u32 read_tim; /* READ_TIM, R */
  85. } omap_mpu_timer_regs_t;
  86. #define omap_mpu_timer_base(n) \
  87. ((volatile omap_mpu_timer_regs_t*)IO_ADDRESS(OMAP_MPU_TIMER_BASE + \
  88. (n)*OMAP_MPU_TIMER_OFFSET))
  89. static inline unsigned long omap_mpu_timer_read(int nr)
  90. {
  91. volatile omap_mpu_timer_regs_t* timer = omap_mpu_timer_base(nr);
  92. return timer->read_tim;
  93. }
  94. static inline void omap_mpu_set_autoreset(int nr)
  95. {
  96. volatile omap_mpu_timer_regs_t* timer = omap_mpu_timer_base(nr);
  97. timer->cntl = timer->cntl | MPU_TIMER_AR;
  98. }
  99. static inline void omap_mpu_remove_autoreset(int nr)
  100. {
  101. volatile omap_mpu_timer_regs_t* timer = omap_mpu_timer_base(nr);
  102. timer->cntl = timer->cntl & ~MPU_TIMER_AR;
  103. }
  104. static inline void omap_mpu_timer_start(int nr, unsigned long load_val,
  105. int autoreset)
  106. {
  107. volatile omap_mpu_timer_regs_t* timer = omap_mpu_timer_base(nr);
  108. unsigned int timerflags = (MPU_TIMER_CLOCK_ENABLE | MPU_TIMER_ST);
  109. if (autoreset) timerflags |= MPU_TIMER_AR;
  110. timer->cntl = MPU_TIMER_CLOCK_ENABLE;
  111. udelay(1);
  112. timer->load_tim = load_val;
  113. udelay(1);
  114. timer->cntl = timerflags;
  115. }
  116. static inline void omap_mpu_timer_stop(int nr)
  117. {
  118. volatile omap_mpu_timer_regs_t* timer = omap_mpu_timer_base(nr);
  119. timer->cntl &= ~MPU_TIMER_ST;
  120. }
  121. /*
  122. * ---------------------------------------------------------------------------
  123. * MPU timer 1 ... count down to zero, interrupt, reload
  124. * ---------------------------------------------------------------------------
  125. */
  126. static int omap_mpu_set_next_event(unsigned long cycles,
  127. struct clock_event_device *evt)
  128. {
  129. omap_mpu_timer_start(0, cycles, 0);
  130. return 0;
  131. }
  132. static void omap_mpu_set_mode(enum clock_event_mode mode,
  133. struct clock_event_device *evt)
  134. {
  135. switch (mode) {
  136. case CLOCK_EVT_MODE_PERIODIC:
  137. omap_mpu_set_autoreset(0);
  138. break;
  139. case CLOCK_EVT_MODE_ONESHOT:
  140. omap_mpu_timer_stop(0);
  141. omap_mpu_remove_autoreset(0);
  142. break;
  143. case CLOCK_EVT_MODE_UNUSED:
  144. case CLOCK_EVT_MODE_SHUTDOWN:
  145. case CLOCK_EVT_MODE_RESUME:
  146. break;
  147. }
  148. }
  149. static struct clock_event_device clockevent_mpu_timer1 = {
  150. .name = "mpu_timer1",
  151. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  152. .shift = 32,
  153. .set_next_event = omap_mpu_set_next_event,
  154. .set_mode = omap_mpu_set_mode,
  155. };
  156. static irqreturn_t omap_mpu_timer1_interrupt(int irq, void *dev_id)
  157. {
  158. struct clock_event_device *evt = &clockevent_mpu_timer1;
  159. evt->event_handler(evt);
  160. return IRQ_HANDLED;
  161. }
  162. static struct irqaction omap_mpu_timer1_irq = {
  163. .name = "mpu_timer1",
  164. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  165. .handler = omap_mpu_timer1_interrupt,
  166. };
  167. static __init void omap_init_mpu_timer(unsigned long rate)
  168. {
  169. set_cyc2ns_scale(rate / 1000);
  170. setup_irq(INT_TIMER1, &omap_mpu_timer1_irq);
  171. omap_mpu_timer_start(0, (rate / HZ) - 1, 1);
  172. clockevent_mpu_timer1.mult = div_sc(rate, NSEC_PER_SEC,
  173. clockevent_mpu_timer1.shift);
  174. clockevent_mpu_timer1.max_delta_ns =
  175. clockevent_delta2ns(-1, &clockevent_mpu_timer1);
  176. clockevent_mpu_timer1.min_delta_ns =
  177. clockevent_delta2ns(1, &clockevent_mpu_timer1);
  178. clockevent_mpu_timer1.cpumask = cpumask_of_cpu(0);
  179. clockevents_register_device(&clockevent_mpu_timer1);
  180. }
  181. /*
  182. * ---------------------------------------------------------------------------
  183. * MPU timer 2 ... free running 32-bit clock source and scheduler clock
  184. * ---------------------------------------------------------------------------
  185. */
  186. static unsigned long omap_mpu_timer2_overflows;
  187. static irqreturn_t omap_mpu_timer2_interrupt(int irq, void *dev_id)
  188. {
  189. omap_mpu_timer2_overflows++;
  190. return IRQ_HANDLED;
  191. }
  192. static struct irqaction omap_mpu_timer2_irq = {
  193. .name = "mpu_timer2",
  194. .flags = IRQF_DISABLED,
  195. .handler = omap_mpu_timer2_interrupt,
  196. };
  197. static cycle_t mpu_read(void)
  198. {
  199. return ~omap_mpu_timer_read(1);
  200. }
  201. static struct clocksource clocksource_mpu = {
  202. .name = "mpu_timer2",
  203. .rating = 300,
  204. .read = mpu_read,
  205. .mask = CLOCKSOURCE_MASK(32),
  206. .shift = 24,
  207. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  208. };
  209. static void __init omap_init_clocksource(unsigned long rate)
  210. {
  211. static char err[] __initdata = KERN_ERR
  212. "%s: can't register clocksource!\n";
  213. clocksource_mpu.mult
  214. = clocksource_khz2mult(rate/1000, clocksource_mpu.shift);
  215. setup_irq(INT_TIMER2, &omap_mpu_timer2_irq);
  216. omap_mpu_timer_start(1, ~0, 1);
  217. if (clocksource_register(&clocksource_mpu))
  218. printk(err, clocksource_mpu.name);
  219. }
  220. /*
  221. * Scheduler clock - returns current time in nanosec units.
  222. */
  223. unsigned long long sched_clock(void)
  224. {
  225. unsigned long ticks = 0 - omap_mpu_timer_read(1);
  226. unsigned long long ticks64;
  227. ticks64 = omap_mpu_timer2_overflows;
  228. ticks64 <<= 32;
  229. ticks64 |= ticks;
  230. return cycles_2_ns(ticks64);
  231. }
  232. /*
  233. * ---------------------------------------------------------------------------
  234. * Timer initialization
  235. * ---------------------------------------------------------------------------
  236. */
  237. static void __init omap_timer_init(void)
  238. {
  239. struct clk *ck_ref = clk_get(NULL, "ck_ref");
  240. unsigned long rate;
  241. BUG_ON(IS_ERR(ck_ref));
  242. rate = clk_get_rate(ck_ref);
  243. clk_put(ck_ref);
  244. /* PTV = 0 */
  245. rate /= 2;
  246. omap_init_mpu_timer(rate);
  247. omap_init_clocksource(rate);
  248. }
  249. struct sys_timer omap_timer = {
  250. .init = omap_timer_init,
  251. };