xref: /netbsd-src/sys/arch/arm/sunxi/sunxi_nmi.c (revision 2c1322c5dded5ddb027a866df87ec728fd83acf7)
1*2c1322c5Sjmcneill /* $NetBSD: sunxi_nmi.c,v 1.12 2021/11/07 17:13:38 jmcneill Exp $ */
2bf222c0dSjmcneill 
3bf222c0dSjmcneill /*-
4bf222c0dSjmcneill  * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca>
5bf222c0dSjmcneill  * All rights reserved.
6bf222c0dSjmcneill  *
7bf222c0dSjmcneill  * Redistribution and use in source and binary forms, with or without
8bf222c0dSjmcneill  * modification, are permitted provided that the following conditions
9bf222c0dSjmcneill  * are met:
10bf222c0dSjmcneill  * 1. Redistributions of source code must retain the above copyright
11bf222c0dSjmcneill  *    notice, this list of conditions and the following disclaimer.
12bf222c0dSjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
13bf222c0dSjmcneill  *    notice, this list of conditions and the following disclaimer in the
14bf222c0dSjmcneill  *    documentation and/or other materials provided with the distribution.
15bf222c0dSjmcneill  *
16bf222c0dSjmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17bf222c0dSjmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18bf222c0dSjmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19bf222c0dSjmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20bf222c0dSjmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21bf222c0dSjmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22bf222c0dSjmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23bf222c0dSjmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24bf222c0dSjmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25bf222c0dSjmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26bf222c0dSjmcneill  * SUCH DAMAGE.
27bf222c0dSjmcneill  */
28bf222c0dSjmcneill 
29bf222c0dSjmcneill #define	_INTR_PRIVATE
30bf222c0dSjmcneill 
31bf222c0dSjmcneill #include <sys/cdefs.h>
32*2c1322c5Sjmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_nmi.c,v 1.12 2021/11/07 17:13:38 jmcneill Exp $");
33bf222c0dSjmcneill 
34bf222c0dSjmcneill #include <sys/param.h>
35bf222c0dSjmcneill #include <sys/bus.h>
36bf222c0dSjmcneill #include <sys/device.h>
37bf222c0dSjmcneill #include <sys/intr.h>
38f8a0ef89Sskrll #include <sys/kernel.h>
39bf222c0dSjmcneill #include <sys/systm.h>
40420676e4Sthorpej #include <sys/atomic.h>
41420676e4Sthorpej #include <sys/mutex.h>
4258bc9390Sskrll #include <sys/lwp.h>
43bf222c0dSjmcneill 
44bf222c0dSjmcneill #include <dev/fdt/fdtvar.h>
45bf222c0dSjmcneill 
46bf222c0dSjmcneill #include <arm/cpu.h>
47bf222c0dSjmcneill #include <arm/pic/picvar.h>
48bf222c0dSjmcneill #include <arm/fdt/arm_fdtvar.h>
49bf222c0dSjmcneill 
50bf222c0dSjmcneill /* ctrl_reg */
51bf222c0dSjmcneill #define	NMI_CTRL_IRQ_LOW_LEVEL	0
52bf222c0dSjmcneill #define	NMI_CTRL_IRQ_LOW_EDGE	1
53bf222c0dSjmcneill #define	NMI_CTRL_IRQ_HIGH_LEVEL	2
54bf222c0dSjmcneill #define	NMI_CTRL_IRQ_HIGH_EDGE	3
55bf222c0dSjmcneill #define	NMI_CTRL_IRQ_TYPE	__BITS(1,0)
56bf222c0dSjmcneill 
57bf222c0dSjmcneill /* pend_reg */
58bf222c0dSjmcneill #define	NMI_PEND_IRQ_ACK	__BIT(0)
59bf222c0dSjmcneill 
60bf222c0dSjmcneill /* enable_reg */
61bf222c0dSjmcneill #define	NMI_ENABLE_IRQEN	__BIT(0)
62bf222c0dSjmcneill 
63bf222c0dSjmcneill struct sunxi_nmi_config {
64bf222c0dSjmcneill 	const char *	name;
65bf222c0dSjmcneill 	bus_size_t	ctrl_reg;
66bf222c0dSjmcneill 	bus_size_t	pend_reg;
67bf222c0dSjmcneill 	bus_size_t	enable_reg;
68bf222c0dSjmcneill };
69bf222c0dSjmcneill 
70bf222c0dSjmcneill static const struct sunxi_nmi_config sun7i_a20_sc_nmi_config = {
71bf222c0dSjmcneill 	.name = "NMI",
72bf222c0dSjmcneill 	.ctrl_reg = 0x00,
73bf222c0dSjmcneill 	.pend_reg = 0x04,
74bf222c0dSjmcneill 	.enable_reg = 0x08,
75bf222c0dSjmcneill };
76bf222c0dSjmcneill 
77bf222c0dSjmcneill static const struct sunxi_nmi_config sun6i_a31_r_intc_config = {
78bf222c0dSjmcneill 	.name = "R_INTC",
79bf222c0dSjmcneill 	.ctrl_reg = 0x0c,
80bf222c0dSjmcneill 	.pend_reg = 0x10,
81bf222c0dSjmcneill 	.enable_reg = 0x40,
82bf222c0dSjmcneill };
83bf222c0dSjmcneill 
84ee5df97dSjmcneill static const struct sunxi_nmi_config sun9i_a80_nmi_config = {
85ee5df97dSjmcneill 	.name = "NMI",
86ee5df97dSjmcneill 	.ctrl_reg = 0x00,
87ee5df97dSjmcneill 	.pend_reg = 0x04,
88ee5df97dSjmcneill 	.enable_reg = 0x08,
89ee5df97dSjmcneill };
90ee5df97dSjmcneill 
91646c0f59Sthorpej static const struct device_compatible_entry compat_data[] = {
92646c0f59Sthorpej 	{ .compat = "allwinner,sun7i-a20-sc-nmi",
93646c0f59Sthorpej 	  .data = &sun7i_a20_sc_nmi_config },
94646c0f59Sthorpej 	{ .compat = "allwinner,sun6i-a31-r-intc",
95646c0f59Sthorpej 	  .data = &sun6i_a31_r_intc_config },
96646c0f59Sthorpej 	{ .compat = "allwinner,sun9i-a80-nmi",
97646c0f59Sthorpej 	  .data = &sun9i_a80_nmi_config },
98646c0f59Sthorpej 
99ec189949Sthorpej 	DEVICE_COMPAT_EOL
100bf222c0dSjmcneill };
101bf222c0dSjmcneill 
102bf222c0dSjmcneill struct sunxi_nmi_softc {
103bf222c0dSjmcneill 	device_t sc_dev;
104bf222c0dSjmcneill 	bus_space_tag_t sc_bst;
105bf222c0dSjmcneill 	bus_space_handle_t sc_bsh;
106bf222c0dSjmcneill 	int sc_phandle;
107bf222c0dSjmcneill 
108*2c1322c5Sjmcneill 	u_int sc_intr_nmi;
109*2c1322c5Sjmcneill 	u_int sc_intr_cells;
110*2c1322c5Sjmcneill 
111420676e4Sthorpej 	kmutex_t sc_intr_lock;
112420676e4Sthorpej 
113bf222c0dSjmcneill 	const struct sunxi_nmi_config *sc_config;
114bf222c0dSjmcneill 
115420676e4Sthorpej 	struct intrsource sc_is;
116420676e4Sthorpej 	void	*sc_ih;
117bf222c0dSjmcneill };
118bf222c0dSjmcneill 
119bf222c0dSjmcneill #define NMI_READ(sc, reg) \
120bf222c0dSjmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
121bf222c0dSjmcneill #define NMI_WRITE(sc, reg, val) \
122bf222c0dSjmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
123bf222c0dSjmcneill 
124bf222c0dSjmcneill static void
sunxi_nmi_irq_ack(struct sunxi_nmi_softc * sc)125bf222c0dSjmcneill sunxi_nmi_irq_ack(struct sunxi_nmi_softc *sc)
126bf222c0dSjmcneill {
127bf222c0dSjmcneill 	uint32_t val;
128bf222c0dSjmcneill 
129bf222c0dSjmcneill 	val = NMI_READ(sc, sc->sc_config->pend_reg);
130bf222c0dSjmcneill 	val |= NMI_PEND_IRQ_ACK;
131bf222c0dSjmcneill 	NMI_WRITE(sc, sc->sc_config->pend_reg, val);
132bf222c0dSjmcneill }
133bf222c0dSjmcneill 
134bf222c0dSjmcneill static void
sunxi_nmi_irq_enable(struct sunxi_nmi_softc * sc,bool on)135bf222c0dSjmcneill sunxi_nmi_irq_enable(struct sunxi_nmi_softc *sc, bool on)
136bf222c0dSjmcneill {
137bf222c0dSjmcneill 	uint32_t val;
138bf222c0dSjmcneill 
139bf222c0dSjmcneill 	val = NMI_READ(sc, sc->sc_config->enable_reg);
140bf222c0dSjmcneill 	if (on)
141bf222c0dSjmcneill 		val |= NMI_ENABLE_IRQEN;
142bf222c0dSjmcneill 	else
143bf222c0dSjmcneill 		val &= ~NMI_ENABLE_IRQEN;
144bf222c0dSjmcneill 	NMI_WRITE(sc, sc->sc_config->enable_reg, val);
145bf222c0dSjmcneill }
146bf222c0dSjmcneill 
147bf222c0dSjmcneill static void
sunxi_nmi_irq_set_type(struct sunxi_nmi_softc * sc,u_int irq_type)148bf222c0dSjmcneill sunxi_nmi_irq_set_type(struct sunxi_nmi_softc *sc, u_int irq_type)
149bf222c0dSjmcneill {
150bf222c0dSjmcneill 	uint32_t val;
151bf222c0dSjmcneill 
152bf222c0dSjmcneill 	val = NMI_READ(sc, sc->sc_config->ctrl_reg);
153bf222c0dSjmcneill 	val &= ~NMI_CTRL_IRQ_TYPE;
154bf222c0dSjmcneill 	val |= __SHIFTIN(irq_type, NMI_CTRL_IRQ_TYPE);
155bf222c0dSjmcneill 	NMI_WRITE(sc, sc->sc_config->ctrl_reg, val);
156bf222c0dSjmcneill }
157bf222c0dSjmcneill 
158bf222c0dSjmcneill static int
sunxi_nmi_intr(void * priv)159bf222c0dSjmcneill sunxi_nmi_intr(void *priv)
160bf222c0dSjmcneill {
161bf222c0dSjmcneill 	struct sunxi_nmi_softc * const sc = priv;
162420676e4Sthorpej 	int (*func)(void *);
163bf222c0dSjmcneill 	int rv = 0;
164bf222c0dSjmcneill 
165420676e4Sthorpej 	func = atomic_load_acquire(&sc->sc_is.is_func);
166420676e4Sthorpej 	if (func)
167420676e4Sthorpej 		rv = func(sc->sc_is.is_arg);
168bf222c0dSjmcneill 
169420676e4Sthorpej 	/*
170420676e4Sthorpej 	 * We don't serialize access to this register because we're the
171420676e4Sthorpej 	 * only thing fiddling wth it.
172420676e4Sthorpej 	 */
173bf222c0dSjmcneill 	sunxi_nmi_irq_ack(sc);
174bf222c0dSjmcneill 
175bf222c0dSjmcneill 	return rv;
176bf222c0dSjmcneill }
177bf222c0dSjmcneill 
178bf222c0dSjmcneill static void *
sunxi_nmi_fdt_establish(device_t dev,u_int * specifier,int ipl,int flags,int (* func)(void *),void * arg,const char * xname)179bf222c0dSjmcneill sunxi_nmi_fdt_establish(device_t dev, u_int *specifier, int ipl, int flags,
18059ad346dSjmcneill     int (*func)(void *), void *arg, const char *xname)
181bf222c0dSjmcneill {
182bf222c0dSjmcneill 	struct sunxi_nmi_softc * const sc = device_private(dev);
183*2c1322c5Sjmcneill 	u_int irq_type, irq, pol;
184420676e4Sthorpej 	int ist;
185bf222c0dSjmcneill 
186*2c1322c5Sjmcneill 	if (sc->sc_intr_cells == 2) {
187bf222c0dSjmcneill 		/* 1st cell is the interrupt number */
188*2c1322c5Sjmcneill 		irq = be32toh(specifier[0]);
189bf222c0dSjmcneill 		/* 2nd cell is polarity */
190*2c1322c5Sjmcneill 		pol = be32toh(specifier[1]);
191*2c1322c5Sjmcneill 	} else {
192*2c1322c5Sjmcneill 		/* 1st cell is the GIC interrupt type and must be GIC_SPI */
193*2c1322c5Sjmcneill 		if (be32toh(specifier[0]) != 0) {
194*2c1322c5Sjmcneill #ifdef DIAGNOSTIC
195*2c1322c5Sjmcneill 			device_printf(dev, "GIC intr type %u is invalid\n",
196*2c1322c5Sjmcneill 			    be32toh(specifier[0]));
197*2c1322c5Sjmcneill #endif
198*2c1322c5Sjmcneill 			return NULL;
199*2c1322c5Sjmcneill 		}
200*2c1322c5Sjmcneill 		/* 2nd cell is the interrupt number */
201*2c1322c5Sjmcneill 		irq = be32toh(specifier[1]);
202*2c1322c5Sjmcneill 		/* 3rd cell is polarity */
203*2c1322c5Sjmcneill 		pol = be32toh(specifier[2]);
204*2c1322c5Sjmcneill 	}
205bf222c0dSjmcneill 
206*2c1322c5Sjmcneill 	if (sc->sc_intr_cells == 3 && irq != sc->sc_intr_nmi) {
207*2c1322c5Sjmcneill 		/*
208*2c1322c5Sjmcneill 		 * Driver is requesting a wakeup irq, which we don't
209*2c1322c5Sjmcneill 		 * support today. Just pass it through to the parent
210*2c1322c5Sjmcneill 		 * interrupt controller.
211*2c1322c5Sjmcneill 		 */
212*2c1322c5Sjmcneill 		const int ihandle = fdtbus_intr_parent(sc->sc_phandle);
213*2c1322c5Sjmcneill 		if (ihandle == -1) {
214*2c1322c5Sjmcneill #ifdef DIAGNOSTIC
215*2c1322c5Sjmcneill 			device_printf(dev, "couldn't find interrupt parent\n");
216*2c1322c5Sjmcneill #endif
217*2c1322c5Sjmcneill 			return NULL;
218*2c1322c5Sjmcneill 		}
219*2c1322c5Sjmcneill 		return fdtbus_intr_establish_raw(ihandle, specifier, ipl,
220*2c1322c5Sjmcneill 		    flags, func, arg, xname);
221*2c1322c5Sjmcneill 	}
222*2c1322c5Sjmcneill 
223*2c1322c5Sjmcneill 	if (sc->sc_intr_cells == 2 && irq != 0) {
224bf222c0dSjmcneill #ifdef DIAGNOSTIC
225bf222c0dSjmcneill 		device_printf(dev, "IRQ %u is invalid\n", irq);
226bf222c0dSjmcneill #endif
227bf222c0dSjmcneill 		return NULL;
228bf222c0dSjmcneill 	}
229bf222c0dSjmcneill 
230*2c1322c5Sjmcneill 	switch (pol & 0xf) {
231bf222c0dSjmcneill 	case 1:	/* IRQ_TYPE_EDGE_RISING */
232bf222c0dSjmcneill 		irq_type = NMI_CTRL_IRQ_HIGH_EDGE;
233420676e4Sthorpej 		ist = IST_EDGE;
234bf222c0dSjmcneill 		break;
235bf222c0dSjmcneill 	case 2:	/* IRQ_TYPE_EDGE_FALLING */
236bf222c0dSjmcneill 		irq_type = NMI_CTRL_IRQ_LOW_EDGE;
237420676e4Sthorpej 		ist = IST_EDGE;
238bf222c0dSjmcneill 		break;
239*2c1322c5Sjmcneill 	case 4:	/* IRQ_TYPE_LEVEL_HIGH */
240bf222c0dSjmcneill 		irq_type = NMI_CTRL_IRQ_HIGH_LEVEL;
241420676e4Sthorpej 		ist = IST_LEVEL;
242bf222c0dSjmcneill 		break;
243*2c1322c5Sjmcneill 	case 8:	/* IRQ_TYPE_LEVEL_LOW */
244bf222c0dSjmcneill 		irq_type = NMI_CTRL_IRQ_LOW_LEVEL;
245420676e4Sthorpej 		ist = IST_LEVEL;
246bf222c0dSjmcneill 		break;
247bf222c0dSjmcneill 	default:
248bf222c0dSjmcneill 		irq_type = NMI_CTRL_IRQ_LOW_LEVEL;
249420676e4Sthorpej 		ist = IST_LEVEL;
250bf222c0dSjmcneill 		break;
251bf222c0dSjmcneill 	}
252bf222c0dSjmcneill 
253420676e4Sthorpej 	mutex_enter(&sc->sc_intr_lock);
254bf222c0dSjmcneill 
255420676e4Sthorpej 	if (atomic_load_relaxed(&sc->sc_is.is_func) != NULL) {
256420676e4Sthorpej 		mutex_exit(&sc->sc_intr_lock);
257420676e4Sthorpej #ifdef DIAGNOSTIC
258420676e4Sthorpej 		device_printf(dev, "%s in use\n", sc->sc_config->name);
259420676e4Sthorpej #endif
260420676e4Sthorpej 		return NULL;
261420676e4Sthorpej 	}
262420676e4Sthorpej 
263420676e4Sthorpej 	sc->sc_is.is_arg = arg;
264420676e4Sthorpej 	atomic_store_release(&sc->sc_is.is_func, func);
265420676e4Sthorpej 
266420676e4Sthorpej 	sc->sc_is.is_type = ist;
267420676e4Sthorpej 	sc->sc_is.is_ipl = ipl;
268420676e4Sthorpej 	sc->sc_is.is_mpsafe = (flags & FDT_INTR_MPSAFE) ? true : false;
269420676e4Sthorpej 
270420676e4Sthorpej 	mutex_exit(&sc->sc_intr_lock);
271420676e4Sthorpej 
272076a1169Sjmcneill 	sc->sc_ih = fdtbus_intr_establish_xname(sc->sc_phandle, 0, ipl, flags,
273076a1169Sjmcneill 	    sunxi_nmi_intr, sc, device_xname(dev));
274420676e4Sthorpej 
275420676e4Sthorpej 	mutex_enter(&sc->sc_intr_lock);
276bf222c0dSjmcneill 	sunxi_nmi_irq_set_type(sc, irq_type);
277bf222c0dSjmcneill 	sunxi_nmi_irq_enable(sc, true);
278420676e4Sthorpej 	mutex_exit(&sc->sc_intr_lock);
279bf222c0dSjmcneill 
280420676e4Sthorpej 	return &sc->sc_is;
281420676e4Sthorpej }
282420676e4Sthorpej 
283420676e4Sthorpej static void
sunxi_nmi_fdt_mask(device_t dev,void * ih __unused)284420676e4Sthorpej sunxi_nmi_fdt_mask(device_t dev, void *ih __unused)
285420676e4Sthorpej {
286420676e4Sthorpej 	struct sunxi_nmi_softc * const sc = device_private(dev);
287420676e4Sthorpej 
288420676e4Sthorpej 	mutex_enter(&sc->sc_intr_lock);
289420676e4Sthorpej 	if (sc->sc_is.is_mask_count++ == 0) {
290420676e4Sthorpej 		sunxi_nmi_irq_enable(sc, false);
291420676e4Sthorpej 	}
292420676e4Sthorpej 	mutex_exit(&sc->sc_intr_lock);
293420676e4Sthorpej }
294420676e4Sthorpej 
295420676e4Sthorpej static void
sunxi_nmi_fdt_unmask(device_t dev,void * ih __unused)296420676e4Sthorpej sunxi_nmi_fdt_unmask(device_t dev, void *ih __unused)
297420676e4Sthorpej {
298420676e4Sthorpej 	struct sunxi_nmi_softc * const sc = device_private(dev);
299420676e4Sthorpej 
300420676e4Sthorpej 	mutex_enter(&sc->sc_intr_lock);
301420676e4Sthorpej 	if (sc->sc_is.is_mask_count-- == 1) {
302420676e4Sthorpej 		sunxi_nmi_irq_enable(sc, true);
303420676e4Sthorpej 	}
304420676e4Sthorpej 	mutex_exit(&sc->sc_intr_lock);
305bf222c0dSjmcneill }
306bf222c0dSjmcneill 
307bf222c0dSjmcneill static void
sunxi_nmi_fdt_disestablish(device_t dev,void * ih)308bf222c0dSjmcneill sunxi_nmi_fdt_disestablish(device_t dev, void *ih)
309bf222c0dSjmcneill {
310bf222c0dSjmcneill 	struct sunxi_nmi_softc * const sc = device_private(dev);
311420676e4Sthorpej 	struct intrsource * const is = ih;
312bf222c0dSjmcneill 
313420676e4Sthorpej 	KASSERT(is == &sc->sc_is);
314420676e4Sthorpej 
315420676e4Sthorpej 	mutex_enter(&sc->sc_intr_lock);
316bf222c0dSjmcneill 	sunxi_nmi_irq_enable(sc, false);
317420676e4Sthorpej 	is->is_mask_count = 0;
318420676e4Sthorpej 	mutex_exit(&sc->sc_intr_lock);
319bf222c0dSjmcneill 
320420676e4Sthorpej 	fdtbus_intr_disestablish(sc->sc_phandle, sc->sc_ih);
321420676e4Sthorpej 	sc->sc_ih = NULL;
322bf222c0dSjmcneill 
323420676e4Sthorpej 	mutex_enter(&sc->sc_intr_lock);
324420676e4Sthorpej 	is->is_arg = NULL;
325420676e4Sthorpej 	is->is_func = NULL;
326420676e4Sthorpej 	mutex_exit(&sc->sc_intr_lock);
327bf222c0dSjmcneill }
328bf222c0dSjmcneill 
329bf222c0dSjmcneill static bool
sunxi_nmi_fdt_intrstr(device_t dev,u_int * specifier,char * buf,size_t buflen)330bf222c0dSjmcneill sunxi_nmi_fdt_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
331bf222c0dSjmcneill {
332bf222c0dSjmcneill 	struct sunxi_nmi_softc * const sc = device_private(dev);
333bf222c0dSjmcneill 
334*2c1322c5Sjmcneill 	if (sc->sc_intr_cells == 3) {
335*2c1322c5Sjmcneill 		const u_int irq = be32toh(specifier[1]);
336*2c1322c5Sjmcneill 		if (irq != sc->sc_intr_nmi) {
337*2c1322c5Sjmcneill 			const int ihandle = fdtbus_intr_parent(sc->sc_phandle);
338*2c1322c5Sjmcneill 			if (ihandle == -1) {
339*2c1322c5Sjmcneill 				return false;
340*2c1322c5Sjmcneill 			}
341*2c1322c5Sjmcneill 			return fdtbus_intr_str_raw(ihandle, specifier, buf,
342*2c1322c5Sjmcneill 			    buflen);
343*2c1322c5Sjmcneill 		}
344*2c1322c5Sjmcneill 	}
345*2c1322c5Sjmcneill 
346bf222c0dSjmcneill 	snprintf(buf, buflen, "%s", sc->sc_config->name);
347bf222c0dSjmcneill 
348bf222c0dSjmcneill 	return true;
349bf222c0dSjmcneill }
350bf222c0dSjmcneill 
351bf222c0dSjmcneill static const struct fdtbus_interrupt_controller_func sunxi_nmi_fdt_funcs = {
352bf222c0dSjmcneill 	.establish = sunxi_nmi_fdt_establish,
353bf222c0dSjmcneill 	.disestablish = sunxi_nmi_fdt_disestablish,
354bf222c0dSjmcneill 	.intrstr = sunxi_nmi_fdt_intrstr,
355420676e4Sthorpej 	.mask = sunxi_nmi_fdt_mask,
356420676e4Sthorpej 	.unmask = sunxi_nmi_fdt_unmask,
357bf222c0dSjmcneill };
358bf222c0dSjmcneill 
359bf222c0dSjmcneill static int
sunxi_nmi_match(device_t parent,cfdata_t cf,void * aux)360bf222c0dSjmcneill sunxi_nmi_match(device_t parent, cfdata_t cf, void *aux)
361bf222c0dSjmcneill {
362bf222c0dSjmcneill 	struct fdt_attach_args * const faa = aux;
363bf222c0dSjmcneill 
3646e54367aSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
365bf222c0dSjmcneill }
366bf222c0dSjmcneill 
367bf222c0dSjmcneill static void
sunxi_nmi_attach(device_t parent,device_t self,void * aux)368bf222c0dSjmcneill sunxi_nmi_attach(device_t parent, device_t self, void *aux)
369bf222c0dSjmcneill {
370bf222c0dSjmcneill 	struct sunxi_nmi_softc * const sc = device_private(self);
371bf222c0dSjmcneill 	struct fdt_attach_args * const faa = aux;
372bf222c0dSjmcneill 	const int phandle = faa->faa_phandle;
373*2c1322c5Sjmcneill 	const u_int *interrupts;
374bf222c0dSjmcneill 	bus_addr_t addr;
375bf222c0dSjmcneill 	bus_size_t size;
376*2c1322c5Sjmcneill 	int error, len;
377bf222c0dSjmcneill 
378bf222c0dSjmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
379bf222c0dSjmcneill 		aprint_error(": couldn't get registers\n");
380bf222c0dSjmcneill 		return;
381bf222c0dSjmcneill 	}
382bf222c0dSjmcneill 
383bf222c0dSjmcneill 	sc->sc_dev = self;
384bf222c0dSjmcneill 	sc->sc_phandle = phandle;
3856e54367aSthorpej 	sc->sc_config = of_compatible_lookup(phandle, compat_data)->data;
386bf222c0dSjmcneill 	sc->sc_bst = faa->faa_bst;
387bf222c0dSjmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
388bf222c0dSjmcneill 		aprint_error(": couldn't map registers\n");
389bf222c0dSjmcneill 		return;
390bf222c0dSjmcneill 	}
391bf222c0dSjmcneill 
392*2c1322c5Sjmcneill 	of_getprop_uint32(phandle, "#interrupt-cells", &sc->sc_intr_cells);
393*2c1322c5Sjmcneill 	interrupts = fdtbus_get_prop(phandle, "interrupts", &len);
394*2c1322c5Sjmcneill 	if (interrupts == NULL || len != 12 ||
395*2c1322c5Sjmcneill 	    be32toh(interrupts[0]) != 0 /* GIC_SPI */ ||
396*2c1322c5Sjmcneill 	    be32toh(interrupts[2]) != 4 /* IRQ_TYPE_LEVEL_HIGH */) {
397*2c1322c5Sjmcneill 		aprint_error(": couldn't find GIC SPI for NMI\n");
398*2c1322c5Sjmcneill 		return;
399*2c1322c5Sjmcneill 	}
400*2c1322c5Sjmcneill 	sc->sc_intr_nmi = be32toh(interrupts[1]);
401*2c1322c5Sjmcneill 
402bf222c0dSjmcneill 	aprint_naive("\n");
403*2c1322c5Sjmcneill 	aprint_normal(": %s, NMI IRQ %u\n", sc->sc_config->name, sc->sc_intr_nmi);
404bf222c0dSjmcneill 
405420676e4Sthorpej 	mutex_init(&sc->sc_intr_lock, MUTEX_SPIN, IPL_HIGH);
406420676e4Sthorpej 
407420676e4Sthorpej 	/*
408420676e4Sthorpej 	 * Normally it's assumed that an intrsource can be passed to
409420676e4Sthorpej 	 * interrupt_distribute().  We're providing our own that's
410420676e4Sthorpej 	 * independent of our parent PIC, but because we will leave
411420676e4Sthorpej 	 * the intrsource::is_pic field NULL, the right thing
412420676e4Sthorpej 	 * (i.e. nothing) will happen in interrupt_distribute().
413420676e4Sthorpej 	 */
414420676e4Sthorpej 	snprintf(sc->sc_is.is_source, sizeof(sc->sc_is.is_source),
415420676e4Sthorpej 		 "%s", sc->sc_config->name);
416420676e4Sthorpej 
417bf222c0dSjmcneill 	sunxi_nmi_irq_enable(sc, false);
418bf222c0dSjmcneill 	sunxi_nmi_irq_ack(sc);
419bf222c0dSjmcneill 
420bf222c0dSjmcneill 	error = fdtbus_register_interrupt_controller(self, phandle,
421bf222c0dSjmcneill 	    &sunxi_nmi_fdt_funcs);
422bf222c0dSjmcneill 	if (error) {
423bf222c0dSjmcneill 		aprint_error_dev(self, "couldn't register with fdtbus: %d\n",
424bf222c0dSjmcneill 		    error);
425bf222c0dSjmcneill 		return;
426bf222c0dSjmcneill 	}
427bf222c0dSjmcneill }
428bf222c0dSjmcneill 
429bf222c0dSjmcneill CFATTACH_DECL_NEW(sunxi_nmi, sizeof(struct sunxi_nmi_softc),
430bf222c0dSjmcneill 	sunxi_nmi_match, sunxi_nmi_attach, NULL, NULL);
431