Date: Sun, 17 Dec 2017 13:19:49 -0700 From: Ian Lepore <ian@freebsd.org> To: Lee D <embaudarm@gmail.com>, freebsd-hackers@freebsd.org Subject: Re: How do I alloc multiple memory regions specified in a device tree? Message-ID: <1513541989.95072.36.camel@freebsd.org> In-Reply-To: <CANC_bnMjPzSKM0q1__or=cTrvoxnM_LXo-avFYgbUZVeZPT9wg@mail.gmail.com> References: <CANC_bnMjPzSKM0q1__or=cTrvoxnM_LXo-avFYgbUZVeZPT9wg@mail.gmail.com>
next in thread | previous in thread | raw e-mail | index | archive | help
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@40000000 {
> device_type = "soc";
> compatible = "simple-bus";
> #address-cells = <0x1>;
> #size-cells = <0x1>;
> ranges = <0x0 0x40000000 0x5000000>;
>
> my_lcd@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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1513541989.95072.36.camel>
