apb.c revision 227843
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: head/sys/mips/atheros/apb.c 227843 2011-11-22 21:28:20Z marius $");
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#include <machine/intr_machdep.h>
42
43#include <mips/atheros/apbvar.h>
44#include <mips/atheros/ar71xxreg.h>
45#include <mips/atheros/ar71xx_setup.h>
46
47#undef APB_DEBUG
48#ifdef APB_DEBUG
49#define dprintf printf
50#else
51#define dprintf(x, arg...)
52#endif  /* APB_DEBUG */
53
54static int	apb_activate_resource(device_t, device_t, int, int,
55		    struct resource *);
56static device_t	apb_add_child(device_t, u_int, const char *, int);
57static struct resource *
58		apb_alloc_resource(device_t, device_t, int, int *, u_long,
59		    u_long, u_long, u_int);
60static int	apb_attach(device_t);
61static int	apb_deactivate_resource(device_t, device_t, int, int,
62		    struct resource *);
63static struct resource_list *
64		apb_get_resource_list(device_t, device_t);
65static void	apb_hinted_child(device_t, const char *, int);
66static int	apb_intr(void *);
67static int	apb_probe(device_t);
68static int	apb_release_resource(device_t, device_t, int, int,
69		    struct resource *);
70static int	apb_setup_intr(device_t, device_t, struct resource *, int,
71		    driver_filter_t *, driver_intr_t *, void *, void **);
72static int	apb_teardown_intr(device_t, device_t, struct resource *,
73		    void *);
74
75static void
76apb_mask_irq(void *source)
77{
78	unsigned int irq = (unsigned int)source;
79	uint32_t reg;
80
81	reg = ATH_READ_REG(AR71XX_MISC_INTR_MASK);
82	ATH_WRITE_REG(AR71XX_MISC_INTR_MASK, reg & ~(1 << irq));
83
84}
85
86static void
87apb_unmask_irq(void *source)
88{
89	uint32_t reg;
90	unsigned int irq = (unsigned int)source;
91
92	reg = ATH_READ_REG(AR71XX_MISC_INTR_MASK);
93	ATH_WRITE_REG(AR71XX_MISC_INTR_MASK, reg | (1 << irq));
94}
95
96static int
97apb_probe(device_t dev)
98{
99
100	return (0);
101}
102
103static int
104apb_attach(device_t dev)
105{
106	struct apb_softc *sc = device_get_softc(dev);
107	int rid = 0;
108
109	device_set_desc(dev, "APB Bus bridge");
110
111	sc->apb_mem_rman.rm_type = RMAN_ARRAY;
112	sc->apb_mem_rman.rm_descr = "APB memory window";
113
114	if (rman_init(&sc->apb_mem_rman) != 0 ||
115	    rman_manage_region(&sc->apb_mem_rman,
116			AR71XX_APB_BASE,
117			AR71XX_APB_BASE + AR71XX_APB_SIZE - 1) != 0)
118		panic("apb_attach: failed to set up memory rman");
119
120	sc->apb_irq_rman.rm_type = RMAN_ARRAY;
121	sc->apb_irq_rman.rm_descr = "APB IRQ";
122
123	if (rman_init(&sc->apb_irq_rman) != 0 ||
124	    rman_manage_region(&sc->apb_irq_rman,
125			APB_IRQ_BASE, APB_IRQ_END) != 0)
126		panic("apb_attach: failed to set up IRQ rman");
127
128	if ((sc->sc_misc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
129	    RF_SHAREABLE | RF_ACTIVE)) == NULL) {
130		device_printf(dev, "unable to allocate IRQ resource\n");
131		return (ENXIO);
132	}
133
134	if ((bus_setup_intr(dev, sc->sc_misc_irq, INTR_TYPE_MISC,
135	    apb_intr, NULL, sc, &sc->sc_misc_ih))) {
136		device_printf(dev,
137		    "WARNING: unable to register interrupt handler\n");
138		return (ENXIO);
139	}
140
141	bus_generic_probe(dev);
142	bus_enumerate_hinted_children(dev);
143	bus_generic_attach(dev);
144
145	return (0);
146}
147
148static struct resource *
149apb_alloc_resource(device_t bus, device_t child, int type, int *rid,
150    u_long start, u_long end, u_long count, u_int flags)
151{
152	struct apb_softc		*sc = device_get_softc(bus);
153	struct apb_ivar			*ivar = device_get_ivars(child);
154	struct resource			*rv;
155	struct resource_list_entry	*rle;
156	struct rman			*rm;
157	int				 isdefault, needactivate, passthrough;
158
159	isdefault = (start == 0UL && end == ~0UL);
160	needactivate = flags & RF_ACTIVE;
161	/*
162	 * Pass memory requests to nexus device
163	 */
164	passthrough = (device_get_parent(child) != bus);
165	rle = NULL;
166
167	dprintf("%s: entry (%p, %p, %d, %d, %p, %p, %ld, %d)\n",
168	    __func__, bus, child, type, *rid, (void *)(intptr_t)start,
169	    (void *)(intptr_t)end, count, flags);
170
171	if (passthrough)
172		return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type,
173		    rid, start, end, count, flags));
174
175	/*
176	 * If this is an allocation of the "default" range for a given RID,
177	 * and we know what the resources for this device are (ie. they aren't
178	 * maintained by a child bus), then work out the start/end values.
179	 */
180
181	if (isdefault) {
182		rle = resource_list_find(&ivar->resources, type, *rid);
183		if (rle == NULL) {
184			return (NULL);
185		}
186
187		if (rle->res != NULL) {
188			panic("%s: resource entry is busy", __func__);
189		}
190		start = rle->start;
191		end = rle->end;
192		count = rle->count;
193
194		dprintf("%s: default resource (%p, %p, %ld)\n",
195		    __func__, (void *)(intptr_t)start,
196		    (void *)(intptr_t)end, count);
197	}
198
199	switch (type) {
200	case SYS_RES_IRQ:
201		rm = &sc->apb_irq_rman;
202		break;
203	case SYS_RES_MEMORY:
204		rm = &sc->apb_mem_rman;
205		break;
206	default:
207		printf("%s: unknown resource type %d\n", __func__, type);
208		return (0);
209	}
210
211	rv = rman_reserve_resource(rm, start, end, count, flags, child);
212	if (rv == 0) {
213		printf("%s: could not reserve resource\n", __func__);
214		return (0);
215	}
216
217	rman_set_rid(rv, *rid);
218
219	if (needactivate) {
220		if (bus_activate_resource(child, type, *rid, rv)) {
221			printf("%s: could not activate resource\n", __func__);
222			rman_release_resource(rv);
223			return (0);
224		}
225	}
226
227	return (rv);
228}
229
230static int
231apb_activate_resource(device_t bus, device_t child, int type, int rid,
232    struct resource *r)
233{
234
235	/* XXX: should we mask/unmask IRQ here? */
236	return (BUS_ACTIVATE_RESOURCE(device_get_parent(bus), child,
237		type, rid, r));
238}
239
240static int
241apb_deactivate_resource(device_t bus, device_t child, int type, int rid,
242    struct resource *r)
243{
244
245	/* XXX: should we mask/unmask IRQ here? */
246	return (BUS_DEACTIVATE_RESOURCE(device_get_parent(bus), child,
247		type, rid, r));
248}
249
250static int
251apb_release_resource(device_t dev, device_t child, int type,
252    int rid, struct resource *r)
253{
254	struct resource_list *rl;
255	struct resource_list_entry *rle;
256
257	rl = apb_get_resource_list(dev, child);
258	if (rl == NULL)
259		return (EINVAL);
260	rle = resource_list_find(rl, type, rid);
261	if (rle == NULL)
262		return (EINVAL);
263	rman_release_resource(r);
264	rle->res = NULL;
265
266	return (0);
267}
268
269static int
270apb_setup_intr(device_t bus, device_t child, struct resource *ires,
271		int flags, driver_filter_t *filt, driver_intr_t *handler,
272		void *arg, void **cookiep)
273{
274	struct apb_softc *sc = device_get_softc(bus);
275	struct intr_event *event;
276	int irq, error;
277
278	irq = rman_get_start(ires);
279
280	if (irq > APB_IRQ_END)
281		panic("%s: bad irq %d", __func__, irq);
282
283	event = sc->sc_eventstab[irq];
284	if (event == NULL) {
285		error = intr_event_create(&event, (void *)irq, 0, irq,
286		    apb_mask_irq, apb_unmask_irq,
287		    NULL, NULL,
288		    "apb intr%d:", irq);
289
290		if (error == 0) {
291			sc->sc_eventstab[irq] = event;
292			sc->sc_intr_counter[irq] =
293			    mips_intrcnt_create(event->ie_name);
294		}
295		else
296			return (error);
297	}
298
299	intr_event_add_handler(event, device_get_nameunit(child), filt,
300	    handler, arg, intr_priority(flags), flags, cookiep);
301	mips_intrcnt_setname(sc->sc_intr_counter[irq], event->ie_fullname);
302
303	apb_unmask_irq((void*)irq);
304
305	return (0);
306}
307
308static int
309apb_teardown_intr(device_t dev, device_t child, struct resource *ires,
310    void *cookie)
311{
312	struct apb_softc *sc = device_get_softc(dev);
313	int irq, result;
314
315	irq = rman_get_start(ires);
316	if (irq > APB_IRQ_END)
317		panic("%s: bad irq %d", __func__, irq);
318
319	if (sc->sc_eventstab[irq] == NULL)
320		panic("Trying to teardown unoccupied IRQ");
321
322	apb_mask_irq((void*)irq);
323
324	result = intr_event_remove_handler(cookie);
325	if (!result)
326		sc->sc_eventstab[irq] = NULL;
327
328	return (result);
329}
330
331static int
332apb_intr(void *arg)
333{
334	struct apb_softc *sc = arg;
335	struct intr_event *event;
336	uint32_t reg, irq;
337
338	reg = ATH_READ_REG(AR71XX_MISC_INTR_STATUS);
339	for (irq = 0; irq < APB_NIRQS; irq++) {
340		if (reg & (1 << irq)) {
341
342			switch (ar71xx_soc) {
343			case AR71XX_SOC_AR7240:
344			case AR71XX_SOC_AR7241:
345			case AR71XX_SOC_AR7242:
346				/* Ack/clear the irq on status register for AR724x */
347				ATH_WRITE_REG(AR71XX_MISC_INTR_STATUS,
348				    reg & ~(1 << irq));
349				break;
350			default:
351				/* fallthrough */
352				break;
353			}
354
355			event = sc->sc_eventstab[irq];
356			if (!event || TAILQ_EMPTY(&event->ie_handlers)) {
357				/* Ignore timer interrupts */
358				if (irq != 0)
359					printf("Stray APB IRQ %d\n", irq);
360				continue;
361			}
362
363			/* TODO: frame instead of NULL? */
364			intr_event_handle(event, NULL);
365			mips_intrcnt_inc(sc->sc_intr_counter[irq]);
366		}
367	}
368
369	return (FILTER_HANDLED);
370}
371
372static void
373apb_hinted_child(device_t bus, const char *dname, int dunit)
374{
375	device_t		child;
376	long			maddr;
377	int			msize;
378	int			irq;
379	int			result;
380	int			mem_hints_count;
381
382	child = BUS_ADD_CHILD(bus, 0, dname, dunit);
383
384	/*
385	 * Set hard-wired resources for hinted child using
386	 * specific RIDs.
387	 */
388	mem_hints_count = 0;
389	if (resource_long_value(dname, dunit, "maddr", &maddr) == 0)
390		mem_hints_count++;
391	if (resource_int_value(dname, dunit, "msize", &msize) == 0)
392		mem_hints_count++;
393
394	/* check if all info for mem resource has been provided */
395	if ((mem_hints_count > 0) && (mem_hints_count < 2)) {
396		printf("Either maddr or msize hint is missing for %s%d\n",
397		    dname, dunit);
398	} else if (mem_hints_count) {
399		result = bus_set_resource(child, SYS_RES_MEMORY, 0,
400		    maddr, msize);
401		if (result != 0)
402			device_printf(bus,
403			    "warning: bus_set_resource() failed\n");
404	}
405
406	if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
407		result = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
408		if (result != 0)
409			device_printf(bus,
410			    "warning: bus_set_resource() failed\n");
411	}
412}
413
414static device_t
415apb_add_child(device_t bus, u_int order, const char *name, int unit)
416{
417	device_t		child;
418	struct apb_ivar	*ivar;
419
420	ivar = malloc(sizeof(struct apb_ivar), M_DEVBUF, M_WAITOK | M_ZERO);
421	if (ivar == NULL) {
422		printf("Failed to allocate ivar\n");
423		return (0);
424	}
425	resource_list_init(&ivar->resources);
426
427	child = device_add_child_ordered(bus, order, name, unit);
428	if (child == NULL) {
429		printf("Can't add child %s%d ordered\n", name, unit);
430		return (0);
431	}
432
433	device_set_ivars(child, ivar);
434
435	return (child);
436}
437
438/*
439 * Helper routine for bus_generic_rl_get_resource/bus_generic_rl_set_resource
440 * Provides pointer to resource_list for these routines
441 */
442static struct resource_list *
443apb_get_resource_list(device_t dev, device_t child)
444{
445	struct apb_ivar *ivar;
446
447	ivar = device_get_ivars(child);
448	return (&(ivar->resources));
449}
450
451static device_method_t apb_methods[] = {
452	DEVMETHOD(bus_activate_resource,	apb_activate_resource),
453	DEVMETHOD(bus_add_child,		apb_add_child),
454	DEVMETHOD(bus_alloc_resource,		apb_alloc_resource),
455	DEVMETHOD(bus_deactivate_resource,	apb_deactivate_resource),
456	DEVMETHOD(bus_get_resource_list,	apb_get_resource_list),
457	DEVMETHOD(bus_hinted_child,		apb_hinted_child),
458	DEVMETHOD(bus_release_resource,		apb_release_resource),
459	DEVMETHOD(bus_setup_intr,		apb_setup_intr),
460	DEVMETHOD(bus_teardown_intr,		apb_teardown_intr),
461	DEVMETHOD(device_attach,		apb_attach),
462	DEVMETHOD(device_probe,			apb_probe),
463	DEVMETHOD(bus_get_resource,		bus_generic_rl_get_resource),
464	DEVMETHOD(bus_set_resource,		bus_generic_rl_set_resource),
465
466	DEVMETHOD_END
467};
468
469static driver_t apb_driver = {
470	"apb",
471	apb_methods,
472	sizeof(struct apb_softc),
473};
474static devclass_t apb_devclass;
475
476DRIVER_MODULE(apb, nexus, apb_driver, apb_devclass, 0, 0);
477