From 8a3269fc21cc4405d80b362139c078cf655a505a Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Tue, 12 May 2009 10:30:41 -0700 Subject: [ARM] orion: sched_clock implementation for orion platforms sched_clock implementation for orion platform. Its realized using free-running clocksource timer, which provides a resolution of 7.5ns (depending on tclk). It's derived from PXA's sched_clock implementation. [ nico: renamed orion2ns to tclk2ns, fixed max value in the comment ] Signed-off-by: Stefan Agner Signed-off-by: Nicolas Pitre --- arch/arm/plat-orion/time.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'arch/arm/plat-orion/time.c') diff --git a/arch/arm/plat-orion/time.c b/arch/arm/plat-orion/time.c index de8a001fc3a9..b856cec81702 100644 --- a/arch/arm/plat-orion/time.c +++ b/arch/arm/plat-orion/time.c @@ -17,6 +17,9 @@ #include #include #include +#include +#include +#include /* * Number of timer ticks per jiffy. @@ -38,6 +41,36 @@ static u32 ticks_per_jiffy; #define TIMER1_VAL (TIMER_VIRT_BASE + 0x001c) +/* + * Orion's sched_clock implementation. It has a resolution of + * at least 7.5ns (133MHz TCLK) and a maximum value of 834 days. + */ +#define TCLK2NS_SCALE_FACTOR 8 + +static unsigned long tclk2ns_scale; + +static void __init set_tclk2ns_scale(unsigned long tclk) +{ + unsigned long long v = NSEC_PER_SEC; + v <<= TCLK2NS_SCALE_FACTOR; + v += tclk/2; + do_div(v, tclk); + /* + * We want an even value to automatically clear the top bit + * returned by cnt32_to_63() without an additional run time + * instruction. So if the LSB is 1 then round it up. + */ + if (v & 1) + v++; + tclk2ns_scale = v; +} + +unsigned long long sched_clock(void) +{ + unsigned long long v = cnt32_to_63(0xffffffff - readl(TIMER0_VAL)); + return (v * tclk2ns_scale) >> TCLK2NS_SCALE_FACTOR; +} + /* * Clocksource handling. */ @@ -176,6 +209,10 @@ void __init orion_time_init(unsigned int irq, unsigned int tclk) ticks_per_jiffy = (tclk + HZ/2) / HZ; + /* + * Set scale for sched_clock + */ + set_tclk2ns_scale(tclk); /* * Setup free-running clocksource timer (interrupts -- cgit v1.2.3 From a399e3fa795afa058e4485b25c498e0c5a860428 Mon Sep 17 00:00:00 2001 From: Nicolas Pitre Date: Fri, 15 May 2009 00:42:36 -0400 Subject: [ARM] orion: make sure sched_clock() usage of cnt32_to_63() is safe With a TCLK = 200MHz, the half period of the hardware timer is roughly 10 seconds. Because cnt32_to_63() must be called at least once per half period of the base hardware counter, it is a bit risky to rely solely on scheduling to generate frequent enough calls. Let's use a kernel timer to ensure this. Signed-off-by: Nicolas Pitre --- arch/arm/plat-orion/time.c | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) (limited to 'arch/arm/plat-orion/time.c') diff --git a/arch/arm/plat-orion/time.c b/arch/arm/plat-orion/time.c index b856cec81702..715a30177f28 100644 --- a/arch/arm/plat-orion/time.c +++ b/arch/arm/plat-orion/time.c @@ -12,14 +12,15 @@ */ #include +#include +#include +#include #include #include #include #include #include #include -#include -#include /* * Number of timer ticks per jiffy. @@ -44,14 +45,36 @@ static u32 ticks_per_jiffy; /* * Orion's sched_clock implementation. It has a resolution of * at least 7.5ns (133MHz TCLK) and a maximum value of 834 days. + * + * Because the hardware timer period is quite short (21 secs if + * 200MHz TCLK) and because cnt32_to_63() needs to be called at + * least once per half period to work properly, a kernel timer is + * set up to ensure this requirement is always met. */ #define TCLK2NS_SCALE_FACTOR 8 static unsigned long tclk2ns_scale; -static void __init set_tclk2ns_scale(unsigned long tclk) +unsigned long long sched_clock(void) +{ + unsigned long long v = cnt32_to_63(0xffffffff - readl(TIMER0_VAL)); + return (v * tclk2ns_scale) >> TCLK2NS_SCALE_FACTOR; +} + +static struct timer_list cnt32_to_63_keepwarm_timer; + +static void cnt32_to_63_keepwarm(unsigned long data) +{ + mod_timer(&cnt32_to_63_keepwarm_timer, round_jiffies(jiffies + data)); + (void) sched_clock(); +} + +static void __init setup_sched_clock(unsigned long tclk) { - unsigned long long v = NSEC_PER_SEC; + unsigned long long v; + unsigned long data; + + v = NSEC_PER_SEC; v <<= TCLK2NS_SCALE_FACTOR; v += tclk/2; do_div(v, tclk); @@ -63,12 +86,10 @@ static void __init set_tclk2ns_scale(unsigned long tclk) if (v & 1) v++; tclk2ns_scale = v; -} -unsigned long long sched_clock(void) -{ - unsigned long long v = cnt32_to_63(0xffffffff - readl(TIMER0_VAL)); - return (v * tclk2ns_scale) >> TCLK2NS_SCALE_FACTOR; + data = (0xffffffffUL / tclk / 2 - 2) * HZ; + setup_timer(&cnt32_to_63_keepwarm_timer, cnt32_to_63_keepwarm, data); + mod_timer(&cnt32_to_63_keepwarm_timer, round_jiffies(jiffies + data)); } /* @@ -210,9 +231,9 @@ void __init orion_time_init(unsigned int irq, unsigned int tclk) ticks_per_jiffy = (tclk + HZ/2) / HZ; /* - * Set scale for sched_clock + * Set scale and timer for sched_clock */ - set_tclk2ns_scale(tclk); + setup_sched_clock(tclk); /* * Setup free-running clocksource timer (interrupts @@ -227,7 +248,6 @@ void __init orion_time_init(unsigned int irq, unsigned int tclk) orion_clksrc.mult = clocksource_hz2mult(tclk, orion_clksrc.shift); clocksource_register(&orion_clksrc); - /* * Setup clockevent timer (interrupt-driven.) */ -- cgit v1.2.3