blob: 77b0c17ce6ba3b1e32774a9e70ce97b93cee200f [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 * These provide the underlying interrupt vector support.
35 * Unfortunately it is a little different on each ColdFire.
36 */
37extern void mcf_settimericr(int timer, int level);
Greg Ungerera7f61fa2008-02-01 17:40:26 +100038void coldfire_profile_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Greg Ungererdeb77c82006-12-06 11:36:59 +100040#if defined(CONFIG_M532x)
41#define __raw_readtrr __raw_readl
42#define __raw_writetrr __raw_writel
43#else
44#define __raw_readtrr __raw_readw
45#define __raw_writetrr __raw_writew
46#endif
47
Greg Ungerera7f61fa2008-02-01 17:40:26 +100048static u32 mcftmr_cycles_per_jiffy;
49static u32 mcftmr_cnt;
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/***************************************************************************/
52
Greg Ungerera7f61fa2008-02-01 17:40:26 +100053static irqreturn_t mcftmr_tick(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 /* Reset the ColdFire timer */
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +100056 __raw_writeb(MCFTIMER_TER_CAP | MCFTIMER_TER_REF, TA(MCFTIMER_TER));
Greg Ungerer2f2c2672007-10-23 14:37:54 +100057
Greg Ungerera7f61fa2008-02-01 17:40:26 +100058 mcftmr_cnt += mcftmr_cycles_per_jiffy;
Greg Ungerer2f2c2672007-10-23 14:37:54 +100059 return arch_timer_interrupt(irq, dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
62/***************************************************************************/
63
Greg Ungerera7f61fa2008-02-01 17:40:26 +100064static struct irqaction mcftmr_timer_irq = {
Greg Ungerer2f2c2672007-10-23 14:37:54 +100065 .name = "timer",
66 .flags = IRQF_DISABLED | IRQF_TIMER,
Greg Ungerera7f61fa2008-02-01 17:40:26 +100067 .handler = mcftmr_tick,
Greg Ungererc52a2cd2007-07-27 01:09:00 +100068};
69
Greg Ungerer2f2c2672007-10-23 14:37:54 +100070/***************************************************************************/
71
Magnus Damm8e196082009-04-21 12:24:00 -070072static cycle_t mcftmr_read_clk(struct clocksource *cs)
Greg Ungerera7f61fa2008-02-01 17:40:26 +100073{
74 unsigned long flags;
75 u32 cycles;
76 u16 tcn;
77
78 local_irq_save(flags);
79 tcn = __raw_readw(TA(MCFTIMER_TCN));
80 cycles = mcftmr_cnt;
81 local_irq_restore(flags);
82
83 return cycles + tcn;
84}
85
86/***************************************************************************/
87
88static struct clocksource mcftmr_clk = {
89 .name = "tmr",
90 .rating = 250,
91 .read = mcftmr_read_clk,
92 .shift = 20,
93 .mask = CLOCKSOURCE_MASK(32),
94 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
95};
96
97/***************************************************************************/
Greg Ungerer4342f4a2007-06-08 13:46:39 -070098
Greg Ungerer2f2c2672007-10-23 14:37:54 +100099void hw_timer_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Greg Ungerer04b75b12009-05-19 14:52:40 +1000101 setup_irq(MCF_IRQ_TIMER, &mcftmr_timer_irq);
Greg Ungererc52a2cd2007-07-27 01:09:00 +1000102
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000103 __raw_writew(MCFTIMER_TMR_DISABLE, TA(MCFTIMER_TMR));
Greg Ungerera7f61fa2008-02-01 17:40:26 +1000104 mcftmr_cycles_per_jiffy = FREQ / HZ;
Philippe De Muyter6c38d852008-06-12 15:21:36 -0700105 /*
106 * The coldfire timer runs from 0 to TRR included, then 0
107 * again and so on. It counts thus actually TRR + 1 steps
108 * for 1 tick, not TRR. So if you want n cycles,
109 * initialize TRR with n - 1.
110 */
111 __raw_writetrr(mcftmr_cycles_per_jiffy - 1, TA(MCFTIMER_TRR));
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000112 __raw_writew(MCFTIMER_TMR_ENORI | MCFTIMER_TMR_CLK16 |
113 MCFTIMER_TMR_RESTART | MCFTIMER_TMR_ENABLE, TA(MCFTIMER_TMR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Greg Ungerera7f61fa2008-02-01 17:40:26 +1000115 mcftmr_clk.mult = clocksource_hz2mult(FREQ, mcftmr_clk.shift);
116 clocksource_register(&mcftmr_clk);
117
Greg Ungerer04b75b12009-05-19 14:52:40 +1000118 mcf_clrimr(MCFINTC_TIMER1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120#ifdef CONFIG_HIGHPROFILE
121 coldfire_profile_init();
122#endif
123}
124
125/***************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#ifdef CONFIG_HIGHPROFILE
127/***************************************************************************/
128
129/*
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000130 * By default use timer2 as the profiler clock timer.
131 */
132#define PA(a) (MCF_MBAR + MCFTIMER_BASE2 + (a))
133
134/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 * Choose a reasonably fast profile timer. Make it an odd value to
Robert P. J. Dayd08df602007-02-17 19:07:33 +0100136 * try and get good coverage of kernel operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 */
138#define PROFILEHZ 1013
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/*
141 * Use the other timer to provide high accuracy profiling info.
142 */
Greg Ungererc051b012007-02-07 12:03:01 +1000143irqreturn_t coldfire_profile_tick(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 /* Reset ColdFire timer2 */
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000146 __raw_writeb(MCFTIMER_TER_CAP | MCFTIMER_TER_REF, PA(MCFTIMER_TER));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (current->pid)
Matt Waddel6ef1e562008-02-14 19:31:27 -0800148 profile_tick(CPU_PROFILING);
Greg Ungererc051b012007-02-07 12:03:01 +1000149 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
152/***************************************************************************/
153
Matt Waddel6ef1e562008-02-14 19:31:27 -0800154static struct irqaction coldfire_profile_irq = {
155 .name = "profile timer",
156 .flags = IRQF_DISABLED | IRQF_TIMER,
157 .handler = coldfire_profile_tick,
158};
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160void coldfire_profile_init(void)
161{
Matt Waddel6ef1e562008-02-14 19:31:27 -0800162 printk(KERN_INFO "PROFILE: lodging TIMER2 @ %dHz as profile timer\n",
163 PROFILEHZ);
164
Greg Ungerer04b75b12009-05-19 14:52:40 +1000165 setup_irq(MCF_IRQ_PROFILER, &coldfire_profile_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 /* Set up TIMER 2 as high speed profile clock */
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000168 __raw_writew(MCFTIMER_TMR_DISABLE, PA(MCFTIMER_TMR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Matt Waddel6ef1e562008-02-14 19:31:27 -0800170 __raw_writetrr(((MCF_BUSCLK / 16) / PROFILEHZ), PA(MCFTIMER_TRR));
Greg Ungerer0b7ac8e2006-06-26 10:33:10 +1000171 __raw_writew(MCFTIMER_TMR_ENORI | MCFTIMER_TMR_CLK16 |
172 MCFTIMER_TMR_RESTART | MCFTIMER_TMR_ENABLE, PA(MCFTIMER_TMR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Greg Ungerer04b75b12009-05-19 14:52:40 +1000174 mcf_clrimr(MCFINTC_TIMER2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
177/***************************************************************************/
178#endif /* CONFIG_HIGHPROFILE */
179/***************************************************************************/