From: Soren Brinkmann <soren.brinkmann@xilinx.com>
To: Rob Herring <rob.herring@calxeda.com>,
Pawel Moll <pawel.moll@arm.com>,
Mark Rutland <mark.rutland@arm.com>,
Stephen Warren <swarren@wwwdotorg.org>,
Ian Campbell <ijc+devicetree@hellion.org.uk>,
Russell King <linux@arm.linux.org.uk>,
Michal Simek <michal.simek@xilinx.com>,
Daniel Lezcano <daniel.lezcano@linaro.org>,
Thomas Gleixner <tglx@linutronix.de>
Cc: linux-kernel@vger.kernel.org,
linux-arm-kernel@lists.infradead.org, devicetree@vger.kernel.org,
Soren Brinkmann <soren.brinkmann@xilinx.com>
Subject: [PATCH v2 6/9] clocksource/cadence_ttc: Overhaul clocksource frequency adjustment
Date: Tue, 26 Nov 2013 17:04:53 -0800 [thread overview]
Message-ID: <1385514296-26702-7-git-send-email-soren.brinkmann@xilinx.com> (raw)
In-Reply-To: <1385514296-26702-1-git-send-email-soren.brinkmann@xilinx.com>
The currently used method adjusting the clocksource to a changing input
frequency does not work on kernels from 3.11 on.
The new approach is to keep the timer frequency as constant as possible.
I.e.
- due to the TTC's prescaler limitations, allow frequency changes
only if the frequency scales by a power of 2
- adjust the counter's divider on the fly when a frequency change
occurs
This limits cpufreq to scale by certain factors only.
But we may keep the time base somewhat constant, so that sleep() & co
keep working as expected, while supporting cpufreq.
Signed-off-by: Soren Brinkmann <soren.brinkmann@xilinx.com>
---
v2:
- remove PM notifier and associated code
---
drivers/clocksource/cadence_ttc_timer.c | 112 ++++++++++++++++++++++++++------
1 file changed, 92 insertions(+), 20 deletions(-)
diff --git a/drivers/clocksource/cadence_ttc_timer.c b/drivers/clocksource/cadence_ttc_timer.c
index 77517675653e..b0cd7368b466 100644
--- a/drivers/clocksource/cadence_ttc_timer.c
+++ b/drivers/clocksource/cadence_ttc_timer.c
@@ -16,6 +16,7 @@
*/
#include <linux/clk.h>
+#include <linux/clk-provider.h>
#include <linux/interrupt.h>
#include <linux/clockchips.h>
#include <linux/of_address.h>
@@ -52,6 +53,8 @@
#define TTC_CNT_CNTRL_DISABLE_MASK 0x1
#define TTC_CLK_CNTRL_CSRC_MASK (1 << 5) /* clock source */
+#define TTC_CLK_CNTRL_PSV_MASK 0x1e
+#define TTC_CLK_CNTRL_PSV_SHIFT 1
/*
* Setup the timers to use pre-scaling, using a fixed value for now that will
@@ -63,6 +66,8 @@
#define CLK_CNTRL_PRESCALE_EN 1
#define CNT_CNTRL_RESET (1 << 4)
+#define MAX_F_ERR 50
+
/**
* struct ttc_timer - This definition defines local timer structure
*
@@ -82,6 +87,9 @@ struct ttc_timer {
container_of(x, struct ttc_timer, clk_rate_change_nb)
struct ttc_timer_clocksource {
+ int scale_dir;
+ u32 scale_clk_ctrl_reg_old;
+ u32 scale_clk_ctrl_reg_new;
struct ttc_timer ttc;
struct clocksource cs;
};
@@ -229,32 +237,96 @@ static int ttc_rate_change_clocksource_cb(struct notifier_block *nb,
struct ttc_timer_clocksource, ttc);
switch (event) {
- case POST_RATE_CHANGE:
+ case PRE_RATE_CHANGE:
+ {
+ u32 psv;
+ unsigned long factor, rate_low, rate_high;
+
+ if (ndata->new_rate == ndata->old_rate) {
+ ttccs->scale_dir = 0;
+ return NOTIFY_OK;
+ }
+
+ if (ndata->new_rate > ndata->old_rate) {
+ factor = DIV_ROUND_CLOSEST(ndata->new_rate,
+ ndata->old_rate);
+ ttccs->scale_dir = 1;
+ rate_low = ndata->old_rate;
+ rate_high = ndata->new_rate;
+ } else {
+ factor = DIV_ROUND_CLOSEST(ndata->old_rate,
+ ndata->new_rate);
+ ttccs->scale_dir = -1;
+ rate_low = ndata->new_rate;
+ rate_high = ndata->old_rate;
+ }
+
+ if (!is_power_of_2(factor))
+ return NOTIFY_BAD;
+
+ if (abs(rate_high - (factor * rate_low)) > MAX_F_ERR)
+ return NOTIFY_BAD;
+
+ factor = __ilog2_u32(factor);
+
/*
- * Do whatever is necessary to maintain a proper time base
- *
- * I cannot find a way to adjust the currently used clocksource
- * to the new frequency. __clocksource_updatefreq_hz() sounds
- * good, but does not work. Not sure what's that missing.
- *
- * This approach works, but triggers two clocksource switches.
- * The first after unregister to clocksource jiffies. And
- * another one after the register to the newly registered timer.
- *
- * Alternatively we could 'waste' another HW timer to ping pong
- * between clock sources. That would also use one register and
- * one unregister call, but only trigger one clocksource switch
- * for the cost of another HW timer used by the OS.
+ * store timer clock ctrl register so we can restore it in case
+ * of an abort.
*/
- clocksource_unregister(&ttccs->cs);
- clocksource_register_hz(&ttccs->cs,
- ndata->new_rate / PRESCALE);
- /* fall through */
- case PRE_RATE_CHANGE:
+ ttccs->scale_clk_ctrl_reg_old =
+ __raw_readl(ttccs->ttc.base_addr +
+ TTC_CLK_CNTRL_OFFSET);
+
+ psv = (ttccs->scale_clk_ctrl_reg_old &
+ TTC_CLK_CNTRL_PSV_MASK) >>
+ TTC_CLK_CNTRL_PSV_SHIFT;
+ if (ttccs->scale_dir < 0)
+ psv -= factor;
+ else
+ psv += factor;
+
+ /* prescaler within legal range? */
+ if (psv & ~(TTC_CLK_CNTRL_PSV_MASK >> TTC_CLK_CNTRL_PSV_SHIFT))
+ return NOTIFY_BAD;
+
+ ttccs->scale_clk_ctrl_reg_new = ttccs->scale_clk_ctrl_reg_old &
+ ~TTC_CLK_CNTRL_PSV_MASK;
+ ttccs->scale_clk_ctrl_reg_new |= psv << TTC_CLK_CNTRL_PSV_SHIFT;
+
+
+ /* scale down: adjust divider in post-change notification */
+ if (ttccs->scale_dir < 0)
+ return NOTIFY_DONE;
+
+ /* scale up: adjust divider now - before frequency change */
+ __raw_writel(ttccs->scale_clk_ctrl_reg_new,
+ ttccs->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
+ break;
+ }
+ case POST_RATE_CHANGE:
+ /* scale up: pre-change notification did the adjustment */
+ if (ttccs->scale_dir >= 0)
+ return NOTIFY_OK;
+
+ /* scale down: adjust divider now - after frequency change */
+ __raw_writel(ttccs->scale_clk_ctrl_reg_new,
+ ttccs->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
+ break;
+
case ABORT_RATE_CHANGE:
+ /* we have to undo the adjustment in case we scale up */
+ if (ttccs->scale_dir <= 0)
+ return NOTIFY_OK;
+
+ /* restore original register value */
+ __raw_writel(ttccs->scale_clk_ctrl_reg_old,
+ ttccs->ttc.base_addr + TTC_CLK_CNTRL_OFFSET);
+ /* fall through */
default:
return NOTIFY_DONE;
}
+
+ return NOTIFY_DONE;
}
static void __init ttc_setup_clocksource(struct clk *clk, void __iomem *base)
--
1.8.4.4
next prev parent reply other threads:[~2013-11-27 1:04 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-11-27 1:04 [PATCH v2 0/9] arm: zynq: Add support for cpufreq Soren Brinkmann
2013-11-27 1:04 ` [PATCH v2 1/9] arm: dt: zynq: Remove 'clock-ranges' from TTC nodes Soren Brinkmann
2013-12-12 8:53 ` Michal Simek
2013-12-12 17:01 ` Sören Brinkmann
2013-12-12 19:07 ` Michal Simek
2013-11-27 1:04 ` [PATCH v2 2/9] arm: dt: zynq: Add 'cpus' node Soren Brinkmann
2013-11-27 1:04 ` [PATCH v2 3/9] clocksource/cadence_ttc: Store timer frequency in driver data Soren Brinkmann
2013-12-17 19:21 ` Sören Brinkmann
[not found] ` <5cff3201-db97-4061-a686-bf79ac17d4fe-W/Q257+MkyH5op9OF0Koj7jjLBE8jN/0@public.gmane.org>
2013-12-18 14:53 ` Daniel Lezcano
[not found] ` <52B1B6FF.7000400-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
2013-12-18 16:47 ` Sören Brinkmann
2013-12-18 21:58 ` Daniel Lezcano
2013-12-19 18:32 ` Sören Brinkmann
2013-12-19 20:53 ` Daniel Lezcano
[not found] ` <52B35CBA.7020202-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
2013-12-19 21:23 ` Sören Brinkmann
[not found] ` <f5bcb974-8645-48d3-b03c-f894c0bb5b7b-6DEzpURlfbPUuUXyfqFqSbjjLBE8jN/0@public.gmane.org>
2013-12-19 21:39 ` Daniel Lezcano
2013-11-27 1:04 ` [PATCH v2 4/9] clocksource/cadence_ttc: Use enable/disable_irq Soren Brinkmann
[not found] ` <1385514296-26702-5-git-send-email-soren.brinkmann-gjFFaj9aHVfQT0dZR+AlfA@public.gmane.org>
2013-11-28 11:55 ` Daniel Lezcano
[not found] ` <52972F3A.9090103-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
2013-11-28 14:18 ` Thomas Gleixner
[not found] ` <alpine.DEB.2.02.1311281507280.30673-3cz04HxQygjZikZi3RtOZ1XZhhPuCNm+@public.gmane.org>
2013-11-28 18:36 ` Sören Brinkmann
[not found] ` <b2ed3cbc-c9df-4235-8137-93514c05bffa-6DEzpURlfbPnHLUNXTEFU7jjLBE8jN/0@public.gmane.org>
2013-11-28 19:07 ` Thomas Gleixner
2013-12-06 22:47 ` Sören Brinkmann
[not found] ` <f6d25456-8e30-4792-b184-21eb02dfaa1c-dAX9Bq04yCRZbvUCbuG1mrjjLBE8jN/0@public.gmane.org>
2013-12-07 10:56 ` Thomas Gleixner
2013-12-10 0:34 ` [PATCH 0/2] clockevents Soren Brinkmann
2013-12-10 0:34 ` [PATCH 1/2] time: Serialize calls to 'clockevents_update_freq' in the timing core Soren Brinkmann
[not found] ` <1386635686-15686-2-git-send-email-soren.brinkmann-gjFFaj9aHVfQT0dZR+AlfA@public.gmane.org>
2013-12-11 14:32 ` Daniel Lezcano
[not found] ` <52A87799.8010300-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
2013-12-11 20:09 ` Sören Brinkmann
[not found] ` <ba4dce4a-1332-4fd8-80de-41347bb5b7b2-8XeO8fnFoNFEus+KprP3J7jjLBE8jN/0@public.gmane.org>
2013-12-12 12:07 ` Daniel Lezcano
2013-12-10 0:34 ` [PATCH 2/2] time: clockevents: Adjust timer interval when frequency changes Soren Brinkmann
2013-11-27 1:04 ` [PATCH v2 5/9] clocksource/cadence_ttc: Adjust interval in clock notifier Soren Brinkmann
[not found] ` <1385514296-26702-6-git-send-email-soren.brinkmann-gjFFaj9aHVfQT0dZR+AlfA@public.gmane.org>
2013-12-12 12:15 ` Daniel Lezcano
2013-12-12 18:44 ` Sören Brinkmann
2013-11-27 1:04 ` Soren Brinkmann [this message]
[not found] ` <1385514296-26702-1-git-send-email-soren.brinkmann-gjFFaj9aHVfQT0dZR+AlfA@public.gmane.org>
2013-11-27 1:04 ` [PATCH v2 7/9] clocksource/cadence_ttc: Use only one counter Soren Brinkmann
2013-11-27 1:04 ` [PATCH v2 8/9] arm: zynq: Don't use arm_global_timer with cpufreq Soren Brinkmann
2013-11-27 1:04 ` [PATCH v2 9/9] arm: zynq: Add support for cpufreq Soren Brinkmann
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=1385514296-26702-7-git-send-email-soren.brinkmann@xilinx.com \
--to=soren.brinkmann@xilinx.com \
--cc=daniel.lezcano@linaro.org \
--cc=devicetree@vger.kernel.org \
--cc=ijc+devicetree@hellion.org.uk \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@arm.linux.org.uk \
--cc=mark.rutland@arm.com \
--cc=michal.simek@xilinx.com \
--cc=pawel.moll@arm.com \
--cc=rob.herring@calxeda.com \
--cc=swarren@wwwdotorg.org \
--cc=tglx@linutronix.de \
/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).