2019-06-04 08:11:33 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2009-01-20 06:15:18 +00:00
|
|
|
/*
|
|
|
|
* linux/arch/arm/mach-mmp/time.c
|
|
|
|
*
|
|
|
|
* Support for clocksource and clockevents
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Marvell International Ltd.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 2008-04-11: Jason Chagas <Jason.chagas@marvell.com>
|
|
|
|
* 2008-10-08: Bin Yang <bin.yang@marvell.com>
|
|
|
|
*
|
2011-03-31 01:57:33 +00:00
|
|
|
* The timers module actually includes three timers, each timer with up to
|
2009-01-20 06:15:18 +00:00
|
|
|
* three match comparators. Timer #0 is used here in free-running mode as
|
|
|
|
* the clock source, and match comparator #1 used as clock event device.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/clockchips.h>
|
2018-11-28 17:53:20 +00:00
|
|
|
#include <linux/clk.h>
|
2009-01-20 06:15:18 +00:00
|
|
|
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/irq.h>
|
2012-04-12 11:05:40 +00:00
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/of_address.h>
|
|
|
|
#include <linux/of_irq.h>
|
2013-06-02 06:39:40 +00:00
|
|
|
#include <linux/sched_clock.h>
|
2009-12-04 14:41:28 +00:00
|
|
|
#include <asm/mach/time.h>
|
2009-01-20 06:15:18 +00:00
|
|
|
|
2014-04-15 18:38:32 +00:00
|
|
|
#include "addr-map.h"
|
|
|
|
#include "regs-timers.h"
|
|
|
|
#include "regs-apbc.h"
|
|
|
|
#include "irqs.h"
|
|
|
|
#include "cputype.h"
|
2009-01-20 06:15:18 +00:00
|
|
|
#include "clock.h"
|
|
|
|
|
|
|
|
#define TIMERS_VIRT_BASE TIMERS1_VIRT_BASE
|
|
|
|
|
|
|
|
#define MAX_DELTA (0xfffffffe)
|
|
|
|
#define MIN_DELTA (16)
|
|
|
|
|
2012-04-12 11:05:40 +00:00
|
|
|
static void __iomem *mmp_timer_base = TIMERS_VIRT_BASE;
|
|
|
|
|
2009-01-20 06:15:18 +00:00
|
|
|
/*
|
|
|
|
* FIXME: the timer needs some delay to stablize the counter capture
|
|
|
|
*/
|
|
|
|
static inline uint32_t timer_read(void)
|
|
|
|
{
|
|
|
|
int delay = 100;
|
|
|
|
|
2012-04-12 11:05:40 +00:00
|
|
|
__raw_writel(1, mmp_timer_base + TMR_CVWR(1));
|
2009-01-20 06:15:18 +00:00
|
|
|
|
|
|
|
while (delay--)
|
|
|
|
cpu_relax();
|
|
|
|
|
2012-04-12 11:05:40 +00:00
|
|
|
return __raw_readl(mmp_timer_base + TMR_CVWR(1));
|
2009-01-20 06:15:18 +00:00
|
|
|
}
|
|
|
|
|
2013-11-15 23:26:15 +00:00
|
|
|
static u64 notrace mmp_read_sched_clock(void)
|
2009-01-20 06:15:18 +00:00
|
|
|
{
|
2011-12-15 11:19:23 +00:00
|
|
|
return timer_read();
|
2009-01-20 06:15:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static irqreturn_t timer_interrupt(int irq, void *dev_id)
|
|
|
|
{
|
|
|
|
struct clock_event_device *c = dev_id;
|
|
|
|
|
2011-08-09 18:37:55 +00:00
|
|
|
/*
|
|
|
|
* Clear pending interrupt status.
|
|
|
|
*/
|
2012-04-12 11:05:40 +00:00
|
|
|
__raw_writel(0x01, mmp_timer_base + TMR_ICR(0));
|
2011-08-09 18:37:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Disable timer 0.
|
|
|
|
*/
|
2012-04-12 11:05:40 +00:00
|
|
|
__raw_writel(0x02, mmp_timer_base + TMR_CER);
|
2011-08-09 18:37:55 +00:00
|
|
|
|
2009-01-20 06:15:18 +00:00
|
|
|
c->event_handler(c);
|
2011-08-09 18:37:55 +00:00
|
|
|
|
2009-01-20 06:15:18 +00:00
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int timer_set_next_event(unsigned long delta,
|
|
|
|
struct clock_event_device *dev)
|
|
|
|
{
|
2011-08-09 18:37:55 +00:00
|
|
|
unsigned long flags;
|
2009-01-20 06:15:18 +00:00
|
|
|
|
|
|
|
local_irq_save(flags);
|
|
|
|
|
2011-08-09 18:37:55 +00:00
|
|
|
/*
|
|
|
|
* Disable timer 0.
|
|
|
|
*/
|
2012-04-12 11:05:40 +00:00
|
|
|
__raw_writel(0x02, mmp_timer_base + TMR_CER);
|
2011-08-09 18:37:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Clear and enable timer match 0 interrupt.
|
|
|
|
*/
|
2012-04-12 11:05:40 +00:00
|
|
|
__raw_writel(0x01, mmp_timer_base + TMR_ICR(0));
|
|
|
|
__raw_writel(0x01, mmp_timer_base + TMR_IER(0));
|
2009-01-20 06:15:18 +00:00
|
|
|
|
2011-08-09 18:37:55 +00:00
|
|
|
/*
|
|
|
|
* Setup new clockevent timer value.
|
|
|
|
*/
|
2012-04-12 11:05:40 +00:00
|
|
|
__raw_writel(delta - 1, mmp_timer_base + TMR_TN_MM(0, 0));
|
2011-08-09 18:37:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Enable timer 0.
|
|
|
|
*/
|
2012-04-12 11:05:40 +00:00
|
|
|
__raw_writel(0x03, mmp_timer_base + TMR_CER);
|
2009-01-20 06:15:18 +00:00
|
|
|
|
|
|
|
local_irq_restore(flags);
|
2011-08-09 18:37:55 +00:00
|
|
|
|
2009-01-20 06:15:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-27 08:09:52 +00:00
|
|
|
static int timer_set_shutdown(struct clock_event_device *evt)
|
2009-01-20 06:15:18 +00:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
local_irq_save(flags);
|
2015-02-27 08:09:52 +00:00
|
|
|
/* disable the matching interrupt */
|
|
|
|
__raw_writel(0x00, mmp_timer_base + TMR_IER(0));
|
2009-01-20 06:15:18 +00:00
|
|
|
local_irq_restore(flags);
|
2015-02-27 08:09:52 +00:00
|
|
|
|
|
|
|
return 0;
|
2009-01-20 06:15:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct clock_event_device ckevt = {
|
2015-02-27 08:09:52 +00:00
|
|
|
.name = "clockevent",
|
|
|
|
.features = CLOCK_EVT_FEAT_ONESHOT,
|
|
|
|
.rating = 200,
|
|
|
|
.set_next_event = timer_set_next_event,
|
|
|
|
.set_state_shutdown = timer_set_shutdown,
|
|
|
|
.set_state_oneshot = timer_set_shutdown,
|
2009-01-20 06:15:18 +00:00
|
|
|
};
|
|
|
|
|
2016-12-21 19:32:01 +00:00
|
|
|
static u64 clksrc_read(struct clocksource *cs)
|
2009-01-20 06:15:18 +00:00
|
|
|
{
|
|
|
|
return timer_read();
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct clocksource cksrc = {
|
|
|
|
.name = "clocksource",
|
|
|
|
.rating = 200,
|
|
|
|
.read = clksrc_read,
|
|
|
|
.mask = CLOCKSOURCE_MASK(32),
|
|
|
|
.flags = CLOCK_SOURCE_IS_CONTINUOUS,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void __init timer_config(void)
|
|
|
|
{
|
2012-04-12 11:05:40 +00:00
|
|
|
uint32_t ccr = __raw_readl(mmp_timer_base + TMR_CCR);
|
2009-01-20 06:15:18 +00:00
|
|
|
|
2012-04-12 11:05:40 +00:00
|
|
|
__raw_writel(0x0, mmp_timer_base + TMR_CER); /* disable */
|
2009-01-20 06:15:18 +00:00
|
|
|
|
2019-05-16 06:19:37 +00:00
|
|
|
ccr &= (cpu_is_mmp2() || cpu_is_mmp3()) ?
|
|
|
|
(TMR_CCR_CS_0(0) | TMR_CCR_CS_1(0)) :
|
2011-08-09 18:36:59 +00:00
|
|
|
(TMR_CCR_CS_0(3) | TMR_CCR_CS_1(3));
|
2012-04-12 11:05:40 +00:00
|
|
|
__raw_writel(ccr, mmp_timer_base + TMR_CCR);
|
2009-01-20 06:15:18 +00:00
|
|
|
|
2011-08-09 18:37:55 +00:00
|
|
|
/* set timer 0 to periodic mode, and timer 1 to free-running mode */
|
2012-04-12 11:05:40 +00:00
|
|
|
__raw_writel(0x2, mmp_timer_base + TMR_CMR);
|
2009-01-20 06:15:18 +00:00
|
|
|
|
2012-04-12 11:05:40 +00:00
|
|
|
__raw_writel(0x1, mmp_timer_base + TMR_PLCR(0)); /* periodic */
|
|
|
|
__raw_writel(0x7, mmp_timer_base + TMR_ICR(0)); /* clear status */
|
|
|
|
__raw_writel(0x0, mmp_timer_base + TMR_IER(0));
|
2009-01-20 06:15:18 +00:00
|
|
|
|
2012-04-12 11:05:40 +00:00
|
|
|
__raw_writel(0x0, mmp_timer_base + TMR_PLCR(1)); /* free-running */
|
|
|
|
__raw_writel(0x7, mmp_timer_base + TMR_ICR(1)); /* clear status */
|
|
|
|
__raw_writel(0x0, mmp_timer_base + TMR_IER(1));
|
2011-08-09 18:36:59 +00:00
|
|
|
|
2011-08-09 18:37:55 +00:00
|
|
|
/* enable timer 1 counter */
|
2012-04-12 11:05:40 +00:00
|
|
|
__raw_writel(0x2, mmp_timer_base + TMR_CER);
|
2009-01-20 06:15:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct irqaction timer_irq = {
|
|
|
|
.name = "timer",
|
2014-03-04 21:07:26 +00:00
|
|
|
.flags = IRQF_TIMER | IRQF_IRQPOLL,
|
2009-01-20 06:15:18 +00:00
|
|
|
.handler = timer_interrupt,
|
|
|
|
.dev_id = &ckevt,
|
|
|
|
};
|
|
|
|
|
2018-12-10 20:43:01 +00:00
|
|
|
void __init mmp_timer_init(int irq, unsigned long rate)
|
2009-01-20 06:15:18 +00:00
|
|
|
{
|
|
|
|
timer_config();
|
|
|
|
|
2018-11-28 17:53:20 +00:00
|
|
|
sched_clock_register(mmp_read_sched_clock, 32, rate);
|
2009-01-20 06:15:18 +00:00
|
|
|
|
|
|
|
ckevt.cpumask = cpumask_of(0);
|
|
|
|
|
|
|
|
setup_irq(irq, &timer_irq);
|
|
|
|
|
2018-11-28 17:53:20 +00:00
|
|
|
clocksource_register_hz(&cksrc, rate);
|
|
|
|
clockevents_config_and_register(&ckevt, rate, MIN_DELTA, MAX_DELTA);
|
2009-01-20 06:15:18 +00:00
|
|
|
}
|
2012-04-12 11:05:40 +00:00
|
|
|
|
2019-07-10 21:13:51 +00:00
|
|
|
static int __init mmp_dt_init_timer(struct device_node *np)
|
2012-04-12 11:05:40 +00:00
|
|
|
{
|
2018-11-28 17:53:20 +00:00
|
|
|
struct clk *clk;
|
2012-04-12 11:05:40 +00:00
|
|
|
int irq, ret;
|
2018-11-28 17:53:20 +00:00
|
|
|
unsigned long rate;
|
2012-04-12 11:05:40 +00:00
|
|
|
|
2018-11-28 17:53:20 +00:00
|
|
|
clk = of_clk_get(np, 0);
|
|
|
|
if (!IS_ERR(clk)) {
|
|
|
|
ret = clk_prepare_enable(clk);
|
|
|
|
if (ret)
|
2019-07-10 21:13:51 +00:00
|
|
|
return ret;
|
2018-11-28 17:53:20 +00:00
|
|
|
rate = clk_get_rate(clk) / 2;
|
|
|
|
} else if (cpu_is_pj4()) {
|
|
|
|
rate = 6500000;
|
|
|
|
} else {
|
|
|
|
rate = 3250000;
|
|
|
|
}
|
|
|
|
|
2012-04-12 11:05:40 +00:00
|
|
|
irq = irq_of_parse_and_map(np, 0);
|
2019-07-10 21:13:51 +00:00
|
|
|
if (!irq)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2012-04-12 11:05:40 +00:00
|
|
|
mmp_timer_base = of_iomap(np, 0);
|
2019-07-10 21:13:51 +00:00
|
|
|
if (!mmp_timer_base)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2018-12-10 20:43:01 +00:00
|
|
|
mmp_timer_init(irq, rate);
|
2019-07-10 21:13:51 +00:00
|
|
|
return 0;
|
2012-04-12 11:05:40 +00:00
|
|
|
}
|
2019-07-10 21:13:51 +00:00
|
|
|
|
|
|
|
TIMER_OF_DECLARE(mmp_timer, "mrvl,mmp-timer", mmp_dt_init_timer);
|