2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 Benno Rice. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/malloc.h>
37 #include <machine/bus.h>
38 #include <machine/intr.h>
40 #include <arm/xscale/pxa/pxavar.h>
41 #include <arm/xscale/pxa/pxareg.h>
43 static void pxa_identify(driver_t *, device_t);
44 static int pxa_probe(device_t);
45 static int pxa_attach(device_t);
47 static int pxa_print_child(device_t, device_t);
49 static int pxa_setup_intr(device_t, device_t, struct resource *, int,
50 driver_filter_t *, driver_intr_t *, void *, void **);
51 static int pxa_read_ivar(device_t, device_t, int, uintptr_t *);
53 static struct resource_list * pxa_get_resource_list(device_t, device_t);
54 static struct resource * pxa_alloc_resource(device_t, device_t, int,
55 int *, rman_res_t, rman_res_t, rman_res_t, u_int);
56 static int pxa_release_resource(device_t, device_t, int,
57 int, struct resource *);
58 static int pxa_activate_resource(device_t, device_t,
59 int, int, struct resource *);
61 static struct resource * pxa_alloc_gpio_irq(device_t, device_t, int,
62 int *, rman_res_t, rman_res_t, rman_res_t, u_int);
69 struct resource_list od_resources;
72 static struct obio_device obio_devices[] = {
73 { "icu", PXA2X0_INTCTL_BASE, PXA2X0_INTCTL_SIZE, { 0 } },
74 { "timer", PXA2X0_OST_BASE, PXA2X0_OST_SIZE, { PXA2X0_INT_OST0, PXA2X0_INT_OST1, PXA2X0_INT_OST2, PXA2X0_INT_OST3, 0 } },
75 { "dmac", PXA2X0_DMAC_BASE, PXA2X0_DMAC_SIZE, { PXA2X0_INT_DMA, 0 } },
76 { "gpio", PXA2X0_GPIO_BASE, PXA250_GPIO_SIZE, { PXA2X0_INT_GPIO0, PXA2X0_INT_GPIO1, PXA2X0_INT_GPION, 0 } },
77 { "uart", PXA2X0_FFUART_BASE, PXA2X0_FFUART_SIZE, { PXA2X0_INT_FFUART, 0 } },
78 { "uart", PXA2X0_BTUART_BASE, PXA2X0_BTUART_SIZE, { PXA2X0_INT_BTUART, 0 } },
79 { "uart", PXA2X0_STUART_BASE, PXA2X0_STUART_SIZE, { PXA2X0_INT_STUART, 0 } },
80 { "uart", PXA2X0_HWUART_BASE, PXA2X0_HWUART_SIZE, { PXA2X0_INT_HWUART, 0 } },
81 { "smi", PXA2X0_CS0_START, PXA2X0_CS_SIZE * 6, { 0 } },
86 pxa_identify(driver_t *driver, device_t parent)
89 BUS_ADD_CHILD(parent, 0, "pxa", 0);
93 pxa_probe(device_t dev)
96 device_set_desc(dev, "XScale PXA On-board IO");
97 return (BUS_PROBE_NOWILDCARD);
101 pxa_attach(device_t dev)
103 struct obio_softc *sc;
104 struct obio_device *od;
108 sc = device_get_softc(dev);
110 sc->obio_bst = obio_tag;
112 sc->obio_mem.rm_type = RMAN_ARRAY;
113 sc->obio_mem.rm_descr = "PXA2X0 OBIO Memory";
114 if (rman_init(&sc->obio_mem) != 0)
115 panic("pxa_attach: failed to init obio mem rman");
116 if (rman_manage_region(&sc->obio_mem, 0, PXA250_PERIPH_END) != 0)
117 panic("pxa_attach: failed to set up obio mem rman");
119 sc->obio_irq.rm_type = RMAN_ARRAY;
120 sc->obio_irq.rm_descr = "PXA2X0 OBIO IRQ";
121 if (rman_init(&sc->obio_irq) != 0)
122 panic("pxa_attach: failed to init obio irq rman");
123 if (rman_manage_region(&sc->obio_irq, 0, 31) != 0)
124 panic("pxa_attach: failed to set up obio irq rman (main irqs)");
125 if (rman_manage_region(&sc->obio_irq, IRQ_GPIO0, IRQ_GPIO_MAX) != 0)
126 panic("pxa_attach: failed to set up obio irq rman (gpio irqs)");
128 for (od = obio_devices; od->od_name != NULL; od++) {
129 resource_list_init(&od->od_resources);
131 resource_list_add(&od->od_resources, SYS_RES_MEMORY, 0,
132 od->od_base, od->od_base + od->od_size, od->od_size);
134 for (i = 0; od->od_irqs[i] != 0; i++) {
135 resource_list_add(&od->od_resources, SYS_RES_IRQ, i,
136 od->od_irqs[i], od->od_irqs[i], 1);
139 child = device_add_child(dev, od->od_name, -1);
140 device_set_ivars(child, od);
143 bus_generic_probe(dev);
144 bus_generic_attach(dev);
150 pxa_print_child(device_t dev, device_t child)
152 struct obio_device *od;
155 od = (struct obio_device *)device_get_ivars(child);
157 panic("Unknown device on pxa0");
161 retval += bus_print_child_header(dev, child);
163 retval += resource_list_print_type(&od->od_resources, "at mem",
164 SYS_RES_MEMORY, "0x%08jx");
165 retval += resource_list_print_type(&od->od_resources, "irq",
168 retval += bus_print_child_footer(dev, child);
174 pxa_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
175 driver_filter_t *filter, driver_intr_t *ithread, void *arg, void **cookiep)
177 struct obio_softc *sc;
180 sc = (struct obio_softc *)device_get_softc(dev);
182 error = BUS_SETUP_INTR(device_get_parent(dev), child, irq, flags,
183 filter, ithread, arg, cookiep);
190 pxa_teardown_intr(device_t dev, device_t child, struct resource *ires,
193 return (BUS_TEARDOWN_INTR(device_get_parent(dev), child, ires, cookie));}
196 pxa_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
198 struct obio_device *od;
200 od = (struct obio_device *)device_get_ivars(child);
204 *((u_long *)result) = od->od_base;
214 static struct resource_list *
215 pxa_get_resource_list(device_t dev, device_t child)
217 struct obio_device *od;
219 od = (struct obio_device *)device_get_ivars(child);
224 return (&od->od_resources);
227 static struct resource *
228 pxa_alloc_resource(device_t dev, device_t child, int type, int *rid,
229 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
231 struct obio_softc *sc;
232 struct obio_device *od;
234 struct resource_list *rl;
235 struct resource_list_entry *rle;
239 sc = (struct obio_softc *)device_get_softc(dev);
240 od = (struct obio_device *)device_get_ivars(child);
241 rl = &od->od_resources;
243 rle = resource_list_find(rl, type, *rid);
245 /* We can allocate GPIO-based IRQs lazily. */
246 if (type == SYS_RES_IRQ)
247 return (pxa_alloc_gpio_irq(dev, child, type, rid,
248 start, end, count, flags));
251 if (rle->res != NULL)
252 panic("pxa_alloc_resource: resource is busy");
267 needactivate = flags & RF_ACTIVE;
269 rv = rman_reserve_resource(rm, rle->start, rle->end, rle->count, flags,
274 rman_set_rid(rv, *rid);
275 if (type == SYS_RES_MEMORY) {
276 rman_set_bustag(rv, sc->obio_bst);
277 rman_set_bushandle(rv, rle->start);
281 if (bus_activate_resource(child, type, *rid, rv)) {
282 rman_release_resource(rv);
291 pxa_release_resource(device_t dev, device_t child, int type, int rid,
294 struct obio_device *od;
295 struct resource_list *rl;
296 struct resource_list_entry *rle;
298 od = (struct obio_device *)device_get_ivars(child);
299 rl = &od->od_resources;
301 if (type == SYS_RES_IOPORT)
302 type = SYS_RES_MEMORY;
304 rle = resource_list_find(rl, type, rid);
307 panic("pxa_release_resource: can't find resource");
309 panic("pxa_release_resource: resource entry is not busy");
311 rman_release_resource(rle->res);
318 pxa_activate_resource(device_t dev, device_t child, int type, int rid,
322 return (rman_activate_resource(r));
325 static device_method_t pxa_methods[] = {
326 DEVMETHOD(device_identify, pxa_identify),
327 DEVMETHOD(device_probe, pxa_probe),
328 DEVMETHOD(device_attach, pxa_attach),
330 DEVMETHOD(bus_print_child, pxa_print_child),
332 DEVMETHOD(bus_read_ivar, pxa_read_ivar),
333 DEVMETHOD(bus_setup_intr, pxa_setup_intr),
334 DEVMETHOD(bus_teardown_intr, pxa_teardown_intr),
336 DEVMETHOD(bus_get_resource_list, pxa_get_resource_list),
337 DEVMETHOD(bus_alloc_resource, pxa_alloc_resource),
338 DEVMETHOD(bus_release_resource, pxa_release_resource),
339 DEVMETHOD(bus_activate_resource, pxa_activate_resource),
344 static driver_t pxa_driver = {
347 sizeof(struct obio_softc),
350 static devclass_t pxa_devclass;
352 DRIVER_MODULE(pxa, nexus, pxa_driver, pxa_devclass, 0, 0);
354 static struct resource *
355 pxa_alloc_gpio_irq(device_t dev, device_t child, int type, int *rid,
356 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
358 struct obio_softc *sc;
359 struct obio_device *od;
360 struct resource_list *rl;
361 struct resource_list_entry *rle;
366 sc = device_get_softc(dev);
367 od = device_get_ivars(child);
368 rl = &od->od_resources;
371 needactivate = flags & RF_ACTIVE;
373 rv = rman_reserve_resource(rm, start, end, count, flags, child);
377 resource_list_add(rl, type, *rid, start, end, count);
378 rle = resource_list_find(rl, type, *rid);
380 panic("pxa_alloc_gpio_irq: unexpectedly can't find resource");
383 rle->start = rman_get_start(rv);
384 rle->end = rman_get_end(rv);
388 if (bus_activate_resource(child, type, *rid, rv)) {
389 rman_release_resource(rv);
395 device_printf(dev, "lazy allocation of irq %jd for %s\n",
396 start, device_get_nameunit(child));