blob: d0b4e9798fdef0fb004024feeaee0dbf1bce62cc [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 Ungererdeb77c82006-12-06 11:36:59 +100039#if defined(CONFIG_M532x)
40#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 Ungerera7f61fa2008-02-01 17:40:26 +100054static irqreturn_t mcftmr_tick(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
56 /* Reset the ColdFire timer */
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +100057 __raw_writeb(MCFTIMER_TER_CAP | MCFTIMER_TER_REF, TA(MCFTIMER_TER));
Greg Ungerer2f2c2672007-10-23 14:37:54 +100058
Greg Ungerera7f61fa2008-02-01 17:40:26 +100059 mcftmr_cnt += mcftmr_cycles_per_jiffy;
Greg Ungerer35aefb22012-01-23 15:34:58 +100060 return timer_interrupt(irq, dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
63/***************************************************************************/
64
Greg Ungerera7f61fa2008-02-01 17:40:26 +100065static struct irqaction mcftmr_timer_irq = {
Greg Ungerer2f2c2672007-10-23 14:37:54 +100066 .name = "timer",
67 .flags = IRQF_DISABLED | IRQF_TIMER,
Greg Ungerera7f61fa2008-02-01 17:40:26 +100068 .handler = mcftmr_tick,
Greg Ungererc52a2cd2007-07-27 01:09:00 +100069};
70
Greg Ungerer2f2c2672007-10-23 14:37:54 +100071/***************************************************************************/
72
Magnus Damm8e196082009-04-21 12:24:00 -070073static cycle_t mcftmr_read_clk(struct clocksource *cs)
Greg Ungerera7f61fa2008-02-01 17:40:26 +100074{
75 unsigned long flags;
76 u32 cycles;
77 u16 tcn;
78
79 local_irq_save(flags);
80 tcn = __raw_readw(TA(MCFTIMER_TCN));
81 cycles = mcftmr_cnt;
82 local_irq_restore(flags);
83
84 return cycles + tcn;
85}
86
87/***************************************************************************/
88
89static struct clocksource mcftmr_clk = {
90 .name = "tmr",
91 .rating = 250,
92 .read = mcftmr_read_clk,
Greg Ungerera7f61fa2008-02-01 17:40:26 +100093 .mask = CLOCKSOURCE_MASK(32),
94 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
95};
96
97/***************************************************************************/
Greg Ungerer4342f4a2007-06-08 13:46:39 -070098
Greg Ungerer35aefb22012-01-23 15:34:58 +100099void hw_timer_init(irq_handler_t handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000101 __raw_writew(MCFTIMER_TMR_DISABLE, TA(MCFTIMER_TMR));
Greg Ungerera7f61fa2008-02-01 17:40:26 +1000102 mcftmr_cycles_per_jiffy = FREQ / HZ;
Philippe De Muyter6c38d852008-06-12 15:21:36 -0700103 /*
104 * The coldfire timer runs from 0 to TRR included, then 0
105 * again and so on. It counts thus actually TRR + 1 steps
106 * for 1 tick, not TRR. So if you want n cycles,
107 * initialize TRR with n - 1.
108 */
109 __raw_writetrr(mcftmr_cycles_per_jiffy - 1, TA(MCFTIMER_TRR));
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000110 __raw_writew(MCFTIMER_TMR_ENORI | MCFTIMER_TMR_CLK16 |
111 MCFTIMER_TMR_RESTART | MCFTIMER_TMR_ENABLE, TA(MCFTIMER_TMR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
John Stultz010f3f12010-04-26 20:21:52 -0700113 clocksource_register_hz(&mcftmr_clk, FREQ);
Greg Ungerera7f61fa2008-02-01 17:40:26 +1000114
Greg Ungerer35aefb22012-01-23 15:34:58 +1000115 timer_interrupt = handler;
Greg Ungerer3945ca0f2009-05-22 13:50:53 +1000116 setup_irq(MCF_IRQ_TIMER, &mcftmr_timer_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118#ifdef CONFIG_HIGHPROFILE
119 coldfire_profile_init();
120#endif
121}
122
123/***************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#ifdef CONFIG_HIGHPROFILE
125/***************************************************************************/
126
127/*
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000128 * By default use timer2 as the profiler clock timer.
129 */
Greg Ungerer58f0ac92011-03-09 09:57:14 +1000130#define PA(a) (MCFTIMER_BASE2 + (a))
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000131
132/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 * Choose a reasonably fast profile timer. Make it an odd value to
Robert P. J. Dayd08df602007-02-17 19:07:33 +0100134 * try and get good coverage of kernel operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 */
136#define PROFILEHZ 1013
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/*
139 * Use the other timer to provide high accuracy profiling info.
140 */
Greg Ungererc051b012007-02-07 12:03:01 +1000141irqreturn_t coldfire_profile_tick(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 /* Reset ColdFire timer2 */
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000144 __raw_writeb(MCFTIMER_TER_CAP | MCFTIMER_TER_REF, PA(MCFTIMER_TER));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (current->pid)
Matt Waddel6ef1e562008-02-14 19:31:27 -0800146 profile_tick(CPU_PROFILING);
Greg Ungererc051b012007-02-07 12:03:01 +1000147 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
150/***************************************************************************/
151
Matt Waddel6ef1e562008-02-14 19:31:27 -0800152static struct irqaction coldfire_profile_irq = {
153 .name = "profile timer",
154 .flags = IRQF_DISABLED | IRQF_TIMER,
155 .handler = coldfire_profile_tick,
156};
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158void coldfire_profile_init(void)
159{
Matt Waddel6ef1e562008-02-14 19:31:27 -0800160 printk(KERN_INFO "PROFILE: lodging TIMER2 @ %dHz as profile timer\n",
161 PROFILEHZ);
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 /* Set up TIMER 2 as high speed profile clock */
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000164 __raw_writew(MCFTIMER_TMR_DISABLE, PA(MCFTIMER_TMR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Matt Waddel6ef1e562008-02-14 19:31:27 -0800166 __raw_writetrr(((MCF_BUSCLK / 16) / PROFILEHZ), PA(MCFTIMER_TRR));
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000167 __raw_writew(MCFTIMER_TMR_ENORI | MCFTIMER_TMR_CLK16 |
168 MCFTIMER_TMR_RESTART | MCFTIMER_TMR_ENABLE, PA(MCFTIMER_TMR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Greg Ungerer3945ca0f2009-05-22 13:50:53 +1000170 setup_irq(MCF_IRQ_PROFILER, &coldfire_profile_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173/***************************************************************************/
174#endif /* CONFIG_HIGHPROFILE */
175/***************************************************************************/