2013-01-18 10:42:18 +01:00
|
|
|
/*
|
2016-10-31 21:46:38 +01:00
|
|
|
* Copyright (C) 2016-17 Synopsys, Inc. (www.synopsys.com)
|
2013-01-18 10:42:18 +01:00
|
|
|
* Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
|
2016-10-31 21:46:38 +01:00
|
|
|
/* ARC700 has two 32bit independent prog Timers: TIMER0 and TIMER1, Each can be
|
|
|
|
* programmed to go from @count to @limit and optionally interrupt.
|
|
|
|
* We've designated TIMER0 for clockevents and TIMER1 for clocksource
|
2013-01-18 10:42:18 +01:00
|
|
|
*
|
2016-10-31 21:46:38 +01:00
|
|
|
* ARCv2 based HS38 cores have RTC (in-core) and GFRC (inside ARConnect/MCIP)
|
|
|
|
* which are suitable for UP and SMP based clocksources respectively
|
2013-01-18 10:42:18 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/interrupt.h>
|
2016-01-14 07:50:08 +01:00
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/clk-provider.h>
|
2013-01-18 10:42:18 +01:00
|
|
|
#include <linux/clocksource.h>
|
|
|
|
#include <linux/clockchips.h>
|
2016-01-01 11:18:49 +01:00
|
|
|
#include <linux/cpu.h>
|
2016-01-01 13:28:45 +01:00
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/of_irq.h>
|
2013-01-18 10:42:18 +01:00
|
|
|
|
2016-10-31 21:06:19 +01:00
|
|
|
#include <soc/arc/timers.h>
|
2016-10-31 19:27:08 +01:00
|
|
|
#include <soc/arc/mcip.h>
|
2014-12-24 14:11:55 +01:00
|
|
|
|
2013-01-18 10:42:18 +01:00
|
|
|
|
2016-01-01 13:28:45 +01:00
|
|
|
static unsigned long arc_timer_freq;
|
|
|
|
|
|
|
|
static int noinline arc_get_timer_clk(struct device_node *node)
|
|
|
|
{
|
|
|
|
struct clk *clk;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
clk = of_clk_get(node, 0);
|
|
|
|
if (IS_ERR(clk)) {
|
2017-03-09 10:47:10 +01:00
|
|
|
pr_err("timer missing clk\n");
|
2016-01-01 13:28:45 +01:00
|
|
|
return PTR_ERR(clk);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = clk_prepare_enable(clk);
|
|
|
|
if (ret) {
|
|
|
|
pr_err("Couldn't enable parent clk\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
arc_timer_freq = clk_get_rate(clk);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-18 10:42:18 +01:00
|
|
|
/********** Clock Source Device *********/
|
|
|
|
|
2016-10-31 22:26:41 +01:00
|
|
|
#ifdef CONFIG_ARC_TIMERS_64BIT
|
2014-12-24 14:11:55 +01:00
|
|
|
|
2016-12-21 20:32:01 +01:00
|
|
|
static u64 arc_read_gfrc(struct clocksource *cs)
|
2014-12-24 14:11:55 +01:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
2016-11-03 19:38:52 +01:00
|
|
|
u32 l, h;
|
2014-12-24 14:11:55 +01:00
|
|
|
|
|
|
|
local_irq_save(flags);
|
|
|
|
|
2016-01-22 09:57:50 +01:00
|
|
|
__mcip_cmd(CMD_GFRC_READ_LO, 0);
|
2016-11-03 19:38:52 +01:00
|
|
|
l = read_aux_reg(ARC_REG_MCIP_READBACK);
|
2014-12-24 14:11:55 +01:00
|
|
|
|
2016-01-22 09:57:50 +01:00
|
|
|
__mcip_cmd(CMD_GFRC_READ_HI, 0);
|
2016-11-03 19:38:52 +01:00
|
|
|
h = read_aux_reg(ARC_REG_MCIP_READBACK);
|
2014-12-24 14:11:55 +01:00
|
|
|
|
|
|
|
local_irq_restore(flags);
|
|
|
|
|
2016-12-21 20:32:01 +01:00
|
|
|
return (((u64)h) << 32) | l;
|
2014-12-24 14:11:55 +01:00
|
|
|
}
|
|
|
|
|
2016-01-01 13:35:48 +01:00
|
|
|
static struct clocksource arc_counter_gfrc = {
|
2016-01-22 09:57:50 +01:00
|
|
|
.name = "ARConnect GFRC",
|
2014-12-24 14:11:55 +01:00
|
|
|
.rating = 400,
|
2016-01-01 13:35:48 +01:00
|
|
|
.read = arc_read_gfrc,
|
2014-12-24 14:11:55 +01:00
|
|
|
.mask = CLOCKSOURCE_MASK(64),
|
|
|
|
.flags = CLOCK_SOURCE_IS_CONTINUOUS,
|
|
|
|
};
|
|
|
|
|
2016-06-15 14:50:12 +02:00
|
|
|
static int __init arc_cs_setup_gfrc(struct device_node *node)
|
2016-01-01 13:35:48 +01:00
|
|
|
{
|
2016-10-31 21:02:31 +01:00
|
|
|
struct mcip_bcr mp;
|
2016-01-01 13:35:48 +01:00
|
|
|
int ret;
|
|
|
|
|
2016-10-31 21:02:31 +01:00
|
|
|
READ_BCR(ARC_REG_MCIP_BCR, mp);
|
|
|
|
if (!mp.gfrc) {
|
2017-03-09 10:47:10 +01:00
|
|
|
pr_warn("Global-64-bit-Ctr clocksource not detected\n");
|
2016-06-15 14:50:12 +02:00
|
|
|
return -ENXIO;
|
2016-10-31 21:02:31 +01:00
|
|
|
}
|
2016-01-01 13:35:48 +01:00
|
|
|
|
|
|
|
ret = arc_get_timer_clk(node);
|
|
|
|
if (ret)
|
2016-06-15 14:50:12 +02:00
|
|
|
return ret;
|
2016-01-01 13:35:48 +01:00
|
|
|
|
2016-06-15 14:50:12 +02:00
|
|
|
return clocksource_register_hz(&arc_counter_gfrc, arc_timer_freq);
|
2016-01-01 13:35:48 +01:00
|
|
|
}
|
2016-06-07 00:27:44 +02:00
|
|
|
CLOCKSOURCE_OF_DECLARE(arc_gfrc, "snps,archs-timer-gfrc", arc_cs_setup_gfrc);
|
2016-01-01 13:35:48 +01:00
|
|
|
|
2013-11-07 10:27:16 +01:00
|
|
|
#define AUX_RTC_CTRL 0x103
|
|
|
|
#define AUX_RTC_LOW 0x104
|
|
|
|
#define AUX_RTC_HIGH 0x105
|
|
|
|
|
2016-12-21 20:32:01 +01:00
|
|
|
static u64 arc_read_rtc(struct clocksource *cs)
|
2013-11-07 10:27:16 +01:00
|
|
|
{
|
|
|
|
unsigned long status;
|
2016-11-03 19:38:52 +01:00
|
|
|
u32 l, h;
|
2013-11-07 10:27:16 +01:00
|
|
|
|
2016-10-31 22:09:52 +01:00
|
|
|
/*
|
|
|
|
* hardware has an internal state machine which tracks readout of
|
|
|
|
* low/high and updates the CTRL.status if
|
|
|
|
* - interrupt/exception taken between the two reads
|
|
|
|
* - high increments after low has been read
|
|
|
|
*/
|
|
|
|
do {
|
2016-11-03 19:38:52 +01:00
|
|
|
l = read_aux_reg(AUX_RTC_LOW);
|
|
|
|
h = read_aux_reg(AUX_RTC_HIGH);
|
2016-10-31 22:09:52 +01:00
|
|
|
status = read_aux_reg(AUX_RTC_CTRL);
|
|
|
|
} while (!(status & _BITUL(31)));
|
2013-11-07 10:27:16 +01:00
|
|
|
|
2016-12-21 20:32:01 +01:00
|
|
|
return (((u64)h) << 32) | l;
|
2013-11-07 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
2016-01-01 13:35:48 +01:00
|
|
|
static struct clocksource arc_counter_rtc = {
|
2013-11-07 10:27:16 +01:00
|
|
|
.name = "ARCv2 RTC",
|
|
|
|
.rating = 350,
|
2016-01-01 13:35:48 +01:00
|
|
|
.read = arc_read_rtc,
|
2013-11-07 10:27:16 +01:00
|
|
|
.mask = CLOCKSOURCE_MASK(64),
|
|
|
|
.flags = CLOCK_SOURCE_IS_CONTINUOUS,
|
|
|
|
};
|
|
|
|
|
2016-06-15 14:50:12 +02:00
|
|
|
static int __init arc_cs_setup_rtc(struct device_node *node)
|
2013-01-18 10:42:18 +01:00
|
|
|
{
|
2016-10-31 21:02:31 +01:00
|
|
|
struct bcr_timer timer;
|
2016-01-01 13:35:48 +01:00
|
|
|
int ret;
|
|
|
|
|
2016-10-31 21:02:31 +01:00
|
|
|
READ_BCR(ARC_REG_TIMERS_BCR, timer);
|
|
|
|
if (!timer.rtc) {
|
2017-03-09 10:47:10 +01:00
|
|
|
pr_warn("Local-64-bit-Ctr clocksource not detected\n");
|
2016-06-15 14:50:12 +02:00
|
|
|
return -ENXIO;
|
2016-10-31 21:02:31 +01:00
|
|
|
}
|
2016-01-01 13:35:48 +01:00
|
|
|
|
|
|
|
/* Local to CPU hence not usable in SMP */
|
2016-10-31 21:02:31 +01:00
|
|
|
if (IS_ENABLED(CONFIG_SMP)) {
|
2017-03-09 10:47:10 +01:00
|
|
|
pr_warn("Local-64-bit-Ctr not usable in SMP\n");
|
2016-06-15 14:50:12 +02:00
|
|
|
return -EINVAL;
|
2016-10-31 21:02:31 +01:00
|
|
|
}
|
2016-01-01 13:35:48 +01:00
|
|
|
|
|
|
|
ret = arc_get_timer_clk(node);
|
|
|
|
if (ret)
|
2016-06-15 14:50:12 +02:00
|
|
|
return ret;
|
2013-01-18 10:42:18 +01:00
|
|
|
|
2016-01-01 13:35:48 +01:00
|
|
|
write_aux_reg(AUX_RTC_CTRL, 1);
|
|
|
|
|
2016-06-15 14:50:12 +02:00
|
|
|
return clocksource_register_hz(&arc_counter_rtc, arc_timer_freq);
|
2013-01-18 10:42:18 +01:00
|
|
|
}
|
2016-06-07 00:27:44 +02:00
|
|
|
CLOCKSOURCE_OF_DECLARE(arc_rtc, "snps,archs-timer-rtc", arc_cs_setup_rtc);
|
2016-01-01 13:35:48 +01:00
|
|
|
|
|
|
|
#endif
|
2013-01-18 10:42:18 +01:00
|
|
|
|
2016-01-01 13:35:48 +01:00
|
|
|
/*
|
|
|
|
* 32bit TIMER1 to keep counting monotonically and wraparound
|
|
|
|
*/
|
|
|
|
|
2016-12-21 20:32:01 +01:00
|
|
|
static u64 arc_read_timer1(struct clocksource *cs)
|
2013-01-18 10:42:18 +01:00
|
|
|
{
|
2016-12-21 20:32:01 +01:00
|
|
|
return (u64) read_aux_reg(ARC_REG_TIMER1_CNT);
|
2013-01-18 10:42:18 +01:00
|
|
|
}
|
|
|
|
|
2016-01-01 13:35:48 +01:00
|
|
|
static struct clocksource arc_counter_timer1 = {
|
2013-01-18 10:42:18 +01:00
|
|
|
.name = "ARC Timer1",
|
|
|
|
.rating = 300,
|
2016-01-01 13:35:48 +01:00
|
|
|
.read = arc_read_timer1,
|
2013-01-18 10:42:18 +01:00
|
|
|
.mask = CLOCKSOURCE_MASK(32),
|
|
|
|
.flags = CLOCK_SOURCE_IS_CONTINUOUS,
|
|
|
|
};
|
|
|
|
|
2016-06-15 14:50:12 +02:00
|
|
|
static int __init arc_cs_setup_timer1(struct device_node *node)
|
2016-01-01 13:35:48 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Local to CPU hence not usable in SMP */
|
|
|
|
if (IS_ENABLED(CONFIG_SMP))
|
2016-06-15 14:50:12 +02:00
|
|
|
return -EINVAL;
|
2016-01-01 13:35:48 +01:00
|
|
|
|
|
|
|
ret = arc_get_timer_clk(node);
|
|
|
|
if (ret)
|
2016-06-15 14:50:12 +02:00
|
|
|
return ret;
|
2016-01-01 13:35:48 +01:00
|
|
|
|
2016-10-31 21:06:19 +01:00
|
|
|
write_aux_reg(ARC_REG_TIMER1_LIMIT, ARC_TIMERN_MAX);
|
2016-01-01 13:35:48 +01:00
|
|
|
write_aux_reg(ARC_REG_TIMER1_CNT, 0);
|
|
|
|
write_aux_reg(ARC_REG_TIMER1_CTRL, TIMER_CTRL_NH);
|
|
|
|
|
2016-06-15 14:50:12 +02:00
|
|
|
return clocksource_register_hz(&arc_counter_timer1, arc_timer_freq);
|
2016-01-01 13:35:48 +01:00
|
|
|
}
|
2013-11-07 10:27:16 +01:00
|
|
|
|
2013-01-18 10:42:18 +01:00
|
|
|
/********** Clock Event Device *********/
|
|
|
|
|
2016-01-01 13:28:45 +01:00
|
|
|
static int arc_timer_irq;
|
2016-01-01 11:18:49 +01:00
|
|
|
|
2013-01-18 10:42:18 +01:00
|
|
|
/*
|
2014-06-25 13:44:03 +02:00
|
|
|
* Arm the timer to interrupt after @cycles
|
2013-01-18 10:42:18 +01:00
|
|
|
* The distinction for oneshot/periodic is done in arc_event_timer_ack() below
|
|
|
|
*/
|
2014-06-25 13:44:03 +02:00
|
|
|
static void arc_timer_event_setup(unsigned int cycles)
|
2013-01-18 10:42:18 +01:00
|
|
|
{
|
2014-06-25 13:44:03 +02:00
|
|
|
write_aux_reg(ARC_REG_TIMER0_LIMIT, cycles);
|
2013-01-18 10:42:18 +01:00
|
|
|
write_aux_reg(ARC_REG_TIMER0_CNT, 0); /* start from 0 */
|
|
|
|
|
|
|
|
write_aux_reg(ARC_REG_TIMER0_CTRL, TIMER_CTRL_IE | TIMER_CTRL_NH);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int arc_clkevent_set_next_event(unsigned long delta,
|
|
|
|
struct clock_event_device *dev)
|
|
|
|
{
|
|
|
|
arc_timer_event_setup(delta);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-07-16 13:26:14 +02:00
|
|
|
static int arc_clkevent_set_periodic(struct clock_event_device *dev)
|
2013-01-18 10:42:18 +01:00
|
|
|
{
|
2015-07-16 13:26:14 +02:00
|
|
|
/*
|
|
|
|
* At X Hz, 1 sec = 1000ms -> X cycles;
|
|
|
|
* 10ms -> X / 100 cycles
|
|
|
|
*/
|
2016-01-01 13:28:45 +01:00
|
|
|
arc_timer_event_setup(arc_timer_freq / HZ);
|
2015-07-16 13:26:14 +02:00
|
|
|
return 0;
|
2013-01-18 10:42:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static DEFINE_PER_CPU(struct clock_event_device, arc_clockevent_device) = {
|
2015-07-16 13:26:14 +02:00
|
|
|
.name = "ARC Timer0",
|
|
|
|
.features = CLOCK_EVT_FEAT_ONESHOT |
|
|
|
|
CLOCK_EVT_FEAT_PERIODIC,
|
|
|
|
.rating = 300,
|
|
|
|
.set_next_event = arc_clkevent_set_next_event,
|
|
|
|
.set_state_periodic = arc_clkevent_set_periodic,
|
2013-01-18 10:42:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static irqreturn_t timer_irq_handler(int irq, void *dev_id)
|
|
|
|
{
|
2014-01-24 20:12:37 +01:00
|
|
|
/*
|
|
|
|
* Note that generic IRQ core could have passed @evt for @dev_id if
|
|
|
|
* irq_set_chip_and_handler() asked for handle_percpu_devid_irq()
|
|
|
|
*/
|
|
|
|
struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
|
2015-07-16 13:26:14 +02:00
|
|
|
int irq_reenable = clockevent_state_periodic(evt);
|
2014-01-24 20:12:37 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Any write to CTRL reg ACks the interrupt, we rewrite the
|
|
|
|
* Count when [N]ot [H]alted bit.
|
|
|
|
* And re-arm it if perioid by [I]nterrupt [E]nable bit
|
|
|
|
*/
|
|
|
|
write_aux_reg(ARC_REG_TIMER0_CTRL, irq_reenable | TIMER_CTRL_NH);
|
|
|
|
|
|
|
|
evt->event_handler(evt);
|
2013-01-18 10:42:18 +01:00
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
2016-07-13 19:17:07 +02:00
|
|
|
|
|
|
|
static int arc_timer_starting_cpu(unsigned int cpu)
|
2016-01-01 11:18:49 +01:00
|
|
|
{
|
|
|
|
struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
|
|
|
|
|
|
|
|
evt->cpumask = cpumask_of(smp_processor_id());
|
|
|
|
|
2016-10-31 21:06:19 +01:00
|
|
|
clockevents_config_and_register(evt, arc_timer_freq, 0, ARC_TIMERN_MAX);
|
2016-07-13 19:17:07 +02:00
|
|
|
enable_percpu_irq(arc_timer_irq, 0);
|
|
|
|
return 0;
|
2016-01-01 11:18:49 +01:00
|
|
|
}
|
|
|
|
|
2016-07-13 19:17:07 +02:00
|
|
|
static int arc_timer_dying_cpu(unsigned int cpu)
|
|
|
|
{
|
|
|
|
disable_percpu_irq(arc_timer_irq);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-01-01 11:18:49 +01:00
|
|
|
|
2013-01-18 10:42:18 +01:00
|
|
|
/*
|
2016-01-01 11:18:49 +01:00
|
|
|
* clockevent setup for boot CPU
|
2013-01-18 10:42:18 +01:00
|
|
|
*/
|
2016-06-15 14:50:12 +02:00
|
|
|
static int __init arc_clockevent_setup(struct device_node *node)
|
2013-01-18 10:42:18 +01:00
|
|
|
{
|
2014-05-08 10:36:38 +02:00
|
|
|
struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
|
2016-01-01 11:18:49 +01:00
|
|
|
int ret;
|
2013-01-18 10:42:18 +01:00
|
|
|
|
2016-01-01 13:28:45 +01:00
|
|
|
arc_timer_irq = irq_of_parse_and_map(node, 0);
|
2016-06-15 14:50:12 +02:00
|
|
|
if (arc_timer_irq <= 0) {
|
2017-03-09 10:47:10 +01:00
|
|
|
pr_err("clockevent: missing irq\n");
|
2016-06-15 14:50:12 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2016-01-01 13:28:45 +01:00
|
|
|
|
|
|
|
ret = arc_get_timer_clk(node);
|
2016-06-15 14:50:12 +02:00
|
|
|
if (ret) {
|
2017-03-09 10:47:10 +01:00
|
|
|
pr_err("clockevent: missing clk\n");
|
2016-06-15 14:50:12 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2016-01-01 13:28:45 +01:00
|
|
|
|
2016-01-01 11:18:49 +01:00
|
|
|
/* Needs apriori irq_set_percpu_devid() done in intc map function */
|
|
|
|
ret = request_percpu_irq(arc_timer_irq, timer_irq_handler,
|
|
|
|
"Timer0 (per-cpu-tick)", evt);
|
2016-06-15 14:50:12 +02:00
|
|
|
if (ret) {
|
|
|
|
pr_err("clockevent: unable to request irq\n");
|
|
|
|
return ret;
|
|
|
|
}
|
2016-01-28 08:26:03 +01:00
|
|
|
|
2016-07-13 19:17:07 +02:00
|
|
|
ret = cpuhp_setup_state(CPUHP_AP_ARC_TIMER_STARTING,
|
2016-12-21 20:19:54 +01:00
|
|
|
"clockevents/arc/timer:starting",
|
2016-07-13 19:17:07 +02:00
|
|
|
arc_timer_starting_cpu,
|
|
|
|
arc_timer_dying_cpu);
|
|
|
|
if (ret) {
|
2017-03-09 10:47:10 +01:00
|
|
|
pr_err("Failed to setup hotplug state\n");
|
2016-07-13 19:17:07 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2016-06-15 14:50:12 +02:00
|
|
|
return 0;
|
2013-01-18 10:42:18 +01:00
|
|
|
}
|
2016-01-01 13:35:48 +01:00
|
|
|
|
2016-06-15 14:50:12 +02:00
|
|
|
static int __init arc_of_timer_init(struct device_node *np)
|
2016-01-01 13:35:48 +01:00
|
|
|
{
|
|
|
|
static int init_count = 0;
|
2016-06-15 14:50:12 +02:00
|
|
|
int ret;
|
2016-01-01 13:35:48 +01:00
|
|
|
|
|
|
|
if (!init_count) {
|
|
|
|
init_count = 1;
|
2016-06-15 14:50:12 +02:00
|
|
|
ret = arc_clockevent_setup(np);
|
2016-01-01 13:35:48 +01:00
|
|
|
} else {
|
2016-06-15 14:50:12 +02:00
|
|
|
ret = arc_cs_setup_timer1(np);
|
2016-01-01 13:35:48 +01:00
|
|
|
}
|
2016-06-15 14:50:12 +02:00
|
|
|
|
|
|
|
return ret;
|
2016-01-01 13:35:48 +01:00
|
|
|
}
|
2016-06-07 00:27:44 +02:00
|
|
|
CLOCKSOURCE_OF_DECLARE(arc_clkevt, "snps,arc-timer", arc_of_timer_init);
|