2 * Copyright (c) 2006 Benno Rice. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
28 #include <sys/param.h>
29 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/malloc.h>
35 #include <machine/bus.h>
36 #include <machine/intr.h>
38 #include <arm/xscale/pxa/pxavar.h>
39 #include <arm/xscale/pxa/pxareg.h>
41 static void pxa_identify(driver_t *, device_t);
42 static int pxa_probe(device_t);
43 static int pxa_attach(device_t);
45 static int pxa_print_child(device_t, device_t);
47 static int pxa_setup_intr(device_t, device_t, struct resource *, int,
48 driver_filter_t *, driver_intr_t *, void *, void **);
49 static int pxa_read_ivar(device_t, device_t, int, uintptr_t *);
51 static struct resource_list * pxa_get_resource_list(device_t, device_t);
52 static struct resource * pxa_alloc_resource(device_t, device_t, int,
53 int *, u_long, u_long, u_long, u_int);
54 static int pxa_release_resource(device_t, device_t, int,
55 int, struct resource *);
56 static int pxa_activate_resource(device_t, device_t,
57 int, int, struct resource *);
59 static struct resource * pxa_alloc_gpio_irq(device_t, device_t, int,
60 int *, u_long, u_long, u_long, u_int);
67 struct resource_list od_resources;
70 static struct obio_device obio_devices[] = {
71 { "icu", PXA2X0_INTCTL_BASE, PXA2X0_INTCTL_SIZE, { 0 } },
72 { "timer", PXA2X0_OST_BASE, PXA2X0_OST_SIZE, { PXA2X0_INT_OST0, PXA2X0_INT_OST1, PXA2X0_INT_OST2, PXA2X0_INT_OST3, 0 } },
73 { "dmac", PXA2X0_DMAC_BASE, PXA2X0_DMAC_SIZE, { PXA2X0_INT_DMA, 0 } },
74 { "gpio", PXA2X0_GPIO_BASE, PXA250_GPIO_SIZE, { PXA2X0_INT_GPIO0, PXA2X0_INT_GPIO1, PXA2X0_INT_GPION, 0 } },
75 { "uart", PXA2X0_FFUART_BASE, PXA2X0_FFUART_SIZE, { PXA2X0_INT_FFUART, 0 } },
76 { "uart", PXA2X0_BTUART_BASE, PXA2X0_BTUART_SIZE, { PXA2X0_INT_BTUART, 0 } },
77 { "uart", PXA2X0_STUART_BASE, PXA2X0_STUART_SIZE, { PXA2X0_INT_STUART, 0 } },
78 { "uart", PXA2X0_HWUART_BASE, PXA2X0_HWUART_SIZE, { PXA2X0_INT_HWUART, 0 } },
79 { "smi", PXA2X0_CS0_START, PXA2X0_CS_SIZE * 6, { 0 } },
84 pxa_identify(driver_t *driver, device_t parent)
87 BUS_ADD_CHILD(parent, 0, "pxa", 0);
91 pxa_probe(device_t dev)
94 device_set_desc(dev, "XScale PXA On-board IO");
95 return (BUS_PROBE_NOWILDCARD);
99 pxa_attach(device_t dev)
101 struct obio_softc *sc;
102 struct obio_device *od;
106 sc = device_get_softc(dev);
108 sc->obio_bst = obio_tag;
110 sc->obio_mem.rm_type = RMAN_ARRAY;
111 sc->obio_mem.rm_descr = "PXA2X0 OBIO Memory";
112 if (rman_init(&sc->obio_mem) != 0)
113 panic("pxa_attach: failed to init obio mem rman");
114 if (rman_manage_region(&sc->obio_mem, 0, PXA250_PERIPH_END) != 0)
115 panic("pxa_attach: failed to set up obio mem rman");
117 sc->obio_irq.rm_type = RMAN_ARRAY;
118 sc->obio_irq.rm_descr = "PXA2X0 OBIO IRQ";
119 if (rman_init(&sc->obio_irq) != 0)
120 panic("pxa_attach: failed to init obio irq rman");
121 if (rman_manage_region(&sc->obio_irq, 0, 31) != 0)
122 panic("pxa_attach: failed to set up obio irq rman (main irqs)");
123 if (rman_manage_region(&sc->obio_irq, IRQ_GPIO0, IRQ_GPIO_MAX) != 0)
124 panic("pxa_attach: failed to set up obio irq rman (gpio irqs)");
126 for (od = obio_devices; od->od_name != NULL; od++) {
127 resource_list_init(&od->od_resources);
129 resource_list_add(&od->od_resources, SYS_RES_MEMORY, 0,
130 od->od_base, od->od_base + od->od_size, od->od_size);
132 for (i = 0; od->od_irqs[i] != 0; i++) {
133 resource_list_add(&od->od_resources, SYS_RES_IRQ, i,
134 od->od_irqs[i], od->od_irqs[i], 1);
137 child = device_add_child(dev, od->od_name, -1);
138 device_set_ivars(child, od);
141 bus_generic_probe(dev);
142 bus_generic_attach(dev);
148 pxa_print_child(device_t dev, device_t child)
150 struct obio_device *od;
153 od = (struct obio_device *)device_get_ivars(child);
155 panic("Unknown device on pxa0");
159 retval += bus_print_child_header(dev, child);
161 retval += resource_list_print_type(&od->od_resources, "at mem",
162 SYS_RES_MEMORY, "0x%08lx");
163 retval += resource_list_print_type(&od->od_resources, "irq",
166 retval += bus_print_child_footer(dev, child);
172 pxa_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
173 driver_filter_t *filter, driver_intr_t *ithread, void *arg, void **cookiep)
175 struct obio_softc *sc;
178 sc = (struct obio_softc *)device_get_softc(dev);
180 error = BUS_SETUP_INTR(device_get_parent(dev), child, irq, flags,
181 filter, ithread, arg, cookiep);
188 pxa_teardown_intr(device_t dev, device_t child, struct resource *ires,
191 return (BUS_TEARDOWN_INTR(device_get_parent(dev), child, ires, cookie));}
194 pxa_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
196 struct obio_device *od;
198 od = (struct obio_device *)device_get_ivars(child);
202 *((u_long *)result) = od->od_base;
212 static struct resource_list *
213 pxa_get_resource_list(device_t dev, device_t child)
215 struct obio_device *od;
217 od = (struct obio_device *)device_get_ivars(child);
222 return (&od->od_resources);
225 static struct resource *
226 pxa_alloc_resource(device_t dev, device_t child, int type, int *rid,
227 u_long start, u_long end, u_long count, u_int flags)
229 struct obio_softc *sc;
230 struct obio_device *od;
232 struct resource_list *rl;
233 struct resource_list_entry *rle;
237 sc = (struct obio_softc *)device_get_softc(dev);
238 od = (struct obio_device *)device_get_ivars(child);
239 rl = &od->od_resources;
241 rle = resource_list_find(rl, type, *rid);
243 /* We can allocate GPIO-based IRQs lazily. */
244 if (type == SYS_RES_IRQ)
245 return (pxa_alloc_gpio_irq(dev, child, type, rid,
246 start, end, count, flags));
249 if (rle->res != NULL)
250 panic("pxa_alloc_resource: resource is busy");
265 needactivate = flags & RF_ACTIVE;
267 rv = rman_reserve_resource(rm, rle->start, rle->end, rle->count, flags,
272 rman_set_rid(rv, *rid);
273 if (type == SYS_RES_MEMORY) {
274 rman_set_bustag(rv, sc->obio_bst);
275 rman_set_bushandle(rv, rle->start);
279 if (bus_activate_resource(child, type, *rid, rv)) {
280 rman_release_resource(rv);
289 pxa_release_resource(device_t dev, device_t child, int type, int rid,
292 struct obio_device *od;
293 struct resource_list *rl;
294 struct resource_list_entry *rle;
296 od = (struct obio_device *)device_get_ivars(child);
297 rl = &od->od_resources;
299 if (type == SYS_RES_IOPORT)
300 type = SYS_RES_MEMORY;
302 rle = resource_list_find(rl, type, rid);
305 panic("pxa_release_resource: can't find resource");
307 panic("pxa_release_resource: resource entry is not busy");
309 rman_release_resource(rle->res);
316 pxa_activate_resource(device_t dev, device_t child, int type, int rid,
320 return (rman_activate_resource(r));
323 static device_method_t pxa_methods[] = {
324 DEVMETHOD(device_identify, pxa_identify),
325 DEVMETHOD(device_probe, pxa_probe),
326 DEVMETHOD(device_attach, pxa_attach),
328 DEVMETHOD(bus_print_child, pxa_print_child),
330 DEVMETHOD(bus_read_ivar, pxa_read_ivar),
331 DEVMETHOD(bus_setup_intr, pxa_setup_intr),
332 DEVMETHOD(bus_teardown_intr, pxa_teardown_intr),
334 DEVMETHOD(bus_get_resource_list, pxa_get_resource_list),
335 DEVMETHOD(bus_alloc_resource, pxa_alloc_resource),
336 DEVMETHOD(bus_release_resource, pxa_release_resource),
337 DEVMETHOD(bus_activate_resource, pxa_activate_resource),
342 static driver_t pxa_driver = {
345 sizeof(struct obio_softc),
348 static devclass_t pxa_devclass;
350 DRIVER_MODULE(pxa, nexus, pxa_driver, pxa_devclass, 0, 0);
352 static struct resource *
353 pxa_alloc_gpio_irq(device_t dev, device_t child, int type, int *rid,
354 u_long start, u_long end, u_long count, u_int flags)
356 struct obio_softc *sc;
357 struct obio_device *od;
358 struct resource_list *rl;
359 struct resource_list_entry *rle;
364 sc = device_get_softc(dev);
365 od = device_get_ivars(child);
366 rl = &od->od_resources;
369 needactivate = flags & RF_ACTIVE;
371 rv = rman_reserve_resource(rm, start, end, count, flags, child);
375 resource_list_add(rl, type, *rid, start, end, count);
376 rle = resource_list_find(rl, type, *rid);
378 panic("pxa_alloc_gpio_irq: unexpectedly can't find resource");
381 rle->start = rman_get_start(rv);
382 rle->end = rman_get_end(rv);
386 if (bus_activate_resource(child, type, *rid, rv)) {
387 rman_release_resource(rv);
393 device_printf(dev, "lazy allocation of irq %ld for %s\n",
394 start, device_get_nameunit(child));