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