18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/***************************************************************************/
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci/*
58c2ecf20Sopenharmony_ci *	sltimers.c -- generic ColdFire slice timer support.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *	Copyright (C) 2009-2010, Philippe De Muyter <phdm@macqel.be>
88c2ecf20Sopenharmony_ci *	based on
98c2ecf20Sopenharmony_ci *	timers.c -- generic ColdFire hardware timer support.
108c2ecf20Sopenharmony_ci *	Copyright (C) 1999-2008, Greg Ungerer <gerg@snapgear.com>
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci/***************************************************************************/
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/kernel.h>
168c2ecf20Sopenharmony_ci#include <linux/init.h>
178c2ecf20Sopenharmony_ci#include <linux/sched.h>
188c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
198c2ecf20Sopenharmony_ci#include <linux/irq.h>
208c2ecf20Sopenharmony_ci#include <linux/profile.h>
218c2ecf20Sopenharmony_ci#include <linux/clocksource.h>
228c2ecf20Sopenharmony_ci#include <asm/io.h>
238c2ecf20Sopenharmony_ci#include <asm/traps.h>
248c2ecf20Sopenharmony_ci#include <asm/machdep.h>
258c2ecf20Sopenharmony_ci#include <asm/coldfire.h>
268c2ecf20Sopenharmony_ci#include <asm/mcfslt.h>
278c2ecf20Sopenharmony_ci#include <asm/mcfsim.h>
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/***************************************************************************/
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#ifdef CONFIG_HIGHPROFILE
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/*
348c2ecf20Sopenharmony_ci *	By default use Slice Timer 1 as the profiler clock timer.
358c2ecf20Sopenharmony_ci */
368c2ecf20Sopenharmony_ci#define	PA(a)	(MCFSLT_TIMER1 + (a))
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci/*
398c2ecf20Sopenharmony_ci *	Choose a reasonably fast profile timer. Make it an odd value to
408c2ecf20Sopenharmony_ci *	try and get good coverage of kernel operations.
418c2ecf20Sopenharmony_ci */
428c2ecf20Sopenharmony_ci#define	PROFILEHZ	1013
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ciirqreturn_t mcfslt_profile_tick(int irq, void *dummy)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	/* Reset Slice Timer 1 */
478c2ecf20Sopenharmony_ci	__raw_writel(MCFSLT_SSR_BE | MCFSLT_SSR_TE, PA(MCFSLT_SSR));
488c2ecf20Sopenharmony_ci	if (current->pid)
498c2ecf20Sopenharmony_ci		profile_tick(CPU_PROFILING);
508c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_civoid mcfslt_profile_init(void)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	int ret;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	printk(KERN_INFO "PROFILE: lodging TIMER 1 @ %dHz as profile timer\n",
588c2ecf20Sopenharmony_ci	       PROFILEHZ);
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	ret = request_irq(MCF_IRQ_PROFILER, mcfslt_profile_tick, IRQF_TIMER,
618c2ecf20Sopenharmony_ci			  "profile timer", NULL);
628c2ecf20Sopenharmony_ci	if (ret) {
638c2ecf20Sopenharmony_ci		pr_err("Failed to request irq %d (profile timer): %pe\n",
648c2ecf20Sopenharmony_ci		       MCF_IRQ_PROFILER, ERR_PTR(ret));
658c2ecf20Sopenharmony_ci	}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	/* Set up TIMER 2 as high speed profile clock */
688c2ecf20Sopenharmony_ci	__raw_writel(MCF_BUSCLK / PROFILEHZ - 1, PA(MCFSLT_STCNT));
698c2ecf20Sopenharmony_ci	__raw_writel(MCFSLT_SCR_RUN | MCFSLT_SCR_IEN | MCFSLT_SCR_TEN,
708c2ecf20Sopenharmony_ci								PA(MCFSLT_SCR));
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci#endif	/* CONFIG_HIGHPROFILE */
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci/***************************************************************************/
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci/*
798c2ecf20Sopenharmony_ci *	By default use Slice Timer 0 as the system clock timer.
808c2ecf20Sopenharmony_ci */
818c2ecf20Sopenharmony_ci#define	TA(a)	(MCFSLT_TIMER0 + (a))
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistatic u32 mcfslt_cycles_per_jiffy;
848c2ecf20Sopenharmony_cistatic u32 mcfslt_cnt;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic irq_handler_t timer_interrupt;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic irqreturn_t mcfslt_tick(int irq, void *dummy)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	/* Reset Slice Timer 0 */
918c2ecf20Sopenharmony_ci	__raw_writel(MCFSLT_SSR_BE | MCFSLT_SSR_TE, TA(MCFSLT_SSR));
928c2ecf20Sopenharmony_ci	mcfslt_cnt += mcfslt_cycles_per_jiffy;
938c2ecf20Sopenharmony_ci	return timer_interrupt(irq, dummy);
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistatic u64 mcfslt_read_clk(struct clocksource *cs)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	unsigned long flags;
998c2ecf20Sopenharmony_ci	u32 cycles, scnt;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	local_irq_save(flags);
1028c2ecf20Sopenharmony_ci	scnt = __raw_readl(TA(MCFSLT_SCNT));
1038c2ecf20Sopenharmony_ci	cycles = mcfslt_cnt;
1048c2ecf20Sopenharmony_ci	if (__raw_readl(TA(MCFSLT_SSR)) & MCFSLT_SSR_TE) {
1058c2ecf20Sopenharmony_ci		cycles += mcfslt_cycles_per_jiffy;
1068c2ecf20Sopenharmony_ci		scnt = __raw_readl(TA(MCFSLT_SCNT));
1078c2ecf20Sopenharmony_ci	}
1088c2ecf20Sopenharmony_ci	local_irq_restore(flags);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	/* subtract because slice timers count down */
1118c2ecf20Sopenharmony_ci	return cycles + ((mcfslt_cycles_per_jiffy - 1) - scnt);
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic struct clocksource mcfslt_clk = {
1158c2ecf20Sopenharmony_ci	.name	= "slt",
1168c2ecf20Sopenharmony_ci	.rating	= 250,
1178c2ecf20Sopenharmony_ci	.read	= mcfslt_read_clk,
1188c2ecf20Sopenharmony_ci	.mask	= CLOCKSOURCE_MASK(32),
1198c2ecf20Sopenharmony_ci	.flags	= CLOCK_SOURCE_IS_CONTINUOUS,
1208c2ecf20Sopenharmony_ci};
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_civoid hw_timer_init(irq_handler_t handler)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	int r;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	mcfslt_cycles_per_jiffy = MCF_BUSCLK / HZ;
1278c2ecf20Sopenharmony_ci	/*
1288c2ecf20Sopenharmony_ci	 *	The coldfire slice timer (SLT) runs from STCNT to 0 included,
1298c2ecf20Sopenharmony_ci	 *	then STCNT again and so on.  It counts thus actually
1308c2ecf20Sopenharmony_ci	 *	STCNT + 1 steps for 1 tick, not STCNT.  So if you want
1318c2ecf20Sopenharmony_ci	 *	n cycles, initialize STCNT with n - 1.
1328c2ecf20Sopenharmony_ci	 */
1338c2ecf20Sopenharmony_ci	__raw_writel(mcfslt_cycles_per_jiffy - 1, TA(MCFSLT_STCNT));
1348c2ecf20Sopenharmony_ci	__raw_writel(MCFSLT_SCR_RUN | MCFSLT_SCR_IEN | MCFSLT_SCR_TEN,
1358c2ecf20Sopenharmony_ci								TA(MCFSLT_SCR));
1368c2ecf20Sopenharmony_ci	/* initialize mcfslt_cnt knowing that slice timers count down */
1378c2ecf20Sopenharmony_ci	mcfslt_cnt = mcfslt_cycles_per_jiffy;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	timer_interrupt = handler;
1408c2ecf20Sopenharmony_ci	r = request_irq(MCF_IRQ_TIMER, mcfslt_tick, IRQF_TIMER, "timer", NULL);
1418c2ecf20Sopenharmony_ci	if (r) {
1428c2ecf20Sopenharmony_ci		pr_err("Failed to request irq %d (timer): %pe\n", MCF_IRQ_TIMER,
1438c2ecf20Sopenharmony_ci		       ERR_PTR(r));
1448c2ecf20Sopenharmony_ci	}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	clocksource_register_hz(&mcfslt_clk, MCF_BUSCLK);
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci#ifdef CONFIG_HIGHPROFILE
1498c2ecf20Sopenharmony_ci	mcfslt_profile_init();
1508c2ecf20Sopenharmony_ci#endif
1518c2ecf20Sopenharmony_ci}
152