blob: cd496a20fcc7ced7b802e705badcae2417eb2256 [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 Ungerer58f0ac92011-03-09 09:57:14 +100031#define TA(a) (MCFTIMER_BASE1 + (a))
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +100032
33/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 * These provide the underlying interrupt vector support.
35 * Unfortunately it is a little different on each ColdFire.
36 */
Greg Ungerera7f61fa2008-02-01 17:40:26 +100037void coldfire_profile_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Greg Ungerer6eac4022012-11-05 12:01:38 +100039#if defined(CONFIG_M53xx) || defined(CONFIG_M5441x)
Greg Ungererdeb77c82006-12-06 11:36:59 +100040#define __raw_readtrr __raw_readl
41#define __raw_writetrr __raw_writel
42#else
43#define __raw_readtrr __raw_readw
44#define __raw_writetrr __raw_writew
45#endif
46
Greg Ungerera7f61fa2008-02-01 17:40:26 +100047static u32 mcftmr_cycles_per_jiffy;
48static u32 mcftmr_cnt;
49
Greg Ungerer35aefb22012-01-23 15:34:58 +100050static irq_handler_t timer_interrupt;
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/***************************************************************************/
53
Greg Ungerer440f6ff2011-12-24 14:36:27 +100054static void init_timer_irq(void)
55{
56#ifdef MCFSIM_ICR_AUTOVEC
57 /* Timer1 is always used as system timer */
58 writeb(MCFSIM_ICR_AUTOVEC | MCFSIM_ICR_LEVEL6 | MCFSIM_ICR_PRI3,
Greg Ungererc986a3d2012-08-17 16:48:16 +100059 MCFSIM_TIMER1ICR);
Greg Ungerer440f6ff2011-12-24 14:36:27 +100060 mcf_mapirq2imr(MCF_IRQ_TIMER, MCFINTC_TIMER1);
61
62#ifdef CONFIG_HIGHPROFILE
63 /* Timer2 is to be used as a high speed profile timer */
64 writeb(MCFSIM_ICR_AUTOVEC | MCFSIM_ICR_LEVEL7 | MCFSIM_ICR_PRI3,
Greg Ungererc986a3d2012-08-17 16:48:16 +100065 MCFSIM_TIMER2ICR);
Greg Ungerer440f6ff2011-12-24 14:36:27 +100066 mcf_mapirq2imr(MCF_IRQ_PROFILER, MCFINTC_TIMER2);
67#endif
68#endif /* MCFSIM_ICR_AUTOVEC */
69}
70
71/***************************************************************************/
72
Greg Ungerera7f61fa2008-02-01 17:40:26 +100073static irqreturn_t mcftmr_tick(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 /* Reset the ColdFire timer */
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +100076 __raw_writeb(MCFTIMER_TER_CAP | MCFTIMER_TER_REF, TA(MCFTIMER_TER));
Greg Ungerer2f2c2672007-10-23 14:37:54 +100077
Greg Ungerera7f61fa2008-02-01 17:40:26 +100078 mcftmr_cnt += mcftmr_cycles_per_jiffy;
Greg Ungerer35aefb22012-01-23 15:34:58 +100079 return timer_interrupt(irq, dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
82/***************************************************************************/
83
Greg Ungerera7f61fa2008-02-01 17:40:26 +100084static struct irqaction mcftmr_timer_irq = {
Greg Ungerer2f2c2672007-10-23 14:37:54 +100085 .name = "timer",
Michael Opdenacker77a42792013-09-07 07:43:08 +020086 .flags = IRQF_TIMER,
Greg Ungerera7f61fa2008-02-01 17:40:26 +100087 .handler = mcftmr_tick,
Greg Ungererc52a2cd2007-07-27 01:09:00 +100088};
89
Greg Ungerer2f2c2672007-10-23 14:37:54 +100090/***************************************************************************/
91
Magnus Damm8e196082009-04-21 12:24:00 -070092static cycle_t mcftmr_read_clk(struct clocksource *cs)
Greg Ungerera7f61fa2008-02-01 17:40:26 +100093{
94 unsigned long flags;
95 u32 cycles;
96 u16 tcn;
97
98 local_irq_save(flags);
99 tcn = __raw_readw(TA(MCFTIMER_TCN));
100 cycles = mcftmr_cnt;
101 local_irq_restore(flags);
102
103 return cycles + tcn;
104}
105
106/***************************************************************************/
107
108static struct clocksource mcftmr_clk = {
109 .name = "tmr",
110 .rating = 250,
111 .read = mcftmr_read_clk,
Greg Ungerera7f61fa2008-02-01 17:40:26 +1000112 .mask = CLOCKSOURCE_MASK(32),
113 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
114};
115
116/***************************************************************************/
Greg Ungerer4342f4a2007-06-08 13:46:39 -0700117
Greg Ungerer35aefb22012-01-23 15:34:58 +1000118void hw_timer_init(irq_handler_t handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000120 __raw_writew(MCFTIMER_TMR_DISABLE, TA(MCFTIMER_TMR));
Greg Ungerera7f61fa2008-02-01 17:40:26 +1000121 mcftmr_cycles_per_jiffy = FREQ / HZ;
Philippe De Muyter6c38d852008-06-12 15:21:36 -0700122 /*
123 * The coldfire timer runs from 0 to TRR included, then 0
124 * again and so on. It counts thus actually TRR + 1 steps
125 * for 1 tick, not TRR. So if you want n cycles,
126 * initialize TRR with n - 1.
127 */
128 __raw_writetrr(mcftmr_cycles_per_jiffy - 1, TA(MCFTIMER_TRR));
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000129 __raw_writew(MCFTIMER_TMR_ENORI | MCFTIMER_TMR_CLK16 |
130 MCFTIMER_TMR_RESTART | MCFTIMER_TMR_ENABLE, TA(MCFTIMER_TMR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
John Stultz010f3f12010-04-26 20:21:52 -0700132 clocksource_register_hz(&mcftmr_clk, FREQ);
Greg Ungerera7f61fa2008-02-01 17:40:26 +1000133
Greg Ungerer35aefb22012-01-23 15:34:58 +1000134 timer_interrupt = handler;
Greg Ungerer440f6ff2011-12-24 14:36:27 +1000135 init_timer_irq();
Greg Ungerer3945ca0f2009-05-22 13:50:53 +1000136 setup_irq(MCF_IRQ_TIMER, &mcftmr_timer_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138#ifdef CONFIG_HIGHPROFILE
139 coldfire_profile_init();
140#endif
141}
142
143/***************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144#ifdef CONFIG_HIGHPROFILE
145/***************************************************************************/
146
147/*
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000148 * By default use timer2 as the profiler clock timer.
149 */
Greg Ungerer58f0ac92011-03-09 09:57:14 +1000150#define PA(a) (MCFTIMER_BASE2 + (a))
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000151
152/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 * Choose a reasonably fast profile timer. Make it an odd value to
Robert P. J. Dayd08df602007-02-17 19:07:33 +0100154 * try and get good coverage of kernel operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 */
156#define PROFILEHZ 1013
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/*
159 * Use the other timer to provide high accuracy profiling info.
160 */
Greg Ungererc051b012007-02-07 12:03:01 +1000161irqreturn_t coldfire_profile_tick(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 /* Reset ColdFire timer2 */
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000164 __raw_writeb(MCFTIMER_TER_CAP | MCFTIMER_TER_REF, PA(MCFTIMER_TER));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (current->pid)
Matt Waddel6ef1e562008-02-14 19:31:27 -0800166 profile_tick(CPU_PROFILING);
Greg Ungererc051b012007-02-07 12:03:01 +1000167 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
170/***************************************************************************/
171
Matt Waddel6ef1e562008-02-14 19:31:27 -0800172static struct irqaction coldfire_profile_irq = {
173 .name = "profile timer",
Michael Opdenacker77a42792013-09-07 07:43:08 +0200174 .flags = IRQF_TIMER,
Matt Waddel6ef1e562008-02-14 19:31:27 -0800175 .handler = coldfire_profile_tick,
176};
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178void coldfire_profile_init(void)
179{
Matt Waddel6ef1e562008-02-14 19:31:27 -0800180 printk(KERN_INFO "PROFILE: lodging TIMER2 @ %dHz as profile timer\n",
181 PROFILEHZ);
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 /* Set up TIMER 2 as high speed profile clock */
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000184 __raw_writew(MCFTIMER_TMR_DISABLE, PA(MCFTIMER_TMR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Matt Waddel6ef1e562008-02-14 19:31:27 -0800186 __raw_writetrr(((MCF_BUSCLK / 16) / PROFILEHZ), PA(MCFTIMER_TRR));
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000187 __raw_writew(MCFTIMER_TMR_ENORI | MCFTIMER_TMR_CLK16 |
188 MCFTIMER_TMR_RESTART | MCFTIMER_TMR_ENABLE, PA(MCFTIMER_TMR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Greg Ungerer3945ca0f2009-05-22 13:50:53 +1000190 setup_irq(MCF_IRQ_PROFILER, &coldfire_profile_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
193/***************************************************************************/
194#endif /* CONFIG_HIGHPROFILE */
195/***************************************************************************/