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
60263279Sadrian#define	DEVTOAPB(dev)	((struct apb_ivar *) device_get_ivars(dev))
61263279Sadrian
62187517Sgonzostatic int	apb_activate_resource(device_t, device_t, int, int,
63187517Sgonzo		    struct resource *);
64212413Savgstatic device_t	apb_add_child(device_t, u_int, const char *, int);
65187517Sgonzostatic struct resource *
66294883Sjhibbits		apb_alloc_resource(device_t, device_t, int, int *, rman_res_t,
67294883Sjhibbits		    rman_res_t, rman_res_t, u_int);
68187517Sgonzostatic int	apb_attach(device_t);
69187517Sgonzostatic int	apb_deactivate_resource(device_t, device_t, int, int,
70187517Sgonzo		    struct resource *);
71187517Sgonzostatic struct resource_list *
72187517Sgonzo		apb_get_resource_list(device_t, device_t);
73187517Sgonzostatic void	apb_hinted_child(device_t, const char *, int);
74232847Sgonzostatic int	apb_filter(void *);
75187517Sgonzostatic int	apb_probe(device_t);
76187517Sgonzostatic int	apb_release_resource(device_t, device_t, int, int,
77187517Sgonzo		    struct resource *);
78187517Sgonzostatic int	apb_setup_intr(device_t, device_t, struct resource *, int,
79187517Sgonzo		    driver_filter_t *, driver_intr_t *, void *, void **);
80187517Sgonzostatic int	apb_teardown_intr(device_t, device_t, struct resource *,
81187517Sgonzo		    void *);
82187517Sgonzo
83192822Sgonzostatic void
84192822Sgonzoapb_mask_irq(void *source)
85187517Sgonzo{
86192822Sgonzo	unsigned int irq = (unsigned int)source;
87187517Sgonzo	uint32_t reg;
88187517Sgonzo
89187517Sgonzo	reg = ATH_READ_REG(AR71XX_MISC_INTR_MASK);
90187517Sgonzo	ATH_WRITE_REG(AR71XX_MISC_INTR_MASK, reg & ~(1 << irq));
91187517Sgonzo
92187517Sgonzo}
93187517Sgonzo
94192822Sgonzostatic void
95192822Sgonzoapb_unmask_irq(void *source)
96187517Sgonzo{
97187517Sgonzo	uint32_t reg;
98192822Sgonzo	unsigned int irq = (unsigned int)source;
99187517Sgonzo
100187517Sgonzo	reg = ATH_READ_REG(AR71XX_MISC_INTR_MASK);
101187517Sgonzo	ATH_WRITE_REG(AR71XX_MISC_INTR_MASK, reg | (1 << irq));
102187517Sgonzo}
103187517Sgonzo
104187517Sgonzostatic int
105187517Sgonzoapb_probe(device_t dev)
106187517Sgonzo{
107187517Sgonzo
108257338Snwhitehorn	return (BUS_PROBE_NOWILDCARD);
109187517Sgonzo}
110187517Sgonzo
111187517Sgonzostatic int
112187517Sgonzoapb_attach(device_t dev)
113187517Sgonzo{
114187517Sgonzo	struct apb_softc *sc = device_get_softc(dev);
115187517Sgonzo	int rid = 0;
116187517Sgonzo
117187517Sgonzo	device_set_desc(dev, "APB Bus bridge");
118191837Sgonzo
119191837Sgonzo	sc->apb_mem_rman.rm_type = RMAN_ARRAY;
120191837Sgonzo	sc->apb_mem_rman.rm_descr = "APB memory window";
121191837Sgonzo
122191837Sgonzo	if (rman_init(&sc->apb_mem_rman) != 0 ||
123191837Sgonzo	    rman_manage_region(&sc->apb_mem_rman,
124191837Sgonzo			AR71XX_APB_BASE,
125191837Sgonzo			AR71XX_APB_BASE + AR71XX_APB_SIZE - 1) != 0)
126191837Sgonzo		panic("apb_attach: failed to set up memory rman");
127191837Sgonzo
128187517Sgonzo	sc->apb_irq_rman.rm_type = RMAN_ARRAY;
129187517Sgonzo	sc->apb_irq_rman.rm_descr = "APB IRQ";
130187517Sgonzo
131187517Sgonzo	if (rman_init(&sc->apb_irq_rman) != 0 ||
132187517Sgonzo	    rman_manage_region(&sc->apb_irq_rman,
133187517Sgonzo			APB_IRQ_BASE, APB_IRQ_END) != 0)
134187517Sgonzo		panic("apb_attach: failed to set up IRQ rman");
135187517Sgonzo
136187517Sgonzo	if ((sc->sc_misc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
137187517Sgonzo	    RF_SHAREABLE | RF_ACTIVE)) == NULL) {
138187517Sgonzo		device_printf(dev, "unable to allocate IRQ resource\n");
139187517Sgonzo		return (ENXIO);
140187517Sgonzo	}
141187517Sgonzo
142187517Sgonzo	if ((bus_setup_intr(dev, sc->sc_misc_irq, INTR_TYPE_MISC,
143232847Sgonzo	    apb_filter, NULL, sc, &sc->sc_misc_ih))) {
144187517Sgonzo		device_printf(dev,
145187517Sgonzo		    "WARNING: unable to register interrupt handler\n");
146187517Sgonzo		return (ENXIO);
147187517Sgonzo	}
148187517Sgonzo
149187517Sgonzo	bus_generic_probe(dev);
150187517Sgonzo	bus_enumerate_hinted_children(dev);
151187517Sgonzo	bus_generic_attach(dev);
152187517Sgonzo
153232847Sgonzo	/*
154232847Sgonzo	 * Unmask performance counter IRQ
155232847Sgonzo	 */
156232847Sgonzo	apb_unmask_irq((void*)APB_INTR_PMC);
157232847Sgonzo	sc->sc_intr_counter[APB_INTR_PMC] = mips_intrcnt_create("apb irq5: pmc");
158232847Sgonzo
159187517Sgonzo	return (0);
160187517Sgonzo}
161187517Sgonzo
162187517Sgonzostatic struct resource *
163187517Sgonzoapb_alloc_resource(device_t bus, device_t child, int type, int *rid,
164294883Sjhibbits    rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
165187517Sgonzo{
166187517Sgonzo	struct apb_softc		*sc = device_get_softc(bus);
167187517Sgonzo	struct apb_ivar			*ivar = device_get_ivars(child);
168187517Sgonzo	struct resource			*rv;
169187517Sgonzo	struct resource_list_entry	*rle;
170187517Sgonzo	struct rman			*rm;
171187517Sgonzo	int				 isdefault, needactivate, passthrough;
172187517Sgonzo
173295832Sjhibbits	isdefault = (RMAN_IS_DEFAULT_RANGE(start, end));
174187517Sgonzo	needactivate = flags & RF_ACTIVE;
175188883Sgonzo	/*
176188883Sgonzo	 * Pass memory requests to nexus device
177188883Sgonzo	 */
178191837Sgonzo	passthrough = (device_get_parent(child) != bus);
179187517Sgonzo	rle = NULL;
180187517Sgonzo
181297000Sjhibbits	dprintf("%s: entry (%p, %p, %d, %d, %p, %p, %jd, %d)\n",
182191837Sgonzo	    __func__, bus, child, type, *rid, (void *)(intptr_t)start,
183188883Sgonzo	    (void *)(intptr_t)end, count, flags);
184188883Sgonzo
185187517Sgonzo	if (passthrough)
186187517Sgonzo		return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type,
187187517Sgonzo		    rid, start, end, count, flags));
188187517Sgonzo
189187517Sgonzo	/*
190187517Sgonzo	 * If this is an allocation of the "default" range for a given RID,
191187517Sgonzo	 * and we know what the resources for this device are (ie. they aren't
192187517Sgonzo	 * maintained by a child bus), then work out the start/end values.
193187517Sgonzo	 */
194187517Sgonzo
195187517Sgonzo	if (isdefault) {
196187517Sgonzo		rle = resource_list_find(&ivar->resources, type, *rid);
197187517Sgonzo		if (rle == NULL) {
198187517Sgonzo			return (NULL);
199187517Sgonzo		}
200187517Sgonzo
201187517Sgonzo		if (rle->res != NULL) {
202187517Sgonzo			panic("%s: resource entry is busy", __func__);
203187517Sgonzo		}
204187517Sgonzo		start = rle->start;
205187517Sgonzo		end = rle->end;
206187517Sgonzo		count = rle->count;
207188883Sgonzo
208188883Sgonzo		dprintf("%s: default resource (%p, %p, %ld)\n",
209188883Sgonzo		    __func__, (void *)(intptr_t)start,
210188883Sgonzo		    (void *)(intptr_t)end, count);
211187517Sgonzo	}
212187517Sgonzo
213187517Sgonzo	switch (type) {
214187517Sgonzo	case SYS_RES_IRQ:
215187517Sgonzo		rm = &sc->apb_irq_rman;
216187517Sgonzo		break;
217191837Sgonzo	case SYS_RES_MEMORY:
218191837Sgonzo		rm = &sc->apb_mem_rman;
219191837Sgonzo		break;
220187517Sgonzo	default:
221187517Sgonzo		printf("%s: unknown resource type %d\n", __func__, type);
222187517Sgonzo		return (0);
223187517Sgonzo	}
224187517Sgonzo
225187517Sgonzo	rv = rman_reserve_resource(rm, start, end, count, flags, child);
226298053Spfg	if (rv == NULL) {
227187517Sgonzo		printf("%s: could not reserve resource\n", __func__);
228187517Sgonzo		return (0);
229187517Sgonzo	}
230187517Sgonzo
231187517Sgonzo	rman_set_rid(rv, *rid);
232187517Sgonzo
233187517Sgonzo	if (needactivate) {
234187517Sgonzo		if (bus_activate_resource(child, type, *rid, rv)) {
235187517Sgonzo			printf("%s: could not activate resource\n", __func__);
236187517Sgonzo			rman_release_resource(rv);
237187517Sgonzo			return (0);
238187517Sgonzo		}
239187517Sgonzo	}
240187517Sgonzo
241187517Sgonzo	return (rv);
242187517Sgonzo}
243187517Sgonzo
244187517Sgonzostatic int
245187517Sgonzoapb_activate_resource(device_t bus, device_t child, int type, int rid,
246187517Sgonzo    struct resource *r)
247187517Sgonzo{
248187517Sgonzo
249187517Sgonzo	/* XXX: should we mask/unmask IRQ here? */
250187517Sgonzo	return (BUS_ACTIVATE_RESOURCE(device_get_parent(bus), child,
251187517Sgonzo		type, rid, r));
252187517Sgonzo}
253187517Sgonzo
254187517Sgonzostatic int
255187517Sgonzoapb_deactivate_resource(device_t bus, device_t child, int type, int rid,
256187517Sgonzo    struct resource *r)
257187517Sgonzo{
258187517Sgonzo
259187517Sgonzo	/* XXX: should we mask/unmask IRQ here? */
260187517Sgonzo	return (BUS_DEACTIVATE_RESOURCE(device_get_parent(bus), child,
261187517Sgonzo		type, rid, r));
262187517Sgonzo}
263187517Sgonzo
264187517Sgonzostatic int
265187517Sgonzoapb_release_resource(device_t dev, device_t child, int type,
266187517Sgonzo    int rid, struct resource *r)
267187517Sgonzo{
268187517Sgonzo	struct resource_list *rl;
269187517Sgonzo	struct resource_list_entry *rle;
270187517Sgonzo
271187517Sgonzo	rl = apb_get_resource_list(dev, child);
272187517Sgonzo	if (rl == NULL)
273187517Sgonzo		return (EINVAL);
274187517Sgonzo	rle = resource_list_find(rl, type, rid);
275187517Sgonzo	if (rle == NULL)
276187517Sgonzo		return (EINVAL);
277187517Sgonzo	rman_release_resource(r);
278187517Sgonzo	rle->res = NULL;
279187517Sgonzo
280187517Sgonzo	return (0);
281187517Sgonzo}
282187517Sgonzo
283187517Sgonzostatic int
284187517Sgonzoapb_setup_intr(device_t bus, device_t child, struct resource *ires,
285187517Sgonzo		int flags, driver_filter_t *filt, driver_intr_t *handler,
286187517Sgonzo		void *arg, void **cookiep)
287187517Sgonzo{
288187517Sgonzo	struct apb_softc *sc = device_get_softc(bus);
289187517Sgonzo	struct intr_event *event;
290187517Sgonzo	int irq, error;
291187517Sgonzo
292187517Sgonzo	irq = rman_get_start(ires);
293187517Sgonzo
294187517Sgonzo	if (irq > APB_IRQ_END)
295187517Sgonzo		panic("%s: bad irq %d", __func__, irq);
296187517Sgonzo
297187517Sgonzo	event = sc->sc_eventstab[irq];
298187517Sgonzo	if (event == NULL) {
299187517Sgonzo		error = intr_event_create(&event, (void *)irq, 0, irq,
300192822Sgonzo		    apb_mask_irq, apb_unmask_irq,
301187517Sgonzo		    NULL, NULL,
302187517Sgonzo		    "apb intr%d:", irq);
303187517Sgonzo
304199497Sgonzo		if (error == 0) {
305199497Sgonzo			sc->sc_eventstab[irq] = event;
306199497Sgonzo			sc->sc_intr_counter[irq] =
307199497Sgonzo			    mips_intrcnt_create(event->ie_name);
308199497Sgonzo		}
309199497Sgonzo		else
310199497Sgonzo			return (error);
311187517Sgonzo	}
312187517Sgonzo
313187517Sgonzo	intr_event_add_handler(event, device_get_nameunit(child), filt,
314187517Sgonzo	    handler, arg, intr_priority(flags), flags, cookiep);
315199497Sgonzo	mips_intrcnt_setname(sc->sc_intr_counter[irq], event->ie_fullname);
316187517Sgonzo
317192822Sgonzo	apb_unmask_irq((void*)irq);
318191837Sgonzo
319187517Sgonzo	return (0);
320187517Sgonzo}
321187517Sgonzo
322187517Sgonzostatic int
323187517Sgonzoapb_teardown_intr(device_t dev, device_t child, struct resource *ires,
324187517Sgonzo    void *cookie)
325187517Sgonzo{
326187517Sgonzo	struct apb_softc *sc = device_get_softc(dev);
327187517Sgonzo	int irq, result;
328187517Sgonzo
329187517Sgonzo	irq = rman_get_start(ires);
330187517Sgonzo	if (irq > APB_IRQ_END)
331187517Sgonzo		panic("%s: bad irq %d", __func__, irq);
332187517Sgonzo
333187517Sgonzo	if (sc->sc_eventstab[irq] == NULL)
334187517Sgonzo		panic("Trying to teardown unoccupied IRQ");
335187517Sgonzo
336192822Sgonzo	apb_mask_irq((void*)irq);
337187517Sgonzo
338187517Sgonzo	result = intr_event_remove_handler(cookie);
339187517Sgonzo	if (!result)
340187517Sgonzo		sc->sc_eventstab[irq] = NULL;
341187517Sgonzo
342187517Sgonzo	return (result);
343187517Sgonzo}
344187517Sgonzo
345187517Sgonzostatic int
346232847Sgonzoapb_filter(void *arg)
347187517Sgonzo{
348187517Sgonzo	struct apb_softc *sc = arg;
349187517Sgonzo	struct intr_event *event;
350187517Sgonzo	uint32_t reg, irq;
351232847Sgonzo	struct thread *td;
352233104Sgonzo	struct trapframe *tf;
353187517Sgonzo
354187517Sgonzo	reg = ATH_READ_REG(AR71XX_MISC_INTR_STATUS);
355187517Sgonzo	for (irq = 0; irq < APB_NIRQS; irq++) {
356187517Sgonzo		if (reg & (1 << irq)) {
357221257Sadrian
358221257Sadrian			switch (ar71xx_soc) {
359221257Sadrian			case AR71XX_SOC_AR7240:
360221257Sadrian			case AR71XX_SOC_AR7241:
361221257Sadrian			case AR71XX_SOC_AR7242:
362249119Sadrian			case AR71XX_SOC_AR9330:
363249119Sadrian			case AR71XX_SOC_AR9331:
364256174Sadrian			case AR71XX_SOC_AR9341:
365256174Sadrian			case AR71XX_SOC_AR9342:
366256174Sadrian			case AR71XX_SOC_AR9344:
367290918Sadrian			case AR71XX_SOC_QCA9533:
368290918Sadrian			case AR71XX_SOC_QCA9533_V2:
369276684Sadrian			case AR71XX_SOC_QCA9556:
370276684Sadrian			case AR71XX_SOC_QCA9558:
371263228Sadrian				/* ACK/clear the given interrupt */
372221257Sadrian				ATH_WRITE_REG(AR71XX_MISC_INTR_STATUS,
373263228Sadrian				    (1 << irq));
374221257Sadrian				break;
375221257Sadrian			default:
376221257Sadrian				/* fallthrough */
377221257Sadrian				break;
378221257Sadrian			}
379221257Sadrian
380187517Sgonzo			event = sc->sc_eventstab[irq];
381292246Sadrian			/* always count interrupts; spurious or otherwise */
382292246Sadrian			mips_intrcnt_inc(sc->sc_intr_counter[irq]);
383187517Sgonzo			if (!event || TAILQ_EMPTY(&event->ie_handlers)) {
384232847Sgonzo				if (irq == APB_INTR_PMC) {
385233104Sgonzo					td = PCPU_GET(curthread);
386233104Sgonzo					tf = td->td_intr_frame;
387233104Sgonzo
388233318Sgonzo					if (pmc_intr)
389233318Sgonzo						(*pmc_intr)(PCPU_GET(cpuid), tf);
390232847Sgonzo					continue;
391232847Sgonzo				}
392191837Sgonzo				/* Ignore timer interrupts */
393263228Sadrian				if (irq != 0 && irq != 8 && irq != 9 && irq != 10)
394199497Sgonzo					printf("Stray APB IRQ %d\n", irq);
395187517Sgonzo				continue;
396187517Sgonzo			}
397187517Sgonzo
398232847Sgonzo			intr_event_handle(event, PCPU_GET(curthread)->td_intr_frame);
399187517Sgonzo		}
400187517Sgonzo	}
401187517Sgonzo
402187517Sgonzo	return (FILTER_HANDLED);
403187517Sgonzo}
404187517Sgonzo
405187517Sgonzostatic void
406187517Sgonzoapb_hinted_child(device_t bus, const char *dname, int dunit)
407187517Sgonzo{
408187517Sgonzo	device_t		child;
409187517Sgonzo	long			maddr;
410187517Sgonzo	int			msize;
411187517Sgonzo	int			irq;
412187517Sgonzo	int			result;
413187517Sgonzo	int			mem_hints_count;
414187517Sgonzo
415187517Sgonzo	child = BUS_ADD_CHILD(bus, 0, dname, dunit);
416187517Sgonzo
417187517Sgonzo	/*
418187517Sgonzo	 * Set hard-wired resources for hinted child using
419187517Sgonzo	 * specific RIDs.
420187517Sgonzo	 */
421187517Sgonzo	mem_hints_count = 0;
422187517Sgonzo	if (resource_long_value(dname, dunit, "maddr", &maddr) == 0)
423187517Sgonzo		mem_hints_count++;
424187517Sgonzo	if (resource_int_value(dname, dunit, "msize", &msize) == 0)
425187517Sgonzo		mem_hints_count++;
426187517Sgonzo
427187517Sgonzo	/* check if all info for mem resource has been provided */
428187517Sgonzo	if ((mem_hints_count > 0) && (mem_hints_count < 2)) {
429187517Sgonzo		printf("Either maddr or msize hint is missing for %s%d\n",
430187517Sgonzo		    dname, dunit);
431187517Sgonzo	} else if (mem_hints_count) {
432187517Sgonzo		result = bus_set_resource(child, SYS_RES_MEMORY, 0,
433187517Sgonzo		    maddr, msize);
434187517Sgonzo		if (result != 0)
435187517Sgonzo			device_printf(bus,
436187517Sgonzo			    "warning: bus_set_resource() failed\n");
437187517Sgonzo	}
438187517Sgonzo
439187517Sgonzo	if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
440187517Sgonzo		result = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
441187517Sgonzo		if (result != 0)
442187517Sgonzo			device_printf(bus,
443187517Sgonzo			    "warning: bus_set_resource() failed\n");
444187517Sgonzo	}
445187517Sgonzo}
446187517Sgonzo
447187517Sgonzostatic device_t
448212413Savgapb_add_child(device_t bus, u_int order, const char *name, int unit)
449187517Sgonzo{
450187517Sgonzo	device_t		child;
451187517Sgonzo	struct apb_ivar	*ivar;
452187517Sgonzo
453187517Sgonzo	ivar = malloc(sizeof(struct apb_ivar), M_DEVBUF, M_WAITOK | M_ZERO);
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
480263279Sadrianstatic int
481263279Sadrianapb_print_all_resources(device_t dev)
482263279Sadrian{
483263279Sadrian	struct apb_ivar *ndev = DEVTOAPB(dev);
484263279Sadrian	struct resource_list *rl = &ndev->resources;
485263279Sadrian	int retval = 0;
486263279Sadrian
487263279Sadrian	if (STAILQ_FIRST(rl))
488263279Sadrian		retval += printf(" at");
489263279Sadrian
490297199Sjhibbits	retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#jx");
491297199Sjhibbits	retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
492263279Sadrian
493263279Sadrian	return (retval);
494263279Sadrian}
495263279Sadrian
496263279Sadrianstatic int
497263279Sadrianapb_print_child(device_t bus, device_t child)
498263279Sadrian{
499263279Sadrian	int retval = 0;
500263279Sadrian
501263279Sadrian	retval += bus_print_child_header(bus, child);
502263279Sadrian	retval += apb_print_all_resources(child);
503263279Sadrian	if (device_get_flags(child))
504263279Sadrian		retval += printf(" flags %#x", device_get_flags(child));
505263279Sadrian	retval += printf(" on %s\n", device_get_nameunit(bus));
506263279Sadrian
507263279Sadrian	return (retval);
508263279Sadrian}
509263279Sadrian
510263279Sadrian
511187517Sgonzostatic device_method_t apb_methods[] = {
512187517Sgonzo	DEVMETHOD(bus_activate_resource,	apb_activate_resource),
513187517Sgonzo	DEVMETHOD(bus_add_child,		apb_add_child),
514187517Sgonzo	DEVMETHOD(bus_alloc_resource,		apb_alloc_resource),
515187517Sgonzo	DEVMETHOD(bus_deactivate_resource,	apb_deactivate_resource),
516187517Sgonzo	DEVMETHOD(bus_get_resource_list,	apb_get_resource_list),
517187517Sgonzo	DEVMETHOD(bus_hinted_child,		apb_hinted_child),
518187517Sgonzo	DEVMETHOD(bus_release_resource,		apb_release_resource),
519187517Sgonzo	DEVMETHOD(bus_setup_intr,		apb_setup_intr),
520187517Sgonzo	DEVMETHOD(bus_teardown_intr,		apb_teardown_intr),
521187517Sgonzo	DEVMETHOD(device_attach,		apb_attach),
522187517Sgonzo	DEVMETHOD(device_probe,			apb_probe),
523187517Sgonzo	DEVMETHOD(bus_get_resource,		bus_generic_rl_get_resource),
524187517Sgonzo	DEVMETHOD(bus_set_resource,		bus_generic_rl_set_resource),
525263279Sadrian	DEVMETHOD(bus_print_child,		apb_print_child),
526187517Sgonzo
527227843Smarius	DEVMETHOD_END
528187517Sgonzo};
529187517Sgonzo
530187517Sgonzostatic driver_t apb_driver = {
531187517Sgonzo	"apb",
532187517Sgonzo	apb_methods,
533187517Sgonzo	sizeof(struct apb_softc),
534187517Sgonzo};
535187517Sgonzostatic devclass_t apb_devclass;
536187517Sgonzo
537187517SgonzoDRIVER_MODULE(apb, nexus, apb_driver, apb_devclass, 0, 0);
538