blob: 83b8b89dfa09e7041ad3fcfb364d7f9eae13e178 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/***************************************************************************/
2
3/*
4 * timers.c -- generic ColdFire hardware timer support.
5 *
6 * Copyright (C) 1999-2003, Greg Ungerer (gerg@snapgear.com)
7 */
8
9/***************************************************************************/
10
11#include <linux/config.h>
12#include <linux/kernel.h>
13#include <linux/sched.h>
14#include <linux/param.h>
15#include <linux/interrupt.h>
16#include <linux/init.h>
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +100017#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/irq.h>
19#include <asm/traps.h>
20#include <asm/machdep.h>
21#include <asm/coldfire.h>
22#include <asm/mcftimer.h>
23#include <asm/mcfsim.h>
24
25/***************************************************************************/
26
27/*
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +100028 * By default use timer1 as the system clock timer.
29 */
30#define TA(a) (MCF_MBAR + MCFTIMER_BASE1 + (a))
31
32/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 * Default the timer and vector to use for ColdFire. Some ColdFire
34 * CPU's and some boards may want different. Their sub-architecture
35 * startup code (in config.c) can change these if they want.
36 */
37unsigned int mcf_timervector = 29;
38unsigned int mcf_profilevector = 31;
39unsigned int mcf_timerlevel = 5;
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/*
42 * These provide the underlying interrupt vector support.
43 * Unfortunately it is a little different on each ColdFire.
44 */
45extern void mcf_settimericr(int timer, int level);
46extern int mcf_timerirqpending(int timer);
47
48/***************************************************************************/
49
50void coldfire_tick(void)
51{
52 /* Reset the ColdFire timer */
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +100053 __raw_writeb(MCFTIMER_TER_CAP | MCFTIMER_TER_REF, TA(MCFTIMER_TER));
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
56/***************************************************************************/
57
58void coldfire_timer_init(irqreturn_t (*handler)(int, void *, struct pt_regs *))
59{
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +100060 __raw_writew(MCFTIMER_TMR_DISABLE, TA(MCFTIMER_TMR));
61 __raw_writew(((MCF_BUSCLK / 16) / HZ), TA(MCFTIMER_TRR));
62 __raw_writew(MCFTIMER_TMR_ENORI | MCFTIMER_TMR_CLK16 |
63 MCFTIMER_TMR_RESTART | MCFTIMER_TMR_ENABLE, TA(MCFTIMER_TMR));
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 request_irq(mcf_timervector, handler, SA_INTERRUPT, "timer", NULL);
66 mcf_settimericr(1, mcf_timerlevel);
67
68#ifdef CONFIG_HIGHPROFILE
69 coldfire_profile_init();
70#endif
71}
72
73/***************************************************************************/
74
75unsigned long coldfire_timer_offset(void)
76{
77 unsigned long trr, tcn, offset;
78
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +100079 tcn = __raw_readw(TA(MCFTIMER_TCN));
80 trr = __raw_readw(TA(MCFTIMER_TRR));
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 offset = (tcn * (1000000 / HZ)) / trr;
82
83 /* Check if we just wrapped the counters and maybe missed a tick */
84 if ((offset < (1000000 / HZ / 2)) && mcf_timerirqpending(1))
85 offset += 1000000 / HZ;
86 return offset;
87}
88
89/***************************************************************************/
90#ifdef CONFIG_HIGHPROFILE
91/***************************************************************************/
92
93/*
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +100094 * By default use timer2 as the profiler clock timer.
95 */
96#define PA(a) (MCF_MBAR + MCFTIMER_BASE2 + (a))
97
98/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 * Choose a reasonably fast profile timer. Make it an odd value to
100 * try and get good coverage of kernal operations.
101 */
102#define PROFILEHZ 1013
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/*
105 * Use the other timer to provide high accuracy profiling info.
106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107void coldfire_profile_tick(int irq, void *dummy, struct pt_regs *regs)
108{
109 /* Reset ColdFire timer2 */
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000110 __raw_writeb(MCFTIMER_TER_CAP | MCFTIMER_TER_REF, PA(MCFTIMER_TER));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (current->pid)
112 profile_tick(CPU_PROFILING, regs);
113}
114
115/***************************************************************************/
116
117void coldfire_profile_init(void)
118{
119 printk(KERN_INFO "PROFILE: lodging TIMER2 @ %dHz as profile timer\n", PROFILEHZ);
120
121 /* Set up TIMER 2 as high speed profile clock */
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000122 __raw_writew(MCFTIMER_TMR_DISABLE, PA(MCFTIMER_TMR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000124 __raw_writew(((MCF_CLK / 16) / PROFILEHZ), PA(MCFTIMER_TRR));
125 __raw_writew(MCFTIMER_TMR_ENORI | MCFTIMER_TMR_CLK16 |
126 MCFTIMER_TMR_RESTART | MCFTIMER_TMR_ENABLE, PA(MCFTIMER_TMR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 request_irq(mcf_profilevector, coldfire_profile_tick,
129 (SA_INTERRUPT | IRQ_FLG_FAST), "profile timer", NULL);
130 mcf_settimericr(2, 7);
131}
132
133/***************************************************************************/
134#endif /* CONFIG_HIGHPROFILE */
135/***************************************************************************/