blob: 8ae4a2c5066fc2a55e7bec5c46377162aa119cc6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/arm/mach-imx/time.c
3 *
4 * Copyright (C) 2000-2001 Deep Blue Solutions
5 * Copyright (C) 2002 Shane Nay (shane@minirl.com)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kernel.h>
12#include <linux/sched.h>
13#include <linux/init.h>
14#include <linux/interrupt.h>
Thomas Gleixnera6284ac2006-07-01 22:32:34 +010015#include <linux/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/time.h>
17
18#include <asm/hardware.h>
19#include <asm/io.h>
20#include <asm/leds.h>
21#include <asm/irq.h>
22#include <asm/mach/time.h>
23
24/* Use timer 1 as system timer */
25#define TIMER_BASE IMX_TIM1_BASE
26
27/*
28 * Returns number of us since last clock interrupt. Note that interrupts
29 * will have been disabled by do_gettimeoffset()
30 */
31static unsigned long imx_gettimeoffset(void)
32{
33 unsigned long ticks;
34
35 /*
36 * Get the current number of ticks. Note that there is a race
37 * condition between us reading the timer and checking for
38 * an interrupt. We get around this by ensuring that the
39 * counter has not reloaded between our two reads.
40 */
41 ticks = IMX_TCN(TIMER_BASE);
42
43 /*
44 * Interrupt pending? If so, we've reloaded once already.
45 */
46 if (IMX_TSTAT(TIMER_BASE) & TSTAT_COMP)
47 ticks += LATCH;
48
49 /*
50 * Convert the ticks to usecs
51 */
52 return (1000000 / CLK32) * ticks;
53}
54
55/*
56 * IRQ handler for the timer
57 */
58static irqreturn_t
Linus Torvalds0cd61b62006-10-06 10:53:39 -070059imx_timer_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
61 write_seqlock(&xtime_lock);
62
63 /* clear the interrupt */
64 if (IMX_TSTAT(TIMER_BASE))
65 IMX_TSTAT(TIMER_BASE) = 0;
66
Linus Torvalds0cd61b62006-10-06 10:53:39 -070067 timer_tick();
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 write_sequnlock(&xtime_lock);
69
70 return IRQ_HANDLED;
71}
72
73static struct irqaction imx_timer_irq = {
74 .name = "i.MX Timer Tick",
Thomas Gleixner52e405e2006-07-03 02:20:05 +020075 .flags = IRQF_DISABLED | IRQF_TIMER,
Russell King09b8b5f2005-06-26 17:06:36 +010076 .handler = imx_timer_interrupt,
Linus Torvalds1da177e2005-04-16 15:20:36 -070077};
78
79/*
80 * Set up timer interrupt, and return the current time in seconds.
81 */
82static void __init imx_timer_init(void)
83{
84 /*
85 * Initialise to a known state (all timers off, and timing reset)
86 */
87 IMX_TCTL(TIMER_BASE) = 0;
88 IMX_TPRER(TIMER_BASE) = 0;
89 IMX_TCMP(TIMER_BASE) = LATCH - 1;
90 IMX_TCTL(TIMER_BASE) = TCTL_CLK_32 | TCTL_IRQEN | TCTL_TEN;
91
92 /*
93 * Make irqs happen for the system timer
94 */
95 setup_irq(TIM1_INT, &imx_timer_irq);
96}
97
98struct sys_timer imx_timer = {
99 .init = imx_timer_init,
100 .offset = imx_gettimeoffset,
101};