From mboxrd@z Thu Jan 1 00:00:00 1970 From: Reinhard Meyer Date: Tue, 26 Oct 2010 08:01:46 +0200 Subject: [U-Boot] [PATCH v2] mmc: omap: timeout counter fix In-Reply-To: <20101026054812.60816135DE3@gemini.denx.de> References: <1288054924-24989-1-git-send-email-nm@ti.com> <4CC62B6C.30601@emk-elektronik.de> <20101026052856.3D467152451@gemini.denx.de> <4CC66A67.4000608@emk-elektronik.de> <20101026054812.60816135DE3@gemini.denx.de> Message-ID: <4CC66ECA.9000106@emk-elektronik.de> List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: u-boot@lists.denx.de Dear Wolfgang Denk, > Dear Reinhard Meyer, > > In message<4CC66A67.4000608@emk-elektronik.de> you wrote: >> >>> 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. >> >> I used and assumed a 64 bit counter, that will not wrap around while >> our civilization still exists... > > > The code is still wrong, and as a simple correct implementation exists > there is no excuse for using such incorrect code. > > Please fix that! Agreed here. People are invited to dig through u-boot and find all those places. > >> If get_ticks() is only 32 bits worth, both methods will misbehave >> at a 32 bit wrap over. > > No. > >>> start = time(); >>> while ((time() - start)< delay) >>> ... >>> >>> This works much better (assuming unsigned arithmetics). >> >> True, provided the underlying timer is really 64 bits, otherwise >> this fails, too... > You are wrong. Try for example this: > > --------------------- snip ------------------- > #include > > int main(void) > { > unsigned int time = 0xFFFFFFF0; > unsigned int delay = 0x20; > unsigned int start; You are wrong here, because you take it out of context. My "demo" is using the (declared as) 64 bit function get_ticks(). I mentioned above that this function MUST be truly returning 64 bits worth of (incrementing) value to make any version work. If get_ticks() just returns a 32 bit counter value neither method will work reliably. Just check all implementations that this function is implemented correctly.