1/* $NetBSD: ti_omapwugen.c,v 1.1 2025/12/16 12:20:22 skrll Exp $ */
2
3/*-
4 * Copyright (c) 2025 Rui-Xiang Guo
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, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__KERNEL_RCSID(0, "$NetBSD: ti_omapwugen.c,v 1.1 2025/12/16 12:20:22 skrll Exp $");
31
32#include <sys/param.h>
33#include <sys/bus.h>
34#include <sys/device.h>
35#include <sys/intr.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
38
39#include <dev/fdt/fdtvar.h>
40
41static int omapwugen_match(device_t, cfdata_t, void *);
42static void omapwugen_attach(device_t, device_t, void *);
43
44static const struct device_compatible_entry compat_data[] = {
45	{ .compat = "ti,omap4-wugen-mpu" },
46	DEVICE_COMPAT_EOL
47};
48
49struct omapwugen_softc {
50	device_t sc_dev;
51	int sc_phandle;
52};
53
54static void *
55omapwugen_fdt_establish(device_t dev, u_int *specifier, int ipl, int flags,
56    int (*func)(void *), void *arg, const char *xname)
57{
58	struct omapwugen_softc * const sc = device_private(dev);
59
60	const int ihandle = fdtbus_intr_parent(sc->sc_phandle);
61	if (ihandle == -1) {
62		return NULL;
63	}
64
65	return fdtbus_intr_establish_raw(ihandle, specifier, ipl,
66	    flags, func, arg, xname);
67}
68
69static void
70omapwugen_fdt_disestablish(device_t dev, void *ih)
71{
72	intr_disestablish(ih);
73}
74
75static bool
76omapwugen_fdt_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
77{
78	struct omapwugen_softc * const sc = device_private(dev);
79
80	const int ihandle = fdtbus_intr_parent(sc->sc_phandle);
81	if (ihandle == -1) {
82		return false;
83	}
84
85	return fdtbus_intr_str_raw(ihandle, specifier, buf, buflen);
86}
87
88static const struct fdtbus_interrupt_controller_func omapwugen_fdt_funcs = {
89	.establish = omapwugen_fdt_establish,
90	.disestablish = omapwugen_fdt_disestablish,
91	.intrstr = omapwugen_fdt_intrstr,
92};
93
94static int
95omapwugen_match(device_t parent, cfdata_t cf, void *aux)
96{
97	struct fdt_attach_args * const faa = aux;
98
99	return of_compatible_match(faa->faa_phandle, compat_data);
100}
101
102static void
103omapwugen_attach(device_t parent, device_t self, void *aux)
104{
105	struct omapwugen_softc * const sc = device_private(self);
106	struct fdt_attach_args * const faa = aux;
107	const int phandle = faa->faa_phandle;
108	int error;
109
110	sc->sc_dev = self;
111	sc->sc_phandle = phandle;
112
113	aprint_naive("\n");
114	aprint_normal("\n");
115
116	error = fdtbus_register_interrupt_controller(self, phandle,
117	    &omapwugen_fdt_funcs);
118	if (error) {
119		aprint_error_dev(self, "couldn't register with fdtbus: %d\n",
120		    error);
121		return;
122	}
123}
124
125CFATTACH_DECL_NEW(omapwugen, sizeof(struct omapwugen_softc),
126	omapwugen_match, omapwugen_attach, NULL, NULL);
127