blob: ba5a9f32ebd4825ae730cb5f9fd6314c51760707 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/***************************************************************************/
2
3/*
4 * timers.c -- generic ColdFire hardware timer support.
5 *
Greg Ungerera7f61fa2008-02-01 17:40:26 +10006 * Copyright (C) 1999-2008, Greg Ungerer <gerg@snapgear.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
9/***************************************************************************/
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/init.h>
Greg Ungerer2f2c2672007-10-23 14:37:54 +100013#include <linux/sched.h>
14#include <linux/interrupt.h>
Greg Ungererc52a2cd2007-07-27 01:09:00 +100015#include <linux/irq.h>
Greg Ungerera7f61fa2008-02-01 17:40:26 +100016#include <linux/profile.h>
17#include <linux/clocksource.h>
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +100018#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#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 */
Greg Ungerera7f61fa2008-02-01 17:40:26 +100030#define FREQ (MCF_BUSCLK / 16)
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +100031#define TA(a) (MCF_MBAR + MCFTIMER_BASE1 + (a))
32
33/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 * Default the timer and vector to use for ColdFire. Some ColdFire
35 * CPU's and some boards may want different. Their sub-architecture
36 * startup code (in config.c) can change these if they want.
37 */
38unsigned int mcf_timervector = 29;
39unsigned int mcf_profilevector = 31;
40unsigned int mcf_timerlevel = 5;
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/*
43 * These provide the underlying interrupt vector support.
44 * Unfortunately it is a little different on each ColdFire.
45 */
46extern void mcf_settimericr(int timer, int level);
Greg Ungerera7f61fa2008-02-01 17:40:26 +100047void coldfire_profile_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Greg Ungererdeb77c82006-12-06 11:36:59 +100049#if defined(CONFIG_M532x)
50#define __raw_readtrr __raw_readl
51#define __raw_writetrr __raw_writel
52#else
53#define __raw_readtrr __raw_readw
54#define __raw_writetrr __raw_writew
55#endif
56
Greg Ungerera7f61fa2008-02-01 17:40:26 +100057static u32 mcftmr_cycles_per_jiffy;
58static u32 mcftmr_cnt;
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/***************************************************************************/
61
Greg Ungerera7f61fa2008-02-01 17:40:26 +100062static irqreturn_t mcftmr_tick(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
64 /* Reset the ColdFire timer */
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +100065 __raw_writeb(MCFTIMER_TER_CAP | MCFTIMER_TER_REF, TA(MCFTIMER_TER));
Greg Ungerer2f2c2672007-10-23 14:37:54 +100066
Greg Ungerera7f61fa2008-02-01 17:40:26 +100067 mcftmr_cnt += mcftmr_cycles_per_jiffy;
Greg Ungerer2f2c2672007-10-23 14:37:54 +100068 return arch_timer_interrupt(irq, dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
71/***************************************************************************/
72
Greg Ungerera7f61fa2008-02-01 17:40:26 +100073static struct irqaction mcftmr_timer_irq = {
Greg Ungerer2f2c2672007-10-23 14:37:54 +100074 .name = "timer",
75 .flags = IRQF_DISABLED | IRQF_TIMER,
Greg Ungerera7f61fa2008-02-01 17:40:26 +100076 .handler = mcftmr_tick,
Greg Ungererc52a2cd2007-07-27 01:09:00 +100077};
78
Greg Ungerer2f2c2672007-10-23 14:37:54 +100079/***************************************************************************/
80
Greg Ungerera7f61fa2008-02-01 17:40:26 +100081static cycle_t mcftmr_read_clk(void)
82{
83 unsigned long flags;
84 u32 cycles;
85 u16 tcn;
86
87 local_irq_save(flags);
88 tcn = __raw_readw(TA(MCFTIMER_TCN));
89 cycles = mcftmr_cnt;
90 local_irq_restore(flags);
91
92 return cycles + tcn;
93}
94
95/***************************************************************************/
96
97static struct clocksource mcftmr_clk = {
98 .name = "tmr",
99 .rating = 250,
100 .read = mcftmr_read_clk,
101 .shift = 20,
102 .mask = CLOCKSOURCE_MASK(32),
103 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
104};
105
106/***************************************************************************/
Greg Ungerer4342f4a2007-06-08 13:46:39 -0700107
Greg Ungerer2f2c2672007-10-23 14:37:54 +1000108void hw_timer_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Greg Ungerera7f61fa2008-02-01 17:40:26 +1000110 setup_irq(mcf_timervector, &mcftmr_timer_irq);
Greg Ungererc52a2cd2007-07-27 01:09:00 +1000111
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000112 __raw_writew(MCFTIMER_TMR_DISABLE, TA(MCFTIMER_TMR));
Greg Ungerera7f61fa2008-02-01 17:40:26 +1000113 mcftmr_cycles_per_jiffy = FREQ / HZ;
114 __raw_writetrr(mcftmr_cycles_per_jiffy, TA(MCFTIMER_TRR));
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000115 __raw_writew(MCFTIMER_TMR_ENORI | MCFTIMER_TMR_CLK16 |
116 MCFTIMER_TMR_RESTART | MCFTIMER_TMR_ENABLE, TA(MCFTIMER_TMR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Greg Ungerera7f61fa2008-02-01 17:40:26 +1000118 mcftmr_clk.mult = clocksource_hz2mult(FREQ, mcftmr_clk.shift);
119 clocksource_register(&mcftmr_clk);
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 mcf_settimericr(1, mcf_timerlevel);
122
123#ifdef CONFIG_HIGHPROFILE
124 coldfire_profile_init();
125#endif
126}
127
128/***************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#ifdef CONFIG_HIGHPROFILE
130/***************************************************************************/
131
132/*
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000133 * By default use timer2 as the profiler clock timer.
134 */
135#define PA(a) (MCF_MBAR + MCFTIMER_BASE2 + (a))
136
137/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 * Choose a reasonably fast profile timer. Make it an odd value to
Robert P. J. Dayd08df602007-02-17 19:07:33 +0100139 * try and get good coverage of kernel operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 */
141#define PROFILEHZ 1013
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/*
144 * Use the other timer to provide high accuracy profiling info.
145 */
Greg Ungererc051b012007-02-07 12:03:01 +1000146irqreturn_t coldfire_profile_tick(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 /* Reset ColdFire timer2 */
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000149 __raw_writeb(MCFTIMER_TER_CAP | MCFTIMER_TER_REF, PA(MCFTIMER_TER));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (current->pid)
Matt Waddel6ef1e562008-02-14 19:31:27 -0800151 profile_tick(CPU_PROFILING);
Greg Ungererc051b012007-02-07 12:03:01 +1000152 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
155/***************************************************************************/
156
Matt Waddel6ef1e562008-02-14 19:31:27 -0800157static struct irqaction coldfire_profile_irq = {
158 .name = "profile timer",
159 .flags = IRQF_DISABLED | IRQF_TIMER,
160 .handler = coldfire_profile_tick,
161};
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163void coldfire_profile_init(void)
164{
Matt Waddel6ef1e562008-02-14 19:31:27 -0800165 printk(KERN_INFO "PROFILE: lodging TIMER2 @ %dHz as profile timer\n",
166 PROFILEHZ);
167
168 setup_irq(mcf_profilevector, &coldfire_profile_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 /* Set up TIMER 2 as high speed profile clock */
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000171 __raw_writew(MCFTIMER_TMR_DISABLE, PA(MCFTIMER_TMR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Matt Waddel6ef1e562008-02-14 19:31:27 -0800173 __raw_writetrr(((MCF_BUSCLK / 16) / PROFILEHZ), PA(MCFTIMER_TRR));
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000174 __raw_writew(MCFTIMER_TMR_ENORI | MCFTIMER_TMR_CLK16 |
175 MCFTIMER_TMR_RESTART | MCFTIMER_TMR_ENABLE, PA(MCFTIMER_TMR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 mcf_settimericr(2, 7);
178}
179
180/***************************************************************************/
181#endif /* CONFIG_HIGHPROFILE */
182/***************************************************************************/