linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alexander Kochetkov <al.kochet@gmail.com>
To: linux-kernel@vger.kernel.org, devicetree@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	linux-rockchip@lists.infradead.org
Cc: Thomas Gleixner <tglx@linutronix.de>,
	Heiko Stuebner <heiko@sntech.de>,
	Mark Rutland <mark.rutland@arm.com>,
	Rob Herring <robh+dt@kernel.org>,
	Russell King <linux@armlinux.org.uk>,
	Caesar Wang <wxt@rock-chips.com>,
	Huang Tao <huangtao@rock-chips.com>,
	Daniel Lezcano <daniel.lezcano@linaro.org>,
	Alexander Kochetkov <al.kochet@gmail.com>
Subject: [PATCHv2 11/11] clocksource/drivers/rockchip_timer: implement clocksource timer
Date: Mon, 28 Nov 2016 17:31:26 +0300	[thread overview]
Message-ID: <1480343486-25539-12-git-send-email-al.kochet@gmail.com> (raw)
In-Reply-To: <1480343486-25539-1-git-send-email-al.kochet@gmail.com>

The clock supplying the arm-global-timer on the rk3188 is coming from the
the cpu clock itself and thus changes its rate everytime cpufreq adjusts
the cpu frequency making this timer unsuitable as a stable clocksource.

The rk3188, rk3288 and following socs share a separate timer block already
handled by the rockchip-timer driver. Therefore adapt this driver to also
be able to act as clocksource on rk3188.

In order to test clocksource you can run following commands and check
how much time it take in real. On rk3188 it take about ~45 seconds.
Such error cannot be fixed using NTP. Haven't test clocksource
on rk3288 and onwards. Guess they can also have unstable clocksource.

    cpufreq-set -f 1.6GHZ
    date; sleep 60; date

In order to use the patch you need to declare two timers in the dts
file. The first timer will be initialized as clockevent provider
and the second one as clocksource. The clockevent must be from
alive subsystem as it used as backup for the local timers at sleep
time.

In order to resolve ambiguity between timers in the device tree,
it is possible to correctly number the timers using "aliases" node.

The patch does not break compatibility with older device tree files.
The older device tree files contain only one timer. The timer
will be initialized as clockevent, as expected.

Signed-off-by: Alexander Kochetkov <al.kochet@gmail.com>
---
 drivers/clocksource/rockchip_timer.c |  101 ++++++++++++++++++++++++++++------
 1 file changed, 85 insertions(+), 16 deletions(-)

diff --git a/drivers/clocksource/rockchip_timer.c b/drivers/clocksource/rockchip_timer.c
index 6224aa9..430b182 100644
--- a/drivers/clocksource/rockchip_timer.c
+++ b/drivers/clocksource/rockchip_timer.c
@@ -11,6 +11,7 @@
 #include <linux/clockchips.h>
 #include <linux/init.h>
 #include <linux/interrupt.h>
+#include <linux/sched_clock.h>
 #include <linux/of.h>
 #include <linux/of_address.h>
 #include <linux/of_irq.h>
@@ -42,7 +43,19 @@ struct rk_clock_event_device {
 	struct rk_timer timer;
 };
 
+struct rk_clocksource {
+	struct clocksource cs;
+	struct rk_timer timer;
+};
+
+enum {
+	ROCKCHIP_CLKSRC_CLOCKEVENT = 0,
+	ROCKCHIP_CLKSRC_CLOCKSOURCE = 1,
+};
+
 static struct rk_clock_event_device bc_timer;
+static struct rk_clocksource cs_timer;
+static int rk_next_clksrc = ROCKCHIP_CLKSRC_CLOCKEVENT;
 
 static inline struct rk_clock_event_device*
 rk_clock_event_device(struct clock_event_device *ce)
@@ -143,13 +156,46 @@ static irqreturn_t rk_timer_interrupt(int irq, void *dev_id)
 	return IRQ_HANDLED;
 }
 
+static cycle_t rk_timer_clocksource_read(struct clocksource *cs)
+{
+	struct rk_clocksource *_cs =
+		container_of(cs, struct rk_clocksource, cs);
+
+	return ~rk_timer_counter_read(&_cs->timer);
+}
+
+static u64 notrace rk_timer_sched_clock_read(void)
+{
+	struct rk_clocksource *_cs = &cs_timer;
+
+	return ~rk_timer_counter_read(&_cs->timer);
+}
+
 static int __init rk_timer_init(struct device_node *np, u32 ctrl_reg)
 {
-	struct clock_event_device *ce = &bc_timer.ce;
-	struct rk_timer *timer = &bc_timer.timer;
+	struct clock_event_device *ce = NULL;
+	struct clocksource *cs = NULL;
+	struct rk_timer *timer = NULL;
 	struct clk *timer_clk;
 	struct clk *pclk;
 	int ret = -EINVAL, irq;
+	int clksrc;
+
+	clksrc = rk_next_clksrc;
+	rk_next_clksrc++;
+
+	switch (clksrc) {
+	case ROCKCHIP_CLKSRC_CLOCKEVENT:
+		ce = &bc_timer.ce;
+		timer = &bc_timer.timer;
+		break;
+	case ROCKCHIP_CLKSRC_CLOCKSOURCE:
+		cs = &cs_timer.cs;
+		timer = &cs_timer.timer;
+		break;
+	default:
+		return -ENODEV;
+	}
 
 	timer->base = of_iomap(np, 0);
 	if (!timer->base) {
@@ -193,26 +239,49 @@ static int __init rk_timer_init(struct device_node *np, u32 ctrl_reg)
 		goto out_irq;
 	}
 
-	ce->name = TIMER_NAME;
-	ce->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT |
-		       CLOCK_EVT_FEAT_DYNIRQ;
-	ce->set_next_event = rk_timer_set_next_event;
-	ce->set_state_shutdown = rk_timer_shutdown;
-	ce->set_state_periodic = rk_timer_set_periodic;
-	ce->irq = irq;
-	ce->cpumask = cpu_possible_mask;
-	ce->rating = 250;
+	if (ce) {
+		ce->name = TIMER_NAME;
+		ce->features = CLOCK_EVT_FEAT_PERIODIC |
+			       CLOCK_EVT_FEAT_ONESHOT |
+			       CLOCK_EVT_FEAT_DYNIRQ;
+		ce->set_next_event = rk_timer_set_next_event;
+		ce->set_state_shutdown = rk_timer_shutdown;
+		ce->set_state_periodic = rk_timer_set_periodic;
+		ce->irq = irq;
+		ce->cpumask = cpu_possible_mask;
+		ce->rating = 250;
+	}
+
+	if (cs) {
+		cs->name = TIMER_NAME;
+		cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
+		cs->mask = CLOCKSOURCE_MASK(64);
+		cs->read = rk_timer_clocksource_read;
+		cs->rating = 350;
+	}
 
 	rk_timer_interrupt_clear(timer);
 	rk_timer_disable(timer);
 
-	ret = request_irq(irq, rk_timer_interrupt, IRQF_TIMER, TIMER_NAME, ce);
-	if (ret) {
-		pr_err("Failed to initialize '%s': %d\n", TIMER_NAME, ret);
-		goto out_irq;
+	if (ce) {
+		ret = request_irq(irq, rk_timer_interrupt, IRQF_TIMER,
+				  TIMER_NAME, ce);
+		if (ret) {
+			pr_err("Failed to initialize '%s': %d\n",
+				TIMER_NAME, ret);
+			goto out_irq;
+		}
+
+		clockevents_config_and_register(ce, timer->freq, 1, UINT_MAX);
 	}
 
-	clockevents_config_and_register(ce, timer->freq, 1, UINT_MAX);
+	if (cs) {
+		rk_timer_update_counter(U64_MAX, timer);
+		rk_timer_enable(timer, 0);
+		clocksource_register_hz(cs, timer->freq);
+		sched_clock_register(rk_timer_sched_clock_read, 64,
+				     timer->freq);
+	}
 
 	return 0;
 
-- 
1.7.9.5

  parent reply	other threads:[~2016-11-28 14:33 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-28 14:31 [PATCHv2 00/11] Implement clocksource for rockchip SoC using rockchip timer Alexander Kochetkov
2016-11-28 14:31 ` [PATCHv2 01/11] dt-bindings: clarify compatible property for rockchip timers Alexander Kochetkov
2016-11-28 14:31 ` [PATCHv2 02/11] ARM: dts: rockchip: update compatible property for rk3228 timer Alexander Kochetkov
2016-11-28 14:31 ` [PATCHv2 03/11] ARM: dts: rockchip: update compatible property for rk3229 timer Alexander Kochetkov
2016-11-28 14:31 ` [PATCHv2 04/11] ARM: dts: rockchip: add timer entries to rk3188 dtsi Alexander Kochetkov
2016-11-28 14:31 ` [PATCHv2 05/11] clocksource/drivers/rockchip_timer: split bc_timer into rk_timer and rk_clock_event_device Alexander Kochetkov
2016-11-28 14:31 ` [PATCHv2 06/11] clocksource/drivers/rockchip_timer: low level routines take rk_timer as parameter Alexander Kochetkov
2016-11-28 14:31 ` [PATCHv2 07/11] clocksource/drivers/rockchip_timer: drop unused rk_base() and rk_ctrl() Alexander Kochetkov
2016-11-28 14:31 ` [PATCHv2 08/11] clocksource/drivers/rockchip_timer: move TIMER_INT_UNMASK out of rk_timer_enable() Alexander Kochetkov
2016-11-28 14:31 ` [PATCHv2 09/11] clocksource/drivers/rockchip_timer: implement loading 64bit value into timer Alexander Kochetkov
2016-11-28 14:31 ` [PATCHv2 10/11] clocksource/drivers/rockchip_timer: implement reading 64bit value from timer Alexander Kochetkov
2016-11-28 14:31 ` Alexander Kochetkov [this message]
2016-11-29 13:45 ` [PATCH v3 00/13] Implement clocksource for rockchip SoC using rockchip timer Alexander Kochetkov
2016-11-29 13:45   ` [PATCH v3 01/13] dt-bindings: clarify compatible property for rockchip timers Alexander Kochetkov
2016-11-29 13:45   ` [PATCH v3 02/13] ARM: dts: rockchip: update compatible property for rk3228 timer Alexander Kochetkov
2016-11-29 13:45   ` [PATCH v3 03/13] ARM: dts: rockchip: update compatible property for rk3229 timer Alexander Kochetkov
2016-11-29 13:45   ` [PATCH v3 04/13] ARM: dts: rockchip: add timer entries to rk3188 dtsi Alexander Kochetkov
2016-11-29 13:45   ` [PATCH v3 05/13] ARM: dts: rockchip: disable arm-global-timer for rk3188 Alexander Kochetkov
2016-11-29 13:45   ` [PATCH v3 06/13] clocksource/drivers/rockchip_timer: split bc_timer into rk_timer and rk_clock_event_device Alexander Kochetkov
2016-11-29 13:45   ` [PATCH v3 07/13] clocksource/drivers/rockchip_timer: low level routines take rk_timer as parameter Alexander Kochetkov
2016-11-29 13:45   ` [PATCH v3 08/13] clocksource/drivers/rockchip_timer: drop unused rk_base() and rk_ctrl() Alexander Kochetkov
2016-11-29 15:03     ` Heiko Stübner
2016-11-29 13:45   ` [PATCH v3 09/13] clocksource/drivers/rockchip_timer: move TIMER_INT_UNMASK out of rk_timer_enable() Alexander Kochetkov
2016-11-29 13:45   ` [PATCH v3 10/13] clocksource/drivers/rockchip_timer: implement loading 64bit value into timer Alexander Kochetkov
2016-11-29 13:45   ` [PATCH v3 11/13] clocksource/drivers/rockchip_timer: implement reading 64bit value from timer Alexander Kochetkov
2016-11-29 13:45   ` [PATCH v3 12/13] clocksource/drivers/rockchip_timer: implement clocksource timer Alexander Kochetkov
2016-11-29 13:45   ` [PATCH v3 13/13] clocksource/drivers/rockchip_timer: Prevent ftrace recursion Alexander Kochetkov
2016-11-29 15:01     ` Heiko Stübner
2016-11-29 15:36       ` Alexander Kochetkov
2016-11-29 16:14 ` [PATCH v4 0/9] Implement clocksource for rockchip SoC using rockchip timer Alexander Kochetkov
2016-11-29 16:14   ` [PATCH v4 1/9] dt-bindings: clarify compatible property for rockchip timers Alexander Kochetkov
2016-12-05 22:22     ` Rob Herring
2016-11-29 16:14   ` [PATCH v4 2/9] ARM: dts: rockchip: update compatible property for rk3228 timer Alexander Kochetkov
2017-01-23 15:40     ` Daniel Lezcano
2017-01-23 16:12       ` Heiko Stübner
2017-01-23 16:13       ` Alexander Kochetkov
2016-11-29 16:14   ` [PATCH v4 3/9] ARM: dts: rockchip: update compatible property for rk3229 timer Alexander Kochetkov
2016-11-29 16:14   ` [PATCH v4 4/9] ARM: dts: rockchip: add timer entries to rk3188 SoC Alexander Kochetkov
2016-11-29 16:14   ` [PATCH v4 5/9] ARM: dts: rockchip: disable arm-global-timer for rk3188 Alexander Kochetkov
2017-01-23 16:14     ` Heiko Stübner
2016-11-29 16:14   ` [PATCH v4 6/9] clocksource/drivers/rockchip_timer: split bc_timer into rk_timer and rk_clock_event_device Alexander Kochetkov
2017-01-23 16:26     ` Daniel Lezcano
2017-01-23 16:45       ` Alexander Kochetkov
2016-11-29 16:14   ` [PATCH v4 7/9] clocksource/drivers/rockchip_timer: low level routines take rk_timer as parameter Alexander Kochetkov
2016-11-29 16:14   ` [PATCH v4 8/9] clocksource/drivers/rockchip_timer: move TIMER_INT_UNMASK out of rk_timer_enable() Alexander Kochetkov
2016-11-29 16:14   ` [PATCH v4 9/9] clocksource/drivers/rockchip_timer: implement clocksource timer Alexander Kochetkov
2017-01-23 17:09     ` Heiko Stübner
2016-12-21 14:21   ` [PATCH v4 0/9] Implement clocksource for rockchip SoC using rockchip timer Alexander Kochetkov
2017-01-23 14:47     ` Daniel Lezcano
2017-01-23 17:12       ` Heiko Stübner
2017-01-23 17:24         ` Alexander Kochetkov
2017-01-23 17:57           ` Daniel Lezcano
2017-01-26 11:08             ` Alexander Kochetkov
2017-01-26 13:19               ` Daniel Lezcano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1480343486-25539-12-git-send-email-al.kochet@gmail.com \
    --to=al.kochet@gmail.com \
    --cc=daniel.lezcano@linaro.org \
    --cc=devicetree@vger.kernel.org \
    --cc=heiko@sntech.de \
    --cc=huangtao@rock-chips.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rockchip@lists.infradead.org \
    --cc=linux@armlinux.org.uk \
    --cc=mark.rutland@arm.com \
    --cc=robh+dt@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=wxt@rock-chips.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).