2010-12-23 15:11:21 +03:00
|
|
|
/*
|
2012-10-06 10:55:42 +04:00
|
|
|
* arch/arm/mach-vt8500/timer.c
|
2010-12-23 15:11:21 +03:00
|
|
|
*
|
2012-08-03 13:00:06 +04:00
|
|
|
* Copyright (C) 2012 Tony Prisk <linux@prisktech.co.nz>
|
2010-12-23 15:11:21 +03:00
|
|
|
* Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2012-08-03 13:00:06 +04:00
|
|
|
/*
|
|
|
|
* This file is copied and modified from the original timer.c provided by
|
|
|
|
* Alexey Charkov. Minor changes have been made for Device Tree Support.
|
|
|
|
*/
|
|
|
|
|
2010-12-23 15:11:21 +03:00
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/irq.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/clocksource.h>
|
|
|
|
#include <linux/clockchips.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
|
2012-08-03 13:00:06 +04:00
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/of_address.h>
|
|
|
|
#include <linux/of_irq.h>
|
2010-12-23 15:11:21 +03:00
|
|
|
|
|
|
|
#define VT8500_TIMER_OFFSET 0x0100
|
2012-08-03 13:00:06 +04:00
|
|
|
#define VT8500_TIMER_HZ 3000000
|
2010-12-23 15:11:21 +03:00
|
|
|
#define TIMER_MATCH_VAL 0x0000
|
|
|
|
#define TIMER_COUNT_VAL 0x0010
|
|
|
|
#define TIMER_STATUS_VAL 0x0014
|
|
|
|
#define TIMER_IER_VAL 0x001c /* interrupt enable */
|
|
|
|
#define TIMER_CTRL_VAL 0x0020
|
|
|
|
#define TIMER_AS_VAL 0x0024 /* access status */
|
|
|
|
#define TIMER_COUNT_R_ACTIVE (1 << 5) /* not ready for read */
|
|
|
|
#define TIMER_COUNT_W_ACTIVE (1 << 4) /* not ready for write */
|
|
|
|
#define TIMER_MATCH_W_ACTIVE (1 << 0) /* not ready for write */
|
|
|
|
|
|
|
|
#define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
|
|
|
|
|
clocksource/drivers/vt8500: Increase the minimum delta
The vt8500 clocksource driver declares itself as capable to handle the
minimum delay of 4 cycles by passing the value into
clockevents_config_and_register(). The vt8500_timer_set_next_event()
requires the passed cycles value to be at least 16. The impact is that
userspace hangs in nanosleep() calls with small delay intervals.
This problem is reproducible in Linux 4.2 starting from:
c6eb3f70d448 ('hrtimer: Get rid of hrtimer softirq')
From Russell King, more detailed explanation:
"It's a speciality of the StrongARM/PXA hardware. It takes a certain
number of OSCR cycles for the value written to hit the compare registers.
So, if a very small delta is written (eg, the compare register is written
with a value of OSCR + 1), the OSCR will have incremented past this value
before it hits the underlying hardware. The result is, that you end up
waiting a very long time for the OSCR to wrap before the event fires.
So, we introduce a check in set_next_event() to detect this and return
-ETIME if the calculated delta is too small, which causes the generic
clockevents code to retry after adding the min_delta specified in
clockevents_config_and_register() to the current time value.
min_delta must be sufficient that we don't re-trip the -ETIME check - if
we do, we will return -ETIME, forward the next event time, try to set it,
return -ETIME again, and basically lock the system up. So, min_delta
must be larger than the check inside set_next_event(). A factor of two
was chosen to ensure that this situation would never occur.
The PXA code worked on PXA systems for years, and I'd suggest no one
changes this mechanism without access to a wide range of PXA systems,
otherwise they're risking breakage."
Cc: stable@vger.kernel.org
Cc: Russell King <linux@arm.linux.org.uk>
Acked-by: Alexey Charkov <alchark@gmail.com>
Signed-off-by: Roman Volkov <rvolkov@v1ros.org>
Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
2016-01-01 16:24:41 +03:00
|
|
|
#define MIN_OSCR_DELTA 16
|
|
|
|
|
2010-12-23 15:11:21 +03:00
|
|
|
static void __iomem *regbase;
|
|
|
|
|
2016-12-21 22:32:01 +03:00
|
|
|
static u64 vt8500_timer_read(struct clocksource *cs)
|
2010-12-23 15:11:21 +03:00
|
|
|
{
|
|
|
|
int loops = msecs_to_loops(10);
|
|
|
|
writel(3, regbase + TIMER_CTRL_VAL);
|
|
|
|
while ((readl((regbase + TIMER_AS_VAL)) & TIMER_COUNT_R_ACTIVE)
|
|
|
|
&& --loops)
|
|
|
|
cpu_relax();
|
|
|
|
return readl(regbase + TIMER_COUNT_VAL);
|
|
|
|
}
|
|
|
|
|
2012-08-03 13:00:06 +04:00
|
|
|
static struct clocksource clocksource = {
|
2010-12-23 15:11:21 +03:00
|
|
|
.name = "vt8500_timer",
|
|
|
|
.rating = 200,
|
|
|
|
.read = vt8500_timer_read,
|
|
|
|
.mask = CLOCKSOURCE_MASK(32),
|
|
|
|
.flags = CLOCK_SOURCE_IS_CONTINUOUS,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int vt8500_timer_set_next_event(unsigned long cycles,
|
|
|
|
struct clock_event_device *evt)
|
|
|
|
{
|
|
|
|
int loops = msecs_to_loops(10);
|
2016-12-21 22:32:01 +03:00
|
|
|
u64 alarm = clocksource.read(&clocksource) + cycles;
|
2010-12-23 15:11:21 +03:00
|
|
|
while ((readl(regbase + TIMER_AS_VAL) & TIMER_MATCH_W_ACTIVE)
|
|
|
|
&& --loops)
|
|
|
|
cpu_relax();
|
|
|
|
writel((unsigned long)alarm, regbase + TIMER_MATCH_VAL);
|
|
|
|
|
clocksource/drivers/vt8500: Increase the minimum delta
The vt8500 clocksource driver declares itself as capable to handle the
minimum delay of 4 cycles by passing the value into
clockevents_config_and_register(). The vt8500_timer_set_next_event()
requires the passed cycles value to be at least 16. The impact is that
userspace hangs in nanosleep() calls with small delay intervals.
This problem is reproducible in Linux 4.2 starting from:
c6eb3f70d448 ('hrtimer: Get rid of hrtimer softirq')
From Russell King, more detailed explanation:
"It's a speciality of the StrongARM/PXA hardware. It takes a certain
number of OSCR cycles for the value written to hit the compare registers.
So, if a very small delta is written (eg, the compare register is written
with a value of OSCR + 1), the OSCR will have incremented past this value
before it hits the underlying hardware. The result is, that you end up
waiting a very long time for the OSCR to wrap before the event fires.
So, we introduce a check in set_next_event() to detect this and return
-ETIME if the calculated delta is too small, which causes the generic
clockevents code to retry after adding the min_delta specified in
clockevents_config_and_register() to the current time value.
min_delta must be sufficient that we don't re-trip the -ETIME check - if
we do, we will return -ETIME, forward the next event time, try to set it,
return -ETIME again, and basically lock the system up. So, min_delta
must be larger than the check inside set_next_event(). A factor of two
was chosen to ensure that this situation would never occur.
The PXA code worked on PXA systems for years, and I'd suggest no one
changes this mechanism without access to a wide range of PXA systems,
otherwise they're risking breakage."
Cc: stable@vger.kernel.org
Cc: Russell King <linux@arm.linux.org.uk>
Acked-by: Alexey Charkov <alchark@gmail.com>
Signed-off-by: Roman Volkov <rvolkov@v1ros.org>
Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
2016-01-01 16:24:41 +03:00
|
|
|
if ((signed)(alarm - clocksource.read(&clocksource)) <= MIN_OSCR_DELTA)
|
2010-12-23 15:11:21 +03:00
|
|
|
return -ETIME;
|
|
|
|
|
|
|
|
writel(1, regbase + TIMER_IER_VAL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-18 13:54:54 +03:00
|
|
|
static int vt8500_shutdown(struct clock_event_device *evt)
|
2010-12-23 15:11:21 +03:00
|
|
|
{
|
2015-06-18 13:54:54 +03:00
|
|
|
writel(readl(regbase + TIMER_CTRL_VAL) | 1, regbase + TIMER_CTRL_VAL);
|
|
|
|
writel(0, regbase + TIMER_IER_VAL);
|
|
|
|
return 0;
|
2010-12-23 15:11:21 +03:00
|
|
|
}
|
|
|
|
|
2012-08-03 13:00:06 +04:00
|
|
|
static struct clock_event_device clockevent = {
|
2015-06-18 13:54:54 +03:00
|
|
|
.name = "vt8500_timer",
|
|
|
|
.features = CLOCK_EVT_FEAT_ONESHOT,
|
|
|
|
.rating = 200,
|
|
|
|
.set_next_event = vt8500_timer_set_next_event,
|
|
|
|
.set_state_shutdown = vt8500_shutdown,
|
|
|
|
.set_state_oneshot = vt8500_shutdown,
|
2010-12-23 15:11:21 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static irqreturn_t vt8500_timer_interrupt(int irq, void *dev_id)
|
|
|
|
{
|
|
|
|
struct clock_event_device *evt = dev_id;
|
|
|
|
writel(0xf, regbase + TIMER_STATUS_VAL);
|
|
|
|
evt->event_handler(evt);
|
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
2012-08-03 13:00:06 +04:00
|
|
|
static struct irqaction irq = {
|
2010-12-23 15:11:21 +03:00
|
|
|
.name = "vt8500_timer",
|
2013-12-09 13:38:50 +04:00
|
|
|
.flags = IRQF_TIMER | IRQF_IRQPOLL,
|
2010-12-23 15:11:21 +03:00
|
|
|
.handler = vt8500_timer_interrupt,
|
|
|
|
.dev_id = &clockevent,
|
|
|
|
};
|
|
|
|
|
2016-06-07 00:30:04 +03:00
|
|
|
static int __init vt8500_timer_init(struct device_node *np)
|
2010-12-23 15:11:21 +03:00
|
|
|
{
|
2016-06-07 00:30:04 +03:00
|
|
|
int timer_irq, ret;
|
2012-08-03 13:00:06 +04:00
|
|
|
|
|
|
|
regbase = of_iomap(np, 0);
|
|
|
|
if (!regbase) {
|
|
|
|
pr_err("%s: Missing iobase description in Device Tree\n",
|
|
|
|
__func__);
|
2016-06-07 00:30:04 +03:00
|
|
|
return -ENXIO;
|
2012-08-03 13:00:06 +04:00
|
|
|
}
|
2016-06-07 00:30:04 +03:00
|
|
|
|
2012-08-03 13:00:06 +04:00
|
|
|
timer_irq = irq_of_parse_and_map(np, 0);
|
|
|
|
if (!timer_irq) {
|
|
|
|
pr_err("%s: Missing irq description in Device Tree\n",
|
|
|
|
__func__);
|
2016-06-07 00:30:04 +03:00
|
|
|
return -EINVAL;
|
2012-08-03 13:00:06 +04:00
|
|
|
}
|
2010-12-23 15:11:21 +03:00
|
|
|
|
|
|
|
writel(1, regbase + TIMER_CTRL_VAL);
|
|
|
|
writel(0xf, regbase + TIMER_STATUS_VAL);
|
|
|
|
writel(~0, regbase + TIMER_MATCH_VAL);
|
|
|
|
|
2016-06-07 00:30:04 +03:00
|
|
|
ret = clocksource_register_hz(&clocksource, VT8500_TIMER_HZ);
|
|
|
|
if (ret) {
|
2012-08-03 13:00:06 +04:00
|
|
|
pr_err("%s: vt8500_timer_init: clocksource_register failed for %s\n",
|
2016-06-07 00:30:04 +03:00
|
|
|
__func__, clocksource.name);
|
|
|
|
return ret;
|
|
|
|
}
|
2010-12-23 15:11:21 +03:00
|
|
|
|
|
|
|
clockevent.cpumask = cpumask_of(0);
|
|
|
|
|
2016-06-07 00:30:04 +03:00
|
|
|
ret = setup_irq(timer_irq, &irq);
|
|
|
|
if (ret) {
|
2012-08-03 13:00:06 +04:00
|
|
|
pr_err("%s: setup_irq failed for %s\n", __func__,
|
|
|
|
clockevent.name);
|
2016-06-07 00:30:04 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-01-12 15:50:05 +04:00
|
|
|
clockevents_config_and_register(&clockevent, VT8500_TIMER_HZ,
|
clocksource/drivers/vt8500: Increase the minimum delta
The vt8500 clocksource driver declares itself as capable to handle the
minimum delay of 4 cycles by passing the value into
clockevents_config_and_register(). The vt8500_timer_set_next_event()
requires the passed cycles value to be at least 16. The impact is that
userspace hangs in nanosleep() calls with small delay intervals.
This problem is reproducible in Linux 4.2 starting from:
c6eb3f70d448 ('hrtimer: Get rid of hrtimer softirq')
From Russell King, more detailed explanation:
"It's a speciality of the StrongARM/PXA hardware. It takes a certain
number of OSCR cycles for the value written to hit the compare registers.
So, if a very small delta is written (eg, the compare register is written
with a value of OSCR + 1), the OSCR will have incremented past this value
before it hits the underlying hardware. The result is, that you end up
waiting a very long time for the OSCR to wrap before the event fires.
So, we introduce a check in set_next_event() to detect this and return
-ETIME if the calculated delta is too small, which causes the generic
clockevents code to retry after adding the min_delta specified in
clockevents_config_and_register() to the current time value.
min_delta must be sufficient that we don't re-trip the -ETIME check - if
we do, we will return -ETIME, forward the next event time, try to set it,
return -ETIME again, and basically lock the system up. So, min_delta
must be larger than the check inside set_next_event(). A factor of two
was chosen to ensure that this situation would never occur.
The PXA code worked on PXA systems for years, and I'd suggest no one
changes this mechanism without access to a wide range of PXA systems,
otherwise they're risking breakage."
Cc: stable@vger.kernel.org
Cc: Russell King <linux@arm.linux.org.uk>
Acked-by: Alexey Charkov <alchark@gmail.com>
Signed-off-by: Roman Volkov <rvolkov@v1ros.org>
Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
2016-01-01 16:24:41 +03:00
|
|
|
MIN_OSCR_DELTA * 2, 0xf0000000);
|
2016-06-07 00:30:04 +03:00
|
|
|
|
|
|
|
return 0;
|
2010-12-23 15:11:21 +03:00
|
|
|
}
|
|
|
|
|
2017-05-26 17:56:11 +03:00
|
|
|
TIMER_OF_DECLARE(vt8500, "via,vt8500-timer", vt8500_timer_init);
|