public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: u-boot@lists.denx.de
Subject: [U-Boot-Users] [Patch 02/17] U-Boot-V2:Common:Clock Handle case of clock	rollover for get_time_ns
Date: Tue, 3 Jun 2008 10:07:32 +0200	[thread overview]
Message-ID: <20080603080732.GA12859@pengutronix.de> (raw)
In-Reply-To: <8E8BB316C604E94AA019A54D0A5A82A201AF027A@dlee13.ent.ti.com>

On Wed, May 21, 2008 at 11:25:15AM -0500, Menon, Nishanth wrote:
> get_time_ns does a simplistic delta = cycle_now - cycle_last. It is possible that the h/w counter reached max and reset back to 0.
> This patch addresses this issue by checking for rollover condition.
> 
> NOTE 1: This does not guarantee that you cannot confuse get_time_ns. You could possibly wait for two reset cycles and then get a messed up value.
> To fix that we may need interrupt mode timer tick - something on the lines of jiffies on linux.
> NOTE 2: the question of cs->mask is not clear. if the mask is for the tick, then it is better done with (cycle_now & cs->mask) - (cs->cycle_last & cs->mask). Do we need min and max for register read?
> 
> Signed-off-by: Nishanth Menon<x0nishan@ti.com>
> 
> ---
>  common/clock.c |   14 ++++++++++++--
>  1 file changed, 12 insertions(+), 2 deletions(-)
> 
> Index: u-boot-v2.git/common/clock.c
> ===================================================================
> --- u-boot-v2.git.orig/common/clock.c	2008-05-20 17:19:44.000000000 -0500
> +++ u-boot-v2.git/common/clock.c	2008-05-20 17:26:31.000000000 -0500
> @@ -37,14 +37,24 @@
>  uint64_t get_time_ns(void)
>  {
>  	struct clocksource *cs = current_clock;
> -        uint64_t cycle_now, cycle_delta;
> +	uint64_t cycle_now, cycle_delta = 0;
>          uint64_t ns_offset;
>  
>          /* read clocksource: */
>          cycle_now = cs->read();
>  
>          /* calculate the delta since the last call: */
> -        cycle_delta = (cycle_now - cs->cycle_last) & cs->mask;

Look closer, the rollover case is handled implicitly by the unsigned
arithmetics.

You are right, we do not have a possibility to detect a double rollover
without interrupts. Normally this is not an issue as this code is used
in timeout polling loops where the current time is polled often enough.
Anyway, maybe some comment should mention that this function is not
useful to measure long periods of time where 'long' is highly
architecture specific.

regards,
  Sascha

> +
> +	/* Handle rollover case */
> +	if (cycle_now < cs->cycle_last) {
> +		/* FIXME: I am not convinced about the cs->mask operation..
> +		 * I am assuming cs->mask is max! Probably need to change
> +		 * clocksource structure to have min and max
> +		 */
> +		cycle_delta = cs->mask - (cs->cycle_last & cs->mask);
> +		cs->cycle_last = 0;
> +	}
> +	cycle_delta += (cycle_now & cs->mask) - (cs->cycle_last & cs->mask);
>  
>          /* convert to nanoseconds: */
>          ns_offset = cyc2ns(cs, cycle_delta);
> 

-- 
Pengutronix e.K. - Linux Solutions for Science and Industry
-----------------------------------------------------------
Kontakt-Informationen finden Sie im Header dieser Mail oder
auf der Webseite -> http://www.pengutronix.de/impressum/ <-

  reply	other threads:[~2008-06-03  8:07 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-05-21 16:25 [U-Boot-Users] [Patch 02/17] U-Boot-V2:Common:Clock Handle case of clock rollover for get_time_ns Menon, Nishanth
2008-06-03  8:07 ` Sascha Hauer [this message]
2008-06-03 12:47   ` [U-Boot-Users] [Patch 02/17] U-Boot-V2:Common:Clock Handle case of clockrollover " Menon, Nishanth
2008-06-03 14:14     ` Sascha Hauer
2008-06-03 14:39       ` [U-Boot-Users] [Patch 02/17] U-Boot-V2:Common:Clock Handle case ofclockrollover " Menon, Nishanth
2008-06-03 15:14         ` Sascha Hauer
2008-06-03 16:27           ` [U-Boot-Users] [Patch 02/17 Try 2] U-Boot-V2:Common:Clock Handle caseofclockrollover " Menon, Nishanth

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=20080603080732.GA12859@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=u-boot@lists.denx.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