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