xref: /freebsd/sys/arm/ti/ti_pruss.c (revision 1de7b4b805ddbf2429da511c053686ac4591ed89)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2013 Rui Paulo <rpaulo@FreeBSD.org>
5  * Copyright (c) 2017 Manuel Stuehn
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following 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 ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
21  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/poll.h>
33 #include <sys/time.h>
34 #include <sys/uio.h>
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/fcntl.h>
38 #include <sys/bus.h>
39 #include <sys/conf.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/malloc.h>
43 #include <sys/rman.h>
44 #include <sys/types.h>
45 #include <sys/sysctl.h>
46 #include <sys/event.h>
47 #include <sys/selinfo.h>
48 #include <machine/bus.h>
49 #include <machine/cpu.h>
50 #include <machine/frame.h>
51 #include <machine/intr.h>
52 #include <machine/atomic.h>
53 
54 #include <dev/ofw/openfirm.h>
55 #include <dev/ofw/ofw_bus.h>
56 #include <dev/ofw/ofw_bus_subr.h>
57 
58 #include <arm/ti/ti_prcm.h>
59 #include <arm/ti/ti_pruss.h>
60 
61 #ifdef DEBUG
62 #define	DPRINTF(fmt, ...)	do {	\
63 	printf("%s: ", __func__);	\
64 	printf(fmt, __VA_ARGS__);	\
65 } while (0)
66 #else
67 #define	DPRINTF(fmt, ...)
68 #endif
69 
70 static d_open_t			ti_pruss_irq_open;
71 static d_read_t			ti_pruss_irq_read;
72 static d_poll_t			ti_pruss_irq_poll;
73 
74 static device_probe_t		ti_pruss_probe;
75 static device_attach_t		ti_pruss_attach;
76 static device_detach_t		ti_pruss_detach;
77 static void			ti_pruss_intr(void *);
78 static d_open_t			ti_pruss_open;
79 static d_mmap_t			ti_pruss_mmap;
80 static void 			ti_pruss_irq_kqread_detach(struct knote *);
81 static int 			ti_pruss_irq_kqevent(struct knote *, long);
82 static d_kqfilter_t		ti_pruss_irq_kqfilter;
83 static void			ti_pruss_privdtor(void *data);
84 
85 #define	TI_PRUSS_PRU_IRQS 2
86 #define	TI_PRUSS_HOST_IRQS 8
87 #define	TI_PRUSS_IRQS (TI_PRUSS_HOST_IRQS+TI_PRUSS_PRU_IRQS)
88 #define	TI_PRUSS_EVENTS 64
89 #define	NOT_SET_STR "NONE"
90 #define	TI_TS_ARRAY 16
91 
92 struct ctl
93 {
94 	size_t cnt;
95 	size_t idx;
96 };
97 
98 struct ts_ring_buf
99 {
100 	struct ctl ctl;
101 	uint64_t ts[TI_TS_ARRAY];
102 };
103 
104 struct ti_pruss_irqsc
105 {
106 	struct mtx		sc_mtx;
107 	struct cdev		*sc_pdev;
108 	struct selinfo		sc_selinfo;
109 	int8_t			channel;
110 	int8_t			last;
111 	int8_t			event;
112 	bool			enable;
113 	struct ts_ring_buf	tstamps;
114 };
115 
116 static struct cdevsw ti_pruss_cdevirq = {
117 	.d_version =	D_VERSION,
118 	.d_name =	"ti_pruss_irq",
119 	.d_open =	ti_pruss_irq_open,
120 	.d_read =	ti_pruss_irq_read,
121 	.d_poll =	ti_pruss_irq_poll,
122 	.d_kqfilter =	ti_pruss_irq_kqfilter,
123 };
124 
125 struct ti_pruss_softc {
126 	struct mtx		sc_mtx;
127 	struct resource 	*sc_mem_res;
128 	struct resource 	*sc_irq_res[TI_PRUSS_HOST_IRQS];
129 	void            	*sc_intr[TI_PRUSS_HOST_IRQS];
130 	struct ti_pruss_irqsc	sc_irq_devs[TI_PRUSS_IRQS];
131 	bus_space_tag_t		sc_bt;
132 	bus_space_handle_t	sc_bh;
133 	struct cdev		*sc_pdev;
134 	struct selinfo		sc_selinfo;
135 	bool			sc_glob_irqen;
136 };
137 
138 static struct cdevsw ti_pruss_cdevsw = {
139 	.d_version =	D_VERSION,
140 	.d_name =	"ti_pruss",
141 	.d_open =	ti_pruss_open,
142 	.d_mmap =	ti_pruss_mmap,
143 };
144 
145 static device_method_t ti_pruss_methods[] = {
146 	DEVMETHOD(device_probe,		ti_pruss_probe),
147 	DEVMETHOD(device_attach,	ti_pruss_attach),
148 	DEVMETHOD(device_detach,	ti_pruss_detach),
149 
150 	DEVMETHOD_END
151 };
152 
153 static driver_t ti_pruss_driver = {
154 	"ti_pruss",
155 	ti_pruss_methods,
156 	sizeof(struct ti_pruss_softc)
157 };
158 
159 static devclass_t ti_pruss_devclass;
160 
161 DRIVER_MODULE(ti_pruss, simplebus, ti_pruss_driver, ti_pruss_devclass, 0, 0);
162 MODULE_DEPEND(ti_pruss, ti_prcm, 1, 1, 1);
163 
164 static struct resource_spec ti_pruss_irq_spec[] = {
165 	{ SYS_RES_IRQ,	    0,  RF_ACTIVE },
166 	{ SYS_RES_IRQ,	    1,  RF_ACTIVE },
167 	{ SYS_RES_IRQ,	    2,  RF_ACTIVE },
168 	{ SYS_RES_IRQ,	    3,  RF_ACTIVE },
169 	{ SYS_RES_IRQ,	    4,  RF_ACTIVE },
170 	{ SYS_RES_IRQ,	    5,  RF_ACTIVE },
171 	{ SYS_RES_IRQ,	    6,  RF_ACTIVE },
172 	{ SYS_RES_IRQ,	    7,  RF_ACTIVE },
173 	{ -1,               0,  0 }
174 };
175 CTASSERT(TI_PRUSS_HOST_IRQS == nitems(ti_pruss_irq_spec) - 1);
176 
177 static int
178 ti_pruss_irq_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
179 {
180 	struct ctl* irqs;
181 	struct ti_pruss_irqsc *sc;
182 	sc = dev->si_drv1;
183 
184 	irqs = malloc(sizeof(struct ctl), M_DEVBUF, M_WAITOK);
185 	if (!irqs)
186 	    return (ENOMEM);
187 
188 	irqs->cnt = sc->tstamps.ctl.cnt;
189 	irqs->idx = sc->tstamps.ctl.idx;
190 
191 	return devfs_set_cdevpriv(irqs, ti_pruss_privdtor);
192 }
193 
194 static void
195 ti_pruss_privdtor(void *data)
196 {
197     free(data, M_DEVBUF);
198 }
199 
200 static int
201 ti_pruss_irq_poll(struct cdev *dev, int events, struct thread *td)
202 {
203 	struct ctl* irqs;
204 	struct ti_pruss_irqsc *sc;
205 	sc = dev->si_drv1;
206 
207 	devfs_get_cdevpriv((void**)&irqs);
208 
209 	if (events & (POLLIN | POLLRDNORM)) {
210 		if (sc->tstamps.ctl.cnt != irqs->cnt)
211 			return events & (POLLIN | POLLRDNORM);
212 		else
213 			selrecord(td, &sc->sc_selinfo);
214 	}
215 	return 0;
216 }
217 
218 static int
219 ti_pruss_irq_read(struct cdev *cdev, struct uio *uio, int ioflag)
220 {
221 	const size_t ts_len = sizeof(uint64_t);
222 	struct ti_pruss_irqsc* irq;
223 	struct ctl* priv;
224 	int error = 0;
225 	size_t idx;
226 	ssize_t level;
227 
228 	irq = cdev->si_drv1;
229 
230 	if (uio->uio_resid < ts_len)
231 		return (EINVAL);
232 
233 	error = devfs_get_cdevpriv((void**)&priv);
234 	if (error)
235 	    return (error);
236 
237 	mtx_lock(&irq->sc_mtx);
238 
239 	if (irq->tstamps.ctl.cnt - priv->cnt > TI_TS_ARRAY)
240 	{
241 		priv->cnt = irq->tstamps.ctl.cnt;
242 		priv->idx = irq->tstamps.ctl.idx;
243 		mtx_unlock(&irq->sc_mtx);
244 		return (ENXIO);
245 	}
246 
247 	do {
248 		idx = priv->idx;
249 		level = irq->tstamps.ctl.idx - idx;
250 		if (level < 0)
251 			level += TI_TS_ARRAY;
252 
253 		if (level == 0) {
254 			if (ioflag & O_NONBLOCK) {
255 				mtx_unlock(&irq->sc_mtx);
256 				return (EWOULDBLOCK);
257 			}
258 
259 			error = msleep(irq, &irq->sc_mtx, PCATCH | PDROP,
260 				"pruirq", 0);
261 			if (error)
262 				return error;
263 
264 			mtx_lock(&irq->sc_mtx);
265 		}
266 	}while(level == 0);
267 
268 	mtx_unlock(&irq->sc_mtx);
269 
270 	error = uiomove(&irq->tstamps.ts[idx], ts_len, uio);
271 
272 	if (++idx == TI_TS_ARRAY)
273 		idx = 0;
274 	priv->idx = idx;
275 
276 	atomic_add_32(&priv->cnt, 1);
277 
278 	return (error);
279 }
280 
281 static struct ti_pruss_irq_arg {
282 	int 		       irq;
283 	struct ti_pruss_softc *sc;
284 } ti_pruss_irq_args[TI_PRUSS_IRQS];
285 
286 static __inline uint32_t
287 ti_pruss_reg_read(struct ti_pruss_softc *sc, uint32_t reg)
288 {
289 	return (bus_space_read_4(sc->sc_bt, sc->sc_bh, reg));
290 }
291 
292 static __inline void
293 ti_pruss_reg_write(struct ti_pruss_softc *sc, uint32_t reg, uint32_t val)
294 {
295 	bus_space_write_4(sc->sc_bt, sc->sc_bh, reg, val);
296 }
297 
298 static __inline void
299 ti_pruss_interrupts_clear(struct ti_pruss_softc *sc)
300 {
301 	/* disable global interrupt */
302 	ti_pruss_reg_write(sc, PRUSS_INTC_GER, 0 );
303 
304 	/* clear all events */
305 	ti_pruss_reg_write(sc, PRUSS_INTC_SECR0, 0xFFFFFFFF);
306 	ti_pruss_reg_write(sc, PRUSS_INTC_SECR1, 0xFFFFFFFF);
307 
308 	/* disable all host interrupts */
309 	ti_pruss_reg_write(sc, PRUSS_INTC_HIER, 0);
310 }
311 
312 static __inline int
313 ti_pruss_interrupts_enable(struct ti_pruss_softc *sc, int8_t irq, bool enable)
314 {
315 	if (enable && ((sc->sc_irq_devs[irq].channel == -1) ||
316 	    (sc->sc_irq_devs[irq].event== -1)))
317 	{
318 		device_printf( sc->sc_pdev->si_drv1,
319 			"Interrupt chain not fully configured, not possible to enable\n" );
320 		return (EINVAL);
321 	}
322 
323 	sc->sc_irq_devs[irq].enable = enable;
324 
325 	if (sc->sc_irq_devs[irq].sc_pdev) {
326 		destroy_dev(sc->sc_irq_devs[irq].sc_pdev);
327 		sc->sc_irq_devs[irq].sc_pdev = NULL;
328 	}
329 
330 	if (enable) {
331 		sc->sc_irq_devs[irq].sc_pdev = make_dev(&ti_pruss_cdevirq, 0, UID_ROOT, GID_WHEEL,
332 		    0600, "pruss%d.irq%d", device_get_unit(sc->sc_pdev->si_drv1), irq);
333 		sc->sc_irq_devs[irq].sc_pdev->si_drv1 = &sc->sc_irq_devs[irq];
334 
335 		sc->sc_irq_devs[irq].tstamps.ctl.idx = 0;
336 	}
337 
338 	uint32_t reg = enable ? PRUSS_INTC_HIEISR : PRUSS_INTC_HIDISR;
339 	ti_pruss_reg_write(sc, reg, sc->sc_irq_devs[irq].channel);
340 
341 	reg = enable ? PRUSS_INTC_EISR : PRUSS_INTC_EICR;
342 	ti_pruss_reg_write(sc, reg, sc->sc_irq_devs[irq].event );
343 
344 	return (0);
345 }
346 
347 static __inline void
348 ti_pruss_map_write(struct ti_pruss_softc *sc, uint32_t basereg, uint8_t index, uint8_t content)
349 {
350 	const size_t regadr = basereg + index & ~0x03;
351 	const size_t bitpos = (index & 0x03) * 8;
352 	uint32_t rmw = ti_pruss_reg_read(sc, regadr);
353 	rmw = (rmw & ~( 0xF << bitpos)) | ( (content & 0xF) << bitpos);
354 	ti_pruss_reg_write(sc, regadr, rmw);
355 }
356 
357 static int
358 ti_pruss_event_map( SYSCTL_HANDLER_ARGS )
359 {
360 	struct ti_pruss_softc *sc;
361 	const int8_t irq = arg2;
362 	int err;
363 	char event[sizeof(NOT_SET_STR)];
364 
365 	sc = arg1;
366 
367 	if(sc->sc_irq_devs[irq].event == -1)
368 		bcopy(NOT_SET_STR, event, sizeof(event));
369 	else
370 		snprintf(event, sizeof(event), "%d", sc->sc_irq_devs[irq].event);
371 
372 	err = sysctl_handle_string(oidp, event, sizeof(event), req);
373 	if(err != 0)
374 		return (err);
375 
376 	if (req->newptr) {  // write event
377 		if (strcmp(NOT_SET_STR, event) == 0) {
378 			ti_pruss_interrupts_enable(sc, irq, false);
379 			sc->sc_irq_devs[irq].event = -1;
380 		} else {
381 			if (sc->sc_irq_devs[irq].channel == -1) {
382 				device_printf( sc->sc_pdev->si_drv1,
383 					"corresponding channel not configured\n");
384 				return (ENXIO);
385 			}
386 
387 			const int8_t channelnr = sc->sc_irq_devs[irq].channel;
388 			const int8_t eventnr = strtol( event, NULL, 10 ); // TODO: check if strol is valid
389 			if (eventnr > TI_PRUSS_EVENTS || eventnr < 0) {
390 				device_printf( sc->sc_pdev->si_drv1,
391 					"Event number %d not valid (0 - %d)",
392 					channelnr, TI_PRUSS_EVENTS -1);
393 				return (EINVAL);
394 			}
395 
396 			sc->sc_irq_devs[irq].channel = channelnr;
397 			sc->sc_irq_devs[irq].event = eventnr;
398 
399 			// event[nr] <= channel
400 			ti_pruss_map_write(sc, PRUSS_INTC_CMR_BASE,
401 			    eventnr, channelnr);
402 		}
403 	}
404 	return (err);
405 }
406 
407 static int
408 ti_pruss_channel_map(SYSCTL_HANDLER_ARGS)
409 {
410 	struct ti_pruss_softc *sc;
411 	int err;
412 	char channel[sizeof(NOT_SET_STR)];
413 	const int8_t irq = arg2;
414 
415 	sc = arg1;
416 
417 	if (sc->sc_irq_devs[irq].channel == -1)
418 		bcopy(NOT_SET_STR, channel, sizeof(channel));
419 	else
420 		snprintf(channel, sizeof(channel), "%d", sc->sc_irq_devs[irq].channel);
421 
422 	err = sysctl_handle_string(oidp, channel, sizeof(channel), req);
423 	if (err != 0)
424 		return (err);
425 
426 	if (req->newptr) { // write event
427 		if (strcmp(NOT_SET_STR, channel) == 0) {
428 			ti_pruss_interrupts_enable(sc, irq, false);
429 			ti_pruss_reg_write(sc, PRUSS_INTC_HIDISR,
430 			    sc->sc_irq_devs[irq].channel);
431 			sc->sc_irq_devs[irq].channel = -1;
432 		} else {
433 			const int8_t channelnr = strtol(channel, NULL, 10); // TODO: check if strol is valid
434 			if (channelnr > TI_PRUSS_IRQS || channelnr < 0)
435 			{
436 				device_printf(sc->sc_pdev->si_drv1,
437 					"Channel number %d not valid (0 - %d)",
438 					channelnr, TI_PRUSS_IRQS-1);
439 				return (EINVAL);
440 			}
441 
442 			sc->sc_irq_devs[irq].channel = channelnr;
443 			sc->sc_irq_devs[irq].last = -1;
444 
445 			// channel[nr] <= irqnr
446 			ti_pruss_map_write(sc, PRUSS_INTC_HMR_BASE,
447 				irq, channelnr);
448 		}
449 	}
450 
451 	return (err);
452 }
453 
454 static int
455 ti_pruss_interrupt_enable(SYSCTL_HANDLER_ARGS)
456 {
457 	struct ti_pruss_softc *sc;
458 	int err;
459 	bool irqenable;
460 	const int8_t irq = arg2;
461 
462 	sc = arg1;
463 	irqenable = sc->sc_irq_devs[arg2].enable;
464 
465 	err = sysctl_handle_bool(oidp, &irqenable, arg2, req);
466 	if (err != 0)
467 		return (err);
468 
469 	if (req->newptr) // write enable
470 		return ti_pruss_interrupts_enable(sc, irq, irqenable);
471 
472 	return (err);
473 }
474 
475 static int
476 ti_pruss_global_interrupt_enable(SYSCTL_HANDLER_ARGS)
477 {
478 	struct ti_pruss_softc *sc;
479 	int err;
480 	bool glob_irqen;
481 
482 	sc = arg1;
483 	glob_irqen = sc->sc_glob_irqen;
484 
485 	err = sysctl_handle_bool(oidp, &glob_irqen, arg2, req);
486 	if (err != 0)
487 		return (err);
488 
489 	if (req->newptr) {
490 		sc->sc_glob_irqen = glob_irqen;
491 		ti_pruss_reg_write(sc, PRUSS_INTC_GER, glob_irqen);
492 	}
493 
494 	return (err);
495 }
496 static int
497 ti_pruss_probe(device_t dev)
498 {
499 
500 	if (!ofw_bus_status_okay(dev))
501 		return (ENXIO);
502 
503 	if (ofw_bus_is_compatible(dev, "ti,pruss-v1") ||
504 	    ofw_bus_is_compatible(dev, "ti,pruss-v2")) {
505 		device_set_desc(dev, "TI Programmable Realtime Unit Subsystem");
506 		return (BUS_PROBE_DEFAULT);
507 	}
508 
509 	return (ENXIO);
510 }
511 
512 static int
513 ti_pruss_attach(device_t dev)
514 {
515 	struct ti_pruss_softc *sc;
516 	int rid, i;
517 
518 	if (ti_prcm_clk_enable(PRUSS_CLK) != 0) {
519 		device_printf(dev, "could not enable PRUSS clock\n");
520 		return (ENXIO);
521 	}
522 	sc = device_get_softc(dev);
523 	rid = 0;
524 	mtx_init(&sc->sc_mtx, "TI PRUSS", NULL, MTX_DEF);
525 	sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
526 	    RF_ACTIVE);
527 	if (sc->sc_mem_res == NULL) {
528 		device_printf(dev, "could not allocate memory resource\n");
529 		return (ENXIO);
530 	}
531 
532 	struct sysctl_ctx_list *clist = device_get_sysctl_ctx(dev);
533 	if (!clist)
534 		return (EINVAL);
535 
536 	struct sysctl_oid *poid;
537 	poid = device_get_sysctl_tree( dev );
538 	if (!poid)
539 		return (EINVAL);
540 
541 	sc->sc_glob_irqen = false;
542 	struct sysctl_oid *irq_root = SYSCTL_ADD_NODE(clist, SYSCTL_CHILDREN(poid),
543 	    OID_AUTO, "irq", CTLFLAG_RD, 0,
544 	    "PRUSS Host Interrupts");
545 	SYSCTL_ADD_PROC(clist, SYSCTL_CHILDREN(poid), OID_AUTO,
546 	    "global_interrupt_enable", CTLFLAG_RW | CTLTYPE_U8,
547 	    sc, 0, ti_pruss_global_interrupt_enable,
548 	    "CU", "Global interrupt enable");
549 
550 	sc->sc_bt = rman_get_bustag(sc->sc_mem_res);
551 	sc->sc_bh = rman_get_bushandle(sc->sc_mem_res);
552 	if (bus_alloc_resources(dev, ti_pruss_irq_spec, sc->sc_irq_res) != 0) {
553 		device_printf(dev, "could not allocate interrupt resource\n");
554 		ti_pruss_detach(dev);
555 		return (ENXIO);
556 	}
557 
558 	ti_pruss_interrupts_clear(sc);
559 
560 	for (i = 0; i < TI_PRUSS_IRQS; i++) {
561 		char name[8];
562 		snprintf(name, sizeof(name), "%d", i);
563 
564 		struct sysctl_oid *irq_nodes = SYSCTL_ADD_NODE(clist, SYSCTL_CHILDREN(irq_root),
565 		    OID_AUTO, name, CTLFLAG_RD, 0,
566 		    "PRUSS Interrupts");
567 		SYSCTL_ADD_PROC(clist, SYSCTL_CHILDREN(irq_nodes), OID_AUTO,
568 		    "channel", CTLFLAG_RW | CTLTYPE_STRING, sc, i, ti_pruss_channel_map,
569 		    "A", "Channel attached to this irq");
570 		SYSCTL_ADD_PROC(clist, SYSCTL_CHILDREN(irq_nodes), OID_AUTO,
571 		    "event", CTLFLAG_RW | CTLTYPE_STRING, sc, i, ti_pruss_event_map,
572 		    "A", "Event attached to this irq");
573 		SYSCTL_ADD_PROC(clist, SYSCTL_CHILDREN(irq_nodes), OID_AUTO,
574 		    "enable", CTLFLAG_RW | CTLTYPE_U8, sc, i, ti_pruss_interrupt_enable,
575 		    "CU", "Enable/Disable interrupt");
576 
577 		sc->sc_irq_devs[i].event = -1;
578 		sc->sc_irq_devs[i].channel = -1;
579 		sc->sc_irq_devs[i].tstamps.ctl.idx = 0;
580 
581 		if (i < TI_PRUSS_HOST_IRQS) {
582 			ti_pruss_irq_args[i].irq = i;
583 			ti_pruss_irq_args[i].sc = sc;
584 			if (bus_setup_intr(dev, sc->sc_irq_res[i],
585 			    INTR_MPSAFE | INTR_TYPE_MISC,
586 			    NULL, ti_pruss_intr, &ti_pruss_irq_args[i],
587 			    &sc->sc_intr[i]) != 0) {
588 				device_printf(dev,
589 				    "unable to setup the interrupt handler\n");
590 				ti_pruss_detach(dev);
591 
592 				return (ENXIO);
593 			}
594 			mtx_init(&sc->sc_irq_devs[i].sc_mtx, "TI PRUSS IRQ", NULL, MTX_DEF);
595 			knlist_init_mtx(&sc->sc_irq_devs[i].sc_selinfo.si_note, &sc->sc_irq_devs[i].sc_mtx);
596 		}
597 	}
598 
599 	if (ti_pruss_reg_read(sc, PRUSS_AM33XX_INTC) == PRUSS_AM33XX_REV)
600 		device_printf(dev, "AM33xx PRU-ICSS\n");
601 
602 	sc->sc_pdev = make_dev(&ti_pruss_cdevsw, 0, UID_ROOT, GID_WHEEL,
603 	    0600, "pruss%d", device_get_unit(dev));
604 	sc->sc_pdev->si_drv1 = dev;
605 
606 	/*  Acc. to datasheet always write 1 to polarity registers */
607 	ti_pruss_reg_write(sc, PRUSS_INTC_SIPR0, 0xFFFFFFFF);
608 	ti_pruss_reg_write(sc, PRUSS_INTC_SIPR1, 0xFFFFFFFF);
609 
610 	/* Acc. to datasheet always write 0 to event type registers */
611 	ti_pruss_reg_write(sc, PRUSS_INTC_SITR0, 0);
612 	ti_pruss_reg_write(sc, PRUSS_INTC_SITR1, 0);
613 
614 	return (0);
615 }
616 
617 static int
618 ti_pruss_detach(device_t dev)
619 {
620 	struct ti_pruss_softc *sc = device_get_softc(dev);
621 
622 	ti_pruss_interrupts_clear(sc);
623 
624 	for (int i = 0; i < TI_PRUSS_HOST_IRQS; i++) {
625 		ti_pruss_interrupts_enable( sc, i, false );
626 
627 		if (sc->sc_intr[i])
628 			bus_teardown_intr(dev, sc->sc_irq_res[i], sc->sc_intr[i]);
629 		if (sc->sc_irq_res[i])
630 			bus_release_resource(dev, SYS_RES_IRQ,
631 			    rman_get_rid(sc->sc_irq_res[i]),
632 			    sc->sc_irq_res[i]);
633 		knlist_clear(&sc->sc_irq_devs[i].sc_selinfo.si_note, 0);
634 		mtx_lock(&sc->sc_irq_devs[i].sc_mtx);
635 		if (!knlist_empty(&sc->sc_irq_devs[i].sc_selinfo.si_note))
636 			printf("IRQ %d KQueue not empty!\n", i );
637 		mtx_unlock(&sc->sc_irq_devs[i].sc_mtx);
638 		knlist_destroy(&sc->sc_irq_devs[i].sc_selinfo.si_note);
639 		mtx_destroy(&sc->sc_irq_devs[i].sc_mtx);
640 	}
641 
642 	mtx_destroy(&sc->sc_mtx);
643 	if (sc->sc_mem_res)
644 		bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(sc->sc_mem_res),
645 		    sc->sc_mem_res);
646 	if (sc->sc_pdev)
647 		destroy_dev(sc->sc_pdev);
648 
649 	return (0);
650 }
651 
652 static void
653 ti_pruss_intr(void *arg)
654 {
655 	int val;
656 	struct ti_pruss_irq_arg *iap = arg;
657 	struct ti_pruss_softc *sc = iap->sc;
658 	/*
659 	 * Interrupts pr1_host_intr[0:7] are mapped to
660 	 * Host-2 to Host-9 of PRU-ICSS IRQ-controller.
661 	 */
662 	const int pru_int = iap->irq + TI_PRUSS_PRU_IRQS;
663 	const int pru_int_mask = (1 << pru_int);
664 	const int pru_channel = sc->sc_irq_devs[pru_int].channel;
665 	const int pru_event = sc->sc_irq_devs[pru_channel].event;
666 
667 	val = ti_pruss_reg_read(sc, PRUSS_INTC_HIER);
668 	if (!(val & pru_int_mask))
669 		return;
670 
671 	ti_pruss_reg_write(sc, PRUSS_INTC_HIDISR, pru_int);
672 	ti_pruss_reg_write(sc, PRUSS_INTC_SICR, pru_event);
673 	ti_pruss_reg_write(sc, PRUSS_INTC_HIEISR, pru_int);
674 
675 	struct ti_pruss_irqsc* irq = &sc->sc_irq_devs[pru_channel];
676 	size_t wr = irq->tstamps.ctl.idx;
677 
678 	struct timespec ts;
679 	nanouptime(&ts);
680 	irq->tstamps.ts[wr] = ts.tv_sec * 1000000000 + ts.tv_nsec;
681 
682 	if (++wr == TI_TS_ARRAY)
683 		wr = 0;
684 	atomic_add_32(&irq->tstamps.ctl.cnt, 1);
685 
686 	irq->tstamps.ctl.idx = wr;
687 
688 	KNOTE_UNLOCKED(&irq->sc_selinfo.si_note, pru_int);
689 	wakeup(irq);
690 	selwakeup(&irq->sc_selinfo);
691 }
692 
693 static int
694 ti_pruss_open(struct cdev *cdev __unused, int oflags __unused,
695     int devtype __unused, struct thread *td __unused)
696 {
697 	return (0);
698 }
699 
700 static int
701 ti_pruss_mmap(struct cdev *cdev, vm_ooffset_t offset, vm_paddr_t *paddr,
702     int nprot, vm_memattr_t *memattr)
703 {
704 	device_t dev = cdev->si_drv1;
705 	struct ti_pruss_softc *sc = device_get_softc(dev);
706 
707 	if (offset > rman_get_size(sc->sc_mem_res))
708 		return (ENOSPC);
709 	*paddr = rman_get_start(sc->sc_mem_res) + offset;
710 	*memattr = VM_MEMATTR_UNCACHEABLE;
711 
712 	return (0);
713 }
714 
715 static struct filterops ti_pruss_kq_read = {
716 	.f_isfd = 1,
717 	.f_detach = ti_pruss_irq_kqread_detach,
718 	.f_event = ti_pruss_irq_kqevent,
719 };
720 
721 static void
722 ti_pruss_irq_kqread_detach(struct knote *kn)
723 {
724 	struct ti_pruss_irqsc *sc = kn->kn_hook;
725 
726 	knlist_remove(&sc->sc_selinfo.si_note, kn, 0);
727 }
728 
729 static int
730 ti_pruss_irq_kqevent(struct knote *kn, long hint)
731 {
732     struct ti_pruss_irqsc* irq_sc;
733     int notify;
734 
735     irq_sc = kn->kn_hook;
736 
737     if (hint > 0)
738         kn->kn_data = hint - 2;
739 
740     if (hint > 0 || irq_sc->last > 0)
741         notify = 1;
742     else
743         notify = 0;
744 
745     irq_sc->last = hint;
746 
747     return (notify);
748 }
749 
750 static int
751 ti_pruss_irq_kqfilter(struct cdev *cdev, struct knote *kn)
752 {
753 	struct ti_pruss_irqsc *sc = cdev->si_drv1;
754 
755 	switch (kn->kn_filter) {
756 	case EVFILT_READ:
757 		kn->kn_hook = sc;
758 		kn->kn_fop = &ti_pruss_kq_read;
759 		knlist_add(&sc->sc_selinfo.si_note, kn, 0);
760 		break;
761 	default:
762 		return (EINVAL);
763 	}
764 
765 	return (0);
766 }
767