1187517Sgonzo/*-
2187517Sgonzo * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
3187517Sgonzo * All rights reserved.
4187517Sgonzo *
5187517Sgonzo * Redistribution and use in source and binary forms, with or without
6187517Sgonzo * modification, are permitted provided that the following conditions
7187517Sgonzo * are met:
8187517Sgonzo * 1. Redistributions of source code must retain the above copyright
9187517Sgonzo *    notice unmodified, this list of conditions, and the following
10187517Sgonzo *    disclaimer.
11187517Sgonzo * 2. Redistributions in binary form must reproduce the above copyright
12187517Sgonzo *    notice, this list of conditions and the following disclaimer in the
13187517Sgonzo *    documentation and/or other materials provided with the distribution.
14187517Sgonzo *
15187517Sgonzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16187517Sgonzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17187517Sgonzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18187517Sgonzo * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19187517Sgonzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20187517Sgonzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21187517Sgonzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22187517Sgonzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23187517Sgonzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24187517Sgonzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25187517Sgonzo * SUCH DAMAGE.
26187517Sgonzo */
27187517Sgonzo
28187517Sgonzo#include <sys/cdefs.h>
29187517Sgonzo__FBSDID("$FreeBSD$");
30187517Sgonzo
31187517Sgonzo#include <sys/param.h>
32187517Sgonzo#include <sys/systm.h>
33187517Sgonzo#include <sys/bus.h>
34187517Sgonzo#include <sys/interrupt.h>
35187517Sgonzo#include <sys/kernel.h>
36187517Sgonzo#include <sys/module.h>
37187517Sgonzo#include <sys/rman.h>
38187517Sgonzo#include <sys/malloc.h>
39232847Sgonzo#include <sys/pcpu.h>
40232847Sgonzo#include <sys/proc.h>
41232847Sgonzo#include <sys/pmc.h>
42232847Sgonzo#include <sys/pmckern.h>
43187517Sgonzo
44187517Sgonzo#include <machine/bus.h>
45199497Sgonzo#include <machine/intr_machdep.h>
46187517Sgonzo
47187517Sgonzo#include <mips/atheros/apbvar.h>
48187517Sgonzo#include <mips/atheros/ar71xxreg.h>
49221257Sadrian#include <mips/atheros/ar71xx_setup.h>
50187517Sgonzo
51232847Sgonzo#define	APB_INTR_PMC	5
52232847Sgonzo
53188883Sgonzo#undef APB_DEBUG
54188883Sgonzo#ifdef APB_DEBUG
55188883Sgonzo#define dprintf printf
56188883Sgonzo#else
57188883Sgonzo#define dprintf(x, arg...)
58188883Sgonzo#endif  /* APB_DEBUG */
59188883Sgonzo
60187517Sgonzostatic int	apb_activate_resource(device_t, device_t, int, int,
61187517Sgonzo		    struct resource *);
62212413Savgstatic device_t	apb_add_child(device_t, u_int, const char *, int);
63187517Sgonzostatic struct resource *
64187517Sgonzo		apb_alloc_resource(device_t, device_t, int, int *, u_long,
65187517Sgonzo		    u_long, u_long, u_int);
66187517Sgonzostatic int	apb_attach(device_t);
67187517Sgonzostatic int	apb_deactivate_resource(device_t, device_t, int, int,
68187517Sgonzo		    struct resource *);
69187517Sgonzostatic struct resource_list *
70187517Sgonzo		apb_get_resource_list(device_t, device_t);
71187517Sgonzostatic void	apb_hinted_child(device_t, const char *, int);
72232847Sgonzostatic int	apb_filter(void *);
73187517Sgonzostatic int	apb_probe(device_t);
74187517Sgonzostatic int	apb_release_resource(device_t, device_t, int, int,
75187517Sgonzo		    struct resource *);
76187517Sgonzostatic int	apb_setup_intr(device_t, device_t, struct resource *, int,
77187517Sgonzo		    driver_filter_t *, driver_intr_t *, void *, void **);
78187517Sgonzostatic int	apb_teardown_intr(device_t, device_t, struct resource *,
79187517Sgonzo		    void *);
80187517Sgonzo
81192822Sgonzostatic void
82192822Sgonzoapb_mask_irq(void *source)
83187517Sgonzo{
84192822Sgonzo	unsigned int irq = (unsigned int)source;
85187517Sgonzo	uint32_t reg;
86187517Sgonzo
87187517Sgonzo	reg = ATH_READ_REG(AR71XX_MISC_INTR_MASK);
88187517Sgonzo	ATH_WRITE_REG(AR71XX_MISC_INTR_MASK, reg & ~(1 << irq));
89187517Sgonzo
90187517Sgonzo}
91187517Sgonzo
92192822Sgonzostatic void
93192822Sgonzoapb_unmask_irq(void *source)
94187517Sgonzo{
95187517Sgonzo	uint32_t reg;
96192822Sgonzo	unsigned int irq = (unsigned int)source;
97187517Sgonzo
98187517Sgonzo	reg = ATH_READ_REG(AR71XX_MISC_INTR_MASK);
99187517Sgonzo	ATH_WRITE_REG(AR71XX_MISC_INTR_MASK, reg | (1 << irq));
100187517Sgonzo}
101187517Sgonzo
102187517Sgonzostatic int
103187517Sgonzoapb_probe(device_t dev)
104187517Sgonzo{
105187517Sgonzo
106265999Sian	return (BUS_PROBE_NOWILDCARD);
107187517Sgonzo}
108187517Sgonzo
109187517Sgonzostatic int
110187517Sgonzoapb_attach(device_t dev)
111187517Sgonzo{
112187517Sgonzo	struct apb_softc *sc = device_get_softc(dev);
113187517Sgonzo	int rid = 0;
114187517Sgonzo
115187517Sgonzo	device_set_desc(dev, "APB Bus bridge");
116191837Sgonzo
117191837Sgonzo	sc->apb_mem_rman.rm_type = RMAN_ARRAY;
118191837Sgonzo	sc->apb_mem_rman.rm_descr = "APB memory window";
119191837Sgonzo
120191837Sgonzo	if (rman_init(&sc->apb_mem_rman) != 0 ||
121191837Sgonzo	    rman_manage_region(&sc->apb_mem_rman,
122191837Sgonzo			AR71XX_APB_BASE,
123191837Sgonzo			AR71XX_APB_BASE + AR71XX_APB_SIZE - 1) != 0)
124191837Sgonzo		panic("apb_attach: failed to set up memory rman");
125191837Sgonzo
126187517Sgonzo	sc->apb_irq_rman.rm_type = RMAN_ARRAY;
127187517Sgonzo	sc->apb_irq_rman.rm_descr = "APB IRQ";
128187517Sgonzo
129187517Sgonzo	if (rman_init(&sc->apb_irq_rman) != 0 ||
130187517Sgonzo	    rman_manage_region(&sc->apb_irq_rman,
131187517Sgonzo			APB_IRQ_BASE, APB_IRQ_END) != 0)
132187517Sgonzo		panic("apb_attach: failed to set up IRQ rman");
133187517Sgonzo
134187517Sgonzo	if ((sc->sc_misc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
135187517Sgonzo	    RF_SHAREABLE | RF_ACTIVE)) == NULL) {
136187517Sgonzo		device_printf(dev, "unable to allocate IRQ resource\n");
137187517Sgonzo		return (ENXIO);
138187517Sgonzo	}
139187517Sgonzo
140187517Sgonzo	if ((bus_setup_intr(dev, sc->sc_misc_irq, INTR_TYPE_MISC,
141232847Sgonzo	    apb_filter, NULL, sc, &sc->sc_misc_ih))) {
142187517Sgonzo		device_printf(dev,
143187517Sgonzo		    "WARNING: unable to register interrupt handler\n");
144187517Sgonzo		return (ENXIO);
145187517Sgonzo	}
146187517Sgonzo
147187517Sgonzo	bus_generic_probe(dev);
148187517Sgonzo	bus_enumerate_hinted_children(dev);
149187517Sgonzo	bus_generic_attach(dev);
150187517Sgonzo
151232847Sgonzo	/*
152232847Sgonzo	 * Unmask performance counter IRQ
153232847Sgonzo	 */
154232847Sgonzo	apb_unmask_irq((void*)APB_INTR_PMC);
155232847Sgonzo	sc->sc_intr_counter[APB_INTR_PMC] = mips_intrcnt_create("apb irq5: pmc");
156232847Sgonzo
157187517Sgonzo	return (0);
158187517Sgonzo}
159187517Sgonzo
160187517Sgonzostatic struct resource *
161187517Sgonzoapb_alloc_resource(device_t bus, device_t child, int type, int *rid,
162187517Sgonzo    u_long start, u_long end, u_long count, u_int flags)
163187517Sgonzo{
164187517Sgonzo	struct apb_softc		*sc = device_get_softc(bus);
165187517Sgonzo	struct apb_ivar			*ivar = device_get_ivars(child);
166187517Sgonzo	struct resource			*rv;
167187517Sgonzo	struct resource_list_entry	*rle;
168187517Sgonzo	struct rman			*rm;
169187517Sgonzo	int				 isdefault, needactivate, passthrough;
170187517Sgonzo
171187517Sgonzo	isdefault = (start == 0UL && end == ~0UL);
172187517Sgonzo	needactivate = flags & RF_ACTIVE;
173188883Sgonzo	/*
174188883Sgonzo	 * Pass memory requests to nexus device
175188883Sgonzo	 */
176191837Sgonzo	passthrough = (device_get_parent(child) != bus);
177187517Sgonzo	rle = NULL;
178187517Sgonzo
179191837Sgonzo	dprintf("%s: entry (%p, %p, %d, %d, %p, %p, %ld, %d)\n",
180191837Sgonzo	    __func__, bus, child, type, *rid, (void *)(intptr_t)start,
181188883Sgonzo	    (void *)(intptr_t)end, count, flags);
182188883Sgonzo
183187517Sgonzo	if (passthrough)
184187517Sgonzo		return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type,
185187517Sgonzo		    rid, start, end, count, flags));
186187517Sgonzo
187187517Sgonzo	/*
188187517Sgonzo	 * If this is an allocation of the "default" range for a given RID,
189187517Sgonzo	 * and we know what the resources for this device are (ie. they aren't
190187517Sgonzo	 * maintained by a child bus), then work out the start/end values.
191187517Sgonzo	 */
192187517Sgonzo
193187517Sgonzo	if (isdefault) {
194187517Sgonzo		rle = resource_list_find(&ivar->resources, type, *rid);
195187517Sgonzo		if (rle == NULL) {
196187517Sgonzo			return (NULL);
197187517Sgonzo		}
198187517Sgonzo
199187517Sgonzo		if (rle->res != NULL) {
200187517Sgonzo			panic("%s: resource entry is busy", __func__);
201187517Sgonzo		}
202187517Sgonzo		start = rle->start;
203187517Sgonzo		end = rle->end;
204187517Sgonzo		count = rle->count;
205188883Sgonzo
206188883Sgonzo		dprintf("%s: default resource (%p, %p, %ld)\n",
207188883Sgonzo		    __func__, (void *)(intptr_t)start,
208188883Sgonzo		    (void *)(intptr_t)end, count);
209187517Sgonzo	}
210187517Sgonzo
211187517Sgonzo	switch (type) {
212187517Sgonzo	case SYS_RES_IRQ:
213187517Sgonzo		rm = &sc->apb_irq_rman;
214187517Sgonzo		break;
215191837Sgonzo	case SYS_RES_MEMORY:
216191837Sgonzo		rm = &sc->apb_mem_rman;
217191837Sgonzo		break;
218187517Sgonzo	default:
219187517Sgonzo		printf("%s: unknown resource type %d\n", __func__, type);
220187517Sgonzo		return (0);
221187517Sgonzo	}
222187517Sgonzo
223187517Sgonzo	rv = rman_reserve_resource(rm, start, end, count, flags, child);
224187517Sgonzo	if (rv == 0) {
225187517Sgonzo		printf("%s: could not reserve resource\n", __func__);
226187517Sgonzo		return (0);
227187517Sgonzo	}
228187517Sgonzo
229187517Sgonzo	rman_set_rid(rv, *rid);
230187517Sgonzo
231187517Sgonzo	if (needactivate) {
232187517Sgonzo		if (bus_activate_resource(child, type, *rid, rv)) {
233187517Sgonzo			printf("%s: could not activate resource\n", __func__);
234187517Sgonzo			rman_release_resource(rv);
235187517Sgonzo			return (0);
236187517Sgonzo		}
237187517Sgonzo	}
238187517Sgonzo
239187517Sgonzo	return (rv);
240187517Sgonzo}
241187517Sgonzo
242187517Sgonzostatic int
243187517Sgonzoapb_activate_resource(device_t bus, device_t child, int type, int rid,
244187517Sgonzo    struct resource *r)
245187517Sgonzo{
246187517Sgonzo
247187517Sgonzo	/* XXX: should we mask/unmask IRQ here? */
248187517Sgonzo	return (BUS_ACTIVATE_RESOURCE(device_get_parent(bus), child,
249187517Sgonzo		type, rid, r));
250187517Sgonzo}
251187517Sgonzo
252187517Sgonzostatic int
253187517Sgonzoapb_deactivate_resource(device_t bus, device_t child, int type, int rid,
254187517Sgonzo    struct resource *r)
255187517Sgonzo{
256187517Sgonzo
257187517Sgonzo	/* XXX: should we mask/unmask IRQ here? */
258187517Sgonzo	return (BUS_DEACTIVATE_RESOURCE(device_get_parent(bus), child,
259187517Sgonzo		type, rid, r));
260187517Sgonzo}
261187517Sgonzo
262187517Sgonzostatic int
263187517Sgonzoapb_release_resource(device_t dev, device_t child, int type,
264187517Sgonzo    int rid, struct resource *r)
265187517Sgonzo{
266187517Sgonzo	struct resource_list *rl;
267187517Sgonzo	struct resource_list_entry *rle;
268187517Sgonzo
269187517Sgonzo	rl = apb_get_resource_list(dev, child);
270187517Sgonzo	if (rl == NULL)
271187517Sgonzo		return (EINVAL);
272187517Sgonzo	rle = resource_list_find(rl, type, rid);
273187517Sgonzo	if (rle == NULL)
274187517Sgonzo		return (EINVAL);
275187517Sgonzo	rman_release_resource(r);
276187517Sgonzo	rle->res = NULL;
277187517Sgonzo
278187517Sgonzo	return (0);
279187517Sgonzo}
280187517Sgonzo
281187517Sgonzostatic int
282187517Sgonzoapb_setup_intr(device_t bus, device_t child, struct resource *ires,
283187517Sgonzo		int flags, driver_filter_t *filt, driver_intr_t *handler,
284187517Sgonzo		void *arg, void **cookiep)
285187517Sgonzo{
286187517Sgonzo	struct apb_softc *sc = device_get_softc(bus);
287187517Sgonzo	struct intr_event *event;
288187517Sgonzo	int irq, error;
289187517Sgonzo
290187517Sgonzo	irq = rman_get_start(ires);
291187517Sgonzo
292187517Sgonzo	if (irq > APB_IRQ_END)
293187517Sgonzo		panic("%s: bad irq %d", __func__, irq);
294187517Sgonzo
295187517Sgonzo	event = sc->sc_eventstab[irq];
296187517Sgonzo	if (event == NULL) {
297187517Sgonzo		error = intr_event_create(&event, (void *)irq, 0, irq,
298192822Sgonzo		    apb_mask_irq, apb_unmask_irq,
299187517Sgonzo		    NULL, NULL,
300187517Sgonzo		    "apb intr%d:", irq);
301187517Sgonzo
302199497Sgonzo		if (error == 0) {
303199497Sgonzo			sc->sc_eventstab[irq] = event;
304199497Sgonzo			sc->sc_intr_counter[irq] =
305199497Sgonzo			    mips_intrcnt_create(event->ie_name);
306199497Sgonzo		}
307199497Sgonzo		else
308199497Sgonzo			return (error);
309187517Sgonzo	}
310187517Sgonzo
311187517Sgonzo	intr_event_add_handler(event, device_get_nameunit(child), filt,
312187517Sgonzo	    handler, arg, intr_priority(flags), flags, cookiep);
313199497Sgonzo	mips_intrcnt_setname(sc->sc_intr_counter[irq], event->ie_fullname);
314187517Sgonzo
315192822Sgonzo	apb_unmask_irq((void*)irq);
316191837Sgonzo
317187517Sgonzo	return (0);
318187517Sgonzo}
319187517Sgonzo
320187517Sgonzostatic int
321187517Sgonzoapb_teardown_intr(device_t dev, device_t child, struct resource *ires,
322187517Sgonzo    void *cookie)
323187517Sgonzo{
324187517Sgonzo	struct apb_softc *sc = device_get_softc(dev);
325187517Sgonzo	int irq, result;
326187517Sgonzo
327187517Sgonzo	irq = rman_get_start(ires);
328187517Sgonzo	if (irq > APB_IRQ_END)
329187517Sgonzo		panic("%s: bad irq %d", __func__, irq);
330187517Sgonzo
331187517Sgonzo	if (sc->sc_eventstab[irq] == NULL)
332187517Sgonzo		panic("Trying to teardown unoccupied IRQ");
333187517Sgonzo
334192822Sgonzo	apb_mask_irq((void*)irq);
335187517Sgonzo
336187517Sgonzo	result = intr_event_remove_handler(cookie);
337187517Sgonzo	if (!result)
338187517Sgonzo		sc->sc_eventstab[irq] = NULL;
339187517Sgonzo
340187517Sgonzo	return (result);
341187517Sgonzo}
342187517Sgonzo
343187517Sgonzostatic int
344232847Sgonzoapb_filter(void *arg)
345187517Sgonzo{
346187517Sgonzo	struct apb_softc *sc = arg;
347187517Sgonzo	struct intr_event *event;
348187517Sgonzo	uint32_t reg, irq;
349232847Sgonzo	struct thread *td;
350233104Sgonzo	struct trapframe *tf;
351187517Sgonzo
352187517Sgonzo	reg = ATH_READ_REG(AR71XX_MISC_INTR_STATUS);
353187517Sgonzo	for (irq = 0; irq < APB_NIRQS; irq++) {
354187517Sgonzo		if (reg & (1 << irq)) {
355221257Sadrian
356221257Sadrian			switch (ar71xx_soc) {
357221257Sadrian			case AR71XX_SOC_AR7240:
358221257Sadrian			case AR71XX_SOC_AR7241:
359221257Sadrian			case AR71XX_SOC_AR7242:
360249119Sadrian			case AR71XX_SOC_AR9330:
361249119Sadrian			case AR71XX_SOC_AR9331:
362256174Sadrian			case AR71XX_SOC_AR9341:
363256174Sadrian			case AR71XX_SOC_AR9342:
364256174Sadrian			case AR71XX_SOC_AR9344:
365221257Sadrian				/* Ack/clear the irq on status register for AR724x */
366221257Sadrian				ATH_WRITE_REG(AR71XX_MISC_INTR_STATUS,
367221257Sadrian				    reg & ~(1 << irq));
368221257Sadrian				break;
369221257Sadrian			default:
370221257Sadrian				/* fallthrough */
371221257Sadrian				break;
372221257Sadrian			}
373221257Sadrian
374187517Sgonzo			event = sc->sc_eventstab[irq];
375187517Sgonzo			if (!event || TAILQ_EMPTY(&event->ie_handlers)) {
376232847Sgonzo				if (irq == APB_INTR_PMC) {
377233104Sgonzo					td = PCPU_GET(curthread);
378233104Sgonzo					tf = td->td_intr_frame;
379233104Sgonzo
380233318Sgonzo					if (pmc_intr)
381233318Sgonzo						(*pmc_intr)(PCPU_GET(cpuid), tf);
382233318Sgonzo
383232847Sgonzo					mips_intrcnt_inc(sc->sc_intr_counter[irq]);
384232847Sgonzo
385232847Sgonzo					continue;
386232847Sgonzo				}
387191837Sgonzo				/* Ignore timer interrupts */
388191837Sgonzo				if (irq != 0)
389199497Sgonzo					printf("Stray APB IRQ %d\n", irq);
390187517Sgonzo				continue;
391187517Sgonzo			}
392187517Sgonzo
393232847Sgonzo			intr_event_handle(event, PCPU_GET(curthread)->td_intr_frame);
394199497Sgonzo			mips_intrcnt_inc(sc->sc_intr_counter[irq]);
395187517Sgonzo		}
396187517Sgonzo	}
397187517Sgonzo
398187517Sgonzo	return (FILTER_HANDLED);
399187517Sgonzo}
400187517Sgonzo
401187517Sgonzostatic void
402187517Sgonzoapb_hinted_child(device_t bus, const char *dname, int dunit)
403187517Sgonzo{
404187517Sgonzo	device_t		child;
405187517Sgonzo	long			maddr;
406187517Sgonzo	int			msize;
407187517Sgonzo	int			irq;
408187517Sgonzo	int			result;
409187517Sgonzo	int			mem_hints_count;
410187517Sgonzo
411187517Sgonzo	child = BUS_ADD_CHILD(bus, 0, dname, dunit);
412187517Sgonzo
413187517Sgonzo	/*
414187517Sgonzo	 * Set hard-wired resources for hinted child using
415187517Sgonzo	 * specific RIDs.
416187517Sgonzo	 */
417187517Sgonzo	mem_hints_count = 0;
418187517Sgonzo	if (resource_long_value(dname, dunit, "maddr", &maddr) == 0)
419187517Sgonzo		mem_hints_count++;
420187517Sgonzo	if (resource_int_value(dname, dunit, "msize", &msize) == 0)
421187517Sgonzo		mem_hints_count++;
422187517Sgonzo
423187517Sgonzo	/* check if all info for mem resource has been provided */
424187517Sgonzo	if ((mem_hints_count > 0) && (mem_hints_count < 2)) {
425187517Sgonzo		printf("Either maddr or msize hint is missing for %s%d\n",
426187517Sgonzo		    dname, dunit);
427187517Sgonzo	} else if (mem_hints_count) {
428187517Sgonzo		result = bus_set_resource(child, SYS_RES_MEMORY, 0,
429187517Sgonzo		    maddr, msize);
430187517Sgonzo		if (result != 0)
431187517Sgonzo			device_printf(bus,
432187517Sgonzo			    "warning: bus_set_resource() failed\n");
433187517Sgonzo	}
434187517Sgonzo
435187517Sgonzo	if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
436187517Sgonzo		result = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
437187517Sgonzo		if (result != 0)
438187517Sgonzo			device_printf(bus,
439187517Sgonzo			    "warning: bus_set_resource() failed\n");
440187517Sgonzo	}
441187517Sgonzo}
442187517Sgonzo
443187517Sgonzostatic device_t
444212413Savgapb_add_child(device_t bus, u_int order, const char *name, int unit)
445187517Sgonzo{
446187517Sgonzo	device_t		child;
447187517Sgonzo	struct apb_ivar	*ivar;
448187517Sgonzo
449187517Sgonzo	ivar = malloc(sizeof(struct apb_ivar), M_DEVBUF, M_WAITOK | M_ZERO);
450187517Sgonzo	if (ivar == NULL) {
451187517Sgonzo		printf("Failed to allocate ivar\n");
452187517Sgonzo		return (0);
453187517Sgonzo	}
454187517Sgonzo	resource_list_init(&ivar->resources);
455187517Sgonzo
456187517Sgonzo	child = device_add_child_ordered(bus, order, name, unit);
457187517Sgonzo	if (child == NULL) {
458187517Sgonzo		printf("Can't add child %s%d ordered\n", name, unit);
459187517Sgonzo		return (0);
460187517Sgonzo	}
461187517Sgonzo
462187517Sgonzo	device_set_ivars(child, ivar);
463187517Sgonzo
464187517Sgonzo	return (child);
465187517Sgonzo}
466187517Sgonzo
467187517Sgonzo/*
468187517Sgonzo * Helper routine for bus_generic_rl_get_resource/bus_generic_rl_set_resource
469187517Sgonzo * Provides pointer to resource_list for these routines
470187517Sgonzo */
471187517Sgonzostatic struct resource_list *
472187517Sgonzoapb_get_resource_list(device_t dev, device_t child)
473187517Sgonzo{
474187517Sgonzo	struct apb_ivar *ivar;
475187517Sgonzo
476187517Sgonzo	ivar = device_get_ivars(child);
477187517Sgonzo	return (&(ivar->resources));
478187517Sgonzo}
479187517Sgonzo
480187517Sgonzostatic device_method_t apb_methods[] = {
481187517Sgonzo	DEVMETHOD(bus_activate_resource,	apb_activate_resource),
482187517Sgonzo	DEVMETHOD(bus_add_child,		apb_add_child),
483187517Sgonzo	DEVMETHOD(bus_alloc_resource,		apb_alloc_resource),
484187517Sgonzo	DEVMETHOD(bus_deactivate_resource,	apb_deactivate_resource),
485187517Sgonzo	DEVMETHOD(bus_get_resource_list,	apb_get_resource_list),
486187517Sgonzo	DEVMETHOD(bus_hinted_child,		apb_hinted_child),
487187517Sgonzo	DEVMETHOD(bus_release_resource,		apb_release_resource),
488187517Sgonzo	DEVMETHOD(bus_setup_intr,		apb_setup_intr),
489187517Sgonzo	DEVMETHOD(bus_teardown_intr,		apb_teardown_intr),
490187517Sgonzo	DEVMETHOD(device_attach,		apb_attach),
491187517Sgonzo	DEVMETHOD(device_probe,			apb_probe),
492187517Sgonzo	DEVMETHOD(bus_get_resource,		bus_generic_rl_get_resource),
493187517Sgonzo	DEVMETHOD(bus_set_resource,		bus_generic_rl_set_resource),
494187517Sgonzo
495227843Smarius	DEVMETHOD_END
496187517Sgonzo};
497187517Sgonzo
498187517Sgonzostatic driver_t apb_driver = {
499187517Sgonzo	"apb",
500187517Sgonzo	apb_methods,
501187517Sgonzo	sizeof(struct apb_softc),
502187517Sgonzo};
503187517Sgonzostatic devclass_t apb_devclass;
504187517Sgonzo
505187517SgonzoDRIVER_MODULE(apb, nexus, apb_driver, apb_devclass, 0, 0);
506