2 * arch/arm/mach-iop3xx/iop321-time.c
4 * Timer code for IOP321 based systems
6 * Author: Deepak Saxena <dsaxena@mvista.com>
8 * Copyright 2002-2003 MontaVista Software Inc.
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
16 #include <linux/kernel.h>
17 #include <linux/interrupt.h>
18 #include <linux/time.h>
19 #include <linux/init.h>
20 #include <linux/timex.h>
22 #include <asm/hardware.h>
25 #include <asm/uaccess.h>
26 #include <asm/mach-types.h>
27 #include <asm/mach/irq.h>
28 #include <asm/mach/time.h>
30 #define IOP321_TIME_SYNC 0
32 static unsigned long iop321_latch;
34 static inline unsigned long get_elapsed(void)
36 return iop321_latch - *IOP321_TU_TCR0;
39 static unsigned long iop321_gettimeoffset(void)
41 unsigned long elapsed, usec;
45 * If an interrupt was pending before we read the timer,
46 * we've already wrapped. Factor this into the time.
47 * If an interrupt was pending after we read the timer,
48 * it may have wrapped between checking the interrupt
49 * status and reading the timer. Re-read the timer to
50 * be sure its value is after the wrap.
53 asm volatile("mrc p6, 0, %0, c6, c1, 0" : "=r" (tisr1));
54 elapsed = get_elapsed();
55 asm volatile("mrc p6, 0, %0, c6, c1, 0" : "=r" (tisr2));
58 elapsed += iop321_latch;
60 elapsed = iop321_latch + get_elapsed();
63 * Now convert them to usec.
65 usec = (unsigned long)(elapsed * (tick_nsec / 1000)) / iop321_latch;
71 iop321_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
74 #ifdef IOP321_TIME_SYNC
76 #define TM_THRESH (iop321_latch*2)
79 asm volatile("mrc p6, 0, %0, c6, c1, 0" : "=r" (tisr));
83 asm volatile("mcr p6, 0, %0, c6, c1, 0" : : "r" (tisr));
85 #ifdef IOP321_TIME_SYNC
86 passed = 0xffffffff - *IOP321_TU_TCR1;
91 if(passed < TM_THRESH)
93 if(passed > iop321_latch)
94 passed -= iop321_latch;
99 asm volatile("mcr p6, 0, %0, c3, c1, 0" : : "r" (0xffffffff));
107 static struct irqaction iop321_timer_irq = {
108 .name = "IOP321 Timer Tick",
109 .handler = iop321_timer_interrupt,
110 .flags = SA_INTERRUPT
113 extern int setup_arm_irq(int, struct irqaction*);
115 void __init iop321_init_time(void)
119 iop321_latch = (CLOCK_TICK_RATE + HZ / 2) / HZ;
120 gettimeoffset = iop321_gettimeoffset;
121 setup_irq(IRQ_IOP321_TIMER0, &iop321_timer_irq);
123 timer_ctl = IOP321_TMR_EN | IOP321_TMR_PRIVILEGED | IOP321_TMR_RELOAD |
124 IOP321_TMR_RATIO_1_1;
126 asm volatile("mcr p6, 0, %0, c4, c1, 0" : : "r" (iop321_latch));
128 asm volatile("mcr p6, 0, %0, c0, c1, 0" : : "r" (timer_ctl));
130 #ifdef IOP321_TIME_SYNC
131 /* Setup second timer */
133 timer_ctl = IOP321_TMR_EN | IOP321_TMR_PRIVILEGED |
134 IOP321_TMR_RATIO_1_1;
135 asm volatile("mcr p6, 0, %0, c3, c1, 0" : : "r" (0xffffffff));
137 asm volatile("mcr p6, 0, %0, c1, c1, 0" : : "r" (timer_ctl));