[U-Boot] [PATCH v2] mmc: omap: timeout counter fix

Wolfgang Denk wd at denx.de
Tue Oct 26 07:28:56 CEST 2010


Dear Reinhard Meyer,

In message <4CC62B6C.30601 at emk-elektronik.de> you wrote:
>
> In such cases I prefer to use:
> 
> 	uint64_t etime;
> ...
> 	etime = get_ticks() + get_tbclk(); /* 1 second */
> 	do {
> 		whatever;
> 		udelay (xx);
> 	} while (condition && get_ticks() <= etime);
> 
> That is far more accurate than calling udelay() 100000 times.

It may be more accuratre, but it may also be HORRIBLY WRONG!!

Do NOT do that!! NEVER implement such a delay loop as

	end = time() + delay;
	while (time() < end)
		...

It fails in case the timer wraps around.

Assume 32 bit counters, start time = 0xFFFFFFF0, delay = 0x20. It
will compute end = 0x10, the while codition is immediately false, and
you don't have any delay at all, which most probably generates a
false error condition.


Correct implementation of a timeout like that should always look like
that:

	start = time();
	while ((time() - start) < delay)
		...

This works much better (assuming unsigned arithmetics).

Best regards,

Wolfgang Denk

-- 
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Ein weiser Herrscher kann in einem großen Land  mehr  Gutes  bewirken
als  in  einem kleinen - ein dummer Herrscher aber auch viel mehr Un-
fug. Da weise Herrscher seltener sind als dumme, war ich schon  immer
gegen große Reiche skeptisch.                   - Herbert Rosendorfer


More information about the U-Boot mailing list