This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / arch / arm / mach-iop3xx / iop331-time.c
1 /*
2  * arch/arm/mach-iop3xx/iop331-time.c
3  *
4  * Timer code for IOP331 based systems
5  *
6  * Author: Dave Jiang <dave.jiang@intel.com>
7  *
8  * Copyright 2003 Intel Corp.
9  *
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.
14  */
15
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>
21
22 #include <asm/hardware.h>
23 #include <asm/io.h>
24 #include <asm/irq.h>
25 #include <asm/uaccess.h>
26 #include <asm/mach-types.h>
27 #include <asm/mach/irq.h>
28 #include <asm/mach/time.h>
29
30 #undef IOP331_TIME_SYNC
31
32 static unsigned long iop331_latch;
33
34 static inline unsigned long get_elapsed(void)
35 {
36         return iop331_latch - *IOP331_TU_TCR0;
37 }
38
39 static unsigned long iop331_gettimeoffset(void)
40 {
41         unsigned long elapsed, usec;
42         u32 tisr1, tisr2;
43
44         /*
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.
51          */
52
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));
56
57         if(tisr1 & 1)
58                 elapsed += iop331_latch;
59         else if (tisr2 & 1)
60                 elapsed = iop331_latch + get_elapsed();
61
62         /*
63          * Now convert them to usec.
64          */
65         usec = (unsigned long)(elapsed * (tick_nsec / 1000)) / iop331_latch;
66
67         return usec;
68 }
69
70 static irqreturn_t
71 iop331_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
72 {
73         u32 tisr;
74 #ifdef IOP331_TIME_SYNC
75         u32 passed;
76 #define TM_THRESH (iop331_latch*2)
77 #endif
78
79         asm volatile("mrc p6, 0, %0, c6, c1, 0" : "=r" (tisr));
80
81         tisr |= 1;
82
83         asm volatile("mcr p6, 0, %0, c6, c1, 0" : : "r" (tisr));
84
85 #ifdef IOP331_TIME_SYNC
86         passed = 0xffffffff - *IOP331_TU_TCR1;
87
88         do
89         {
90                 do_timer(regs);
91                 if(passed < TM_THRESH)
92                         break;
93                 if(passed > iop331_latch)
94                         passed -= iop331_latch;
95                 else
96                         passed = 0;
97         } while(1);
98
99         asm volatile("mcr p6, 0, %0, c3, c1, 0" : : "r" (0xffffffff));
100 #else
101         do_timer(regs);
102 #endif
103
104         return IRQ_HANDLED;
105 }
106
107 static struct irqaction iop331_timer_irq = {
108         .name           = "IOP331 Timer Tick",
109         .handler        = iop331_timer_interrupt,
110         .flags          = SA_INTERRUPT
111 };
112
113 extern int setup_arm_irq(int, struct irqaction*);
114
115 void __init iop331_init_time(void)
116 {
117         u32 timer_ctl;
118
119         iop331_latch = (CLOCK_TICK_RATE + HZ / 2) / HZ;
120         gettimeoffset = iop331_gettimeoffset;
121         setup_irq(IRQ_IOP331_TIMER0, &iop331_timer_irq);
122
123         timer_ctl = IOP331_TMR_EN | IOP331_TMR_PRIVILEGED | IOP331_TMR_RELOAD |
124                         IOP331_TMR_RATIO_1_1;
125
126         asm volatile("mcr p6, 0, %0, c4, c1, 0" : : "r" (iop331_latch));
127
128         asm volatile("mcr p6, 0, %0, c0, c1, 0" : : "r" (timer_ctl));
129
130 #ifdef IOP331_TIME_SYNC
131         /* Setup second timer */
132         /* setup counter */
133         timer_ctl = IOP331_TMR_EN | IOP331_TMR_PRIVILEGED |
134                         IOP331_TMR_RATIO_1_1;
135         asm volatile("mcr p6, 0, %0, c3, c1, 0" : : "r" (0xffffffff));
136         /* setup control */
137         asm volatile("mcr p6, 0, %0, c1, c1, 0" : : "r" (timer_ctl));
138 #endif
139 }
140
141