Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[sfrench/cifs-2.6.git] / arch / m68knommu / platform / 5307 / pit.c
1 /***************************************************************************/
2
3 /*
4  *      pit.c -- Freescale ColdFire PIT timer. Currently this type of
5  *               hardware timer only exists in the Freescale ColdFire
6  *               5270/5271, 5282 and other CPUs.
7  *
8  *      Copyright (C) 1999-2006, Greg Ungerer (gerg@snapgear.com)
9  *      Copyright (C) 2001-2004, SnapGear Inc. (www.snapgear.com)
10  *
11  */
12
13 /***************************************************************************/
14
15 #include <linux/config.h>
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/param.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <asm/io.h>
22 #include <asm/irq.h>
23 #include <asm/coldfire.h>
24 #include <asm/mcfpit.h>
25 #include <asm/mcfsim.h>
26
27 /***************************************************************************/
28
29 /*
30  *      By default use timer1 as the system clock timer.
31  */
32 #define TA(a)   (MCF_IPSBAR + MCFPIT_BASE1 + (a))
33
34 /***************************************************************************/
35
36 void coldfire_pit_tick(void)
37 {
38         unsigned short pcsr;
39
40         /* Reset the ColdFire timer */
41         pcsr = __raw_readw(TA(MCFPIT_PCSR));
42         __raw_writew(pcsr | MCFPIT_PCSR_PIF, TA(MCFPIT_PCSR));
43 }
44
45 /***************************************************************************/
46
47 void coldfire_pit_init(irqreturn_t (*handler)(int, void *, struct pt_regs *))
48 {
49         volatile unsigned char *icrp;
50         volatile unsigned long *imrp;
51
52         request_irq(MCFINT_VECBASE + MCFINT_PIT1, handler, SA_INTERRUPT,
53                 "ColdFire Timer", NULL);
54
55         icrp = (volatile unsigned char *) (MCF_IPSBAR + MCFICM_INTC0 +
56                 MCFINTC_ICR0 + MCFINT_PIT1);
57         *icrp = ICR_INTRCONF;
58
59         imrp = (volatile unsigned long *) (MCF_IPSBAR + MCFICM_INTC0 + MCFPIT_IMR);
60         *imrp &= ~MCFPIT_IMR_IBIT;
61
62         /* Set up PIT timer 1 as poll clock */
63         __raw_writew(MCFPIT_PCSR_DISABLE, TA(MCFPIT_PCSR));
64         __raw_writew(((MCF_CLK / 2) / 64) / HZ, TA(MCFPIT_PMR));
65         __raw_writew(MCFPIT_PCSR_EN | MCFPIT_PCSR_PIE | MCFPIT_PCSR_OVW |
66                 MCFPIT_PCSR_RLD | MCFPIT_PCSR_CLK64, TA(MCFPIT_PCSR));
67 }
68
69 /***************************************************************************/
70
71 unsigned long coldfire_pit_offset(void)
72 {
73         volatile unsigned long *ipr;
74         unsigned long pmr, pcntr, offset;
75
76         ipr = (volatile unsigned long *) (MCF_IPSBAR + MCFICM_INTC0 + MCFPIT_IMR);
77
78         pmr = __raw_readw(TA(MCFPIT_PMR));
79         pcntr = __raw_readw(TA(MCFPIT_PCNTR));
80
81         /*
82          * If we are still in the first half of the upcount and a
83          * timer interupt is pending, then add on a ticks worth of time.
84          */
85         offset = ((pmr - pcntr) * (1000000 / HZ)) / pmr;
86         if ((offset < (1000000 / HZ / 2)) && (*ipr & MCFPIT_IMR_IBIT))
87                 offset += 1000000 / HZ;
88         return offset;  
89 }
90
91 /***************************************************************************/