1ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter/***************************************************************************/
2ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter
3ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter/*
4ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter *	sltimers.c -- generic ColdFire slice timer support.
5ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter *
6ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter *	Copyright (C) 2009-2010, Philippe De Muyter <phdm@macqel.be>
7ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter *	based on
8ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter *	timers.c -- generic ColdFire hardware timer support.
9ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter *	Copyright (C) 1999-2008, Greg Ungerer <gerg@snapgear.com>
10ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter */
11ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter
12ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter/***************************************************************************/
13ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter
14ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter#include <linux/kernel.h>
15ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter#include <linux/init.h>
16ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter#include <linux/sched.h>
17ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter#include <linux/interrupt.h>
18ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter#include <linux/irq.h>
19ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter#include <linux/profile.h>
20ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter#include <linux/clocksource.h>
21ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter#include <asm/io.h>
22ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter#include <asm/traps.h>
23ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter#include <asm/machdep.h>
24ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter#include <asm/coldfire.h>
25ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter#include <asm/mcfslt.h>
26ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter#include <asm/mcfsim.h>
27ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter
28ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter/***************************************************************************/
29ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter
30ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter#ifdef CONFIG_HIGHPROFILE
31ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter
32ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter/*
33ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter *	By default use Slice Timer 1 as the profiler clock timer.
34ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter */
35f2f41c68eabfb32574f9088135480618206dd432Greg Ungerer#define	PA(a)	(MCFSLT_TIMER1 + (a))
36ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter
37ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter/*
38ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter *	Choose a reasonably fast profile timer. Make it an odd value to
39ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter *	try and get good coverage of kernel operations.
40ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter */
41ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter#define	PROFILEHZ	1013
42ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter
43ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyterirqreturn_t mcfslt_profile_tick(int irq, void *dummy)
44ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter{
45ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	/* Reset Slice Timer 1 */
46ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	__raw_writel(MCFSLT_SSR_BE | MCFSLT_SSR_TE, PA(MCFSLT_SSR));
47ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	if (current->pid)
48ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter		profile_tick(CPU_PROFILING);
49ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	return IRQ_HANDLED;
50ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter}
51ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter
52ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyterstatic struct irqaction mcfslt_profile_irq = {
53ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	.name	 = "profile timer",
5477a42796786c2ea2d3a67262fb5f1f707c3e0594Michael Opdenacker	.flags	 = IRQF_TIMER,
55ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	.handler = mcfslt_profile_tick,
56ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter};
57ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter
58ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muytervoid mcfslt_profile_init(void)
59ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter{
60ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	printk(KERN_INFO "PROFILE: lodging TIMER 1 @ %dHz as profile timer\n",
61ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	       PROFILEHZ);
62ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter
63ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	setup_irq(MCF_IRQ_PROFILER, &mcfslt_profile_irq);
64ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter
65ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	/* Set up TIMER 2 as high speed profile clock */
66ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	__raw_writel(MCF_BUSCLK / PROFILEHZ - 1, PA(MCFSLT_STCNT));
67ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	__raw_writel(MCFSLT_SCR_RUN | MCFSLT_SCR_IEN | MCFSLT_SCR_TEN,
68ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter								PA(MCFSLT_SCR));
69ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter
70ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter}
71ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter
72ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter#endif	/* CONFIG_HIGHPROFILE */
73ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter
74ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter/***************************************************************************/
75ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter
76ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter/*
77ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter *	By default use Slice Timer 0 as the system clock timer.
78ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter */
79f2f41c68eabfb32574f9088135480618206dd432Greg Ungerer#define	TA(a)	(MCFSLT_TIMER0 + (a))
80ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter
81ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyterstatic u32 mcfslt_cycles_per_jiffy;
82ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyterstatic u32 mcfslt_cnt;
83ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter
8435aefb2645d1ae7576699b2d7b66d6c9503113fcGreg Ungererstatic irq_handler_t timer_interrupt;
8535aefb2645d1ae7576699b2d7b66d6c9503113fcGreg Ungerer
86ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyterstatic irqreturn_t mcfslt_tick(int irq, void *dummy)
87ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter{
88ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	/* Reset Slice Timer 0 */
89ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	__raw_writel(MCFSLT_SSR_BE | MCFSLT_SSR_TE, TA(MCFSLT_SSR));
90ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	mcfslt_cnt += mcfslt_cycles_per_jiffy;
9135aefb2645d1ae7576699b2d7b66d6c9503113fcGreg Ungerer	return timer_interrupt(irq, dummy);
92ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter}
93ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter
94ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyterstatic struct irqaction mcfslt_timer_irq = {
95ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	.name	 = "timer",
9677a42796786c2ea2d3a67262fb5f1f707c3e0594Michael Opdenacker	.flags	 = IRQF_TIMER,
97ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	.handler = mcfslt_tick,
98ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter};
99ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter
100ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyterstatic cycle_t mcfslt_read_clk(struct clocksource *cs)
101ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter{
102ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	unsigned long flags;
1031f2aab01ba2f6c591dee93daf4b57fd9785f3b41Greg Ungerer	u32 cycles, scnt;
104ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter
105ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	local_irq_save(flags);
106ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	scnt = __raw_readl(TA(MCFSLT_SCNT));
107ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	cycles = mcfslt_cnt;
1081f2aab01ba2f6c591dee93daf4b57fd9785f3b41Greg Ungerer	if (__raw_readl(TA(MCFSLT_SSR)) & MCFSLT_SSR_TE) {
1091f2aab01ba2f6c591dee93daf4b57fd9785f3b41Greg Ungerer		cycles += mcfslt_cycles_per_jiffy;
1101f2aab01ba2f6c591dee93daf4b57fd9785f3b41Greg Ungerer		scnt = __raw_readl(TA(MCFSLT_SCNT));
1111f2aab01ba2f6c591dee93daf4b57fd9785f3b41Greg Ungerer	}
112ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	local_irq_restore(flags);
113ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter
11425985edcedea6396277003854657b5f3cb31a628Lucas De Marchi	/* subtract because slice timers count down */
1151f2aab01ba2f6c591dee93daf4b57fd9785f3b41Greg Ungerer	return cycles + ((mcfslt_cycles_per_jiffy - 1) - scnt);
116ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter}
117ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter
118ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyterstatic struct clocksource mcfslt_clk = {
119ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	.name	= "slt",
120ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	.rating	= 250,
121ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	.read	= mcfslt_read_clk,
122ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	.mask	= CLOCKSOURCE_MASK(32),
123ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	.flags	= CLOCK_SOURCE_IS_CONTINUOUS,
124ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter};
125ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter
12635aefb2645d1ae7576699b2d7b66d6c9503113fcGreg Ungerervoid hw_timer_init(irq_handler_t handler)
127ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter{
128ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	mcfslt_cycles_per_jiffy = MCF_BUSCLK / HZ;
129ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	/*
130ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	 *	The coldfire slice timer (SLT) runs from STCNT to 0 included,
131ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	 *	then STCNT again and so on.  It counts thus actually
132ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	 *	STCNT + 1 steps for 1 tick, not STCNT.  So if you want
133ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	 *	n cycles, initialize STCNT with n - 1.
134ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	 */
135ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	__raw_writel(mcfslt_cycles_per_jiffy - 1, TA(MCFSLT_STCNT));
136ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	__raw_writel(MCFSLT_SCR_RUN | MCFSLT_SCR_IEN | MCFSLT_SCR_TEN,
137ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter								TA(MCFSLT_SCR));
138ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	/* initialize mcfslt_cnt knowing that slice timers count down */
139ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	mcfslt_cnt = mcfslt_cycles_per_jiffy;
140ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter
14135aefb2645d1ae7576699b2d7b66d6c9503113fcGreg Ungerer	timer_interrupt = handler;
142ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	setup_irq(MCF_IRQ_TIMER, &mcfslt_timer_irq);
143ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter
144a2a3dfb8efc5d14cf39358ae0ec1da39667c2e6cJohn Stultz	clocksource_register_hz(&mcfslt_clk, MCF_BUSCLK);
145ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter
146ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter#ifdef CONFIG_HIGHPROFILE
147ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter	mcfslt_profile_init();
148ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter#endif
149ea49f8ffae6262e8de9a0d3e9fcdd384156c7e05Philippe De Muyter}
150