[U-Boot] [RFC] Review of U-Boot timer API

Graeme Russ
2011-05-25T11:52:21Z

Hi Wolfgang,

On 25/05/11 21:37, Wolfgang Denk wrote:
> Dear Graeme Russ,
>
> In message <4DDCBFF4.40002@gmail.com> you wrote:
>>
>>> I don't get you. In such a system, the interrupt would be the tick
>>> (see the PPC implementation). If you miss interrupts, you miss ticks.
>>
>> Yes, you miss ticks, but if the hardware is keeping the tick counter
>> current in the background (i.e. without software or interrupts) then this
>> does not matter - The prescaler takes care of this...
>
> I think you don't want to understand.
>
> Look at the PPC implementation. The decrementer causes an interrupt
> every millisecond. This is our tick. We never ever actually read the
> decrementer register itself. We just use the interrupt.

x86 is the same - a 1ms interrupt which increments the timer

>> The key is the prescaler takes a 'tick delta' between the last time it was
>> called and now, uses the 'tick frequency' to calculate a corresponding
>> 'timer delta' which it adds to the current timer
>
> The "tick frequency" in above example is 1,000 Hz, and determined by
> the frequency of the interrupts. There is not any "timer count
> register" we are accessing here.
>
>> This relies on the tick counter wrapping properly.
>
> The "tick counter" is already in the higher level, i. e. implemented
> in software, without any hardware based registers.

And in the PPC case - No prescaler and life is easy

BUT - I just found something very disturbing in /board/zylonite/flash.c -
Interrupts are disables before the timer is used to detect a timeout!

Yes, this is in a board specific flash driver which is bad but who knows
where else this might (quite unintentionally) occur

No if you have:

start = get_timer(0)

while (hardware_busy) {
if (get_timer(start) > TIMEOUT) {
/* deal with timeout */
break;
}
/* Do stuf (poll hardware etc) */
}

Now every time get_timer() is called, the prescaler updates the timer from
the current tick count - This cannot fail if interrupts are inadvertently
disabled.

And if you do not care about the timer updating while you are not 'using
it' (i.e. checking for a timeout) - it doesn't really matter - You will get
a glitch, but this glitch is 'reset' by the call to get_timer() and your
timeout check starts a new timer increment afresh

Regards,

Graeme