From owner-freebsd-hackers@freebsd.org Sun Dec 17 20:19:52 2017 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 6C7C8E948B6 for ; Sun, 17 Dec 2017 20:19:52 +0000 (UTC) (envelope-from ian@freebsd.org) Received: from outbound1b.ore.mailhop.org (outbound1b.ore.mailhop.org [54.200.247.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 50B407CC2D for ; Sun, 17 Dec 2017 20:19:51 +0000 (UTC) (envelope-from ian@freebsd.org) X-MHO-User: 9e5dddbb-e367-11e7-8486-0934409070aa X-Report-Abuse-To: https://support.duocircle.com/support/solutions/articles/5000540958-duocircle-standard-smtp-abuse-information X-Originating-IP: 73.78.92.27 X-Mail-Handler: DuoCircle Outbound SMTP Received: from ilsoft.org (unknown [73.78.92.27]) by outbound1.ore.mailhop.org (Halon) with ESMTPSA id 9e5dddbb-e367-11e7-8486-0934409070aa; Sun, 17 Dec 2017 20:19:44 +0000 (UTC) Received: from rev (rev [172.22.42.240]) by ilsoft.org (8.15.2/8.15.2) with ESMTP id vBHKJnOl003743; Sun, 17 Dec 2017 13:19:49 -0700 (MST) (envelope-from ian@freebsd.org) Message-ID: <1513541989.95072.36.camel@freebsd.org> Subject: Re: How do I alloc multiple memory regions specified in a device tree? From: Ian Lepore To: Lee D , freebsd-hackers@freebsd.org Date: Sun, 17 Dec 2017 13:19:49 -0700 In-Reply-To: References: Content-Type: text/plain; charset="ISO-8859-1" X-Mailer: Evolution 3.18.5.1 FreeBSD GNOME Team Port Mime-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 17 Dec 2017 20:19:52 -0000 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