apb.c revision 192822
1/*-
2 * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice unmodified, this list of conditions, and the following
10 *    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.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD$");
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/bus.h>
34#include <sys/interrupt.h>
35#include <sys/kernel.h>
36#include <sys/module.h>
37#include <sys/rman.h>
38#include <sys/malloc.h>
39
40#include <machine/bus.h>
41
42#include <mips/atheros/apbvar.h>
43#include <mips/atheros/ar71xxreg.h>
44
45#undef APB_DEBUG
46#ifdef APB_DEBUG
47#define dprintf printf
48#else
49#define dprintf(x, arg...)
50#endif  /* APB_DEBUG */
51
52static int	apb_activate_resource(device_t, device_t, int, int,
53		    struct resource *);
54static device_t	apb_add_child(device_t, int, const char *, int);
55static struct resource *
56		apb_alloc_resource(device_t, device_t, int, int *, u_long,
57		    u_long, u_long, u_int);
58static int	apb_attach(device_t);
59static int	apb_deactivate_resource(device_t, device_t, int, int,
60		    struct resource *);
61static struct resource_list *
62		apb_get_resource_list(device_t, device_t);
63static void	apb_hinted_child(device_t, const char *, int);
64static int	apb_intr(void *);
65static int	apb_probe(device_t);
66static int	apb_release_resource(device_t, device_t, int, int,
67		    struct resource *);
68static int	apb_setup_intr(device_t, device_t, struct resource *, int,
69		    driver_filter_t *, driver_intr_t *, void *, void **);
70static int	apb_teardown_intr(device_t, device_t, struct resource *,
71		    void *);
72
73static void
74apb_mask_irq(void *source)
75{
76	unsigned int irq = (unsigned int)source;
77	uint32_t reg;
78
79	reg = ATH_READ_REG(AR71XX_MISC_INTR_MASK);
80	ATH_WRITE_REG(AR71XX_MISC_INTR_MASK, reg & ~(1 << irq));
81
82}
83
84static void
85apb_unmask_irq(void *source)
86{
87	uint32_t reg;
88	unsigned int irq = (unsigned int)source;
89
90	reg = ATH_READ_REG(AR71XX_MISC_INTR_MASK);
91	ATH_WRITE_REG(AR71XX_MISC_INTR_MASK, reg | (1 << irq));
92}
93
94static int
95apb_probe(device_t dev)
96{
97
98	return (0);
99}
100
101static int
102apb_attach(device_t dev)
103{
104	struct apb_softc *sc = device_get_softc(dev);
105	int rid = 0;
106
107	device_set_desc(dev, "APB Bus bridge");
108
109	sc->apb_mem_rman.rm_type = RMAN_ARRAY;
110	sc->apb_mem_rman.rm_descr = "APB memory window";
111
112	if (rman_init(&sc->apb_mem_rman) != 0 ||
113	    rman_manage_region(&sc->apb_mem_rman,
114			AR71XX_APB_BASE,
115			AR71XX_APB_BASE + AR71XX_APB_SIZE - 1) != 0)
116		panic("apb_attach: failed to set up memory rman");
117
118	sc->apb_irq_rman.rm_type = RMAN_ARRAY;
119	sc->apb_irq_rman.rm_descr = "APB IRQ";
120
121	if (rman_init(&sc->apb_irq_rman) != 0 ||
122	    rman_manage_region(&sc->apb_irq_rman,
123			APB_IRQ_BASE, APB_IRQ_END) != 0)
124		panic("apb_attach: failed to set up IRQ rman");
125
126	if ((sc->sc_misc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
127	    RF_SHAREABLE | RF_ACTIVE)) == NULL) {
128		device_printf(dev, "unable to allocate IRQ resource\n");
129		return (ENXIO);
130	}
131
132	if ((bus_setup_intr(dev, sc->sc_misc_irq, INTR_TYPE_MISC,
133	    apb_intr, NULL, sc, &sc->sc_misc_ih))) {
134		device_printf(dev,
135		    "WARNING: unable to register interrupt handler\n");
136		return (ENXIO);
137	}
138
139	bus_generic_probe(dev);
140	bus_enumerate_hinted_children(dev);
141	bus_generic_attach(dev);
142
143	return (0);
144}
145
146static struct resource *
147apb_alloc_resource(device_t bus, device_t child, int type, int *rid,
148    u_long start, u_long end, u_long count, u_int flags)
149{
150	struct apb_softc		*sc = device_get_softc(bus);
151	struct apb_ivar			*ivar = device_get_ivars(child);
152	struct resource			*rv;
153	struct resource_list_entry	*rle;
154	struct rman			*rm;
155	int				 isdefault, needactivate, passthrough;
156
157	isdefault = (start == 0UL && end == ~0UL);
158	needactivate = flags & RF_ACTIVE;
159	/*
160	 * Pass memory requests to nexus device
161	 */
162	passthrough = (device_get_parent(child) != bus);
163	rle = NULL;
164
165	dprintf("%s: entry (%p, %p, %d, %d, %p, %p, %ld, %d)\n",
166	    __func__, bus, child, type, *rid, (void *)(intptr_t)start,
167	    (void *)(intptr_t)end, count, flags);
168
169	if (passthrough)
170		return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type,
171		    rid, start, end, count, flags));
172
173	/*
174	 * If this is an allocation of the "default" range for a given RID,
175	 * and we know what the resources for this device are (ie. they aren't
176	 * maintained by a child bus), then work out the start/end values.
177	 */
178
179	if (isdefault) {
180		rle = resource_list_find(&ivar->resources, type, *rid);
181		if (rle == NULL) {
182			return (NULL);
183		}
184
185		if (rle->res != NULL) {
186			panic("%s: resource entry is busy", __func__);
187		}
188		start = rle->start;
189		end = rle->end;
190		count = rle->count;
191
192		dprintf("%s: default resource (%p, %p, %ld)\n",
193		    __func__, (void *)(intptr_t)start,
194		    (void *)(intptr_t)end, count);
195	}
196
197	switch (type) {
198	case SYS_RES_IRQ:
199		rm = &sc->apb_irq_rman;
200		break;
201	case SYS_RES_MEMORY:
202		rm = &sc->apb_mem_rman;
203		break;
204	default:
205		printf("%s: unknown resource type %d\n", __func__, type);
206		return (0);
207	}
208
209	rv = rman_reserve_resource(rm, start, end, count, flags, child);
210	if (rv == 0) {
211		printf("%s: could not reserve resource\n", __func__);
212		return (0);
213	}
214
215	rman_set_rid(rv, *rid);
216
217	if (needactivate) {
218		if (bus_activate_resource(child, type, *rid, rv)) {
219			printf("%s: could not activate resource\n", __func__);
220			rman_release_resource(rv);
221			return (0);
222		}
223	}
224
225	return (rv);
226}
227
228static int
229apb_activate_resource(device_t bus, device_t child, int type, int rid,
230    struct resource *r)
231{
232
233	/* XXX: should we mask/unmask IRQ here? */
234	return (BUS_ACTIVATE_RESOURCE(device_get_parent(bus), child,
235		type, rid, r));
236}
237
238static int
239apb_deactivate_resource(device_t bus, device_t child, int type, int rid,
240    struct resource *r)
241{
242
243	/* XXX: should we mask/unmask IRQ here? */
244	return (BUS_DEACTIVATE_RESOURCE(device_get_parent(bus), child,
245		type, rid, r));
246}
247
248static int
249apb_release_resource(device_t dev, device_t child, int type,
250    int rid, struct resource *r)
251{
252	struct resource_list *rl;
253	struct resource_list_entry *rle;
254
255	rl = apb_get_resource_list(dev, child);
256	if (rl == NULL)
257		return (EINVAL);
258	rle = resource_list_find(rl, type, rid);
259	if (rle == NULL)
260		return (EINVAL);
261	rman_release_resource(r);
262	rle->res = NULL;
263
264	return (0);
265}
266
267static int
268apb_setup_intr(device_t bus, device_t child, struct resource *ires,
269		int flags, driver_filter_t *filt, driver_intr_t *handler,
270		void *arg, void **cookiep)
271{
272	struct apb_softc *sc = device_get_softc(bus);
273	struct intr_event *event;
274	int irq, error;
275
276	irq = rman_get_start(ires);
277
278	if (irq > APB_IRQ_END)
279		panic("%s: bad irq %d", __func__, irq);
280
281	event = sc->sc_eventstab[irq];
282	if (event == NULL) {
283		error = intr_event_create(&event, (void *)irq, 0, irq,
284		    apb_mask_irq, apb_unmask_irq,
285		    NULL, NULL,
286		    "apb intr%d:", irq);
287
288		sc->sc_eventstab[irq] = event;
289	}
290
291	intr_event_add_handler(event, device_get_nameunit(child), filt,
292	    handler, arg, intr_priority(flags), flags, cookiep);
293
294	apb_unmask_irq((void*)irq);
295
296	return (0);
297}
298
299static int
300apb_teardown_intr(device_t dev, device_t child, struct resource *ires,
301    void *cookie)
302{
303	struct apb_softc *sc = device_get_softc(dev);
304	int irq, result;
305
306	irq = rman_get_start(ires);
307	if (irq > APB_IRQ_END)
308		panic("%s: bad irq %d", __func__, irq);
309
310	if (sc->sc_eventstab[irq] == NULL)
311		panic("Trying to teardown unoccupied IRQ");
312
313	apb_mask_irq((void*)irq);
314
315	result = intr_event_remove_handler(cookie);
316	if (!result)
317		sc->sc_eventstab[irq] = NULL;
318
319	return (result);
320}
321
322static int
323apb_intr(void *arg)
324{
325	struct apb_softc *sc = arg;
326	struct intr_event *event;
327	uint32_t reg, irq;
328
329	reg = ATH_READ_REG(AR71XX_MISC_INTR_STATUS);
330	for (irq = 0; irq < APB_NIRQS; irq++) {
331		if (reg & (1 << irq)) {
332			event = sc->sc_eventstab[irq];
333			if (!event || TAILQ_EMPTY(&event->ie_handlers)) {
334				/* Ignore timer interrupts */
335				if (irq != 0)
336					printf("Stray IRQ %d\n", irq);
337				continue;
338			}
339
340			/* TODO: frame instead of NULL? */
341			intr_event_handle(event, NULL);
342		}
343	}
344
345	return (FILTER_HANDLED);
346}
347
348static void
349apb_hinted_child(device_t bus, const char *dname, int dunit)
350{
351	device_t		child;
352	long			maddr;
353	int			msize;
354	int			irq;
355	int			result;
356	int			mem_hints_count;
357
358	child = BUS_ADD_CHILD(bus, 0, dname, dunit);
359
360	/*
361	 * Set hard-wired resources for hinted child using
362	 * specific RIDs.
363	 */
364	mem_hints_count = 0;
365	if (resource_long_value(dname, dunit, "maddr", &maddr) == 0)
366		mem_hints_count++;
367	if (resource_int_value(dname, dunit, "msize", &msize) == 0)
368		mem_hints_count++;
369
370	/* check if all info for mem resource has been provided */
371	if ((mem_hints_count > 0) && (mem_hints_count < 2)) {
372		printf("Either maddr or msize hint is missing for %s%d\n",
373		    dname, dunit);
374	} else if (mem_hints_count) {
375		result = bus_set_resource(child, SYS_RES_MEMORY, 0,
376		    maddr, msize);
377		if (result != 0)
378			device_printf(bus,
379			    "warning: bus_set_resource() failed\n");
380	}
381
382	if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
383		result = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
384		if (result != 0)
385			device_printf(bus,
386			    "warning: bus_set_resource() failed\n");
387	}
388}
389
390static device_t
391apb_add_child(device_t bus, int order, const char *name, int unit)
392{
393	device_t		child;
394	struct apb_ivar	*ivar;
395
396	ivar = malloc(sizeof(struct apb_ivar), M_DEVBUF, M_WAITOK | M_ZERO);
397	if (ivar == NULL) {
398		printf("Failed to allocate ivar\n");
399		return (0);
400	}
401	resource_list_init(&ivar->resources);
402
403	child = device_add_child_ordered(bus, order, name, unit);
404	if (child == NULL) {
405		printf("Can't add child %s%d ordered\n", name, unit);
406		return (0);
407	}
408
409	device_set_ivars(child, ivar);
410
411	return (child);
412}
413
414/*
415 * Helper routine for bus_generic_rl_get_resource/bus_generic_rl_set_resource
416 * Provides pointer to resource_list for these routines
417 */
418static struct resource_list *
419apb_get_resource_list(device_t dev, device_t child)
420{
421	struct apb_ivar *ivar;
422
423	ivar = device_get_ivars(child);
424	return (&(ivar->resources));
425}
426
427static device_method_t apb_methods[] = {
428	DEVMETHOD(bus_activate_resource,	apb_activate_resource),
429	DEVMETHOD(bus_add_child,		apb_add_child),
430	DEVMETHOD(bus_alloc_resource,		apb_alloc_resource),
431	DEVMETHOD(bus_deactivate_resource,	apb_deactivate_resource),
432	DEVMETHOD(bus_get_resource_list,	apb_get_resource_list),
433	DEVMETHOD(bus_hinted_child,		apb_hinted_child),
434	DEVMETHOD(bus_print_child,		bus_generic_print_child),
435	DEVMETHOD(bus_release_resource,		apb_release_resource),
436	DEVMETHOD(bus_setup_intr,		apb_setup_intr),
437	DEVMETHOD(bus_teardown_intr,		apb_teardown_intr),
438	DEVMETHOD(device_attach,		apb_attach),
439	DEVMETHOD(device_probe,			apb_probe),
440	DEVMETHOD(bus_get_resource,		bus_generic_rl_get_resource),
441	DEVMETHOD(bus_set_resource,		bus_generic_rl_set_resource),
442
443	{0, 0},
444};
445
446static driver_t apb_driver = {
447	"apb",
448	apb_methods,
449	sizeof(struct apb_softc),
450};
451static devclass_t apb_devclass;
452
453DRIVER_MODULE(apb, nexus, apb_driver, apb_devclass, 0, 0);
454