apb.c revision 187517
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>
39187517Sgonzo
40187517Sgonzo#include <machine/bus.h>
41187517Sgonzo
42187517Sgonzo#include <mips/atheros/apbvar.h>
43187517Sgonzo#include <mips/atheros/ar71xxreg.h>
44187517Sgonzo
45187517Sgonzostatic int	apb_activate_resource(device_t, device_t, int, int,
46187517Sgonzo		    struct resource *);
47187517Sgonzostatic device_t	apb_add_child(device_t, int, const char *, int);
48187517Sgonzostatic struct resource *
49187517Sgonzo		apb_alloc_resource(device_t, device_t, int, int *, u_long,
50187517Sgonzo		    u_long, u_long, u_int);
51187517Sgonzostatic int	apb_attach(device_t);
52187517Sgonzostatic int	apb_deactivate_resource(device_t, device_t, int, int,
53187517Sgonzo		    struct resource *);
54187517Sgonzostatic struct resource_list *
55187517Sgonzo		apb_get_resource_list(device_t, device_t);
56187517Sgonzostatic void	apb_hinted_child(device_t, const char *, int);
57187517Sgonzostatic int	apb_intr(void *);
58187517Sgonzostatic int	apb_probe(device_t);
59187517Sgonzostatic int	apb_release_resource(device_t, device_t, int, int,
60187517Sgonzo		    struct resource *);
61187517Sgonzostatic int	apb_setup_intr(device_t, device_t, struct resource *, int,
62187517Sgonzo		    driver_filter_t *, driver_intr_t *, void *, void **);
63187517Sgonzostatic int	apb_teardown_intr(device_t, device_t, struct resource *,
64187517Sgonzo		    void *);
65187517Sgonzo
66187517Sgonzostatic void apb_mask_irq(unsigned int irq)
67187517Sgonzo{
68187517Sgonzo	uint32_t reg;
69187517Sgonzo
70187517Sgonzo	reg = ATH_READ_REG(AR71XX_MISC_INTR_MASK);
71187517Sgonzo	ATH_WRITE_REG(AR71XX_MISC_INTR_MASK, reg & ~(1 << irq));
72187517Sgonzo
73187517Sgonzo}
74187517Sgonzo
75187517Sgonzostatic void apb_unmask_irq(unsigned int irq)
76187517Sgonzo{
77187517Sgonzo	uint32_t reg;
78187517Sgonzo
79187517Sgonzo	reg = ATH_READ_REG(AR71XX_MISC_INTR_MASK);
80187517Sgonzo	ATH_WRITE_REG(AR71XX_MISC_INTR_MASK, reg | (1 << irq));
81187517Sgonzo}
82187517Sgonzo
83187517Sgonzostatic int
84187517Sgonzoapb_probe(device_t dev)
85187517Sgonzo{
86187517Sgonzo
87187517Sgonzo	return (0);
88187517Sgonzo}
89187517Sgonzo
90187517Sgonzostatic int
91187517Sgonzoapb_attach(device_t dev)
92187517Sgonzo{
93187517Sgonzo	struct apb_softc *sc = device_get_softc(dev);
94187517Sgonzo	int rid = 0;
95187517Sgonzo
96187517Sgonzo	device_set_desc(dev, "APB Bus bridge");
97187517Sgonzo	sc->apb_mem_rman.rm_type = RMAN_ARRAY;
98187517Sgonzo	sc->apb_mem_rman.rm_descr = "APB memory";
99187517Sgonzo	if (rman_init(&sc->apb_mem_rman) != 0 ||
100187517Sgonzo	    rman_manage_region(&sc->apb_mem_rman, APB_MEM_START,
101187517Sgonzo	        APB_MEM_END) != 0)
102187517Sgonzo		panic("apb_attach: failed to set up I/O rman");
103187517Sgonzo
104187517Sgonzo	sc->apb_irq_rman.rm_type = RMAN_ARRAY;
105187517Sgonzo	sc->apb_irq_rman.rm_descr = "APB IRQ";
106187517Sgonzo
107187517Sgonzo	if (rman_init(&sc->apb_irq_rman) != 0 ||
108187517Sgonzo	    rman_manage_region(&sc->apb_irq_rman,
109187517Sgonzo			APB_IRQ_BASE, APB_IRQ_END) != 0)
110187517Sgonzo		panic("apb_attach: failed to set up IRQ rman");
111187517Sgonzo
112187517Sgonzo	if ((sc->sc_misc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
113187517Sgonzo	    RF_SHAREABLE | RF_ACTIVE)) == NULL) {
114187517Sgonzo		device_printf(dev, "unable to allocate IRQ resource\n");
115187517Sgonzo		return (ENXIO);
116187517Sgonzo	}
117187517Sgonzo
118187517Sgonzo	if ((bus_setup_intr(dev, sc->sc_misc_irq, INTR_TYPE_MISC,
119187517Sgonzo	    apb_intr, NULL, sc, &sc->sc_misc_ih))) {
120187517Sgonzo		device_printf(dev,
121187517Sgonzo		    "WARNING: unable to register interrupt handler\n");
122187517Sgonzo		return (ENXIO);
123187517Sgonzo	}
124187517Sgonzo
125187517Sgonzo	bus_generic_probe(dev);
126187517Sgonzo	bus_enumerate_hinted_children(dev);
127187517Sgonzo	bus_generic_attach(dev);
128187517Sgonzo
129187517Sgonzo	return (0);
130187517Sgonzo}
131187517Sgonzo
132187517Sgonzostatic struct resource *
133187517Sgonzoapb_alloc_resource(device_t bus, device_t child, int type, int *rid,
134187517Sgonzo    u_long start, u_long end, u_long count, u_int flags)
135187517Sgonzo{
136187517Sgonzo	struct apb_softc		*sc = device_get_softc(bus);
137187517Sgonzo	struct apb_ivar			*ivar = device_get_ivars(child);
138187517Sgonzo	struct resource			*rv;
139187517Sgonzo	struct resource_list_entry	*rle;
140187517Sgonzo	struct rman			*rm;
141187517Sgonzo	int				 isdefault, needactivate, passthrough;
142187517Sgonzo
143187517Sgonzo	isdefault = (start == 0UL && end == ~0UL);
144187517Sgonzo	needactivate = flags & RF_ACTIVE;
145187517Sgonzo	passthrough = (device_get_parent(child) != bus);
146187517Sgonzo	rle = NULL;
147187517Sgonzo
148187517Sgonzo	if (passthrough)
149187517Sgonzo		return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type,
150187517Sgonzo		    rid, start, end, count, flags));
151187517Sgonzo
152187517Sgonzo	/*
153187517Sgonzo	 * If this is an allocation of the "default" range for a given RID,
154187517Sgonzo	 * and we know what the resources for this device are (ie. they aren't
155187517Sgonzo	 * maintained by a child bus), then work out the start/end values.
156187517Sgonzo	 */
157187517Sgonzo
158187517Sgonzo	if (isdefault) {
159187517Sgonzo		rle = resource_list_find(&ivar->resources, type, *rid);
160187517Sgonzo		if (rle == NULL) {
161187517Sgonzo			return (NULL);
162187517Sgonzo		}
163187517Sgonzo
164187517Sgonzo		if (rle->res != NULL) {
165187517Sgonzo			panic("%s: resource entry is busy", __func__);
166187517Sgonzo		}
167187517Sgonzo		start = rle->start;
168187517Sgonzo		end = rle->end;
169187517Sgonzo		count = rle->count;
170187517Sgonzo	}
171187517Sgonzo
172187517Sgonzo	switch (type) {
173187517Sgonzo	case SYS_RES_IRQ:
174187517Sgonzo		rm = &sc->apb_irq_rman;
175187517Sgonzo		break;
176187517Sgonzo	case SYS_RES_MEMORY:
177187517Sgonzo		rm = &sc->apb_mem_rman;
178187517Sgonzo		break;
179187517Sgonzo	default:
180187517Sgonzo		printf("%s: unknown resource type %d\n", __func__, type);
181187517Sgonzo		return (0);
182187517Sgonzo	}
183187517Sgonzo
184187517Sgonzo	rv = rman_reserve_resource(rm, start, end, count, flags, child);
185187517Sgonzo	if (rv == 0) {
186187517Sgonzo		printf("%s: could not reserve resource\n", __func__);
187187517Sgonzo		return (0);
188187517Sgonzo	}
189187517Sgonzo
190187517Sgonzo	rman_set_rid(rv, *rid);
191187517Sgonzo
192187517Sgonzo	if (needactivate) {
193187517Sgonzo		if (bus_activate_resource(child, type, *rid, rv)) {
194187517Sgonzo			printf("%s: could not activate resource\n", __func__);
195187517Sgonzo			rman_release_resource(rv);
196187517Sgonzo			return (0);
197187517Sgonzo		}
198187517Sgonzo	}
199187517Sgonzo
200187517Sgonzo	return (rv);
201187517Sgonzo}
202187517Sgonzo
203187517Sgonzostatic int
204187517Sgonzoapb_activate_resource(device_t bus, device_t child, int type, int rid,
205187517Sgonzo    struct resource *r)
206187517Sgonzo{
207187517Sgonzo
208187517Sgonzo	/* XXX: should we mask/unmask IRQ here? */
209187517Sgonzo	return (BUS_ACTIVATE_RESOURCE(device_get_parent(bus), child,
210187517Sgonzo		type, rid, r));
211187517Sgonzo}
212187517Sgonzo
213187517Sgonzostatic int
214187517Sgonzoapb_deactivate_resource(device_t bus, device_t child, int type, int rid,
215187517Sgonzo    struct resource *r)
216187517Sgonzo{
217187517Sgonzo
218187517Sgonzo	/* XXX: should we mask/unmask IRQ here? */
219187517Sgonzo	return (BUS_DEACTIVATE_RESOURCE(device_get_parent(bus), child,
220187517Sgonzo		type, rid, r));
221187517Sgonzo}
222187517Sgonzo
223187517Sgonzostatic int
224187517Sgonzoapb_release_resource(device_t dev, device_t child, int type,
225187517Sgonzo    int rid, struct resource *r)
226187517Sgonzo{
227187517Sgonzo	struct resource_list *rl;
228187517Sgonzo	struct resource_list_entry *rle;
229187517Sgonzo
230187517Sgonzo	rl = apb_get_resource_list(dev, child);
231187517Sgonzo	if (rl == NULL)
232187517Sgonzo		return (EINVAL);
233187517Sgonzo	rle = resource_list_find(rl, type, rid);
234187517Sgonzo	if (rle == NULL)
235187517Sgonzo		return (EINVAL);
236187517Sgonzo	rman_release_resource(r);
237187517Sgonzo	rle->res = NULL;
238187517Sgonzo
239187517Sgonzo	return (0);
240187517Sgonzo}
241187517Sgonzo
242187517Sgonzostatic int
243187517Sgonzoapb_setup_intr(device_t bus, device_t child, struct resource *ires,
244187517Sgonzo		int flags, driver_filter_t *filt, driver_intr_t *handler,
245187517Sgonzo		void *arg, void **cookiep)
246187517Sgonzo{
247187517Sgonzo	struct apb_softc *sc = device_get_softc(bus);
248187517Sgonzo	struct intr_event *event;
249187517Sgonzo	int irq, error;
250187517Sgonzo
251187517Sgonzo	irq = rman_get_start(ires);
252187517Sgonzo
253187517Sgonzo	if (irq > APB_IRQ_END)
254187517Sgonzo		panic("%s: bad irq %d", __func__, irq);
255187517Sgonzo
256187517Sgonzo	event = sc->sc_eventstab[irq];
257187517Sgonzo	if (event == NULL) {
258187517Sgonzo		error = intr_event_create(&event, (void *)irq, 0, irq,
259187517Sgonzo		    (mask_fn)apb_mask_irq, (mask_fn)apb_unmask_irq,
260187517Sgonzo		    NULL, NULL,
261187517Sgonzo		    "apb intr%d:", irq);
262187517Sgonzo
263187517Sgonzo		sc->sc_eventstab[irq] = event;
264187517Sgonzo	}
265187517Sgonzo
266187517Sgonzo	intr_event_add_handler(event, device_get_nameunit(child), filt,
267187517Sgonzo	    handler, arg, intr_priority(flags), flags, cookiep);
268187517Sgonzo
269187517Sgonzo	return (0);
270187517Sgonzo}
271187517Sgonzo
272187517Sgonzostatic int
273187517Sgonzoapb_teardown_intr(device_t dev, device_t child, struct resource *ires,
274187517Sgonzo    void *cookie)
275187517Sgonzo{
276187517Sgonzo	struct apb_softc *sc = device_get_softc(dev);
277187517Sgonzo	int irq, result;
278187517Sgonzo
279187517Sgonzo	irq = rman_get_start(ires);
280187517Sgonzo	if (irq > APB_IRQ_END)
281187517Sgonzo		panic("%s: bad irq %d", __func__, irq);
282187517Sgonzo
283187517Sgonzo	if (sc->sc_eventstab[irq] == NULL)
284187517Sgonzo		panic("Trying to teardown unoccupied IRQ");
285187517Sgonzo
286187517Sgonzo	apb_mask_irq(irq);
287187517Sgonzo
288187517Sgonzo	result = intr_event_remove_handler(cookie);
289187517Sgonzo	if (!result)
290187517Sgonzo		sc->sc_eventstab[irq] = NULL;
291187517Sgonzo
292187517Sgonzo	return (result);
293187517Sgonzo}
294187517Sgonzo
295187517Sgonzostatic int
296187517Sgonzoapb_intr(void *arg)
297187517Sgonzo{
298187517Sgonzo	struct apb_softc *sc = arg;
299187517Sgonzo	struct intr_event *event;
300187517Sgonzo	uint32_t reg, irq;
301187517Sgonzo
302187517Sgonzo	reg = ATH_READ_REG(AR71XX_MISC_INTR_STATUS);
303187517Sgonzo	for (irq = 0; irq < APB_NIRQS; irq++) {
304187517Sgonzo		if (reg & (1 << irq)) {
305187517Sgonzo			event = sc->sc_eventstab[irq];
306187517Sgonzo			if (!event || TAILQ_EMPTY(&event->ie_handlers)) {
307187517Sgonzo				printf("Stray IRQ %d\n", irq);
308187517Sgonzo				continue;
309187517Sgonzo			}
310187517Sgonzo
311187517Sgonzo			/* TODO: frame instead of NULL? */
312187517Sgonzo			intr_event_handle(event, NULL);
313187517Sgonzo		}
314187517Sgonzo	}
315187517Sgonzo
316187517Sgonzo	return (FILTER_HANDLED);
317187517Sgonzo}
318187517Sgonzo
319187517Sgonzostatic void
320187517Sgonzoapb_hinted_child(device_t bus, const char *dname, int dunit)
321187517Sgonzo{
322187517Sgonzo	device_t		child;
323187517Sgonzo	long			maddr;
324187517Sgonzo	int			msize;
325187517Sgonzo	int			irq;
326187517Sgonzo	int			result;
327187517Sgonzo	int			mem_hints_count;
328187517Sgonzo
329187517Sgonzo	child = BUS_ADD_CHILD(bus, 0, dname, dunit);
330187517Sgonzo
331187517Sgonzo	/*
332187517Sgonzo	 * Set hard-wired resources for hinted child using
333187517Sgonzo	 * specific RIDs.
334187517Sgonzo	 */
335187517Sgonzo	mem_hints_count = 0;
336187517Sgonzo	if (resource_long_value(dname, dunit, "maddr", &maddr) == 0)
337187517Sgonzo		mem_hints_count++;
338187517Sgonzo	if (resource_int_value(dname, dunit, "msize", &msize) == 0)
339187517Sgonzo		mem_hints_count++;
340187517Sgonzo
341187517Sgonzo	/* check if all info for mem resource has been provided */
342187517Sgonzo	if ((mem_hints_count > 0) && (mem_hints_count < 2)) {
343187517Sgonzo		printf("Either maddr or msize hint is missing for %s%d\n",
344187517Sgonzo		    dname, dunit);
345187517Sgonzo	} else if (mem_hints_count) {
346187517Sgonzo		result = bus_set_resource(child, SYS_RES_MEMORY, 0,
347187517Sgonzo		    maddr, msize);
348187517Sgonzo		if (result != 0)
349187517Sgonzo			device_printf(bus,
350187517Sgonzo			    "warning: bus_set_resource() failed\n");
351187517Sgonzo	}
352187517Sgonzo
353187517Sgonzo	if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
354187517Sgonzo		result = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
355187517Sgonzo		if (result != 0)
356187517Sgonzo			device_printf(bus,
357187517Sgonzo			    "warning: bus_set_resource() failed\n");
358187517Sgonzo	}
359187517Sgonzo}
360187517Sgonzo
361187517Sgonzostatic device_t
362187517Sgonzoapb_add_child(device_t bus, int order, const char *name, int unit)
363187517Sgonzo{
364187517Sgonzo	device_t		child;
365187517Sgonzo	struct apb_ivar	*ivar;
366187517Sgonzo
367187517Sgonzo	ivar = malloc(sizeof(struct apb_ivar), M_DEVBUF, M_WAITOK | M_ZERO);
368187517Sgonzo	if (ivar == NULL) {
369187517Sgonzo		printf("Failed to allocate ivar\n");
370187517Sgonzo		return (0);
371187517Sgonzo	}
372187517Sgonzo	resource_list_init(&ivar->resources);
373187517Sgonzo
374187517Sgonzo	child = device_add_child_ordered(bus, order, name, unit);
375187517Sgonzo	if (child == NULL) {
376187517Sgonzo		printf("Can't add child %s%d ordered\n", name, unit);
377187517Sgonzo		return (0);
378187517Sgonzo	}
379187517Sgonzo
380187517Sgonzo	device_set_ivars(child, ivar);
381187517Sgonzo
382187517Sgonzo	return (child);
383187517Sgonzo}
384187517Sgonzo
385187517Sgonzo/*
386187517Sgonzo * Helper routine for bus_generic_rl_get_resource/bus_generic_rl_set_resource
387187517Sgonzo * Provides pointer to resource_list for these routines
388187517Sgonzo */
389187517Sgonzostatic struct resource_list *
390187517Sgonzoapb_get_resource_list(device_t dev, device_t child)
391187517Sgonzo{
392187517Sgonzo	struct apb_ivar *ivar;
393187517Sgonzo
394187517Sgonzo	ivar = device_get_ivars(child);
395187517Sgonzo	return (&(ivar->resources));
396187517Sgonzo}
397187517Sgonzo
398187517Sgonzostatic device_method_t apb_methods[] = {
399187517Sgonzo	DEVMETHOD(bus_activate_resource,	apb_activate_resource),
400187517Sgonzo	DEVMETHOD(bus_add_child,		apb_add_child),
401187517Sgonzo	DEVMETHOD(bus_alloc_resource,		apb_alloc_resource),
402187517Sgonzo	DEVMETHOD(bus_deactivate_resource,	apb_deactivate_resource),
403187517Sgonzo	DEVMETHOD(bus_get_resource_list,	apb_get_resource_list),
404187517Sgonzo	DEVMETHOD(bus_hinted_child,		apb_hinted_child),
405187517Sgonzo	DEVMETHOD(bus_print_child,		bus_generic_print_child),
406187517Sgonzo	DEVMETHOD(bus_release_resource,		apb_release_resource),
407187517Sgonzo	DEVMETHOD(bus_setup_intr,		apb_setup_intr),
408187517Sgonzo	DEVMETHOD(bus_teardown_intr,		apb_teardown_intr),
409187517Sgonzo	DEVMETHOD(device_attach,		apb_attach),
410187517Sgonzo	DEVMETHOD(device_probe,			apb_probe),
411187517Sgonzo	DEVMETHOD(bus_get_resource,		bus_generic_rl_get_resource),
412187517Sgonzo	DEVMETHOD(bus_set_resource,		bus_generic_rl_set_resource),
413187517Sgonzo
414187517Sgonzo	{0, 0},
415187517Sgonzo};
416187517Sgonzo
417187517Sgonzostatic driver_t apb_driver = {
418187517Sgonzo	"apb",
419187517Sgonzo	apb_methods,
420187517Sgonzo	sizeof(struct apb_softc),
421187517Sgonzo};
422187517Sgonzostatic devclass_t apb_devclass;
423187517Sgonzo
424187517SgonzoDRIVER_MODULE(apb, nexus, apb_driver, apb_devclass, 0, 0);
425