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