How do I alloc multiple memory regions specified in a device tree?

Ian Lepore ian at freebsd.org
Sun Dec 17 20:19:52 UTC 2017


On Sun, 2017-12-17 at 15:00 -0500, Lee D wrote:
> Hi everyone.
> 
> I have a device driver that must access registers in multiple memory
> ranges.  How do I pull those values out of the device tree?
> 
> This is for a custom ARM embedded board.
> 
> bus_alloc_resource_any() works, but only for the first memory range.
> The second time I call it, it crashes the kernel.
> 
> If I alloc the same memory resources by hardcoding the values in calls
> to bus_alloc_resource(), it works.
> 
> The technique of calling bus_alloc_resource_any() multiple times is
> used in src/sys/dev/sdhci/sdhci_fdt.c, but I can't get it to work.
> 
> Here is a snippet from my device tree:
> 
> ...
>   fabric at 40000000 {
>     device_type = "soc";
>     compatible = "simple-bus";
>     #address-cells = <0x1>;
>     #size-cells = <0x1>;
>     ranges = <0x0 0x40000000 0x5000000>;
> 
>     my_lcd at 3C00000 {
>       status = "okay";
>       compatible = "xlnx,my_lcd";
>       reg = <0x3C00000 0x1000
>              0x400000 0x1000
>              0x3000000 0x1000>;
>       interrupts = <0x0 0x1d 0x1>;
>       interrupt-parent = <0x1>;
>     };
> ...
> 
> Here is my driver code from my_lcd_attach:
> 
>   rid=0;
>   sc->lcd_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
> RF_ACTIVE);
> 
>   if (sc->lcd_mem_res == NULL) {
>     my_lcd_detach(dev);
>     return (ENOMEM);
>   }
> 
>   rid=0;
>   sc->dma_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
> RF_ACTIVE);
> 
>   if (sc->dma_mem_res == NULL) {
>     my_lcd_detach(dev);
>     return (ENOMEM);
>   }
> 
> Thank you.

The 'rid' argument is the zero-based index of the register range you
want to allocate.  Just increment rid from 0 through the number of
offset/length tuples in the fdt regs property.

You can allocate all the ranges at once with bus_alloc_resources().
For an example, see the a10fb_spec array in arm/allwinner/a10_fb.c

-- Ian



More information about the freebsd-hackers mailing list