18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * dma_timer.c -- Freescale ColdFire DMA Timer.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2007, Benedikt Spranger <b.spranger@linutronix.de>
68c2ecf20Sopenharmony_ci * Copyright (C) 2008. Sebastian Siewior, Linutronix
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/clocksource.h>
118c2ecf20Sopenharmony_ci#include <linux/io.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <asm/machdep.h>
148c2ecf20Sopenharmony_ci#include <asm/coldfire.h>
158c2ecf20Sopenharmony_ci#include <asm/mcfpit.h>
168c2ecf20Sopenharmony_ci#include <asm/mcfsim.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define DMA_TIMER_0	(0x00)
198c2ecf20Sopenharmony_ci#define DMA_TIMER_1	(0x40)
208c2ecf20Sopenharmony_ci#define DMA_TIMER_2	(0x80)
218c2ecf20Sopenharmony_ci#define DMA_TIMER_3	(0xc0)
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define DTMR0	(MCF_IPSBAR + DMA_TIMER_0 + 0x400)
248c2ecf20Sopenharmony_ci#define DTXMR0	(MCF_IPSBAR + DMA_TIMER_0 + 0x402)
258c2ecf20Sopenharmony_ci#define DTER0	(MCF_IPSBAR + DMA_TIMER_0 + 0x403)
268c2ecf20Sopenharmony_ci#define DTRR0	(MCF_IPSBAR + DMA_TIMER_0 + 0x404)
278c2ecf20Sopenharmony_ci#define DTCR0	(MCF_IPSBAR + DMA_TIMER_0 + 0x408)
288c2ecf20Sopenharmony_ci#define DTCN0	(MCF_IPSBAR + DMA_TIMER_0 + 0x40c)
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define DMA_FREQ    ((MCF_CLK / 2) / 16)
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/* DTMR */
338c2ecf20Sopenharmony_ci#define DMA_DTMR_RESTART	(1 << 3)
348c2ecf20Sopenharmony_ci#define DMA_DTMR_CLK_DIV_1	(1 << 1)
358c2ecf20Sopenharmony_ci#define DMA_DTMR_CLK_DIV_16	(2 << 1)
368c2ecf20Sopenharmony_ci#define DMA_DTMR_ENABLE		(1 << 0)
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic u64 cf_dt_get_cycles(struct clocksource *cs)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	return __raw_readl(DTCN0);
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic struct clocksource clocksource_cf_dt = {
448c2ecf20Sopenharmony_ci	.name		= "coldfire_dma_timer",
458c2ecf20Sopenharmony_ci	.rating		= 200,
468c2ecf20Sopenharmony_ci	.read		= cf_dt_get_cycles,
478c2ecf20Sopenharmony_ci	.mask		= CLOCKSOURCE_MASK(32),
488c2ecf20Sopenharmony_ci	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
498c2ecf20Sopenharmony_ci};
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic int __init  init_cf_dt_clocksource(void)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	/*
548c2ecf20Sopenharmony_ci	 * We setup DMA timer 0 in free run mode. This incrementing counter is
558c2ecf20Sopenharmony_ci	 * used as a highly precious clock source. With MCF_CLOCK = 150 MHz we
568c2ecf20Sopenharmony_ci	 * get a ~213 ns resolution and the 32bit register will overflow almost
578c2ecf20Sopenharmony_ci	 * every 15 minutes.
588c2ecf20Sopenharmony_ci	 */
598c2ecf20Sopenharmony_ci	__raw_writeb(0x00, DTXMR0);
608c2ecf20Sopenharmony_ci	__raw_writeb(0x00, DTER0);
618c2ecf20Sopenharmony_ci	__raw_writel(0x00000000, DTRR0);
628c2ecf20Sopenharmony_ci	__raw_writew(DMA_DTMR_CLK_DIV_16 | DMA_DTMR_ENABLE, DTMR0);
638c2ecf20Sopenharmony_ci	return clocksource_register_hz(&clocksource_cf_dt, DMA_FREQ);
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ciarch_initcall(init_cf_dt_clocksource);
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
698c2ecf20Sopenharmony_ci#define CYC2NS_SCALE	((1000000 << CYC2NS_SCALE_FACTOR) / (DMA_FREQ / 1000))
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistatic unsigned long long cycles2ns(unsigned long cycl)
728c2ecf20Sopenharmony_ci{
738c2ecf20Sopenharmony_ci	return (unsigned long long) ((unsigned long long)cycl *
748c2ecf20Sopenharmony_ci			CYC2NS_SCALE) >> CYC2NS_SCALE_FACTOR;
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ciunsigned long long sched_clock(void)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	unsigned long cycl = __raw_readl(DTCN0);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	return cycles2ns(cycl);
828c2ecf20Sopenharmony_ci}
83