blob: 40039b2a90b39079a7af6f5fcdd5856f6452280e [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>
Pavel Pisa86987d52006-12-06 17:19:44 +010017#include <linux/clocksource.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <asm/hardware.h>
20#include <asm/io.h>
21#include <asm/leds.h>
22#include <asm/irq.h>
23#include <asm/mach/time.h>
24
25/* Use timer 1 as system timer */
26#define TIMER_BASE IMX_TIM1_BASE
27
Pavel Pisa86987d52006-12-06 17:19:44 +010028static unsigned long evt_diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/*
31 * IRQ handler for the timer
32 */
33static irqreturn_t
Linus Torvalds0cd61b62006-10-06 10:53:39 -070034imx_timer_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Pavel Pisa86987d52006-12-06 17:19:44 +010036 uint32_t tstat;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38 /* clear the interrupt */
Pavel Pisa86987d52006-12-06 17:19:44 +010039 tstat = IMX_TSTAT(TIMER_BASE);
40 IMX_TSTAT(TIMER_BASE) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Pavel Pisa86987d52006-12-06 17:19:44 +010042 if (tstat & TSTAT_COMP) {
43 do {
44
45 write_seqlock(&xtime_lock);
46 timer_tick();
47 write_sequnlock(&xtime_lock);
48 IMX_TCMP(TIMER_BASE) += evt_diff;
49
50 } while (unlikely((int32_t)(IMX_TCMP(TIMER_BASE)
51 - IMX_TCN(TIMER_BASE)) < 0));
52 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54 return IRQ_HANDLED;
55}
56
57static struct irqaction imx_timer_irq = {
58 .name = "i.MX Timer Tick",
Thomas Gleixner52e405e2006-07-03 02:20:05 +020059 .flags = IRQF_DISABLED | IRQF_TIMER,
Russell King09b8b5f2005-06-26 17:06:36 +010060 .handler = imx_timer_interrupt,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061};
62
63/*
Pavel Pisa86987d52006-12-06 17:19:44 +010064 * Set up timer hardware into expected mode and state.
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 */
Pavel Pisa86987d52006-12-06 17:19:44 +010066static void __init imx_timer_hardware_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 /*
69 * Initialise to a known state (all timers off, and timing reset)
70 */
71 IMX_TCTL(TIMER_BASE) = 0;
72 IMX_TPRER(TIMER_BASE) = 0;
73 IMX_TCMP(TIMER_BASE) = LATCH - 1;
Pavel Pisa86987d52006-12-06 17:19:44 +010074
75 IMX_TCTL(TIMER_BASE) = TCTL_FRR | TCTL_CLK_PCLK1 | TCTL_IRQEN | TCTL_TEN;
76 evt_diff = LATCH;
77}
78
79cycle_t imx_get_cycles(void)
80{
81 return IMX_TCN(TIMER_BASE);
82}
83
84static struct clocksource clocksource_imx = {
85 .name = "imx_timer1",
86 .rating = 200,
87 .read = imx_get_cycles,
88 .mask = 0xFFFFFFFF,
89 .shift = 20,
90 .is_continuous = 1,
91};
92
93static int __init imx_clocksource_init(void)
94{
95 clocksource_imx.mult =
96 clocksource_hz2mult(imx_get_perclk1(), clocksource_imx.shift);
97 clocksource_register(&clocksource_imx);
98
99 return 0;
100}
101
102static void __init imx_timer_init(void)
103{
104 imx_timer_hardware_init();
105 imx_clocksource_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 /*
108 * Make irqs happen for the system timer
109 */
110 setup_irq(TIM1_INT, &imx_timer_irq);
111}
112
113struct sys_timer imx_timer = {
114 .init = imx_timer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115};