diff mbox

[U-Boot] zynq: timer: Fix wrong timer calculation

Message ID e5dfd89780ac9d5f55d4415080a2768e0f523cd5.1429177165.git.michal.simek@xilinx.com
State Accepted
Delegated to: Michal Simek
Headers show

Commit Message

Michal Simek April 16, 2015, 9:39 a.m. UTC
From: Siva Durga Prasad Paladugu <siva.durga.paladugu@xilinx.com>

Fix wrong timer calculation in get_timer_masked incase of
overflow.
This fixes the issue of getting wrong time from get_timer()
calls.

Signed-off-by: Siva Durga Prasad Paladugu <sivadur@xilinx.com>
Signed-off-by: Michal Simek <michal.simek@xilinx.com>
---

 arch/arm/cpu/armv7/zynq/timer.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

thomas.langer@lantiq.com April 16, 2015, 12:57 p.m. UTC | #1
Hello Michal,

Michal Simek wrote onĀ 2015-04-16:
> From: Siva Durga Prasad Paladugu <siva.durga.paladugu@xilinx.com>
> 
> Fix wrong timer calculation in get_timer_masked incase of overflow. This
> fixes the issue of getting wrong time from get_timer() calls.
> 
> Signed-off-by: Siva Durga Prasad Paladugu <sivadur@xilinx.com>
> Signed-off-by: Michal Simek <michal.simek@xilinx.com>
> ---
> 
>  arch/arm/cpu/armv7/zynq/timer.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> diff --git a/arch/arm/cpu/armv7/zynq/timer.c
> b/arch/arm/cpu/armv7/zynq/timer.c index 303dbcfceafb..5ed9642df9b3
> 100644 --- a/arch/arm/cpu/armv7/zynq/timer.c +++
> b/arch/arm/cpu/armv7/zynq/timer.c @@ -93,7 +93,9 @@ ulong
> get_timer_masked(void)
>  		gd->arch.tbl += gd->arch.lastinc - now;
>  	} else {
>  		/* We have an overflow ... */
> -		gd->arch.tbl += gd->arch.lastinc + TIMER_LOAD_VAL - now + 1;
> +		gd->arch.tbl += gd->arch.lastinc + (TIMER_LOAD_VAL /
> +				(gd->arch.timer_rate_hz / CONFIG_SYS_HZ))
> -
> +				now + 1;
>  	}
>  	gd->arch.lastinc = now;

I had similar problems with the MIPS timer code some time ago.
I solved it by switching to the common timer implementation:
http://git.denx.de/cgi-bin/gitweb.cgi?p=u-boot.git;a=commitdiff;h=a18a477147ce2493ef9ee93b8981b34929fc48a5

I  don't know the arm/zynq hardware in detail, but if you use some free running counter for this,
the switch should be simple.

As you can see from the diff, the previous code was also doing some complex checks to detect an overflow.
With the common code, it could be reduced to two simple  functions (where one could be removed, 
if we update all configs to define a different macro for the timer rate).

Best Regards,
Thomas
Michal Simek April 22, 2015, 1:02 p.m. UTC | #2
Hi,

On 04/16/2015 02:57 PM, thomas.langer@lantiq.com wrote:
> Hello Michal,
> 
> Michal Simek wrote on 2015-04-16:
>> From: Siva Durga Prasad Paladugu <siva.durga.paladugu@xilinx.com>
>>
>> Fix wrong timer calculation in get_timer_masked incase of overflow. This
>> fixes the issue of getting wrong time from get_timer() calls.
>>
>> Signed-off-by: Siva Durga Prasad Paladugu <sivadur@xilinx.com>
>> Signed-off-by: Michal Simek <michal.simek@xilinx.com>
>> ---
>>
>>  arch/arm/cpu/armv7/zynq/timer.c | 4 +++-
>>  1 file changed, 3 insertions(+), 1 deletion(-)
>> diff --git a/arch/arm/cpu/armv7/zynq/timer.c
>> b/arch/arm/cpu/armv7/zynq/timer.c index 303dbcfceafb..5ed9642df9b3
>> 100644 --- a/arch/arm/cpu/armv7/zynq/timer.c +++
>> b/arch/arm/cpu/armv7/zynq/timer.c @@ -93,7 +93,9 @@ ulong
>> get_timer_masked(void)
>>  		gd->arch.tbl += gd->arch.lastinc - now;
>>  	} else {
>>  		/* We have an overflow ... */
>> -		gd->arch.tbl += gd->arch.lastinc + TIMER_LOAD_VAL - now + 1;
>> +		gd->arch.tbl += gd->arch.lastinc + (TIMER_LOAD_VAL /
>> +				(gd->arch.timer_rate_hz / CONFIG_SYS_HZ))
>> -
>> +				now + 1;
>>  	}
>>  	gd->arch.lastinc = now;
> 
> I had similar problems with the MIPS timer code some time ago.
> I solved it by switching to the common timer implementation:
> http://git.denx.de/cgi-bin/gitweb.cgi?p=u-boot.git;a=commitdiff;h=a18a477147ce2493ef9ee93b8981b34929fc48a5
> 
> I  don't know the arm/zynq hardware in detail, but if you use some free running counter for this,
> the switch should be simple.
> 
> As you can see from the diff, the previous code was also doing some complex checks to detect an overflow.
> With the common code, it could be reduced to two simple  functions (where one could be removed, 
> if we update all configs to define a different macro for the timer rate).

Thanks - I have changed the code and will do more testing and will
update. I think make sense to apply this because it is fixes real
problem and then add that new one on the top of it.

Definitely thanks for hint we should do it in past.

Thanks,
Michal
diff mbox

Patch

diff --git a/arch/arm/cpu/armv7/zynq/timer.c b/arch/arm/cpu/armv7/zynq/timer.c
index 303dbcfceafb..5ed9642df9b3 100644
--- a/arch/arm/cpu/armv7/zynq/timer.c
+++ b/arch/arm/cpu/armv7/zynq/timer.c
@@ -93,7 +93,9 @@  ulong get_timer_masked(void)
 		gd->arch.tbl += gd->arch.lastinc - now;
 	} else {
 		/* We have an overflow ... */
-		gd->arch.tbl += gd->arch.lastinc + TIMER_LOAD_VAL - now + 1;
+		gd->arch.tbl += gd->arch.lastinc + (TIMER_LOAD_VAL /
+				(gd->arch.timer_rate_hz / CONFIG_SYS_HZ)) -
+				now + 1;
 	}
 	gd->arch.lastinc = now;