162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * dma_timer.c -- Freescale ColdFire DMA Timer.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2007, Benedikt Spranger <b.spranger@linutronix.de>
662306a36Sopenharmony_ci * Copyright (C) 2008. Sebastian Siewior, Linutronix
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <linux/clocksource.h>
1162306a36Sopenharmony_ci#include <linux/io.h>
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#include <asm/machdep.h>
1462306a36Sopenharmony_ci#include <asm/coldfire.h>
1562306a36Sopenharmony_ci#include <asm/mcfpit.h>
1662306a36Sopenharmony_ci#include <asm/mcfsim.h>
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci#define DMA_TIMER_0	(0x00)
1962306a36Sopenharmony_ci#define DMA_TIMER_1	(0x40)
2062306a36Sopenharmony_ci#define DMA_TIMER_2	(0x80)
2162306a36Sopenharmony_ci#define DMA_TIMER_3	(0xc0)
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci#define DTMR0	(MCF_IPSBAR + DMA_TIMER_0 + 0x400)
2462306a36Sopenharmony_ci#define DTXMR0	(MCF_IPSBAR + DMA_TIMER_0 + 0x402)
2562306a36Sopenharmony_ci#define DTER0	(MCF_IPSBAR + DMA_TIMER_0 + 0x403)
2662306a36Sopenharmony_ci#define DTRR0	(MCF_IPSBAR + DMA_TIMER_0 + 0x404)
2762306a36Sopenharmony_ci#define DTCR0	(MCF_IPSBAR + DMA_TIMER_0 + 0x408)
2862306a36Sopenharmony_ci#define DTCN0	(MCF_IPSBAR + DMA_TIMER_0 + 0x40c)
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci#define DMA_FREQ    ((MCF_CLK / 2) / 16)
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci/* DTMR */
3362306a36Sopenharmony_ci#define DMA_DTMR_RESTART	(1 << 3)
3462306a36Sopenharmony_ci#define DMA_DTMR_CLK_DIV_1	(1 << 1)
3562306a36Sopenharmony_ci#define DMA_DTMR_CLK_DIV_16	(2 << 1)
3662306a36Sopenharmony_ci#define DMA_DTMR_ENABLE		(1 << 0)
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_cistatic u64 cf_dt_get_cycles(struct clocksource *cs)
3962306a36Sopenharmony_ci{
4062306a36Sopenharmony_ci	return __raw_readl(DTCN0);
4162306a36Sopenharmony_ci}
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_cistatic struct clocksource clocksource_cf_dt = {
4462306a36Sopenharmony_ci	.name		= "coldfire_dma_timer",
4562306a36Sopenharmony_ci	.rating		= 200,
4662306a36Sopenharmony_ci	.read		= cf_dt_get_cycles,
4762306a36Sopenharmony_ci	.mask		= CLOCKSOURCE_MASK(32),
4862306a36Sopenharmony_ci	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
4962306a36Sopenharmony_ci};
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_cistatic int __init init_cf_dt_clocksource(void)
5262306a36Sopenharmony_ci{
5362306a36Sopenharmony_ci	/*
5462306a36Sopenharmony_ci	 * We setup DMA timer 0 in free run mode. This incrementing counter is
5562306a36Sopenharmony_ci	 * used as a highly precious clock source. With MCF_CLOCK = 150 MHz we
5662306a36Sopenharmony_ci	 * get a ~213 ns resolution and the 32bit register will overflow almost
5762306a36Sopenharmony_ci	 * every 15 minutes.
5862306a36Sopenharmony_ci	 */
5962306a36Sopenharmony_ci	__raw_writeb(0x00, DTXMR0);
6062306a36Sopenharmony_ci	__raw_writeb(0x00, DTER0);
6162306a36Sopenharmony_ci	__raw_writel(0x00000000, DTRR0);
6262306a36Sopenharmony_ci	__raw_writew(DMA_DTMR_CLK_DIV_16 | DMA_DTMR_ENABLE, DTMR0);
6362306a36Sopenharmony_ci	return clocksource_register_hz(&clocksource_cf_dt, DMA_FREQ);
6462306a36Sopenharmony_ci}
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ciarch_initcall(init_cf_dt_clocksource);
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci#define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
6962306a36Sopenharmony_ci#define CYC2NS_SCALE	((1000000 << CYC2NS_SCALE_FACTOR) / (DMA_FREQ / 1000))
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_cistatic unsigned long long cycles2ns(unsigned long cycl)
7262306a36Sopenharmony_ci{
7362306a36Sopenharmony_ci	return (unsigned long long) ((unsigned long long)cycl *
7462306a36Sopenharmony_ci			CYC2NS_SCALE) >> CYC2NS_SCALE_FACTOR;
7562306a36Sopenharmony_ci}
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ciunsigned long long sched_clock(void)
7862306a36Sopenharmony_ci{
7962306a36Sopenharmony_ci	unsigned long cycl = __raw_readl(DTCN0);
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci	return cycles2ns(cycl);
8262306a36Sopenharmony_ci}
83