[U-Boot] Access IO register with writel/readl?

Simon Glass sjg at chromium.org
Thu Oct 18 14:09:19 CEST 2012


Hi,

On Wed, Oct 17, 2012 at 6:47 PM, Dennis Lan (dlan)
<dennis.yxun at gmail.com> wrote:
> HI ALL:
>    Is it just conventional to access IO register with wriltel, readl?
> or is there any specific reason here.
> what's the difference with direct access? writel has few arch releated
> operation to guarantee hardware access?

Yes it is a convention. For CPUs which need to introduce
compiler/memory barriers or other operations, this is supposed to be
done automatically in the access functions rather than in the code.

Volatile tells the compiler that the value can change, and not to
optimise accesses to it, but this is just the compiler and does not
necessarily do everything required for I/O access on the platform.
Linux has a README on this topic (search for
Documentation/volatile-considered-harmful.txt) but I'm not sure that
the locking / multi-threading discussion has much relevance to U-Boot.

(Oddly, I did notice at one point that on ARM some of the access
functions don't actually have any barriers. It might have been
clrsetbits.)

People tend to use writel() rather than __raw_writel(). In U-Boot with
a flat address mapping I'm not sure there is much difference in
practice, but it is nice to keep code consistent.

I have seen various comments about this topic on the list, but I'm not
sure if there is a definitive document anywhere - you could write one
with U-Boot in mind if you like.

>
>
>   for example:
>
> struct base_gpt {
>         unsigned int control;
>         unsigned int prescaler;
> };
>
> #define GPT1_BASE_ADDR GPTCR_SWR
> volatile struct base_gpt *cur_gpt = (struct base_gpt *)GPT1_BASE_ADDR;
>
> A) acess with writel
>         __raw_writel(GPTCR_SWR, &cur_gpt->control);
>
> B) direct access
>        cur_gpt->control = GPTCR_SWR;
>
> (Note I define the structure with volatile )
>
> Dennis

Regards,
Simon

> _______________________________________________
> U-Boot mailing list
> U-Boot at lists.denx.de
> http://lists.denx.de/mailman/listinfo/u-boot


More information about the U-Boot mailing list