Files
linux-apfs/drivers/clocksource/arc_timer.c
T

337 lines
7.7 KiB
C
Raw Normal View History

2013-01-18 15:12:18 +05:30
/*
2016-10-31 13:46:38 -07:00
* Copyright (C) 2016-17 Synopsys, Inc. (www.synopsys.com)
2013-01-18 15:12:18 +05:30
* 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 13:46:38 -07: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 15:12:18 +05:30
*
2016-10-31 13:46:38 -07: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 15:12:18 +05:30
*/
#include <linux/interrupt.h>
2016-01-14 12:20:08 +05:30
#include <linux/clk.h>
#include <linux/clk-provider.h>
2013-01-18 15:12:18 +05:30
#include <linux/clocksource.h>
#include <linux/clockchips.h>
#include <linux/cpu.h>
2016-01-01 17:58:45 +05:30
#include <linux/of.h>
#include <linux/of_irq.h>
2013-01-18 15:12:18 +05:30
#include <soc/arc/timers.h>
#include <soc/arc/mcip.h>
2013-01-18 15:12:18 +05:30
2016-01-01 17:58:45 +05:30
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)) {
pr_err("timer missing clk");
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 15:12:18 +05:30
/********** Clock Source Device *********/
#ifdef CONFIG_ARC_TIMERS_64BIT
static u64 arc_read_gfrc(struct clocksource *cs)
{
unsigned long flags;
u32 l, h;
local_irq_save(flags);
2016-01-22 14:27:50 +05:30
__mcip_cmd(CMD_GFRC_READ_LO, 0);
l = read_aux_reg(ARC_REG_MCIP_READBACK);
2016-01-22 14:27:50 +05:30
__mcip_cmd(CMD_GFRC_READ_HI, 0);
h = read_aux_reg(ARC_REG_MCIP_READBACK);
local_irq_restore(flags);
return (((u64)h) << 32) | l;
}
2016-01-01 18:05:48 +05:30
static struct clocksource arc_counter_gfrc = {
2016-01-22 14:27:50 +05:30
.name = "ARConnect GFRC",
.rating = 400,
2016-01-01 18:05:48 +05:30
.read = arc_read_gfrc,
.mask = CLOCKSOURCE_MASK(64),
.flags = CLOCK_SOURCE_IS_CONTINUOUS,
};
static int __init arc_cs_setup_gfrc(struct device_node *node)
2016-01-01 18:05:48 +05:30
{
struct mcip_bcr mp;
2016-01-01 18:05:48 +05:30
int ret;
READ_BCR(ARC_REG_MCIP_BCR, mp);
if (!mp.gfrc) {
pr_warn("Global-64-bit-Ctr clocksource not detected");
return -ENXIO;
}
2016-01-01 18:05:48 +05:30
ret = arc_get_timer_clk(node);
if (ret)
return ret;
2016-01-01 18:05:48 +05:30
return clocksource_register_hz(&arc_counter_gfrc, arc_timer_freq);
2016-01-01 18:05:48 +05:30
}
CLOCKSOURCE_OF_DECLARE(arc_gfrc, "snps,archs-timer-gfrc", arc_cs_setup_gfrc);
2016-01-01 18:05:48 +05:30
#define AUX_RTC_CTRL 0x103
#define AUX_RTC_LOW 0x104
#define AUX_RTC_HIGH 0x105
static u64 arc_read_rtc(struct clocksource *cs)
{
unsigned long status;
u32 l, h;
/*
* 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 {
l = read_aux_reg(AUX_RTC_LOW);
h = read_aux_reg(AUX_RTC_HIGH);
status = read_aux_reg(AUX_RTC_CTRL);
} while (!(status & _BITUL(31)));
return (((u64)h) << 32) | l;
}
2016-01-01 18:05:48 +05:30
static struct clocksource arc_counter_rtc = {
.name = "ARCv2 RTC",
.rating = 350,
2016-01-01 18:05:48 +05:30
.read = arc_read_rtc,
.mask = CLOCKSOURCE_MASK(64),
.flags = CLOCK_SOURCE_IS_CONTINUOUS,
};
static int __init arc_cs_setup_rtc(struct device_node *node)
2016-01-01 18:05:48 +05:30
{
struct bcr_timer timer;
2016-01-01 18:05:48 +05:30
int ret;
READ_BCR(ARC_REG_TIMERS_BCR, timer);
if (!timer.rtc) {
pr_warn("Local-64-bit-Ctr clocksource not detected");
return -ENXIO;
}
2016-01-01 18:05:48 +05:30
/* Local to CPU hence not usable in SMP */
if (IS_ENABLED(CONFIG_SMP)) {
pr_warn("Local-64-bit-Ctr not usable in SMP");
return -EINVAL;
}
2016-01-01 18:05:48 +05:30
ret = arc_get_timer_clk(node);
if (ret)
return ret;
2016-01-01 18:05:48 +05:30
write_aux_reg(AUX_RTC_CTRL, 1);
return clocksource_register_hz(&arc_counter_rtc, arc_timer_freq);
2016-01-01 18:05:48 +05:30
}
CLOCKSOURCE_OF_DECLARE(arc_rtc, "snps,archs-timer-rtc", arc_cs_setup_rtc);
2016-01-01 18:05:48 +05:30
#endif
2013-01-18 15:12:18 +05:30
/*
2016-01-01 18:05:48 +05:30
* 32bit TIMER1 to keep counting monotonically and wraparound
2013-01-18 15:12:18 +05:30
*/
static u64 arc_read_timer1(struct clocksource *cs)
2013-01-18 15:12:18 +05:30
{
return (u64) read_aux_reg(ARC_REG_TIMER1_CNT);
2013-01-18 15:12:18 +05:30
}
2016-01-01 18:05:48 +05:30
static struct clocksource arc_counter_timer1 = {
2013-01-18 15:12:18 +05:30
.name = "ARC Timer1",
.rating = 300,
2016-01-01 18:05:48 +05:30
.read = arc_read_timer1,
2013-01-18 15:12:18 +05:30
.mask = CLOCKSOURCE_MASK(32),
.flags = CLOCK_SOURCE_IS_CONTINUOUS,
};
static int __init arc_cs_setup_timer1(struct device_node *node)
2016-01-01 18:05:48 +05:30
{
int ret;
/* Local to CPU hence not usable in SMP */
if (IS_ENABLED(CONFIG_SMP))
return -EINVAL;
2016-01-01 18:05:48 +05:30
ret = arc_get_timer_clk(node);
if (ret)
return ret;
2016-01-01 18:05:48 +05:30
write_aux_reg(ARC_REG_TIMER1_LIMIT, ARC_TIMERN_MAX);
2016-01-01 18:05:48 +05:30
write_aux_reg(ARC_REG_TIMER1_CNT, 0);
write_aux_reg(ARC_REG_TIMER1_CTRL, TIMER_CTRL_NH);
return clocksource_register_hz(&arc_counter_timer1, arc_timer_freq);
2016-01-01 18:05:48 +05:30
}
2013-01-18 15:12:18 +05:30
/********** Clock Event Device *********/
2016-01-01 17:58:45 +05:30
static int arc_timer_irq;
2013-01-18 15:12:18 +05:30
/*
2014-06-25 17:14:03 +05:30
* Arm the timer to interrupt after @cycles
2013-01-18 15:12:18 +05:30
* The distinction for oneshot/periodic is done in arc_event_timer_ack() below
*/
2014-06-25 17:14:03 +05:30
static void arc_timer_event_setup(unsigned int cycles)
2013-01-18 15:12:18 +05:30
{
2014-06-25 17:14:03 +05:30
write_aux_reg(ARC_REG_TIMER0_LIMIT, cycles);
2013-01-18 15:12:18 +05:30
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;
}
static int arc_clkevent_set_periodic(struct clock_event_device *dev)
2013-01-18 15:12:18 +05:30
{
/*
* At X Hz, 1 sec = 1000ms -> X cycles;
* 10ms -> X / 100 cycles
*/
2016-01-01 17:58:45 +05:30
arc_timer_event_setup(arc_timer_freq / HZ);
return 0;
2013-01-18 15:12:18 +05:30
}
static DEFINE_PER_CPU(struct clock_event_device, arc_clockevent_device) = {
.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 15:12:18 +05:30
};
static irqreturn_t timer_irq_handler(int irq, void *dev_id)
{
2014-01-25 00:42:37 +05:30
/*
* 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);
int irq_reenable = clockevent_state_periodic(evt);
2014-01-25 00:42:37 +05:30
/*
* 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 15:12:18 +05:30
return IRQ_HANDLED;
}
2016-07-13 17:17:07 +00:00
static int arc_timer_starting_cpu(unsigned int cpu)
2013-01-18 15:12:18 +05:30
{
struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
2013-01-18 15:12:18 +05:30
evt->cpumask = cpumask_of(smp_processor_id());
clockevents_config_and_register(evt, arc_timer_freq, 0, ARC_TIMERN_MAX);
2016-07-13 17:17:07 +00:00
enable_percpu_irq(arc_timer_irq, 0);
return 0;
}
2016-07-13 17:17:07 +00:00
static int arc_timer_dying_cpu(unsigned int cpu)
{
disable_percpu_irq(arc_timer_irq);
return 0;
}
/*
* clockevent setup for boot CPU
*/
static int __init arc_clockevent_setup(struct device_node *node)
{
struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
int ret;
2016-01-01 17:58:45 +05:30
arc_timer_irq = irq_of_parse_and_map(node, 0);
if (arc_timer_irq <= 0) {
pr_err("clockevent: missing irq");
return -EINVAL;
}
2016-01-01 17:58:45 +05:30
ret = arc_get_timer_clk(node);
if (ret) {
pr_err("clockevent: missing clk");
return ret;
}
2016-01-01 17:58:45 +05:30
/* 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);
if (ret) {
pr_err("clockevent: unable to request irq\n");
return ret;
}
2016-01-28 12:56:03 +05:30
2016-07-13 17:17:07 +00: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 17:17:07 +00:00
arc_timer_starting_cpu,
arc_timer_dying_cpu);
if (ret) {
pr_err("Failed to setup hotplug state");
return ret;
}
return 0;
2013-01-18 15:12:18 +05:30
}
2016-01-01 18:05:48 +05:30
static int __init arc_of_timer_init(struct device_node *np)
2016-01-01 18:05:48 +05:30
{
static int init_count = 0;
int ret;
2016-01-01 18:05:48 +05:30
if (!init_count) {
init_count = 1;
ret = arc_clockevent_setup(np);
2016-01-01 18:05:48 +05:30
} else {
ret = arc_cs_setup_timer1(np);
2016-01-01 18:05:48 +05:30
}
return ret;
2016-01-01 18:05:48 +05:30
}
CLOCKSOURCE_OF_DECLARE(arc_clkevt, "snps,arc-timer", arc_of_timer_init);